blob: 813960ca95d783e5ebcf45f0c66a8ee2449462d6 [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});
George Rimar2f95c8b2017-09-04 10:30:39 +0000163 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000164 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();
George Rimar2f95c8b2017-09-04 10:30:39 +0000245 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
246 if (Optional<uint64_t> Addr = toAddress(PC))
247 setBaseAddress({*Addr, PC->getSectionIndex()});
248
David Blaikie22dc4472017-08-02 20:16:22 +0000249 if (!isDWO) {
250 assert(AddrOffsetSectionBase == 0);
251 assert(RangeSectionBase == 0);
252 AddrOffsetSectionBase =
253 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
254 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
255 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000256
257 // In general, we derive the offset of the unit's contibution to the
258 // debug_str_offsets{.dwo} section from the unit DIE's
259 // DW_AT_str_offsets_base attribute. In dwp files we add to it the offset
260 // we get from the index table.
261 StringOffsetSectionBase =
262 toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
263 if (IndexEntry)
264 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
265 StringOffsetSectionBase += C->Offset;
266
Alexey Samsonovaa909982014-06-13 22:31:03 +0000267 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
268 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000269 }
270
David Blaikie07e22442013-09-23 22:44:40 +0000271 return DieArray.size();
272}
273
David Blaikie07e22442013-09-23 22:44:40 +0000274bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000275 if (isDWO)
276 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000277 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000278 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000279 DWARFDie UnitDie = getUnitDIE();
280 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000281 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000282 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000283 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000284 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000285 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000286 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000287 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
288 *CompilationDir) {
289 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000290 }
Greg Clayton97d22182017-01-13 21:08:18 +0000291 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000292 auto DWOId = getDWOId();
293 if (!DWOId)
294 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000295 auto DWOContext = Context.getDWOContext(AbsolutePath);
296 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000297 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000298
David Blaikie15d85fc2017-05-23 06:48:53 +0000299 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
300 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000301 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000302 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000303 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000304 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000305 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000306 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000307 return true;
308}
309
310void DWARFUnit::clearDIEs(bool KeepCUDie) {
311 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000312 DieArray.resize((unsigned)KeepCUDie);
313 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000314 }
315}
316
Alexey Samsonov762343d2014-04-18 17:25:46 +0000317void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000318 DWARFDie UnitDie = getUnitDIE();
319 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000320 return;
321 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000322 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000323 if (!CUDIERanges.empty()) {
324 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
325 return;
326 }
327
David Blaikie07e22442013-09-23 22:44:40 +0000328 // This function is usually called if there in no .debug_aranges section
329 // in order to produce a compile unit level set of address ranges that
330 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
331 // all compile units to stay loaded when they weren't needed. So we can end
332 // up parsing the DWARF and then throwing them all away to keep memory usage
333 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000334 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000335 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000336
337 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000338 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000339 if (DWO)
340 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000341 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000342 DWO.reset();
343
344 // Keep memory down by clearing DIEs if this generate function
345 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000346 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000347 clearDIEs(true);
348}
349
Dehao Chena364f092017-04-19 20:09:38 +0000350void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
351 if (Die.isSubroutineDIE()) {
352 for (const auto &R : Die.getAddressRanges()) {
353 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000354 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000355 continue;
George Rimar4671f2e2017-05-16 12:30:59 +0000356 auto B = AddrDieMap.upper_bound(R.LowPC);
357 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
Dehao Chena364f092017-04-19 20:09:38 +0000358 // The range is a sub-range of existing ranges, we need to split the
359 // existing range.
George Rimar4671f2e2017-05-16 12:30:59 +0000360 if (R.HighPC < B->second.first)
361 AddrDieMap[R.HighPC] = B->second;
362 if (R.LowPC > B->first)
363 AddrDieMap[B->first].first = R.LowPC;
Dehao Chena364f092017-04-19 20:09:38 +0000364 }
George Rimar4671f2e2017-05-16 12:30:59 +0000365 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
David Blaikie07e22442013-09-23 22:44:40 +0000366 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000367 }
Dehao Chena364f092017-04-19 20:09:38 +0000368 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
369 // simplify the logic to update AddrDieMap. The child's range will always
370 // be equal or smaller than the parent's range. With this assumption, when
371 // adding one range into the map, it will at most split a range into 3
372 // sub-ranges.
373 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
374 updateAddressDieMap(Child);
375}
376
377DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
378 extractDIEsIfNeeded(false);
379 if (AddrDieMap.empty())
380 updateAddressDieMap(getUnitDIE());
381 auto R = AddrDieMap.upper_bound(Address);
382 if (R == AddrDieMap.begin())
383 return DWARFDie();
384 // upper_bound's previous item contains Address.
385 --R;
386 if (Address >= R->second.first)
387 return DWARFDie();
388 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000389}
390
Greg Claytonc8c10322016-12-13 18:25:19 +0000391void
392DWARFUnit::getInlinedChainForAddress(uint64_t Address,
393 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000394 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000395 // Try to look for subprogram DIEs in the DWO file.
396 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000397 // First, find the subroutine that contains the given address (the leaf
398 // of inlined chain).
399 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000400 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000401
Dehao Chendb569ba2017-04-19 20:52:21 +0000402 while (SubroutineDIE) {
403 if (SubroutineDIE.isSubroutineDIE())
404 InlinedChain.push_back(SubroutineDIE);
405 SubroutineDIE = SubroutineDIE.getParent();
406 }
David Blaikie07e22442013-09-23 22:44:40 +0000407}
David Blaikie82641be2015-11-17 00:39:55 +0000408
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000409const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
410 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000411 if (Kind == DW_SECT_INFO)
412 return Context.getCUIndex();
413 assert(Kind == DW_SECT_TYPES);
414 return Context.getTUIndex();
415}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000416
Greg Clayton78a07bf2016-12-21 21:37:06 +0000417DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
418 if (!Die)
419 return DWARFDie();
420 const uint32_t Depth = Die->getDepth();
421 // Unit DIEs always have a depth of zero and never have parents.
422 if (Depth == 0)
423 return DWARFDie();
424 // Depth of 1 always means parent is the compile/type unit.
425 if (Depth == 1)
426 return getUnitDIE();
427 // Look for previous DIE with a depth that is one less than the Die's depth.
428 const uint32_t ParentDepth = Depth - 1;
429 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
430 if (DieArray[I].getDepth() == ParentDepth)
431 return DWARFDie(this, &DieArray[I]);
432 }
433 return DWARFDie();
434}
435
436DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
437 if (!Die)
438 return DWARFDie();
439 uint32_t Depth = Die->getDepth();
440 // Unit DIEs always have a depth of zero and never have siblings.
441 if (Depth == 0)
442 return DWARFDie();
443 // NULL DIEs don't have siblings.
444 if (Die->getAbbreviationDeclarationPtr() == nullptr)
445 return DWARFDie();
446
447 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000448 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
449 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000450 if (DieArray[I].getDepth() == Depth)
451 return DWARFDie(this, &DieArray[I]);
452 }
453 return DWARFDie();
454}