univdreams 0.4.0

Byte-identical binary <-> source round-tripping: binary formats, multi-arch decode/encode, a .ud decompiler/compiler, and an x86 emulator with Win16/Win32/Linux hosts.
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
//! ELF loader for the Linux personality.
//!
//! Maps an executable's `PT_LOAD` segments into the MMU and builds the initial
//! process stack (argc / argv / envp / auxv) per the System V ABI, then reports
//! the entry point and stack pointer the run loop jumps to.
//!
//! Both **static** and **dynamically linked** (`PT_INTERP`) executables load:
//! for a dynamic binary the interpreter (`ld-musl`, …) is read from the guest
//! rootfs (the [`MountTable`](crate::emulator::fsmount::MountTable)), mapped at a fixed
//! base, and execution starts in it with a full auxv (`AT_BASE`, `AT_PHDR`,
//! `AT_ENTRY`, `AT_RANDOM`, …) so it can relocate and run the main object.

use crate::format::elf::{EM_386, EM_AARCH64, EM_X86_64, Elf64File};

use super::mem::GuestMem;
use crate::emulator::emulator::Perm;

/// `p_type` of a loadable segment.
const PT_LOAD: u32 = 1;
/// `p_type` of the interpreter path (present iff dynamically linked).
const PT_INTERP: u32 = 3;
/// `e_type` for an executable / a position-independent executable.
const ET_EXEC: u16 = 2;
const ET_DYN: u16 = 3;

/// ELF segment permission flags (`p_flags`).
const PF_X: u32 = 1;
const PF_W: u32 = 2;
const PF_R: u32 = 4;

/// Page size we report to the guest (`AT_PAGESZ`) and align maps to.
const PAGE: u32 = 0x1000;

/// Top of the user stack. The 32-bit interpreter has a 4 GiB space; we
/// keep the classic i386 Linux user/kernel split feel by placing the
/// stack just under 0xC000_0000.
const STACK_TOP: u32 = 0xC000_0000;
/// How much stack to map below [`STACK_TOP`].
const STACK_SIZE: u32 = 0x0080_0000; // 8 MiB

/// Auxiliary-vector tags we populate.
const AT_NULL: u32 = 0;
const AT_PHDR: u32 = 3;
const AT_PHENT: u32 = 4;
const AT_PHNUM: u32 = 5;
const AT_PAGESZ: u32 = 6;
const AT_BASE: u32 = 7;
const AT_ENTRY: u32 = 9;
const AT_UID: u32 = 11;
const AT_EUID: u32 = 12;
const AT_GID: u32 = 13;
const AT_EGID: u32 = 14;
const AT_CLKTCK: u32 = 17;
const AT_SECURE: u32 = 23;
const AT_RANDOM: u32 = 25;
const AT_EXECFN: u32 = 31;

/// Load bias for a position-independent (`ET_DYN`) **main** executable, and the
/// fixed base for the **interpreter** (`ld-musl`). Chosen low so everything
/// fits the 4 GiB MMU, and spaced apart from the mmap arena (`0x4000_0000`) and
/// stack (`0xC000_0000`). The main exe + its `brk` heap live below the interp.
const MAIN_DYN_BASE: u32 = 0x0100_0000;
const INTERP_BASE: u32 = 0x3000_0000;

/// The result of loading a static ELF: where to start executing, the
/// initial stack pointer, and the program break.
#[derive(Debug, Clone)]
pub struct ElfImage {
    pub entry: u32,
    pub stack_ptr: u32,
    pub brk: u32,
    /// `e_machine` of the loaded image (e.g. [`EM_386`]).
    pub machine: u16,
}

/// Errors the loader can surface.
#[derive(Debug, Clone)]
pub enum LoadError {
    Parse(String),
    Dynamic,
    /// A dynamic ELF named an interpreter we couldn't read from the rootfs.
    InterpNotFound(String),
    NoLoadable,
    SegmentOutOfRange {
        offset: u64,
        len: u64,
    },
}

