RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Microsoft::PEFormat Class Reference

Microsoft PE .EXE portable executable file format. More...

#include <peexe.h>

Inheritance diagram for Microsoft::PEFormat:
COFF::COFFFormat Linker::InputFormat Linker::SegmentManager Linker::Format Linker::OutputFormat Linker::Contents Linker::Format Linker::Contents

Classes

class  BaseRelocation
 Represents a base relocation, as stored in the image. More...
 
class  BaseRelocationBlock
 A block of base relocations, corresponding to a single page in the image. More...
 
class  BaseRelocationsSection
 Represents a .reloc section for the base relocations for the binary. More...
 
class  ExportedEntry
 Represents a single exported entry in the file. More...
 
class  ExportsSection
 Represents an .edata section for the exported entries in the binary. More...
 
class  ImportedLibrary
 A collection of the imported names for a specific dynamic linking library. More...
 
class  ImportsSection
 Represents an .idata section for imported DLLs in the binary. More...
 
class  MemoryPortionImage
 
class  PEOptionalHeader
 The PE optional header as specified by Microsoft. More...
 
class  PEOptionCollector
 
struct  PESections
 Wrapper around COFFFormat::sections to return PEFormat::Section pointers instead of COFFFormat::Section pointers. More...
 
class  Resource
 Represents a resource inside the image. More...
 
class  ResourceDirectory
 
class  ResourcesSection
 Represents an .rsrc resource section in the binary. More...
 
class  RVADisplay
 Represents a field that displays a relative virtual address and file offset pair. More...
 
class  Section
 A section, with the PE extensions. More...
 
struct  version_type
 Represents a version entry with major and minor versions. More...
 

Public Types

enum  compatibility_type {
  CompatibleNone , CompatibleWatcom , CompatibleMicrosoft , CompatibleBorland ,
  CompatibleGNU
}
 
enum  target_type {
  TargetWin9x = 1 , TargetWinNT , TargetWinCE , TargetMacintosh ,
  TargetWin32s , TargetTNT , TargetEFI , TargetDotNET ,
  TargetXbox
}
 Represents settings and assumptions about the target. More...
 
enum  output_type { OUTPUT_EXE , OUTPUT_DLL , OUTPUT_SYS }
 Represents the target image type. More...
 
enum  {
  FLAG_NO_RELOCATIONS = 0x0001 , FLAG_EXECUTABLE = 0x0002 , FLAG_NO_LINE_NUMBERS = 0x0004 , FLAG_NO_SYMBOLS = 0x0008 ,
  FLAG_TRIM_WORKING_SET = 0x0010 , FLAG_LARGE_ADDRESS = 0x0020 , FLAG_LITTLE_ENDIAN = 0x0080 , FLAG_32BIT = 0x0100 ,
  FLAG_NO_DEBUG = 0x0200 , FLAG_ON_REMOVABLE = 0x0400 , FLAG_ON_NETWORK = 0x0800 , FLAG_SYSTEM = 0x1000 ,
  FLAG_LIBRARY = 0x2000 , FLAG_UNIPROCESSOR_ONLY = 0x4000 , FLAG_BIG_ENDIAN = 0x8000
}
 COFF file header flags, most of these are obsolete, we only use them as precombined flag sets. More...
 
- Public Types inherited from COFF::COFFFormat
enum  cpu {
  CPU_UNKNOWN = 0 , CPU_I386 = 0x014C , CPU_M68K = 0x0150 , CPU_W65 = 0x6500 ,
  CPU_Z80 = 0x805A , CPU_Z8K = 0x8000 , CPU_I86 = 0x0148 , CPU_I286 = 0x014A ,
  CPU_NS32K = 0x0154 , CPU_I370 = 0x0158 , CPU_MIPS = 0x0160 , CPU_M88K = 0x016D ,
  CPU_WE32K = 0x0170 , CPU_VAX = 0x0178 , CPU_AM29K = 0x017A , CPU_ALPHA = 0x0183 ,
  CPU_PPC = 0x01DF , CPU_PPC64 = 0x01F7 , CPU_SHARC = 0x521C , CPU_C5400 = 0x0098 ,
  CPU_C6000 = 0x0099 , CPU_C5500 = 0x009C , CPU_MSP430 = 0x00A0 , CPU_I860 = 0x014D ,
  CPU_SH = 0x01A2 , CPU_ARM = 0x01C0 , CPU_AM33 = 0x01D3 , CPU_IA64 = 0x0200 ,
  CPU_HPPA = 0x0290 , CPU_EFI = 0x0EBC , CPU_RISCV32 = 0x5032 , CPU_RISCV64 = 0x5064 ,
  CPU_RISCV128 = 0x5128 , CPU_AMD64 = 0x8664 , CPU_M32R = 0x9041 , CPU_ARM64 = 0xAA64
}
 Represents the first 16-bit word of a COFF file. More...
 
enum  COFFVariantType {
  COFF = 1 , ECOFF = 2 , XCOFF32 = 3 , XCOFF64 = 4 ,
  PECOFF = 5 , TICOFF = 6 , TICOFF1 = 7
}
 Variants of the COFF file format. More...
 
enum  relocation_format_type {
  COFF_10 , COFF_14 , COFF_16 , ECOFF_8 ,
  ECOFF_16 , XCOFF_10 , XCOFF_14 , TICOFF_10 ,
  TICOFF_12
}
 
enum  format_type {
  GENERIC , DJGPP , CDOS68K , CDOS386 ,
  WINDOWS
}
 Represents the type of target system, which will determine the CPU type and several other fields. More...
 
enum  {
  FLAG_NO_RELOCATIONS = 0x0001 , FLAG_EXECUTABLE = 0x0002 , FLAG_NO_LINE_NUMBERS = 0x0004 , FLAG_NO_SYMBOLS = 0x0008 ,
  FLAG_PDP11_ENDIAN = 0x0080 , FLAG_32BIT_LITTLE_ENDIAN = 0x0100 , FLAG_32BIT_BIG_ENDIAN = 0x0200 , OMAGIC = 0x0107 ,
  NMAGIC = 0x0108 , ZMAGIC = 0x010B , MAGIC_FLEXOS386 = 0x01C0
}
 COFF file header flags, most of these are obsolete, we only use them as precombined flag sets. More...
 

