backtrace/symbolize/gimli/
elf.rs

1use super::mystd::ffi::{OsStr, OsString};
2use super::mystd::fs;
3use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt};
4use super::mystd::path::{Path, PathBuf};
5use super::Either;
6use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash, Vec};
7use alloc::sync::Arc;
8use core::convert::{TryFrom, TryInto};
9use core::str;
10use object::elf::{ELFCOMPRESS_ZLIB, ELF_NOTE_GNU, NT_GNU_BUILD_ID, SHF_COMPRESSED};
11use object::read::elf::{CompressionHeader, FileHeader, SectionHeader, SectionTable, Sym};
12use object::read::StringTable;
13use object::{BigEndian, Bytes, NativeEndian};
14
15#[cfg(target_pointer_width = "32")]
16type Elf = object::elf::FileHeader32<NativeEndian>;
17#[cfg(target_pointer_width = "64")]
18type Elf = object::elf::FileHeader64<NativeEndian>;
19
20impl Mapping {
21    pub fn new(path: &Path) -> Option<Mapping> {
22        let map = super::mmap(path)?;
23        Mapping::mk_or_other(map, |map, stash| {
24            let object = Object::parse(&map)?;
25
26            // Try to locate an external debug file using the build ID.
27            if let Some(path_debug) = object.build_id().and_then(locate_build_id) {
28                if let Some(mapping) = Mapping::new_debug(path, path_debug, None) {
29                    return Some(Either::A(mapping));
30                }
31            }
32
33            // Try to locate an external debug file using the GNU debug link section.
34            if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) {
35                if let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) {
36                    return Some(Either::A(mapping));
37                }
38            }
39
40            let dwp = Mapping::load_dwarf_package(path, stash);
41
42            Context::new(stash, object, None, dwp).map(Either::B)
43        })
44    }
45
46    /// Load debuginfo from an external debug file.
47    fn new_debug(original_path: &Path, path: PathBuf, crc: Option<u32>) -> Option<Mapping> {
48        let map = super::mmap(&path)?;
49        Mapping::mk(map, |map, stash| {
50            let object = Object::parse(&map)?;
51
52            if let Some(_crc) = crc {
53                // TODO: check crc
54            }
55
56            // Try to locate a supplementary object file.
57            let mut sup = None;
58            if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) {
59                if let Some(map_sup) = super::mmap(&path_sup) {
60                    let map_sup = stash.cache_mmap(map_sup);
61                    if let Some(sup_) = Object::parse(map_sup) {
62                        if sup_.build_id() == Some(build_id_sup) {
63                            sup = Some(sup_);
64                        }
65                    }
66                }
67            }
68
69            let dwp = Mapping::load_dwarf_package(original_path, stash);
70
71            Context::new(stash, object, sup, dwp)
72        })
73    }
74
75    /// Try to locate a DWARF package file.
76    fn load_dwarf_package<'data>(path: &Path, stash: &'data Stash) -> Option<Object<'data>> {
77        let mut path_dwp = path.to_path_buf();
78        let dwp_extension = path
79            .extension()
80            .map(|previous_extension| {
81                let mut previous_extension = previous_extension.to_os_string();
82                previous_extension.push(".dwp");
83                previous_extension
84            })
85            .unwrap_or_else(|| "dwp".into());
86        path_dwp.set_extension(dwp_extension);
87        if let Some(map_dwp) = super::mmap(&path_dwp) {
88            let map_dwp = stash.cache_mmap(map_dwp);
89            if let Some(dwp_) = Object::parse(map_dwp) {
90                return Some(dwp_);
91            }
92        }
93
94        None
95    }
96}
97
98struct ParsedSym {
99    address: u64,
100    size: u64,
101    name: u32,
102}
103
104pub struct Object<'a> {
105    /// Zero-sized type representing the native endianness.
106    ///
107    /// We could use a literal instead, but this helps ensure correctness.
108    endian: NativeEndian,
109    /// The entire file data.
110    data: &'a [u8],
111    sections: SectionTable<'a, Elf>,
112    strings: StringTable<'a>,
113    /// List of pre-parsed and sorted symbols by base address.
114    syms: Vec<ParsedSym>,
115}
116
117impl<'a> Object<'a> {
118    fn parse(data: &'a [u8]) -> Option<Object<'a>> {
119        let elf = Elf::parse(data).ok()?;
120        let endian = elf.endian().ok()?;
121        let sections = elf.sections(endian, data).ok()?;
122        let mut syms = sections
123            .symbols(endian, data, object::elf::SHT_SYMTAB)
124            .ok()?;
125        if syms.is_empty() {
126            syms = sections
127                .symbols(endian, data, object::elf::SHT_DYNSYM)
128                .ok()?;
129        }
130        let strings = syms.strings();
131
132        let mut syms = syms
133            .iter()
134            // Only look at function/object symbols. This mirrors what
135            // libbacktrace does and in general we're only symbolicating
136            // function addresses in theory. Object symbols correspond
137            // to data, and maybe someone's crazy enough to have a
138            // function go into static data?
139            .filter(|sym| {
140                let st_type = sym.st_type();
141                st_type == object::elf::STT_FUNC || st_type == object::elf::STT_OBJECT
142            })
143            // skip anything that's in an undefined section header,
144            // since it means it's an imported function and we're only
145            // symbolicating with locally defined functions.
146            .filter(|sym| sym.st_shndx(endian) != object::elf::SHN_UNDEF)
147            .map(|sym| {
148                let address = sym.st_value(endian).into();
149                let size = sym.st_size(endian).into();
150                let name = sym.st_name(endian);
151                ParsedSym {
152                    address,
153                    size,
154                    name,
155                }
156            })
157            .collect::<Vec<_>>();
158        syms.sort_unstable_by_key(|s| s.address);
159        Some(Object {
160            endian,
161            data,
162            sections,
163            strings,
164            syms,
165        })
166    }
167
168    pub fn section(&self, stash: &'a Stash, name: &str) -> Option<&'a [u8]> {
169        if let Some(section) = self.section_header(name) {
170            let mut data = Bytes(section.data(self.endian, self.data).ok()?);
171
172            // Check for DWARF-standard (gABI) compression, i.e., as generated
173            // by ld's `--compress-debug-sections=zlib-gabi` flag.
174            let flags: u64 = section.sh_flags(self.endian).into();
175            if (flags & u64::from(SHF_COMPRESSED)) == 0 {
176                // Not compressed.
177                return Some(data.0);
178            }
179
180            let header = data.read::<<Elf as FileHeader>::CompressionHeader>().ok()?;
181            if header.ch_type(self.endian) != ELFCOMPRESS_ZLIB {
182                // Zlib compression is the only known type.
183                return None;
184            }
185            let size = usize::try_from(header.ch_size(self.endian)).ok()?;
186            let buf = stash.allocate(size);
187            decompress_zlib(data.0, buf)?;
188            return Some(buf);
189        }
190
191        // Check for the nonstandard GNU compression format, i.e., as generated
192        // by ld's `--compress-debug-sections=zlib-gnu` flag. This means that if
193        // we're actually asking for `.debug_info` then we need to look up a
194        // section named `.zdebug_info`.
195        if !name.starts_with(".debug_") {
196            return None;
197        }
198        let debug_name = name[7..].as_bytes();
199        let compressed_section = self
200            .sections
201            .iter()
202            .filter_map(|header| {
203                let name = self.sections.section_name(self.endian, header).ok()?;
204                if name.starts_with(b".zdebug_") && &name[8..] == debug_name {
205                    Some(header)
206                } else {
207                    None
208                }
209            })
210            .next()?;
211        let mut data = Bytes(compressed_section.data(self.endian, self.data).ok()?);
212        if data.read_bytes(8).ok()?.0 != b"ZLIB\0\0\0\0" {
213            return None;
214        }
215        let size = usize::try_from(data.read::<object::U32Bytes<_>>().ok()?.get(BigEndian)).ok()?;
216        let buf = stash.allocate(size);
217        decompress_zlib(data.0, buf)?;
218        Some(buf)
219    }
220
221    fn section_header(&self, name: &str) -> Option<&<Elf as FileHeader>::SectionHeader> {
222        self.sections
223            .section_by_name(self.endian, name.as_bytes())
224            .map(|(_index, section)| section)
225    }
226
227    pub fn search_symtab<'b>(&'b self, addr: u64) -> Option<&'b [u8]> {
228        // Same sort of binary search as Windows above
229        let i = match self.syms.binary_search_by_key(&addr, |sym| sym.address) {
230            Ok(i) => i,
231            Err(i) => i.checked_sub(1)?,
232        };
233        let sym = self.syms.get(i)?;
234        if sym.address <= addr && addr <= sym.address + sym.size {
235            self.strings.get(sym.name).ok()
236        } else {
237            None
238        }
239    }
240
241    pub(super) fn search_object_map(&self, _addr: u64) -> Option<(&Context<'_>, u64)> {
242        None
243    }
244
245    fn build_id(&self) -> Option<&'a [u8]> {
246        for section in self.sections.iter() {
247            if let Ok(Some(mut notes)) = section.notes(self.endian, self.data) {
248                while let Ok(Some(note)) = notes.next() {
249                    if note.name() == ELF_NOTE_GNU && note.n_type(self.endian) == NT_GNU_BUILD_ID {
250                        return Some(note.desc());
251                    }
252                }
253            }
254        }
255        None
256    }
257
258    // The contents of the ".gnu_debuglink" section is documented at:
259    // https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
260    fn gnu_debuglink_path(&self, path: &Path) -> Option<(PathBuf, u32)> {
261        let section = self.section_header(".gnu_debuglink")?;
262        let data = section.data(self.endian, self.data).ok()?;
263        let len = data.iter().position(|x| *x == 0)?;
264        let filename = &data[..len];
265        let offset = (len + 1 + 3) & !3;
266        let crc_bytes = data
267            .get(offset..offset + 4)
268            .and_then(|bytes| bytes.try_into().ok())?;
269        let crc = u32::from_ne_bytes(crc_bytes);
270        let path_debug = locate_debuglink(path, filename)?;
271        Some((path_debug, crc))
272    }
273
274    // The format of the ".gnu_debugaltlink" section is based on gdb.
275    fn gnu_debugaltlink_path(&self, path: &Path) -> Option<(PathBuf, &'a [u8])> {
276        let section = self.section_header(".gnu_debugaltlink")?;
277        let data = section.data(self.endian, self.data).ok()?;
278        let len = data.iter().position(|x| *x == 0)?;
279        let filename = &data[..len];
280        let build_id = &data[len + 1..];
281        let path_sup = locate_debugaltlink(path, filename, build_id)?;
282        Some((path_sup, build_id))
283    }
284}
285
286fn decompress_zlib(input: &[u8], output: &mut [u8]) -> Option<()> {
287    use miniz_oxide::inflate::core::inflate_flags::{
288        TINFL_FLAG_PARSE_ZLIB_HEADER, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
289    };
290    use miniz_oxide::inflate::core::{decompress, DecompressorOxide};
291    use miniz_oxide::inflate::TINFLStatus;
292
293    let (status, in_read, out_read) = decompress(
294        &mut DecompressorOxide::new(),
295        input,
296        output,
297        0,
298        TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | TINFL_FLAG_PARSE_ZLIB_HEADER,
299    );
300    if status == TINFLStatus::Done && in_read == input.len() && out_read == output.len() {
301        Some(())
302    } else {
303        None
304    }
305}
306
307const DEBUG_PATH: &[u8] = b"/usr/lib/debug";
308
309fn debug_path_exists() -> bool {
310    cfg_if::cfg_if! {
311        if #[cfg(any(target_os = "freebsd", target_os = "hurd", target_os = "linux"))] {
312            use core::sync::atomic::{AtomicU8, Ordering};
313            static DEBUG_PATH_EXISTS: AtomicU8 = AtomicU8::new(0);
314
315            let mut exists = DEBUG_PATH_EXISTS.load(Ordering::Relaxed);
316            if exists == 0 {
317                exists = if Path::new(OsStr::from_bytes(DEBUG_PATH)).is_dir() {
318                    1
319                } else {
320                    2
321                };
322                DEBUG_PATH_EXISTS.store(exists, Ordering::Relaxed);
323            }
324            exists == 1
325        } else {
326            false
327        }
328    }
329}
330
331/// Locate a debug file based on its build ID.
332///
333/// The format of build id paths is documented at:
334/// https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
335fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
336    const BUILD_ID_PATH: &[u8] = b"/usr/lib/debug/.build-id/";
337    const BUILD_ID_SUFFIX: &[u8] = b".debug";
338
339    if build_id.len() < 2 {
340        return None;
341    }
342
343    if !debug_path_exists() {
344        return None;
345    }
346
347    let mut path =
348        Vec::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
349    path.extend(BUILD_ID_PATH);
350    path.push(hex(build_id[0] >> 4));
351    path.push(hex(build_id[0] & 0xf));
352    path.push(b'/');
353    for byte in &build_id[1..] {
354        path.push(hex(byte >> 4));
355        path.push(hex(byte & 0xf));
356    }
357    path.extend(BUILD_ID_SUFFIX);
358    Some(PathBuf::from(OsString::from_vec(path)))
359}
360
361fn hex(byte: u8) -> u8 {
362    if byte < 10 {
363        b'0' + byte
364    } else {
365        b'a' + byte - 10
366    }
367}
368
369/// Locate a file specified in a `.gnu_debuglink` section.
370///
371/// `path` is the file containing the section.
372/// `filename` is from the contents of the section.
373///
374/// Search order is based on gdb, documented at:
375/// https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
376///
377/// gdb also allows the user to customize the debug search path, but we don't.
378///
379/// gdb also supports debuginfod, but we don't yet.
380fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
381    let path = fs::canonicalize(path).ok()?;
382    let parent = path.parent()?;
383    let mut f = PathBuf::from(OsString::with_capacity(
384        DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2,
385    ));
386    let filename = Path::new(OsStr::from_bytes(filename));
387
388    // Try "/parent/filename" if it differs from "path"
389    f.push(parent);
390    f.push(filename);
391    if f != path && f.is_file() {
392        return Some(f);
393    }
394
395    // Try "/parent/.debug/filename"
396    let mut s = OsString::from(f);
397    s.clear();
398    f = PathBuf::from(s);
399    f.push(parent);
400    f.push(".debug");
401    f.push(filename);
402    if f.is_file() {
403        return Some(f);
404    }
405
406    if debug_path_exists() {
407        // Try "/usr/lib/debug/parent/filename"
408        let mut s = OsString::from(f);
409        s.clear();
410        f = PathBuf::from(s);
411        f.push(OsStr::from_bytes(DEBUG_PATH));
412        f.push(parent.strip_prefix("/").unwrap());
413        f.push(filename);
414        if f.is_file() {
415            return Some(f);
416        }
417    }
418
419    None
420}
421
422/// Locate a file specified in a `.gnu_debugaltlink` section.
423///
424/// `path` is the file containing the section.
425/// `filename` and `build_id` are the contents of the section.
426///
427/// Search order is based on gdb:
428/// - filename, which is either absolute or relative to `path`
429/// - the build ID path under `BUILD_ID_PATH`
430///
431/// gdb also allows the user to customize the debug search path, but we don't.
432///
433/// gdb also supports debuginfod, but we don't yet.
434fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option<PathBuf> {
435    let filename = Path::new(OsStr::from_bytes(filename));
436    if filename.is_absolute() {
437        if filename.is_file() {
438            return Some(filename.into());
439        }
440    } else {
441        let path = fs::canonicalize(path).ok()?;
442        let parent = path.parent()?;
443        let mut f = PathBuf::from(parent);
444        f.push(filename);
445        if f.is_file() {
446            return Some(f);
447        }
448    }
449
450    locate_build_id(build_id)
451}
452
453fn convert_path<R: gimli::Reader>(r: &R) -> Result<PathBuf, gimli::Error> {
454    let bytes = r.to_slice()?;
455    Ok(PathBuf::from(OsStr::from_bytes(&bytes)))
456}
457
458pub(super) fn handle_split_dwarf<'data>(
459    package: Option<&gimli::DwarfPackage<EndianSlice<'data, Endian>>>,
460    stash: &'data Stash,
461    load: addr2line::SplitDwarfLoad<EndianSlice<'data, Endian>>,
462) -> Option<Arc<gimli::Dwarf<EndianSlice<'data, Endian>>>> {
463    if let Some(dwp) = package.as_ref() {
464        if let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) {
465            return Some(Arc::new(cu));
466        }
467    }
468
469    let mut path = PathBuf::new();
470    if let Some(p) = load.comp_dir.as_ref() {
471        path.push(convert_path(p).ok()?);
472    }
473
474    path.push(convert_path(load.path.as_ref()?).ok()?);
475
476    if let Some(map_dwo) = super::mmap(&path) {
477        let map_dwo = stash.cache_mmap(map_dwo);
478        if let Some(dwo) = Object::parse(map_dwo) {
479            return gimli::Dwarf::load(|id| -> Result<_, ()> {
480                let data = id
481                    .dwo_name()
482                    .and_then(|name| dwo.section(stash, name))
483                    .unwrap_or(&[]);
484                Ok(EndianSlice::new(data, Endian))
485            })
486            .ok()
487            .map(|mut dwo_dwarf| {
488                dwo_dwarf.make_dwo(&load.parent);
489                Arc::new(dwo_dwarf)
490            });
491        }
492    }
493
494    None
495}