1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! # Relink
//!
//! Relink is a high-performance, `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`]
//!
//! - Use [`Loader::load`] to auto-detect whether the input is a dylib, executable, or
//! relocatable object.
//! - Use [`Loader::scan`] to classify executable or dynamic ELF metadata without mapping it.
//! - Use [`Loader::load_dylib`] or [`Loader::load_exec`] when you want strict type checks.
//! - Use [`Loader::load_dynamic`] when you want any `PT_DYNAMIC` image, including a dynamic
//! `ET_EXEC`.
//! - Use [`Loader::scan`] and [`Loader::load_scanned_dynamic`] to split dynamic metadata
//! discovery from mapping.
//! - Use `Loader::load_object` to load `ET_REL` object files when the `object` feature is enabled.
//! - Inputs can come from file paths, [`input::Path`] / [`input::PathBuf`], raw bytes,
//! [`input::ElfFile`], or [`input::ElfBinary`].
//!
//! ## Highlights
//!
//! - Safer symbol lifetimes. Typed symbols borrow the loaded image, so they cannot outlive
//! the library that produced them.
//! - Hybrid linking. Compose `.so`, `.o`, and synthetic modules at runtime with `scope()` and
//! `extend_scope()`.
//! - Explicit dependency loading. Build your own dependency policy with an
//! actual [`Loader`], [`linker::KeyResolver`], [`linker::Linker`], and
//! [`linker::LinkContext`].
//! - Deep customization. Inject host or bridge symbols with
//! [`image::SyntheticModule`], intercept relocations with handlers, and
//! inspect segments with [`loader::LoadHook`].
//! - Optional advanced features. TLS relocation handling, lazy binding, relocatable object
//! loading, logging, and versioned symbol lookup are feature-gated.
//!
//! ## Example
//!
//! ```rust,no_run
//! use elf_loader::{
//! Loader, Result,
//! image::{SyntheticSymbol, SyntheticModule},
//! };
//!
//! extern "C" fn host_double(value: i32) -> i32 {
//! value * 2
//! }
//!
//! fn main() -> Result<()> {
//! let host = SyntheticModule::new(
//! "__host",
//! [SyntheticSymbol::function("host_double", host_double as *const ())],
//! );
//!
//! let lib = Loader::new()
//! .load_dylib("path/to/plugin.so")?
//! .relocator()
//! .scope([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::Linker`]
//!
//! Use [`linker::Linker::load`] when you want a reusable [`linker::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.
//!
//! ```rust,no_run
//! use elf_loader::{
//! Result,
//! input::PathBuf,
//! linker::{LinkContext, Linker, SearchPathResolver},
//! };
//!
//! fn main() -> Result<()> {
//! let root = PathBuf::from("path/to/plugin.so");
//! let mut context: LinkContext<PathBuf, ()> = LinkContext::new();
//!
//! let loaded = Linker::new()
//! .resolver(SearchPathResolver::new())
//! .load(&mut context, root)?;
//!
//! let run = unsafe {
//! loaded
//! .get::<extern "C" fn() -> i32>("run")
//! .expect("symbol `run` not found")
//! };
//! let _ = run();
//!
//! Ok(())
//! }
//! ```
//!
//! ## Feature Flags
//!
//! - `tls` (default): enables TLS relocation handling. For TLS-using modules, start from
//! [`Loader::with_default_tls_resolver`] or provide a custom TLS resolver.
//! - `lazy-binding`: enables `Relocator::lazy` and PLT/GOT lazy binding.
//! - `object`: enables `Loader::load_object` and relocatable object (`ET_REL`) loading.
//! - `version`: enables version-aware symbol lookup via `ElfCore::get_version`.
//! - `log`, `portable-atomic`, and `use-syscall`: optional integrations for diagnostics and
//! specialized targets.
//!
//! ## More
//!
//! - The [`examples`](https://github.com/weizhiao/elf_loader/tree/main/examples) directory
//! covers loading from memory, `Linker::load`, scan-first linking, lifecycle hooks,
//! relocation handlers, and object loading.
//! - The crate currently targets `x86_64`, `x86`, `aarch64`, `arm`, `riscv64`, `riscv32`,
//! and `loongarch64`.
//! - Relocatable object support is currently centered on `x86_64`.
extern crate alloc;
/// Compile-time check for supported architectures
compile_error!;
pub use AlignedBytes;
pub use *;
pub use ByteRepr;
pub use ;
pub use Loader;
/// A type alias for `Result`s returned by `elf_loader` functions.
///
/// This is a convenience alias that eliminates the need to repeatedly specify
/// the `Error` type in function signatures.
pub type Result<T> = Result;