LLVM API Documentation
00001 //===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "DWARFDebugAranges.h" 00011 #include "DWARFCompileUnit.h" 00012 #include "DWARFContext.h" 00013 #include "DWARFDebugArangeSet.h" 00014 #include "llvm/Support/Format.h" 00015 #include "llvm/Support/raw_ostream.h" 00016 #include <algorithm> 00017 #include <cassert> 00018 #include <set> 00019 using namespace llvm; 00020 00021 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) { 00022 if (!DebugArangesData.isValidOffset(0)) 00023 return; 00024 uint32_t Offset = 0; 00025 DWARFDebugArangeSet Set; 00026 00027 while (Set.extract(DebugArangesData, &Offset)) { 00028 uint32_t CUOffset = Set.getCompileUnitDIEOffset(); 00029 for (const auto &Desc : Set.descriptors()) { 00030 uint64_t LowPC = Desc.Address; 00031 uint64_t HighPC = Desc.getEndAddress(); 00032 appendRange(CUOffset, LowPC, HighPC); 00033 } 00034 ParsedCUOffsets.insert(CUOffset); 00035 } 00036 } 00037 00038 void DWARFDebugAranges::generate(DWARFContext *CTX) { 00039 clear(); 00040 if (!CTX) 00041 return; 00042 00043 // Extract aranges from .debug_aranges section. 00044 DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0); 00045 extract(ArangesData); 00046 00047 // Generate aranges from DIEs: even if .debug_aranges section is present, 00048 // it may describe only a small subset of compilation units, so we need to 00049 // manually build aranges for the rest of them. 00050 for (const auto &CU : CTX->compile_units()) { 00051 uint32_t CUOffset = CU->getOffset(); 00052 if (ParsedCUOffsets.insert(CUOffset).second) { 00053 DWARFAddressRangesVector CURanges; 00054 CU->collectAddressRanges(CURanges); 00055 for (const auto &R : CURanges) { 00056 appendRange(CUOffset, R.first, R.second); 00057 } 00058 } 00059 } 00060 00061 construct(); 00062 } 00063 00064 void DWARFDebugAranges::clear() { 00065 Endpoints.clear(); 00066 Aranges.clear(); 00067 ParsedCUOffsets.clear(); 00068 } 00069 00070 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC, 00071 uint64_t HighPC) { 00072 if (LowPC >= HighPC) 00073 return; 00074 Endpoints.emplace_back(LowPC, CUOffset, true); 00075 Endpoints.emplace_back(HighPC, CUOffset, false); 00076 } 00077 00078 void DWARFDebugAranges::construct() { 00079 std::multiset<uint32_t> ValidCUs; // Maintain the set of CUs describing 00080 // a current address range. 00081 std::sort(Endpoints.begin(), Endpoints.end()); 00082 uint64_t PrevAddress = -1ULL; 00083 for (const auto &E : Endpoints) { 00084 if (PrevAddress < E.Address && ValidCUs.size() > 0) { 00085 // If the address range between two endpoints is described by some 00086 // CU, first try to extend the last range in Aranges. If we can't 00087 // do it, start a new range. 00088 if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress && 00089 ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) { 00090 Aranges.back().setHighPC(E.Address); 00091 } else { 00092 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin()); 00093 } 00094 } 00095 // Update the set of valid CUs. 00096 if (E.IsRangeStart) { 00097 ValidCUs.insert(E.CUOffset); 00098 } else { 00099 auto CUPos = ValidCUs.find(E.CUOffset); 00100 assert(CUPos != ValidCUs.end()); 00101 ValidCUs.erase(CUPos); 00102 } 00103 PrevAddress = E.Address; 00104 } 00105 assert(ValidCUs.empty()); 00106 00107 // Endpoints are not needed now. 00108 std::vector<RangeEndpoint> EmptyEndpoints; 00109 EmptyEndpoints.swap(Endpoints); 00110 } 00111 00112 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { 00113 if (!Aranges.empty()) { 00114 Range range(Address); 00115 RangeCollIterator begin = Aranges.begin(); 00116 RangeCollIterator end = Aranges.end(); 00117 RangeCollIterator pos = 00118 std::lower_bound(begin, end, range); 00119 00120 if (pos != end && pos->containsAddress(Address)) { 00121 return pos->CUOffset; 00122 } else if (pos != begin) { 00123 --pos; 00124 if (pos->containsAddress(Address)) 00125 return pos->CUOffset; 00126 } 00127 } 00128 return -1U; 00129 }