1//===- DWARFDebugPubTable.cpp ---------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/BinaryFormat/Dwarf.h"
12#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
13#include "llvm/Support/DataExtractor.h"
14#include "llvm/Support/Errc.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/FormatAdapters.h"
17#include "llvm/Support/FormatVariadic.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cstdint>
20
21using namespace llvm;
22using namespace dwarf;
23
24void DWARFDebugPubTable::extract(
25 DWARFDataExtractor Data, bool GnuStyle,
26 function_ref<void(Error)> RecoverableErrorHandler) {
27 this->GnuStyle = GnuStyle;
28 Sets.clear();
29 uint64_t Offset = 0;
30 while (Data.isValidOffset(offset: Offset)) {
31 uint64_t SetOffset = Offset;
32 Sets.push_back(x: {});
33 Set &NewSet = Sets.back();
34
35 DataExtractor::Cursor C(Offset);
36 std::tie(args&: NewSet.Length, args&: NewSet.Format) = Data.getInitialLength(C);
37 if (!C) {
38 // Drop the newly added set because it does not contain anything useful
39 // to dump.
40 Sets.pop_back();
41 RecoverableErrorHandler(createStringError(
42 EC: errc::invalid_argument,
43 Fmt: "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
44 Vals: SetOffset, Vals: toString(E: C.takeError()).c_str()));
45 return;
46 }
47
48 Offset = C.tell() + NewSet.Length;
49 DWARFDataExtractor SetData(Data, Offset);
50 const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format: NewSet.Format);
51
52 NewSet.Version = SetData.getU16(C);
53 NewSet.Offset = SetData.getRelocatedValue(C, Size: OffsetSize);
54 NewSet.Size = SetData.getUnsigned(C, Size: OffsetSize);
55
56 if (!C) {
57 // Preserve the newly added set because at least some fields of the header
58 // are read and can be dumped.
59 RecoverableErrorHandler(
60 createStringError(EC: errc::invalid_argument,
61 Fmt: "name lookup table at offset 0x%" PRIx64
62 " does not have a complete header: %s",
63 Vals: SetOffset, Vals: toString(E: C.takeError()).c_str()));
64 continue;
65 }
66
67 while (C) {
68 uint64_t DieRef = SetData.getUnsigned(C, Size: OffsetSize);
69 if (DieRef == 0)
70 break;
71 uint8_t IndexEntryValue = GnuStyle ? SetData.getU8(C) : 0;
72 StringRef Name = SetData.getCStrRef(C);
73 if (C)
74 NewSet.Entries.push_back(
75 x: {.SecOffset: DieRef, .Descriptor: PubIndexEntryDescriptor(IndexEntryValue), .Name: Name});
76 }
77
78 if (!C) {
79 RecoverableErrorHandler(createStringError(
80 EC: errc::invalid_argument,
81 Fmt: "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
82 Vals: SetOffset, Vals: toString(E: C.takeError()).c_str()));
83 continue;
84 }
85 if (C.tell() != Offset)
86 RecoverableErrorHandler(createStringError(
87 EC: errc::invalid_argument,
88 Fmt: "name lookup table at offset 0x%" PRIx64
89 " has a terminator at offset 0x%" PRIx64
90 " before the expected end at 0x%" PRIx64,
91 Vals: SetOffset, Vals: C.tell() - OffsetSize, Vals: Offset - OffsetSize));
92 }
93}
94
95void DWARFDebugPubTable::dump(raw_ostream &OS) const {
96 for (const Set &S : Sets) {
97 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format: S.Format);
98 OS << "length = "
99 << formatv(Fmt: "0x{0:x-}",
100 Vals: fmt_align(Item: S.Length, Where: AlignStyle::Right, Amount: OffsetDumpWidth, Fill: '0'));
101 OS << ", format = " << dwarf::FormatString(Format: S.Format);
102 OS << ", version = " << formatv(Fmt: "{0:x4}", Vals: S.Version);
103 OS << ", unit_offset = "
104 << formatv(Fmt: "0x{0:x-}",
105 Vals: fmt_align(Item: S.Offset, Where: AlignStyle::Right, Amount: OffsetDumpWidth, Fill: '0'));
106 OS << ", unit_size = "
107 << formatv(Fmt: "0x{0:x-}",
108 Vals: fmt_align(Item: S.Size, Where: AlignStyle::Right, Amount: OffsetDumpWidth, Fill: '0'))
109 << '\n';
110 OS << (GnuStyle ? "Offset Linkage Kind Name\n"
111 : "Offset Name\n");
112
113 for (const Entry &E : S.Entries) {
114 OS << formatv(Fmt: "0x{0:x-} ", Vals: fmt_align(Item: E.SecOffset, Where: AlignStyle::Right,
115 Amount: OffsetDumpWidth, Fill: '0'));
116 if (GnuStyle) {
117 StringRef EntryLinkage =
118 GDBIndexEntryLinkageString(Linkage: E.Descriptor.Linkage);
119 StringRef EntryKind = dwarf::GDBIndexEntryKindString(Kind: E.Descriptor.Kind);
120 OS << formatv(Fmt: "{0,-8}", Vals: EntryLinkage.data()) << ' '
121 << formatv(Fmt: "{0,-8}", Vals: EntryKind.data()) << ' ';
122 }
123 OS << '\"' << E.Name << "\"\n";
124 }
125 }
126}
127