blob: 5b0b3f65f6c30639cba6578479dbee25ef438805 [file] [log] [blame]
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +00001//===- DWARFUnit.cpp ------------------------------------------------------===//
David Blaikie07e22442013-09-23 22:44:40 +00002//
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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Zachary Turner82af9432015-01-30 18:07:45 +000014#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000015#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
17#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton97d22182017-01-13 21:08:18 +000018#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000019#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000020#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000021#include <algorithm>
22#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000023#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000025#include <cstdio>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000026#include <utility>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000028
Eugene Zelenko28db7e62017-03-01 01:14:23 +000029using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000030using namespace dwarf;
31
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000032void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
Rafael Espindolac398e672017-07-19 22:27:28 +000033 const DWARFObject &D = C.getDWARFObj();
34 parseImpl(C, Section, C.getDebugAbbrev(), &D.getRangeSection(),
35 D.getStringSection(), D.getStringOffsetSection(),
36 &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000037}
38
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000039void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikie82641be2015-11-17 00:39:55 +000040 const DWARFSection &DWOSection,
41 DWARFUnitIndex *Index) {
Rafael Espindolac398e672017-07-19 22:27:28 +000042 const DWARFObject &D = C.getDWARFObj();
43 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
44 D.getStringDWOSection(), D.getStringOffsetDWOSection(),
45 &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(),
David Blaikiee438cff2016-04-22 22:50:56 +000046 true);
Frederic Riss6005dbd2014-10-06 03:36:18 +000047}
48
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000049DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
George Rimarca532112017-04-24 10:19:45 +000050 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000051 StringRef SS, const DWARFSection &SOS,
Paul Robinson17536b92017-06-29 16:52:08 +000052 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
53 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000054 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000055 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000056 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000057 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
58 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000059 clear();
60}
61
Eugene Zelenko570e39a2016-11-23 23:16:32 +000062DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000063
Rafael Espindolac398e672017-07-19 22:27:28 +000064DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
65 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
66 getAddressByteSize());
67}
68
David Blaikie07e22442013-09-23 22:44:40 +000069bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
70 uint64_t &Result) const {
Paul Robinson75c068c2017-06-26 18:43:01 +000071 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
72 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie07e22442013-09-23 22:44:40 +000073 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000074 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
75 isLittleEndian, getAddressByteSize());
Paul Robinson17536b92017-06-29 16:52:08 +000076 Result = DA.getRelocatedAddress(&Offset);
David Blaikie07e22442013-09-23 22:44:40 +000077 return true;
78}
79
80bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000081 uint64_t &Result) const {
Paul Robinsond66ee0f2017-06-27 15:40:18 +000082 unsigned ItemSize = getDwarfOffsetByteSize();
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000083 uint32_t Offset = StringOffsetSectionBase + Index * ItemSize;
84 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +000085 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000086 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
87 isLittleEndian, 0);
Paul Robinson17536b92017-06-29 16:52:08 +000088 Result = DA.getRelocatedValue(ItemSize, &Offset);
David Blaikie07e22442013-09-23 22:44:40 +000089 return true;
90}
91
92bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
93 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000094 // FIXME: Support DWARF64.
95 FormParams.Format = DWARF32;
96 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +000097 uint64_t AbbrOffset;
Paul Robinson75c068c2017-06-26 18:43:01 +000098 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +000099 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000100 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000101 AbbrOffset = debug_info.getU32(offset_ptr);
102 } else {
103 AbbrOffset = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000104 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000105 }
David Blaikie82641be2015-11-17 00:39:55 +0000106 if (IndexEntry) {
107 if (AbbrOffset)
108 return false;
109 auto *UnitContrib = IndexEntry->getOffset();
110 if (!UnitContrib || UnitContrib->Length != (Length + 4))
111 return false;
112 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
113 if (!AbbrEntry)
114 return false;
115 AbbrOffset = AbbrEntry->Offset;
116 }
David Blaikie07e22442013-09-23 22:44:40 +0000117
Alexey Samsonov7682f812014-04-24 22:51:03 +0000118 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000119 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
120 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000121
Alexey Samsonov7682f812014-04-24 22:51:03 +0000122 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000123 return false;
124
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000125 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000126 Context.setMaxVersionIfGreater(getVersion());
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000127
Alexey Samsonov7682f812014-04-24 22:51:03 +0000128 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000129 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000130}
131
132bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
133 clear();
134
135 Offset = *offset_ptr;
136
137 if (debug_info.isValidOffset(*offset_ptr)) {
138 if (extractImpl(debug_info, offset_ptr))
139 return true;
140
141 // reset the offset to where we tried to parse from if anything went wrong
142 *offset_ptr = Offset;
143 }
144
145 return false;
146}
147
David Blaikie07e22442013-09-23 22:44:40 +0000148bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
Paul Robinson17536b92017-06-29 16:52:08 +0000149 DWARFDebugRangeList &RangeList) const {
David Blaikie07e22442013-09-23 22:44:40 +0000150 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000151 assert(!DieArray.empty());
Rafael Espindolac398e672017-07-19 22:27:28 +0000152 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
153 isLittleEndian, getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000154 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000155 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000156}
157
158void DWARFUnit::clear() {
159 Offset = 0;
160 Length = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000161 Abbrevs = nullptr;
Paul Robinson75c068c2017-06-26 18:43:01 +0000162 FormParams = DWARFFormParams({0, 0, DWARF32});
David Blaikie07e22442013-09-23 22:44:40 +0000163 BaseAddr = 0;
164 RangeSectionBase = 0;
165 AddrOffsetSectionBase = 0;
166 clearDIEs(false);
167 DWO.reset();
168}
169
170const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000171 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000172}
173
Greg Clayton52fe1f62016-12-14 22:38:08 +0000174Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000175 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000176}
177
David Blaikie07e22442013-09-23 22:44:40 +0000178void DWARFUnit::extractDIEsToVector(
179 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000180 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000181 if (!AppendCUDie && !AppendNonCUDies)
182 return;
183
184 // Set the offset to that of the first DIE and calculate the start of the
185 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000186 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000187 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000188 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000189 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000190 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000191 bool IsCUDie = true;
192
Greg Clayton78a07bf2016-12-21 21:37:06 +0000193 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
194 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000195 if (IsCUDie) {
196 if (AppendCUDie)
197 Dies.push_back(DIE);
198 if (!AppendNonCUDies)
199 break;
200 // The average bytes per DIE entry has been seen to be
201 // around 14-20 so let's pre-reserve the needed memory for
202 // our DIE entries accordingly.
203 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
204 IsCUDie = false;
205 } else {
206 Dies.push_back(DIE);
207 }
208
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000209 if (const DWARFAbbreviationDeclaration *AbbrDecl =
210 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000211 // Normal DIE
212 if (AbbrDecl->hasChildren())
213 ++Depth;
214 } else {
215 // NULL DIE.
216 if (Depth > 0)
217 --Depth;
218 if (Depth == 0)
219 break; // We are done with this compile unit!
220 }
221 }
222
223 // Give a little bit of info if we encounter corrupt DWARF (our offset
224 // should always terminate at or before the start of the next compilation
225 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000226 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000227 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000228 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000229}
230
231size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000232 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000233 DieArray.size() > 1)
234 return 0; // Already parsed.
235
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000236 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000237 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
238
239 if (DieArray.empty())
240 return 0;
241
242 // If CU DIE was just parsed, copy several attribute values from it.
243 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000244 DWARFDie UnitDie = getUnitDIE();
Greg Claytonc109bbe2017-01-13 22:32:12 +0000245 auto BaseAddr = toAddress(UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000246 if (BaseAddr)
247 setBaseAddress(*BaseAddr);
David Blaikie22dc4472017-08-02 20:16:22 +0000248 if (!isDWO) {
249 assert(AddrOffsetSectionBase == 0);
250 assert(RangeSectionBase == 0);
251 AddrOffsetSectionBase =
252 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
253 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
254 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000255
256 // In general, we derive the offset of the unit's contibution to the
257 // debug_str_offsets{.dwo} section from the unit DIE's
258 // DW_AT_str_offsets_base attribute. In dwp files we add to it the offset
259 // we get from the index table.
260 StringOffsetSectionBase =
261 toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
262 if (IndexEntry)
263 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
264 StringOffsetSectionBase += C->Offset;
265
Alexey Samsonovaa909982014-06-13 22:31:03 +0000266 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
267 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000268 }
269
David Blaikie07e22442013-09-23 22:44:40 +0000270 return DieArray.size();
271}
272
David Blaikie07e22442013-09-23 22:44:40 +0000273bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000274 if (isDWO)
275 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000276 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000277 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000278 DWARFDie UnitDie = getUnitDIE();
279 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000280 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000281 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000282 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000283 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000284 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000285 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000286 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
287 *CompilationDir) {
288 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000289 }
Greg Clayton97d22182017-01-13 21:08:18 +0000290 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000291 auto DWOId = getDWOId();
292 if (!DWOId)
293 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000294 auto DWOContext = Context.getDWOContext(AbsolutePath);
295 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000296 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000297
David Blaikie15d85fc2017-05-23 06:48:53 +0000298 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
299 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000300 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000301 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000302 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000303 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000304 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000305 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000306 return true;
307}
308
309void DWARFUnit::clearDIEs(bool KeepCUDie) {
310 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000311 DieArray.resize((unsigned)KeepCUDie);
312 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000313 }
314}
315
Alexey Samsonov762343d2014-04-18 17:25:46 +0000316void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000317 DWARFDie UnitDie = getUnitDIE();
318 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000319 return;
320 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000321 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000322 if (!CUDIERanges.empty()) {
323 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
324 return;
325 }
326
David Blaikie07e22442013-09-23 22:44:40 +0000327 // This function is usually called if there in no .debug_aranges section
328 // in order to produce a compile unit level set of address ranges that
329 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
330 // all compile units to stay loaded when they weren't needed. So we can end
331 // up parsing the DWARF and then throwing them all away to keep memory usage
332 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000333 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000334 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000335
336 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000337 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000338 if (DWO)
339 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000340 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000341 DWO.reset();
342
343 // Keep memory down by clearing DIEs if this generate function
344 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000345 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000346 clearDIEs(true);
347}
348
Dehao Chena364f092017-04-19 20:09:38 +0000349void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
350 if (Die.isSubroutineDIE()) {
351 for (const auto &R : Die.getAddressRanges()) {
352 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000353 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000354 continue;
George Rimar4671f2e2017-05-16 12:30:59 +0000355 auto B = AddrDieMap.upper_bound(R.LowPC);
356 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
Dehao Chena364f092017-04-19 20:09:38 +0000357 // The range is a sub-range of existing ranges, we need to split the
358 // existing range.
George Rimar4671f2e2017-05-16 12:30:59 +0000359 if (R.HighPC < B->second.first)
360 AddrDieMap[R.HighPC] = B->second;
361 if (R.LowPC > B->first)
362 AddrDieMap[B->first].first = R.LowPC;
Dehao Chena364f092017-04-19 20:09:38 +0000363 }
George Rimar4671f2e2017-05-16 12:30:59 +0000364 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
David Blaikie07e22442013-09-23 22:44:40 +0000365 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000366 }
Dehao Chena364f092017-04-19 20:09:38 +0000367 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
368 // simplify the logic to update AddrDieMap. The child's range will always
369 // be equal or smaller than the parent's range. With this assumption, when
370 // adding one range into the map, it will at most split a range into 3
371 // sub-ranges.
372 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
373 updateAddressDieMap(Child);
374}
375
376DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
377 extractDIEsIfNeeded(false);
378 if (AddrDieMap.empty())
379 updateAddressDieMap(getUnitDIE());
380 auto R = AddrDieMap.upper_bound(Address);
381 if (R == AddrDieMap.begin())
382 return DWARFDie();
383 // upper_bound's previous item contains Address.
384 --R;
385 if (Address >= R->second.first)
386 return DWARFDie();
387 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000388}
389
Greg Claytonc8c10322016-12-13 18:25:19 +0000390void
391DWARFUnit::getInlinedChainForAddress(uint64_t Address,
392 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000393 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000394 // Try to look for subprogram DIEs in the DWO file.
395 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000396 // First, find the subroutine that contains the given address (the leaf
397 // of inlined chain).
398 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000399 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000400
Dehao Chendb569ba2017-04-19 20:52:21 +0000401 while (SubroutineDIE) {
402 if (SubroutineDIE.isSubroutineDIE())
403 InlinedChain.push_back(SubroutineDIE);
404 SubroutineDIE = SubroutineDIE.getParent();
405 }
David Blaikie07e22442013-09-23 22:44:40 +0000406}
David Blaikie82641be2015-11-17 00:39:55 +0000407
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000408const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
409 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000410 if (Kind == DW_SECT_INFO)
411 return Context.getCUIndex();
412 assert(Kind == DW_SECT_TYPES);
413 return Context.getTUIndex();
414}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000415
Greg Clayton78a07bf2016-12-21 21:37:06 +0000416DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
417 if (!Die)
418 return DWARFDie();
419 const uint32_t Depth = Die->getDepth();
420 // Unit DIEs always have a depth of zero and never have parents.
421 if (Depth == 0)
422 return DWARFDie();
423 // Depth of 1 always means parent is the compile/type unit.
424 if (Depth == 1)
425 return getUnitDIE();
426 // Look for previous DIE with a depth that is one less than the Die's depth.
427 const uint32_t ParentDepth = Depth - 1;
428 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
429 if (DieArray[I].getDepth() == ParentDepth)
430 return DWARFDie(this, &DieArray[I]);
431 }
432 return DWARFDie();
433}
434
435DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
436 if (!Die)
437 return DWARFDie();
438 uint32_t Depth = Die->getDepth();
439 // Unit DIEs always have a depth of zero and never have siblings.
440 if (Depth == 0)
441 return DWARFDie();
442 // NULL DIEs don't have siblings.
443 if (Die->getAbbreviationDeclarationPtr() == nullptr)
444 return DWARFDie();
445
446 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000447 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
448 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000449 if (DieArray[I].getDepth() == Depth)
450 return DWARFDie(this, &DieArray[I]);
451 }
452 return DWARFDie();
453}