1 use alloc::string::String;
2 use alloc::sync::Arc;
3 
4 use crate::common::{
5     DebugAddrBase, DebugAddrIndex, DebugInfoOffset, DebugLineStrOffset, DebugLocListsBase,
6     DebugLocListsIndex, DebugRngListsBase, DebugRngListsIndex, DebugStrOffset, DebugStrOffsetsBase,
7     DebugStrOffsetsIndex, DebugTypeSignature, DebugTypesOffset, DwarfFileType, DwoId, Encoding,
8     LocationListsOffset, RangeListsOffset, RawRangeListsOffset, SectionId, UnitSectionOffset,
9 };
10 use crate::constants;
11 use crate::read::{
12     Abbreviations, AttributeValue, DebugAbbrev, DebugAddr, DebugAranges, DebugCuIndex, DebugInfo,
13     DebugInfoUnitHeadersIter, DebugLine, DebugLineStr, DebugLoc, DebugLocLists, DebugRngLists,
14     DebugStr, DebugStrOffsets, DebugTuIndex, DebugTypes, DebugTypesUnitHeadersIter,
15     DebuggingInformationEntry, EntriesCursor, EntriesRaw, EntriesTree, Error,
16     IncompleteLineProgram, LocListIter, LocationLists, Range, RangeLists, RawLocListIter,
17     RawRngListIter, Reader, ReaderOffset, ReaderOffsetId, Result, RngListIter, Section, UnitHeader,
18     UnitIndex, UnitIndexSectionIterator, UnitOffset, UnitType,
19 };
20 
21 /// All of the commonly used DWARF sections, and other common information.
22 #[derive(Debug, Default)]
23 pub struct Dwarf<R> {
24     /// The `.debug_abbrev` section.
25     pub debug_abbrev: DebugAbbrev<R>,
26 
27     /// The `.debug_addr` section.
28     pub debug_addr: DebugAddr<R>,
29 
30     /// The `.debug_aranges` section.
31     pub debug_aranges: DebugAranges<R>,
32 
33     /// The `.debug_info` section.
34     pub debug_info: DebugInfo<R>,
35 
36     /// The `.debug_line` section.
37     pub debug_line: DebugLine<R>,
38 
39     /// The `.debug_line_str` section.
40     pub debug_line_str: DebugLineStr<R>,
41 
42     /// The `.debug_str` section.
43     pub debug_str: DebugStr<R>,
44 
45     /// The `.debug_str_offsets` section.
46     pub debug_str_offsets: DebugStrOffsets<R>,
47 
48     /// The `.debug_types` section.
49     pub debug_types: DebugTypes<R>,
50 
51     /// The location lists in the `.debug_loc` and `.debug_loclists` sections.
52     pub locations: LocationLists<R>,
53 
54     /// The range lists in the `.debug_ranges` and `.debug_rnglists` sections.
55     pub ranges: RangeLists<R>,
56 
57     /// The type of this file.
58     pub file_type: DwarfFileType,
59 
60     /// The DWARF sections for a supplementary object file.
61     pub sup: Option<Arc<Dwarf<R>>>,
62 }
63 
64 impl<T> Dwarf<T> {
65     /// Try to load the DWARF sections using the given loader function.
66     ///
67     /// `section` loads a DWARF section from the object file.
68     /// It should return an empty section if the section does not exist.
69     ///
70     /// `section` may either directly return a `Reader` instance (such as
71     /// `EndianSlice`), or it may return some other type and then convert
72     /// that type into a `Reader` using `Dwarf::borrow`.
73     ///
74     /// After loading, the user should set the `file_type` field and
75     /// call `load_sup` if required.
load<F, E>(mut section: F) -> core::result::Result<Self, E> where F: FnMut(SectionId) -> core::result::Result<T, E>,76     pub fn load<F, E>(mut section: F) -> core::result::Result<Self, E>
77     where
78         F: FnMut(SectionId) -> core::result::Result<T, E>,
79     {
80         // Section types are inferred.
81         let debug_loc = Section::load(&mut section)?;
82         let debug_loclists = Section::load(&mut section)?;
83         let debug_ranges = Section::load(&mut section)?;
84         let debug_rnglists = Section::load(&mut section)?;
85         Ok(Dwarf {
86             debug_abbrev: Section::load(&mut section)?,
87             debug_addr: Section::load(&mut section)?,
88             debug_aranges: Section::load(&mut section)?,
89             debug_info: Section::load(&mut section)?,
90             debug_line: Section::load(&mut section)?,
91             debug_line_str: Section::load(&mut section)?,
92             debug_str: Section::load(&mut section)?,
93             debug_str_offsets: Section::load(&mut section)?,
94             debug_types: Section::load(&mut section)?,
95             locations: LocationLists::new(debug_loc, debug_loclists),
96             ranges: RangeLists::new(debug_ranges, debug_rnglists),
97             file_type: DwarfFileType::Main,
98             sup: None,
99         })
100     }
101 
102     /// Load the DWARF sections from the supplementary object file.
103     ///
104     /// `section` operates the same as for `load`.
105     ///
106     /// Sets `self.sup`, replacing any previous value.
load_sup<F, E>(&mut self, section: F) -> core::result::Result<(), E> where F: FnMut(SectionId) -> core::result::Result<T, E>,107     pub fn load_sup<F, E>(&mut self, section: F) -> core::result::Result<(), E>
108     where
109         F: FnMut(SectionId) -> core::result::Result<T, E>,
110     {
111         self.sup = Some(Arc::new(Self::load(section)?));
112         Ok(())
113     }
114 
115     /// Create a `Dwarf` structure that references the data in `self`.
116     ///
117     /// This is useful when `R` implements `Reader` but `T` does not.
118     ///
119     /// ## Example Usage
120     ///
121     /// It can be useful to load DWARF sections into owned data structures,
122     /// such as `Vec`. However, we do not implement the `Reader` trait
123     /// for `Vec`, because it would be very inefficient, but this trait
124     /// is required for all of the methods that parse the DWARF data.
125     /// So we first load the DWARF sections into `Vec`s, and then use
126     /// `borrow` to create `Reader`s that reference the data.
127     ///
128     /// ```rust,no_run
129     /// # fn example() -> Result<(), gimli::Error> {
130     /// # let loader = |name| -> Result<_, gimli::Error> { unimplemented!() };
131     /// # let sup_loader = |name| -> Result<_, gimli::Error> { unimplemented!() };
132     /// // Read the DWARF sections into `Vec`s with whatever object loader you're using.
133     /// let mut owned_dwarf: gimli::Dwarf<Vec<u8>> = gimli::Dwarf::load(loader)?;
134     /// owned_dwarf.load_sup(sup_loader)?;
135     /// // Create references to the DWARF sections.
136     /// let dwarf = owned_dwarf.borrow(|section| {
137     ///     gimli::EndianSlice::new(&section, gimli::LittleEndian)
138     /// });
139     /// # unreachable!()
140     /// # }
141     /// ```
borrow<'a, F, R>(&'a self, mut borrow: F) -> Dwarf<R> where F: FnMut(&'a T) -> R,142     pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> Dwarf<R>
143     where
144         F: FnMut(&'a T) -> R,
145     {
146         Dwarf {
147             debug_abbrev: self.debug_abbrev.borrow(&mut borrow),
148             debug_addr: self.debug_addr.borrow(&mut borrow),
149             debug_aranges: self.debug_aranges.borrow(&mut borrow),
150             debug_info: self.debug_info.borrow(&mut borrow),
151             debug_line: self.debug_line.borrow(&mut borrow),
152             debug_line_str: self.debug_line_str.borrow(&mut borrow),
153             debug_str: self.debug_str.borrow(&mut borrow),
154             debug_str_offsets: self.debug_str_offsets.borrow(&mut borrow),
155             debug_types: self.debug_types.borrow(&mut borrow),
156             locations: self.locations.borrow(&mut borrow),
157             ranges: self.ranges.borrow(&mut borrow),
158             file_type: self.file_type,
159             sup: self.sup().map(|sup| Arc::new(sup.borrow(borrow))),
160         }
161     }
162 
163     /// Return a reference to the DWARF sections for supplementary object file.
sup(&self) -> Option<&Dwarf<T>>164     pub fn sup(&self) -> Option<&Dwarf<T>> {
165         self.sup.as_ref().map(Arc::as_ref)
166     }
167 }
168 
169 impl<R: Reader> Dwarf<R> {
170     /// Iterate the unit headers in the `.debug_info` section.
171     ///
172     /// Can be [used with
173     /// `FallibleIterator`](./index.html#using-with-fallibleiterator).
174     #[inline]
units(&self) -> DebugInfoUnitHeadersIter<R>175     pub fn units(&self) -> DebugInfoUnitHeadersIter<R> {
176         self.debug_info.units()
177     }
178 
179     /// Construct a new `Unit` from the given unit header.
180     #[inline]
unit(&self, header: UnitHeader<R>) -> Result<Unit<R>>181     pub fn unit(&self, header: UnitHeader<R>) -> Result<Unit<R>> {
182         Unit::new(self, header)
183     }
184 
185     /// Iterate the type-unit headers in the `.debug_types` section.
186     ///
187     /// Can be [used with
188     /// `FallibleIterator`](./index.html#using-with-fallibleiterator).
189     #[inline]
type_units(&self) -> DebugTypesUnitHeadersIter<R>190     pub fn type_units(&self) -> DebugTypesUnitHeadersIter<R> {
191         self.debug_types.units()
192     }
193 
194     /// Parse the abbreviations for a compilation unit.
195     // TODO: provide caching of abbreviations
196     #[inline]
abbreviations(&self, unit: &UnitHeader<R>) -> Result<Abbreviations>197     pub fn abbreviations(&self, unit: &UnitHeader<R>) -> Result<Abbreviations> {
198         unit.abbreviations(&self.debug_abbrev)
199     }
200 
201     /// Return the string offset at the given index.
202     #[inline]
string_offset( &self, unit: &Unit<R>, index: DebugStrOffsetsIndex<R::Offset>, ) -> Result<DebugStrOffset<R::Offset>>203     pub fn string_offset(
204         &self,
205         unit: &Unit<R>,
206         index: DebugStrOffsetsIndex<R::Offset>,
207     ) -> Result<DebugStrOffset<R::Offset>> {
208         self.debug_str_offsets
209             .get_str_offset(unit.header.format(), unit.str_offsets_base, index)
210     }
211 
212     /// Return the string at the given offset in `.debug_str`.
213     #[inline]
string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R>214     pub fn string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R> {
215         self.debug_str.get_str(offset)
216     }
217 
218     /// Return the string at the given offset in `.debug_line_str`.
219     #[inline]
line_string(&self, offset: DebugLineStrOffset<R::Offset>) -> Result<R>220     pub fn line_string(&self, offset: DebugLineStrOffset<R::Offset>) -> Result<R> {
221         self.debug_line_str.get_str(offset)
222     }
223 
224     /// Return an attribute value as a string slice.
225     ///
226     /// If the attribute value is one of:
227     ///
228     /// - an inline `DW_FORM_string` string
229     /// - a `DW_FORM_strp` reference to an offset into the `.debug_str` section
230     /// - a `DW_FORM_strp_sup` reference to an offset into a supplementary
231     /// object file
232     /// - a `DW_FORM_line_strp` reference to an offset into the `.debug_line_str`
233     /// section
234     /// - a `DW_FORM_strx` index into the `.debug_str_offsets` entries for the unit
235     ///
236     /// then return the attribute's string value. Returns an error if the attribute
237     /// value does not have a string form, or if a string form has an invalid value.
attr_string(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<R>238     pub fn attr_string(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<R> {
239         match attr {
240             AttributeValue::String(string) => Ok(string),
241             AttributeValue::DebugStrRef(offset) => self.debug_str.get_str(offset),
242             AttributeValue::DebugStrRefSup(offset) => {
243                 if let Some(sup) = self.sup() {
244                     sup.debug_str.get_str(offset)
245                 } else {
246                     Err(Error::ExpectedStringAttributeValue)
247                 }
248             }
249             AttributeValue::DebugLineStrRef(offset) => self.debug_line_str.get_str(offset),
250             AttributeValue::DebugStrOffsetsIndex(index) => {
251                 let offset = self.debug_str_offsets.get_str_offset(
252                     unit.header.format(),
253                     unit.str_offsets_base,
254                     index,
255                 )?;
256                 self.debug_str.get_str(offset)
257             }
258             _ => Err(Error::ExpectedStringAttributeValue),
259         }
260     }
261 
262     /// Return the address at the given index.
address(&self, unit: &Unit<R>, index: DebugAddrIndex<R::Offset>) -> Result<u64>263     pub fn address(&self, unit: &Unit<R>, index: DebugAddrIndex<R::Offset>) -> Result<u64> {
264         self.debug_addr
265             .get_address(unit.encoding().address_size, unit.addr_base, index)
266     }
267 
268     /// Try to return an attribute value as an address.
269     ///
270     /// If the attribute value is one of:
271     ///
272     /// - a `DW_FORM_addr`
273     /// - a `DW_FORM_addrx` index into the `.debug_addr` entries for the unit
274     ///
275     /// then return the address.
276     /// Returns `None` for other forms.
attr_address(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<Option<u64>>277     pub fn attr_address(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<Option<u64>> {
278         match attr {
279             AttributeValue::Addr(addr) => Ok(Some(addr)),
280             AttributeValue::DebugAddrIndex(index) => self.address(unit, index).map(Some),
281             _ => Ok(None),
282         }
283     }
284 
285     /// Return the range list offset for the given raw offset.
286     ///
287     /// This handles adding `DW_AT_GNU_ranges_base` if required.
ranges_offset_from_raw( &self, unit: &Unit<R>, offset: RawRangeListsOffset<R::Offset>, ) -> RangeListsOffset<R::Offset>288     pub fn ranges_offset_from_raw(
289         &self,
290         unit: &Unit<R>,
291         offset: RawRangeListsOffset<R::Offset>,
292     ) -> RangeListsOffset<R::Offset> {
293         if self.file_type == DwarfFileType::Dwo && unit.header.version() < 5 {
294             RangeListsOffset(offset.0.wrapping_add(unit.rnglists_base.0))
295         } else {
296             RangeListsOffset(offset.0)
297         }
298     }
299 
300     /// Return the range list offset at the given index.
ranges_offset( &self, unit: &Unit<R>, index: DebugRngListsIndex<R::Offset>, ) -> Result<RangeListsOffset<R::Offset>>301     pub fn ranges_offset(
302         &self,
303         unit: &Unit<R>,
304         index: DebugRngListsIndex<R::Offset>,
305     ) -> Result<RangeListsOffset<R::Offset>> {
306         self.ranges
307             .get_offset(unit.encoding(), unit.rnglists_base, index)
308     }
309 
310     /// Iterate over the `RangeListEntry`s starting at the given offset.
ranges( &self, unit: &Unit<R>, offset: RangeListsOffset<R::Offset>, ) -> Result<RngListIter<R>>311     pub fn ranges(
312         &self,
313         unit: &Unit<R>,
314         offset: RangeListsOffset<R::Offset>,
315     ) -> Result<RngListIter<R>> {
316         self.ranges.ranges(
317             offset,
318             unit.encoding(),
319             unit.low_pc,
320             &self.debug_addr,
321             unit.addr_base,
322         )
323     }
324 
325     /// Iterate over the `RawRngListEntry`ies starting at the given offset.
raw_ranges( &self, unit: &Unit<R>, offset: RangeListsOffset<R::Offset>, ) -> Result<RawRngListIter<R>>326     pub fn raw_ranges(
327         &self,
328         unit: &Unit<R>,
329         offset: RangeListsOffset<R::Offset>,
330     ) -> Result<RawRngListIter<R>> {
331         self.ranges.raw_ranges(offset, unit.encoding())
332     }
333 
334     /// Try to return an attribute value as a range list offset.
335     ///
336     /// If the attribute value is one of:
337     ///
338     /// - a `DW_FORM_sec_offset` reference to the `.debug_ranges` or `.debug_rnglists` sections
339     /// - a `DW_FORM_rnglistx` index into the `.debug_rnglists` entries for the unit
340     ///
341     /// then return the range list offset of the range list.
342     /// Returns `None` for other forms.
attr_ranges_offset( &self, unit: &Unit<R>, attr: AttributeValue<R>, ) -> Result<Option<RangeListsOffset<R::Offset>>>343     pub fn attr_ranges_offset(
344         &self,
345         unit: &Unit<R>,
346         attr: AttributeValue<R>,
347     ) -> Result<Option<RangeListsOffset<R::Offset>>> {
348         match attr {
349             AttributeValue::RangeListsRef(offset) => {
350                 Ok(Some(self.ranges_offset_from_raw(unit, offset)))
351             }
352             AttributeValue::DebugRngListsIndex(index) => self.ranges_offset(unit, index).map(Some),
353             _ => Ok(None),
354         }
355     }
356 
357     /// Try to return an attribute value as a range list entry iterator.
358     ///
359     /// If the attribute value is one of:
360     ///
361     /// - a `DW_FORM_sec_offset` reference to the `.debug_ranges` or `.debug_rnglists` sections
362     /// - a `DW_FORM_rnglistx` index into the `.debug_rnglists` entries for the unit
363     ///
364     /// then return an iterator over the entries in the range list.
365     /// Returns `None` for other forms.
attr_ranges( &self, unit: &Unit<R>, attr: AttributeValue<R>, ) -> Result<Option<RngListIter<R>>>366     pub fn attr_ranges(
367         &self,
368         unit: &Unit<R>,
369         attr: AttributeValue<R>,
370     ) -> Result<Option<RngListIter<R>>> {
371         match self.attr_ranges_offset(unit, attr)? {
372             Some(offset) => Ok(Some(self.ranges(unit, offset)?)),
373             None => Ok(None),
374         }
375     }
376 
377     /// Return an iterator for the address ranges of a `DebuggingInformationEntry`.
378     ///
379     /// This uses `DW_AT_low_pc`, `DW_AT_high_pc` and `DW_AT_ranges`.
die_ranges( &self, unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, ) -> Result<RangeIter<R>>380     pub fn die_ranges(
381         &self,
382         unit: &Unit<R>,
383         entry: &DebuggingInformationEntry<R>,
384     ) -> Result<RangeIter<R>> {
385         let mut low_pc = None;
386         let mut high_pc = None;
387         let mut size = None;
388         let mut attrs = entry.attrs();
389         while let Some(attr) = attrs.next()? {
390             match attr.name() {
391                 constants::DW_AT_low_pc => {
392                     low_pc = Some(
393                         self.attr_address(unit, attr.value())?
394                             .ok_or(Error::UnsupportedAttributeForm)?,
395                     );
396                 }
397                 constants::DW_AT_high_pc => match attr.value() {
398                     AttributeValue::Udata(val) => size = Some(val),
399                     attr => {
400                         high_pc = Some(
401                             self.attr_address(unit, attr)?
402                                 .ok_or(Error::UnsupportedAttributeForm)?,
403                         );
404                     }
405                 },
406                 constants::DW_AT_ranges => {
407                     if let Some(list) = self.attr_ranges(unit, attr.value())? {
408                         return Ok(RangeIter(RangeIterInner::List(list)));
409                     }
410                 }
411                 _ => {}
412             }
413         }
414         let range = low_pc.and_then(|begin| {
415             let end = size.map(|size| begin + size).or(high_pc);
416             // TODO: perhaps return an error if `end` is `None`
417             end.map(|end| Range { begin, end })
418         });
419         Ok(RangeIter(RangeIterInner::Single(range)))
420     }
421 
422     /// Return an iterator for the address ranges of a `Unit`.
423     ///
424     /// This uses `DW_AT_low_pc`, `DW_AT_high_pc` and `DW_AT_ranges` of the
425     /// root `DebuggingInformationEntry`.
unit_ranges(&self, unit: &Unit<R>) -> Result<RangeIter<R>>426     pub fn unit_ranges(&self, unit: &Unit<R>) -> Result<RangeIter<R>> {
427         let mut cursor = unit.header.entries(&unit.abbreviations);
428         cursor.next_dfs()?;
429         let root = cursor.current().ok_or(Error::MissingUnitDie)?;
430         self.die_ranges(unit, root)
431     }
432 
433     /// Return the location list offset at the given index.
locations_offset( &self, unit: &Unit<R>, index: DebugLocListsIndex<R::Offset>, ) -> Result<LocationListsOffset<R::Offset>>434     pub fn locations_offset(
435         &self,
436         unit: &Unit<R>,
437         index: DebugLocListsIndex<R::Offset>,
438     ) -> Result<LocationListsOffset<R::Offset>> {
439         self.locations
440             .get_offset(unit.encoding(), unit.loclists_base, index)
441     }
442 
443     /// Iterate over the `LocationListEntry`s starting at the given offset.
locations( &self, unit: &Unit<R>, offset: LocationListsOffset<R::Offset>, ) -> Result<LocListIter<R>>444     pub fn locations(
445         &self,
446         unit: &Unit<R>,
447         offset: LocationListsOffset<R::Offset>,
448     ) -> Result<LocListIter<R>> {
449         match self.file_type {
450             DwarfFileType::Main => self.locations.locations(
451                 offset,
452                 unit.encoding(),
453                 unit.low_pc,
454                 &self.debug_addr,
455                 unit.addr_base,
456             ),
457             DwarfFileType::Dwo => self.locations.locations_dwo(
458                 offset,
459                 unit.encoding(),
460                 unit.low_pc,
461                 &self.debug_addr,
462                 unit.addr_base,
463             ),
464         }
465     }
466 
467     /// Iterate over the raw `LocationListEntry`s starting at the given offset.
raw_locations( &self, unit: &Unit<R>, offset: LocationListsOffset<R::Offset>, ) -> Result<RawLocListIter<R>>468     pub fn raw_locations(
469         &self,
470         unit: &Unit<R>,
471         offset: LocationListsOffset<R::Offset>,
472     ) -> Result<RawLocListIter<R>> {
473         match self.file_type {
474             DwarfFileType::Main => self.locations.raw_locations(offset, unit.encoding()),
475             DwarfFileType::Dwo => self.locations.raw_locations_dwo(offset, unit.encoding()),
476         }
477     }
478 
479     /// Try to return an attribute value as a location list offset.
480     ///
481     /// If the attribute value is one of:
482     ///
483     /// - a `DW_FORM_sec_offset` reference to the `.debug_loc` or `.debug_loclists` sections
484     /// - a `DW_FORM_loclistx` index into the `.debug_loclists` entries for the unit
485     ///
486     /// then return the location list offset of the location list.
487     /// Returns `None` for other forms.
attr_locations_offset( &self, unit: &Unit<R>, attr: AttributeValue<R>, ) -> Result<Option<LocationListsOffset<R::Offset>>>488     pub fn attr_locations_offset(
489         &self,
490         unit: &Unit<R>,
491         attr: AttributeValue<R>,
492     ) -> Result<Option<LocationListsOffset<R::Offset>>> {
493         match attr {
494             AttributeValue::LocationListsRef(offset) => Ok(Some(offset)),
495             AttributeValue::DebugLocListsIndex(index) => {
496                 self.locations_offset(unit, index).map(Some)
497             }
498             _ => Ok(None),
499         }
500     }
501 
502     /// Try to return an attribute value as a location list entry iterator.
503     ///
504     /// If the attribute value is one of:
505     ///
506     /// - a `DW_FORM_sec_offset` reference to the `.debug_loc` or `.debug_loclists` sections
507     /// - a `DW_FORM_loclistx` index into the `.debug_loclists` entries for the unit
508     ///
509     /// then return an iterator over the entries in the location list.
510     /// Returns `None` for other forms.
attr_locations( &self, unit: &Unit<R>, attr: AttributeValue<R>, ) -> Result<Option<LocListIter<R>>>511     pub fn attr_locations(
512         &self,
513         unit: &Unit<R>,
514         attr: AttributeValue<R>,
515     ) -> Result<Option<LocListIter<R>>> {
516         match self.attr_locations_offset(unit, attr)? {
517             Some(offset) => Ok(Some(self.locations(unit, offset)?)),
518             None => Ok(None),
519         }
520     }
521 
522     /// Call `Reader::lookup_offset_id` for each section, and return the first match.
523     ///
524     /// The first element of the tuple is `true` for supplementary sections.
lookup_offset_id(&self, id: ReaderOffsetId) -> Option<(bool, SectionId, R::Offset)>525     pub fn lookup_offset_id(&self, id: ReaderOffsetId) -> Option<(bool, SectionId, R::Offset)> {
526         None.or_else(|| self.debug_abbrev.lookup_offset_id(id))
527             .or_else(|| self.debug_addr.lookup_offset_id(id))
528             .or_else(|| self.debug_aranges.lookup_offset_id(id))
529             .or_else(|| self.debug_info.lookup_offset_id(id))
530             .or_else(|| self.debug_line.lookup_offset_id(id))
531             .or_else(|| self.debug_line_str.lookup_offset_id(id))
532             .or_else(|| self.debug_str.lookup_offset_id(id))
533             .or_else(|| self.debug_str_offsets.lookup_offset_id(id))
534             .or_else(|| self.debug_types.lookup_offset_id(id))
535             .or_else(|| self.locations.lookup_offset_id(id))
536             .or_else(|| self.ranges.lookup_offset_id(id))
537             .map(|(id, offset)| (false, id, offset))
538             .or_else(|| {
539                 self.sup()
540                     .and_then(|sup| sup.lookup_offset_id(id))
541                     .map(|(_, id, offset)| (true, id, offset))
542             })
543     }
544 
545     /// Returns a string representation of the given error.
546     ///
547     /// This uses information from the DWARF sections to provide more information in some cases.
format_error(&self, err: Error) -> String548     pub fn format_error(&self, err: Error) -> String {
549         #[allow(clippy::single_match)]
550         match err {
551             Error::UnexpectedEof(id) => match self.lookup_offset_id(id) {
552                 Some((sup, section, offset)) => {
553                     return format!(
554                         "{} at {}{}+0x{:x}",
555                         err,
556                         section.name(),
557                         if sup { "(sup)" } else { "" },
558                         offset.into_u64(),
559                     );
560                 }
561                 None => {}
562             },
563             _ => {}
564         }
565         err.description().into()
566     }
567 }
568 
569 /// The sections from a `.dwp` file.
570 #[derive(Debug)]
571 pub struct DwarfPackage<R: Reader> {
572     /// The compilation unit index in the `.debug_cu_index` section.
573     pub cu_index: UnitIndex<R>,
574 
575     /// The type unit index in the `.debug_tu_index` section.
576     pub tu_index: UnitIndex<R>,
577 
578     /// The `.debug_abbrev.dwo` section.
579     pub debug_abbrev: DebugAbbrev<R>,
580 
581     /// The `.debug_info.dwo` section.
582     pub debug_info: DebugInfo<R>,
583 
584     /// The `.debug_line.dwo` section.
585     pub debug_line: DebugLine<R>,
586 
587     /// The `.debug_str.dwo` section.
588     pub debug_str: DebugStr<R>,
589 
590     /// The `.debug_str_offsets.dwo` section.
591     pub debug_str_offsets: DebugStrOffsets<R>,
592 
593     /// The `.debug_loc.dwo` section.
594     ///
595     /// Only present when using GNU split-dwarf extension to DWARF 4.
596     pub debug_loc: DebugLoc<R>,
597 
598     /// The `.debug_loclists.dwo` section.
599     pub debug_loclists: DebugLocLists<R>,
600 
601     /// The `.debug_rnglists.dwo` section.
602     pub debug_rnglists: DebugRngLists<R>,
603 
604     /// The `.debug_types.dwo` section.
605     ///
606     /// Only present when using GNU split-dwarf extension to DWARF 4.
607     pub debug_types: DebugTypes<R>,
608 
609     /// An empty section.
610     ///
611     /// Used when creating `Dwarf<R>`.
612     pub empty: R,
613 }
614 
615 impl<R: Reader> DwarfPackage<R> {
616     /// Try to load the `.dwp` sections using the given loader function.
617     ///
618     /// `section` loads a DWARF section from the object file.
619     /// It should return an empty section if the section does not exist.
load<F, E>(mut section: F, empty: R) -> core::result::Result<Self, E> where F: FnMut(SectionId) -> core::result::Result<R, E>, E: From<Error>,620     pub fn load<F, E>(mut section: F, empty: R) -> core::result::Result<Self, E>
621     where
622         F: FnMut(SectionId) -> core::result::Result<R, E>,
623         E: From<Error>,
624     {
625         Ok(DwarfPackage {
626             cu_index: DebugCuIndex::load(&mut section)?.index()?,
627             tu_index: DebugTuIndex::load(&mut section)?.index()?,
628             // Section types are inferred.
629             debug_abbrev: Section::load(&mut section)?,
630             debug_info: Section::load(&mut section)?,
631             debug_line: Section::load(&mut section)?,
632             debug_str: Section::load(&mut section)?,
633             debug_str_offsets: Section::load(&mut section)?,
634             debug_loc: Section::load(&mut section)?,
635             debug_loclists: Section::load(&mut section)?,
636             debug_rnglists: Section::load(&mut section)?,
637             debug_types: Section::load(&mut section)?,
638             empty,
639         })
640     }
641 
642     /// Find the compilation unit with the given DWO identifier and return its section
643     /// contributions.
find_cu(&self, id: DwoId, parent: &Dwarf<R>) -> Result<Option<Dwarf<R>>>644     pub fn find_cu(&self, id: DwoId, parent: &Dwarf<R>) -> Result<Option<Dwarf<R>>> {
645         let row = match self.cu_index.find(id.0) {
646             Some(row) => row,
647             None => return Ok(None),
648         };
649         self.cu_sections(row, parent).map(Some)
650     }
651 
652     /// Find the type unit with the given type signature and return its section
653     /// contributions.
find_tu( &self, signature: DebugTypeSignature, parent: &Dwarf<R>, ) -> Result<Option<Dwarf<R>>>654     pub fn find_tu(
655         &self,
656         signature: DebugTypeSignature,
657         parent: &Dwarf<R>,
658     ) -> Result<Option<Dwarf<R>>> {
659         let row = match self.tu_index.find(signature.0) {
660             Some(row) => row,
661             None => return Ok(None),
662         };
663         self.tu_sections(row, parent).map(Some)
664     }
665 
666     /// Return the section contributions of the compilation unit at the given index.
667     ///
668     /// The index must be in the range `1..cu_index.unit_count`.
669     ///
670     /// This function should only be needed by low level parsers.
cu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>>671     pub fn cu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>> {
672         self.sections(self.cu_index.sections(index)?, parent)
673     }
674 
675     /// Return the section contributions of the compilation unit at the given index.
676     ///
677     /// The index must be in the range `1..tu_index.unit_count`.
678     ///
679     /// This function should only be needed by low level parsers.
tu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>>680     pub fn tu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>> {
681         self.sections(self.tu_index.sections(index)?, parent)
682     }
683 
684     /// Return the section contributions of a unit.
685     ///
686     /// This function should only be needed by low level parsers.
sections( &self, sections: UnitIndexSectionIterator<R>, parent: &Dwarf<R>, ) -> Result<Dwarf<R>>687     pub fn sections(
688         &self,
689         sections: UnitIndexSectionIterator<R>,
690         parent: &Dwarf<R>,
691     ) -> Result<Dwarf<R>> {
692         let mut abbrev_offset = 0;
693         let mut abbrev_size = 0;
694         let mut info_offset = 0;
695         let mut info_size = 0;
696         let mut line_offset = 0;
697         let mut line_size = 0;
698         let mut loc_offset = 0;
699         let mut loc_size = 0;
700         let mut loclists_offset = 0;
701         let mut loclists_size = 0;
702         let mut str_offsets_offset = 0;
703         let mut str_offsets_size = 0;
704         let mut rnglists_offset = 0;
705         let mut rnglists_size = 0;
706         let mut types_offset = 0;
707         let mut types_size = 0;
708         for section in sections {
709             match section.section {
710                 SectionId::DebugAbbrev => {
711                     abbrev_offset = section.offset;
712                     abbrev_size = section.size;
713                 }
714                 SectionId::DebugInfo => {
715                     info_offset = section.offset;
716                     info_size = section.size;
717                 }
718                 SectionId::DebugLine => {
719                     line_offset = section.offset;
720                     line_size = section.size;
721                 }
722                 SectionId::DebugLoc => {
723                     loc_offset = section.offset;
724                     loc_size = section.size;
725                 }
726                 SectionId::DebugLocLists => {
727                     loclists_offset = section.offset;
728                     loclists_size = section.size;
729                 }
730                 SectionId::DebugStrOffsets => {
731                     str_offsets_offset = section.offset;
732                     str_offsets_size = section.size;
733                 }
734                 SectionId::DebugRngLists => {
735                     rnglists_offset = section.offset;
736                     rnglists_size = section.size;
737                 }
738                 SectionId::DebugTypes => {
739                     types_offset = section.offset;
740                     types_size = section.size;
741                 }
742                 SectionId::DebugMacro | SectionId::DebugMacinfo => {
743                     // These are valid but we can't parse these yet.
744                 }
745                 _ => return Err(Error::UnknownIndexSection),
746             }
747         }
748 
749         let debug_abbrev = self.debug_abbrev.dwp_range(abbrev_offset, abbrev_size)?;
750         let debug_info = self.debug_info.dwp_range(info_offset, info_size)?;
751         let debug_line = self.debug_line.dwp_range(line_offset, line_size)?;
752         let debug_loc = self.debug_loc.dwp_range(loc_offset, loc_size)?;
753         let debug_loclists = self
754             .debug_loclists
755             .dwp_range(loclists_offset, loclists_size)?;
756         let debug_str_offsets = self
757             .debug_str_offsets
758             .dwp_range(str_offsets_offset, str_offsets_size)?;
759         let debug_rnglists = self
760             .debug_rnglists
761             .dwp_range(rnglists_offset, rnglists_size)?;
762         let debug_types = self.debug_types.dwp_range(types_offset, types_size)?;
763 
764         let debug_str = self.debug_str.clone();
765 
766         let debug_addr = parent.debug_addr.clone();
767         let debug_ranges = parent.ranges.debug_ranges().clone();
768 
769         let debug_aranges = self.empty.clone().into();
770         let debug_line_str = self.empty.clone().into();
771 
772         Ok(Dwarf {
773             debug_abbrev,
774             debug_addr,
775             debug_aranges,
776             debug_info,
777             debug_line,
778             debug_line_str,
779             debug_str,
780             debug_str_offsets,
781             debug_types,
782             locations: LocationLists::new(debug_loc, debug_loclists),
783             ranges: RangeLists::new(debug_ranges, debug_rnglists),
784             file_type: DwarfFileType::Dwo,
785             sup: None,
786         })
787     }
788 }
789 
790 /// All of the commonly used information for a unit in the `.debug_info` or `.debug_types`
791 /// sections.
792 #[derive(Debug)]
793 pub struct Unit<R, Offset = <R as Reader>::Offset>
794 where
795     R: Reader<Offset = Offset>,
796     Offset: ReaderOffset,
797 {
798     /// The header of the unit.
799     pub header: UnitHeader<R, Offset>,
800 
801     /// The parsed abbreviations for the unit.
802     pub abbreviations: Abbreviations,
803 
804     /// The `DW_AT_name` attribute of the unit.
805     pub name: Option<R>,
806 
807     /// The `DW_AT_comp_dir` attribute of the unit.
808     pub comp_dir: Option<R>,
809 
810     /// The `DW_AT_low_pc` attribute of the unit. Defaults to 0.
811     pub low_pc: u64,
812 
813     /// The `DW_AT_str_offsets_base` attribute of the unit. Defaults to 0.
814     pub str_offsets_base: DebugStrOffsetsBase<Offset>,
815 
816     /// The `DW_AT_addr_base` attribute of the unit. Defaults to 0.
817     pub addr_base: DebugAddrBase<Offset>,
818 
819     /// The `DW_AT_loclists_base` attribute of the unit. Defaults to 0.
820     pub loclists_base: DebugLocListsBase<Offset>,
821 
822     /// The `DW_AT_rnglists_base` attribute of the unit. Defaults to 0.
823     pub rnglists_base: DebugRngListsBase<Offset>,
824 
825     /// The line number program of the unit.
826     pub line_program: Option<IncompleteLineProgram<R, Offset>>,
827 
828     /// The DWO ID of a skeleton unit or split compilation unit.
829     pub dwo_id: Option<DwoId>,
830 }
831 
832 impl<R: Reader> Unit<R> {
833     /// Construct a new `Unit` from the given unit header.
834     #[inline]
new(dwarf: &Dwarf<R>, header: UnitHeader<R>) -> Result<Self>835     pub fn new(dwarf: &Dwarf<R>, header: UnitHeader<R>) -> Result<Self> {
836         let abbreviations = header.abbreviations(&dwarf.debug_abbrev)?;
837         let mut unit = Unit {
838             abbreviations,
839             name: None,
840             comp_dir: None,
841             low_pc: 0,
842             str_offsets_base: DebugStrOffsetsBase::default_for_encoding_and_file(
843                 header.encoding(),
844                 dwarf.file_type,
845             ),
846             // NB: Because the .debug_addr section never lives in a .dwo, we can assume its base is always 0 or provided.
847             addr_base: DebugAddrBase(R::Offset::from_u8(0)),
848             loclists_base: DebugLocListsBase::default_for_encoding_and_file(
849                 header.encoding(),
850                 dwarf.file_type,
851             ),
852             rnglists_base: DebugRngListsBase::default_for_encoding_and_file(
853                 header.encoding(),
854                 dwarf.file_type,
855             ),
856             line_program: None,
857             dwo_id: match header.type_() {
858                 UnitType::Skeleton(dwo_id) | UnitType::SplitCompilation(dwo_id) => Some(dwo_id),
859                 _ => None,
860             },
861             header,
862         };
863         let mut name = None;
864         let mut comp_dir = None;
865         let mut line_program_offset = None;
866         let mut low_pc_attr = None;
867 
868         {
869             let mut cursor = unit.header.entries(&unit.abbreviations);
870             cursor.next_dfs()?;
871             let root = cursor.current().ok_or(Error::MissingUnitDie)?;
872             let mut attrs = root.attrs();
873             while let Some(attr) = attrs.next()? {
874                 match attr.name() {
875                     constants::DW_AT_name => {
876                         name = Some(attr.value());
877                     }
878                     constants::DW_AT_comp_dir => {
879                         comp_dir = Some(attr.value());
880                     }
881                     constants::DW_AT_low_pc => {
882                         low_pc_attr = Some(attr.value());
883                     }
884                     constants::DW_AT_stmt_list => {
885                         if let AttributeValue::DebugLineRef(offset) = attr.value() {
886                             line_program_offset = Some(offset);
887                         }
888                     }
889                     constants::DW_AT_str_offsets_base => {
890                         if let AttributeValue::DebugStrOffsetsBase(base) = attr.value() {
891                             unit.str_offsets_base = base;
892                         }
893                     }
894                     constants::DW_AT_addr_base | constants::DW_AT_GNU_addr_base => {
895                         if let AttributeValue::DebugAddrBase(base) = attr.value() {
896                             unit.addr_base = base;
897                         }
898                     }
899                     constants::DW_AT_loclists_base => {
900                         if let AttributeValue::DebugLocListsBase(base) = attr.value() {
901                             unit.loclists_base = base;
902                         }
903                     }
904                     constants::DW_AT_rnglists_base | constants::DW_AT_GNU_ranges_base => {
905                         if let AttributeValue::DebugRngListsBase(base) = attr.value() {
906                             unit.rnglists_base = base;
907                         }
908                     }
909                     constants::DW_AT_GNU_dwo_id => {
910                         if unit.dwo_id.is_none() {
911                             if let AttributeValue::DwoId(dwo_id) = attr.value() {
912                                 unit.dwo_id = Some(dwo_id);
913                             }
914                         }
915                     }
916                     _ => {}
917                 }
918             }
919         }
920 
921         unit.name = match name {
922             Some(val) => dwarf.attr_string(&unit, val).ok(),
923             None => None,
924         };
925         unit.comp_dir = match comp_dir {
926             Some(val) => dwarf.attr_string(&unit, val).ok(),
927             None => None,
928         };
929         unit.line_program = match line_program_offset {
930             Some(offset) => Some(dwarf.debug_line.program(
931                 offset,
932                 unit.header.address_size(),
933                 unit.comp_dir.clone(),
934                 unit.name.clone(),
935             )?),
936             None => None,
937         };
938         if let Some(low_pc_attr) = low_pc_attr {
939             if let Some(addr) = dwarf.attr_address(&unit, low_pc_attr)? {
940                 unit.low_pc = addr;
941             }
942         }
943         Ok(unit)
944     }
945 
946     /// Return the encoding parameters for this unit.
947     #[inline]
encoding(&self) -> Encoding948     pub fn encoding(&self) -> Encoding {
949         self.header.encoding()
950     }
951 
952     /// Read the `DebuggingInformationEntry` at the given offset.
entry(&self, offset: UnitOffset<R::Offset>) -> Result<DebuggingInformationEntry<R>>953     pub fn entry(&self, offset: UnitOffset<R::Offset>) -> Result<DebuggingInformationEntry<R>> {
954         self.header.entry(&self.abbreviations, offset)
955     }
956 
957     /// Navigate this unit's `DebuggingInformationEntry`s.
958     #[inline]
entries(&self) -> EntriesCursor<R>959     pub fn entries(&self) -> EntriesCursor<R> {
960         self.header.entries(&self.abbreviations)
961     }
962 
963     /// Navigate this unit's `DebuggingInformationEntry`s
964     /// starting at the given offset.
965     #[inline]
entries_at_offset(&self, offset: UnitOffset<R::Offset>) -> Result<EntriesCursor<R>>966     pub fn entries_at_offset(&self, offset: UnitOffset<R::Offset>) -> Result<EntriesCursor<R>> {
967         self.header.entries_at_offset(&self.abbreviations, offset)
968     }
969 
970     /// Navigate this unit's `DebuggingInformationEntry`s as a tree
971     /// starting at the given offset.
972     #[inline]
entries_tree(&self, offset: Option<UnitOffset<R::Offset>>) -> Result<EntriesTree<R>>973     pub fn entries_tree(&self, offset: Option<UnitOffset<R::Offset>>) -> Result<EntriesTree<R>> {
974         self.header.entries_tree(&self.abbreviations, offset)
975     }
976 
977     /// Read the raw data that defines the Debugging Information Entries.
978     #[inline]
entries_raw(&self, offset: Option<UnitOffset<R::Offset>>) -> Result<EntriesRaw<R>>979     pub fn entries_raw(&self, offset: Option<UnitOffset<R::Offset>>) -> Result<EntriesRaw<R>> {
980         self.header.entries_raw(&self.abbreviations, offset)
981     }
982 
983     /// Copy attributes that are subject to relocation from another unit. This is intended
984     /// to be used to copy attributes from a skeleton compilation unit to the corresponding
985     /// split compilation unit.
copy_relocated_attributes(&mut self, other: &Unit<R>)986     pub fn copy_relocated_attributes(&mut self, other: &Unit<R>) {
987         self.low_pc = other.low_pc;
988         self.addr_base = other.addr_base;
989         if self.header.version() < 5 {
990             self.rnglists_base = other.rnglists_base;
991         }
992     }
993 }
994 
995 impl<T: ReaderOffset> UnitSectionOffset<T> {
996     /// Convert an offset to be relative to the start of the given unit,
997     /// instead of relative to the start of the section.
998     /// Returns `None` if the offset is not within the unit entries.
to_unit_offset<R>(&self, unit: &Unit<R>) -> Option<UnitOffset<T>> where R: Reader<Offset = T>,999     pub fn to_unit_offset<R>(&self, unit: &Unit<R>) -> Option<UnitOffset<T>>
1000     where
1001         R: Reader<Offset = T>,
1002     {
1003         let (offset, unit_offset) = match (self, unit.header.offset()) {
1004             (
1005                 UnitSectionOffset::DebugInfoOffset(offset),
1006                 UnitSectionOffset::DebugInfoOffset(unit_offset),
1007             ) => (offset.0, unit_offset.0),
1008             (
1009                 UnitSectionOffset::DebugTypesOffset(offset),
1010                 UnitSectionOffset::DebugTypesOffset(unit_offset),
1011             ) => (offset.0, unit_offset.0),
1012             _ => return None,
1013         };
1014         let offset = match offset.checked_sub(unit_offset) {
1015             Some(offset) => UnitOffset(offset),
1016             None => return None,
1017         };
1018         if !unit.header.is_valid_offset(offset) {
1019             return None;
1020         }
1021         Some(offset)
1022     }
1023 }
1024 
1025 impl<T: ReaderOffset> UnitOffset<T> {
1026     /// Convert an offset to be relative to the start of the .debug_info section,
1027     /// instead of relative to the start of the given compilation unit.
1028     ///
1029     /// Does not check that the offset is valid.
to_unit_section_offset<R>(&self, unit: &Unit<R>) -> UnitSectionOffset<T> where R: Reader<Offset = T>,1030     pub fn to_unit_section_offset<R>(&self, unit: &Unit<R>) -> UnitSectionOffset<T>
1031     where
1032         R: Reader<Offset = T>,
1033     {
1034         match unit.header.offset() {
1035             UnitSectionOffset::DebugInfoOffset(unit_offset) => {
1036                 DebugInfoOffset(unit_offset.0 + self.0).into()
1037             }
1038             UnitSectionOffset::DebugTypesOffset(unit_offset) => {
1039                 DebugTypesOffset(unit_offset.0 + self.0).into()
1040             }
1041         }
1042     }
1043 }
1044 
1045 /// An iterator for the address ranges of a `DebuggingInformationEntry`.
1046 ///
1047 /// Returned by `Dwarf::die_ranges` and `Dwarf::unit_ranges`.
1048 #[derive(Debug)]
1049 pub struct RangeIter<R: Reader>(RangeIterInner<R>);
1050 
1051 #[derive(Debug)]
1052 enum RangeIterInner<R: Reader> {
1053     Single(Option<Range>),
1054     List(RngListIter<R>),
1055 }
1056 
1057 impl<R: Reader> Default for RangeIter<R> {
default() -> Self1058     fn default() -> Self {
1059         RangeIter(RangeIterInner::Single(None))
1060     }
1061 }
1062 
1063 impl<R: Reader> RangeIter<R> {
1064     /// Advance the iterator to the next range.
next(&mut self) -> Result<Option<Range>>1065     pub fn next(&mut self) -> Result<Option<Range>> {
1066         match self.0 {
1067             RangeIterInner::Single(ref mut range) => Ok(range.take()),
1068             RangeIterInner::List(ref mut list) => list.next(),
1069         }
1070     }
1071 }
1072 
1073 #[cfg(feature = "fallible-iterator")]
1074 impl<R: Reader> fallible_iterator::FallibleIterator for RangeIter<R> {
1075     type Item = Range;
1076     type Error = Error;
1077 
1078     #[inline]
next(&mut self) -> ::core::result::Result<Option<Self::Item>, Self::Error>1079     fn next(&mut self) -> ::core::result::Result<Option<Self::Item>, Self::Error> {
1080         RangeIter::next(self)
1081     }
1082 }
1083 
1084 #[cfg(test)]
1085 mod tests {
1086     use super::*;
1087     use crate::read::EndianSlice;
1088     use crate::{Endianity, LittleEndian};
1089 
1090     /// Ensure that `Dwarf<R>` is covariant wrt R.
1091     #[test]
test_dwarf_variance()1092     fn test_dwarf_variance() {
1093         /// This only needs to compile.
1094         fn _f<'a: 'b, 'b, E: Endianity>(x: Dwarf<EndianSlice<'a, E>>) -> Dwarf<EndianSlice<'b, E>> {
1095             x
1096         }
1097     }
1098 
1099     /// Ensure that `Unit<R>` is covariant wrt R.
1100     #[test]
test_dwarf_unit_variance()1101     fn test_dwarf_unit_variance() {
1102         /// This only needs to compile.
1103         fn _f<'a: 'b, 'b, E: Endianity>(x: Unit<EndianSlice<'a, E>>) -> Unit<EndianSlice<'b, E>> {
1104             x
1105         }
1106     }
1107 
1108     #[test]
test_send()1109     fn test_send() {
1110         fn assert_is_send<T: Send>() {}
1111         assert_is_send::<Dwarf<EndianSlice<LittleEndian>>>();
1112         assert_is_send::<Unit<EndianSlice<LittleEndian>>>();
1113     }
1114 
1115     #[test]
test_format_error()1116     fn test_format_error() {
1117         let mut owned_dwarf = Dwarf::load(|_| -> Result<_> { Ok(vec![1, 2]) }).unwrap();
1118         owned_dwarf
1119             .load_sup(|_| -> Result<_> { Ok(vec![1, 2]) })
1120             .unwrap();
1121         let dwarf = owned_dwarf.borrow(|section| EndianSlice::new(&section, LittleEndian));
1122 
1123         match dwarf.debug_str.get_str(DebugStrOffset(1)) {
1124             Ok(r) => panic!("Unexpected str {:?}", r),
1125             Err(e) => {
1126                 assert_eq!(
1127                     dwarf.format_error(e),
1128                     "Hit the end of input before it was expected at .debug_str+0x1"
1129                 );
1130             }
1131         }
1132         match dwarf.sup().unwrap().debug_str.get_str(DebugStrOffset(1)) {
1133             Ok(r) => panic!("Unexpected str {:?}", r),
1134             Err(e) => {
1135                 assert_eq!(
1136                     dwarf.format_error(e),
1137                     "Hit the end of input before it was expected at .debug_str(sup)+0x1"
1138                 );
1139             }
1140         }
1141         assert_eq!(dwarf.format_error(Error::Io), Error::Io.description());
1142     }
1143 }
1144