From 0a328898d000f4f75071e0886915a64b0c2f439a Mon Sep 17 00:00:00 2001 From: Nikita Smith Date: Fri, 21 Aug 2026 04:07:56 -0700 Subject: [PATCH] sort contributions after section merges --- src/linker/lnk.c | 11 +++++++++-- src/linker/tests/linker_tests.c | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/linker/lnk.c b/src/linker/lnk.c index 543a4be5..e74af162 100644 --- a/src/linker/lnk.c +++ b/src/linker/lnk.c @@ -3899,7 +3899,7 @@ THREAD_POOL_TASK_FUNC(lnk_gather_sections_task) String8 defn_name_with_flags = lnk_make_name_with_flags(sectab->arena, sect_defn->name, sect_defn->flags); LNK_SectionContribChunk *contrib_chunk = hash_table_search_string_raw(task->contribs_ht, defn_name_with_flags); if (!contrib_chunk) { - contrib_chunk = lnk_section_contrib_chunk_list_push_chunk(main_arena, §->contribs, sect_defn->contribs_count, sort_idx); + contrib_chunk = lnk_section_contrib_chunk_list_push_chunk(main_arena, §->contribs, sect_defn->contribs_count, sect_defn->name); hash_table_push_string_raw(sectab->arena, task->contribs_ht, defn_name_with_flags, contrib_chunk); } @@ -5654,7 +5654,7 @@ lnk_build_image(TP_Arena *arena, TP_Context *tp, LNK_Config *config, LNK_SymbolT } // append common block's contribution - LNK_SectionContribChunk *common_block_chunk = lnk_section_contrib_chunk_list_push_chunk(sectab->arena, &common_block_sect->contribs, 1, str8(0,0)); + LNK_SectionContribChunk *common_block_chunk = lnk_section_contrib_chunk_list_push_chunk(sectab->arena, &common_block_sect->contribs, 1, str8_lit(".bss")); LNK_SectionContrib *common_block_sc = lnk_section_contrib_chunk_push(common_block_chunk, 1); common_block_sc->u.obj_idx = max_U32; common_block_sc->u.obj_section_number = 0; @@ -5675,6 +5675,7 @@ lnk_build_image(TP_Arena *arena, TP_Context *tp, LNK_Config *config, LNK_SymbolT // Grouped Sections (PE Format) // "All contributions with the same object-section name are allocated contiguously in the image, // and the blocks of contributions are sorted in lexical order by object-section name." + // Preserve each source section's contribution range across the merge. ProfBegin("Sort Sections"); for (LNK_SectionNode *sect_n = sectab->list.first; sect_n != 0; sect_n = sect_n->next) { lnk_sort_section_contribs(§_n->data); @@ -5686,6 +5687,12 @@ lnk_build_image(TP_Arena *arena, TP_Context *tp, LNK_Config *config, LNK_SymbolT lnk_section_table_merge(sectab, config->merge_list); } + ProfBegin("Sort Merged Sections"); + for (LNK_SectionNode *sect_n = sectab->list.first; sect_n != 0; sect_n = sect_n->next) { + lnk_sort_section_contribs(§_n->data); + } + ProfEnd(); + if (config->do_function_pad_min == LNK_SwitchState_Yes) { tp_for_parallel_prof(tp, arena, objs_count, lnk_flag_hotpatch_contribs_task, &task, "Flag Hotpatch Section Contribs"); } diff --git a/src/linker/tests/linker_tests.c b/src/linker/tests/linker_tests.c index 6a4b6b35..98203a2e 100644 --- a/src/linker/tests/linker_tests.c +++ b/src/linker/tests/linker_tests.c @@ -728,6 +728,29 @@ TEST(merge) t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe /merge:.qwe=.test entry.obj test.obj"); T_Ok(g_last_exit_code == 0); + // merged contribution groups retain lexical order + { + T_Ok(t_write_def_obj("order.obj", (T_COFF_DefObj){ + .machine = T_COFF_DefSetMachine(X64), + .sections = (T_COFF_DefSection[]){ + { "a", ".a$m", str8_lit_comp("a"), .flags = "rw:data@1" }, + { "z", ".z$m", str8_lit_comp("z"), .flags = "rw:data@1" }, + {0} + } + })); + + t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe /merge:.a=.z entry.obj order.obj"); + T_Ok(g_last_exit_code == 0); + + String8 exe = t_read_file(arena, str8_lit("a.exe")); + PE_BinInfo pe = pe_bin_info_from_data(arena, exe); + COFF_SectionHeader *section_table = (COFF_SectionHeader *)str8_substr(exe, pe.section_table_range).str; + COFF_SectionHeader *sect = coff_section_header_from_name(exe, section_table, pe.section_count, str8_lit(".z")); + T_Ok(sect != 0); + String8 data = str8_substr(exe, rng_1u64(sect->foff, sect->foff + 2)); + T_Ok(str8_match(data, str8_lit("az"), 0)); + } + // merge .test -> .qwe -> .data { t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe /merge:.test=.qwe /merge:.qwe=.data entry.obj test.obj");