blob: d8a755e90df4bf39d17714e59813817fe71acae0 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDebugInfoEntry.cpp --------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000010#include "llvm/ADT/Optional.h"
Zachary Turner82af9432015-01-30 18:07:45 +000011#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Zachary Turner82af9432015-01-30 18:07:45 +000012#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000013#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
14#include "llvm/Support/DataExtractor.h"
15#include <cstddef>
16#include <cstdint>
17
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000018using namespace llvm;
19using namespace dwarf;
20
Greg Claytonc8c10322016-12-13 18:25:19 +000021bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U,
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000022 uint32_t *OffsetPtr) {
Paul Robinson17536b92017-06-29 16:52:08 +000023 DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
Greg Clayton6f6e4db2016-11-15 01:23:06 +000024 const uint32_t UEndOffset = U.getNextUnitOffset();
Greg Clayton78a07bf2016-12-21 21:37:06 +000025 return extractFast(U, OffsetPtr, DebugInfoData, UEndOffset, 0);
Greg Clayton6f6e4db2016-11-15 01:23:06 +000026}
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000027
Greg Clayton78a07bf2016-12-21 21:37:06 +000028bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint32_t *OffsetPtr,
Paul Robinson17536b92017-06-29 16:52:08 +000029 const DWARFDataExtractor &DebugInfoData,
Greg Clayton78a07bf2016-12-21 21:37:06 +000030 uint32_t UEndOffset, uint32_t D) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000031 Offset = *OffsetPtr;
Greg Clayton78a07bf2016-12-21 21:37:06 +000032 Depth = D;
Alexey Samsonov330b8932013-10-28 23:58:58 +000033 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000034 return false;
35 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
36 if (0 == AbbrCode) {
37 // NULL debug tag entry.
Craig Topper2617dcc2014-04-15 06:32:26 +000038 AbbrevDecl = nullptr;
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000039 return true;
40 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +000041 AbbrevDecl = U.getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Craig Topper2617dcc2014-04-15 06:32:26 +000042 if (nullptr == AbbrevDecl) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000043 // Restore the original offset.
44 *OffsetPtr = Offset;
45 return false;
46 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +000047 // See if all attributes in this DIE have fixed byte sizes. If so, we can
48 // just add this size to the offset to skip to the next DIE.
49 if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) {
50 *OffsetPtr += *FixedSize;
51 return true;
52 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000053
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000054 // Skip all data in the .debug_info for the attributes
Alexey Samsonov1eabf982014-03-13 07:52:54 +000055 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Greg Clayton6f6e4db2016-11-15 01:23:06 +000056 // Check if this attribute has a fixed byte size.
Victor Leschukcbddae72017-01-10 21:18:26 +000057 if (auto FixedSize = AttrSpec.getByteSize(U)) {
Greg Clayton6f6e4db2016-11-15 01:23:06 +000058 // Attribute byte size if fixed, just add the size to the offset.
Greg Clayton82f12b12016-11-11 16:21:37 +000059 *OffsetPtr += *FixedSize;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000060 } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
Paul Robinson75c068c2017-06-26 18:43:01 +000061 OffsetPtr, U.getFormParams())) {
Greg Clayton6f6e4db2016-11-15 01:23:06 +000062 // We failed to skip this attribute's value, restore the original offset
63 // and return the failure status.
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000064 *OffsetPtr = Offset;
65 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000066 }
67 }
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +000068 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000069}