From 53c889e5d3dde1de0f788cf7520ad35da4967fe5 Mon Sep 17 00:00:00 2001 From: Nikita Smith Date: Thu, 27 Mar 2025 11:12:48 -0700 Subject: [PATCH] switched to using base Arch enum for converting DWARF registers, simplified top level building path --- src/dwarf/dwarf.c | 20 +++++------ src/dwarf/dwarf.h | 12 +++---- src/radcon/radcon_coff.c | 22 ------------ src/radcon/radcon_coff.h | 1 - src/radcon/radcon_dwarf.c | 66 +++++++++++++++-------------------- src/radcon/radcon_elf.c | 7 ---- src/radcon/radcon_elf.h | 1 - src/radcon/radcon_pdb.c | 10 +++--- src/rdi_make/rdi_make_local.c | 33 ++++++++++++++++++ src/rdi_make/rdi_make_local.h | 5 +++ 10 files changed, 87 insertions(+), 90 deletions(-) diff --git a/src/dwarf/dwarf.c b/src/dwarf/dwarf.c index 77d334a1..0c552663 100644 --- a/src/dwarf/dwarf.c +++ b/src/dwarf/dwarf.c @@ -2,7 +2,7 @@ // Licensed under the MIT license (https://opensource.org/license/mit/) internal U64 -dw_reg_size_from_code_x86(U64 reg_code) +dw_reg_size_from_code_x86(DW_Reg reg_code) { switch (reg_code) { #define X(reg_name_dw, reg_code_dw, reg_name_rdi, reg_pos, reg_size) case DW_RegX86_##reg_name_dw: return reg_size; @@ -13,7 +13,7 @@ dw_reg_size_from_code_x86(U64 reg_code) } internal U64 -dw_reg_pos_from_code_x86(U64 reg_code) +dw_reg_pos_from_code_x86(DW_Reg reg_code) { switch (reg_code) { #define X(reg_name_dw, reg_code_dw, reg_name_rdi, reg_pos, reg_size) case DW_RegX86_##reg_name_dw: return reg_pos; @@ -24,7 +24,7 @@ dw_reg_pos_from_code_x86(U64 reg_code) } internal U64 -dw_reg_size_from_code_x64(U64 reg_code) +dw_reg_size_from_code_x64(DW_Reg reg_code) { switch (reg_code) { #define X(reg_name_dw, reg_code_dw, reg_name_rdi, reg_pos, reg_size) case DW_RegX64_##reg_name_dw: return reg_size; @@ -35,7 +35,7 @@ dw_reg_size_from_code_x64(U64 reg_code) } internal U64 -dw_reg_pos_from_code_x64(U64 reg_code) +dw_reg_pos_from_code_x64(DW_Reg reg_code) { switch (reg_code) { #define X(reg_name_dw, reg_code_dw, reg_name_rdi, reg_pos, reg_size) case DW_RegX64_##reg_name_dw: return reg_pos; @@ -46,21 +46,21 @@ dw_reg_pos_from_code_x64(U64 reg_code) } internal U64 -dw_reg_size_from_code(RDI_Arch arch, U64 reg_code) +dw_reg_size_from_code(RDI_Arch arch, DW_Reg reg_code) { switch (arch) { - case RDI_Arch_X86: return dw_reg_size_from_code_x86(reg_code); - case RDI_Arch_X64: return dw_reg_size_from_code_x64(reg_code); + case Arch_x86: return dw_reg_size_from_code_x86(reg_code); + case Arch_x64: return dw_reg_size_from_code_x64(reg_code); } return 0; } internal U64 -dw_reg_pos_from_code(RDI_Arch arch, U64 reg_code) +dw_reg_pos_from_code(RDI_Arch arch, DW_Reg reg_code) { switch (arch) { - case RDI_Arch_X86: return dw_reg_pos_from_code_x86(reg_code); - case RDI_Arch_X64: return dw_reg_pos_from_code_x64(reg_code); + case Arch_x86: return dw_reg_pos_from_code_x86(reg_code); + case Arch_x64: return dw_reg_pos_from_code_x64(reg_code); } return max_U64; } diff --git a/src/dwarf/dwarf.h b/src/dwarf/dwarf.h index e6a246af..4c7132a4 100644 --- a/src/dwarf/dwarf.h +++ b/src/dwarf/dwarf.h @@ -1734,12 +1734,12 @@ typedef enum DW_RegX64Enum //////////////////////////////// -internal U64 dw_reg_size_from_code_x86(U64 reg_code); -internal U64 dw_reg_pos_from_code_x86(U64 reg_code); -internal U64 dw_reg_size_from_code_x64(U64 reg_code); -internal U64 dw_reg_pos_from_code_x64(U64 reg_code); -internal U64 dw_reg_size_from_code(Arch arch, U64 reg_code); -internal U64 dw_reg_pos_from_code(Arch arch, U64 reg_code); +internal U64 dw_reg_size_from_code_x86(DW_Reg reg_code); +internal U64 dw_reg_pos_from_code_x86(DW_Reg reg_code); +internal U64 dw_reg_size_from_code_x64(DW_Reg reg_code); +internal U64 dw_reg_pos_from_code_x64(DW_Reg reg_code); +internal U64 dw_reg_size_from_code(Arch arch, DW_Reg reg_code); +internal U64 dw_reg_pos_from_code(Arch arch, DW_Reg reg_code); //- Attrib Class Encodings diff --git a/src/radcon/radcon_coff.c b/src/radcon/radcon_coff.c index 9f5b6059..56306141 100644 --- a/src/radcon/radcon_coff.c +++ b/src/radcon/radcon_coff.c @@ -78,25 +78,3 @@ c2r_rdi_binary_sections_from_coff_sections(Arena *arena, String8 image_data, U64 return binary_sections; } -internal RDIM_TopLevelInfo -c2r_make_rdim_top_level_info(String8 image_name, RDI_Arch arch, U64 exe_hash, U64 sectab_count, COFF_SectionHeader *sectab) -{ - U64 exe_voff_max = 0; - { - COFF_SectionHeader *coff_sec_ptr = sectab; - COFF_SectionHeader *coff_ptr_opl = sectab + sectab_count; - for (;coff_sec_ptr < coff_ptr_opl; coff_sec_ptr += 1) { - U64 sec_voff_max = coff_sec_ptr->voff + coff_sec_ptr->vsize; - exe_voff_max = Max(exe_voff_max, sec_voff_max); - } - } - - RDIM_TopLevelInfo top_level_info = {0}; - top_level_info.arch = arch; - top_level_info.exe_name = str8_skip_last_slash(image_name); - top_level_info.exe_hash = exe_hash; - top_level_info.voff_max = exe_voff_max; - top_level_info.producer_name = str8_lit(BUILD_TITLE_STRING_LITERAL); - - return top_level_info; -} diff --git a/src/radcon/radcon_coff.h b/src/radcon/radcon_coff.h index abed4aef..57cf7a47 100644 --- a/src/radcon/radcon_coff.h +++ b/src/radcon/radcon_coff.h @@ -7,7 +7,6 @@ internal RDI_Arch c2r_rdi_arch_from_coff_machine(COFF_MachineType machine); internal RDI_BinarySectionFlags c2r_rdi_binary_section_flags_from_coff_section_flags(COFF_SectionFlags flags); internal RDIM_BinarySectionList c2r_rdi_binary_sections_from_coff_sections(Arena *arena, String8 image_data, U64 string_table_off, U64 sectab_count, COFF_SectionHeader *sectab); -internal RDIM_TopLevelInfo c2r_make_rdim_top_level_info(String8 image_name, RDI_Arch arch, U64 exe_hash, U64 sectab_count, COFF_SectionHeader *sectab); #endif // RADCON_COFF_H diff --git a/src/radcon/radcon_dwarf.c b/src/radcon/radcon_dwarf.c index 02f95aa7..98ad228c 100644 --- a/src/radcon/radcon_dwarf.c +++ b/src/radcon/radcon_dwarf.c @@ -32,12 +32,12 @@ d2r_rdi_reg_from_dw_reg_code_x86(U64 reg_code) } internal RDI_RegCode -d2r_rdi_reg_from_dw_reg_code(RDI_Arch arch, U64 reg_code) +d2r_rdi_reg_from_dw_reg_code(Arch arch, U64 reg_code) { switch (arch) { - case RDI_Arch_NULL: return 0; - case RDI_Arch_X64: return d2r_rdi_reg_from_dw_reg_code_x64(reg_code); - case RDI_Arch_X86: return d2r_rdi_reg_from_dw_reg_code_x86(reg_code); + case Arch_Null: return 0; + case Arch_x64: return d2r_rdi_reg_from_dw_reg_code_x64(reg_code); + case Arch_x86: return d2r_rdi_reg_from_dw_reg_code_x86(reg_code); } InvalidPath; return 0; @@ -243,7 +243,7 @@ d2r_bytecode_from_expression(Arena *arena, DW_Input *input, U64 image_base, U64 address_size, - RDI_Arch arch, + Arch arch, DW_ListUnit *addr_lu, String8 expr, DW_CompUnit *cu, @@ -821,7 +821,7 @@ d2r_bytecode_from_expression(Arena *arena, } internal RDIM_Location * -d2r_transpile_expression(Arena *arena, DW_Input *input, U64 image_base, U64 address_size, RDI_Arch arch, DW_ListUnit *addr_lu, DW_CompUnit *cu, String8 expr) +d2r_transpile_expression(Arena *arena, DW_Input *input, U64 image_base, U64 address_size, Arch arch, DW_ListUnit *addr_lu, DW_CompUnit *cu, String8 expr) { RDIM_Location *loc = 0; if (expr.size) { @@ -836,7 +836,7 @@ d2r_transpile_expression(Arena *arena, DW_Input *input, U64 image_base, U64 addr } internal RDIM_Location * -d2r_location_from_attrib(Arena *arena, DW_Input *input, DW_CompUnit *cu, U64 image_base, RDI_Arch arch, DW_Tag tag, DW_AttribKind kind) +d2r_location_from_attrib(Arena *arena, DW_Input *input, DW_CompUnit *cu, U64 image_base, Arch arch, DW_Tag tag, DW_AttribKind kind) { String8 expr = dw_exprloc_from_attrib(input, cu, tag, kind); RDIM_Location *location = d2r_transpile_expression(arena, input, image_base, cu->address_size, arch, cu->addr_lu, cu, expr); @@ -850,7 +850,7 @@ d2r_locset_from_attrib(Arena *arena, RDIM_ScopeChunkList *scopes, RDIM_Scope *curr_scope, U64 image_base, - RDI_Arch arch, + Arch arch, DW_Tag tag, DW_AttribKind kind) { @@ -897,7 +897,7 @@ d2r_var_locset_from_tag(Arena *arena, RDIM_ScopeChunkList *scopes, RDIM_Scope *curr_scope, U64 image_base, - RDI_Arch arch, + Arch arch, DW_Tag tag) { RDIM_LocationSet locset = {0}; @@ -1086,60 +1086,46 @@ d2r_convert(Arena *arena, RDIM_LocalState *local_state, RC_Context *in) //////////////////////////////// - RDI_Arch arch = RDI_Arch_NULL; + Arch arch = Arch_Null; + U64 image_base = 0; RDIM_BinarySectionList binary_sections = {0}; - RDIM_TopLevelInfo top_level_info = {0}; - - U64 image_base = 0; - DW_Input input = {0}; - DW_ListUnitInput lui = {0}; + DW_Input input = {0}; if (in->image == Image_CoffPe) { PE_BinInfo pe = pe_bin_info_from_data(scratch.arena, in->image_data); - // convert arch - switch (pe.arch) { - case Arch_Null: arch = RDI_Arch_NULL; break; - case Arch_x64: arch = RDI_Arch_X64; break; - case Arch_x86: arch = RDI_Arch_X86; break; - default: NotImplemented; break; - } + // get image arch + arch = pe.arch; // get image base image_base = pe.image_base; - // get COFF sections + // get image sections String8 raw_sections = str8_substr(in->image_data, rng_1u64(pe.section_array_off, pe.section_array_off+sizeof(COFF_SectionHeader)*pe.section_count)); U64 section_count = raw_sections.size / sizeof(COFF_SectionHeader); COFF_SectionHeader *section_array = (COFF_SectionHeader *)raw_sections.str; - // convert sections & top level info + // convert sections binary_sections = c2r_rdi_binary_sections_from_coff_sections(arena, in->image_data, pe.string_table_off, section_count, section_array); - top_level_info = c2r_make_rdim_top_level_info(in->image_name, arch, exe_hash, section_count, section_array); - // find DWARF sections + // make DWARF input input = dw_input_from_coff_section_table(scratch.arena, in->image_data, pe.string_table_off, section_count, section_array); } else if (in->image == Image_Elf32 || in->image == Image_Elf64) { ELF_BinInfo elf = elf_bin_from_data(in->debug_data); + // get image arch + arch = arch_from_elf_machine(elf.hdr.e_machine); + // get image base image_base = elf_base_addr_from_bin(&elf.hdr); - // convert arch - switch (elf.hdr.e_machine) { - case ELF_MachineKind_None: arch = RDI_Arch_NULL; break; - case ELF_MachineKind_X86_64: arch = RDI_Arch_X64; break; - case ELF_MachineKind_386: arch = RDI_Arch_X86; break; - default: NotImplemented; break; - } - + // get image sections ELF_Shdr64Array shdrs = elf_shdr64_array_from_bin(scratch.arena, in->debug_data, &elf.hdr); - // convert sections & top level info - binary_sections = e2r_rdi_binary_sections_from_elf_section_table(arena, shdrs); - top_level_info = e2r_make_rdim_top_level_info(in->debug_data, exe_hash, shdrs); + // convert sections + binary_sections = e2r_rdi_binary_sections_from_elf_section_table(arena, shdrs); - // find DWARF sections + // make DWARF input input = dw_input_from_elf_section_table(scratch.arena, in->debug_data, &elf); } else { InvalidPath; @@ -1147,6 +1133,10 @@ d2r_convert(Arena *arena, RDIM_LocalState *local_state, RC_Context *in) //////////////////////////////// + RDIM_TopLevelInfo top_level_info = rdim_make_top_level_info(in->image_name, arch, exe_hash, binary_sections); + + //////////////////////////////// + U64 arch_addr_size = rdi_addr_size_from_arch(arch); //////////////////////////////// diff --git a/src/radcon/radcon_elf.c b/src/radcon/radcon_elf.c index 6a262ee2..d5ab8333 100644 --- a/src/radcon/radcon_elf.c +++ b/src/radcon/radcon_elf.c @@ -8,10 +8,3 @@ e2r_rdi_binary_sections_from_elf_section_table(Arena *arena, ELF_Shdr64Array shd return result; } -internal RDIM_TopLevelInfo -e2r_make_rdim_top_level_info(String8 image_name, RDI_Arch arch, ELF_Shdr64Array shdrs) -{ - RDIM_TopLevelInfo top_level_info = {0}; - return top_level_info; -} - diff --git a/src/radcon/radcon_elf.h b/src/radcon/radcon_elf.h index bcf40e08..dadb2ae2 100644 --- a/src/radcon/radcon_elf.h +++ b/src/radcon/radcon_elf.h @@ -5,6 +5,5 @@ #define RADCON_ELF_H internal RDIM_BinarySectionList e2r_rdi_binary_sections_from_elf_section_table(Arena *arena, ELF_Shdr64Array shdrs); -internal RDIM_TopLevelInfo e2r_make_rdim_top_level_info(String8 image_name, RDI_Arch arch, ELF_Shdr64Array shdrs); #endif // RADCON_ELF_H diff --git a/src/radcon/radcon_pdb.c b/src/radcon/radcon_pdb.c index 4b395510..4a24ed43 100644 --- a/src/radcon/radcon_pdb.c +++ b/src/radcon/radcon_pdb.c @@ -2363,15 +2363,15 @@ p2r_convert(Arena *arena, RDIM_LocalState *local_state, RC_Context *in) // U64 exe_hash = *async_task_join_struct(exe_hash_task, U64); - ////////////////////////////////////////////////////////////// - //- rjf: produce top-level-info - // - RDIM_TopLevelInfo top_level_info = c2r_make_rdim_top_level_info(in->image_name, arch, exe_hash, coff_sections.count, coff_sections.v); - ////////////////////////////////////////////////////////////// //- rjf: build binary sections list // RDIM_BinarySectionList binary_sections = c2r_rdi_binary_sections_from_coff_sections(arena, str8_zero(), 0, coff_sections.count, coff_sections.v); + + ////////////////////////////////////////////////////////////// + //- rjf: produce top-level-info + // + RDIM_TopLevelInfo top_level_info = rdim_make_top_level_info(in->image_name, arch_from_coff_machine(dbi->machine_type), exe_hash, binary_sections); ////////////////////////////////////////////////////////////// //- rjf: kick off unit conversion & source file collection diff --git a/src/rdi_make/rdi_make_local.c b/src/rdi_make/rdi_make_local.c index df463138..3de6d4af 100644 --- a/src/rdi_make/rdi_make_local.c +++ b/src/rdi_make/rdi_make_local.c @@ -37,6 +37,39 @@ rdim_infer_data_model(OperatingSystem os, RDI_Arch arch) return data_model; } +//////////////////////////////// + +internal RDIM_TopLevelInfo +rdim_make_top_level_info(String8 image_name, Arch arch, U64 exe_hash, RDIM_BinarySectionList sections) +{ + // convert arch + RDI_Arch arch_rdi; + switch (arch) { + case Arch_Null: arch_rdi = RDI_Arch_NULL; break; + case Arch_x64: arch_rdi = RDI_Arch_X64; break; + case Arch_x86: arch_rdi = RDI_Arch_X86; break; + default: NotImplemented; break; + } + + + // find max VOFF + U64 exe_voff_max = 0; + for (RDIM_BinarySectionNode *sect_n = sections.first; sect_n != 0 ; sect_n = sect_n->next) { + exe_voff_max = Max(exe_voff_max, sect_n->v.voff_opl); + } + + + // fill out top level info + RDIM_TopLevelInfo top_level_info = {0}; + top_level_info.arch = arch_rdi; + top_level_info.exe_hash = exe_hash; + top_level_info.voff_max = exe_voff_max; + top_level_info.producer_name = str8_lit(BUILD_TITLE_STRING_LITERAL); + + + return top_level_info; +} + //////////////////////////////// //~ rjf: Baking Stage Tasks diff --git a/src/rdi_make/rdi_make_local.h b/src/rdi_make/rdi_make_local.h index f8bffbfa..290265b8 100644 --- a/src/rdi_make/rdi_make_local.h +++ b/src/rdi_make/rdi_make_local.h @@ -384,6 +384,11 @@ global RDIM_LocalState *rdim_local_state = 0; //////////////////////////////// +internal RDIM_DataModel rdim_infer_data_model(OperatingSystem os, RDI_Arch arch); +internal RDIM_TopLevelInfo rdim_make_top_level_info(String8 image_name, Arch arch, U64 exe_hash, RDIM_BinarySectionList sections); + +//////////////////////////////// + internal RDIM_LocalState * rdim_local_init(void); internal RDIM_BakeResults rdim_bake(RDIM_LocalState *state, RDIM_BakeParams *in); internal RDIM_SerializedSectionBundle rdim_compress(Arena *arena, RDIM_SerializedSectionBundle *in);