summaryrefslogtreecommitdiffstats
path: root/src/PEImage.cpp
blob: d9260786d938e0166ee08c3e45b4ad0ad54058ec (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Convert DMD CodeView debug information to PDB files

// Copyright (c) 2009 by Rainer Schuetze, All Rights Reserved

//

// License for redistribution is given by the Artistic License 2.0

// see file LICENSE for further details


#include "PEImage.h"


extern "C" {
#include "mscvpdb.h"

}

#include <stdio.h>

#include <io.h>

#include <fcntl.h>

#include <ctype.h>

#include <direct.h>

#include <sys/stat.h>


///////////////////////////////////////////////////////////////////////

PEImage::PEImage(const char* iname)
: dump_base(0)
, dump_total_len(0)
, dirHeader(0)
, fd(-1)
{
	if(iname)
		load(iname);
}

PEImage::~PEImage()
{
	if(fd != -1)
		close(fd);
	if(dump_base)
		free_aligned(dump_base);
}

///////////////////////////////////////////////////////////////////////

bool PEImage::load(const char* iname)
{
	if (fd != -1)
		return setError("file already open");

	fd = open(iname, O_RDONLY | O_BINARY);
	if (fd == -1) 
		return setError("Can't open file");

	struct stat s;
	if (fstat(fd, &s) < 0)
		return setError("Can't get size");
	dump_total_len = s.st_size;

	dump_base = alloc_aligned(dump_total_len, 0x1000);
	if (!dump_base)
		return setError("Out of memory");
	if (read(fd, dump_base, dump_total_len) != dump_total_len) 
		return setError("Cannot read file");
	

	close(fd);
	fd = -1;
	return initPtr(true);
}

///////////////////////////////////////////////////////////////////////

bool PEImage::save(const char* oname)
{
	if (fd != -1)
		return setError("file already open");

	if (!dump_base)
		return setError("no data to dump");

	fd = open(oname, O_WRONLY | O_CREAT | O_BINARY | O_TRUNC, S_IREAD | S_IWRITE | S_IEXEC);
	if (fd == -1) 
		return setError("Can't create file");

	if (write(fd, dump_base, dump_total_len) != dump_total_len) 
		return setError("Cannot write file");

	close(fd);
	fd = -1;
	return true;
}

///////////////////////////////////////////////////////////////////////

bool PEImage::replaceDebugSection (const void* data, int datalen)
{
	// append new debug directory to data

	IMAGE_DEBUG_DIRECTORY debugdir = *dbgDir;
	int xdatalen = datalen + sizeof(debugdir);

	// assume there is place for another section because of section alignment

	int s;
	DWORD lastVirtualAddress = 0;
	for(s = 0; s < hdr->FileHeader.NumberOfSections; s++)
	{
		if (strcmp ((char*) sec [s].Name, ".debug") == 0)
		{
			if (s == hdr->FileHeader.NumberOfSections - 1)
			{
				dump_total_len = sec[s].PointerToRawData;
				break;
			}
			strcpy ((char*) sec [s].Name, ".ddebug");
			printf("warning: .debug not last section, cannot remove section\n");
		}
		lastVirtualAddress = sec [s].VirtualAddress + sec[s].Misc.VirtualSize;
	}

	int align = hdr->OptionalHeader.FileAlignment;
	int align_len = xdatalen;
	int fill = 0;

	if (align > 0)
	{
		fill = (align - (dump_total_len % align)) % align;
		align_len = ((xdatalen + align - 1) / align) * align;
	}
	char* newdata = (char*) alloc_aligned(dump_total_len + fill + xdatalen, 0x1000);
	if(!newdata)
		return setError("cannot alloc new image");

	int salign_len = xdatalen;
	align = hdr->OptionalHeader.SectionAlignment;
	if (align > 0)
	{
		lastVirtualAddress = ((lastVirtualAddress + align - 1) / align) * align;
		salign_len = ((xdatalen + align - 1) / align) * align;
	}

	strcpy((char*) sec[s].Name, ".debug");
	sec[s].Misc.VirtualSize = align_len; // union with PhysicalAddress;

	sec[s].VirtualAddress = lastVirtualAddress;
	sec[s].SizeOfRawData = xdatalen;
	sec[s].PointerToRawData = dump_total_len + fill;
	sec[s].PointerToRelocations = 0;
	sec[s].PointerToLinenumbers = 0;
	sec[s].NumberOfRelocations = 0;
	sec[s].NumberOfLinenumbers = 0;
	sec[s].Characteristics = IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_CNT_INITIALIZED_DATA;

	hdr->FileHeader.NumberOfSections = s + 1;
	// hdr->OptionalHeader.SizeOfImage += salign_len;

	hdr->OptionalHeader.SizeOfImage = sec[s].VirtualAddress + salign_len;

	hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = lastVirtualAddress + datalen;

	// append debug data chunk to existing file image

	memcpy(newdata, dump_base, dump_total_len);
	memset(newdata + dump_total_len, 0, fill);
	memcpy(newdata + dump_total_len + fill, data, datalen);

	dbgDir = (IMAGE_DEBUG_DIRECTORY*) (newdata + dump_total_len + fill + datalen);
	memcpy(dbgDir, &debugdir, sizeof(debugdir));

	dbgDir->PointerToRawData = sec[s].PointerToRawData;
	dbgDir->AddressOfRawData = sec[s].PointerToRawData;
	dbgDir->SizeOfData = sec[s].SizeOfRawData;
	
	free_aligned(dump_base);
	dump_base = newdata;
	dump_total_len += fill + xdatalen;

	return initPtr(false);
}

///////////////////////////////////////////////////////////////////////

bool PEImage::initPtr(bool initDbgDir)
{
	dos = DPV<IMAGE_DOS_HEADER> (0);
	if(!dos)
		return setError("file too small for DOS header");
	if(dos->e_magic != IMAGE_DOS_SIGNATURE)
		return setError("this is not a DOS executable");

	hdr = DPV<IMAGE_NT_HEADERS32> (dos->e_lfanew);
	if(!hdr)
		return setError("no optional header found");

	if(hdr->Signature != IMAGE_NT_SIGNATURE)
		return setError("optional header does not have PE signature");
	if(hdr->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER32))
		return setError("optional header too small");

	sec = IMAGE_FIRST_SECTION(hdr);

	if(hdr->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_DEBUG)
		return setError("too few entries in data directory");

	if(hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size != 0x1c)
		return setError("unexpected size of DEBUG data directory entry");

	int off = hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
	dbgDir = RVA<IMAGE_DEBUG_DIRECTORY>(off, 0x1c);
	if (!dbgDir)
		return setError("debug directory not placed in image");
	if (dbgDir->Type != IMAGE_DEBUG_TYPE_CODEVIEW)
		return setError("debug directory not of type CodeView");

	cv_base = dbgDir->PointerToRawData;
	OMFSignature* sig = DPV<OMFSignature>(cv_base, dbgDir->SizeOfData);
	if (!sig)
		return setError("invalid debug data base address and size");
	if (memcmp(sig->Signature, "NB09", 4) != 0 && memcmp(sig->Signature, "NB11", 4) != 0)
	{
		// return setError("can only handle debug info of type NB09 and NB11");

		dirHeader = 0;
		dirEntry = 0;
		return true;
	}
	dirHeader = CVP<OMFDirHeader>(sig->filepos);
	if (!dirHeader)
		return setError("invalid cv dir header data base address");
	dirEntry = CVP<OMFDirEntry>(sig->filepos + dirHeader->cbDirHeader);
	if (!dirEntry)
		return setError("cv debug dir entries invalid");

	//if (dirHeader->cDir == 0)

	//	return setError("cv debug dir has no entries");


	return true;
}

///////////////////////////////////////////////////////////////////////

int PEImage::countCVEntries() const
{
	return dirHeader ? dirHeader->cDir : 0; 
}

OMFDirEntry* PEImage::getCVEntry(int i) const
{
	return dirEntry + i; 
}


///////////////////////////////////////////////////////////////////////

// utilities

void* PEImage::alloc_aligned(unsigned int size, unsigned int align, unsigned int alignoff)
{
	if (align & (align - 1))
		return 0;

	unsigned int pad = align + sizeof(void*);
	char* p = (char*) malloc(size + pad);
	unsigned int off = (align + alignoff - sizeof(void*) - (p - (char*) 0)) & (align - 1);
	char* q = p + sizeof(void*) + off;
	((void**) q)[-1] = p;
	return q;
}

///////////////////////////////////////////////////////////////////////

void PEImage::free_aligned(void* p)
{
	void* q = ((void**) p)[-1];
	free(q);
}