Table of Contents

Class OptionalHeader

Namespace
AsmResolver.PE.File
Assembly
AsmResolver.PE.File.dll

Represents a 32-bit or 64-bit optional header in a portable executable (PE) file.

public class OptionalHeader : SegmentBase, ISegment, IOffsetProvider, IWritable
Inheritance
OptionalHeader
Implements
Inherited Members

Fields

DefaultNumberOfRvasAndSizes

Indicates the default number of data directory entries in an optional header.

public const int DefaultNumberOfRvasAndSizes = 16

Field Value

int

OptionalHeader32SizeExcludingDataDirectories

Indicates the static size of an optional header in a 32-bit portable executable file, excluding the data directory entries.

public const uint OptionalHeader32SizeExcludingDataDirectories = 96

Field Value

uint

OptionalHeader64SizeExcludingDataDirectories

Indicates the static size of an optional header in a 64-bit portable executable file, excluding the data directory entries.

public const uint OptionalHeader64SizeExcludingDataDirectories = 112

Field Value

uint

OptionalHeaderSizeOfImageFieldOffset

Indicates the offset of the SizeOfImage field in the optional header.

public const uint OptionalHeaderSizeOfImageFieldOffset = 56

Field Value

uint

Properties

AddressOfEntryPoint

Gets or sets the relative virtual address to the entry point of the portable executable (PE) file.

public uint AddressOfEntryPoint { get; set; }

Property Value

uint

BaseOfCode

Gets or sets the relative virtual address (RVA) to the beginning of the code section, when loaded into memory.

public uint BaseOfCode { get; set; }

Property Value

uint

BaseOfData

Gets or sets the relative virtual address to the begin of the data section, when loaded into memory.

public uint BaseOfData { get; set; }

Property Value

uint

CheckSum

Gets or sets the checksum of the portable executable (PE) file.

public uint CheckSum { get; set; }

Property Value

uint

DataDirectories

Gets the data directory headers defined in the optional header.

public IList<DataDirectory> DataDirectories { get; }

Property Value

IList<DataDirectory>

DllCharacteristics

Gets or sets the dynamic linked library characteristics of the portable executable (PE) file.

public DllCharacteristics DllCharacteristics { get; set; }

Property Value

DllCharacteristics

FileAlignment

Gets or sets the alignment of the raw data of sections in the image file. Must be a power of 2 between 512 and 64,000.

public uint FileAlignment { get; set; }

Property Value

uint

ImageBase

Gets or sets the preferred address of the first byte of the image when loaded into memory. Must be a multiple of 64,000.

public ulong ImageBase { get; set; }

Property Value

ulong

LoaderFlags

Obsolete.

public uint LoaderFlags { get; set; }

Property Value

uint

Magic

Gets or sets the magic optional header signature, determining whether the image is a PE32 (32-bit) or a PE32+ (64-bit) image.

public OptionalHeaderMagic Magic { get; set; }

Property Value

OptionalHeaderMagic

MajorImageVersion

Gets or sets the major image version.

public ushort MajorImageVersion { get; set; }

Property Value

ushort

MajorLinkerVersion

Gets or sets the major linker version used to link the portable executable (PE) file.

public byte MajorLinkerVersion { get; set; }

Property Value

byte

MajorOperatingSystemVersion

Gets or sets the minimum major version of the operating system required to run the portable executable (PE) file.

public ushort MajorOperatingSystemVersion { get; set; }

Property Value

ushort

MajorSubsystemVersion

Gets or sets the major version of the subsystem.

public ushort MajorSubsystemVersion { get; set; }

Property Value

ushort

MinorImageVersion

Gets or sets the minor image version.

public ushort MinorImageVersion { get; set; }

Property Value

ushort

MinorLinkerVersion

Gets or sets the minor linker version used to link the portable executable (PE) file.

public byte MinorLinkerVersion { get; set; }

Property Value

byte

MinorOperatingSystemVersion

Gets or sets the minimum minor version of the operating system required to run the portable executable (PE) file.

public ushort MinorOperatingSystemVersion { get; set; }

Property Value

ushort

MinorSubsystemVersion

Gets or sets the minor version of the subsystem.

