blob: 09e6a292e5fe1e221835a2f76fdfdf53c7263322 [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000017#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
18#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton97d22182017-01-13 21:08:18 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000020#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000023#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include <algorithm>
25#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000026#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000028#include <cstdio>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000030
Eugene Zelenko28db7e62017-03-01 01:14:23 +000031using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000032using namespace dwarf;
33
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000034void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
George Rimarca532112017-04-24 10:19:45 +000035 parseImpl(C, Section, C.getDebugAbbrev(), &C.getRangeSection(),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000036 C.getStringSection(), C.getStringOffsetSection(),
37 &C.getAddrSection(), C.getLineSection().Data, C.isLittleEndian(),
38 false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000039}
40
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000041void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikie82641be2015-11-17 00:39:55 +000042 const DWARFSection &DWOSection,
43 DWARFUnitIndex *Index) {
George Rimarca532112017-04-24 10:19:45 +000044 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &C.getRangeDWOSection(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000045 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
David Blaikied2f3a942017-05-22 07:02:47 +000046 &C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
David Blaikiee438cff2016-04-22 22:50:56 +000047 true);
Frederic Riss6005dbd2014-10-06 03:36:18 +000048}
49
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000050DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
George Rimarca532112017-04-24 10:19:45 +000051 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000052 StringRef SS, const DWARFSection &SOS,
53 const DWARFSection *AOS, StringRef LS, bool LE, bool IsDWO,
George Rimarca532112017-04-24 10:19:45 +000054 const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000055 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000056 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000057 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
58 StringOffsetSectionBase(0), AddrOffsetSection(AOS), isLittleEndian(LE),
59 isDWO(IsDWO), UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000060 clear();
61}
62
Eugene Zelenko570e39a2016-11-23 23:16:32 +000063DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000064
65bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
66 uint64_t &Result) const {
67 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
David Blaikied2f3a942017-05-22 07:02:47 +000068 if (AddrOffsetSection->Data.size() < Offset + AddrSize)
David Blaikie07e22442013-09-23 22:44:40 +000069 return false;
David Blaikied2f3a942017-05-22 07:02:47 +000070 DataExtractor DA(AddrOffsetSection->Data, isLittleEndian, AddrSize);
71 Result = getRelocatedValue(DA, AddrSize, &Offset, &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);
97 Version = debug_info.getU16(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +000098 uint64_t AbbrOffset;
99 if (Version >= 5) {
100 UnitType = debug_info.getU8(offset_ptr);
101 AddrSize = debug_info.getU8(offset_ptr);
102 AbbrOffset = debug_info.getU32(offset_ptr);
103 } else {
104 AbbrOffset = debug_info.getU32(offset_ptr);
105 AddrSize = debug_info.getU8(offset_ptr);
106 }
David Blaikie82641be2015-11-17 00:39:55 +0000107 if (IndexEntry) {
108 if (AbbrOffset)
109 return false;
110 auto *UnitContrib = IndexEntry->getOffset();
111 if (!UnitContrib || UnitContrib->Length != (Length + 4))
112 return false;
113 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
114 if (!AbbrEntry)
115 return false;
116 AbbrOffset = AbbrEntry->Offset;
117 }
David Blaikie07e22442013-09-23 22:44:40 +0000118
Alexey Samsonov7682f812014-04-24 22:51:03 +0000119 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
120 bool VersionOK = DWARFContext::isSupportedVersion(Version);
121 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000122
Alexey Samsonov7682f812014-04-24 22:51:03 +0000123 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000124 return false;
125
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000126 // Keep track of the highest DWARF version we encounter across all units.
127 Context.setMaxVersionIfGreater(Version);
128
Alexey Samsonov7682f812014-04-24 22:51:03 +0000129 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000130 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000131}
132
133bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
134 clear();
135
136 Offset = *offset_ptr;
137
138 if (debug_info.isValidOffset(*offset_ptr)) {
139 if (extractImpl(debug_info, offset_ptr))
140 return true;
141
142 // reset the offset to where we tried to parse from if anything went wrong
143 *offset_ptr = Offset;
144 }
145
146 return false;
147}
148
David Blaikie07e22442013-09-23 22:44:40 +0000149bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
150 DWARFDebugRangeList &RangeList) const {
151 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000152 assert(!DieArray.empty());
George Rimarca532112017-04-24 10:19:45 +0000153 DataExtractor RangesData(RangeSection->Data, isLittleEndian, AddrSize);
David Blaikie07e22442013-09-23 22:44:40 +0000154 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
George Rimarca532112017-04-24 10:19:45 +0000155 return RangeList.extract(RangesData, &ActualRangeListOffset,
156 RangeSection->Relocs);
David Blaikie07e22442013-09-23 22:44:40 +0000157}
158
159void DWARFUnit::clear() {
160 Offset = 0;
161 Length = 0;
162 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000163 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000164 AddrSize = 0;
165 BaseAddr = 0;
166 RangeSectionBase = 0;
167 AddrOffsetSectionBase = 0;
168 clearDIEs(false);
169 DWO.reset();
170}
171
172const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000173 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000174}
175
Greg Clayton52fe1f62016-12-14 22:38:08 +0000176Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000177 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000178}
179
David Blaikie07e22442013-09-23 22:44:40 +0000180void DWARFUnit::extractDIEsToVector(
181 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000182 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000183 if (!AppendCUDie && !AppendNonCUDies)
184 return;
185
186 // Set the offset to that of the first DIE and calculate the start of the
187 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000188 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000189 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000190 DWARFDebugInfoEntry DIE;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000191 DataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000192 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000193 bool IsCUDie = true;
194
Greg Clayton78a07bf2016-12-21 21:37:06 +0000195 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
196 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000197 if (IsCUDie) {
198 if (AppendCUDie)
199 Dies.push_back(DIE);
200 if (!AppendNonCUDies)
201 break;
202 // The average bytes per DIE entry has been seen to be
203 // around 14-20 so let's pre-reserve the needed memory for
204 // our DIE entries accordingly.
205 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
206 IsCUDie = false;
207 } else {
208 Dies.push_back(DIE);
209 }
210
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000211 if (const DWARFAbbreviationDeclaration *AbbrDecl =
212 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000213 // Normal DIE
214 if (AbbrDecl->hasChildren())
215 ++Depth;
216 } else {
217 // NULL DIE.
218 if (Depth > 0)
219 --Depth;
220 if (Depth == 0)
221 break; // We are done with this compile unit!
222 }
223 }
224
225 // Give a little bit of info if we encounter corrupt DWARF (our offset
226 // should always terminate at or before the start of the next compilation
227 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000228 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000229 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000230 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000231}
232
233size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000234 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000235 DieArray.size() > 1)
236 return 0; // Already parsed.
237
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000238 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000239 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
240
241 if (DieArray.empty())
242 return 0;
243
244 // If CU DIE was just parsed, copy several attribute values from it.
245 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000246 DWARFDie UnitDie = getUnitDIE();
Greg Claytonc109bbe2017-01-13 22:32:12 +0000247 auto BaseAddr = toAddress(UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000248 if (BaseAddr)
249 setBaseAddress(*BaseAddr);
Greg Clayton97d22182017-01-13 21:08:18 +0000250 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
251 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000252
253 // In general, we derive the offset of the unit's contibution to the
254 // debug_str_offsets{.dwo} section from the unit DIE's
255 // DW_AT_str_offsets_base attribute. In dwp files we add to it the offset
256 // we get from the index table.
257 StringOffsetSectionBase =
258 toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
259 if (IndexEntry)
260 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
261 StringOffsetSectionBase += C->Offset;
262
Alexey Samsonovaa909982014-06-13 22:31:03 +0000263 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
264 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000265 }
266
David Blaikie07e22442013-09-23 22:44:40 +0000267 return DieArray.size();
268}
269
David Blaikie07e22442013-09-23 22:44:40 +0000270bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000271 if (isDWO)
272 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000273 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000274 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000275 DWARFDie UnitDie = getUnitDIE();
276 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000277 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000278 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000279 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000280 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000281 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000282 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000283 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
284 *CompilationDir) {
285 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000286 }
Greg Clayton97d22182017-01-13 21:08:18 +0000287 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000288 auto DWOId = getDWOId();
289 if (!DWOId)
290 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000291 auto DWOContext = Context.getDWOContext(AbsolutePath);
292 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000293 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000294
David Blaikie15d85fc2017-05-23 06:48:53 +0000295 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
296 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000297 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000298 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000299 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000300 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000301 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000302 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000303 return true;
304}
305
306void DWARFUnit::clearDIEs(bool KeepCUDie) {
307 if (DieArray.size() > (unsigned)KeepCUDie) {
308 // std::vectors never get any smaller when resized to a smaller size,
309 // or when clear() or erase() are called, the size will report that it
310 // is smaller, but the memory allocated remains intact (call capacity()
311 // to see this). So we need to create a temporary vector and swap the
312 // contents which will cause just the internal pointers to be swapped
313 // so that when temporary vector goes out of scope, it will destroy the
314 // contents.
Greg Claytonc8c10322016-12-13 18:25:19 +0000315 std::vector<DWARFDebugInfoEntry> TmpArray;
David Blaikie07e22442013-09-23 22:44:40 +0000316 DieArray.swap(TmpArray);
317 // Save at least the compile unit DIE
318 if (KeepCUDie)
319 DieArray.push_back(TmpArray.front());
320 }
321}
322
Alexey Samsonov762343d2014-04-18 17:25:46 +0000323void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000324 DWARFDie UnitDie = getUnitDIE();
325 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000326 return;
327 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000328 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000329 if (!CUDIERanges.empty()) {
330 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
331 return;
332 }
333
David Blaikie07e22442013-09-23 22:44:40 +0000334 // This function is usually called if there in no .debug_aranges section
335 // in order to produce a compile unit level set of address ranges that
336 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
337 // all compile units to stay loaded when they weren't needed. So we can end
338 // up parsing the DWARF and then throwing them all away to keep memory usage
339 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000340 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000341 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000342
343 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000344 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000345 if (DWO)
346 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000347 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000348 DWO.reset();
349
350 // Keep memory down by clearing DIEs if this generate function
351 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000352 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000353 clearDIEs(true);
354}
355
Dehao Chena364f092017-04-19 20:09:38 +0000356void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
357 if (Die.isSubroutineDIE()) {
358 for (const auto &R : Die.getAddressRanges()) {
359 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000360 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000361 continue;
George Rimar4671f2e2017-05-16 12:30:59 +0000362 auto B = AddrDieMap.upper_bound(R.LowPC);
363 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
Dehao Chena364f092017-04-19 20:09:38 +0000364 // The range is a sub-range of existing ranges, we need to split the
365 // existing range.
George Rimar4671f2e2017-05-16 12:30:59 +0000366 if (R.HighPC < B->second.first)
367 AddrDieMap[R.HighPC] = B->second;
368 if (R.LowPC > B->first)
369 AddrDieMap[B->first].first = R.LowPC;
Dehao Chena364f092017-04-19 20:09:38 +0000370 }
George Rimar4671f2e2017-05-16 12:30:59 +0000371 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
David Blaikie07e22442013-09-23 22:44:40 +0000372 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000373 }
Dehao Chena364f092017-04-19 20:09:38 +0000374 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
375 // simplify the logic to update AddrDieMap. The child's range will always
376 // be equal or smaller than the parent's range. With this assumption, when
377 // adding one range into the map, it will at most split a range into 3
378 // sub-ranges.
379 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
380 updateAddressDieMap(Child);
381}
382
383DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
384 extractDIEsIfNeeded(false);
385 if (AddrDieMap.empty())
386 updateAddressDieMap(getUnitDIE());
387 auto R = AddrDieMap.upper_bound(Address);
388 if (R == AddrDieMap.begin())
389 return DWARFDie();
390 // upper_bound's previous item contains Address.
391 --R;
392 if (Address >= R->second.first)
393 return DWARFDie();
394 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000395}
396
Greg Claytonc8c10322016-12-13 18:25:19 +0000397void
398DWARFUnit::getInlinedChainForAddress(uint64_t Address,
399 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000400 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000401 // Try to look for subprogram DIEs in the DWO file.
402 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000403 // First, find the subroutine that contains the given address (the leaf
404 // of inlined chain).
405 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000406 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000407
Dehao Chendb569ba2017-04-19 20:52:21 +0000408 while (SubroutineDIE) {
409 if (SubroutineDIE.isSubroutineDIE())
410 InlinedChain.push_back(SubroutineDIE);
411 SubroutineDIE = SubroutineDIE.getParent();
412 }
David Blaikie07e22442013-09-23 22:44:40 +0000413}
David Blaikie82641be2015-11-17 00:39:55 +0000414
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000415const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
416 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000417 if (Kind == DW_SECT_INFO)
418 return Context.getCUIndex();
419 assert(Kind == DW_SECT_TYPES);
420 return Context.getTUIndex();
421}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000422
Greg Clayton78a07bf2016-12-21 21:37:06 +0000423DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
424 if (!Die)
425 return DWARFDie();
426 const uint32_t Depth = Die->getDepth();
427 // Unit DIEs always have a depth of zero and never have parents.
428 if (Depth == 0)
429 return DWARFDie();
430 // Depth of 1 always means parent is the compile/type unit.
431 if (Depth == 1)
432 return getUnitDIE();
433 // Look for previous DIE with a depth that is one less than the Die's depth.
434 const uint32_t ParentDepth = Depth - 1;
435 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
436 if (DieArray[I].getDepth() == ParentDepth)
437 return DWARFDie(this, &DieArray[I]);
438 }
439 return DWARFDie();
440}
441
442DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
443 if (!Die)
444 return DWARFDie();
445 uint32_t Depth = Die->getDepth();
446 // Unit DIEs always have a depth of zero and never have siblings.
447 if (Depth == 0)
448 return DWARFDie();
449 // NULL DIEs don't have siblings.
450 if (Die->getAbbreviationDeclarationPtr() == nullptr)
451 return DWARFDie();
452
453 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000454 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
455 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000456 if (DieArray[I].getDepth() == Depth)
457 return DWARFDie(this, &DieArray[I]);
458 }
459 return DWARFDie();
460}