Class ManagedPEFileBuilder
- Namespace
- AsmResolver.PE.Builder
- Assembly
- AsmResolver.PE.dll
Provides a mechanism for constructing PE files from images containing .NET metadata.
public class ManagedPEFileBuilder : PEFileBuilder, IPEFileBuilder
- Inheritance
-
ManagedPEFileBuilder
- Implements
- Inherited Members
Remarks
This PE builder is focused on .NET images only, and assumes that every input PE image is either a fully .NET image with only architecture independent code (CIL), or contains native methods that are fully well-defined, i.e. they are represented by a single segment. Any method definition in the metadata table that references a native method body of which the size is not explicitly defined will cause an exception. This class also might replace rows in the method and/or field RVA metadata tables with new ones containing the updated references to method bodies and/or field data. All remaining metadata in the tables stream and in the other metadata streams is written as-is without any change or verification.
This class might modify the final imports directory (exposed by the Imports property),
as well as the base relocations directory (exposed by the Relocations property). In
particular, it might add or remove the entry to mscoree.dll!_CorExeMain or mscoree.dll!_CorDllMain,
depending on the machine type specified by the MachineType property.
This class builds up at most four PE sections; .text, .sdata, .rsrc and .reloc,
similar to what a normal .NET language compiler would emit. Almost everything is put into the .text section,
including the import and debug directories. The win32 resources are put into .rsrc section, and this
section will only be added if there is at least one entry in the root resource directory of the
Resources property. Similarly, the .sdata section is only added if at least
one unmanaged export is added to the PE image. Finally, the .reloc section is only added if at least
one base relocation was put into the directory, or when the CLR bootstrapper requires one.
Constructors
ManagedPEFileBuilder()
Creates a new managed PE file builder with default settings.
public ManagedPEFileBuilder()
ManagedPEFileBuilder(IErrorListener)
Creates a new managed PE file builder with the provided error listener.
public ManagedPEFileBuilder(IErrorListener errorListener)
Parameters
errorListenerIErrorListener
Properties
ErrorListener
Gets or sets the object responsible for recording diagnostic information during the building process.
public IErrorListener ErrorListener { get; set; }
Property Value
Methods
AddFieldRvaSegments(PEFileBuilderContext, TablesStream)
Adds all segments referenced by the FieldRva metadata table to the PE file.
protected virtual void AddFieldRvaSegments(PEFileBuilderContext context, TablesStream tablesStream)
Parameters
contextPEFileBuilderContextThe context for the new PE file.
tablesStreamTablesStreamThe tables stream to get the Field RVAs from.
AddMethodBodySegments(PEFileBuilderContext, TablesStream)
Adds all method bodies referenced by the method metadata table to the PE file.
protected virtual void AddMethodBodySegments(PEFileBuilderContext context, TablesStream tablesStream)
Parameters
contextPEFileBuilderContextThe context for the new PE file.
tablesStreamTablesStreamThe tables stream to get the method body RVAs from.
AssignDataDirectories(PEFileBuilderContext, PEFile)
Creates the data directory headers stored in the optional header of the PE file.
protected override void AssignDataDirectories(PEFileBuilderContext context, PEFile outputFile)
Parameters
contextPEFileBuilderContextThe object containing the intermediate values used during the PE file construction.
outputFilePEFileThe (incomplete) PE file that contains the sections.
CreateDataDirectoryBuffers(PEFileBuilderContext)
Populates all buffers with data from the input image.
protected override void CreateDataDirectoryBuffers(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe builder context.
CreateImportDirectory(PEFileBuilderContext)
Populates the import directory buffer.
protected override void CreateImportDirectory(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe builder context.
CreateRelocSection(PEFileBuilderContext)
Creates the base relocations section (.reloc) of the new .NET PE file.
protected virtual PESection CreateRelocSection(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe working space of the builder.
Returns
- PESection
The base relocations section.
CreateRelocationsDirectory(PEFileBuilderContext)
Populates the base relocations directory buffer.
protected override void CreateRelocationsDirectory(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe builder context.
CreateRsrcSection(PEFileBuilderContext)
Creates the win32 resources section (.rsrc) of the new .NET PE file.
protected virtual PESection CreateRsrcSection(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe working space of the builder.
Returns
- PESection
The resources section.
CreateSDataSection(PEFileBuilderContext)
Creates the .sdata section containing the exports and vtables directory of the new .NET PE file.
protected virtual PESection CreateSDataSection(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe working space of the builder.
Returns
- PESection
The section.
CreateSections(PEFileBuilderContext)
Creates the sections of the PE image.
protected override IEnumerable<PESection> CreateSections(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe object containing the intermediate values used during the PE file construction.
Returns
- IEnumerable<PESection>
The sections.
CreateTextSection(PEFileBuilderContext)
Builds up the main text section (.text) of the new .NET PE file.
protected virtual PESection CreateTextSection(PEFileBuilderContext context)
Parameters
contextPEFileBuilderContextThe working space of the builder.
Returns
- PESection
The .text section.
GetEntryPointAddress(PEFileBuilderContext, PEFile)
Gets the relative virtual address (RVA) to the entry point of the PE file.
protected override uint GetEntryPointAddress(PEFileBuilderContext context, PEFile outputFile)
Parameters
contextPEFileBuilderContextThe object containing the intermediate values used during the PE file construction.
outputFilePEFileThe (incomplete) PE file.
Returns
- uint
The relative virtual address to the entry point.