-
Notifications
You must be signed in to change notification settings - Fork 10
/
objdump.cpp
139 lines (119 loc) · 4 KB
/
objdump.cpp
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
//==================================
// PEDUMP - Matt Pietrek 1997
// FILE: OBJDUMP.C
//==================================
#include <windows.h>
#include <stdio.h>
#include "common.h"
#include "SymbolTableSupport.h"
#include "COFFSymbolTable.h"
#include "extrnvar.h"
typedef struct _i386RelocTypes
{
WORD type;
PSTR name;
} i386RelocTypes;
// ASCII names for the various relocations used in i386 COFF OBJs
i386RelocTypes i386Relocations[] =
{
{ IMAGE_REL_I386_ABSOLUTE, "ABSOLUTE" },
{ IMAGE_REL_I386_DIR16, "DIR16" },
{ IMAGE_REL_I386_REL16, "REL16" },
{ IMAGE_REL_I386_DIR32, "DIR32" },
{ IMAGE_REL_I386_DIR32NB, "DIR32NB" },
{ IMAGE_REL_I386_SEG12, "SEG12" },
{ IMAGE_REL_I386_SECTION, "SECTION" },
{ IMAGE_REL_I386_SECREL, "SECREL" },
{ IMAGE_REL_I386_REL32, "REL32" }
};
#define I386RELOCTYPECOUNT (sizeof(i386Relocations) / sizeof(i386RelocTypes))
//
// Given an i386 OBJ relocation type, return its ASCII name in a buffer
//
void GetObjRelocationName(WORD type, PSTR buffer, DWORD cBytes)
{
DWORD i;
for ( i=0; i < I386RELOCTYPECOUNT; i++ )
if ( type == i386Relocations[i].type )
{
strncpy(buffer, i386Relocations[i].name, cBytes);
return;
}
sprintf( buffer, "???_%X", type);
}
//
// Dump the relocation table for one COFF section
//
void DumpObjRelocations(PIMAGE_RELOCATION pRelocs, DWORD count)
{
DWORD i;
char szTypeName[32];
for ( i=0; i < count; i++ )
{
GetObjRelocationName(pRelocs->Type, szTypeName, sizeof(szTypeName));
printf(" Address: %08X SymIndex: %08X Type: %s\n",
pRelocs->VirtualAddress, pRelocs->SymbolTableIndex,
szTypeName);
pRelocs++;
}
}
//
// top level routine called from PEDUMP.C to dump the components of a
// COFF OBJ file.
//
void DumpObjFile( PIMAGE_FILE_HEADER pImageFileHeader )
{
unsigned i;
PIMAGE_SECTION_HEADER pSections;
DumpHeader(pImageFileHeader);
printf("\n");
pSections = MakePtr(PIMAGE_SECTION_HEADER, (pImageFileHeader+1),
pImageFileHeader->SizeOfOptionalHeader);
DumpSectionTable(pSections, pImageFileHeader->NumberOfSections, FALSE);
printf("\n");
if ( fShowRelocations )
{
for ( i=0; i < pImageFileHeader->NumberOfSections; i++ )
{
if ( pSections[i].PointerToRelocations == 0 )
continue;
printf("Section %02X (%.8s) relocations\n", i, pSections[i].Name);
DumpObjRelocations( MakePtr(PIMAGE_RELOCATION, pImageFileHeader,
pSections[i].PointerToRelocations),
pSections[i].NumberOfRelocations );
printf("\n");
}
}
if ( fShowSymbolTable && pImageFileHeader->PointerToSymbolTable )
{
g_pCOFFSymbolTable = new COFFSymbolTable(
MakePtr(PVOID, pImageFileHeader,
pImageFileHeader->PointerToSymbolTable),
pImageFileHeader->NumberOfSymbols );
DumpSymbolTable( g_pCOFFSymbolTable );
printf("\n");
}
if ( fShowLineNumbers )
{
// Walk through the section table...
for (i=0; i < pImageFileHeader->NumberOfSections; i++)
{
// if there's any line numbers for this section, dump'em
if ( pSections->NumberOfLinenumbers )
{
DumpLineNumbers( MakePtr(PIMAGE_LINENUMBER, pImageFileHeader,
pSections->PointerToLinenumbers),
pSections->NumberOfLinenumbers );
printf("\n");
}
pSections++;
}
}
if ( fShowRawSectionData )
{
DumpRawSectionData( (PIMAGE_SECTION_HEADER)(pImageFileHeader+1),
pImageFileHeader,
pImageFileHeader->NumberOfSections);
}
delete g_pCOFFSymbolTable;
}