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" 11*fe6060f1SDimitry 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 220b57cec5SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, 238bcb0991SDimitry Andric uint64_t *OffsetPtr) { 240b57cec5SDimitry Andric DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor(); 258bcb0991SDimitry Andric const uint64_t UEndOffset = U.getNextUnitOffset(); 260b57cec5SDimitry Andric return extractFast(U, OffsetPtr, DebugInfoData, UEndOffset, 0); 270b57cec5SDimitry Andric } 280b57cec5SDimitry Andric 298bcb0991SDimitry Andric bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr, 300b57cec5SDimitry Andric const DWARFDataExtractor &DebugInfoData, 318bcb0991SDimitry Andric uint64_t UEndOffset, uint32_t D) { 320b57cec5SDimitry Andric Offset = *OffsetPtr; 330b57cec5SDimitry Andric Depth = D; 34*fe6060f1SDimitry Andric if (Offset >= UEndOffset) { 35*fe6060f1SDimitry Andric U.getContext().getWarningHandler()( 36*fe6060f1SDimitry Andric createStringError(errc::invalid_argument, 37*fe6060f1SDimitry Andric "DWARF unit from offset 0x%8.8" PRIx64 " incl. " 38*fe6060f1SDimitry Andric "to offset 0x%8.8" PRIx64 " excl. " 39*fe6060f1SDimitry Andric "tries to read DIEs at offset 0x%8.8" PRIx64, 40*fe6060f1SDimitry Andric U.getOffset(), U.getNextUnitOffset(), *OffsetPtr)); 410b57cec5SDimitry Andric return false; 42*fe6060f1SDimitry Andric } 43*fe6060f1SDimitry Andric assert(DebugInfoData.isValidOffset(UEndOffset - 1)); 440b57cec5SDimitry Andric uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr); 450b57cec5SDimitry Andric if (0 == AbbrCode) { 460b57cec5SDimitry Andric // NULL debug tag entry. 470b57cec5SDimitry Andric AbbrevDecl = nullptr; 480b57cec5SDimitry Andric return true; 490b57cec5SDimitry Andric } 50*fe6060f1SDimitry Andric const auto *AbbrevSet = U.getAbbreviations(); 51*fe6060f1SDimitry Andric if (!AbbrevSet) { 52*fe6060f1SDimitry Andric U.getContext().getWarningHandler()( 53*fe6060f1SDimitry Andric createStringError(errc::invalid_argument, 54*fe6060f1SDimitry Andric "DWARF unit at offset 0x%8.8" PRIx64 " " 55*fe6060f1SDimitry Andric "contains invalid abbreviation set offset 0x%" PRIx64, 56*fe6060f1SDimitry Andric U.getOffset(), U.getAbbreviationsOffset())); 57*fe6060f1SDimitry Andric // Restore the original offset. 58*fe6060f1SDimitry Andric *OffsetPtr = Offset; 59*fe6060f1SDimitry Andric return false; 60*fe6060f1SDimitry Andric } 61e8d8bef9SDimitry Andric AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode); 62*fe6060f1SDimitry Andric if (!AbbrevDecl) { 63*fe6060f1SDimitry Andric U.getContext().getWarningHandler()( 64*fe6060f1SDimitry Andric createStringError(errc::invalid_argument, 65*fe6060f1SDimitry Andric "DWARF unit at offset 0x%8.8" PRIx64 " " 66*fe6060f1SDimitry Andric "contains invalid abbreviation %" PRIu64 " at " 67*fe6060f1SDimitry Andric "offset 0x%8.8" PRIx64 ", valid abbreviations are %s", 68*fe6060f1SDimitry Andric U.getOffset(), AbbrCode, *OffsetPtr, 69*fe6060f1SDimitry Andric AbbrevSet->getCodeRange().c_str())); 700b57cec5SDimitry Andric // Restore the original offset. 710b57cec5SDimitry Andric *OffsetPtr = Offset; 720b57cec5SDimitry Andric return false; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric // See if all attributes in this DIE have fixed byte sizes. If so, we can 750b57cec5SDimitry Andric // just add this size to the offset to skip to the next DIE. 760b57cec5SDimitry Andric if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) { 770b57cec5SDimitry Andric *OffsetPtr += *FixedSize; 780b57cec5SDimitry Andric return true; 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // Skip all data in the .debug_info for the attributes 820b57cec5SDimitry Andric for (const auto &AttrSpec : AbbrevDecl->attributes()) { 830b57cec5SDimitry Andric // Check if this attribute has a fixed byte size. 840b57cec5SDimitry Andric if (auto FixedSize = AttrSpec.getByteSize(U)) { 850b57cec5SDimitry Andric // Attribute byte size if fixed, just add the size to the offset. 860b57cec5SDimitry Andric *OffsetPtr += *FixedSize; 870b57cec5SDimitry Andric } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData, 880b57cec5SDimitry Andric OffsetPtr, U.getFormParams())) { 890b57cec5SDimitry Andric // We failed to skip this attribute's value, restore the original offset 900b57cec5SDimitry Andric // and return the failure status. 91*fe6060f1SDimitry Andric U.getContext().getWarningHandler()(createStringError( 92*fe6060f1SDimitry Andric errc::invalid_argument, 93*fe6060f1SDimitry Andric "DWARF unit at offset 0x%8.8" PRIx64 " " 94*fe6060f1SDimitry Andric "contains invalid FORM_* 0x%" PRIx16 " at offset 0x%8.8" PRIx64, 95*fe6060f1SDimitry Andric U.getOffset(), AttrSpec.Form, *OffsetPtr)); 960b57cec5SDimitry Andric *OffsetPtr = Offset; 970b57cec5SDimitry Andric return false; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric return true; 1010b57cec5SDimitry Andric } 102