blob: 86451faa79deb6b46031aaa86918de9aa7f2add7 [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(),
David Blaikiee79dda32017-09-19 18:36:11 +000036 &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false,
37 false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000038}
39
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000040void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikiee79dda32017-09-19 18:36:11 +000041 const DWARFSection &DWOSection, bool Lazy) {
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 Blaikiee79dda32017-09-19 18:36:11 +000046 true, Lazy);
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 Robinson75c068c2017-06-26 18:43:01 +000097 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +000098 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000099 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000100 AbbrOffset = debug_info.getU32(offset_ptr);
101 } else {
102 AbbrOffset = debug_info.getU32(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 }
David Blaikie82641be2015-11-17 00:39:55 +0000105 if (IndexEntry) {
106 if (AbbrOffset)
107 return false;
108 auto *UnitContrib = IndexEntry->getOffset();
109 if (!UnitContrib || UnitContrib->Length != (Length + 4))
110 return false;
111 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
112 if (!AbbrEntry)
113 return false;
114 AbbrOffset = AbbrEntry->Offset;
115 }
David Blaikie07e22442013-09-23 22:44:40 +0000116
Alexey Samsonov7682f812014-04-24 22:51:03 +0000117 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000118 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
119 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000120
Alexey Samsonov7682f812014-04-24 22:51:03 +0000121 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000122 return false;
123
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000124 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000125 Context.setMaxVersionIfGreater(getVersion());
David Blaikie485e01b2017-09-19 15:13:55 +0000126 return true;
David Blaikie07e22442013-09-23 22:44:40 +0000127}
128
129bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
130 clear();
131
132 Offset = *offset_ptr;
133
134 if (debug_info.isValidOffset(*offset_ptr)) {
135 if (extractImpl(debug_info, offset_ptr))
136 return true;
137
138 // reset the offset to where we tried to parse from if anything went wrong
139 *offset_ptr = Offset;
140 }
141
142 return false;
143}
144
David Blaikie07e22442013-09-23 22:44:40 +0000145bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
Paul Robinson17536b92017-06-29 16:52:08 +0000146 DWARFDebugRangeList &RangeList) const {
David Blaikie07e22442013-09-23 22:44:40 +0000147 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000148 assert(!DieArray.empty());
Rafael Espindolac398e672017-07-19 22:27:28 +0000149 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
150 isLittleEndian, getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000151 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000152 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000153}
154
155void DWARFUnit::clear() {
156 Offset = 0;
157 Length = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000158 Abbrevs = nullptr;
Paul Robinson75c068c2017-06-26 18:43:01 +0000159 FormParams = DWARFFormParams({0, 0, DWARF32});
George Rimar2f95c8b2017-09-04 10:30:39 +0000160 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000161 RangeSectionBase = 0;
162 AddrOffsetSectionBase = 0;
163 clearDIEs(false);
164 DWO.reset();
165}
166
167const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000168 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000169}
170
Greg Clayton52fe1f62016-12-14 22:38:08 +0000171Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000172 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000173}
174
David Blaikie07e22442013-09-23 22:44:40 +0000175void DWARFUnit::extractDIEsToVector(
176 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000177 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000178 if (!AppendCUDie && !AppendNonCUDies)
179 return;
180
181 // Set the offset to that of the first DIE and calculate the start of the
182 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000183 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000184 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000185 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000186 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000187 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000188 bool IsCUDie = true;
189
Greg Clayton78a07bf2016-12-21 21:37:06 +0000190 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
191 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000192 if (IsCUDie) {
193 if (AppendCUDie)
194 Dies.push_back(DIE);
195 if (!AppendNonCUDies)
196 break;
197 // The average bytes per DIE entry has been seen to be
198 // around 14-20 so let's pre-reserve the needed memory for
199 // our DIE entries accordingly.
200 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
201 IsCUDie = false;
202 } else {
203 Dies.push_back(DIE);
204 }
205
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000206 if (const DWARFAbbreviationDeclaration *AbbrDecl =
207 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000208 // Normal DIE
209 if (AbbrDecl->hasChildren())
210 ++Depth;
211 } else {
212 // NULL DIE.
213 if (Depth > 0)
214 --Depth;
215 if (Depth == 0)
216 break; // We are done with this compile unit!
217 }
218 }
219
220 // Give a little bit of info if we encounter corrupt DWARF (our offset
221 // should always terminate at or before the start of the next compilation
222 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000223 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000224 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000225 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000226}
227
228size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000229 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000230 DieArray.size() > 1)
231 return 0; // Already parsed.
232
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000233 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000234 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
235
236 if (DieArray.empty())
237 return 0;
238
239 // If CU DIE was just parsed, copy several attribute values from it.
240 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000241 DWARFDie UnitDie = getUnitDIE();
George Rimar2f95c8b2017-09-04 10:30:39 +0000242 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
243 if (Optional<uint64_t> Addr = toAddress(PC))
244 setBaseAddress({*Addr, PC->getSectionIndex()});
245
David Blaikie22dc4472017-08-02 20:16:22 +0000246 if (!isDWO) {
247 assert(AddrOffsetSectionBase == 0);
248 assert(RangeSectionBase == 0);
249 AddrOffsetSectionBase =
250 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
251 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
252 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000253
254 // In general, we derive the offset of the unit's contibution to the
255 // debug_str_offsets{.dwo} section from the unit DIE's
256 // DW_AT_str_offsets_base attribute. In dwp files we add to it the offset
257 // we get from the index table.
258 StringOffsetSectionBase =
259 toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
260 if (IndexEntry)
261 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
262 StringOffsetSectionBase += C->Offset;
263
Alexey Samsonovaa909982014-06-13 22:31:03 +0000264 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
265 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000266 }
267
David Blaikie07e22442013-09-23 22:44:40 +0000268 return DieArray.size();
269}
270
David Blaikie07e22442013-09-23 22:44:40 +0000271bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000272 if (isDWO)
273 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000274 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000275 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000276 DWARFDie UnitDie = getUnitDIE();
277 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000278 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000279 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000280 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000281 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000282 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000283 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000284 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
285 *CompilationDir) {
286 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000287 }
Greg Clayton97d22182017-01-13 21:08:18 +0000288 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000289 auto DWOId = getDWOId();
290 if (!DWOId)
291 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000292 auto DWOContext = Context.getDWOContext(AbsolutePath);
293 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000294 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000295
David Blaikie15d85fc2017-05-23 06:48:53 +0000296 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
297 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000298 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000299 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000300 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000301 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000302 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000303 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000304 return true;
305}
306
307void DWARFUnit::clearDIEs(bool KeepCUDie) {
308 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000309 DieArray.resize((unsigned)KeepCUDie);
310 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000311 }
312}
313
Alexey Samsonov762343d2014-04-18 17:25:46 +0000314void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000315 DWARFDie UnitDie = getUnitDIE();
316 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000317 return;
318 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000319 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000320 if (!CUDIERanges.empty()) {
321 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
322 return;
323 }
324
David Blaikie07e22442013-09-23 22:44:40 +0000325 // This function is usually called if there in no .debug_aranges section
326 // in order to produce a compile unit level set of address ranges that
327 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
328 // all compile units to stay loaded when they weren't needed. So we can end
329 // up parsing the DWARF and then throwing them all away to keep memory usage
330 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000331 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000332 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000333
334 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000335 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000336 if (DWO)
337 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000338 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000339 DWO.reset();
340
341 // Keep memory down by clearing DIEs if this generate function
342 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000343 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000344 clearDIEs(true);
345}
346
Dehao Chena364f092017-04-19 20:09:38 +0000347void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
348 if (Die.isSubroutineDIE()) {
349 for (const auto &R : Die.getAddressRanges()) {
350 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000351 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000352 continue;
George Rimar4671f2e2017-05-16 12:30:59 +0000353 auto B = AddrDieMap.upper_bound(R.LowPC);
354 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
Dehao Chena364f092017-04-19 20:09:38 +0000355 // The range is a sub-range of existing ranges, we need to split the
356 // existing range.
George Rimar4671f2e2017-05-16 12:30:59 +0000357 if (R.HighPC < B->second.first)
358 AddrDieMap[R.HighPC] = B->second;
359 if (R.LowPC > B->first)
360 AddrDieMap[B->first].first = R.LowPC;
Dehao Chena364f092017-04-19 20:09:38 +0000361 }
George Rimar4671f2e2017-05-16 12:30:59 +0000362 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
David Blaikie07e22442013-09-23 22:44:40 +0000363 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000364 }
Dehao Chena364f092017-04-19 20:09:38 +0000365 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
366 // simplify the logic to update AddrDieMap. The child's range will always
367 // be equal or smaller than the parent's range. With this assumption, when
368 // adding one range into the map, it will at most split a range into 3
369 // sub-ranges.
370 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
371 updateAddressDieMap(Child);
372}
373
374DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
375 extractDIEsIfNeeded(false);
376 if (AddrDieMap.empty())
377 updateAddressDieMap(getUnitDIE());
378 auto R = AddrDieMap.upper_bound(Address);
379 if (R == AddrDieMap.begin())
380 return DWARFDie();
381 // upper_bound's previous item contains Address.
382 --R;
383 if (Address >= R->second.first)
384 return DWARFDie();
385 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000386}
387
Greg Claytonc8c10322016-12-13 18:25:19 +0000388void
389DWARFUnit::getInlinedChainForAddress(uint64_t Address,
390 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000391 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000392 // Try to look for subprogram DIEs in the DWO file.
393 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000394 // First, find the subroutine that contains the given address (the leaf
395 // of inlined chain).
396 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000397 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000398
Dehao Chendb569ba2017-04-19 20:52:21 +0000399 while (SubroutineDIE) {
400 if (SubroutineDIE.isSubroutineDIE())
401 InlinedChain.push_back(SubroutineDIE);
402 SubroutineDIE = SubroutineDIE.getParent();
403 }
David Blaikie07e22442013-09-23 22:44:40 +0000404}
David Blaikie82641be2015-11-17 00:39:55 +0000405
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000406const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
407 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000408 if (Kind == DW_SECT_INFO)
409 return Context.getCUIndex();
410 assert(Kind == DW_SECT_TYPES);
411 return Context.getTUIndex();
412}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000413
Greg Clayton78a07bf2016-12-21 21:37:06 +0000414DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
415 if (!Die)
416 return DWARFDie();
417 const uint32_t Depth = Die->getDepth();
418 // Unit DIEs always have a depth of zero and never have parents.
419 if (Depth == 0)
420 return DWARFDie();
421 // Depth of 1 always means parent is the compile/type unit.
422 if (Depth == 1)
423 return getUnitDIE();
424 // Look for previous DIE with a depth that is one less than the Die's depth.
425 const uint32_t ParentDepth = Depth - 1;
426 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
427 if (DieArray[I].getDepth() == ParentDepth)
428 return DWARFDie(this, &DieArray[I]);
429 }
430 return DWARFDie();
431}
432
433DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
434 if (!Die)
435 return DWARFDie();
436 uint32_t Depth = Die->getDepth();
437 // Unit DIEs always have a depth of zero and never have siblings.
438 if (Depth == 0)
439 return DWARFDie();
440 // NULL DIEs don't have siblings.
441 if (Die->getAbbreviationDeclarationPtr() == nullptr)
442 return DWARFDie();
Hans Wennborgab2177e2017-10-03 18:39:13 +0000443
Greg Clayton78a07bf2016-12-21 21:37:06 +0000444 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000445 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
446 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000447 if (DieArray[I].getDepth() == Depth)
448 return DWARFDie(this, &DieArray[I]);
449 }
450 return DWARFDie();
451}
David Blaikie485e01b2017-09-19 15:13:55 +0000452
453const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
454 if (!Abbrevs)
455 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
456 return Abbrevs;
457}