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
|
/*
* winDumpExts.c --
* Author: Gordon Chaffee, Scott Stanton
*
* History: The real functionality of this file was written by
* Matt Pietrek in 1993 in his pedump utility. I've
* modified it to dump the externals in a bunch of object
* files to create a .def file.
*
* 10/12/95 Modified by Scott Stanton to support Relocatable Object Module
* Format files for Borland C++ 4.5.
*
* Notes: Visual C++ puts an underscore before each exported symbol.
* This file removes them. I don't know if this is a problem
* this other compilers. If _MSC_VER is defined,
* the underscore is removed. If not, it isn't. To get a
* full dump of an object file, use the -f option. This can
* help determine the something that may be different with a
* compiler other than Visual C++.
*----------------------------------------------------------------------
*
* RCS: @(#) $Id: winDumpExts.c,v 1.1.2.2 1999/02/10 23:31:29 stanton Exp $
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#ifdef _ALPHA_
#define e_magic_number IMAGE_FILE_MACHINE_ALPHA
#else
#define e_magic_number IMAGE_FILE_MACHINE_I386
#endif
/*
*----------------------------------------------------------------------
* GetArgcArgv --
*
* Break up a line into argc argv
*----------------------------------------------------------------------
*/
int
GetArgcArgv(char *s, char **argv)
{
int quote = 0;
int argc = 0;
char *bp;
bp = s;
while (1) {
while (isspace(*bp)) {
bp++;
}
if (*bp == '\n' || *bp == '\0') {
*bp = '\0';
return argc;
}
if (*bp == '\"') {
quote = 1;
bp++;
}
argv[argc++] = bp;
while (*bp != '\0') {
if (quote) {
if (*bp == '\"') {
quote = 0;
*bp = '\0';
bp++;
break;
}
bp++;
continue;
}
if (isspace(*bp)) {
*bp = '\0';
bp++;
break;
}
bp++;
}
}
}
/*
* The names of the first group of possible symbol table storage classes
*/
char * SzStorageClass1[] = {
"NULL","AUTOMATIC","EXTERNAL","STATIC","REGISTER","EXTERNAL_DEF","LABEL",
"UNDEFINED_LABEL","MEMBER_OF_STRUCT","ARGUMENT","STRUCT_TAG",
"MEMBER_OF_UNION","UNION_TAG","TYPE_DEFINITION","UNDEFINED_STATIC",
"ENUM_TAG","MEMBER_OF_ENUM","REGISTER_PARAM","BIT_FIELD"
};
/*
* The names of the second group of possible symbol table storage classes
*/
char * SzStorageClass2[] = {
"BLOCK","FUNCTION","END_OF_STRUCT","FILE","SECTION","WEAK_EXTERNAL"
};
/*
*----------------------------------------------------------------------
* GetSZStorageClass --
*
* Given a symbol storage class value, return a descriptive
* ASCII string
*----------------------------------------------------------------------
*/
PSTR
GetSZStorageClass(BYTE storageClass)
{
if ( storageClass <= IMAGE_SYM_CLASS_BIT_FIELD )
return SzStorageClass1[storageClass];
else if ( (storageClass >= IMAGE_SYM_CLASS_BLOCK)
&& (storageClass <= IMAGE_SYM_CLASS_WEAK_EXTERNAL) )
return SzStorageClass2[storageClass-IMAGE_SYM_CLASS_BLOCK];
else
return "???";
}
/*
*----------------------------------------------------------------------
* GetSectionName --
*
* Used by DumpSymbolTable, it gives meaningful names to
* the non-normal section number.
*
* Results:
* A name is returned in buffer
*----------------------------------------------------------------------
*/
void
GetSectionName(WORD section, PSTR buffer, unsigned cbBuffer)
{
char tempbuffer[10];
switch ( (SHORT)section )
{
case IMAGE_SYM_UNDEFINED: strcpy(tempbuffer, "UNDEF"); break;
case IMAGE_SYM_ABSOLUTE: strcpy(tempbuffer, "ABS "); break;
case IMAGE_SYM_DEBUG: strcpy(tempbuffer, "DEBUG"); break;
default: wsprintf(tempbuffer, "%-5X", section);
}
strncpy(buffer, tempbuffer, cbBuffer-1);
}
/*
*----------------------------------------------------------------------
* DumpSymbolTable --
*
* Dumps a COFF symbol table from an EXE or OBJ. We only use
* it to dump tables from OBJs.
*----------------------------------------------------------------------
*/
void
DumpSymbolTable(PIMAGE_SYMBOL pSymbolTable, FILE *fout, unsigned cSymbols)
{
unsigned i;
PSTR stringTable;
char sectionName[10];
fprintf(fout, "Symbol Table - %X entries (* = auxillary symbol)\n",
cSymbols);
fprintf(fout,
"Indx Name Value Section cAux Type Storage\n"
"---- -------------------- -------- ---------- ----- ------- --------\n");
/*
* The string table apparently starts right after the symbol table
*/
stringTable = (PSTR)&pSymbolTable[cSymbols];
for ( i=0; i < cSymbols; i++ ) {
fprintf(fout, "%04X ", i);
if ( pSymbolTable->N.Name.Short != 0 )
fprintf(fout, "%-20.8s", pSymbolTable->N.ShortName);
else
fprintf(fout, "%-20s", stringTable + pSymbolTable->N.Name.Long);
fprintf(fout, " %08X", pSymbolTable->Value);
GetSectionName(pSymbolTable->SectionNumber, sectionName,
sizeof(sectionName));
fprintf(fout, " sect:%s aux:%X type:%02X st:%s\n",
sectionName,
pSymbolTable->NumberOfAuxSymbols,
pSymbolTable->Type,
GetSZStorageClass(pSymbolTable->StorageClass) );
#if 0
if ( pSymbolTable->NumberOfAuxSymbols )
DumpAuxSymbols(pSymbolTable);
#endif
/*
* Take into account any aux symbols
*/
i += pSymbolTable->NumberOfAuxSymbols;
pSymbolTable += pSymbolTable->NumberOfAuxSymbols;
pSymbolTable++;
}
}
/*
*----------------------------------------------------------------------
* DumpExternals --
*
* Dumps a COFF symbol table from an EXE or OBJ. We only use
* it to dump tables from OBJs.
*----------------------------------------------------------------------
*/
void
DumpExternals(PIMAGE_SYMBOL pSymbolTable, FILE *fout, unsigned cSymbols)
{
unsigned i;
PSTR stringTable;
char *s, *f;
char symbol[1024];
/*
* The string table apparently starts right after the symbol table
*/
stringTable = (PSTR)&pSymbolTable[cSymbols];
for ( i=0; i < cSymbols; i++ ) {
if (pSymbolTable->SectionNumber > 0 && pSymbolTable->Type == 0x20) {
if (pSymbolTable->StorageClass == IMAGE_SYM_CLASS_EXTERNAL) {
if (pSymbolTable->N.Name.Short != 0) {
strncpy(symbol, pSymbolTable->N.ShortName, 8);
symbol[8] = 0;
} else {
s = stringTable + pSymbolTable->N.Name.Long;
strcpy(symbol, s);
}
s = symbol;
/*
* Skip to the last @ and ensure it is followed by digits,
* otherwise it is probably part of a C++ mangled name.
*/
f = strrchr(s, '@');
if (f && f[1] >= '0' && f[1] <= '9') {
*f = 0;
}
#if defined(_MSC_VER) && defined(_X86_)
if (symbol[0] == '_') {
s = &symbol[1];
}
#endif
if ((stricmp(s, "DllEntryPoint") != 0)
&& (stricmp(s, "DllMain") != 0)) {
fprintf(fout, "\t%s\n", s);
}
}
}
/*
* Take into account any aux symbols
*/
i += pSymbolTable->NumberOfAuxSymbols;
pSymbolTable += pSymbolTable->NumberOfAuxSymbols;
pSymbolTable++;
}
}
/*
*----------------------------------------------------------------------
* DumpObjFile --
*
* Dump an object file--either a full listing or just the exported
* symbols.
*----------------------------------------------------------------------
*/
void
DumpObjFile(PIMAGE_FILE_HEADER pImageFileHeader, FILE *fout, int full)
{
PIMAGE_SYMBOL PCOFFSymbolTable;
DWORD COFFSymbolCount;
PCOFFSymbolTable = (PIMAGE_SYMBOL)
((DWORD)pImageFileHeader + pImageFileHeader->PointerToSymbolTable);
COFFSymbolCount = pImageFileHeader->NumberOfSymbols;
if (full) {
DumpSymbolTable(PCOFFSymbolTable, fout, COFFSymbolCount);
} else {
DumpExternals(PCOFFSymbolTable, fout, COFFSymbolCount);
}
}
/*
*----------------------------------------------------------------------
* SkipToNextRecord --
*
* Skip over the current ROMF record and return the type of the
* next record.
*----------------------------------------------------------------------
*/
BYTE
SkipToNextRecord(BYTE **ppBuffer)
{
int length;
(*ppBuffer)++; /* Skip over the type.*/
length = *((WORD*)(*ppBuffer))++; /* Retrieve the length. */
*ppBuffer += length; /* Skip over the rest. */
return **ppBuffer; /* Return the type. */
}
/*
*----------------------------------------------------------------------
* DumpROMFObjFile --
*
* Dump a Relocatable Object Module Format file, displaying only
* the exported symbols.
*----------------------------------------------------------------------
*/
void
DumpROMFObjFile(LPVOID pBuffer, FILE *fout)
{
BYTE type, length;
char symbol[1024], *s;
while (1) {
type = SkipToNextRecord(&(BYTE*)pBuffer);
if (type == 0x90) { /* PUBDEF */
if (((BYTE*)pBuffer)[4] != 0) {
length = ((BYTE*)pBuffer)[5];
strncpy(symbol, ((char*)pBuffer) + 6, length);
symbol[length] = '\0';
s = symbol;
if ((stricmp(s, "DllEntryPoint") != 0)
&& (stricmp(s, "DllMain") != 0)) {
if (s[0] == '_') {
s++;
fprintf(fout, "\t_%s\n\t%s=_%s\n", s, s, s);
} else {
fprintf(fout, "\t%s\n", s);
}
}
}
} else if (type == 0x8B || type == 0x8A) { /* MODEND */
break;
}
}
}
/*
*----------------------------------------------------------------------
* DumpFile --
*
* Open up a file, memory map it, and call the appropriate
* dumping routine
*----------------------------------------------------------------------
*/
void
DumpFile(LPSTR filename, FILE *fout, int full)
{
HANDLE hFile;
HANDLE hFileMapping;
LPVOID lpFileBase;
PIMAGE_DOS_HEADER dosHeader;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Couldn't open file with CreateFile()\n");
return;
}
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hFileMapping == 0) {
CloseHandle(hFile);
fprintf(stderr, "Couldn't open file mapping with CreateFileMapping()\n");
return;
}
lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
if (lpFileBase == 0) {
CloseHandle(hFileMapping);
CloseHandle(hFile);
fprintf(stderr, "Couldn't map view of file with MapViewOfFile()\n");
return;
}
dosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) {
#if 0
DumpExeFile( dosHeader );
#else
fprintf(stderr, "File is an executable. I don't dump those.\n");
return;
#endif
}
/* Does it look like a i386 COFF OBJ file??? */
else if ((dosHeader->e_magic == e_magic_number)
&& (dosHeader->e_sp == 0)) {
/*
* The two tests above aren't what they look like. They're
* really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C)
* and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0;
*/
DumpObjFile((PIMAGE_FILE_HEADER) lpFileBase, fout, full);
} else if (*((BYTE *)lpFileBase) == 0x80) {
/*
* This file looks like it might be a ROMF file.
*/
DumpROMFObjFile(lpFileBase, fout);
} else {
printf("unrecognized file format\n");
}
UnmapViewOfFile(lpFileBase);
CloseHandle(hFileMapping);
CloseHandle(hFile);
}
void
main(int argc, char **argv)
{
char *fargv[1000];
char cmdline[10000];
int i, arg;
FILE *fout;
int pos;
int full = 0;
char *outfile = NULL;
if (argc < 3) {
Usage:
fprintf(stderr, "Usage: %s ?-o outfile? ?-f(ull)? <dllname> <object filenames> ..\n", argv[0]);
exit(1);
}
arg = 1;
while (argv[arg][0] == '-') {
if (strcmp(argv[arg], "--") == 0) {
arg++;
break;
} else if (strcmp(argv[arg], "-f") == 0) {
full = 1;
} else if (strcmp(argv[arg], "-o") == 0) {
arg++;
if (arg == argc) {
goto Usage;
}
outfile = argv[arg];
}
arg++;
}
if (arg == argc) {
goto Usage;
}
if (outfile) {
fout = fopen(outfile, "w+");
if (fout == NULL) {
fprintf(stderr, "Unable to open \'%s\' for writing:\n",
argv[arg]);
perror("");
exit(1);
}
} else {
fout = stdout;
}
if (! full) {
char *dllname = argv[arg];
arg++;
if (arg == argc) {
goto Usage;
}
fprintf(fout, "EXPORTS\n");
}
for (; arg < argc; arg++) {
if (argv[arg][0] == '@') {
FILE *fargs = fopen(&argv[arg][1], "r");
if (fargs == NULL) {
fprintf(stderr, "Unable to open \'%s\' for reading:\n",
argv[arg]);
perror("");
exit(1);
}
pos = 0;
for (i = 0; i < arg; i++) {
strcpy(&cmdline[pos], argv[i]);
pos += strlen(&cmdline[pos]) + 1;
fargv[i] = argv[i];
}
fgets(&cmdline[pos], sizeof(cmdline), fargs);
fprintf(stderr, "%s\n", &cmdline[pos]);
fclose(fargs);
i += GetArgcArgv(&cmdline[pos], &fargv[i]);
argc = i;
argv = fargv;
}
DumpFile(argv[arg], fout, full);
}
exit(0);
}
|