impl std::fmt::Display for LoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LoadError::Parse(e) => write!(f, "ELF parse: {e}"),
            LoadError::Dynamic => write!(
                f,
                "dynamically-linked ELF (PT_INTERP) needs a root filesystem (--rootfs) to load its interpreter"
            ),
            LoadError::InterpNotFound(p) => {
                write!(f, "interpreter {p:?} not found in the root filesystem")
            }
            LoadError::NoLoadable => write!(f, "ELF has no PT_LOAD segments"),
            LoadError::SegmentOutOfRange { offset, len } => {
                write!(
                    f,
                    "PT_LOAD data [{offset:#x}..+{len:#x}] runs past the file"
                )
            }
        }
    }
}

/// Map `bytes` (a static ELF executable) into `mmu` and set up the initial
/// stack. Convenience wrapper over [`load_elf`] with no filesystem — a
/// dynamically linked ELF is rejected with [`LoadError::Dynamic`].
///
/// # Errors
/// [`LoadError`] if the file is unparsable, dynamically linked, has no
/// loadable segments, or a segment's file range is out of bounds.
pub fn load_static(
    mmu: &mut dyn GuestMem,
    bytes: &[u8],
    argv: &[&str],
    envp: &[&str],
) -> Result<ElfImage, LoadError> {
    load_elf(mmu, None, bytes, argv, envp)
}

/// Map `bytes` into `mmu` and set up the initial process stack. Handles both
/// **static** executables and **dynamically linked** ones (`PT_INTERP`): the
/// interpreter (`ld-musl`, …) is read from `mounts` (the guest rootfs), mapped
/// at `INTERP_BASE`, and execution starts in it with a full auxv so it can
/// relocate the main object and run it.
///
/// `mounts` is required for dynamic binaries; a dynamic ELF with `mounts =
/// None` is rejected with [`LoadError::Dynamic`].
///
/// # Errors
/// [`LoadError`] for parse failures, a dynamic binary with no rootfs, a
/// missing/unreadable interpreter, no loadable segments, or an out-of-range
/// segment.
pub fn load_elf(
    mmu: &mut dyn GuestMem,
    mounts: Option<&mut crate::emulator::fsmount::MountTable>,
    bytes: &[u8],
    argv: &[&str],
    envp: &[&str],
) -> Result<ElfImage, LoadError> {
    let elf = Elf64File::parse(bytes).map_err(|e| LoadError::Parse(e.to_string()))?;
    let ptr64 = matches!(elf.ehdr.e_machine, EM_X86_64 | EM_AARCH64);

    let interp_path = interp_of(&elf, bytes);
    if interp_path.is_some() && mounts.is_none() {
        return Err(LoadError::Dynamic);
    }

    // The main object: PIE (`ET_DYN`) gets a load bias; a fixed-address
    // executable maps at its own vaddrs.
    let main_bias = if elf.ehdr.e_type == ET_DYN && interp_path.is_some() {
        MAIN_DYN_BASE
    } else {
        0
    };
    let brk = map_segments(mmu, bytes, &elf, main_bias)?;

    // Stack region.
    mmu.map(STACK_TOP - STACK_SIZE, STACK_SIZE, Perm::R | Perm::W);

    // AT_PHDR: where the program headers ended up in memory.
    let e_phoff = elf.ehdr.e_phoff;
    let phdr_vaddr = elf
        .phdrs
        .iter()
        .find(|p| p.p_type == PT_LOAD && e_phoff >= p.p_offset && e_phoff < p.p_offset + p.p_filesz)
        .map_or(0, |p| {
            main_bias.wrapping_add((p.p_vaddr + (e_phoff - p.p_offset)) as u32)
        });

    let main_entry = main_bias.wrapping_add(elf.ehdr.e_entry as u32);

    // Dynamic: load the interpreter and start execution in it.
    let (entry, at_base) = if let Some(path) = interp_path {
        let mounts = mounts.expect("checked above");
        let interp_bytes = mounts
            .read_file(&path)
            .ok_or_else(|| LoadError::InterpNotFound(path.clone()))?;
        let interp = Elf64File::parse(&interp_bytes)
            .map_err(|e| LoadError::Parse(format!("interp {path}: {e}")))?;
        map_segments(mmu, &interp_bytes, &interp, INTERP_BASE)?;
        (
            INTERP_BASE.wrapping_add(interp.ehdr.e_entry as u32),
            Some(INTERP_BASE),
        )
    } else {
        (main_entry, None)
    };

    let aux = AuxParams {
        phdr: phdr_vaddr,
        phent: u32::from(elf.ehdr.e_phentsize),
        phnum: u32::from(elf.ehdr.e_phnum),
        entry: main_entry,
        base: at_base,
    };
    let stack_ptr = build_stack(mmu, argv, envp, ptr64, &aux)?;

    Ok(ElfImage {
        entry,
        stack_ptr,
        brk,
        machine: elf.ehdr.e_machine,
    })
}