Public Member Functions

PEOptionalHeaderGetOptionalHeader ()
 Retrieves the optional header.
 
const PEOptionalHeaderGetOptionalHeader () const
 Retrieves the optional header.
 
bool Is64Bit () const
 Whether the file is in the PE32+ (64-bit) format.
 
uint32_t AddressToRVA (offset_t address) const
 Converts a virtual address into an image base relative virtual address.
 
offset_t RVAToAddress (uint32_t rva, bool suppress_on_zero=false) const
 Converts an image base relative virtual address into a virtual address.
 
offset_t RVAToFileOffset (uint32_t rva) const
 Converts an image base relative virtual address into a file offset.
 
const PESections Sections () const
 Access the sections as PEFormat::Section.
 
PESections Sections ()
 Access the sections as PEFormat::Section.
 
size_t MapRVAToSectionData (uint32_t rva, size_t bytes, std::shared_ptr< Section > &found_section, size_t &section_offset) const
 Attempts to find the section:offset pair that encompasses this memory range.
 
size_t ReadData (size_t bytes, uint32_t rva, void *buffer) const
 Loads a sequence of bytes from the filled section data.
 
uint64_t ReadUnsigned (size_t bytes, uint32_t rva, ::EndianType endiantype) const
 Loads an unsigned word from the filled section data.
 
uint64_t ReadSigned (size_t bytes, uint32_t rva, ::EndianType endiantype) const
 Loads a signed word from the filled section data.
 
std::string ReadASCII (uint32_t rva, char terminator, size_t maximum=size_t(-1)) const
 Loads a sequence of bytes terminated by a specific character from the filled section data.
 
std::string ReadData (uint32_t rva, size_t count) const
 Loads a sequence of bytes.
 
void AddBaseRelocation (uint32_t rva, BaseRelocation::relocation_type type, uint16_t low_ref=0)
 Adds a base relocation at the specified relative virtual address.
 
void ReadFile (Linker::Reader &rd) override
 Loads file into memory.
 
::EndianType GetMachineEndianType () const
 
void CalculateValues () override
 Intermediate step between processing module and generating output file to set up headers and management sections It is expected that after a module is processed, additional steps are required to evaluate the final values of the fields.
 
offset_t WriteFile (Linker::Writer &wr) const override
 Stores data in memory to file.
 
void Dump (Dumper::Dumper &dump) const override
 Display file contents in a nice manner.
 
 PEFormat (target_type target=TargetWinNT, PEOptionalHeader::SubsystemType subsystem=PEOptionalHeader::WindowsGUI, output_type output=OUTPUT_EXE)
 
std::shared_ptr< PEFormatSimulateLinker (compatibility_type compatibility)
 
bool FormatSupportsLibraries () const override
 Whether the format supports libraries.
 
bool FormatSupportsResources () const override
 Whether the format supports resources.
 
unsigned FormatAdditionalSectionFlags (std::string section_name) const override
 
std::shared_ptr< Resource > & AddResource (std::shared_ptr< Resource > &resource)
 
ImportedLibraryFetchImportLibrary (std::string library_name, bool create_if_not_present=false)
 
void AddImportByName (std::string library_name, std::string entry_name, uint16_t hint)
 
void AddImportByOrdinal (std::string library_name, uint16_t ordinal)
 
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames () override
 Returns a list of the parameters used in the linker scripts, used for documentation.
 
std::shared_ptr< Linker::OptionCollectorGetOptions () override
 Returns object containing a sequence of option fields provided with the -S command line flag.
 
void SetOptions (std::map< std::string, std::string > &options) override
 Passes command line parameters as settings over to format object.
 
void AllocateSymbols (Linker::Module &module) const override
 Instructs the module to allocate any unallocated local symbols.
 
void OnCallDirective (std::string identifier) override
 Callback function for the CALL directive.
 
void OnNewSegment (std::shared_ptr< Linker::Segment > segment) override
 Callback function when allocating a new segment When the linker script runs, it creates segments consecutively. Overriding this method permits the output format to handle the allocated segment.
 
std::unique_ptr< Script::ListGetScript (Linker::Module &module)
 
void Link (Linker::Module &module)
 
offset_t GenerateResourceSection (Linker::Module &module, offset_t image_end)
 Creates a resource section and attaches it to the end of the file.
 
offset_t GenerateImportSection (Linker::Module &module, offset_t image_end)
 Creates an import section and attaches it to the end of the file.
 
offset_t GenerateExportSection (Linker::Module &module, offset_t image_end)
 Creates an export section and attaches it to the end of the file.
 
offset_t GenerateBaseRelocationSection (Linker::Module &module, offset_t image_end)
 Creates a base relocation section and attaches it to the end of the file.
 
void ProcessRelocations (Linker::Module &module)
 Processes relocations present in the module and stores them as base relocations and imports.
 
void FixupImportThunk (Linker::Module &module, offset_t offset, offset_t address)
 Adjust addresses in the text section where the indirect jumps appear.
 
offset_t GetMemoryImageEnd () const
 
void ProcessModule (Linker::Module &module) override
 Processes the module object and initializes format fields.
 
void GenerateFile (std::string filename, Linker::Module &module) override
 The main function that handles processing, calculating and generating the final image.
 
std::string GetDefaultExtension (Linker::Module &module, std::string filename) const override
 Appends a default extension to the filename.
 
std::shared_ptr< RVADisplayMakeRVADisplay (unsigned width=8) const
 
- Public Member Functions inherited from COFF::COFFFormat
::EndianType GetEndianType () const
 Retrieves the natural byte order for the architecture.
 
constexpr size_t GetRelocationEntrySize () const
 
void Clear () override
 Resets all fields to their default values, deallocate memory.
 
void AssignMagicValue (uint16_t value, ::EndianType as_endian_type)
 Loads the specified 16-bit integer in the specified byte order into the COFF header signature.
 
