public ManagedPEBuilder( PEHeaderBuilder header, TypeSystemMetadataSerializer metadataSerializer, BlobBuilder ilStream, BlobBuilder mappedFieldData, BlobBuilder managedResourceData, Action <BlobBuilder, SectionLocation> nativeResourceSectionSerializer, // opt int strongNameSignatureSize, MethodDefinitionHandle entryPoint, string pdbPathOpt, // TODO: DebugTableBuilder ContentId nativePdbContentId, // TODO: DebugTableBuilder ContentId portablePdbContentId, // TODO: DebugTableBuilder CorFlags corFlags, Func <IEnumerable <Blob>, ContentId> deterministicIdProvider = null) : base(header, deterministicIdProvider) { _metadataSerializer = metadataSerializer; _ilStream = ilStream; _mappedFieldData = mappedFieldData; _managedResourceData = managedResourceData; _nativeResourceSectionSerializerOpt = nativeResourceSectionSerializer; _strongNameSignatureSize = strongNameSignatureSize; _entryPoint = entryPoint; _pdbPathOpt = pdbPathOpt; _nativePdbContentId = nativePdbContentId; _portablePdbContentId = portablePdbContentId; _corFlags = corFlags; _peDirectoriesBuilder = new PEDirectoriesBuilder(); }
public void ManagedPEBuilder_Errors() { var hdr = new PEHeaderBuilder(); var ms = new TypeSystemMetadataSerializer(new MetadataBuilder(), "v4.0.30319", false); var il = new BlobBuilder(); Assert.Throws <ArgumentNullException>(() => new ManagedPEBuilder(null, ms, il)); Assert.Throws <ArgumentNullException>(() => new ManagedPEBuilder(hdr, null, il)); Assert.Throws <ArgumentNullException>(() => new ManagedPEBuilder(hdr, ms, null)); Assert.Throws <ArgumentOutOfRangeException>(() => new ManagedPEBuilder(hdr, ms, il, strongNameSignatureSize: -1)); }
public ManagedPEBuilder( PEHeaderBuilder header, TypeSystemMetadataSerializer metadataSerializer, BlobBuilder ilStream, BlobBuilder mappedFieldData = null, BlobBuilder managedResources = null, ResourceSectionBuilder nativeResources = null, DebugDirectoryBuilder debugDirectoryBuilder = null, int strongNameSignatureSize = DefaultStrongNameSignatureSize, MethodDefinitionHandle entryPoint = default(MethodDefinitionHandle), CorFlags flags = CorFlags.ILOnly, Func <IEnumerable <Blob>, BlobContentId> deterministicIdProvider = null) : base(header, deterministicIdProvider) { if (header == null) { Throw.ArgumentNull(nameof(header)); } if (metadataSerializer == null) { Throw.ArgumentNull(nameof(metadataSerializer)); } if (ilStream == null) { Throw.ArgumentNull(nameof(ilStream)); } if (strongNameSignatureSize < 0) { Throw.ArgumentOutOfRange(nameof(strongNameSignatureSize)); } _metadataSerializer = metadataSerializer; _ilStream = ilStream; _mappedFieldDataOpt = mappedFieldData; _managedResourcesOpt = managedResources; _nativeResourcesOpt = nativeResources; _strongNameSignatureSize = strongNameSignatureSize; _entryPointOpt = entryPoint; _debugDirectoryBuilderOpt = debugDirectoryBuilder ?? CreateDefaultDebugDirectoryBuilder(); _corFlags = flags; _peDirectoriesBuilder = new PEDirectoriesBuilder(); }
public static void AddManagedSections( this PEBuilder peBuilder, PEDirectoriesBuilder peDirectoriesBuilder, TypeSystemMetadataSerializer metadataSerializer, BlobBuilder ilStream, BlobBuilder mappedFieldData, BlobBuilder managedResourceData, Action <BlobBuilder, PESectionLocation> nativeResourceSectionSerializer, // opt int strongNameSignatureSize, // TODO MethodDefinitionHandle entryPoint, string pdbPathOpt, // TODO ContentId nativePdbContentId, // TODO ContentId portablePdbContentId, // TODO CorFlags corFlags) { int entryPointAddress = 0; // .text peBuilder.AddSection(".text", SectionCharacteristics.MemRead | SectionCharacteristics.MemExecute | SectionCharacteristics.ContainsCode, location => { var sectionBuilder = new BlobBuilder(); var metadataBuilder = new BlobBuilder(); var metadataSizes = metadataSerializer.MetadataSizes; var textSection = new ManagedTextSection( metadataSizes.MetadataSize, ilStreamSize: ilStream.Count, mappedFieldDataSize: mappedFieldData.Count, resourceDataSize: managedResourceData.Count, strongNameSignatureSize: strongNameSignatureSize, imageCharacteristics: peBuilder.ImageCharacteristics, machine: peBuilder.Machine, pdbPathOpt: pdbPathOpt, isDeterministic: peBuilder.IsDeterministic); int methodBodyStreamRva = location.RelativeVirtualAddress + textSection.OffsetToILStream; int mappedFieldDataStreamRva = location.RelativeVirtualAddress + textSection.CalculateOffsetToMappedFieldDataStream(); metadataSerializer.SerializeMetadata(metadataBuilder, methodBodyStreamRva, mappedFieldDataStreamRva); BlobBuilder debugTableBuilderOpt; if (pdbPathOpt != null || peBuilder.IsDeterministic) { debugTableBuilderOpt = new BlobBuilder(); textSection.WriteDebugTable(debugTableBuilderOpt, location, nativePdbContentId, portablePdbContentId); } else { debugTableBuilderOpt = null; } entryPointAddress = textSection.GetEntryPointAddress(location.RelativeVirtualAddress); textSection.Serialize( sectionBuilder, location.RelativeVirtualAddress, entryPoint.IsNil ? 0 : MetadataTokens.GetToken(entryPoint), corFlags, peBuilder.ImageBase, metadataBuilder, ilStream, mappedFieldData, managedResourceData, debugTableBuilderOpt); peDirectoriesBuilder.AddressOfEntryPoint = entryPointAddress; peDirectoriesBuilder.DebugTable = textSection.GetDebugDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.ImportAddressTable = textSection.GetImportAddressTableDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.ImportTable = textSection.GetImportTableDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.CorHeaderTable = textSection.GetCorHeaderDirectoryEntry(location.RelativeVirtualAddress); return(sectionBuilder); }); // .rsrc if (nativeResourceSectionSerializer != null) { peBuilder.AddSection(".rsrc", SectionCharacteristics.MemRead | SectionCharacteristics.ContainsInitializedData, location => { var sectionBuilder = new BlobBuilder(); nativeResourceSectionSerializer(sectionBuilder, location); peDirectoriesBuilder.ResourceTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuilder.Count); return(sectionBuilder); }); } // .reloc if (peBuilder.Machine == Machine.I386 || peBuilder.Machine == 0) { peBuilder.AddSection(".reloc", SectionCharacteristics.MemRead | SectionCharacteristics.MemDiscardable | SectionCharacteristics.ContainsInitializedData, location => { var sectionBuilder = new BlobBuilder(); WriteRelocSection(sectionBuilder, peBuilder.Machine, entryPointAddress); peDirectoriesBuilder.BaseRelocationTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuilder.Count); return(sectionBuilder); }); } }
public static void AddManagedSections( this PEBuilder peBuilder, PEDirectoriesBuilder peDirectoriesBuilder, TypeSystemMetadataSerializer metadataSerializer, BlobBuilder ilStream, BlobBuilder mappedFieldData, BlobBuilder managedResourceData, Action<BlobBuilder, PESectionLocation> nativeResourceSectionSerializer, // opt int strongNameSignatureSize, // TODO MethodDefinitionHandle entryPoint, string pdbPathOpt, // TODO ContentId nativePdbContentId, // TODO ContentId portablePdbContentId, // TODO CorFlags corFlags) { int entryPointAddress = 0; // .text peBuilder.AddSection(".text", SectionCharacteristics.MemRead | SectionCharacteristics.MemExecute | SectionCharacteristics.ContainsCode, location => { var sectionBuilder = new BlobBuilder(); var metadataBuilder = new BlobBuilder(); var metadataSizes = metadataSerializer.MetadataSizes; var textSection = new ManagedTextSection( metadataSizes.MetadataSize, ilStreamSize: ilStream.Count, mappedFieldDataSize: mappedFieldData.Count, resourceDataSize: managedResourceData.Count, strongNameSignatureSize: strongNameSignatureSize, imageCharacteristics: peBuilder.ImageCharacteristics, machine: peBuilder.Machine, pdbPathOpt: pdbPathOpt, isDeterministic: peBuilder.IsDeterministic); int methodBodyStreamRva = location.RelativeVirtualAddress + textSection.OffsetToILStream; int mappedFieldDataStreamRva = location.RelativeVirtualAddress + textSection.CalculateOffsetToMappedFieldDataStream(); metadataSerializer.SerializeMetadata(metadataBuilder, methodBodyStreamRva, mappedFieldDataStreamRva); BlobBuilder debugTableBuilderOpt; if (pdbPathOpt != null || peBuilder.IsDeterministic) { debugTableBuilderOpt = new BlobBuilder(); textSection.WriteDebugTable(debugTableBuilderOpt, location, nativePdbContentId, portablePdbContentId); } else { debugTableBuilderOpt = null; } entryPointAddress = textSection.GetEntryPointAddress(location.RelativeVirtualAddress); textSection.Serialize( sectionBuilder, location.RelativeVirtualAddress, entryPoint.IsNil ? 0 : MetadataTokens.GetToken(entryPoint), corFlags, peBuilder.ImageBase, metadataBuilder, ilStream, mappedFieldData, managedResourceData, debugTableBuilderOpt); peDirectoriesBuilder.AddressOfEntryPoint = entryPointAddress; peDirectoriesBuilder.DebugTable = textSection.GetDebugDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.ImportAddressTable = textSection.GetImportAddressTableDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.ImportTable = textSection.GetImportTableDirectoryEntry(location.RelativeVirtualAddress); peDirectoriesBuilder.CorHeaderTable = textSection.GetCorHeaderDirectoryEntry(location.RelativeVirtualAddress); return sectionBuilder; }); // .rsrc if (nativeResourceSectionSerializer != null) { peBuilder.AddSection(".rsrc", SectionCharacteristics.MemRead | SectionCharacteristics.ContainsInitializedData, location => { var sectionBuilder = new BlobBuilder(); nativeResourceSectionSerializer(sectionBuilder, location); peDirectoriesBuilder.ResourceTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuilder.Count); return sectionBuilder; }); } // .reloc if (peBuilder.Machine == Machine.I386 || peBuilder.Machine == 0) { peBuilder.AddSection(".reloc", SectionCharacteristics.MemRead | SectionCharacteristics.MemDiscardable | SectionCharacteristics.ContainsInitializedData, location => { var sectionBuilder = new BlobBuilder(); WriteRelocSection(sectionBuilder, peBuilder.Machine, entryPointAddress); peDirectoriesBuilder.BaseRelocationTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuilder.Count); return sectionBuilder; }); } }