Skip to main content

elf_loader/
lib.rs

1//! # Relink (elf_loader)
2//!
3//! **Relink** is a high-performance runtime linker (JIT Linker) tailor-made for the Rust ecosystem.
4//! It efficiently parses Various ELF formats, supporting loading from both traditional file systems
5//! and direct memory images, and performs flexible dynamic and static hybrid linking.
6//!
7//! Whether you are developing **OS kernels**, **embedded systems**, **JIT compilers**, or building
8//! **plugin-based applications**, Relink provides a solid foundation with zero-cost abstractions,
9//! high-speed execution, and powerful extensibility.
10//!
11//! ## Core Features
12//!
13//! * **🛡️ Memory Safety**: Leverages Rust's ownership and `Arc` to manage library lifetimes and dependencies automatically.
14//! * **🔀 Hybrid Linking**: Seamlessly mix Relocatable Object files (`.o`) and Dynamic Shared Objects (`.so`).
15//! * **🎭 Customization**: Deeply intervene in symbol resolution and relocation through `SymbolLookup` and `RelocationHandler`.
16//! * **⚡ Performance & Versatility**: Optimized for `no_std` environments with support for RELR and Lazy Binding.
17//!
18//! ## Quick Start
19//!
20//! ```rust,no_run
21//! use elf_loader::{Loader, input::ElfBinary};
22//!
23//! fn main() -> Result<(), Box<dyn std::error::Error>> {
24//!     // 1. Load the library and perform instant linking
25//!     let lib = Loader::new().load_dylib(ElfBinary::new("my_lib", &[]))?
26//!         .relocator()
27//!         .relocate()?; // Complete all relocations
28//!
29//!     // 2. Safely retrieve and call the function
30//!     let awesome_func = unsafe {
31//!         lib.get::<fn(i32) -> i32>("awesome_func").ok_or("symbol not found")?
32//!     };
33//!     let result = awesome_func(42);
34//!     
35//!     Ok(())
36//! }
37//! ```
38#![no_std]
39#![warn(
40    clippy::unnecessary_wraps,
41    clippy::unnecessary_lazy_evaluations,
42    clippy::collapsible_if,
43    clippy::cast_lossless,
44    clippy::explicit_iter_loop,
45    clippy::manual_assert,
46    clippy::needless_question_mark,
47    clippy::needless_return,
48    clippy::needless_update,
49    clippy::redundant_clone,
50    clippy::redundant_else,
51    clippy::redundant_static_lifetimes
52)]
53#![allow(
54    clippy::len_without_is_empty,
55    clippy::unnecessary_cast,
56    clippy::uninit_vec
57)]
58extern crate alloc;
59
60/// Compile-time check for supported architectures
61#[cfg(not(any(
62    target_arch = "x86_64",
63    target_arch = "aarch64",
64    target_arch = "riscv64",
65    target_arch = "riscv32",
66    target_arch = "loongarch64",
67    target_arch = "x86",
68    target_arch = "arm",
69)))]
70compile_error!(
71    "Unsupported target architecture. Supported architectures: x86_64, aarch64, riscv64, riscv32, loongarch64, x86, arm"
72);
73
74pub mod arch;
75pub mod elf;
76mod error;
77pub mod image;
78pub mod input;
79pub mod loader;
80pub mod os;
81pub mod relocation;
82mod segment;
83mod sync;
84pub mod tls;
85
86pub(crate) use error::*;
87
88pub use error::Error;
89pub use loader::Loader;
90
91/// A type alias for `Result`s returned by `elf_loader` functions.
92///
93/// This is a convenience alias that eliminates the need to repeatedly specify
94/// the `Error` type in function signatures.
95pub type Result<T> = core::result::Result<T, Error>;