xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric //===- DWARFDebugInfoEntry.cpp --------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
100b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
140b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
150b57cec5SDimitry Andric #include <cstddef>
160b57cec5SDimitry Andric #include <cstdint>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric using namespace dwarf;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U,
228bcb0991SDimitry Andric                                              uint64_t *OffsetPtr) {
230b57cec5SDimitry Andric   DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
248bcb0991SDimitry Andric   const uint64_t UEndOffset = U.getNextUnitOffset();
250b57cec5SDimitry Andric   return extractFast(U, OffsetPtr, DebugInfoData, UEndOffset, 0);
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric 
288bcb0991SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
290b57cec5SDimitry Andric                                       const DWARFDataExtractor &DebugInfoData,
308bcb0991SDimitry Andric                                       uint64_t UEndOffset, uint32_t D) {
310b57cec5SDimitry Andric   Offset = *OffsetPtr;
320b57cec5SDimitry Andric   Depth = D;
330b57cec5SDimitry Andric   if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
340b57cec5SDimitry Andric     return false;
350b57cec5SDimitry Andric   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
360b57cec5SDimitry Andric   if (0 == AbbrCode) {
370b57cec5SDimitry Andric     // NULL debug tag entry.
380b57cec5SDimitry Andric     AbbrevDecl = nullptr;
390b57cec5SDimitry Andric     return true;
400b57cec5SDimitry Andric   }
41*e8d8bef9SDimitry Andric   if (const auto *AbbrevSet = U.getAbbreviations())
42*e8d8bef9SDimitry Andric     AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
430b57cec5SDimitry Andric   if (nullptr == AbbrevDecl) {
440b57cec5SDimitry Andric     // Restore the original offset.
450b57cec5SDimitry Andric     *OffsetPtr = Offset;
460b57cec5SDimitry Andric     return false;
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric   // See if all attributes in this DIE have fixed byte sizes. If so, we can
490b57cec5SDimitry Andric   // just add this size to the offset to skip to the next DIE.
500b57cec5SDimitry Andric   if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) {
510b57cec5SDimitry Andric     *OffsetPtr += *FixedSize;
520b57cec5SDimitry Andric     return true;
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // Skip all data in the .debug_info for the attributes
560b57cec5SDimitry Andric   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
570b57cec5SDimitry Andric     // Check if this attribute has a fixed byte size.
580b57cec5SDimitry Andric     if (auto FixedSize = AttrSpec.getByteSize(U)) {
590b57cec5SDimitry Andric       // Attribute byte size if fixed, just add the size to the offset.
600b57cec5SDimitry Andric       *OffsetPtr += *FixedSize;
610b57cec5SDimitry Andric     } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
620b57cec5SDimitry Andric                                           OffsetPtr, U.getFormParams())) {
630b57cec5SDimitry Andric       // We failed to skip this attribute's value, restore the original offset
640b57cec5SDimitry Andric       // and return the failure status.
650b57cec5SDimitry Andric       *OffsetPtr = Offset;
660b57cec5SDimitry Andric       return false;
670b57cec5SDimitry Andric     }
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric   return true;
700b57cec5SDimitry Andric }
71