Module Tolk.Elf

Relocatable ELF object loading.

Parses 64-bit little-endian ELF relocatable objects (ET_REL) and lays out their allocatable sections into a contiguous flat image. Section and relocation metadata is preserved for backend-specific loaders, but no machine-specific relocations are applied.

Types

type section = private {
  1. name : string;
    (*

    The ELF section name (e.g. ".text", ".data", ".bss").

    *)
  2. addr : int;
    (*

    Byte offset of the section within the flat image.

    *)
  3. size : int;
    (*

    Size of the section in bytes.

    *)
  4. content : Stdlib.Bytes.t;
    (*

    Section contents. For SHT_NOBITS sections (e.g. .bss), content is a zero-filled buffer of length size.

    *)
}

The type for sections after image layout.

type symbol = private {
  1. name : string;
    (*

    The symbol name from the string table.

    *)
  2. shndx : int;
    (*

    Section header index the symbol belongs to. 0 for undefined symbols.

    *)
  3. value : int;
    (*

    Symbol value: byte offset from the start of the symbol's section.

    *)
}

The type for symbols from the object's symbol table.

type reloc = private {
  1. offset : int;
    (*

    Absolute byte offset within the flat image where the relocation applies.

    *)
  2. symbol : symbol;
    (*

    The referenced symbol.

    *)
  3. r_type : int;
    (*

    Machine-specific relocation type (e.g. R_AARCH64_CALL26, R_X86_64_PC32).

    *)
  4. addend : int;
    (*

    Relocation addend. 0 for SHT_REL entries.

    *)
}

The type for relocations anchored at absolute image offsets.

type t

The type for a laid-out relocatable ELF object. Holds the flat image, resolved section addresses, symbols, and pending relocations.

Loading

val load : ?force_section_align:int -> Stdlib.Bytes.t -> t

load ?force_section_align obj parses ELF relocatable object obj and lays out its allocatable sections into a flat image.

Sections with a fixed address (sh_addr <> 0) are placed first. Remaining allocatable sections are appended sequentially, each aligned to the maximum of the ELF section alignment and force_section_align (defaults to 1).

Raises Invalid_argument if obj is not a valid 64-bit little-endian ELF relocatable object.

Accessors

val image : t -> Stdlib.Bytes.t

image t is the flat image built from allocatable sections.

val sections : t -> section array

sections t is all object sections in section-header order, with addr set to their final image offsets.

val relocs : t -> reloc list

relocs t is the list of relocations with offsets resolved to absolute image positions.

Lookup

val find_section : t -> string -> section option

find_section t name is the section named name, if any.

val find_symbol_offset : t -> string -> int

find_symbol_offset t name is the absolute byte offset in image of the defined symbol name.

Raises Invalid_argument if no symbol named name exists or if the symbol is undefined.