void AssignMagicValue (uint16_t value)
 Loads the specified 16-bit integer in the currently set byte order into the COFF header signature.
 
void AssignMagicValue ()
 Loads the currently set CPU value in the currently set byte order into the COFF header signature.
 
bool DetectCpuType (::EndianType expected)
 
void DetectCpuType ()
 
offset_t ImageSize () const override
 Retrieves size of stored data.
 
void SetupOptions (std::shared_ptr< Linker::OutputFormat > format) override
 Initializes the reader for linking purposes.
 
bool FormatRequiresDataStreamFix () const override
 Whether the generated file might contain bugs that require fixing.
 
void GenerateModule (Linker::Module &module) const override
 Loads the information into a module object, a convenience method when there is a single module generated.
 
 COFFFormat (format_type type=GENERIC, COFFVariantType coff_variant=COFF, EndianType endiantype=::UndefinedEndian)
 
void CreateDefaultSegments ()
 
std::unique_ptr< Script::ListGetScript (Linker::Module &module)
 
void Link (Linker::Module &module)
 
std::shared_ptr< Linker::SegmentGetSegment (std::shared_ptr< Section > &section)
 Return the segment stored inside the section, note that this only works for binary generation.
 
std::shared_ptr< Linker::SegmentGetCodeSegment ()
 
std::shared_ptr< Linker::SegmentGetDataSegment ()
 
std::shared_ptr< Linker::SegmentGetBssSegment ()
 
- Public Member Functions inherited from Linker::InputFormat
virtual void ProduceModule (ModuleCollector &linker, Reader &rd, std::string file_name)
 Reads a file and loads the information into a module object.
 
virtual void ProduceModule (Module &module, Reader &rd)
 Reads a file and loads the information into a module object, a convenience method when there is a single module generated.
 
virtual void GenerateModule (ModuleCollector &linker, std::string file_name, bool is_library=false) const
 Loads the information into a module object.
 
virtual bool FormatProvidesSegmentation () const
 Whether the format enables multiple x86 segments.
 
virtual bool FormatProvidesResources () const
 Whether the format supports resources.
 
virtual bool FormatProvidesLibraries () const
 Whether the format enables importing/exporting libraries.
 
- Public Member Functions inherited from Linker::Format
 Format (offset_t file_offset=0)
 
offset_t ImageSize () const override
 Retrieves size of stored data.
 
offset_t WriteFile (Writer &wr, offset_t count, offset_t offset=0) const override
 Writes data of non-zero filled sections.
 
- Public Member Functions inherited from Linker::Contents
virtual std::shared_ptr< const ImageAsImage () const
 Retrieves a randomly accessible image.
 
std::shared_ptr< ImageAsImage ()
 Retrieves a randomly accessible image.
 
- Public Member Functions inherited from Linker::SegmentManager
void ClearSegmentManager ()
 
void SetLinkScript (std::string script_file, std::map< std::string, std::string > &options) override
 Sets up the linker script and linker parameters.
 
std::unique_ptr< Script::ListGetScript (Module &module)
 Compiles the linker script into an internal format.
 
offset_t GetCurrentAddress () const
 Retrieves current address pointer.
 
void SetCurrentAddress (offset_t address)
 Moves the current address pointer further, and if the current segment already contains data, fill it up to the point.
 
void AlignCurrentAddress (offset_t align)
 Aligns current address to alignment, using SetCurrentAddress.
 
void SetLatestBase (offset_t address)
 Sets the base of the current section (the value from which offsets are counted from)
 
void FinishCurrentSegment ()
 Closes the current segment, sets current_segment to null.
 
std::shared_ptr< SegmentAppendSegment (std::string name)
 Creates a new segment and attaches it to the image.
 
void AppendSegment (std::shared_ptr< Segment > segment)
 Attaches the segment to the image.
 
std::shared_ptr< SegmentFetchSegment (std::string name)
 Attempts to fetch a segment, returns null if not found.
 
void AppendSection (std::shared_ptr< Section > section)
 Adds a new section to the current segment, sets the base to the same as the segment.
 
void ProcessScript (std::unique_ptr< Script::List > &directives, Module &module)
 Executes a parsed linker script on a module and collects segments The function OnNewSegment can be defined to handle each newly allocated segment.
 
void ProcessAction (std::unique_ptr< Script::Node > &action, Module &module)
 
void PostProcessAction (std::unique_ptr< Script::Node > &action, Module &module)
 
void ProcessCommand (std::unique_ptr< Script::Node > &command, Module &module)
 
bool CheckPredicate (std::unique_ptr< Script::Node > &predicate, std::shared_ptr< Section > section, Module &module)
 
offset_t EvaluateExpression (std::unique_ptr< Script::Node > &expression, Module &module)
 
- Public Member Functions inherited from Linker::OutputFormat
virtual bool AddSupplementaryOutputFormat (std::string subformat)
 If the output format actually drives multiple output formats (resource file, apple double, etc.), specify multiple types, return false if unknown.
 
virtual std::vector< OptionDescription< void > > GetMemoryModelNames ()
 Returns a list of the supported memory models, used for documentation.
 
virtual std::vector< OptionDescription< void > > GetSpecialSymbolNames ()
 Returns a list of special symbol names recognized by the format, used for documentation.
 
std::optional< std::string > FetchOption (std::map< std::string, std::string > &options, std::string name)
 Convenience method to look up option by name.
 
std::string FetchOption (std::map< std::string, std::string > &options, std::string name, std::string default_value)
 Convenience method to look up option by name, returning default value if name is missing.
 
std::optional< offset_t > FetchIntegerOption (std::map< std::string, std::string > &options, std::string name)
 Convenience method to look up option by name and convert it to integer.
 
virtual void SetModel (std::string model)
 Sets the way memory is organized, typically modifying a built-in script.
 
virtual std::string GetDefaultExtension (Module &module) const
 Provides a default filename for the output file.
 
virtual bool FormatSupportsSegmentation () const
 Whether the format supports multiple segments.
 
