-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiself.e
577 lines (481 loc) · 16.2 KB
/
diself.e
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
-> DisELF by Leif Salomonsson [ecx tele2 se] is Copyright (c) 2003-2007.
-> Released under ECX TOOLS LICENSE, see ECXTOOLSLICENSE.TXT
OPT PREPROCESS
MODULE '*eecelf'
MODULE '*ppcdisasm'
-> Jan, Apr, Jul 2003
-> Jan 2004: added HIT option.
-> fixed mtspr, mfspr
-> Apr 2004: added SECT option: SECT sectname.
-> todo: show symbols within code disasm.
-> May 2004: now interprets .line sections.
-> fixed fabs in ppcdisasm.e
-> April 2007: fixes to HIT information..
-> April 2007: adjstments for sligthly changed .line format (numlines added)
-> Aug 2007: - fixed some STT_XXX names that was wrong.
-> - added bunch of reloc types missing
-> - added getrelocname(id,str), added mos specific relocs
-> - replaced cx() functions with char() func and fixed a bug, thx Stefan H.
-> June 2008: some improvements on page header display.
-> June 2011: adding symbol disassembly. also needs ecx now.
-> bug: only shows first smbol in a section that
-> matches a hit..
RAISE "FILE" IF FileLength()=-1,
"OPEN" IF Open()=NIL,
"ARGS" IF ReadArgs()=NIL,
"^C" IF CtrlC()=TRUE
PROC shr(x,v)
#ifndef ECX_VERSION
MOVE.L x, D0
MOVE.L v, D1
LSR.L D1, D0
ENDPROC D0
#endif
#ifdef ECX_VERSION
ENDPROC x SHR v
#endif
OBJECT args
file
dis
hit
sect
label
ENDOBJECT
STATIC phdrtypes = ['NULL',
'LOAD',
'DYNAMIC',
'INTERP',
'NOTE',
'SHLIB',
'PHDR',
'???']
STATIC shdrtypes = ['',
'PROGBITS',
'SYMTAB',
'STRTAB',
'RELA',
'HASH',
'DYNAMIC',
'7',
'NOBITS',
'REL',
'10',
'DYNSYM',
'???']
STATIC symbolbinds = ['LOCAL',
'GLOBAL',
'WEAK',
'???']
STATIC symboltypes = ['NOTYPE',
'OBJECT',
'FUNC',
'SECTION',
'FILE',
'COMMON',
'TLS',
'???']
DEF args:args
DEF fbuf, flen
PROC main() HANDLE
DEF fh=NIL, rdargs=NIL
DEF ehdr:PTR TO ehdr, phdr:PTR TO phdr, phnum
DEF shdr:PTR TO shdr, shnum
DEF shstrtable, a, b, t
DEF sym:PTR TO sym
DEF shdr2:PTR TO shdr, strtable
DEF rel:PTR TO rel, rela:PTR TO rela
DEF str[100]:STRING
DEF c, hit, sectname[12]:STRING
/* read agrs */
rdargs := ReadArgs('FILE/A,'+
'DIS/S,'+
'HIT/N,'+
'SECT/K,'+
'LABEL/K',
args, NIL)
flen := FileLength(args.file)
fbuf := NewR(flen+4)
fh := Open(args.file, OLDFILE)
Read(fh, fbuf, flen)
Close(fh) ; fh := NIL
WriteF('\s -> \q\s\q\n', {verstr}, args.file)
hit := IF args.hit THEN Long(args.hit) ELSE -1
IF args.sect THEN StrCopy(sectname, args.sect)
ehdr := fbuf
IF ehdr.ident[EI_MAG0] <> 127 THEN Throw("FORM", 'elf id')
IF ehdr.ident[EI_MAG1] <> "E" THEN Throw("FORM", 'elf id')
IF ehdr.ident[EI_MAG2] <> "L" THEN Throw("FORM", 'elf id')
IF ehdr.ident[EI_MAG3] <> "F" THEN Throw("FORM", 'elf id')
IF ehdr.ident[EI_CLASS] <> ELFCLASS32 THEN Raise("BITS")
IF ehdr.ident[EI_DATA] <> ELFDATA2MSB THEN Raise("ENDI")
IF hit > -1
showhit(hit, ehdr)
Raise(0)
ENDIF
IF args.label
show_label(ehdr, args.label)
Raise(0)
ENDIF
IF EstrLen(sectname) = NIL
WriteF('EHDR\n')
WriteF(' BITS=\d, ENDIAN=\s, IDENT[8-11]=\z$\h[8] IDENT[12-15]=\z$\h[8]\n',
IF Long(4+ehdr.ident) = 2 THEN 64 ELSE 32, IF Long(8+ehdr.ident) = 1 THEN 'LITTLE' ELSE 'BIG',Long(8+ehdr.ident) ,Long(12+ehdr.ident))
WriteF(' TYPE=\s, MACHINE=\d, VERSION=\d, FLAGS=$\h\n', ListItem(['0','REL','EXEC','DYN','???'], ehdr.type), ehdr.machine, ehdr.version, ehdr.flags)
WriteF(' ENTRY=\d, PHOFF=$\h, SHOFF=$\h\n', ehdr.entry, ehdr.phoff, ehdr.shoff)
WriteF(' EHSIZE=\d, PHENTSIZE=\d, PHNUM=\d\n', ehdr.ehsize, ehdr.phentsize, ehdr.phnum)
WriteF(' SHENTSIZE=\d, SHNUM=\d, SHSTRNDX=\d\n', ehdr.shentsize, ehdr.shnum, ehdr.shstrndx)
a := 0
WHILE phdr := get_phdr(ehdr, a)
WriteF('PHDR \d[3], \s[6], $\h[6], \d[6], \d[6], \d[6], \d[6], ', a,
IF phdr.type <= ListLen(phdrtypes) THEN ListItem(phdrtypes,phdr.type) ELSE StringF(str, '\d', phdr.type),
phdr.offset, phdr.vaddr, phdr.paddr, phdr.filesz, phdr.memsz)
t := phdr.flags
SetStr(str, 0)
IF t AND PF_R
StrAdd(str, 'R ')
t := t AND Not(PF_R)
ENDIF
IF t AND PF_W
StrAdd(str, 'W ')
t := t AND Not(PF_W)
ENDIF
IF t AND PF_X
StrAdd(str, 'X ')
t := t AND Not(PF_X)
ENDIF
IF t
WriteF('\h[8], \d\n', phdr.flags, phdr.align)
ELSE
WriteF('\s[6], \d\n', str, phdr.align)
ENDIF
a++
CtrlC()
ENDWHILE
ENDIF
shdr := get_shdr(ehdr, ehdr.shstrndx)
shstrtable := ehdr + shdr.offset
a := 0
WHILE shdr := get_shdr(ehdr, a)
IF EstrLen(sectname)
IF shdr.name < 0 THEN JUMP _sectionskip
IF StrCmp(shdr.name + shstrtable, sectname) = FALSE
JUMP _sectionskip
ENDIF
ENDIF
WriteF('SHDR \z\d[3] \l\s[12] \l\s[10] ', a,
IF shdr.name > -1 THEN shstrtable + shdr.name ELSE StringF(str, '$\h', shdr.name),
IF shdr.type <= ListLen(shdrtypes) THEN ListItem(shdrtypes,shdr.type) ELSE StringF(str, '\d', shdr.type))
t := shdr.flags
SetStr(str, 0)
IF t AND SHF_WRITE
StrAdd(str, 'WRITE ')
t := t AND Not(SHF_WRITE)
ENDIF
IF t AND SHF_ALLOC
StrAdd(str, 'ALLOC ')
t := t AND Not(SHF_ALLOC)
ENDIF
IF t AND SHF_EXECINSTR
StrAdd(str, 'EXECINSTR ')
t := t AND Not(SHF_EXECINSTR)
ENDIF
IF t THEN StringF(str, '$\h ', shdr.flags)
WriteF('\s[20]', str)
WriteF('\r\d[2] \d[2] (\z\d[3]) \d, $\h, \d, \d\n',
shdr.addralign, shdr.entsize, shdr.link,
shdr.size, shdr.offset, shdr.info, shdr.addr)
IF (shdr.type = SHT_SYMTAB) OR (shdr.type = SHT_DYNSYM)
shdr2 := get_shdr(ehdr, shdr.link)
strtable := ehdr + shdr2.offset
b := 0
WHILE sym := get_shent(ehdr, shdr, b)
WriteF(' SYM \r\z\d[6] (\z\d[3]) L\z\r\h[8] \l\s[7] \l\s[7] \a\s\a \d, \d\n', b++,
sym.shndx,
sym.value,
IF ST_BIND(sym.info) <= ListLen(symbolbinds) THEN ListItem(symbolbinds,
ST_BIND(sym.info)) ELSE StringF(str, '\d', ST_BIND(sym.info)),
IF ST_TYPE(sym.info) <= ListLen(symboltypes) THEN ListItem(symboltypes,
ST_TYPE(sym.info)) ELSE StringF(str, '\d', ST_TYPE(sym.info)),
strtable + sym.name,
sym.size,
sym.other)
CtrlC()
ENDWHILE
ELSEIF (shdr.type = SHT_REL) AND (args.dis<>FALSE)
shdr2 := get_shdr(ehdr, shdr.link) -> symbol shdr
sym := ehdr + shdr2.offset -> symboltable
shdr2 := get_shdr(ehdr, shdr2.link) -> str shdr
strtable := ehdr + shdr2.offset
b := 0
WHILE rel := get_shent(ehdr, shdr, b)
WriteF(' REL \r\z\d[6] (\z\d[3]) L\z\h[7] \a\s\a \s = L\z\h[7]\n',
b++,
R_SYM(rel.info),
rel.offset,
sym[R_SYM(rel.info)].name + strtable,
getrelocname(R_TYPE(rel.info), str),
peekSym(ehdr, sym[R_SYM(rel.info)], rel.offset))
CtrlC()
ENDWHILE
ELSEIF (shdr.type = SHT_RELA) AND (args.dis <> FALSE)
shdr2 := get_shdr(ehdr, shdr.link) -> symbol shdr
sym := ehdr + shdr2.offset -> symboltable
shdr2 := get_shdr(ehdr, shdr2.link) -> str shdr
strtable := ehdr + shdr2.offset
b := 0
WHILE rela := get_shent(ehdr, shdr, b)
WriteF(' RELA \r\z\d[6] (\z\d[3]) L\z\h[7] \a\s\a \s, $\h\n',
b++,
R_SYM(rela.info),
rela.offset,
sym[R_SYM(rela.info)].name + strtable,
getrelocname(R_TYPE(rela.info), str),
rela.addend)
CtrlC()
ENDWHILE
ELSEIF (shdr.type = SHT_PROGBITS) AND (args.dis<>FALSE)
b := 0
c := ehdr + shdr.offset
WHILE b < shdr.size
IF shdr.flags AND SHF_EXECINSTR
dodis(b, Long(c), str)
WriteF(' L\z\h[7]: \l\s[24] [\z\r\h[8]] \a\c\c\c\c\a (\d\n',
b, str, Long(c), char(c), char(c+1), char(c+2), char(c+3), b)
ELSE
WriteF(' L\z\h[7]: [\z\r\h[8]] \a\c\c\c\c\a (\d\n',
b, Long(c), char(c), char(c+1), char(c+2), char(c+3), b)
ENDIF
b := b + 4
c := c + 4
CtrlC()
ENDWHILE
ENDIF
_sectionskip:
a++
CtrlC()
ENDWHILE
WriteF('<END>\n')
EXCEPT DO
IF fh THEN Close(fh)
IF rdargs THEN FreeArgs(rdargs)
SELECT exception
CASE "FILE" ; WriteF('unable to open file.\n')
CASE "OPEN" ; WriteF('unable to open file.\n')
CASE "ARGS" ; WriteF('bad args.\n')
CASE "FORM" ; WriteF('format is not understood: \s.\n', exceptioninfo)
CASE "^C" ; WriteF('user aborted.\n')
CASE "BITS" ; WriteF('elf is not of 32bit type')
CASE "ENDI" ; WriteF('elf is not big endian')
ENDSELECT
ENDPROC
PROC getrelocname(id, str)
DEF relocnames, relocnames200
relocnames := ['NONE',
'ADDR32',
'ADDR24',
'ADDR16',
'ADDR16_LO',
'ADDR16_HI',
'ADDR16_HA',
'ADDR14',
'ADDR14_BRTAKEN',
'ADDR14_BRNTAKEN',
'REL24',
'REL14',
'REL14_BRTAKEN',
'REL14_BRNTAKEN',
'GOT16',
'GOT16_LO',
'GOT16_HI',
'GOT16_HA',
'PLTREL24',
'COPY',
'GLOB_DAT',
'JMP_SLOT',
'RELATIVE',
'LOCAL24PC',
'UADDR32',
'UADDR16',
'REL32',
'PLT32',
'PLTREL32',
'PLT16_LO',
'PLT16_HI',
'PLT16_HA',
'SDAREL16',
'SECTOFF',
'SECTOFF_LO',
'SECTOFF_HI',
'SECTOFF_HA']
relocnames200 := ['MORPHOS_DREL',
'MORPHOS_DREL_LO',
'MORPHOS_DREL_HI',
'MORPHOS_DREL_HA']
IF id < ListLen(relocnames)
StrCopy(str, ListItem(relocnames,id))
ELSEIF id < (ListLen(relocnames200)+200)
StrCopy(str, ListItem(relocnames200, id-200))
ELSE
StringF(str, '\d', id)
ENDIF
ENDPROC str
PROC peekSym(ehdr:PTR TO ehdr, sym:PTR TO sym, offset)
DEF shdr:PTR TO shdr
shdr := get_shdr(ehdr, sym.shndx)
ENDPROC Long(ehdr + shdr.offset + offset)
PROC char(ptr)
DEF c
c := Char(ptr)
IF (c > 31) AND (c < 127) THEN RETURN c
ENDPROC "."
CHAR 0, '$VER:'
verstr:
CHAR 'DisELF 1.10 by LS 2003-11', 0
OBJECT line
line
offset
ENDOBJECT
PROC showhit(ofs:PTR TO LONG, ehdr:PTR TO ehdr)
DEF shdr:PTR TO shdr, shnum
DEF shstrtable, a, b, t, c:PTR TO LONG
DEF shdr2:PTR TO shdr, strtable
DEF str[100]:STRING
DEF sym:PTR TO sym, offsetsym:PTR TO sym
DEF lptr:PTR TO LONG, d, line:PTR TO line, tline:PTR TO line
ofs := ofs AND $FFFFFC
shdr := get_shdr(ehdr, ehdr.shstrndx)
shstrtable := ehdr + shdr.offset
a := 0
WHILE shdr := get_shdr(ehdr, a)
IF shdr.type = SHT_PROGBITS
IF shdr.flags AND SHF_EXECINSTR
IF shdr.size > ofs
WriteF('\n HIT at offset $\h in section \d:\n\n', ofs, a)
IF ofs > 3
c := ehdr + shdr.offset + ofs - 4
dodis(ofs-4, c[], str)
WriteF(' L\z\h[7]: \l\s[24] [\z\r\h[8]] \a\c\c\c\c\a\n',
ofs-4, str, c[], char(c), char(c+1), char(c+2), char(c+3))
ENDIF
c := ehdr + shdr.offset + ofs
dodis(ofs, c[], str)
WriteF('-->L\z\h[7]: \l\s[24] [\z\r\h[8]] \a\c\c\c\c\a\n',
ofs, str, c[], char(c), char(c+1), char(c+2), char(c+3))
c := ehdr + shdr.offset + ofs + 4
dodis(ofs+4, c[], str)
WriteF(' L\z\h[7]: \l\s[24] [\z\r\h[8]] \a\c\c\c\c\a\n',
ofs+4, str, c[], char(c), char(c+1), char(c+2), char(c+3))
ENDIF ; ENDIF ; ENDIF
IF (shdr.type = SHT_SYMTAB) OR (shdr.type = SHT_DYNSYM)
sym := ehdr + shdr.offset -> symboltable
shdr2 := get_shdr(ehdr, shdr.link) ->
strtable := ehdr + shdr2.offset
offsetsym := NIL
FOR b := 0 TO (shdr.size / shdr.entsize) -1
IF sym.shndx > 0
IF sym.value <= ofs
IF offsetsym
IF sym.value > offsetsym.value THEN offsetsym := sym
ELSE
offsetsym := sym
ENDIF
ENDIF
ENDIF
sym := sym + shdr.entsize
CtrlC()
ENDFOR
IF offsetsym
IF StrLen(offsetsym.name + strtable)
WriteF('\n Below symbol \a\s\a ($\h) in section \d\n',
strtable + offsetsym.name,
offsetsym.value,
offsetsym.shndx)
ENDIF
ENDIF
ENDIF
IF (shdr.type = SHT_PROGBITS) AND (shdr.flags = NIL) AND (shdr.name > 0)
IF StrCmp(shdr.name + shstrtable, '.ldbg')
tline := NIL
d := NIL
lptr := shdr.offset + ehdr
WHILE lptr[]++ = "LDBG"
->WriteF('"LINE" numlines=\d name=\s\n', lptr[], lptr + 8)
t := lptr[]++ -> numlines
b := lptr + 4 -> save ptr to name
line := lptr + Shl(lptr[]++, 2) + 4-> jump over name
->WriteF('low offset=\d, highoffset=\d\n', line.offset, line[t-1].offset)
IF (ofs >= line.offset) AND (ofs <= line[t-1].offset)
d := b
WHILE t
t--
IF line.offset <= ofs THEN tline := line
line++
ENDWHILE
lptr := line
ELSE
lptr := line + Mul(t, SIZEOF line)
ENDIF
EXIT d
ENDWHILE
IF d
WriteF('\n Source: "\s"\n Line: \d\n', d, tline.line)
ENDIF
ENDIF
ENDIF
a++
CtrlC()
ENDWHILE
WriteF('<END>\n')
ENDPROC
-> 1.10
PROC get_phdr(ehdr:PTR TO ehdr, i)
IF i >= ehdr.phnum THEN RETURN NIL
ENDPROC ehdr + ehdr.phoff + (i * ehdr.phentsize)
PROC get_shdr(ehdr:PTR TO ehdr, i)
IF i >= ehdr.shnum THEN RETURN NIL
ENDPROC ehdr + (ehdr.shoff) + (ehdr.shentsize * i)
PROC get_shent(ehdr:PTR TO ehdr, shdr:PTR TO shdr, i)
IF (i * shdr.entsize) >= shdr.size THEN RETURN NIL
ENDPROC ehdr + shdr.offset + (shdr.entsize * i)
PROC find_sym(ehdr:PTR TO ehdr, name)
DEF a, shdr:PTR TO shdr, sym:PTR TO sym, shdr2:PTR TO shdr, b, strtable
a := 0
WHILE shdr := get_shdr(ehdr, a)
IF (shdr.type = SHT_SYMTAB) OR (shdr.type = SHT_DYNSYM)
shdr2 := get_shdr(ehdr, shdr.link) ->
strtable := ehdr + shdr2.offset
b := 0
WHILE sym := get_shent(ehdr, shdr, b++)
IF StrCmp(name, strtable + sym.name) THEN RETURN sym
ENDWHILE
ENDIF
a++
ENDWHILE
ENDPROC NIL
PROC show_label(ehdr, name)
DEF sym:PTR TO sym, shdr:PTR TO shdr, ofs:PTR TO LONG, endofs, c:PTR TO LONG
DEF str[128]:STRING
sym := find_sym(ehdr, name)
IFN sym THEN RETURN NIL
IF ST_TYPE(sym.info) = NIL THEN RETURN NIL
shdr := get_shdr(ehdr, sym.shndx)
IFN shdr THEN RETURN NIL
IF shdr.type <> SHT_PROGBITS THEN RETURN NIL
WriteF('\nLabel "\s" in section \d @ offset $\h with size \d and other \d:\n\n',
name,
sym.shndx,
sym.value,
sym.size,
sym.other)
ofs := sym.value
endofs := ofs + sym.size
c := ehdr + shdr.offset + ofs
WHILE ofs < endofs
dodis(ofs, c[], str)
WriteF(' L\z\h[7]: \l\s[24] [\z\r\h[8]] \a\c\c\c\c\a\n',
ofs, str, c[], char(c), char(c+1), char(c+2), char(c+3))
ofs++
c++
ENDWHILE
WriteF('\n')
ENDPROC TRUE