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.

132 lines
3.2 KiB

  1. //--------------------
  2. // PROGRAM: PEDUMP
  3. // FILE: OBJDUMP.C
  4. // AUTHOR: Matt Pietrek - 1993
  5. //--------------------
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include "common.h"
  9. #include "extrnvar.h"
  10. typedef struct _i386RelocTypes
  11. {
  12. WORD type;
  13. PSTR name;
  14. } i386RelocTypes;
  15. // ASCII names for the various relocations used in i386 COFF OBJs
  16. i386RelocTypes i386Relocations[] =
  17. {
  18. { IMAGE_REL_I386_ABSOLUTE, "ABSOLUTE" },
  19. { IMAGE_REL_I386_DIR16, "DIR16" },
  20. { IMAGE_REL_I386_REL16, "REL16" },
  21. { IMAGE_REL_I386_DIR32, "DIR32" },
  22. { IMAGE_REL_I386_DIR32NB, "DIR32NB" },
  23. { IMAGE_REL_I386_SEG12, "SEG12" },
  24. { IMAGE_REL_I386_SECTION, "SECTION" },
  25. { IMAGE_REL_I386_SECREL, "SECREL" },
  26. { IMAGE_REL_I386_REL32, "REL32" }
  27. };
  28. #define I386RELOCTYPECOUNT (sizeof(i386Relocations) / sizeof(i386RelocTypes))
  29. //
  30. // Given an i386 OBJ relocation type, return its ASCII name in a buffer
  31. //
  32. void GetObjRelocationName(WORD type, PSTR buffer, DWORD cBytes)
  33. {
  34. DWORD i;
  35. for ( i=0; i < I386RELOCTYPECOUNT; i++ )
  36. if ( type == i386Relocations[i].type )
  37. {
  38. strncpy(buffer, i386Relocations[i].name, cBytes);
  39. return;
  40. }
  41. wsprintf( buffer, "???_%X", type);
  42. }
  43. //
  44. // Dump the relocation table for one COFF section
  45. //
  46. void DumpObjRelocations(PIMAGE_RELOCATION pRelocs, DWORD count)
  47. {
  48. DWORD i;
  49. char szTypeName[32];
  50. for ( i=0; i < count; i++ )
  51. {
  52. GetObjRelocationName(pRelocs->Type, szTypeName, sizeof(szTypeName));
  53. printf(" Address: %08X SymIndex: %08X Type: %s\n",
  54. pRelocs->VirtualAddress, pRelocs->SymbolTableIndex,
  55. szTypeName);
  56. pRelocs++;
  57. }
  58. }
  59. //
  60. // top level routine called from PEDUMP.C to dump the components of a
  61. // COFF OBJ file.
  62. //
  63. void DumpObjFile( PIMAGE_FILE_HEADER pImageFileHeader )
  64. {
  65. unsigned i;
  66. PIMAGE_SECTION_HEADER pSections;
  67. DumpHeader(pImageFileHeader);
  68. printf("\n");
  69. pSections = (PIMAGE_SECTION_HEADER)(pImageFileHeader+1);
  70. DumpSectionTable(pSections, pImageFileHeader->NumberOfSections, FALSE);
  71. printf("\n");
  72. if ( fShowRelocations )
  73. {
  74. for ( i=0; i < pImageFileHeader->NumberOfSections; i++ )
  75. {
  76. if ( pSections[i].PointerToRelocations == 0 )
  77. continue;
  78. printf("Section %02X (%.8s) relocations\n", i, pSections[i].Name);
  79. DumpObjRelocations( MakePtr(PIMAGE_RELOCATION, pImageFileHeader,
  80. pSections[i].PointerToRelocations),
  81. pSections[i].NumberOfRelocations );
  82. printf("\n");
  83. }
  84. }
  85. PCOFFSymbolTable = MakePtr(PIMAGE_SYMBOL, pImageFileHeader,
  86. pImageFileHeader->PointerToSymbolTable);
  87. COFFSymbolCount = pImageFileHeader->NumberOfSymbols;
  88. if ( fShowLineNumbers )
  89. {
  90. // Walk through the section table...
  91. for (i=0; i < pImageFileHeader->NumberOfSections; i++)
  92. {
  93. // if there's any line numbers for this section, dump'em
  94. if ( pSections->NumberOfLinenumbers )
  95. {
  96. DumpLineNumbers( MakePtr(PIMAGE_LINENUMBER, pImageFileHeader,
  97. pSections->PointerToLinenumbers),
  98. pSections->NumberOfLinenumbers );
  99. printf("\n");
  100. }
  101. pSections++;
  102. }
  103. }
  104. if ( fShowSymbolTable )
  105. {
  106. DumpSymbolTable(PCOFFSymbolTable, COFFSymbolCount);
  107. printf("\n");
  108. }
  109. if ( fShowRawSectionData )
  110. {
  111. DumpRawSectionData( (PIMAGE_SECTION_HEADER)(pImageFileHeader+1),
  112. pImageFileHeader,
  113. pImageFileHeader->NumberOfSections);
  114. }
  115. }