elf_loader 0.16.0

A no_std-friendly ELF loader and runtime linker for Rust.
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use super::run::LoaderRun;
#[cfg(feature = "object")]
use crate::object::SectionGroups;
use crate::{
    MmapError, Result,
    arch::NativeArch,
    const_builder::NoDrop,
    os::{DefaultMmap, Mmap, PageSize},
    relocation::RelocationArch,
    runtime::{CodeExecutor, NativeCodeExecutor},
    sync::Arc,
    tls::TlsResolver,
};
use alloc::boxed::Box;
use core::{marker::PhantomData, mem::MaybeUninit, ptr};

/// Reusable configuration for reading and mapping ELF images.
///
/// `Loader` maps ELF objects from files or memory and produces raw image types such as
/// [`crate::image::RawElf`], [`crate::image::RawDynamic`], [`crate::image::RawDylib`],
/// and [`crate::image::RawExec`].
/// Those raw images can then be relocated with [`crate::Relocator`].
///
/// A loader contains only stable configuration: target architecture, mapping
/// backend, user-data type, executor, and TLS resolver. Per-load scratch state
/// and observers live in [`LoaderRun`], so a configured loader can be reused
/// across many inputs.
///
/// Start with [`Loader::new`], call configuration methods such as
/// [`for_arch`](Self::for_arch) or `with_default_tls_resolver`, then call a
/// `load_*` method. Attach per-run observers with
/// [`LoaderRun::with_observer`]. Use
/// [`Relocator`](crate::Relocator) for explicit symbol scopes and final
/// relocation, or [`Linker`](crate::Linker) when `DT_NEEDED` dependencies should
/// be resolved automatically.
///
/// # Examples
///
/// ```no_run
/// use elf_loader::{Loader, Relocator};
///
/// let mut loader = Loader::new();
/// let raw = loader.load_dylib("path/to/liba.so").unwrap();
/// let lib = Relocator::new().run(raw).relocate().unwrap();
/// ```
pub struct Loader<
    D: 'static = (),
    Tls = (),
    Arch = NativeArch,
    M = DefaultMmap,
    Exec = NativeCodeExecutor,
> where
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap,
{
    mapper: M,
    executor: Exec,
    page_size: Option<PageSize>,
    force_static_tls: bool,
    _marker: PhantomData<fn() -> (D, Tls, Arch)>,
}

impl<D, Tls, Arch, M, Exec> Clone for Loader<D, Tls, Arch, M, Exec>
where
    D: 'static,
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap + Clone,
    Exec: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        Self {
            mapper: self.mapper.clone(),
            executor: self.executor.clone(),
            page_size: self.page_size,
            force_static_tls: self.force_static_tls,
            _marker: PhantomData,
        }
    }
}

impl<D, Tls, Arch, M, Exec> Copy for Loader<D, Tls, Arch, M, Exec>
where
    D: 'static,
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap + Copy,
    Exec: Copy,
{
}

struct LoaderFields<M, Exec> {
    mapper: NoDrop<M>,
    executor: NoDrop<Exec>,
    page_size: Option<PageSize>,
    force_static_tls: bool,
}

impl<M, Exec> LoaderFields<M, Exec> {
    #[inline]
    const fn into_loader<D, Tls, Arch>(self) -> Loader<D, Tls, Arch, M, Exec>
    where
        D: 'static,
        Tls: TlsResolver<Arch>,
        Arch: RelocationArch,
        M: Mmap,
    {
        let Self {
            mapper,
            executor,
            page_size,
            force_static_tls,
        } = self;

        Loader {
            mapper: mapper.into_inner(),
            executor: executor.into_inner(),
            page_size,
            force_static_tls,
            _marker: PhantomData,
        }
    }

    #[inline]
    const fn with_executor<D, Tls, Arch, NewExec>(
        self,
        executor: NewExec,
    ) -> Loader<D, Tls, Arch, M, NewExec>
    where
        D: 'static,
        Tls: TlsResolver<Arch>,
        Arch: RelocationArch,
        M: Mmap,
        Exec: Copy,
    {
        let Self {
            mapper,
            page_size,
            force_static_tls,
            ..
        } = self;

        Loader {
            mapper: mapper.into_inner(),
            executor,
            page_size,
            force_static_tls,
            _marker: PhantomData,
        }
    }

