| 1 |
/* |
| 2 |
* winDumpExts.c -- |
| 3 |
* Author: Gordon Chaffee, Scott Stanton |
| 4 |
* |
| 5 |
* History: The real functionality of this file was written by |
| 6 |
* Matt Pietrek in 1993 in his pedump utility. I've |
| 7 |
* modified it to dump the externals in a bunch of object |
| 8 |
* files to create a .def file. |
| 9 |
* |
| 10 |
* 10/12/95 Modified by Scott Stanton to support Relocatable Object Module |
| 11 |
* Format files for Borland C++ 4.5. |
| 12 |
* |
| 13 |
* Notes: Visual C++ puts an underscore before each exported symbol. |
| 14 |
* This file removes them. I don't know if this is a problem |
| 15 |
* this other compilers. If _MSC_VER is defined, |
| 16 |
* the underscore is removed. If not, it isn't. To get a |
| 17 |
* full dump of an object file, use the -f option. This can |
| 18 |
* help determine the something that may be different with a |
| 19 |
* compiler other than Visual C++. |
| 20 |
*---------------------------------------------------------------------- |
| 21 |
* |
| 22 |
* RCS: @(#) $Id: winDumpExts.c,v 1.1 2000/11/23 02:21:11 idiscovery Exp $ |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <windows.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <process.h> |
| 29 |
|
| 30 |
#ifdef _ALPHA_ |
| 31 |
#define e_magic_number IMAGE_FILE_MACHINE_ALPHA |
| 32 |
#else |
| 33 |
#define e_magic_number IMAGE_FILE_MACHINE_I386 |
| 34 |
#endif |
| 35 |
|
| 36 |
/* |
| 37 |
*---------------------------------------------------------------------- |
| 38 |
* GetArgcArgv -- |
| 39 |
* |
| 40 |
* Break up a line into argc argv |
| 41 |
*---------------------------------------------------------------------- |
| 42 |
*/ |
| 43 |
int |
| 44 |
GetArgcArgv(char *s, char **argv) |
| 45 |
{ |
| 46 |
int quote = 0; |
| 47 |
int argc = 0; |
| 48 |
char *bp; |
| 49 |
|
| 50 |
bp = s; |
| 51 |
while (1) { |
| 52 |
while (isspace(*bp)) { |
| 53 |
bp++; |
| 54 |
} |
| 55 |
if (*bp == '\n' || *bp == '\0') { |
| 56 |
*bp = '\0'; |
| 57 |
return argc; |
| 58 |
} |
| 59 |
if (*bp == '\"') { |
| 60 |
quote = 1; |
| 61 |
bp++; |
| 62 |
} |
| 63 |
argv[argc++] = bp; |
| 64 |
|
| 65 |
while (*bp != '\0') { |
| 66 |
if (quote) { |
| 67 |
if (*bp == '\"') { |
| 68 |
quote = 0; |
| 69 |
*bp = '\0'; |
| 70 |
bp++; |
| 71 |
break; |
| 72 |
} |
| 73 |
bp++; |
| 74 |
continue; |
| 75 |
} |
| 76 |
if (isspace(*bp)) { |
| 77 |
*bp = '\0'; |
| 78 |
bp++; |
| 79 |
break; |
| 80 |
} |
| 81 |
bp++; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* The names of the first group of possible symbol table storage classes |
| 88 |
*/ |
| 89 |
char * SzStorageClass1[] = { |
| 90 |
"NULL","AUTOMATIC","EXTERNAL","STATIC","REGISTER","EXTERNAL_DEF","LABEL", |
| 91 |
"UNDEFINED_LABEL","MEMBER_OF_STRUCT","ARGUMENT","STRUCT_TAG", |
| 92 |
"MEMBER_OF_UNION","UNION_TAG","TYPE_DEFINITION","UNDEFINED_STATIC", |
| 93 |
"ENUM_TAG","MEMBER_OF_ENUM","REGISTER_PARAM","BIT_FIELD" |
| 94 |
}; |
| 95 |
|
| 96 |
/* |
| 97 |
* The names of the second group of possible symbol table storage classes |
| 98 |
*/ |
| 99 |
char * SzStorageClass2[] = { |
| 100 |
"BLOCK","FUNCTION","END_OF_STRUCT","FILE","SECTION","WEAK_EXTERNAL" |
| 101 |
}; |
| 102 |
|
| 103 |
/* |
| 104 |
*---------------------------------------------------------------------- |
| 105 |
* GetSZStorageClass -- |
| 106 |
* |
| 107 |
* Given a symbol storage class value, return a descriptive |
| 108 |
* ASCII string |
| 109 |
*---------------------------------------------------------------------- |
| 110 |
*/ |
| 111 |
PSTR |
| 112 |
GetSZStorageClass(BYTE storageClass) |
| 113 |
{ |
| 114 |
if ( storageClass <= IMAGE_SYM_CLASS_BIT_FIELD ) |
| 115 |
return SzStorageClass1[storageClass]; |
| 116 |
else if ( (storageClass >= IMAGE_SYM_CLASS_BLOCK) |
| 117 |
&& (storageClass <= IMAGE_SYM_CLASS_WEAK_EXTERNAL) ) |
| 118 |
return SzStorageClass2[storageClass-IMAGE_SYM_CLASS_BLOCK]; |
| 119 |
else |
| 120 |
return "???"; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
*---------------------------------------------------------------------- |
| 125 |
* GetSectionName -- |
| 126 |
* |
| 127 |
* Used by DumpSymbolTable, it gives meaningful names to |
| 128 |
* the non-normal section number. |
| 129 |
* |
| 130 |
* Results: |
| 131 |
* A name is returned in buffer |
| 132 |
*---------------------------------------------------------------------- |
| 133 |
*/ |
| 134 |
void |
| 135 |
GetSectionName(WORD section, PSTR buffer, unsigned cbBuffer) |
| 136 |
{ |
| 137 |
char tempbuffer[10]; |
| 138 |
|
| 139 |
switch ( (SHORT)section ) |
| 140 |
{ |
| 141 |
case IMAGE_SYM_UNDEFINED: strcpy(tempbuffer, "UNDEF"); break; |
| 142 |
case IMAGE_SYM_ABSOLUTE: strcpy(tempbuffer, "ABS "); break; |
| 143 |
case IMAGE_SYM_DEBUG: strcpy(tempbuffer, "DEBUG"); break; |
| 144 |
default: wsprintf(tempbuffer, "%-5X", section); |
| 145 |
} |
| 146 |
|
| 147 |
strncpy(buffer, tempbuffer, cbBuffer-1); |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
*---------------------------------------------------------------------- |
| 152 |
* DumpSymbolTable -- |
| 153 |
* |
| 154 |
* Dumps a COFF symbol table from an EXE or OBJ. We only use |
| 155 |
* it to dump tables from OBJs. |
| 156 |
*---------------------------------------------------------------------- |
| 157 |
*/ |
| 158 |
void |
| 159 |
DumpSymbolTable(PIMAGE_SYMBOL pSymbolTable, FILE *fout, unsigned cSymbols) |
| 160 |
{ |
| 161 |
unsigned i; |
| 162 |
PSTR stringTable; |
| 163 |
char sectionName[10]; |
| 164 |
|
| 165 |
fprintf(fout, "Symbol Table - %X entries (* = auxillary symbol)\n", |
| 166 |
cSymbols); |
| 167 |
|
| 168 |
fprintf(fout, |
| 169 |
"Indx Name Value Section cAux Type Storage\n" |
| 170 |
"---- -------------------- -------- ---------- ----- ------- --------\n"); |
| 171 |
|
| 172 |
/* |
| 173 |
* The string table apparently starts right after the symbol table |
| 174 |
*/ |
| 175 |
stringTable = (PSTR)&pSymbolTable[cSymbols]; |
| 176 |
|
| 177 |
for ( i=0; i < cSymbols; i++ ) { |
| 178 |
fprintf(fout, "%04X ", i); |
| 179 |
if ( pSymbolTable->N.Name.Short != 0 ) |
| 180 |
fprintf(fout, "%-20.8s", pSymbolTable->N.ShortName); |
| 181 |
else |
| 182 |
fprintf(fout, "%-20s", stringTable + pSymbolTable->N.Name.Long); |
| 183 |
|
| 184 |
fprintf(fout, " %08X", pSymbolTable->Value); |
| 185 |
|
| 186 |
GetSectionName(pSymbolTable->SectionNumber, sectionName, |
| 187 |
sizeof(sectionName)); |
| 188 |
fprintf(fout, " sect:%s aux:%X type:%02X st:%s\n", |
| 189 |
sectionName, |
| 190 |
pSymbolTable->NumberOfAuxSymbols, |
| 191 |
pSymbolTable->Type, |
| 192 |
GetSZStorageClass(pSymbolTable->StorageClass) ); |
| 193 |
#if 0 |
| 194 |
if ( pSymbolTable->NumberOfAuxSymbols ) |
| 195 |
DumpAuxSymbols(pSymbolTable); |
| 196 |
#endif |
| 197 |
|
| 198 |
/* |
| 199 |
* Take into account any aux symbols |
| 200 |
*/ |
| 201 |
i += pSymbolTable->NumberOfAuxSymbols; |
| 202 |
pSymbolTable += pSymbolTable->NumberOfAuxSymbols; |
| 203 |
pSymbolTable++; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
*---------------------------------------------------------------------- |
| 209 |
* DumpExternals -- |
| 210 |
* |
| 211 |
* Dumps a COFF symbol table from an EXE or OBJ. We only use |
| 212 |
* it to dump tables from OBJs. |
| 213 |
*---------------------------------------------------------------------- |
| 214 |
*/ |
| 215 |
void |
| 216 |
DumpExternals(PIMAGE_SYMBOL pSymbolTable, FILE *fout, unsigned cSymbols) |
| 217 |
{ |
| 218 |
unsigned i; |
| 219 |
PSTR stringTable; |
| 220 |
char *s, *f; |
| 221 |
char symbol[1024]; |
| 222 |
|
| 223 |
/* |
| 224 |
* The string table apparently starts right after the symbol table |
| 225 |
*/ |
| 226 |
stringTable = (PSTR)&pSymbolTable[cSymbols]; |
| 227 |
|
| 228 |
for ( i=0; i < cSymbols; i++ ) { |
| 229 |
if (pSymbolTable->SectionNumber > 0 && pSymbolTable->Type == 0x20) { |
| 230 |
if (pSymbolTable->StorageClass == IMAGE_SYM_CLASS_EXTERNAL) { |
| 231 |
if (pSymbolTable->N.Name.Short != 0) { |
| 232 |
strncpy(symbol, pSymbolTable->N.ShortName, 8); |
| 233 |
symbol[8] = 0; |
| 234 |
} else { |
| 235 |
s = stringTable + pSymbolTable->N.Name.Long; |
| 236 |
strcpy(symbol, s); |
| 237 |
} |
| 238 |
s = symbol; |
| 239 |
|
| 240 |
/* |
| 241 |
* Skip to the last @ and ensure it is followed by digits, |
| 242 |
* otherwise it is probably part of a C++ mangled name. |
| 243 |
*/ |
| 244 |
|
| 245 |
f = strrchr(s, '@'); |
| 246 |
if (f && f[1] >= '0' && f[1] <= '9') { |
| 247 |
*f = 0; |
| 248 |
} |
| 249 |
#if defined(_MSC_VER) && defined(_X86_) |
| 250 |
if (symbol[0] == '_') { |
| 251 |
s = &symbol[1]; |
| 252 |
} |
| 253 |
#endif |
| 254 |
if ((stricmp(s, "DllEntryPoint") != 0) |
| 255 |
&& (stricmp(s, "DllMain") != 0)) { |
| 256 |
fprintf(fout, "\t%s\n", s); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* Take into account any aux symbols |
| 263 |
*/ |
| 264 |
i += pSymbolTable->NumberOfAuxSymbols; |
| 265 |
pSymbolTable += pSymbolTable->NumberOfAuxSymbols; |
| 266 |
pSymbolTable++; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
*---------------------------------------------------------------------- |
| 272 |
* DumpObjFile -- |
| 273 |
* |
| 274 |
* Dump an object file--either a full listing or just the exported |
| 275 |
* symbols. |
| 276 |
*---------------------------------------------------------------------- |
| 277 |
*/ |
| 278 |
void |
| 279 |
DumpObjFile(PIMAGE_FILE_HEADER pImageFileHeader, FILE *fout, int full) |
| 280 |
{ |
| 281 |
PIMAGE_SYMBOL PCOFFSymbolTable; |
| 282 |
DWORD COFFSymbolCount; |
| 283 |
|
| 284 |
PCOFFSymbolTable = (PIMAGE_SYMBOL) |
| 285 |
((DWORD)pImageFileHeader + pImageFileHeader->PointerToSymbolTable); |
| 286 |
COFFSymbolCount = pImageFileHeader->NumberOfSymbols; |
| 287 |
|
| 288 |
if (full) { |
| 289 |
DumpSymbolTable(PCOFFSymbolTable, fout, COFFSymbolCount); |
| 290 |
} else { |
| 291 |
DumpExternals(PCOFFSymbolTable, fout, COFFSymbolCount); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* |
| 296 |
*---------------------------------------------------------------------- |
| 297 |
* SkipToNextRecord -- |
| 298 |
* |
| 299 |
* Skip over the current ROMF record and return the type of the |
| 300 |
* next record. |
| 301 |
*---------------------------------------------------------------------- |
| 302 |
*/ |
| 303 |
|
| 304 |
BYTE |
| 305 |
SkipToNextRecord(BYTE **ppBuffer) |
| 306 |
{ |
| 307 |
int length; |
| 308 |
(*ppBuffer)++; /* Skip over the type.*/ |
| 309 |
length = *((WORD*)(*ppBuffer))++; /* Retrieve the length. */ |
| 310 |
*ppBuffer += length; /* Skip over the rest. */ |
| 311 |
return **ppBuffer; /* Return the type. */ |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
*---------------------------------------------------------------------- |
| 316 |
* DumpROMFObjFile -- |
| 317 |
* |
| 318 |
* Dump a Relocatable Object Module Format file, displaying only |
| 319 |
* the exported symbols. |
| 320 |
*---------------------------------------------------------------------- |
| 321 |
*/ |
| 322 |
void |
| 323 |
DumpROMFObjFile(LPVOID pBuffer, FILE *fout) |
| 324 |
{ |
| 325 |
BYTE type, length; |
| 326 |
char symbol[1024], *s; |
| 327 |
|
| 328 |
while (1) { |
| 329 |
type = SkipToNextRecord(&(BYTE*)pBuffer); |
| 330 |
if (type == 0x90) { /* PUBDEF */ |
| 331 |
if (((BYTE*)pBuffer)[4] != 0) { |
| 332 |
length = ((BYTE*)pBuffer)[5]; |
| 333 |
strncpy(symbol, ((char*)pBuffer) + 6, length); |
| 334 |
symbol[length] = '\0'; |
| 335 |
s = symbol; |
| 336 |
if ((stricmp(s, "DllEntryPoint") != 0) |
| 337 |
&& (stricmp(s, "DllMain") != 0)) { |
| 338 |
if (s[0] == '_') { |
| 339 |
s++; |
| 340 |
fprintf(fout, "\t_%s\n\t%s=_%s\n", s, s, s); |
| 341 |
} else { |
| 342 |
fprintf(fout, "\t%s\n", s); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} else if (type == 0x8B || type == 0x8A) { /* MODEND */ |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
*---------------------------------------------------------------------- |
| 354 |
* DumpFile -- |
| 355 |
* |
| 356 |
* Open up a file, memory map it, and call the appropriate |
| 357 |
* dumping routine |
| 358 |
*---------------------------------------------------------------------- |
| 359 |
*/ |
| 360 |
void |
| 361 |
DumpFile(LPSTR filename, FILE *fout, int full) |
| 362 |
{ |
| 363 |
HANDLE hFile; |
| 364 |
HANDLE hFileMapping; |
| 365 |
LPVOID lpFileBase; |
| 366 |
PIMAGE_DOS_HEADER dosHeader; |
| 367 |
|
| 368 |
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 369 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 370 |
|
| 371 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 372 |
fprintf(stderr, "Couldn't open file with CreateFile()\n"); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); |
| 377 |
if (hFileMapping == 0) { |
| 378 |
CloseHandle(hFile); |
| 379 |
fprintf(stderr, "Couldn't open file mapping with CreateFileMapping()\n"); |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0); |
| 384 |
if (lpFileBase == 0) { |
| 385 |
CloseHandle(hFileMapping); |
| 386 |
CloseHandle(hFile); |
| 387 |
fprintf(stderr, "Couldn't map view of file with MapViewOfFile()\n"); |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
dosHeader = (PIMAGE_DOS_HEADER)lpFileBase; |
| 392 |
if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { |
| 393 |
#if 0 |
| 394 |
DumpExeFile( dosHeader ); |
| 395 |
#else |
| 396 |
fprintf(stderr, "File is an executable. I don't dump those.\n"); |
| 397 |
return; |
| 398 |
#endif |
| 399 |
} |
| 400 |
/* Does it look like a i386 COFF OBJ file??? */ |
| 401 |
else if ((dosHeader->e_magic == e_magic_number) |
| 402 |
&& (dosHeader->e_sp == 0)) { |
| 403 |
/* |
| 404 |
* The two tests above aren't what they look like. They're |
| 405 |
* really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C) |
| 406 |
* and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0; |
| 407 |
*/ |
| 408 |
DumpObjFile((PIMAGE_FILE_HEADER) lpFileBase, fout, full); |
| 409 |
} else if (*((BYTE *)lpFileBase) == 0x80) { |
| 410 |
/* |
| 411 |
* This file looks like it might be a ROMF file. |
| 412 |
*/ |
| 413 |
DumpROMFObjFile(lpFileBase, fout); |
| 414 |
} else { |
| 415 |
printf("unrecognized file format\n"); |
| 416 |
} |
| 417 |
UnmapViewOfFile(lpFileBase); |
| 418 |
CloseHandle(hFileMapping); |
| 419 |
CloseHandle(hFile); |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
main(int argc, char **argv) |
| 424 |
{ |
| 425 |
char *fargv[1000]; |
| 426 |
char cmdline[10000]; |
| 427 |
int i, arg; |
| 428 |
FILE *fout; |
| 429 |
int pos; |
| 430 |
int full = 0; |
| 431 |
char *outfile = NULL; |
| 432 |
|
| 433 |
if (argc < 3) { |
| 434 |
Usage: |
| 435 |
fprintf(stderr, "Usage: %s ?-o outfile? ?-f(ull)? <dllname> <object filenames> ..\n", argv[0]); |
| 436 |
exit(1); |
| 437 |
} |
| 438 |
|
| 439 |
arg = 1; |
| 440 |
while (argv[arg][0] == '-') { |
| 441 |
if (strcmp(argv[arg], "--") == 0) { |
| 442 |
arg++; |
| 443 |
break; |
| 444 |
} else if (strcmp(argv[arg], "-f") == 0) { |
| 445 |
full = 1; |
| 446 |
} else if (strcmp(argv[arg], "-o") == 0) { |
| 447 |
arg++; |
| 448 |
if (arg == argc) { |
| 449 |
goto Usage; |
| 450 |
} |
| 451 |
outfile = argv[arg]; |
| 452 |
} |
| 453 |
arg++; |
| 454 |
} |
| 455 |
if (arg == argc) { |
| 456 |
goto Usage; |
| 457 |
} |
| 458 |
|
| 459 |
if (outfile) { |
| 460 |
fout = fopen(outfile, "w+"); |
| 461 |
if (fout == NULL) { |
| 462 |
fprintf(stderr, "Unable to open \'%s\' for writing:\n", |
| 463 |
argv[arg]); |
| 464 |
perror(""); |
| 465 |
exit(1); |
| 466 |
} |
| 467 |
} else { |
| 468 |
fout = stdout; |
| 469 |
} |
| 470 |
|
| 471 |
if (! full) { |
| 472 |
char *dllname = argv[arg]; |
| 473 |
arg++; |
| 474 |
if (arg == argc) { |
| 475 |
goto Usage; |
| 476 |
} |
| 477 |
fprintf(fout, "EXPORTS\n"); |
| 478 |
} |
| 479 |
|
| 480 |
for (; arg < argc; arg++) { |
| 481 |
if (argv[arg][0] == '@') { |
| 482 |
FILE *fargs = fopen(&argv[arg][1], "r"); |
| 483 |
if (fargs == NULL) { |
| 484 |
fprintf(stderr, "Unable to open \'%s\' for reading:\n", |
| 485 |
argv[arg]); |
| 486 |
perror(""); |
| 487 |
exit(1); |
| 488 |
} |
| 489 |
pos = 0; |
| 490 |
for (i = 0; i < arg; i++) { |
| 491 |
strcpy(&cmdline[pos], argv[i]); |
| 492 |
pos += strlen(&cmdline[pos]) + 1; |
| 493 |
fargv[i] = argv[i]; |
| 494 |
} |
| 495 |
fgets(&cmdline[pos], sizeof(cmdline), fargs); |
| 496 |
fprintf(stderr, "%s\n", &cmdline[pos]); |
| 497 |
fclose(fargs); |
| 498 |
i += GetArgcArgv(&cmdline[pos], &fargv[i]); |
| 499 |
argc = i; |
| 500 |
argv = fargv; |
| 501 |
} |
| 502 |
DumpFile(argv[arg], fout, full); |
| 503 |
} |
| 504 |
exit(0); |
| 505 |
} |