blob: 169acee62d79408ac2bf0cc0094c75d38197a0df [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(),
Frederic Riss6005dbd2014-10-06 03:36:18 +000023 C.isLittleEndian());
24}
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(),
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000031 C.getAddrSection(), 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,
36 StringRef SOS, StringRef AOS, 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),
40 StringSection(SS), StringOffsetSection(SOS), AddrOffsetSection(AOS),
David Blaikie82641be2015-11-17 00:39:55 +000041 isLittleEndian(LE), UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000042 clear();
43}
44
45DWARFUnit::~DWARFUnit() {
46}
47
48bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
49 uint64_t &Result) const {
50 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
51 if (AddrOffsetSection.size() < Offset + AddrSize)
52 return false;
53 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
54 Result = DA.getAddress(&Offset);
55 return true;
56}
57
58bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
59 uint32_t &Result) const {
60 // FIXME: string offset section entries are 8-byte for DWARF64.
61 const uint32_t ItemSize = 4;
62 uint32_t Offset = Index * ItemSize;
63 if (StringOffsetSection.size() < Offset + ItemSize)
64 return false;
65 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
66 Result = DA.getU32(&Offset);
67 return true;
68}
69
70bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
71 Length = debug_info.getU32(offset_ptr);
72 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000073 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie82641be2015-11-17 00:39:55 +000074 if (IndexEntry) {
75 if (AbbrOffset)
76 return false;
77 auto *UnitContrib = IndexEntry->getOffset();
78 if (!UnitContrib || UnitContrib->Length != (Length + 4))
79 return false;
80 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
81 if (!AbbrEntry)
82 return false;
83 AbbrOffset = AbbrEntry->Offset;
84 }
David Blaikie07e22442013-09-23 22:44:40 +000085 AddrSize = debug_info.getU8(offset_ptr);
86
Alexey Samsonov7682f812014-04-24 22:51:03 +000087 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
88 bool VersionOK = DWARFContext::isSupportedVersion(Version);
89 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +000090
Alexey Samsonov7682f812014-04-24 22:51:03 +000091 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +000092 return false;
93
Alexey Samsonov7682f812014-04-24 22:51:03 +000094 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
Benjamin Kramer68a29562015-05-25 13:28:03 +000095 return Abbrevs != nullptr;
David Blaikie07e22442013-09-23 22:44:40 +000096}
97
98bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
99 clear();
100
101 Offset = *offset_ptr;
102
103 if (debug_info.isValidOffset(*offset_ptr)) {
104 if (extractImpl(debug_info, offset_ptr))
105 return true;
106
107 // reset the offset to where we tried to parse from if anything went wrong
108 *offset_ptr = Offset;
109 }
110
111 return false;
112}
113
David Blaikie07e22442013-09-23 22:44:40 +0000114bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
115 DWARFDebugRangeList &RangeList) const {
116 // Require that compile unit is extracted.
117 assert(DieArray.size() > 0);
118 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
119 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
120 return RangeList.extract(RangesData, &ActualRangeListOffset);
121}
122
123void DWARFUnit::clear() {
124 Offset = 0;
125 Length = 0;
126 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000127 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000128 AddrSize = 0;
129 BaseAddr = 0;
130 RangeSectionBase = 0;
131 AddrOffsetSectionBase = 0;
132 clearDIEs(false);
133 DWO.reset();
134}
135
136const char *DWARFUnit::getCompilationDir() {
137 extractDIEsIfNeeded(true);
138 if (DieArray.empty())
Craig Topper2617dcc2014-04-15 06:32:26 +0000139 return nullptr;
140 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000141}
142
143uint64_t DWARFUnit::getDWOId() {
144 extractDIEsIfNeeded(true);
145 const uint64_t FailValue = -1ULL;
146 if (DieArray.empty())
147 return FailValue;
148 return DieArray[0]
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000149 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
David Blaikie07e22442013-09-23 22:44:40 +0000150}
151
152void DWARFUnit::setDIERelations() {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000153 if (DieArray.size() <= 1)
David Blaikie07e22442013-09-23 22:44:40 +0000154 return;
David Blaikie07e22442013-09-23 22:44:40 +0000155
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000156 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
157 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
158 for (auto &DIE : DieArray) {
159 if (SiblingChain) {
160 SiblingChain->setSibling(&DIE);
161 }
162 if (const DWARFAbbreviationDeclaration *AbbrDecl =
163 DIE.getAbbreviationDeclarationPtr()) {
164 // Normal DIE.
165 if (AbbrDecl->hasChildren()) {
166 ParentChain.push_back(&DIE);
167 SiblingChain = nullptr;
168 } else {
169 SiblingChain = &DIE;
170 }
David Blaikie07e22442013-09-23 22:44:40 +0000171 } else {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000172 // NULL entry terminates the sibling chain.
173 SiblingChain = ParentChain.back();
174 ParentChain.pop_back();
David Blaikie07e22442013-09-23 22:44:40 +0000175 }
176 }
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000177 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
178 assert(ParentChain.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000179}
180
181void DWARFUnit::extractDIEsToVector(
182 bool AppendCUDie, bool AppendNonCUDies,
183 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
184 if (!AppendCUDie && !AppendNonCUDies)
185 return;
186
187 // Set the offset to that of the first DIE and calculate the start of the
188 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000189 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000190 uint32_t NextCUOffset = getNextUnitOffset();
191 DWARFDebugInfoEntryMinimal DIE;
192 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000193 bool IsCUDie = true;
194
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000195 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
David Blaikie07e22442013-09-23 22:44:40 +0000196 if (IsCUDie) {
197 if (AppendCUDie)
198 Dies.push_back(DIE);
199 if (!AppendNonCUDies)
200 break;
201 // The average bytes per DIE entry has been seen to be
202 // around 14-20 so let's pre-reserve the needed memory for
203 // our DIE entries accordingly.
204 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
205 IsCUDie = false;
206 } else {
207 Dies.push_back(DIE);
208 }
209
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000210 if (const DWARFAbbreviationDeclaration *AbbrDecl =
211 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000212 // Normal DIE
213 if (AbbrDecl->hasChildren())
214 ++Depth;
215 } else {
216 // NULL DIE.
217 if (Depth > 0)
218 --Depth;
219 if (Depth == 0)
220 break; // We are done with this compile unit!
221 }
222 }
223
224 // Give a little bit of info if we encounter corrupt DWARF (our offset
225 // should always terminate at or before the start of the next compilation
226 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000227 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000228 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000229 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000230}
231
232size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
233 if ((CUDieOnly && DieArray.size() > 0) ||
234 DieArray.size() > 1)
235 return 0; // Already parsed.
236
237 bool HasCUDie = DieArray.size() > 0;
238 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
239
240 if (DieArray.empty())
241 return 0;
242
243 // If CU DIE was just parsed, copy several attribute values from it.
244 if (!HasCUDie) {
245 uint64_t BaseAddr =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000246 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
247 if (BaseAddr == -1ULL)
248 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
David Blaikie07e22442013-09-23 22:44:40 +0000249 setBaseAddress(BaseAddr);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000250 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
251 this, DW_AT_GNU_addr_base, 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000252 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
253 this, DW_AT_ranges_base, 0);
254 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
255 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000256 }
257
258 setDIERelations();
259 return DieArray.size();
260}
261
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000262DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
263 : DWOFile(), DWOContext(), DWOU(nullptr) {
264 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
265 if (!Obj)
266 return;
267 DWOFile = std::move(Obj.get());
268 DWOContext.reset(
Zachary Turner6489d7b2015-04-23 17:37:47 +0000269 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000270 if (DWOContext->getNumDWOCompileUnits() > 0)
271 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
272}
273
274bool DWARFUnit::parseDWO() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000275 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000276 return false;
277 extractDIEsIfNeeded(true);
278 if (DieArray.empty())
279 return false;
280 const char *DWOFileName =
Craig Topper2617dcc2014-04-15 06:32:26 +0000281 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
282 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000283 return false;
284 const char *CompilationDir =
Craig Topper2617dcc2014-04-15 06:32:26 +0000285 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000286 SmallString<16> AbsolutePath;
Craig Topper2617dcc2014-04-15 06:32:26 +0000287 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
David Blaikie07e22442013-09-23 22:44:40 +0000288 sys::path::append(AbsolutePath, CompilationDir);
289 }
290 sys::path::append(AbsolutePath, DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000291 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000292 DWARFUnit *DWOCU = DWO->getUnit();
293 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000294 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000295 DWO.reset();
296 return false;
297 }
298 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
299 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000300 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
Alexey Samsonovf0e40342014-06-12 18:52:35 +0000301 DWOCU->setRangesSection(RangeSection, DWORangesBase);
David Blaikie07e22442013-09-23 22:44:40 +0000302 return true;
303}
304
305void DWARFUnit::clearDIEs(bool KeepCUDie) {
306 if (DieArray.size() > (unsigned)KeepCUDie) {
307 // std::vectors never get any smaller when resized to a smaller size,
308 // or when clear() or erase() are called, the size will report that it
309 // is smaller, but the memory allocated remains intact (call capacity()
310 // to see this). So we need to create a temporary vector and swap the
311 // contents which will cause just the internal pointers to be swapped
312 // so that when temporary vector goes out of scope, it will destroy the
313 // contents.
314 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
315 DieArray.swap(TmpArray);
316 // Save at least the compile unit DIE
317 if (KeepCUDie)
318 DieArray.push_back(TmpArray.front());
319 }
320}
321
Alexey Samsonov762343d2014-04-18 17:25:46 +0000322void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000323 const auto *U = getUnitDIE();
324 if (U == nullptr)
325 return;
326 // First, check if unit DIE describes address ranges for the whole unit.
327 const auto &CUDIERanges = U->getAddressRanges(this);
Alexey Samsonov84e24232014-04-18 20:30:27 +0000328 if (!CUDIERanges.empty()) {
329 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
330 return;
331 }
332
David Blaikie07e22442013-09-23 22:44:40 +0000333 // This function is usually called if there in no .debug_aranges section
334 // in order to produce a compile unit level set of address ranges that
335 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
336 // all compile units to stay loaded when they weren't needed. So we can end
337 // up parsing the DWARF and then throwing them all away to keep memory usage
338 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000339 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
340 DieArray[0].collectChildrenAddressRanges(this, CURanges);
341
342 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000343 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000344 if (DWO.get())
345 DWO->getUnit()->collectAddressRanges(CURanges);
346 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000347 DWO.reset();
348
349 // Keep memory down by clearing DIEs if this generate function
350 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000351 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000352 clearDIEs(true);
353}
354
355const DWARFDebugInfoEntryMinimal *
356DWARFUnit::getSubprogramForAddress(uint64_t Address) {
357 extractDIEsIfNeeded(false);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000358 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
359 if (DIE.isSubprogramDIE() &&
360 DIE.addressRangeContainsAddress(this, Address)) {
361 return &DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000362 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000363 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000364 return nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000365}
366
367DWARFDebugInfoEntryInlinedChain
368DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
369 // First, find a subprogram that contains the given address (the root
370 // of inlined chain).
Craig Topper2617dcc2014-04-15 06:32:26 +0000371 const DWARFUnit *ChainCU = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000372 const DWARFDebugInfoEntryMinimal *SubprogramDIE =
373 getSubprogramForAddress(Address);
374 if (SubprogramDIE) {
375 ChainCU = this;
376 } else {
377 // Try to look for subprogram DIEs in the DWO file.
378 parseDWO();
379 if (DWO.get()) {
380 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
381 if (SubprogramDIE)
382 ChainCU = DWO->getUnit();
383 }
384 }
385
386 // Get inlined chain rooted at this subprogram DIE.
387 if (!SubprogramDIE)
388 return DWARFDebugInfoEntryInlinedChain();
389 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
390}
David Blaikie82641be2015-11-17 00:39:55 +0000391
392const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
393 DWARFSectionKind Kind) {
394 if (Kind == DW_SECT_INFO)
395 return Context.getCUIndex();
396 assert(Kind == DW_SECT_TYPES);
397 return Context.getTUIndex();
398}
399}