crabc-libc 0.7.8

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
411
// Copyright (c) 2026 Meng Zhuo
// SPDX-License-Identifier: BSD-3-Clause

// dladdr / dl_iterate_phdr / dlinfo.

#[repr(C)]
pub struct DlInfo {
    pub dli_fname: *const c_char,
    pub dli_fbase: *mut c_void,
    pub dli_sname: *const c_char,
    pub dli_saddr: *mut c_void,
}

#[repr(C)]
pub struct DlPhdrInfo {
    pub dlpi_addr: u64,
    pub dlpi_name: *const c_char,
    pub dlpi_phdr: *const u8,
    pub dlpi_phnum: u16,
    __pad0: u16,
    __pad1: u32,
    pub dlpi_adds: u64,
    pub dlpi_subs: u64,
    pub dlpi_tls_modid: usize,
    pub dlpi_tls_data: *mut c_void,
}

type DlIterateCb = unsafe extern "C" fn(*mut DlPhdrInfo, usize, *mut c_void) -> c_int;
type LdsoIterateFn = unsafe extern "C" fn(Option<DlIterateCb>, *mut c_void) -> c_int;
type LdsoDlinfoFn = unsafe extern "C" fn(*mut c_void, c_int, *mut c_void) -> c_int;

static mut LDSO_ITERATE: Option<LdsoIterateFn> = None;
static mut LDSO_DLINFO: Option<LdsoDlinfoFn> = None;

#[no_mangle]
pub unsafe extern "C" fn __ldso_register_dl_iterate(f: LdsoIterateFn) {
    LDSO_ITERATE = Some(f);
}

#[no_mangle]
pub unsafe extern "C" fn __ldso_register_dlinfo(f: LdsoDlinfoFn) {
    LDSO_DLINFO = Some(f);
}

// ------------------------------------------------------------------
// /proc/self/maps
// ------------------------------------------------------------------

static mut MAPS_PATH: [c_char; PATH_MAX_V] = [0; PATH_MAX_V];
static mut MAPS_LINE: *mut c_char = core::ptr::null_mut();
static mut MAPS_LINE_CAP: usize = 0;

unsafe fn maps_parse_hex(p: *const u8) -> (u64, *const u8) {
    let mut v: u64 = 0;
    let mut q = p;
    loop {
        let c = *q;
        let d = if c >= b'0' && c <= b'9' {
            (c - b'0') as u64
        } else if c >= b'a' && c <= b'f' {
            (c - b'a' + 10) as u64
        } else if c >= b'A' && c <= b'F' {
            (c - b'A' + 10) as u64
        } else {
            break;
        };
        v = (v << 4) | d;
        q = q.add(1);
    }
    (v, q)
}

/// Find the mapping containing `addr`: (base, path). The path is copied into
/// MAPS_PATH and must be consumed before the next call.
unsafe fn maps_lookup(addr: usize) -> (u64, u64, *const c_char, bool) {
    MAPS_PATH[0] = 0;
    let f = crate::fopen(b"/proc/self/maps\0".as_ptr() as *const c_char, b"r\0".as_ptr() as *const c_char);
    if f.is_null() { return (0, 0, MAPS_PATH.as_ptr(), false); }
    let mut start_out = 0u64;
    let mut off_out = 0u64;
    let mut found = false;
    while crate::getline(&raw mut MAPS_LINE, &raw mut MAPS_LINE_CAP, f) >= 0 {
        let p = MAPS_LINE as *const u8;
        let (start, after) = maps_parse_hex(p);
        if *after != b'-' { continue; }
        let (end, after2) = maps_parse_hex(after.add(1));
        if (addr as u64) < start || (addr as u64) >= end { continue; }
        // Field order after the range: perms offset dev inode path.
        let mut q = after2;
        while *q == b' ' { q = q.add(1); }
        while *q != 0 && *q != b' ' { q = q.add(1); }
        while *q == b' ' { q = q.add(1); }
        let (off, after_off) = maps_parse_hex(q);
        start_out = start;
        off_out = off;
        let mut q2 = after_off;
        for _ in 0..2 {
            while *q2 == b' ' { q2 = q2.add(1); }
            while *q2 != 0 && *q2 != b' ' { q2 = q2.add(1); }
        }
        while *q2 == b' ' { q2 = q2.add(1); }
        if *q2 != 0 {
            let mut i = 0usize;
            while *q2 != 0 && *q2 != b'\n' && i + 1 < MAPS_PATH.len() {
                MAPS_PATH[i] = *q2 as c_char;
                i += 1;
                q2 = q2.add(1);
            }
            MAPS_PATH[i] = 0;
        }
        found = true;
        break;
    }
    crate::fclose(f);
    (start_out, off_out, MAPS_PATH.as_ptr(), found)
}

