aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c169
1 files changed, 93 insertions, 76 deletions
diff --git a/src/main.c b/src/main.c
index 55cee66..880cd0b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,5 @@
// #include <winternl.h>
#include <Windows.h>
-#include <strsafe.h>
#include <stdint.h>
#include <winnt.h>
@@ -27,7 +26,29 @@ static u8 is_decimal_digit(char c) {
return '0' <= c && c <= '9';
}
-// TODO: Improve
+// TODO: Improve basic functions perfs
+// max_size: str allocated size
+static size_t str_len(const char* str, size_t max_size) {
+ const char* c = str;
+ size_t count = 0U;
+ while (*c != '\0' && count < max_size) {
+ c++;
+ count++;
+ }
+ return count;
+}
+
+static void str_cat(char* out, const char* str, size_t c_str) {
+ char* cur_out = out;
+ const char* cur_str = str;
+ u32 index = 0U;
+ while(index < c_str) {
+ *cur_out = *cur_str;
+ cur_out++;
+ cur_str++;
+ index++;
+ }
+}
static void mem_copy(void* src, void* dst, size_t size) {
u8* byte_src = (u8*)src;
u8* byte_dst = (u8*)dst;
@@ -118,7 +139,7 @@ static void print(const char* format, ...) {
va_list args;
va_start(args, format);
char formatted_str[4096U] = { 0 };
- STRSAFE_LPSTR cur_char = formatted_str;
+ char* cur_char = formatted_str;
const char* cur_fmt = format;
while(*cur_fmt != '\0') {
@@ -138,16 +159,13 @@ static void print(const char* format, ...) {
}
cur_fmt += size + 1U;
}
- if (length == 0U) {
- StringCchLengthA(str, sizeof(formatted_str), &length);
- }
- if(str != NULL) {
- StringCchCatA(cur_char, length + 1U, str);
+ if (length == 0U && str != NULL) {
+ length = str_len(str, sizeof(formatted_str));
+ str_cat(cur_char, str, length);
cur_char += length;
}
} else if(*cur_fmt == 'c') {
- char c = (char)va_arg(args, int);
- StringCchCatA(cur_char, 1U, &c);
+ *cur_char = (char)va_arg(args, int);
cur_char += 1U;
} else if (*cur_fmt == 'x' && is_alpha(cur_fmt[1])) {
u32 base = 16U;
@@ -161,26 +179,26 @@ static void print(const char* format, ...) {
u8 number = (u8)va_arg(args, int);
u64toa((u64)number, cur_char, base);
size_t length = 0U;
- StringCchLengthA(formatted_str, sizeof(formatted_str), &length);
+ length = str_len(formatted_str, sizeof(formatted_str));
cur_char = formatted_str + length;
} else if(*cur_fmt == 'u') {
u64 number = va_arg(args, u64);
u64toa(number, cur_char, base);
size_t length = 0U;
- StringCchLengthA(formatted_str, sizeof(formatted_str), &length);
+ length = str_len(formatted_str, sizeof(formatted_str));
cur_char = formatted_str + length;
}
} else if(*cur_fmt == 'b') {
u8 number = (u8)va_arg(args, int);
u64toa((u64)number, cur_char, 10U);
size_t length = 0U;
- StringCchLengthA(formatted_str, sizeof(formatted_str), &length);
+ length = str_len(formatted_str, sizeof(formatted_str));
cur_char = formatted_str + length;
} else if(*cur_fmt == 'u') {
u64 number = va_arg(args, u64);
u64toa(number, cur_char, 10U);
size_t length = 0U;
- StringCchLengthA(formatted_str, sizeof(formatted_str), &length);
+ length = str_len(formatted_str, sizeof(formatted_str));
cur_char = formatted_str + length;
}
} else {
@@ -236,11 +254,11 @@ static struct COFF_sections* read_COFFs_header(const char* obj_files[], u32 obj_
// TODO: Make an array type
u32 nb_sections_size = sizeof(struct COFF_sections) * obj_files_count;
- struct COFF_sections* sections = (struct COFF_sections*)VirtualAlloc(NULL, nb_sections_size, MEM_RESERVE, PAGE_READWRITE);
+ struct COFF_sections* sections = (struct COFF_sections*)VirtualAlloc(NULL, nb_sections_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
mem_set((void*)sections, nb_sections_size, 0U);
- IMAGE_FILE_HEADER* header = (IMAGE_FILE_HEADER*)VirtualAlloc(NULL, max_header_size, MEM_RESERVE, PAGE_READWRITE);
+ IMAGE_FILE_HEADER* header = (IMAGE_FILE_HEADER*)VirtualAlloc(NULL, max_header_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
IMAGE_SECTION_HEADER* section_table = (IMAGE_SECTION_HEADER*)(((u8*)header) + sizeof(*header));
for (u32 obj_id = 0U; obj_id < obj_files_count; obj_id++) {
@@ -259,7 +277,7 @@ static struct COFF_sections* read_COFFs_header(const char* obj_files[], u32 obj_
continue;
}
- if (!ReadFile(obj_file_handle, (void*)header, sizeof(section_table[0U]) * header->NumberOfSections, &bread, NULL)) {
+ if (!ReadFile(obj_file_handle, (void*)section_table, sizeof(section_table[0U]) * header->NumberOfSections, &bread, NULL)) {
CloseHandle(obj_file_handle);
continue;
}
@@ -290,7 +308,7 @@ static struct COFF_sections* read_COFFs_header(const char* obj_files[], u32 obj_
print("text section already found");
__debugbreak();
}
- *sections_flag |= BSS_SECTION_FLAG;
+ *sections_flag |= TEXT_SECTION_FLAG;
section->text_section_off = curr_section->PointerToRawData;
section->text_section_size = curr_section->SizeOfRawData;
}
@@ -391,8 +409,7 @@ static void read_COFF(u8* buffer) {
u8* data_start = &buffer[section->PointerToRawData];
u8* cur_data = data_start;
while ((cur_data - data_start) < section->SizeOfRawData) {
- size_t len = 0U;
- StringCchLengthA((STRSAFE_LPSTR)cur_data, section->SizeOfRawData, &len);
+ size_t len = str_len((const char*) cur_data, section->SizeOfRawData);
if (len == 0U) {
cur_data++;
} else {
@@ -457,8 +474,7 @@ static void read_lib(u8* buffer) {
u8* first_linker_string_table = next_byte;
u8* cur_string = first_linker_string_table;
for (u32 index = 0U; index < symbols_count; index++) {
- size_t size = 0U;
- StringCchLengthA((const char*)cur_string, 256U, &size);
+ size_t size = str_len((const char*)cur_string, 256U);
print("%s\n", cur_string);
cur_string += size + 1U;
}
@@ -490,8 +506,7 @@ static void read_lib(u8* buffer) {
u8* second_linker_string_table = next_byte;
u8* cur_string = second_linker_string_table;
for (u32 index = 0U; index < symbols_count; index++) {
- size_t size = 0U;
- StringCchLengthA((const char*)cur_string, 256U, &size);
+ size_t size = str_len((const char*)cur_string, 256U);
print("%s\n", cur_string);
cur_string += size + 1U;
}
@@ -529,11 +544,10 @@ static void read_lib(u8* buffer) {
IMPORT_OBJECT_HEADER* import_header = (IMPORT_OBJECT_HEADER*)next_byte;
char* import_name = (char*)next_byte + sizeof(*import_header);
- size_t len = 0U;
- StringCchLengthA((char*)import_name, 256U, &len);
+ size_t len = str_len((char*)import_name, 256U);
print("IMPORT NAME: %s\n", import_name);
- char* dll_name = import_name + len + 1U;
+ char* dll_name = import_name + len;
print("DLL NAME: %s\n", dll_name);
} else {
read_COFF(next_byte);
@@ -657,6 +671,8 @@ static void write_text_section(IMAGE_SECTION_HEADER* section_table, u16* section
text_section_header->NumberOfRelocations = 0U;
text_section_header->NumberOfLinenumbers = 0U;
text_section_header->Characteristics = TEXT_SECTION_FLAGS;
+
+ (*section_count)++;
}
static void write_data_section(IMAGE_SECTION_HEADER* section_table, u16* section_count) {
@@ -817,65 +833,66 @@ int mainCRTStartup(void) {
__debugbreak();
- struct PE_headers* headers = (struct PE_headers*)exe_buffer;
- IMAGE_SECTION_HEADER* section_table = (IMAGE_SECTION_HEADER*)(exe_buffer + sizeof(*headers));
- u16 c_sections = 0U;
-
- // dont write section count to img header
- write_exe_headers(exe_buffer);
+ // executable generation
+ {
+ struct PE_headers* headers = (struct PE_headers*)exe_buffer;
+ IMAGE_SECTION_HEADER* section_table = (IMAGE_SECTION_HEADER*)(exe_buffer + sizeof(*headers));
+ u16 c_sections = 0U;
- // write the relevant sections
- if (sections_flag & TEXT_SECTION_FLAG) {
- // TODO: copy text to the relevant part of the buffer
- write_text_section(section_table, &c_sections);
- }
- if (sections_flag & DATA_SECTION_FLAG) {
- write_data_section(section_table, &c_sections);
- }
- if (sections_flag & BSS_SECTION_FLAG) {
- write_bss_section(section_table, &c_sections);
- }
+ // dont write section count to img header
+ write_exe_headers(exe_buffer);
- headers->img_header.NumberOfSections = c_sections;
+ // write the relevant sections
+ if (sections_flag & TEXT_SECTION_FLAG) {
+ // TODO: copy text to the relevant part of the buffer
+ write_text_section(section_table, &c_sections);
+ }
+ if (sections_flag & DATA_SECTION_FLAG) {
+ write_data_section(section_table, &c_sections);
+ }
+ if (sections_flag & BSS_SECTION_FLAG) {
+ write_bss_section(section_table, &c_sections);
+ }
- // Write section bodies
- // TODO: index per section ?
- u32 code_size = sizeof(test_main);
- u32 headers_size = ALIGN(sizeof(*headers) + (sizeof(IMAGE_SECTION_HEADER) * c_sections), FILE_ALIGNMENT);
- mem_copy(test_main, (void*)(exe_buffer + headers_size), code_size);
- section_table[0U].SizeOfRawData = ALIGN(code_size, FILE_ALIGNMENT);
- section_table[0U].Misc.VirtualSize = ALIGN(code_size, SECTION_ALIGNMENT);
+ headers->img_header.NumberOfSections = c_sections;
- // Set sections pointer to raw data
- _Static_assert(FILE_ALIGNMENT < SECTION_ALIGNMENT, "section alignment must greater or equal to file alignment in order to align headers_size on section alignment");
- u32 file_offset = headers_size;
- u32 virtual_offset = ALIGN(headers_size, SECTION_ALIGNMENT);
- IMAGE_SECTION_HEADER* cur_section = section_table;
- for (u32 section_index = 0U; section_index < c_sections; section_index++, cur_section++) {
- cur_section->PointerToRawData = file_offset;
- cur_section->VirtualAddress = virtual_offset;
+ // Write section bodies
+ // TODO: index per section ?
+ u32 code_size = sizeof(test_main);
+ u32 headers_size = ALIGN(sizeof(*headers) + (sizeof(IMAGE_SECTION_HEADER) * c_sections), FILE_ALIGNMENT);
+ mem_copy(test_main, (void*)(exe_buffer + headers_size), code_size);
+ section_table[0U].SizeOfRawData = ALIGN(code_size, FILE_ALIGNMENT);
+ section_table[0U].Misc.VirtualSize = ALIGN(code_size, SECTION_ALIGNMENT);
- ASSERT(cur_section->SizeOfRawData == ALIGN(cur_section->SizeOfRawData, FILE_ALIGNMENT));
- ASSERT(cur_section->Misc.VirtualSize == ALIGN(cur_section->Misc.VirtualSize, SECTION_ALIGNMENT));
- file_offset += cur_section->SizeOfRawData;
- virtual_offset += cur_section->Misc.VirtualSize;
- }
+ // Set sections pointer to raw data
+ _Static_assert(FILE_ALIGNMENT < SECTION_ALIGNMENT, "section alignment must greater or equal to file alignment in order to align headers_size on section alignment");
+ u32 file_offset = headers_size;
+ u32 virtual_offset = ALIGN(headers_size, SECTION_ALIGNMENT);
+ IMAGE_SECTION_HEADER* cur_section = section_table;
+ for (u32 section_index = 0U; section_index < c_sections; section_index++, cur_section++) {
+ cur_section->PointerToRawData = file_offset;
+ cur_section->VirtualAddress = virtual_offset;
- // Fill remaining header informations
+ ASSERT(cur_section->SizeOfRawData == ALIGN(cur_section->SizeOfRawData, FILE_ALIGNMENT));
+ ASSERT(cur_section->Misc.VirtualSize == ALIGN(cur_section->Misc.VirtualSize, SECTION_ALIGNMENT));
+ file_offset += cur_section->SizeOfRawData;
+ virtual_offset += cur_section->Misc.VirtualSize;
+ }
- headers->opt_header.SizeOfHeaders = headers_size; // aligned on file alignment
- headers->opt_header.AddressOfEntryPoint = section_table[0U].VirtualAddress;
- headers->opt_header.BaseOfCode = section_table[0U].PointerToRawData;
- headers->opt_header.SizeOfCode = section_table[0U].SizeOfRawData;
- headers->opt_header.SizeOfImage = ALIGN(section_table[c_sections - 1U].VirtualAddress + section_table[c_sections - 1U].Misc.VirtualSize, SECTION_ALIGNMENT);
+ // Fill remaining header informations
- DWORD bwrite = 0U;
- WriteFile(output_file, exe_buffer, headers->opt_header.SizeOfImage, &bwrite, NULL);
- ASSERT(bwrite == headers->opt_header.SizeOfImage);
+ headers->opt_header.SizeOfHeaders = headers_size; // aligned on file alignment
+ headers->opt_header.AddressOfEntryPoint = section_table[0U].VirtualAddress;
+ headers->opt_header.BaseOfCode = section_table[0U].PointerToRawData;
+ headers->opt_header.SizeOfCode = section_table[0U].SizeOfRawData;
+ headers->opt_header.SizeOfImage = ALIGN(section_table[c_sections - 1U].VirtualAddress + section_table[c_sections - 1U].Misc.VirtualSize, SECTION_ALIGNMENT);
- CloseHandle(output_file);
+ DWORD bwrite = 0U;
+ WriteFile(output_file, exe_buffer, headers->opt_header.SizeOfImage, &bwrite, NULL);
+ ASSERT(bwrite == headers->opt_header.SizeOfImage);
- __debugbreak();
+ CloseHandle(output_file);
+ }
VirtualFree(sections, 0U, MEM_RELEASE);
VirtualFree(exe_buffer, 0U, MEM_RELEASE);