elf_loader 0.14.0

A high-performance, no_std compliant ELF loader 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
use crate::sync::{Arc, AtomicBool};
use crate::{
    Result,
    elf::{ElfDyn, ElfDynamic, ElfPhdr, ElfPhdrs, ElfRelType, SymbolTable},
    image::{ElfCore, ImageBuilder, common::CoreInner},
    loader::{LifecycleContext, LoadHook},
    os::Mmap,
    relocation::{DynamicRelocation, SymbolLookup},
    segment::ELFRelro,
    tls::{TlsIndex, TlsResolver},
};
use alloc::{boxed::Box, vec::Vec};
use core::{
    ffi::CStr,
    ptr::{NonNull, null},
};

pub(crate) struct DynamicInfo {
    pub(crate) eh_frame_hdr: Option<NonNull<u8>>,
    pub(crate) dynamic_ptr: NonNull<ElfDyn>,
    pub(crate) pltrel: Option<NonNull<ElfRelType>>,
    pub(crate) phdrs: ElfPhdrs,
    pub(crate) lazy_scope: Option<Box<dyn SymbolLookup>>,
}

/// 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 {
    /// Indicates whether lazy binding is enabled for this object
    lazy: bool,

    /// Pointer to the Global Offset Table (.got.plt section)
    got_plt: Option<NonNull<usize>>,

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

    /// GNU_RELRO segment information for memory protection
    relro: Option<ELFRelro>,

    /// Initialization function to be called after relocation
    init: Box<dyn Fn()>,

    /// 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]>,

    /// Function to get TLS address
    tls_get_addr: extern "C" fn(*const crate::tls::TlsIndex) -> *mut u8,
}

impl core::fmt::Debug for ElfExtraData {
    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 common part of relocated ELF objects.
///
/// This structure represents the common components shared by all relocated
/// ELF objects, whether they are dynamic libraries or executables.
/// It contains basic information like entry point, name, and program headers,
/// as well as the parsed data required for relocation and symbol lookup.
pub(crate) struct DynamicImage<D>
where
    D: 'static,
{
    /// Entry point of the ELF object.
    entry: usize,
    /// PT_INTERP segment value (interpreter path).
    interp: Option<&'static str>,
    /// Program headers.
    phdrs: ElfPhdrs,
    /// Core component containing the basic ELF object information
    module: ElfCore<D>,
    /// Extra data needed for relocation
    extra: ElfExtraData,
}

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

impl<D> DynamicImage<D> {
    /// Gets the entry point of the ELF object.
    #[inline]
    pub(crate) fn entry(&self) -> usize {
        self.entry
    }

    pub(crate) fn tls_mod_id(&self) -> Option<usize> {
        self.module.tls_mod_id()
    }

    /// Gets the TLS thread pointer offset
    pub(crate) fn tls_tp_offset(&self) -> Option<isize> {
        self.module.tls_tp_offset()
    }

    pub(crate) fn tls_get_addr(&self) -> extern "C" fn(*const TlsIndex) -> *mut u8 {
        self.extra.tls_get_addr
    }

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

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

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

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

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

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

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

    /// Gets the name of the ELF object
    #[inline]
    pub(crate) fn name(&self) -> &str {
        self.module.name()
    }

    /// Gets the program headers of the ELF object
    pub(crate) fn phdrs(&self) -> &[ElfPhdr] {
        match &self.phdrs {
            ElfPhdrs::Mmap(phdrs) => &phdrs,
            ElfPhdrs::Vec(phdrs) => &phdrs,
        }
    }

    /// Gets the Global Offset Table pointer
    ///
    /// # Returns
    /// An optional NonNull pointer to the GOT
    #[inline]
    pub(crate) fn got(&self) -> Option<NonNull<usize>> {
        self.extra.got_plt
    }

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

    /// 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(&self) {
        self.module.set_init();
        self.extra.init.as_ref()();
    }

    /// Gets the GNU_RELRO segment information
    ///
    /// # Returns
    /// An optional reference to the ELFRelro structure
    #[inline]
    pub(crate) fn relro(&self) -> Option<&ELFRelro> {
        self.extra.relro.as_ref()
    }

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

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

    /// Gets the total length of mapped memory for the ELF object
    pub(crate) fn mapped_len(&self) -> usize {
        self.module.segments().len()
    }

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

    /// Gets the dynamic section pointer
    ///
    /// # Returns
    /// An optional NonNull pointer to the dynamic section
    pub(crate) fn dynamic_ptr(&self) -> Option<NonNull<ElfDyn>> {
        self.module.dynamic_ptr()
    }

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

