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
use super::{
    Materialization, ModuleLayout, SectionId, SectionMetadata, SectionPlacement,
    layout::{DataAccess, MemoryLayoutPlan, SectionDataAccessRef},
};
use crate::linker::session::ModulePayload;
use crate::{
    AlignedBytes, LinkerError, Result,
    aligned_bytes::ByteRepr,
    entity::{PrimaryMap, entity_ref},
    image::{ModuleCapability, ScannedDynamic, ScannedSectionId},
    relocation::RelocationArch,
};
use alloc::{boxed::Box, collections::BTreeMap, vec::Vec};

/// A stable id for one planned module stored inside a [`LinkPlan`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(in crate::linker) struct ModuleId(usize);
entity_ref!(ModuleId);

pub struct PlannedModule<K, Arch: RelocationArch> {
    key: K,
    module: ModulePayload<ScannedDynamic<Arch>, Arch>,
    direct_deps: Box<[ModuleId]>,
}

fn resolve_direct_deps<K>(module_ids: &BTreeMap<K, ModuleId>, direct_deps: &[K]) -> Box<[ModuleId]>
where
    K: Ord,
{
    direct_deps
        .iter()
        .map(|dep_key| {
            *module_ids
                .get(dep_key)
                .expect("planned module dependency referenced an unknown module key")
        })
        .collect::<Vec<_>>()
        .into_boxed_slice()
}

impl<K, Arch> PlannedModule<K, Arch>
where
    Arch: RelocationArch,
{
    #[inline]
    pub(in crate::linker) fn new(
        key: K,
        module: ModulePayload<ScannedDynamic<Arch>, Arch>,
        direct_deps: Box<[ModuleId]>,
    ) -> Self {
        Self {
            key,
            module,
            direct_deps,
        }
    }

    #[inline]
    pub fn key(&self) -> &K {
        &self.key
    }

    #[inline]
    pub(in crate::linker) const fn is_dynamic(&self) -> bool {
        matches!(self.module, ModulePayload::Dynamic(_))
    }

    #[inline]
    pub(in crate::linker) fn dynamic(&self) -> Option<&ScannedDynamic<Arch>> {
        match &self.module {
            ModulePayload::Dynamic(module) => Some(module),
            ModulePayload::Synthetic(_) => None,
        }
    }

    #[inline]
    pub(in crate::linker) fn dynamic_mut(&mut self) -> Option<&mut ScannedDynamic<Arch>> {
        match &mut self.module {
            ModulePayload::Dynamic(module) => Some(module),
            ModulePayload::Synthetic(_) => None,
        }
    }

    #[inline]
    pub fn direct_deps(&self) -> &[ModuleId] {
        &self.direct_deps
    }

    #[inline]
    pub(crate) fn into_parts(
        self,
    ) -> (
        K,
        ModulePayload<ScannedDynamic<Arch>, Arch>,
        Box<[ModuleId]>,
    ) {
        (self.key, self.module, self.direct_deps)
    }
}

type LinkPlanParts<K, Arch> = (
    ModuleId,
    Vec<ModuleId>,
    PrimaryMap<ModuleId, PlannedModule<K, Arch>>,
    MemoryLayoutPlan,
);

fn section_data_entries<T: ByteRepr>(data: &AlignedBytes) -> Result<&[T]> {
    data.try_cast_slice::<T>().ok_or_else(|| {
        LinkerError::section_data("section data bytes do not match requested entry type").into()
    })
}

/// A global, pre-map link plan built from metadata discovery.
///
/// This plan owns the discovered logical module graph and accumulates later
/// planning decisions such as physical memory-layout plans or future
/// materialization policies.
pub(crate) struct LinkPlan<K, Arch: RelocationArch = crate::arch::NativeArch> {
    root: ModuleId,
    group_order: Vec<ModuleId>,
    module_ids: BTreeMap<K, ModuleId>,
    entries: PrimaryMap<ModuleId, PlannedModule<K, Arch>>,
    memory_layout: MemoryLayoutPlan,
}