virtual bool FormatIs16bit () const
 Whether the format is 16-bit or not.
 
virtual bool FormatIsProtectedMode () const
 Whether the format is in protected mode or not (x86 only)
 
virtual bool FormatIsLinear () const
 Whether the address space is linear or segmented.
 

Static Public Member Functions

static std::shared_ptr< PEFormatCreateConsoleApplication (target_type target=TargetWinNT, PEOptionalHeader::SubsystemType subsystem=PEOptionalHeader::WindowsCUI)
 
static std::shared_ptr< PEFormatCreateGUIApplication (target_type target=TargetWinNT, PEOptionalHeader::SubsystemType subsystem=PEOptionalHeader::WindowsGUI)
 
static std::shared_ptr< PEFormatCreateLibraryModule (target_type target=TargetWinNT)
 
static std::shared_ptr< PEFormatCreateDeviceDriver (target_type target=TargetWinNT)
 

Public Attributes

std::array< char, 4 > pe_signature
 
std::shared_ptr< ResourcesSectionresources = std::make_shared<ResourcesSection>()
 The resources in this file.
 
std::shared_ptr< ImportsSectionimports = std::make_shared<ImportsSection>()
 The collection of imports in the file.
 
std::shared_ptr< ExportsSectionexports = std::make_shared<ExportsSection>()
 The collection of exported symbols.
 
std::shared_ptr< BaseRelocationsSectionbase_relocations = std::make_shared<BaseRelocationsSection>()
 The collection of base relocations.
 
MZStubWriter stub
 
compatibility_type compatibility = CompatibleNone
 
target_type target = target_type(0)
 The expected target system, only used for setting up default values.
 
output_type output = OUTPUT_EXE
 
bool option_relocatable = true
 Make generated image relocatable (TODO: expose as command line option)
 
bool option_include_deprecated_flags = false
 Certain header flags are deprecated and should be zero, this option sets them if applicable (TODO: expose as command line option)
 
bool option_coff_line_numbers = false
 Include COFF line numbers (TODO: not implemented)
 
bool option_coff_local_symbols = false
 Include COFF local symbols (TODO: not implemented)
 
bool option_debug_info = false
 Include debug information (TODO: not implemented)
 
bool option_import_thunks = false
 By default, imported labels address the import address table directly.
 
std::shared_ptr< Linker::Segmentimport_thunk_segment = nullptr
 Holds the segment that contains the import thinks.
 
Linker::Modulecurrent_module = nullptr
 
std::map< std::pair< std::string, std::string >, uint32_t > import_thunks_by_name
 
std::map< std::pair< std::string, uint16_t >, uint32_t > import_thunks_by_ordinal
 
- Public Attributes inherited from COFF::COFFFormat
offset_t file_size
 
char signature [2] = { }
 The actual value of the magic number (COFF name: f_magic)
 
relocation_format_type relocation_format
 
std::vector< std::shared_ptr< Section > > sections
 The list of COFF sections.
 
uint16_t section_count = 0
 Section count (COFF name: f_nscns)
 
uint32_t timestamp = 0
 Time stamp, unused (COFF name: f_timdat)
 
offset_t symbol_table_offset = 0
 Offset to the first symbol (COFF name: f_symptr)
 
uint32_t symbol_count = 0
 The number of symbols (COFF name: f_nsyms)
 
std::vector< std::unique_ptr< Symbol > > symbols
 The symbols stored inside the COFF file.
 
uint32_t optional_header_size = 0
 The size of the optional header (COFF: f_opthdr)
 
uint16_t flags = 0
 COFF flags, such as whether the file is executable (f_flags)
 
uint16_t target = 0
 TI system target ID.
 
std::unique_ptr< OptionalHeaderoptional_header = nullptr
 The optional header instance used for reading/writing the COFF file.
 
std::map< uint32_t, CDOS68K_Relocationrelocations
 Concurrent DOS 68K requires a special block of data to represent "crunched" relocations (see CPM68KWriter for more details)
 
COFFVariantType coff_variant = AnyCOFFVariant
 
cpu cpu_type = CPU_UNKNOWN
 The CPU type, reflected by the first 16-bit word of a COFF file.
 
::EndianType endiantype = ::UndefinedEndian
 The byte order.
 
bool option_segmentation = false
 
Microsoft::MZSimpleStubWriter stub
 
format_type type = GENERIC
 A representation of the format to generate.
 
bool option_no_relocation = false
 Suppress relocation generation, only relevant for Concurrent DOS 68K, since the other target formats do not store relocations.
 
uint32_t stub_size = 0
 Size of MZ stub, only used for DJGPP COFF executables.
 
std::shared_ptr< Linker::Segmentstack
 Concurrent DOS 68K and FlexOS 386: The stack segment, not stored as part of any section.
 
uint32_t entry_address = 0
 Entry address, gets stored in optional header later.
 
uint32_t relocations_offset = 0
 Concurrent DOS 68K: Offset to relocations.
 
- Public Attributes inherited from Linker::Format
offset_t file_offset
 
- Public Attributes inherited from Linker::SegmentManager
offset_t current_base = 0
 The base address of the current section.
 
std::vector< std::shared_ptr< Segment > > segment_vector
 Ordered sequence of segments.
 
std::map< std::string, std::shared_ptr< Segment > > segment_map
 Map of segments from their names.
 
std::shared_ptr< Segmentcurrent_segment
 Currently processed segment.
 
std::map< std::string, Locationlinker_parameters
 Parameters that permit customizing the linker script.
 
std::string linker_script
 Contents of the linker script.
 

Static Public Attributes

static std::vector< Linker::OptionDescription< void > * > ParameterNames
 
- Static Public Attributes inherited from COFF::COFFFormat
static constexpr COFFVariantType AnyCOFFVariant = COFFVariantType(0)
 
static const std::map< uint32_t, MachineTypeMACHINE_TYPES
 
static constexpr uint16_t R_ABS = 0
 No relocation.
 
static constexpr uint16_t R_DIR16 = 1
 16-bit direct address of symbol
 
