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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use crate::arch::NativeArch;
use crate::elf::ElfRelType;
use crate::{
    ParseDynamicError, ParsePhdrError, Result,
    elf::{
        ElfDyn, ElfDynamic, ElfDynamicTag, ElfLayout, ElfPhdr, ElfPhdrs, ElfStringTable, ElfSymbol,
        HashTable, Lifecycle, LifecycleSpec, SymbolTable,
    },
    input::Path,
    lazy::{LazyBinder, SupportLazy},
    loader::ImageBuilder,
    logging,
    memory::{HostRegion, ImageMemoryExt, MappedView, RegionAccess, VmAddr, VmOffset},
    observer::{InitEvent, RelocationObserver},
    relocation::{DynamicRelocation, Relocatable, RelocateArgs, RelocationArch},
    segment::{ElfSegments, MemoryProtection},
    sync::{Arc, AtomicBool},
    tls::{CoreTlsState, TlsResolver},
};
use alloc::{boxed::Box, vec::Vec};
use core::{cell::OnceCell, mem::size_of, ptr::NonNull};

use crate::image::{ElfCore, LoadedCore, ModuleTls, core::CoreInner, exports_handle};

impl<L: ElfLayout> SymbolTable<L> {
    pub(crate) fn from_dynamic<Arch, R>(
        dynamic: &ElfDynamic<Arch>,
        segments: &ElfSegments<R>,
    ) -> Result<Self>
    where
        Arch: RelocationArch<Layout = L>,
        R: RegionAccess,
    {
        let hashtab = HashTable::from_dynamic(dynamic, segments)?;
        let symbol_count = hashtab.count_syms();

        let symtab_off = dynamic
            .symtab
            .checked_offset_from(segments.base())
            .ok_or(ParseDynamicError::AddressOverflow)?;
        let symtab_size = symbol_count
            .checked_mul(size_of::<ElfSymbol<L>>())
            .ok_or(ParseDynamicError::AddressOverflow)?;
        let symbols = segments
            .read_view::<ElfSymbol<L>>(symtab_off, symtab_size)
            .ok_or(ParseDynamicError::MalformedSymbolTable {
                detail: "DT_SYMTAB symbol table size is malformed",
            })?
            .as_slice();

        let strtab_size = dynamic
            .strtab_size
            .ok_or(ParseDynamicError::MissingRequiredTag {
                tag: ElfDynamicTag::STRSZ,
            })?;
        let strtab_off = dynamic
            .strtab
            .checked_offset_from(segments.base())
            .ok_or(ParseDynamicError::AddressOverflow)?;
        let strtab = segments
            .read_view::<u8>(strtab_off, strtab_size.get())
            .ok_or(ParseDynamicError::MalformedStringTable {
                detail: "DT_STRTAB string table size is malformed",
            })?;
        let strtab = ElfStringTable::new(strtab);

        #[cfg(feature = "version")]
        let version = crate::elf::version::ELFVersion::new(
            dynamic.version_idx,
            dynamic.verneed,
            dynamic.verdef,
            &strtab,
        );

        Ok(Self {
            hashtab,
            symbols,
            strtab,
            #[cfg(feature = "version")]
            version,
        })
    }
}

pub(crate) struct PltRelocInfo<Arch: RelocationArch = NativeArch> {
    pub(crate) relocs: MappedView<ElfRelType<Arch>>,
    pub(crate) symbols: SymbolTable<Arch::Layout>,
}

impl<Arch: RelocationArch> PltRelocInfo<Arch> {
    #[inline]
    pub(crate) fn new(
        pltrel: Option<MappedView<ElfRelType<Arch>>>,
        symtab: SymbolTable<Arch::Layout>,
    ) -> Self {
        Self {
            relocs: pltrel.unwrap_or_else(MappedView::empty),
            symbols: symtab,
        }
    }
}

pub(crate) struct DynamicInfo<Arch: RelocationArch = NativeArch> {
    pub(crate) eh_frame_hdr: Option<NonNull<u8>>,
    pub(crate) phdrs: ElfPhdrs<Arch::Layout>,
    pub(crate) soname: Option<&'static str>,
    pub(crate) symbolic: bool,
}

/// Extra data associated with ELF objects during relocation
///
/// This structure holds additional data that is needed during the relocation
/// process but is not part of the core ELF object structure.
struct ElfExtraData<Arch: RelocationArch = NativeArch> {
    /// Indicates whether lazy binding is enabled for this object
    lazy: bool,

    /// Dynamic relocation information (rela.dyn and rela.plt)
    relocation: DynamicRelocation<Arch>,

    /// GNU_RELRO memory protection.
    relro: Option<MemoryProtection>,

