package object

All checked builds pass
  1. Overview
  2. Docs
A unified interface for reading and writing object file formats

Install

dune-project
 Dependency

Authors

Maintainers

Sources

object-0.2.tbz
sha256=9c5c1294abd9075f25639ec2a10f7673b7aa6f5fd8915d24d39e17195a05bd5c
sha512=569061b63b8c537a5fc2bc900b6c9cf2b8e0fd01937980eb5fa94cf48479847b5f732d6f0d125ac1d5bffdaa9555417905ff271bea12d4ce9b24d2e1ef28fcd1

doc/object/Pe/index.html

Module PeSource

Windows PE/COFF parser based on https://docs.microsoft.com/en-us/windows/win32/debug/pe-format.

Overview

PE (Portable Executable) and COFF (Common Object File Format) are Microsoft's standard binary file formats for Windows executables, object files, and DLLs. This module provides parsing capabilities for both formats.

What are PE/COFF Files?

COFF (Common Object File Format):

  • Object file format for intermediate compilation results
  • Contains relocatable code and data sections
  • Used by linkers to create final executables
  • Originally developed by AT&T for Unix System V
  • Adopted and extended by Microsoft for Windows

PE (Portable Executable):

  • Executable file format based on COFF
  • Used for .exe, .dll, .sys, and other executable files on Windows
  • Extends COFF with additional Windows-specific headers and structures
  • Supports both 32-bit (PE32) and 64-bit (PE32+) architectures
  • Contains runtime loading and dynamic linking information

File Structure

PE files have a layered structure that builds upon the COFF foundation:

PE File Layout:
+---------------------------+
|      DOS Header           |  <- Legacy 16-bit DOS compatibility
|      ("MZ" signature)     |
+---------------------------+
|      DOS Stub Program     |  <- Optional 16-bit code
+---------------------------+
|      PE Signature         |  <- "PE\0\0" magic number
+---------------------------+
|      COFF Header          |  <- Core file metadata (20 bytes)
+---------------------------+
|      Optional Header      |  <- PE-specific information
|      (PE32: 224 bytes)    |     - Entry point, image base
|      (PE32+: 240 bytes)   |     - Subsystem, DLL characteristics
+---------------------------+
|      Section Headers      |  <- Array of section metadata
|      (40 bytes each)      |
+---------------------------+
|                           |
|      Section Data         |  <- Actual code and data
|      (.text, .data, etc.) |
|                           |
+---------------------------+

Key Components

DOS Header: Legacy compatibility header allowing PE files to run on DOS systems (displaying "This program cannot be run in DOS mode" message).

COFF Header: Core 20-byte structure containing:

  • Target machine architecture (x86, x64, ARM, etc.)
  • Number of sections in the file
  • File creation timestamp
  • Symbol table information
  • File characteristics flags

Optional Header: PE-specific extended header (despite the name, always present in PE files) containing:

  • Entry point address for program execution
  • Image base address for memory loading
  • Section and file alignment requirements
  • Subsystem type (console, GUI, driver, etc.)
  • DLL characteristics and stack/heap sizes

Section Headers: Array of 40-byte structures describing each section:

  • Section name (up to 8 characters)
  • Virtual and file addresses/sizes
  • Section characteristics (readable, writable, executable)
  • Relocation and line number information

Common Sections

  • .text: Executable code section
  • Characteristics: CNT_CODE; MEM_EXECUTE; MEM_READ
  • .data: Initialised global and static variables
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ; MEM_WRITE
  • .rdata: Read-only data (constants, string literals)
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ
  • .bss: Uninitialised data (zero-filled at load time)
  • Characteristics: CNT_UNINITIALIZED_DATA; MEM_READ; MEM_WRITE
  • .idata: Import address table for DLL functions
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ; MEM_WRITE
  • .edata: Export table for DLL-exported functions
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ
  • .rsrc: Embedded resources (icons, dialogs, version info)
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ
  • .reloc: Base relocation table for ASLR support
  • Characteristics: CNT_INITIALIZED_DATA; MEM_READ; MEM_DISCARDABLE
  • .debug_*: Debug information sections
  • Characteristics: LNK_INFO; LNK_REMOVE; MEM_DISCARDABLE

Section Characteristics Details

The PE format defines comprehensive section characteristics that control linking, loading, and runtime behaviour:

Content Classification:

  • TYPE_NO_PAD: Section should not be padded to alignment boundary
  • CNT_CODE: Contains executable machine code
  • CNT_INITIALIZED_DATA: Contains initialised data (variables, constants)
  • CNT_UNINITIALIZED_DATA: Contains uninitialised data (BSS segment)

Linker Control:

  • LNK_INFO: Informational section (comments, debug info)
  • LNK_REMOVE: Section excluded from final executable image
  • LNK_COMDAT: COMDAT section for template instantiation/deduplication