static constexpr uint16_t R_REL16 = 2
 16-bit relative address of symbol
 
static constexpr uint16_t R_IND16 = 3
 16-bit indirect address of symbol
 
static constexpr uint16_t R_DIR24 = 4
 24-bit direct address of symbol
 
static constexpr uint16_t R_REL24 = 5
 24-bit relative address of symbol
 
static constexpr uint16_t R_DIR32 = 6
 32-bit direct address of symbol
 
static constexpr uint16_t R_OFF8 = 7
 8-bit low byte of address of symbol
 
static constexpr uint16_t R_OFF16 = 8
 8-bit high byte of address of symbol
 
static constexpr uint16_t R_SEG12 = 9
 (Intel x86) 16-bit segment selector of symbol
 
static constexpr uint16_t R_DIR32S = 10
 (WE32K) 32-bit direct address of symbol, byte swapped
 
static constexpr uint16_t R_AUX = 11
 Auxiliary relocation.
 
static constexpr uint16_t R_OPT16 = 12
 16-bit (WE32K) optimized indirect address of symbol
 
static constexpr uint16_t R_IND24 = 13
 24-bit indirect address of symbol
 
static constexpr uint16_t R_IND32 = 14
 32-bit indirect address of symbol
 
static constexpr uint16_t R_RELBYTE = 15
 8-bit direct (symbol relative) address of symbol
 
static constexpr uint16_t R_RELWORD = 16
 16-bit direct (symbol relative) address of symbol
 
static constexpr uint16_t R_RELLONG = 17
 32-bit direct (symbol relative) address of symbol
 
static constexpr uint16_t R_PCRBYTE = 18
 8-bit PC relative address of symbol
 
static constexpr uint16_t R_PCRWORD = 19
 16-bit PC relative address of symbol
 
static constexpr uint16_t R_PCRLONG = 20
 32-bit PC relative address of symbol
 
static constexpr uint16_t REL_I386_ABSOLUTE = 0
 No relocation.
 
static constexpr uint16_t REL_I386_DIR16 = 1
 16-bit virtual address of symbol (not supported)
 
static constexpr uint16_t REL_I386_REL16 = 2
 16-bit relative address of symbol (not supported)
 
static constexpr uint16_t REL_I386_DIR32 = 6
 32-bit virtual address of symbol
 
static constexpr uint16_t REL_I386_DIR32NB = 7
 32-bit relative virtual address of symbol
 
static constexpr uint16_t REL_I386_SEG12 = 9
 16-bit segment selector of symbol (not supported)
 
static constexpr uint16_t REL_I386_SECTION = 10
 16-bit section index (debugging)
 
static constexpr uint16_t REL_I386_SECREL = 11
 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_I386_TOKEN = 12
 CLR token.
 
static constexpr uint16_t REL_I386_SECREL7 = 13
 7-bit offset from section base
 
static constexpr uint16_t REL_I386_REL32 = 20
 32-bit relative address of symbol
 
static constexpr uint16_t REL_AMD64_ABSOLUTE = 0
 No relocation.
 
static constexpr uint16_t REL_AMD64_ADDR64 = 1
 64-bit virtual address of symbol
 
static constexpr uint16_t REL_AMD64_ADDR32 = 2
 32-bit virtual address of symbol
 
static constexpr uint16_t REL_AMD64_ADDR32NB = 3
 32-bit relative virtual address of symbol
 
static constexpr uint16_t REL_AMD64_REL32 = 4
 32-bit relative address of symbol
 
static constexpr uint16_t REL_AMD64_REL32_1 = 5
 32-bit relative address of symbol minus 1
 
static constexpr uint16_t REL_AMD64_REL32_2 = 6
 32-bit relative address of symbol minus 2
 
static constexpr uint16_t REL_AMD64_REL32_3 = 7
 32-bit relative address of symbol minus 3
 
static constexpr uint16_t REL_AMD64_REL32_4 = 8
 32-bit relative address of symbol minus 4
 
static constexpr uint16_t REL_AMD64_REL32_5 = 9
 32-bit relative address of symbol minus 5
 
static constexpr uint16_t REL_AMD64_SECTION = 10
 16-bit section index (debugging)
 
static constexpr uint16_t REL_AMD64_SECREL = 11
 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_AMD64_SECREL7 = 12
 7-bit offset from section base
 
static constexpr uint16_t REL_AMD64_TOKEN = 13
 CLR token.
 
static constexpr uint16_t REL_AMD64_SREL32 = 14
 ?
 
static constexpr uint16_t REL_AMD64_PAIR = 15
 ?
 
static constexpr uint16_t REL_AMD64_SSPAN32 = 16
 ?
 
static constexpr uint16_t REL_ARM_ABSOLUTE = 0
 No relocation.
 
static constexpr uint16_t REL_ARM_ADDR32 = 1
 32-bit virtual address of symbol
 
static constexpr uint16_t REL_ARM_ADDR32NB = 2
 32-bit relative virtual address of symbol
 
static constexpr uint16_t REL_ARM_BRANCH24 = 3
 24-bit ARM relative address of symbol
 
static constexpr uint16_t REL_ARM_BRANCH11 = 4
 Two 16-bit instructions with 11 bits of the relative address of symbol each.
 
static constexpr uint16_t REL_ARM_REL32 = 10
 32-bit relative address of symbol
 
static constexpr uint16_t REL_ARM_SECTION = 14
 16-bit section index (debugging)
 
static constexpr uint16_t REL_ARM_SECREL = 15
 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_ARM_MOV32 = 16
 Two 32-bit ARM instructions with 32 bits of the virtual address of the symbol.
 
static constexpr uint16_t REL_THUMB_MOV32 = 17
 Two 32-bit Thumb instructions with 32 bits of the virtual address of the symbol.
 
static constexpr uint16_t REL_THUMB_BRANCH24 = 20
 24-bit Thumb relative address of symbol
 
static constexpr uint16_t REL_THUMB_BLX23 = 21
 23-bit Thumb BLX relative address of symbol
 
static constexpr uint16_t REL_ARM_PAIR = 22
 ?
 