/// The `PT_INTERP` path (the dynamic linker), if this ELF is dynamically
/// linked.
fn interp_of(elf: &Elf64File, bytes: &[u8]) -> Option<String> {
    let ph = elf.phdrs.iter().find(|p| p.p_type == PT_INTERP)?;
    let s = bytes.get(ph.p_offset as usize..(ph.p_offset + ph.p_filesz) as usize)?;
    let s = s.split(|&b| b == 0).next().unwrap_or(s);
    Some(String::from_utf8_lossy(s).into_owned())
}

/// Map every `PT_LOAD` of `elf` into `mmu`, offset by `bias` (0 for a
/// fixed-address object). Populates file bytes then enforces the segment's
/// permissions. Returns the page-aligned high-water mark (the program break /
/// end of the object).
fn map_segments(
    mmu: &mut dyn GuestMem,
    bytes: &[u8],
    elf: &Elf64File,
    bias: u32,
) -> Result<u32, LoadError> {
    let mut high = 0u32;
    let mut mapped_any = false;
    for ph in elf.phdrs.iter().filter(|p| p.p_type == PT_LOAD) {
        mapped_any = true;
        let vaddr = (ph.p_vaddr as u32).wrapping_add(bias);
        let memsz = ph.p_memsz as u32;
        let filesz = ph.p_filesz as u32;
        let offset = ph.p_offset as usize;

        let start = vaddr & !(PAGE - 1);
        let end = (vaddr.wrapping_add(memsz).wrapping_add(PAGE - 1)) & !(PAGE - 1);
        let perm = seg_perm(ph.p_flags);
        // Map writable first so the initializer can populate it; then enforce
        // the segment's real perms (map() overwrites perms, keeps bytes).
        mmu.map(start, end.wrapping_sub(start), Perm::R | Perm::W | perm);

        let len = filesz as usize;
        let data = bytes
            .get(offset..offset + len)
            .ok_or(LoadError::SegmentOutOfRange {
                offset: ph.p_offset,
                len: ph.p_filesz,
            })?;
        mmu.write_initializer(vaddr, data)
            .map_err(|_| LoadError::SegmentOutOfRange {
                offset: ph.p_offset,
                len: ph.p_filesz,
            })?;
        // Zero the `.bss` tail (`[filesz, memsz)`). On a fresh address space the
        // pages are already zero, but an `execve` that reloads over dirty memory
        // leaves stale bytes there — corrupting zero-initialized globals
        // (function pointers, flags) and crashing the program.
        if memsz > filesz {
            let zeros = vec![0u8; (memsz - filesz) as usize];
            let _ = mmu.write_initializer(vaddr.wrapping_add(filesz), &zeros);
        }
        mmu.map(start, end.wrapping_sub(start), perm);

        high = high.max(end);
    }
    if !mapped_any {
        return Err(LoadError::NoLoadable);
    }
    Ok(high)
}

/// True iff `bytes` is an ELF this loader recognises as a Linux i386
/// executable.
#[must_use]
pub fn is_runnable_i386(bytes: &[u8]) -> bool {
    Elf64File::parse(bytes)
        .is_ok_and(|e| e.ehdr.e_machine == EM_386 && matches!(e.ehdr.e_type, ET_EXEC | ET_DYN))
}

/// True iff `bytes` is a static ELF whose `e_machine` an interpreter
/// back-end exists for (i386, x86-64, or aarch64).
#[must_use]
pub fn is_runnable(bytes: &[u8]) -> bool {
    Elf64File::parse(bytes).is_ok_and(|e| {
        matches!(e.ehdr.e_machine, EM_386 | EM_X86_64 | EM_AARCH64)
            && matches!(e.ehdr.e_type, ET_EXEC | ET_DYN)
    })
}

fn seg_perm(p_flags: u32) -> Perm {
    let mut perm = Perm::default();
    if p_flags & PF_R != 0 {
        perm = perm | Perm::R;
    }
    if p_flags & PF_W != 0 {
        perm = perm | Perm::W;
    }
    if p_flags & PF_X != 0 {
        perm = perm | Perm::X;
    }
    perm
}