Memory Management:

  • MEM_EXECUTE: Section can be executed (code pages)
  • MEM_READ: Section can be read (most sections)
  • MEM_WRITE: Section can be written to (data sections)
  • MEM_SHARED: Section shared between processes (system sections)
  • MEM_DISCARDABLE: Section can be discarded after loading (debug info)
  • MEM_NOT_CACHED: Disable caching (device drivers, memory-mapped I/O)
  • MEM_NOT_PAGED: Keep in physical memory (kernel code, interrupt handlers)

PE32 vs PE32+

The format supports both 32-bit and 64-bit architectures:

  • PE32: 32-bit format with 32-bit addresses and pointers
  • PE32+: 64-bit format with 64-bit addresses and some expanded fields
  • Detected by the magic number in the optional header (0x10B vs 0x20B)
  • Some fields change size between formats (image base, stack/heap sizes)

Usage Examples

  (* Detect if a buffer contains a PE file *)
  if Pe.is_pe buffer then
    Printf.printf "Found PE file\n"

  (* Parse complete PE structure *)
  let pe_obj = Pe.read buffer in

  (* Get basic information *)
  let arch = Pe.machine_to_arch pe_obj.coff_header.machine in
  let is_64 = Pe.is_64bit pe_obj in
  let is_exe = Pe.is_executable pe_obj in

  (* Access sections *)
  let sections = pe_obj.section_headers in
  Array.iter (fun sec ->
    Printf.printf "Section: %s\n" sec.name
  ) sections

References

Sourcetype machine_type =
  1. | IMAGE_FILE_MACHINE_I386
    (*

    Intel 386 or later processors

    *)
  2. | IMAGE_FILE_MACHINE_AMD64
    (*

    AMD64/x64 architecture

    *)
  3. | IMAGE_FILE_MACHINE_ARM
    (*

    ARM little-endian

    *)
  4. | IMAGE_FILE_MACHINE_ARM64
    (*

    ARM64 little-endian

    *)
  5. | IMAGE_FILE_MACHINE_ARM64EC
    (*

    ARM64EC (emulation-compatible)

    *)
  6. | IMAGE_FILE_MACHINE_ARM64X
    (*

    ARM64X (mixed ARM64 + ARM64EC)

    *)
  7. | IMAGE_FILE_MACHINE_IA64
    (*

    Intel Itanium

    *)
  8. | IMAGE_FILE_MACHINE_RISCV32
    (*

    RISC-V 32-bit

    *)
  9. | IMAGE_FILE_MACHINE_RISCV64
    (*

    RISC-V 64-bit

    *)
  10. | IMAGE_FILE_MACHINE_UNKNOWN of Object_types.u16
    (*

    Unknown machine type with raw value

    *)

Machine architecture types supported by PE/COFF format

Sourcetype pe_magic =
  1. | PE32
    (*

    32-bit PE format (0x10B)

    *)
  2. | PE32PLUS
    (*

    64-bit PE format (0x20B)

    *)

PE format magic numbers indicating 32-bit vs 64-bit PE files

Sourcetype section_characteristics =
  1. | IMAGE_SCN_TYPE_NO_PAD
    (*

    Section should not be padded to next boundary

    *)
  2. | IMAGE_SCN_CNT_CODE
    (*

    Section contains executable code

    *)
  3. | IMAGE_SCN_CNT_INITIALIZED_DATA
    (*

    Section contains initialised data

    *)
  4. | IMAGE_SCN_CNT_UNINITIALIZED_DATA
    (*

    Section contains uninitialised data

    *)
  5. | IMAGE_SCN_LNK_INFO
    (*

    Section contains comments or other information

    *)
  6. | IMAGE_SCN_LNK_REMOVE
    (*

    Section will not become part of the image

    *)
  7. | IMAGE_SCN_LNK_COMDAT
    (*

    Section contains COMDAT data

    *)
  8. | IMAGE_SCN_MEM_DISCARDABLE
    (*

    Section can be discarded as needed

    *)
  9. | IMAGE_SCN_MEM_NOT_CACHED
    (*

    Section cannot be cached

    *)
  10. | IMAGE_SCN_MEM_NOT_PAGED
    (*

    Section is not pageable

    *)
  11. | IMAGE_SCN_MEM_SHARED
    (*

    Section can be shared in memory

    *)
  12. | IMAGE_SCN_MEM_EXECUTE
    (*

    Section can be executed as code

    *)
  13. | IMAGE_SCN_MEM_READ
    (*

    Section can be read

    *)
  14. | IMAGE_SCN_MEM_WRITE
    (*

    Section can be written to

    *)
  15. | IMAGE_SCN_UNKNOWN of Object_types.u32
    (*

    Unknown characteristics with raw flags

    *)

