1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/* Copyright (C) 2005, 2007, 2008 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2005.
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include "libasmP.h"
#include "../libebl/libeblP.h"
struct symtoken
{
DisasmCtx_t *ctx;
void *symcbarg;
};
static int
default_elf_getsym (GElf_Addr addr, Elf32_Word scnndx, GElf_Addr value,
char **buf, size_t *buflen, void *arg)
{
struct symtoken *symtoken = (struct symtoken *) arg;
/* First try the user provided function. */
if (symtoken->ctx->symcb != NULL)
{
int res = symtoken->ctx->symcb (addr, scnndx, value, buf, buflen,
symtoken->symcbarg);
if (res >= 0)
return res;
}
// XXX Look up in ELF file.
return -1;
}
struct symaddrpair
{
GElf_Addr addr;
const char *name;
};
static void
read_symtab_exec (DisasmCtx_t *ctx)
{
/* We simply use all we can get our hands on. This will produce
some duplicate information but this is no problem, we simply
ignore the latter definitions. */
Elf_Scn *scn= NULL;
while ((scn = elf_nextscn (ctx->elf, scn)) != NULL)
{
GElf_Shdr shdr_mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
Elf_Data *data;
if (shdr == NULL || shdr->sh_type != SHT_SYMTAB
|| (data = elf_getdata (scn, NULL)) == NULL)
continue;
int xndxscnidx = elf_scnshndx (scn);
Elf_Data *xndxdata = NULL;
if (xndxscnidx > 0)
xndxdata = elf_getdata (elf_getscn (ctx->elf, xndxscnidx), NULL);
/* Iterate over all symbols. Add all defined symbols. */
int nsyms = shdr->sh_size / shdr->sh_entsize;
for (int cnt = 1; cnt < nsyms; ++cnt)
{
Elf32_Word xshndx;
GElf_Sym sym_mem;
GElf_Sym *sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem,
&xshndx);
if (sym == NULL)
continue;
/* Undefined symbols are useless here. */
if (sym->st_shndx == SHN_UNDEF)
continue;
}
}
}
static void
read_symtab (DisasmCtx_t *ctx)
{
/* Find the symbol table(s). */
GElf_Ehdr ehdr_mem;
GElf_Ehdr *ehdr = gelf_getehdr (ctx->elf, &ehdr_mem);
if (ehdr == NULL)
return;
switch (ehdr->e_type)
{
case ET_EXEC:
case ET_DYN:
read_symtab_exec (ctx);
break;
case ET_REL:
// XXX Handle
break;
default:
break;
}
}
static int
null_elf_getsym (GElf_Addr addr __attribute__ ((unused)),
Elf32_Word scnndx __attribute__ ((unused)),
GElf_Addr value __attribute__ ((unused)),
char **buf __attribute__ ((unused)),
size_t *buflen __attribute__ ((unused)),
void *arg __attribute__ ((unused)))
{
return -1;
}
int
disasm_cb (DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end,
GElf_Addr addr, const char *fmt, DisasmOutputCB_t outcb,
void *outcbarg, void *symcbarg)
{
struct symtoken symtoken;
DisasmGetSymCB_t getsym = ctx->symcb ?: null_elf_getsym;
if (ctx->elf != NULL)
{
/* Read all symbols of the ELF file and stuff them into a hash
table. The key is the address and the section index. */
read_symtab (ctx);
symtoken.ctx = ctx;
symtoken.symcbarg = symcbarg;
symcbarg = &symtoken;
getsym = default_elf_getsym;
}
return ctx->ebl->disasm (startp, end, addr, fmt, outcb, getsym, outcbarg,
symcbarg);
}
INTDEF (disasm_cb)
|