/// Compute the load bias for a mapping given the mapping start and file offset.
/// For a PT_LOAD segment, start = bias + p_vaddr - adj and off = p_offset - adj,
/// so bias = (start - off) - (p_vaddr - p_offset).
unsafe fn elf_bias_for(path: *const c_char, start: u64, off: u64) -> u64 {
    let fd = crate::open(path, 0, 0);
    if fd < 0 { return start.wrapping_sub(off); }
    let mut eh = [0u8; 64];
    if crate::pread(fd, eh.as_mut_ptr() as *mut c_void, 64, 0) != 64 || eh[0] != 0x7f {
        crate::close(fd);
        return start.wrapping_sub(off);
    }
    let e_phoff = rd64(eh.as_ptr().add(0x20));
    let e_phentsize = rd16(eh.as_ptr().add(0x36)) as usize;
    let e_phnum = rd16(eh.as_ptr().add(0x38)) as usize;
    if e_phentsize != 56 || e_phnum == 0 || e_phnum > 512 {
        crate::close(fd);
        return start.wrapping_sub(off);
    }
    let total = e_phnum * e_phentsize;
    let ph = crate::malloc(total) as *mut u8;
    if ph.is_null() {
        crate::close(fd);
        return start.wrapping_sub(off);
    }
    let got = crate::pread(fd, ph as *mut c_void, total, e_phoff as i64);
    crate::close(fd);
    if got != total as isize {
        crate::free(ph as *mut c_void);
        return start.wrapping_sub(off);
    }
    let mut bias = start.wrapping_sub(off);
    for i in 0..e_phnum {
        let p = ph.add(i * e_phentsize);
        let p_type = rd32(p);
        if p_type != 1 { continue; }
        let p_offset = rd64(p.add(8));
        let p_vaddr = rd64(p.add(0x10));
        // The loader maps each segment at file offset p_offset - (p_vaddr&0xfff).
        let map_off = p_offset.wrapping_sub(p_vaddr & 0xfff);
        if off == map_off {
            bias = start.wrapping_sub(off).wrapping_sub(p_vaddr.wrapping_sub(p_offset));
            break;
        }
    }
    crate::free(ph as *mut c_void);
    bias
}

// ------------------------------------------------------------------
// ELF symbol lookup (for dladdr)
// ------------------------------------------------------------------

static mut DLADDR_NAME: [c_char; PATH_MAX_V] = [0; PATH_MAX_V];

unsafe fn rd16(p: *const u8) -> u16 { core::ptr::read_unaligned(p as *const u16) }
unsafe fn rd32(p: *const u8) -> u32 { core::ptr::read_unaligned(p as *const u32) }
unsafe fn rd64(p: *const u8) -> u64 { core::ptr::read_unaligned(p as *const u64) }

