From 3534100e8d59fd8f78489337e72e1b82fd928780 Mon Sep 17 00:00:00 2001 From: Nikita Smith Date: Wed, 17 Jun 2026 16:56:22 -0700 Subject: [PATCH] lazy touch obj string tables --- src/linker/lnk_debug_info.c | 5 +++-- src/linker/lnk_obj.c | 42 ++++++++++++++++++++++++------------- src/linker/lnk_obj.h | 3 ++- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/linker/lnk_debug_info.c b/src/linker/lnk_debug_info.c index 83f57496..3b6eabb4 100644 --- a/src/linker/lnk_debug_info.c +++ b/src/linker/lnk_debug_info.c @@ -3074,14 +3074,15 @@ THREAD_POOL_TASK_FUNC(lnk_push_dbi_sec_contrib_task) PDB_DbiSectionContribNode *sc_arr = push_array_no_zero(arena, PDB_DbiSectionContribNode, obj->header.section_count_no_null); U64 sc_count = 0; - for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) { + for EachIndex(sect_idx, obj->header.section_count_no_null) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); if (*section.flags & COFF_SectionFlag_LnkInfo) { continue; } if (*section.flags & COFF_SectionFlag_LnkRemove) { continue; } if (*section.flags & LNK_SECTION_FLAG_DEBUG) { continue; } - if (str8_match(section.name, str8_lit(".pdata"), 0)) { continue; } + String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); + if (str8_match(section_name, str8_lit(".pdata"), 0)) { continue; } U64 sect_number; String8 sect_data; diff --git a/src/linker/lnk_obj.c b/src/linker/lnk_obj.c index dabd77f8..c2f9649e 100644 --- a/src/linker/lnk_obj.c +++ b/src/linker/lnk_obj.c @@ -351,12 +351,12 @@ THREAD_POOL_TASK_FUNC(lnk_obj_find_debug_t) { LNK_Obj *obj = &((LNK_ObjNode *)raw_task)[task_id].data; for EachIndex(sect_idx, obj->header.section_count_no_null) { - LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); - if (str8_match(section.name, str8_lit(".debug$T"), 0)) { + String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); + if (str8_match(section_name, str8_lit(".debug$T"), 0)) { obj->debug_t_sect_idx = sect_idx; - } else if (str8_match(section.name, str8_lit(".debug$P"), 0)) { + } else if (str8_match(section_name, str8_lit(".debug$P"), 0)) { obj->debug_p_sect_idx = sect_idx; - } else if (str8_match(section.name, str8_lit(".debug$H"), 0)) { + } else if (str8_match(section_name, str8_lit(".debug$H"), 0)) { obj->debug_h_sect_idx = sect_idx; } } @@ -570,6 +570,20 @@ lnk_obj_section_number_from_sect_idx(LNK_Obj *obj, U64 sect_idx) return sect_idx+1; } +internal String8 +lnk_obj_section_name_from_sect_idx(LNK_Obj *obj, U64 sect_idx) +{ + COFF_SectionHeader *section_header = &lnk_coff_section_table_from_obj(obj)[sect_idx]; + return coff_name_from_section_header(lnk_coff_string_table_from_obj(obj), section_header); +} + +internal String8 +lnk_obj_section_name_from_section_number(LNK_Obj *obj, U64 section_number) +{ + Assert(section_number > 0); + return lnk_obj_section_name_from_sect_idx(obj, section_number-1); +} + internal LNK_ObjSection lnk_obj_section_from_sect_idx(LNK_Obj *obj, U64 sect_idx) { @@ -580,7 +594,6 @@ lnk_obj_section_from_sect_idx(LNK_Obj *obj, U64 sect_idx) section.section_number = sect_idx+1; section.header = &lnk_coff_section_table_from_obj(obj)[sect_idx]; section.flags = &obj->section_flags[sect_idx]; - section.name = coff_name_from_section_header(lnk_coff_string_table_from_obj(obj), section.header); section.vrange = rng_1u64(section.header->voff, section.header->voff + section.header->vsize); section.frange = rng_1u64(section.header->foff, section.header->foff + section.header->fsize); section.reloc_count = section.header->reloc_count; @@ -669,16 +682,15 @@ THREAD_POOL_TASK_FUNC(lnk_collect_obj_chunks_task) LNK_SectionCollector *task = raw_task; LNK_Obj *obj = task->objs[task_id]; - for (U32 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) { + for EachIndex(sect_idx, obj->header.section_count_no_null) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); - if (*section.flags & COFF_SectionFlag_LnkRemove) { - if (!task->collect_discarded) { - continue; - } + if (*section.flags & COFF_SectionFlag_LnkRemove && !task->collect_discarded) { + continue; } - if (str8_match(section.name, task->name, 0)) { + String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); + if (str8_match(section_name, task->name, 0)) { String8 section_data = str8_substr(obj->data, section.frange); str8_list_push(arena, &task->out_lists[task_id], section_data); } @@ -758,7 +770,8 @@ lnk_raw_directives_from_obj(Arena *arena, LNK_Obj *obj) for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); if (*section.flags & COFF_SectionFlag_LnkInfo) { - if (str8_match(section.name, str8_lit(".drectve"), 0)) { + String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); + if (str8_match(section_name, str8_lit(".drectve"), 0)) { if (*section.flags & COFF_SectionFlag_CntUninitializedData) { lnk_error_obj(LNK_Error_IllData, obj, ".drectve section header has flag COFF_SectionFlag_CntUninitializedData"); break; @@ -796,8 +809,9 @@ lnk_debug_s_from_obj(Arena *arena, LNK_Obj *obj) String8List raw_debug_s = {0}; { for EachIndex(sect_idx, obj->header.section_count_no_null) { - LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); - if (str8_match(section.name, str8_lit(".debug$S"), 0)) { + LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); + String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); + if (str8_match(section_name, str8_lit(".debug$S"), 0)) { String8 debug_s = str8_substr(obj->data, section.frange); str8_list_push(scratch.arena, &raw_debug_s, debug_s); } diff --git a/src/linker/lnk_obj.h b/src/linker/lnk_obj.h index d13a3d1e..9a21de59 100644 --- a/src/linker/lnk_obj.h +++ b/src/linker/lnk_obj.h @@ -47,7 +47,6 @@ typedef struct LNK_ObjSection U64 section_number; COFF_SectionHeader *header; COFF_SectionFlags *flags; - String8 name; Rng1U64 vrange; Rng1U64 frange; U32 reloc_count; @@ -148,6 +147,8 @@ internal COFF_SectionHeader * lnk_coff_section_header_from_section_number(LNK_Ob internal COFF_ParsedSymbol lnk_parsed_symbol_from_coff_symbol_idx(LNK_Obj *obj, U64 symbol_idx); internal U64 lnk_obj_sect_idx_from_section_number(LNK_Obj *obj, U64 section_number); internal U64 lnk_obj_section_number_from_sect_idx(LNK_Obj *obj, U64 sect_idx); +internal String8 lnk_obj_section_name_from_section_number(LNK_Obj *obj, U64 section_number); +internal String8 lnk_obj_section_name_from_sect_idx(LNK_Obj *obj, U64 sect_idx); internal LNK_ObjSection lnk_obj_section_from_sect_idx(LNK_Obj *obj, U64 sect_idx); internal LNK_ObjSection lnk_obj_section_from_section_number(LNK_Obj *obj, U64 section_number); internal COFF_RelocArray lnk_coff_relocs_from_section_header(LNK_Obj *obj, COFF_SectionHeader *section_header);