-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathloadelf.c
366 lines (320 loc) · 11.5 KB
/
loadelf.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
/* loadelf.c - an elf loader for the Parallax Propeller microcontroller
Copyright (c) 2011 David Michael Betz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "loadelf.h"
#define message printf
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define IDENT_SIGNIFICANT_BYTES 9
static uint8_t ident[] = {
0x7f, 'E', 'L', 'F', // magic number
0x01, // class
0x01, // data
0x01, // version
0x00, // os / abi identification
0x00, // abi version
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // padding
};
static int FindProgramTableEntry(ElfContext *c, ElfSectionHdr *section, ElfProgramHdr *program);
static void ShowSectionHdr(ElfSectionHdr *section);
static void ShowProgramHdr(ElfProgramHdr *program);
int ReadAndCheckElfHdr(FILE *fp, ElfHdr *hdr)
{
if (fread(hdr, 1, sizeof(ElfHdr), fp) != sizeof(ElfHdr))
return FALSE;
return memcmp(ident, hdr->ident, IDENT_SIGNIFICANT_BYTES) == 0;
}
ElfContext *OpenElfFile(FILE *fp, ElfHdr *hdr)
{
ElfSectionHdr section;
ElfContext *c;
/* allocate and initialize a context structure */
if (!(c = (ElfContext *)malloc(sizeof(ElfContext))))
return NULL;
memset(c, 0, sizeof(ElfContext));
c->hdr = *hdr;
c->fp = fp;
/* get the string section offset */
if (!LoadSectionTableEntry(c, c->hdr.shstrndx, §ion)) {
free(c);
return NULL;
}
c->stringOff = section.offset;
/* get the symbol table section offset */
if (FindSectionTableEntry(c, ".symtab", §ion)) {
c->symbolOff = section.offset;
c->symbolCnt = section.size / sizeof(ElfSymbol);
if (FindSectionTableEntry(c, ".strtab", §ion))
c->symbolStringOff = section.offset;
else {
c->symbolOff = 0;
c->symbolCnt = 0;
}
}
/* return the context */
return c;
}
void FreeElfContext(ElfContext *c)
{
free(c);
}
void CloseElfFile(ElfContext *c)
{
if (c->fp) {
fclose(c->fp);
c->fp = 0;
}
}
int GetProgramSize(ElfContext *c, uint32_t *pStart, uint32_t *pSize, uint32_t *pCogImagesSize)
{
ElfProgramHdr program;
uint32_t start = 0xffffffff;
uint32_t end = 0;
uint32_t cogImagesStart = 0xffffffff;
uint32_t cogImagesEnd = 0;
int cogImagesFound = FALSE;
int i;
for (i = 0; i < c->hdr.phnum; ++i) {
if (!LoadProgramTableEntry(c, i, &program)) {
message("Can't read ELF program header %d", i);
return FALSE;
}
if (program.paddr < COG_DRIVER_IMAGE_BASE) {
if (program.paddr < start)
start = program.paddr;
if (program.paddr + program.filesz > end)
end = program.paddr + program.filesz;
}
else {
if (program.paddr < cogImagesStart)
cogImagesStart = program.paddr;
if (program.paddr + program.filesz > cogImagesEnd)
cogImagesEnd = program.paddr + program.filesz;
cogImagesFound = TRUE;
}
}
*pStart = start;
*pSize = end - start;
*pCogImagesSize = cogImagesFound ? cogImagesEnd - cogImagesStart : 0;
return TRUE;
}
int FindProgramSegment(ElfContext *c, const char *name, ElfProgramHdr *program)
{
ElfSectionHdr section;
if (!FindSectionTableEntry(c, name, §ion))
return -1;
return FindProgramTableEntry(c, §ion, program);
}
uint8_t *LoadProgramSegment(ElfContext *c, ElfProgramHdr *program)
{
uint8_t *buf;
if (!(buf = (uint8_t *)malloc(program->filesz)))
return NULL;
if (fseek(c->fp, program->offset, SEEK_SET) != 0) {
free(buf);
return NULL;
}
if (fread(buf, 1, program->filesz, c->fp) != program->filesz) {
free(buf);
return NULL;
}
return buf;
}
int FindSectionTableEntry(ElfContext *c, const char *name, ElfSectionHdr *section)
{
int i;
for (i = 0; i < c->hdr.shnum; ++i) {
char thisName[ELFNAMEMAX], *p = thisName;
int cnt, ch;
if (!LoadSectionTableEntry(c, i, section)) {
message("Can't read ELF section header %d", i);
return 1;
}
fseek(c->fp, c->stringOff + section->name, SEEK_SET);
for (cnt = 0; ++cnt < ELFNAMEMAX && (ch = getc(c->fp)) != '\0'; )
*p++ = ch;
*p = '\0';
if (strcmp(name, thisName) == 0)
return TRUE;
}
return FALSE;
}
int LoadSectionTableEntry(ElfContext *c, int i, ElfSectionHdr *section)
{
return fseek(c->fp, c->hdr.shoff + i * c->hdr.shentsize, SEEK_SET) == 0
&& fread(section, 1, sizeof(ElfSectionHdr), c->fp) == sizeof(ElfSectionHdr);
}
static int FindProgramTableEntry(ElfContext *c, ElfSectionHdr *section, ElfProgramHdr *program)
{
int i;
for (i = 0; i < c->hdr.shnum; ++i) {
if (!LoadProgramTableEntry(c, i, program)) {
message("Can't read ELF program header %d", i);
return -1;
}
if (SectionInProgramSegment(section, program))
return i;
}
return -1;
}
int LoadProgramTableEntry(ElfContext *c, int i, ElfProgramHdr *program)
{
return fseek(c->fp, c->hdr.phoff + i * c->hdr.phentsize, SEEK_SET) == 0
&& fread(program, 1, sizeof(ElfProgramHdr), c->fp) == sizeof(ElfProgramHdr);
}
int FindElfSymbol(ElfContext *c, const char *name, ElfSymbol *symbol)
{
int i;
for (i = 1; i < c->symbolCnt; ++i) {
char thisName[ELFNAMEMAX];
if (LoadElfSymbol(c, i, thisName, symbol) == 0 && strcmp(name, thisName) == 0)
return TRUE;
}
return FALSE;
}
int LoadElfSymbol(ElfContext *c, int i, char *name, ElfSymbol *symbol)
{
char *p = name;
if (fseek(c->fp, c->symbolOff + i * sizeof(ElfSymbol), SEEK_SET) != 0
|| fread(symbol, 1, sizeof(ElfSymbol), c->fp) != sizeof(ElfSymbol))
return -1;
if (symbol->name) {
int cnt, ch;
fseek(c->fp, c->symbolStringOff + symbol->name, SEEK_SET);
for (cnt = 0; ++cnt < ELFNAMEMAX && (ch = getc(c->fp)) != '\0'; )
*p++ = ch;
}
*p = '\0';
return 0;
}
void ShowElfFile(ElfContext *c)
{
ElfSectionHdr section;
ElfProgramHdr program;
int i;
/* show file header */
printf("ELF Header:\n");
printf(" ident: ");
for (i = 0; i < sizeof(c->hdr.ident); ++i)
printf(" %02x", c->hdr.ident[i]);
putchar('\n');
printf(" type: %04x\n", c->hdr.type);
printf(" machine: %04x\n", c->hdr.machine);
printf(" version: %08x\n", c->hdr.version);
printf(" entry: %08x\n", c->hdr.entry);
printf(" phoff: %08x\n", c->hdr.phoff);
printf(" shoff: %08x\n", c->hdr.shoff);
printf(" flags: %08x\n", c->hdr.flags);
printf(" ehsize: %d\n", c->hdr.entry);
printf(" phentsize: %d\n", c->hdr.phentsize);
printf(" phnum: %d\n", c->hdr.phnum);
printf(" shentsize: %d\n", c->hdr.shentsize);
printf(" shnum: %d\n", c->hdr.shnum);
printf(" shstrndx: %d\n", c->hdr.shstrndx);
/* show the section table */
for (i = 0; i < c->hdr.shnum; ++i) {
char name[ELFNAMEMAX], *p = name;
int cnt, ch;
if (!LoadSectionTableEntry(c, i, §ion)) {
printf("error: can't read section header %d\n", i);
return;
}
fseek(c->fp, c->stringOff + section.name, SEEK_SET);
for (cnt = 0; ++cnt < ELFNAMEMAX && (ch = getc(c->fp)) != '\0'; )
*p++ = ch;
*p = '\0';
printf("SectionHdr %d:\n", i);
printf(" name: %08x %s\n", section.name, name);
ShowSectionHdr(§ion);
}
/* show the program table */
for (i = 0; i < c->hdr.phnum; ++i) {
if (!LoadProgramTableEntry(c, i, &program)) {
printf("error: can't read program header %d\n", i);
return;
}
printf("ProgramHdr %d:\n", i);
ShowProgramHdr(&program);
}
/* show the symbol table */
for (i = 1; i < c->symbolCnt; ++i) {
char name[ELFNAMEMAX];
ElfSymbol symbol;
if (LoadElfSymbol(c, i, name, &symbol) == 0 && symbol.name && INFO_BIND(symbol.info) == STB_GLOBAL)
printf(" %08x %s: %08x\n", symbol.name, name, symbol.value);
}
}
static void ShowSectionHdr(ElfSectionHdr *section)
{
printf(" type: %08x\n", section->type);
printf(" flags: %08x\n", section->flags);
printf(" addr: %08x\n", section->addr);
printf(" offset: %08x\n", section->offset);
printf(" size: %08x\n", section->size);
printf(" link: %08x\n", section->link);
printf(" info: %08x\n", section->info);
printf(" addralign: %08x\n", section->addralign);
printf(" entsize: %08x\n", section->entsize);
}
static void ShowProgramHdr(ElfProgramHdr *program)
{
printf(" type: %08x\n", program->type);
printf(" offset: %08x\n", program->offset);
printf(" vaddr: %08x\n", program->vaddr);
printf(" paddr: %08x\n", program->paddr);
printf(" filesz: %08x\n", program->filesz);
printf(" memsz: %08x\n", program->memsz);
printf(" flags: %08x\n", program->flags);
printf(" align: %08x\n", program->align);
}
#ifdef MAIN
int main(int argc, char *argv[])
{
ElfContext *c;
ElfHdr hdr;
FILE *fp;
/* check the arguments */
if (argc != 2) {
printf("usage: loadelf <file>\n");
return 1;
}
/* open the image file */
if (!(fp = fopen(argv[1], "rb"))) {
printf("error: opening '%s'\n", argv[1]);
return 1;
}
/* make sure it's an elf file */
if (!ReadAndCheckElfHdr(fp, &hdr)) {
printf("error: not an elf file");
return 1;
}
/* open the elf file */
if (!(c = OpenElfFile(fp, &hdr))) {
printf("error: opening elf file\n");
return 1;
}
/* show the contents of the elf file */
ShowElfFile(c);
/* close the elf file */
CloseElfFile(c);
return 0;
}
#endif