unsafe fn elf_symbol_lookup(path: *const c_char, addr: u64) -> (bool, u64) {
    let fd = crate::open(path, 0, 0);
    if fd < 0 { return (false, 0); }
    let mut eh = [0u8; 64];
    if crate::pread(fd, eh.as_mut_ptr() as *mut c_void, 64, 0) != 64 {
        crate::close(fd);
        return (false, 0);
    }
    if eh[0] != 0x7f || eh[1] != b'E' || eh[2] != b'L' || eh[3] != b'F' {
        crate::close(fd);
        return (false, 0);
    }
    let e_shoff = rd64(eh.as_ptr().add(0x28));
    let e_shentsize = rd16(eh.as_ptr().add(0x3a)) as usize;
    let e_shnum = rd16(eh.as_ptr().add(0x3c)) as usize;
    if e_shoff == 0 || e_shentsize < 64 || e_shnum == 0 || e_shnum > 4096 {
        crate::close(fd);
        return (false, 0);
    }
    let shtotal = e_shnum * e_shentsize;
    let sh = crate::malloc(shtotal) as *mut u8;
    if sh.is_null() { crate::close(fd); return (false, 0); }
    if crate::pread(fd, sh as *mut c_void, shtotal, e_shoff as i64) != shtotal as isize {
        crate::free(sh as *mut c_void);
        crate::close(fd);
        return (false, 0);
    }
    let mut best: u64 = 0;
    let mut have = false;
    for i in 0..e_shnum {
        let sec = sh.add(i * e_shentsize);
        let sh_type = rd32(sec.add(4));
        if sh_type != 2 && sh_type != 11 { continue; }
        let sh_offset = rd64(sec.add(0x18));
        let sh_size = rd64(sec.add(0x20)) as usize;
        let sh_link = rd32(sec.add(0x28)) as usize;
        let sh_entsize = rd64(sec.add(0x38)) as usize;
        if sh_size == 0 || sh_entsize < 24 || sh_link >= e_shnum { continue; }
        let syms = crate::malloc(sh_size) as *mut u8;
        if syms.is_null() { continue; }
        let got = crate::pread(fd, syms as *mut c_void, sh_size, sh_offset as i64);
        if got != sh_size as isize {
            crate::free(syms as *mut c_void);
            continue;
        }
        let strsec = sh.add(sh_link * e_shentsize);
        let str_off = rd64(strsec.add(0x18));
        let str_size = rd64(strsec.add(0x20)) as usize;
        if str_size == 0 || str_size > (1 << 24) {
            crate::free(syms as *mut c_void);
            continue;
        }
        let strs = crate::malloc(str_size) as *mut u8;
        if strs.is_null() {
            crate::free(syms as *mut c_void);
            continue;
        }
        if crate::pread(fd, strs as *mut c_void, str_size, str_off as i64) != str_size as isize {
            crate::free(strs as *mut c_void);
            crate::free(syms as *mut c_void);
            continue;
        }
        let count = sh_size / sh_entsize;
        for j in 0..count {
            let sym = syms.add(j * sh_entsize);
            let st_name = rd32(sym) as usize;
            let st_value = rd64(sym.add(8));
            let st_size = rd64(sym.add(16));
            let st_shndx = rd16(sym.add(6));
            if st_name == 0 || st_name >= str_size || st_shndx == 0 { continue; }
            if st_value > addr { continue; }
            if st_size != 0 && addr >= st_value + st_size { continue; }
            if !have || st_value > best {
                best = st_value;
                have = true;
                let s = strs.add(st_name);
                let mut k = 0usize;
                while *s.add(k) != 0 && k + 1 < DLADDR_NAME.len() {
                    DLADDR_NAME[k] = *s.add(k) as c_char;
                    k += 1;
                }
                DLADDR_NAME[k] = 0;
            }
        }
        crate::free(strs as *mut c_void);
        crate::free(syms as *mut c_void);
    }
    crate::free(sh as *mut c_void);
    crate::close(fd);
    (have, best)
}

#[no_mangle]
pub unsafe extern "C" fn dladdr(addr: *const c_void, info: *mut DlInfo) -> c_int {
    if info.is_null() { ERRNO!() = EINVAL; return 0; }
    (*info).dli_fname = core::ptr::null();
    (*info).dli_fbase = core::ptr::null_mut();
    (*info).dli_sname = core::ptr::null();
    (*info).dli_saddr = core::ptr::null_mut();
    if addr.is_null() { return 0; }
    let (start, off, path, found) = maps_lookup(addr as usize);
    if !found { return 0; }
    let base = if MAPS_PATH[0] != 0 && MAPS_PATH[0] != b'[' as c_char {
        elf_bias_for(path, start, off)
    } else {
        start.wrapping_sub(off)
    };
    (*info).dli_fname = path;
    (*info).dli_fbase = base as *mut c_void;
    if MAPS_PATH[0] == 0 || MAPS_PATH[0] == b'[' as c_char { return 1; }
    let off = addr as u64 - base;
    let (have, value) = elf_symbol_lookup(path, off);
    if have {
        (*info).dli_sname = DLADDR_NAME.as_ptr();
        (*info).dli_saddr = (base + value) as *mut c_void;
    }
    1
}

// ------------------------------------------------------------------
// dl_iterate_phdr
// ------------------------------------------------------------------

static mut EXE_PATH: [c_char; PATH_MAX_V] = [0; PATH_MAX_V];

unsafe fn read_exe_path() -> *const c_char {
    let n = crate::readlink(
        b"/proc/self/exe\0".as_ptr() as *const c_char,
        EXE_PATH.as_mut_ptr(),
        EXE_PATH.len() - 1,
    );
    if n < 0 {
        EXE_PATH[0] = 0;
    } else {
        EXE_PATH[n as usize] = 0;
    }
    EXE_PATH.as_ptr()
}

