Commit 0201c575 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Ensure ELF symbols table is found before further ELF processing

libbpf ELF parsing logic might need symbols available before ELF parsing is
completed, so we need to make sure that symbols table section is found in
a separate pass before all the subsequent sections are processed.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200903203542.15944-2-andriin@fb.com
parent 83cf5c68
...@@ -2720,14 +2720,38 @@ static int bpf_object__elf_collect(struct bpf_object *obj) ...@@ -2720,14 +2720,38 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
Elf *elf = obj->efile.elf; Elf *elf = obj->efile.elf;
Elf_Data *btf_ext_data = NULL; Elf_Data *btf_ext_data = NULL;
Elf_Data *btf_data = NULL; Elf_Data *btf_data = NULL;
Elf_Scn *scn = NULL;
int idx = 0, err = 0; int idx = 0, err = 0;
const char *name;
Elf_Data *data;
Elf_Scn *scn;
GElf_Shdr sh;
/* a bunch of ELF parsing functionality depends on processing symbols,
* so do the first pass and find the symbol table
*/
scn = NULL;
while ((scn = elf_nextscn(elf, scn)) != NULL) { while ((scn = elf_nextscn(elf, scn)) != NULL) {
const char *name; if (elf_sec_hdr(obj, scn, &sh))
GElf_Shdr sh; return -LIBBPF_ERRNO__FORMAT;
Elf_Data *data;
if (sh.sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) {
pr_warn("elf: multiple symbol tables in %s\n", obj->path);
return -LIBBPF_ERRNO__FORMAT;
}
data = elf_sec_data(obj, scn);
if (!data)
return -LIBBPF_ERRNO__FORMAT;
obj->efile.symbols = data;
obj->efile.symbols_shndx = elf_ndxscn(scn);
obj->efile.strtabidx = sh.sh_link;
}
}
scn = NULL;
while ((scn = elf_nextscn(elf, scn)) != NULL) {
idx++; idx++;
if (elf_sec_hdr(obj, scn, &sh)) if (elf_sec_hdr(obj, scn, &sh))
...@@ -2766,13 +2790,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) ...@@ -2766,13 +2790,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
btf_ext_data = data; btf_ext_data = data;
} else if (sh.sh_type == SHT_SYMTAB) { } else if (sh.sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) { /* already processed during the first pass above */
pr_warn("elf: multiple symbol tables in %s\n", obj->path);
return -LIBBPF_ERRNO__FORMAT;
}
obj->efile.symbols = data;
obj->efile.symbols_shndx = idx;
obj->efile.strtabidx = sh.sh_link;
} else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) { } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
if (sh.sh_flags & SHF_EXECINSTR) { if (sh.sh_flags & SHF_EXECINSTR) {
if (strcmp(name, ".text") == 0) if (strcmp(name, ".text") == 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment