blob: e0dcd9b20b2a8079baa1af96e34bddbfda438fb5 [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"
Greg Clayton97d22182017-01-13 21:08:18 +000016#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000017#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/Casting.h"
20#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000021#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000022#include <algorithm>
23#include <cassert>
24#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000025#include <cstdio>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000027
David Blaikie82641be2015-11-17 00:39:55 +000028namespace llvm {
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029
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) {
33 parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
34 C.getStringSection(), StringRef(), C.getAddrSection(),
David Blaikiee438cff2016-04-22 22:50:56 +000035 C.getLineSection().Data, C.isLittleEndian(), false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000036}
37
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000038void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikie82641be2015-11-17 00:39:55 +000039 const DWARFSection &DWOSection,
40 DWARFUnitIndex *Index) {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000041 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000042 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
David Blaikiee438cff2016-04-22 22:50:56 +000043 C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
44 true);
Frederic Riss6005dbd2014-10-06 03:36:18 +000045}
46
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000047DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
48 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
David Blaikiec4e2bed2015-11-17 21:08:05 +000049 StringRef SOS, StringRef AOS, StringRef LS, bool LE,
David Blaikiee438cff2016-04-22 22:50:56 +000050 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000051 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000052 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
David Blaikie35c2eeb2015-11-17 22:39:23 +000053 LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
David Blaikie4689ef52015-11-17 22:39:26 +000054 if (IndexEntry)
55 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
56 return SOS.slice(C->Offset, C->Offset + C->Length);
David Blaikie35c2eeb2015-11-17 22:39:23 +000057 return SOS;
58 }()),
David Blaikiee438cff2016-04-22 22:50:56 +000059 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
60 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000061 clear();
62}
63
Eugene Zelenko570e39a2016-11-23 23:16:32 +000064DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000065
66bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
67 uint64_t &Result) const {
68 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
69 if (AddrOffsetSection.size() < Offset + AddrSize)
70 return false;
71 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
72 Result = DA.getAddress(&Offset);
73 return true;
74}
75
76bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
77 uint32_t &Result) const {
78 // FIXME: string offset section entries are 8-byte for DWARF64.
79 const uint32_t ItemSize = 4;
80 uint32_t Offset = Index * ItemSize;
81 if (StringOffsetSection.size() < Offset + ItemSize)
82 return false;
83 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
84 Result = DA.getU32(&Offset);
85 return true;
86}
87
88bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
89 Length = debug_info.getU32(offset_ptr);
90 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000091 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie82641be2015-11-17 00:39:55 +000092 if (IndexEntry) {
93 if (AbbrOffset)
94 return false;
95 auto *UnitContrib = IndexEntry->getOffset();
96 if (!UnitContrib || UnitContrib->Length != (Length + 4))
97 return false;
98 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
99 if (!AbbrEntry)
100 return false;
101 AbbrOffset = AbbrEntry->Offset;
102 }
David Blaikie07e22442013-09-23 22:44:40 +0000103 AddrSize = debug_info.getU8(offset_ptr);
104
Alexey Samsonov7682f812014-04-24 22:51:03 +0000105 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
106 bool VersionOK = DWARFContext::isSupportedVersion(Version);
107 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000108
Alexey Samsonov7682f812014-04-24 22:51:03 +0000109 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000110 return false;
111
Alexey Samsonov7682f812014-04-24 22:51:03 +0000112 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000113 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000114}
115
116bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
117 clear();
118
119 Offset = *offset_ptr;
120
121 if (debug_info.isValidOffset(*offset_ptr)) {
122 if (extractImpl(debug_info, offset_ptr))
123 return true;
124
125 // reset the offset to where we tried to parse from if anything went wrong
126 *offset_ptr = Offset;
127 }
128
129 return false;
130}
131
David Blaikie07e22442013-09-23 22:44:40 +0000132bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
133 DWARFDebugRangeList &RangeList) const {
134 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000135 assert(!DieArray.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000136 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
137 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
138 return RangeList.extract(RangesData, &ActualRangeListOffset);
139}
140
141void DWARFUnit::clear() {
142 Offset = 0;
143 Length = 0;
144 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000145 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000146 AddrSize = 0;
147 BaseAddr = 0;
148 RangeSectionBase = 0;
149 AddrOffsetSectionBase = 0;
150 clearDIEs(false);
151 DWO.reset();
152}
153
154const char *DWARFUnit::getCompilationDir() {
Greg Clayton97d22182017-01-13 21:08:18 +0000155 return toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000156}
157
Greg Clayton52fe1f62016-12-14 22:38:08 +0000158Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000159 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000160}
161
David Blaikie07e22442013-09-23 22:44:40 +0000162void DWARFUnit::extractDIEsToVector(
163 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000164 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000165 if (!AppendCUDie && !AppendNonCUDies)
166 return;
167
168 // Set the offset to that of the first DIE and calculate the start of the
169 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000170 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000171 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000172 DWARFDebugInfoEntry DIE;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000173 DataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000174 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000175 bool IsCUDie = true;
176
Greg Clayton78a07bf2016-12-21 21:37:06 +0000177 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
178 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000179 if (IsCUDie) {
180 if (AppendCUDie)
181 Dies.push_back(DIE);
182 if (!AppendNonCUDies)
183 break;
184 // The average bytes per DIE entry has been seen to be
185 // around 14-20 so let's pre-reserve the needed memory for
186 // our DIE entries accordingly.
187 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
188 IsCUDie = false;
189 } else {
190 Dies.push_back(DIE);
191 }
192
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000193 if (const DWARFAbbreviationDeclaration *AbbrDecl =
194 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000195 // Normal DIE
196 if (AbbrDecl->hasChildren())
197 ++Depth;
198 } else {
199 // NULL DIE.
200 if (Depth > 0)
201 --Depth;
202 if (Depth == 0)
203 break; // We are done with this compile unit!
204 }
205 }
206
207 // Give a little bit of info if we encounter corrupt DWARF (our offset
208 // should always terminate at or before the start of the next compilation
209 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000210 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000211 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000212 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000213}
214
215size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000216 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000217 DieArray.size() > 1)
218 return 0; // Already parsed.
219
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000220 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000221 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
222
223 if (DieArray.empty())
224 return 0;
225
226 // If CU DIE was just parsed, copy several attribute values from it.
227 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000228 DWARFDie UnitDie = getUnitDIE();
Greg Clayton97d22182017-01-13 21:08:18 +0000229 auto BaseAddr = toAddress(UnitDie.find(DW_AT_low_pc));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000230 if (!BaseAddr)
Greg Clayton97d22182017-01-13 21:08:18 +0000231 BaseAddr = toAddress(UnitDie.find(DW_AT_entry_pc));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000232 if (BaseAddr)
233 setBaseAddress(*BaseAddr);
Greg Clayton97d22182017-01-13 21:08:18 +0000234 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
235 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000236 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
237 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000238 }
239
David Blaikie07e22442013-09-23 22:44:40 +0000240 return DieArray.size();
241}
242
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000243DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000244 : DWOU(nullptr) {
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000245 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000246 if (!Obj) {
247 // TODO: Actually report errors helpfully.
248 consumeError(Obj.takeError());
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000249 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000250 }
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000251 DWOFile = std::move(Obj.get());
252 DWOContext.reset(
Zachary Turner6489d7b2015-04-23 17:37:47 +0000253 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000254 if (DWOContext->getNumDWOCompileUnits() > 0)
255 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
256}
257
258bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000259 if (isDWO)
260 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000261 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000262 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000263 DWARFDie UnitDie = getUnitDIE();
264 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000265 return false;
Greg Clayton97d22182017-01-13 21:08:18 +0000266 auto DWOFileName = toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000267 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000268 return false;
Greg Clayton97d22182017-01-13 21:08:18 +0000269 auto CompilationDir = toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000270 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000271 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
272 *CompilationDir) {
273 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000274 }
Greg Clayton97d22182017-01-13 21:08:18 +0000275 sys::path::append(AbsolutePath, *DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000276 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000277 DWARFUnit *DWOCU = DWO->getUnit();
278 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000279 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000280 DWO.reset();
281 return false;
282 }
283 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
284 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000285 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
286 DWOCU->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000287 return true;
288}
289
290void DWARFUnit::clearDIEs(bool KeepCUDie) {
291 if (DieArray.size() > (unsigned)KeepCUDie) {
292 // std::vectors never get any smaller when resized to a smaller size,
293 // or when clear() or erase() are called, the size will report that it
294 // is smaller, but the memory allocated remains intact (call capacity()
295 // to see this). So we need to create a temporary vector and swap the
296 // contents which will cause just the internal pointers to be swapped
297 // so that when temporary vector goes out of scope, it will destroy the
298 // contents.
Greg Claytonc8c10322016-12-13 18:25:19 +0000299 std::vector<DWARFDebugInfoEntry> TmpArray;
David Blaikie07e22442013-09-23 22:44:40 +0000300 DieArray.swap(TmpArray);
301 // Save at least the compile unit DIE
302 if (KeepCUDie)
303 DieArray.push_back(TmpArray.front());
304 }
305}
306
Alexey Samsonov762343d2014-04-18 17:25:46 +0000307void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000308 DWARFDie UnitDie = getUnitDIE();
309 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000310 return;
311 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000312 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000313 if (!CUDIERanges.empty()) {
314 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
315 return;
316 }
317
David Blaikie07e22442013-09-23 22:44:40 +0000318 // This function is usually called if there in no .debug_aranges section
319 // in order to produce a compile unit level set of address ranges that
320 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
321 // all compile units to stay loaded when they weren't needed. So we can end
322 // up parsing the DWARF and then throwing them all away to keep memory usage
323 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000324 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000325 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000326
327 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000328 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000329 if (DWO.get())
330 DWO->getUnit()->collectAddressRanges(CURanges);
331 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000332 DWO.reset();
333
334 // Keep memory down by clearing DIEs if this generate function
335 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000336 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000337 clearDIEs(true);
338}
339
Greg Claytonc8c10322016-12-13 18:25:19 +0000340DWARFDie
David Blaikie07e22442013-09-23 22:44:40 +0000341DWARFUnit::getSubprogramForAddress(uint64_t Address) {
342 extractDIEsIfNeeded(false);
Greg Claytonc8c10322016-12-13 18:25:19 +0000343 for (const DWARFDebugInfoEntry &D : DieArray) {
344 DWARFDie DIE(this, &D);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000345 if (DIE.isSubprogramDIE() &&
Greg Claytonc8c10322016-12-13 18:25:19 +0000346 DIE.addressRangeContainsAddress(Address)) {
347 return DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000348 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000349 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000350 return DWARFDie();
David Blaikie07e22442013-09-23 22:44:40 +0000351}
352
Greg Claytonc8c10322016-12-13 18:25:19 +0000353void
354DWARFUnit::getInlinedChainForAddress(uint64_t Address,
355 SmallVectorImpl<DWARFDie> &InlinedChain) {
David Blaikie07e22442013-09-23 22:44:40 +0000356 // First, find a subprogram that contains the given address (the root
357 // of inlined chain).
Greg Claytonc8c10322016-12-13 18:25:19 +0000358 DWARFDie SubprogramDIE;
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000359 // Try to look for subprogram DIEs in the DWO file.
360 parseDWO();
Greg Claytonc8c10322016-12-13 18:25:19 +0000361 if (DWO)
362 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
363 else
364 SubprogramDIE = getSubprogramForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000365
366 // Get inlined chain rooted at this subprogram DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000367 if (SubprogramDIE)
368 SubprogramDIE.getInlinedChainForAddress(Address, InlinedChain);
369 else
370 InlinedChain.clear();
David Blaikie07e22442013-09-23 22:44:40 +0000371}
David Blaikie82641be2015-11-17 00:39:55 +0000372
373const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
374 DWARFSectionKind Kind) {
375 if (Kind == DW_SECT_INFO)
376 return Context.getCUIndex();
377 assert(Kind == DW_SECT_TYPES);
378 return Context.getTUIndex();
379}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000380
Greg Clayton78a07bf2016-12-21 21:37:06 +0000381DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
382 if (!Die)
383 return DWARFDie();
384 const uint32_t Depth = Die->getDepth();
385 // Unit DIEs always have a depth of zero and never have parents.
386 if (Depth == 0)
387 return DWARFDie();
388 // Depth of 1 always means parent is the compile/type unit.
389 if (Depth == 1)
390 return getUnitDIE();
391 // Look for previous DIE with a depth that is one less than the Die's depth.
392 const uint32_t ParentDepth = Depth - 1;
393 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
394 if (DieArray[I].getDepth() == ParentDepth)
395 return DWARFDie(this, &DieArray[I]);
396 }
397 return DWARFDie();
398}
399
400DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
401 if (!Die)
402 return DWARFDie();
403 uint32_t Depth = Die->getDepth();
404 // Unit DIEs always have a depth of zero and never have siblings.
405 if (Depth == 0)
406 return DWARFDie();
407 // NULL DIEs don't have siblings.
408 if (Die->getAbbreviationDeclarationPtr() == nullptr)
409 return DWARFDie();
410
411 // Find the next DIE whose depth is the same as the Die's depth.
412 for (size_t I=getDIEIndex(Die)+1, EndIdx = DieArray.size(); I<EndIdx; ++I) {
413 if (DieArray[I].getDepth() == Depth)
414 return DWARFDie(this, &DieArray[I]);
415 }
416 return DWARFDie();
417}
418
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000419} // end namespace llvm