gen-elf
gen-elf is a utility for generating ELF files (Shared Objects and Relocatable Objects) specifically designed for testing ELF loaders. It simplifies the process of creating binaries with specific symbol structures, relocation entries, and memory layouts for elf_loader verification.
Features
- Multi-architecture Support: Supports x86_64, x86, Aarch64, Riscv64, Riscv32, Arm, and Loongarch64.
- Dynamic Library Generation: Generates Shared Objects (.so) with
.dynamicsections, relocation tables (RELA/REL), symbol tables, and GOT/PLT. - TLS and IFUNC Support: Easily generate Thread-Local Storage (TLS) symbols and Indirect Functions (IFUNC) with automatic resolver generation.
- Relocatable Object Generation: Generates standard relocatable object files (.o).
- Customizable Layout: Configure base address and page size for memory mapping tests.
- Metadata Export: Exports detailed relocation information and section addresses alongside the ELF data for easy verification in tests.
Core Interfaces
DylibWriter
Used for generating dynamic libraries. You can customize the generation using ElfWriterConfig.
use ;
let arch = current;
let config = default
.with_base_addr
.with_page_size;
let writer = with_config;
let relocs = vec!;
let symbols = vec!;
let output = writer.write?;
println!;
ObjectWriter
Used for generating relocatable object files.
use ;
let arch = X86_64;
let writer = new;
let symbols = vec!;
// Symbols and Relocs
writer.write_file?;
RelocEntry
Common relocation types are available as high-level methods:
RelocEntry::jump_slot(name, arch): PLT-based functions.RelocEntry::glob_dat(name, arch): Global variable references.RelocEntry::relative(arch): Base-relative relocations.RelocEntry::irelative(arch): Indirect (IFUNC) relocations.RelocEntry::copy(name, arch): Copy relocations for variables.RelocEntry::dtpmod(name, arch)/RelocEntry::dtpoff(name, arch): TLS-related relocations.
SymbolDesc
Describe symbols with various scopes and types:
SymbolDesc::global_func(name, code): A global function.SymbolDesc::global_object(name, data): A global variable.SymbolDesc::global_tls(name, data): A thread-local variable.SymbolDesc::undefined_func(name): Reference to an external function..with_scope(SymbolScope::Weak): Mark a symbol as weak.
Usage in Tests
This tool is particularly useful for integration testing of elf_loader. You can dynamically generate an ELF with specific relocation types, load it with your loader, and verify that relocations are applied correctly by inspecting ElfWriteOutput.
See tests/gen_elf.rs for comprehensive examples.