2023-05-30 17:23:16

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 05/22] objtool: Fix reloc_hash size

With CONFIG_DEBUG_INFO, DWARF creates a lot of relocations and
reloc_hash is woefully undersized, which can affect performance
significantly. Fix that.

Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/elf.c | 16 +++++++---------
tools/objtool/include/objtool/elf.h | 8 +++++++-
2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 4bbdd8e6df2c..f72ec6d8fb14 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -328,12 +328,12 @@ static int read_sections(struct elf *elf)
}
}

- if (sec->sh.sh_flags & SHF_EXECINSTR)
- elf->text_size += sec->sh.sh_size;
-
list_add_tail(&sec->list, &elf->sections);
elf_hash_add(section, &sec->hash, sec->idx);
elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
+
+ if (is_reloc_sec(sec))
+ elf->num_relocs += sec->sh.sh_size / sec->sh.sh_entsize;
}

if (opts.stats) {
@@ -888,19 +888,18 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc)

static int read_relocs(struct elf *elf)
{
- unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
+ unsigned long nr_reloc, max_reloc = 0;
struct section *rsec;
struct reloc *reloc;
unsigned int symndx;
struct symbol *sym;
int i;

- if (!elf_alloc_hash(reloc, elf->text_size / 16))
+ if (!elf_alloc_hash(reloc, elf->num_relocs))
return -1;

list_for_each_entry(rsec, &elf->sections, list) {
- if ((rsec->sh.sh_type != SHT_RELA) &&
- (rsec->sh.sh_type != SHT_REL))
+ if (!is_reloc_sec(rsec))
continue;

rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
@@ -942,12 +941,11 @@ static int read_relocs(struct elf *elf)
nr_reloc++;
}
max_reloc = max(max_reloc, nr_reloc);
- tot_reloc += nr_reloc;
}

if (opts.stats) {
printf("max_reloc: %lu\n", max_reloc);
- printf("tot_reloc: %lu\n", tot_reloc);
+ printf("num_relocs: %lu\n", elf->num_relocs);
printf("reloc_bits: %d\n", elf->reloc_bits);
}

diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 6f82f2515d6b..b81d78b35126 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -90,8 +90,9 @@ struct elf {
int fd;
bool changed;
char *name;
- unsigned int text_size, num_files;
+ unsigned int num_files;
struct list_head sections;
+ unsigned long num_relocs;

int symbol_bits;
int symbol_name_bits;
@@ -158,6 +159,11 @@ static inline size_t elf_rela_size(struct elf *elf)
return elf_addr_size(elf) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela);
}

+static inline bool is_reloc_sec(struct section *sec)
+{
+ return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL;
+}
+
#define for_each_sec(file, sec) \
list_for_each_entry(sec, &file->elf->sections, list)

--
2.40.1



Subject: [tip: objtool/core] objtool: Fix reloc_hash size

The following commit has been merged into the objtool/core branch of tip:

Commit-ID: eb0481bbc4ce386e73e28ad8590b4f12c8aded56
Gitweb: https://git.kernel.org/tip/eb0481bbc4ce386e73e28ad8590b4f12c8aded56
Author: Josh Poimboeuf <[email protected]>
AuthorDate: Tue, 30 May 2023 10:20:57 -07:00
Committer: Josh Poimboeuf <[email protected]>
CommitterDate: Wed, 07 Jun 2023 10:03:16 -07:00

objtool: Fix reloc_hash size

With CONFIG_DEBUG_INFO, DWARF creates a lot of relocations and
reloc_hash is woefully undersized, which can affect performance
significantly. Fix that.

Link: https://lore.kernel.org/r/38ef60dc8043270bf3b9dfd139ae2a30ca3f75cc.1685464332.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/elf.c | 16 +++++++---------
tools/objtool/include/objtool/elf.h | 8 +++++++-
2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 4bbdd8e..f72ec6d 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -328,12 +328,12 @@ static int read_sections(struct elf *elf)
}
}

- if (sec->sh.sh_flags & SHF_EXECINSTR)
- elf->text_size += sec->sh.sh_size;
-
list_add_tail(&sec->list, &elf->sections);
elf_hash_add(section, &sec->hash, sec->idx);
elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
+
+ if (is_reloc_sec(sec))
+ elf->num_relocs += sec->sh.sh_size / sec->sh.sh_entsize;
}

if (opts.stats) {
@@ -888,19 +888,18 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc)

static int read_relocs(struct elf *elf)
{
- unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
+ unsigned long nr_reloc, max_reloc = 0;
struct section *rsec;
struct reloc *reloc;
unsigned int symndx;
struct symbol *sym;
int i;

- if (!elf_alloc_hash(reloc, elf->text_size / 16))
+ if (!elf_alloc_hash(reloc, elf->num_relocs))
return -1;

list_for_each_entry(rsec, &elf->sections, list) {
- if ((rsec->sh.sh_type != SHT_RELA) &&
- (rsec->sh.sh_type != SHT_REL))
+ if (!is_reloc_sec(rsec))
continue;

rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
@@ -942,12 +941,11 @@ static int read_relocs(struct elf *elf)
nr_reloc++;
}
max_reloc = max(max_reloc, nr_reloc);
- tot_reloc += nr_reloc;
}

if (opts.stats) {
printf("max_reloc: %lu\n", max_reloc);
- printf("tot_reloc: %lu\n", tot_reloc);
+ printf("num_relocs: %lu\n", elf->num_relocs);
printf("reloc_bits: %d\n", elf->reloc_bits);
}

diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 6f82f25..b81d78b 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -90,8 +90,9 @@ struct elf {
int fd;
bool changed;
char *name;
- unsigned int text_size, num_files;
+ unsigned int num_files;
struct list_head sections;
+ unsigned long num_relocs;

int symbol_bits;
int symbol_name_bits;
@@ -158,6 +159,11 @@ static inline size_t elf_rela_size(struct elf *elf)
return elf_addr_size(elf) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela);
}

+static inline bool is_reloc_sec(struct section *sec)
+{
+ return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL;
+}
+
#define for_each_sec(file, sec) \
list_for_each_entry(sec, &file->elf->sections, list)