-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdojreader.cpp
467 lines (388 loc) · 14 KB
/
dojreader.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "dojreader.h"
dojreader::dojreader(char *file,int verb)
{
struct stat elf_stats; // fstat struct
elf_header=0;
okay=false;
verbose=verb;
if((infile = open(file, O_RDWR)) == ERR)
{
printf("couldn't open %s\n", file);
return;
}
if((fstat(infile, &elf_stats)))
{
printf("couldn't fstat %s\n", file);
return;
}
if((elf_header = (Elf32_Ehdr *) malloc((size_t)(elf_stats.st_size))) == NULL)
{
printf("couldn't malloc\n");
return;
}
if((read(infile, elf_header, (size_t)(elf_stats.st_size))) < elf_stats.st_size)
{
printf("couldn't read %s\n", file);
return;
}
okay=true;
/* Check libelf version first */
if(elf_version(EV_CURRENT) == EV_NONE)
printf("WARNING Elf Library is out of date!\n");
Elf *start;
start = elf_begin(infile, ELF_C_READ, NULL); // Initialize 'elf' pointer to our file descriptor
if(elf_kind(start)==ELF_K_ELF)
{
elf=start;
process_file();
return;
}
if(elf_kind(start)==ELF_K_AR)
{
while((elf=elf_begin(infile,ELF_C_READ,start))!=NULL)
{
Elf_Arhdr *arh;
if((arh=elf_getarhdr(elf))==NULL)
errx(EX_SOFTWARE,"elf_getarhdr() failed:%s.",elf_errmsg(-1));
printf("%20s %d\n",arh->ar_name,(int)(arh->ar_size));
if(elf_kind(start)==ELF_K_ELF)
process_file();
elf_next(elf);
elf_end(elf);
}
return;
}
printf("Filetype unknown or wrong\n");
}
dojreader::~dojreader()
{
if(elf_header) free(elf_header);
if(infile) close(infile);
}
void dojreader::process_file()
{
find_functionblocks();
fill_datablocks();
fill_symbols();
process_relocations();
if(verbose)
{
//printf("Found %u symbols\n",fb.getsymbolcount());
fb.printsymbols();
}
}
void dojreader::find_functionblocks()
{
int i=0;
Elf_Data *edata=0;
GElf_Sym symbol;
GElf_Shdr shdr;
Elf_Scn *secdescr=0;
while((secdescr = elf_nextscn(elf,secdescr))!=NULL)
{
gelf_getshdr(secdescr, &shdr);
if(shdr.sh_type == SHT_SYMTAB)
{
edata = elf_getdata(secdescr, edata);
for(i = 0;i < (int)(shdr.sh_size/shdr.sh_entsize); i++)
{
gelf_getsym(edata, (int)i, &symbol);
if(!strncmp(elf_strptr(elf, shdr.sh_link, symbol.st_name),".LN.",4))
{
//found end
char name[256];
strncpy(name,elf_strptr(elf, shdr.sh_link, symbol.st_name)+4,256);
name[strlen(name)-4]=0;
fb.setblockend(name,((uint32_t)symbol.st_value));
}
else if(!strncmp(elf_strptr(elf, shdr.sh_link, symbol.st_name),".LN",3))
{
//found start
char name[256];
strncpy(name,elf_strptr(elf, shdr.sh_link, symbol.st_name)+3,256);
fb.setblockstart(name,((uint32_t)symbol.st_value));
}
}
}
}
}
void dojreader::fill_datablocks()
{
Elf_Scn *scn=0;
Elf_Data *edata=0;
GElf_Shdr shdr;
scn=0;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
if(shdr.sh_type == SHT_PROGBITS)
{
if(!strcmp("program",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name)))
{
edata = elf_getdata(scn, 0);
fb.setdata(((const unsigned char*)(edata->d_buf)),edata->d_size);
}
}
}
}
void dojreader::process_relocations()
{
Elf_Scn *scn=0;
Elf_Data *edata=0;
GElf_Rela rela;
GElf_Shdr shdr;
scn=0;
unsigned int symbol_count;
unsigned int i;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
if((shdr.sh_type == SHT_RELA) &&
(!strcmp(".rela.program",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name))))
{
edata=0;
edata = elf_getdata(scn, edata);
symbol_count = (unsigned int)(shdr.sh_size / shdr.sh_entsize);
// process all relocations
for(i = 0; i < symbol_count; i++)
{
if(gelf_getrela(edata,(int)i,&rela))
{
int symnum=(int)GELF_R_SYM(rela.r_info);
fb.relocate(((uint32_t)GELF_R_TYPE(rela.r_info)),(uint32_t)rela.r_offset,symb.getoffset(symnum),symb.getname(symnum));
}
else
printf("%6d: err\n",i);
}
}
}
}
void dojreader::process_sections()
{
Elf_Scn *scn=0;
Elf_Data *edata=0;
GElf_Rela rela;
GElf_Shdr shdr;
scn=0;
unsigned int symbol_count;
unsigned int i;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
if((shdr.sh_type == SHT_RELA) &&
(!strcmp(".rela.program",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name))))
{
edata=0;
edata = elf_getdata(scn, edata);
symbol_count = (unsigned int)(shdr.sh_size / shdr.sh_entsize);
printf("%x xxx\n",(int)((uint64_t)edata));
// loop through to grab all symbols
for(i = 0; i < symbol_count; i++)
{
if(gelf_getrela(edata,(int)i,&rela))
printf("%6d: %08x %08x %08x\n",
i,
((int32_t)rela.r_offset),
((int32_t)GELF_R_TYPE(rela.r_info)),
((int32_t)GELF_R_SYM(rela.r_info)));
else
printf("%6d: err\n",i);
}
}
else if(shdr.sh_type == SHT_PROGBITS)
{
edata = elf_getdata(scn, 0);
fb.setdata((const unsigned char*)edata,edata->d_size);
}
}
}
void dojreader::fill_symbols()
{
Elf_Scn *scn=0;
Elf_Data *edata=0;
GElf_Sym sym;
GElf_Shdr shdr;
unsigned int symbol_count;
unsigned int i;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
if((shdr.sh_type == SHT_SYMTAB) &&
(!strcmp(".symtab",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name))))
{
edata = elf_getdata(scn, edata);
symbol_count = (unsigned int)(shdr.sh_size / shdr.sh_entsize);
for(i = 0; i < symbol_count; i++)
{
// libelf grabs the symbol data using gelf_getsym()
gelf_getsym(edata, (int)i, &sym);
symbol *s=new symbol(
((int32_t)sym.st_value),
((int32_t)sym.st_shndx),
sym.st_info,
(const char*)elf_strptr(elf, shdr.sh_link, sym.st_name));
symb.addsymbol(i,*s);
}
}
}
}
void dojreader::print_pattern()
{
fb.printpattern();
}
void dojreader::print_all()
{
Elf_Scn *scn=0;
Elf_Data *edata=0;
GElf_Sym sym;
GElf_Rela rela;
GElf_Shdr shdr;
if(!okay)
{
printf("file not correct loaded !\n");
return;
}
while((scn = elf_nextscn(elf, scn)) != 0)
{
// point shdr at this section header entry
gelf_getshdr(scn, &shdr);
// print the section header type
printf("Type: ");
switch(shdr.sh_type)
{
case SHT_NULL: printf( "SHT_NULL\t"); break;
case SHT_PROGBITS: printf( "SHT_PROGBITS"); break;
case SHT_SYMTAB: printf( "SHT_SYMTAB"); break;
case SHT_STRTAB: printf( "SHT_STRTAB"); break;
case SHT_RELA: printf( "SHT_RELA\t"); break;
case SHT_HASH: printf( "SHT_HASH\t"); break;
case SHT_DYNAMIC: printf( "SHT_DYNAMIC"); break;
case SHT_NOTE: printf( "SHT_NOTE\t"); break;
case SHT_NOBITS: printf( "SHT_NOBITS"); break;
case SHT_REL: printf( "SHT_REL\t"); break;
case SHT_SHLIB: printf( "SHT_SHLIB"); break;
case SHT_DYNSYM: printf( "SHT_DYNSYM"); break;
case SHT_INIT_ARRAY: printf( "SHT_INIT_ARRAY"); break;
case SHT_FINI_ARRAY: printf( "SHT_FINI_ARRAY"); break;
case SHT_PREINIT_ARRAY: printf( "SHT_PREINIT_ARRAY"); break;
case SHT_GROUP: printf( "SHT_GROUP"); break;
case SHT_SYMTAB_SHNDX: printf( "SHT_SYMTAB_SHNDX"); break;
case SHT_NUM: printf( "SHT_NUM\t"); break;
case SHT_LOOS: printf( "SHT_LOOS\t"); break;
case SHT_GNU_verdef: printf( "SHT_GNU_verdef"); break;
case SHT_GNU_verneed: printf( "SHT_VERNEED"); break;
case SHT_GNU_versym: printf( "SHT_GNU_versym"); break;
default: printf( "(none) "); break;
}
// print the section header flags
printf("\t(");
if(shdr.sh_flags & SHF_WRITE) { printf("W"); }
if(shdr.sh_flags & SHF_ALLOC) { printf("A"); }
if(shdr.sh_flags & SHF_EXECINSTR) { printf("X"); }
if(shdr.sh_flags & SHF_STRINGS) { printf("S"); }
printf(")\t");
// the shdr name is in a string table, libelf uses elf_strptr() to find it
// using the e_shstrndx value from the elf_header
printf("%s\n", elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name));
}
// Iterate through section headers again this time well stop when we find symbols
scn=0;
unsigned int symbol_count;
unsigned int i;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
// When we find a section header marked SHT_SYMTAB stop and get symbols
if((shdr.sh_type == SHT_SYMTAB) &&
(!strcmp(".symtab",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name))))
{
printf("%s\n",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name));
// edata points to our symbol table
edata = elf_getdata(scn, edata);
// how many symbols are there? this number comes from the size of
// the section divided by the entry size
symbol_count = (unsigned int)(shdr.sh_size / shdr.sh_entsize);
// loop through to grab all symbols
for(i = 0; i < symbol_count; i++)
{
// libelf grabs the symbol data using gelf_getsym()
gelf_getsym(edata, (int)i, &sym);
// print out the value and size
printf("%6d: %08x %d ", i, ((int32_t)sym.st_value), ((int32_t)sym.st_shndx));
// type of symbol binding
switch(ELF32_ST_BIND(sym.st_info))
{
case STB_LOCAL: printf("LOCAL"); break;
case STB_GLOBAL: printf("GLOBAL"); break;
case STB_WEAK: printf("WEAK"); break;
case STB_NUM: printf("NUM"); break;
case STB_LOOS: printf("LOOS"); break;
case STB_HIOS: printf("HIOS"); break;
case STB_LOPROC: printf("LOPROC"); break;
case STB_HIPROC: printf("HIPROC"); break;
default: printf("UNKNOWN"); break;
}
printf("\t");
// type of symbol
switch(ELF32_ST_TYPE(sym.st_info))
{
case STT_NOTYPE: printf("NOTYPE"); break;
case STT_OBJECT: printf("OBJECT"); break;
case STT_FUNC: printf("FUNC"); break;
case STT_SECTION: printf("SECTION"); break;
case STT_FILE: printf("FILE"); break;
case STT_COMMON: printf("COMMON"); break;
case STT_TLS: printf("TLS"); break;
case STT_NUM: printf("NUM"); break;
case STT_LOOS: printf("LOOS"); break;
case STT_HIOS: printf("HIOS"); break;
case STT_LOPROC: printf("LOPROC"); break;
case STT_HIPROC: printf("HIPROC"); break;
default: printf("UNKNOWN"); break;
}
printf("\t");
// the name of the symbol is somewhere in a string table
// we know which one using the shdr.sh_link member
// libelf grabs the string using elf_strptr()
printf("%s\n", elf_strptr(elf, shdr.sh_link, sym.st_name));
/*symbol *s=new symbol(
((int32_t)sym.st_value),
((int32_t)sym.st_shndx),
sym.st_info,
(const char*)elf_strptr(elf, shdr.sh_link, sym.st_name));
symb.addsymbol(i,*s);*/
}
}
else if((shdr.sh_type == SHT_RELA) &&
(!strcmp(".rela.program",elf_strptr(elf, elf_header->e_shstrndx, shdr.sh_name))))
{
edata=0;
edata = elf_getdata(scn, edata);
symbol_count = (unsigned int)(shdr.sh_size / shdr.sh_entsize);
printf("%x xxx\n",(int)((uint64_t)edata));
// loop through to grab all symbols
for(i = 0; i < symbol_count; i++)
{
if(gelf_getrela(edata,(int)i,&rela))
printf("%6d: %08x %08x %08x\n",
i,
((int32_t)rela.r_offset),
((int32_t)GELF_R_TYPE(rela.r_info)),
((int32_t)GELF_R_SYM(rela.r_info)));
else
printf("%6d: err\n",i);
}
}
else if(shdr.sh_type == SHT_PROGBITS)
{
edata = elf_getdata(scn, 0);
for(i=0;i<(edata->d_size);i++)
{
if(!(i%16))
printf("\n");
printf(" %02x",((unsigned char*)(edata->d_buf))[i]);
}
printf("\n");
}
}
}