blob: 51c6c090eb31ebee9e620fbc8e955ec30dc42ebb [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 Blaikiec4e2bed2015-11-17 21:08:05 +000040 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
41 AddrOffsetSection(AOS), isLittleEndian(LE), UnitSection(UnitSection),
42 IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000043 clear();
44}
45
46DWARFUnit::~DWARFUnit() {
47}
48
49bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
50 uint64_t &Result) const {
51 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
52 if (AddrOffsetSection.size() < Offset + AddrSize)
53 return false;
54 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
55 Result = DA.getAddress(&Offset);
56 return true;
57}
58
59bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
60 uint32_t &Result) const {
61 // FIXME: string offset section entries are 8-byte for DWARF64.
62 const uint32_t ItemSize = 4;
63 uint32_t Offset = Index * ItemSize;
64 if (StringOffsetSection.size() < Offset + ItemSize)
65 return false;
66 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
67 Result = DA.getU32(&Offset);
68 return true;
69}
70
71bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
72 Length = debug_info.getU32(offset_ptr);
73 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000074 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie82641be2015-11-17 00:39:55 +000075 if (IndexEntry) {
76 if (AbbrOffset)
77 return false;
78 auto *UnitContrib = IndexEntry->getOffset();
79 if (!UnitContrib || UnitContrib->Length != (Length + 4))
80 return false;
81 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
82 if (!AbbrEntry)
83 return false;
84 AbbrOffset = AbbrEntry->Offset;
85 }
David Blaikie07e22442013-09-23 22:44:40 +000086 AddrSize = debug_info.getU8(offset_ptr);
87
Alexey Samsonov7682f812014-04-24 22:51:03 +000088 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
89 bool VersionOK = DWARFContext::isSupportedVersion(Version);
90 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +000091
Alexey Samsonov7682f812014-04-24 22:51:03 +000092 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +000093 return false;
94
Alexey Samsonov7682f812014-04-24 22:51:03 +000095 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +000096 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +000097}
98
99bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
100 clear();
101
102 Offset = *offset_ptr;
103
104 if (debug_info.isValidOffset(*offset_ptr)) {
105 if (extractImpl(debug_info, offset_ptr))
106 return true;
107
108 // reset the offset to where we tried to parse from if anything went wrong
109 *offset_ptr = Offset;
110 }
111
112 return false;
113}
114
David Blaikie07e22442013-09-23 22:44:40 +0000115bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
116 DWARFDebugRangeList &RangeList) const {
117 // Require that compile unit is extracted.
118 assert(DieArray.size() > 0);
119 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
120 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
121 return RangeList.extract(RangesData, &ActualRangeListOffset);
122}
123
124void DWARFUnit::clear() {
125 Offset = 0;
126 Length = 0;
127 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000128 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000129 AddrSize = 0;
130 BaseAddr = 0;
131 RangeSectionBase = 0;
132 AddrOffsetSectionBase = 0;
133 clearDIEs(false);
134 DWO.reset();
135}
136
137const char *DWARFUnit::getCompilationDir() {
138 extractDIEsIfNeeded(true);
139 if (DieArray.empty())
Craig Topper2617dcc2014-04-15 06:32:26 +0000140 return nullptr;
141 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000142}
143
144uint64_t DWARFUnit::getDWOId() {
145 extractDIEsIfNeeded(true);
146 const uint64_t FailValue = -1ULL;
147 if (DieArray.empty())
148 return FailValue;
149 return DieArray[0]
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000150 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
David Blaikie07e22442013-09-23 22:44:40 +0000151}
152
153void DWARFUnit::setDIERelations() {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000154 if (DieArray.size() <= 1)
David Blaikie07e22442013-09-23 22:44:40 +0000155 return;
David Blaikie07e22442013-09-23 22:44:40 +0000156
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000157 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
158 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
159 for (auto &DIE : DieArray) {
160 if (SiblingChain) {
161 SiblingChain->setSibling(&DIE);
162 }
163 if (const DWARFAbbreviationDeclaration *AbbrDecl =
164 DIE.getAbbreviationDeclarationPtr()) {
165 // Normal DIE.
166 if (AbbrDecl->hasChildren()) {
167 ParentChain.push_back(&DIE);
168 SiblingChain = nullptr;
169 } else {
170 SiblingChain = &DIE;
171 }
David Blaikie07e22442013-09-23 22:44:40 +0000172 } else {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000173 // NULL entry terminates the sibling chain.
174 SiblingChain = ParentChain.back();
175 ParentChain.pop_back();
David Blaikie07e22442013-09-23 22:44:40 +0000176 }
177 }
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000178 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
179 assert(ParentChain.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000180}
181
182void DWARFUnit::extractDIEsToVector(
183 bool AppendCUDie, bool AppendNonCUDies,
184 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
185 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();
192 DWARFDebugInfoEntryMinimal DIE;
193 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000194 bool IsCUDie = true;
195
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000196 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
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) {
234 if ((CUDieOnly && DieArray.size() > 0) ||
235 DieArray.size() > 1)
236 return 0; // Already parsed.
237
238 bool HasCUDie = DieArray.size() > 0;
239 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) {
246 uint64_t BaseAddr =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000247 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
248 if (BaseAddr == -1ULL)
249 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
David Blaikie07e22442013-09-23 22:44:40 +0000250 setBaseAddress(BaseAddr);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000251 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
252 this, DW_AT_GNU_addr_base, 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000253 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
254 this, DW_AT_ranges_base, 0);
255 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
256 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000257 }
258
259 setDIERelations();
260 return DieArray.size();
261}
262
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000263DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
264 : DWOFile(), DWOContext(), DWOU(nullptr) {
265 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
266 if (!Obj)
267 return;
268 DWOFile = std::move(Obj.get());
269 DWOContext.reset(
Zachary Turner6489d7b2015-04-23 17:37:47 +0000270 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000271 if (DWOContext->getNumDWOCompileUnits() > 0)
272 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
273}
274
275bool DWARFUnit::parseDWO() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000276 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000277 return false;
278 extractDIEsIfNeeded(true);
279 if (DieArray.empty())
280 return false;
281 const char *DWOFileName =
Craig Topper2617dcc2014-04-15 06:32:26 +0000282 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
283 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000284 return false;
285 const char *CompilationDir =
Craig Topper2617dcc2014-04-15 06:32:26 +0000286 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000287 SmallString<16> AbsolutePath;
Craig Topper2617dcc2014-04-15 06:32:26 +0000288 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
David Blaikie07e22442013-09-23 22:44:40 +0000289 sys::path::append(AbsolutePath, CompilationDir);
290 }
291 sys::path::append(AbsolutePath, DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000292 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000293 DWARFUnit *DWOCU = DWO->getUnit();
294 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000295 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000296 DWO.reset();
297 return false;
298 }
299 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
300 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000301 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
Alexey Samsonovf0e40342014-06-12 18:52:35 +0000302 DWOCU->setRangesSection(RangeSection, DWORangesBase);
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.
315 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
316 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) {
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000324 const auto *U = getUnitDIE();
325 if (U == nullptr)
326 return;
327 // First, check if unit DIE describes address ranges for the whole unit.
328 const auto &CUDIERanges = U->getAddressRanges(this);
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;
341 DieArray[0].collectChildrenAddressRanges(this, CURanges);
342
343 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000344 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000345 if (DWO.get())
346 DWO->getUnit()->collectAddressRanges(CURanges);
347 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
356const DWARFDebugInfoEntryMinimal *
357DWARFUnit::getSubprogramForAddress(uint64_t Address) {
358 extractDIEsIfNeeded(false);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000359 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
360 if (DIE.isSubprogramDIE() &&
361 DIE.addressRangeContainsAddress(this, Address)) {
362 return &DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000363 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000364 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000365 return nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000366}
367
368DWARFDebugInfoEntryInlinedChain
369DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
370 // First, find a subprogram that contains the given address (the root
371 // of inlined chain).
Craig Topper2617dcc2014-04-15 06:32:26 +0000372 const DWARFUnit *ChainCU = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000373 const DWARFDebugInfoEntryMinimal *SubprogramDIE =
374 getSubprogramForAddress(Address);
375 if (SubprogramDIE) {
376 ChainCU = this;
377 } else {
378 // Try to look for subprogram DIEs in the DWO file.
379 parseDWO();
380 if (DWO.get()) {
381 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
382 if (SubprogramDIE)
383 ChainCU = DWO->getUnit();
384 }
385 }
386
387 // Get inlined chain rooted at this subprogram DIE.
388 if (!SubprogramDIE)
389 return DWARFDebugInfoEntryInlinedChain();
390 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
391}
David Blaikie82641be2015-11-17 00:39:55 +0000392
393const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
394 DWARFSectionKind Kind) {
395 if (Kind == DW_SECT_INFO)
396 return Context.getCUIndex();
397 assert(Kind == DW_SECT_TYPES);
398 return Context.getTUIndex();
399}
400}