Section characteristics flags indicating section properties and behaviour.

These flags control how sections are processed during linking and loading:

Content Type Flags:

  • Content classification flags indicate the nature of section data
  • Used by linkers to group related sections and set appropriate permissions

Linker Flags:

  • Control linker behaviour for object files and debugging information
  • Determine whether sections are included in the final executable image

Memory Management Flags:

  • Control runtime memory behaviour and access permissions
  • Set by the loader to configure memory protection and caching behaviour

Common flag combinations:

  • Executable code: CNT_CODE; MEM_EXECUTE; MEM_READ
  • Read-only data: CNT_INITIALIZED_DATA; MEM_READ
  • Read-write data: CNT_INITIALIZED_DATA; MEM_READ; MEM_WRITE
  • Debug info: LNK_INFO; LNK_REMOVE; MEM_DISCARDABLE
Sourcetype coff_header = {
  1. machine : machine_type;
    (*

    Target machine architecture

    *)
  2. number_of_sections : Object_types.u16;
    (*

    Number of section headers

    *)
  3. time_date_stamp : Object_types.u32;
    (*

    File creation timestamp

    *)
  4. pointer_to_symbol_table : Object_types.u32;
    (*

    File offset to symbol table

    *)
  5. number_of_symbols : Object_types.u32;
    (*

    Number of symbol table entries

    *)
  6. size_of_optional_header : Object_types.u16;
    (*

    Size of optional header

    *)
  7. characteristics : Object_types.u16;
    (*

    File characteristics flags

    *)
}

COFF file header structure (20 bytes)

Sourcetype data_directory_type =
  1. | IMAGE_DIRECTORY_ENTRY_EXPORT
    (*

    Export table

    *)
  2. | IMAGE_DIRECTORY_ENTRY_IMPORT
    (*

    Import table

    *)
  3. | IMAGE_DIRECTORY_ENTRY_RESOURCE
    (*

    Resource table

    *)
  4. | IMAGE_DIRECTORY_ENTRY_EXCEPTION
    (*

    Exception table

    *)
  5. | IMAGE_DIRECTORY_ENTRY_SECURITY
    (*

    Certificate table

    *)
  6. | IMAGE_DIRECTORY_ENTRY_BASERELOC
    (*

    Base relocation table

    *)
  7. | IMAGE_DIRECTORY_ENTRY_DEBUG
    (*

    Debug data

    *)
  8. | IMAGE_DIRECTORY_ENTRY_ARCHITECTURE
    (*

    Architecture specific data

    *)
  9. | IMAGE_DIRECTORY_ENTRY_GLOBALPTR
    (*

    Global pointer register

    *)
  10. | IMAGE_DIRECTORY_ENTRY_TLS
    (*

    Thread local storage table

    *)
  11. | IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
    (*

    Load configuration table

    *)
  12. | IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
    (*

    Bound import table

    *)
  13. | IMAGE_DIRECTORY_ENTRY_IAT
    (*

    Import address table

    *)
  14. | IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
    (*

    Delay import descriptor

    *)
  15. | IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
    (*

    COM+ runtime descriptor

    *)
  16. | IMAGE_DIRECTORY_ENTRY_RESERVED
    (*

    Reserved

    *)

Data directory types as defined in the PE specification

Sourcetype data_directory_entry = {
  1. virtual_address : Object_types.u32;
    (*

    RVA of the table

    *)
  2. size : Object_types.u32;
    (*

    Size of the table in bytes

    *)
}

Data directory entry containing address and size of a table

Sourcetype data_directory = {
  1. export_table : data_directory_entry option;
    (*

    Export table

    *)
  2. import_table : data_directory_entry option;
    (*

    Import table

    *)
  3. resource_table : data_directory_entry option;
    (*

    Resource table

    *)
  4. exception_table : data_directory_entry option;
    (*

    Exception table

    *)
  5. certificate_table : data_directory_entry option;
    (*

    Certificate table

    *)
  6. base_relocation_table : data_directory_entry option;
    (*

    Base relocation table

    *)
  7. debug_data : data_directory_entry option;
    (*

    Debug data

    *)
  8. architecture_data : data_directory_entry option;
    (*

    Architecture specific data

    *)
  9. global_ptr : data_directory_entry option;
    (*

    Global pointer register

    *)
  10. tls_table : data_directory_entry option;
    (*

    Thread local storage table

    *)
  11. load_config_table : data_directory_entry option;
    (*

    Load configuration table

    *)
  12. bound_import_table : data_directory_entry option;
    (*

    Bound import table

    *)
  13. import_address_table : data_directory_entry option;
    (*

    Import address table

    *)
  14. delay_import_table : data_directory_entry option;
    (*

    Delay import descriptor

    *)
  15. com_descriptor : data_directory_entry option;
    (*

    COM+ runtime descriptor

    *)
  16. reserved : data_directory_entry option;
    (*

    Reserved

    *)
}

