Skip to content

Commit

Permalink
fix _STL configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wbenny committed Dec 20, 2018
1 parent b9a7e4a commit 66c18bd
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 55 deletions.
52 changes: 32 additions & 20 deletions mini-tor.vcxproj

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions mini/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma once
#include <mini/crt/crt0.h>

#include <cstdint>
#include <cstddef>
#include <cstdarg>
Expand Down Expand Up @@ -43,6 +41,10 @@ struct MINI_UNREFERENCED_PARAMETER_PACK_impl
# define MINI_CONFIG_DEBUG
#endif

#if defined(_VC_NODEFAULTLIB)
# define MINI_CONFIG_NO_DEFAULT_LIBS
#endif

#if defined(_M_IX86)
# define MINI_ARCH_X86
# define MINI_ARCH_BITS 32
Expand All @@ -63,6 +65,7 @@ struct MINI_UNREFERENCED_PARAMETER_PACK_impl
# define MINI_MODE_KERNEL
#endif


#define MINI_MAKE_NONCONSTRUCTIBLE(type) \
type( \
void \
Expand Down Expand Up @@ -195,3 +198,6 @@ assert(
);

}

#include <mini/crt/crt0.h>

74 changes: 56 additions & 18 deletions mini/crt/crt0.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _DEBUG
#include "crt0.h"
#if defined(MINI_CONFIG_NO_DEFAULT_LIBS)

#include <cstdlib>

