Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.2 KiB

  1. //--------------------
  2. // PROGRAM: PEDUMP
  3. // FILE: PEDUMP.C
  4. // AUTHOR: Matt Pietrek - 1993
  5. //--------------------
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include "objdump.h"
  9. #include "exedump.h"
  10. #include "extrnvar.h"
  11. // Global variables set here, and used in EXEDUMP.C and OBJDUMP.C
  12. BOOL fShowRelocations = FALSE;
  13. BOOL fShowRawSectionData = FALSE;
  14. BOOL fShowSymbolTable = FALSE;
  15. BOOL fShowLineNumbers = FALSE;
  16. char HelpText[] =
  17. "PEDUMP - Win32/COFF .EXE/.OBJ file dumper - 1993 Matt Pietrek\n\n"
  18. "Syntax: PEDUMP [switches] filename\n\n"
  19. " /A include everything in dump\n"
  20. " /H include hex dump of sections\n"
  21. " /L include line number information\n"
  22. " /R show base relocations\n"
  23. " /S show symbol table\n";
  24. //
  25. // Open up a file, memory map it, and call the appropriate dumping routine
  26. //
  27. void DumpFile(LPSTR filename)
  28. {
  29. HANDLE hFile;
  30. HANDLE hFileMapping;
  31. LPVOID lpFileBase;
  32. PIMAGE_DOS_HEADER dosHeader;
  33. hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
  34. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  35. if ( hFile == INVALID_HANDLE_VALUE )
  36. {
  37. printf("Couldn't open file with CreateFile()\n");
  38. return;
  39. }
  40. hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  41. if ( hFileMapping == 0 )
  42. {
  43. CloseHandle(hFile);
  44. printf("Couldn't open file mapping with CreateFileMapping()\n");
  45. return;
  46. }
  47. lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  48. if ( lpFileBase == 0 )
  49. {
  50. CloseHandle(hFileMapping);
  51. CloseHandle(hFile);
  52. printf("Couldn't map view of file with MapViewOfFile()\n");
  53. return;
  54. }
  55. printf("Dump of file %s\n\n", filename);
  56. dosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
  57. if ( dosHeader->e_magic == IMAGE_DOS_SIGNATURE )
  58. {
  59. DumpExeFile( dosHeader );
  60. }
  61. else if ( (dosHeader->e_magic == 0x014C) // Does it look like a i386
  62. && (dosHeader->e_sp == 0) ) // COFF OBJ file???
  63. {
  64. // The two tests above aren't what they look like. They're
  65. // really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C)
  66. // and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0;
  67. DumpObjFile( (PIMAGE_FILE_HEADER)lpFileBase );
  68. }
  69. else
  70. printf("unrecognized file format\n");
  71. UnmapViewOfFile(lpFileBase);
  72. CloseHandle(hFileMapping);
  73. CloseHandle(hFile);
  74. }
  75. //
  76. // process all the command line arguments and return a pointer to
  77. // the filename argument.
  78. //
  79. PSTR ProcessCommandLine(int argc, char *argv[])
  80. {
  81. int i;
  82. for ( i=1; i < argc; i++ )
  83. {
  84. strupr(argv[i]);
  85. // Is it a switch character?
  86. if ( (argv[i][0] == '-') || (argv[i][0] == '/') )
  87. {
  88. if ( argv[i][1] == 'A' )
  89. {
  90. fShowRelocations = TRUE;
  91. fShowRawSectionData = TRUE;
  92. fShowSymbolTable = TRUE;
  93. fShowLineNumbers = TRUE;
  94. }
  95. else if ( argv[i][1] == 'H' )
  96. fShowRawSectionData = TRUE;
  97. else if ( argv[i][1] == 'L' )
  98. fShowLineNumbers = TRUE;
  99. else if ( argv[i][1] == 'R' )
  100. fShowRelocations = TRUE;
  101. else if ( argv[i][1] == 'S' )
  102. fShowSymbolTable = TRUE;
  103. }
  104. else // Not a switch character. Must be the filename
  105. {
  106. return argv[i];
  107. }
  108. }
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. PSTR filename;
  113. if ( argc == 1 )
  114. {
  115. printf( HelpText );
  116. return 1;
  117. }
  118. filename = ProcessCommandLine(argc, argv);
  119. if ( filename )
  120. DumpFile( filename );
  121. return 0;
  122. }