forked from vext01/dismantle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dm_elf.c
380 lines (305 loc) · 8.11 KB
/
dm_elf.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2011, Edd Barrett <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <gelf.h>
#include "dm_elf.h"
Elf *elf = NULL;
SIMPLEQ_HEAD(tailhead, dm_pht_cache_entry) pht_cache;
struct dm_pht_type pht_types[] = {
{PT_NULL, "PT_NULL", "Unused"},
{PT_LOAD, "PT_LOAD", "Loadable segment"},
{PT_DYNAMIC, "PT_DYNAMIC", "Dynamic linking info"},
{PT_INTERP, "PT_INTERP", "Interpreter field"},
{PT_NOTE, "PT_NOTE", "Auxillary info"},
{PT_SHLIB, "PT_SHLIB", "Non-standard"},
{PT_PHDR, "PT_PHDR", "PHT size"},
{PT_TLS, "PT_TLS", "Thread local storage"},
{PT_LOOS, "PT_LOOS", "System specific (lo/start mark)"},
{PT_HIOS, "PT_HIOS", "System specific (hi/end mark)"},
{PT_LOPROC, "PT_LOPROC", "CPU specific (lo/start mark)"},
{PT_HIPROC, "PT_HIPROC", "CPU system-specific (hi/end mark)"},
/* XXX define thses for !glibc systems */
#ifdef __LINUX__
{PT_GNU_EH_FRAME, "PT_GNU_EH_FRAME", "GCC .eh_frame_hdr segment"},
{PT_GNU_STACK, "PT_GNU_STACK", "Indicates stack executability"},
{PT_GNU_RELRO, "PT_GNU_RELRO", "Read-only after relocation"},
{PT_LOSUNW, "PT_LOSUNW", "Sun specific (lo/start mark)"},
{PT_SUNWBSS, "PT_SUNWBSS", "Sun specific segment"},
{PT_SUNWSTACK, "PT_SUNWSTACK", "Sun stack segment"},
{PT_HISUNW, "PT_HISUNW", "Sun specific (hi/end mark)"},
#endif
{-1, NULL, NULL},
};
struct dm_pht_type unknown_pht_type = {-1, "???", "Unknown PHT type"};
struct dm_pht_type *
dm_get_pht_info(int find)
{
struct dm_pht_type *t = pht_types;
while (t->type_int != -1) {
if (t->type_int == find)
break;
t++;
}
if (t->type_int == -1)
return (NULL);
return (t);
}
/*
* get the offset of a section name
*/
int
dm_find_section(char *find_sec, GElf_Shdr *shdr)
{
Elf_Scn *sec;
size_t shdrs_idx;
char *sec_name;
NADDR ret = DM_FAIL;
if (elf == NULL)
goto clean;
if (elf_getshdrstrndx(elf, &shdrs_idx) != 0) {
fprintf(stderr, "elf_getshdrsrtndx: %s", elf_errmsg(-1));
goto clean;
}
sec = NULL ;
while ((sec = elf_nextscn(elf, sec)) != NULL) {
if (gelf_getshdr(sec, shdr) != shdr) {
fprintf(stderr, "gelf_getshdr: %s", elf_errmsg(-1));
goto clean;
}
if ((sec_name =
elf_strptr(elf, shdrs_idx, shdr->sh_name)) == NULL) {
fprintf(stderr, "elf_strptr: %s", elf_errmsg(-1));
goto clean;
}
if (strcmp(sec_name, find_sec) == 0) {
ret = DM_OK;
break;
}
}
clean:
return (ret);
}
int
dm_init_elf()
{
Elf_Kind ek;
GElf_Ehdr ehdr;
int nbits;
SIMPLEQ_INIT(&pht_cache);
if(elf_version(EV_CURRENT) == EV_NONE) {
fprintf(stderr, "elf_version: %s\n", elf_errmsg(-1));
goto err;
}
if ((elf = elf_begin(fileno(file_info.fptr), ELF_C_READ, NULL)) == NULL) {
fprintf(stderr, "elf_begin: %s\n", elf_errmsg(-1));
goto err;
}
ek = elf_kind(elf);
if (ek != ELF_K_ELF) {
DPRINTF(DM_D_INFO, "No ELF header present");
goto err;
}
file_info.elf = 1;
/* Let's take a look at the execution header */
if ( gelf_getehdr(elf, &ehdr) == NULL) {
fprintf(stderr, "%s\n", elf_errmsg(-1));
} else {
if ((nbits = gelf_getclass(elf)) == ELFCLASSNONE)
fprintf(stderr, "%s\n", elf_errmsg(-1));
if (nbits == ELFCLASS32)
file_info.bits = 32;
else
file_info.bits = 64;
}
if ((file_info.ident = elf_getident(elf, NULL)) == NULL)
fprintf(stderr, "%s\n", elf_errmsg(-1));
return (DM_OK);
err:
elf = NULL;
return (DM_FAIL);
}
/*
* make a RWX string for program header flags
* ret is a preallocated string of atleast 4 in length
*/
#define DM_APPEND_FLAG(x) *p = (x); p++;
int
dm_make_pht_flag_str(int flags, char *ret)
{
char *p = ret;
memset(ret, 0, 4);
if (flags & PF_R)
DM_APPEND_FLAG('R');
if (flags & PF_W)
DM_APPEND_FLAG('W');
if (flags & PF_X)
DM_APPEND_FLAG('X');
return (DM_OK);
}
/*
* show the program header table
*/
int
dm_cmd_pht(char **args)
{
int ret = DM_FAIL;
char flags[4];
struct dm_pht_cache_entry *cent;
(void) args;
if (elf == NULL)
goto clean;
/* Get program header table */
printf("%s\n", DM_RULE);
printf("%-10s | %-10s | %-5s | %-10s | %-20s\n",
"Offset", "Virtual", "Flags", "Type", "Description");
printf("%s\n", DM_RULE);
SIMPLEQ_FOREACH(cent, &pht_cache, entries) {
dm_make_pht_flag_str(cent->flags, flags);
/* offset is ElfAddr_64 bit on every arch for some reason */
printf(ADDR_FMT_64 " | " ADDR_FMT_64 " | %-5s | %-10s | %-20s\n",
cent->start_offset, cent->start_vaddr, flags,
cent->type->type_str, cent->type->descr);
}
ret = DM_OK;
printf("%s\n", DM_RULE);
clean:
return (ret);
}
/*
* dump the section headers
*/
int
dm_cmd_sht(char **args)
{
Elf_Scn *sec;
size_t shdrs_idx;
GElf_Shdr shdr;
char *sec_name;
int ret = DM_FAIL;
(void) args;
if (elf == NULL)
goto clean;
/* Get section header table */
if (elf_getshdrstrndx(elf, &shdrs_idx) != 0) {
fprintf(stderr, "elf_getshdrsrtndx: %s", elf_errmsg(-1));
goto clean;
}
printf("\nFound %lu section header records:\n", shdrs_idx);
printf("%s\n", DM_RULE);
printf("%-20s | %-10s | %-10s\n", "Name", "Offset", "Virtual");
printf("%s\n", DM_RULE);
sec = NULL ;
while ((sec = elf_nextscn(elf, sec)) != NULL) {
if (gelf_getshdr(sec, &shdr) != &shdr) {
fprintf(stderr, "gelf_getshdr: %s", elf_errmsg(-1));
goto clean;
}
if (!(sec_name = elf_strptr(elf, shdrs_idx, shdr.sh_name))) {
fprintf(stderr, "elf_strptr: %s", elf_errmsg(-1));
goto clean;
}
printf("%-20s | " ADDR_FMT_64 " | " NADDR_FMT "\n",
sec_name, shdr.sh_offset, (NADDR) shdr.sh_addr);
}
printf("%s\n", DM_RULE);
ret = DM_OK;
clean:
return (ret);
}
/*
* parse and cache the PHT
*/
int
dm_parse_pht()
{
int ret = DM_FAIL;
GElf_Phdr phdr;
size_t num_phdrs, i;
struct dm_pht_type *pht_t;
struct dm_pht_cache_entry *rec;
if (elf == NULL)
goto clean;
if (elf_getphdrnum(elf, &num_phdrs) != 0) {
fprintf(stderr, "elf_getphdrnum: %s", elf_errmsg ( -1));
goto clean;
}
for (i = 0; i < num_phdrs; i++) {
if (gelf_getphdr(elf, i, &phdr) != &phdr) {
fprintf(stderr, "elf_getphdr: %s", elf_errmsg(-1));
goto clean;
}
pht_t = dm_get_pht_info(phdr.p_type);
if (!pht_t)
pht_t = &unknown_pht_type;
/* make linked list entry */
rec = calloc(1, sizeof(struct dm_pht_cache_entry));
if (!rec)
fprintf(stderr, "malloc\n");
rec->type = pht_t;
rec->flags = phdr.p_flags;
rec->start_offset = phdr.p_offset;
rec->start_vaddr = phdr.p_vaddr;
rec->memsz = phdr.p_memsz;
rec->filesz = phdr.p_filesz;
SIMPLEQ_INSERT_TAIL(&pht_cache, rec, entries);
}
ret = DM_OK;
clean:
return (ret);
}
int
dm_clean_elf()
{
struct dm_pht_cache_entry *n;
while (!SIMPLEQ_EMPTY(&pht_cache)) {
n = SIMPLEQ_FIRST(&pht_cache);
SIMPLEQ_REMOVE_HEAD(&pht_cache, entries);
free(n);
}
elf_end(elf);
return (DM_OK);
}
int
dm_offset_from_vaddr(ADDR64 vaddr, ADDR64 *offset)
{
struct dm_pht_cache_entry *cent;
ADDR64 diff;
int found = 0;
SIMPLEQ_FOREACH(cent, &pht_cache, entries) {
if ((vaddr < cent->start_vaddr) ||
(vaddr > cent->start_vaddr + cent->filesz))
continue;
diff = vaddr - cent->start_vaddr;
*offset = cent->start_offset + diff;
found = 1;
break;
}
if (!found)
return (DM_FAIL);
return (DM_OK);
}
int
dm_cmd_offset(char **args)
{
ADDR64 off;
ADDR64 vaddr = strtoll(args[0], NULL, 0);
if (dm_offset_from_vaddr(vaddr, &off) != DM_OK) {
fprintf(stderr, "could not find offset from vaddr\n");
return (DM_FAIL);
}
printf(" offset_of(" ADDR_FMT_64 ") = " ADDR_FMT_64 "\n", vaddr, off);
return (DM_OK);
}