2017-02-28 07:43:14 +08:00
|
|
|
//===- DWARFDebugAranges.cpp ----------------------------------------------===//
|
2011-09-14 09:09:52 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-09-14 09:09:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-31 02:07:45 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2018-12-22 08:31:02 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2011-09-14 09:09:52 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2017-02-28 07:43:14 +08:00
|
|
|
#include <cstdint>
|
2014-06-13 07:58:49 +08:00
|
|
|
#include <set>
|
2017-02-28 07:43:14 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2011-09-14 09:09:52 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-10-01 23:48:10 +08:00
|
|
|
void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
|
|
|
|
if (!DebugArangesData.isValidOffset(0))
|
|
|
|
return;
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t Offset = 0;
|
2013-10-02 00:52:46 +08:00
|
|
|
DWARFDebugArangeSet Set;
|
|
|
|
|
|
|
|
while (Set.extract(DebugArangesData, &Offset)) {
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t CUOffset = Set.getCompileUnitDIEOffset();
|
2014-04-29 06:27:46 +08:00
|
|
|
for (const auto &Desc : Set.descriptors()) {
|
2014-03-13 15:52:54 +08:00
|
|
|
uint64_t LowPC = Desc.Address;
|
|
|
|
uint64_t HighPC = Desc.getEndAddress();
|
2013-10-02 00:52:46 +08:00
|
|
|
appendRange(CUOffset, LowPC, HighPC);
|
|
|
|
}
|
2014-06-13 07:58:49 +08:00
|
|
|
ParsedCUOffsets.insert(CUOffset);
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 23:48:10 +08:00
|
|
|
void DWARFDebugAranges::generate(DWARFContext *CTX) {
|
2013-10-02 15:12:47 +08:00
|
|
|
clear();
|
|
|
|
if (!CTX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Extract aranges from .debug_aranges section.
|
2019-08-08 01:18:11 +08:00
|
|
|
DataExtractor ArangesData(CTX->getDWARFObj().getArangesSection(),
|
2017-07-20 06:27:28 +08:00
|
|
|
CTX->isLittleEndian(), 0);
|
2013-10-02 15:12:47 +08:00
|
|
|
extract(ArangesData);
|
2011-09-15 04:52:27 +08:00
|
|
|
|
2013-10-02 15:12:47 +08:00
|
|
|
// Generate aranges from DIEs: even if .debug_aranges section is present,
|
|
|
|
// it may describe only a small subset of compilation units, so we need to
|
|
|
|
// manually build aranges for the rest of them.
|
2014-03-13 15:52:54 +08:00
|
|
|
for (const auto &CU : CTX->compile_units()) {
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t CUOffset = CU->getOffset();
|
2014-04-19 01:25:46 +08:00
|
|
|
if (ParsedCUOffsets.insert(CUOffset).second) {
|
2018-12-22 08:31:02 +08:00
|
|
|
Expected<DWARFAddressRangesVector> CURanges = CU->collectAddressRanges();
|
|
|
|
if (!CURanges)
|
|
|
|
WithColor::error() << toString(CURanges.takeError()) << '\n';
|
|
|
|
else
|
|
|
|
for (const auto &R : *CURanges)
|
|
|
|
appendRange(CUOffset, R.LowPC, R.HighPC);
|
2017-05-16 20:05:03 +08:00
|
|
|
}
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 07:58:49 +08:00
|
|
|
construct();
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 05:30:03 +08:00
|
|
|
void DWARFDebugAranges::clear() {
|
2014-06-13 07:58:49 +08:00
|
|
|
Endpoints.clear();
|
2014-04-26 05:30:03 +08:00
|
|
|
Aranges.clear();
|
|
|
|
ParsedCUOffsets.clear();
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:49:40 +08:00
|
|
|
void DWARFDebugAranges::appendRange(uint64_t CUOffset, uint64_t LowPC,
|
2013-10-01 23:48:10 +08:00
|
|
|
uint64_t HighPC) {
|
2014-06-13 07:58:49 +08:00
|
|
|
if (LowPC >= HighPC)
|
2011-09-14 09:09:52 +08:00
|
|
|
return;
|
2014-06-13 07:58:49 +08:00
|
|
|
Endpoints.emplace_back(LowPC, CUOffset, true);
|
|
|
|
Endpoints.emplace_back(HighPC, CUOffset, false);
|
|
|
|
}
|
2011-09-14 09:09:52 +08:00
|
|
|
|
2014-06-13 07:58:49 +08:00
|
|
|
void DWARFDebugAranges::construct() {
|
2019-08-06 18:49:40 +08:00
|
|
|
std::multiset<uint64_t> ValidCUs; // Maintain the set of CUs describing
|
2014-06-13 07:58:49 +08:00
|
|
|
// a current address range.
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(Endpoints);
|
2014-06-13 07:58:49 +08:00
|
|
|
uint64_t PrevAddress = -1ULL;
|
|
|
|
for (const auto &E : Endpoints) {
|
2017-02-28 07:43:14 +08:00
|
|
|
if (PrevAddress < E.Address && !ValidCUs.empty()) {
|
2014-06-13 07:58:49 +08:00
|
|
|
// If the address range between two endpoints is described by some
|
|
|
|
// CU, first try to extend the last range in Aranges. If we can't
|
|
|
|
// do it, start a new range.
|
|
|
|
if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress &&
|
|
|
|
ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) {
|
|
|
|
Aranges.back().setHighPC(E.Address);
|
|
|
|
} else {
|
|
|
|
Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the set of valid CUs.
|
|
|
|
if (E.IsRangeStart) {
|
|
|
|
ValidCUs.insert(E.CUOffset);
|
2011-09-14 09:09:52 +08:00
|
|
|
} else {
|
2014-06-13 07:58:49 +08:00
|
|
|
auto CUPos = ValidCUs.find(E.CUOffset);
|
|
|
|
assert(CUPos != ValidCUs.end());
|
|
|
|
ValidCUs.erase(CUPos);
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
2014-06-13 07:58:49 +08:00
|
|
|
PrevAddress = E.Address;
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
2014-06-13 07:58:49 +08:00
|
|
|
assert(ValidCUs.empty());
|
2011-09-14 09:09:52 +08:00
|
|
|
|
2014-06-13 07:58:49 +08:00
|
|
|
// Endpoints are not needed now.
|
2017-08-01 22:38:08 +08:00
|
|
|
Endpoints.clear();
|
|
|
|
Endpoints.shrink_to_fit();
|
2011-09-14 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
2013-10-01 23:48:10 +08:00
|
|
|
uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
|
2019-04-06 17:12:53 +08:00
|
|
|
RangeCollIterator It =
|
2019-06-30 19:19:56 +08:00
|
|
|
partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
|
2019-04-06 17:12:53 +08:00
|
|
|
if (It != Aranges.end() && It->LowPC <= Address)
|
|
|
|
return It->CUOffset;
|
2011-09-14 09:09:52 +08:00
|
|
|
return -1U;
|
|
|
|
}
|