static constexpr uint16_t REL_ARM64_ABSOLUTE = 0
 No relocation.
 
static constexpr uint16_t REL_ARM64_ADDR32 = 1
 32-bit virtual address of symbol
 
static constexpr uint16_t REL_ARM64_ADDR32NB = 2
 32-bit relative virtual address of symbol
 
static constexpr uint16_t REL_ARM64_BRANCH26 = 3
 26-bit ARM relative address of symbol
 
static constexpr uint16_t REL_ARM64_PAGEBASE_REL21 = 4
 Page base of symbol for ADRP.
 
static constexpr uint16_t REL_ARM64_REL21 = 5
 12-bit relative address of symbol for ADR
 
static constexpr uint16_t REL_ARM64_PAGEOFFSET_12A = 6
 12-bit page offset of symbol for ADD/ADDS
 
static constexpr uint16_t REL_ARM64_PAGEOFFSET_12L = 7
 12-bit page offset of symbol for LDR
 
static constexpr uint16_t REL_ARM64_SECREL = 8
 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_ARM64_SECREL_LOW12A = 9
 Low 12 bits of offset from section start for ADD/ADDS.
 
static constexpr uint16_t REL_ARM64_SECREL_HIGH12A = 10
 Bits 12 to 23 of offset from section start for ADD/ADDS.
 
static constexpr uint16_t REL_ARM64_SECREL_LOW12L = 11
 Low 12 bits of offset from section start for LDR.
 
static constexpr uint16_t REL_ARM64_TOKEN = 12
 CLR token.
 
static constexpr uint16_t REL_ARM64_SECTION = 13
 16-bit section index (debugging)
 
static constexpr uint16_t REL_ARM64_ADDR64 = 14
 64-bit virtual address of symbol
 
static constexpr uint16_t REL_ARM64_BRANCH19 = 15
 19-bit relative address of symbol
 
static constexpr uint16_t REL_ARM64_BRANCH14 = 16
 14-bit relative address of symbol for TBZ/TBNZ
 
static constexpr uint16_t REL_ARM64_REL32 = 17
 32-bit relative address of symbol
 
static constexpr uint16_t REL_ALPHA_ABSOLUTE = 0
 No relocation.
 
static constexpr uint16_t REL_ALPHA_REFLONG = 1
 32-bit virtual address of symbol
 
static constexpr uint16_t REL_ALPHA_REFQUAD = 2
 64-bit virtual address of symbol
 
static constexpr uint16_t REL_ALPHA_GPREL32 = 3
 32-bit global pointer relative address of symbol
 
static constexpr uint16_t REL_ALPHA_LITERAL = 4
 16-bit global pointer relative address of symbol
 
static constexpr uint16_t REL_ALPHA_LITUSE = 5
 reserved
 
static constexpr uint16_t REL_ALPHA_GPDISP = 6
 reserved
 
static constexpr uint16_t REL_ALPHA_BRADDR = 7
 21-bit relative address of symbol
 
static constexpr uint16_t REL_ALPHA_HINT = 8
 14-bit hint for jump target
 
static constexpr uint16_t REL_ALPHA_INLINE_REFLONG = 9
 32-bit virtual address of symbol split into two 16-bit values; this relocation must be followed by an ABSOLUTE or MATCH relocation
 
static constexpr uint16_t REL_ALPHA_REFHI = 10
 High 16 bits of 32-bit virtual address of symbol; this relocation must be followed by a PAIR relocation.
 
static constexpr uint16_t REL_ALPHA_REFLO = 11
 Low 16 bits of 32-bit virtual address of symbol.
 
static constexpr uint16_t REL_ALPHA_PAIR = 12
 Displacement value for a preceding REFHI, SECRELHI, REFQ3 or REFQ2 relocation.
 
static constexpr uint16_t REL_ALPHA_MATCH = 13
 Displacement value for a preceding REFLONG relocation.
 
static constexpr uint16_t REL_ALPHA_SECTION = 14
 16-bit section index (debugging)
 
static constexpr uint16_t REL_ALPHA_SECREL = 15
 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_ALPHA_REFLONGNB = 16
 32-bit relative virtual address of symbol
 
static constexpr uint16_t REL_ALPHA_SECRELLO = 17
 Low 16 bits of 32-bit offset from section start (debugging)
 
static constexpr uint16_t REL_ALPHA_SECRELHI = 18
 High 16 bits of 32-bit offset from section start; this relocation must be followed by a PAIR relocation (debugging)
 
static constexpr uint16_t REL_ALPHA_REFQ3 = 19
 The second most significant 16 bits of the 64-bit virtual address of symbol; this relocation must be followed by a PAIR relocation.
 
static constexpr uint16_t REL_ALPHA_REFQ2 = 20
 The third most significant 16 bits of the 64-bit virtual address of symbol; this relocation must be followed by a PAIR relocation.
 
static constexpr uint16_t REL_ALPHA_REFQ1 = 21
 The least significant 16 bits of the 64-bit virtual address of symbol.
 
static constexpr uint16_t REL_ALPHA_GPRELLO = 22
 Low 16 bits of 32-bit global pointer relative address of symbol.
 
static constexpr uint16_t REL_ALPHA_GPRELHI = 23
 High 16 bits of 32-bit global pointer relative address of symbol.
 
static constexpr uint16_t R_Z80_IMM8 = 0x22
 
static constexpr uint16_t R_Z80_IMM16 = 0x01
 
static constexpr uint16_t R_Z80_IMM24 = 0x33
 
static constexpr uint16_t R_Z80_IMM32 = 0x17
 
static constexpr uint16_t R_Z80_OFF8 = 0x32
 
static constexpr uint16_t R_Z80_JR = 0x02
 
static constexpr uint16_t R_Z8K_IMM4L = 0x23
 
static constexpr uint16_t R_Z8K_IMM4H = 0x24
 
static constexpr uint16_t R_Z8K_DISP7 = 0x25
 
static constexpr uint16_t R_Z8K_IMM8 = 0x22
 
static constexpr uint16_t R_Z8K_IMM16 = 0x01
 
