LLVM 19.0.0git
DWARFDebugPubTable.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/Errc.h"
15#include "llvm/Support/Format.h"
17#include <cstdint>
18
19using namespace llvm;
20using namespace dwarf;
21
23 DWARFDataExtractor Data, bool GnuStyle,
24 function_ref<void(Error)> RecoverableErrorHandler) {
25 this->GnuStyle = GnuStyle;
26 Sets.clear();
27 uint64_t Offset = 0;
28 while (Data.isValidOffset(Offset)) {
29 uint64_t SetOffset = Offset;
30 Sets.push_back({});
31 Set &NewSet = Sets.back();
32
34 std::tie(NewSet.Length, NewSet.Format) = Data.getInitialLength(C);
35 if (!C) {
36 // Drop the newly added set because it does not contain anything useful
37 // to dump.
38 Sets.pop_back();
39 RecoverableErrorHandler(createStringError(
41 "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
42 SetOffset, toString(C.takeError()).c_str()));
43 return;
44 }
45
46 Offset = C.tell() + NewSet.Length;
48 const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(NewSet.Format);
49
50 NewSet.Version = SetData.getU16(C);
51 NewSet.Offset = SetData.getRelocatedValue(C, OffsetSize);
52 NewSet.Size = SetData.getUnsigned(C, OffsetSize);
53
54 if (!C) {
55 // Preserve the newly added set because at least some fields of the header
56 // are read and can be dumped.
57 RecoverableErrorHandler(
59 "name lookup table at offset 0x%" PRIx64
60 " does not have a complete header: %s",
61 SetOffset, toString(C.takeError()).c_str()));
62 continue;
63 }
64
65 while (C) {
66 uint64_t DieRef = SetData.getUnsigned(C, OffsetSize);
67 if (DieRef == 0)
68 break;
69 uint8_t IndexEntryValue = GnuStyle ? SetData.getU8(C) : 0;
70 StringRef Name = SetData.getCStrRef(C);
71 if (C)
72 NewSet.Entries.push_back(
73 {DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});
74 }
75
76 if (!C) {
77 RecoverableErrorHandler(createStringError(
79 "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
80 SetOffset, toString(C.takeError()).c_str()));
81 continue;
82 }
83 if (C.tell() != Offset)
84 RecoverableErrorHandler(createStringError(
86 "name lookup table at offset 0x%" PRIx64
87 " has a terminator at offset 0x%" PRIx64
88 " before the expected end at 0x%" PRIx64,
89 SetOffset, C.tell() - OffsetSize, Offset - OffsetSize));
90 }
91}
92
94 for (const Set &S : Sets) {
95 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(S.Format);
96 OS << "length = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Length);
97 OS << ", format = " << dwarf::FormatString(S.Format);
98 OS << ", version = " << format("0x%04x", S.Version);
99 OS << ", unit_offset = "
100 << format("0x%0*" PRIx64, OffsetDumpWidth, S.Offset);
101 OS << ", unit_size = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Size)
102 << '\n';
103 OS << (GnuStyle ? "Offset Linkage Kind Name\n"
104 : "Offset Name\n");
105
106 for (const Entry &E : S.Entries) {
107 OS << format("0x%0*" PRIx64 " ", OffsetDumpWidth, E.SecOffset);
108 if (GnuStyle) {
109 StringRef EntryLinkage =
112 OS << format("%-8s", EntryLinkage.data()) << ' '
113 << format("%-8s", EntryKind.data()) << ' ';
114 }
115 OS << '\"' << E.Name << "\"\n";
116 }
117 }
118}
This file contains constants used for implementing Dwarf debug support.
std::string Name
raw_pwrite_stream & OS
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, uint64_t *SectionIndex=nullptr, Error *Err=nullptr) const
Extracts a value and applies a relocation to the result if one exists for the given offset.
void extract(DWARFDataExtractor Data, bool GnuStyle, function_ref< void(Error)> RecoverableErrorHandler)
void dump(raw_ostream &OS) const
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage)
Definition: Dwarf.cpp:664
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:826
StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind)
Definition: Dwarf.cpp:641
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:739
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
uint64_t SecOffset
Section offset from the beginning of the compilation unit.
StringRef Name
The name of the object as given by the DW_AT_name attribute of the referenced DIE.
dwarf::PubIndexEntryDescriptor Descriptor
An entry of the various gnu_pub* debug sections.
Each table consists of sets of variable length entries.
uint64_t Length
The total length of the entries for that set, not including the length field itself.
uint64_t Offset
The offset from the beginning of the .debug_info section of the compilation unit header referenced by...
dwarf::DwarfFormat Format
The DWARF format of the set.
uint64_t Size
The size in bytes of the contents of the .debug_info section generated to represent that compilation ...
uint16_t Version
This number is specific to the name lookup table and is independent of the DWARF version number.
Describes an entry of the various gnu_pub* debug sections.
Definition: Dwarf.h:823
GDBIndexEntryLinkage Linkage
Definition: Dwarf.h:825