1e94042caSEugene Zelenko //===- DWARFDebugInfoEntry.cpp --------------------------------------------===//
282af9438SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
682af9438SZachary Turner //
782af9438SZachary Turner //===----------------------------------------------------------------------===//
882af9438SZachary Turner 
96bda14b3SChandler Carruth #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
10e94042caSEugene Zelenko #include "llvm/ADT/Optional.h"
11c19a2891SJan Kratochvil #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12*290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
1382af9438SZachary Turner #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
1482af9438SZachary Turner #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
15e94042caSEugene Zelenko #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16*290e4823Sserge-sans-paille #include "llvm/Support/Errc.h"
17e94042caSEugene Zelenko #include <cstddef>
18e94042caSEugene Zelenko #include <cstdint>
19e94042caSEugene Zelenko 
2082af9438SZachary Turner using namespace llvm;
2182af9438SZachary Turner using namespace dwarf;
2282af9438SZachary Turner 
extractFast(const DWARFUnit & U,uint64_t * OffsetPtr,const DWARFDataExtractor & DebugInfoData,uint64_t UEndOffset,uint32_t ParentIdx)23f26a70a5SIgor Kudrin bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
2417536b93SPaul Robinson                                       const DWARFDataExtractor &DebugInfoData,
250b8c5081SAlexey Lapshin                                       uint64_t UEndOffset, uint32_t ParentIdx) {
2682af9438SZachary Turner   Offset = *OffsetPtr;
270b8c5081SAlexey Lapshin   this->ParentIdx = ParentIdx;
28c19a2891SJan Kratochvil   if (Offset >= UEndOffset) {
29c19a2891SJan Kratochvil     U.getContext().getWarningHandler()(
30c19a2891SJan Kratochvil         createStringError(errc::invalid_argument,
31c19a2891SJan Kratochvil                           "DWARF unit from offset 0x%8.8" PRIx64 " incl. "
32c19a2891SJan Kratochvil                           "to offset 0x%8.8" PRIx64 " excl. "
33c19a2891SJan Kratochvil                           "tries to read DIEs at offset 0x%8.8" PRIx64,
34c19a2891SJan Kratochvil                           U.getOffset(), U.getNextUnitOffset(), *OffsetPtr));
3582af9438SZachary Turner     return false;
36c19a2891SJan Kratochvil   }
37c19a2891SJan Kratochvil   assert(DebugInfoData.isValidOffset(UEndOffset - 1));
3882af9438SZachary Turner   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
3982af9438SZachary Turner   if (0 == AbbrCode) {
4082af9438SZachary Turner     // NULL debug tag entry.
4182af9438SZachary Turner     AbbrevDecl = nullptr;
4282af9438SZachary Turner     return true;
4382af9438SZachary Turner   }
44c19a2891SJan Kratochvil   const auto *AbbrevSet = U.getAbbreviations();
45c19a2891SJan Kratochvil   if (!AbbrevSet) {
46c19a2891SJan Kratochvil     U.getContext().getWarningHandler()(
47c19a2891SJan Kratochvil         createStringError(errc::invalid_argument,
48c19a2891SJan Kratochvil                           "DWARF unit at offset 0x%8.8" PRIx64 " "
49c19a2891SJan Kratochvil                           "contains invalid abbreviation set offset 0x%" PRIx64,
50c19a2891SJan Kratochvil                           U.getOffset(), U.getAbbreviationsOffset()));
51c19a2891SJan Kratochvil     // Restore the original offset.
52c19a2891SJan Kratochvil     *OffsetPtr = Offset;
53c19a2891SJan Kratochvil     return false;
54c19a2891SJan Kratochvil   }
559ceb192eSIgor Kudrin   AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
56c19a2891SJan Kratochvil   if (!AbbrevDecl) {
57c19a2891SJan Kratochvil     U.getContext().getWarningHandler()(
58c19a2891SJan Kratochvil         createStringError(errc::invalid_argument,
59c19a2891SJan Kratochvil                           "DWARF unit at offset 0x%8.8" PRIx64 " "
60c19a2891SJan Kratochvil                           "contains invalid abbreviation %" PRIu64 " at "
61c19a2891SJan Kratochvil                           "offset 0x%8.8" PRIx64 ", valid abbreviations are %s",
62c19a2891SJan Kratochvil                           U.getOffset(), AbbrCode, *OffsetPtr,
63c19a2891SJan Kratochvil                           AbbrevSet->getCodeRange().c_str()));
6482af9438SZachary Turner     // Restore the original offset.
6582af9438SZachary Turner     *OffsetPtr = Offset;
6682af9438SZachary Turner     return false;
6782af9438SZachary Turner   }
686f6e4dbdSGreg Clayton   // See if all attributes in this DIE have fixed byte sizes. If so, we can
696f6e4dbdSGreg Clayton   // just add this size to the offset to skip to the next DIE.
706f6e4dbdSGreg Clayton   if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) {
716f6e4dbdSGreg Clayton     *OffsetPtr += *FixedSize;
726f6e4dbdSGreg Clayton     return true;
736f6e4dbdSGreg Clayton   }
7482af9438SZachary Turner 
7582af9438SZachary Turner   // Skip all data in the .debug_info for the attributes
7682af9438SZachary Turner   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
776f6e4dbdSGreg Clayton     // Check if this attribute has a fixed byte size.
78cbddae74SVictor Leschuk     if (auto FixedSize = AttrSpec.getByteSize(U)) {
796f6e4dbdSGreg Clayton       // Attribute byte size if fixed, just add the size to the offset.
8082f12b14SGreg Clayton       *OffsetPtr += *FixedSize;
816f6e4dbdSGreg Clayton     } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
8275c068c5SPaul Robinson                                           OffsetPtr, U.getFormParams())) {
836f6e4dbdSGreg Clayton       // We failed to skip this attribute's value, restore the original offset
846f6e4dbdSGreg Clayton       // and return the failure status.
85c19a2891SJan Kratochvil       U.getContext().getWarningHandler()(createStringError(
86c19a2891SJan Kratochvil           errc::invalid_argument,
87c19a2891SJan Kratochvil           "DWARF unit at offset 0x%8.8" PRIx64 " "
88c19a2891SJan Kratochvil           "contains invalid FORM_* 0x%" PRIx16 " at offset 0x%8.8" PRIx64,
89c19a2891SJan Kratochvil           U.getOffset(), AttrSpec.Form, *OffsetPtr));
9082af9438SZachary Turner       *OffsetPtr = Offset;
9182af9438SZachary Turner       return false;
9282af9438SZachary Turner     }
9382af9438SZachary Turner   }
9482af9438SZachary Turner   return true;
9582af9438SZachary Turner }
96