static constexpr uint16_t R_Z8K_REL16 = 0x04
 
static constexpr uint16_t R_Z8K_IMM32 = 0x11
 
static constexpr uint16_t R_Z8K_JR = 0x02
 
static constexpr uint16_t R_Z8K_CALLR = 0x05
 
static constexpr uint16_t R_W65_ABS8 = 0x01
 
static constexpr uint16_t R_W65_ABS16 = 0x02
 
static constexpr uint16_t R_W65_ABS24 = 0x03
 
static constexpr uint16_t R_W65_ABS8S8 = 0x04
 
static constexpr uint16_t R_W65_ABS8S16 = 0x05
 
static constexpr uint16_t R_W65_ABS16S8 = 0x06
 
static constexpr uint16_t R_W65_ABS16S16 = 0x07
 
static constexpr uint16_t R_W65_PCR8 = 0x08
 
static constexpr uint16_t R_W65_PCR16 = 0x09
 
static constexpr uint16_t R_W65_DP = 0x0A
 
static std::vector< Linker::OptionDescription< void > * > ParameterNames
 

Additional Inherited Members

- Protected Member Functions inherited from COFF::COFFFormat
void ReadCOFFHeader (Linker::Reader &rd)
 
void ReadOptionalHeader (Linker::Reader &rd)
 
void ReadRestOfFile (Linker::Reader &rd)
 
offset_t WriteFileContents (Linker::Writer &wr) const
 
- Protected Attributes inherited from Linker::SegmentManager
offset_t current_address = 0
 Holds the current address value when there is no current_segment.
 
bool current_is_template_head = false
 Set during evaluation of guard in a for <guard> { <body> } statement.
 
bool current_is_template = false
 Set during evaluation of body in a for <guard> { <body> } statement.
 
offset_t template_counter = 0
 Counts how many times a for statement has been invoked.
 
bool condition_failed = false
 Condition to signal end of current for statement.
 
std::string current_template_name
 Records the name of the currently generating segment for a for statement.
 

Detailed Description

Microsoft PE .EXE portable executable file format.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

COFF file header flags, most of these are obsolete, we only use them as precombined flag sets.

Enumerator
FLAG_NO_RELOCATIONS 

IMAGE_FILE_RELOCS_STRIPPED (F_RELFLG)

FLAG_EXECUTABLE 

IMAGE_FILE_EXECUTABLE_IMAGE (F_EXEC)

FLAG_NO_LINE_NUMBERS 

(deprecated) IMAGE_FILE_LINE_NUMS_STRIPPED (F_LNNO)

FLAG_NO_SYMBOLS 

(deprecated) IMAGE_FILE_LOCAL_SYMS_STRIPPED (F_LSYMS)

FLAG_TRIM_WORKING_SET 

(deprecated) IMAGE_FILE_AGGRESSIVE_WS_TRIM (no COFF meaning)

FLAG_LARGE_ADDRESS 

IMAGE_FILE_LARGE_ADDRESS_AWARE (no COFF meaning)

FLAG_LITTLE_ENDIAN 

(deprecated) IMAGE_FILE_BYTES_REVERSED_LO (redefined from COFF F_AR16WR)

FLAG_32BIT 

IMAGE_FILE_32BIT_MACHINE (redefined from COFF F_AR32WR)

FLAG_NO_DEBUG 

IMAGE_FILE_DEBUG_STRIPPED (redefined from COFF F_AR32W)

FLAG_ON_REMOVABLE 

IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP.

FLAG_ON_NETWORK 

IMAGE_FILE_NET_RUN_FROM_SWAP.

FLAG_SYSTEM 

IMAGE_FILE_SYSTEM.

FLAG_LIBRARY 

IMAGE_FILE_DLL.

FLAG_UNIPROCESSOR_ONLY 

IMAGE_FILE_UP_SYSTEM_ONLY.

FLAG_BIG_ENDIAN 

(deprecated) IMAGE_FILE_BYTES_REVERSED_HI

◆ compatibility_type

Enumerator
CompatibleNone 

No emulation is attempted.

CompatibleWatcom 

Attempts to mimic the output of the Watcom linker.

CompatibleMicrosoft 

Attempts to mimic the output of the Microsoft linker (TODO: unimplemented)

CompatibleBorland 

Attempts to mimic the output of the Borland linker (TODO: unimplemented)

CompatibleGNU 

Attempts to mimic the output of the GNU (MinGW) linker, this is undefined for the LE output.

◆ output_type

Represents the target image type.

Enumerator
OUTPUT_EXE 

An executable image, conventionally taking the suffix .exe

OUTPUT_DLL 

A dynamically linked library, conventionally taking the suffix .dll

OUTPUT_SYS 

A system file or driver, conventionally taking the suffix .sys

◆ target_type

Represents settings and assumptions about the target.

Enumerator
TargetWin9x 

Windows 95/98/ME.

TargetWinNT 

Windows NT kernel.

TargetWinCE 

Windows CE kernel.

TargetMacintosh 

Windows Portability Library.

TargetWin32s 

Win32s compatibility library for Windows 3.1.

TargetTNT 

Phar Lap TNT DOS Extender.

TargetEFI 

EFI and UEFI firmware.

TargetDotNET 

.NET environment

TargetXbox 

Xbox.

Member Function Documentation

◆ AddBaseRelocation()

void PEFormat::AddBaseRelocation ( uint32_t rva,
BaseRelocation::relocation_type type,
uint16_t low_ref = 0 )

Adds a base relocation at the specified relative virtual address.

Parameters
rvaThe relative virtual address where the relocation appears, to be split into the page address and the page offset
typeThe type of the base relocation
low_refOnly used for HIGHADJ relocations, the parameter to be stored after the base relocation entry

◆ AllocateSymbols()

void PEFormat::AllocateSymbols ( Linker::Module & module) const
overridevirtual

Instructs the module to allocate any unallocated local symbols.

Reimplemented from Linker::OutputFormat.

◆ CalculateValues()

void PEFormat::CalculateValues ( )
overridevirtual

