went over COFF string table and changed the parser code accept to String8

This commit is contained in:
Nikita Smith
2025-04-04 15:45:51 -07:00
parent c090bee2f3
commit 8beb168905
17 changed files with 335 additions and 380 deletions
+19 -84
View File
@@ -600,20 +600,20 @@ pe_bin_info_from_data(Arena *arena, String8 data)
// rjf: fill info
if(valid)
{
info.image_base = image_base;
info.entry_point = entry_point;
info.is_pe32 = (optional_magic == PE_PE32_MAGIC);
info.virt_section_align = virt_section_align;
info.file_section_align = file_section_align;
info.section_array_off = sec_array_off;
info.section_count = clamped_sec_count;
info.symbol_array_off = symbol_array_off;
info.symbol_count = symbol_count;
info.string_table_off = string_table_off;
info.data_dir_franges = data_dir_franges;
info.data_dir_count = data_dir_count;
info.arch = arch_from_coff_machine(file_header.machine);
info.tls_header = tls_header;
info.arch = arch_from_coff_machine(file_header.machine);
info.image_base = image_base;
info.entry_point = entry_point;
info.is_pe32 = (optional_magic == PE_PE32_MAGIC);
info.virt_section_align = virt_section_align;
info.file_section_align = file_section_align;
info.section_count = clamped_sec_count;
info.symbol_count = symbol_count;
info.section_table_range = rng_1u64(sec_array_off, sec_array_off + sizeof(COFF_SectionHeader) * clamped_sec_count);
info.symbol_table_range = rng_1u64(symbol_array_off, symbol_array_off + sizeof(COFF_Symbol16) * symbol_count);
info.string_table_range = rng_1u64(string_table_off, data.size);
info.data_dir_franges = data_dir_franges;
info.data_dir_count = data_dir_count;
info.tls_header = tls_header;
}
return info;
@@ -762,80 +762,15 @@ pe_pdata_off_from_voff__binary_search_x8664(String8 raw_pdata, U64 voff)
return result;
}
internal void *
pe_ptr_from_voff(String8 data, PE_BinInfo *bin, U64 voff)
{
// rjf: get the section for this voff
U64 sec_count = bin->section_count;
COFF_SectionHeader *sec_array = (COFF_SectionHeader*)((U8*)data.str + bin->section_array_off);
COFF_SectionHeader *sec_ptr = sec_array;
COFF_SectionHeader *sec = 0;
for(U64 i = 1; i <= sec_count; i += 1, sec_ptr += 1)
{
if(sec_ptr->voff <= voff && voff < sec_ptr->voff + sec_ptr->vsize)
{
sec = sec_ptr;
break;
}
}
// rjf: adjust to file pointer
void *result = 0;
if(sec != 0 && sec_ptr->fsize > 0)
{
U64 off = voff - sec->voff + sec->foff;
if(off < data.size)
{
result = data.str + off;
}
}
return result;
}
internal U64
pe_section_num_from_voff(String8 data, PE_BinInfo *bin, U64 voff)
{
U64 sec_count = bin->section_count;
COFF_SectionHeader *sec_array = (COFF_SectionHeader*)((U8*)data.str + bin->section_array_off);
COFF_SectionHeader *sec_ptr = sec_array;
U64 result = 0;
for(U64 i = 1; i <= sec_count; i += 1, sec_ptr += 1)
{
if(sec_ptr->voff <= voff && voff < sec_ptr->voff + sec_ptr->vsize)
{
result = i;
break;
}
}
return result;
}
internal void *
pe_ptr_from_section_num(String8 data, PE_BinInfo *bin, U64 n)
{
void *result = 0;
U64 sec_count = bin->section_count;
if(1 <= n && n <= sec_count)
{
COFF_SectionHeader *sec_array = (COFF_SectionHeader*)((U8*)data.str + bin->section_array_off);
COFF_SectionHeader *sec = sec_array + n - 1;
if(sec->fsize > 0)
{
result = data.str + sec->foff;
}
}
return(result);
}
internal U64
pe_foff_from_voff(String8 data, PE_BinInfo *bin, U64 voff)
{
U64 foff = 0;
COFF_SectionHeader *sections = (COFF_SectionHeader*)(data.str+bin->section_array_off);
U64 section_count = bin->section_count;
for(U64 sect_idx = 0; sect_idx < section_count; sect_idx += 1)
U64 foff = 0;
String8 raw_section_table = str8_substr(data, bin->section_table_range);
COFF_SectionHeader *section_table = (COFF_SectionHeader *)raw_section_table.str;
for(U64 sect_idx = 0; sect_idx < bin->section_count; sect_idx += 1)
{
COFF_SectionHeader *sect = &sections[sect_idx];
COFF_SectionHeader *sect = &section_table[sect_idx];
if(sect->voff <= voff && voff < sect->voff + sect->vsize)
{
if(!(sect->flags & COFF_SectionFlag_CntUninitializedData))
+4 -7
View File
@@ -993,17 +993,17 @@ struct PE_HandlerScope
typedef struct PE_BinInfo PE_BinInfo;
struct PE_BinInfo
{
Arch arch;
U64 image_base;
U64 entry_point;
B32 is_pe32;
U64 virt_section_align;
U64 file_section_align;
U64 section_array_off;
U64 section_count;
U64 symbol_array_off;
U64 symbol_count;
U64 string_table_off;
Arch arch;
Rng1U64 section_table_range;
Rng1U64 symbol_table_range;
Rng1U64 string_table_range;
Rng1U64 *data_dir_franges;
U32 data_dir_count;
PE_TLSHeader64 tls_header;
@@ -1085,9 +1085,6 @@ internal PE_ParsedTLS pe_tls_from_data(Arena *arena, COFF_MachineT
//~ rjf: Helpers
internal U64 pe_pdata_off_from_voff__binary_search_x8664(String8 raw_data, U64 voff);
internal void * pe_ptr_from_voff(String8 data, PE_BinInfo *bin, U64 voff);
internal U64 pe_section_num_from_voff(String8 data, PE_BinInfo *bin, U64 voff);
internal void * pe_ptr_from_section_num(String8 data, PE_BinInfo *bin, U64 n);
internal U64 pe_foff_from_voff(String8 data, PE_BinInfo *bin, U64 voff);
internal PE_BaseRelocBlockList pe_base_reloc_block_list_from_data(Arena *arena, String8 raw_relocs);
internal Rng1U64 pe_tls_rng_from_bin_base_vaddr(String8 data, PE_BinInfo *bin, U64 base_vaddr);