This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
bfdglue.c
353 lines (294 loc) · 7.58 KB
/
bfdglue.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
#include <bfdglue.h>
#include <signal.h>
#include <string.h>
#if defined(__linux__) || defined(__FreeBSD__)
#include <link.h>
#include <elf.h>
#endif
#ifdef __linux__
#include <sys/user.h>
#include <sys/procfs.h>
#endif
#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/procfs.h>
#endif
bfd *
bfd_glue_openr (const char *filename, const char *target)
{
return bfd_openr (g_strdup (filename), NULL);
}
gchar *
bfd_glue_get_target_name (bfd *abfd)
{
return g_strdup (abfd->xvec->name);
}
gboolean
bfd_glue_check_format_object (bfd *abfd)
{
return bfd_check_format (abfd, bfd_object);
}
gboolean
bfd_glue_check_format_core (bfd *abfd)
{
return bfd_check_format (abfd, bfd_core);
}
gboolean
bfd_glue_check_format_archive (bfd *abfd)
{
return bfd_check_format (abfd, bfd_archive);
}
bfd *
bfd_glue_openr_next_archived_file (bfd* archive, bfd* last)
{
return bfd_openr_next_archived_file (archive, last);
}
int
bfd_glue_get_symbols (bfd *abfd, asymbol ***symbol_table)
{
int storage_needed = bfd_get_symtab_upper_bound (abfd);
if (storage_needed <= 0) {
*symbol_table = NULL;
return storage_needed;
}
*symbol_table = g_malloc0 (storage_needed);
return bfd_canonicalize_symtab (abfd, *symbol_table);
}
gchar *
bfd_glue_get_symbol (bfd *abfd, asymbol **symbol_table, int idx, int *is_function, guint64 *address)
{
asymbol *symbol;
int flags;
symbol = symbol_table [idx];
if ((symbol->flags & (BSF_WEAK | BSF_DYNAMIC)) == (BSF_WEAK | BSF_DYNAMIC))
return NULL;
if ((symbol->flags & BSF_DEBUGGING) || !symbol->name || !strlen (symbol->name))
return NULL;
flags = symbol->flags & ~(BSF_DYNAMIC | BSF_NOT_AT_END);
if (flags == (BSF_OBJECT | BSF_GLOBAL)) {
*is_function = 0;
*address = symbol->section->vma + symbol->value;
} else if (flags == BSF_FUNCTION) {
*is_function = 1;
*address = symbol->value;
} else if (flags == (BSF_FUNCTION | BSF_GLOBAL)) {
*is_function = 1;
*address = symbol->section->vma + symbol->value;
} else if (flags == (BSF_OBJECT | BSF_LOCAL)) {
#if 0
if (strncmp (symbol->name, "__pthread_", 10) &&
strncmp (symbol->name, "MONO_DEBUGGER_", 14) &&
strcmp (symbol->name, "__libc_pthread_functions"))
return NULL;
#endif
*is_function = 0;
*address = symbol->section->vma + symbol->value;
}
else
{
//Mach headers don't have a function flag. Mark everything as function for now.
//Possibly check address against text section to fix this properly?
*is_function = 1;
*address = symbol->section->vma + symbol->value;
}
return g_strdup (symbol->name);
}
int
bfd_glue_get_dynamic_symbols (bfd *abfd, asymbol ***symbol_table)
{
int storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
if (storage_needed <= 0) {
*symbol_table = NULL;
return storage_needed;
}
*symbol_table = g_malloc0 (storage_needed);
return bfd_canonicalize_dynamic_symtab (abfd, *symbol_table);
}
static int
read_memory_func (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
{
BfdGlueDisassemblerInfo *data = info->application_data;
return (* data->read_memory_cb) (memaddr, myaddr, length);
}
static int
fprintf_func (gpointer stream, const char *message, ...)
{
BfdGlueDisassemblerInfo *data = stream;
va_list args;
gchar *output;
int retval;
va_start (args, message);
output = g_strdup_vprintf (message, args);
va_end (args);
data->output_cb (output);
retval = strlen (output);
g_free (output);
return retval;
}
static void
print_address_func (bfd_vma address, struct disassemble_info *info)
{
BfdGlueDisassemblerInfo *data = info->application_data;
(* data->print_address_cb) (address);
}
BfdGlueDisassemblerInfo *
bfd_glue_create_disassembler (gboolean is_x86_64,
BfdGlueReadMemoryHandler read_memory_cb,
BfdGlueOutputHandler output_cb,
BfdGluePrintAddressHandler print_address_cb)
{
BfdGlueDisassemblerInfo *handle;
struct disassemble_info *info;
info = g_new0 (struct disassemble_info, 1);
INIT_DISASSEMBLE_INFO (*info, stderr, fprintf);
info->flavour = bfd_target_elf_flavour;
info->arch = bfd_arch_i386;
info->mach = is_x86_64 ? bfd_mach_x86_64 : bfd_mach_i386_i386;
info->octets_per_byte = 1;
info->display_endian = info->endian = BFD_ENDIAN_LITTLE;
handle = g_new0 (BfdGlueDisassemblerInfo, 1);
handle->read_memory_cb = read_memory_cb;
handle->output_cb = output_cb;
handle->print_address_cb = print_address_cb;
handle->disassembler = print_insn_i386;
handle->info = info;
info->application_data = handle;
info->read_memory_func = read_memory_func;
info->fprintf_func = fprintf_func;
info->print_address_func = print_address_func;
info->stream = handle;
return handle;
}
void
bfd_glue_free_disassembler (BfdGlueDisassemblerInfo *handle)
{
g_free (handle->info);
g_free (handle);
}
int
bfd_glue_disassemble_insn (BfdGlueDisassemblerInfo *handle, guint64 address)
{
return handle->disassembler (address, handle->info);
}
gboolean
bfd_glue_get_section_contents (bfd *abfd, asection *section, gpointer data, guint32 size)
{
return bfd_get_section_contents (abfd, section, data, 0, size);
}
asection *
bfd_glue_get_first_section (bfd *abfd)
{
return abfd->sections;
}
asection *
bfd_glue_get_next_section (asection *p)
{
return p->next;
}
guint64
bfd_glue_get_section_vma (asection *p)
{
return p->vma;
}
gchar *
bfd_glue_get_section_name (asection *p)
{
return g_strdup (p->name);
}
gchar *
bfd_glue_get_errormsg (void)
{
return g_strdup (bfd_errmsg (bfd_get_error ()));
}
guint32
bfd_glue_get_section_size (asection *p)
{
return p->_raw_size;
}
BfdGlueSectionFlags
bfd_glue_get_section_flags (asection *p)
{
BfdGlueSectionFlags flags = 0;
if (p->flags & SEC_LOAD)
flags |= SECTION_FLAGS_LOAD;
if (p->flags & SEC_ALLOC)
flags |= SECTION_FLAGS_ALLOC;
if (p->flags & SEC_READONLY)
flags |= SECTION_FLAGS_READONLY;
return flags;
}
gchar *
bfd_glue_core_file_failing_command (bfd *abfd)
{
return g_strdup (bfd_core_file_failing_command (abfd));
}
guint64
bfd_glue_elfi386_locate_base (bfd *abfd, const guint8 *data, int size)
{
#if defined(__linux__) || defined(__FreeBSD__)
const guint8 *ptr;
#if defined(__i386__)
for (ptr = data; ptr < data + size; ptr += sizeof (Elf32_Dyn)) {
Elf32_Dyn *dyn = (Elf32_Dyn *) ptr;
if (dyn->d_tag == DT_NULL)
break;
else if (dyn->d_tag == DT_DEBUG)
return (guint32) dyn->d_un.d_ptr;
}
#elif defined(__x86_64__)
for (ptr = data; ptr < data + size; ptr += sizeof (Elf64_Dyn)) {
Elf64_Dyn *dyn = (Elf64_Dyn *) ptr;
if (dyn->d_tag == DT_NULL)
break;
else if (dyn->d_tag == DT_DEBUG)
return (guint64) dyn->d_un.d_ptr;
}
#else
#error "Unknown architecture"
#endif
#endif
return 0;
}
gboolean
bfd_glue_core_file_elfi386_get_registers (const guint8 *data, int size, guint32 *regs)
{
#ifdef __linux__
if (size != 68) {
g_warning (G_STRLOC ": Core file has unknown .reg section size %d", size);
return FALSE;
}
memcpy (regs, data, size);
return TRUE;
#endif
#ifdef __FreeBSD__
gregset_t *regset = (gregset_t *) data;
if (size != sizeof (gregset_t)) {
g_warning (G_STRLOC ": Core file has unknown .reg section size %d (expected %d)",
size, sizeof (gregset_t));
return FALSE;
}
regs [0] = regset->r_ebx;
regs [1] = regset->r_ecx;
regs [2] = regset->r_edx;
regs [3] = regset->r_esi;
regs [4] = regset->r_edi;
regs [5] = regset->r_ebp;
regs [6] = regset->r_eax;
regs [7] = regset->r_ds;
regs [8] = regset->r_es;
regs [9] = regset->r_fs;
regs [10] = regset->r_gs;
regs [12] = regset->r_eip;
regs [13] = regset->r_cs;
regs [14] = regset->r_eflags;
regs [15] = regset->r_esp;
regs [16] = regset->r_ss;
return TRUE;
#endif
return FALSE;
}
guint64
bfd_glue_get_start_address (bfd *abfd)
{
return bfd_get_start_address (abfd);
}