Data directory containing all Windows-specific tables

Sourceval get_data_directory_entry : data_directory -> data_directory_type -> data_directory_entry option

Get a data directory entry by type

Sourceval has_data_directory : data_directory -> data_directory_type -> bool

Check if a data directory entry exists and is non-empty

Sourceval get_data_directory_address : data_directory -> data_directory_type -> Object_types.u32 option

Get the virtual address of a data directory entry

Sourceval get_data_directory_size : data_directory -> data_directory_type -> Object_types.u32 option

Get the size of a data directory entry

Sourcetype optional_header = {
  1. magic : pe_magic;
    (*

    PE format version

    *)
  2. major_linker_version : Object_types.u8;
    (*

    Major linker version

    *)
  3. minor_linker_version : Object_types.u8;
    (*

    Minor linker version

    *)
  4. size_of_code : Object_types.u32;
    (*

    Total size of code sections

    *)
  5. size_of_initialized_data : Object_types.u32;
    (*

    Total size of initialised data

    *)
  6. size_of_uninitialized_data : Object_types.u32;
    (*

    Total size of uninitialised data

    *)
  7. address_of_entry_point : Object_types.u32;
    (*

    RVA of entry point

    *)
  8. base_of_code : Object_types.u32;
    (*

    RVA of code section base

    *)
  9. base_of_data : Object_types.u32 option;
    (*

    RVA of data section base (PE32 only)

    *)
  10. image_base : Object_types.u64;
    (*

    Preferred load address

    *)
  11. section_alignment : Object_types.u32;
    (*

    Section alignment in memory

    *)
  12. file_alignment : Object_types.u32;
    (*

    Section alignment in file

    *)
  13. major_operating_system_version : Object_types.u16;
    (*

    Major OS version

    *)
  14. minor_operating_system_version : Object_types.u16;
    (*

    Minor OS version

    *)
  15. major_image_version : Object_types.u16;
    (*

    Major image version

    *)
  16. minor_image_version : Object_types.u16;
    (*

    Minor image version

    *)
  17. major_subsystem_version : Object_types.u16;
    (*

    Major subsystem version

    *)
  18. minor_subsystem_version : Object_types.u16;
    (*

    Minor subsystem version

    *)
  19. win32_version_value : Object_types.u32;
    (*

    Win32 version (reserved)

    *)
  20. size_of_image : Object_types.u32;
    (*

    Total image size in memory

    *)
  21. size_of_headers : Object_types.u32;
    (*

    Total header size

    *)
  22. checksum : Object_types.u32;
    (*

    Image checksum

    *)
  23. subsystem : Object_types.u16;
    (*

    Target subsystem

    *)
  24. dll_characteristics : Object_types.u16;
    (*

    DLL characteristics

    *)
  25. size_of_stack_reserve : Object_types.u64;
    (*

    Stack reserve size

    *)
  26. size_of_stack_commit : Object_types.u64;
    (*

    Stack commit size

    *)
  27. size_of_heap_reserve : Object_types.u64;
    (*

    Heap reserve size

    *)
  28. size_of_heap_commit : Object_types.u64;
    (*

    Heap commit size

    *)
  29. loader_flags : Object_types.u32;
    (*

    Loader flags (reserved)

    *)
  30. number_of_rva_and_sizes : Object_types.u32;
    (*

    Number of data directory entries

    *)
  31. data_directory : data_directory;
    (*

    Data directory tables

    *)
}

PE optional header structure containing Windows-specific information

Sourcetype section_header = {
  1. name : string;
    (*

    Section name (up to 8 characters)

    *)
  2. virtual_size : Object_types.u32;
    (*

    Section size in memory

    *)
  3. virtual_address : Object_types.u32;
    (*

    Section RVA when loaded

    *)
  4. size_of_raw_data : Object_types.u32;
    (*

    Section size in file

    *)
  5. pointer_to_raw_data : Object_types.u32;
    (*

    File offset to section data

    *)
  6. pointer_to_relocations : Object_types.u32;
    (*

    File offset to relocations

    *)
  7. pointer_to_line_numbers : Object_types.u32;
    (*

    File offset to line numbers

    *)
  8. number_of_relocations : Object_types.u16;
    (*

    Number of relocation entries

    *)
  9. number_of_line_numbers : Object_types.u16;
    (*

    Number of line number entries

    *)
  10. characteristics : section_characteristics list;
    (*

    Section properties

    *)
}

