blob: fd9c7c2b1d46ca0dd6c2c6b756805f84b7683d44 [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) {
George Rimarca532112017-04-24 10:19:45 +000033 parseImpl(C, Section, C.getDebugAbbrev(), &C.getRangeSection(),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000034 C.getStringSection(), C.getStringOffsetSection(),
35 &C.getAddrSection(), C.getLineSection().Data, C.isLittleEndian(),
36 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) {
George Rimarca532112017-04-24 10:19:45 +000042 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &C.getRangeDWOSection(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000043 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
David Blaikied2f3a942017-05-22 07:02:47 +000044 &C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
David Blaikiee438cff2016-04-22 22:50:56 +000045 true);
Frederic Riss6005dbd2014-10-06 03:36:18 +000046}
47
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000048DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
George Rimarca532112017-04-24 10:19:45 +000049 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000050 StringRef SS, const DWARFSection &SOS,
51 const DWARFSection *AOS, StringRef LS, bool LE, bool IsDWO,
George Rimarca532112017-04-24 10:19:45 +000052 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),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000055 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000056 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
57 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000058 clear();
59}
60
Eugene Zelenko570e39a2016-11-23 23:16:32 +000061DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000062
63bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
64 uint64_t &Result) const {
Paul Robinson75c068c2017-06-26 18:43:01 +000065 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
66 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie07e22442013-09-23 22:44:40 +000067 return false;
Paul Robinson75c068c2017-06-26 18:43:01 +000068 DataExtractor DA(AddrOffsetSection->Data, isLittleEndian,
69 getAddressByteSize());
70 Result = getRelocatedValue(DA, getAddressByteSize(), &Offset,
71 &AddrOffsetSection->Relocs);
David Blaikie07e22442013-09-23 22:44:40 +000072 return true;
73}
74
75bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000076 uint64_t &Result) const {
77 unsigned ItemSize = getFormat() == DWARF64 ? 8 : 4;
78 uint32_t Offset = StringOffsetSectionBase + Index * ItemSize;
79 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +000080 return false;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000081 DataExtractor DA(StringOffsetSection.Data, isLittleEndian, 0);
82 Result = ItemSize == 4 ? DA.getU32(&Offset) : DA.getU64(&Offset);
David Blaikie07e22442013-09-23 22:44:40 +000083 return true;
84}
85
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000086uint64_t DWARFUnit::getStringOffsetSectionRelocation(uint32_t Index) const {
87 unsigned ItemSize = getFormat() == DWARF64 ? 8 : 4;
88 uint64_t ByteOffset = StringOffsetSectionBase + Index * ItemSize;
89 RelocAddrMap::const_iterator AI = getStringOffsetsRelocMap().find(ByteOffset);
90 if (AI != getStringOffsetsRelocMap().end())
91 return AI->second.Value;
92 return 0;
93}
94
David Blaikie07e22442013-09-23 22:44:40 +000095bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
96 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000097 // FIXME: Support DWARF64.
98 FormParams.Format = DWARF32;
99 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000100 uint64_t AbbrOffset;
Paul Robinson75c068c2017-06-26 18:43:01 +0000101 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +0000102 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000103 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000104 AbbrOffset = debug_info.getU32(offset_ptr);
105 } else {
106 AbbrOffset = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000107 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000108 }
David Blaikie82641be2015-11-17 00:39:55 +0000109 if (IndexEntry) {
110 if (AbbrOffset)
111 return false;
112 auto *UnitContrib = IndexEntry->getOffset();
113 if (!UnitContrib || UnitContrib->Length != (Length + 4))
114 return false;
115 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
116 if (!AbbrEntry)
117 return false;
118 AbbrOffset = AbbrEntry->Offset;
119 }
David Blaikie07e22442013-09-23 22:44:40 +0000120
Alexey Samsonov7682f812014-04-24 22:51:03 +0000121 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000122 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
123 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000124
Alexey Samsonov7682f812014-04-24 22:51:03 +0000125 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000126 return false;
127
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000128 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000129 Context.setMaxVersionIfGreater(getVersion());
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000130
Alexey Samsonov7682f812014-04-24 22:51:03 +0000131 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000132 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000133}
134
135bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
136 clear();
137
138 Offset = *offset_ptr;
139
140 if (debug_info.isValidOffset(*offset_ptr)) {
141 if (extractImpl(debug_info, offset_ptr))
142 return true;
143
144 // reset the offset to where we tried to parse from if anything went wrong
145 *offset_ptr = Offset;
146 }
147
148 return false;
149}
150
David Blaikie07e22442013-09-23 22:44:40 +0000151bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
152 DWARFDebugRangeList &RangeList) const {
153 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000154 assert(!DieArray.empty());
Paul Robinson75c068c2017-06-26 18:43:01 +0000155 DataExtractor RangesData(RangeSection->Data, isLittleEndian,
156 getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000157 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
George Rimarca532112017-04-24 10:19:45 +0000158 return RangeList.extract(RangesData, &ActualRangeListOffset,
159 RangeSection->Relocs);
David Blaikie07e22442013-09-23 22:44:40 +0000160}
161
162void DWARFUnit::clear() {
163 Offset = 0;
164 Length = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000165 Abbrevs = nullptr;
Paul Robinson75c068c2017-06-26 18:43:01 +0000166 FormParams = DWARFFormParams({0, 0, DWARF32});
David Blaikie07e22442013-09-23 22:44:40 +0000167 BaseAddr = 0;
168 RangeSectionBase = 0;
169 AddrOffsetSectionBase = 0;
170 clearDIEs(false);
171 DWO.reset();
172}
173
174const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000175 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000176}
177
Greg Clayton52fe1f62016-12-14 22:38:08 +0000178Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000179 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000180}
181
David Blaikie07e22442013-09-23 22:44:40 +0000182void DWARFUnit::extractDIEsToVector(
183 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000184 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000185 if (!AppendCUDie && !AppendNonCUDies)
186 return;
187
188 // Set the offset to that of the first DIE and calculate the start of the
189 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000190 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000191 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000192 DWARFDebugInfoEntry DIE;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000193 DataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000194 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000195 bool IsCUDie = true;
196
Greg Clayton78a07bf2016-12-21 21:37:06 +0000197 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
198 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000199 if (IsCUDie) {
200 if (AppendCUDie)
201 Dies.push_back(DIE);
202 if (!AppendNonCUDies)
203 break;
204 // The average bytes per DIE entry has been seen to be
205 // around 14-20 so let's pre-reserve the needed memory for
206 // our DIE entries accordingly.
207 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
208 IsCUDie = false;
209 } else {
210 Dies.push_back(DIE);
211 }
212
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000213 if (const DWARFAbbreviationDeclaration *AbbrDecl =
214 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000215 // Normal DIE
216 if (AbbrDecl->hasChildren())
217 ++Depth;
218 } else {
219 // NULL DIE.
220 if (Depth > 0)
221 --Depth;
222 if (Depth == 0)
223 break; // We are done with this compile unit!
224 }
225 }
226
227 // Give a little bit of info if we encounter corrupt DWARF (our offset
228 // should always terminate at or before the start of the next compilation
229 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000230 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000231 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000232 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000233}
234
235size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000236 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000237 DieArray.size() > 1)
238 return 0; // Already parsed.
239
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000240 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000241 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
242
243 if (DieArray.empty())
244 return 0;
245
246 // If CU DIE was just parsed, copy several attribute values from it.
247 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000248 DWARFDie UnitDie = getUnitDIE();
Greg Claytonc109bbe2017-01-13 22:32:12 +0000249 auto BaseAddr = toAddress(UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000250 if (BaseAddr)
251 setBaseAddress(*BaseAddr);
Greg Clayton97d22182017-01-13 21:08:18 +0000252 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
253 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000254
255 // In general, we derive the offset of the unit's contibution to the
256 // debug_str_offsets{.dwo} section from the unit DIE's
257 // DW_AT_str_offsets_base attribute. In dwp files we add to it the offset
258 // we get from the index table.
259 StringOffsetSectionBase =
260 toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
261 if (IndexEntry)
262 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
263 StringOffsetSectionBase += C->Offset;
264
Alexey Samsonovaa909982014-06-13 22:31:03 +0000265 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
266 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000267 }
268
David Blaikie07e22442013-09-23 22:44:40 +0000269 return DieArray.size();
270}
271
David Blaikie07e22442013-09-23 22:44:40 +0000272bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000273 if (isDWO)
274 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000275 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000276 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000277 DWARFDie UnitDie = getUnitDIE();
278 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000279 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000280 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000281 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000282 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000283 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000284 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000285 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
286 *CompilationDir) {
287 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000288 }
Greg Clayton97d22182017-01-13 21:08:18 +0000289 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000290 auto DWOId = getDWOId();
291 if (!DWOId)
292 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000293 auto DWOContext = Context.getDWOContext(AbsolutePath);
294 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000295 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000296
David Blaikie15d85fc2017-05-23 06:48:53 +0000297 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
298 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000299 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000300 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000301 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000302 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000303 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000304 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000305 return true;
306}
307
308void DWARFUnit::clearDIEs(bool KeepCUDie) {
309 if (DieArray.size() > (unsigned)KeepCUDie) {
310 // std::vectors never get any smaller when resized to a smaller size,
311 // or when clear() or erase() are called, the size will report that it
312 // is smaller, but the memory allocated remains intact (call capacity()
313 // to see this). So we need to create a temporary vector and swap the
314 // contents which will cause just the internal pointers to be swapped
315 // so that when temporary vector goes out of scope, it will destroy the
316 // contents.
Greg Claytonc8c10322016-12-13 18:25:19 +0000317 std::vector<DWARFDebugInfoEntry> TmpArray;
David Blaikie07e22442013-09-23 22:44:40 +0000318 DieArray.swap(TmpArray);
319 // Save at least the compile unit DIE
320 if (KeepCUDie)
321 DieArray.push_back(TmpArray.front());
322 }
323}
324
Alexey Samsonov762343d2014-04-18 17:25:46 +0000325void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000326 DWARFDie UnitDie = getUnitDIE();
327 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000328 return;
329 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000330 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000331 if (!CUDIERanges.empty()) {
332 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
333 return;
334 }
335
David Blaikie07e22442013-09-23 22:44:40 +0000336 // This function is usually called if there in no .debug_aranges section
337 // in order to produce a compile unit level set of address ranges that
338 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
339 // all compile units to stay loaded when they weren't needed. So we can end
340 // up parsing the DWARF and then throwing them all away to keep memory usage
341 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000342 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000343 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000344
345 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000346 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000347 if (DWO)
348 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000349 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000350 DWO.reset();
351
352 // Keep memory down by clearing DIEs if this generate function
353 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000354 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000355 clearDIEs(true);
356}
357
Dehao Chena364f092017-04-19 20:09:38 +0000358void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
359 if (Die.isSubroutineDIE()) {
360 for (const auto &R : Die.getAddressRanges()) {
361 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000362 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000363 continue;
George Rimar4671f2e2017-05-16 12:30:59 +0000364 auto B = AddrDieMap.upper_bound(R.LowPC);
365 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
Dehao Chena364f092017-04-19 20:09:38 +0000366 // The range is a sub-range of existing ranges, we need to split the
367 // existing range.
George Rimar4671f2e2017-05-16 12:30:59 +0000368 if (R.HighPC < B->second.first)
369 AddrDieMap[R.HighPC] = B->second;
370 if (R.LowPC > B->first)
371 AddrDieMap[B->first].first = R.LowPC;
Dehao Chena364f092017-04-19 20:09:38 +0000372 }
George Rimar4671f2e2017-05-16 12:30:59 +0000373 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
David Blaikie07e22442013-09-23 22:44:40 +0000374 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000375 }
Dehao Chena364f092017-04-19 20:09:38 +0000376 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
377 // simplify the logic to update AddrDieMap. The child's range will always
378 // be equal or smaller than the parent's range. With this assumption, when
379 // adding one range into the map, it will at most split a range into 3
380 // sub-ranges.
381 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
382 updateAddressDieMap(Child);
383}
384
385DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
386 extractDIEsIfNeeded(false);
387 if (AddrDieMap.empty())
388 updateAddressDieMap(getUnitDIE());
389 auto R = AddrDieMap.upper_bound(Address);
390 if (R == AddrDieMap.begin())
391 return DWARFDie();
392 // upper_bound's previous item contains Address.
393 --R;
394 if (Address >= R->second.first)
395 return DWARFDie();
396 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000397}
398
Greg Claytonc8c10322016-12-13 18:25:19 +0000399void
400DWARFUnit::getInlinedChainForAddress(uint64_t Address,
401 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000402 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000403 // Try to look for subprogram DIEs in the DWO file.
404 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000405 // First, find the subroutine that contains the given address (the leaf
406 // of inlined chain).
407 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000408 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000409
Dehao Chendb569ba2017-04-19 20:52:21 +0000410 while (SubroutineDIE) {
411 if (SubroutineDIE.isSubroutineDIE())
412 InlinedChain.push_back(SubroutineDIE);
413 SubroutineDIE = SubroutineDIE.getParent();
414 }
David Blaikie07e22442013-09-23 22:44:40 +0000415}
David Blaikie82641be2015-11-17 00:39:55 +0000416
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000417const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
418 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000419 if (Kind == DW_SECT_INFO)
420 return Context.getCUIndex();
421 assert(Kind == DW_SECT_TYPES);
422 return Context.getTUIndex();
423}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000424
Greg Clayton78a07bf2016-12-21 21:37:06 +0000425DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
426 if (!Die)
427 return DWARFDie();
428 const uint32_t Depth = Die->getDepth();
429 // Unit DIEs always have a depth of zero and never have parents.
430 if (Depth == 0)
431 return DWARFDie();
432 // Depth of 1 always means parent is the compile/type unit.
433 if (Depth == 1)
434 return getUnitDIE();
435 // Look for previous DIE with a depth that is one less than the Die's depth.
436 const uint32_t ParentDepth = Depth - 1;
437 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
438 if (DieArray[I].getDepth() == ParentDepth)
439 return DWARFDie(this, &DieArray[I]);
440 }
441 return DWARFDie();
442}
443
444DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
445 if (!Die)
446 return DWARFDie();
447 uint32_t Depth = Die->getDepth();
448 // Unit DIEs always have a depth of zero and never have siblings.
449 if (Depth == 0)
450 return DWARFDie();
451 // NULL DIEs don't have siblings.
452 if (Die->getAbbreviationDeclarationPtr() == nullptr)
453 return DWARFDie();
454
455 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000456 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
457 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000458 if (DieArray[I].getDepth() == Depth)
459 return DWARFDie(this, &DieArray[I]);
460 }
461 return DWARFDie();
462}