From 2be196869a1833f41f1792357443dec2aacf350c Mon Sep 17 00:00:00 2001 From: ladevieq Date: Wed, 13 Aug 2025 23:51:34 +0200 Subject: Change the way sections are written --- src/main.c | 392 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 266 insertions(+), 126 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 096cc01..55cee66 100644 --- a/src/main.c +++ b/src/main.c @@ -199,10 +199,109 @@ static void print(const char* format, ...) { } +static const u32 BSS_SECTION_FLAGS = (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE); +static const u32 DRECTVE_SECTION_FLAGS = IMAGE_SCN_LNK_INFO; +static const u32 DATA_SECTION_FLAGS = (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE); static const u32 TEXT_SECTION_FLAGS = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ); -static const u32 DRECTVE_SECTION_FLAGS = (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE); static const u64 lib_signature = 0x0A3E686372613C21; // IMAGE_ARCHIVE_START +enum { + NONE_SECTION_FLAG = 0U, + TEXT_SECTION_FLAG = 1U, + DATA_SECTION_FLAG = 2U, + BSS_SECTION_FLAG = 4U, +}; + +// we allow for only one of each type +struct COFF_sections { + u32 text_section_off; + u32 text_section_size; + u32 data_section_off; + u32 data_section_size; + u32 bss_section_off; + u32 bss_section_size; + u32 symbol_table_off; + u32 symbols_count; + u32 string_table_off; + u32 string_section_size; +}; + +static struct COFF_sections* read_COFFs_header(const char* obj_files[], u32 obj_files_count, u8* sections_flag) { + ASSERT(*sections_flag == 0U); + if (obj_files_count == 0U) { + return NULL; + } + + size_t max_header_size = sizeof(IMAGE_SECTION_HEADER) * 96U + sizeof(IMAGE_FILE_HEADER); + + // 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); + + 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_SECTION_HEADER* section_table = (IMAGE_SECTION_HEADER*)(((u8*)header) + sizeof(*header)); + + for (u32 obj_id = 0U; obj_id < obj_files_count; obj_id++) { + struct COFF_sections* section = §ions[obj_id]; + const char* obj_file = obj_files[obj_id]; + + HANDLE obj_file_handle = CreateFileA(obj_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0U, NULL); + + if (!obj_file_handle && obj_file_handle != INVALID_HANDLE_VALUE) { + continue; + } + + DWORD bread = 0U; + if (!ReadFile(obj_file_handle, (void*)header, sizeof(*header), &bread, NULL)) { + CloseHandle(obj_file_handle); + continue; + } + + if (!ReadFile(obj_file_handle, (void*)header, sizeof(section_table[0U]) * header->NumberOfSections, &bread, NULL)) { + CloseHandle(obj_file_handle); + continue; + } + + section->symbol_table_off = header->PointerToSymbolTable; + section->symbols_count = header->NumberOfSymbols; + + for (u32 section_index = 0U; section_index < header->NumberOfSections; section_index++) { + IMAGE_SECTION_HEADER* curr_section = §ion_table[section_index]; + if ((curr_section->Characteristics & BSS_SECTION_FLAGS) == BSS_SECTION_FLAGS) { + if (section->bss_section_off != 0U) { + print("BSS section already found"); + __debugbreak(); + } + *sections_flag |= BSS_SECTION_FLAG; + section->bss_section_off = curr_section->PointerToRawData; + section->bss_section_size = curr_section->SizeOfRawData; + } else if ((curr_section->Characteristics & DATA_SECTION_FLAGS) == DATA_SECTION_FLAGS) { + if (section->data_section_size != 0U) { + print("data section already found"); + __debugbreak(); + } + *sections_flag |= DATA_SECTION_FLAG; + section->data_section_off = curr_section->PointerToRawData; + section->data_section_size = curr_section->SizeOfRawData; + } else if ((curr_section->Characteristics & TEXT_SECTION_FLAGS) == TEXT_SECTION_FLAGS) { + if (section->text_section_off != 0U) { + print("text section already found"); + __debugbreak(); + } + *sections_flag |= BSS_SECTION_FLAG; + section->text_section_off = curr_section->PointerToRawData; + section->text_section_size = curr_section->SizeOfRawData; + } + } + CloseHandle(obj_file_handle); + } + + VirtualFree(header, 0U, MEM_RELEASE); + return sections; +} + static void read_COFF(u8* buffer) { u8* next_byte = buffer; IMAGE_FILE_HEADER header = *(IMAGE_FILE_HEADER*)next_byte; @@ -228,7 +327,7 @@ static void read_COFF(u8* buffer) { next_byte += sizeof(sections[0]) * header.NumberOfSections; for (u32 section_index = 0U; section_index < header.NumberOfSections; section_index++) { IMAGE_SECTION_HEADER* section = §ions[section_index]; - print("SECTION NAME: %s\n", section->Name); + print("\nSECTION NAME: %s\n", section->Name); print("SECTION OFFSET: %u\n", section->PointerToRawData); print("SECTION SIZE: %u\n", section->SizeOfRawData); print("SECTION RELOCATIONS COUNT: %u\n", section->NumberOfRelocations); @@ -240,57 +339,55 @@ static void read_COFF(u8* buffer) { for (u32 reloc_index = 0U; reloc_index < section->NumberOfRelocations; reloc_index++, cur_reloc_entry++) { print("SYMBOL INDEX : %u\n", cur_reloc_entry->SymbolTableIndex); - IMAGE_SYMBOL* sym = symbol_table; - for (u32 symbol_index = 0U; symbol_index < cur_reloc_entry->SymbolTableIndex; symbol_index++, sym++) { - if (sym->N.Name.Short == 0U) { // use longname - print("SYMBOL NAME : %s\n", string_table + sym->N.Name.Long); - } else { - print("SYMBOL NAME : %s.8\n", sym->N.ShortName); - } + IMAGE_SYMBOL* sym = symbol_table + cur_reloc_entry->SymbolTableIndex; + if (sym->N.Name.Short == 0U) { // use longname + print("SYMBOL NAME : %s\n", string_table + sym->N.Name.Long); + } else { + print("SYMBOL NAME : %s.8\n", sym->N.ShortName); } - // if (cur_reloc_entry->type == IMAGE_REL_AMD64_ABSOLUTE) { - // print("IMAGE_REL_AMD64_ABSOLUTE\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_ADDR64) { - // print("IMAGE_REL_AMD64_ADDR64\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_ADDR32) { - // print("IMAGE_REL_AMD64_ADDR32\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_ADDR32NB) { - // print("IMAGE_REL_AMD64_ADDR32NB\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32) { - // print("IMAGE_REL_AMD64_REL32\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32_1) { - // print("IMAGE_REL_AMD64_REL32_1\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32_2) { - // print("IMAGE_REL_AMD64_REL32_2\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32_3) { - // print("IMAGE_REL_AMD64_REL32_3\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32_4) { - // print("IMAGE_REL_AMD64_REL32_4\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_REL32_5) { - // print("IMAGE_REL_AMD64_REL32_5\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_SECTION) { - // print("IMAGE_REL_AMD64_SECTION\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_SECREL) { - // print("IMAGE_REL_AMD64_SECREL\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_SECREL7) { - // print("IMAGE_REL_AMD64_SECREL7\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_TOKEN) { - // print("IMAGE_REL_AMD64_TOKEN\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_SREL32) { - // print("IMAGE_REL_AMD64_SREL32\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_PAIR) { - // print("IMAGE_REL_AMD64_PAIR\n"); - // } else if (cur_reloc_entry->type == IMAGE_REL_AMD64_SSPAN32) { - // print("IMAGE_REL_AMD64_SSPAN32\n"); - // } + if (cur_reloc_entry->Type == IMAGE_REL_AMD64_ABSOLUTE) { + print("IMAGE_REL_AMD64_ABSOLUTE\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_ADDR64) { + print("IMAGE_REL_AMD64_ADDR64\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_ADDR32) { + print("IMAGE_REL_AMD64_ADDR32\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_ADDR32NB) { + print("IMAGE_REL_AMD64_ADDR32NB\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32) { + print("IMAGE_REL_AMD64_REL32\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32_1) { + print("IMAGE_REL_AMD64_REL32_1\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32_2) { + print("IMAGE_REL_AMD64_REL32_2\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32_3) { + print("IMAGE_REL_AMD64_REL32_3\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32_4) { + print("IMAGE_REL_AMD64_REL32_4\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_REL32_5) { + print("IMAGE_REL_AMD64_REL32_5\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_SECTION) { + print("IMAGE_REL_AMD64_SECTION\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_SECREL) { + print("IMAGE_REL_AMD64_SECREL\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_SECREL7) { + print("IMAGE_REL_AMD64_SECREL7\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_TOKEN) { + print("IMAGE_REL_AMD64_TOKEN\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_SREL32) { + print("IMAGE_REL_AMD64_SREL32\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_PAIR) { + print("IMAGE_REL_AMD64_PAIR\n"); + } else if (cur_reloc_entry->Type == IMAGE_REL_AMD64_SSPAN32) { + print("IMAGE_REL_AMD64_SSPAN32\n"); + } } - if (section->Characteristics & IMAGE_SCN_LNK_INFO) // .drectve + if (section->Characteristics & DRECTVE_SECTION_FLAGS) // .drectve { u8* drectve_start = &buffer[section->PointerToRawData]; print("%s\n", drectve_start); - } else if ((section->Characteristics & DRECTVE_SECTION_FLAGS) == DRECTVE_SECTION_FLAGS) { // .data + } else if ((section->Characteristics & DATA_SECTION_FLAGS) == DATA_SECTION_FLAGS) { // .data u8* data_start = &buffer[section->PointerToRawData]; u8* cur_data = data_start; while ((cur_data - data_start) < section->SizeOfRawData) { @@ -307,6 +404,7 @@ static void read_COFF(u8* buffer) { // u8* text_start = &buffer[section->pointer_to_raw_data]; } } + print("\n"); // TODO: Check that this doesn't overlap with sections relocation symbols IMAGE_SYMBOL* cur_symbol = symbol_table; @@ -334,6 +432,7 @@ static void read_COFF(u8* buffer) { symbol_index++; } } + print("\n"); } #define BSWAP32(x) ((x & 0x000000FF) << 24U) | ((x & 0x0000FF00) << 8U) | ((x & 0x00FF0000) >> 8U) | ((x & 0xFF000000) >> 24U) @@ -440,6 +539,7 @@ static void read_lib(u8* buffer) { read_COFF(next_byte); } next_byte += archive_size; + print("\n"); } } @@ -556,7 +656,35 @@ static void write_text_section(IMAGE_SECTION_HEADER* section_table, u16* section text_section_header->PointerToLinenumbers = 0x0U; text_section_header->NumberOfRelocations = 0U; text_section_header->NumberOfLinenumbers = 0U; - text_section_header->Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE; + text_section_header->Characteristics = TEXT_SECTION_FLAGS; +} + +static void write_data_section(IMAGE_SECTION_HEADER* section_table, u16* section_count) { + IMAGE_SECTION_HEADER* data_section_header = §ion_table[*section_count]; + mem_copy(".text", data_section_header->Name, 6U); + + data_section_header->VirtualAddress = 0x0U; + data_section_header->PointerToRawData = 0x0U; + data_section_header->PointerToRelocations = 0x0U; + data_section_header->PointerToLinenumbers = 0x0U; + data_section_header->NumberOfRelocations = 0U; + data_section_header->NumberOfLinenumbers = 0U; + data_section_header->Characteristics = DATA_SECTION_FLAGS; + + (*section_count)++; +} + +static void write_bss_section(IMAGE_SECTION_HEADER* section_table, u16* section_count) { + IMAGE_SECTION_HEADER* bss_section_header = §ion_table[*section_count]; + mem_copy(".text", bss_section_header->Name, 6U); + + bss_section_header->VirtualAddress = 0x0U; + bss_section_header->PointerToRawData = 0x0U; + bss_section_header->PointerToRelocations = 0x0U; + bss_section_header->PointerToLinenumbers = 0x0U; + bss_section_header->NumberOfRelocations = 0U; + bss_section_header->NumberOfLinenumbers = 0U; + bss_section_header->Characteristics = BSS_SECTION_FLAGS; (*section_count)++; } @@ -570,27 +698,26 @@ int mainCRTStartup(void) { u32* argc = VirtualAlloc(NULL, 4096U, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); argv[0U] = args_string; argc[0U] = 0U; - u32 arg_count = 1U; + u32 c_arg = 1U; { LPSTR args = GetCommandLineA(); for (u32 char_index = 0U; *(args + char_index) != '\0'; char_index++) { char c = *(args + char_index); - argc[arg_count - 1U]++; + argc[c_arg - 1U]++; if (c == ' ') { args_string[char_index] = '\0'; - argv[arg_count] = &args_string[char_index + 1U]; - arg_count++; - argc[arg_count - 1U] = 0U; - } else - { + argv[c_arg] = &args_string[char_index + 1U]; + c_arg++; + argc[c_arg - 1U] = 0U; + } else { args_string[char_index] = c; } } } - for (u32 arg_index = 0U; arg_index < arg_count; arg_index++) { + for (u32 arg_index = 0U; arg_index < c_arg; arg_index++) { const char* arg = argv[arg_index]; u32 arg_size = argc[arg_index]; @@ -598,110 +725,123 @@ int mainCRTStartup(void) { } const char* obj_files[64U] = { NULL }; - u32 obj_files_count = 0U; + u32 c_obj_files = 0U; - for (u32 arg_index = 0U; arg_index < arg_count; arg_index++) { + for (u32 arg_index = 0U; arg_index < c_arg; arg_index++) { const char* arg = argv[arg_index]; u32 arg_size = argc[arg_index]; u32 arg_ext = *(u32*)(arg + arg_size - 5U); if (arg_ext == OBJ_EXT) { - obj_files[obj_files_count] = arg; - obj_files_count++; + obj_files[c_obj_files] = arg; + c_obj_files++; } } - char* libs[64U] = { 0U }; - u32 libs_count = 0U; - char* lib_paths = VirtualAlloc(NULL, 4096U, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + char* lib_paths[64U] = { 0U }; + u32 c_lib_paths = 0U; + char* lib_path_env = VirtualAlloc(NULL, 4096U, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); { - DWORD ret = GetEnvironmentVariableA("LIB", (char*)lib_paths, 4096U); + DWORD ret = GetEnvironmentVariableA("LIB", (char*)lib_path_env, 4096U); if (ret != 0U) { - print("LIB: %s\n", lib_paths); - libs[0U] = lib_paths; - - for (u32 char_index = 0U; *(lib_paths + char_index) != '\0'; char_index++) { - char c = *(lib_paths + char_index); - if (c == ';') { - libs_count++; - lib_paths[char_index] = '\0'; - libs[libs_count] = &lib_paths[char_index + 1U]; + print ("LIB : %s\n", lib_path_env); + lib_paths[0U] = lib_path_env; + + for (char* c = lib_path_env; *c != '\0'; c++) { + if (*c == ';') { + c_lib_paths++; + *c = '\0'; + lib_paths[c_lib_paths] = c + 1U; } } - if (libs[libs_count][0U] != '\0') { - libs_count++; + if (lib_paths[c_lib_paths][0U] != '\0') { + c_lib_paths++; } } } - for (u32 index = 0U; index < libs_count; index++) { - print("LIB %u: %s\n", index, libs[index]); + for (u32 index = 0U; index < c_lib_paths; index++) { + print("LIB %u: %s\n", index, lib_paths[index]); } - char* filepath = VirtualAlloc(NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); - for (u32 arg_index = 1U; arg_index < arg_count; arg_index++) { - // NtCreateFile(".\build\main.obj", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0U, NULL); - print("ARG %u: %s\n", arg_index, argv[arg_index]); - - u32 pathsize = SearchPathA(".", argv[arg_index], NULL, 4096U, filepath, NULL); - if (pathsize == 0U) { - for (u32 lib_index = 0U; lib_index < libs_count && pathsize == 0U; lib_index++) { - pathsize = SearchPathA(libs[lib_index], argv[arg_index], NULL, 4096U, filepath, NULL); - } - } - - if (pathsize != 0U) { - print("FOUND: %s\n", filepath); - } else { - print("COULD NOT FOUND %s\n", argv[arg_index]); - continue; - } - - HANDLE file = CreateFile(filepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0U, NULL); - - if (!file && file != INVALID_HANDLE_VALUE) { - continue; - } - - LARGE_INTEGER file_size = { 0 }; - GetFileSizeEx(file, &file_size); - - u8* buffer = VirtualAlloc(NULL, file_size.LowPart, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); - - DWORD bread = 0U; - ReadFile(file, buffer, file_size.LowPart, &bread, NULL); - - if (*(u64*)buffer == lib_signature) { - read_lib(buffer + sizeof(lib_signature)); - } else { - read_COFF(buffer); - } - CloseHandle(file); - VirtualFree(buffer, 0U, MEM_RELEASE); - } + // char* filepath = VirtualAlloc(NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + // for (u32 arg_index = 1U; arg_index < c_arg; arg_index++) { + // // NtCreateFile(".\build\main.obj", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0U, NULL); + // print("ARG %u: %s\n", arg_index, argv[arg_index]); + + // u32 pathsize = SearchPathA(".", argv[arg_index], NULL, 4096U, filepath, NULL); + // if (pathsize == 0U) { + // for (u32 lib_index = 0U; lib_index < c_libs && pathsize == 0U; lib_index++) { + // pathsize = SearchPathA(libs[lib_index], argv[arg_index], NULL, 4096U, filepath, NULL); + // } + // } + + // if (pathsize != 0U) { + // print("FOUND: %s\n", filepath); + // } else { + // print("COULD NOT FOUND %s\n", argv[arg_index]); + // continue; + // } + + // HANDLE file = CreateFileA(filepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0U, NULL); + + // if (!file && file != INVALID_HANDLE_VALUE) { + // continue; + // } + + // LARGE_INTEGER file_size = { 0 }; + // GetFileSizeEx(file, &file_size); + + // u8* buffer = VirtualAlloc(NULL, file_size.LowPart, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + + // DWORD bread = 0U; + // ReadFile(file, buffer, file_size.LowPart, &bread, NULL); + + // if (*(u64*)buffer == lib_signature) { + // read_lib(buffer + sizeof(lib_signature)); + // } else { + // read_COFF(buffer); + // } + // CloseHandle(file); + // VirtualFree(buffer, 0U, MEM_RELEASE); + // } + // VirtualFree(filepath, 0U, MEM_RELEASE); // TODO: Reserve a huge block and commit when we need more memory u8* exe_buffer = VirtualAlloc(NULL, 4096U * 10U, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); HANDLE output_file = CreateFile("./test.exe", GENERIC_WRITE, 0U, NULL, CREATE_ALWAYS, 0U, NULL); + // loop through all OBJs header to find out which sections we need + u8 sections_flag = 0U; + struct COFF_sections* sections = read_COFFs_header(obj_files, c_obj_files, §ions_flag); + __debugbreak(); struct PE_headers* headers = (struct PE_headers*)exe_buffer; IMAGE_SECTION_HEADER* section_table = (IMAGE_SECTION_HEADER*)(exe_buffer + sizeof(*headers)); - u16 section_count = 0U; + u16 c_sections = 0U; // dont write section count to img header write_exe_headers(exe_buffer); - // Write section headers - write_text_section(section_table, §ion_count); + // 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); + } - headers->img_header.NumberOfSections = section_count; + headers->img_header.NumberOfSections = 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) * section_count), FILE_ALIGNMENT); + 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); @@ -711,7 +851,7 @@ int mainCRTStartup(void) { 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 < section_count; section_index++, cur_section++) { + for (u32 section_index = 0U; section_index < c_sections; section_index++, cur_section++) { cur_section->PointerToRawData = file_offset; cur_section->VirtualAddress = virtual_offset; @@ -727,7 +867,7 @@ int mainCRTStartup(void) { 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[section_count - 1U].VirtualAddress + section_table[section_count - 1U].Misc.VirtualSize, SECTION_ALIGNMENT); + headers->opt_header.SizeOfImage = ALIGN(section_table[c_sections - 1U].VirtualAddress + section_table[c_sections - 1U].Misc.VirtualSize, SECTION_ALIGNMENT); DWORD bwrite = 0U; WriteFile(output_file, exe_buffer, headers->opt_header.SizeOfImage, &bwrite, NULL); @@ -737,9 +877,9 @@ int mainCRTStartup(void) { __debugbreak(); + VirtualFree(sections, 0U, MEM_RELEASE); VirtualFree(exe_buffer, 0U, MEM_RELEASE); - VirtualFree(filepath, 0U, MEM_RELEASE); - VirtualFree(lib_paths, 0U, MEM_RELEASE); + VirtualFree(lib_path_env, 0U, MEM_RELEASE); VirtualFree(argv, 0U, MEM_RELEASE); VirtualFree(args_string, 0U, MEM_RELEASE); -- cgit v1.3