ImageWriter.cs 23 KB

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