1 //===-- DWARFUnit.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
14 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
18 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/DataExtractor.h"
23 #include "llvm/Support/Path.h"
32 using namespace dwarf;
34 void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
35 parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
36 C.getStringSection(), StringRef(), C.getAddrSection(),
37 C.getLineSection().Data, C.isLittleEndian(), false);
40 void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
41 const DWARFSection &DWOSection,
42 DWARFUnitIndex *Index) {
43 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
44 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
45 C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
49 DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
50 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
51 StringRef SOS, StringRef AOS, StringRef LS, bool LE,
52 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
53 const DWARFUnitIndex::Entry *IndexEntry)
54 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
55 LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
57 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
58 return SOS.slice(C->Offset, C->Offset + C->Length);
61 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
62 UnitSection(UnitSection), IndexEntry(IndexEntry) {
66 DWARFUnit::~DWARFUnit() = default;
68 bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
69 uint64_t &Result) const {
70 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
71 if (AddrOffsetSection.size() < Offset + AddrSize)
73 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
74 Result = DA.getAddress(&Offset);
78 bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
79 uint32_t &Result) const {
80 // FIXME: string offset section entries are 8-byte for DWARF64.
81 const uint32_t ItemSize = 4;
82 uint32_t Offset = Index * ItemSize;
83 if (StringOffsetSection.size() < Offset + ItemSize)
85 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
86 Result = DA.getU32(&Offset);
90 bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
91 Length = debug_info.getU32(offset_ptr);
92 Version = debug_info.getU16(offset_ptr);
95 UnitType = debug_info.getU8(offset_ptr);
96 AddrSize = debug_info.getU8(offset_ptr);
97 AbbrOffset = debug_info.getU32(offset_ptr);
99 AbbrOffset = debug_info.getU32(offset_ptr);
100 AddrSize = debug_info.getU8(offset_ptr);
105 auto *UnitContrib = IndexEntry->getOffset();
106 if (!UnitContrib || UnitContrib->Length != (Length + 4))
108 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
111 AbbrOffset = AbbrEntry->Offset;
114 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
115 bool VersionOK = DWARFContext::isSupportedVersion(Version);
116 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
118 if (!LengthOK || !VersionOK || !AddrSizeOK)
121 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
122 return Abbrevs != nullptr;
125 bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
128 Offset = *offset_ptr;
130 if (debug_info.isValidOffset(*offset_ptr)) {
131 if (extractImpl(debug_info, offset_ptr))
134 // reset the offset to where we tried to parse from if anything went wrong
135 *offset_ptr = Offset;
141 bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
142 DWARFDebugRangeList &RangeList) const {
143 // Require that compile unit is extracted.
144 assert(!DieArray.empty());
145 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
146 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
147 return RangeList.extract(RangesData, &ActualRangeListOffset);
150 void DWARFUnit::clear() {
157 RangeSectionBase = 0;
158 AddrOffsetSectionBase = 0;
163 const char *DWARFUnit::getCompilationDir() {
164 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
167 Optional<uint64_t> DWARFUnit::getDWOId() {
168 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
171 void DWARFUnit::extractDIEsToVector(
172 bool AppendCUDie, bool AppendNonCUDies,
173 std::vector<DWARFDebugInfoEntry> &Dies) const {
174 if (!AppendCUDie && !AppendNonCUDies)
177 // Set the offset to that of the first DIE and calculate the start of the
178 // next compilation unit header.
179 uint32_t DIEOffset = Offset + getHeaderSize();
180 uint32_t NextCUOffset = getNextUnitOffset();
181 DWARFDebugInfoEntry DIE;
182 DataExtractor DebugInfoData = getDebugInfoExtractor();
186 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
191 if (!AppendNonCUDies)
193 // The average bytes per DIE entry has been seen to be
194 // around 14-20 so let's pre-reserve the needed memory for
195 // our DIE entries accordingly.
196 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
202 if (const DWARFAbbreviationDeclaration *AbbrDecl =
203 DIE.getAbbreviationDeclarationPtr()) {
205 if (AbbrDecl->hasChildren())
212 break; // We are done with this compile unit!
216 // Give a little bit of info if we encounter corrupt DWARF (our offset
217 // should always terminate at or before the start of the next compilation
219 if (DIEOffset > NextCUOffset)
220 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
221 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
224 size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
225 if ((CUDieOnly && !DieArray.empty()) ||
227 return 0; // Already parsed.
229 bool HasCUDie = !DieArray.empty();
230 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
232 if (DieArray.empty())
235 // If CU DIE was just parsed, copy several attribute values from it.
237 DWARFDie UnitDie = getUnitDIE();
238 auto BaseAddr = toAddress(UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}));
240 setBaseAddress(*BaseAddr);
241 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
242 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
243 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
244 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
247 return DieArray.size();
250 DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath) {
251 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
253 // TODO: Actually report errors helpfully.
254 consumeError(Obj.takeError());
257 DWOFile = std::move(Obj.get());
259 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
260 if (DWOContext->getNumDWOCompileUnits() > 0)
261 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
264 bool DWARFUnit::parseDWO() {
269 DWARFDie UnitDie = getUnitDIE();
272 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
275 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
276 SmallString<16> AbsolutePath;
277 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
279 sys::path::append(AbsolutePath, *CompilationDir);
281 sys::path::append(AbsolutePath, *DWOFileName);
282 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
283 DWARFUnit *DWOCU = DWO->getUnit();
284 // Verify that compile unit in .dwo file is valid.
285 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
289 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
290 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
291 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
292 DWOCU->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
296 void DWARFUnit::clearDIEs(bool KeepCUDie) {
297 if (DieArray.size() > (unsigned)KeepCUDie) {
298 // std::vectors never get any smaller when resized to a smaller size,
299 // or when clear() or erase() are called, the size will report that it
300 // is smaller, but the memory allocated remains intact (call capacity()
301 // to see this). So we need to create a temporary vector and swap the
302 // contents which will cause just the internal pointers to be swapped
303 // so that when temporary vector goes out of scope, it will destroy the
305 std::vector<DWARFDebugInfoEntry> TmpArray;
306 DieArray.swap(TmpArray);
307 // Save at least the compile unit DIE
309 DieArray.push_back(TmpArray.front());
313 void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
314 DWARFDie UnitDie = getUnitDIE();
317 // First, check if unit DIE describes address ranges for the whole unit.
318 const auto &CUDIERanges = UnitDie.getAddressRanges();
319 if (!CUDIERanges.empty()) {
320 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
324 // This function is usually called if there in no .debug_aranges section
325 // in order to produce a compile unit level set of address ranges that
326 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
327 // all compile units to stay loaded when they weren't needed. So we can end
328 // up parsing the DWARF and then throwing them all away to keep memory usage
330 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
331 getUnitDIE().collectChildrenAddressRanges(CURanges);
333 // Collect address ranges from DIEs in .dwo if necessary.
334 bool DWOCreated = parseDWO();
336 DWO->getUnit()->collectAddressRanges(CURanges);
340 // Keep memory down by clearing DIEs if this generate function
341 // caused them to be parsed.
347 DWARFUnit::getSubprogramForAddress(uint64_t Address) {
348 extractDIEsIfNeeded(false);
349 for (const DWARFDebugInfoEntry &D : DieArray) {
350 DWARFDie DIE(this, &D);
351 if (DIE.isSubprogramDIE() &&
352 DIE.addressRangeContainsAddress(Address)) {
360 DWARFUnit::getInlinedChainForAddress(uint64_t Address,
361 SmallVectorImpl<DWARFDie> &InlinedChain) {
362 // First, find a subprogram that contains the given address (the root
363 // of inlined chain).
364 DWARFDie SubprogramDIE;
365 // Try to look for subprogram DIEs in the DWO file.
368 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
370 SubprogramDIE = getSubprogramForAddress(Address);
372 // Get inlined chain rooted at this subprogram DIE.
374 SubprogramDIE.getInlinedChainForAddress(Address, InlinedChain);
376 InlinedChain.clear();
379 const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
380 DWARFSectionKind Kind) {
381 if (Kind == DW_SECT_INFO)
382 return Context.getCUIndex();
383 assert(Kind == DW_SECT_TYPES);
384 return Context.getTUIndex();
387 DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
390 const uint32_t Depth = Die->getDepth();
391 // Unit DIEs always have a depth of zero and never have parents.
394 // Depth of 1 always means parent is the compile/type unit.
397 // Look for previous DIE with a depth that is one less than the Die's depth.
398 const uint32_t ParentDepth = Depth - 1;
399 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
400 if (DieArray[I].getDepth() == ParentDepth)
401 return DWARFDie(this, &DieArray[I]);
406 DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
409 uint32_t Depth = Die->getDepth();
410 // Unit DIEs always have a depth of zero and never have siblings.
413 // NULL DIEs don't have siblings.
414 if (Die->getAbbreviationDeclarationPtr() == nullptr)
417 // Find the next DIE whose depth is the same as the Die's depth.
418 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
420 if (DieArray[I].getDepth() == Depth)
421 return DWARFDie(this, &DieArray[I]);