home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / MSJMAR94.ZIP / PE.ZIP / OBJDUMP.C < prev    next >
C/C++ Source or Header  |  1994-03-01  |  3KB  |  133 lines

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