Skip to content

Commit 584fd3b

Browse files
author
Peter Zijlstra
committed
objtool: Fix .symtab_shndx handling for elf_create_undef_symbol()
When an ELF object uses extended symbol section indexes (IOW it has a .symtab_shndx section), these must be kept in sync with the regular symbol table (.symtab). So for every new symbol we emit, make sure to also emit a .symtab_shndx value to keep the arrays of equal size. Note: since we're writing an UNDEF symbol, most GElf_Sym fields will be 0 and we can repurpose one (st_size) to host the 0 for the xshndx value. Fixes: 2f2f7e4 ("objtool: Add elf_create_undef_symbol()") Reported-by: Nick Desaulniers <[email protected]> Suggested-by: Fangrui Song <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Nick Desaulniers <[email protected]> Link: https://lkml.kernel.org/r/YL3q1qFO9QIRL/[email protected]
1 parent 614124b commit 584fd3b

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tools/objtool/elf.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
717717

718718
struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
719719
{
720-
struct section *symtab;
720+
struct section *symtab, *symtab_shndx;
721721
struct symbol *sym;
722722
Elf_Data *data;
723723
Elf_Scn *s;
@@ -769,6 +769,29 @@ struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
769769
symtab->len += data->d_size;
770770
symtab->changed = true;
771771

772+
symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
773+
if (symtab_shndx) {
774+
s = elf_getscn(elf->elf, symtab_shndx->idx);
775+
if (!s) {
776+
WARN_ELF("elf_getscn");
777+
return NULL;
778+
}
779+
780+
data = elf_newdata(s);
781+
if (!data) {
782+
WARN_ELF("elf_newdata");
783+
return NULL;
784+
}
785+
786+
data->d_buf = &sym->sym.st_size; /* conveniently 0 */
787+
data->d_size = sizeof(Elf32_Word);
788+
data->d_align = 4;
789+
data->d_type = ELF_T_WORD;
790+
791+
symtab_shndx->len += 4;
792+
symtab_shndx->changed = true;
793+
}
794+
772795
sym->sec = find_section_by_index(elf, 0);
773796

774797
elf_add_symbol(elf, sym);

0 commit comments

Comments
 (0)