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" 120b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" 130b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 140b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 150b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h" 160b57cec5SDimitry Andric #include <cstddef> 170b57cec5SDimitry Andric #include <cstdint> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric using namespace llvm; 200b57cec5SDimitry Andric using namespace dwarf; 210b57cec5SDimitry Andric 228bcb0991SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr, 230b57cec5SDimitry Andric const DWARFDataExtractor &DebugInfoData, 24*349cc55cSDimitry Andric uint64_t UEndOffset, uint32_t ParentIdx) { 250b57cec5SDimitry Andric Offset = *OffsetPtr; 26*349cc55cSDimitry 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. 690b57cec5SDimitry Andric if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) { 700b57cec5SDimitry Andric *OffsetPtr += *FixedSize; 710b57cec5SDimitry Andric return true; 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // Skip all data in the .debug_info for the attributes 750b57cec5SDimitry Andric for (const auto &AttrSpec : AbbrevDecl->attributes()) { 760b57cec5SDimitry Andric // Check if this attribute has a fixed byte size. 770b57cec5SDimitry Andric if (auto FixedSize = AttrSpec.getByteSize(U)) { 780b57cec5SDimitry Andric // Attribute byte size if fixed, just add the size to the offset. 790b57cec5SDimitry Andric *OffsetPtr += *FixedSize; 800b57cec5SDimitry Andric } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData, 810b57cec5SDimitry Andric OffsetPtr, U.getFormParams())) { 820b57cec5SDimitry Andric // We failed to skip this attribute's value, restore the original offset 830b57cec5SDimitry Andric // and return the failure status. 84fe6060f1SDimitry Andric U.getContext().getWarningHandler()(createStringError( 85fe6060f1SDimitry Andric errc::invalid_argument, 86fe6060f1SDimitry Andric "DWARF unit at offset 0x%8.8" PRIx64 " " 87fe6060f1SDimitry Andric "contains invalid FORM_* 0x%" PRIx16 " at offset 0x%8.8" PRIx64, 88fe6060f1SDimitry Andric U.getOffset(), AttrSpec.Form, *OffsetPtr)); 890b57cec5SDimitry Andric *OffsetPtr = Offset; 900b57cec5SDimitry Andric return false; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric return true; 940b57cec5SDimitry Andric } 95