std/backtrace/src/symbolize/gimli/
libs_haiku.rs

1// Haiku implements the image_info struct and the get_next_image_info()
2// functions to iterate through the loaded executable images. The
3// image_info struct contains a pointer to the start of the .text
4// section within the virtual address space, as well as the size of
5// that section. All the read-only segments of the ELF-binary are in
6// that part of the address space.
7
8use super::mystd::ffi::OsStr;
9use super::mystd::os::unix::prelude::*;
10use super::{Library, LibrarySegment};
11use alloc::borrow::ToOwned;
12use alloc::vec::Vec;
13use core::ffi::CStr;
14use core::mem::MaybeUninit;
15
16pub(super) fn native_libraries() -> Vec<Library> {
17    let mut libraries: Vec<Library> = Vec::new();
18
19    unsafe {
20        let mut info = MaybeUninit::<libc::image_info>::zeroed();
21        let mut cookie: i32 = 0;
22        // Load the first image to get a valid info struct
23        let mut status =
24            libc::get_next_image_info(libc::B_CURRENT_TEAM, &mut cookie, info.as_mut_ptr());
25        if status != libc::B_OK {
26            return libraries;
27        }
28        let mut info = info.assume_init();
29
30        while status == libc::B_OK {
31            let mut segments = Vec::new();
32            segments.push(LibrarySegment {
33                stated_virtual_memory_address: 0,
34                len: info.text_size as usize,
35            });
36
37            let bytes = CStr::from_ptr(info.name.as_ptr()).to_bytes();
38            let name = OsStr::from_bytes(bytes).to_owned();
39            libraries.push(Library {
40                name: name,
41                segments: segments,
42                bias: info.text as usize,
43            });
44
45            status = libc::get_next_image_info(libc::B_CURRENT_TEAM, &mut cookie, &mut info);
46        }
47    }
48
49    libraries
50}