-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpedump.cpp
More file actions
174 lines (153 loc) · 4.95 KB
/
pedump.cpp
File metadata and controls
174 lines (153 loc) · 4.95 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//==================================
// PEDUMP - Matt Pietrek 1994-1998
// FILE: PEDUMP.C
//==================================
#include <windows.h>
#include <stdio.h>
#include "objdump.h"
#include "exedump.h"
#include "dbgdump.h"
#include "libdump.h"
#include "romimage.h"
#include "extrnvar.h"
// Global variables set here, and used in EXEDUMP.C and OBJDUMP.C
BOOL fShowRelocations = FALSE;
BOOL fShowRawSectionData = FALSE;
BOOL fShowSymbolTable = FALSE;
BOOL fShowLineNumbers = FALSE;
BOOL fShowIATentries = FALSE;
BOOL fShowPDATA = FALSE;
BOOL fShowResources = FALSE;
char HelpText[] =
"PEDUMP - Win32/COFF EXE/OBJ/LIB file dumper - 1998 Matt Pietrek\n\n"
"Syntax: PEDUMP [switches] filename\n\n"
" /A include everything in dump\n"
" /B show base relocations\n"
" /H include hex dump of sections\n"
" /I include Import Address Table thunk addresses\n"
" /L include line number information\n"
" /P include PDATA (runtime functions)\n"
" /R include detailed resources (stringtables and dialogs)\n"
" /S show symbol table\n";
//
// Open up a file, memory map it, and call the appropriate dumping routine
//
void DumpFile(LPSTR filename)
{
HANDLE hFile;
HANDLE hFileMapping;
LPVOID lpFileBase;
PIMAGE_DOS_HEADER dosHeader;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if ( hFile == INVALID_HANDLE_VALUE )
{
printf("Couldn't open file with CreateFile()\n");
return;
}
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if ( hFileMapping == 0 )
{
CloseHandle(hFile);
printf("Couldn't open file mapping with CreateFileMapping()\n");
return;
}
lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
if ( lpFileBase == 0 )
{
CloseHandle(hFileMapping);
CloseHandle(hFile);
printf("Couldn't map view of file with MapViewOfFile()\n");
return;
}
printf("Dump of file %s\n\n", filename);
dosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
PIMAGE_FILE_HEADER pImgFileHdr = (PIMAGE_FILE_HEADER)lpFileBase;
if ( dosHeader->e_magic == IMAGE_DOS_SIGNATURE )
{
DumpExeFile( dosHeader );
}
else if ( dosHeader->e_magic == IMAGE_SEPARATE_DEBUG_SIGNATURE )
{
DumpDbgFile( (PIMAGE_SEPARATE_DEBUG_HEADER)lpFileBase );
}
else if ( (pImgFileHdr->Machine == IMAGE_FILE_MACHINE_I386) // FIX THIS!!!
||(pImgFileHdr->Machine == IMAGE_FILE_MACHINE_ALPHA) )
{
if ( 0 == pImgFileHdr->SizeOfOptionalHeader ) // 0 optional header
DumpObjFile( pImgFileHdr ); // means it's an OBJ
else if ( pImgFileHdr->SizeOfOptionalHeader
== IMAGE_SIZEOF_ROM_OPTIONAL_HEADER )
{
DumpROMImage( (PIMAGE_ROM_HEADERS)pImgFileHdr );
}
}
else if ( 0 == strncmp((char *)lpFileBase, IMAGE_ARCHIVE_START,
IMAGE_ARCHIVE_START_SIZE ) )
{
DumpLibFile( lpFileBase );
}
else
printf("unrecognized file format\n");
UnmapViewOfFile(lpFileBase);
CloseHandle(hFileMapping);
CloseHandle(hFile);
}
//
// process all the command line arguments and return a pointer to
// the filename argument.
//
PSTR ProcessCommandLine(int argc, char *argv[])
{
int i;
for ( i=1; i < argc; i++ )
{
strupr(argv[i]);
// Is it a switch character?
if ( (argv[i][0] == '-') || (argv[i][0] == '/') )
{
if ( argv[i][1] == 'A' )
{
fShowRelocations = TRUE;
fShowRawSectionData = TRUE;
fShowSymbolTable = TRUE;
fShowLineNumbers = TRUE;
fShowIATentries = TRUE;
fShowPDATA = TRUE;
fShowResources = TRUE;
}
else if ( argv[i][1] == 'H' )
fShowRawSectionData = TRUE;
else if ( argv[i][1] == 'L' )
fShowLineNumbers = TRUE;
else if ( argv[i][1] == 'P' )
fShowPDATA = TRUE;
else if ( argv[i][1] == 'B' )
fShowRelocations = TRUE;
else if ( argv[i][1] == 'S' )
fShowSymbolTable = TRUE;
else if ( argv[i][1] == 'I' )
fShowIATentries = TRUE;
else if ( argv[i][1] == 'R' )
fShowResources = TRUE;
}
else // Not a switch character. Must be the filename
{
return argv[i];
}
}
return NULL;
}
int main(int argc, char *argv[])
{
PSTR filename;
if ( argc == 1 )
{
printf( HelpText );
return 1;
}
filename = ProcessCommandLine(argc, argv);
if ( filename )
DumpFile( filename );
return 0;
}