    /// Runtime address of the dynamic section.
    dynamic_addr: VmAddr,

    /// Runtime address of the DT_DEBUG entry, when present.
    dt_debug_addr: Option<VmAddr>,

    /// Runtime address of the DT_PLTGOT entry, when present.
    got_plt: Option<VmAddr>,

    /// Initialization functions to resolve after relocation.
    init: LifecycleSpec,

    /// Finalization functions to resolve after relocation.
    fini: LifecycleSpec,

    /// DT_RPATH value from the dynamic section
    rpath: Option<&'static str>,

    /// DT_RUNPATH value from the dynamic section
    runpath: Option<&'static str>,

    /// List of needed library names from the dynamic section
    needed_libs: Box<[&'static str]>,

    /// Relocation-only dynamic symbol table.
    symtab: SymbolTable<Arch::Layout>,
}

impl<Arch: RelocationArch> core::fmt::Debug for ElfExtraData<Arch> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ElfExtraData")
            .field("lazy", &self.lazy)
            .field("relro", &self.relro.is_some())
            .field("needed_libs", &self.needed_libs)
            .finish()
    }
}

/// A mapped but unrelocated dynamic ELF image.
///
/// This is the common raw representation for ELF images with a `PT_DYNAMIC`
/// segment, including shared objects and dynamically linked executables.
///
/// The optional `Arch` type parameter selects the target architecture used
/// during [`crate::Relocator::run`]. By default it is
/// [`crate::arch::NativeArch`].
pub struct RawDynamic<
    D,
    Arch = NativeArch,
    R: RegionAccess = HostRegion,
    Tls: TlsResolver<Arch> = (),
> where
    D: 'static,
    Arch: RelocationArch,
{
    /// Entry point of the ELF object.
    entry: VmAddr,
    /// PT_INTERP segment value (interpreter path).
    interp: Option<&'static str>,
    /// Core component containing the basic ELF object information
    module: ElfCore<D, Arch, R, Tls>,
    /// Extra data needed for relocation
    extra: ElfExtraData<Arch>,
}

impl<D, Arch: RelocationArch, R: RegionAccess, Tls: TlsResolver<Arch>> core::fmt::Debug
    for RawDynamic<D, Arch, R, Tls>
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RawDynamic")
            .field("entry", &format_args!("0x{:x}", self.entry.get()))
            .field("module", &self.module)
            .field("extra", &self.extra)
            .finish()
    }
}

impl<D: 'static, Arch: RelocationArch, R: RegionAccess, Tls: TlsResolver<Arch>> SupportLazy
    for RawDynamic<D, Arch, R, Tls>
{
}

impl<D, Arch: RelocationArch, R: RegionAccess, Tls: TlsResolver<Arch>> RawDynamic<D, Arch, R, Tls> {
    /// Gets the entry point of the ELF object.
    #[inline]
    pub fn entry(&self) -> usize {
        self.entry_addr().get()
    }

    #[inline]
    pub(crate) fn entry_addr(&self) -> VmAddr {
        self.entry
    }

    /// Returns TLS metadata associated with this image.
    pub fn tls(&self) -> ModuleTls {
        self.module.tls()
    }

    /// Gets the core component reference of the ELF object.
    #[inline]
    pub fn core_ref(&self) -> &ElfCore<D, Arch, R, Tls> {
        &self.module
    }

    /// Gets the core component of the ELF object.
    #[inline]
    pub fn core(&self) -> ElfCore<D, Arch, R, Tls> {
        self.core_ref().clone()
    }

    /// Converts this object into its core component.
    #[inline]
    pub fn into_core(self) -> ElfCore<D, Arch, R, Tls> {
        self.core()
    }

    /// Whether lazy binding is enabled for the current ELF object
    #[inline]
    pub fn is_lazy(&self) -> bool {
        self.extra.lazy
    }

    /// Returns the target-visible `DT_PLTGOT` address used by lazy binding.
    #[inline]
    pub(crate) fn got_plt(&self) -> Option<VmAddr> {
        self.extra.got_plt
    }

    /// Gets the DT_RPATH value
    ///
    /// # Returns
    /// An optional string slice containing the RPATH value
    #[inline]
    pub fn rpath(&self) -> Option<&str> {
        self.extra.rpath
    }

    /// Gets the DT_RUNPATH value
    ///
    /// # Returns
    /// An optional string slice containing the RUNPATH value
    #[inline]
    pub fn runpath(&self) -> Option<&str> {
        self.extra.runpath
    }

    /// Gets the DT_SONAME value
    ///
    /// # Returns
    /// An optional string slice containing the shared-object name
    #[inline]
    pub fn soname(&self) -> Option<&str> {
        self.module.soname()
    }