public ushort MinorSubsystemVersion { get; set; }

Property Value

ushort

NumberOfRvaAndSizes

Gets or sets the number of data directory headers defined in the optional header.

public uint NumberOfRvaAndSizes { get; set; }

Property Value

uint

SectionAlignment

Gets or sets the alignment of the sections when loaded into memory. Must be greater or equal to FileAlignment. Default is the page size for the architecture.

public uint SectionAlignment { get; set; }

Property Value

uint

SizeOfCode

Gets or sets the total amount of bytes the code sections consist of.

public uint SizeOfCode { get; set; }

Property Value

uint

SizeOfHeaders

Gets or sets the size of the headers of the portable executable (PE) file, including the DOS-, PE- and section headers, rounded to FileAlignment.

public uint SizeOfHeaders { get; set; }

Property Value

uint

SizeOfHeapCommit

Gets or sets the size of the heap to commit.

public ulong SizeOfHeapCommit { get; set; }

Property Value

ulong

SizeOfHeapReserve

Gets or sets the size of the heap to reserve.

public ulong SizeOfHeapReserve { get; set; }

Property Value

ulong

SizeOfImage

Gets or sets the size in bytes of the portable executable (PE) file, including all headers. Must be a multiple of SectionAlignment.

public uint SizeOfImage { get; set; }

Property Value

uint

SizeOfInitializedData

Gets or sets the total amount of bytes the initialized data sections consist of.

public uint SizeOfInitializedData { get; set; }

Property Value

uint

SizeOfStackCommit

Gets or sets the size of the stack to commit.

public ulong SizeOfStackCommit { get; set; }

Property Value

ulong

SizeOfStackReserve

Gets or sets the size of the stack to reserve.

public ulong SizeOfStackReserve { get; set; }

Property Value

ulong

SizeOfUninitializedData

Gets or sets the total amount of bytes the uninitialized data sections consist of.

public uint SizeOfUninitializedData { get; set; }

Property Value

uint

SubSystem

Gets or sets the subsystem to use when running the portable executable (PE) file.

public SubSystem SubSystem { get; set; }

Property Value

SubSystem

Win32VersionValue

Reserved, should be zero.

public uint Win32VersionValue { get; set; }

Property Value

uint

Methods

EnsureDataDirectoryCount(int)

Ensures the provided number of data directories are present in DataDirectories.

public void EnsureDataDirectoryCount(int count)

Parameters

count int

The number of data directories.

FromReader(ref BinaryStreamReader, bool)

Reads an optional header from the provided input stream.

public static OptionalHeader FromReader(ref BinaryStreamReader reader, bool ignoreNumberOfRvasAndSizes = true)

Parameters

reader BinaryStreamReader

The input stream.

ignoreNumberOfRvasAndSizes bool

Indicates whether the number of data directories should be ignored and DefaultNumberOfRvasAndSizes should be used instead.

Returns

OptionalHeader

The optional header.

Exceptions

BadImageFormatException

Occurs when the input stream is malformed.

GetDataDirectory(DataDirectoryIndex)

Gets a data directory by its index.

public DataDirectory GetDataDirectory(DataDirectoryIndex index)

Parameters

index DataDirectoryIndex

The index.

Returns

DataDirectory

The data directory entry.

GetPhysicalSize()

Computes the number of bytes that the structure contains.

public override uint GetPhysicalSize()

Returns

uint

The number of bytes.

SetDataDirectory(DataDirectoryIndex, ISegment?)

Maps a segment to a data directory.

public void SetDataDirectory(DataDirectoryIndex index, ISegment? contents)

Parameters

index DataDirectoryIndex

The index.

contents ISegment

The contents of the data directory, or null to indicate an absence of the directory.

SetDataDirectory(DataDirectoryIndex, DataDirectory)

Sets a data directory by its index.

public void SetDataDirectory(DataDirectoryIndex index, DataDirectory directory)

Parameters

index DataDirectoryIndex

The index.

directory DataDirectory

The new data directory entry.

Write(BinaryStreamWriter)

Serializes the structure to an output stream.

public override void Write(BinaryStreamWriter writer)

Parameters

writer BinaryStreamWriter

The output stream to write the data to.