Skip to content

Commit cdb3d05

Browse files
author
Peter Zijlstra
committed
objtool: Optimize find_symbol_by_name()
Perf showed that find_symbol_by_name() takes time; add a symbol name hash. This shaves another second off of objtool on vmlinux.o runtime, down to 15 seconds. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 513b5ca commit cdb3d05

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

tools/objtool/elf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,11 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset)
203203

204204
struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
205205
{
206-
struct section *sec;
207206
struct symbol *sym;
208207

209-
list_for_each_entry(sec, &elf->sections, list)
210-
list_for_each_entry(sym, &sec->symbol_list, list)
211-
if (!strcmp(sym->name, name))
212-
return sym;
208+
hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
209+
if (!strcmp(sym->name, name))
210+
return sym;
213211

214212
return NULL;
215213
}
@@ -386,6 +384,7 @@ static int read_symbols(struct elf *elf)
386384
entry = &sym->sec->symbol_list;
387385
list_add(&sym->list, entry);
388386
hash_add(elf->symbol_hash, &sym->hash, sym->idx);
387+
hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
389388
}
390389

391390
if (stats)
@@ -524,6 +523,7 @@ struct elf *elf_read(const char *name, int flags)
524523
memset(elf, 0, sizeof(*elf));
525524

526525
hash_init(elf->symbol_hash);
526+
hash_init(elf->symbol_name_hash);
527527
hash_init(elf->section_hash);
528528
hash_init(elf->section_name_hash);
529529
INIT_LIST_HEAD(&elf->sections);

tools/objtool/elf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct symbol {
4747
struct list_head list;
4848
struct rb_node node;
4949
struct hlist_node hash;
50+
struct hlist_node name_hash;
5051
GElf_Sym sym;
5152
struct section *sec;
5253
char *name;
@@ -77,6 +78,7 @@ struct elf {
7778
char *name;
7879
struct list_head sections;
7980
DECLARE_HASHTABLE(symbol_hash, 20);
81+
DECLARE_HASHTABLE(symbol_name_hash, 20);
8082
DECLARE_HASHTABLE(section_hash, 16);
8183
DECLARE_HASHTABLE(section_name_hash, 16);
8284
};

0 commit comments

Comments
 (0)