private ImageWriter(ModuleDefinition module, MetadataBuilder metadata, Stream stream) : base(stream) { this.module = module; this.metadata = metadata; this.pe64 = (module.Architecture == TargetArchitecture.AMD64 || module.Architecture == TargetArchitecture.IA64); this.has_reloc = (module.Architecture == TargetArchitecture.I386); this.GetDebugHeader(); this.GetWin32Resources(); this.text_map = this.BuildTextMap(); this.sections = (ushort)(has_reloc ? 2 : 1); // text + reloc? this.time_stamp = (uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; }
private TextMap BuildTextMap() { TextMap textMap = this.metadata.text_map; textMap.AddMap(TextSegment.Code, this.metadata.code.length, (!this.pe64) ? 4 : 16); textMap.AddMap(TextSegment.Resources, this.metadata.resources.length, 8); textMap.AddMap(TextSegment.Data, this.metadata.data.length, 4); if (this.metadata.data.length > 0) { this.metadata.table_heap.FixupData(textMap.GetRVA(TextSegment.Data)); } textMap.AddMap(TextSegment.StrongNameSignature, this.GetStrongNameLength(), 4); textMap.AddMap(TextSegment.MetadataHeader, this.GetMetadataHeaderLength(this.module.RuntimeVersion)); textMap.AddMap(TextSegment.TableHeap, this.metadata.table_heap.length, 4); textMap.AddMap(TextSegment.StringHeap, this.metadata.string_heap.length, 4); textMap.AddMap(TextSegment.UserStringHeap, this.metadata.user_string_heap.IsEmpty ? 0 : this.metadata.user_string_heap.length, 4); textMap.AddMap(TextSegment.GuidHeap, 16); textMap.AddMap(TextSegment.BlobHeap, this.metadata.blob_heap.IsEmpty ? 0 : this.metadata.blob_heap.length, 4); int length = 0; if (!this.debug_data.IsNullOrEmpty <byte>()) { this.debug_directory.AddressOfRawData = (int)(textMap.GetNextRVA(TextSegment.BlobHeap) + 28u); length = this.debug_data.Length + 28; } textMap.AddMap(TextSegment.DebugDirectory, length, 4); if (!this.has_reloc) { uint nextRVA = textMap.GetNextRVA(TextSegment.DebugDirectory); textMap.AddMap(TextSegment.ImportDirectory, new Range(nextRVA, 0u)); textMap.AddMap(TextSegment.ImportHintNameTable, new Range(nextRVA, 0u)); textMap.AddMap(TextSegment.StartupStub, new Range(nextRVA, 0u)); return(textMap); } uint nextRVA2 = textMap.GetNextRVA(TextSegment.DebugDirectory); uint num = nextRVA2 + 48u; num = (num + 15u & 4294967280u); uint num2 = num - nextRVA2 + 27u; uint num3 = nextRVA2 + num2; num3 = ((this.module.Architecture == TargetArchitecture.IA64) ? (num3 + 15u & 4294967280u) : (2u + (num3 + 3u & 4294967292u))); textMap.AddMap(TextSegment.ImportDirectory, new Range(nextRVA2, num2)); textMap.AddMap(TextSegment.ImportHintNameTable, new Range(num, 0u)); textMap.AddMap(TextSegment.StartupStub, new Range(num3, this.GetStartupStubLength())); return(textMap); }