summaryrefslogtreecommitdiffstats
path: root/src/PEImage.h
blob: 039d2a1d6203af0d78e97fef76cfea5eea42fee2 (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
// 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


#ifndef __PEIMAGE_H__

#define __PEIMAGE_H__


#include "LastError.h"


#include <windows.h>


struct OMFDirHeader;
struct OMFDirEntry;

class PEImage : public LastError
{
public:
	PEImage(const char* iname = 0);
	~PEImage();

	template<class P> P* DP(int off) 
	{
		return (P*) ((char*) dump_base + off); 
	}
	template<class P> P* DPV(int off, int size) 
	{ 
		if(off < 0 || off + size > dump_total_len)
			return 0;
		return (P*) ((char*) dump_base + off); 
	}
	template<class P> P* DPV(int off) 
	{
		return DPV<P>(off, sizeof(P));
	}
	template<class P> P* CVP(int off) 
	{
		return DPV<P>(cv_base + off, sizeof(P));
	}

	template<class P> P* RVA(unsigned long rva, int len)
	{
		IMAGE_DOS_HEADER *dos = DPV<IMAGE_DOS_HEADER> (0);
		IMAGE_NT_HEADERS32* hdr = DPV<IMAGE_NT_HEADERS32> (dos->e_lfanew);
		IMAGE_SECTION_HEADER* sec = IMAGE_FIRST_SECTION(hdr);

		for (int i = 0; i < hdr->FileHeader.NumberOfSections; i++)
		{
			if (rva       >= sec[i].VirtualAddress &&
			    rva + len <= sec[i].VirtualAddress + sec[i].SizeOfRawData)
				return DPV<P>(sec[i].PointerToRawData + rva - sec[i].VirtualAddress, len);
		}
		return 0;
	}

	bool load(const char* iname);
	bool save(const char* oname);

	bool replaceDebugSection (const void* data, int datalen);
	bool initPtr(bool initDbgDir);

	int countCVEntries() const;
	OMFDirEntry* getCVEntry(int i) const;

	int getCVSize() const { return dbgDir->SizeOfData; }

	// utilities

	static void* alloc_aligned(unsigned int size, unsigned int align, unsigned int alignoff = 0);
	static void free_aligned(void* p);

private:
	int fd;
	void* dump_base;
	int dump_total_len;

	IMAGE_DOS_HEADER *dos;
	IMAGE_NT_HEADERS32* hdr;
	IMAGE_SECTION_HEADER* sec;
	IMAGE_DEBUG_DIRECTORY* dbgDir;
	OMFDirHeader* dirHeader;
	OMFDirEntry* dirEntry;
	
	int cv_base;
};


#endif //__PEIMAGE_H__