Skip to content

Commit

Permalink
objtool: Optimize find_section_by_index()
Browse files Browse the repository at this point in the history
In order to avoid a linear search (over 20k entries), add an
section_hash to the elf object.

This reduces objtool on vmlinux.o from a few minutes to around 45
seconds.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lkml.kernel.org/r/20200324160924.381249993@infradead.org
  • Loading branch information
Peter Zijlstra committed Mar 25, 2020
1 parent 1e11f3f commit 5303899
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tools/objtool/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static struct section *find_section_by_index(struct elf *elf,
{
struct section *sec;

list_for_each_entry(sec, &elf->sections, list)
hash_for_each_possible(elf->section_hash, sec, hash, idx)
if (sec->idx == idx)
return sec;

Expand Down Expand Up @@ -166,8 +166,6 @@ static int read_sections(struct elf *elf)
INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);

list_add_tail(&sec->list, &elf->sections);

s = elf_getscn(elf->elf, i);
if (!s) {
WARN_ELF("elf_getscn");
Expand Down Expand Up @@ -201,6 +199,9 @@ static int read_sections(struct elf *elf)
}
}
sec->len = sec->sh.sh_size;

list_add_tail(&sec->list, &elf->sections);
hash_add(elf->section_hash, &sec->hash, sec->idx);
}

if (stats)
Expand Down Expand Up @@ -439,6 +440,7 @@ struct elf *elf_read(const char *name, int flags)
memset(elf, 0, sizeof(*elf));

hash_init(elf->symbol_hash);
hash_init(elf->section_hash);
INIT_LIST_HEAD(&elf->sections);

elf->fd = open(name, flags);
Expand Down Expand Up @@ -501,8 +503,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);

list_add_tail(&sec->list, &elf->sections);

s = elf_newscn(elf->elf);
if (!s) {
WARN_ELF("elf_newscn");
Expand Down Expand Up @@ -579,6 +579,9 @@ struct section *elf_create_section(struct elf *elf, const char *name,
shstrtab->len += strlen(name) + 1;
shstrtab->changed = true;

list_add_tail(&sec->list, &elf->sections);
hash_add(elf->section_hash, &sec->hash, sec->idx);

return sec;
}

Expand Down
2 changes: 2 additions & 0 deletions tools/objtool/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

struct section {
struct list_head list;
struct hlist_node hash;
GElf_Shdr sh;
struct list_head symbol_list;
struct list_head rela_list;
Expand Down Expand Up @@ -71,6 +72,7 @@ struct elf {
char *name;
struct list_head sections;
DECLARE_HASHTABLE(symbol_hash, 20);
DECLARE_HASHTABLE(section_hash, 16);
};


Expand Down

0 comments on commit 5303899

Please sign in to comment.