Skip to content

Commit 4ed9d82

Browse files
jpoimboePeter Zijlstra
authored andcommitted
objtool: Speed up SHT_GROUP reindexing
After elf_update_group_sh_info() was introduced, a prototype version of "objtool klp diff" went from taking ~1s to several minutes, due to looping almost endlessly in elf_update_group_sh_info() while creating thousands of local symbols in a file with thousands of sections. Dramatically improve the performance by marking all symbols' correlated SHT_GROUP sections while reading the object. That way there's no need to search for it every time a symbol gets reindexed. Fixes: 2cb2915 ("objtool: Fix up st_info in COMDAT group section") Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Rong Xu <xur@google.com> Link: https://lkml.kernel.org/r/2a33e583c87e3283706f346f9d59aac20653b7fd.1746662991.git.jpoimboe@kernel.org
1 parent 2cb2915 commit 4ed9d82

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

tools/objtool/elf.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -572,28 +572,32 @@ 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)
575+
static int mark_group_syms(struct elf *elf)
580576
{
581-
struct section *sec;
577+
struct section *symtab, *sec;
578+
struct symbol *sym;
579+
580+
symtab = find_section_by_name(elf, ".symtab");
581+
if (!symtab) {
582+
ERROR("no .symtab");
583+
return -1;
584+
}
582585

583586
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;
587+
if (sec->sh.sh_type == SHT_GROUP &&
588+
sec->sh.sh_link == symtab->idx) {
589+
sym = find_symbol_by_index(elf, sec->sh.sh_info);
590+
if (!sym) {
591+
ERROR("%s: can't find SHT_GROUP signature symbol",
592+
sec->name);
593+
return -1;
594+
}
595+
596+
sym->group_sec = sec;
595597
}
596598
}
599+
600+
return 0;
597601
}
598602

599603
/*
@@ -787,7 +791,11 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym)
787791
if (elf_update_sym_relocs(elf, old))
788792
return NULL;
789793

790-
elf_update_group_sh_info(elf, symtab->idx, new_idx, first_non_local);
794+
if (old->group_sec) {
795+
old->group_sec->sh.sh_info = new_idx;
796+
mark_sec_changed(elf, old->group_sec, true);
797+
}
798+
791799
new_idx = first_non_local;
792800
}
793801

@@ -1060,6 +1068,9 @@ struct elf *elf_open_read(const char *name, int flags)
10601068
if (read_symbols(elf))
10611069
goto err;
10621070

1071+
if (mark_group_syms(elf))
1072+
goto err;
1073+
10631074
if (read_relocs(elf))
10641075
goto err;
10651076

tools/objtool/include/objtool/elf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct symbol {
7272
u8 ignore : 1;
7373
struct list_head pv_target;
7474
struct reloc *relocs;
75+
struct section *group_sec;
7576
};
7677

7778
struct reloc {

0 commit comments

Comments
 (0)