mdump.c
11.6 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
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
#include <stdio.h>
#include <elf.h>
#include <sym.h>
#define NSECTS 32
FILE * in;
int verbose = 1;
int mtabindex;
Elf32_Ehdr header;
Elf32_Shdr sectHeader[32];
long sstabStart; /* start of string table for sections */
HDRR debugHeader;
char * typeNames[] = {
"nil",
"global",
"static",
"parm",
"local",
"label",
"proc",
"block",
"end",
"member",
"typedef",
"file",
"regreloc",
"forward",
"staticproc",
"constant"
};
void usage( void )
{
fprintf( stderr, "usage: mdump file\n" );
exit( 1 );
}
void DumpFileHeader()
{
register int i;
if ( fread( &header, sizeof( header ), 1, in ) != 1 )
{
fprintf( stderr, "Can't read header\n" );
fclose( in );
exit( 1 );
}
if ( (header.e_ident[0] != 0x7f) ||
(header.e_ident[1] != 'E') ||
(header.e_ident[2] != 'L') ||
(header.e_ident[3] != 'F') )
{
fprintf( stderr, "Not an ELF file...\n" );
exit( 1 );
}
if ( verbose )
{
if ( header.e_ident[4] == 1 )
printf( "32 bit " );
if ( header.e_ident[5] == 1 )
printf( "LSB " );
else if ( header.e_ident[5] == 2 )
printf( "MSB " );
printf( "ELF file\n" );
printf( "Type %x\n", header.e_type );
printf( "Entry %x\n", header.e_entry );
printf( "Progheader Offset %x\n", header.e_phoff );
printf( "Sectheader Offset %x\n", header.e_shoff );
printf( "Num progentries %x\n", header.e_phnum );
printf( "Num segentries %d\n", header.e_shnum );
printf( "Sectheader Index %x\n", header.e_shstrndx );
}
}
void ReadSection( int sectNum )
{
if ( fread( §Header[sectNum], sizeof( Elf32_Shdr ), 1, in ) != 1 )
{
fprintf( stderr, "Can't read section header\n" );
fclose( in );
exit( 1 );
}
}
void ShowSectionName( int index )
{
char sectnamebuf[32], *s;
if ( fseek( in, sstabStart + sectHeader[index].sh_name, SEEK_SET ) )
fprintf( stderr, "Seek error to address %lx\n",
sstabStart + sectHeader[index].sh_name );
else
{
int c;
s = sectnamebuf;
while ( c = getc( in ) )
*s++ = c;
*s = 0;
printf( "Section %d name is %s\n", index, sectnamebuf );
if ( !strcmp( sectnamebuf, ".mdebug" ) )
{
mtabindex = index;
printf( "Section type %x\n", sectHeader[index].sh_type );
}
}
}
DumpRawBits( int lines )
{
char buf[16];
register int i, j;
for ( i = 0; i < lines; ++i )
{
fread( buf, 16, 1, in );
for ( j = 0; j < 16; ++j )
printf( "%02x ", buf[j] & 0xff );
printf( "\n" );
for ( j = 0; j < 16; ++j )
printf( "%c ", isprint( buf[j] ) ? buf[j] : ' ' );
printf( "\n" );
}
}
void DumpFile( void )
{
FDR fH;
register int c, file;
long nextFile;
printf( "\nFile info:\n" );
if ( !debugHeader.ifdMax )
{
printf( "This file had no file info\n" );
return;
}
if ( fseek( in, debugHeader.cbFdOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
nextFile = ftell( in );
for ( file = 0; file < debugHeader.ifdMax; ++ file )
{
printf( "File %d\n", file );
if ( fread( &fH, sizeof( FDR ), 1, in ) != 1 )
{
fprintf( stderr, "Couldn't read file header\n" );
return;
}
nextFile = ftell( in );
printf( "Mem addr = %08x\n", fH.adr );
if ( fseek( in, debugHeader.cbSsOffset + fH.rss + fH.issBase, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
printf( "File name = " );
while ( (c = fgetc( in )) )
putchar( c );
printf( "\n" );
printf( "Strings, base = %d, count = %d\n", fH.issBase, fH.cbSs );
printf( "Symbols, base = %d, count = %d\n", fH.isymBase, fH.csym );
printf( "Line # entries, base = %d, count = %d\n", fH.ilineBase, fH.cline );
printf( "Opts, base = %d, count = %d\n", fH.ioptBase, fH.copt );
printf( "Procs, base = %d, count = %d\n", fH.ipdFirst, fH.cpd );
printf( "Aux, base = %d, count = %d\n", fH.iauxBase, fH.caux );
printf( "Fds, base = %d, count = %d\n", fH.rfdBase, fH.crfd );
printf( "Lines, offset = %d, size = %d\n", fH.cbLineOffset, fH.cbLine );
fseek( in, nextFile, SEEK_SET );
}
}
void DumpLocalStrings( void )
{
register int i, chars = 0;
printf( "\nLocal Strings:\n" );
if ( !debugHeader.issMax )
{
printf( "This file had no local strings\n" );
return;
}
if ( fseek( in, debugHeader.cbSsOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; ; ++i )
{
int c;
printf( "(%d) start %d: ", i, chars );
while ( c = fgetc( in ) )
{
putchar( c );
++chars;
}
printf( "\n" );
++chars;
if ( chars >= debugHeader.issMax )
break;
}
}
void DumpExtStrings( void )
{
register int i, chars = 0;
printf( "\nExt Strings:\n" );
if ( !debugHeader.issExtMax )
{
printf( "This file had no ext strings\n" );
return;
}
if ( fseek( in, debugHeader.cbSsExtOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; ; ++i )
{
int c;
printf( "(%d) start %d: ", i, chars );
while ( c = fgetc( in ) )
{
putchar( c );
++chars;
}
printf( "\n" );
++chars;
if ( chars >= debugHeader.issExtMax )
break;
}
}
void DumpLocals( void )
{
register int i;
SYMR symbol;
long whereIwas;
printf( "\nLocal Symbols:\n" );
if ( !debugHeader.isymMax )
{
printf( "This file had no local symbols\n" );
return;
}
if ( fseek( in, debugHeader.cbSymOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; i < debugHeader.isymMax; ++i )
{
int c;
if ( fread( &symbol, sizeof( SYMR ), 1, in ) != 1 )
{
fprintf( stderr, "Read failed in DumpLocals\n" );
return;
}
whereIwas = ftell( in );
fseek( in, debugHeader.cbSsOffset + symbol.iss, 0 );
printf( "Symbol %d:\n", i );
printf( "sym string index %d\tsym value %x\tsym type %s (%d)\n",
symbol.iss, symbol.value, typeNames[symbol.st],
symbol.st );
printf( "sym class %d\tsym index %d\n",
symbol.sc,
symbol.index == 0xfffff ? -1 : symbol.index );
fseek( in, whereIwas, 0 );
printf( "\n" );
}
}
void DumpLineInfo( void )
{
register int i;
EXTR symbol;
long whereIwas;
printf( "\nLine Info:\n" );
if ( !debugHeader.cbLine )
{
printf( "This file had no line info\n" );
return;
}
if ( fseek( in, debugHeader.cbLineOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
printf( "%d bytes valid:\n", debugHeader.cbLine );
DumpRawBits( (debugHeader.cbLine + 16) / 16 );
}
void DumpGlobals( void )
{
register int i;
EXTR symbol;
long whereIwas;
printf( "\nGlobal Symbols:\n" );
if ( !debugHeader.iextMax )
{
printf( "This file had no global symbols\n" );
return;
}
if ( fseek( in, debugHeader.cbExtOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; i < debugHeader.iextMax; ++i )
{
int c;
if ( fread( &symbol, sizeof( EXTR ), 1, in ) != 1 )
{
fprintf( stderr, "Read failed in DumpGlobals\n" );
return;
}
whereIwas = ftell( in );
fseek( in, debugHeader.cbSsExtOffset + symbol.asym.iss, 0 );
printf( "Symbol name = " );
while ( (c = fgetc( in )) )
putchar( c );
printf( "\n" );
printf( "sym string index %d\tsym value %x\tsym type %s\n",
symbol.asym.iss, symbol.asym.value,
typeNames[symbol.asym.st] );
printf( "sym class %d\tsym index %d\tsym ifd %d\n",
symbol.asym.sc,
symbol.asym.index == 0xfffff ? -1 : symbol.asym.index,
symbol.ifd );
fseek( in, whereIwas, 0 );
printf( "\n" );
}
}
void DumpAux( void )
{
register int i;
printf( "\nAux Symbols:\n" );
if ( !debugHeader.iauxMax )
{
printf( "This file had no aux symbols\n" );
return;
}
if ( fseek( in, debugHeader.cbAuxOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; i < debugHeader.iauxMax; ++i )
{
AUXU a;
if ( fread( &a, sizeof( AUXU ), 1, in ) != 1 )
{
fprintf( stderr, "Read failed in DumpAux\n" );
return;
}
printf( "%08x\n", a );
}
}
void DumpRelInfo( void )
{
register int i;
printf( "\nRel Files:\n" );
if ( !debugHeader.crfd )
{
printf( "This file had no rel files\n" );
return;
}
if ( fseek( in, debugHeader.cbRfdOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; i < debugHeader.crfd; ++i )
{
RNDXR r;
if ( fread( &r, sizeof( RNDXR ), 1, in ) != 1 )
{
fprintf( stderr, "Read failed in DumpRelInfo\n" );
return;
}
printf( "rfd %d, string index %d\n", r.rfd, r.index );
}
}
void DumpProcs( void )
{
register int i;
printf( "\nProcs:\n" );
if ( !debugHeader.ipdMax )
{
printf( "This file had no procs\n" );
return;
}
if ( fseek( in, debugHeader.cbPdOffset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
return;
}
for ( i = 0; i < debugHeader.ipdMax; ++i )
{
PDR r;
if ( fread( &r, sizeof( PDR ), 1, in ) != 1 )
{
fprintf( stderr, "Read failed in DumpProcs\n" );
return;
}
printf( "Proc %d:\n", i );
printf( "\tStarts %08x 1st sym %d 1st line rec %d range %d:%d\n",
r.adr, r.isym, r.iline, r.lnLow, r.lnHigh );
}
}
void DumpDebug( void )
{
if ( !mtabindex )
{
printf( "This file has no .mdebug section\n" );
fclose( in );
exit( 0 );
}
printf( ".mdebug is section %d at offset %04x\n", mtabindex,
sectHeader[mtabindex].sh_offset );
if ( fseek( in, sectHeader[mtabindex].sh_offset, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in input file\n" );
exit( 1 );
}
if ( fread( &debugHeader, sizeof( HDRR ), 1, in ) != 1 )
{
fprintf( stderr, "Couldn't read debug record\n" );
exit( 1 );
}
printf( "magic = %04x\n", debugHeader.magic );
printf( "vstanp = %04x\n", debugHeader.vstamp );
printf( "line tab size = %d\n", debugHeader.ilineMax );
printf( "bytes per line = %d\n", debugHeader.cbLine );
printf( "offset of line info = %04x\n", debugHeader.cbLineOffset );
printf( "dense # tab size = %d\t", debugHeader.idnMax );
printf( "offset of dense # = %04x\n", debugHeader.cbDnOffset );
printf( "proc tab size = %d\t", debugHeader.ipdMax );
printf( "proc tab offset = %04x\n", debugHeader.cbPdOffset );
printf( "local sym size = %d\t", debugHeader.isymMax );
printf( "local sym offset = %04x\n", debugHeader.cbSymOffset );
printf( "opt sym size = %d\t", debugHeader.ioptMax );
printf( "opt sym offset = %04x\n", debugHeader.cbOptOffset );
printf( "aux sym size = %d\t", debugHeader.iauxMax );
printf( "aux sym offset = %04x\n", debugHeader.cbAuxOffset );
printf( "local strings size = %d\t", debugHeader.issMax );
printf( "local strings offset = %04x\n", debugHeader.cbSsOffset );
printf( "ext strings size = %d\t", debugHeader.issExtMax );
printf( "ext strings offset = %04x\n", debugHeader.cbSsExtOffset );
printf( "file size = %d\t", debugHeader.ifdMax );
printf( "file offset = %04x\n", debugHeader.cbFdOffset );
printf( "rel file size = %d\t", debugHeader.crfd );
printf( "rel file offset = %04x\n", debugHeader.cbRfdOffset );
printf( "ext sym size = %d\t", debugHeader.iextMax );
printf( "ext sym offset = %04x\n", debugHeader.cbExtOffset );
DumpLocalStrings();
DumpExtStrings();
DumpFile();
DumpGlobals();
DumpLocals();
DumpAux();
DumpProcs();
DumpRelInfo();
DumpLineInfo();
}
main( int argc, char ** argv )
{
register int i;
if ( argc != 2 )
usage();
if ( (in = fopen( argv[1], "r" )) == NULL )
{
fprintf( stderr, "Can't open %s\n", argv[1] );
exit( 1 );
}
DumpFileHeader();
if ( header.e_shnum > NSECTS )
{
fprintf( stderr,
"Sorry, can't deal with %d sections in this version\n",
header.e_shnum );
fclose( in );
exit( 1 );
}
if ( fseek( in, header.e_shoff, SEEK_SET ) )
{
fprintf( stderr, "Seek failed in %s\n", argv[1] );
fclose( in );
exit( 1 );
}
for ( i = 0; i < header.e_shnum; ++i )
ReadSection( i );
sstabStart = sectHeader[header.e_shstrndx].sh_offset;
for ( i = 0; i < header.e_shnum; ++i )
ShowSectionName( i );
DumpDebug();
fclose( in );
}