1use alloc::string::String;
2use alloc::sync::Arc;
3
4use crate::common::{
5 DebugAddrBase, DebugAddrIndex, DebugInfoOffset, DebugLineStrOffset, DebugLocListsBase,
6 DebugLocListsIndex, DebugMacinfoOffset, DebugRngListsBase, DebugRngListsIndex, DebugStrOffset,
7 DebugStrOffsetsBase, DebugStrOffsetsIndex, DebugTypeSignature, DebugTypesOffset, DwarfFileType,
8 DwoId, Encoding, LocationListsOffset, RangeListsOffset, RawRangeListsOffset, SectionId,
9 UnitSectionOffset,
10};
11use crate::read::{
12 Abbreviations, AbbreviationsCache, AbbreviationsCacheStrategy, AttributeValue, DebugAbbrev,
13 DebugAddr, DebugAranges, DebugCuIndex, DebugInfo, DebugInfoUnitHeadersIter, DebugLine,
14 DebugLineStr, DebugLoc, DebugLocLists, DebugMacinfo, DebugMacro, DebugRanges, DebugRngLists,
15 DebugStr, DebugStrOffsets, DebugTuIndex, DebugTypes, DebugTypesUnitHeadersIter,
16 DebuggingInformationEntry, EntriesCursor, EntriesRaw, EntriesTree, Error,
17 IncompleteLineProgram, IndexSectionId, LocListIter, LocationLists, MacroIter, Range,
18 RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId, Result,
19 RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset, UnitType,
20};
21use crate::{constants, DebugMacroOffset};
22
23#[derive(Debug, Default)]
51pub struct DwarfSections<T> {
52 pub debug_abbrev: DebugAbbrev<T>,
54 pub debug_addr: DebugAddr<T>,
56 pub debug_aranges: DebugAranges<T>,
58 pub debug_info: DebugInfo<T>,
60 pub debug_line: DebugLine<T>,
62 pub debug_line_str: DebugLineStr<T>,
64 pub debug_macinfo: DebugMacinfo<T>,
66 pub debug_macro: DebugMacro<T>,
68 pub debug_str: DebugStr<T>,
70 pub debug_str_offsets: DebugStrOffsets<T>,
72 pub debug_types: DebugTypes<T>,
74 pub debug_loc: DebugLoc<T>,
76 pub debug_loclists: DebugLocLists<T>,
78 pub debug_ranges: DebugRanges<T>,
80 pub debug_rnglists: DebugRngLists<T>,
82}
83
84impl<T> DwarfSections<T> {
85 pub fn load<F, E>(mut section: F) -> core::result::Result<Self, E>
90 where
91 F: FnMut(SectionId) -> core::result::Result<T, E>,
92 {
93 Ok(DwarfSections {
94 debug_abbrev: Section::load(&mut section)?,
96 debug_addr: Section::load(&mut section)?,
97 debug_aranges: Section::load(&mut section)?,
98 debug_info: Section::load(&mut section)?,
99 debug_line: Section::load(&mut section)?,
100 debug_line_str: Section::load(&mut section)?,
101 debug_macinfo: Section::load(&mut section)?,
102 debug_macro: Section::load(&mut section)?,
103 debug_str: Section::load(&mut section)?,
104 debug_str_offsets: Section::load(&mut section)?,
105 debug_types: Section::load(&mut section)?,
106 debug_loc: Section::load(&mut section)?,
107 debug_loclists: Section::load(&mut section)?,
108 debug_ranges: Section::load(&mut section)?,
109 debug_rnglists: Section::load(&mut section)?,
110 })
111 }
112
113 pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> Dwarf<R>
115 where
116 F: FnMut(&'a T) -> R,
117 {
118 Dwarf::from_sections(DwarfSections {
119 debug_abbrev: self.debug_abbrev.borrow(&mut borrow),
120 debug_addr: self.debug_addr.borrow(&mut borrow),
121 debug_aranges: self.debug_aranges.borrow(&mut borrow),
122 debug_info: self.debug_info.borrow(&mut borrow),
123 debug_line: self.debug_line.borrow(&mut borrow),
124 debug_line_str: self.debug_line_str.borrow(&mut borrow),
125 debug_macinfo: self.debug_macinfo.borrow(&mut borrow),
126 debug_macro: self.debug_macro.borrow(&mut borrow),
127 debug_str: self.debug_str.borrow(&mut borrow),
128 debug_str_offsets: self.debug_str_offsets.borrow(&mut borrow),
129 debug_types: self.debug_types.borrow(&mut borrow),
130 debug_loc: self.debug_loc.borrow(&mut borrow),
131 debug_loclists: self.debug_loclists.borrow(&mut borrow),
132 debug_ranges: self.debug_ranges.borrow(&mut borrow),
133 debug_rnglists: self.debug_rnglists.borrow(&mut borrow),
134 })
135 }
136
137 pub fn borrow_with_sup<'a, F, R>(&'a self, sup: &'a Self, mut borrow: F) -> Dwarf<R>
159 where
160 F: FnMut(&'a T) -> R,
161 {
162 let mut dwarf = self.borrow(&mut borrow);
163 dwarf.set_sup(sup.borrow(&mut borrow));
164 dwarf
165 }
166}
167
168#[derive(Debug, Default)]
170pub struct Dwarf<R> {
171 pub debug_abbrev: DebugAbbrev<R>,
173
174 pub debug_addr: DebugAddr<R>,
176
177 pub debug_aranges: DebugAranges<R>,
179
180 pub debug_info: DebugInfo<R>,
182
183 pub debug_line: DebugLine<R>,
185
186 pub debug_line_str: DebugLineStr<R>,
188
189 pub debug_macinfo: DebugMacinfo<R>,
191
192 pub debug_macro: DebugMacro<R>,
194
195 pub debug_str: DebugStr<R>,
197
198 pub debug_str_offsets: DebugStrOffsets<R>,
200
201 pub debug_types: DebugTypes<R>,
203
204 pub locations: LocationLists<R>,
206
207 pub ranges: RangeLists<R>,
209
210 pub file_type: DwarfFileType,
212
213 pub sup: Option<Arc<Dwarf<R>>>,
215
216 pub abbreviations_cache: AbbreviationsCache,
218}
219
220impl<T> Dwarf<T> {
221 pub fn load<F, E>(section: F) -> core::result::Result<Self, E>
229 where
230 F: FnMut(SectionId) -> core::result::Result<T, E>,
231 {
232 let sections = DwarfSections::load(section)?;
233 Ok(Self::from_sections(sections))
234 }
235
236 pub fn load_sup<F, E>(&mut self, section: F) -> core::result::Result<(), E>
242 where
243 F: FnMut(SectionId) -> core::result::Result<T, E>,
244 {
245 self.set_sup(Self::load(section)?);
246 Ok(())
247 }
248
249 fn from_sections(sections: DwarfSections<T>) -> Self {
253 Dwarf {
254 debug_abbrev: sections.debug_abbrev,
255 debug_addr: sections.debug_addr,
256 debug_aranges: sections.debug_aranges,
257 debug_info: sections.debug_info,
258 debug_line: sections.debug_line,
259 debug_line_str: sections.debug_line_str,
260 debug_macinfo: sections.debug_macinfo,
261 debug_macro: sections.debug_macro,
262 debug_str: sections.debug_str,
263 debug_str_offsets: sections.debug_str_offsets,
264 debug_types: sections.debug_types,
265 locations: LocationLists::new(sections.debug_loc, sections.debug_loclists),
266 ranges: RangeLists::new(sections.debug_ranges, sections.debug_rnglists),
267 file_type: DwarfFileType::Main,
268 sup: None,
269 abbreviations_cache: AbbreviationsCache::new(),
270 }
271 }
272
273 #[deprecated(note = "use `DwarfSections::borrow` instead")]
301 pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> Dwarf<R>
302 where
303 F: FnMut(&'a T) -> R,
304 {
305 Dwarf {
306 debug_abbrev: self.debug_abbrev.borrow(&mut borrow),
307 debug_addr: self.debug_addr.borrow(&mut borrow),
308 debug_aranges: self.debug_aranges.borrow(&mut borrow),
309 debug_info: self.debug_info.borrow(&mut borrow),
310 debug_line: self.debug_line.borrow(&mut borrow),
311 debug_line_str: self.debug_line_str.borrow(&mut borrow),
312 debug_macinfo: self.debug_macinfo.borrow(&mut borrow),
313 debug_macro: self.debug_macro.borrow(&mut borrow),
314 debug_str: self.debug_str.borrow(&mut borrow),
315 debug_str_offsets: self.debug_str_offsets.borrow(&mut borrow),
316 debug_types: self.debug_types.borrow(&mut borrow),
317 locations: self.locations.borrow(&mut borrow),
318 ranges: self.ranges.borrow(&mut borrow),
319 file_type: self.file_type,
320 sup: self.sup().map(|sup| Arc::new(sup.borrow(borrow))),
321 abbreviations_cache: AbbreviationsCache::new(),
322 }
323 }
324
325 pub fn set_sup(&mut self, sup: Dwarf<T>) {
327 self.sup = Some(Arc::new(sup));
328 }
329
330 pub fn sup(&self) -> Option<&Dwarf<T>> {
332 self.sup.as_ref().map(Arc::as_ref)
333 }
334}
335
336impl<R: Reader> Dwarf<R> {
337 pub fn populate_abbreviations_cache(&mut self, strategy: AbbreviationsCacheStrategy) {
345 self.abbreviations_cache
346 .populate(strategy, &self.debug_abbrev, self.debug_info.units());
347 }
348
349 #[inline]
354 pub fn units(&self) -> DebugInfoUnitHeadersIter<R> {
355 self.debug_info.units()
356 }
357
358 #[inline]
360 pub fn unit(&self, header: UnitHeader<R>) -> Result<Unit<R>> {
361 Unit::new(self, header)
362 }
363
364 #[inline]
369 pub fn type_units(&self) -> DebugTypesUnitHeadersIter<R> {
370 self.debug_types.units()
371 }
372
373 #[inline]
375 pub fn abbreviations(&self, unit: &UnitHeader<R>) -> Result<Arc<Abbreviations>> {
376 self.abbreviations_cache
377 .get(&self.debug_abbrev, unit.debug_abbrev_offset())
378 }
379
380 #[inline]
382 pub fn string_offset(
383 &self,
384 unit: &Unit<R>,
385 index: DebugStrOffsetsIndex<R::Offset>,
386 ) -> Result<DebugStrOffset<R::Offset>> {
387 self.debug_str_offsets
388 .get_str_offset(unit.header.format(), unit.str_offsets_base, index)
389 }
390
391 #[inline]
393 pub fn string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R> {
394 self.debug_str.get_str(offset)
395 }
396
397 #[inline]
399 pub fn line_string(&self, offset: DebugLineStrOffset<R::Offset>) -> Result<R> {
400 self.debug_line_str.get_str(offset)
401 }
402
403 #[inline]
406 pub fn sup_string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R> {
407 if let Some(sup) = self.sup() {
408 sup.debug_str.get_str(offset)
409 } else {
410 Err(Error::ExpectedStringAttributeValue)
411 }
412 }
413
414 pub fn attr_string(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<R> {
429 match attr {
430 AttributeValue::String(string) => Ok(string),
431 AttributeValue::DebugStrRef(offset) => self.string(offset),
432 AttributeValue::DebugStrRefSup(offset) => self.sup_string(offset),
433 AttributeValue::DebugLineStrRef(offset) => self.line_string(offset),
434 AttributeValue::DebugStrOffsetsIndex(index) => {
435 let offset = self.string_offset(unit, index)?;
436 self.string(offset)
437 }
438 _ => Err(Error::ExpectedStringAttributeValue),
439 }
440 }
441
442 pub fn address(&self, unit: &Unit<R>, index: DebugAddrIndex<R::Offset>) -> Result<u64> {
444 self.debug_addr
445 .get_address(unit.encoding().address_size, unit.addr_base, index)
446 }
447
448 pub fn attr_address(&self, unit: &Unit<R>, attr: AttributeValue<R>) -> Result<Option<u64>> {
458 match attr {
459 AttributeValue::Addr(addr) => Ok(Some(addr)),
460 AttributeValue::DebugAddrIndex(index) => self.address(unit, index).map(Some),
461 _ => Ok(None),
462 }
463 }
464
465 pub fn ranges_offset_from_raw(
469 &self,
470 unit: &Unit<R>,
471 offset: RawRangeListsOffset<R::Offset>,
472 ) -> RangeListsOffset<R::Offset> {
473 if self.file_type == DwarfFileType::Dwo && unit.header.version() < 5 {
474 RangeListsOffset(offset.0.wrapping_add(unit.rnglists_base.0))
475 } else {
476 RangeListsOffset(offset.0)
477 }
478 }
479
480 pub fn ranges_offset(
482 &self,
483 unit: &Unit<R>,
484 index: DebugRngListsIndex<R::Offset>,
485 ) -> Result<RangeListsOffset<R::Offset>> {
486 self.ranges
487 .get_offset(unit.encoding(), unit.rnglists_base, index)
488 }
489
490 pub fn ranges(
492 &self,
493 unit: &Unit<R>,
494 offset: RangeListsOffset<R::Offset>,
495 ) -> Result<RngListIter<R>> {
496 self.ranges.ranges(
497 offset,
498 unit.encoding(),
499 unit.low_pc,
500 &self.debug_addr,
501 unit.addr_base,
502 )
503 }
504
505 pub fn raw_ranges(
507 &self,
508 unit: &Unit<R>,
509 offset: RangeListsOffset<R::Offset>,
510 ) -> Result<RawRngListIter<R>> {
511 self.ranges.raw_ranges(offset, unit.encoding())
512 }
513
514 pub fn attr_ranges_offset(
524 &self,
525 unit: &Unit<R>,
526 attr: AttributeValue<R>,
527 ) -> Result<Option<RangeListsOffset<R::Offset>>> {
528 match attr {
529 AttributeValue::RangeListsRef(offset) => {
530 Ok(Some(self.ranges_offset_from_raw(unit, offset)))
531 }
532 AttributeValue::DebugRngListsIndex(index) => self.ranges_offset(unit, index).map(Some),
533 _ => Ok(None),
534 }
535 }
536
537 pub fn attr_ranges(
547 &self,
548 unit: &Unit<R>,
549 attr: AttributeValue<R>,
550 ) -> Result<Option<RngListIter<R>>> {
551 match self.attr_ranges_offset(unit, attr)? {
552 Some(offset) => Ok(Some(self.ranges(unit, offset)?)),
553 None => Ok(None),
554 }
555 }
556
557 pub fn die_ranges(
561 &self,
562 unit: &Unit<R>,
563 entry: &DebuggingInformationEntry<'_, '_, R>,
564 ) -> Result<RangeIter<R>> {
565 let mut low_pc = None;
566 let mut high_pc = None;
567 let mut size = None;
568 let mut attrs = entry.attrs();
569 while let Some(attr) = attrs.next()? {
570 match attr.name() {
571 constants::DW_AT_low_pc => {
572 low_pc = Some(
573 self.attr_address(unit, attr.value())?
574 .ok_or(Error::UnsupportedAttributeForm)?,
575 );
576 }
577 constants::DW_AT_high_pc => match attr.value() {
578 AttributeValue::Udata(val) => size = Some(val),
579 attr => {
580 high_pc = Some(
581 self.attr_address(unit, attr)?
582 .ok_or(Error::UnsupportedAttributeForm)?,
583 );
584 }
585 },
586 constants::DW_AT_ranges => {
587 if let Some(list) = self.attr_ranges(unit, attr.value())? {
588 return Ok(RangeIter(RangeIterInner::List(list)));
589 }
590 }
591 _ => {}
592 }
593 }
594 let range = low_pc.and_then(|begin| {
595 let end = size.map(|size| begin + size).or(high_pc);
596 end.map(|end| Range { begin, end })
598 });
599 Ok(RangeIter(RangeIterInner::Single(range)))
600 }
601
602 pub fn unit_ranges(&self, unit: &Unit<R>) -> Result<RangeIter<R>> {
607 let mut cursor = unit.header.entries(&unit.abbreviations);
608 cursor.next_dfs()?;
609 let root = cursor.current().ok_or(Error::MissingUnitDie)?;
610 self.die_ranges(unit, root)
611 }
612
613 pub fn locations_offset(
615 &self,
616 unit: &Unit<R>,
617 index: DebugLocListsIndex<R::Offset>,
618 ) -> Result<LocationListsOffset<R::Offset>> {
619 self.locations
620 .get_offset(unit.encoding(), unit.loclists_base, index)
621 }
622
623 pub fn locations(
625 &self,
626 unit: &Unit<R>,
627 offset: LocationListsOffset<R::Offset>,
628 ) -> Result<LocListIter<R>> {
629 match self.file_type {
630 DwarfFileType::Main => self.locations.locations(
631 offset,
632 unit.encoding(),
633 unit.low_pc,
634 &self.debug_addr,
635 unit.addr_base,
636 ),
637 DwarfFileType::Dwo => self.locations.locations_dwo(
638 offset,
639 unit.encoding(),
640 unit.low_pc,
641 &self.debug_addr,
642 unit.addr_base,
643 ),
644 }
645 }
646
647 pub fn raw_locations(
649 &self,
650 unit: &Unit<R>,
651 offset: LocationListsOffset<R::Offset>,
652 ) -> Result<RawLocListIter<R>> {
653 match self.file_type {
654 DwarfFileType::Main => self.locations.raw_locations(offset, unit.encoding()),
655 DwarfFileType::Dwo => self.locations.raw_locations_dwo(offset, unit.encoding()),
656 }
657 }
658
659 pub fn attr_locations_offset(
669 &self,
670 unit: &Unit<R>,
671 attr: AttributeValue<R>,
672 ) -> Result<Option<LocationListsOffset<R::Offset>>> {
673 match attr {
674 AttributeValue::LocationListsRef(offset) => Ok(Some(offset)),
675 AttributeValue::DebugLocListsIndex(index) => {
676 self.locations_offset(unit, index).map(Some)
677 }
678 _ => Ok(None),
679 }
680 }
681
682 pub fn attr_locations(
692 &self,
693 unit: &Unit<R>,
694 attr: AttributeValue<R>,
695 ) -> Result<Option<LocListIter<R>>> {
696 match self.attr_locations_offset(unit, attr)? {
697 Some(offset) => Ok(Some(self.locations(unit, offset)?)),
698 None => Ok(None),
699 }
700 }
701
702 pub fn lookup_offset_id(&self, id: ReaderOffsetId) -> Option<(bool, SectionId, R::Offset)> {
706 None.or_else(|| self.debug_abbrev.lookup_offset_id(id))
707 .or_else(|| self.debug_addr.lookup_offset_id(id))
708 .or_else(|| self.debug_aranges.lookup_offset_id(id))
709 .or_else(|| self.debug_info.lookup_offset_id(id))
710 .or_else(|| self.debug_line.lookup_offset_id(id))
711 .or_else(|| self.debug_line_str.lookup_offset_id(id))
712 .or_else(|| self.debug_str.lookup_offset_id(id))
713 .or_else(|| self.debug_str_offsets.lookup_offset_id(id))
714 .or_else(|| self.debug_types.lookup_offset_id(id))
715 .or_else(|| self.locations.lookup_offset_id(id))
716 .or_else(|| self.ranges.lookup_offset_id(id))
717 .map(|(id, offset)| (false, id, offset))
718 .or_else(|| {
719 self.sup()
720 .and_then(|sup| sup.lookup_offset_id(id))
721 .map(|(_, id, offset)| (true, id, offset))
722 })
723 }
724
725 pub fn format_error(&self, err: Error) -> String {
729 #[allow(clippy::single_match)]
730 match err {
731 Error::UnexpectedEof(id) => match self.lookup_offset_id(id) {
732 Some((sup, section, offset)) => {
733 return format!(
734 "{} at {}{}+0x{:x}",
735 err,
736 section.name(),
737 if sup { "(sup)" } else { "" },
738 offset.into_u64(),
739 );
740 }
741 None => {}
742 },
743 _ => {}
744 }
745 err.description().into()
746 }
747
748 pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacroIter<R>> {
750 self.debug_macinfo.get_macinfo(offset)
751 }
752
753 pub fn macros(&self, offset: DebugMacroOffset<R::Offset>) -> Result<MacroIter<R>> {
755 self.debug_macro.get_macros(offset)
756 }
757}
758
759impl<R: Clone> Dwarf<R> {
760 pub fn make_dwo(&mut self, parent: &Dwarf<R>) {
764 self.file_type = DwarfFileType::Dwo;
765 self.debug_addr = parent.debug_addr.clone();
767 self.ranges
770 .set_debug_ranges(parent.ranges.debug_ranges().clone());
771 self.sup.clone_from(&parent.sup);
772 }
773}
774
775#[derive(Debug, Default)]
804pub struct DwarfPackageSections<T> {
805 pub cu_index: DebugCuIndex<T>,
807 pub tu_index: DebugTuIndex<T>,
809 pub debug_abbrev: DebugAbbrev<T>,
811 pub debug_info: DebugInfo<T>,
813 pub debug_line: DebugLine<T>,
815 pub debug_str: DebugStr<T>,
817 pub debug_str_offsets: DebugStrOffsets<T>,
819 pub debug_loc: DebugLoc<T>,
823 pub debug_loclists: DebugLocLists<T>,
825 pub debug_rnglists: DebugRngLists<T>,
827 pub debug_types: DebugTypes<T>,
831}
832
833impl<T> DwarfPackageSections<T> {
834 pub fn load<F, E>(mut section: F) -> core::result::Result<Self, E>
839 where
840 F: FnMut(SectionId) -> core::result::Result<T, E>,
841 E: From<Error>,
842 {
843 Ok(DwarfPackageSections {
844 cu_index: Section::load(&mut section)?,
846 tu_index: Section::load(&mut section)?,
847 debug_abbrev: Section::load(&mut section)?,
848 debug_info: Section::load(&mut section)?,
849 debug_line: Section::load(&mut section)?,
850 debug_str: Section::load(&mut section)?,
851 debug_str_offsets: Section::load(&mut section)?,
852 debug_loc: Section::load(&mut section)?,
853 debug_loclists: Section::load(&mut section)?,
854 debug_rnglists: Section::load(&mut section)?,
855 debug_types: Section::load(&mut section)?,
856 })
857 }
858
859 pub fn borrow<'a, F, R>(&'a self, mut borrow: F, empty: R) -> Result<DwarfPackage<R>>
861 where
862 F: FnMut(&'a T) -> R,
863 R: Reader,
864 {
865 DwarfPackage::from_sections(
866 DwarfPackageSections {
867 cu_index: self.cu_index.borrow(&mut borrow),
868 tu_index: self.tu_index.borrow(&mut borrow),
869 debug_abbrev: self.debug_abbrev.borrow(&mut borrow),
870 debug_info: self.debug_info.borrow(&mut borrow),
871 debug_line: self.debug_line.borrow(&mut borrow),
872 debug_str: self.debug_str.borrow(&mut borrow),
873 debug_str_offsets: self.debug_str_offsets.borrow(&mut borrow),
874 debug_loc: self.debug_loc.borrow(&mut borrow),
875 debug_loclists: self.debug_loclists.borrow(&mut borrow),
876 debug_rnglists: self.debug_rnglists.borrow(&mut borrow),
877 debug_types: self.debug_types.borrow(&mut borrow),
878 },
879 empty,
880 )
881 }
882}
883
884#[derive(Debug)]
886pub struct DwarfPackage<R: Reader> {
887 pub cu_index: UnitIndex<R>,
889
890 pub tu_index: UnitIndex<R>,
892
893 pub debug_abbrev: DebugAbbrev<R>,
895
896 pub debug_info: DebugInfo<R>,
898
899 pub debug_line: DebugLine<R>,
901
902 pub debug_str: DebugStr<R>,
904
905 pub debug_str_offsets: DebugStrOffsets<R>,
907
908 pub debug_loc: DebugLoc<R>,
912
913 pub debug_loclists: DebugLocLists<R>,
915
916 pub debug_rnglists: DebugRngLists<R>,
918
919 pub debug_types: DebugTypes<R>,
923
924 pub empty: R,
928}
929
930impl<R: Reader> DwarfPackage<R> {
931 pub fn load<F, E>(section: F, empty: R) -> core::result::Result<Self, E>
936 where
937 F: FnMut(SectionId) -> core::result::Result<R, E>,
938 E: From<Error>,
939 {
940 let sections = DwarfPackageSections::load(section)?;
941 Ok(Self::from_sections(sections, empty)?)
942 }
943
944 fn from_sections(sections: DwarfPackageSections<R>, empty: R) -> Result<Self> {
946 Ok(DwarfPackage {
947 cu_index: sections.cu_index.index()?,
948 tu_index: sections.tu_index.index()?,
949 debug_abbrev: sections.debug_abbrev,
950 debug_info: sections.debug_info,
951 debug_line: sections.debug_line,
952 debug_str: sections.debug_str,
953 debug_str_offsets: sections.debug_str_offsets,
954 debug_loc: sections.debug_loc,
955 debug_loclists: sections.debug_loclists,
956 debug_rnglists: sections.debug_rnglists,
957 debug_types: sections.debug_types,
958 empty,
959 })
960 }
961
962 pub fn find_cu(&self, id: DwoId, parent: &Dwarf<R>) -> Result<Option<Dwarf<R>>> {
981 let row = match self.cu_index.find(id.0) {
982 Some(row) => row,
983 None => return Ok(None),
984 };
985 self.cu_sections(row, parent).map(Some)
986 }
987
988 pub fn find_tu(
991 &self,
992 signature: DebugTypeSignature,
993 parent: &Dwarf<R>,
994 ) -> Result<Option<Dwarf<R>>> {
995 let row = match self.tu_index.find(signature.0) {
996 Some(row) => row,
997 None => return Ok(None),
998 };
999 self.tu_sections(row, parent).map(Some)
1000 }
1001
1002 pub fn cu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>> {
1008 self.sections(self.cu_index.sections(index)?, parent)
1009 }
1010
1011 pub fn tu_sections(&self, index: u32, parent: &Dwarf<R>) -> Result<Dwarf<R>> {
1017 self.sections(self.tu_index.sections(index)?, parent)
1018 }
1019
1020 pub fn sections(
1024 &self,
1025 sections: UnitIndexSectionIterator<'_, R>,
1026 parent: &Dwarf<R>,
1027 ) -> Result<Dwarf<R>> {
1028 let mut abbrev_offset = 0;
1029 let mut abbrev_size = 0;
1030 let mut info_offset = 0;
1031 let mut info_size = 0;
1032 let mut line_offset = 0;
1033 let mut line_size = 0;
1034 let mut loc_offset = 0;
1035 let mut loc_size = 0;
1036 let mut loclists_offset = 0;
1037 let mut loclists_size = 0;
1038 let mut str_offsets_offset = 0;
1039 let mut str_offsets_size = 0;
1040 let mut rnglists_offset = 0;
1041 let mut rnglists_size = 0;
1042 let mut types_offset = 0;
1043 let mut types_size = 0;
1044 for section in sections {
1045 match section.section {
1046 IndexSectionId::DebugAbbrev => {
1047 abbrev_offset = section.offset;
1048 abbrev_size = section.size;
1049 }
1050 IndexSectionId::DebugInfo => {
1051 info_offset = section.offset;
1052 info_size = section.size;
1053 }
1054 IndexSectionId::DebugLine => {
1055 line_offset = section.offset;
1056 line_size = section.size;
1057 }
1058 IndexSectionId::DebugLoc => {
1059 loc_offset = section.offset;
1060 loc_size = section.size;
1061 }
1062 IndexSectionId::DebugLocLists => {
1063 loclists_offset = section.offset;
1064 loclists_size = section.size;
1065 }
1066 IndexSectionId::DebugStrOffsets => {
1067 str_offsets_offset = section.offset;
1068 str_offsets_size = section.size;
1069 }
1070 IndexSectionId::DebugRngLists => {
1071 rnglists_offset = section.offset;
1072 rnglists_size = section.size;
1073 }
1074 IndexSectionId::DebugTypes => {
1075 types_offset = section.offset;
1076 types_size = section.size;
1077 }
1078 IndexSectionId::DebugMacro | IndexSectionId::DebugMacinfo => {
1079 }
1081 }
1082 }
1083
1084 let debug_abbrev = self.debug_abbrev.dwp_range(abbrev_offset, abbrev_size)?;
1085 let debug_info = self.debug_info.dwp_range(info_offset, info_size)?;
1086 let debug_line = self.debug_line.dwp_range(line_offset, line_size)?;
1087 let debug_loc = self.debug_loc.dwp_range(loc_offset, loc_size)?;
1088 let debug_loclists = self
1089 .debug_loclists
1090 .dwp_range(loclists_offset, loclists_size)?;
1091 let debug_str_offsets = self
1092 .debug_str_offsets
1093 .dwp_range(str_offsets_offset, str_offsets_size)?;
1094 let debug_rnglists = self
1095 .debug_rnglists
1096 .dwp_range(rnglists_offset, rnglists_size)?;
1097 let debug_types = self.debug_types.dwp_range(types_offset, types_size)?;
1098
1099 let debug_str = self.debug_str.clone();
1100
1101 let debug_addr = parent.debug_addr.clone();
1102 let debug_ranges = parent.ranges.debug_ranges().clone();
1103
1104 let debug_aranges = self.empty.clone().into();
1105 let debug_line_str = self.empty.clone().into();
1106 let debug_macinfo = self.empty.clone().into();
1107 let debug_macro = self.empty.clone().into();
1108
1109 Ok(Dwarf {
1110 debug_abbrev,
1111 debug_addr,
1112 debug_aranges,
1113 debug_info,
1114 debug_line,
1115 debug_line_str,
1116 debug_macinfo,
1117 debug_macro,
1118 debug_str,
1119 debug_str_offsets,
1120 debug_types,
1121 locations: LocationLists::new(debug_loc, debug_loclists),
1122 ranges: RangeLists::new(debug_ranges, debug_rnglists),
1123 file_type: DwarfFileType::Dwo,
1124 sup: parent.sup.clone(),
1125 abbreviations_cache: AbbreviationsCache::new(),
1126 })
1127 }
1128}
1129
1130#[derive(Debug)]
1133pub struct Unit<R, Offset = <R as Reader>::Offset>
1134where
1135 R: Reader<Offset = Offset>,
1136 Offset: ReaderOffset,
1137{
1138 pub header: UnitHeader<R, Offset>,
1140
1141 pub abbreviations: Arc<Abbreviations>,
1143
1144 pub name: Option<R>,
1146
1147 pub comp_dir: Option<R>,
1149
1150 pub low_pc: u64,
1152
1153 pub str_offsets_base: DebugStrOffsetsBase<Offset>,
1155
1156 pub addr_base: DebugAddrBase<Offset>,
1158
1159 pub loclists_base: DebugLocListsBase<Offset>,
1161
1162 pub rnglists_base: DebugRngListsBase<Offset>,
1164
1165 pub line_program: Option<IncompleteLineProgram<R, Offset>>,
1167
1168 pub dwo_id: Option<DwoId>,
1170}
1171
1172impl<R: Reader> Unit<R> {
1173 #[inline]
1175 pub fn new(dwarf: &Dwarf<R>, header: UnitHeader<R>) -> Result<Self> {
1176 let abbreviations = dwarf.abbreviations(&header)?;
1177 Self::new_with_abbreviations(dwarf, header, abbreviations)
1178 }
1179
1180 #[inline]
1186 pub fn new_with_abbreviations(
1187 dwarf: &Dwarf<R>,
1188 header: UnitHeader<R>,
1189 abbreviations: Arc<Abbreviations>,
1190 ) -> Result<Self> {
1191 let mut unit = Unit {
1192 abbreviations,
1193 name: None,
1194 comp_dir: None,
1195 low_pc: 0,
1196 str_offsets_base: DebugStrOffsetsBase::default_for_encoding_and_file(
1197 header.encoding(),
1198 dwarf.file_type,
1199 ),
1200 addr_base: DebugAddrBase(R::Offset::from_u8(0)),
1202 loclists_base: DebugLocListsBase::default_for_encoding_and_file(
1203 header.encoding(),
1204 dwarf.file_type,
1205 ),
1206 rnglists_base: DebugRngListsBase::default_for_encoding_and_file(
1207 header.encoding(),
1208 dwarf.file_type,
1209 ),
1210 line_program: None,
1211 dwo_id: match header.type_() {
1212 UnitType::Skeleton(dwo_id) | UnitType::SplitCompilation(dwo_id) => Some(dwo_id),
1213 _ => None,
1214 },
1215 header,
1216 };
1217 let mut name = None;
1218 let mut comp_dir = None;
1219 let mut line_program_offset = None;
1220 let mut low_pc_attr = None;
1221
1222 {
1223 let mut cursor = unit.header.entries(&unit.abbreviations);
1224 cursor.next_dfs()?;
1225 let root = cursor.current().ok_or(Error::MissingUnitDie)?;
1226 let mut attrs = root.attrs();
1227 while let Some(attr) = attrs.next()? {
1228 match attr.name() {
1229 constants::DW_AT_name => {
1230 name = Some(attr.value());
1231 }
1232 constants::DW_AT_comp_dir => {
1233 comp_dir = Some(attr.value());
1234 }
1235 constants::DW_AT_low_pc => {
1236 low_pc_attr = Some(attr.value());
1237 }
1238 constants::DW_AT_stmt_list => {
1239 if let AttributeValue::DebugLineRef(offset) = attr.value() {
1240 line_program_offset = Some(offset);
1241 }
1242 }
1243 constants::DW_AT_str_offsets_base => {
1244 if let AttributeValue::DebugStrOffsetsBase(base) = attr.value() {
1245 unit.str_offsets_base = base;
1246 }
1247 }
1248 constants::DW_AT_addr_base | constants::DW_AT_GNU_addr_base => {
1249 if let AttributeValue::DebugAddrBase(base) = attr.value() {
1250 unit.addr_base = base;
1251 }
1252 }
1253 constants::DW_AT_loclists_base => {
1254 if let AttributeValue::DebugLocListsBase(base) = attr.value() {
1255 unit.loclists_base = base;
1256 }
1257 }
1258 constants::DW_AT_rnglists_base | constants::DW_AT_GNU_ranges_base => {
1259 if let AttributeValue::DebugRngListsBase(base) = attr.value() {
1260 unit.rnglists_base = base;
1261 }
1262 }
1263 constants::DW_AT_GNU_dwo_id => {
1264 if unit.dwo_id.is_none() {
1265 if let AttributeValue::DwoId(dwo_id) = attr.value() {
1266 unit.dwo_id = Some(dwo_id);
1267 }
1268 }
1269 }
1270 _ => {}
1271 }
1272 }
1273 }
1274
1275 unit.name = match name {
1276 Some(val) => dwarf.attr_string(&unit, val).ok(),
1277 None => None,
1278 };
1279 unit.comp_dir = match comp_dir {
1280 Some(val) => dwarf.attr_string(&unit, val).ok(),
1281 None => None,
1282 };
1283 unit.line_program = match line_program_offset {
1284 Some(offset) => Some(dwarf.debug_line.program(
1285 offset,
1286 unit.header.address_size(),
1287 unit.comp_dir.clone(),
1288 unit.name.clone(),
1289 )?),
1290 None => None,
1291 };
1292 if let Some(low_pc_attr) = low_pc_attr {
1293 if let Some(addr) = dwarf.attr_address(&unit, low_pc_attr)? {
1294 unit.low_pc = addr;
1295 }
1296 }
1297 Ok(unit)
1298 }
1299
1300 pub fn unit_ref<'a>(&'a self, dwarf: &'a Dwarf<R>) -> UnitRef<'a, R> {
1302 UnitRef::new(dwarf, self)
1303 }
1304
1305 #[inline]
1307 pub fn encoding(&self) -> Encoding {
1308 self.header.encoding()
1309 }
1310
1311 pub fn entry(
1313 &self,
1314 offset: UnitOffset<R::Offset>,
1315 ) -> Result<DebuggingInformationEntry<'_, '_, R>> {
1316 self.header.entry(&self.abbreviations, offset)
1317 }
1318
1319 #[inline]
1321 pub fn entries(&self) -> EntriesCursor<'_, '_, R> {
1322 self.header.entries(&self.abbreviations)
1323 }
1324
1325 #[inline]
1328 pub fn entries_at_offset(
1329 &self,
1330 offset: UnitOffset<R::Offset>,
1331 ) -> Result<EntriesCursor<'_, '_, R>> {
1332 self.header.entries_at_offset(&self.abbreviations, offset)
1333 }
1334
1335 #[inline]
1338 pub fn entries_tree(
1339 &self,
1340 offset: Option<UnitOffset<R::Offset>>,
1341 ) -> Result<EntriesTree<'_, '_, R>> {
1342 self.header.entries_tree(&self.abbreviations, offset)
1343 }
1344
1345 #[inline]
1347 pub fn entries_raw(
1348 &self,
1349 offset: Option<UnitOffset<R::Offset>>,
1350 ) -> Result<EntriesRaw<'_, '_, R>> {
1351 self.header.entries_raw(&self.abbreviations, offset)
1352 }
1353
1354 pub fn copy_relocated_attributes(&mut self, other: &Unit<R>) {
1358 self.low_pc = other.low_pc;
1359 self.addr_base = other.addr_base;
1360 if self.header.version() < 5 {
1361 self.rnglists_base = other.rnglists_base;
1362 }
1363 }
1364
1365 pub fn dwo_name(&self) -> Result<Option<AttributeValue<R>>> {
1371 let mut entries = self.entries();
1372 entries.next_entry()?;
1373 let entry = entries.current().ok_or(Error::MissingUnitDie)?;
1374 if self.header.version() < 5 {
1375 entry.attr_value(constants::DW_AT_GNU_dwo_name)
1376 } else {
1377 entry.attr_value(constants::DW_AT_dwo_name)
1378 }
1379 }
1380}
1381
1382#[derive(Debug)]
1389pub struct UnitRef<'a, R: Reader> {
1390 pub dwarf: &'a Dwarf<R>,
1392
1393 pub unit: &'a Unit<R>,
1395}
1396
1397impl<'a, R: Reader> Clone for UnitRef<'a, R> {
1398 fn clone(&self) -> Self {
1399 *self
1400 }
1401}
1402
1403impl<'a, R: Reader> Copy for UnitRef<'a, R> {}
1404
1405impl<'a, R: Reader> core::ops::Deref for UnitRef<'a, R> {
1406 type Target = Unit<R>;
1407
1408 fn deref(&self) -> &Self::Target {
1409 self.unit
1410 }
1411}
1412
1413impl<'a, R: Reader> UnitRef<'a, R> {
1414 pub fn new(dwarf: &'a Dwarf<R>, unit: &'a Unit<R>) -> Self {
1416 UnitRef { dwarf, unit }
1417 }
1418
1419 #[inline]
1421 pub fn string_offset(
1422 &self,
1423 index: DebugStrOffsetsIndex<R::Offset>,
1424 ) -> Result<DebugStrOffset<R::Offset>> {
1425 self.dwarf.string_offset(self.unit, index)
1426 }
1427
1428 #[inline]
1430 pub fn string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R> {
1431 self.dwarf.string(offset)
1432 }
1433
1434 #[inline]
1436 pub fn line_string(&self, offset: DebugLineStrOffset<R::Offset>) -> Result<R> {
1437 self.dwarf.line_string(offset)
1438 }
1439
1440 #[inline]
1443 pub fn sup_string(&self, offset: DebugStrOffset<R::Offset>) -> Result<R> {
1444 self.dwarf.sup_string(offset)
1445 }
1446
1447 pub fn attr_string(&self, attr: AttributeValue<R>) -> Result<R> {
1451 self.dwarf.attr_string(self.unit, attr)
1452 }
1453
1454 pub fn address(&self, index: DebugAddrIndex<R::Offset>) -> Result<u64> {
1456 self.dwarf.address(self.unit, index)
1457 }
1458
1459 pub fn attr_address(&self, attr: AttributeValue<R>) -> Result<Option<u64>> {
1463 self.dwarf.attr_address(self.unit, attr)
1464 }
1465
1466 pub fn ranges_offset_from_raw(
1470 &self,
1471 offset: RawRangeListsOffset<R::Offset>,
1472 ) -> RangeListsOffset<R::Offset> {
1473 self.dwarf.ranges_offset_from_raw(self.unit, offset)
1474 }
1475
1476 pub fn ranges_offset(
1478 &self,
1479 index: DebugRngListsIndex<R::Offset>,
1480 ) -> Result<RangeListsOffset<R::Offset>> {
1481 self.dwarf.ranges_offset(self.unit, index)
1482 }
1483
1484 pub fn ranges(&self, offset: RangeListsOffset<R::Offset>) -> Result<RngListIter<R>> {
1486 self.dwarf.ranges(self.unit, offset)
1487 }
1488
1489 pub fn raw_ranges(&self, offset: RangeListsOffset<R::Offset>) -> Result<RawRngListIter<R>> {
1491 self.dwarf.raw_ranges(self.unit, offset)
1492 }
1493
1494 pub fn attr_ranges_offset(
1498 &self,
1499 attr: AttributeValue<R>,
1500 ) -> Result<Option<RangeListsOffset<R::Offset>>> {
1501 self.dwarf.attr_ranges_offset(self.unit, attr)
1502 }
1503
1504 pub fn attr_ranges(&self, attr: AttributeValue<R>) -> Result<Option<RngListIter<R>>> {
1508 self.dwarf.attr_ranges(self.unit, attr)
1509 }
1510
1511 pub fn die_ranges(&self, entry: &DebuggingInformationEntry<'_, '_, R>) -> Result<RangeIter<R>> {
1515 self.dwarf.die_ranges(self.unit, entry)
1516 }
1517
1518 pub fn unit_ranges(&self) -> Result<RangeIter<R>> {
1523 self.dwarf.unit_ranges(self.unit)
1524 }
1525
1526 pub fn locations_offset(
1528 &self,
1529 index: DebugLocListsIndex<R::Offset>,
1530 ) -> Result<LocationListsOffset<R::Offset>> {
1531 self.dwarf.locations_offset(self.unit, index)
1532 }
1533
1534 pub fn locations(&self, offset: LocationListsOffset<R::Offset>) -> Result<LocListIter<R>> {
1536 self.dwarf.locations(self.unit, offset)
1537 }
1538
1539 pub fn raw_locations(
1541 &self,
1542 offset: LocationListsOffset<R::Offset>,
1543 ) -> Result<RawLocListIter<R>> {
1544 self.dwarf.raw_locations(self.unit, offset)
1545 }
1546
1547 pub fn attr_locations_offset(
1551 &self,
1552 attr: AttributeValue<R>,
1553 ) -> Result<Option<LocationListsOffset<R::Offset>>> {
1554 self.dwarf.attr_locations_offset(self.unit, attr)
1555 }
1556
1557 pub fn attr_locations(&self, attr: AttributeValue<R>) -> Result<Option<LocListIter<R>>> {
1561 self.dwarf.attr_locations(self.unit, attr)
1562 }
1563
1564 pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacroIter<R>> {
1566 self.dwarf.macinfo(offset)
1567 }
1568
1569 pub fn macros(&self, offset: DebugMacroOffset<R::Offset>) -> Result<MacroIter<R>> {
1571 self.dwarf.macros(offset)
1572 }
1573}
1574
1575impl<T: ReaderOffset> UnitSectionOffset<T> {
1576 pub fn to_unit_offset<R>(&self, unit: &Unit<R>) -> Option<UnitOffset<T>>
1581 where
1582 R: Reader<Offset = T>,
1583 {
1584 let (offset, unit_offset) = match (self, unit.header.offset()) {
1585 (
1586 UnitSectionOffset::DebugInfoOffset(offset),
1587 UnitSectionOffset::DebugInfoOffset(unit_offset),
1588 ) => (offset.0, unit_offset.0),
1589 (
1590 UnitSectionOffset::DebugTypesOffset(offset),
1591 UnitSectionOffset::DebugTypesOffset(unit_offset),
1592 ) => (offset.0, unit_offset.0),
1593 _ => return None,
1594 };
1595 let offset = match offset.checked_sub(unit_offset) {
1596 Some(offset) => UnitOffset(offset),
1597 None => return None,
1598 };
1599 if !unit.header.is_valid_offset(offset) {
1600 return None;
1601 }
1602 Some(offset)
1603 }
1604}
1605
1606impl<T: ReaderOffset> UnitOffset<T> {
1607 pub fn to_unit_section_offset<R>(&self, unit: &Unit<R>) -> UnitSectionOffset<T>
1612 where
1613 R: Reader<Offset = T>,
1614 {
1615 match unit.header.offset() {
1616 UnitSectionOffset::DebugInfoOffset(unit_offset) => {
1617 DebugInfoOffset(unit_offset.0 + self.0).into()
1618 }
1619 UnitSectionOffset::DebugTypesOffset(unit_offset) => {
1620 DebugTypesOffset(unit_offset.0 + self.0).into()
1621 }
1622 }
1623 }
1624}
1625
1626#[derive(Debug)]
1630pub struct RangeIter<R: Reader>(RangeIterInner<R>);
1631
1632#[derive(Debug)]
1633enum RangeIterInner<R: Reader> {
1634 Single(Option<Range>),
1635 List(RngListIter<R>),
1636}
1637
1638impl<R: Reader> Default for RangeIter<R> {
1639 fn default() -> Self {
1640 RangeIter(RangeIterInner::Single(None))
1641 }
1642}
1643
1644impl<R: Reader> RangeIter<R> {
1645 pub fn next(&mut self) -> Result<Option<Range>> {
1647 match self.0 {
1648 RangeIterInner::Single(ref mut range) => Ok(range.take()),
1649 RangeIterInner::List(ref mut list) => list.next(),
1650 }
1651 }
1652}
1653
1654#[cfg(feature = "fallible-iterator")]
1655impl<R: Reader> fallible_iterator::FallibleIterator for RangeIter<R> {
1656 type Item = Range;
1657 type Error = Error;
1658
1659 #[inline]
1660 fn next(&mut self) -> ::core::result::Result<Option<Self::Item>, Self::Error> {
1661 RangeIter::next(self)
1662 }
1663}
1664
1665#[cfg(test)]
1666mod tests {
1667 use super::*;
1668 use crate::read::EndianSlice;
1669 use crate::{Endianity, LittleEndian};
1670
1671 #[test]
1673 fn test_dwarf_variance() {
1674 fn _f<'a: 'b, 'b, E: Endianity>(x: Dwarf<EndianSlice<'a, E>>) -> Dwarf<EndianSlice<'b, E>> {
1676 x
1677 }
1678 }
1679
1680 #[test]
1682 fn test_dwarf_unit_variance() {
1683 fn _f<'a: 'b, 'b, E: Endianity>(x: Unit<EndianSlice<'a, E>>) -> Unit<EndianSlice<'b, E>> {
1685 x
1686 }
1687 }
1688
1689 #[test]
1690 fn test_send() {
1691 fn assert_is_send<T: Send>() {}
1692 assert_is_send::<Dwarf<EndianSlice<'_, LittleEndian>>>();
1693 assert_is_send::<Unit<EndianSlice<'_, LittleEndian>>>();
1694 }
1695
1696 #[test]
1697 fn test_format_error() {
1698 let dwarf_sections = DwarfSections::load(|_| -> Result<_> { Ok(vec![1, 2]) }).unwrap();
1699 let sup_sections = DwarfSections::load(|_| -> Result<_> { Ok(vec![1, 2]) }).unwrap();
1700 let dwarf = dwarf_sections.borrow_with_sup(&sup_sections, |section| {
1701 EndianSlice::new(section, LittleEndian)
1702 });
1703
1704 match dwarf.debug_str.get_str(DebugStrOffset(1)) {
1705 Ok(r) => panic!("Unexpected str {:?}", r),
1706 Err(e) => {
1707 assert_eq!(
1708 dwarf.format_error(e),
1709 "Hit the end of input before it was expected at .debug_str+0x1"
1710 );
1711 }
1712 }
1713 match dwarf.sup().unwrap().debug_str.get_str(DebugStrOffset(1)) {
1714 Ok(r) => panic!("Unexpected str {:?}", r),
1715 Err(e) => {
1716 assert_eq!(
1717 dwarf.format_error(e),
1718 "Hit the end of input before it was expected at .debug_str(sup)+0x1"
1719 );
1720 }
1721 }
1722 assert_eq!(dwarf.format_error(Error::Io), Error::Io.description());
1723 }
1724}