dwarves/elf_symtab.c
Jiri Olsa 1bb49897dd bpf_encoder: Translate SHN_XINDEX in symbol's st_shndx values
For very large ELF objects (with many sections), we could get special
value SHN_XINDEX (65535) for symbol's st_shndx.

This patch is adding code to detect the optional extended section index
table and use it to resolve symbol's section index.

Adding elf_symtab__for_each_symbol_index macro that returns symbol's
section index and usign it in collect functions.

Tested by running pahole on kernel compiled with:

  make KCFLAGS="-ffunction-sections -fdata-sections" -j$(nproc) vmlinux

and ensure FUNC records are generated and match normal build (without
above KCFLAGS).

Also bpf selftest passed and generated kernel BTF, is same as without
the patch.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Hao Luo <haoluo@google.com>
Cc: Joe Lawrence <joe.lawrence@redhat.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: KP Singh <kpsingh@chromium.org>
Cc: Mark Wieelard <mjw@redhat.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: dwarves@vger.kernel.org
Cc: netdev@vger.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-01-26 22:36:36 -03:00

106 lines
2.2 KiB
C

/*
SPDX-License-Identifier: GPL-2.0-only
Copyright (C) 2009 Red Hat Inc.
Copyright (C) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
*/
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include "dutil.h"
#include "elf_symtab.h"
#define HASHSYMS__BITS 8
#define HASHSYMS__SIZE (1UL << HASHSYMS__BITS)
struct elf_symtab *elf_symtab__new(const char *name, Elf *elf, GElf_Ehdr *ehdr)
{
size_t symtab_index;
if (name == NULL)
name = ".symtab";
GElf_Shdr shdr;
Elf_Scn *sec = elf_section_by_name(elf, ehdr, &shdr, name, &symtab_index);
if (sec == NULL)
return NULL;
if (gelf_getshdr(sec, &shdr) == NULL)
return NULL;
struct elf_symtab *symtab = zalloc(sizeof(*symtab));
if (symtab == NULL)
return NULL;
symtab->name = strdup(name);
if (symtab->name == NULL)
goto out_delete;
symtab->syms = elf_getdata(sec, NULL);
if (symtab->syms == NULL)
goto out_free_name;
/*
* This returns extended section index table's
* section index, if it exists.
*/
int symtab_xindex = elf_scnshndx(sec);
sec = elf_getscn(elf, shdr.sh_link);
if (sec == NULL)
goto out_free_name;
symtab->symstrs = elf_getdata(sec, NULL);
if (symtab->symstrs == NULL)
goto out_free_name;
/*
* The .symtab section has optional extended section index
* table, load its data so it can be used to resolve symbol's
* section index.
**/
if (symtab_xindex > 0) {
GElf_Shdr shdr_xindex;
Elf_Scn *sec_xindex;
sec_xindex = elf_getscn(elf, symtab_xindex);
if (sec_xindex == NULL)
goto out_free_name;
if (gelf_getshdr(sec_xindex, &shdr_xindex) == NULL)
goto out_free_name;
/* Extra check to verify it's correct type */
if (shdr_xindex.sh_type != SHT_SYMTAB_SHNDX)
goto out_free_name;
/* Extra check to verify it belongs to the .symtab */
if (symtab_index != shdr_xindex.sh_link)
goto out_free_name;
symtab->syms_sec_idx_table = elf_getdata(elf_getscn(elf, symtab_xindex), NULL);
if (symtab->syms_sec_idx_table == NULL)
goto out_free_name;
}
symtab->nr_syms = shdr.sh_size / shdr.sh_entsize;
return symtab;
out_free_name:
free(symtab->name);
out_delete:
free(symtab);
return NULL;
}
void elf_symtab__delete(struct elf_symtab *symtab)
{
if (symtab == NULL)
return;
free(symtab->name);
free(symtab);
}