blob: faf385c147413840a1ddd3039a4e2e27328f3c9d [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,
Frederic Riss4e126a02014-09-15 07:50:27 +000022 StringRef AOS, const RelocAddrMap *M, bool LE,
23 const DWARFUnitSectionBase& UnitSection)
Frederic Risse10ccef2014-09-04 06:14:28 +000024 : Context(DC), Abbrev(DA), InfoSection(IS), RangeSection(RS),
25 StringSection(SS), StringOffsetSection(SOS), AddrOffsetSection(AOS),
Frederic Riss4e126a02014-09-15 07:50:27 +000026 RelocMap(M), isLittleEndian(LE), UnitSection(UnitSection) {
David Blaikie07e22442013-09-23 22:44:40 +000027 clear();
28}
29
30DWARFUnit::~DWARFUnit() {
31}
32
33bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
34 uint64_t &Result) const {
35 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
36 if (AddrOffsetSection.size() < Offset + AddrSize)
37 return false;
38 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
39 Result = DA.getAddress(&Offset);
40 return true;
41}
42
43bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
44 uint32_t &Result) const {
45 // FIXME: string offset section entries are 8-byte for DWARF64.
46 const uint32_t ItemSize = 4;
47 uint32_t Offset = Index * ItemSize;
48 if (StringOffsetSection.size() < Offset + ItemSize)
49 return false;
50 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
51 Result = DA.getU32(&Offset);
52 return true;
53}
54
55bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
56 Length = debug_info.getU32(offset_ptr);
57 Version = debug_info.getU16(offset_ptr);
Alexey Samsonov7682f812014-04-24 22:51:03 +000058 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
David Blaikie07e22442013-09-23 22:44:40 +000059 AddrSize = debug_info.getU8(offset_ptr);
60
Alexey Samsonov7682f812014-04-24 22:51:03 +000061 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
62 bool VersionOK = DWARFContext::isSupportedVersion(Version);
63 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
David Blaikie07e22442013-09-23 22:44:40 +000064
Alexey Samsonov7682f812014-04-24 22:51:03 +000065 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +000066 return false;
67
Alexey Samsonov7682f812014-04-24 22:51:03 +000068 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
69 if (Abbrevs == nullptr)
70 return false;
71
David Blaikie07e22442013-09-23 22:44:40 +000072 return true;
73}
74
75bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
76 clear();
77
78 Offset = *offset_ptr;
79
80 if (debug_info.isValidOffset(*offset_ptr)) {
81 if (extractImpl(debug_info, offset_ptr))
82 return true;
83
84 // reset the offset to where we tried to parse from if anything went wrong
85 *offset_ptr = Offset;
86 }
87
88 return false;
89}
90
David Blaikie07e22442013-09-23 22:44:40 +000091bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
92 DWARFDebugRangeList &RangeList) const {
93 // Require that compile unit is extracted.
94 assert(DieArray.size() > 0);
95 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
96 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
97 return RangeList.extract(RangesData, &ActualRangeListOffset);
98}
99
100void DWARFUnit::clear() {
101 Offset = 0;
102 Length = 0;
103 Version = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000104 Abbrevs = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000105 AddrSize = 0;
106 BaseAddr = 0;
107 RangeSectionBase = 0;
108 AddrOffsetSectionBase = 0;
109 clearDIEs(false);
110 DWO.reset();
111}
112
113const char *DWARFUnit::getCompilationDir() {
114 extractDIEsIfNeeded(true);
115 if (DieArray.empty())
Craig Topper2617dcc2014-04-15 06:32:26 +0000116 return nullptr;
117 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000118}
119
120uint64_t DWARFUnit::getDWOId() {
121 extractDIEsIfNeeded(true);
122 const uint64_t FailValue = -1ULL;
123 if (DieArray.empty())
124 return FailValue;
125 return DieArray[0]
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000126 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
David Blaikie07e22442013-09-23 22:44:40 +0000127}
128
129void DWARFUnit::setDIERelations() {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000130 if (DieArray.size() <= 1)
David Blaikie07e22442013-09-23 22:44:40 +0000131 return;
David Blaikie07e22442013-09-23 22:44:40 +0000132
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000133 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
134 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
135 for (auto &DIE : DieArray) {
136 if (SiblingChain) {
137 SiblingChain->setSibling(&DIE);
138 }
139 if (const DWARFAbbreviationDeclaration *AbbrDecl =
140 DIE.getAbbreviationDeclarationPtr()) {
141 // Normal DIE.
142 if (AbbrDecl->hasChildren()) {
143 ParentChain.push_back(&DIE);
144 SiblingChain = nullptr;
145 } else {
146 SiblingChain = &DIE;
147 }
David Blaikie07e22442013-09-23 22:44:40 +0000148 } else {
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000149 // NULL entry terminates the sibling chain.
150 SiblingChain = ParentChain.back();
151 ParentChain.pop_back();
David Blaikie07e22442013-09-23 22:44:40 +0000152 }
153 }
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000154 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
155 assert(ParentChain.empty());
David Blaikie07e22442013-09-23 22:44:40 +0000156}
157
158void DWARFUnit::extractDIEsToVector(
159 bool AppendCUDie, bool AppendNonCUDies,
160 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
161 if (!AppendCUDie && !AppendNonCUDies)
162 return;
163
164 // Set the offset to that of the first DIE and calculate the start of the
165 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000166 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000167 uint32_t NextCUOffset = getNextUnitOffset();
168 DWARFDebugInfoEntryMinimal DIE;
169 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000170 bool IsCUDie = true;
171
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000172 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
David Blaikie07e22442013-09-23 22:44:40 +0000173 if (IsCUDie) {
174 if (AppendCUDie)
175 Dies.push_back(DIE);
176 if (!AppendNonCUDies)
177 break;
178 // The average bytes per DIE entry has been seen to be
179 // around 14-20 so let's pre-reserve the needed memory for
180 // our DIE entries accordingly.
181 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
182 IsCUDie = false;
183 } else {
184 Dies.push_back(DIE);
185 }
186
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000187 if (const DWARFAbbreviationDeclaration *AbbrDecl =
188 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000189 // Normal DIE
190 if (AbbrDecl->hasChildren())
191 ++Depth;
192 } else {
193 // NULL DIE.
194 if (Depth > 0)
195 --Depth;
196 if (Depth == 0)
197 break; // We are done with this compile unit!
198 }
199 }
200
201 // Give a little bit of info if we encounter corrupt DWARF (our offset
202 // should always terminate at or before the start of the next compilation
203 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000204 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000205 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000206 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000207}
208
209size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
210 if ((CUDieOnly && DieArray.size() > 0) ||
211 DieArray.size() > 1)
212 return 0; // Already parsed.
213
214 bool HasCUDie = DieArray.size() > 0;
215 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
216
217 if (DieArray.empty())
218 return 0;
219
220 // If CU DIE was just parsed, copy several attribute values from it.
221 if (!HasCUDie) {
222 uint64_t BaseAddr =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000223 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
224 if (BaseAddr == -1ULL)
225 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
David Blaikie07e22442013-09-23 22:44:40 +0000226 setBaseAddress(BaseAddr);
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000227 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
228 this, DW_AT_GNU_addr_base, 0);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000229 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
230 this, DW_AT_ranges_base, 0);
231 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
232 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000233 }
234
235 setDIERelations();
236 return DieArray.size();
237}
238
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000239DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
240 : DWOFile(), DWOContext(), DWOU(nullptr) {
241 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
242 if (!Obj)
243 return;
244 DWOFile = std::move(Obj.get());
245 DWOContext.reset(
246 cast<DWARFContext>(DIContext::getDWARFContext(*DWOFile.getBinary())));
David Blaikie07e22442013-09-23 22:44:40 +0000247 if (DWOContext->getNumDWOCompileUnits() > 0)
248 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
249}
250
251bool DWARFUnit::parseDWO() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000252 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000253 return false;
254 extractDIEsIfNeeded(true);
255 if (DieArray.empty())
256 return false;
257 const char *DWOFileName =
Craig Topper2617dcc2014-04-15 06:32:26 +0000258 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
259 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000260 return false;
261 const char *CompilationDir =
Craig Topper2617dcc2014-04-15 06:32:26 +0000262 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000263 SmallString<16> AbsolutePath;
Craig Topper2617dcc2014-04-15 06:32:26 +0000264 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
David Blaikie07e22442013-09-23 22:44:40 +0000265 sys::path::append(AbsolutePath, CompilationDir);
266 }
267 sys::path::append(AbsolutePath, DWOFileName);
Alexey Samsonovd3e12132014-09-05 19:29:45 +0000268 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
David Blaikie07e22442013-09-23 22:44:40 +0000269 DWARFUnit *DWOCU = DWO->getUnit();
270 // Verify that compile unit in .dwo file is valid.
Craig Topper2617dcc2014-04-15 06:32:26 +0000271 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
David Blaikie07e22442013-09-23 22:44:40 +0000272 DWO.reset();
273 return false;
274 }
275 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
276 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000277 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
Alexey Samsonovf0e40342014-06-12 18:52:35 +0000278 DWOCU->setRangesSection(RangeSection, DWORangesBase);
David Blaikie07e22442013-09-23 22:44:40 +0000279 return true;
280}
281
282void DWARFUnit::clearDIEs(bool KeepCUDie) {
283 if (DieArray.size() > (unsigned)KeepCUDie) {
284 // std::vectors never get any smaller when resized to a smaller size,
285 // or when clear() or erase() are called, the size will report that it
286 // is smaller, but the memory allocated remains intact (call capacity()
287 // to see this). So we need to create a temporary vector and swap the
288 // contents which will cause just the internal pointers to be swapped
289 // so that when temporary vector goes out of scope, it will destroy the
290 // contents.
291 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
292 DieArray.swap(TmpArray);
293 // Save at least the compile unit DIE
294 if (KeepCUDie)
295 DieArray.push_back(TmpArray.front());
296 }
297}
298
Alexey Samsonov762343d2014-04-18 17:25:46 +0000299void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Alexey Samsonov84e24232014-04-18 20:30:27 +0000300 // First, check if CU DIE describes address ranges for the unit.
301 const auto &CUDIERanges = getCompileUnitDIE()->getAddressRanges(this);
302 if (!CUDIERanges.empty()) {
303 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
304 return;
305 }
306
David Blaikie07e22442013-09-23 22:44:40 +0000307 // This function is usually called if there in no .debug_aranges section
308 // in order to produce a compile unit level set of address ranges that
309 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
310 // all compile units to stay loaded when they weren't needed. So we can end
311 // up parsing the DWARF and then throwing them all away to keep memory usage
312 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000313 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
314 DieArray[0].collectChildrenAddressRanges(this, CURanges);
315
316 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000317 bool DWOCreated = parseDWO();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000318 if (DWO.get())
319 DWO->getUnit()->collectAddressRanges(CURanges);
320 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000321 DWO.reset();
322
323 // Keep memory down by clearing DIEs if this generate function
324 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000325 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000326 clearDIEs(true);
327}
328
329const DWARFDebugInfoEntryMinimal *
330DWARFUnit::getSubprogramForAddress(uint64_t Address) {
331 extractDIEsIfNeeded(false);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000332 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
333 if (DIE.isSubprogramDIE() &&
334 DIE.addressRangeContainsAddress(this, Address)) {
335 return &DIE;
David Blaikie07e22442013-09-23 22:44:40 +0000336 }
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000337 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000338 return nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000339}
340
341DWARFDebugInfoEntryInlinedChain
342DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
343 // First, find a subprogram that contains the given address (the root
344 // of inlined chain).
Craig Topper2617dcc2014-04-15 06:32:26 +0000345 const DWARFUnit *ChainCU = nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000346 const DWARFDebugInfoEntryMinimal *SubprogramDIE =
347 getSubprogramForAddress(Address);
348 if (SubprogramDIE) {
349 ChainCU = this;
350 } else {
351 // Try to look for subprogram DIEs in the DWO file.
352 parseDWO();
353 if (DWO.get()) {
354 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
355 if (SubprogramDIE)
356 ChainCU = DWO->getUnit();
357 }
358 }
359
360 // Get inlined chain rooted at this subprogram DIE.
361 if (!SubprogramDIE)
362 return DWARFDebugInfoEntryInlinedChain();
363 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
364}