Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 1 | //===- DWARFDebugRangesList.cpp -------------------------------------------===// |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Errc.h" |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Format.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 15 | #include <cinttypes> |
| 16 | #include <cstdint> |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | void DWARFDebugRangeList::clear() { |
| 21 | Offset = -1U; |
| 22 | AddressSize = 0; |
| 23 | Entries.clear(); |
| 24 | } |
| 25 | |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 26 | Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data, |
| 27 | uint32_t *offset_ptr) { |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 28 | clear(); |
| 29 | if (!data.isValidOffset(*offset_ptr)) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 30 | return createStringError(errc::invalid_argument, |
| 31 | "invalid range list offset 0x%" PRIx32, *offset_ptr); |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 32 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 33 | AddressSize = data.getAddressSize(); |
| 34 | if (AddressSize != 4 && AddressSize != 8) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 35 | return createStringError(errc::invalid_argument, |
| 36 | "invalid address size: %" PRIu8, AddressSize); |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 37 | Offset = *offset_ptr; |
| 38 | while (true) { |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 39 | RangeListEntry Entry; |
| 40 | Entry.SectionIndex = -1ULL; |
| 41 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 42 | uint32_t prev_offset = *offset_ptr; |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 43 | Entry.StartAddress = data.getRelocatedAddress(offset_ptr); |
| 44 | Entry.EndAddress = |
| 45 | data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex); |
George Rimar | ca53211 | 2017-04-24 10:19:45 +0000 | [diff] [blame] | 46 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 47 | // Check that both values were extracted correctly. |
| 48 | if (*offset_ptr != prev_offset + 2 * AddressSize) { |
| 49 | clear(); |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 50 | return createStringError(errc::invalid_argument, |
| 51 | "invalid range list entry at offset 0x%" PRIx32, |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 52 | prev_offset); |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 53 | } |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 54 | if (Entry.isEndOfListEntry()) |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 55 | break; |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 56 | Entries.push_back(Entry); |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 57 | } |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 58 | return Error::success(); |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void DWARFDebugRangeList::dump(raw_ostream &OS) const { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 62 | for (const RangeListEntry &RLE : Entries) { |
NAKAMURA Takumi | 37338a8 | 2012-08-27 10:10:10 +0000 | [diff] [blame] | 63 | const char *format_str = (AddressSize == 4 |
Marshall Clow | ef271cc | 2012-08-27 22:53:35 +0000 | [diff] [blame] | 64 | ? "%08x %08" PRIx64 " %08" PRIx64 "\n" |
| 65 | : "%08x %016" PRIx64 " %016" PRIx64 "\n"); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 66 | OS << format(format_str, Offset, RLE.StartAddress, RLE.EndAddress); |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 67 | } |
| 68 | OS << format("%08x <End of list>\n", Offset); |
| 69 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 70 | |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 71 | DWARFAddressRangesVector DWARFDebugRangeList::getAbsoluteRanges( |
| 72 | llvm::Optional<BaseAddress> BaseAddr) const { |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 73 | DWARFAddressRangesVector Res; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 74 | for (const RangeListEntry &RLE : Entries) { |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 75 | if (RLE.isBaseAddressSelectionEntry(AddressSize)) { |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 76 | BaseAddr = {RLE.EndAddress, RLE.SectionIndex}; |
| 77 | continue; |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 78 | } |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 79 | |
| 80 | DWARFAddressRange E; |
| 81 | E.LowPC = RLE.StartAddress; |
| 82 | E.HighPC = RLE.EndAddress; |
| 83 | E.SectionIndex = RLE.SectionIndex; |
| 84 | // Base address of a range list entry is determined by the closest preceding |
| 85 | // base address selection entry in the same range list. It defaults to the |
| 86 | // base address of the compilation unit if there is no such entry. |
| 87 | if (BaseAddr) { |
| 88 | E.LowPC += BaseAddr->Address; |
| 89 | E.HighPC += BaseAddr->Address; |
| 90 | if (E.SectionIndex == -1ULL) |
| 91 | E.SectionIndex = BaseAddr->SectionIndex; |
| 92 | } |
| 93 | Res.push_back(E); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 94 | } |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 95 | return Res; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 96 | } |