    /// Gets the PT_INTERP value
    ///
    /// # Returns
    /// An optional string slice containing the interpreter path
    #[inline]
    pub fn interp(&self) -> Option<&str> {
        self.interp
    }

    /// Returns the loader source path or caller-provided source identifier.
    #[inline]
    pub fn path(&self) -> &Path {
        self.module.path()
    }

    /// Gets the ELF module identity used for diagnostics.
    #[inline]
    pub fn name(&self) -> &str {
        self.module.name()
    }

    /// Gets the program headers of the ELF object
    pub fn phdrs(&self) -> &[ElfPhdr<Arch::Layout>] {
        self.module
            .phdrs()
            .expect("raw dynamic image should always carry program headers")
    }

    /// Gets the dynamic relocation information
    ///
    /// # Returns
    /// A reference to the DynamicRelocation structure
    #[inline]
    pub(crate) fn relocation(&self) -> &DynamicRelocation<Arch> {
        &self.extra.relocation
    }

    pub(crate) fn resolve_lifecycle(&self) -> Result<(Lifecycle, Lifecycle)> {
        let segments = self.module.segments();
        let init = self
            .extra
            .init
            .resolve::<Arch::Layout, R>(segments, "DT_INIT_ARRAY size is malformed")?;
        let fini = self
            .extra
            .fini
            .resolve::<Arch::Layout, R>(segments, "DT_FINI_ARRAY size is malformed")?;
        Ok((init, fini))
    }

    /// Marks the ELF object as finished and calls the initialization function
    ///
    /// This method marks the ELF object as fully initialized and calls
    /// any registered initialization functions.
    #[inline]
    pub(crate) fn call_init<Obs>(&self, observer: &mut Obs, init: &Lifecycle) -> Result<()>
    where
        Obs: RelocationObserver<Arch> + ?Sized,
    {
        self.module.set_init();
        let segments = self.module.segments();
        let mut event = InitEvent::new(self.core_ref(), init);
        observer.on_init(&mut event)?;
        event.run_with(segments, self.module.executor())?;
        Ok(())
    }

    /// Gets the GNU_RELRO memory protection.
    ///
    /// # Returns
    /// An optional reference to the protection range.
    #[inline]
    pub(crate) fn relro(&self) -> Option<&MemoryProtection> {
        self.extra.relro.as_ref()
    }

    /// Gets a mutable reference to the user data
    ///
    /// # Returns
    /// An optional mutable reference to the user data
    #[inline]
    pub fn user_data_mut(&mut self) -> Option<&mut D> {
        self.module.user_data_mut()
    }

    /// Gets the base address of the loaded ELF object
    pub fn base(&self) -> VmAddr {
        self.module.base()
    }

    /// Returns the mapped segments owned by this image.
    pub fn segments(&self) -> &ElfSegments<R> {
        self.module.segments()
    }

    /// Gets the list of needed library names from the dynamic section
    pub fn needed_libs(&self) -> &[&str] {
        &self.extra.needed_libs
    }

    #[inline]
    pub(crate) fn dynamic_addr(&self) -> VmAddr {
        self.extra.dynamic_addr
    }

    /// Returns the runtime address of the `DT_DEBUG` dynamic entry, when present.
    #[inline]
    pub fn dt_debug_addr(&self) -> Option<VmAddr> {
        self.extra.dt_debug_addr
    }

    /// Writes the runtime address of an externally owned `r_debug` object into `DT_DEBUG`.
    ///
    /// Returns `Ok(true)` when this image has a `DT_DEBUG` entry and it was patched,
    /// or `Ok(false)` when no `DT_DEBUG` entry exists.
    #[inline]
    pub fn write_dt_debug_addr(&self, addr: VmAddr) -> Result<bool> {
        let Some(dt_debug_addr) = self.dt_debug_addr() else {
            return Ok(false);
        };
        let entry = ElfDyn::<Arch::Layout>::new(ElfDynamicTag::DEBUG, addr.get());
        unsafe { ImageMemoryExt::write_value(self.module.segments(), dt_debug_addr, entry)? };
        Ok(true)
    }

    #[inline]
    pub(crate) fn symtab(&self) -> &SymbolTable<Arch::Layout> {
        &self.extra.symtab
    }

    #[inline]
    /// Gets a reference to the user data
    pub fn user_data(&self) -> &D {
        self.module.user_data()
    }
}