/// Returns (load bias, phdr pointer, phnum) for the main executable.
unsafe fn main_program_info() -> (u64, *const u8, u16) {
    let exe = read_exe_path();
    if *exe != 0 {
        let f = crate::fopen(b"/proc/self/maps\0".as_ptr() as *const c_char, b"r\0".as_ptr() as *const c_char);
        if !f.is_null() {
            while crate::getline(&raw mut MAPS_LINE, &raw mut MAPS_LINE_CAP, f) >= 0 {
                let p = MAPS_LINE as *const u8;
                let (start, after) = maps_parse_hex(p);
                if *after != b'-' { continue; }
                let (_end, after2) = maps_parse_hex(after.add(1));
                let mut q = after2;
                while *q == b' ' { q = q.add(1); }
                while *q != 0 && *q != b' ' { q = q.add(1); }
                while *q == b' ' { q = q.add(1); }
                let (off, after_off) = maps_parse_hex(q);
                if off != 0 { continue; }
                let mut q2 = after_off;
                for _ in 0..2 {
                    while *q2 == b' ' { q2 = q2.add(1); }
                    while *q2 != 0 && *q2 != b' ' { q2 = q2.add(1); }
                }
                while *q2 == b' ' { q2 = q2.add(1); }
                if *q2 == 0 { continue; }
                let mut path = [0 as c_char; PATH_MAX_V];
                let mut i = 0usize;
                while *q2 != 0 && *q2 != b'\n' && i + 1 < path.len() {
                    path[i] = *q2 as c_char;
                    i += 1;
                    q2 = q2.add(1);
                }
                path[i] = 0;
                if crate::strcmp(path.as_ptr() as *const u8, exe as *const u8) != 0 { continue; }
                let base = start;
                let eh = base as *const u8;
                if *eh == 0x7f && *eh.add(1) == b'E' && *eh.add(2) == b'L' && *eh.add(3) == b'F' {
                    let e_phoff = rd64(eh.add(32));
                    let e_phentsize = rd16(eh.add(54));
                    let e_phnum = rd16(eh.add(56));
                    if e_phentsize as usize == 56 {
                        crate::fclose(f);
                        return (base, (base + e_phoff) as *const u8, e_phnum);
                    }
                }
                crate::fclose(f);
                return (base, core::ptr::null(), 0);
            }
            crate::fclose(f);
        }
    }
    let phdr = crate::getauxval(3);
    let phnum = crate::getauxval(5) as u16;
    if phdr != 0 && phnum != 0 {
        let (start, off, path, _f) = maps_lookup(phdr as usize);
        let base = elf_bias_for(path, start, off);
        return (base, phdr as *const u8, phnum);
    }
    (0, core::ptr::null(), 0)
}

#[no_mangle]
pub unsafe extern "C" fn dl_iterate_phdr(
    callback: Option<unsafe extern "C" fn(*mut DlPhdrInfo, usize, *mut c_void) -> c_int>,
    data: *mut c_void,
) -> c_int {
    let cb = match callback { Some(c) => c, None => return 0 };
    let (base, phdr, phnum) = main_program_info();
    if !phdr.is_null() && phnum != 0 {
        let info = DlPhdrInfo {
            dlpi_addr: base,
            dlpi_name: b"\0".as_ptr() as *const c_char,
            dlpi_phdr: phdr,
            dlpi_phnum: phnum,
            __pad0: 0,
            __pad1: 0,
            dlpi_adds: 0,
            dlpi_subs: 0,
            dlpi_tls_modid: 0,
            dlpi_tls_data: core::ptr::null_mut(),
        };
        let r = cb(&info as *const DlPhdrInfo as *mut DlPhdrInfo, core::mem::size_of::<DlPhdrInfo>(), data);
        if r != 0 { return r; }
    }
    if let Some(f) = LDSO_ITERATE {
        return f(Some(cb), data);
    }
    0
}

#[no_mangle]
pub unsafe extern "C" fn dlinfo(handle: *mut c_void, request: c_int, info: *mut c_void) -> c_int {
    if let Some(f) = LDSO_DLINFO {
        return f(handle, request, info);
    }
    ERRNO!() = 95; // ENOTSUP
    -1
}