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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! # 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 `modules()` or an
//! explicit [`LocalScope`].
//! - Explicit dependency loading. Build your own dependency policy with an
//! actual [`Loader`], [`linker::KeyResolver`], [`Linker`], and [`LinkContext`].
//! - Deep customization. Inject host or bridge symbols with
//! [`image::SyntheticModule`] and intercept relocations with handlers.
//! - Optional built-ins. The native lazy binder, relocatable object loading, logging, and
//! versioned symbol lookup are feature-gated; TLS and custom lazy binders remain available.
//!
//! ## Example
//!
//! ```rust,no_run
//! use elf_loader::{
//! Loader, Relocator, 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 = Relocator::new()
//! .run(Loader::new().load_dylib("path/to/plugin.so")?)
//! .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.
//!
//! ```rust,no_run
//! use elf_loader::{
//! LinkContext, Linker, Result,
//! input::PathBuf,
//! linker::SearchPathResolver,
//! runtime::DomainId,
//! };
//!
//! fn main() -> Result<()> {
//! let root = PathBuf::from("path/to/plugin.so");
//! let mut context = LinkContext::<()>::new(DomainId::PROCESS);
//! let mut resolver = SearchPathResolver::new();
//! resolver.push_rpath();
//! resolver.push_runpath();
//!
//! let loaded = Linker::new()
//! .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.
//!
//! ```rust,no_run
//! use elf_loader::{
//! Loader, Result,
//! arch::NativeArch,
//! observer::{BeforeLoadEvent, LoadObserver},
//! relocation::RelocationArch,
//! };
//!
//! 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()
//! .with_observer(TraceLoads)
//! .load_dylib("path/to/lib.so")?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Feature Flags
//!
//! - TLS relocation handling is always available. For TLS-using modules, start from
//! `Loader::with_default_tls_resolver` or provide a custom TLS resolver.
//! - `lazy-binding`: enables the built-in `NativeLazyBinder`. Custom
//! [`lazy::LazyBinder`] implementations and [`RelocatorRun::lazy`] are always available;
//! [`Relocator::new`] remains eager until a binder is configured.
//! - `object`: enables `Loader::load_object` and relocatable object (`ET_REL`) loading.
//! - `version`: enables version-aware `get_version` symbol lookups.
//! - `log`, `portable-atomic`, and `use-syscall`: optional integrations for diagnostics and
//! specialized targets.
//!
//! ## More
//!
//! - The [`examples`](https://github.com/weizhiao/Relink/tree/main/examples) directory
//! 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`,
//! and `loongarch64`.
//! - 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_64` and `riscv64`.
extern crate alloc;
/// Compile-time check for supported architectures
compile_error!;
pub use ;
pub use *;
pub use ByteRepr;
pub use Error;
pub use ;
pub use ModuleSourceId;
pub use ;
pub use ;
pub use ;
/// 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;