    /// Sets the lazy scope for this component
    ///
    /// # Arguments
    /// * `lazy_scope` - The lazy scope to set
    #[inline]
    pub(crate) fn set_lazy_scope<LazyS>(&self, lazy_scope: LazyS)
    where
        D: 'static,
        LazyS: SymbolLookup + Send + Sync + 'static,
    {
        let info = unsafe {
            &mut *(Arc::as_ptr(&self.module.inner.dynamic_info.as_ref().unwrap())
                as *mut DynamicInfo)
        };
        info.lazy_scope = Some(Box::new(lazy_scope));
    }
}

impl<'hook, H, M, Tls, D> ImageBuilder<'hook, H, M, Tls, D>
where
    H: LoadHook,
    Tls: TlsResolver,
    M: Mmap,
{
    /// Build the final DynamicImage object
    ///
    /// This method completes the building process by parsing all program
    /// headers and constructing the final DynamicImage object.
    ///
    /// # Arguments
    /// * `phdrs` - Slice of program headers
    ///
    /// # Returns
    /// * `Ok(DynamicImage)` - The built relocated ELF object
    /// * `Err(Error)` - If building fails
    pub(crate) fn build_dynamic(mut self, phdrs: &[ElfPhdr]) -> Result<DynamicImage<D>> {
        // Determine if this is a dynamic library
        let is_dylib = self.ehdr.is_dylib();

        // Parse all program headers
        for phdr in phdrs {
            self.parse_phdr(phdr)?;
        }

        let dynamic_ptr = self.dynamic_ptr.expect("dynamic section not found");

        // Create program headers representation
        let phdrs_repr = self.create_phdrs(phdrs);

        let dynamic = ElfDynamic::new(dynamic_ptr.as_ptr(), &self.segments).unwrap();

        #[cfg(feature = "log")]
        log::trace!("[{}] Dynamic info: {:?}", self.name, dynamic);

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

        let static_tls = self.static_tls | dynamic.static_tls;

        // Create symbol table from dynamic section
        let symtab = SymbolTable::from_dynamic(&dynamic);

        // Collect needed library names
        let needed_libs: Vec<&'static str> = dynamic
            .needed_libs
            .iter()
            .map(|needed_lib| symtab.strtab().get_str(needed_lib.get()))
            .collect();

        #[cfg(feature = "log")]
        if !needed_libs.is_empty() {
            log::debug!("[{}] Dependencies: {:?}", self.name, needed_libs);
        }

        let init_handler = self.init_fn;

        let (tls_mod_id, tls_tp_offset) = if let Some(info) = &self.tls_info {
            // The Tls::register will register the TLS module and return the ID.
            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)
        };

        // Build and return the relocated common part
        Ok(DynamicImage {
            entry: self.ehdr.e_entry as usize + if is_dylib { self.segments.base() } else { 0 },
            interp: self
                .interp
                .map(|s| unsafe { CStr::from_ptr(s.as_ptr()).to_str().unwrap() }),
            phdrs: phdrs_repr.clone(),
            extra: ElfExtraData {
                // Determine if lazy binding should be enabled
                lazy: !dynamic.bind_now,

                // Store GNU_RELRO segment information
                relro: self.relro,

                // Store relocation information
                relocation,

                // Create initialization function
                init: Box::new(move || {
                    init_handler.call(&LifecycleContext::new(
                        dynamic.init_fn,
                        dynamic.init_array_fn,
                    ))
                }),

                // Store GOT pointer
                got_plt: dynamic.got_plt,

                // Store RPATH value
                rpath: dynamic
                    .rpath_off
                    .map(|rpath_off| symtab.strtab().get_str(rpath_off.get())),

                // Store needed library names
                needed_libs: needed_libs.into_boxed_slice(),

                // Store RUNPATH value
                runpath: dynamic
                    .runpath_off
                    .map(|runpath_off| symtab.strtab().get_str(runpath_off.get())),

                // Store TLS get addr function
                tls_get_addr: Tls::tls_get_addr,
            },
            module: ElfCore {
                inner: Arc::new(CoreInner {
                    is_init: AtomicBool::new(false),
                    name: self.name,
                    symtab,
                    fini: dynamic.fini_fn,
                    fini_array: dynamic.fini_array_fn,
                    fini_handler: self.fini_fn,
                    user_data: self.user_data,
                    dynamic_info: Some(Arc::new(DynamicInfo {
                        eh_frame_hdr: self.eh_frame_hdr,
                        dynamic_ptr: NonNull::new(dynamic.dyn_ptr as _).unwrap(),
                        pltrel: NonNull::new(dynamic.pltrel.map_or(null(), |plt| plt.as_ptr()) as _),
                        phdrs: phdrs_repr,
                        lazy_scope: None,
                    })),
                    tls_mod_id,
                    tls_tp_offset,
                    tls_unregister: Tls::unregister,
                    tls_desc_args: Box::new([]),
                    segments: self.segments,
                }),
            },
        })
    }
}