Page
Library
Module
Module type
Parameter
Class
Class type
Source
ElfSourceLinux ELF parser based on /usr/include/elf.h
ELF file class indicating the architecture word size. Determines the size of addresses and offsets throughout the ELF file format.
type elf_data = [ | `ELFDATANONEInvalid data encoding
*)| `ELFDATA2LSB2's complement, little endian
*)| `ELFDATA2MSB2's complement, big endian
*) ]ELF data encoding specifying the byte order used for multi-byte values within the ELF file. Affects interpretation of all integers and addresses in the file.
type elf_osabi = [ | `ELFOSABI_NONEUNIX System V ABI
*)| `ELFOSABI_SYSVAlias for ELFOSABI_NONE
*)| `ELFOSABI_HPUXHP-UX
*)| `ELFOSABI_NETBSDNetBSD
*)| `ELFOSABI_GNUObject uses GNU ELF extensions
*)| `ELFOSABI_LINUXCompatibility alias for GNU
*)| `ELFOSABI_SOLARISSun Solaris
*)| `ELFOSABI_AIXIBM AIX
*)| `ELFOSABI_IRIXSGI Irix
*)| `ELFOSABI_FREEBSDFreeBSD
*)| `ELFOSABI_TRU64Compaq TRU64 UNIX
*)| `ELFOSABI_MODESTONovell Modesto
*)| `ELFOSABI_OPENBSDOpenBSD
*)| `ELFOSABI_ARM_AEABIARM EABI
*)| `ELFOSABI_ARMARM
*)| `ELFOSABI_STANDALONEStandalone (embedded) application
*)| `ELFOSABI_UNKNOWN of intUnknown OS ABI
*) ]ELF OS/ABI identification specifying the target operating system and ABI. Indicates which OS-specific extensions and conventions are used in the ELF file.
type elf_type = [ | `ET_NONENo file type
*)| `ET_RELRelocatable file
*)| `ET_EXECExecutable file
*)| `ET_DYNShared object file
*)| `ET_CORECore file
*)| `ET_LOOSOS-specific range start
*)| `ET_HIOSOS-specific range end
*)| `ET_LOPROCProcessor-specific range start
*)| `ET_HIPROCProcessor-specific range end
*)| `ET_UNKNOWN of intUnknown file type
*) ]ELF file type indicating the purpose and format of the ELF file. Determines how the file should be processed - whether it's an intermediate object file for linking, a complete executable, a shared library, or a core dump.
type elf_machine = [ | `EM_NONENo machine
*)| `EM_M32AT&T WE 32100
*)| `EM_SPARCSUN SPARC
*)| `EM_386Intel 80386
*)| `EM_68KMotorola m68k family
*)| `EM_88KMotorola m88k family
*)| `EM_860Intel 80860
*)| `EM_MIPSMIPS R3000 big-endian
*)| `EM_S370IBM System/370
*)| `EM_MIPS_RS3_LEMIPS R3000 little-endian
*)| `EM_PARISCHPPA
*)| `EM_VPP500Fujitsu VPP500
*)| `EM_SPARC32PLUSSun's "v8plus"
*)| `EM_960Intel 80960
*)| `EM_PPCPowerPC
*)| `EM_PPC64PowerPC 64-bit
*)| `EM_S390IBM S390
*)| `EM_ARMARM
*)| `EM_SHHitachi SH
*)| `EM_SPARCV9SPARC v9 64-bit
*)| `EM_IA_64Intel Merced
*)| `EM_X86_64AMD x86-64 architecture
*)| `EM_AARCH64ARM AARCH64
*)| `EM_RISCV| `EM_UNKNOWN of intUnknown machine type
*) ]ELF target machine architecture specification. Indicates the required architecture for the ELF file, determining instruction set compatibility and execution requirements. This field ensures that the file can only be executed on systems with compatible processor architectures.
type identification = {elf_class : elf_class;Object file class (32-bit, 64-bit, etc.)
*)elf_data : elf_data;Data encoding (little-endian, big-endian)
*)elf_version : Object_types.u8;ELF header version (must be 1)
*)elf_osabi : elf_osabi;Operating system/ABI identification
*)elf_abiversion : Object_types.u8;ABI version
*)}ELF identification structure from the e_ident field. Contains basic file format information including architecture class, byte order, and target operating system ABI. This is parsed from the first 16 bytes of an ELF file.
type header = {e_ident : identification;ELF identification and magic number
*)e_type : elf_type;Object file type (executable, shared lib, relocatable, core)
*)e_machine : elf_machine;Target architecture (e.g., EM_X86_64 for x64)
*)e_version : Object_types.u32;ELF version (must be 1)
*)e_entry : Object_types.u64;Program entry point virtual address
*)e_phoff : Object_types.u64;Program header table file offset
*)e_shoff : Object_types.u64;Section header table file offset
*)e_flags : Object_types.u32;Processor-specific flags
*)e_ehsize : Object_types.u16;ELF header size in bytes
*)e_phentsize : Object_types.u16;Size of a program header table entry
*)e_phnum : Object_types.u16;Number of entries in program header table
*)e_shentsize : Object_types.u16;Size of a section header table entry
*)e_shnum : Object_types.u16;Number of entries in section header table
*)e_shstrndx : Object_types.u16;Section header string table index
*)}ELF header structure containing essential file metadata. This is the first structure in an ELF file and provides information needed to interpret the rest of the file, including table locations, entry points, and architecture details.
type section_type = [ | `SHT_NULLSection header table entry is unused.
*)| `SHT_PROGBITSProgram data.
*)| `SHT_SYMTABSymbol table.
*)| `SHT_STRTABString table.
*)| `SHT_RELARelocation entries with explicit addends.
*)| `SHT_HASHSymbol hash table.
*)| `SHT_DYNAMICDynamic linking information.
*)| `SHT_NOTENotes.
*)| `SHT_NOBITSProgram space with no data (bss).
*)| `SHT_RELRelocation entries without explicit addends.
*)| `SHT_SHLIBReserved section type.
*)| `SHT_DYNSYMDynamic linker symbol table.
*)| `SHT_INIT_ARRAYArray of constructors.
*)| `SHT_FINI_ARRAYArray of destructors.
*)| `SHT_PREINIT_ARRAYArray of pre-constructors.
*)| `SHT_GROUPSection group.
*)| `SHT_SYMTAB_SHNDXExtended section indices for a symbol table.
*)| `SHT_RELRRelocation entries; only offsets.
*)| `SHT_LOOSStart of OS-specific section types.
*)| `SHT_LLVM_DEPENDENT_LIBRARIESLLVM-style dependent libraries.
*)| `SHT_GNU_ATTRIBUTESObject attributes
*)| `SHT_GNU_HASHGNU-style hash table.
*)| `SHT_GNU_LIBLISTPrelink library list.
*)| `SHT_CHECKSUMChecksum for DSO content.
*)| `SHT_LOSUNWSun-specific low bound.
*)| `SHT_SUNW_move| `SHT_SUNW_COMDAT| `SHT_SUNW_syminfo| `SHT_GNU_VERDEFVersion definition section.
*)| `SHT_GNU_VERNEEDVersion needs section.
*)| `SHT_GNU_VERSYMVersion symbol table.
*)| `SHT_HISUNWSun-specific high bound.
*)| `SHT_HIOSEnd of OS-specific section types.
*)| `SHT_LOPROCStart of processor-specific section types.
*)| `SHT_HIPROCEnd of processor-specific section types.
*)| `SHT_LOUSERStart of application-specific section types.
*)| `SHT_HIUSEREnd of application-specific section types.
*) ]ELF section type classification. Identifies the specific purpose and format of section contents, enabling proper interpretation by linkers, loaders, and debuggers. Different section types require different processing during linking and program loading.
type section_flags = [ | `SHF_WRITEWritable
*)| `SHF_ALLOCOccupies memory during execution
*)| `SHF_EXECINSTRExecutable
*)| `SHF_MERGEMight be merged
*)| `SHF_STRINGSContains nul-terminated strings
*)| `SHF_INFO_LINK`sh_info' contains SHT index
*)| `SHF_LINK_ORDERPreserve order after combining
*)| `SHF_OS_NONCONFORMINGNon-standard OS specific handling required
*)| `SHF_GROUPSection is member of a group.
*)| `SHF_TLSSection hold thread-local data.
*)| `SHF_COMPRESSEDSection with compressed data.
*)| `SHF_MASKOSOS-specific.
*)| `SHF_MASKPROCProcessor-specific
*)| `SHF_ORDEREDSpecial ordering requirement (Solaris).
*)| `SHF_EXCLUDESection is excluded unless referenced or allocated (Solaris).
*) ]Values for section.sh_flags.
type section = {sh_name : Object_types.u32;Section name as string table index
*)sh_name_str : string;Section name as resolved string
*)sh_type : Object_types.u32;Section type (code, data, symbol table, etc.)
*)sh_flags : Object_types.u64;Section attributes (writable, executable, allocatable, etc.)
*)sh_addr : Object_types.u64;Virtual address where section should be loaded
*)sh_offset : Object_types.u64;File offset to section data
*)sh_size : Object_types.u64;Section size in bytes
*)sh_link : Object_types.u32;Index of associated section (type-dependent)
*)sh_info : Object_types.u32;Additional section information (type-dependent)
*)sh_addralign : Object_types.u64;Section alignment constraint
*)sh_entsize : Object_types.u64;Size of entries if section contains a table
*)}ELF section header describing a section within the file. Sections provide fine-grained organization of the file content for linking, debugging, and analysis. Each section has a specific type and set of attributes that determine how it should be processed.
type program_type = [ | `PT_NULLProgram header table entry is unused.
*)| `PT_LOADSpecifies a segment to load into memory at the given file address.
*)| `PT_DYNAMICDynamic linking information.
*)| `PT_INTERPSpecifies the location and size of the path to the dynamic linker.
*)| `PT_NOTEThe location and size of auxiliary information about the binary.
*)| `PT_SHLIBReserved
*)| `PT_PHDRLocation to load the program headers
*)| `PT_TLSThread-local storage.
*)| `PT_NUMNumber of defined types
*)| `PT_LOOSStart of OS-specific
*)| `PT_GNU_EH_FRAMEThe stack unwinding information (points to the same memory as .eh_frame section).
*)| `PT_GNU_STACKIndicates stack executability, specific to the Linux kernel
*)| `PT_GNU_RELRORead-only after relocation.
*)| `PT_GNU_PROPERTYSpecial note with dynamic linker specific information (.note.gnu.property)
*)| `PT_GNU_SFRAMESFrame segment.
*)| `PT_LOSUNW| `PT_SUNWBSSSun Specific segment
*)| `PT_SUNWSTACKStack segment
*)| `PT_HISUNW| `PT_HIOSEnd of OS-specific
*)| `PT_LOPROCStart of processor-specific
*)| `PT_HIPROCEnd of processor-specific
*) ]Legal values for p_type field of program (segment type).
type program_flags = [ | `PF_XSegment is executable
*)| `PF_WSegment is writable
*)| `PF_RSegment is readable
*)| `PF_RXSegment is readable and executable
*)| `PF_RWSegment is readable and writable
*)| `PF_WXSegment is writable and executable
*)| `PF_RWXSegment is readable, writable and executable
*)| `PF_MASKOSOS-specific
*)| `PF_MASKPROCProcessor-specific
*) ]ELF program segment permission flags. Defines access permissions for segments in memory during program execution. These flags control memory protection by specifying whether a segment can be read from, written to, or executed, enabling proper memory management and security.
type program = {p_type : program_type;Segment type
*)p_flags : program_flags;Segment flags
*)p_offset : Object_types.u64;Segment file offset
*)p_vaddr : Object_types.u64;Segment virtual address
*)p_paddr : Object_types.u64;Segment physical address
*)p_filesz : Object_types.u64;Segment size in file
*)p_memsz : Object_types.u64;Segment size in memory
*)p_align : Object_types.u64;Segment alignment
*)}Program headers describe the segments of the program relevant to program loading.
The auxiliary vector (auxv) is a mechanism used by the Linux kernel and other Unix-like systems to pass system and program-specific information to user programs at startup. It consists of an array of key-value pairs containing essential data that programs and dynamic linkers need to function properly.
The auxiliary vector serves several critical purposes:
Common auxiliary vector entries include:
AT_PAGESZ: System memory page size (typically 4096 bytes)AT_PHDR: Memory address where program headers are loadedAT_ENTRY: Program entry point addressAT_BASE: Base address of the dynamic linker/interpreterAT_UID/AT_GID: Real user and group IDs for security contextThe auxiliary vector can be accessed from two sources:
/proc/<pid>/auxv for any running processThe exact memory location is platform-specific; consult the platform ELF ABI documents for details. See: https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
The auxiliary vector is essential for program interpreters and dynamic linkers (such as /lib/ld-linux-x86-64.so.2) to bootstrap the dynamic linking process without requiring additional system calls to examine executable files.
type entry_type = [ | `AT_NULLEnd of vector
*)| `AT_IGNOREEntry should be ignored
*)| `AT_EXECFDFile descriptor of program
*)| `AT_PHDRProgram headers for program
*)| `AT_PHENTSize of program header entry
*)| `AT_PHNUMNumber of program headers
*)| `AT_PAGESZSystem page size
*)| `AT_BASEBase address of interpreter
*)| `AT_FLAGSFlags
*)| `AT_ENTRYEntry point of program
*)| `AT_NOTELFProgram is not ELF
*)| `AT_UIDReal uid
*)| `AT_EUIDEffective uid
*)| `AT_GIDReal gid
*)| `AT_EGIDEffective gid
*)| `AT_CLKTCKFrequency of times()
*) ]Auxiliary vector entry type identifiers. These constants define the different types of information that can be passed from the kernel to userspace programs through the auxiliary vector, including system parameters, program metadata, and security contexts.
type auxiliary_vector = {a_type : entry_type;Entry type identifying what information this entry contains
*)a_val : Object_types.u64;Value associated with the entry type
*)}Auxiliary vector entry containing system information passed from kernel to user programs at startup. Each entry consists of a type identifier and an associated value (address, size, flags, etc.).
read_elf buffer decodes the header and section table from a buffer pointing to an ELF image.
ELF files organize their content using two overlapping but distinct structures: sections and segments. Understanding their relationship is crucial for working with ELF files effectively.
Sections are used during linking and debugging:
Segments are used during program loading:
ELF File Structure: +------------------+ | ELF Header | <- Points to both header tables +------------------+ | Program Headers | <- Segments (for loading) +------------------+ | | | File Content | <- Actual data/code | | +------------------+ | Section Headers | <- Sections (for linking/debug) +------------------+ Segment-to-Section Mapping: +----------------+ +------------------+ | LOAD |---->| .text (code) | | Segment 1 | | .rodata (const) | +----------------+ +------------------+ | LOAD |---->| .data (init var) | | Segment 2 | | .bss (uninit var)| +----------------+ +------------------+ | DYNAMIC |---->| .dynamic | +----------------+ +------------------+
.text: Executable code.rodata: Read-only data (string literals, constants).data: Initialized global and static variables.bss: Uninitialized global and static variables.symtab: Static symbol table (for debugging).dynsym: Dynamic symbol table (for runtime linking).strtab/.dynstr: String tables for symbol names.rel/.rela: Relocation information.dynamic: Dynamic linking informationLOAD: Segments to be loaded into memory (typically .text, .data)DYNAMIC: Dynamic linking informationINTERP: Path to program interpreter (dynamic linker)NOTE: Auxiliary information (build ID, ABI notes)GNU_STACK: Stack permissions and propertiesThe dual organization serves different phases of a program's lifecycle:
read_programs buffer header decodes the program headers from a buffer pointing to an ELF image.
read_auxiliary_vector buffer decodes the auxiliary vector from buffer.
ELF files contain standardized symbol tables as defined by the ELF specification and System V Application Binary Interface (ABI):
read_symbol_table with ~symtab_name:".symtab".strtabread_symbol_table with ~symtab_name:".dynsym".dynstr(* Read default symbol table (tries .symtab first, falls back to .dynsym) *)
let symbols = read_symbol_table buffer header sections
(* Read static symbols explicitly *)
let static_symbols =
read_symbol_table ~symtab_name:".symtab" buffer header sections
(* Read dynamic symbols explicitly *)
let dynamic_symbols =
read_symbol_table ~symtab_name:".dynsym" buffer header sectionsELF symbol table entry
type symbol_binding = [ | `STB_LOCALLocal symbol
*)| `STB_GLOBALGlobal symbol
*)| `STB_WEAKWeak symbol
*)| `STB_LOOSStart of OS-specific binding
*)| `STB_HIOSEnd of OS-specific binding
*)| `STB_LOPROCStart of processor-specific binding
*)| `STB_HIPROCEnd of processor-specific binding
*)| `STB_UNKNOWN of intUnknown binding
*) ]ELF symbol binding attributes. Defines the linkage visibility and behavior of symbols in the symbol table, controlling how symbols are resolved during linking. This determines whether symbols are local to the file, globally visible across modules, or have weak linkage semantics.
type symbol_type = [ | `STT_NOTYPESymbol type is unspecified
*)| `STT_OBJECTSymbol is a data object
*)| `STT_FUNCSymbol is a code object
*)| `STT_SECTIONSymbol associated with a section
*)| `STT_FILESymbol's name is file name
*)| `STT_COMMONSymbol is a common data object
*)| `STT_TLSSymbol is thread-local data object
*)| `STT_LOOSStart of OS-specific symbol types
*)| `STT_HIOSEnd of OS-specific symbol types
*)| `STT_LOPROCStart of processor-specific symbol types
*)| `STT_HIPROCEnd of processor-specific symbol types
*)| `STT_UNKNOWN of intUnknown type
*) ]ELF symbol type classification. Categorizes symbols by their nature and intended use, distinguishing between data objects, functions, sections, and special symbol types. This classification helps linkers and debuggers understand how to process and resolve different kinds of symbols.
type symbol_visibility = [ | `STV_DEFAULTDefault visibility
*)| `STV_INTERNALProcessor specific hidden class
*)| `STV_HIDDENSymbol unavailable to other modules
*)| `STV_PROTECTEDNot preemptible, not exported
*)| `STV_UNKNOWN of intUnknown visibility
*) ]ELF symbol visibility attributes. Controls the visibility and preemption behavior of symbols during dynamic linking. This determines how symbols are exposed to other modules and whether they can be intercepted or overridden by definitions in other shared libraries.
type symbol = {st_name : Object_types.u32;Symbol name string table index
*)st_name_str : string;Symbol name
*)st_info : Object_types.u8;Symbol binding and type
*)st_other : Object_types.u8;Symbol visibility
*)st_shndx : Object_types.u16;Section index
*)st_value : Object_types.u64;Symbol value
*)st_size : Object_types.u64;Symbol size
*)st_binding : symbol_binding;Symbol binding
*)st_type : symbol_type;Symbol type
*)st_visibility : symbol_visibility;Symbol visibility
*)}ELF symbol table entry. Represents a single symbol in the symbol table, containing all information needed to identify, locate, and link to the symbol. This includes both the raw ELF fields and parsed representations of binding, type, and visibility attributes.
val read_symbol_table :
?symtab_name:string ->
Buffer.t ->
header ->
section array ->
symbol arrayread_symbol_table ?symtab_name buffer header sections reads symbol table from ELF file.
Raises Failure if the specified symbol table or its corresponding string table is not found.
read_section_contents buf section_name reads the section contents for section_name. Returns None if the section name is not found