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/DebugInfo/DWARF/DWARFUnit.h"
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/Path.h"
18 using namespace dwarf;
20 void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
21 parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
22 C.getStringSection(), StringRef(), C.getAddrSection(),
23 C.getLineSection().Data, C.isLittleEndian(), false);
26 void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
27 const DWARFSection &DWOSection,
28 DWARFUnitIndex *Index) {
29 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
30 C.getStringDWOSection(), C.getStringOffsetDWOSection(),
31 C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
35 DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
36 const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
37 StringRef SOS, StringRef AOS, StringRef LS, bool LE,
38 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
39 const DWARFUnitIndex::Entry *IndexEntry)
40 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
41 LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
43 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
44 return SOS.slice(C->Offset, C->Offset + C->Length);
47 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
48 UnitSection(UnitSection), IndexEntry(IndexEntry) {
52 DWARFUnit::~DWARFUnit() {
55 bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
56 uint64_t &Result) const {
57 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
58 if (AddrOffsetSection.size() < Offset + AddrSize)
60 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
61 Result = DA.getAddress(&Offset);
65 bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
66 uint32_t &Result) const {
67 // FIXME: string offset section entries are 8-byte for DWARF64.
68 const uint32_t ItemSize = 4;
69 uint32_t Offset = Index * ItemSize;
70 if (StringOffsetSection.size() < Offset + ItemSize)
72 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
73 Result = DA.getU32(&Offset);
77 bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
78 Length = debug_info.getU32(offset_ptr);
79 Version = debug_info.getU16(offset_ptr);
80 uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
84 auto *UnitContrib = IndexEntry->getOffset();
85 if (!UnitContrib || UnitContrib->Length != (Length + 4))
87 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
90 AbbrOffset = AbbrEntry->Offset;
92 AddrSize = debug_info.getU8(offset_ptr);
94 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
95 bool VersionOK = DWARFContext::isSupportedVersion(Version);
96 bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
98 if (!LengthOK || !VersionOK || !AddrSizeOK)
101 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
102 return Abbrevs != nullptr;
105 bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
108 Offset = *offset_ptr;
110 if (debug_info.isValidOffset(*offset_ptr)) {
111 if (extractImpl(debug_info, offset_ptr))
114 // reset the offset to where we tried to parse from if anything went wrong
115 *offset_ptr = Offset;
121 bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
122 DWARFDebugRangeList &RangeList) const {
123 // Require that compile unit is extracted.
124 assert(DieArray.size() > 0);
125 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
126 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
127 return RangeList.extract(RangesData, &ActualRangeListOffset);
130 void DWARFUnit::clear() {
137 RangeSectionBase = 0;
138 AddrOffsetSectionBase = 0;
143 const char *DWARFUnit::getCompilationDir() {
144 extractDIEsIfNeeded(true);
145 if (DieArray.empty())
147 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
150 uint64_t DWARFUnit::getDWOId() {
151 extractDIEsIfNeeded(true);
152 const uint64_t FailValue = -1ULL;
153 if (DieArray.empty())
156 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
159 void DWARFUnit::setDIERelations() {
160 if (DieArray.size() <= 1)
163 std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
164 DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
165 for (auto &DIE : DieArray) {
167 SiblingChain->setSibling(&DIE);
169 if (const DWARFAbbreviationDeclaration *AbbrDecl =
170 DIE.getAbbreviationDeclarationPtr()) {
172 if (AbbrDecl->hasChildren()) {
173 ParentChain.push_back(&DIE);
174 SiblingChain = nullptr;
179 // NULL entry terminates the sibling chain.
180 SiblingChain = ParentChain.back();
181 ParentChain.pop_back();
184 assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
185 assert(ParentChain.empty());
188 void DWARFUnit::extractDIEsToVector(
189 bool AppendCUDie, bool AppendNonCUDies,
190 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
191 if (!AppendCUDie && !AppendNonCUDies)
194 // Set the offset to that of the first DIE and calculate the start of the
195 // next compilation unit header.
196 uint32_t DIEOffset = Offset + getHeaderSize();
197 uint32_t NextCUOffset = getNextUnitOffset();
198 DWARFDebugInfoEntryMinimal DIE;
202 while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
206 if (!AppendNonCUDies)
208 // The average bytes per DIE entry has been seen to be
209 // around 14-20 so let's pre-reserve the needed memory for
210 // our DIE entries accordingly.
211 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
217 if (const DWARFAbbreviationDeclaration *AbbrDecl =
218 DIE.getAbbreviationDeclarationPtr()) {
220 if (AbbrDecl->hasChildren())
227 break; // We are done with this compile unit!
231 // Give a little bit of info if we encounter corrupt DWARF (our offset
232 // should always terminate at or before the start of the next compilation
234 if (DIEOffset > NextCUOffset)
235 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
236 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
239 size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
240 if ((CUDieOnly && DieArray.size() > 0) ||
242 return 0; // Already parsed.
244 bool HasCUDie = DieArray.size() > 0;
245 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
247 if (DieArray.empty())
250 // If CU DIE was just parsed, copy several attribute values from it.
253 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
254 if (BaseAddr == -1ULL)
255 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
256 setBaseAddress(BaseAddr);
257 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
258 this, DW_AT_GNU_addr_base, 0);
259 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
260 this, DW_AT_ranges_base, 0);
261 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
262 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
266 return DieArray.size();
269 DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
270 : DWOFile(), DWOContext(), DWOU(nullptr) {
271 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
273 // TODO: Actually report errors helpfully.
274 consumeError(Obj.takeError());
277 DWOFile = std::move(Obj.get());
279 cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
280 if (DWOContext->getNumDWOCompileUnits() > 0)
281 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
284 bool DWARFUnit::parseDWO() {
289 extractDIEsIfNeeded(true);
290 if (DieArray.empty())
292 const char *DWOFileName =
293 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
296 const char *CompilationDir =
297 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
298 SmallString<16> AbsolutePath;
299 if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
300 sys::path::append(AbsolutePath, CompilationDir);
302 sys::path::append(AbsolutePath, DWOFileName);
303 DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
304 DWARFUnit *DWOCU = DWO->getUnit();
305 // Verify that compile unit in .dwo file is valid.
306 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
310 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
311 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
312 uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0);
313 DWOCU->setRangesSection(RangeSection, DWORangesBase);
317 void DWARFUnit::clearDIEs(bool KeepCUDie) {
318 if (DieArray.size() > (unsigned)KeepCUDie) {
319 // std::vectors never get any smaller when resized to a smaller size,
320 // or when clear() or erase() are called, the size will report that it
321 // is smaller, but the memory allocated remains intact (call capacity()
322 // to see this). So we need to create a temporary vector and swap the
323 // contents which will cause just the internal pointers to be swapped
324 // so that when temporary vector goes out of scope, it will destroy the
326 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
327 DieArray.swap(TmpArray);
328 // Save at least the compile unit DIE
330 DieArray.push_back(TmpArray.front());
334 void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
335 const auto *U = getUnitDIE();
338 // First, check if unit DIE describes address ranges for the whole unit.
339 const auto &CUDIERanges = U->getAddressRanges(this);
340 if (!CUDIERanges.empty()) {
341 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
345 // This function is usually called if there in no .debug_aranges section
346 // in order to produce a compile unit level set of address ranges that
347 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
348 // all compile units to stay loaded when they weren't needed. So we can end
349 // up parsing the DWARF and then throwing them all away to keep memory usage
351 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
352 DieArray[0].collectChildrenAddressRanges(this, CURanges);
354 // Collect address ranges from DIEs in .dwo if necessary.
355 bool DWOCreated = parseDWO();
357 DWO->getUnit()->collectAddressRanges(CURanges);
361 // Keep memory down by clearing DIEs if this generate function
362 // caused them to be parsed.
367 const DWARFDebugInfoEntryMinimal *
368 DWARFUnit::getSubprogramForAddress(uint64_t Address) {
369 extractDIEsIfNeeded(false);
370 for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
371 if (DIE.isSubprogramDIE() &&
372 DIE.addressRangeContainsAddress(this, Address)) {
379 DWARFDebugInfoEntryInlinedChain
380 DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
381 // First, find a subprogram that contains the given address (the root
382 // of inlined chain).
383 const DWARFUnit *ChainCU = nullptr;
384 const DWARFDebugInfoEntryMinimal *SubprogramDIE;
385 // Try to look for subprogram DIEs in the DWO file.
388 if ((SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address)))
389 ChainCU = DWO->getUnit();
390 } else if ((SubprogramDIE = getSubprogramForAddress(Address)))
393 // Get inlined chain rooted at this subprogram DIE.
395 return DWARFDebugInfoEntryInlinedChain();
396 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
399 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
400 DWARFSectionKind Kind) {
401 if (Kind == DW_SECT_INFO)
402 return Context.getCUIndex();
403 assert(Kind == DW_SECT_TYPES);
404 return Context.getTUIndex();