keel-libc 0.1.3

A Rust no_std musl-compatible libc implementation
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
// Copyright (c) 2026 Meng Zhuo
// SPDX-License-Identifier: BSD-3-Clause

// Shared ELF constants and segment-protection mapping for the in-libc
// dynamic linker and the standalone `keel` mapper. Included by both crates.

pub const PT_LOAD: u32 = 1;
pub const PT_DYNAMIC: u32 = 2;
pub const PT_PHDR: u32 = 6;

pub const ELF_MAGIC: [u8; 4] = [0x7f, b'E', b'L', b'F'];

#[inline]
pub unsafe fn is_elf_magic(p: *const u8) -> bool {
    *p == ELF_MAGIC[0]
        && *p.add(1) == ELF_MAGIC[1]
        && *p.add(2) == ELF_MAGIC[2]
        && *p.add(3) == ELF_MAGIC[3]
}

// ELF64 program-header layout.  Both loaders parse phdrs from raw bytes
// because the executable's phdrs are already mapped by the kernel.
pub const PHDR_SIZE: usize = 56;
pub const PH_TYPE: usize = 0;
pub const PH_FLAGS: usize = 4;
pub const PH_OFFSET: usize = 8;
pub const PH_VADDR: usize = 16;
pub const PH_FILESZ: usize = 32;
pub const PH_MEMSZ: usize = 40;
pub const PH_ALIGN: usize = 48;

// ELF64 executable-header offsets used by the loader.
pub const EH_PHOFF: usize = 32;
pub const EH_PHENTSIZE: usize = 54;
pub const EH_PHNUM: usize = 56;

#[inline]
pub unsafe fn ehdr_u16(eh: *const u8, off: usize) -> u16 {
    u16::from_le_bytes(core::ptr::read_unaligned(eh.add(off) as *const [u8; 2]))
}

#[inline]
pub unsafe fn ehdr_u64(eh: *const u8, off: usize) -> u64 {
    u64::from_le_bytes(core::ptr::read_unaligned(eh.add(off) as *const [u8; 8]))
}

#[inline]
pub unsafe fn phdr_u32(ph: *const u8, off: usize) -> u32 {
    u32::from_le_bytes(core::ptr::read_unaligned(ph.add(off) as *const [u8; 4]))
}

#[inline]
pub unsafe fn phdr_u64(ph: *const u8, off: usize) -> u64 {
    u64::from_le_bytes(core::ptr::read_unaligned(ph.add(off) as *const [u8; 8]))
}

#[inline]
pub unsafe fn phdr_type(ph: *const u8) -> u32 {
    phdr_u32(ph, PH_TYPE)
}

pub const PF_R: u32 = 4;
pub const PF_W: u32 = 2;
pub const PF_X: u32 = 1;

pub const PROT_READ: i32 = 1;
pub const PROT_WRITE: i32 = 2;
pub const PROT_EXEC: i32 = 4;
pub const PROT_NONE: i32 = 0;

pub const MAP_PRIVATE: i32 = 0x02;
pub const MAP_FIXED: i32 = 0x10;
pub const MAP_ANONYMOUS: i32 = 0x20;

pub const DT_NULL: u64 = 0;
pub const DT_RELA: u64 = 7;
pub const DT_RELAENT: u64 = 9;
pub const DT_RELASZ: u64 = 8;
pub const DT_RELRSZ: u64 = 35;
pub const DT_RELR: u64 = 36;
pub const DT_RELRENT: u64 = 37;

pub const RELA_ENT_SIZE: usize = 24;

#[allow(dead_code)] // selected only on x86_64
pub const R_X86_64_RELATIVE: u64 = 8;
#[allow(dead_code)] // selected only on aarch64
pub const R_AARCH64_RELATIVE: u64 = 1027;
#[allow(dead_code)] // selected only on riscv64
pub const R_RISCV_RELATIVE: u64 = 3;

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
pub const R_RELATIVE: u64 = R_X86_64_RELATIVE;
#[cfg(target_arch = "aarch64")]
#[allow(dead_code)]
pub const R_RELATIVE: u64 = R_AARCH64_RELATIVE;
#[cfg(target_arch = "riscv64")]
#[allow(dead_code)]
pub const R_RELATIVE: u64 = R_RISCV_RELATIVE;

pub const AT_NULL: u64 = 0;
pub const AT_PHDR: u64 = 3;
pub const AT_PHENT: u64 = 4;
pub const AT_PHNUM: u64 = 5;
pub const AT_PAGESZ: u64 = 6;
pub const AT_ENTRY: u64 = 9;
#[allow(dead_code)] // used by the dynamic linker, not the standalone mapper
pub const AT_BASE: u64 = 7;
pub const AT_UID: u64 = 11;
pub const AT_EUID: u64 = 12;
pub const AT_GID: u64 = 13;
pub const AT_EGID: u64 = 14;
pub const AT_SECURE: u64 = 23;
pub const AT_RANDOM: u64 = 25;
pub const AT_SYSINFO_EHDR: u64 = 33;

