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
use endianity::{Endianity, EndianBuf};
use parser::{parse_null_terminated_string, Error, Result};
use std::ffi;
use std::marker::PhantomData;
/// An offset into the `.debug_str` section.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DebugStrOffset(pub usize);
/// The `DebugStr` struct represents the DWARF strings
/// found in the `.debug_str` section.
#[derive(Debug, Clone, Copy)]
pub struct DebugStr<'input, Endian>
where Endian: Endianity
{
debug_str_section: EndianBuf<'input, Endian>,
}
impl<'input, Endian> DebugStr<'input, Endian>
where Endian: Endianity
{
/// Construct a new `DebugStr` instance from the data in the `.debug_str`
/// section.
///
/// It is the caller's responsibility to read the `.debug_str` section and
/// present it as a `&[u8]` slice. That means using some ELF loader on
/// Linux, a Mach-O loader on OSX, etc.
///
/// ```
/// use gimli::{DebugStr, LittleEndian};
///
/// # let buf = [0x00, 0x01, 0x02, 0x03];
/// # let read_debug_str_section_somehow = || &buf;
/// let debug_str = DebugStr::<LittleEndian>::new(read_debug_str_section_somehow());
/// ```
pub fn new(debug_str_section: &'input [u8]) -> DebugStr<'input, Endian> {
DebugStr { debug_str_section: EndianBuf(debug_str_section, PhantomData) }
}
/// Lookup a string from the `.debug_str` section by DebugStrOffset.
///
/// ```
/// use gimli::{DebugStr, DebugStrOffset, LittleEndian};
///
/// # let buf = [0x01, 0x02, 0x00];
/// # let offset = DebugStrOffset(0);
/// # let read_debug_str_section_somehow = || &buf;
/// # let debug_str_offset_somehow = || offset;
/// let debug_str = DebugStr::<LittleEndian>::new(read_debug_str_section_somehow());
/// println!("Found string {:?}", debug_str.get_str(debug_str_offset_somehow()));
/// ```
pub fn get_str(&self, offset: DebugStrOffset) -> Result<&'input ffi::CStr> {
if self.debug_str_section.len() < offset.0 {
return Err(Error::UnexpectedEof);
}
let buf = self.debug_str_section.range_from(offset.0..);
let result = parse_null_terminated_string(buf.0);
result.map(|(_, cstr)| cstr)
}
}