1 /* Copyright (C) 2005, 2007, 2008 Red Hat, Inc.
2 This file is part of elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
36 #include "../libebl/libeblP.h"
47 default_elf_getsym (GElf_Addr addr, Elf32_Word scnndx, GElf_Addr value,
48 char **buf, size_t *buflen, void *arg)
50 struct symtoken *symtoken = (struct symtoken *) arg;
52 /* First try the user provided function. */
53 if (symtoken->ctx->symcb != NULL)
55 int res = symtoken->ctx->symcb (addr, scnndx, value, buf, buflen,
61 // XXX Look up in ELF file.
75 read_symtab_exec (DisasmCtx_t *ctx)
77 /* We simply use all we can get our hands on. This will produce
78 some duplicate information but this is no problem, we simply
79 ignore the latter definitions. */
81 while ((scn = elf_nextscn (ctx->elf, scn)) != NULL)
84 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
86 if (shdr == NULL || shdr->sh_type != SHT_SYMTAB
87 || (data = elf_getdata (scn, NULL)) == NULL)
90 int xndxscnidx = elf_scnshndx (scn);
91 Elf_Data *xndxdata = NULL;
93 xndxdata = elf_getdata (elf_getscn (ctx->elf, xndxscnidx), NULL);
95 /* Iterate over all symbols. Add all defined symbols. */
96 int nsyms = shdr->sh_size / shdr->sh_entsize;
97 for (int cnt = 1; cnt < nsyms; ++cnt)
101 GElf_Sym *sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem,
106 /* Undefined symbols are useless here. */
107 if (sym->st_shndx == SHN_UNDEF)
117 read_symtab (DisasmCtx_t *ctx)
119 /* Find the symbol table(s). */
121 GElf_Ehdr *ehdr = gelf_getehdr (ctx->elf, &ehdr_mem);
125 switch (ehdr->e_type)
129 read_symtab_exec (ctx);
143 null_elf_getsym (GElf_Addr addr __attribute__ ((unused)),
144 Elf32_Word scnndx __attribute__ ((unused)),
145 GElf_Addr value __attribute__ ((unused)),
146 char **buf __attribute__ ((unused)),
147 size_t *buflen __attribute__ ((unused)),
148 void *arg __attribute__ ((unused)))
155 disasm_cb (DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end,
156 GElf_Addr addr, const char *fmt, DisasmOutputCB_t outcb,
157 void *outcbarg, void *symcbarg)
159 struct symtoken symtoken;
160 DisasmGetSymCB_t getsym = ctx->symcb ?: null_elf_getsym;
162 if (ctx->elf != NULL)
164 /* Read all symbols of the ELF file and stuff them into a hash
165 table. The key is the address and the section index. */
169 symtoken.symcbarg = symcbarg;
171 symcbarg = &symtoken;
173 getsym = default_elf_getsym;
176 return ctx->ebl->disasm (startp, end, addr, fmt, outcb, getsym, outcbarg,