1 //===- DWARFDebugAranges.cpp ----------------------------------------------===// 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 10 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" 11 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 12 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 13 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" 14 #include "llvm/Support/DataExtractor.h" 15 #include <algorithm> 16 #include <cassert> 17 #include <cstdint> 18 #include <set> 19 #include <vector> 20 21 using namespace llvm; 22 23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) { 24 if (!DebugArangesData.isValidOffset(0)) 25 return; 26 uint32_t Offset = 0; 27 DWARFDebugArangeSet Set; 28 29 while (Set.extract(DebugArangesData, &Offset)) { 30 uint32_t CUOffset = Set.getCompileUnitDIEOffset(); 31 for (const auto &Desc : Set.descriptors()) { 32 uint64_t LowPC = Desc.Address; 33 uint64_t HighPC = Desc.getEndAddress(); 34 appendRange(CUOffset, LowPC, HighPC); 35 } 36 ParsedCUOffsets.insert(CUOffset); 37 } 38 } 39 40 void DWARFDebugAranges::generate(DWARFContext *CTX) { 41 clear(); 42 if (!CTX) 43 return; 44 45 // Extract aranges from .debug_aranges section. 46 DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0); 47 extract(ArangesData); 48 49 // Generate aranges from DIEs: even if .debug_aranges section is present, 50 // it may describe only a small subset of compilation units, so we need to 51 // manually build aranges for the rest of them. 52 for (const auto &CU : CTX->compile_units()) { 53 uint32_t CUOffset = CU->getOffset(); 54 if (ParsedCUOffsets.insert(CUOffset).second) { 55 DWARFAddressRangesVector CURanges; 56 CU->collectAddressRanges(CURanges); 57 for (const auto &R : CURanges) 58 appendRange(CUOffset, R.LowPC, R.HighPC); 59 } 60 } 61 62 construct(); 63 } 64 65 void DWARFDebugAranges::clear() { 66 Endpoints.clear(); 67 Aranges.clear(); 68 ParsedCUOffsets.clear(); 69 } 70 71 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC, 72 uint64_t HighPC) { 73 if (LowPC >= HighPC) 74 return; 75 Endpoints.emplace_back(LowPC, CUOffset, true); 76 Endpoints.emplace_back(HighPC, CUOffset, false); 77 } 78 79 void DWARFDebugAranges::construct() { 80 std::multiset<uint32_t> ValidCUs; // Maintain the set of CUs describing 81 // a current address range. 82 std::sort(Endpoints.begin(), Endpoints.end()); 83 uint64_t PrevAddress = -1ULL; 84 for (const auto &E : Endpoints) { 85 if (PrevAddress < E.Address && !ValidCUs.empty()) { 86 // If the address range between two endpoints is described by some 87 // CU, first try to extend the last range in Aranges. If we can't 88 // do it, start a new range. 89 if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress && 90 ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) { 91 Aranges.back().setHighPC(E.Address); 92 } else { 93 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin()); 94 } 95 } 96 // Update the set of valid CUs. 97 if (E.IsRangeStart) { 98 ValidCUs.insert(E.CUOffset); 99 } else { 100 auto CUPos = ValidCUs.find(E.CUOffset); 101 assert(CUPos != ValidCUs.end()); 102 ValidCUs.erase(CUPos); 103 } 104 PrevAddress = E.Address; 105 } 106 assert(ValidCUs.empty()); 107 108 // Endpoints are not needed now. 109 std::vector<RangeEndpoint> EmptyEndpoints; 110 EmptyEndpoints.swap(Endpoints); 111 } 112 113 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { 114 if (!Aranges.empty()) { 115 Range range(Address); 116 RangeCollIterator begin = Aranges.begin(); 117 RangeCollIterator end = Aranges.end(); 118 RangeCollIterator pos = 119 std::lower_bound(begin, end, range); 120 121 if (pos != end && pos->containsAddress(Address)) { 122 return pos->CUOffset; 123 } else if (pos != begin) { 124 --pos; 125 if (pos->containsAddress(Address)) 126 return pos->CUOffset; 127 } 128 } 129 return -1U; 130 } 131