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