Section header structure (40 bytes) describing individual sections

Sourcetype string_table

The COFF string table, holding section and symbol names longer than 8 bytes.

Sourceval string_table_get : string_table -> int -> string

string_table_get tbl offset returns the NUL-terminated name at offset, measured from the table start (the first string is at offset 4).

Sourcetype pe_object = {
  1. coff_header : coff_header;
    (*

    COFF file header

    *)
  2. optional_header : optional_header option;
    (*

    PE optional header (absent for COFF object files)

    *)
  3. string_table : string_table option;
    (*

    COFF string table, if the file has a symbol table

    *)
  4. section_headers : section_header array;
    (*

    Array of section headers

    *)
}

Complete PE/COFF object structure containing all parsed components

Sourceval is_pe : Buffer.t -> bool

is_pe buffer returns true if the buffer contains a PE image. Checks for the DOS signature "MZ" and PE signature "PE\0\0".

Sourceval is_coff : Buffer.t -> bool

is_coff buffer returns true for a bare COFF object file (no DOS/PE header) whose first field is a recognised machine type.

read buffer parses a PE image or a bare COFF object file and returns the complete object structure with all headers and sections.

Symbol Table

Sourcetype storage_class =
  1. | IMAGE_SYM_CLASS_NULL
  2. | IMAGE_SYM_CLASS_AUTOMATIC
  3. | IMAGE_SYM_CLASS_EXTERNAL
  4. | IMAGE_SYM_CLASS_STATIC
  5. | IMAGE_SYM_CLASS_REGISTER
  6. | IMAGE_SYM_CLASS_EXTERNAL_DEF
  7. | IMAGE_SYM_CLASS_LABEL
  8. | IMAGE_SYM_CLASS_UNDEFINED_LABEL
  9. | IMAGE_SYM_CLASS_MEMBER_OF_STRUCT
  10. | IMAGE_SYM_CLASS_ARGUMENT
  11. | IMAGE_SYM_CLASS_STRUCT_TAG
  12. | IMAGE_SYM_CLASS_MEMBER_OF_UNION
  13. | IMAGE_SYM_CLASS_UNION_TAG
  14. | IMAGE_SYM_CLASS_TYPE_DEFINITION
  15. | IMAGE_SYM_CLASS_UNDEFINED_STATIC
  16. | IMAGE_SYM_CLASS_ENUM_TAG
  17. | IMAGE_SYM_CLASS_MEMBER_OF_ENUM
  18. | IMAGE_SYM_CLASS_REGISTER_PARAM
  19. | IMAGE_SYM_CLASS_BIT_FIELD
  20. | IMAGE_SYM_CLASS_BLOCK
  21. | IMAGE_SYM_CLASS_FUNCTION
  22. | IMAGE_SYM_CLASS_END_OF_STRUCT
  23. | IMAGE_SYM_CLASS_FILE
  24. | IMAGE_SYM_CLASS_SECTION
  25. | IMAGE_SYM_CLASS_WEAK_EXTERNAL
  26. | IMAGE_SYM_CLASS_CLR_TOKEN
  27. | IMAGE_SYM_CLASS_END_OF_FUNCTION
  28. | IMAGE_SYM_CLASS_UNKNOWN of Object_types.u8

COFF storage class of a symbol.

Sourcetype symbol_base_type =
  1. | IMAGE_SYM_TYPE_NULL
  2. | IMAGE_SYM_TYPE_VOID
  3. | IMAGE_SYM_TYPE_CHAR
  4. | IMAGE_SYM_TYPE_SHORT
  5. | IMAGE_SYM_TYPE_INT
  6. | IMAGE_SYM_TYPE_LONG
  7. | IMAGE_SYM_TYPE_FLOAT
  8. | IMAGE_SYM_TYPE_DOUBLE
  9. | IMAGE_SYM_TYPE_STRUCT
  10. | IMAGE_SYM_TYPE_UNION
  11. | IMAGE_SYM_TYPE_ENUM
  12. | IMAGE_SYM_TYPE_MOE
  13. | IMAGE_SYM_TYPE_BYTE
  14. | IMAGE_SYM_TYPE_WORD
  15. | IMAGE_SYM_TYPE_UINT
  16. | IMAGE_SYM_TYPE_DWORD
  17. | IMAGE_SYM_TYPE_UNKNOWN of int

Base type of a symbol (low nibble of the Type field).

Sourcetype symbol_complex_type =
  1. | IMAGE_SYM_DTYPE_NULL
  2. | IMAGE_SYM_DTYPE_POINTER
  3. | IMAGE_SYM_DTYPE_FUNCTION
  4. | IMAGE_SYM_DTYPE_ARRAY
  5. | IMAGE_SYM_DTYPE_UNKNOWN of int

