blob: 4ee8e8f46d2eb540b0bc21f001023106da81d08d [file] [log] [blame]
David Blaikie07e22442013-09-23 22:44:40 +00001//===-- DWARFUnit.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
Eugene Zelenko570e39a2016-11-23 23:16:32 +000010#include "llvm/ADT/SmallString.h"
11#include "llvm/ADT/STLExtras.h"
12#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/DebugInfo/DWARF/DWARFUnit.h"
20#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000023#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include <algorithm>
25#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000026#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000028#include <cstdio>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000030
Eugene Zelenko28db7e62017-03-01 01:14:23 +000031using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000032using namespace dwarf;
33
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000034void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
35 parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
36 C.getStringSection(), StringRef(), C.getAddrSection(),
David Blaikiee438cff2016-04-22 22:50:56 +000037 C.getLineSection().Data, C.isLittleEndian(), false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000038}
39
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000040void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikie82641be2015-11-17 00:39:55 +000041 const DWARFSection &DWOSection,
42 DWARFUnitIndex *Index) {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000043 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000044 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
David Blaikiee438cff2016-04-22 22:50:56 +000045 C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
46 true);
Frederic Riss6005dbd2014-10-06 03:36:18 +000047}
48
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000049DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
50 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
David Blaikiec4e2bed2015-11-17 21:08:05 +000051 StringRef SOS, StringRef AOS, StringRef LS, bool LE,
David Blaikiee438cff2016-04-22 22:50:56 +000052 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000053 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000054 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
David Blaikie35c2eeb2015-11-17 22:39:23 +000055 LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
David Blaikie4689ef52015-11-17 22:39:26 +000056 if (IndexEntry)
57 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
58 return SOS.slice(C->Offset, C->Offset + C->Length);
David Blaikie35c2eeb2015-11-17 22:39:23 +000059 return SOS;
60 }()),
David Blaikiee438cff2016-04-22 22:50:56 +000061 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
62 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000063 clear();
64}
65
Eugene Zelenko570e39a2016-11-23 23:16:32 +000066DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000067
68bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
69 uint64_t &Result) const {
70 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
71 if (AddrOffsetSection.size() < Offset + AddrSize)
72 return false;
73 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
74 Result = DA.getAddress(&Offset);
75 return true;
76}
77
78bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
79 uint32_t &Result) const {
80 // FIXME: string offset section entries are 8-byte for DWARF64.
81 const uint32_t ItemSize = 4;
82 uint32_t Offset = Index * ItemSize;
83 if (StringOffsetSection.size() < Offset + ItemSize)
84 return false;
85 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
86 Result = DA.getU32(&Offset);
87 return true;
88}
89
90bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
91 Length = debug_info.getU32(offset_ptr);
92 Version = debug_info.getU16(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +000093 uint64_t AbbrOffset;
94 if (Version >= 5) {
95 UnitType = debug_info.getU8(offset_ptr);
96 AddrSize = debug_info.getU8(offset_ptr);
97 AbbrOffset = debug_info.getU32(offset_ptr);
98 } else {
99 AbbrOffset = debug_info.getU32(offset_ptr);
100 AddrSize = debug_info.getU8(offset_ptr);
101 }
David Blaikie82641be2015-11-17 00:39:55 +0000102 if (IndexEntry) {
103 if (AbbrOffset)
104 return false;
105 auto *UnitContrib = IndexEntry->getOffset();
106 if (!UnitContrib || UnitContrib->Length != (Length + 4))
107 return false;
108 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
109 if (!AbbrEntry)
110 return false;
111 AbbrOffset = AbbrEntry->Offset;
112 }
David Blaikie07e22442013-09-23 22:44:40 +0000113
Alexey Samsonov7682f812014-04-24 22:51:03 +0000114 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
115 bool VersionOK = DWARFContext::isSupportedVersion(Version);
116 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000117
Alexey Samsonov7682f812014-04-24 22:51:03 +0000118 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000119 return false;
120
Alexey Samsonov7682f812014-04-24 22:51:03 +0000121 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000122 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000123}
124
125bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
126 clear();
127
128 Offset = *offset_ptr;
129
130 if (debug_info.isValidOffset(*offset_ptr)) {
131 if (extractImpl(debug_info, offset_ptr))
132 return true;
133
134 // reset the offset to where we tried to parse from if anything went wrong
135 *offset_ptr = Offset;
136 }
137
138 return false;
139}
140
David Blaikie07e22442013-09-23 22:44:40 +0000141bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
142 DWARFDebugRangeList &RangeList) const {
143 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000144 assert(!DieArray.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000145 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
146 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
147 return RangeList.extract(RangesData, &ActualRangeListOffset);
148}
149
150void DWARFUnit::clear() {
151 Offset = 0;
152 Length = 0;
153 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000154 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000155 AddrSize = 0;
156 BaseAddr = 0;
157 RangeSectionBase = 0;
158 AddrOffsetSectionBase = 0;
159 clearDIEs(false);
160 DWO.reset();
161}
162
163const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000164 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000165}
166
Greg Clayton52fe1f62016-12-14 22:38:08 +0000167Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000168 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000169}
170
David Blaikie07e22442013-09-23 22:44:40 +0000171void DWARFUnit::extractDIEsToVector(
172 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000173 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000174 if (!AppendCUDie && !AppendNonCUDies)
175 return;
176
177 // Set the offset to that of the first DIE and calculate the start of the
178 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000179 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000180 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000181 DWARFDebugInfoEntry DIE;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000182 DataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000183 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000184 bool IsCUDie = true;
185
Greg Clayton78a07bf2016-12-21 21:37:06 +0000186 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
187 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000188 if (IsCUDie) {
189 if (AppendCUDie)
190 Dies.push_back(DIE);
191 if (!AppendNonCUDies)
192 break;
193 // The average bytes per DIE entry has been seen to be
194 // around 14-20 so let's pre-reserve the needed memory for
195 // our DIE entries accordingly.
196 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
197 IsCUDie = false;
198 } else {
199 Dies.push_back(DIE);
200 }
201
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000202 if (const DWARFAbbreviationDeclaration *AbbrDecl =
203 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000204 // Normal DIE
205 if (AbbrDecl->hasChildren())
206 ++Depth;
207 } else {
208 // NULL DIE.
209 if (Depth > 0)
210 --Depth;
211 if (Depth == 0)
212 break; // We are done with this compile unit!
213 }
214 }
215
216 // Give a little bit of info if we encounter corrupt DWARF (our offset
217 // should always terminate at or before the start of the next compilation
218 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000219 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000220 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000221 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000222}
223
224size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000225 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000226 DieArray.size() > 1)
227 return 0; // Already parsed.
228
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000229 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000230 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
231
232 if (DieArray.empty())
233 return 0;
234
235 // If CU DIE was just parsed, copy several attribute values from it.
236 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000237 DWARFDie UnitDie = getUnitDIE();
Greg Claytonc109bbe2017-01-13 22:32:12 +0000238 auto BaseAddr = toAddress(UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000239 if (BaseAddr)
240 setBaseAddress(*BaseAddr);
Greg Clayton97d22182017-01-13 21:08:18 +0000241 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
242 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000243 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
244 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000245 }
246
David Blaikie07e22442013-09-23 22:44:40 +0000247 return DieArray.size();
248}
249
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000250DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath) {
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000251 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000252 if (!Obj) {
253 // TODO: Actually report errors helpfully.
254 consumeError(Obj.takeError());
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000255 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000256 }
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000257 DWOFile = std::move(Obj.get());
258 DWOContext.reset(
Zachary Turner6489d7b2015-04-23 17:37:47 +0000259 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000260 if (DWOContext->getNumDWOCompileUnits() > 0)
261 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
262}
263
264bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000265 if (isDWO)
266 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000267 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000268 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000269 DWARFDie UnitDie = getUnitDIE();
270 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000271 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000272 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000273 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000274 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000275 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000276 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000277 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
278 *CompilationDir) {
279 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000280 }
Greg Clayton97d22182017-01-13 21:08:18 +0000281 sys::path::append(AbsolutePath, *DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000282 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000283 DWARFUnit *DWOCU = DWO->getUnit();
284 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000285 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000286 DWO.reset();
287 return false;
288 }
289 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
290 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000291 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
292 DWOCU->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000293 return true;
294}
295
296void DWARFUnit::clearDIEs(bool KeepCUDie) {
297 if (DieArray.size() > (unsigned)KeepCUDie) {
298 // std::vectors never get any smaller when resized to a smaller size,
299 // or when clear() or erase() are called, the size will report that it
300 // is smaller, but the memory allocated remains intact (call capacity()
301 // to see this). So we need to create a temporary vector and swap the
302 // contents which will cause just the internal pointers to be swapped
303 // so that when temporary vector goes out of scope, it will destroy the
304 // contents.
Greg Claytonc8c10322016-12-13 18:25:19 +0000305 std::vector<DWARFDebugInfoEntry> TmpArray;
David Blaikie07e22442013-09-23 22:44:40 +0000306 DieArray.swap(TmpArray);
307 // Save at least the compile unit DIE
308 if (KeepCUDie)
309 DieArray.push_back(TmpArray.front());
310 }
311}
312
Alexey Samsonov762343d2014-04-18 17:25:46 +0000313void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000314 DWARFDie UnitDie = getUnitDIE();
315 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000316 return;
317 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000318 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000319 if (!CUDIERanges.empty()) {
320 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
321 return;
322 }
323
David Blaikie07e22442013-09-23 22:44:40 +0000324 // This function is usually called if there in no .debug_aranges section
325 // in order to produce a compile unit level set of address ranges that
326 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
327 // all compile units to stay loaded when they weren't needed. So we can end
328 // up parsing the DWARF and then throwing them all away to keep memory usage
329 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000330 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000331 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000332
333 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000334 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000335 if (DWO.get())
336 DWO->getUnit()->collectAddressRanges(CURanges);
337 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000338 DWO.reset();
339
340 // Keep memory down by clearing DIEs if this generate function
341 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000342 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000343 clearDIEs(true);
344}
345
Greg Claytonc8c10322016-12-13 18:25:19 +0000346DWARFDie
David Blaikie07e22442013-09-23 22:44:40 +0000347DWARFUnit::getSubprogramForAddress(uint64_t Address) {
348 extractDIEsIfNeeded(false);
Greg Claytonc8c10322016-12-13 18:25:19 +0000349 for (const DWARFDebugInfoEntry &D : DieArray) {
350 DWARFDie DIE(this, &D);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000351 if (DIE.isSubprogramDIE() &&
Greg Claytonc8c10322016-12-13 18:25:19 +0000352 DIE.addressRangeContainsAddress(Address)) {
353 return DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000354 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000355 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000356 return DWARFDie();
David Blaikie07e22442013-09-23 22:44:40 +0000357}
358
Greg Claytonc8c10322016-12-13 18:25:19 +0000359void
360DWARFUnit::getInlinedChainForAddress(uint64_t Address,
361 SmallVectorImpl<DWARFDie> &InlinedChain) {
David Blaikie07e22442013-09-23 22:44:40 +0000362 // First, find a subprogram that contains the given address (the root
363 // of inlined chain).
Greg Claytonc8c10322016-12-13 18:25:19 +0000364 DWARFDie SubprogramDIE;
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000365 // Try to look for subprogram DIEs in the DWO file.
366 parseDWO();
Greg Claytonc8c10322016-12-13 18:25:19 +0000367 if (DWO)
368 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
369 else
370 SubprogramDIE = getSubprogramForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000371
372 // Get inlined chain rooted at this subprogram DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000373 if (SubprogramDIE)
374 SubprogramDIE.getInlinedChainForAddress(Address, InlinedChain);
375 else
376 InlinedChain.clear();
David Blaikie07e22442013-09-23 22:44:40 +0000377}
David Blaikie82641be2015-11-17 00:39:55 +0000378
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000379const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
380 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000381 if (Kind == DW_SECT_INFO)
382 return Context.getCUIndex();
383 assert(Kind == DW_SECT_TYPES);
384 return Context.getTUIndex();
385}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000386
Greg Clayton78a07bf2016-12-21 21:37:06 +0000387DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
388 if (!Die)
389 return DWARFDie();
390 const uint32_t Depth = Die->getDepth();
391 // Unit DIEs always have a depth of zero and never have parents.
392 if (Depth == 0)
393 return DWARFDie();
394 // Depth of 1 always means parent is the compile/type unit.
395 if (Depth == 1)
396 return getUnitDIE();
397 // Look for previous DIE with a depth that is one less than the Die's depth.
398 const uint32_t ParentDepth = Depth - 1;
399 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
400 if (DieArray[I].getDepth() == ParentDepth)
401 return DWARFDie(this, &DieArray[I]);
402 }
403 return DWARFDie();
404}
405
406DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
407 if (!Die)
408 return DWARFDie();
409 uint32_t Depth = Die->getDepth();
410 // Unit DIEs always have a depth of zero and never have siblings.
411 if (Depth == 0)
412 return DWARFDie();
413 // NULL DIEs don't have siblings.
414 if (Die->getAbbreviationDeclarationPtr() == nullptr)
415 return DWARFDie();
416
417 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000418 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
419 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000420 if (DieArray[I].getDepth() == Depth)
421 return DWARFDie(this, &DieArray[I]);
422 }
423 return DWARFDie();
424}