Skip to content

Commit 2cb2915

Browse files
xur-llvmPeter Zijlstra
authored andcommitted
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
1 parent b443265 commit 2cb2915

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tools/objtool/elf.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,30 @@ static int read_symbols(struct elf *elf)
572572
return -1;
573573
}
574574

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+
575599
/*
576600
* @sym's idx has changed. Update the relocs which reference it.
577601
*/
@@ -745,7 +769,7 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym)
745769

746770
/*
747771
* Move the first global symbol, as per sh_info, into a new, higher
748-
* symbol index. This fees up a spot for a new local symbol.
772+
* symbol index. This frees up a spot for a new local symbol.
749773
*/
750774
first_non_local = symtab->sh.sh_info;
751775
old = find_symbol_by_index(elf, first_non_local);
@@ -763,6 +787,7 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym)
763787
if (elf_update_sym_relocs(elf, old))
764788
return NULL;
765789

790+
elf_update_group_sh_info(elf, symtab->idx, new_idx, first_non_local);
766791
new_idx = first_non_local;
767792
}
768793

0 commit comments

Comments
 (0)