1use crate::common::{
2 DebugAddrBase, DebugAddrIndex, DebugLocListsBase, DebugLocListsIndex, DwarfFileType, Encoding,
3 LocationListsOffset, SectionId,
4};
5use crate::constants;
6use crate::endianity::Endianity;
7use crate::read::{
8 lists::ListsHeader, DebugAddr, EndianSlice, Error, Expression, Range, RawRange, Reader,
9 ReaderAddress, ReaderOffset, ReaderOffsetId, Result, Section,
10};
11
12#[derive(Debug, Default, Clone, Copy)]
14pub struct DebugLoc<R> {
15 pub(crate) section: R,
16}
17
18impl<'input, Endian> DebugLoc<EndianSlice<'input, Endian>>
19where
20 Endian: Endianity,
21{
22 pub fn new(section: &'input [u8], endian: Endian) -> Self {
37 Self::from(EndianSlice::new(section, endian))
38 }
39}
40
41impl<T> DebugLoc<T> {
42 pub(crate) fn borrow<'a, F, R>(&'a self, mut borrow: F) -> DebugLoc<R>
48 where
49 F: FnMut(&'a T) -> R,
50 {
51 borrow(&self.section).into()
52 }
53}
54
55impl<R> Section<R> for DebugLoc<R> {
56 fn id() -> SectionId {
57 SectionId::DebugLoc
58 }
59
60 fn reader(&self) -> &R {
61 &self.section
62 }
63}
64
65impl<R> From<R> for DebugLoc<R> {
66 fn from(section: R) -> Self {
67 DebugLoc { section }
68 }
69}
70
71#[derive(Debug, Default, Clone, Copy)]
74pub struct DebugLocLists<R> {
75 section: R,
76}
77
78impl<'input, Endian> DebugLocLists<EndianSlice<'input, Endian>>
79where
80 Endian: Endianity,
81{
82 pub fn new(section: &'input [u8], endian: Endian) -> Self {
97 Self::from(EndianSlice::new(section, endian))
98 }
99}
100
101impl<T> DebugLocLists<T> {
102 pub(crate) fn borrow<'a, F, R>(&'a self, mut borrow: F) -> DebugLocLists<R>
108 where
109 F: FnMut(&'a T) -> R,
110 {
111 borrow(&self.section).into()
112 }
113}
114
115impl<R> Section<R> for DebugLocLists<R> {
116 fn id() -> SectionId {
117 SectionId::DebugLocLists
118 }
119
120 fn reader(&self) -> &R {
121 &self.section
122 }
123}
124
125impl<R> From<R> for DebugLocLists<R> {
126 fn from(section: R) -> Self {
127 DebugLocLists { section }
128 }
129}
130
131pub(crate) type LocListsHeader = ListsHeader;
132
133impl<Offset> DebugLocListsBase<Offset>
134where
135 Offset: ReaderOffset,
136{
137 pub fn default_for_encoding_and_file(
140 encoding: Encoding,
141 file_type: DwarfFileType,
142 ) -> DebugLocListsBase<Offset> {
143 if encoding.version >= 5 && file_type == DwarfFileType::Dwo {
144 DebugLocListsBase(Offset::from_u8(LocListsHeader::size_for_encoding(encoding)))
148 } else {
149 DebugLocListsBase(Offset::from_u8(0))
150 }
151 }
152}
153
154#[derive(Debug, Default, Clone, Copy)]
156pub struct LocationLists<R> {
157 debug_loc: DebugLoc<R>,
158 debug_loclists: DebugLocLists<R>,
159}
160
161impl<R> LocationLists<R> {
162 pub fn new(debug_loc: DebugLoc<R>, debug_loclists: DebugLocLists<R>) -> LocationLists<R> {
165 LocationLists {
166 debug_loc,
167 debug_loclists,
168 }
169 }
170}
171
172impl<T> LocationLists<T> {
173 pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> LocationLists<R>
179 where
180 F: FnMut(&'a T) -> R,
181 {
182 LocationLists {
183 debug_loc: borrow(&self.debug_loc.section).into(),
184 debug_loclists: borrow(&self.debug_loclists.section).into(),
185 }
186 }
187}
188
189impl<R: Reader> LocationLists<R> {
190 pub fn locations(
202 &self,
203 offset: LocationListsOffset<R::Offset>,
204 unit_encoding: Encoding,
205 base_address: u64,
206 debug_addr: &DebugAddr<R>,
207 debug_addr_base: DebugAddrBase<R::Offset>,
208 ) -> Result<LocListIter<R>> {
209 Ok(LocListIter::new(
210 self.raw_locations(offset, unit_encoding)?,
211 base_address,
212 debug_addr.clone(),
213 debug_addr_base,
214 ))
215 }
216
217 pub fn locations_dwo(
221 &self,
222 offset: LocationListsOffset<R::Offset>,
223 unit_encoding: Encoding,
224 base_address: u64,
225 debug_addr: &DebugAddr<R>,
226 debug_addr_base: DebugAddrBase<R::Offset>,
227 ) -> Result<LocListIter<R>> {
228 Ok(LocListIter::new(
229 self.raw_locations_dwo(offset, unit_encoding)?,
230 base_address,
231 debug_addr.clone(),
232 debug_addr_base,
233 ))
234 }
235
236 pub fn raw_locations(
247 &self,
248 offset: LocationListsOffset<R::Offset>,
249 unit_encoding: Encoding,
250 ) -> Result<RawLocListIter<R>> {
251 let (mut input, format) = if unit_encoding.version <= 4 {
252 (self.debug_loc.section.clone(), LocListsFormat::Bare)
253 } else {
254 (self.debug_loclists.section.clone(), LocListsFormat::Lle)
255 };
256 input.skip(offset.0)?;
257 Ok(RawLocListIter::new(input, unit_encoding, format))
258 }
259
260 pub fn raw_locations_dwo(
264 &self,
265 offset: LocationListsOffset<R::Offset>,
266 unit_encoding: Encoding,
267 ) -> Result<RawLocListIter<R>> {
268 let mut input = if unit_encoding.version <= 4 {
269 self.debug_loc.section.clone()
273 } else {
274 self.debug_loclists.section.clone()
275 };
276 input.skip(offset.0)?;
277 Ok(RawLocListIter::new(
278 input,
279 unit_encoding,
280 LocListsFormat::Lle,
281 ))
282 }
283
284 pub fn get_offset(
291 &self,
292 unit_encoding: Encoding,
293 base: DebugLocListsBase<R::Offset>,
294 index: DebugLocListsIndex<R::Offset>,
295 ) -> Result<LocationListsOffset<R::Offset>> {
296 let format = unit_encoding.format;
297 let input = &mut self.debug_loclists.section.clone();
298 input.skip(base.0)?;
299 input.skip(R::Offset::from_u64(
300 index.0.into_u64() * u64::from(format.word_size()),
301 )?)?;
302 input
303 .read_offset(format)
304 .map(|x| LocationListsOffset(base.0 + x))
305 }
306
307 pub fn lookup_offset_id(&self, id: ReaderOffsetId) -> Option<(SectionId, R::Offset)> {
309 self.debug_loc
310 .lookup_offset_id(id)
311 .or_else(|| self.debug_loclists.lookup_offset_id(id))
312 }
313}
314
315#[derive(Debug, Clone, Copy, PartialEq, Eq)]
316enum LocListsFormat {
317 Bare,
319 Lle,
322}
323
324#[derive(Debug)]
329pub struct RawLocListIter<R: Reader> {
330 input: R,
331 encoding: Encoding,
332 format: LocListsFormat,
333}
334
335#[derive(Clone, Debug)]
337pub enum RawLocListEntry<R: Reader> {
338 AddressOrOffsetPair {
340 begin: u64,
342 end: u64,
344 data: Expression<R>,
346 },
347 BaseAddress {
349 addr: u64,
351 },
352 BaseAddressx {
354 addr: DebugAddrIndex<R::Offset>,
356 },
357 StartxEndx {
359 begin: DebugAddrIndex<R::Offset>,
361 end: DebugAddrIndex<R::Offset>,
363 data: Expression<R>,
365 },
366 StartxLength {
368 begin: DebugAddrIndex<R::Offset>,
370 length: u64,
372 data: Expression<R>,
374 },
375 OffsetPair {
377 begin: u64,
379 end: u64,
381 data: Expression<R>,
383 },
384 DefaultLocation {
386 data: Expression<R>,
388 },
389 StartEnd {
391 begin: u64,
393 end: u64,
395 data: Expression<R>,
397 },
398 StartLength {
400 begin: u64,
402 length: u64,
404 data: Expression<R>,
406 },
407}
408
409fn parse_data<R: Reader>(input: &mut R, encoding: Encoding) -> Result<Expression<R>> {
410 if encoding.version >= 5 {
411 let len = R::Offset::from_u64(input.read_uleb128()?)?;
412 Ok(Expression(input.split(len)?))
413 } else {
414 let len = R::Offset::from_u16(input.read_u16()?);
416 Ok(Expression(input.split(len)?))
417 }
418}
419
420impl<R: Reader> RawLocListEntry<R> {
421 fn parse(input: &mut R, encoding: Encoding, format: LocListsFormat) -> Result<Option<Self>> {
423 Ok(match format {
424 LocListsFormat::Bare => {
425 let range = RawRange::parse(input, encoding.address_size)?;
426 if range.is_end() {
427 None
428 } else if range.is_base_address(encoding.address_size) {
429 Some(RawLocListEntry::BaseAddress { addr: range.end })
430 } else {
431 let len = R::Offset::from_u16(input.read_u16()?);
432 let data = Expression(input.split(len)?);
433 Some(RawLocListEntry::AddressOrOffsetPair {
434 begin: range.begin,
435 end: range.end,
436 data,
437 })
438 }
439 }
440 LocListsFormat::Lle => match constants::DwLle(input.read_u8()?) {
441 constants::DW_LLE_end_of_list => None,
442 constants::DW_LLE_base_addressx => Some(RawLocListEntry::BaseAddressx {
443 addr: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?),
444 }),
445 constants::DW_LLE_startx_endx => Some(RawLocListEntry::StartxEndx {
446 begin: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?),
447 end: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?),
448 data: parse_data(input, encoding)?,
449 }),
450 constants::DW_LLE_startx_length => Some(RawLocListEntry::StartxLength {
451 begin: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?),
452 length: if encoding.version >= 5 {
453 input.read_uleb128()?
454 } else {
455 input.read_u32()? as u64
457 },
458 data: parse_data(input, encoding)?,
459 }),
460 constants::DW_LLE_offset_pair => Some(RawLocListEntry::OffsetPair {
461 begin: input.read_uleb128()?,
462 end: input.read_uleb128()?,
463 data: parse_data(input, encoding)?,
464 }),
465 constants::DW_LLE_default_location => Some(RawLocListEntry::DefaultLocation {
466 data: parse_data(input, encoding)?,
467 }),
468 constants::DW_LLE_base_address => Some(RawLocListEntry::BaseAddress {
469 addr: input.read_address(encoding.address_size)?,
470 }),
471 constants::DW_LLE_start_end => Some(RawLocListEntry::StartEnd {
472 begin: input.read_address(encoding.address_size)?,
473 end: input.read_address(encoding.address_size)?,
474 data: parse_data(input, encoding)?,
475 }),
476 constants::DW_LLE_start_length => Some(RawLocListEntry::StartLength {
477 begin: input.read_address(encoding.address_size)?,
478 length: input.read_uleb128()?,
479 data: parse_data(input, encoding)?,
480 }),
481 entry => {
482 return Err(Error::UnknownLocListsEntry(entry));
483 }
484 },
485 })
486 }
487}
488
489impl<R: Reader> RawLocListIter<R> {
490 fn new(input: R, encoding: Encoding, format: LocListsFormat) -> RawLocListIter<R> {
492 RawLocListIter {
493 input,
494 encoding,
495 format,
496 }
497 }
498
499 pub fn next(&mut self) -> Result<Option<RawLocListEntry<R>>> {
501 if self.input.is_empty() {
502 return Ok(None);
503 }
504
505 match RawLocListEntry::parse(&mut self.input, self.encoding, self.format) {
506 Ok(entry) => {
507 if entry.is_none() {
508 self.input.empty();
509 }
510 Ok(entry)
511 }
512 Err(e) => {
513 self.input.empty();
514 Err(e)
515 }
516 }
517 }
518}
519
520#[cfg(feature = "fallible-iterator")]
521impl<R: Reader> fallible_iterator::FallibleIterator for RawLocListIter<R> {
522 type Item = RawLocListEntry<R>;
523 type Error = Error;
524
525 fn next(&mut self) -> ::core::result::Result<Option<Self::Item>, Self::Error> {
526 RawLocListIter::next(self)
527 }
528}
529
530#[derive(Debug)]
536pub struct LocListIter<R: Reader> {
537 raw: RawLocListIter<R>,
538 base_address: u64,
539 debug_addr: DebugAddr<R>,
540 debug_addr_base: DebugAddrBase<R::Offset>,
541}
542
543impl<R: Reader> LocListIter<R> {
544 fn new(
546 raw: RawLocListIter<R>,
547 base_address: u64,
548 debug_addr: DebugAddr<R>,
549 debug_addr_base: DebugAddrBase<R::Offset>,
550 ) -> LocListIter<R> {
551 LocListIter {
552 raw,
553 base_address,
554 debug_addr,
555 debug_addr_base,
556 }
557 }
558
559 #[inline]
560 fn get_address(&self, index: DebugAddrIndex<R::Offset>) -> Result<u64> {
561 self.debug_addr
562 .get_address(self.raw.encoding.address_size, self.debug_addr_base, index)
563 }
564
565 pub fn next(&mut self) -> Result<Option<LocationListEntry<R>>> {
567 loop {
568 let raw_loc = match self.raw.next()? {
569 Some(loc) => loc,
570 None => return Ok(None),
571 };
572
573 let loc = self.convert_raw(raw_loc)?;
574 if loc.is_some() {
575 return Ok(loc);
576 }
577 }
578 }
579
580 #[doc(hidden)]
584 pub fn next_raw(&mut self) -> Result<Option<RawLocListEntry<R>>> {
585 self.raw.next()
586 }
587
588 #[doc(hidden)]
592 pub fn convert_raw(
593 &mut self,
594 raw_loc: RawLocListEntry<R>,
595 ) -> Result<Option<LocationListEntry<R>>> {
596 let address_size = self.raw.encoding.address_size;
597 let mask = u64::ones_sized(address_size);
598 let tombstone = if self.raw.encoding.version <= 4 {
599 mask - 1
600 } else {
601 mask
602 };
603
604 let (range, data) = match raw_loc {
605 RawLocListEntry::BaseAddress { addr } => {
606 self.base_address = addr;
607 return Ok(None);
608 }
609 RawLocListEntry::BaseAddressx { addr } => {
610 self.base_address = self.get_address(addr)?;
611 return Ok(None);
612 }
613 RawLocListEntry::StartxEndx { begin, end, data } => {
614 let begin = self.get_address(begin)?;
615 let end = self.get_address(end)?;
616 (Range { begin, end }, data)
617 }
618 RawLocListEntry::StartxLength {
619 begin,
620 length,
621 data,
622 } => {
623 let begin = self.get_address(begin)?;
624 let end = begin.wrapping_add_sized(length, address_size);
625 (Range { begin, end }, data)
626 }
627 RawLocListEntry::DefaultLocation { data } => (
628 Range {
629 begin: 0,
630 end: u64::MAX,
631 },
632 data,
633 ),
634 RawLocListEntry::AddressOrOffsetPair { begin, end, data }
635 | RawLocListEntry::OffsetPair { begin, end, data } => {
636 if self.base_address == tombstone {
637 return Ok(None);
638 }
639 let mut range = Range { begin, end };
640 range.add_base_address(self.base_address, self.raw.encoding.address_size);
641 (range, data)
642 }
643 RawLocListEntry::StartEnd { begin, end, data } => (Range { begin, end }, data),
644 RawLocListEntry::StartLength {
645 begin,
646 length,
647 data,
648 } => {
649 let end = begin.wrapping_add_sized(length, address_size);
650 (Range { begin, end }, data)
651 }
652 };
653
654 if range.begin == tombstone || range.begin > range.end {
655 return Ok(None);
656 }
657
658 Ok(Some(LocationListEntry { range, data }))
659 }
660}
661
662#[cfg(feature = "fallible-iterator")]
663impl<R: Reader> fallible_iterator::FallibleIterator for LocListIter<R> {
664 type Item = LocationListEntry<R>;
665 type Error = Error;
666
667 fn next(&mut self) -> ::core::result::Result<Option<Self::Item>, Self::Error> {
668 LocListIter::next(self)
669 }
670}
671
672#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
674pub struct LocationListEntry<R: Reader> {
675 pub range: Range,
677
678 pub data: Expression<R>,
680}
681
682#[cfg(test)]
683mod tests {
684 use super::*;
685 use crate::common::Format;
686 use crate::endianity::LittleEndian;
687 use crate::read::{EndianSlice, Range};
688 use crate::test_util::GimliSectionMethods;
689 use test_assembler::{Endian, Label, LabelMaker, Section};
690
691 #[test]
692 fn test_loclists_32() {
693 let tombstone = !0u32;
694 let encoding = Encoding {
695 format: Format::Dwarf32,
696 version: 5,
697 address_size: 4,
698 };
699
700 let section = Section::with_endian(Endian::Little)
701 .L32(0x0300_0000)
702 .L32(0x0301_0300)
703 .L32(0x0301_0400)
704 .L32(0x0301_0500)
705 .L32(tombstone)
706 .L32(0x0301_0600);
707 let buf = section.get_contents().unwrap();
708 let debug_addr = &DebugAddr::from(EndianSlice::new(&buf, LittleEndian));
709 let debug_addr_base = DebugAddrBase(0);
710
711 let start = Label::new();
712 let first = Label::new();
713 let size = Label::new();
714 #[rustfmt::skip]
715 let section = Section::with_endian(Endian::Little)
716 .mark(&start)
718 .L32(&size)
719 .L16(encoding.version)
720 .L8(encoding.address_size)
721 .L8(0)
722 .L32(0)
723 .mark(&first)
724 .L8(4).uleb(0x10200).uleb(0x10300).uleb(4).L32(2)
726 .L8(6).L32(0x0200_0000)
728 .L8(4).uleb(0x10400).uleb(0x10500).uleb(4).L32(3)
729 .L8(4).uleb(0x10600).uleb(0x10600).uleb(4).L32(4)
731 .L8(4).uleb(0x10800).uleb(0x10900).uleb(4).L32(5)
732 .L8(7).L32(0x201_0a00).L32(0x201_0b00).uleb(4).L32(6)
734 .L8(8).L32(0x201_0c00).uleb(0x100).uleb(4).L32(7)
736 .L8(4).uleb(0).uleb(1).uleb(4).L32(8)
738 .L8(6).L32(0)
740 .L8(4).uleb(0).uleb(0xffff_ffff).uleb(4).L32(9)
741 .L8(5).uleb(4).L32(10)
743 .L8(1).uleb(0)
745 .L8(4).uleb(0x10100).uleb(0x10200).uleb(4).L32(11)
746 .L8(2).uleb(1).uleb(2).uleb(4).L32(12)
748 .L8(3).uleb(3).uleb(0x100).uleb(4).L32(13)
750
751 .L8(1).uleb(4)
754 .L8(4).uleb(0x11100).uleb(0x11200).uleb(4).L32(20)
755 .L8(6).L32(tombstone)
757 .L8(4).uleb(0x11300).uleb(0x11400).uleb(4).L32(21)
758 .L8(2).uleb(4).uleb(5).uleb(4).L32(22)
760 .L8(3).uleb(4).uleb(0x100).uleb(4).L32(23)
762 .L8(7).L32(tombstone).L32(0x201_1500).uleb(4).L32(24)
764 .L8(8).L32(tombstone).uleb(0x100).uleb(4).L32(25)
766 .L8(7).L32(0x201_1600).L32(0x201_1700).uleb(4).L32(26)
768
769 .L8(0)
771 .L32(0xffff_ffff);
773 size.set_const((§ion.here() - &start - 4) as u64);
774
775 let buf = section.get_contents().unwrap();
776 let debug_loc = DebugLoc::new(&[], LittleEndian);
777 let debug_loclists = DebugLocLists::new(&buf, LittleEndian);
778 let loclists = LocationLists::new(debug_loc, debug_loclists);
779 let offset = LocationListsOffset((&first - &start) as usize);
780 let mut locations = loclists
781 .locations(offset, encoding, 0x0100_0000, debug_addr, debug_addr_base)
782 .unwrap();
783
784 assert_eq!(
786 locations.next(),
787 Ok(Some(LocationListEntry {
788 range: Range {
789 begin: 0x0101_0200,
790 end: 0x0101_0300,
791 },
792 data: Expression(EndianSlice::new(&[2, 0, 0, 0], LittleEndian)),
793 }))
794 );
795
796 assert_eq!(
798 locations.next(),
799 Ok(Some(LocationListEntry {
800 range: Range {
801 begin: 0x0201_0400,
802 end: 0x0201_0500,
803 },
804 data: Expression(EndianSlice::new(&[3, 0, 0, 0], LittleEndian)),
805 }))
806 );
807
808 assert_eq!(
810 locations.next(),
811 Ok(Some(LocationListEntry {
812 range: Range {
813 begin: 0x0201_0600,
814 end: 0x0201_0600,
815 },
816 data: Expression(EndianSlice::new(&[4, 0, 0, 0], LittleEndian)),
817 }))
818 );
819 assert_eq!(
820 locations.next(),
821 Ok(Some(LocationListEntry {
822 range: Range {
823 begin: 0x0201_0800,
824 end: 0x0201_0900,
825 },
826 data: Expression(EndianSlice::new(&[5, 0, 0, 0], LittleEndian)),
827 }))
828 );
829
830 assert_eq!(
832 locations.next(),
833 Ok(Some(LocationListEntry {
834 range: Range {
835 begin: 0x0201_0a00,
836 end: 0x0201_0b00,
837 },
838 data: Expression(EndianSlice::new(&[6, 0, 0, 0], LittleEndian)),
839 }))
840 );
841
842 assert_eq!(
844 locations.next(),
845 Ok(Some(LocationListEntry {
846 range: Range {
847 begin: 0x0201_0c00,
848 end: 0x0201_0d00,
849 },
850 data: Expression(EndianSlice::new(&[7, 0, 0, 0], LittleEndian)),
851 }))
852 );
853
854 assert_eq!(
856 locations.next(),
857 Ok(Some(LocationListEntry {
858 range: Range {
859 begin: 0x0200_0000,
860 end: 0x0200_0001,
861 },
862 data: Expression(EndianSlice::new(&[8, 0, 0, 0], LittleEndian)),
863 }))
864 );
865
866 assert_eq!(
868 locations.next(),
869 Ok(Some(LocationListEntry {
870 range: Range {
871 begin: 0x0000_0000,
872 end: 0xffff_ffff,
873 },
874 data: Expression(EndianSlice::new(&[9, 0, 0, 0], LittleEndian)),
875 }))
876 );
877
878 assert_eq!(
880 locations.next(),
881 Ok(Some(LocationListEntry {
882 range: Range {
883 begin: 0,
884 end: u64::MAX,
885 },
886 data: Expression(EndianSlice::new(&[10, 0, 0, 0], LittleEndian)),
887 }))
888 );
889
890 assert_eq!(
892 locations.next(),
893 Ok(Some(LocationListEntry {
894 range: Range {
895 begin: 0x0301_0100,
896 end: 0x0301_0200,
897 },
898 data: Expression(EndianSlice::new(&[11, 0, 0, 0], LittleEndian)),
899 }))
900 );
901
902 assert_eq!(
904 locations.next(),
905 Ok(Some(LocationListEntry {
906 range: Range {
907 begin: 0x0301_0300,
908 end: 0x0301_0400,
909 },
910 data: Expression(EndianSlice::new(&[12, 0, 0, 0], LittleEndian)),
911 }))
912 );
913
914 assert_eq!(
916 locations.next(),
917 Ok(Some(LocationListEntry {
918 range: Range {
919 begin: 0x0301_0500,
920 end: 0x0301_0600,
921 },
922 data: Expression(EndianSlice::new(&[13, 0, 0, 0], LittleEndian)),
923 }))
924 );
925
926 assert_eq!(
928 locations.next(),
929 Ok(Some(LocationListEntry {
930 range: Range {
931 begin: 0x0201_1600,
932 end: 0x0201_1700,
933 },
934 data: Expression(EndianSlice::new(&[26, 0, 0, 0], LittleEndian)),
935 }))
936 );
937
938 assert_eq!(locations.next(), Ok(None));
940
941 let mut locations = loclists
943 .locations(
944 LocationListsOffset(buf.len()),
945 encoding,
946 0x0100_0000,
947 debug_addr,
948 debug_addr_base,
949 )
950 .unwrap();
951 assert_eq!(locations.next(), Ok(None));
952 }
953
954 #[test]
955 fn test_loclists_64() {
956 let tombstone = !0u64;
957 let encoding = Encoding {
958 format: Format::Dwarf64,
959 version: 5,
960 address_size: 8,
961 };
962
963 let section = Section::with_endian(Endian::Little)
964 .L64(0x0300_0000)
965 .L64(0x0301_0300)
966 .L64(0x0301_0400)
967 .L64(0x0301_0500)
968 .L64(tombstone)
969 .L64(0x0301_0600);
970 let buf = section.get_contents().unwrap();
971 let debug_addr = &DebugAddr::from(EndianSlice::new(&buf, LittleEndian));
972 let debug_addr_base = DebugAddrBase(0);
973
974 let start = Label::new();
975 let first = Label::new();
976 let size = Label::new();
977 #[rustfmt::skip]
978 let section = Section::with_endian(Endian::Little)
979 .mark(&start)
981 .L32(0xffff_ffff)
982 .L64(&size)
983 .L16(encoding.version)
984 .L8(encoding.address_size)
985 .L8(0)
986 .L32(0)
987 .mark(&first)
988 .L8(4).uleb(0x10200).uleb(0x10300).uleb(4).L32(2)
990 .L8(6).L64(0x0200_0000)
992 .L8(4).uleb(0x10400).uleb(0x10500).uleb(4).L32(3)
993 .L8(4).uleb(0x10600).uleb(0x10600).uleb(4).L32(4)
995 .L8(4).uleb(0x10800).uleb(0x10900).uleb(4).L32(5)
996 .L8(7).L64(0x201_0a00).L64(0x201_0b00).uleb(4).L32(6)
998 .L8(8).L64(0x201_0c00).uleb(0x100).uleb(4).L32(7)
1000 .L8(4).uleb(0).uleb(1).uleb(4).L32(8)
1002 .L8(6).L64(0)
1004 .L8(4).uleb(0).uleb(0xffff_ffff).uleb(4).L32(9)
1005 .L8(5).uleb(4).L32(10)
1007 .L8(1).uleb(0)
1009 .L8(4).uleb(0x10100).uleb(0x10200).uleb(4).L32(11)
1010 .L8(2).uleb(1).uleb(2).uleb(4).L32(12)
1012 .L8(3).uleb(3).uleb(0x100).uleb(4).L32(13)
1014
1015 .L8(1).uleb(4)
1018 .L8(4).uleb(0x11100).uleb(0x11200).uleb(4).L32(20)
1019 .L8(6).L64(tombstone)
1021 .L8(4).uleb(0x11300).uleb(0x11400).uleb(4).L32(21)
1022 .L8(2).uleb(4).uleb(5).uleb(4).L32(22)
1024 .L8(3).uleb(4).uleb(0x100).uleb(4).L32(23)
1026 .L8(7).L64(tombstone).L64(0x201_1500).uleb(4).L32(24)
1028 .L8(8).L64(tombstone).uleb(0x100).uleb(4).L32(25)
1030 .L8(7).L64(0x201_1600).L64(0x201_1700).uleb(4).L32(26)
1032
1033 .L8(0)
1035 .L32(0xffff_ffff);
1037 size.set_const((§ion.here() - &start - 12) as u64);
1038
1039 let buf = section.get_contents().unwrap();
1040 let debug_loc = DebugLoc::new(&[], LittleEndian);
1041 let debug_loclists = DebugLocLists::new(&buf, LittleEndian);
1042 let loclists = LocationLists::new(debug_loc, debug_loclists);
1043 let offset = LocationListsOffset((&first - &start) as usize);
1044 let mut locations = loclists
1045 .locations(offset, encoding, 0x0100_0000, debug_addr, debug_addr_base)
1046 .unwrap();
1047
1048 assert_eq!(
1050 locations.next(),
1051 Ok(Some(LocationListEntry {
1052 range: Range {
1053 begin: 0x0101_0200,
1054 end: 0x0101_0300,
1055 },
1056 data: Expression(EndianSlice::new(&[2, 0, 0, 0], LittleEndian)),
1057 }))
1058 );
1059
1060 assert_eq!(
1062 locations.next(),
1063 Ok(Some(LocationListEntry {
1064 range: Range {
1065 begin: 0x0201_0400,
1066 end: 0x0201_0500,
1067 },
1068 data: Expression(EndianSlice::new(&[3, 0, 0, 0], LittleEndian)),
1069 }))
1070 );
1071
1072 assert_eq!(
1074 locations.next(),
1075 Ok(Some(LocationListEntry {
1076 range: Range {
1077 begin: 0x0201_0600,
1078 end: 0x0201_0600,
1079 },
1080 data: Expression(EndianSlice::new(&[4, 0, 0, 0], LittleEndian)),
1081 }))
1082 );
1083 assert_eq!(
1084 locations.next(),
1085 Ok(Some(LocationListEntry {
1086 range: Range {
1087 begin: 0x0201_0800,
1088 end: 0x0201_0900,
1089 },
1090 data: Expression(EndianSlice::new(&[5, 0, 0, 0], LittleEndian)),
1091 }))
1092 );
1093
1094 assert_eq!(
1096 locations.next(),
1097 Ok(Some(LocationListEntry {
1098 range: Range {
1099 begin: 0x0201_0a00,
1100 end: 0x0201_0b00,
1101 },
1102 data: Expression(EndianSlice::new(&[6, 0, 0, 0], LittleEndian)),
1103 }))
1104 );
1105
1106 assert_eq!(
1108 locations.next(),
1109 Ok(Some(LocationListEntry {
1110 range: Range {
1111 begin: 0x0201_0c00,
1112 end: 0x0201_0d00,
1113 },
1114 data: Expression(EndianSlice::new(&[7, 0, 0, 0], LittleEndian)),
1115 }))
1116 );
1117
1118 assert_eq!(
1120 locations.next(),
1121 Ok(Some(LocationListEntry {
1122 range: Range {
1123 begin: 0x0200_0000,
1124 end: 0x0200_0001,
1125 },
1126 data: Expression(EndianSlice::new(&[8, 0, 0, 0], LittleEndian)),
1127 }))
1128 );
1129
1130 assert_eq!(
1132 locations.next(),
1133 Ok(Some(LocationListEntry {
1134 range: Range {
1135 begin: 0x0000_0000,
1136 end: 0xffff_ffff,
1137 },
1138 data: Expression(EndianSlice::new(&[9, 0, 0, 0], LittleEndian)),
1139 }))
1140 );
1141
1142 assert_eq!(
1144 locations.next(),
1145 Ok(Some(LocationListEntry {
1146 range: Range {
1147 begin: 0,
1148 end: u64::MAX,
1149 },
1150 data: Expression(EndianSlice::new(&[10, 0, 0, 0], LittleEndian)),
1151 }))
1152 );
1153
1154 assert_eq!(
1156 locations.next(),
1157 Ok(Some(LocationListEntry {
1158 range: Range {
1159 begin: 0x0301_0100,
1160 end: 0x0301_0200,
1161 },
1162 data: Expression(EndianSlice::new(&[11, 0, 0, 0], LittleEndian)),
1163 }))
1164 );
1165
1166 assert_eq!(
1168 locations.next(),
1169 Ok(Some(LocationListEntry {
1170 range: Range {
1171 begin: 0x0301_0300,
1172 end: 0x0301_0400,
1173 },
1174 data: Expression(EndianSlice::new(&[12, 0, 0, 0], LittleEndian)),
1175 }))
1176 );
1177
1178 assert_eq!(
1180 locations.next(),
1181 Ok(Some(LocationListEntry {
1182 range: Range {
1183 begin: 0x0301_0500,
1184 end: 0x0301_0600,
1185 },
1186 data: Expression(EndianSlice::new(&[13, 0, 0, 0], LittleEndian)),
1187 }))
1188 );
1189
1190 assert_eq!(
1192 locations.next(),
1193 Ok(Some(LocationListEntry {
1194 range: Range {
1195 begin: 0x0201_1600,
1196 end: 0x0201_1700,
1197 },
1198 data: Expression(EndianSlice::new(&[26, 0, 0, 0], LittleEndian)),
1199 }))
1200 );
1201
1202 assert_eq!(locations.next(), Ok(None));
1204
1205 let mut locations = loclists
1207 .locations(
1208 LocationListsOffset(buf.len()),
1209 encoding,
1210 0x0100_0000,
1211 debug_addr,
1212 debug_addr_base,
1213 )
1214 .unwrap();
1215 assert_eq!(locations.next(), Ok(None));
1216 }
1217
1218 #[test]
1219 fn test_location_list_32() {
1220 let tombstone = !0u32 - 1;
1221 let start = Label::new();
1222 let first = Label::new();
1223 #[rustfmt::skip]
1224 let section = Section::with_endian(Endian::Little)
1225 .mark(&start)
1227 .L32(0x10000).L32(0x10100).L16(4).L32(1)
1228 .mark(&first)
1229 .L32(0x10200).L32(0x10300).L16(4).L32(2)
1231 .L32(0xffff_ffff).L32(0x0200_0000)
1233 .L32(0x10400).L32(0x10500).L16(4).L32(3)
1234 .L32(0x10600).L32(0x10600).L16(4).L32(4)
1236 .L32(0x10800).L32(0x10900).L16(4).L32(5)
1237 .L32(0).L32(1).L16(4).L32(6)
1239 .L32(0xffff_ffff).L32(0x0000_0000)
1241 .L32(0).L32(0xffff_ffff).L16(4).L32(7)
1242 .L32(tombstone).L32(tombstone).L16(4).L32(8)
1244 .L32(0xffff_ffff).L32(tombstone)
1246 .L32(0x10a00).L32(0x10b00).L16(4).L32(9)
1247 .L32(0).L32(0)
1249 .L32(0);
1251
1252 let buf = section.get_contents().unwrap();
1253 let debug_loc = DebugLoc::new(&buf, LittleEndian);
1254 let debug_loclists = DebugLocLists::new(&[], LittleEndian);
1255 let loclists = LocationLists::new(debug_loc, debug_loclists);
1256 let offset = LocationListsOffset((&first - &start) as usize);
1257 let debug_addr = &DebugAddr::from(EndianSlice::new(&[], LittleEndian));
1258 let debug_addr_base = DebugAddrBase(0);
1259 let encoding = Encoding {
1260 format: Format::Dwarf32,
1261 version: 4,
1262 address_size: 4,
1263 };
1264 let mut locations = loclists
1265 .locations(offset, encoding, 0x0100_0000, debug_addr, debug_addr_base)
1266 .unwrap();
1267
1268 assert_eq!(
1270 locations.next(),
1271 Ok(Some(LocationListEntry {
1272 range: Range {
1273 begin: 0x0101_0200,
1274 end: 0x0101_0300,
1275 },
1276 data: Expression(EndianSlice::new(&[2, 0, 0, 0], LittleEndian)),
1277 }))
1278 );
1279
1280 assert_eq!(
1282 locations.next(),
1283 Ok(Some(LocationListEntry {
1284 range: Range {
1285 begin: 0x0201_0400,
1286 end: 0x0201_0500,
1287 },
1288 data: Expression(EndianSlice::new(&[3, 0, 0, 0], LittleEndian)),
1289 }))
1290 );
1291
1292 assert_eq!(
1294 locations.next(),
1295 Ok(Some(LocationListEntry {
1296 range: Range {
1297 begin: 0x0201_0600,
1298 end: 0x0201_0600,
1299 },
1300 data: Expression(EndianSlice::new(&[4, 0, 0, 0], LittleEndian)),
1301 }))
1302 );
1303 assert_eq!(
1304 locations.next(),
1305 Ok(Some(LocationListEntry {
1306 range: Range {
1307 begin: 0x0201_0800,
1308 end: 0x0201_0900,
1309 },
1310 data: Expression(EndianSlice::new(&[5, 0, 0, 0], LittleEndian)),
1311 }))
1312 );
1313
1314 assert_eq!(
1316 locations.next(),
1317 Ok(Some(LocationListEntry {
1318 range: Range {
1319 begin: 0x0200_0000,
1320 end: 0x0200_0001,
1321 },
1322 data: Expression(EndianSlice::new(&[6, 0, 0, 0], LittleEndian)),
1323 }))
1324 );
1325
1326 assert_eq!(
1328 locations.next(),
1329 Ok(Some(LocationListEntry {
1330 range: Range {
1331 begin: 0x0000_0000,
1332 end: 0xffff_ffff,
1333 },
1334 data: Expression(EndianSlice::new(&[7, 0, 0, 0], LittleEndian)),
1335 }))
1336 );
1337
1338 assert_eq!(locations.next(), Ok(None));
1340
1341 let mut locations = loclists
1343 .locations(
1344 LocationListsOffset(buf.len()),
1345 encoding,
1346 0x0100_0000,
1347 debug_addr,
1348 debug_addr_base,
1349 )
1350 .unwrap();
1351 assert_eq!(locations.next(), Ok(None));
1352 }
1353
1354 #[test]
1355 fn test_location_list_64() {
1356 let tombstone = !0u64 - 1;
1357 let start = Label::new();
1358 let first = Label::new();
1359 #[rustfmt::skip]
1360 let section = Section::with_endian(Endian::Little)
1361 .mark(&start)
1363 .L64(0x10000).L64(0x10100).L16(4).L32(1)
1364 .mark(&first)
1365 .L64(0x10200).L64(0x10300).L16(4).L32(2)
1367 .L64(0xffff_ffff_ffff_ffff).L64(0x0200_0000)
1369 .L64(0x10400).L64(0x10500).L16(4).L32(3)
1370 .L64(0x10600).L64(0x10600).L16(4).L32(4)
1372 .L64(0x10800).L64(0x10900).L16(4).L32(5)
1373 .L64(0).L64(1).L16(4).L32(6)
1375 .L64(0xffff_ffff_ffff_ffff).L64(0x0000_0000)
1377 .L64(0).L64(0xffff_ffff_ffff_ffff).L16(4).L32(7)
1378 .L64(tombstone).L64(tombstone).L16(4).L32(8)
1380 .L64(0xffff_ffff_ffff_ffff).L64(tombstone)
1382 .L64(0x10a00).L64(0x10b00).L16(4).L32(9)
1383 .L64(0).L64(0)
1385 .L64(0);
1387
1388 let buf = section.get_contents().unwrap();
1389 let debug_loc = DebugLoc::new(&buf, LittleEndian);
1390 let debug_loclists = DebugLocLists::new(&[], LittleEndian);
1391 let loclists = LocationLists::new(debug_loc, debug_loclists);
1392 let offset = LocationListsOffset((&first - &start) as usize);
1393 let debug_addr = &DebugAddr::from(EndianSlice::new(&[], LittleEndian));
1394 let debug_addr_base = DebugAddrBase(0);
1395 let encoding = Encoding {
1396 format: Format::Dwarf64,
1397 version: 4,
1398 address_size: 8,
1399 };
1400 let mut locations = loclists
1401 .locations(offset, encoding, 0x0100_0000, debug_addr, debug_addr_base)
1402 .unwrap();
1403
1404 assert_eq!(
1406 locations.next(),
1407 Ok(Some(LocationListEntry {
1408 range: Range {
1409 begin: 0x0101_0200,
1410 end: 0x0101_0300,
1411 },
1412 data: Expression(EndianSlice::new(&[2, 0, 0, 0], LittleEndian)),
1413 }))
1414 );
1415
1416 assert_eq!(
1418 locations.next(),
1419 Ok(Some(LocationListEntry {
1420 range: Range {
1421 begin: 0x0201_0400,
1422 end: 0x0201_0500,
1423 },
1424 data: Expression(EndianSlice::new(&[3, 0, 0, 0], LittleEndian)),
1425 }))
1426 );
1427
1428 assert_eq!(
1430 locations.next(),
1431 Ok(Some(LocationListEntry {
1432 range: Range {
1433 begin: 0x0201_0600,
1434 end: 0x0201_0600,
1435 },
1436 data: Expression(EndianSlice::new(&[4, 0, 0, 0], LittleEndian)),
1437 }))
1438 );
1439 assert_eq!(
1440 locations.next(),
1441 Ok(Some(LocationListEntry {
1442 range: Range {
1443 begin: 0x0201_0800,
1444 end: 0x0201_0900,
1445 },
1446 data: Expression(EndianSlice::new(&[5, 0, 0, 0], LittleEndian)),
1447 }))
1448 );
1449
1450 assert_eq!(
1452 locations.next(),
1453 Ok(Some(LocationListEntry {
1454 range: Range {
1455 begin: 0x0200_0000,
1456 end: 0x0200_0001,
1457 },
1458 data: Expression(EndianSlice::new(&[6, 0, 0, 0], LittleEndian)),
1459 }))
1460 );
1461
1462 assert_eq!(
1464 locations.next(),
1465 Ok(Some(LocationListEntry {
1466 range: Range {
1467 begin: 0x0,
1468 end: 0xffff_ffff_ffff_ffff,
1469 },
1470 data: Expression(EndianSlice::new(&[7, 0, 0, 0], LittleEndian)),
1471 }))
1472 );
1473
1474 assert_eq!(locations.next(), Ok(None));
1476
1477 let mut locations = loclists
1479 .locations(
1480 LocationListsOffset(buf.len()),
1481 encoding,
1482 0x0100_0000,
1483 debug_addr,
1484 debug_addr_base,
1485 )
1486 .unwrap();
1487 assert_eq!(locations.next(), Ok(None));
1488 }
1489
1490 #[test]
1491 fn test_locations_invalid() {
1492 #[rustfmt::skip]
1493 let section = Section::with_endian(Endian::Little)
1494 .L32(0x20000).L32(0x10000).L16(4).L32(1)
1496 .L32(0x20000).L32(0xff01_0000).L16(4).L32(2);
1498
1499 let buf = section.get_contents().unwrap();
1500 let debug_loc = DebugLoc::new(&buf, LittleEndian);
1501 let debug_loclists = DebugLocLists::new(&[], LittleEndian);
1502 let loclists = LocationLists::new(debug_loc, debug_loclists);
1503 let debug_addr = &DebugAddr::from(EndianSlice::new(&[], LittleEndian));
1504 let debug_addr_base = DebugAddrBase(0);
1505 let encoding = Encoding {
1506 format: Format::Dwarf32,
1507 version: 4,
1508 address_size: 4,
1509 };
1510
1511 let mut locations = loclists
1513 .locations(
1514 LocationListsOffset(0x0),
1515 encoding,
1516 0x0100_0000,
1517 debug_addr,
1518 debug_addr_base,
1519 )
1520 .unwrap();
1521 assert_eq!(locations.next(), Ok(None));
1522
1523 let mut locations = loclists
1525 .locations(
1526 LocationListsOffset(14),
1527 encoding,
1528 0x0100_0000,
1529 debug_addr,
1530 debug_addr_base,
1531 )
1532 .unwrap();
1533 assert_eq!(locations.next(), Ok(None));
1534
1535 match loclists.locations(
1537 LocationListsOffset(buf.len() + 1),
1538 encoding,
1539 0x0100_0000,
1540 debug_addr,
1541 debug_addr_base,
1542 ) {
1543 Err(Error::UnexpectedEof(_)) => {}
1544 otherwise => panic!("Unexpected result: {:?}", otherwise),
1545 }
1546 }
1547
1548 #[test]
1549 fn test_get_offset() {
1550 for format in [Format::Dwarf32, Format::Dwarf64] {
1551 let encoding = Encoding {
1552 format,
1553 version: 5,
1554 address_size: 4,
1555 };
1556
1557 let zero = Label::new();
1558 let length = Label::new();
1559 let start = Label::new();
1560 let first = Label::new();
1561 let end = Label::new();
1562 let mut section = Section::with_endian(Endian::Little)
1563 .mark(&zero)
1564 .initial_length(format, &length, &start)
1565 .D16(encoding.version)
1566 .D8(encoding.address_size)
1567 .D8(0)
1568 .D32(20)
1569 .mark(&first);
1570 for i in 0..20 {
1571 section = section.word(format.word_size(), 1000 + i);
1572 }
1573 section = section.mark(&end);
1574 length.set_const((&end - &start) as u64);
1575 let section = section.get_contents().unwrap();
1576
1577 let debug_loc = DebugLoc::from(EndianSlice::new(&[], LittleEndian));
1578 let debug_loclists = DebugLocLists::from(EndianSlice::new(§ion, LittleEndian));
1579 let locations = LocationLists::new(debug_loc, debug_loclists);
1580
1581 let base = DebugLocListsBase((&first - &zero) as usize);
1582 assert_eq!(
1583 locations.get_offset(encoding, base, DebugLocListsIndex(0)),
1584 Ok(LocationListsOffset(base.0 + 1000))
1585 );
1586 assert_eq!(
1587 locations.get_offset(encoding, base, DebugLocListsIndex(19)),
1588 Ok(LocationListsOffset(base.0 + 1019))
1589 );
1590 }
1591 }
1592
1593 #[test]
1594 fn test_loclists_gnu_v4_split_dwarf() {
1595 #[rustfmt::skip]
1596 let buf = [
1597 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x11, 0x00, 0x9f, ];
1606 let data_buf = [0x11, 0x00, 0x9f];
1607 let expected_data = EndianSlice::new(&data_buf, LittleEndian);
1608 let debug_loc = DebugLoc::new(&buf, LittleEndian);
1609 let debug_loclists = DebugLocLists::new(&[], LittleEndian);
1610 let loclists = LocationLists::new(debug_loc, debug_loclists);
1611 let debug_addr =
1612 &DebugAddr::from(EndianSlice::new(&[0x01, 0x02, 0x03, 0x04], LittleEndian));
1613 let debug_addr_base = DebugAddrBase(0);
1614 let encoding = Encoding {
1615 format: Format::Dwarf32,
1616 version: 4,
1617 address_size: 4,
1618 };
1619
1620 let mut locations = loclists
1622 .locations_dwo(
1623 LocationListsOffset(0x0),
1624 encoding,
1625 0,
1626 debug_addr,
1627 debug_addr_base,
1628 )
1629 .unwrap();
1630 assert_eq!(
1631 locations.next(),
1632 Ok(Some(LocationListEntry {
1633 range: Range {
1634 begin: 0x0403_0201,
1635 end: 0x0403_0209
1636 },
1637 data: Expression(expected_data),
1638 }))
1639 );
1640 }
1641}