summaryrefslogtreecommitdiff
path: root/devtools/test_binaries/PEDUMP.C
blob: dd47ff44a265a85524e007a8d88bd27f644b22e8 (plain) (blame)
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
//--------------------
// PROGRAM: PEDUMP
// FILE:    PEDUMP.C
// AUTHOR:  Matt Pietrek - 1993
//--------------------
#include <windows.h>
#include <stdio.h>
#include "objdump.h"
#include "exedump.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;

char HelpText[] = 
"PEDUMP - Win32/COFF .EXE/.OBJ file dumper - 1993 Matt Pietrek\n\n"
"Syntax: PEDUMP [switches] filename\n\n"
"  /A    include everything in dump\n"
"  /H    include hex dump of sections\n"
"  /L    include line number information\n"
"  /R    show base relocations\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;
	if ( dosHeader->e_magic == IMAGE_DOS_SIGNATURE )
	{
		DumpExeFile( dosHeader );
	}
	else if ( (dosHeader->e_magic == 0x014C)	// Does it look like a i386
		      && (dosHeader->e_sp == 0) )		// COFF OBJ file???
	{
		// The two tests above aren't what they look like.  They're
		// really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C)
		// and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0;
			
		DumpObjFile( (PIMAGE_FILE_HEADER)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;
			}
			else if ( argv[i][1] == 'H' )
				fShowRawSectionData = TRUE;
			else if ( argv[i][1] == 'L' )
				fShowLineNumbers = TRUE;
			else if ( argv[i][1] == 'R' )
				fShowRelocations = TRUE;
			else if ( argv[i][1] == 'S' )
				fShowSymbolTable = TRUE;
		}
		else	// Not a switch character.  Must be the filename
		{
			return argv[i];
		}
	}
}

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;
}