1 //===- DWARFDebugInfoEntry.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
12 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
13 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include <cstddef>
16 #include <cstdint>
17 
18 using namespace llvm;
19 using namespace dwarf;
20 
extractFast(const DWARFUnit & U,uint64_t * OffsetPtr)21 bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U,
22                                              uint64_t *OffsetPtr) {
23   DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
24   const uint64_t UEndOffset = U.getNextUnitOffset();
25   return extractFast(U, OffsetPtr, DebugInfoData, UEndOffset, 0);
26 }
27 
extractFast(const DWARFUnit & U,uint64_t * OffsetPtr,const DWARFDataExtractor & DebugInfoData,uint64_t UEndOffset,uint32_t D)28 bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
29                                       const DWARFDataExtractor &DebugInfoData,
30                                       uint64_t UEndOffset, uint32_t D) {
31   Offset = *OffsetPtr;
32   Depth = D;
33   if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
34     return false;
35   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
36   if (0 == AbbrCode) {
37     // NULL debug tag entry.
38     AbbrevDecl = nullptr;
39     return true;
40   }
41   if (const auto *AbbrevSet = U.getAbbreviations())
42     AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
43   if (nullptr == AbbrevDecl) {
44     // Restore the original offset.
45     *OffsetPtr = Offset;
46     return false;
47   }
48   // See if all attributes in this DIE have fixed byte sizes. If so, we can
49   // just add this size to the offset to skip to the next DIE.
50   if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) {
51     *OffsetPtr += *FixedSize;
52     return true;
53   }
54 
55   // Skip all data in the .debug_info for the attributes
56   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
57     // Check if this attribute has a fixed byte size.
58     if (auto FixedSize = AttrSpec.getByteSize(U)) {
59       // Attribute byte size if fixed, just add the size to the offset.
60       *OffsetPtr += *FixedSize;
61     } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
62                                           OffsetPtr, U.getFormParams())) {
63       // We failed to skip this attribute's value, restore the original offset
64       // and return the failure status.
65       *OffsetPtr = Offset;
66       return false;
67     }
68   }
69   return true;
70 }
71