pahole: Describe expected use of 'default' in the man page
[dwarves.git] / elf_symtab.c
blob3a31d643b420c0fee4044cec62f746c8e9dbe2f5
1 /*
2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2009 Red Hat Inc.
5 Copyright (C) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
6 */
8 #include <malloc.h>
9 #include <stdio.h>
10 #include <string.h>
12 #include "dutil.h"
13 #include "elf_symtab.h"
15 #define HASHSYMS__BITS 8
16 #define HASHSYMS__SIZE (1UL << HASHSYMS__BITS)
18 struct elf_symtab *elf_symtab__new(const char *name, Elf *elf)
20 size_t symtab_index;
22 if (name == NULL)
23 name = ".symtab";
25 GElf_Shdr shdr;
26 Elf_Scn *sec = elf_section_by_name(elf, &shdr, name, &symtab_index);
28 if (sec == NULL)
29 return NULL;
31 if (gelf_getshdr(sec, &shdr) == NULL)
32 return NULL;
34 struct elf_symtab *symtab = zalloc(sizeof(*symtab));
35 if (symtab == NULL)
36 return NULL;
38 symtab->name = strdup(name);
39 if (symtab->name == NULL)
40 goto out_delete;
42 symtab->syms = elf_getdata(sec, NULL);
43 if (symtab->syms == NULL)
44 goto out_free_name;
47 * This returns extended section index table's
48 * section index, if it exists.
50 int symtab_xindex = elf_scnshndx(sec);
52 sec = elf_getscn(elf, shdr.sh_link);
53 if (sec == NULL)
54 goto out_free_name;
56 symtab->symstrs = elf_getdata(sec, NULL);
57 if (symtab->symstrs == NULL)
58 goto out_free_name;
61 * The .symtab section has optional extended section index
62 * table, load its data so it can be used to resolve symbol's
63 * section index.
64 **/
65 if (symtab_xindex > 0) {
66 GElf_Shdr shdr_xindex;
67 Elf_Scn *sec_xindex;
69 sec_xindex = elf_getscn(elf, symtab_xindex);
70 if (sec_xindex == NULL)
71 goto out_free_name;
73 if (gelf_getshdr(sec_xindex, &shdr_xindex) == NULL)
74 goto out_free_name;
76 /* Extra check to verify it's correct type */
77 if (shdr_xindex.sh_type != SHT_SYMTAB_SHNDX)
78 goto out_free_name;
80 /* Extra check to verify it belongs to the .symtab */
81 if (symtab_index != shdr_xindex.sh_link)
82 goto out_free_name;
84 symtab->syms_sec_idx_table = elf_getdata(elf_getscn(elf, symtab_xindex), NULL);
85 if (symtab->syms_sec_idx_table == NULL)
86 goto out_free_name;
89 symtab->nr_syms = shdr.sh_size / shdr.sh_entsize;
91 return symtab;
92 out_free_name:
93 zfree(&symtab->name);
94 out_delete:
95 free(symtab);
96 return NULL;
99 void elf_symtab__delete(struct elf_symtab *symtab)
101 if (symtab == NULL)
102 return;
103 zfree(&symtab->name);
104 free(symtab);