121
|
1 //===- DWARFDebugRangesList.cpp -------------------------------------------===//
|
83
|
2 //
|
147
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
83
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
|
9 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
|
121
|
10 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
147
|
11 #include "llvm/Support/Errc.h"
|
83
|
12 #include "llvm/Support/Format.h"
|
|
13 #include "llvm/Support/raw_ostream.h"
|
121
|
14 #include <cinttypes>
|
|
15 #include <cstdint>
|
83
|
16
|
|
17 using namespace llvm;
|
|
18
|
|
19 void DWARFDebugRangeList::clear() {
|
147
|
20 Offset = -1ULL;
|
83
|
21 AddressSize = 0;
|
|
22 Entries.clear();
|
|
23 }
|
|
24
|
147
|
25 Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
|
|
26 uint64_t *offset_ptr) {
|
83
|
27 clear();
|
|
28 if (!data.isValidOffset(*offset_ptr))
|
147
|
29 return createStringError(errc::invalid_argument,
|
|
30 "invalid range list offset 0x%" PRIx64, *offset_ptr);
|
|
31
|
83
|
32 AddressSize = data.getAddressSize();
|
|
33 if (AddressSize != 4 && AddressSize != 8)
|
147
|
34 return createStringError(errc::invalid_argument,
|
|
35 "invalid address size: %" PRIu8, AddressSize);
|
83
|
36 Offset = *offset_ptr;
|
|
37 while (true) {
|
121
|
38 RangeListEntry Entry;
|
|
39 Entry.SectionIndex = -1ULL;
|
|
40
|
147
|
41 uint64_t prev_offset = *offset_ptr;
|
121
|
42 Entry.StartAddress = data.getRelocatedAddress(offset_ptr);
|
|
43 Entry.EndAddress =
|
|
44 data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex);
|
|
45
|
83
|
46 // Check that both values were extracted correctly.
|
|
47 if (*offset_ptr != prev_offset + 2 * AddressSize) {
|
|
48 clear();
|
147
|
49 return createStringError(errc::invalid_argument,
|
|
50 "invalid range list entry at offset 0x%" PRIx64,
|
|
51 prev_offset);
|
83
|
52 }
|
121
|
53 if (Entry.isEndOfListEntry())
|
83
|
54 break;
|
121
|
55 Entries.push_back(Entry);
|
83
|
56 }
|
147
|
57 return Error::success();
|
83
|
58 }
|
|
59
|
|
60 void DWARFDebugRangeList::dump(raw_ostream &OS) const {
|
|
61 for (const RangeListEntry &RLE : Entries) {
|
147
|
62 const char *format_str =
|
|
63 (AddressSize == 4 ? "%08" PRIx64 " %08" PRIx64 " %08" PRIx64 "\n"
|
|
64 : "%08" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n");
|
83
|
65 OS << format(format_str, Offset, RLE.StartAddress, RLE.EndAddress);
|
|
66 }
|
147
|
67 OS << format("%08" PRIx64 " <End of list>\n", Offset);
|
83
|
68 }
|
|
69
|
121
|
70 DWARFAddressRangesVector DWARFDebugRangeList::getAbsoluteRanges(
|
147
|
71 llvm::Optional<object::SectionedAddress> BaseAddr) const {
|
83
|
72 DWARFAddressRangesVector Res;
|
|
73 for (const RangeListEntry &RLE : Entries) {
|
|
74 if (RLE.isBaseAddressSelectionEntry(AddressSize)) {
|
121
|
75 BaseAddr = {RLE.EndAddress, RLE.SectionIndex};
|
|
76 continue;
|
83
|
77 }
|
121
|
78
|
|
79 DWARFAddressRange E;
|
|
80 E.LowPC = RLE.StartAddress;
|
|
81 E.HighPC = RLE.EndAddress;
|
|
82 E.SectionIndex = RLE.SectionIndex;
|
|
83 // Base address of a range list entry is determined by the closest preceding
|
|
84 // base address selection entry in the same range list. It defaults to the
|
|
85 // base address of the compilation unit if there is no such entry.
|
|
86 if (BaseAddr) {
|
|
87 E.LowPC += BaseAddr->Address;
|
|
88 E.HighPC += BaseAddr->Address;
|
|
89 if (E.SectionIndex == -1ULL)
|
|
90 E.SectionIndex = BaseAddr->SectionIndex;
|
|
91 }
|
|
92 Res.push_back(E);
|
83
|
93 }
|
|
94 return Res;
|
|
95 }
|