Skip to content

Commit ae35819

Browse files
author
Peter Zijlstra
committed
objtool: Optimize find_section_by_name()
In order to avoid yet another linear search of (20k) sections, add a name based hash. This reduces objtool runtime on vmlinux.o by some 10s to around 35s. 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 5303899 commit ae35819

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

tools/objtool/elf.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222

2323
#define MAX_NAME_LEN 128
2424

25+
static inline u32 str_hash(const char *str)
26+
{
27+
return jhash(str, strlen(str), 0);
28+
}
29+
2530
struct section *find_section_by_name(struct elf *elf, const char *name)
2631
{
2732
struct section *sec;
2833

29-
list_for_each_entry(sec, &elf->sections, list)
34+
hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
3035
if (!strcmp(sec->name, name))
3136
return sec;
3237

@@ -202,6 +207,7 @@ static int read_sections(struct elf *elf)
202207

203208
list_add_tail(&sec->list, &elf->sections);
204209
hash_add(elf->section_hash, &sec->hash, sec->idx);
210+
hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
205211
}
206212

207213
if (stats)
@@ -441,6 +447,7 @@ struct elf *elf_read(const char *name, int flags)
441447

442448
hash_init(elf->symbol_hash);
443449
hash_init(elf->section_hash);
450+
hash_init(elf->section_name_hash);
444451
INIT_LIST_HEAD(&elf->sections);
445452

446453
elf->fd = open(name, flags);
@@ -581,6 +588,7 @@ struct section *elf_create_section(struct elf *elf, const char *name,
581588

582589
list_add_tail(&sec->list, &elf->sections);
583590
hash_add(elf->section_hash, &sec->hash, sec->idx);
591+
hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
584592

585593
return sec;
586594
}

tools/objtool/elf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <gelf.h>
1111
#include <linux/list.h>
1212
#include <linux/hashtable.h>
13+
#include <linux/jhash.h>
1314

1415
#ifdef LIBELF_USE_DEPRECATED
1516
# define elf_getshdrnum elf_getshnum
@@ -26,6 +27,7 @@
2627
struct section {
2728
struct list_head list;
2829
struct hlist_node hash;
30+
struct hlist_node name_hash;
2931
GElf_Shdr sh;
3032
struct list_head symbol_list;
3133
struct list_head rela_list;
@@ -73,6 +75,7 @@ struct elf {
7375
struct list_head sections;
7476
DECLARE_HASHTABLE(symbol_hash, 20);
7577
DECLARE_HASHTABLE(section_hash, 16);
78+
DECLARE_HASHTABLE(section_name_hash, 16);
7679
};
7780

7881

0 commit comments

Comments
 (0)