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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use super::{KeyResolver, ResolvedKey};
use crate::{
    Error, IoError, LinkResolverError, LinkerError, ParseEhdrError, Result,
    input::{ElfFile, ElfReader, Path, PathBuf},
    linker::{DependencyRequest, RootRequest},
    relocation::RelocationArch,
    sync::Arc,
    tls::TlsResolver,
};
use alloc::{boxed::Box, vec::Vec};
use core::{fmt, marker::PhantomData};

fn expand_origin(value: &str, origin: &Path) -> PathBuf {
    PathBuf::from(
        value
            .replace("${ORIGIN}", origin.as_str())
            .replace("$ORIGIN", origin.as_str()),
    )
}

/// Runtime directory provider used by [`SearchPathEntry::Dynamic`].
///
/// Implementations append directories to `out` in the order they should be
/// searched for `request.requested()`.
pub type SearchDirProvider =
    dyn for<'req> Fn(CandidateRequest<'req>, &mut Vec<PathBuf>) -> Result<()> + 'static;

/// One ordered search-path entry.
#[derive(Clone)]
pub enum SearchPathEntry {
    /// A fixed directory joined with the requested value when it has no
    /// directory separators.
    Dir(PathBuf),
    /// A runtime directory source that can inspect the current request.
    Dynamic(Arc<SearchDirProvider>),
}

impl SearchPathEntry {
    /// Creates a fixed search directory entry.
    #[inline]
    pub fn dir(dir: impl Into<PathBuf>) -> Self {
        Self::Dir(dir.into())
    }

    /// Creates a callback-backed search directory entry.
    #[inline]
    pub fn dynamic<F>(resolver: F) -> Self
    where
        F: for<'req> Fn(CandidateRequest<'req>, &mut Vec<PathBuf>) -> Result<()> + 'static,
    {
        Self::Dynamic(Arc::from(Box::new(resolver) as Box<SearchDirProvider>))
    }
}

impl fmt::Debug for SearchPathEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Dir(dir) => f.debug_tuple("Dir").field(dir).finish(),
            Self::Dynamic(_) => f.write_str("Dynamic(..)"),
        }
    }
}

/// Search request used to build filesystem candidates for a root or dependency.
#[derive(Clone, Copy)]
pub enum CandidateRequest<'a> {
    /// Resolving the root key passed to [`KeyResolver::load_root`].
    Root {
        /// Path requested by the root load.
        requested: &'a Path,
    },
    /// Resolving one `DT_NEEDED` entry for an already-loaded owner.
    Dependency {
        /// Dependency name after applying `$ORIGIN` to the requested value.
        requested: &'a Path,
        /// Diagnostic name of the owner that requested this dependency.
        owner_name: &'a str,
        /// Loaded path/key of the owner that requested this dependency.
        owner_path: &'a Path,
        /// Raw `DT_RUNPATH` value, when present.
        runpath: Option<&'a str>,
        /// Raw `DT_RPATH` value, when present.
        rpath: Option<&'a str>,
    },
}

impl<'a> CandidateRequest<'a> {
    /// Creates a root candidate request.
    #[inline]
    pub const fn root(requested: &'a Path) -> Self {
        Self::Root { requested }
    }

    /// Creates a dependency candidate request.
    #[inline]
    pub const fn dependency(
        requested: &'a Path,
        owner_name: &'a str,
        owner_path: &'a Path,
        runpath: Option<&'a str>,
        rpath: Option<&'a str>,
    ) -> Self {
        Self::Dependency {
            requested,
            owner_name,
            owner_path,
            runpath,
            rpath,
        }
    }
}

impl<'a> CandidateRequest<'a> {
    /// Returns the requested root path or dependency name/path.
    #[inline]
    pub const fn requested(&self) -> &'a Path {
        match self {
            Self::Root { requested } | Self::Dependency { requested, .. } => requested,
        }
    }

    /// Returns the owner name for dependency requests.
    #[inline]
    pub const fn owner_name(&self) -> Option<&'a str> {
        match self {
            Self::Root { .. } => None,
            Self::Dependency { owner_name, .. } => Some(owner_name),
        }
    }

    /// Returns the owner path for dependency requests.
    #[inline]
    pub const fn owner_path(&self) -> Option<&'a Path> {
        match self {
            Self::Root { .. } => None,
            Self::Dependency { owner_path, .. } => Some(owner_path),
        }
    }

    /// Returns the owner directory used for `$ORIGIN` expansion.
    #[inline]
    pub fn origin(&self) -> Option<&'a Path> {
        match self {
            Self::Root { .. } => None,
            Self::Dependency { owner_path, .. } => Some(owner_path.parent()),
        }
    }

    /// Returns expanded `DT_RUNPATH` directories for dependency requests.
    #[inline]
    pub fn runpath(&self) -> Option<Vec<PathBuf>> {
        match self {
            Self::Root { .. } => None,
            Self::Dependency { runpath, .. } => self.expand_dynamic_path_list(*runpath),
        }
    }

    /// Returns expanded `DT_RPATH` directories for dependency requests.
    #[inline]
    pub fn rpath(&self) -> Option<Vec<PathBuf>> {
        match self {
            Self::Root { .. } => None,
            Self::Dependency { rpath, .. } => self.expand_dynamic_path_list(*rpath),
        }
    }

    fn expand_dynamic_path_list(&self, path_list: Option<&str>) -> Option<Vec<PathBuf>> {
        let Self::Dependency {
            requested,
            owner_path,
            ..
        } = *self
        else {
            return None;
        };

        if requested.has_dir_separator() {
            return None;
        }

        let origin = owner_path.parent();
        Some(
            path_list?
                .split(':')
                .filter(|dir| !dir.is_empty())
                .map(|dir| expand_origin(dir, origin))
                .collect(),
        )
    }
}

/// Context passed to existing-candidate reuse callbacks.
pub struct CandidateContext<'a, LinkKey> {
    candidate: &'a Path,
    key: &'a LinkKey,
}

// Keep these impls manual so callback contexts remain copyable for any LinkKey.
impl<LinkKey> Clone for CandidateContext<'_, LinkKey> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<LinkKey> Copy for CandidateContext<'_, LinkKey> {}

impl<'a, LinkKey> CandidateContext<'a, LinkKey> {
    #[inline]
    fn new(candidate: &'a Path, key: &'a LinkKey) -> Self {
        Self { candidate, key }
    }

    /// Returns the concrete filesystem candidate currently being considered.
    #[inline]
    pub const fn candidate(&self) -> &'a Path {
        self.candidate
    }

    /// Returns the key that would be used if the current candidate were loaded.
    #[inline]
    pub const fn key(&self) -> &'a LinkKey {
        self.key
    }
}

impl<LinkKey: fmt::Debug> fmt::Debug for CandidateContext<'_, LinkKey> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CandidateContext")
            .field("candidate", &self.candidate)
            .field("key", &self.key)
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for CandidateRequest<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Root { requested } => f
                .debug_struct("Root")
                .field("requested", requested)
                .finish(),
            Self::Dependency {
                requested,
                owner_name,
                owner_path,
                runpath,
                rpath,
            } => f
                .debug_struct("Dependency")
                .field("requested", requested)
                .field("owner_name", owner_name)
                .field("owner_path", owner_path)
                .field("runpath", runpath)
                .field("rpath", rpath)
                .finish(),
        }
    }
}

/// Chooses which linker key [`SearchPathResolver`] commits for a resolved file.
pub trait KeyRule<LinkKey> {
    /// Returns the key for a load resolved to `candidate`.
    fn key_for_candidate(candidate: &Path) -> LinkKey;
}

/// Default filesystem key behavior for [`SearchPathResolver`].
///
/// Loads use the concrete resolved candidate path.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PathKey;

impl<LinkKey> KeyRule<LinkKey> for PathKey
where
    LinkKey: From<PathBuf>,
{
    #[inline]
    fn key_for_candidate(candidate: &Path) -> LinkKey {
        LinkKey::from(PathBuf::from(candidate))
    }
}

/// Uses the resolved candidate's last path component as the linker key.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FileNameKey;

impl<LinkKey> KeyRule<LinkKey> for FileNameKey
where
    LinkKey: From<PathBuf>,
{
    #[inline]
    fn key_for_candidate(candidate: &Path) -> LinkKey {
        LinkKey::from(PathBuf::from(candidate.file_name()))
    }
}

/// Filesystem-backed dependency resolver for [`Linker`](crate::Linker).
///
/// `SearchPathResolver` is an opt-in convenience resolver for callers whose
/// linker keys can be viewed as loader paths and constructed from resolved
/// paths. Root requests and dependencies with directory separators are tried
/// directly. Plain-name searches walk the ordered
/// [`SearchPathEntry`] list.
///
/// This resolver intentionally does not model the host dynamic linker's global
/// policy: it does not read `LD_LIBRARY_PATH`, system cache files, or default
/// system library directories unless callers add runtime directory providers
/// for them.
pub struct SearchPathResolver<LinkKey = PathBuf, Rule = PathKey> {
    entries: Vec<SearchPathEntry>,
    _marker: PhantomData<fn() -> (LinkKey, Rule)>,
}