Derived ("complex") type of a symbol (next nibble of the Type field).

Sourcetype symbol_type = {
  1. base_type : symbol_base_type;
  2. complex_type : symbol_complex_type;
    (*

    IMAGE_SYM_DTYPE_FUNCTION = function

    *)
}

A decoded symbol Type field.

Sourcetype aux =
  1. | Aux_file of string
    (*

    .file symbol: the source file name

    *)
  2. | Aux_function_definition of {
    1. tag_index : Object_types.u32;
    2. total_size : Object_types.u32;
    3. pointer_to_line_number : Object_types.u32;
    4. pointer_to_next_function : Object_types.u32;
    }
  3. | Aux_function_boundary of {
    1. line_number : int;
    2. pointer_to_next_function : Object_types.u32;
    }
    (*

    .bf/.ef line-number records

    *)
  4. | Aux_weak_external of {
    1. tag_index : Object_types.u32;
    2. characteristics : Object_types.u32;
    }
  5. | Aux_section_definition of {
    1. length : Object_types.u32;
    2. number_of_relocations : int;
    3. number_of_line_numbers : int;
    4. checksum : Object_types.u32;
    5. number : int;
    6. selection : int;
    }
  6. | Aux_unknown of string
    (*

    Raw 18 bytes for an undecoded record

    *)

A decoded auxiliary symbol record, interpreted from the storage class of the primary symbol it follows.

Sourcetype symbol = {
  1. sym_name : string;
    (*

    Symbol name, resolved from the string table if longer than 8 bytes

    *)
  2. sym_value : Object_types.u32;
    (*

    Symbol value; meaning depends on section and class

    *)
  3. sym_section_number : int;
    (*

    1-based section index, or 0 (undefined), -1 (absolute), -2 (debug)

    *)
  4. sym_type : symbol_type;
    (*

    Decoded symbol type

    *)
  5. sym_storage_class : storage_class;
    (*

    Storage class

    *)
  6. sym_number_of_aux_symbols : Object_types.u8;
    (*

    Count of raw auxiliary records following this one

    *)
  7. sym_aux : aux list;
    (*

    Decoded auxiliary records

    *)
}

A primary COFF symbol-table record.

Sourceval read_symbols : Buffer.t -> pe_object -> symbol array

read_symbols buf pe_obj reads the COFF symbol table, returning the primary records with their auxiliary records decoded. Empty when there is no symbol table.

Relocations

Sourcetype amd64_relocation =
  1. | IMAGE_REL_AMD64_ABSOLUTE
  2. | IMAGE_REL_AMD64_ADDR64
  3. | IMAGE_REL_AMD64_ADDR32
  4. | IMAGE_REL_AMD64_ADDR32NB
  5. | IMAGE_REL_AMD64_REL32
  6. | IMAGE_REL_AMD64_REL32_1
  7. | IMAGE_REL_AMD64_REL32_2
  8. | IMAGE_REL_AMD64_REL32_3
  9. | IMAGE_REL_AMD64_REL32_4
  10. | IMAGE_REL_AMD64_REL32_5
  11. | IMAGE_REL_AMD64_SECTION
  12. | IMAGE_REL_AMD64_SECREL
  13. | IMAGE_REL_AMD64_SECREL7
  14. | IMAGE_REL_AMD64_TOKEN
  15. | IMAGE_REL_AMD64_SREL32
  16. | IMAGE_REL_AMD64_PAIR
  17. | IMAGE_REL_AMD64_SSPAN32
  18. | IMAGE_REL_AMD64_UNKNOWN of Object_types.u16

AMD64 (x86-64) relocation type.

Sourcetype i386_relocation =
  1. | IMAGE_REL_I386_ABSOLUTE
  2. | IMAGE_REL_I386_DIR16
  3. | IMAGE_REL_I386_REL16
  4. | IMAGE_REL_I386_DIR32
  5. | IMAGE_REL_I386_DIR32NB
  6. | IMAGE_REL_I386_SEG12
  7. | IMAGE_REL_I386_SECTION
  8. | IMAGE_REL_I386_SECREL
  9. | IMAGE_REL_I386_TOKEN
  10. | IMAGE_REL_I386_SECREL7
  11. | IMAGE_REL_I386_REL32
  12. | IMAGE_REL_I386_UNKNOWN of Object_types.u16

i386 (x86) relocation type.

