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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
Copyright (C) 2008 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.IO;
using BYTE = System.Byte;
using WORD = System.UInt16;
using DWORD = System.UInt32;
using ULONGLONG = System.UInt64;
using IMAGE_DATA_DIRECTORY = IKVM.Reflection.Reader.IMAGE_DATA_DIRECTORY;
namespace IKVM.Reflection.Writer
{
sealed class PEWriter
{
private readonly BinaryWriter bw;
private readonly IMAGE_NT_HEADERS hdr = new IMAGE_NT_HEADERS();
internal PEWriter(Stream stream)
{
bw = new BinaryWriter(stream);
WriteMSDOSHeader();
}
public IMAGE_NT_HEADERS Headers
{
get { return hdr; }
}
public uint HeaderSize
{
get
{
return (uint)
((8 * 16) + // MSDOS header
4 + // signature
20 + // IMAGE_FILE_HEADER
hdr.FileHeader.SizeOfOptionalHeader +
hdr.FileHeader.NumberOfSections * 40);
}
}
private void WriteMSDOSHeader()
{
bw.Write(new byte[] {
0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD,
0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68,
0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72,
0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E,
0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A,
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
});
}
internal void WritePEHeaders()
{
bw.Write(hdr.Signature);
// IMAGE_FILE_HEADER
bw.Write(hdr.FileHeader.Machine);
bw.Write(hdr.FileHeader.NumberOfSections);
bw.Write(hdr.FileHeader.TimeDateStamp);
bw.Write(hdr.FileHeader.PointerToSymbolTable);
bw.Write(hdr.FileHeader.NumberOfSymbols);
bw.Write(hdr.FileHeader.SizeOfOptionalHeader);
bw.Write(hdr.FileHeader.Characteristics);
// IMAGE_OPTIONAL_HEADER
hdr.OptionalHeader.Write(bw);
}
internal void WriteSectionHeader(SectionHeader sectionHeader)
{
byte[] name = new byte[8];
System.Text.Encoding.UTF8.GetBytes(sectionHeader.Name, 0, sectionHeader.Name.Length, name, 0);
bw.Write(name);
bw.Write(sectionHeader.VirtualSize);
bw.Write(sectionHeader.VirtualAddress);
bw.Write(sectionHeader.SizeOfRawData);
bw.Write(sectionHeader.PointerToRawData);
bw.Write(sectionHeader.PointerToRelocations);
bw.Write(sectionHeader.PointerToLinenumbers);
bw.Write(sectionHeader.NumberOfRelocations);
bw.Write(sectionHeader.NumberOfLinenumbers);
bw.Write(sectionHeader.Characteristics);
}
internal uint ToFileAlignment(uint p)
{
return (p + (Headers.OptionalHeader.FileAlignment - 1)) & ~(Headers.OptionalHeader.FileAlignment - 1);
}
internal uint ToSectionAlignment(uint p)
{
return (p + (Headers.OptionalHeader.SectionAlignment - 1)) & ~(Headers.OptionalHeader.SectionAlignment - 1);
}
}
sealed class IMAGE_NT_HEADERS
{
public DWORD Signature = 0x00004550; // "PE\0\0"
public IMAGE_FILE_HEADER FileHeader = new IMAGE_FILE_HEADER();
public IMAGE_OPTIONAL_HEADER OptionalHeader = new IMAGE_OPTIONAL_HEADER();
}
sealed class IMAGE_FILE_HEADER
{
public const WORD IMAGE_FILE_MACHINE_I386 = 0x014c;
public const WORD IMAGE_FILE_MACHINE_ARM = 0x01c4;
public const WORD IMAGE_FILE_MACHINE_IA64 = 0x0200;
public const WORD IMAGE_FILE_MACHINE_AMD64 = 0x8664;
public const WORD IMAGE_FILE_32BIT_MACHINE = 0x0100;
public const WORD IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002;
public const WORD IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020;
public const WORD IMAGE_FILE_DLL = 0x2000;
public WORD Machine;
public WORD NumberOfSections;
public DWORD TimeDateStamp = (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
public DWORD PointerToSymbolTable = 0;
public DWORD NumberOfSymbols = 0;
public WORD SizeOfOptionalHeader = 0xE0;
public WORD Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
}
sealed class IMAGE_OPTIONAL_HEADER
{
public const WORD IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
public const WORD IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
public const WORD IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
public const WORD IMAGE_SUBSYSTEM_WINDOWS_CUI = 3;
public const WORD IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020;
public const WORD IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040;
public const WORD IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100;
public const WORD IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400;
public const WORD IMAGE_DLLCHARACTERISTICS_APPCONTAINER = 0x1000;
public const WORD IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000;
public WORD Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC;
public BYTE MajorLinkerVersion = 8;
public BYTE MinorLinkerVersion = 0;
public DWORD SizeOfCode;
public DWORD SizeOfInitializedData;
public DWORD SizeOfUninitializedData;
public DWORD AddressOfEntryPoint;
public DWORD BaseOfCode;
public DWORD BaseOfData;
public ULONGLONG ImageBase;
public DWORD SectionAlignment = 0x2000;
public DWORD FileAlignment = 0x200;
public WORD MajorOperatingSystemVersion = 4;
public WORD MinorOperatingSystemVersion = 0;
public WORD MajorImageVersion = 0;
public WORD MinorImageVersion = 0;
public WORD MajorSubsystemVersion = 4;
public WORD MinorSubsystemVersion = 0;
public DWORD Win32VersionValue = 0;
public DWORD SizeOfImage;
public DWORD SizeOfHeaders;
public DWORD CheckSum = 0;
public WORD Subsystem;
public WORD DllCharacteristics;
public ULONGLONG SizeOfStackReserve;
public ULONGLONG SizeOfStackCommit = 0x1000;
public ULONGLONG SizeOfHeapReserve = 0x100000;
public ULONGLONG SizeOfHeapCommit = 0x1000;
public DWORD LoaderFlags = 0;
public DWORD NumberOfRvaAndSizes = 16;
public IMAGE_DATA_DIRECTORY[] DataDirectory = new IMAGE_DATA_DIRECTORY[16];
internal void Write(BinaryWriter bw)
{
bw.Write(Magic);
bw.Write(MajorLinkerVersion);
bw.Write(MinorLinkerVersion);
bw.Write(SizeOfCode);
bw.Write(SizeOfInitializedData);
bw.Write(SizeOfUninitializedData);
bw.Write(AddressOfEntryPoint);
bw.Write(BaseOfCode);
if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
bw.Write(BaseOfData);
bw.Write((DWORD)ImageBase);
}
else
{
bw.Write(ImageBase);
}
bw.Write(SectionAlignment);
bw.Write(FileAlignment);
bw.Write(MajorOperatingSystemVersion);
bw.Write(MinorOperatingSystemVersion);
bw.Write(MajorImageVersion);
bw.Write(MinorImageVersion);
bw.Write(MajorSubsystemVersion);
bw.Write(MinorSubsystemVersion);
bw.Write(Win32VersionValue);
bw.Write(SizeOfImage);
bw.Write(SizeOfHeaders);
bw.Write(CheckSum);
bw.Write(Subsystem);
bw.Write(DllCharacteristics);
if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
bw.Write((DWORD)SizeOfStackReserve);
}
else
{
bw.Write(SizeOfStackReserve);
}
if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
bw.Write((DWORD)SizeOfStackCommit);
}
else
{
bw.Write(SizeOfStackCommit);
}
if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
bw.Write((DWORD)SizeOfHeapReserve);
}
else
{
bw.Write(SizeOfHeapReserve);
}
if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
bw.Write((DWORD)SizeOfHeapCommit);
}
else
{
bw.Write(SizeOfHeapCommit);
}
bw.Write(LoaderFlags);
bw.Write(NumberOfRvaAndSizes);
for (int i = 0; i < DataDirectory.Length; i++)
{
bw.Write(DataDirectory[i].VirtualAddress);
bw.Write(DataDirectory[i].Size);
}
}
}
class SectionHeader
{
public const DWORD IMAGE_SCN_CNT_CODE = 0x00000020;
public const DWORD IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040;
public const DWORD IMAGE_SCN_MEM_DISCARDABLE = 0x02000000;
public const DWORD IMAGE_SCN_MEM_EXECUTE = 0x20000000;
public const DWORD IMAGE_SCN_MEM_READ = 0x40000000;
public const DWORD IMAGE_SCN_MEM_WRITE = 0x80000000;
public string Name; // 8 byte UTF8 encoded 0-padded
public DWORD VirtualSize;
public DWORD VirtualAddress;
public DWORD SizeOfRawData;
public DWORD PointerToRawData;
#pragma warning disable 649 // the follow fields are never assigned to
public DWORD PointerToRelocations;
public DWORD PointerToLinenumbers;
public WORD NumberOfRelocations;
public WORD NumberOfLinenumbers;
#pragma warning restore 649
public DWORD Characteristics;
}
}
|