Skip to main content

blazesym/dwarf/
reader.rs

1use gimli::EndianSlice;
2use gimli::SectionId;
3
4use crate::elf::ElfParser;
5use crate::Result;
6
7
8#[cfg(target_endian = "little")]
9pub(super) type Endianess = gimli::LittleEndian;
10#[cfg(target_endian = "big")]
11pub(super) type Endianess = gimli::BigEndian;
12
13/// The gimli reader type we currently use. Could be made generic if
14/// need be, but we keep things simple while we can.
15pub(crate) type R<'dat> = EndianSlice<'dat, Endianess>;
16
17
18fn load_section_impl<'elf>(parser: &'elf ElfParser, name: Option<&str>) -> Result<R<'elf>> {
19    let data = if let Some(name) = name {
20        let result = parser.find_section(name)?;
21        match result {
22            Some(idx) => parser.section_data(idx)?,
23            // Make sure to return empty data if a section does not exist.
24            None => &[],
25        }
26    } else {
27        &[]
28    };
29
30    let reader = EndianSlice::new(data, Endianess::default());
31    Ok(reader)
32}
33
34pub(super) fn load_section(parser: &ElfParser, id: SectionId) -> Result<R<'_>> {
35    load_section_impl(parser, Some(id.name()))
36}
37
38pub(super) fn load_dwo_section(parser: &ElfParser, id: SectionId) -> Result<R<'_>> {
39    load_section_impl(parser, id.dwo_name())
40}