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
|
#include <elf.h>
#include <merror.h>
#include <melf.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "link.h"
extern char *current_file;
static int obj_assert_ehdr(const void *given, const void *assert, size_t size)
{
if (memcmp(given, assert, size) == 0)
return M_SUCCESS;
ERROR("invalid elf file");
return M_ERROR;
}
/**
* Map the ehdr
*/
static int load_ehdr(struct object *object)
{
uint32_t off = 0;
object->ehdr = (Elf32_Ehdr *) (object->mapped + off);
if (BOUND_CHK(object, sizeof(Elf32_Ehdr), off)) {
ERROR("cannot read ehdr");
return M_ERROR;
}
/**
* Compare each "static" value in the ehdr and make sure it
* is what it should be. If not throw and error and eventually
* return.
*/
#define EHDR_ASSERT(name, size) \
if (res == M_SUCCESS) \
res |= obj_assert_ehdr(&MIPS_ELF_EHDR.e_##name, \
&object->ehdr->e_##name, size) \
int res = 0;
EHDR_ASSERT(ident, EI_NIDENT);
EHDR_ASSERT(type, sizeof(Elf32_Half));
EHDR_ASSERT(machine, sizeof(Elf32_Half));
EHDR_ASSERT(version, sizeof(Elf32_Word));
EHDR_ASSERT(flags, sizeof(Elf32_Word));
EHDR_ASSERT(ehsize, sizeof(Elf32_Half));
EHDR_ASSERT(phentsize, sizeof(Elf32_Half));
EHDR_ASSERT(shentsize, sizeof(Elf32_Half));
#undef EHDR_ASSERT
if (res)
return M_ERROR;
return M_SUCCESS;
}
/**
* Map the shdr
*/
static int load_shdr(struct object *object)
{
size_t shdr_len = B16(object->ehdr->e_shentsize) *
B16(object->ehdr->e_shnum);
size_t shdr_off = B32(object->ehdr->e_shoff);
object->shdr = (Elf32_Shdr *) (object->mapped + shdr_off);
object->shdr_len = B16(object->ehdr->e_shnum);
if (BOUND_CHK(object, shdr_len, shdr_off)) {
ERROR("cannot read shdr");
return M_ERROR;
}
return M_SUCCESS;
}
/**
* Map the phdr
*/
static int load_phdr(struct object *object)
{
size_t phdr_len = B16(object->ehdr->e_phentsize) *
B16(object->ehdr->e_phnum);
size_t phdr_off = B32(object->ehdr->e_phoff);
object->phdr = (Elf32_Phdr *) (object->mapped + phdr_off);
object->phdr_len = B16(object->ehdr->e_phnum);
if (BOUND_CHK(object, phdr_len, phdr_off)) {
ERROR("cannot read phdr");
return M_ERROR;
}
return M_SUCCESS;
}
/**
* Load the strtabs
*/
static int load_strtabs(struct object *object)
{
uint32_t max_entries = object->shdr_len;
struct string_table *strtabs = malloc(max_entries *
sizeof(struct string_table));
if (strtabs == NULL) {
PERROR("cannot alloc");
return M_ERROR;
}
for (size_t i = 0; i < object->shdr_len; i++) {
Elf32_Shdr *hdr = &object->shdr[i];
struct string_table *strtab = &strtabs[i];
if (B32(hdr->sh_type) != SHT_STRTAB) {
strtab->len = 0;
continue;
}
uint32_t off = B32(hdr->sh_offset);
uint32_t len = B32(hdr->sh_size);
strtab->len = len;
strtab->data = object->mapped + off;
if (len < 1) {
ERROR("invalid or empty strtab [%d]", i);
return M_ERROR;
}
if (BOUND_CHK(object, len, off)) {
ERROR("cannot map strtab [%d]", i);
return M_ERROR;
}
if (strtab->data[0] != '\0' ||
strtab->data[len - 1] != '\0') {
ERROR("strtab [%d] doesn't not have bounding null "
"values", i);
return M_ERROR;
}
}
object->strtabs = strtabs;
return M_SUCCESS;
}
/**
* Load the symtabs
*/
static int load_symtabs(struct object *object)
{
uint32_t max_entries = object->shdr_len;
struct symbol_table *symtabs = malloc(max_entries *
sizeof(struct symbol_table));
if (symtabs == NULL) {
PERROR("cannot alloc");
return M_ERROR;
}
for (size_t i = 0; i < object->shdr_len; i++) {
Elf32_Shdr *hdr = &object->shdr[i];
struct symbol_table *symtab = &symtabs[i];
if (B32(hdr->sh_type) != SHT_SYMTAB) {
symtab->len = 0;
continue;
}
uint32_t off = B32(hdr->sh_offset);
uint32_t len = B32(hdr->sh_size);
uint32_t stridx = B32(hdr->sh_link);
if (stridx >= max_entries) {
ERROR("strtab index [%d] out of bounds", stridx);
return M_ERROR;
}
struct string_table *strtab = &object->strtabs[stridx];
if (strtab->len < 1) {
ERROR("strtab index [%d] empty or invalid", stridx);
return M_ERROR;
}
symtab->strtab = strtab;
symtab->len = len;
symtab->syms = (Elf32_Sym *) (object->mapped + off);
if (BOUND_CHK(object, len, off)) {
ERROR("cannot map symtab index [%d]", i);
return M_ERROR;
}
}
object->symtabs = symtabs;
return M_SUCCESS;
}
/**
* Load the shstrtab
*/
static int load_shstrtab(struct object *object)
{
uint16_t idx = B16(object->ehdr->e_shstrndx);
if (idx >= object->shdr_len) {
ERROR("shstrndx [%d] out of bounds", idx);
return M_ERROR;
}
struct string_table *strtab = &object->strtabs[idx];
if (strtab->len < 1) {
ERROR("shstrndx is invalid or empty");
return M_ERROR;
}
object->shstrtab = strtab;
return M_SUCCESS;
}
/**
* Load the program segments
*/
static int load_segments(struct object *object)
{
object->segment_len = B16(object->ehdr->e_phnum);
object->segments = malloc(sizeof(struct segment) *
object->segment_len);
if (object->segments == NULL) {
PERROR("cannot alloc");
return M_ERROR;
}
for (size_t i = 0; i < object->segment_len; i++) {
struct segment *seg = &object->segments[i];
if (segment_load(object, seg, i)) {
return M_ERROR;
}
}
return M_SUCCESS;
}
static int map_file(struct object *obj, char *path)
{
obj->fd = open(path, O_RDONLY);
if (obj->fd == -1) {
PERROR("cannot read");
return M_ERROR;
}
struct stat st;
if (stat(path, &st)) {
PERROR("cannot stat");
return M_ERROR;
}
obj->mapped_size = st.st_size;
obj->mapped = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
obj->fd, 0);
if (obj->mapped == MAP_FAILED) {
PERROR("cannot map");
return M_ERROR;
}
return M_SUCCESS;
}
int object_load(struct object *object, char *path)
{
current_file = path;
object->fd = 0;
object->segments = NULL;
object->symtabs = NULL;
object->strtabs = NULL;
object->mapped = NULL;
object->name = path;
/** load the file */
if (map_file(object, path))
return M_ERROR;
/* ehdr */
if (load_ehdr(object))
return M_ERROR;
/* shdr */
if (load_shdr(object))
return M_ERROR;
/* phdr */
if (load_phdr(object))
return M_ERROR;
/* strtabs */
if (load_strtabs(object))
return M_ERROR;
/* symtabs */
if (load_symtabs(object))
return M_ERROR;
/* shstrtab */
if (load_shstrtab(object))
return M_ERROR;
/* segments */
if (load_segments(object))
return M_ERROR;
return M_SUCCESS;
}
void object_free(struct object *obj)
{
if (obj->symtabs != NULL)
free(obj->symtabs);
if (obj->strtabs != NULL)
free(obj->strtabs);
if (obj->segments != NULL)
free(obj->segments);
if (obj->fd > 0)
close(obj->fd);
if (obj->mapped != NULL && obj->mapped != MAP_FAILED)
munmap(obj->mapped, obj->mapped_size);
}
|