    #[inline]
    const fn with_mapper<D, Tls, Arch, NewM>(self, mapper: NewM) -> Loader<D, Tls, Arch, NewM, Exec>
    where
        D: 'static,
        Tls: TlsResolver<Arch>,
        Arch: RelocationArch,
        NewM: Mmap,
        M: Copy,
    {
        let Self {
            executor,
            page_size,
            force_static_tls,
            ..
        } = self;

        Loader {
            mapper,
            executor: executor.into_inner(),
            page_size,
            force_static_tls,
            _marker: PhantomData,
        }
    }
}

impl Loader<(), (), NativeArch, DefaultMmap, NativeCodeExecutor> {
    /// Creates a new [`Loader`] with the default mmap backend, no observer, no
    /// custom user data, no TLS resolver, and the host target architecture
    /// ([`NativeArch`]).
    ///
    /// To target a different ELF architecture (e.g. load an x86-64 shared
    /// object on a RISC-V host), switch the target architecture with
    /// [`for_arch::<NewArch>()`](Self::for_arch); the `e_machine` gate
    /// then validates against `NewArch::MACHINE` automatically.
    #[inline]
    pub const fn new() -> Self {
        Self {
            mapper: DefaultMmap::new(),
            executor: NativeCodeExecutor,
            page_size: None,
            force_static_tls: false,
            _marker: PhantomData,
        }
    }
}

impl Default for Loader<(), (), NativeArch, DefaultMmap, NativeCodeExecutor> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<D, Tls, Arch, M, Exec> Loader<D, Tls, Arch, M, Exec>
where
    D: 'static,
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap,
{
    #[inline]
    const fn into_fields(self) -> LoaderFields<M, Exec> {
        let this = MaybeUninit::new(self);
        let this = this.as_ptr();

        // SAFETY: `this` points at the fully initialized `self` stored inside
        // `MaybeUninit`, so every field read below is initialized and aligned.
        // The original `Loader` is intentionally not dropped; every owned
        // field that must survive is moved into `LoaderFields`. Helpers that
        // discard an old field require that old field to be `Copy`.
        unsafe {
            LoaderFields {
                mapper: NoDrop::read(ptr::addr_of!((*this).mapper)),
                executor: NoDrop::read(ptr::addr_of!((*this).executor)),
                page_size: ptr::read(ptr::addr_of!((*this).page_size)),
                force_static_tls: ptr::read(ptr::addr_of!((*this).force_static_tls)),
            }
        }
    }
}

impl<D, Tls, Arch, M, Exec> Loader<D, Tls, Arch, M, Exec>
where
    D: 'static,
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap,
    Exec: CodeExecutor<Arch> + Clone,
{
    #[inline]
    pub(crate) const fn mapper(&self) -> &M {
        &self.mapper
    }

    #[inline]
    pub(crate) const fn force_static_tls(&self) -> bool {
        self.force_static_tls
    }

    #[inline]
    pub(crate) fn executor(&self) -> Arc<dyn CodeExecutor<Arch>> {
        Arc::from(Box::new(self.executor.clone()) as Box<dyn CodeExecutor<Arch>>)
    }

    /// Starts a loader run with fresh ELF metadata scratch space.
    #[inline]
    pub fn run(&self) -> LoaderRun<'_, (), D, Tls, Arch, M, Exec> {
        LoaderRun {
            loader: self,
            observer: (),
            buf: super::ElfBuf::new(),
            #[cfg(feature = "object")]
            object_groups: Arc::new(SectionGroups::default()),
        }
    }

    #[inline]
    pub(crate) fn page_size(&self) -> Result<PageSize> {
        let required = self.mapper.page_size();
        let page_size = self.page_size.unwrap_or(required);
        if page_size.bytes() < required.bytes()
            || !page_size.bytes().is_multiple_of(required.bytes())
        {
            return Err(MmapError::InvalidPageSize {
                configured: page_size.bytes(),
                required: required.bytes(),
            }
            .into());
        }

        Ok(page_size)
    }

    /// Consumes the current loader and returns a new one with the specified
    /// dynamic-image user data type.
    ///
    /// Dynamic images are created with `NewD::default()`. To fill or adjust
    /// that data after dynamic metadata has been parsed, implement
    /// [`LoadObserver::on_after_dynamic_load`](crate::observer::LoadObserver::on_after_dynamic_load)
    /// on the configured load observer.
    pub const fn with_data<NewD>(self) -> Loader<NewD, Tls, Arch, M, Exec>
    where
        NewD: Default + 'static,
    {
        self.into_fields().into_loader()
    }

    /// Overrides the base page size used for segment layout decisions.
    ///
    /// By default, the loader uses [`Mmap::page_size`]. An override is useful
    /// for special runtimes and tests, but it must remain compatible with the
    /// mapping backend and with every loaded ELF's `PT_LOAD` alignment.
    pub const fn with_page_size(mut self, page_size: PageSize) -> Self {
        self.page_size = Some(page_size);
        self
    }

    /// Overrides the runtime-code executor used for init, fini and IFUNC.
    pub const fn with_executor<E>(self, executor: E) -> Loader<D, Tls, Arch, M, E>
    where
        E: CodeExecutor<Arch> + Clone,
        Exec: Copy,
    {
        self.into_fields().with_executor(executor)
    }

    /// Consumes the current loader and returns a new one with the specified TLS resolver.
    pub const fn with_tls_resolver<NewTls>(self) -> Loader<D, NewTls, Arch, M, Exec>
    where
        NewTls: TlsResolver<Arch>,
    {
        self.into_fields().into_loader()
    }

    /// Sets whether to force static TLS for all loaded modules.
    pub const fn with_static_tls(mut self, enabled: bool) -> Self {
        self.force_static_tls = enabled;
        self
    }
}

