Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

objtool: Fix up st_info in COMDAT group section

When __elf_create_symbol creates a local symbol, it relocates the first
global symbol upwards to make space. Subsequently, elf_update_symbol()
is called to refresh the symbol table section. However, this isn't
sufficient, as other sections might have the reference to the old
symbol index, for instance, the sh_info field of an SHT_GROUP section.

This patch updates the `sh_info` field when necessary. This field
serves as the key for the COMDAT group. An incorrect key would prevent
the linker's from deduplicating COMDAT symbols, leading to duplicate
definitions in the final link.

Signed-off-by: Rong Xu <xur@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20250425200541.113015-1-xur@google.com

authored by

Rong Xu and committed by
Peter Zijlstra
2cb29159 b4432656

+26 -1
+26 -1
tools/objtool/elf.c
··· 573 573 } 574 574 575 575 /* 576 + * @sym's idx has changed. Update the sh_info in group sections. 577 + */ 578 + static void elf_update_group_sh_info(struct elf *elf, Elf32_Word symtab_idx, 579 + Elf32_Word new_idx, Elf32_Word old_idx) 580 + { 581 + struct section *sec; 582 + 583 + list_for_each_entry(sec, &elf->sections, list) { 584 + if (sec->sh.sh_type != SHT_GROUP) 585 + continue; 586 + if (sec->sh.sh_link == symtab_idx && 587 + sec->sh.sh_info == old_idx) { 588 + sec->sh.sh_info = new_idx; 589 + mark_sec_changed(elf, sec, true); 590 + /* 591 + * Each ELF group should have a unique symbol key. 592 + * Return early on match. 593 + */ 594 + return; 595 + } 596 + } 597 + } 598 + 599 + /* 576 600 * @sym's idx has changed. Update the relocs which reference it. 577 601 */ 578 602 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) ··· 769 745 770 746 /* 771 747 * Move the first global symbol, as per sh_info, into a new, higher 772 - * symbol index. This fees up a spot for a new local symbol. 748 + * symbol index. This frees up a spot for a new local symbol. 773 749 */ 774 750 first_non_local = symtab->sh.sh_info; 775 751 old = find_symbol_by_index(elf, first_non_local); ··· 787 763 if (elf_update_sym_relocs(elf, old)) 788 764 return NULL; 789 765 766 + elf_update_group_sh_info(elf, symtab->idx, new_idx, first_non_local); 790 767 new_idx = first_non_local; 791 768 } 792 769