/Mono.Cecil.PE/ImageWriter.cs

http://github.com/jbevain/cecil · C# · 856 lines · 662 code · 156 blank · 38 comment · 90 complexity · b27fbef79e291e83c782272c26f4665c MD5 · raw file

  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.IO;
  12. using Mono.Cecil.Cil;
  13. using Mono.Cecil.Metadata;
  14. using RVA = System.UInt32;
  15. namespace Mono.Cecil.PE {
  16. sealed class ImageWriter : BinaryStreamWriter {
  17. readonly ModuleDefinition module;
  18. readonly MetadataBuilder metadata;
  19. readonly TextMap text_map;
  20. readonly internal Disposable<Stream> stream;
  21. readonly string runtime_version;
  22. ImageDebugHeader debug_header;
  23. ByteBuffer win32_resources;
  24. const uint pe_header_size = 0x98u;
  25. const uint section_header_size = 0x28u;
  26. const uint file_alignment = 0x200;
  27. const uint section_alignment = 0x2000;
  28. const ulong image_base = 0x00400000;
  29. internal const RVA text_rva = 0x2000;
  30. readonly bool pe64;
  31. readonly bool has_reloc;
  32. internal Section text;
  33. internal Section rsrc;
  34. internal Section reloc;
  35. ushort sections;
  36. ImageWriter (ModuleDefinition module, string runtime_version, MetadataBuilder metadata, Disposable<Stream> stream, bool metadataOnly = false)
  37. : base (stream.value)
  38. {
  39. this.module = module;
  40. this.runtime_version = runtime_version;
  41. this.text_map = metadata.text_map;
  42. this.stream = stream;
  43. this.metadata = metadata;
  44. if (metadataOnly)
  45. return;
  46. this.pe64 = module.Architecture == TargetArchitecture.AMD64 || module.Architecture == TargetArchitecture.IA64 || module.Architecture == TargetArchitecture.ARM64;
  47. this.has_reloc = module.Architecture == TargetArchitecture.I386;
  48. this.GetDebugHeader ();
  49. this.GetWin32Resources ();
  50. this.BuildTextMap ();
  51. this.sections = (ushort) (has_reloc ? 2 : 1); // text + reloc?
  52. }
  53. void GetDebugHeader ()
  54. {
  55. var symbol_writer = metadata.symbol_writer;
  56. if (symbol_writer != null)
  57. debug_header = symbol_writer.GetDebugHeader ();
  58. if (module.HasDebugHeader) {
  59. var header = module.GetDebugHeader ();
  60. var deterministic = header.GetDeterministicEntry ();
  61. if (deterministic == null)
  62. return;
  63. debug_header = debug_header.AddDeterministicEntry ();
  64. }
  65. }
  66. void GetWin32Resources ()
  67. {
  68. if (!module.HasImage)
  69. return;
  70. DataDirectory win32_resources_directory = module.Image.Win32Resources;
  71. var size = win32_resources_directory.Size;
  72. if (size > 0) {
  73. win32_resources = module.Image.GetReaderAt (win32_resources_directory.VirtualAddress, size, (s, reader) => new ByteBuffer (reader.ReadBytes ((int) s)));
  74. }
  75. }
  76. public static ImageWriter CreateWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable<Stream> stream)
  77. {
  78. var writer = new ImageWriter (module, module.runtime_version, metadata, stream);
  79. writer.BuildSections ();
  80. return writer;
  81. }
  82. public static ImageWriter CreateDebugWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable<Stream> stream)
  83. {
  84. var writer = new ImageWriter (module, "PDB v1.0", metadata, stream, metadataOnly: true);
  85. var length = metadata.text_map.GetLength ();
  86. writer.text = new Section { SizeOfRawData = length, VirtualSize = length };
  87. return writer;
  88. }
  89. void BuildSections ()
  90. {
  91. var has_win32_resources = win32_resources != null;
  92. if (has_win32_resources)
  93. sections++;
  94. text = CreateSection (".text", text_map.GetLength (), null);
  95. var previous = text;
  96. if (has_win32_resources) {
  97. rsrc = CreateSection (".rsrc", (uint) win32_resources.length, previous);
  98. PatchWin32Resources (win32_resources);
  99. previous = rsrc;
  100. }
  101. if (has_reloc)
  102. reloc = CreateSection (".reloc", 12u, previous);
  103. }
  104. Section CreateSection (string name, uint size, Section previous)
  105. {
  106. return new Section {
  107. Name = name,
  108. VirtualAddress = previous != null
  109. ? previous.VirtualAddress + Align (previous.VirtualSize, section_alignment)
  110. : text_rva,
  111. VirtualSize = size,
  112. PointerToRawData = previous != null
  113. ? previous.PointerToRawData + previous.SizeOfRawData
  114. : Align (GetHeaderSize (), file_alignment),
  115. SizeOfRawData = Align (size, file_alignment)
  116. };
  117. }
  118. static uint Align (uint value, uint align)
  119. {
  120. align--;
  121. return (value + align) & ~align;
  122. }
  123. void WriteDOSHeader ()
  124. {
  125. Write (new byte [] {
  126. // dos header start
  127. 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00,
  128. 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff,
  129. 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00,
  136. // lfanew
  137. 0x80, 0x00, 0x00, 0x00,
  138. // dos header end
  139. 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09,
  140. 0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21,
  141. 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72,
  142. 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x63,
  143. 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62,
  144. 0x65, 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69,
  145. 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, 0x6d,
  146. 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a,
  147. 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00
  149. });
  150. }
  151. ushort SizeOfOptionalHeader ()
  152. {
  153. return (ushort) (!pe64 ? 0xe0 : 0xf0);
  154. }
  155. void WritePEFileHeader ()
  156. {
  157. WriteUInt32 (0x00004550); // Magic
  158. WriteUInt16 ((ushort) module.Architecture); // Machine
  159. WriteUInt16 (sections); // NumberOfSections
  160. WriteUInt32 (metadata.timestamp);
  161. WriteUInt32 (0); // PointerToSymbolTable
  162. WriteUInt32 (0); // NumberOfSymbols
  163. WriteUInt16 (SizeOfOptionalHeader ()); // SizeOfOptionalHeader
  164. // ExecutableImage | (pe64 ? 32BitsMachine : LargeAddressAware)
  165. var characteristics = (ushort) (0x0002 | (!pe64 ? 0x0100 : 0x0020));
  166. if (module.Kind == ModuleKind.Dll || module.Kind == ModuleKind.NetModule)
  167. characteristics |= 0x2000;
  168. WriteUInt16 (characteristics); // Characteristics
  169. }
  170. Section LastSection ()
  171. {
  172. if (reloc != null)
  173. return reloc;
  174. if (rsrc != null)
  175. return rsrc;
  176. return text;
  177. }
  178. void WriteOptionalHeaders ()
  179. {
  180. WriteUInt16 ((ushort) (!pe64 ? 0x10b : 0x20b)); // Magic
  181. WriteUInt16 (module.linker_version);
  182. WriteUInt32 (text.SizeOfRawData); // CodeSize
  183. WriteUInt32 ((reloc != null ? reloc.SizeOfRawData : 0)
  184. + (rsrc != null ? rsrc.SizeOfRawData : 0)); // InitializedDataSize
  185. WriteUInt32 (0); // UninitializedDataSize
  186. var startub_stub = text_map.GetRange (TextSegment.StartupStub);
  187. WriteUInt32 (startub_stub.Length > 0 ? startub_stub.Start : 0); // EntryPointRVA
  188. WriteUInt32 (text_rva); // BaseOfCode
  189. if (!pe64) {
  190. WriteUInt32 (0); // BaseOfData
  191. WriteUInt32 ((uint) image_base); // ImageBase
  192. } else {
  193. WriteUInt64 (image_base); // ImageBase
  194. }
  195. WriteUInt32 (section_alignment); // SectionAlignment
  196. WriteUInt32 (file_alignment); // FileAlignment
  197. WriteUInt16 (4); // OSMajor
  198. WriteUInt16 (0); // OSMinor
  199. WriteUInt16 (0); // UserMajor
  200. WriteUInt16 (0); // UserMinor
  201. WriteUInt16 (module.subsystem_major); // SubSysMajor
  202. WriteUInt16 (module.subsystem_minor); // SubSysMinor
  203. WriteUInt32 (0); // Reserved
  204. var last_section = LastSection();
  205. WriteUInt32 (last_section.VirtualAddress + Align (last_section.VirtualSize, section_alignment)); // ImageSize
  206. WriteUInt32 (text.PointerToRawData); // HeaderSize
  207. WriteUInt32 (0); // Checksum
  208. WriteUInt16 (GetSubSystem ()); // SubSystem
  209. WriteUInt16 ((ushort) module.Characteristics); // DLLFlags
  210. if (!pe64) {
  211. const uint stack_reserve = 0x100000;
  212. const uint stack_commit = 0x1000;
  213. const uint heap_reserve = 0x100000;
  214. const uint heap_commit = 0x1000;
  215. WriteUInt32 (stack_reserve);
  216. WriteUInt32 (stack_commit);
  217. WriteUInt32 (heap_reserve);
  218. WriteUInt32 (heap_commit);
  219. } else {
  220. const ulong stack_reserve = 0x400000;
  221. const ulong stack_commit = 0x4000;
  222. const ulong heap_reserve = 0x100000;
  223. const ulong heap_commit = 0x2000;
  224. WriteUInt64 (stack_reserve);
  225. WriteUInt64 (stack_commit);
  226. WriteUInt64 (heap_reserve);
  227. WriteUInt64 (heap_commit);
  228. }
  229. WriteUInt32 (0); // LoaderFlags
  230. WriteUInt32 (16); // NumberOfDataDir
  231. WriteZeroDataDirectory (); // ExportTable
  232. WriteDataDirectory (text_map.GetDataDirectory (TextSegment.ImportDirectory)); // ImportTable
  233. if (rsrc != null) { // ResourceTable
  234. WriteUInt32 (rsrc.VirtualAddress);
  235. WriteUInt32 (rsrc.VirtualSize);
  236. } else
  237. WriteZeroDataDirectory ();
  238. WriteZeroDataDirectory (); // ExceptionTable
  239. WriteZeroDataDirectory (); // CertificateTable
  240. WriteUInt32 (reloc != null ? reloc.VirtualAddress : 0); // BaseRelocationTable
  241. WriteUInt32 (reloc != null ? reloc.VirtualSize : 0);
  242. if (text_map.GetLength (TextSegment.DebugDirectory) > 0) {
  243. WriteUInt32 (text_map.GetRVA (TextSegment.DebugDirectory));
  244. WriteUInt32 ((uint) (debug_header.Entries.Length * ImageDebugDirectory.Size));
  245. } else
  246. WriteZeroDataDirectory ();
  247. WriteZeroDataDirectory (); // Copyright
  248. WriteZeroDataDirectory (); // GlobalPtr
  249. WriteZeroDataDirectory (); // TLSTable
  250. WriteZeroDataDirectory (); // LoadConfigTable
  251. WriteZeroDataDirectory (); // BoundImport
  252. WriteDataDirectory (text_map.GetDataDirectory (TextSegment.ImportAddressTable)); // IAT
  253. WriteZeroDataDirectory (); // DelayImportDesc
  254. WriteDataDirectory (text_map.GetDataDirectory (TextSegment.CLIHeader)); // CLIHeader
  255. WriteZeroDataDirectory (); // Reserved
  256. }
  257. void WriteZeroDataDirectory ()
  258. {
  259. WriteUInt32 (0);
  260. WriteUInt32 (0);
  261. }
  262. ushort GetSubSystem ()
  263. {
  264. switch (module.Kind) {
  265. case ModuleKind.Console:
  266. case ModuleKind.Dll:
  267. case ModuleKind.NetModule:
  268. return 0x3;
  269. case ModuleKind.Windows:
  270. return 0x2;
  271. default:
  272. throw new ArgumentOutOfRangeException ();
  273. }
  274. }
  275. void WriteSectionHeaders ()
  276. {
  277. WriteSection (text, 0x60000020);
  278. if (rsrc != null)
  279. WriteSection (rsrc, 0x40000040);
  280. if (reloc != null)
  281. WriteSection (reloc, 0x42000040);
  282. }
  283. void WriteSection (Section section, uint characteristics)
  284. {
  285. var name = new byte [8];
  286. var sect_name = section.Name;
  287. for (int i = 0; i < sect_name.Length; i++)
  288. name [i] = (byte) sect_name [i];
  289. WriteBytes (name);
  290. WriteUInt32 (section.VirtualSize);
  291. WriteUInt32 (section.VirtualAddress);
  292. WriteUInt32 (section.SizeOfRawData);
  293. WriteUInt32 (section.PointerToRawData);
  294. WriteUInt32 (0); // PointerToRelocations
  295. WriteUInt32 (0); // PointerToLineNumbers
  296. WriteUInt16 (0); // NumberOfRelocations
  297. WriteUInt16 (0); // NumberOfLineNumbers
  298. WriteUInt32 (characteristics);
  299. }
  300. uint GetRVAFileOffset (Section section, RVA rva)
  301. {
  302. return section.PointerToRawData + rva - section.VirtualAddress;
  303. }
  304. void MoveTo (uint pointer)
  305. {
  306. BaseStream.Seek (pointer, SeekOrigin.Begin);
  307. }
  308. void MoveToRVA (Section section, RVA rva)
  309. {
  310. BaseStream.Seek (GetRVAFileOffset (section, rva), SeekOrigin.Begin);
  311. }
  312. void MoveToRVA (TextSegment segment)
  313. {
  314. MoveToRVA (text, text_map.GetRVA (segment));
  315. }
  316. void WriteRVA (RVA rva)
  317. {
  318. if (!pe64)
  319. WriteUInt32 (rva);
  320. else
  321. WriteUInt64 (rva);
  322. }
  323. void PrepareSection (Section section)
  324. {
  325. MoveTo (section.PointerToRawData);
  326. const int buffer_size = 4096;
  327. if (section.SizeOfRawData <= buffer_size) {
  328. Write (new byte [section.SizeOfRawData]);
  329. MoveTo (section.PointerToRawData);
  330. return;
  331. }
  332. var written = 0;
  333. var buffer = new byte [buffer_size];
  334. while (written != section.SizeOfRawData) {
  335. var write_size = System.Math.Min((int) section.SizeOfRawData - written, buffer_size);
  336. Write (buffer, 0, write_size);
  337. written += write_size;
  338. }
  339. MoveTo (section.PointerToRawData);
  340. }
  341. void WriteText ()
  342. {
  343. PrepareSection (text);
  344. // ImportAddressTable
  345. if (has_reloc) {
  346. WriteRVA (text_map.GetRVA (TextSegment.ImportHintNameTable));
  347. WriteRVA (0);
  348. }
  349. // CLIHeader
  350. WriteUInt32 (0x48);
  351. WriteUInt16 (2);
  352. WriteUInt16 ((ushort) ((module.Runtime <= TargetRuntime.Net_1_1) ? 0 : 5));
  353. WriteUInt32 (text_map.GetRVA (TextSegment.MetadataHeader));
  354. WriteUInt32 (GetMetadataLength ());
  355. WriteUInt32 ((uint) module.Attributes);
  356. WriteUInt32 (metadata.entry_point.ToUInt32 ());
  357. WriteDataDirectory (text_map.GetDataDirectory (TextSegment.Resources));
  358. WriteDataDirectory (text_map.GetDataDirectory (TextSegment.StrongNameSignature));
  359. WriteZeroDataDirectory (); // CodeManagerTable
  360. WriteZeroDataDirectory (); // VTableFixups
  361. WriteZeroDataDirectory (); // ExportAddressTableJumps
  362. WriteZeroDataDirectory (); // ManagedNativeHeader
  363. // Code
  364. MoveToRVA (TextSegment.Code);
  365. WriteBuffer (metadata.code);
  366. // Resources
  367. MoveToRVA (TextSegment.Resources);
  368. WriteBuffer (metadata.resources);
  369. // Data
  370. if (metadata.data.length > 0) {
  371. MoveToRVA (TextSegment.Data);
  372. WriteBuffer (metadata.data);
  373. }
  374. // StrongNameSignature
  375. // stays blank
  376. // MetadataHeader
  377. MoveToRVA (TextSegment.MetadataHeader);
  378. WriteMetadataHeader ();
  379. WriteMetadata ();
  380. // DebugDirectory
  381. if (text_map.GetLength (TextSegment.DebugDirectory) > 0) {
  382. MoveToRVA (TextSegment.DebugDirectory);
  383. WriteDebugDirectory ();
  384. }
  385. if (!has_reloc)
  386. return;
  387. // ImportDirectory
  388. MoveToRVA (TextSegment.ImportDirectory);
  389. WriteImportDirectory ();
  390. // StartupStub
  391. MoveToRVA (TextSegment.StartupStub);
  392. WriteStartupStub ();
  393. }
  394. uint GetMetadataLength ()
  395. {
  396. return text_map.GetRVA (TextSegment.DebugDirectory) - text_map.GetRVA (TextSegment.MetadataHeader);
  397. }
  398. public void WriteMetadataHeader ()
  399. {
  400. WriteUInt32 (0x424a5342); // Signature
  401. WriteUInt16 (1); // MajorVersion
  402. WriteUInt16 (1); // MinorVersion
  403. WriteUInt32 (0); // Reserved
  404. var version = GetZeroTerminatedString (runtime_version);
  405. WriteUInt32 ((uint) version.Length);
  406. WriteBytes (version);
  407. WriteUInt16 (0); // Flags
  408. WriteUInt16 (GetStreamCount ());
  409. uint offset = text_map.GetRVA (TextSegment.TableHeap) - text_map.GetRVA (TextSegment.MetadataHeader);
  410. WriteStreamHeader (ref offset, TextSegment.TableHeap, "#~");
  411. WriteStreamHeader (ref offset, TextSegment.StringHeap, "#Strings");
  412. WriteStreamHeader (ref offset, TextSegment.UserStringHeap, "#US");
  413. WriteStreamHeader (ref offset, TextSegment.GuidHeap, "#GUID");
  414. WriteStreamHeader (ref offset, TextSegment.BlobHeap, "#Blob");
  415. WriteStreamHeader (ref offset, TextSegment.PdbHeap, "#Pdb");
  416. }
  417. ushort GetStreamCount ()
  418. {
  419. return (ushort) (
  420. 1 // #~
  421. + 1 // #Strings
  422. + (metadata.user_string_heap.IsEmpty ? 0 : 1) // #US
  423. + (metadata.guid_heap.IsEmpty ? 0 : 1) // GUID
  424. + (metadata.blob_heap.IsEmpty ? 0 : 1)
  425. + (metadata.pdb_heap == null ? 0 : 1)); // #Blob
  426. }
  427. void WriteStreamHeader (ref uint offset, TextSegment heap, string name)
  428. {
  429. var length = (uint) text_map.GetLength (heap);
  430. if (length == 0)
  431. return;
  432. WriteUInt32 (offset);
  433. WriteUInt32 (length);
  434. WriteBytes (GetZeroTerminatedString (name));
  435. offset += length;
  436. }
  437. static int GetZeroTerminatedStringLength (string @string)
  438. {
  439. return (@string.Length + 1 + 3) & ~3;
  440. }
  441. static byte [] GetZeroTerminatedString (string @string)
  442. {
  443. return GetString (@string, GetZeroTerminatedStringLength (@string));
  444. }
  445. static byte [] GetSimpleString (string @string)
  446. {
  447. return GetString (@string, @string.Length);
  448. }
  449. static byte [] GetString (string @string, int length)
  450. {
  451. var bytes = new byte [length];
  452. for (int i = 0; i < @string.Length; i++)
  453. bytes [i] = (byte) @string [i];
  454. return bytes;
  455. }
  456. public void WriteMetadata ()
  457. {
  458. WriteHeap (TextSegment.TableHeap, metadata.table_heap);
  459. WriteHeap (TextSegment.StringHeap, metadata.string_heap);
  460. WriteHeap (TextSegment.UserStringHeap, metadata.user_string_heap);
  461. WriteHeap (TextSegment.GuidHeap, metadata.guid_heap);
  462. WriteHeap (TextSegment.BlobHeap, metadata.blob_heap);
  463. WriteHeap (TextSegment.PdbHeap, metadata.pdb_heap);
  464. }
  465. void WriteHeap (TextSegment heap, HeapBuffer buffer)
  466. {
  467. if (buffer == null || buffer.IsEmpty)
  468. return;
  469. MoveToRVA (heap);
  470. WriteBuffer (buffer);
  471. }
  472. void WriteDebugDirectory ()
  473. {
  474. var data_start = (int) BaseStream.Position + (debug_header.Entries.Length * ImageDebugDirectory.Size);
  475. for (var i = 0; i < debug_header.Entries.Length; i++) {
  476. var entry = debug_header.Entries [i];
  477. var directory = entry.Directory;
  478. WriteInt32 (directory.Characteristics);
  479. WriteInt32 (directory.TimeDateStamp);
  480. WriteInt16 (directory.MajorVersion);
  481. WriteInt16 (directory.MinorVersion);
  482. WriteInt32 ((int) directory.Type);
  483. WriteInt32 (directory.SizeOfData);
  484. WriteInt32 (directory.AddressOfRawData);
  485. WriteInt32 (data_start);
  486. data_start += entry.Data.Length;
  487. }
  488. for (var i = 0; i < debug_header.Entries.Length; i++) {
  489. var entry = debug_header.Entries [i];
  490. WriteBytes (entry.Data);
  491. }
  492. }
  493. void WriteImportDirectory ()
  494. {
  495. WriteUInt32 (text_map.GetRVA (TextSegment.ImportDirectory) + 40); // ImportLookupTable
  496. WriteUInt32 (0); // DateTimeStamp
  497. WriteUInt32 (0); // ForwarderChain
  498. WriteUInt32 (text_map.GetRVA (TextSegment.ImportHintNameTable) + 14);
  499. WriteUInt32 (text_map.GetRVA (TextSegment.ImportAddressTable));
  500. Advance (20);
  501. // ImportLookupTable
  502. WriteUInt32 (text_map.GetRVA (TextSegment.ImportHintNameTable));
  503. // ImportHintNameTable
  504. MoveToRVA (TextSegment.ImportHintNameTable);
  505. WriteUInt16 (0); // Hint
  506. WriteBytes (GetRuntimeMain ());
  507. WriteByte (0);
  508. WriteBytes (GetSimpleString ("mscoree.dll"));
  509. WriteUInt16 (0);
  510. }
  511. byte [] GetRuntimeMain ()
  512. {
  513. return module.Kind == ModuleKind.Dll || module.Kind == ModuleKind.NetModule
  514. ? GetSimpleString ("_CorDllMain")
  515. : GetSimpleString ("_CorExeMain");
  516. }
  517. void WriteStartupStub ()
  518. {
  519. switch (module.Architecture) {
  520. case TargetArchitecture.I386:
  521. WriteUInt16 (0x25ff);
  522. WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.ImportAddressTable));
  523. return;
  524. default:
  525. throw new NotSupportedException ();
  526. }
  527. }
  528. void WriteRsrc ()
  529. {
  530. PrepareSection (rsrc);
  531. WriteBuffer (win32_resources);
  532. }
  533. void WriteReloc ()
  534. {
  535. PrepareSection (reloc);
  536. var reloc_rva = text_map.GetRVA (TextSegment.StartupStub);
  537. reloc_rva += module.Architecture == TargetArchitecture.IA64 ? 0x20u : 2;
  538. var page_rva = reloc_rva & ~0xfffu;
  539. WriteUInt32 (page_rva); // PageRVA
  540. WriteUInt32 (0x000c); // Block Size
  541. switch (module.Architecture) {
  542. case TargetArchitecture.I386:
  543. WriteUInt32 (0x3000 + reloc_rva - page_rva);
  544. break;
  545. default:
  546. throw new NotSupportedException();
  547. }
  548. }
  549. public void WriteImage ()
  550. {
  551. WriteDOSHeader ();
  552. WritePEFileHeader ();
  553. WriteOptionalHeaders ();
  554. WriteSectionHeaders ();
  555. WriteText ();
  556. if (rsrc != null)
  557. WriteRsrc ();
  558. if (reloc != null)
  559. WriteReloc ();
  560. Flush ();
  561. }
  562. void BuildTextMap ()
  563. {
  564. var map = text_map;
  565. map.AddMap (TextSegment.Code, metadata.code.length, !pe64 ? 4 : 16);
  566. map.AddMap (TextSegment.Resources, metadata.resources.length, 8);
  567. map.AddMap (TextSegment.Data, metadata.data.length, 4);
  568. if (metadata.data.length > 0)
  569. metadata.table_heap.FixupData (map.GetRVA (TextSegment.Data));
  570. map.AddMap (TextSegment.StrongNameSignature, GetStrongNameLength (), 4);
  571. BuildMetadataTextMap ();
  572. int debug_dir_len = 0;
  573. if (debug_header != null && debug_header.HasEntries) {
  574. var directories_len = debug_header.Entries.Length * ImageDebugDirectory.Size;
  575. var data_address = (int) map.GetNextRVA (TextSegment.BlobHeap) + directories_len;
  576. var data_len = 0;
  577. for (var i = 0; i < debug_header.Entries.Length; i++) {
  578. var entry = debug_header.Entries [i];
  579. var directory = entry.Directory;
  580. directory.AddressOfRawData = entry.Data.Length == 0 ? 0 : data_address;
  581. entry.Directory = directory;
  582. data_len += entry.Data.Length;
  583. data_address += data_len;
  584. }
  585. debug_dir_len = directories_len + data_len;
  586. }
  587. map.AddMap (TextSegment.DebugDirectory, debug_dir_len, 4);
  588. if (!has_reloc) {
  589. var start = map.GetNextRVA (TextSegment.DebugDirectory);
  590. map.AddMap (TextSegment.ImportDirectory, new Range (start, 0));
  591. map.AddMap (TextSegment.ImportHintNameTable, new Range (start, 0));
  592. map.AddMap (TextSegment.StartupStub, new Range (start, 0));
  593. return;
  594. }
  595. RVA import_dir_rva = map.GetNextRVA (TextSegment.DebugDirectory);
  596. RVA import_hnt_rva = import_dir_rva + 48u;
  597. import_hnt_rva = (import_hnt_rva + 15u) & ~15u;
  598. uint import_dir_len = (import_hnt_rva - import_dir_rva) + 27u;
  599. RVA startup_stub_rva = import_dir_rva + import_dir_len;
  600. startup_stub_rva = module.Architecture == TargetArchitecture.IA64
  601. ? (startup_stub_rva + 15u) & ~15u
  602. : 2 + ((startup_stub_rva + 3u) & ~3u);
  603. map.AddMap (TextSegment.ImportDirectory, new Range (import_dir_rva, import_dir_len));
  604. map.AddMap (TextSegment.ImportHintNameTable, new Range (import_hnt_rva, 0));
  605. map.AddMap (TextSegment.StartupStub, new Range (startup_stub_rva, GetStartupStubLength ()));
  606. }
  607. public void BuildMetadataTextMap ()
  608. {
  609. var map = text_map;
  610. map.AddMap (TextSegment.MetadataHeader, GetMetadataHeaderLength (module.RuntimeVersion));
  611. map.AddMap (TextSegment.TableHeap, metadata.table_heap.length, 4);
  612. map.AddMap (TextSegment.StringHeap, metadata.string_heap.length, 4);
  613. map.AddMap (TextSegment.UserStringHeap, metadata.user_string_heap.IsEmpty ? 0 : metadata.user_string_heap.length, 4);
  614. map.AddMap (TextSegment.GuidHeap, metadata.guid_heap.length, 4);
  615. map.AddMap (TextSegment.BlobHeap, metadata.blob_heap.IsEmpty ? 0 : metadata.blob_heap.length, 4);
  616. map.AddMap (TextSegment.PdbHeap, metadata.pdb_heap == null ? 0 : metadata.pdb_heap.length, 4);
  617. }
  618. uint GetStartupStubLength ()
  619. {
  620. switch (module.Architecture) {
  621. case TargetArchitecture.I386:
  622. return 6;
  623. default:
  624. throw new NotSupportedException ();
  625. }
  626. }
  627. int GetMetadataHeaderLength (string runtimeVersion)
  628. {
  629. return
  630. // MetadataHeader
  631. 20 + GetZeroTerminatedStringLength (runtimeVersion)
  632. // #~ header
  633. + 12
  634. // #Strings header
  635. + 20
  636. // #US header
  637. + (metadata.user_string_heap.IsEmpty ? 0 : 12)
  638. // #GUID header
  639. + 16
  640. // #Blob header
  641. + (metadata.blob_heap.IsEmpty ? 0 : 16)
  642. //
  643. + (metadata.pdb_heap == null ? 0 : 16);
  644. }
  645. int GetStrongNameLength ()
  646. {
  647. if (module.Assembly == null)
  648. return 0;
  649. var public_key = module.Assembly.Name.PublicKey;
  650. if (public_key.IsNullOrEmpty ())
  651. return 0;
  652. // in fx 2.0 the key may be from 384 to 16384 bits
  653. // so we must calculate the signature size based on
  654. // the size of the public key (minus the 32 byte header)
  655. int size = public_key.Length;
  656. if (size > 32)
  657. return size - 32;
  658. // note: size == 16 for the ECMA "key" which is replaced
  659. // by the runtime with a 1024 bits key (128 bytes)
  660. return 128; // default strongname signature size
  661. }
  662. public DataDirectory GetStrongNameSignatureDirectory ()
  663. {
  664. return text_map.GetDataDirectory (TextSegment.StrongNameSignature);
  665. }
  666. public uint GetHeaderSize ()
  667. {
  668. return pe_header_size + SizeOfOptionalHeader () + (sections * section_header_size);
  669. }
  670. void PatchWin32Resources (ByteBuffer resources)
  671. {
  672. PatchResourceDirectoryTable (resources);
  673. }
  674. void PatchResourceDirectoryTable (ByteBuffer resources)
  675. {
  676. resources.Advance (12);
  677. var entries = resources.ReadUInt16 () + resources.ReadUInt16 ();
  678. for (int i = 0; i < entries; i++)
  679. PatchResourceDirectoryEntry (resources);
  680. }
  681. void PatchResourceDirectoryEntry (ByteBuffer resources)
  682. {
  683. resources.Advance (4);
  684. var child = resources.ReadUInt32 ();
  685. var position = resources.position;
  686. resources.position = (int) child & 0x7fffffff;
  687. if ((child & 0x80000000) != 0)
  688. PatchResourceDirectoryTable (resources);
  689. else
  690. PatchResourceDataEntry (resources);
  691. resources.position = position;
  692. }
  693. void PatchResourceDataEntry (ByteBuffer resources)
  694. {
  695. var rva = resources.ReadUInt32 ();
  696. resources.position -= 4;
  697. resources.WriteUInt32 (rva - module.Image.Win32Resources.VirtualAddress + rsrc.VirtualAddress);
  698. }
  699. }
  700. }