Debian Codegraph @

external/ikvm/reflect/Writer/PEWriter.cs

raw
1 /*
2 Copyright (C) 2008-2013 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 [email protected]
22
23 */
24 using System;
25 using System.IO;
26 using BYTE = System.Byte;
27 using WORD = System.UInt16;
28 using DWORD = System.UInt32;
29 using ULONGLONG = System.UInt64;
30 using IMAGE_DATA_DIRECTORY = IKVM.Reflection.Reader.IMAGE_DATA_DIRECTORY;
31
32 namespace IKVM.Reflection.Writer
33 {
34 sealed class PEWriter
35 {
36 private readonly BinaryWriter bw;
37 private readonly IMAGE_NT_HEADERS hdr = new IMAGE_NT_HEADERS();
38
39 internal PEWriter(Stream stream)
40 {
41 bw = new BinaryWriter(stream);
42 WriteMSDOSHeader();
43 }
44
45 public IMAGE_NT_HEADERS Headers
46 {
47 get { return hdr; }
48 }
49
50 public uint HeaderSize
51 {
52 get
53 {
54 return (uint)
55 ((8 * 16) + // MSDOS header
56 4 + // signature
57 20 + // IMAGE_FILE_HEADER
58 hdr.FileHeader.SizeOfOptionalHeader +
59 hdr.FileHeader.NumberOfSections * 40);
60 }
61 }
62
63 private void WriteMSDOSHeader()
64 {
65 bw.Write(new byte[] {
66 0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00,
67 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
68 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
74 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD,
75 0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68,
76 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72,
77 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
78 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E,
79 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
80 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A,
81 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
82 });
83 }
84
85 internal void WritePEHeaders()
86 {
87 bw.Write(hdr.Signature);
88
89 // IMAGE_FILE_HEADER
90 bw.Write(hdr.FileHeader.Machine);
91 bw.Write(hdr.FileHeader.NumberOfSections);
92 bw.Write(hdr.FileHeader.TimeDateStamp);
93 bw.Write(hdr.FileHeader.PointerToSymbolTable);
94 bw.Write(hdr.FileHeader.NumberOfSymbols);
95 bw.Write(hdr.FileHeader.SizeOfOptionalHeader);
96 bw.Write(hdr.FileHeader.Characteristics);
97
98 // IMAGE_OPTIONAL_HEADER
99 hdr.OptionalHeader.Write(bw);
100 }
101
102 internal void WriteSectionHeader(SectionHeader sectionHeader)
103 {
104 byte[] name = new byte[8];
105 System.Text.Encoding.UTF8.GetBytes(sectionHeader.Name, 0, sectionHeader.Name.Length, name, 0);
106 bw.Write(name);
107 bw.Write(sectionHeader.VirtualSize);
108 bw.Write(sectionHeader.VirtualAddress);
109 bw.Write(sectionHeader.SizeOfRawData);
110 bw.Write(sectionHeader.PointerToRawData);
111 bw.Write(sectionHeader.PointerToRelocations);
112 bw.Write(sectionHeader.PointerToLinenumbers);
113 bw.Write(sectionHeader.NumberOfRelocations);
114 bw.Write(sectionHeader.NumberOfLinenumbers);
115 bw.Write(sectionHeader.Characteristics);
116 }
117
118 internal uint ToFileAlignment(uint p)
119 {
120 return (p + (Headers.OptionalHeader.FileAlignment - 1)) & ~(Headers.OptionalHeader.FileAlignment - 1);
121 }
122
123 internal uint ToSectionAlignment(uint p)
124 {
125 return (p + (Headers.OptionalHeader.SectionAlignment - 1)) & ~(Headers.OptionalHeader.SectionAlignment - 1);
126 }
127
128 internal bool Is32Bit
129 {
130 get { return (Headers.FileHeader.Characteristics & IMAGE_FILE_HEADER.IMAGE_FILE_32BIT_MACHINE) != 0; }
131 }
132
133 internal uint Thumb
134 {
135 // On ARM we need to set the least significant bit of the program counter to select the Thumb instruction set
136 get { return Headers.FileHeader.Machine == IMAGE_FILE_HEADER.IMAGE_FILE_MACHINE_ARM ? 1u : 0u; }
137 }
138 }
139
140 sealed class IMAGE_NT_HEADERS
141 {
142 public DWORD Signature = 0x00004550; // "PE\0\0"
143 public IMAGE_FILE_HEADER FileHeader = new IMAGE_FILE_HEADER();
144 public IMAGE_OPTIONAL_HEADER OptionalHeader = new IMAGE_OPTIONAL_HEADER();
145 }
146
147 sealed class IMAGE_FILE_HEADER
148 {
149 public const WORD IMAGE_FILE_MACHINE_I386 = 0x014c;
150 public const WORD IMAGE_FILE_MACHINE_ARM = 0x01c4;
151 public const WORD IMAGE_FILE_MACHINE_IA64 = 0x0200;
152 public const WORD IMAGE_FILE_MACHINE_AMD64 = 0x8664;
153
154 public const WORD IMAGE_FILE_32BIT_MACHINE = 0x0100;
155 public const WORD IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002;
156 public const WORD IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020;
157 public const WORD IMAGE_FILE_DLL = 0x2000;
158
159 public WORD Machine;
160 public WORD NumberOfSections;
161 public DWORD TimeDateStamp;
162 public DWORD PointerToSymbolTable = 0;
163 public DWORD NumberOfSymbols = 0;
164 public WORD SizeOfOptionalHeader = 0xE0;
165 public WORD Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
166 }
167
168 sealed class IMAGE_OPTIONAL_HEADER
169 {
170 public const WORD IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
171 public const WORD IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
172
173 public const WORD IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
174 public const WORD IMAGE_SUBSYSTEM_WINDOWS_CUI = 3;
175
176 public WORD Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC;
177 public BYTE MajorLinkerVersion = 8;
178 public BYTE MinorLinkerVersion = 0;
179 public DWORD SizeOfCode;
180 public DWORD SizeOfInitializedData;
181 public DWORD SizeOfUninitializedData;
182 public DWORD AddressOfEntryPoint;
183 public DWORD BaseOfCode;
184 public DWORD BaseOfData;
185 public ULONGLONG ImageBase;
186 public DWORD SectionAlignment = 0x2000;
187 public DWORD FileAlignment;
188 public WORD MajorOperatingSystemVersion = 4;
189 public WORD MinorOperatingSystemVersion = 0;
190 public WORD MajorImageVersion = 0;
191 public WORD MinorImageVersion = 0;
192 public WORD MajorSubsystemVersion = 4;
193 public WORD MinorSubsystemVersion = 0;
194 public DWORD Win32VersionValue = 0;
195 public DWORD SizeOfImage;
196 public DWORD SizeOfHeaders;
197 public DWORD CheckSum = 0;
198 public WORD Subsystem;
199 public WORD DllCharacteristics;
200 public ULONGLONG SizeOfStackReserve;
201 public ULONGLONG SizeOfStackCommit = 0x1000;
202 public ULONGLONG SizeOfHeapReserve = 0x100000;
203 public ULONGLONG SizeOfHeapCommit = 0x1000;
204 public DWORD LoaderFlags = 0;
205 public DWORD NumberOfRvaAndSizes = 16;
206 public IMAGE_DATA_DIRECTORY[] DataDirectory = new IMAGE_DATA_DIRECTORY[16];
207
208 internal void Write(BinaryWriter bw)
209 {
210 bw.Write(Magic);
211 bw.Write(MajorLinkerVersion);
212 bw.Write(MinorLinkerVersion);
213 bw.Write(SizeOfCode);
214 bw.Write(SizeOfInitializedData);
215 bw.Write(SizeOfUninitializedData);
216 bw.Write(AddressOfEntryPoint);
217 bw.Write(BaseOfCode);
218 if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
219 {
220 bw.Write(BaseOfData);
221 bw.Write((DWORD)ImageBase);
222 }
223 else
224 {
225 bw.Write(ImageBase);
226 }
227 bw.Write(SectionAlignment);
228 bw.Write(FileAlignment);
229 bw.Write(MajorOperatingSystemVersion);
230 bw.Write(MinorOperatingSystemVersion);
231 bw.Write(MajorImageVersion);
232 bw.Write(MinorImageVersion);
233 bw.Write(MajorSubsystemVersion);
234 bw.Write(MinorSubsystemVersion);
235 bw.Write(Win32VersionValue);
236 bw.Write(SizeOfImage);
237 bw.Write(SizeOfHeaders);
238 bw.Write(CheckSum);
239 bw.Write(Subsystem);
240 bw.Write(DllCharacteristics);
241 if (Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
242 {
243 bw.Write((DWORD)SizeOfStackReserve);
244 bw.Write((DWORD)SizeOfStackCommit);
245 bw.Write((DWORD)SizeOfHeapReserve);
246 bw.Write((DWORD)SizeOfHeapCommit);
247 }
248 else
249 {
250 bw.Write(SizeOfStackReserve);
251 bw.Write(SizeOfStackCommit);
252 bw.Write(SizeOfHeapReserve);
253 bw.Write(SizeOfHeapCommit);
254 }
255 bw.Write(LoaderFlags);
256 bw.Write(NumberOfRvaAndSizes);
257 for (int i = 0; i < DataDirectory.Length; i++)
258 {
259 bw.Write(DataDirectory[i].VirtualAddress);
260 bw.Write(DataDirectory[i].Size);
261 }
262 }
263 }
264
265 class SectionHeader
266 {
267 public const DWORD IMAGE_SCN_CNT_CODE = 0x00000020;
268 public const DWORD IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040;
269 public const DWORD IMAGE_SCN_MEM_DISCARDABLE = 0x02000000;
270 public const DWORD IMAGE_SCN_MEM_EXECUTE = 0x20000000;
271 public const DWORD IMAGE_SCN_MEM_READ = 0x40000000;
272 public const DWORD IMAGE_SCN_MEM_WRITE = 0x80000000;
273
274 public string Name; // 8 byte UTF8 encoded 0-padded
275 public DWORD VirtualSize;
276 public DWORD VirtualAddress;
277 public DWORD SizeOfRawData;
278 public DWORD PointerToRawData;
279 #pragma warning disable 649 // the follow fields are never assigned to
280 public DWORD PointerToRelocations;
281 public DWORD PointerToLinenumbers;
282 public WORD NumberOfRelocations;
283 public WORD NumberOfLinenumbers;
284 #pragma warning restore 649
285 public DWORD Characteristics;
286 }
287 }