blob: 2bb7933e88bd7141eb8e705e064657d002f80e51 [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
10#include "DWARFUnit.h"
11#include "DWARFContext.h"
12#include "llvm/DebugInfo/DWARFFormValue.h"
13#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
17using namespace llvm;
18using namespace dwarf;
19
Frederic Risse10ccef2014-09-04 06:14:28 +000020DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFDebugAbbrev *DA,
21 StringRef IS, StringRef RS, StringRef SS, StringRef SOS,
22 StringRef AOS, const RelocAddrMap *M, bool LE)
23 : Context(DC), Abbrev(DA), InfoSection(IS), RangeSection(RS),
24 StringSection(SS), StringOffsetSection(SOS), AddrOffsetSection(AOS),
25 RelocMap(M), isLittleEndian(LE) {
David Blaikie07e22442013-09-23 22:44:40 +000026 clear();
27}
28
29DWARFUnit::~DWARFUnit() {
30}
31
32bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
33 uint64_t &Result) const {
34 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
35 if (AddrOffsetSection.size() < Offset + AddrSize)
36 return false;
37 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
38 Result = DA.getAddress(&Offset);
39 return true;
40}
41
42bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
43 uint32_t &Result) const {
44 // FIXME: string offset section entries are 8-byte for DWARF64.
45 const uint32_t ItemSize = 4;
46 uint32_t Offset = Index * ItemSize;
47 if (StringOffsetSection.size() < Offset + ItemSize)
48 return false;
49 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
50 Result = DA.getU32(&Offset);
51 return true;
52}
53
54bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
55 Length = debug_info.getU32(offset_ptr);
56 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000057 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie07e22442013-09-23 22:44:40 +000058 AddrSize = debug_info.getU8(offset_ptr);
59
Alexey Samsonov7682f812014-04-24 22:51:03 +000060 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
61 bool VersionOK = DWARFContext::isSupportedVersion(Version);
62 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +000063
Alexey Samsonov7682f812014-04-24 22:51:03 +000064 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +000065 return false;
66
Alexey Samsonov7682f812014-04-24 22:51:03 +000067 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
68 if (Abbrevs == nullptr)
69 return false;
70
David Blaikie07e22442013-09-23 22:44:40 +000071 return true;
72}
73
74bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
75 clear();
76
77 Offset = *offset_ptr;
78
79 if (debug_info.isValidOffset(*offset_ptr)) {
80 if (extractImpl(debug_info, offset_ptr))
81 return true;
82
83 // reset the offset to where we tried to parse from if anything went wrong
84 *offset_ptr = Offset;
85 }
86
87 return false;
88}
89
David Blaikie07e22442013-09-23 22:44:40 +000090bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
91 DWARFDebugRangeList &RangeList) const {
92 // Require that compile unit is extracted.
93 assert(DieArray.size() > 0);
94 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
95 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
96 return RangeList.extract(RangesData, &ActualRangeListOffset);
97}
98
99void DWARFUnit::clear() {
100 Offset = 0;
101 Length = 0;
102 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000103 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000104 AddrSize = 0;
105 BaseAddr = 0;
106 RangeSectionBase = 0;
107 AddrOffsetSectionBase = 0;
108 clearDIEs(false);
109 DWO.reset();
110}
111
112const char *DWARFUnit::getCompilationDir() {
113 extractDIEsIfNeeded(true);
114 if (DieArray.empty())
Craig Topper2617dcc2014-04-15 06:32:26 +0000115 return nullptr;
116 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000117}
118
119uint64_t DWARFUnit::getDWOId() {
120 extractDIEsIfNeeded(true);
121 const uint64_t FailValue = -1ULL;
122 if (DieArray.empty())
123 return FailValue;
124 return DieArray[0]
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000125 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
David Blaikie07e22442013-09-23 22:44:40 +0000126}
127
128void DWARFUnit::setDIERelations() {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000129 if (DieArray.size() <= 1)
David Blaikie07e22442013-09-23 22:44:40 +0000130 return;
David Blaikie07e22442013-09-23 22:44:40 +0000131
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000132 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
133 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
134 for (auto &DIE : DieArray) {
135 if (SiblingChain) {
136 SiblingChain->setSibling(&DIE);
137 }
138 if (const DWARFAbbreviationDeclaration *AbbrDecl =
139 DIE.getAbbreviationDeclarationPtr()) {
140 // Normal DIE.
141 if (AbbrDecl->hasChildren()) {
142 ParentChain.push_back(&DIE);
143 SiblingChain = nullptr;
144 } else {
145 SiblingChain = &DIE;
146 }
David Blaikie07e22442013-09-23 22:44:40 +0000147 } else {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000148 // NULL entry terminates the sibling chain.
149 SiblingChain = ParentChain.back();
150 ParentChain.pop_back();
David Blaikie07e22442013-09-23 22:44:40 +0000151 }
152 }
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000153 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
154 assert(ParentChain.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000155}
156
157void DWARFUnit::extractDIEsToVector(
158 bool AppendCUDie, bool AppendNonCUDies,
159 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
160 if (!AppendCUDie && !AppendNonCUDies)
161 return;
162
163 // Set the offset to that of the first DIE and calculate the start of the
164 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000165 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000166 uint32_t NextCUOffset = getNextUnitOffset();
167 DWARFDebugInfoEntryMinimal DIE;
168 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000169 bool IsCUDie = true;
170
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000171 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
David Blaikie07e22442013-09-23 22:44:40 +0000172 if (IsCUDie) {
173 if (AppendCUDie)
174 Dies.push_back(DIE);
175 if (!AppendNonCUDies)
176 break;
177 // The average bytes per DIE entry has been seen to be
178 // around 14-20 so let's pre-reserve the needed memory for
179 // our DIE entries accordingly.
180 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
181 IsCUDie = false;
182 } else {
183 Dies.push_back(DIE);
184 }
185
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000186 if (const DWARFAbbreviationDeclaration *AbbrDecl =
187 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000188 // Normal DIE
189 if (AbbrDecl->hasChildren())
190 ++Depth;
191 } else {
192 // NULL DIE.
193 if (Depth > 0)
194 --Depth;
195 if (Depth == 0)
196 break; // We are done with this compile unit!
197 }
198 }
199
200 // Give a little bit of info if we encounter corrupt DWARF (our offset
201 // should always terminate at or before the start of the next compilation
202 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000203 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000204 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000205 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000206}
207
208size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
209 if ((CUDieOnly && DieArray.size() > 0) ||
210 DieArray.size() > 1)
211 return 0; // Already parsed.
212
213 bool HasCUDie = DieArray.size() > 0;
214 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
215
216 if (DieArray.empty())
217 return 0;
218
219 // If CU DIE was just parsed, copy several attribute values from it.
220 if (!HasCUDie) {
221 uint64_t BaseAddr =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000222 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
223 if (BaseAddr == -1ULL)
224 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
David Blaikie07e22442013-09-23 22:44:40 +0000225 setBaseAddress(BaseAddr);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000226 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
227 this, DW_AT_GNU_addr_base, 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000228 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
229 this, DW_AT_ranges_base, 0);
230 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
231 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000232 }
233
234 setDIERelations();
235 return DieArray.size();
236}
237
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000238DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
239 : DWOFile(), DWOContext(), DWOU(nullptr) {
240 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
241 if (!Obj)
242 return;
243 DWOFile = std::move(Obj.get());
244 DWOContext.reset(
245 cast<DWARFContext>(DIContext::getDWARFContext(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000246 if (DWOContext->getNumDWOCompileUnits() > 0)
247 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
248}
249
250bool DWARFUnit::parseDWO() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000251 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000252 return false;
253 extractDIEsIfNeeded(true);
254 if (DieArray.empty())
255 return false;
256 const char *DWOFileName =
Craig Topper2617dcc2014-04-15 06:32:26 +0000257 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
258 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000259 return false;
260 const char *CompilationDir =
Craig Topper2617dcc2014-04-15 06:32:26 +0000261 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000262 SmallString<16> AbsolutePath;
Craig Topper2617dcc2014-04-15 06:32:26 +0000263 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
David Blaikie07e22442013-09-23 22:44:40 +0000264 sys::path::append(AbsolutePath, CompilationDir);
265 }
266 sys::path::append(AbsolutePath, DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000267 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000268 DWARFUnit *DWOCU = DWO->getUnit();
269 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000270 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000271 DWO.reset();
272 return false;
273 }
274 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
275 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000276 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
Alexey Samsonovf0e40342014-06-12 18:52:35 +0000277 DWOCU->setRangesSection(RangeSection, DWORangesBase);
David Blaikie07e22442013-09-23 22:44:40 +0000278 return true;
279}
280
281void DWARFUnit::clearDIEs(bool KeepCUDie) {
282 if (DieArray.size() > (unsigned)KeepCUDie) {
283 // std::vectors never get any smaller when resized to a smaller size,
284 // or when clear() or erase() are called, the size will report that it
285 // is smaller, but the memory allocated remains intact (call capacity()
286 // to see this). So we need to create a temporary vector and swap the
287 // contents which will cause just the internal pointers to be swapped
288 // so that when temporary vector goes out of scope, it will destroy the
289 // contents.
290 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
291 DieArray.swap(TmpArray);
292 // Save at least the compile unit DIE
293 if (KeepCUDie)
294 DieArray.push_back(TmpArray.front());
295 }
296}
297
Alexey Samsonov762343d2014-04-18 17:25:46 +0000298void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Alexey Samsonov84e24232014-04-18 20:30:27 +0000299 // First, check if CU DIE describes address ranges for the unit.
300 const auto &CUDIERanges = getCompileUnitDIE()->getAddressRanges(this);
301 if (!CUDIERanges.empty()) {
302 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
303 return;
304 }
305
David Blaikie07e22442013-09-23 22:44:40 +0000306 // This function is usually called if there in no .debug_aranges section
307 // in order to produce a compile unit level set of address ranges that
308 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
309 // all compile units to stay loaded when they weren't needed. So we can end
310 // up parsing the DWARF and then throwing them all away to keep memory usage
311 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000312 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
313 DieArray[0].collectChildrenAddressRanges(this, CURanges);
314
315 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000316 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000317 if (DWO.get())
318 DWO->getUnit()->collectAddressRanges(CURanges);
319 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000320 DWO.reset();
321
322 // Keep memory down by clearing DIEs if this generate function
323 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000324 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000325 clearDIEs(true);
326}
327
328const DWARFDebugInfoEntryMinimal *
329DWARFUnit::getSubprogramForAddress(uint64_t Address) {
330 extractDIEsIfNeeded(false);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000331 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
332 if (DIE.isSubprogramDIE() &&
333 DIE.addressRangeContainsAddress(this, Address)) {
334 return &DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000335 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000336 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000337 return nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000338}
339
340DWARFDebugInfoEntryInlinedChain
341DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
342 // First, find a subprogram that contains the given address (the root
343 // of inlined chain).
Craig Topper2617dcc2014-04-15 06:32:26 +0000344 const DWARFUnit *ChainCU = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000345 const DWARFDebugInfoEntryMinimal *SubprogramDIE =
346 getSubprogramForAddress(Address);
347 if (SubprogramDIE) {
348 ChainCU = this;
349 } else {
350 // Try to look for subprogram DIEs in the DWO file.
351 parseDWO();
352 if (DWO.get()) {
353 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
354 if (SubprogramDIE)
355 ChainCU = DWO->getUnit();
356 }
357 }
358
359 // Get inlined chain rooted at this subprogram DIE.
360 if (!SubprogramDIE)
361 return DWARFDebugInfoEntryInlinedChain();
362 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
363}