sort contributions after section merges

This commit is contained in:
Nikita Smith committed 2026-08-24 22:27:27 -07:00
1 parent ad8fe1c61e
commit 0a328898d0
2 files changed
+32 -2

No files matched your search

+9 -2
View File
@@ -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, &sect->contribs, sect_defn->contribs_count, sort_idx);
contrib_chunk = lnk_section_contrib_chunk_list_push_chunk(main_arena, &sect->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(&sect_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(&sect_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");
}
+23
View File
@@ -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");