1//===-- 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 "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"
15#include <cstdio>
16
17namespace llvm {
18using namespace dwarf;
19
20void 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);
24}
25
26void 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(),
32            true);
33}
34
35DWARFUnit::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([&]() {
42        if (IndexEntry)
43          if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
44            return SOS.slice(C->Offset, C->Offset + C->Length);
45        return SOS;
46      }()),
47      AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
48      UnitSection(UnitSection), IndexEntry(IndexEntry) {
49  clear();
50}
51
52DWARFUnit::~DWARFUnit() {
53}
54
55bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
56                                                uint64_t &Result) const {
57  uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
58  if (AddrOffsetSection.size() < Offset + AddrSize)
59    return false;
60  DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
61  Result = DA.getAddress(&Offset);
62  return true;
63}
64
65bool 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)
71    return false;
72  DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
73  Result = DA.getU32(&Offset);
74  return true;
75}
76
77bool 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);
81  if (IndexEntry) {
82    if (AbbrOffset)
83      return false;
84    auto *UnitContrib = IndexEntry->getOffset();
85    if (!UnitContrib || UnitContrib->Length != (Length + 4))
86      return false;
87    auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
88    if (!AbbrEntry)
89      return false;
90    AbbrOffset = AbbrEntry->Offset;
91  }
92  AddrSize = debug_info.getU8(offset_ptr);
93
94  bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
95  bool VersionOK = DWARFContext::isSupportedVersion(Version);
96  bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
97
98  if (!LengthOK || !VersionOK || !AddrSizeOK)
99    return false;
100
101  Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
102  return Abbrevs != nullptr;
103}
104
105bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
106  clear();
107
108  Offset = *offset_ptr;
109
110  if (debug_info.isValidOffset(*offset_ptr)) {
111    if (extractImpl(debug_info, offset_ptr))
112      return true;
113
114    // reset the offset to where we tried to parse from if anything went wrong
115    *offset_ptr = Offset;
116  }
117
118  return false;
119}
120
121bool 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);
128}
129
130void DWARFUnit::clear() {
131  Offset = 0;
132  Length = 0;
133  Version = 0;
134  Abbrevs = nullptr;
135  AddrSize = 0;
136  BaseAddr = 0;
137  RangeSectionBase = 0;
138  AddrOffsetSectionBase = 0;
139  clearDIEs(false);
140  DWO.reset();
141}
142
143const char *DWARFUnit::getCompilationDir() {
144  extractDIEsIfNeeded(true);
145  if (DieArray.empty())
146    return nullptr;
147  return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr);
148}
149
150uint64_t DWARFUnit::getDWOId() {
151  extractDIEsIfNeeded(true);
152  const uint64_t FailValue = -1ULL;
153  if (DieArray.empty())
154    return FailValue;
155  return DieArray[0]
156      .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
157}
158
159void DWARFUnit::setDIERelations() {
160  if (DieArray.size() <= 1)
161    return;
162
163  std::vector<DWARFDebugInfoEntryMinimal *> ParentChain;
164  DWARFDebugInfoEntryMinimal *SiblingChain = nullptr;
165  for (auto &DIE : DieArray) {
166    if (SiblingChain) {
167      SiblingChain->setSibling(&DIE);
168    }
169    if (const DWARFAbbreviationDeclaration *AbbrDecl =
170            DIE.getAbbreviationDeclarationPtr()) {
171      // Normal DIE.
172      if (AbbrDecl->hasChildren()) {
173        ParentChain.push_back(&DIE);
174        SiblingChain = nullptr;
175      } else {
176        SiblingChain = &DIE;
177      }
178    } else {
179      // NULL entry terminates the sibling chain.
180      SiblingChain = ParentChain.back();
181      ParentChain.pop_back();
182    }
183  }
184  assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]);
185  assert(ParentChain.empty());
186}
187
188void DWARFUnit::extractDIEsToVector(
189    bool AppendCUDie, bool AppendNonCUDies,
190    std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
191  if (!AppendCUDie && !AppendNonCUDies)
192    return;
193
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;
199  uint32_t Depth = 0;
200  bool IsCUDie = true;
201
202  while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) {
203    if (IsCUDie) {
204      if (AppendCUDie)
205        Dies.push_back(DIE);
206      if (!AppendNonCUDies)
207        break;
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);
212      IsCUDie = false;
213    } else {
214      Dies.push_back(DIE);
215    }
216
217    if (const DWARFAbbreviationDeclaration *AbbrDecl =
218            DIE.getAbbreviationDeclarationPtr()) {
219      // Normal DIE
220      if (AbbrDecl->hasChildren())
221        ++Depth;
222    } else {
223      // NULL DIE.
224      if (Depth > 0)
225        --Depth;
226      if (Depth == 0)
227        break;  // We are done with this compile unit!
228    }
229  }
230
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
233  // unit header).
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);
237}
238
239size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
240  if ((CUDieOnly && DieArray.size() > 0) ||
241      DieArray.size() > 1)
242    return 0; // Already parsed.
243
244  bool HasCUDie = DieArray.size() > 0;
245  extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
246
247  if (DieArray.empty())
248    return 0;
249
250  // If CU DIE was just parsed, copy several attribute values from it.
251  if (!HasCUDie) {
252    uint64_t BaseAddr =
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.
263  }
264
265  setDIERelations();
266  return DieArray.size();
267}
268
269DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
270    : DWOFile(), DWOContext(), DWOU(nullptr) {
271  auto Obj = object::ObjectFile::createObjectFile(DWOPath);
272  if (!Obj) {
273    // TODO: Actually report errors helpfully.
274    consumeError(Obj.takeError());
275    return;
276  }
277  DWOFile = std::move(Obj.get());
278  DWOContext.reset(
279      cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
280  if (DWOContext->getNumDWOCompileUnits() > 0)
281    DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
282}
283
284bool DWARFUnit::parseDWO() {
285  if (isDWO)
286    return false;
287  if (DWO.get())
288    return false;
289  extractDIEsIfNeeded(true);
290  if (DieArray.empty())
291    return false;
292  const char *DWOFileName =
293      DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr);
294  if (!DWOFileName)
295    return false;
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);
301  }
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()) {
307    DWO.reset();
308    return false;
309  }
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);
314  return true;
315}
316
317void 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
325    // contents.
326    std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
327    DieArray.swap(TmpArray);
328    // Save at least the compile unit DIE
329    if (KeepCUDie)
330      DieArray.push_back(TmpArray.front());
331  }
332}
333
334void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
335  const auto *U = getUnitDIE();
336  if (U == nullptr)
337    return;
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());
342    return;
343  }
344
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
350  // down.
351  const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
352  DieArray[0].collectChildrenAddressRanges(this, CURanges);
353
354  // Collect address ranges from DIEs in .dwo if necessary.
355  bool DWOCreated = parseDWO();
356  if (DWO.get())
357    DWO->getUnit()->collectAddressRanges(CURanges);
358  if (DWOCreated)
359    DWO.reset();
360
361  // Keep memory down by clearing DIEs if this generate function
362  // caused them to be parsed.
363  if (ClearDIEs)
364    clearDIEs(true);
365}
366
367const DWARFDebugInfoEntryMinimal *
368DWARFUnit::getSubprogramForAddress(uint64_t Address) {
369  extractDIEsIfNeeded(false);
370  for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) {
371    if (DIE.isSubprogramDIE() &&
372        DIE.addressRangeContainsAddress(this, Address)) {
373      return &DIE;
374    }
375  }
376  return nullptr;
377}
378
379DWARFDebugInfoEntryInlinedChain
380DWARFUnit::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.
386  parseDWO();
387  if (DWO) {
388    if ((SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address)))
389      ChainCU = DWO->getUnit();
390  } else if ((SubprogramDIE = getSubprogramForAddress(Address)))
391    ChainCU = this;
392
393  // Get inlined chain rooted at this subprogram DIE.
394  if (!SubprogramDIE)
395    return DWARFDebugInfoEntryInlinedChain();
396  return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);
397}
398
399const 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();
405}
406}
407