Skip to content

Commit 1952671

Browse files
author
Peter Zijlstra
committed
objtool: Optimize elf_dirty_reloc_sym()
When moving a symbol in the symtab its index changes and any reloc referring that symtol-table-index will need to be rewritten too. In order to facilitate this, objtool simply marks the whole reloc section 'changed' which will cause the whole section to be re-generated. However, finding the relocs that use any given symbol is implemented rather crudely -- a fully iteration of all sections and their relocs. Given that some builds have over 20k sections (kallsyms etc..) iterating all that for *each* symbol moved takes a bit of time. Instead have each symbol keep a list of relocs that reference it. This *vastly* improves build times for certain configs. Reported-by: Borislav Petkov <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0c3e806 commit 1952671

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

tools/objtool/elf.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
356356
struct rb_node *pnode;
357357
struct symbol *iter;
358358

359+
INIT_LIST_HEAD(&sym->reloc_list);
359360
INIT_LIST_HEAD(&sym->pv_target);
360361
sym->alias = sym;
361362

@@ -557,6 +558,7 @@ int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
557558
reloc->sym = sym;
558559
reloc->addend = addend;
559560

561+
list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list);
560562
list_add_tail(&reloc->list, &sec->reloc->reloc_list);
561563
elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
562564

@@ -573,21 +575,10 @@ int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
573575
*/
574576
static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym)
575577
{
576-
struct section *sec;
577-
578-
list_for_each_entry(sec, &elf->sections, list) {
579-
struct reloc *reloc;
580-
581-
if (sec->changed)
582-
continue;
578+
struct reloc *reloc;
583579

584-
list_for_each_entry(reloc, &sec->reloc_list, list) {
585-
if (reloc->sym == sym) {
586-
sec->changed = true;
587-
break;
588-
}
589-
}
590-
}
580+
list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry)
581+
reloc->sec->changed = true;
591582
}
592583

593584
/*
@@ -902,11 +893,12 @@ static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsi
902893

903894
static int read_relocs(struct elf *elf)
904895
{
896+
unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
905897
struct section *sec;
906898
struct reloc *reloc;
907-
int i;
908899
unsigned int symndx;
909-
unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
900+
struct symbol *sym;
901+
int i;
910902

911903
if (!elf_alloc_hash(reloc, elf->text_size / 16))
912904
return -1;
@@ -947,13 +939,14 @@ static int read_relocs(struct elf *elf)
947939

948940
reloc->sec = sec;
949941
reloc->idx = i;
950-
reloc->sym = find_symbol_by_index(elf, symndx);
942+
reloc->sym = sym = find_symbol_by_index(elf, symndx);
951943
if (!reloc->sym) {
952944
WARN("can't find reloc entry symbol %d for %s",
953945
symndx, sec->name);
954946
return -1;
955947
}
956948

949+
list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list);
957950
list_add_tail(&reloc->list, &sec->reloc_list);
958951
elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
959952

tools/objtool/include/objtool/elf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct symbol {
6262
u8 fentry : 1;
6363
u8 profiling_func : 1;
6464
struct list_head pv_target;
65+
struct list_head reloc_list;
6566
};
6667

6768
struct reloc {
@@ -73,6 +74,7 @@ struct reloc {
7374
};
7475
struct section *sec;
7576
struct symbol *sym;
77+
struct list_head sym_reloc_entry;
7678
unsigned long offset;
7779
unsigned int type;
7880
s64 addend;

0 commit comments

Comments
 (0)