// Keep this impl manual so cloning a resolver does not require LinkKey or Rule to be Clone.
impl<LinkKey, Rule> Clone for SearchPathResolver<LinkKey, Rule> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            entries: self.entries.clone(),
            _marker: PhantomData,
        }
    }
}

impl<LinkKey, Rule> Default for SearchPathResolver<LinkKey, Rule> {
    fn default() -> Self {
        Self::empty()
    }
}

impl<LinkKey, Rule> fmt::Debug for SearchPathResolver<LinkKey, Rule> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SearchPathResolver")
            .field("entries", &self.entries)
            .field("link_key", &core::any::type_name::<LinkKey>())
            .field("rule", &core::any::type_name::<Rule>())
            .finish()
    }
}

impl<LinkKey> SearchPathResolver<LinkKey, PathKey> {
    /// Creates an empty search-path resolver using the default path key rule.
    #[inline]
    pub fn new() -> Self {
        Self::empty()
    }
}

impl<LinkKey, Rule> SearchPathResolver<LinkKey, Rule> {
    #[inline]
    fn empty() -> Self {
        Self {
            entries: Vec::new(),
            _marker: PhantomData,
        }
    }

    /// Appends one search-path entry.
    pub fn push_entry(&mut self, entry: SearchPathEntry) -> &mut Self {
        self.entries.push(entry);
        self
    }

    /// Appends a fixed search directory.
    pub fn push_fixed_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self {
        self.push_entry(SearchPathEntry::Dir(dir.into()))
    }

    /// Appends a callback that can provide search directories per request.
    pub fn push_search_dir_provider<F>(&mut self, provider: F) -> &mut Self
    where
        F: for<'req> Fn(CandidateRequest<'req>, &mut Vec<PathBuf>) -> Result<()> + 'static,
    {
        self.push_entry(SearchPathEntry::Dynamic(Arc::from(
            Box::new(provider) as Box<SearchDirProvider>
        )))
    }

    /// Returns the configured search-path entries in lookup order.
    #[inline]
    pub fn entries(&self) -> &[SearchPathEntry] {
        &self.entries
    }

    fn resolve_key<F>(
        &self,
        request: CandidateRequest<'_>,
        contains_key: &dyn Fn(&LinkKey) -> bool,
        reuse: &F,
    ) -> Result<Option<ResolvedCandidate<LinkKey>>>
    where
        Rule: KeyRule<LinkKey>,
        LinkKey: AsRef<Path>,
        F: for<'req> Fn(CandidateContext<'req, LinkKey>) -> Result<Option<LinkKey>> + ?Sized,
    {
        let try_candidate = |candidate: &Path| -> Result<Option<ResolvedCandidate<LinkKey>>> {
            let key = Rule::key_for_candidate(candidate);
            if contains_key(&key) {
                return Ok(Some(ResolvedCandidate::Existing(key)));
            }

            let Some(file) = Self::open_elf(candidate)? else {
                return Ok(None);
            };

            let context = CandidateContext::new(candidate, &key);
            if let Some(existing) = reuse(context)? {
                return Ok(Some(ResolvedCandidate::Existing(existing)));
            }

            Ok(Some(ResolvedCandidate::Load { key, file }))
        };

        let requested = request.requested();
        let has_dir_separator = requested.has_dir_separator();
        if has_dir_separator {
            if let Some(resolved) = try_candidate(requested)? {
                return Ok(Some(resolved));
            }
            return Ok(None);
        }

        let mut dynamic_dirs = Vec::new();
        for entry in &self.entries {
            match entry {
                SearchPathEntry::Dir(dir) => {
                    let candidate = dir.join(requested.as_str());
                    if let Some(resolved) = try_candidate(candidate.as_path())? {
                        return Ok(Some(resolved));
                    }
                }
                SearchPathEntry::Dynamic(resolver) => {
                    dynamic_dirs.clear();
                    resolver(request, &mut dynamic_dirs)?;
                    for dir in &dynamic_dirs {
                        let candidate = dir.join(requested.as_str());
                        if let Some(resolved) = try_candidate(candidate.as_path())? {
                            return Ok(Some(resolved));
                        }
                    }
                }
            }
        }

        Ok(None)
    }

    fn resolved_key<'cfg, Arch, Tls>(
        resolved: ResolvedCandidate<LinkKey>,
    ) -> ResolvedKey<'cfg, LinkKey, Arch, Tls>
    where
        LinkKey: 'cfg,
        Arch: RelocationArch,
        Tls: TlsResolver<Arch>,
    {
        match resolved {
            ResolvedCandidate::Existing(key) => ResolvedKey::existing(key),
            ResolvedCandidate::Load { key, file } => ResolvedKey::load(key, file),
        }
    }

    /// Resolves a root key using the configured search policy plus a
    /// per-operation reuse callback.
    pub fn load_root_with<'cfg, Arch, Tls, F>(
        &self,
        req: &RootRequest<'_, LinkKey>,
        reuse: &F,
    ) -> Result<ResolvedKey<'cfg, LinkKey, Arch, Tls>>
    where
        Rule: KeyRule<LinkKey>,
        LinkKey: Clone + AsRef<Path> + 'cfg,
        Arch: RelocationArch,
        Tls: TlsResolver<Arch>,
        F: for<'req> Fn(CandidateContext<'req, LinkKey>) -> Result<Option<LinkKey>> + ?Sized,
    {
        let contains_key = |key: &LinkKey| req.contains_key(key);
        if let Some(resolved) = self.resolve_key(
            CandidateRequest::root(req.key().as_ref()),
            &contains_key,
            reuse,
        )? {
            return Ok(Self::resolved_key(resolved));
        }

        Err(LinkerError::resolver(LinkResolverError::RootNotFound).into())
    }

    /// Resolves a dependency using the configured search policy plus a
    /// per-operation reuse callback.
    pub fn resolve_dependency_with<'cfg, Arch, Tls, F>(
        &self,
        req: &DependencyRequest<'_, LinkKey>,
        reuse: &F,
    ) -> Result<ResolvedKey<'cfg, LinkKey, Arch, Tls>>
    where
        Rule: KeyRule<LinkKey>,
        LinkKey: Clone + AsRef<Path> + 'cfg,
        Arch: RelocationArch,
        Tls: TlsResolver<Arch>,
        F: for<'req> Fn(CandidateContext<'req, LinkKey>) -> Result<Option<LinkKey>> + ?Sized,
    {
        let origin = req.owner_path().parent();
        let needed = expand_origin(req.needed(), origin);
        let request = CandidateRequest::dependency(
            needed.as_path(),
            req.owner_name(),
            req.owner_path(),
            req.runpath(),
            req.rpath(),
        );
        let contains_key = |key: &LinkKey| req.contains_key(key);
        if let Some(resolved) = self.resolve_key(request, &contains_key, reuse)? {
            return Ok(Self::resolved_key(resolved));
        }

        Err(req.unresolved())
    }

    /// Open `path` if it exists, returning `Ok(None)` for ordinary open
    /// failures and propagating parse/read errors for files that were found.
    fn open_elf(path: &Path) -> Result<Option<ElfFile>> {
        let file = match ElfFile::from_path(path) {
            Ok(file) => file,
            Err(Error::Io(IoError::OpenFailed { .. })) => return Ok(None),
            Err(err) => return Err(err),
        };

        let mut magic = [0; 4];
        file.read(&mut magic, 0)?;
        if magic == *b"\x7fELF" {
            Ok(Some(file))
        } else {
            Err(ParseEhdrError::InvalidMagic.into())
        }
    }
}

