blob: 0430265ae74e81818d3e124f3a47c892acb7b676 [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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
11#include "llvm/DebugInfo/DWARF/DWARFContext.h"
12#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
David Blaikie07e22442013-09-23 22:44:40 +000013#include "llvm/Support/Dwarf.h"
14#include "llvm/Support/Path.h"
David Blaikie8de5e982013-09-23 23:15:57 +000015#include <cstdio>
David Blaikie07e22442013-09-23 22:44:40 +000016
David Blaikie82641be2015-11-17 00:39:55 +000017namespace llvm {
David Blaikie07e22442013-09-23 22:44:40 +000018using namespace dwarf;
19
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000020void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
21 parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
22 C.getStringSection(), StringRef(), C.getAddrSection(),
David Blaikiec4e2bed2015-11-17 21:08:05 +000023 C.getLineSection().Data, C.isLittleEndian());
Frederic Riss6005dbd2014-10-06 03:36:18 +000024}
25
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000026void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikie82641be2015-11-17 00:39:55 +000027 const DWARFSection &DWOSection,
28 DWARFUnitIndex *Index) {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000029 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000030 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
David Blaikiec4e2bed2015-11-17 21:08:05 +000031 C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian());
Frederic Riss6005dbd2014-10-06 03:36:18 +000032}
33
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000034DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
35 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
David Blaikiec4e2bed2015-11-17 21:08:05 +000036 StringRef SOS, StringRef AOS, StringRef LS, bool LE,
David Blaikie82641be2015-11-17 00:39:55 +000037 const DWARFUnitSectionBase &UnitSection,
38 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000039 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
David Blaikie35c2eeb2015-11-17 22:39:23 +000040 LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
David Blaikie4689ef52015-11-17 22:39:26 +000041 if (IndexEntry)
42 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
43 return SOS.slice(C->Offset, C->Offset + C->Length);
David Blaikie35c2eeb2015-11-17 22:39:23 +000044 return SOS;
45 }()),
David Blaikiec4e2bed2015-11-17 21:08:05 +000046 AddrOffsetSection(AOS), isLittleEndian(LE), UnitSection(UnitSection),
47 IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000048 clear();
49}
50
51DWARFUnit::~DWARFUnit() {
52}
53
54bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
55 uint64_t &Result) const {
56 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
57 if (AddrOffsetSection.size() < Offset + AddrSize)
58 return false;
59 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
60 Result = DA.getAddress(&Offset);
61 return true;
62}
63
64bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
65 uint32_t &Result) const {
66 // FIXME: string offset section entries are 8-byte for DWARF64.
67 const uint32_t ItemSize = 4;
68 uint32_t Offset = Index * ItemSize;
69 if (StringOffsetSection.size() < Offset + ItemSize)
70 return false;
71 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
72 Result = DA.getU32(&Offset);
73 return true;
74}
75
76bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
77 Length = debug_info.getU32(offset_ptr);
78 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000079 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie82641be2015-11-17 00:39:55 +000080 if (IndexEntry) {
81 if (AbbrOffset)
82 return false;
83 auto *UnitContrib = IndexEntry->getOffset();
84 if (!UnitContrib || UnitContrib->Length != (Length + 4))
85 return false;
86 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
87 if (!AbbrEntry)
88 return false;
89 AbbrOffset = AbbrEntry->Offset;
90 }
David Blaikie07e22442013-09-23 22:44:40 +000091 AddrSize = debug_info.getU8(offset_ptr);
92
Alexey Samsonov7682f812014-04-24 22:51:03 +000093 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
94 bool VersionOK = DWARFContext::isSupportedVersion(Version);
95 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +000096
Alexey Samsonov7682f812014-04-24 22:51:03 +000097 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +000098 return false;
99
Alexey Samsonov7682f812014-04-24 22:51:03 +0000100 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +0000101 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000102}
103
104bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
105 clear();
106
107 Offset = *offset_ptr;
108
109 if (debug_info.isValidOffset(*offset_ptr)) {
110 if (extractImpl(debug_info, offset_ptr))
111 return true;
112
113 // reset the offset to where we tried to parse from if anything went wrong
114 *offset_ptr = Offset;
115 }
116
117 return false;
118}
119
David Blaikie07e22442013-09-23 22:44:40 +0000120bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
121 DWARFDebugRangeList &RangeList) const {
122 // Require that compile unit is extracted.
123 assert(DieArray.size() > 0);
124 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
125 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
126 return RangeList.extract(RangesData, &ActualRangeListOffset);
127}
128
129void DWARFUnit::clear() {
130 Offset = 0;
131 Length = 0;
132 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000133 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000134 AddrSize = 0;
135 BaseAddr = 0;
136 RangeSectionBase = 0;
137 AddrOffsetSectionBase = 0;
138 clearDIEs(false);
139 DWO.reset();
140}
141
142const char *DWARFUnit::getCompilationDir() {
143 extractDIEsIfNeeded(true);
144 if (DieArray.empty())
Craig Topper2617dcc2014-04-15 06:32:26 +0000145 return nullptr;
146 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000147}
148
149uint64_t DWARFUnit::getDWOId() {
150 extractDIEsIfNeeded(true);
151 const uint64_t FailValue = -1ULL;
152 if (DieArray.empty())
153 return FailValue;
154 return DieArray[0]
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000155 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
David Blaikie07e22442013-09-23 22:44:40 +0000156}
157
158void DWARFUnit::setDIERelations() {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000159 if (DieArray.size() <= 1)
David Blaikie07e22442013-09-23 22:44:40 +0000160 return;
David Blaikie07e22442013-09-23 22:44:40 +0000161
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000162 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
163 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
164 for (auto &DIE : DieArray) {
165 if (SiblingChain) {
166 SiblingChain->setSibling(&DIE);
167 }
168 if (const DWARFAbbreviationDeclaration *AbbrDecl =
169 DIE.getAbbreviationDeclarationPtr()) {
170 // Normal DIE.
171 if (AbbrDecl->hasChildren()) {
172 ParentChain.push_back(&DIE);
173 SiblingChain = nullptr;
174 } else {
175 SiblingChain = &DIE;
176 }
David Blaikie07e22442013-09-23 22:44:40 +0000177 } else {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000178 // NULL entry terminates the sibling chain.
179 SiblingChain = ParentChain.back();
180 ParentChain.pop_back();
David Blaikie07e22442013-09-23 22:44:40 +0000181 }
182 }
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000183 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
184 assert(ParentChain.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000185}
186
187void DWARFUnit::extractDIEsToVector(
188 bool AppendCUDie, bool AppendNonCUDies,
189 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
190 if (!AppendCUDie && !AppendNonCUDies)
191 return;
192
193 // Set the offset to that of the first DIE and calculate the start of the
194 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000195 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000196 uint32_t NextCUOffset = getNextUnitOffset();
197 DWARFDebugInfoEntryMinimal DIE;
198 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000199 bool IsCUDie = true;
200
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000201 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
David Blaikie07e22442013-09-23 22:44:40 +0000202 if (IsCUDie) {
203 if (AppendCUDie)
204 Dies.push_back(DIE);
205 if (!AppendNonCUDies)
206 break;
207 // The average bytes per DIE entry has been seen to be
208 // around 14-20 so let's pre-reserve the needed memory for
209 // our DIE entries accordingly.
210 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
211 IsCUDie = false;
212 } else {
213 Dies.push_back(DIE);
214 }
215
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000216 if (const DWARFAbbreviationDeclaration *AbbrDecl =
217 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000218 // Normal DIE
219 if (AbbrDecl->hasChildren())
220 ++Depth;
221 } else {
222 // NULL DIE.
223 if (Depth > 0)
224 --Depth;
225 if (Depth == 0)
226 break; // We are done with this compile unit!
227 }
228 }
229
230 // Give a little bit of info if we encounter corrupt DWARF (our offset
231 // should always terminate at or before the start of the next compilation
232 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000233 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000234 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000235 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000236}
237
238size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
239 if ((CUDieOnly && DieArray.size() > 0) ||
240 DieArray.size() > 1)
241 return 0; // Already parsed.
242
243 bool HasCUDie = DieArray.size() > 0;
244 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
245
246 if (DieArray.empty())
247 return 0;
248
249 // If CU DIE was just parsed, copy several attribute values from it.
250 if (!HasCUDie) {
251 uint64_t BaseAddr =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000252 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
253 if (BaseAddr == -1ULL)
254 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
David Blaikie07e22442013-09-23 22:44:40 +0000255 setBaseAddress(BaseAddr);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000256 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
257 this, DW_AT_GNU_addr_base, 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000258 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
259 this, DW_AT_ranges_base, 0);
260 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
261 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000262 }
263
264 setDIERelations();
265 return DieArray.size();
266}
267
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000268DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
269 : DWOFile(), DWOContext(), DWOU(nullptr) {
270 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000271 if (!Obj) {
272 // TODO: Actually report errors helpfully.
273 consumeError(Obj.takeError());
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000274 return;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000275 }
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000276 DWOFile = std::move(Obj.get());
277 DWOContext.reset(
Zachary Turner6489d7b2015-04-23 17:37:47 +0000278 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000279 if (DWOContext->getNumDWOCompileUnits() > 0)
280 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
281}
282
283bool DWARFUnit::parseDWO() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000284 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000285 return false;
286 extractDIEsIfNeeded(true);
287 if (DieArray.empty())
288 return false;
289 const char *DWOFileName =
Craig Topper2617dcc2014-04-15 06:32:26 +0000290 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
291 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000292 return false;
293 const char *CompilationDir =
Craig Topper2617dcc2014-04-15 06:32:26 +0000294 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000295 SmallString<16> AbsolutePath;
Craig Topper2617dcc2014-04-15 06:32:26 +0000296 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
David Blaikie07e22442013-09-23 22:44:40 +0000297 sys::path::append(AbsolutePath, CompilationDir);
298 }
299 sys::path::append(AbsolutePath, DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000300 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000301 DWARFUnit *DWOCU = DWO->getUnit();
302 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000303 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000304 DWO.reset();
305 return false;
306 }
307 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
308 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000309 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
Alexey Samsonovf0e40342014-06-12 18:52:35 +0000310 DWOCU->setRangesSection(RangeSection, DWORangesBase);
David Blaikie07e22442013-09-23 22:44:40 +0000311 return true;
312}
313
314void DWARFUnit::clearDIEs(bool KeepCUDie) {
315 if (DieArray.size() > (unsigned)KeepCUDie) {
316 // std::vectors never get any smaller when resized to a smaller size,
317 // or when clear() or erase() are called, the size will report that it
318 // is smaller, but the memory allocated remains intact (call capacity()
319 // to see this). So we need to create a temporary vector and swap the
320 // contents which will cause just the internal pointers to be swapped
321 // so that when temporary vector goes out of scope, it will destroy the
322 // contents.
323 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
324 DieArray.swap(TmpArray);
325 // Save at least the compile unit DIE
326 if (KeepCUDie)
327 DieArray.push_back(TmpArray.front());
328 }
329}
330
Alexey Samsonov762343d2014-04-18 17:25:46 +0000331void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000332 const auto *U = getUnitDIE();
333 if (U == nullptr)
334 return;
335 // First, check if unit DIE describes address ranges for the whole unit.
336 const auto &CUDIERanges = U->getAddressRanges(this);
Alexey Samsonov84e24232014-04-18 20:30:27 +0000337 if (!CUDIERanges.empty()) {
338 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
339 return;
340 }
341
David Blaikie07e22442013-09-23 22:44:40 +0000342 // This function is usually called if there in no .debug_aranges section
343 // in order to produce a compile unit level set of address ranges that
344 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
345 // all compile units to stay loaded when they weren't needed. So we can end
346 // up parsing the DWARF and then throwing them all away to keep memory usage
347 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000348 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
349 DieArray[0].collectChildrenAddressRanges(this, CURanges);
350
351 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000352 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000353 if (DWO.get())
354 DWO->getUnit()->collectAddressRanges(CURanges);
355 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000356 DWO.reset();
357
358 // Keep memory down by clearing DIEs if this generate function
359 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000360 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000361 clearDIEs(true);
362}
363
364const DWARFDebugInfoEntryMinimal *
365DWARFUnit::getSubprogramForAddress(uint64_t Address) {
366 extractDIEsIfNeeded(false);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000367 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
368 if (DIE.isSubprogramDIE() &&
369 DIE.addressRangeContainsAddress(this, Address)) {
370 return &DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000371 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000372 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000373 return nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000374}
375
376DWARFDebugInfoEntryInlinedChain
377DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
378 // First, find a subprogram that contains the given address (the root
379 // of inlined chain).
Craig Topper2617dcc2014-04-15 06:32:26 +0000380 const DWARFUnit *ChainCU = nullptr;
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000381 const DWARFDebugInfoEntryMinimal *SubprogramDIE;
382 // Try to look for subprogram DIEs in the DWO file.
383 parseDWO();
384 if (DWO) {
385 if ((SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address)))
386 ChainCU = DWO->getUnit();
387 } else if ((SubprogramDIE = getSubprogramForAddress(Address)))
David Blaikie07e22442013-09-23 22:44:40 +0000388 ChainCU = this;
David Blaikie07e22442013-09-23 22:44:40 +0000389
390 // Get inlined chain rooted at this subprogram DIE.
391 if (!SubprogramDIE)
392 return DWARFDebugInfoEntryInlinedChain();
393 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
394}
David Blaikie82641be2015-11-17 00:39:55 +0000395
396const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
397 DWARFSectionKind Kind) {
398 if (Kind == DW_SECT_INFO)
399 return Context.getCUIndex();
400 assert(Kind == DW_SECT_TYPES);
401 return Context.getTUIndex();
402}
403}