diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/loader.c | 23 | ||||
| -rw-r--r-- | src/module.c | 3 | ||||
| -rw-r--r-- | src/pdb.c | 20 |
3 files changed, 29 insertions, 17 deletions
diff --git a/src/loader.c b/src/loader.c index 2e79e67..8f2ac28 100644 --- a/src/loader.c +++ b/src/loader.c @@ -133,10 +133,8 @@ size_t str_len(const char* str) { return count; } -i32 str_cmp(const i8* str1, size_t str1_len, const i8* str2, size_t str2_len) { - u32 min_len = str1_len < str2_len ? str1_len : str2_len; - - for (u32 index = 0U; index < min_len; index++) { +i32 str_cmpn(const i8* str1, const i8* str2, size_t count) { + for (u32 index = 0U; index < count; index++) { if (str1[index] < str2[index]) { return -1; } else if (str1[index] > str2[index]) { @@ -147,10 +145,8 @@ i32 str_cmp(const i8* str1, size_t str1_len, const i8* str2, size_t str2_len) { return 0; } -i32 wstr_cmp(const u16* str1, size_t str1_len, const u16* str2, size_t str2_len) { - u32 min_len = str1_len < str2_len ? str1_len : str2_len; - - for (u32 index = 0U; index < min_len; index++) { +i32 wstr_cmpn(const u16* str1, const u16* str2, size_t count) { + for (u32 index = 0U; index < count; index++) { if (str1[index] < str2[index]) { return -1; } else if (str1[index] > str2[index]) { @@ -174,8 +170,9 @@ void* get_module(u16* name, size_t name_size) { LDR_DATA_TABLE_ENTRY* table_entry = (LDR_DATA_TABLE_ENTRY*)((u8*)cur_entry - 0x10); UNICODE_STRING* BaseDllName = &table_entry->BaseDllName; - if (BaseDllName->Buffer) { - if (wstr_cmp(name, name_size / sizeof(u16), BaseDllName->Buffer, BaseDllName->Length / sizeof(u16)) == 0) { + if (BaseDllName->Buffer && ((name_size / sizeof(u16)) - 1) == (BaseDllName->Length / sizeof(u16))) { + if (wstr_cmpn(name, BaseDllName->Buffer, BaseDllName->Length / sizeof(u16)) == 0) { + return table_entry->DllBase; } } @@ -183,6 +180,7 @@ void* get_module(u16* name, size_t name_size) { cur_entry = cur_entry->Flink; } while(cur_entry != first_entry); + assert(FALSE); return NULL; } @@ -198,13 +196,12 @@ void* load_func(u64 base_addr, const char* func_name, u32 func_name_len) { for (u32 name_index = 0U; name_index < export_dir->NumberOfNames; name_index++) { const char* name = (const char*)(base_addr + names_address[name_index]); - u32 length = str_len(name); - - if (str_cmp((i8*)name, length, (i8*)func_name, func_name_len) == 0) { + if (str_cmpn((i8*)name, (i8*)func_name, func_name_len - 1U) == 0) { return (void*)(base_addr + addresses[ordinals[name_index]]); } } + assert(FALSE); return NULL; } diff --git a/src/module.c b/src/module.c index 25ab2ad..13f0a8d 100644 --- a/src/module.c +++ b/src/module.c @@ -5,7 +5,8 @@ extern unsigned char _BitScanForward64(unsigned long *_Index, uint64_t _Mask); struct module modules[64U] = { 0U }; -u64 modules_list = ~0ULL; +// u64 modules_list = ~0ULL; // TODO: Check if modules_list is supposed to be filled with one's ? +u64 modules_list = 0ULL; // TODO: Filled modules_list with zeroes since we assert during the first call to add_module, see comment above // u64 module_symbols @@ -181,6 +181,12 @@ // ====== !windows.h ====== // ======================== +#if defined(_MSC_VER) && !defined(__clang__) + #define alloca +#elif defined(__clang__) + #define alloca __builtin_alloca +#endif + enum STREAM_INDEX { OLD_DIRECTORY, PDB, @@ -193,13 +199,16 @@ extern u8* string_stack_base; extern u8* string_stack_current; void open_pdb(const i8* pdb_path, struct module* module) { - void* pdb_file = CreateFileA(pdb_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + void* pdb_file = CreateFileA(pdb_path, GENERIC_READ | FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (pdb_file == INVALID_HANDLE_VALUE) { + if (pdb_file == NULL || pdb_file == INVALID_HANDLE_VALUE) { print("Cannot open pdb file at path %s due to error %u\n", pdb_path, GetLastError()); return; } + LARGE_INTEGER file_size = { 0 }; + assert(SUCCEEDED(GetFileSizeEx(pdb_file, &file_size))); + // MSF superblock struct msf_superblock { u8 magic[32U]; @@ -211,14 +220,19 @@ void open_pdb(const i8* pdb_path, struct module* module) { u32 block_map_addr; }; struct msf_superblock superblock = { 0U }; - u8 block[4096U] = { 0U }; u32 b_read = 0U; ReadFile(pdb_file, (void*)&superblock, sizeof(superblock), &b_read, NULL); + + assert(b_read == sizeof(superblock)); + assert(((i64)superblock.num_blocks) * superblock.block_size == file_size.QuadPart); + u8* block = alloca(superblock.block_size); + // Stream directory u32 dir_addr = (superblock.block_map_addr - 1U) * sizeof(block); SetFilePointer(pdb_file, dir_addr, NULL, FILE_BEGIN); ReadFile(pdb_file, (void*)block, superblock.num_directory_bytes, &b_read, NULL); + assert(b_read == superblock.num_directory_bytes); // Loog through streams u32 num_streams = ((u32*)block)[0U]; |
