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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*-
* Copyright (c) 2009 Kai Wang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "_libdwarf.h"
ELFTC_VCSID("$Id: libdwarf_elf_init.c 3161 2015-02-15 21:43:36Z emaste $");
static const char *debug_name[] = {
".debug_abbrev",
".debug_aranges",
".debug_frame",
".debug_info",
".debug_types",
".debug_line",
".debug_pubnames",
".eh_frame",
".debug_macinfo",
".debug_str",
".debug_loc",
".debug_pubtypes",
".debug_ranges",
".debug_static_func",
".debug_static_vars",
".debug_typenames",
".debug_weaknames",
NULL
};
static void
_dwarf_elf_write_reloc(Dwarf_Debug dbg, Elf_Data *symtab_data, int endian,
void *buf, uint64_t offset, GElf_Xword r_info, GElf_Sxword r_addend)
{
GElf_Sym sym;
int size;
if (gelf_getsym(symtab_data, GELF_R_SYM(r_info), &sym) == NULL)
return;
if ((size = _dwarf_get_reloc_size(dbg, GELF_R_TYPE(r_info))) == 0)
return; /* Unknown or non-absolute relocation. */
if (endian == ELFDATA2MSB)
_dwarf_write_msb(buf, &offset, sym.st_value + r_addend, size);
else
_dwarf_write_lsb(buf, &offset, sym.st_value + r_addend, size);
}
static void
_dwarf_elf_apply_rel_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data,
Elf_Data *symtab_data, int endian)
{
GElf_Rel rel;
int j;
j = 0;
while (gelf_getrel(rel_data, j++, &rel) != NULL)
_dwarf_elf_write_reloc(dbg, symtab_data, endian, buf,
rel.r_offset, rel.r_info, 0);
}
static void
_dwarf_elf_apply_rela_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data,
Elf_Data *symtab_data, int endian)
{
GElf_Rela rela;
int j;
j = 0;
while (gelf_getrela(rel_data, j++, &rela) != NULL)
_dwarf_elf_write_reloc(dbg, symtab_data, endian, buf,
rela.r_offset, rela.r_info, rela.r_addend);
}
static int
_dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx,
size_t symtab, Elf_Data *symtab_data, Dwarf_Error *error)
{
GElf_Ehdr eh;
GElf_Shdr sh;
Elf_Scn *scn;
Elf_Data *rel;
int elferr;
if (symtab == 0 || symtab_data == NULL)
return (DW_DLE_NONE);
if (gelf_getehdr(elf, &eh) == NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
return (DW_DLE_ELF);
}
scn = NULL;
(void) elf_errno();
while ((scn = elf_nextscn(elf, scn)) != NULL) {
if (gelf_getshdr(scn, &sh) == NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
return (DW_DLE_ELF);
}
if ((sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA) ||
sh.sh_size == 0)
continue;
if (sh.sh_info == shndx && sh.sh_link == symtab) {
if ((rel = elf_getdata(scn, NULL)) == NULL) {
elferr = elf_errno();
if (elferr != 0) {
_DWARF_SET_ERROR(NULL, error,
DW_DLE_ELF, elferr);
return (DW_DLE_ELF);
} else
return (DW_DLE_NONE);
}
ed->ed_alloc = malloc(ed->ed_data->d_size);
if (ed->ed_alloc == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
memcpy(ed->ed_alloc, ed->ed_data->d_buf,
ed->ed_data->d_size);
if (sh.sh_type == SHT_REL)
_dwarf_elf_apply_rel_reloc(dbg, ed->ed_alloc,
rel, symtab_data, eh.e_ident[EI_DATA]);
else
_dwarf_elf_apply_rela_reloc(dbg, ed->ed_alloc,
rel, symtab_data, eh.e_ident[EI_DATA]);
return (DW_DLE_NONE);
}
}
elferr = elf_errno();
if (elferr != 0) {
DWARF_SET_ELF_ERROR(dbg, error);
return (DW_DLE_ELF);
}
return (DW_DLE_NONE);
}
int
_dwarf_elf_init(Dwarf_Debug dbg, Elf *elf, Dwarf_Error *error)
{
Dwarf_Obj_Access_Interface *iface;
Dwarf_Elf_Object *e;
const char *name;
GElf_Shdr sh;
Elf_Scn *scn;
Elf_Data *symtab_data;
size_t symtab_ndx;
int elferr, i, j, n, ret;
ret = DW_DLE_NONE;
if ((iface = calloc(1, sizeof(*iface))) == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
if ((e = calloc(1, sizeof(*e))) == NULL) {
free(iface);
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
e->eo_elf = elf;
e->eo_methods.get_section_info = _dwarf_elf_get_section_info;
e->eo_methods.get_byte_order = _dwarf_elf_get_byte_order;
e->eo_methods.get_length_size = _dwarf_elf_get_length_size;
e->eo_methods.get_pointer_size = _dwarf_elf_get_pointer_size;
e->eo_methods.get_section_count = _dwarf_elf_get_section_count;
e->eo_methods.load_section = _dwarf_elf_load_section;
iface->object = e;
iface->methods = &e->eo_methods;
dbg->dbg_iface = iface;
if (gelf_getehdr(elf, &e->eo_ehdr) == NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
dbg->dbg_machine = e->eo_ehdr.e_machine;
if (!elf_getshstrndx(elf, &e->eo_strndx)) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
n = 0;
symtab_ndx = 0;
symtab_data = NULL;
scn = NULL;
(void) elf_errno();
while ((scn = elf_nextscn(elf, scn)) != NULL) {
if (gelf_getshdr(scn, &sh) == NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
if (!strcmp(name, ".symtab")) {
symtab_ndx = elf_ndxscn(scn);
if ((symtab_data = elf_getdata(scn, NULL)) == NULL) {
elferr = elf_errno();
if (elferr != 0) {
_DWARF_SET_ERROR(NULL, error,
DW_DLE_ELF, elferr);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
}
continue;
}
for (i = 0; debug_name[i] != NULL; i++) {
if (!strcmp(name, debug_name[i]))
n++;
}
}
elferr = elf_errno();
if (elferr != 0) {
DWARF_SET_ELF_ERROR(dbg, error);
return (DW_DLE_ELF);
}
e->eo_seccnt = n;
if (n == 0)
return (DW_DLE_NONE);
if ((e->eo_data = calloc(n, sizeof(Dwarf_Elf_Data))) == NULL ||
(e->eo_shdr = calloc(n, sizeof(GElf_Shdr))) == NULL) {
DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
ret = DW_DLE_MEMORY;
goto fail_cleanup;
}
scn = NULL;
j = 0;
while ((scn = elf_nextscn(elf, scn)) != NULL && j < n) {
if (gelf_getshdr(scn, &sh) == NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
memcpy(&e->eo_shdr[j], &sh, sizeof(sh));
if ((name = elf_strptr(elf, e->eo_strndx, sh.sh_name)) ==
NULL) {
DWARF_SET_ELF_ERROR(dbg, error);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
for (i = 0; debug_name[i] != NULL; i++) {
if (strcmp(name, debug_name[i]))
continue;
(void) elf_errno();
if ((e->eo_data[j].ed_data = elf_getdata(scn, NULL)) ==
NULL) {
elferr = elf_errno();
if (elferr != 0) {
_DWARF_SET_ERROR(dbg, error,
DW_DLE_ELF, elferr);
ret = DW_DLE_ELF;
goto fail_cleanup;
}
}
if (_libdwarf.applyreloc) {
if (_dwarf_elf_relocate(dbg, elf,
&e->eo_data[j], elf_ndxscn(scn), symtab_ndx,
symtab_data, error) != DW_DLE_NONE)
goto fail_cleanup;
}
j++;
}
}
assert(j == n);
return (DW_DLE_NONE);
fail_cleanup:
_dwarf_elf_deinit(dbg);
return (ret);
}
void
_dwarf_elf_deinit(Dwarf_Debug dbg)
{
Dwarf_Obj_Access_Interface *iface;
Dwarf_Elf_Object *e;
int i;
iface = dbg->dbg_iface;
assert(iface != NULL);
e = iface->object;
assert(e != NULL);
if (e->eo_data) {
for (i = 0; (Dwarf_Unsigned) i < e->eo_seccnt; i++) {
if (e->eo_data[i].ed_alloc)
free(e->eo_data[i].ed_alloc);
}
free(e->eo_data);
}
if (e->eo_shdr)
free(e->eo_shdr);
free(e);
free(iface);
dbg->dbg_iface = NULL;
}
|