xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
11fe6060f1SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12*81ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
150b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16*81ad6265SDimitry Andric #include "llvm/Support/Errc.h"
170b57cec5SDimitry Andric #include <cstddef>
180b57cec5SDimitry Andric #include <cstdint>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric using namespace dwarf;
220b57cec5SDimitry Andric 
238bcb0991SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
240b57cec5SDimitry Andric                                       const DWARFDataExtractor &DebugInfoData,
25349cc55cSDimitry Andric                                       uint64_t UEndOffset, uint32_t ParentIdx) {
260b57cec5SDimitry Andric   Offset = *OffsetPtr;
27349cc55cSDimitry Andric   this->ParentIdx = ParentIdx;
28fe6060f1SDimitry Andric   if (Offset >= UEndOffset) {
29fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
30fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
31fe6060f1SDimitry Andric                           "DWARF unit from offset 0x%8.8" PRIx64 " incl. "
32fe6060f1SDimitry Andric                           "to offset 0x%8.8" PRIx64 " excl. "
33fe6060f1SDimitry Andric                           "tries to read DIEs at offset 0x%8.8" PRIx64,
34fe6060f1SDimitry Andric                           U.getOffset(), U.getNextUnitOffset(), *OffsetPtr));
350b57cec5SDimitry Andric     return false;
36fe6060f1SDimitry Andric   }
37fe6060f1SDimitry Andric   assert(DebugInfoData.isValidOffset(UEndOffset - 1));
380b57cec5SDimitry Andric   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
390b57cec5SDimitry Andric   if (0 == AbbrCode) {
400b57cec5SDimitry Andric     // NULL debug tag entry.
410b57cec5SDimitry Andric     AbbrevDecl = nullptr;
420b57cec5SDimitry Andric     return true;
430b57cec5SDimitry Andric   }
44fe6060f1SDimitry Andric   const auto *AbbrevSet = U.getAbbreviations();
45fe6060f1SDimitry Andric   if (!AbbrevSet) {
46fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
47fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
48fe6060f1SDimitry Andric                           "DWARF unit at offset 0x%8.8" PRIx64 " "
49fe6060f1SDimitry Andric                           "contains invalid abbreviation set offset 0x%" PRIx64,
50fe6060f1SDimitry Andric                           U.getOffset(), U.getAbbreviationsOffset()));
51fe6060f1SDimitry Andric     // Restore the original offset.
52fe6060f1SDimitry Andric     *OffsetPtr = Offset;
53fe6060f1SDimitry Andric     return false;
54fe6060f1SDimitry Andric   }
55e8d8bef9SDimitry Andric   AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
56fe6060f1SDimitry Andric   if (!AbbrevDecl) {
57fe6060f1SDimitry Andric     U.getContext().getWarningHandler()(
58fe6060f1SDimitry Andric         createStringError(errc::invalid_argument,
59fe6060f1SDimitry Andric                           "DWARF unit at offset 0x%8.8" PRIx64 " "
60fe6060f1SDimitry Andric                           "contains invalid abbreviation %" PRIu64 " at "
61fe6060f1SDimitry Andric                           "offset 0x%8.8" PRIx64 ", valid abbreviations are %s",
62fe6060f1SDimitry Andric                           U.getOffset(), AbbrCode, *OffsetPtr,
63fe6060f1SDimitry Andric                           AbbrevSet->getCodeRange().c_str()));
640b57cec5SDimitry Andric     // Restore the original offset.
650b57cec5SDimitry Andric     *OffsetPtr = Offset;
660b57cec5SDimitry Andric     return false;
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric   // See if all attributes in this DIE have fixed byte sizes. If so, we can
690b57cec5SDimitry Andric   // just add this size to the offset to skip to the next DIE.
700b57cec5SDimitry Andric   if (Optional<size_t> FixedSize = 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