Expand Down Expand Up @@ -127,6 +127,53 @@ _initterm(

int _fltused = 0;

void __cdecl
crt0_initialize(
void
)
{
//
// Call C initializers.
//
_initterm_e(__xi_a, __xi_z);

//
// Call C++ initializers.
//
_initterm(__xc_a, __xc_z);
}

#ifndef MINI_MSVCRT_LIB

static const size_t g_atexit_fn_max_count = 32;
static atexit_fn_t g_atexit_fn_table[g_atexit_fn_max_count];
static size_t g_atexit_fn_count = 0;

void __cdecl
crt0_destroy(
void
)
{
while (g_atexit_fn_count-- > 0)
{
g_atexit_fn_table[g_atexit_fn_count]();
}
}

#else

void __cdecl
crt0_destroy(
void
)
{

}

#endif

#if !defined(MINI_MODE_KERNEL)

int __argc;
char** __argv;

Expand Down Expand Up @@ -167,15 +214,7 @@ mainCRTStartup(
void
)
{
//
// Call C initializers.
//
_initterm_e(__xi_a, __xi_z);

//
// Call C++ initializers.
//
_initterm(__xc_a, __xc_z);
crt0_initialize();

//
// Exit with whatever main will return.
Expand All @@ -187,10 +226,14 @@ mainCRTStartup(
exit(exit_code);
}

#endif

#pragma endregion

#pragma region CRT internal functions

#if !defined(MINI_MODE_KERNEL)

int __cdecl
_purecall(
void
Expand All @@ -199,6 +242,8 @@ _purecall(
return 0;
}

#endif

#pragma endregion

#pragma region CRT standard functions
Expand Down Expand Up @@ -263,10 +308,6 @@ free(

#pragma region Program

static const size_t g_atexit_fn_max_count = 32;
static atexit_fn_t g_atexit_fn_table[g_atexit_fn_max_count];
static size_t g_atexit_fn_count = 0;

[[noreturn]]
void __cdecl
abort(
Expand All @@ -282,10 +323,7 @@ exit(
int exit_code
)
{
while (g_atexit_fn_count-- > 0)
{
g_atexit_fn_table[g_atexit_fn_count]();
}
crt0_destroy();

ExitProcess(exit_code);
}
Expand Down
21 changes: 19 additions & 2 deletions mini/crt/crt0.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifdef _DEBUG
#pragma once
#include <mini/common.h>

#if !defined(MINI_CONFIG_NO_DEFAULT_LIBS)
#include <cstdio>
#include <cstdlib>
#include <cstddef>
#else
#else // defined(MINI_CONFIG_NO_DEFAULT_LIBS)
#include <cstddef>

#define MINI_MSVCRT_LIB
Expand All @@ -11,6 +14,18 @@ extern "C" {

#pragma region CRT startup

void __cdecl
crt0_initialize(
void
);

void __cdecl
crt0_destroy(
void
);

#if !defined(MINI_MODE_KERNEL)

extern int __argc;
extern char** __argv;

Expand All @@ -25,6 +40,8 @@ mainCRTStartup(
void
);

#endif

#pragma endregion

#pragma region CRT internal functions
Expand Down
24 changes: 14 additions & 10 deletions mini/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <cstdlib>
#include <cstring>

#if defined(MINI_MODE_KERNEL)
#include <ntddk.h>
#endif

namespace mini::memory {

namespace detail {
Expand Down Expand Up @@ -64,20 +68,20 @@ free(
struct pool_header
{
size_t size;
byte_type data[];
}
/* byte_type data[] */;
};

void*
allocate(
size_t size
)
{
pool_header* result = reinterpret_cast<pool_header*>(
ExAllocatePoolWithTag(size + sizeof(pool_header), MINI_MEMORY_TAG)
ExAllocatePoolWithTag(NonPagedPool, size + sizeof(pool_header), MINI_MEMORY_TAG)
);

result->size = size;
return result->data;
return &result[1];
}

void*
Expand All @@ -86,7 +90,7 @@ reallocate(
size_t new_size
)
{
pool_header* ptr_header = reinterpret_cast<pool_header*>(ptr)[-1];
pool_header* ptr_header = &reinterpret_cast<pool_header*>(ptr)[-1];

if (ptr_header->size >= new_size)
{
Expand All @@ -105,7 +109,7 @@ free(
void* ptr
)
{
pool_header* result = reinterpret_cast<pool_header*>(ptr)[-1];
pool_header* result = &reinterpret_cast<pool_header*>(ptr)[-1];
ExFreePoolWithTag(result, MINI_MEMORY_TAG);
}

Expand Down Expand Up @@ -188,9 +192,9 @@ equal(

void*
find(
const void *haystack,
const void* haystack,
size_t haystack_size,
const void *needle,
const void* needle,
size_t needle_size
)
{
Expand Down Expand Up @@ -229,9 +233,9 @@ find(

void*
reverse_find(
const void *haystack,
const void* haystack,
size_t haystack_size,
const void *needle,
const void* needle,
size_t needle_size
)
{
Expand Down
4 changes: 4 additions & 0 deletions mini/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ string::from_int(
*this = tmp;
}

#if !defined(MINI_MODE_KERNEL)

int
string::to_int(
void
Expand All @@ -348,6 +350,8 @@ string::to_int(
return string_ref(*this).to_int();
}

#endif

//
// modifiers.
//
Expand Down
4 changes: 4 additions & 0 deletions mini/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,15 @@ class string
int value
);

#if !defined(MINI_MODE_KERNEL)

int
to_int(
void
) const;

#endif

//
// modifiers.
//
Expand Down
4 changes: 4 additions & 0 deletions mini/string_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ string_ref::split(
return tokens;
}

#if !defined(MINI_MODE_KERNEL)

int
string_ref::to_int(
void
Expand All @@ -221,6 +223,8 @@ string_ref::to_int(
return result;
}

#endif

bool
string_ref::is_zero_terminated(
void
Expand Down
4 changes: 4 additions & 0 deletions mini/string_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ class string_ref
size_type count = size_type_max
) const;

#if !defined(MINI_MODE_KERNEL)

int
to_int(
void
) const;

#endif

bool
is_zero_terminated(
void
Expand Down
6 changes: 3 additions & 3 deletions mini/win32/pe/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct image_data_directory_t
uint32_t size;
};

constexpr auto image_numberof_directory_entries = 16;
constexpr auto image_number_of_directory_entries = 16;

//
// Optional header format.
Expand Down Expand Up @@ -215,7 +215,7 @@ struct image_optional_header_32_t
uint32_t size_of_heap_commit;
uint32_t loader_flags;
uint32_t number_of_rva_and_sizes;
image_data_directory_t data_directory[image_numberof_directory_entries];
image_data_directory_t data_directory[image_number_of_directory_entries];
};

struct image_optional_header_64_t
Expand Down Expand Up @@ -249,7 +249,7 @@ struct image_optional_header_64_t
uint64_t size_of_heap_commit;
uint32_t loader_flags;
uint32_t number_of_rva_and_sizes;
image_data_directory_t data_directory[image_numberof_directory_entries];
image_data_directory_t data_directory[image_number_of_directory_entries];
};

using image_optional_header_t = std::conditional_t<
Expand Down

0 comments on commit 66c18bd

Please sign in to comment.