xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
10fe6060f1SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1181ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
1581ad6265SDimitry Andric #include "llvm/Support/Errc.h"
160b57cec5SDimitry Andric #include <cstddef>
170b57cec5SDimitry Andric #include <cstdint>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric using namespace dwarf;
210b57cec5SDimitry Andric 
extractFast(const DWARFUnit & U,uint64_t * OffsetPtr,const DWARFDataExtractor & DebugInfoData,uint64_t UEndOffset,uint32_t ParentIdx)228bcb0991SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
230b57cec5SDimitry Andric                                       const DWARFDataExtractor &DebugInfoData,
24349cc55cSDimitry Andric                                       uint64_t UEndOffset, uint32_t ParentIdx) {
250b57cec5SDimitry Andric   Offset = *OffsetPtr;
26349cc55cSDimitry Andric   this->ParentIdx = ParentIdx;
27fe6060f1SDimitry Andric   if (Offset >= UEndOffset) {
28fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
29fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
30fe6060f1SDimitry Andric                           "DWARF unit from offset 0x%8.8" PRIx64 " incl. "
31fe6060f1SDimitry Andric                           "to offset 0x%8.8" PRIx64 " excl. "
32fe6060f1SDimitry Andric                           "tries to read DIEs at offset 0x%8.8" PRIx64,
33fe6060f1SDimitry Andric                           U.getOffset(), U.getNextUnitOffset(), *OffsetPtr));
340b57cec5SDimitry Andric     return false;
35fe6060f1SDimitry Andric   }
36fe6060f1SDimitry Andric   assert(DebugInfoData.isValidOffset(UEndOffset - 1));
370b57cec5SDimitry Andric   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
380b57cec5SDimitry Andric   if (0 == AbbrCode) {
390b57cec5SDimitry Andric     // NULL debug tag entry.
400b57cec5SDimitry Andric     AbbrevDecl = nullptr;
410b57cec5SDimitry Andric     return true;
420b57cec5SDimitry Andric   }
43fe6060f1SDimitry Andric   const auto *AbbrevSet = U.getAbbreviations();
44fe6060f1SDimitry Andric   if (!AbbrevSet) {
45fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
46fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
47fe6060f1SDimitry Andric                           "DWARF unit at offset 0x%8.8" PRIx64 " "
48fe6060f1SDimitry Andric                           "contains invalid abbreviation set offset 0x%" PRIx64,
49fe6060f1SDimitry Andric                           U.getOffset(), U.getAbbreviationsOffset()));
50fe6060f1SDimitry Andric     // Restore the original offset.
51fe6060f1SDimitry Andric     *OffsetPtr = Offset;
52fe6060f1SDimitry Andric     return false;
53fe6060f1SDimitry Andric   }
54e8d8bef9SDimitry Andric   AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
55fe6060f1SDimitry Andric   if (!AbbrevDecl) {
56fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
57fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
58fe6060f1SDimitry Andric                           "DWARF unit at offset 0x%8.8" PRIx64 " "
59fe6060f1SDimitry Andric                           "contains invalid abbreviation %" PRIu64 " at "
60fe6060f1SDimitry Andric                           "offset 0x%8.8" PRIx64 ", valid abbreviations are %s",
61fe6060f1SDimitry Andric                           U.getOffset(), AbbrCode, *OffsetPtr,
62fe6060f1SDimitry Andric                           AbbrevSet->getCodeRange().c_str()));
630b57cec5SDimitry Andric     // Restore the original offset.
640b57cec5SDimitry Andric     *OffsetPtr = Offset;
650b57cec5SDimitry Andric     return false;
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric   // See if all attributes in this DIE have fixed byte sizes. If so, we can
680b57cec5SDimitry Andric   // just add this size to the offset to skip to the next DIE.
69*bdd1243dSDimitry Andric   if (std::optional<size_t> FixedSize =
70*bdd1243dSDimitry Andric           AbbrevDecl->getFixedAttributesByteSize(U)) {
710b57cec5SDimitry Andric     *OffsetPtr += *FixedSize;
720b57cec5SDimitry Andric     return true;
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // Skip all data in the .debug_info for the attributes
760b57cec5SDimitry Andric   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
770b57cec5SDimitry Andric     // Check if this attribute has a fixed byte size.
780b57cec5SDimitry Andric     if (auto FixedSize = AttrSpec.getByteSize(U)) {
790b57cec5SDimitry Andric       // Attribute byte size if fixed, just add the size to the offset.
800b57cec5SDimitry Andric       *OffsetPtr += *FixedSize;
810b57cec5SDimitry Andric     } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
820b57cec5SDimitry Andric                                           OffsetPtr, U.getFormParams())) {
830b57cec5SDimitry Andric       // We failed to skip this attribute's value, restore the original offset
840b57cec5SDimitry Andric       // and return the failure status.
85fe6060f1SDimitry Andric       U.getContext().getWarningHandler()(createStringError(
86fe6060f1SDimitry Andric           errc::invalid_argument,
87fe6060f1SDimitry Andric           "DWARF unit at offset 0x%8.8" PRIx64 " "
88fe6060f1SDimitry Andric           "contains invalid FORM_* 0x%" PRIx16 " at offset 0x%8.8" PRIx64,
89fe6060f1SDimitry Andric           U.getOffset(), AttrSpec.Form, *OffsetPtr));
900b57cec5SDimitry Andric       *OffsetPtr = Offset;
910b57cec5SDimitry Andric       return false;
920b57cec5SDimitry Andric     }
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric   return true;
950b57cec5SDimitry Andric }
96