/// Iterate a `{ d_tag, d_val }` array in memory, stopping at DT_NULL.
pub struct DynRange {
    p: usize,
    end: usize,
}

impl DynRange {
    #[inline]
    pub fn new(start: usize, size: usize) -> Self {
        Self { p: start, end: start.saturating_add(size) }
    }

    #[inline]
    pub unsafe fn next(&mut self) -> Option<(u64, u64)> {
        if self.p.saturating_add(16) > self.end {
            return None;
        }
        let tag = u64::from_le_bytes(core::ptr::read_unaligned(self.p as *const [u8; 8]));
        let val = u64::from_le_bytes(core::ptr::read_unaligned((self.p + 8) as *const [u8; 8]));
        self.p += 16;
        if tag == DT_NULL {
            return None;
        }
        Some((tag, val))
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct Rela {
    pub r_offset: u64,
    pub r_info: u64,
    pub r_addend: i64,
}

#[inline]
pub unsafe fn read_rela(entry: *const u8) -> Rela {
    Rela {
        r_offset: u64::from_le_bytes(core::ptr::read_unaligned(entry as *const [u8; 8])),
        r_info: u64::from_le_bytes(core::ptr::read_unaligned(entry.add(8) as *const [u8; 8])),
        r_addend: i64::from_le_bytes(core::ptr::read_unaligned(entry.add(16) as *const [u8; 8])),
    }
}

/// Apply one R_*_RELATIVE relocation and return the written address.
#[inline]
pub unsafe fn apply_relative(base: u64, r_offset: u64, r_addend: i64) -> u64 {
    let addr = base.wrapping_add(r_offset);
    let val = (base as i64).wrapping_add(r_addend) as u64;
    *(addr as *mut u64) = val;
    addr
}

/// Apply packed RELR relocations: entries are either an address or a bitmap
/// of the 63 words following the previous address.
pub unsafe fn apply_relr(base: u64, relr_off: u64, relr_sz: u64) {
    if relr_sz == 0 {
        return;
    }
    let table = (base + relr_off) as *const u8;
    let count = (relr_sz / 8) as usize;
    let mut where_: u64 = 0;
    let mut i = 0usize;
    while i < count {
        let e = u64::from_le_bytes(core::ptr::read_unaligned(table.add(i * 8) as *const [u8; 8]));
        if e & 1 == 0 {
            where_ = e;
            let slot = (base + where_) as *mut u64;
            *slot = (*slot).wrapping_add(base);
            where_ += 8;
        } else {
            let mut bits = e >> 1;
            let mut off = where_;
            while bits != 0 {
                if bits & 1 != 0 {
                    let slot = (base + off) as *mut u64;
                    *slot = (*slot).wrapping_add(base);
                }
                bits >>= 1;
                off += 8;
            }
            where_ += 63 * 8;
        }
        i += 1;
    }
}

pub fn prot_from_flags(flags: u32) -> i32 {
    let mut prot = 0;
    if flags & PF_R != 0 {
        prot |= PROT_READ;
    }
    if flags & PF_W != 0 {
        prot |= PROT_WRITE;
    }
    if flags & PF_X != 0 {
        prot |= PROT_EXEC;
    }
    prot
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct Ehdr {
    pub e_ident: [u8; 16],
    pub e_type: u16,
    pub e_machine: u16,
    pub e_version: u32,
    pub e_entry: u64,
    pub e_phoff: u64,
    pub e_shoff: u64,
    pub e_flags: u32,
    pub e_ehsize: u16,
    pub e_phentsize: u16,
    pub e_phnum: u16,
    pub e_shentsize: u16,
    pub e_shnum: u16,
    pub e_shstrndx: u16,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct Phdr {
    pub p_type: u32,
    pub p_flags: u32,
    pub p_offset: u64,
    pub p_vaddr: u64,
    pub p_paddr: u64,
    pub p_filesz: u64,
    pub p_memsz: u64,
    pub p_align: u64,
}

pub const ET_DYN: u16 = 3;
pub const EM_X86_64: u16 = 62;
pub const EM_AARCH64: u16 = 183;
pub const EM_RISCV: u16 = 243;

pub const PAGE_SIZE: usize = 4096;

const MMAP_FAILED: *mut u8 = !0 as *mut u8;

pub fn parse_ehdr(data: &[u8]) -> Result<Ehdr, &'static str> {
    if data.len() < 64 {
        return Err("file too small for ELF header");
    }
    if data[..4] != ELF_MAGIC {
        return Err("bad ELF magic");
    }
    if data[4] != 2 {
        return Err("not ELFCLASS64");
    }
    let ehdr = unsafe { core::ptr::read_unaligned(data.as_ptr() as *const Ehdr) };
    if ehdr.e_machine != EM_X86_64 && ehdr.e_machine != EM_AARCH64 && ehdr.e_machine != EM_RISCV {
        return Err("not a supported EM machine");
    }
    Ok(ehdr)
}

pub fn parse_phdrs<'a>(data: &'a [u8], ehdr: &Ehdr) -> Result<&'a [Phdr], &'static str> {
    if ehdr.e_phentsize as usize != core::mem::size_of::<Phdr>() {
        return Err("unsupported e_phentsize");
    }
    let count = ehdr.e_phnum as usize;
    let bytes = count
        .checked_mul(ehdr.e_phentsize as usize)
        .ok_or("phdr table overflow")?;
    let end = (ehdr.e_phoff as usize)
        .checked_add(bytes)
        .ok_or("phdr table overflow")?;
    if end > data.len() {
        return Err("phdr table extends past EOF");
    }
    let off = ehdr.e_phoff as usize;
    if off % core::mem::align_of::<Phdr>() != 0 {
        return Err("unaligned phdr table");
    }
    let base = unsafe { data.as_ptr().add(off) as *const Phdr };
    if (base as usize) % core::mem::align_of::<Phdr>() != 0 {
        return Err("unaligned phdr table");
    }
    Ok(unsafe { core::slice::from_raw_parts(base, count) })
}

#[allow(unused_unsafe)]
pub fn map_segments(phdrs: &[Phdr], fd: i32, e_type: u16) -> Result<u64, &'static str> {
    let page = PAGE_SIZE as u64;

    let mut min = u64::MAX;
    let mut max = 0u64;
    for p in phdrs.iter().filter(|p| p.p_type == PT_LOAD) {
        let seg_start = p.p_vaddr & !(page - 1);
        let seg_end = p
            .p_vaddr
            .checked_add(p.p_memsz)
            .and_then(|v| v.checked_add(page - 1))
            .ok_or("PT_LOAD range overflow")?
            & !(page - 1);
        min = min.min(seg_start);
        max = max.max(seg_end);
    }
    if min >= max {
        return Err("no PT_LOAD segments");
    }

    let mut load_bias = 0u64;
    if e_type == ET_DYN {
        let span = max.checked_sub(min).ok_or("PT_LOAD span overflow")?;
        let reserve = unsafe {
            sys_mmap(
                core::ptr::null_mut(),
                span as usize,
                PROT_NONE,
                MAP_PRIVATE | MAP_ANONYMOUS,
                -1,
                0,
            )
        };
        if reserve == MMAP_FAILED {
            return Err("mmap failed while reserving ET_DYN span");
        }
        load_bias = (reserve as u64).checked_sub(min).ok_or("load bias overflow")?;
    }

    for ph in phdrs.iter().filter(|p| p.p_type == PT_LOAD) {
        let adj = ph.p_vaddr & (page - 1);
        let map_addr = load_bias
            .checked_add(ph.p_vaddr)
            .and_then(|v| v.checked_sub(adj))
            .ok_or("PT_LOAD address overflow")?;
        let map_off = ph.p_offset.checked_sub(adj).ok_or("PT_LOAD p_offset underflow")?;
        // Map the file-backed pages, not just p_filesz: the pages rounded up
        // to a page boundary may be needed before BSS starts.  Then extend
        // with anonymous pages so that BSS never lands outside the mapping.
        let file_len = ph
            .p_filesz
            .checked_add(adj)
            .and_then(|v| v.checked_add(page - 1))
            .ok_or("PT_LOAD file size overflow")?
            & !(page - 1);
        let mem_len = ph
            .p_memsz
            .checked_add(adj)
            .and_then(|v| v.checked_add(page - 1))
            .ok_or("PT_LOAD memory size overflow")?
            & !(page - 1);

        let prot = prot_from_flags(ph.p_flags);
        if file_len > 0 {
            let ptr = unsafe {
                sys_mmap(
                    map_addr as *mut u8,
                    file_len as usize,
                    prot,
                    MAP_PRIVATE | MAP_FIXED,
                    fd,
                    map_off as i64,
                )
            };
            if ptr == MMAP_FAILED {
                return Err("mmap failed for PT_LOAD segment");
            }
        }
        if mem_len > file_len {
            let bss_map = map_addr.checked_add(file_len).ok_or("PT_LOAD BSS address overflow")?;
            let ptr = unsafe {
                sys_mmap(
                    bss_map as *mut u8,
                    (mem_len - file_len) as usize,
                    prot,
                    MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
                    -1,
                    0,
                )
            };
            if ptr == MMAP_FAILED {
                return Err("mmap failed for PT_LOAD BSS");
            }
        }

        if ph.p_memsz > ph.p_filesz {
            let bss_addr = ph
                .p_vaddr
                .checked_add(ph.p_filesz)
                .and_then(|v| load_bias.checked_add(v))
                .ok_or("PT_LOAD BSS address overflow")?;
            let bss_len = (ph.p_memsz - ph.p_filesz) as usize;
            unsafe { core::ptr::write_bytes(bss_addr as *mut u8, 0, bss_len) };
        }
    }

    Ok(load_bias)
}