impl<D, Tls, M, Exec> Loader<D, Tls, NativeArch, M, Exec>
where
    D: 'static,
    Tls: TlsResolver<NativeArch>,
    M: Mmap,
    Exec: CodeExecutor<NativeArch> + Clone,
{
    /// Consumes the current loader and returns a new one with the default TLS resolver.
    #[cfg(feature = "tls")]
    pub const fn with_default_tls_resolver(
        self,
    ) -> Loader<D, crate::tls::DefaultTlsResolver, NativeArch, M, Exec> {
        self.into_fields().into_loader()
    }
}

/// Cross-architecture builder step.
///
/// Switching the target architecture is only meaningful while the loader has
/// not yet been bound to a user-data type. The builder therefore exposes
/// [`Loader::for_arch`] only on loaders whose `D` is still `()`. Callers should
/// pick the target architecture first and attach the user-data type afterwards:
///
/// ```no_run
/// use elf_loader::Loader;
/// use elf_loader::arch::x86_64::relocation::X86_64Arch;
///
/// let _loader = Loader::new()
///     .for_arch::<X86_64Arch>()
///     .with_data::<()>();
/// ```
impl<Tls, Arch, M, Exec> Loader<(), Tls, Arch, M, Exec>
where
    Tls: TlsResolver<Arch>,
    Arch: RelocationArch,
    M: Mmap,
    Exec: CodeExecutor<Arch> + Clone,
{
    /// Returns a new loader with a custom `Mmap` backend.
    pub const fn with_mmap<NewMmap>(self, mapper: NewMmap) -> Loader<(), Tls, Arch, NewMmap, Exec>
    where
        NewMmap: Mmap,
        M: Copy,
    {
        self.into_fields().with_mapper(mapper)
    }

    /// Consumes the current loader and returns a new one whose target
    /// architecture is `NewArch` instead of the previous `Arch`.
    ///
    /// This is the primary entry point for cross-architecture loading. Picking
    /// a non-host architecture (e.g.
    /// [`X86_64Arch`](crate::arch::x86_64::relocation::X86_64Arch)) makes
    /// every subsequent `load_*` call validate the ELF `e_machine` against
    /// `NewArch::MACHINE` instead of the host's, and stamps the resulting
    /// raw images with `NewArch` so [`Relocator::run`](crate::Relocator::run)
    /// and [`RelocatorRun::relocate`](crate::RelocatorRun::relocate) use the
    /// matching relocation numbering.
    ///
    /// Non-host architectures do not execute guest
    /// IFUNC resolvers, TLSDESC stubs, lazy-binding trampolines, and init
    /// arrays are *not* executed on the host CPU.
    ///
    /// # Builder ordering
    ///
    /// `for_arch` is only available before
    /// [`with_data`](Loader::with_data) has been called; instead, switch `Arch`
    /// first and then attach the user-data type once the target architecture is
    /// fixed.
    ///
    /// [`RelocatorRun::relocate`]: crate::RelocatorRun::relocate
    pub const fn for_arch<NewArch>(self) -> Loader<(), Tls, NewArch, M, NativeCodeExecutor>
    where
        NewArch: RelocationArch,
        Tls: TlsResolver<NewArch>,
        Exec: Copy,
    {
        self.into_fields().with_executor(NativeCodeExecutor)
    }
}