impl<Tls, D: 'static, Arch: RelocationArch, R: RegionAccess> ImageBuilder<Tls, D, Arch, R>
where
    Tls: TlsResolver<Arch>,
{
    /// Build a dynamic image from the collected loader state.
    pub(crate) fn build_dynamic(
        mut self,
        phdrs: &[ElfPhdr<Arch::Layout>],
    ) -> Result<RawDynamic<D, Arch, R, Tls>> {
        self.parse_phdrs(phdrs)?;

        let phdrs = self.create_phdrs(phdrs)?;
        let dynamic = self.dynamic.ok_or(ParsePhdrError::MissingDynamicSection)?;
        let dynamic_addr = self
            .dynamic_addr
            .ok_or(ParsePhdrError::MissingDynamicSection)?;
        let dynamic = ElfDynamic::<Arch>::new(dynamic, dynamic_addr, &self.segments)?;

        logging::trace!("[{}] Dynamic info: {:?}", self.path, dynamic);

        let relocation = DynamicRelocation::new(
            dynamic.pltrel.clone(),
            dynamic.dynrel.clone(),
            dynamic.relr.clone(),
            dynamic.rel_count,
            dynamic.pltrel_is_dynrel_tail,
        )?;

        let static_tls = self.static_tls | dynamic.static_tls;
        let symtab = SymbolTable::from_dynamic(&dynamic, &self.segments)?;
        let exports = symtab.clone();
        let lazy_symtab = symtab.clone();
        let needed_libs: Vec<&'static str> = dynamic
            .needed_libs
            .iter()
            .map(|needed_lib| symtab.strtab().get_str(needed_lib.get()))
            .collect();

        if !needed_libs.is_empty() {
            logging::debug!("[{}] Dependencies: {:?}", self.path, needed_libs);
        }
        let soname = dynamic
            .soname_off
            .map(|soname_off| symtab.strtab().get_str(soname_off.get()));

        let tls_image = if let Some(info) = &self.tls_info {
            Some(
                self.segments
                    .read_view::<u8>(VmOffset::new(info.vaddr), info.filesz)
                    .ok_or_else(|| ParsePhdrError::malformed("PT_TLS image is malformed"))?,
            )
        } else {
            None
        };

        let (tls_mod_id, tls_tp_offset) = if let Some(info) = &self.tls_info {
            if static_tls {
                let (mod_id, offset) = Tls::register_static(info)?;
                (Some(mod_id), Some(offset))
            } else {
                (Some(Tls::register(info)?), None)
            }
        } else {
            (None, None)
        };
        let lazy_plt = PltRelocInfo::new(dynamic.pltrel.clone(), lazy_symtab);

        let inner = Arc::new(CoreInner {
            runtime: Box::new(crate::image::CoreRuntime::new::<D, R, Tls>(Some(lazy_plt))),
            executor: self.executor,
            is_init: AtomicBool::new(false),
            path: self.path,
            exports: exports_handle(exports),
            finalizer: OnceCell::new(),
            user_data: self.user_data,
            dynamic_info: Some(Arc::new(DynamicInfo::<Arch> {
                eh_frame_hdr: self.eh_frame_hdr,
                phdrs,
                soname,
                symbolic: dynamic.symbolic,
            })),
            scope: OnceCell::new(),
            tls: CoreTlsState::new(tls_mod_id, tls_tp_offset, self.tls_info, tls_image),
            segments: self.segments,
        });
        CoreInner::bind_runtime_owner(&inner);

        Ok(RawDynamic {
            entry: self.entry,
            interp: self.interp,
            extra: ElfExtraData::<Arch> {
                lazy: !dynamic.bind_now,
                relro: self.relro,
                dynamic_addr,
                dt_debug_addr: dynamic.dt_debug_addr,
                got_plt: dynamic.got_plt,
                relocation,
                init: dynamic.init,
                fini: dynamic.fini,
                rpath: dynamic
                    .rpath_off
                    .map(|rpath_off| symtab.strtab().get_str(rpath_off.get())),
                needed_libs: needed_libs.into_boxed_slice(),
                runpath: dynamic
                    .runpath_off
                    .map(|runpath_off| symtab.strtab().get_str(runpath_off.get())),
                symtab,
            },
            module: ElfCore { inner },
        })
    }
}

impl<D, Arch, R, Tls> Relocatable<D> for RawDynamic<D, Arch, R, Tls>
where
    D: 'static,
    Arch: RelocationArch,
    R: RegionAccess,
    Tls: TlsResolver<Arch>,
    <Arch::Layout as crate::elf::ElfLayout>::Word: crate::ByteRepr,
{
    type Output = LoadedCore<D, Arch, R, Tls>;
    type Arch = Arch;
    type Tls = Tls;

    fn relocate<Obs, Binder>(
        self,
        args: RelocateArgs<'_, Arch, Tls, Obs, Binder>,
    ) -> Result<Self::Output>
    where
        Obs: RelocationObserver<Arch> + ?Sized,
        Binder: LazyBinder<Arch> + ?Sized,
    {
        self.relocate_impl(args)
    }
}