enum ResolvedCandidate<LinkKey> {
    Existing(LinkKey),
    Load { key: LinkKey, file: ElfFile },
}

impl<LinkKey, Arch, Tls, Rule> KeyResolver<LinkKey, Arch, LinkKey, Tls>
    for SearchPathResolver<LinkKey, Rule>
where
    Rule: KeyRule<LinkKey>,
    LinkKey: Clone + AsRef<Path>,
    Arch: RelocationArch,
    Tls: TlsResolver<Arch>,
{
    fn load_root<'cfg>(
        &self,
        req: &RootRequest<'_, LinkKey>,
    ) -> Result<ResolvedKey<'cfg, LinkKey, Arch, Tls>>
    where
        LinkKey: 'cfg,
    {
        let no_reuse = |_context: CandidateContext<'_, LinkKey>| Ok(None);
        self.load_root_with::<Arch, Tls, _>(req, &no_reuse)
    }

    fn resolve_dependency<'cfg>(
        &self,
        req: &DependencyRequest<'_, LinkKey>,
    ) -> Result<ResolvedKey<'cfg, LinkKey, Arch, Tls>>
    where
        LinkKey: 'cfg,
    {
        let no_reuse = |_context: CandidateContext<'_, LinkKey>| Ok(None);
        self.resolve_dependency_with::<Arch, Tls, _>(req, &no_reuse)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct NonCloneKey;
    struct NonCloneRule;

    #[test]
    fn search_path_resolver_clone_does_not_require_key_or_rule_clone() {
        fn assert_clone<T: Clone>() {}

        assert_clone::<SearchPathResolver<NonCloneKey, NonCloneRule>>();
    }
}