impl<K, Arch> LinkPlan<K, Arch>
where
    K: Clone + Ord,
    Arch: RelocationArch,
{
    #[inline]
    pub(in crate::linker) fn new(
        root: K,
        group_order: Vec<K>,
        mut entries: BTreeMap<K, (ModulePayload<ScannedDynamic<Arch>, Arch>, Box<[K]>)>,
    ) -> Self {
        let group_keys = group_order;
        let mut module_ids = BTreeMap::new();
        let mut group_order = Vec::with_capacity(group_keys.len());
        let mut pending_entries = PrimaryMap::default();
        for key in group_keys {
            let (module, direct_deps) = entries
                .remove(&key)
                .expect("scan plan group order referenced a missing discovered module");
            let id = pending_entries.push((key.clone(), module, direct_deps));
            let previous = module_ids.insert(key, id);
            assert!(
                previous.is_none(),
                "scan plan discovered duplicate module key"
            );
            group_order.push(id);
        }

        let root = *module_ids
            .get(&root)
            .expect("scan plan root must exist in discovery order");

        let planned_entries = pending_entries.map_values(|_, (key, module, direct_deps)| {
            let direct_deps = resolve_direct_deps(&module_ids, &direct_deps);
            PlannedModule::new(key, module, direct_deps)
        });
        assert!(
            entries.is_empty(),
            "scan plan contained modules that were not present in discovery order"
        );

        let memory_layout = MemoryLayoutPlan::from_scanned(
            planned_entries
                .iter()
                .filter_map(|(id, entry)| entry.dynamic().map(|module| (id, module))),
        );
        Self {
            root,
            group_order,
            module_ids,
            entries: planned_entries,
            memory_layout,
        }
    }

    /// Returns the canonical root key of the plan.
    #[inline]
    pub(in crate::linker) fn root_key(&self) -> &K {
        self.module_key(self.root)
            .expect("planned root module must resolve to a key")
    }

    /// Returns the canonical root module id of the plan.
    #[inline]
    pub(in crate::linker) const fn root_module(&self) -> ModuleId {
        self.root
    }

    /// Returns the breadth-first module ids discovered from the root.
    #[inline]
    pub(in crate::linker) fn group_order(&self) -> &[ModuleId] {
        &self.group_order
    }

    pub(in crate::linker) fn modules_with_materialization(
        &self,
        mode: Materialization,
    ) -> impl Iterator<Item = ModuleId> + '_ {
        self.group_order
            .iter()
            .copied()
            .filter(move |module_id| self.materialization(*module_id) == Some(mode))
    }

    pub(in crate::linker) fn try_for_each_module(
        &mut self,
        mut f: impl FnMut(&mut Self, ModuleId) -> Result<()>,
    ) -> Result<()> {
        let group_len = self.group_order.len();
        for index in 0..group_len {
            let id = self.group_order[index];
            let Some(entry) = self.entries.get(id) else {
                continue;
            };
            if !entry.is_dynamic() {
                continue;
            }
            f(self, id)?;
        }
        Ok(())
    }

    /// Returns whether the plan contains `key`.
    #[inline]
    pub(in crate::linker) fn contains_key(&self, key: &K) -> bool {
        self.module_ids.contains_key(key)
    }

    /// Returns the stable module id for `key`.
    #[inline]
    pub(in crate::linker) fn module_id(&self, key: &K) -> Option<ModuleId> {
        self.module_ids.get(key).copied()
    }

    #[inline]
    pub(in crate::linker) fn module_key(&self, id: ModuleId) -> Option<&K> {
        self.entries.get(id).map(PlannedModule::key)
    }

    #[inline]
    pub(in crate::linker) fn get(&self, id: ModuleId) -> Option<&PlannedModule<K, Arch>> {
        self.entries.get(id)
    }

    #[inline]
    pub(in crate::linker) fn get_mut(
        &mut self,
        id: ModuleId,
    ) -> Option<&mut PlannedModule<K, Arch>> {
        self.entries.get_mut(id)
    }

    pub(crate) fn placement(&self, section: SectionId) -> Option<SectionPlacement> {
        self.memory_layout.placement(section)
    }

    /// Returns the physical memory-layout plan associated with this graph.
    #[inline]
    pub(in crate::linker) fn memory_layout(&self) -> &MemoryLayoutPlan {
        &self.memory_layout
    }

    /// Returns the physical memory-layout plan mutably.
    #[inline]
    pub(in crate::linker) fn memory_layout_mut(&mut self) -> &mut MemoryLayoutPlan {
        &mut self.memory_layout
    }

    /// Returns one module's layout view by stable module id.
    #[inline]
    pub(in crate::linker) fn module_layout(&self, id: ModuleId) -> &ModuleLayout {
        self.memory_layout.module(id)
    }

    /// Returns the owning module id for one stable section id.
    #[inline]
    pub(in crate::linker) fn section_owner(&self, section: SectionId) -> Option<ModuleId> {
        self.memory_layout.owner(section)
    }

    /// Returns the stable section id for one scanned section inside one module.
    #[inline]
    pub(in crate::linker) fn module_section_id(
        &self,
        module_id: ModuleId,
        id: impl Into<ScannedSectionId>,
    ) -> Option<SectionId> {
        self.memory_layout.section_id(module_id, id)
    }

    /// Returns one section metadata record by stable section id.
    #[inline]
    pub(crate) fn section_metadata(&self, section: SectionId) -> &SectionMetadata {
        self.memory_layout.section(section)
    }

    #[inline]
    pub(in crate::linker) fn module_capability(&self, id: ModuleId) -> Option<ModuleCapability> {
        self.get(id)
            .and_then(|entry| entry.dynamic().map(ScannedDynamic::capability))
    }

    #[inline]
    pub(in crate::linker) fn materialization(&self, id: ModuleId) -> Option<Materialization> {
        self.memory_layout.materialization(id)
    }

    /// Selects the materialization mode for one module.
    #[inline]
    pub(in crate::linker) fn set_materialization(
        &mut self,
        id: ModuleId,
        mode: Materialization,
    ) -> Option<Materialization> {
        self.memory_layout.set_materialization(id, mode)
    }

    pub(in crate::linker) fn materialize_section_data(&mut self, section: SectionId) -> Result<()> {
        if self.memory_layout.data(section).is_some() {
            return Ok(());
        }

        let id = self.memory_layout.owner(section).ok_or_else(|| {
            LinkerError::section_data("section data requested for an unowned section")
        })?;
        let scanned_section = self.memory_layout.section(section).scanned_section();
        let entry = self.entries.get_mut(id).ok_or_else(|| {
            LinkerError::section_data("section data requested for a missing planned module")
        })?;
        let module = entry.dynamic_mut().ok_or_else(|| {
            LinkerError::section_data("section data requested for a synthetic module")
        })?;
        if !module.capability().has_section_data() {
            return Err(LinkerError::section_data(
                "section data requested for a module without section data",
            )
            .into());
        }

        let snapshot = module.section_data(scanned_section)?.ok_or_else(|| {
            LinkerError::section_data("section data requested for a missing scanned section")
        })?;

        self.memory_layout.install_data(section, snapshot);
        Ok(())
    }

    pub(in crate::linker) fn with_disjoint_section_data<const N: usize, R>(
        &mut self,
        accesses: [(SectionId, DataAccess); N],
        f: impl FnOnce([SectionDataAccessRef<'_>; N]) -> Result<R>,
    ) -> Result<R> {
        for (index, (section, _)) in accesses.iter().enumerate() {
            if accesses[index + 1..]
                .iter()
                .any(|(other, _)| section == other)
            {
                return Err(LinkerError::duplicate_section_data_access().into());
            }
        }

        for &(section, access) in &accesses {
            self.materialize_section_data(section)?;
            if access == DataAccess::Write {
                self.memory_layout.mark_section_data_override(section);
            }
        }

        self.memory_layout
            .with_disjoint_section_data(accesses, f)
            .ok_or_else(LinkerError::missing_section_data_access)?
    }

    pub(in crate::linker) fn for_each_section_data<T, P>(
        &mut self,
        section: SectionId,
        mut prepare: impl FnMut(&T, &MemoryLayoutPlan) -> Result<Option<P>>,
        mut apply: impl FnMut(&mut Self, usize, P) -> Result<()>,
    ) -> Result<()>
    where
        T: ByteRepr,
    {
        self.materialize_section_data(section)?;
        let entry_count = {
            let plan = &self.memory_layout;
            let data = plan
                .data(section)
                .ok_or_else(|| LinkerError::section_data("section data was not materialized"))?;
            section_data_entries::<T>(data)?.len()
        };
        for index in 0..entry_count {
            let prepared = {
                let plan = &self.memory_layout;
                let data = plan.data(section).ok_or_else(|| {
                    LinkerError::section_data("section data was not materialized")
                })?;
                let entries = data.try_cast_slice::<T>().ok_or_else(|| {
                    LinkerError::section_data(
                        "section data bytes do not match requested entry type",
                    )
                })?;
                let entry = entries
                    .get(index)
                    .expect("section data entry index should remain valid");
                prepare(entry, plan)?
            };
            if let Some(prepared) = prepared {
                apply(self, index, prepared)?;
            }
        }
        Ok(())
    }

    /// Returns one section's data, materializing it on demand when needed.
    pub(crate) fn section_data(&mut self, section: SectionId) -> Result<&AlignedBytes> {
        self.materialize_section_data(section)?;
        self.memory_layout
            .data(section)
            .ok_or_else(|| LinkerError::section_data("section data was not materialized"))
            .map_err(Into::into)
    }

    /// Returns mutable section data, materializing it on demand when needed.
    pub(crate) fn section_data_mut(&mut self, section: SectionId) -> Result<&mut AlignedBytes> {
        self.materialize_section_data(section)?;
        self.memory_layout.mark_section_data_override(section);
        self.memory_layout
            .data_mut(section)
            .ok_or_else(|| LinkerError::section_data("section data was not materialized"))
            .map_err(Into::into)
    }

    pub(in crate::linker) fn resize_section(
        &mut self,
        section: SectionId,
        byte_len: usize,
    ) -> Result<()> {
        self.materialize_section_data(section)?;
        self.memory_layout.resize_section(section, byte_len)?;
        self.memory_layout.mark_section_data_override(section);
        if self.memory_layout.section(section).is_allocated() {
            let owner = self.memory_layout.owner(section).ok_or_else(|| {
                LinkerError::section_data("section resize requested for an unowned section")
            })?;
            self.memory_layout
                .set_materialization(owner, Materialization::SectionRegions);
        }
        Ok(())
    }

    #[inline]
    pub(in crate::linker) fn into_parts(self) -> LinkPlanParts<K, Arch> {
        (
            self.root,
            self.group_order,
            self.entries,
            self.memory_layout,
        )
    }
}