Debian Codegraph @

third_party/rust/gimli/src/read/loclists.rs

raw
1 use crate::common::{
2 DebugAddrBase, DebugAddrIndex, DebugLocListsBase, DebugLocListsIndex, DwarfFileType, Encoding,
3 LocationListsOffset, SectionId,
4 };
5 use crate::constants;
6 use crate::endianity::Endianity;
7 use crate::read::{
8 lists::ListsHeader, DebugAddr, EndianSlice, Error, Expression, Range, RawRange, Reader,
9 ReaderAddress, ReaderOffset, ReaderOffsetId, Result, Section,
10 };
11
12 /// The raw contents of the `.debug_loc` section.
13 #[derive(Debug, Default, Clone, Copy)]
14 pub struct DebugLoc<R> {
15 pub(crate) section: R,
16 }
17
18 impl<'input, Endian> DebugLoc<EndianSlice<'input, Endian>>
19 where
20 Endian: Endianity,
21 {
22 /// Construct a new `DebugLoc` instance from the data in the `.debug_loc`
23 /// section.
24 ///
25 /// It is the caller's responsibility to read the `.debug_loc` section and
26 /// present it as a `&[u8]` slice. That means using some ELF loader on
27 /// Linux, a Mach-O loader on macOS, etc.
28 ///
29 /// ```
30 /// use gimli::{DebugLoc, LittleEndian};
31 ///
32 /// # let buf = [0x00, 0x01, 0x02, 0x03];
33 /// # let read_debug_loc_section_somehow = || &buf;
34 /// let debug_loc = DebugLoc::new(read_debug_loc_section_somehow(), LittleEndian);
35 /// ```
36 pub fn new(section: &'input [u8], endian: Endian) -> Self {
37 Self::from(EndianSlice::new(section, endian))
38 }
39 }
40
41 impl<T> DebugLoc<T> {
42 /// Create a `DebugLoc` section that references the data in `self`.
43 ///
44 /// This is useful when `R` implements `Reader` but `T` does not.
45 ///
46 /// Used by `DwarfSections::borrow`.
47 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
55 impl<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
65 impl<R> From<R> for DebugLoc<R> {
66 fn from(section: R) -> Self {
67 DebugLoc { section }
68 }
69 }
70
71 /// The `DebugLocLists` struct represents the DWARF data
72 /// found in the `.debug_loclists` section.
73 #[derive(Debug, Default, Clone, Copy)]
74 pub struct DebugLocLists<R> {
75 section: R,
76 }
77
78 impl<'input, Endian> DebugLocLists<EndianSlice<'input, Endian>>
79 where
80 Endian: Endianity,
81 {
82 /// Construct a new `DebugLocLists` instance from the data in the `.debug_loclists`
83 /// section.
84 ///
85 /// It is the caller's responsibility to read the `.debug_loclists` section and
86 /// present it as a `&[u8]` slice. That means using some ELF loader on
87 /// Linux, a Mach-O loader on macOS, etc.
88 ///
89 /// ```
90 /// use gimli::{DebugLocLists, LittleEndian};
91 ///
92 /// # let buf = [0x00, 0x01, 0x02, 0x03];
93 /// # let read_debug_loclists_section_somehow = || &buf;
94 /// let debug_loclists = DebugLocLists::new(read_debug_loclists_section_somehow(), LittleEndian);
95 /// ```
96 pub fn new(section: &'input [u8], endian: Endian) -> Self {
97 Self::from(EndianSlice::new(section, endian))
98 }
99 }
100
101 impl<T> DebugLocLists<T> {
102 /// Create a `DebugLocLists` section that references the data in `self`.
103 ///
104 /// This is useful when `R` implements `Reader` but `T` does not.
105 ///
106 /// Used by `DwarfSections::borrow`.
107 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
115 impl<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
125 impl<R> From<R> for DebugLocLists<R> {
126 fn from(section: R) -> Self {
127 DebugLocLists { section }
128 }
129 }
130
131 pub(crate) type LocListsHeader = ListsHeader;
132
133 impl<Offset> DebugLocListsBase<Offset>
134 where
135 Offset: ReaderOffset,
136 {
137 /// Returns a `DebugLocListsBase` with the default value of DW_AT_loclists_base
138 /// for the given `Encoding` and `DwarfFileType`.
139 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 // In .dwo files, the compiler omits the DW_AT_loclists_base attribute (because there is
145 // only a single unit in the file) but we must skip past the header, which the attribute
146 // would normally do for us.
147 DebugLocListsBase(Offset::from_u8(LocListsHeader::size_for_encoding(encoding)))
148 } else {
149 DebugLocListsBase(Offset::from_u8(0))
150 }
151 }
152 }
153
154 /// The DWARF data found in `.debug_loc` and `.debug_loclists` sections.
155 #[derive(Debug, Default, Clone, Copy)]
156 pub struct LocationLists<R> {
157 debug_loc: DebugLoc<R>,
158 debug_loclists: DebugLocLists<R>,
159 }
160
161 impl<R> LocationLists<R> {
162 /// Construct a new `LocationLists` instance from the data in the `.debug_loc` and
163 /// `.debug_loclists` sections.
164 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
172 impl<T> LocationLists<T> {
173 /// Create a `LocationLists` that references the data in `self`.
174 ///
175 /// This is useful when `R` implements `Reader` but `T` does not.
176 ///
177 /// Used by `Dwarf::borrow`.
178 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
189 impl<R: Reader> LocationLists<R> {
190 /// Iterate over the `LocationListEntry`s starting at the given offset.
191 ///
192 /// The `unit_encoding` must match the compilation unit that the
193 /// offset was contained in.
194 ///
195 /// The `base_address` should be obtained from the `DW_AT_low_pc` attribute in the
196 /// `DW_TAG_compile_unit` entry for the compilation unit that contains this location
197 /// list.
198 ///
199 /// Can be [used with
200 /// `FallibleIterator`](./index.html#using-with-fallibleiterator).
201 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 /// Similar to `locations`, but with special handling for .dwo files.
218 /// This should only been used when this `LocationLists` was loaded from a
219 /// .dwo file.
220 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 /// Iterate over the raw `LocationListEntry`s starting at the given offset.
237 ///
238 /// The `unit_encoding` must match the compilation unit that the
239 /// offset was contained in.
240 ///
241 /// This iterator does not perform any processing of the location entries,
242 /// such as handling base addresses.
243 ///
244 /// Can be [used with
245 /// `FallibleIterator`](./index.html#using-with-fallibleiterator).
246 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 /// Similar to `raw_locations`, but with special handling for .dwo files.
261 /// This should only been used when this `LocationLists` was loaded from a
262 /// .dwo file.
263 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 // In the GNU split dwarf extension the locations are present in the
270 // .debug_loc section but are encoded with the DW_LLE values used
271 // for the DWARF 5 .debug_loclists section.
272 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 /// Returns the `.debug_loclists` offset at the given `base` and `index`.
285 ///
286 /// The `base` must be the `DW_AT_loclists_base` value from the compilation unit DIE.
287 /// This is an offset that points to the first entry following the header.
288 ///
289 /// The `index` is the value of a `DW_FORM_loclistx` attribute.
290 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 /// Call `Reader::lookup_offset_id` for each section, and return the first match.
308 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)]
316 enum LocListsFormat {
317 /// The bare location list format used before DWARF 5.
318 Bare,
319 /// The DW_LLE encoded range list format used in DWARF 5 and the non-standard GNU
320 /// split dwarf extension.
321 Lle,
322 }
323
324 /// A raw iterator over a location list.
325 ///
326 /// This iterator does not perform any processing of the location entries,
327 /// such as handling base addresses.
328 #[derive(Debug)]
329 pub struct RawLocListIter<R: Reader> {
330 input: R,
331 encoding: Encoding,
332 format: LocListsFormat,
333 }
334
335 /// A raw entry in .debug_loclists.
336 #[derive(Clone, Debug)]
337 pub enum RawLocListEntry<R: Reader> {
338 /// A location from DWARF version <= 4.
339 AddressOrOffsetPair {
340 /// Start of range. May be an address or an offset.
341 begin: u64,
342 /// End of range. May be an address or an offset.
343 end: u64,
344 /// expression
345 data: Expression<R>,
346 },
347 /// DW_LLE_base_address
348 BaseAddress {
349 /// base address
350 addr: u64,
351 },
352 /// DW_LLE_base_addressx
353 BaseAddressx {
354 /// base address
355 addr: DebugAddrIndex<R::Offset>,
356 },
357 /// DW_LLE_startx_endx
358 StartxEndx {
359 /// start of range
360 begin: DebugAddrIndex<R::Offset>,
361 /// end of range
362 end: DebugAddrIndex<R::Offset>,
363 /// expression
364 data: Expression<R>,
365 },
366 /// DW_LLE_startx_length
367 StartxLength {
368 /// start of range
369 begin: DebugAddrIndex<R::Offset>,
370 /// length of range
371 length: u64,
372 /// expression
373 data: Expression<R>,
374 },
375 /// DW_LLE_offset_pair
376 OffsetPair {
377 /// start of range
378 begin: u64,
379 /// end of range
380 end: u64,
381 /// expression
382 data: Expression<R>,
383 },
384 /// DW_LLE_default_location
385 DefaultLocation {
386 /// expression
387 data: Expression<R>,
388 },
389 /// DW_LLE_start_end
390 StartEnd {
391 /// start of range
392 begin: u64,
393 /// end of range
394 end: u64,
395 /// expression
396 data: Expression<R>,
397 },
398 /// DW_LLE_start_length
399 StartLength {
400 /// start of range
401 begin: u64,
402 /// length of range
403 length: u64,
404 /// expression
405 data: Expression<R>,
406 },
407 }
408
409 fn 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 // In the GNU split-dwarf extension this is a fixed 2 byte value.
415 let len = R::Offset::from_u16(input.read_u16()?);
416 Ok(Expression(input.split(len)?))
417 }
418 }
419
420 impl<R: Reader> RawLocListEntry<R> {
421 /// Parse a location list entry from `.debug_loclists`
422 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 // In the GNU split-dwarf extension this is a fixed 4 byte value.
456 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
489 impl<R: Reader> RawLocListIter<R> {
490 /// Construct a `RawLocListIter`.
491 fn new(input: R, encoding: Encoding, format: LocListsFormat) -> RawLocListIter<R> {
492 RawLocListIter {
493 input,
494 encoding,
495 format,
496 }
497 }
498
499 /// Advance the iterator to the next location.
500 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")]
521 impl<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 /// An iterator over a location list.
531 ///
532 /// This iterator internally handles processing of base address selection entries
533 /// and list end entries. Thus, it only returns location entries that are valid
534 /// and already adjusted for the base address.
535 #[derive(Debug)]
536 pub 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
543 impl<R: Reader> LocListIter<R> {
544 /// Construct a `LocListIter`.
545 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 /// Advance the iterator to the next location.
566 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 /// Return the next raw location.
581 ///
582 /// The raw location should be passed to `convert_raw`.
583 #[doc(hidden)]
584 pub fn next_raw(&mut self) -> Result<Option<RawLocListEntry<R>>> {
585 self.raw.next()
586 }
587
588 /// Convert a raw location into a location, and update the state of the iterator.
589 ///
590 /// The raw location should have been obtained from `next_raw`.
591 #[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")]
663 impl<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 /// A location list entry from the `.debug_loc` or `.debug_loclists` sections.
673 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
674 pub struct LocationListEntry<R: Reader> {
675 /// The address range that this location is valid for.
676 pub range: Range,
677
678 /// The data containing a single location description.
679 pub data: Expression<R>,
680 }
681
682 #[cfg(test)]
683 mod 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 // Header
717 .mark(&start)
718 .L32(&size)
719 .L16(encoding.version)
720 .L8(encoding.address_size)
721 .L8(0)
722 .L32(0)
723 .mark(&first)
724 // OffsetPair
725 .L8(4).uleb(0x10200).uleb(0x10300).uleb(4).L32(2)
726 // A base address selection followed by an OffsetPair.
727 .L8(6).L32(0x0200_0000)
728 .L8(4).uleb(0x10400).uleb(0x10500).uleb(4).L32(3)
729 // An empty OffsetPair followed by a normal OffsetPair.
730 .L8(4).uleb(0x10600).uleb(0x10600).uleb(4).L32(4)
731 .L8(4).uleb(0x10800).uleb(0x10900).uleb(4).L32(5)
732 // A StartEnd
733 .L8(7).L32(0x201_0a00).L32(0x201_0b00).uleb(4).L32(6)
734 // A StartLength
735 .L8(8).L32(0x201_0c00).uleb(0x100).uleb(4).L32(7)
736 // An OffsetPair that starts at 0.
737 .L8(4).uleb(0).uleb(1).uleb(4).L32(8)
738 // An OffsetPair that ends at -1.
739 .L8(6).L32(0)
740 .L8(4).uleb(0).uleb(0xffff_ffff).uleb(4).L32(9)
741 // A DefaultLocation
742 .L8(5).uleb(4).L32(10)
743 // A BaseAddressx + OffsetPair
744 .L8(1).uleb(0)
745 .L8(4).uleb(0x10100).uleb(0x10200).uleb(4).L32(11)
746 // A StartxEndx
747 .L8(2).uleb(1).uleb(2).uleb(4).L32(12)
748 // A StartxLength
749 .L8(3).uleb(3).uleb(0x100).uleb(4).L32(13)
750
751 // Tombstone entries, all of which should be ignored.
752 // A BaseAddressx that is a tombstone.
753 .L8(1).uleb(4)
754 .L8(4).uleb(0x11100).uleb(0x11200).uleb(4).L32(20)
755 // A BaseAddress that is a tombstone.
756 .L8(6).L32(tombstone)
757 .L8(4).uleb(0x11300).uleb(0x11400).uleb(4).L32(21)
758 // A StartxEndx that is a tombstone.
759 .L8(2).uleb(4).uleb(5).uleb(4).L32(22)
760 // A StartxLength that is a tombstone.
761 .L8(3).uleb(4).uleb(0x100).uleb(4).L32(23)
762 // A StartEnd that is a tombstone.
763 .L8(7).L32(tombstone).L32(0x201_1500).uleb(4).L32(24)
764 // A StartLength that is a tombstone.
765 .L8(8).L32(tombstone).uleb(0x100).uleb(4).L32(25)
766 // A StartEnd (not ignored)
767 .L8(7).L32(0x201_1600).L32(0x201_1700).uleb(4).L32(26)
768
769 // A range end.
770 .L8(0)
771 // Some extra data.
772 .L32(0xffff_ffff);
773 size.set_const((&section.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 // A normal location.
785 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 // A base address selection followed by a normal location.
797 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 // An empty location range followed by a normal location.
809 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 // A normal location.
831 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 // A normal location.
843 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 // A location range that starts at 0.
855 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 // A location range that ends at -1.
867 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 // A DefaultLocation.
879 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 // A BaseAddressx + OffsetPair
891 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 // A StartxEndx
903 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 // A StartxLength
915 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 // A StartEnd location following the tombstones
927 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 // A location list end.
939 assert_eq!(locations.next(), Ok(None));
940
941 // An offset at the end of buf.
942 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 // Header
980 .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 // OffsetPair
989 .L8(4).uleb(0x10200).uleb(0x10300).uleb(4).L32(2)
990 // A base address selection followed by an OffsetPair.
991 .L8(6).L64(0x0200_0000)
992 .L8(4).uleb(0x10400).uleb(0x10500).uleb(4).L32(3)
993 // An empty OffsetPair followed by a normal OffsetPair.
994 .L8(4).uleb(0x10600).uleb(0x10600).uleb(4).L32(4)
995 .L8(4).uleb(0x10800).uleb(0x10900).uleb(4).L32(5)
996 // A StartEnd
997 .L8(7).L64(0x201_0a00).L64(0x201_0b00).uleb(4).L32(6)
998 // A StartLength
999 .L8(8).L64(0x201_0c00).uleb(0x100).uleb(4).L32(7)
1000 // An OffsetPair that starts at 0.
1001 .L8(4).uleb(0).uleb(1).uleb(4).L32(8)
1002 // An OffsetPair that ends at -1.
1003 .L8(6).L64(0)
1004 .L8(4).uleb(0).uleb(0xffff_ffff).uleb(4).L32(9)
1005 // A DefaultLocation
1006 .L8(5).uleb(4).L32(10)
1007 // A BaseAddressx + OffsetPair
1008 .L8(1).uleb(0)
1009 .L8(4).uleb(0x10100).uleb(0x10200).uleb(4).L32(11)
1010 // A StartxEndx
1011 .L8(2).uleb(1).uleb(2).uleb(4).L32(12)
1012 // A StartxLength
1013 .L8(3).uleb(3).uleb(0x100).uleb(4).L32(13)
1014
1015 // Tombstone entries, all of which should be ignored.
1016 // A BaseAddressx that is a tombstone.
1017 .L8(1).uleb(4)
1018 .L8(4).uleb(0x11100).uleb(0x11200).uleb(4).L32(20)
1019 // A BaseAddress that is a tombstone.
1020 .L8(6).L64(tombstone)
1021 .L8(4).uleb(0x11300).uleb(0x11400).uleb(4).L32(21)
1022 // A StartxEndx that is a tombstone.
1023 .L8(2).uleb(4).uleb(5).uleb(4).L32(22)
1024 // A StartxLength that is a tombstone.
1025 .L8(3).uleb(4).uleb(0x100).uleb(4).L32(23)
1026 // A StartEnd that is a tombstone.
1027 .L8(7).L64(tombstone).L64(0x201_1500).uleb(4).L32(24)
1028 // A StartLength that is a tombstone.
1029 .L8(8).L64(tombstone).uleb(0x100).uleb(4).L32(25)
1030 // A StartEnd (not ignored)
1031 .L8(7).L64(0x201_1600).L64(0x201_1700).uleb(4).L32(26)
1032
1033 // A range end.
1034 .L8(0)
1035 // Some extra data.
1036 .L32(0xffff_ffff);
1037 size.set_const((&section.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 // A normal location.
1049 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 // A base address selection followed by a normal location.
1061 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 // An empty location range followed by a normal location.
1073 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 // A normal location.
1095 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 // A normal location.
1107 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 // A location range that starts at 0.
1119 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 // A location range that ends at -1.
1131 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 // A DefaultLocation.
1143 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 // A BaseAddressx + OffsetPair
1155 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 // A StartxEndx
1167 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 // A StartxLength
1179 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 // A StartEnd location following the tombstones
1191 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 // A location list end.
1203 assert_eq!(locations.next(), Ok(None));
1204
1205 // An offset at the end of buf.
1206 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 // A location before the offset.
1226 .mark(&start)
1227 .L32(0x10000).L32(0x10100).L16(4).L32(1)
1228 .mark(&first)
1229 // A normal location.
1230 .L32(0x10200).L32(0x10300).L16(4).L32(2)
1231 // A base address selection followed by a normal location.
1232 .L32(0xffff_ffff).L32(0x0200_0000)
1233 .L32(0x10400).L32(0x10500).L16(4).L32(3)
1234 // An empty location range followed by a normal location.
1235 .L32(0x10600).L32(0x10600).L16(4).L32(4)
1236 .L32(0x10800).L32(0x10900).L16(4).L32(5)
1237 // A location range that starts at 0.
1238 .L32(0).L32(1).L16(4).L32(6)
1239 // A location range that ends at -1.
1240 .L32(0xffff_ffff).L32(0x0000_0000)
1241 .L32(0).L32(0xffff_ffff).L16(4).L32(7)
1242 // A normal location with tombstone.
1243 .L32(tombstone).L32(tombstone).L16(4).L32(8)
1244 // A base address selection with tombstone followed by a normal location.
1245 .L32(0xffff_ffff).L32(tombstone)
1246 .L32(0x10a00).L32(0x10b00).L16(4).L32(9)
1247 // A location list end.
1248 .L32(0).L32(0)
1249 // Some extra data.
1250 .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 // A normal location.
1269 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 // A base address selection followed by a normal location.
1281 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 // An empty location range followed by a normal location.
1293 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 // A location range that starts at 0.
1315 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 // A location range that ends at -1.
1327 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 // A location list end.
1339 assert_eq!(locations.next(), Ok(None));
1340
1341 // An offset at the end of buf.
1342 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 // A location before the offset.
1362 .mark(&start)
1363 .L64(0x10000).L64(0x10100).L16(4).L32(1)
1364 .mark(&first)
1365 // A normal location.
1366 .L64(0x10200).L64(0x10300).L16(4).L32(2)
1367 // A base address selection followed by a normal location.
1368 .L64(0xffff_ffff_ffff_ffff).L64(0x0200_0000)
1369 .L64(0x10400).L64(0x10500).L16(4).L32(3)
1370 // An empty location range followed by a normal location.
1371 .L64(0x10600).L64(0x10600).L16(4).L32(4)
1372 .L64(0x10800).L64(0x10900).L16(4).L32(5)
1373 // A location range that starts at 0.
1374 .L64(0).L64(1).L16(4).L32(6)
1375 // A location range that ends at -1.
1376 .L64(0xffff_ffff_ffff_ffff).L64(0x0000_0000)
1377 .L64(0).L64(0xffff_ffff_ffff_ffff).L16(4).L32(7)
1378 // A normal location with tombstone.
1379 .L64(tombstone).L64(tombstone).L16(4).L32(8)
1380 // A base address selection with tombstone followed by a normal location.
1381 .L64(0xffff_ffff_ffff_ffff).L64(tombstone)
1382 .L64(0x10a00).L64(0x10b00).L16(4).L32(9)
1383 // A location list end.
1384 .L64(0).L64(0)
1385 // Some extra data.
1386 .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 // A normal location.
1405 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 // A base address selection followed by a normal location.
1417 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 // An empty location range followed by a normal location.
1429 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 // A location range that starts at 0.
1451 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 // A location range that ends at -1.
1463 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 // A location list end.
1475 assert_eq!(locations.next(), Ok(None));
1476
1477 // An offset at the end of buf.
1478 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 // An invalid location range.
1495 .L32(0x20000).L32(0x10000).L16(4).L32(1)
1496 // An invalid range after wrapping.
1497 .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 // An invalid location range.
1512 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 // An invalid location range after wrapping.
1524 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 // An invalid offset.
1536 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(&section, 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, // DW_LLE_startx_length
1598 0x00, // ULEB encoded b7
1599 0x08, 0x00, 0x00, 0x00, // Fixed 4 byte length of 8
1600 0x03, 0x00, // Fixed two byte length of the location
1601 0x11, 0x00, // DW_OP_constu 0
1602 0x9f, // DW_OP_stack_value
1603 // Padding data
1604 //0x99, 0x99, 0x99, 0x99
1605 ];
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 // An invalid location range.
1621 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 }