Re: [PATCH 01/13] objtool: Rewrite hashtable sizing

From: Peter Zijlstra
Date: Wed May 12 2021 - 06:43:04 EST


On Thu, May 06, 2021 at 09:33:53PM +0200, Peter Zijlstra wrote:
> @@ -343,6 +360,10 @@ static int read_symbols(struct elf *elf)
>
> symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
>
> + if (!elf_alloc_hash(symbol, symbols_nr) ||
> + !elf_alloc_hash(symbol_name, symbols_nr))
> + return -1;
> +
> for (i = 0; i < symbols_nr; i++) {
> sym = malloc(sizeof(*sym));
> if (!sym) {

Ingo ran into the empty file without .symtab case with as-2.36.1, which
then means we don't even allocate the symbol hashes which then explodes
later.

The below seems to fix things.

---
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 6942357cd4a2..60bef847ee85 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -340,25 +340,19 @@ static int read_symbols(struct elf *elf)
{
struct section *symtab, *symtab_shndx, *sec;
struct symbol *sym, *pfunc;
- int symbols_nr, i;
+ int i, symbols_nr = 0;
char *coldstr;
Elf_Data *shndx_data = NULL;
Elf32_Word shndx;

symtab = find_section_by_name(elf, ".symtab");
- if (!symtab) {
- /*
- * A missing symbol table is actually possible if it's an empty
- * .o file. This can happen for thunk_64.o.
- */
- return 0;
- }
-
- symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
- if (symtab_shndx)
- shndx_data = symtab_shndx->data;
+ if (symtab) {
+ symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
+ if (symtab_shndx)
+ shndx_data = symtab_shndx->data;

- symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
+ symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
+ }

if (!elf_alloc_hash(symbol, symbols_nr) ||
!elf_alloc_hash(symbol_name, symbols_nr))