Intermediate step between processing module and generating output file to set up headers and management sections It is expected that after a module is processed, additional steps are required to evaluate the final values of the fields.

Reimplemented from COFF::COFFFormat.

◆ Dump()

void PEFormat::Dump ( Dumper::Dumper & dump) const
overridevirtual

Display file contents in a nice manner.

Reimplemented from COFF::COFFFormat.

◆ FormatAdditionalSectionFlags()

unsigned PEFormat::FormatAdditionalSectionFlags ( std::string section_name) const
overridevirtual

Reimplemented from COFF::COFFFormat.

◆ FormatSupportsLibraries()

bool PEFormat::FormatSupportsLibraries ( ) const
overridevirtual

Whether the format supports libraries.

Reimplemented from Linker::OutputFormat.

◆ FormatSupportsResources()

bool PEFormat::FormatSupportsResources ( ) const
overridevirtual

Whether the format supports resources.

Formats such as NE, LE/LX, PE and the Macintosh classic support including resources in the final binary. To simplify writing resources, the ELF parser permits incorporating them directly in the binary image as $$RSRC$_<type>$<id>.

Reimplemented from Linker::OutputFormat.

◆ GenerateBaseRelocationSection()

offset_t PEFormat::GenerateBaseRelocationSection ( Linker::Module & module,
offset_t image_end )

Creates a base relocation section and attaches it to the end of the file.

Parameters
moduleThe module currently being processed
image_endThe last address currently used by the module (should be identical to GetMemoryImageEnd())
Returns
The last address after the section has been added (identical to new GetMemoryImageEnd())

◆ GenerateExportSection()

offset_t PEFormat::GenerateExportSection ( Linker::Module & module,
offset_t image_end )

Creates an export section and attaches it to the end of the file.

Parameters
moduleThe module currently being processed
image_endThe last address currently used by the module (should be identical to GetMemoryImageEnd())
Returns
The last address after the section has been added (identical to new GetMemoryImageEnd())

◆ GenerateFile()

void PEFormat::GenerateFile ( std::string filename,
Linker::Module & module )
overridevirtual

The main function that handles processing, calculating and generating the final image.

Reimplemented from COFF::COFFFormat.

◆ GenerateImportSection()

offset_t PEFormat::GenerateImportSection ( Linker::Module & module,
offset_t image_end )

Creates an import section and attaches it to the end of the file.

Parameters
moduleThe module currently being processed
image_endThe last address currently used by the module (should be identical to GetMemoryImageEnd())
Returns
The last address after the section has been added (identical to new GetMemoryImageEnd())

◆ GenerateResourceSection()

offset_t PEFormat::GenerateResourceSection ( Linker::Module & module,
offset_t image_end )

Creates a resource section and attaches it to the end of the file.

Parameters
moduleThe module currently being processed
image_endThe last address currently used by the module (should be identical to GetMemoryImageEnd())
Returns
The last address after the section has been added (identical to new GetMemoryImageEnd())

◆ GetDefaultExtension()

std::string PEFormat::GetDefaultExtension ( Linker::Module & module,
std::string filename ) const
overridevirtual

Appends a default extension to the filename.

A typical behavior would be to append .exe at the end of the filename. The default action is to leave it intact.

Reimplemented from COFF::COFFFormat.

◆ GetLinkerScriptParameterNames()

std::vector< Linker::OptionDescription< void > * > PEFormat::GetLinkerScriptParameterNames ( )
overridevirtual

Returns a list of the parameters used in the linker scripts, used for documentation.

Reimplemented from COFF::COFFFormat.

◆ GetOptions()

std::shared_ptr< Linker::OptionCollector > PEFormat::GetOptions ( )
overridevirtual

Returns object containing a sequence of option fields provided with the -S command line flag.

Reimplemented from COFF::COFFFormat.

◆ MapRVAToSectionData()

size_t PEFormat::MapRVAToSectionData ( uint32_t rva,
size_t bytes,
std::shared_ptr< Section > & found_section,
size_t & section_offset ) const

Attempts to find the section:offset pair that encompasses this memory range.

This function can be used to find the data corresponding to some relative virtual address, once loaded into sections. To fill the entire memory range, this function should be called repeatedly as it crosses section boundaries to retrieve all the parts of the memory.

Parameters
rvaThe relative virtual address of the start of the range, relative to the image base as specified in the optional header
bytesThe number of bytes in this range
found_sectionThe section that contains this relative virtual address
section_offsetThe offset within the section that has this relative virtual address
Returns
The largest number of bytes of this range that still belongs to this section, or 0 if the address does not belong to any section data

◆ OnCallDirective()

void PEFormat::OnCallDirective ( std::string identifier)
overridevirtual

Callback function for the CALL directive.

Reimplemented from Linker::SegmentManager.

◆ OnNewSegment()

void PEFormat::OnNewSegment ( std::shared_ptr< Linker::Segment > segment)
overridevirtual

Callback function when allocating a new segment When the linker script runs, it creates segments consecutively. Overriding this method permits the output format to handle the allocated segment.

Reimplemented from COFF::COFFFormat.

◆ ProcessModule()

void PEFormat::ProcessModule ( Linker::Module & object)
overridevirtual

Processes the module object and initializes format fields.

Reimplemented from COFF::COFFFormat.

◆ ReadFile()

void PEFormat::ReadFile ( Linker::Reader & rd)
overridevirtual

Loads file into memory.

Reimplemented from COFF::COFFFormat.

◆ SetOptions()

void PEFormat::SetOptions ( std::map< std::string, std::string > & options)
overridevirtual

Passes command line parameters as settings over to format object.

Reimplemented from COFF::COFFFormat.

◆ WriteFile()

offset_t PEFormat::WriteFile ( Linker::Writer & wr) const
overridevirtual

Stores data in memory to file.

Reimplemented from COFF::COFFFormat.

Member Data Documentation

◆ ParameterNames

std::vector< Linker::OptionDescription< void > * > PEFormat::ParameterNames
static
Initial value:
=
{
}

The documentation for this class was generated from the following files: