elf_loader 0.15.1

A no_std-friendly ELF loader, runtime linker, and JIT 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
//! Relocation of elf objects
use crate::{
    ParseDynamicError, RelocReason, Result,
    elf::{ElfLayout, ElfRelEntry, ElfRelType, ElfRelr, ElfWord},
    image::{LoadedCore, RawDynamic},
    logging,
    relocation::{
        BindingMode, RelocHelper, RelocValue, RelocateArgs, RelocationArch, RelocationHandler,
        ResolvedBinding, likely, reloc_error, resolve_ifunc, unlikely,
    },
    tls::{TlsRelocOutcome, handle_tls_reloc},
};
use core::num::NonZeroUsize;

impl<D, Arch: RelocationArch> RawDynamic<D, Arch> {
    fn apply_relro(&self, binding: &ResolvedBinding) -> Result<()> {
        if binding.is_lazy() {
            return Ok(());
        }

        if let Some(relro) = self.relro() {
            relro.relro()?;
        }
        Ok(())
    }

    pub(crate) fn relocate_impl<PreH, PostH>(
        self,
        args: RelocateArgs<'_, D, Arch, PreH, PostH>,
    ) -> Result<LoadedCore<D, Arch>>
    where
        D: 'static,
        PreH: RelocationHandler<Arch> + ?Sized,
        PostH: RelocationHandler<Arch> + ?Sized,
    {
        logging::info!("Relocating dynamic library: {}", self.name());

        let RelocateArgs {
            scope,
            binding,
            pre_handler,
            post_handler,
            emu,
            ..
        } = args;

        let relocation = self.relocation();
        if relocation.is_empty() {
            logging::debug!("No relocations needed for {}", self.name());
        }

        let binding = self.resolve_binding(if Arch::SUPPORTS_NATIVE_RUNTIME {
            binding
        } else {
            BindingMode::Eager
        });
        let tls_get_addr = self.tls_get_addr();

        if binding.is_lazy() {
            logging::debug!("Using lazy binding for {}", self.name());
        }

        let mut helper = RelocHelper::new(
            self.core_ref(),
            scope,
            pre_handler,
            post_handler,
            tls_get_addr,
            emu.clone(),
        );

        if !relocation.is_empty() {
            self.relocate_relative()
                .relocate_dynrel(&mut helper)?
                .relocate_pltrel(&binding, &mut helper)?;
        }

        let RelocHelper {
            scope,
            tls_desc_args,
            ..
        } = helper;
        // Persist TLSDESC backing storage collected during relocation.
        unsafe {
            self.core_ref().set_tls_desc_args(tls_desc_args);
        }

        let dep_names = scope
            .iter()
            .filter_map(|source| source.as_any().downcast_ref::<LoadedCore<D, Arch>>())
            .map(|d| d.name())
            .collect::<alloc::vec::Vec<_>>();
        if !dep_names.is_empty() {
            logging::debug!("[{}] Bound dependencies: {:?}", self.name(), dep_names);
        }

        self.apply_relro(&binding)?;
        self.install_lazy_lookup(binding, scope.clone())?;

        if Arch::SUPPORTS_NATIVE_RUNTIME {
            logging::debug!("Executing initialization functions for {}", self.name());
            self.call_init();
        } else if let Some(emu) = emu {
            logging::debug!(
                "Executing initialization functions with emulator for {}",
                self.name()
            );
            self.call_init_with_emu(emu)?;
        } else {
            logging::debug!(
                "Skipping initialization functions for non-native relocation of {}",
                self.name()
            );
        }

        logging::info!("Relocation completed for {}", self.name());

        Ok(unsafe { LoadedCore::from_core_deps(self.into_core(), scope) })
    }
}

/// Types of relative relocations
enum RelativeRel<Arch: RelocationArch> {
    /// Standard REL/RELA relocations
    Rel(&'static [ElfRelType<Arch>]),
    /// Compact RELR relocations
    Relr(&'static [ElfRelr<Arch::Layout>]),
}

impl<Arch: RelocationArch> RelativeRel<Arch> {
    #[inline]
    fn is_empty(&self) -> bool {
        match self {
            RelativeRel::Rel(rel) => rel.is_empty(),
            RelativeRel::Relr(relr) => relr.is_empty(),
        }
    }
}

/// Holds parsed relocation information
pub(crate) struct DynamicRelocation<Arch: RelocationArch = crate::arch::NativeArch> {
    /// Relative relocations (REL_RELATIVE)
    relative: RelativeRel<Arch>,
    /// PLT relocations
    pub(in crate::relocation) pltrel: &'static [ElfRelType<Arch>],
    /// Other dynamic relocations
    dynrel: &'static [ElfRelType<Arch>],
}

#[inline]
fn write_reloc_addr<Arch: RelocationArch>(
    segments: &crate::segment::ElfSegments,
    r_offset: usize,
    value: crate::relocation::RelocAddr,
) {
    segments.write(
        r_offset,
        RelocValue::new(<Arch::Layout as ElfLayout>::Word::from_usize(
            value.into_inner(),
        )),
    );
}

impl<D, Arch: RelocationArch> RawDynamic<D, Arch> {
    /// Relocate PLT (Procedure Linkage Table) entries
    fn relocate_pltrel<PreH, PostH>(
        &self,
        binding: &ResolvedBinding,
        helper: &mut RelocHelper<'_, D, Arch, PreH, PostH>,
    ) -> Result<&Self>
    where
        PreH: RelocationHandler<Arch> + ?Sized,
        PostH: RelocationHandler<Arch> + ?Sized,
    {
        let core = self.core_ref();
        let base = core.base_addr();
        let segments = core.segments();
        let reloc = self.relocation();
        debug_assert!(Arch::SUPPORTS_NATIVE_RUNTIME || !binding.is_lazy());
        binding.prepare_plt(self)?;

        // Process PLT relocations
        for rel in reloc.pltrel {
            if !helper.handle_pre(rel)?.is_unhandled() {
                continue;
            }
            let r_type = rel.r_type();
            let r_sym = rel.r_symbol();
            let mut failure_reason = RelocReason::Unsupported;

            // Handle jump slot relocations
            if likely(r_type == Arch::JUMP_SLOT) {
                if binding.relocate_jump_slot::<Arch>(base, rel) {
                    continue;
                }

                if let Some(symbol) = helper.find_symbol(r_sym) {
                    write_reloc_addr::<Arch>(segments, rel.r_offset(), symbol);
                    continue;
                }
                failure_reason = RelocReason::UnknownSymbol;
            } else if unlikely(r_type == Arch::IRELATIVE) {
                let r_addend = rel.r_addend(base.into_inner());
                let addr = base.addend(r_addend);
                if !Arch::SUPPORTS_NATIVE_RUNTIME {
                    if let Some(resolved) = helper.resolve_ifunc_with_emu(rel, addr)? {
                        write_reloc_addr::<Arch>(segments, rel.r_offset(), resolved);
                        continue;
                    }
                    failure_reason = RelocReason::MissingEmulator;
                } else {
                    write_reloc_addr::<Arch>(segments, rel.r_offset(), unsafe {
                        resolve_ifunc(addr)
                    });
                    continue;
                }
            } else if unlikely(Arch::is_tlsdesc(r_type)) {
                // `handle_tls_reloc` performs its own SUPPORTS_NATIVE_RUNTIME
                // gate for TLSDESC. If the built-in path cannot handle it,
                // keep the specific TLS failure for the final error while
                // still giving the post handler a chance.
                match handle_tls_reloc::<_, Arch, _, _>(helper, rel)? {
                    TlsRelocOutcome::Applied => continue,
                    TlsRelocOutcome::Failed(reason) => failure_reason = reason,
                }
            }
            // Handle unknown relocations with the provided handler
            if helper.handle_post(rel)?.is_unhandled() {
                return Err(reloc_error::<Arch, _>(rel, failure_reason, core));
            }
        }
        Ok(self)
    }

    /// Perform relative relocations (REL_RELATIVE)
    fn relocate_relative(&self) -> &Self {
        let core = self.core_ref();
        let reloc = self.relocation();
        let segments = core.segments();
        let base = core.base_addr();

        match reloc.relative {
            RelativeRel::Rel(rel) => {
                assert!(rel.is_empty() || rel[0].r_type() == Arch::RELATIVE);
                // Apply all relative relocations: new_value = base_address + addend
                for rel in rel {
                    debug_assert!(rel.r_type() == Arch::RELATIVE);
                    let r_addend = rel.r_addend(base.into_inner());
                    write_reloc_addr::<Arch>(segments, rel.r_offset(), base.addend(r_addend));
                }
            }
            RelativeRel::Relr(relr) => {
                // Apply compact relative relocations (RELR format)
                let mut reloc_addr = core::ptr::null_mut::<<Arch::Layout as ElfLayout>::Word>();

                for relr in relr {
                    let value = relr.value();

                    unsafe {
                        if (value & 1) == 0 {
                            reloc_addr =
                                segments.get_mut_ptr::<<Arch::Layout as ElfLayout>::Word>(value);
                            reloc_addr.write(<Arch::Layout as ElfLayout>::Word::from_usize(
                                base.offset(reloc_addr.read().to_usize()).into_inner(),
                            ));
                            reloc_addr = reloc_addr.add(1);
                            continue;
                        }

                        let mut bitmap = value >> 1;
                        let mut ptr = reloc_addr;
                        while bitmap != 0 {
                            if (bitmap & 1) != 0 {
                                ptr.write(<Arch::Layout as ElfLayout>::Word::from_usize(
                                    base.offset(ptr.read().to_usize()).into_inner(),
                                ));
                            }
                            bitmap >>= 1;
                            ptr = ptr.add(1);
                        }
                        reloc_addr = reloc_addr.add(<Arch::Layout as ElfLayout>::Word::BITS - 1);
                    }
                }
            }
        }
        self
    }

    /// Perform dynamic relocations (non-PLT, non-relative)
    fn relocate_dynrel<PreH, PostH>(
        &self,
        helper: &mut RelocHelper<'_, D, Arch, PreH, PostH>,
    ) -> Result<&Self>
    where
        PreH: RelocationHandler<Arch> + ?Sized,
        PostH: RelocationHandler<Arch> + ?Sized,
    {
        /*
            Relocation formula components:
            A = Addend used to compute the value of the relocatable field
            B = Base address at which a shared object is loaded
            S = Value of the symbol whose index resides in the relocation entry
        */

        let core = self.core_ref();
        let reloc = self.relocation();
        let segments = core.segments();
        let base = core.base_addr();

        // Process each dynamic relocation entry
        for rel in reloc.dynrel {
            if !helper.handle_pre(rel)?.is_unhandled() {
                continue;
            }
            let r_type = rel.r_type();
            let r_sym = rel.r_symbol();
            let mut failure_reason = RelocReason::Unsupported;

            // Handle `REL_NONE` first because some architectures use `0` as a
            // sentinel for unsupported relocation classes such as TLSDESC.
            if r_type == Arch::NONE {
                continue;
            }

            if r_type == Arch::GOT || r_type == Arch::SYMBOLIC {
                // Handle GOT and symbolic relocations
                if let Some(symbol) = helper.find_symbol(r_sym) {
                    let r_addend = rel.r_addend(base.into_inner());
                    write_reloc_addr::<Arch>(segments, rel.r_offset(), symbol.addend(r_addend));
                    continue;
                }
                failure_reason = RelocReason::UnknownSymbol;
            } else if r_type == Arch::COPY {
                // Handle copy relocations (typically for global data)
                if let Some(symdef) = helper.find_symdef(r_sym) {
                    if let Some(sym) = symdef.symbol() {
                        let len = core.symtab().symbol_idx(r_sym).0.st_size();
                        let dest = core.segments().get_slice_mut::<u8>(rel.r_offset(), len);
                        if let Some(src) = symdef.segment_slice(sym.st_value(), len) {
                            dest.copy_from_slice(src);
                            continue;
                        }
                    }
                }
                failure_reason = RelocReason::UnknownSymbol;
            } else if r_type == Arch::IRELATIVE {
                let r_addend = rel.r_addend(base.into_inner());
                let addr = base.addend(r_addend);
                if !Arch::SUPPORTS_NATIVE_RUNTIME {
                    if let Some(resolved) = helper.resolve_ifunc_with_emu(rel, addr)? {
                        write_reloc_addr::<Arch>(segments, rel.r_offset(), resolved);
                        continue;
                    }
                    failure_reason = RelocReason::MissingEmulator;
                } else {
                    write_reloc_addr::<Arch>(segments, rel.r_offset(), unsafe {
                        resolve_ifunc(addr)
                    });
                    continue;
                }
            } else if Arch::is_tls(r_type) {
                // `handle_tls_reloc` is a pure data computation for
                // DTPMOD/DTPOFF/TPOFF (safe under cross-arch loads) and
                // gates TLSDESC on SUPPORTS_NATIVE_RUNTIME internally.
                // Anything the built-in path cannot handle still gets a post
                // handler chance before reporting the specific TLS reason.
                match handle_tls_reloc::<_, Arch, _, _>(helper, rel)? {
                    TlsRelocOutcome::Applied => continue,
                    TlsRelocOutcome::Failed(reason) => failure_reason = reason,
                }
            }

            // Handle unknown relocations with the provided handler
            if helper.handle_post(rel)?.is_unhandled() {
                return Err(reloc_error::<Arch, _>(rel, failure_reason, core));
            }
        }
        Ok(self)
    }
}

impl<Arch: RelocationArch> DynamicRelocation<Arch> {
    /// Create a new DynamicRelocation instance from parsed relocation data
    #[inline]
    pub(crate) fn new(
        pltrel: Option<&'static [ElfRelType<Arch>]>,
        dynrel: Option<&'static [ElfRelType<Arch>]>,
        relr: Option<&'static [ElfRelr<Arch::Layout>]>,
        rela_count: Option<NonZeroUsize>,
    ) -> Result<Self> {
        if let Some(relr) = relr {
            // Use RELR relocations if available (more compact format)
            Ok(Self {
                relative: RelativeRel::Relr(relr),
                pltrel: pltrel.unwrap_or(&[]),
                dynrel: dynrel.unwrap_or(&[]),
            })
        } else {
            // Use traditional REL/RELA relocations
            // nrelative indicates the count of REL_RELATIVE relocation types
            let nrelative = rela_count.map(|v| v.get()).unwrap_or(0);
            let old_dynrel = dynrel.unwrap_or(&[]);

            if nrelative > old_dynrel.len() {
                return Err(ParseDynamicError::MalformedRelocationTable {
                    detail:
                        "DT_RELCOUNT/DT_RELACOUNT relocation table is malformed: relative relocation count exceeds the relocation table length",
                }
                .into());
            }

            // Split relocations into relative and non-relative parts
            let relative = RelativeRel::Rel(&old_dynrel[..nrelative]);
            let temp_dynrel = &old_dynrel[nrelative..];

            let pltrel = pltrel.unwrap_or(&[]);
            let dynrel = if unsafe {
                // Check if dynrel and pltrel are contiguous in memory
                core::ptr::eq(
                    old_dynrel.as_ptr().add(old_dynrel.len()),
                    pltrel.as_ptr().add(pltrel.len()),
                )
            } {
                // If contiguous, exclude pltrel entries from dynrel
                let dynrel_len = temp_dynrel.len().checked_sub(pltrel.len()).ok_or(
                    ParseDynamicError::MalformedRelocationTable {
                        detail:
                            "DT_JMPREL relocation table is malformed: PLT relocations exceed the tail of DT_REL/DT_RELA",
                    },
                )?;
                &temp_dynrel[..dynrel_len]
            } else {
                // Otherwise, use all remaining entries
                temp_dynrel
            };

            Ok(Self {
                relative,
                pltrel,
                dynrel,
            })
        }
    }

    /// Check if there are no relocations to process
    #[inline]
    fn is_empty(&self) -> bool {
        self.relative.is_empty() && self.dynrel.is_empty() && self.pltrel.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::DynamicRelocation;
    use crate::{Error, ParseDynamicError, arch::NativeArch, elf::ElfRelType};
    use alloc::boxed::Box;
    use core::num::NonZeroUsize;

    fn zeroed_rel() -> ElfRelType {
        unsafe { core::mem::zeroed() }
    }

    #[test]
    fn rejects_relative_count_past_dynrel_len() {
        let dynrel = Box::leak(Box::new([zeroed_rel()]));
        let err = match DynamicRelocation::<NativeArch>::new(
            None,
            Some(&dynrel[..]),
            None,
            NonZeroUsize::new(2),
        ) {
            Ok(_) => panic!("relative count should be validated"),
            Err(err) => err,
        };

        assert!(matches!(
            err,
            Error::ParseDynamic(ParseDynamicError::MalformedRelocationTable { .. })
        ));
    }

    #[test]
    fn rejects_pltrel_suffix_longer_than_remaining_dynrel() {
        let dynrel = Box::leak(Box::new([zeroed_rel(), zeroed_rel(), zeroed_rel()]));
        let err = match DynamicRelocation::<NativeArch>::new(
            Some(&dynrel[..]),
            Some(&dynrel[..]),
            None,
            NonZeroUsize::new(1),
        ) {
            Ok(_) => panic!("contiguous PLT suffix should fit in the non-relative tail"),
            Err(err) => err,
        };

        assert!(matches!(
            err,
            Error::ParseDynamic(ParseDynamicError::MalformedRelocationTable { .. })
        ));
    }
}