private byte[] GetRuntimeMain() { if (this.module.Kind != ModuleKind.Dll && this.module.Kind != ModuleKind.NetModule) { return(ImageWriter.GetSimpleString("_CorExeMain")); } return(ImageWriter.GetSimpleString("_CorDllMain")); }
private Section CreateSection(string name, uint size, Section previous) { return(new Section { Name = name, VirtualAddress = (previous != null) ? (previous.VirtualAddress + ImageWriter.Align(previous.VirtualSize, 8192u)) : 8192u, VirtualSize = size, PointerToRawData = (previous != null) ? (previous.PointerToRawData + previous.SizeOfRawData) : ImageWriter.Align(this.GetHeaderSize(), 512u), SizeOfRawData = ImageWriter.Align(size, 512u) }); }
private void WriteStreamHeader(ref uint offset, TextSegment heap, string name) { uint length = (uint)this.text_map.GetLength(heap); if (length == 0u) { return; } base.WriteUInt32(offset); base.WriteUInt32(length); base.WriteBytes(ImageWriter.GetZeroTerminatedString(name)); offset += length; }
private void WriteImportDirectory() { base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportDirectory) + 40u); base.WriteUInt32(0u); base.WriteUInt32(0u); base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportHintNameTable) + 14u); base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportAddressTable)); base.Advance(20); base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportHintNameTable)); this.MoveToRVA(TextSegment.ImportHintNameTable); base.WriteUInt16(0); base.WriteBytes(this.GetRuntimeMain()); base.WriteByte(0); base.WriteBytes(ImageWriter.GetSimpleString("mscoree.dll")); base.WriteUInt16(0); }
private void WriteMetadataHeader() { base.WriteUInt32(1112167234u); base.WriteUInt16(1); base.WriteUInt16(1); base.WriteUInt32(0u); byte[] zeroTerminatedString = ImageWriter.GetZeroTerminatedString(this.module.runtime_version); base.WriteUInt32((uint)zeroTerminatedString.Length); base.WriteBytes(zeroTerminatedString); base.WriteUInt16(0); base.WriteUInt16(this.GetStreamCount()); uint num = this.text_map.GetRVA(TextSegment.TableHeap) - this.text_map.GetRVA(TextSegment.MetadataHeader); this.WriteStreamHeader(ref num, TextSegment.TableHeap, "#~"); this.WriteStreamHeader(ref num, TextSegment.StringHeap, "#Strings"); this.WriteStreamHeader(ref num, TextSegment.UserStringHeap, "#US"); this.WriteStreamHeader(ref num, TextSegment.GuidHeap, "#GUID"); this.WriteStreamHeader(ref num, TextSegment.BlobHeap, "#Blob"); }
private int GetMetadataHeaderLength(string runtimeVersion) { return(20 + ImageWriter.GetZeroTerminatedStringLength(runtimeVersion) + 12 + 20 + (this.metadata.user_string_heap.IsEmpty ? 0 : 12) + 16 + (this.metadata.blob_heap.IsEmpty ? 0 : 16)); }
private static byte[] GetSimpleString(string @string) { return(ImageWriter.GetString(@string, @string.Length)); }
private static byte[] GetZeroTerminatedString(string @string) { return(ImageWriter.GetString(@string, ImageWriter.GetZeroTerminatedStringLength(@string))); }
private void WriteOptionalHeaders() { WriteUInt16((ushort)(!pe64 ? 0x10b : 0x20b)); // Magic base.WriteByte(8); base.WriteByte(0); base.WriteUInt32(this.text.SizeOfRawData); base.WriteUInt32(((this.reloc != null) ? this.reloc.SizeOfRawData : 0u) + ((this.rsrc != null) ? this.rsrc.SizeOfRawData : 0u)); base.WriteUInt32(0u); Range range = this.text_map.GetRange(TextSegment.StartupStub); base.WriteUInt32((range.Length > 0u) ? range.Start : 0u); base.WriteUInt32(8192u); if (!this.pe64) { base.WriteUInt32(0u); base.WriteUInt32(4194304u); } else { base.WriteUInt64(4194304uL); } base.WriteUInt32(8192u); base.WriteUInt32(512u); base.WriteUInt16(4); base.WriteUInt16(0); base.WriteUInt16(0); base.WriteUInt16(0); base.WriteUInt16(4); base.WriteUInt16(0); base.WriteUInt32(0u); Section section = this.LastSection(); base.WriteUInt32(section.VirtualAddress + ImageWriter.Align(section.VirtualSize, 8192u)); base.WriteUInt32(this.text.PointerToRawData); base.WriteUInt32(0u); base.WriteUInt16(this.GetSubSystem()); base.WriteUInt16((ushort)this.module.Characteristics); if (!this.pe64) { base.WriteUInt32(1048576u); base.WriteUInt32(4096u); base.WriteUInt32(1048576u); base.WriteUInt32(4096u); } else { base.WriteUInt64(1048576uL); base.WriteUInt64(4096uL); base.WriteUInt64(1048576uL); base.WriteUInt64(4096uL); } base.WriteUInt32(0u); base.WriteUInt32(16u); this.WriteZeroDataDirectory(); base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.ImportDirectory)); if (this.rsrc != null) { base.WriteUInt32(this.rsrc.VirtualAddress); base.WriteUInt32(this.rsrc.VirtualSize); } else { this.WriteZeroDataDirectory(); } this.WriteZeroDataDirectory(); this.WriteZeroDataDirectory(); base.WriteUInt32((this.reloc != null) ? this.reloc.VirtualAddress : 0u); base.WriteUInt32((this.reloc != null) ? this.reloc.VirtualSize : 0u); if (this.text_map.GetLength(TextSegment.DebugDirectory) > 0) { base.WriteUInt32(this.text_map.GetRVA(TextSegment.DebugDirectory)); base.WriteUInt32(28u); } else { this.WriteZeroDataDirectory(); } this.WriteZeroDataDirectory(); this.WriteZeroDataDirectory(); this.WriteZeroDataDirectory(); this.WriteZeroDataDirectory(); this.WriteZeroDataDirectory(); base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.ImportAddressTable)); this.WriteZeroDataDirectory(); base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.CLIHeader)); this.WriteZeroDataDirectory(); }