Expand description
§Relink
Relink is a no_std-friendly ELF loader and runtime linker for Rust.
It maps ELF images from files or memory, performs relocations at runtime, and exposes
typed symbol lookups with Rust lifetimes.
§Start with Loader
Bind a runtime domain with Loader::run before loading an image.
- Use
LoaderRun::loadto auto-detect whether the input is a dylib, executable, or relocatable object. - Use
LoaderRun::load_dyliborLoaderRun::load_execwhen you want strict type checks. - Use
LoaderRun::load_dynamicwhen you want anyPT_DYNAMICimage, including a dynamicET_EXEC. - Use
LoaderRun::scanandLoaderRun::load_scanned_dynamicto split dynamic metadata discovery from mapping. - Use
LoaderRun::load_objectto loadET_RELobject files when theobjectfeature is enabled. - Inputs can come from file paths,
input::Path/input::PathBuf, raw bytes,input::ElfFile, orinput::ElfBinary.
Typed symbols borrow their loaded image. Use RelocatorRun::with_modules or
LocalScope to resolve references against other ELF images and synthetic modules.
§Example
use elf_loader::{
Loader, Relocator, Result,
AddressSpaceId, Domain,
image::{SyntheticSymbol, SyntheticModule},
};
extern "C" fn host_double(value: i32) -> i32 {
value * 2
}
fn main() -> Result<()> {
let domain = Domain::native(AddressSpaceId::PROCESS);
let host = SyntheticModule::new(
&domain,
"__host",
[SyntheticSymbol::function(c"host_double", host_double as *const ())],
);
let lib = Relocator::new()
.run(Loader::new().run(domain).load_dylib("path/to/plugin.so")?)
.with_modules([host])?
.relocate()?;
let run = unsafe {
lib.get::<extern "C" fn(i32) -> i32>("run")?
.expect("symbol `run` not found")
};
assert_eq!(run(21), 42);
Ok(())
}§Loading Dependencies With Linker
Use Linker::run and LinkerRun::load when you want a reusable LinkContext
and resolver-driven DT_NEEDED dependency loading. The built-in
linker::SearchPathResolver covers the common filesystem search-path case;
implement linker::KeyResolver when dependencies come from memory,
package stores, or another registry.
use elf_loader::{
LinkContext, Linker, Result,
input::PathBuf,
linker::SearchPathResolver,
};
fn main() -> Result<()> {
let root = PathBuf::from("path/to/plugin.so");
let mut context = LinkContext::for_process();
let mut resolver = SearchPathResolver::new();
resolver.push_rpath();
resolver.push_runpath();
let loaded = Linker::new()
.with_resolver(resolver)
.run()
.load(&mut context, root)?;
let run = unsafe {
context
.module(loaded.root())?
.get::<extern "C" fn() -> i32>("run")?
.expect("symbol `run` not found")
};
let _ = run();
drop(loaded.release(&mut context)?);
Ok(())
}§Observer Hooks
Observers are attached to a single loader or linker run, so reusable
Loader and Linker configuration can stay immutable while each run
decides which events to inspect or override.
use elf_loader::{
Loader, Result,
arch::NativeArch,
observer::{BeforeLoadEvent, LoadObserver},
relocation::RelocationArch,
AddressSpaceId, Domain,
};
struct TraceLoads;
impl LoadObserver for TraceLoads {
fn on_before_load(
&mut self,
event: BeforeLoadEvent<'_, (), <NativeArch as RelocationArch>::Layout>,
) -> Result<()> {
let _path = event.path();
let _is_dynamic = event.is_dynamic();
Ok(())
}
}
fn main() -> Result<()> {
let _raw = Loader::new()
.run(Domain::native(AddressSpaceId::PROCESS))
.with_observer(TraceLoads)
.load_dylib("path/to/lib.so")?;
Ok(())
}§Feature Flags
The default full feature enables std, object, and lazy-binding.
Disable default features for no_std builds. use-syscall and std are
mutually exclusive.
- TLS relocation handling is always available. Pass a
DomaintoLoader::runorLinkContext::new. Domains start with TLS disabled; useDomain::with_tlsto enable it. lazy-binding: enables the built-inNativeLazyBinder. Customlazy::LazyBinderimplementations andRelocatorRun::lazyare always available;Relocator::newremains eager until a binder is configured.object: enablesLoaderRun::load_objectand relocatable object (ET_REL) loading.version: enables version-awareget_versionsymbol lookups.std: enables standard-library integration and host-library imports on Linux/glibc.log,portable-atomic, anduse-syscall: optional integrations for diagnostics and specialized targets.
§Platforms and Examples
- The
examplesdirectory covers loading from memory,LinkerRun::load, scan-first linking, observer hooks, and object loading. - The crate currently targets
x86_64,x86,aarch64,arm,riscv64,riscv32, andloongarch64. - Little-endian Xtensa ELF32 images have basic cross-architecture dynamic relocation support; lazy binding, native runtime hooks, and TLS relocation support are pending.
- Relocatable object support is currently centered on
x86_64andriscv64.
Re-exports§
pub use error::Error;pub use image::ElfModule;pub use image::GlobalScope;pub use image::LocalScope;pub use image::Module;pub use image::ModuleInstanceId;pub use image::ModuleScope;pub use image::ModuleSearch;pub use image::ModuleState;pub use image::SearchPathPool;pub use input::ModuleSourceId;pub use linker::GraphModule;pub use linker::LinkContext;pub use linker::Linker;pub use linker::LinkerRun;pub use linker::ModuleKey;pub use loader::Loader;pub use loader::LoaderRun;pub use relocation::Relocator;pub use relocation::RelocatorRun;pub use runtime::AddressSpaceId;pub use runtime::Domain;pub use runtime::DomainId;
Modules§
- arch
- Architecture-specific definitions and relocation logic.
- elf
- ELF (Executable and Linkable Format) parsing and data structures.
- error
- Error types returned by Relink APIs.
- host
GNU and Linux and std - Import libraries managed by the Linux/glibc loader into a Relink scope.
- image
- Raw and relocated ELF images, synthetic modules, and symbol scopes.
- input
- ELF input traits and built-in data sources.
- lazy
- Lazy PLT binding support.
- linker
- Loads ELF dependency graphs into a
LinkContext. - loader
- Loading entry points and customization hooks.
- memory
- Virtual memory and mapped image abstractions.
- object
object - Relocatable-object loading, layout, and export helpers.
- observer
- Observer traits and event payloads for load and relocation hooks.
- os
- Memory mapping, page protection, and platform backends.
- relocation
- Relocation, symbol lookup, and binding policy.
- runtime
- Runtime execution abstractions for mapped images.
- tls
- TLS registration and per-thread storage.
Traits§
- Byte
Repr - Types that can be safely viewed from arbitrary bytes.
Type Aliases§
- Result
- Result returned by loader and linker operations.