-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.cc
More file actions
275 lines (231 loc) · 5.92 KB
/
loader.cc
File metadata and controls
275 lines (231 loc) · 5.92 KB
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
#include <bfd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <vector>
#include "loader.h"
static bfd* open_bfd(std::string &fname)
{
static int bfd_inited = 0;
bfd *bfd_h;
if(!bfd_inited) {
bfd_init();
bfd_inited = 1;
}
bfd_h = bfd_openr(fname.c_str(), NULL);
if(!bfd_h) {
fprintf(stderr, "failed to open binary '%s' (%s)\n", fname.c_str(),
bfd_errmsg(bfd_get_error()));
return NULL;
}
if(!bfd_check_format(bfd_h, bfd_object)) {
fprintf(stderr, "file '%s' does not look like an executable (%s)\n",
fname.c_str(), bfd_errmsg(bfd_get_error()));
return NULL;
}
bfd_set_error(bfd_error_no_error);
if(bfd_get_flavour(bfd_h) == bfd_target_unknown_flavour) {
fprintf(stderr, "unrecognized format for binary '%s' (%s)\n",
fname.c_str(), bfd_errmsg(bfd_get_error()));
return NULL;
}
return bfd_h;
}
static int load_symbol_bfd(bfd *bfd_h, Binary *bin)
{
int ret;
long n, nsyms, i;
asymbol **bfd_symtab;
Symbol *sym;
bfd_symtab = NULL;
n = bfd_get_symtab_upper_bound(bfd_h);
if(n < 0) {
fprintf(stderr, "Failed to read symtab (%s)\n",
bfd_errmsg(bfd_get_error()));
goto fail;
} else if(n) {
bfd_symtab = (asymbol**)malloc(n);
if(!bfd_symtab) {
fprintf(stderr, "Out of memory\n");
goto fail;
}
nsyms = bfd_canonicalize_symtab(bfd_h, bfd_symtab);
if(nsyms < 0) {
fprintf(stderr, "Failed to read symtab (%s)\n",
bfd_errmsg(bfd_get_error()));
goto fail;
}
for(i = 0; i < nsyms; ++i) {
if(bfd_symtab[i]->flags & BSF_FUNCTION) {
bin->symbols.push_back(Symbol());
sym = &bin->symbols.back();
sym->type = Symbol::SYM_TYPE_FUNC;
sym->name = std::string(bfd_symtab[i]->name);
sym->addr = bfd_asymbol_value(bfd_symtab[i]);
}
}
}
ret = 0;
goto cleanup;
fail:
ret = -1;
cleanup:
if(bfd_symtab)
free(bfd_symtab);
return ret;
}
static int load_dynsym_bfd(bfd *bfd_h, Binary *bin)
{
int ret;
long n, nsyms, i;
asymbol **bfd_dynsym;
Symbol *sym;
bfd_dynsym = NULL;
n = bfd_get_dynamic_symtab_upper_bound(bfd_h);
if(n < 0) {
fprintf(stderr, "Failed to read dynamic symtab (%s)\n",
bfd_errmsg(bfd_get_error()));
goto fail;
} else if(n) {
bfd_dynsym = (asymbol**)malloc(n);
if(!bfd_dynsym) {
fprintf(stderr, "Out of memory\n");
goto fail;
}
nsyms = bfd_canonicalize_dynamic_symtab(bfd_h, bfd_dynsym);
if(nsyms < 0) {
fprintf(stderr, "Failed to read dynamic symtab(%s)\n",
bfd_errmsg(bfd_get_error()));
goto fail;
}
for(i = 0; i < nsyms; ++i) {
if(bfd_dynsym[i]->flags & BSF_FUNCTION) {
bin->symbols.push_back(Symbol());
sym = &bin->symbols.back();
sym->type = Symbol::SYM_TYPE_FUNC;
sym->name = std::string(bfd_dynsym[i]->name);
sym->addr = bfd_asymbol_value(bfd_dynsym[i]);
}
}
}
ret = 0;
goto cleanup;
fail:
ret = -1;
cleanup:
if(bfd_dynsym)
free(bfd_dynsym);
return ret;
}
static int load_sections_bfd(bfd *bfd_h, Binary *bin)
{
int bfd_flags;
uint64_t vma, size;
const char *secname;
asection* bfd_sec;
Section *sec;
Section::SectionType sectype;
for(bfd_sec = bfd_h->sections; bfd_sec; bfd_sec = bfd_sec->next) {
bfd_flags = bfd_section_flags(bfd_sec);
sectype = Section::SEC_TYPE_NONE;
if(bfd_flags & SEC_CODE) {
sectype = Section::SEC_TYPE_CODE;
} else if(bfd_flags & SEC_DATA) {
sectype = Section::SEC_TYPE_DATA;
} else {
continue;
}
vma = bfd_section_vma(bfd_sec);
size = bfd_section_size(bfd_sec);
secname = bfd_section_name(bfd_sec);
if(!secname) secname = "<unnamed>";
bin->sections.push_back(Section());
sec = &bin->sections.back();
sec->binary = bin;
sec->name = std::string(secname);
sec->type = sectype;
sec->vma = vma;
sec->size = size;
sec->bytes = (uint8_t*)malloc(size);
if(!sec->bytes) {
fprintf(stderr, "Out of memory\n");
return -1;
}
if(!bfd_get_section_contents(bfd_h, bfd_sec, sec->bytes, 0, size)) {
fprintf(stderr, "Failed to read section '%s' (%s)\n", secname,
bfd_errmsg(bfd_get_error()));
return -1;
}
}
return 0;
}
static int
load_binary_bfd(std::string &fname, Binary *bin, Binary::BinaryType type)
{
int ret;
bfd *bfd_h;
const bfd_arch_info_type *bfd_info;
bfd_h = NULL;
bfd_h = open_bfd(fname);
if(!bfd_h)
goto fail;
bin->filename = std::string(fname);
bin->entry = bfd_get_start_address(bfd_h);
bin->type_str = std::string(bfd_h->xvec->name);
switch(bfd_h->xvec->flavour) {
case bfd_target_elf_flavour:
bin->type = Binary::BIN_TYPE_ELF;
break;
case bfd_target_coff_flavour:
bin->type = Binary::BIN_TYPE_PE;
break;
case bfd_target_unknown_flavour:
default:
fprintf(stderr, "Unsupported binary type (%s)\n", bfd_h->xvec->name);
goto fail;
}
bfd_info = bfd_get_arch_info(bfd_h);
bin->arch_str = std::string(bfd_info->printable_name);
switch(bfd_info->mach) {
case bfd_mach_i386_i386:
bin->arch = Binary::ARCH_X86;
bin->bits = 32;
break;
case bfd_mach_x86_64:
bin->arch = Binary::ARCH_X86;
bin->bits = 64;
break;
default:
fprintf(stderr, "Unsupported architecture (%s)\n",
bfd_info->printable_name);
goto fail;
}
load_symbol_bfd(bfd_h, bin);
load_dynsym_bfd(bfd_h, bin);
if(load_sections_bfd(bfd_h, bin) < 0)
goto fail;
ret = 0;
goto cleanup;
fail:
ret = -1;
cleanup:
if(bfd_h)
bfd_close(bfd_h);
return ret;
}
int load_binary(std::string &fname, Binary *bin, Binary::BinaryType type)
{
return load_binary_bfd(fname, bin, type);
}
void unload_binary(Binary *bin)
{
size_t i;
Section *sec;
for(i = 0; i < bin->sections.size(); ++i) {
sec = &bin->sections[i];
if(sec->bytes) {
free(sec->bytes);
}
}
}