/// Auxiliary-vector values the caller computed from the loaded image(s).
struct AuxParams {
    phdr: u32,
    phent: u32,
    phnum: u32,
    /// Entry point of the **main** object (not the interpreter).
    entry: u32,
    /// `AT_BASE` — the interpreter's load base, for a dynamic binary.
    base: Option<u32>,
}

/// Write the initial stack and return the final `sp` (points at `argc`).
///
/// `ptr64` selects the word size of the argc/argv/envp/auxv vector: 8-byte
/// for x86-64 / aarch64, 4-byte for i386. The string area and AT_RANDOM
/// block are byte-addressed identically either way.
fn build_stack(
    mmu: &mut dyn GuestMem,
    argv: &[&str],
    envp: &[&str],
    ptr64: bool,
    aux: &AuxParams,
) -> Result<u32, LoadError> {
    let word = if ptr64 { 8u32 } else { 4u32 };
    let mut sp = STACK_TOP;
    let mut push_bytes = |mmu: &mut dyn GuestMem, data: &[u8]| -> u32 {
        sp -= data.len() as u32;
        let _ = mmu.write_initializer(sp, data);
        sp
    };

    // Argument + environment strings (NUL-terminated), recording pointers.
    let mut argv_ptrs = Vec::with_capacity(argv.len());
    for s in argv {
        let mut b = s.as_bytes().to_vec();
        b.push(0);
        argv_ptrs.push(push_bytes(mmu, &b));
    }
    let mut envp_ptrs = Vec::with_capacity(envp.len());
    for s in envp {
        let mut b = s.as_bytes().to_vec();
        b.push(0);
        envp_ptrs.push(push_bytes(mmu, &b));
    }
    // 16 bytes for AT_RANDOM (glibc/musl stack-canary + TLS seed).
    let at_random = push_bytes(mmu, &[0x42u8; 16]);
    // AT_EXECFN points at argv[0]'s string (the program path).
    let execfn = argv_ptrs.first().copied().unwrap_or(0);

    let mut auxv: Vec<(u32, u32)> = vec![
        (AT_PHDR, aux.phdr),
        (AT_PHENT, aux.phent),
        (AT_PHNUM, aux.phnum),
        (AT_PAGESZ, PAGE),
        (AT_ENTRY, aux.entry),
        (AT_RANDOM, at_random),
        (AT_EXECFN, execfn),
        (AT_CLKTCK, 100),
        (AT_SECURE, 0),
        (AT_UID, 0),
        (AT_EUID, 0),
        (AT_GID, 0),
        (AT_EGID, 0),
    ];
    if let Some(base) = aux.base {
        auxv.push((AT_BASE, base));
    }
    auxv.push((AT_NULL, 0));

    // Size of the main vector (words): argc + argv + NULL + envp + NULL
    // + auxv pairs.
    let words = 1 + (argv_ptrs.len() + 1) + (envp_ptrs.len() + 1) + auxv.len() * 2;
    let total = words as u32 * word;
    // 16-byte align the base so sp at entry is aligned.
    let mut wsp = (sp - total) & !0xF;
    let base = wsp;
    let mut put = |mmu: &mut dyn GuestMem, v: u32| {
        if ptr64 {
            let _ = mmu.write_initializer(wsp, &u64::from(v).to_le_bytes());
        } else {
            let _ = mmu.write_initializer(wsp, &v.to_le_bytes());
        }
        wsp += word;
    };
    put(mmu, argv_ptrs.len() as u32); // argc
    for p in &argv_ptrs {
        put(mmu, *p);
    }
    put(mmu, 0); // argv NULL
    for p in &envp_ptrs {
        put(mmu, *p);
    }
    put(mmu, 0); // envp NULL
    for (tag, val) in auxv {
        put(mmu, tag);
        put(mmu, val);
    }
    Ok(base)
}

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

    /// Minimal one-segment ELF32 EXEC (EM_386) with `code` at the entry.
    fn tiny_elf(code: &[u8]) -> Vec<u8> {
        const LOAD: u32 = 0x0804_8000;
        let (ehdr, phdr) = (52u32, 32u32);
        let code_off = ehdr + phdr;
        let filesz = code_off + code.len() as u32;
        let entry = LOAD + code_off;
        let mut e = Vec::new();
        e.extend_from_slice(&[0x7f, b'E', b'L', b'F', 1, 1, 1, 0]);
        e.extend_from_slice(&[0u8; 8]);
        e.extend_from_slice(&2u16.to_le_bytes());
        e.extend_from_slice(&3u16.to_le_bytes());
        e.extend_from_slice(&1u32.to_le_bytes());
        e.extend_from_slice(&entry.to_le_bytes());
        e.extend_from_slice(&ehdr.to_le_bytes());
        e.extend_from_slice(&0u32.to_le_bytes());
        e.extend_from_slice(&0u32.to_le_bytes());
        e.extend_from_slice(&(ehdr as u16).to_le_bytes());
        e.extend_from_slice(&(phdr as u16).to_le_bytes());
        e.extend_from_slice(&1u16.to_le_bytes());
        e.extend_from_slice(&[0u8; 6]);
        e.extend_from_slice(&1u32.to_le_bytes()); // p_type = PT_LOAD
        e.extend_from_slice(&0u32.to_le_bytes()); // p_offset
        e.extend_from_slice(&LOAD.to_le_bytes());
        e.extend_from_slice(&LOAD.to_le_bytes());
        e.extend_from_slice(&filesz.to_le_bytes());
        e.extend_from_slice(&filesz.to_le_bytes());
        e.extend_from_slice(&5u32.to_le_bytes()); // R|X
        e.extend_from_slice(&0x1000u32.to_le_bytes());
        e.extend_from_slice(code);
        e
    }

    #[test]
    fn maps_segment_and_lays_out_stack() {
        let mut mmu = Mmu::new();
        let elf = tiny_elf(&[0x90, 0x90, 0xc3]); // nop;nop;ret
        let img = load_static(&mut mmu, &elf, &["prog", "arg1"], &["X=1"]).expect("load");

        assert_eq!(img.entry, 0x0804_8000 + 52 + 32);
        assert_eq!(img.machine, EM_386);
        assert_eq!(mmu.load8(img.entry).unwrap(), 0x90);

        assert_eq!(mmu.load32(img.stack_ptr).unwrap(), 2, "argc");
        let argv0 = mmu.load32(img.stack_ptr + 4).unwrap();
        let argv1 = mmu.load32(img.stack_ptr + 8).unwrap();
        assert_eq!(mmu.load32(img.stack_ptr + 12).unwrap(), 0, "argv NULL");
        let read = |a: u32| {
            (0..)
                .map(|i| mmu.load8(a + i).unwrap())
                .take_while(|&b| b != 0)
                .collect::<Vec<_>>()
        };
        assert_eq!(read(argv0), b"prog");
        assert_eq!(read(argv1), b"arg1");
        assert_ne!(mmu.load32(img.stack_ptr + 16).unwrap(), 0, "envp[0]");
        assert_eq!(mmu.load32(img.stack_ptr + 20).unwrap(), 0, "envp NULL");
        assert_eq!(img.stack_ptr & 0xF, 0, "esp 16-byte aligned");
    }

    #[test]
    fn rejects_dynamic() {
        let mut e = tiny_elf(&[0xc3]);
        e[52] = 3; // p_type PT_LOAD -> PT_INTERP
        assert!(matches!(
            load_static(&mut Mmu::new(), &e, &["x"], &[]),
            Err(LoadError::Dynamic)
        ));
    }

    /// An ELF32 `ET_DYN` with a second program header of type `PT_INTERP`
    /// naming `interp`. The first PT_LOAD maps file offset 0 (covering the
    /// headers + interp string + code) at vaddr 0 so it's a clean PIE.
    fn tiny_dyn(code: &[u8], interp: &str) -> Vec<u8> {
        let (ehdr, phdr) = (52u32, 32u32);
        let phnum = 2u32;
        let interp_off = ehdr + phdr * phnum; // 116
        let mut interp_b = interp.as_bytes().to_vec();
        interp_b.push(0);
        let code_off = interp_off + interp_b.len() as u32;
        let filesz = code_off + code.len() as u32;
        let entry = code_off; // vaddr 0 base
        let mut e = Vec::new();
        e.extend_from_slice(&[0x7f, b'E', b'L', b'F', 1, 1, 1, 0]);
        e.extend_from_slice(&[0u8; 8]);
        e.extend_from_slice(&3u16.to_le_bytes()); // e_type = ET_DYN
        e.extend_from_slice(&3u16.to_le_bytes()); // e_machine = EM_386
        e.extend_from_slice(&1u32.to_le_bytes());
        e.extend_from_slice(&entry.to_le_bytes());
        e.extend_from_slice(&ehdr.to_le_bytes()); // e_phoff
        e.extend_from_slice(&0u32.to_le_bytes());
        e.extend_from_slice(&0u32.to_le_bytes());
        e.extend_from_slice(&(ehdr as u16).to_le_bytes());
        e.extend_from_slice(&(phdr as u16).to_le_bytes());
        e.extend_from_slice(&(phnum as u16).to_le_bytes());
        e.extend_from_slice(&[0u8; 6]);
        // phdr[0] PT_LOAD: file [0, filesz] at vaddr 0.
        e.extend_from_slice(&1u32.to_le_bytes()); // PT_LOAD
        e.extend_from_slice(&0u32.to_le_bytes()); // p_offset
        e.extend_from_slice(&0u32.to_le_bytes()); // p_vaddr
        e.extend_from_slice(&0u32.to_le_bytes()); // p_paddr
        e.extend_from_slice(&filesz.to_le_bytes());
        e.extend_from_slice(&filesz.to_le_bytes());
        e.extend_from_slice(&5u32.to_le_bytes()); // R|X
        e.extend_from_slice(&0x1000u32.to_le_bytes());
        // phdr[1] PT_INTERP.
        e.extend_from_slice(&3u32.to_le_bytes()); // PT_INTERP
        e.extend_from_slice(&interp_off.to_le_bytes()); // p_offset
        e.extend_from_slice(&interp_off.to_le_bytes()); // p_vaddr
        e.extend_from_slice(&interp_off.to_le_bytes()); // p_paddr
        e.extend_from_slice(&(interp_b.len() as u32).to_le_bytes());
        e.extend_from_slice(&(interp_b.len() as u32).to_le_bytes());
        e.extend_from_slice(&4u32.to_le_bytes()); // R
        e.extend_from_slice(&1u32.to_le_bytes());
        debug_assert_eq!(e.len() as u32, interp_off);
        e.extend_from_slice(&interp_b);
        e.extend_from_slice(code);
        e
    }

    #[test]
    fn dynamic_loads_interp_from_mounts() {
        use crate::emulator::fsmount::MountTable;
        // The interpreter is just another tiny ELF placed in the rootfs.
        let interp = tiny_elf(&[0x90, 0xc3]);
        let interp_entry = u32::from_le_bytes(interp[24..28].try_into().unwrap());
        let mut mounts = MountTable::new();
        mounts.insert("/lib/myld", interp.clone());

        let main = tiny_dyn(&[0xc3], "/lib/myld");
        let mut mmu = Mmu::new();
        let img = load_elf(&mut mmu, Some(&mut mounts), &main, &["prog"], &[]).expect("dyn load");

        // Execution starts in the interpreter (mapped at INTERP_BASE).
        assert_eq!(img.entry, INTERP_BASE.wrapping_add(interp_entry));
        // The interpreter's first instruction is reachable in memory.
        assert_eq!(mmu.load8(img.entry).unwrap(), 0x90);
        // The main object was biased to MAIN_DYN_BASE; its entry byte (0xc3)
        // sits at MAIN_DYN_BASE + code_off.
        assert_eq!(img.machine, EM_386);
    }

    #[test]
    fn dynamic_without_interp_in_mounts_errors() {
        use crate::emulator::fsmount::MountTable;
        let main = tiny_dyn(&[0xc3], "/lib/absent");
        let mut mounts = MountTable::new();
        assert!(matches!(
            load_elf(&mut Mmu::new(), Some(&mut mounts), &main, &["p"], &[]),
            Err(LoadError::InterpNotFound(_))
        ));
        // And with no rootfs at all, it's the friendlier Dynamic error.
        assert!(matches!(
            load_elf(&mut Mmu::new(), None, &main, &["p"], &[]),
            Err(LoadError::Dynamic)
        ));
    }
}