1use crate::common::{DebugAddrBase, DebugAddrIndex, SectionId};
2use crate::read::{Reader, ReaderOffset, Result, Section};
34/// The raw contents of the `.debug_addr` section.
5#[derive(Debug, Default, Clone, Copy)]
6pub struct DebugAddr<R> {
7 section: R,
8}
910impl<R: Reader> DebugAddr<R> {
11// TODO: add an iterator over the sets of addresses in the section.
12 // This is not needed for common usage of the section though.
1314/// Returns the address at the given `base` and `index`.
15 ///
16 /// A set of addresses in the `.debug_addr` section consists of a header
17 /// followed by a series of addresses.
18 ///
19 /// The `base` must be the `DW_AT_addr_base` value from the compilation unit DIE.
20 /// This is an offset that points to the first address following the header.
21 ///
22 /// The `index` is the value of a `DW_FORM_addrx` attribute.
23 ///
24 /// The `address_size` must be the size of the address for the compilation unit.
25 /// This value must also match the header. However, note that we do not parse the
26 /// header to validate this, since locating the header is unreliable, and the GNU
27 /// extensions do not emit it.
28pub fn get_address(
29&self,
30 address_size: u8,
31 base: DebugAddrBase<R::Offset>,
32 index: DebugAddrIndex<R::Offset>,
33 ) -> Result<u64> {
34let input = &mut self.section.clone();
35 input.skip(base.0)?;
36 input.skip(R::Offset::from_u64(
37 index.0.into_u64() * u64::from(address_size),
38 )?)?;
39 input.read_address(address_size)
40 }
41}
4243impl<T> DebugAddr<T> {
44/// Create a `DebugAddr` section that references the data in `self`.
45 ///
46 /// This is useful when `R` implements `Reader` but `T` does not.
47 ///
48 /// Used by `DwarfSections::borrow`.
49pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> DebugAddr<R>
50where
51F: FnMut(&'a T) -> R,
52 {
53 borrow(&self.section).into()
54 }
55}
5657impl<R> Section<R> for DebugAddr<R> {
58fn id() -> SectionId {
59 SectionId::DebugAddr
60 }
6162fn reader(&self) -> &R {
63&self.section
64 }
65}
6667impl<R> From<R> for DebugAddr<R> {
68fn from(section: R) -> Self {
69 DebugAddr { section }
70 }
71}
7273#[cfg(test)]
74mod tests {
75use super::*;
76use crate::read::EndianSlice;
77use crate::test_util::GimliSectionMethods;
78use crate::{Format, LittleEndian};
79use test_assembler::{Endian, Label, LabelMaker, Section};
8081#[test]
82fn test_get_address() {
83for format in [Format::Dwarf32, Format::Dwarf64] {
84for address_size in [4, 8] {
85let zero = Label::new();
86let length = Label::new();
87let start = Label::new();
88let first = Label::new();
89let end = Label::new();
90let mut section = Section::with_endian(Endian::Little)
91 .mark(&zero)
92 .initial_length(format, &length, &start)
93 .D16(5)
94 .D8(address_size)
95 .D8(0)
96 .mark(&first);
97for i in 0..20 {
98 section = section.word(address_size, 1000 + i);
99 }
100 section = section.mark(&end);
101 length.set_const((&end - &start) as u64);
102103let section = section.get_contents().unwrap();
104let debug_addr = DebugAddr::from(EndianSlice::new(§ion, LittleEndian));
105let base = DebugAddrBase((&first - &zero) as usize);
106107assert_eq!(
108 debug_addr.get_address(address_size, base, DebugAddrIndex(0)),
109Ok(1000)
110 );
111assert_eq!(
112 debug_addr.get_address(address_size, base, DebugAddrIndex(19)),
113Ok(1019)
114 );
115 }
116 }
117 }
118}