Sourcetype arm64_relocation =
  1. | IMAGE_REL_ARM64_ABSOLUTE
  2. | IMAGE_REL_ARM64_ADDR32
  3. | IMAGE_REL_ARM64_ADDR32NB
  4. | IMAGE_REL_ARM64_BRANCH26
  5. | IMAGE_REL_ARM64_PAGEBASE_REL21
  6. | IMAGE_REL_ARM64_REL21
  7. | IMAGE_REL_ARM64_PAGEOFFSET_12A
  8. | IMAGE_REL_ARM64_PAGEOFFSET_12L
  9. | IMAGE_REL_ARM64_SECREL
  10. | IMAGE_REL_ARM64_SECREL_LOW12A
  11. | IMAGE_REL_ARM64_SECREL_HIGH12A
  12. | IMAGE_REL_ARM64_SECREL_LOW12L
  13. | IMAGE_REL_ARM64_TOKEN
  14. | IMAGE_REL_ARM64_SECTION
  15. | IMAGE_REL_ARM64_ADDR64
  16. | IMAGE_REL_ARM64_BRANCH19
  17. | IMAGE_REL_ARM64_BRANCH14
  18. | IMAGE_REL_ARM64_REL32
  19. | IMAGE_REL_ARM64_UNKNOWN of Object_types.u16

ARM64 relocation type.

Sourcetype relocation_type =
  1. | Reloc_amd64 of amd64_relocation
  2. | Reloc_i386 of i386_relocation
  3. | Reloc_arm64 of arm64_relocation
  4. | Reloc_other of Object_types.u16
    (*

    A machine without a decoder for its relocations

    *)

A relocation type, decoded according to the COFF machine.

Sourcetype relocation = {
  1. reloc_virtual_address : Object_types.u32;
    (*

    Section offset of the item to fix up

    *)
  2. reloc_symbol_table_index : Object_types.u32;
    (*

    Index of the referenced symbol

    *)
  3. reloc_type : relocation_type;
}

A COFF relocation record.

Sourceval read_relocations : Buffer.t -> pe_object -> section_header -> relocation array

read_relocations buf pe_obj section reads section's relocation table, decoding the relocation type for the file's machine. Empty when the section has no relocations.

Exports

Sourcetype export = {
  1. export_ordinal : int;
    (*

    Biased ordinal (ordinal_base + index)

    *)
  2. export_name : string option;
    (*

    Export name, or None for ordinal-only exports

    *)
  3. export_address_rva : Object_types.u32;
    (*

    RVA of the export, or of a forwarder string

    *)
}

An entry from a PE image's export table.

Sourcetype export_directory = {
  1. dll_name : string;
    (*

    The exporting image's own name

    *)
  2. export_time_date_stamp : Object_types.u32;
  3. export_major_version : Object_types.u16;
  4. export_minor_version : Object_types.u16;
  5. ordinal_base : int;
    (*

    Ordinal of the first export-address-table entry

    *)
  6. exports : export array;
}

A PE image's export directory.

Sourceval read_exports : Buffer.t -> pe_object -> export_directory option

read_exports buf pe_obj reads the image's export directory (the IMAGE_DIRECTORY_ENTRY_EXPORT data directory), or None when the image exports nothing.

Imports

Sourcetype imported_symbol =
  1. | Import_by_name of {
    1. hint : int;
    2. name : string;
    }
  2. | Import_by_ordinal of int

A symbol imported from a DLL, either by name (with a hint) or by ordinal.

Sourcetype import_descriptor = {
  1. import_dll_name : string;
    (*

    Name of the imported DLL

    *)
  2. imports : imported_symbol list;
    (*

    Symbols imported from it

    *)
}

The imports from a single DLL.

Sourceval read_imports : Buffer.t -> pe_object -> import_descriptor array

read_imports buf pe_obj reads the image's import table (the IMAGE_DIRECTORY_ENTRY_IMPORT data directory), one descriptor per imported DLL. Empty when the image imports nothing.

