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>
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
)
26 Elf_Scn
*sec
= elf_section_by_name(elf
, &shdr
, name
, &symtab_index
);
31 if (gelf_getshdr(sec
, &shdr
) == NULL
)
34 struct elf_symtab
*symtab
= zalloc(sizeof(*symtab
));
38 symtab
->name
= strdup(name
);
39 if (symtab
->name
== NULL
)
42 symtab
->syms
= elf_getdata(sec
, NULL
);
43 if (symtab
->syms
== NULL
)
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
);
56 symtab
->symstrs
= elf_getdata(sec
, NULL
);
57 if (symtab
->symstrs
== NULL
)
61 * The .symtab section has optional extended section index
62 * table, load its data so it can be used to resolve symbol's
65 if (symtab_xindex
> 0) {
66 GElf_Shdr shdr_xindex
;
69 sec_xindex
= elf_getscn(elf
, symtab_xindex
);
70 if (sec_xindex
== NULL
)
73 if (gelf_getshdr(sec_xindex
, &shdr_xindex
) == NULL
)
76 /* Extra check to verify it's correct type */
77 if (shdr_xindex
.sh_type
!= SHT_SYMTAB_SHNDX
)
80 /* Extra check to verify it belongs to the .symtab */
81 if (symtab_index
!= shdr_xindex
.sh_link
)
84 symtab
->syms_sec_idx_table
= elf_getdata(elf_getscn(elf
, symtab_xindex
), NULL
);
85 if (symtab
->syms_sec_idx_table
== NULL
)
89 symtab
->nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
99 void elf_symtab__delete(struct elf_symtab
*symtab
)
103 zfree(&symtab
->name
);