Sourceval machine_to_arch : machine_type -> [ `X86 | `X86_64 | `ARM | `ARM64 | `Unknown of int ]

machine_to_arch machine maps a COFF machine type to a generic architecture. Access section headers directly via pe_obj.section_headers.

Sourceval machine_type_to_string : machine_type -> string

machine_type_to_string machine renders a short architecture name for the COFF machine type, e.g. "amd64" or "riscv64".

Sourceval is_64bit : pe_object -> bool

is_64bit pe_obj returns true if the PE file is 64-bit (PE32+).

Sourceval is_executable : pe_object -> bool

is_executable pe_obj returns true if the PE file is executable.

Sourceval entry_point : pe_object -> Object_types.u64 option

entry_point pe_obj returns the entry point virtual address (image_base plus the entry-point RVA) if available.

Sourceval section_characteristics_to_type_string : section_characteristics list -> string

section_characteristics_to_type_string characteristics converts section characteristics to a human-readable type string.

Section Characteristic Utilities

Sourceval section_has_characteristic : section_header -> section_characteristics -> bool

section_has_characteristic section_header characteristic returns true if the section has the specified characteristic flag.

Sourceval section_is_executable : section_header -> bool

section_is_executable section_header returns true if the section can be executed as code (has IMAGE_SCN_MEM_EXECUTE flag).

Sourceval section_is_writable : section_header -> bool

section_is_writable section_header returns true if the section can be written to (has IMAGE_SCN_MEM_WRITE flag).

Sourceval section_is_readable : section_header -> bool

section_is_readable section_header returns true if the section can be read (has IMAGE_SCN_MEM_READ flag).

Sourceval section_contains_code : section_header -> bool

section_contains_code section_header returns true if the section contains executable code (has IMAGE_SCN_CNT_CODE flag).

Sourceval section_contains_data : section_header -> bool

section_contains_data section_header returns true if the section contains initialised or uninitialised data.

Sourceval section_is_discardable : section_header -> bool

section_is_discardable section_header returns true if the section can be discarded as needed (has IMAGE_SCN_MEM_DISCARDABLE flag).

Sourceval section_contents : Buffer.t -> section_header -> Buffer.t

section_contents buf section returns the raw data of section in buf. Raises Buffer.Invalid_format if it extends past the end of buf.

Debug Directory

The debug directory is found via the IMAGE_DIRECTORY_ENTRY_DEBUG data directory entry. It provides information about embedded debug data and paths to external PDB files.

Sourcetype debug_type =
  1. | IMAGE_DEBUG_TYPE_UNKNOWN
  2. | IMAGE_DEBUG_TYPE_COFF
  3. | IMAGE_DEBUG_TYPE_CODEVIEW
  4. | IMAGE_DEBUG_TYPE_FPO
  5. | IMAGE_DEBUG_TYPE_MISC
  6. | IMAGE_DEBUG_TYPE_EXCEPTION
  7. | IMAGE_DEBUG_TYPE_FIXUP
  8. | IMAGE_DEBUG_TYPE_BORLAND
  9. | IMAGE_DEBUG_TYPE_REPRO
  10. | IMAGE_DEBUG_TYPE_POGO
  11. | IMAGE_DEBUG_TYPE_ILTCG
  12. | IMAGE_DEBUG_TYPE_MPX
  13. | IMAGE_DEBUG_TYPE_OTHER of int
Sourcetype debug_directory_entry = {
  1. characteristics : Object_types.u32;
  2. time_date_stamp : Object_types.u32;
  3. major_version : Object_types.u16;
  4. minor_version : Object_types.u16;
  5. debug_type : debug_type;
  6. size_of_data : Object_types.u32;
  7. address_of_raw_data : Object_types.u32;
  8. pointer_to_raw_data : Object_types.u32;
}
Sourcetype codeview_info = {
  1. cv_signature : string;
  2. guid : string;
  3. age : Object_types.u32;
  4. pdb_path : string;
}
Sourceval parse_debug_directory : Buffer.t -> pe_object -> debug_directory_entry list

parse_debug_directory buf pe_obj parses the debug directory entries from the PE file.

Sourceval parse_codeview_info : Buffer.t -> debug_directory_entry -> codeview_info option

parse_codeview_info buf entry parses a CodeView (RSDS) debug entry to extract the PDB GUID, age, and file path.

Sourceval find_pdb_path : Buffer.t -> pe_object -> string option

find_pdb_path buf pe_obj finds the PDB file path from a PE file's CodeView debug directory entry, if present.

Exception Data (.pdata / .xdata)

On Windows x86-64, stack unwinding uses structured exception handling (SEH) rather than DWARF .eh_frame. The .pdata section contains a sorted array of RUNTIME_FUNCTION entries (one per non-leaf function), each pointing to an UNWIND_INFO structure in the .xdata section.

These are the Windows equivalent of DWARF CFI (Call Frame Information). The assembler directives .seh_proc, .seh_pushreg, .seh_stackalloc, .seh_setframe, .seh_endprologue, .seh_endproc generate these structures.

.pdata: Runtime Function Table

Sourcetype runtime_function = {
  1. begin_address : Object_types.u32;
    (*

    Image-relative RVA of function start

    *)
  2. end_address : Object_types.u32;
    (*

    Image-relative RVA of function end

    *)
  3. unwind_info_address : Object_types.u32;
    (*

    Image-relative RVA of UNWIND_INFO in .xdata

    *)
}

A RUNTIME_FUNCTION entry from the .pdata section.

Sourceval parse_pdata : Buffer.t -> section_header -> runtime_function array

parse_pdata buf pdata_section parses the .pdata section into an array of RUNTIME_FUNCTION entries.

Sourceval write_pdata : Buffer.t -> runtime_function list -> unit

write_pdata buf entries serialises RUNTIME_FUNCTION entries into .pdata format.