Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
DWARFUnitIndex.cpp
Go to the documentation of this file.
1//===- DWARFUnitIndex.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/DWARFUnitIndex.h"
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/DataExtractor.h"
13#include "llvm/Support/ErrorHandling.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <cinttypes>
17#include <cstdint>
18
19using namespacellvm;
20
21namespace{
22
23enum class DWARFSectionKindV2 {
24 DW_SECT_INFO = 1,
25 DW_SECT_TYPES = 2,
26 DW_SECT_ABBREV = 3,
27 DW_SECT_LINE = 4,
28 DW_SECT_LOC = 5,
29 DW_SECT_STR_OFFSETS = 6,
30 DW_SECT_MACINFO = 7,
31 DW_SECT_MACRO = 8,
32};
33
34}// namespace
35
36// Return true if the section identifier is defined in the DWARFv5 standard.
37constexprboolisKnownV5SectionID(uint32_tID) {
38returnID >= DW_SECT_INFO &&ID <= DW_SECT_RNGLISTS &&
39ID !=DW_SECT_EXT_TYPES;
40}
41
42uint32_tllvm::serializeSectionKind(DWARFSectionKind Kind,
43unsigned IndexVersion) {
44if (IndexVersion == 5) {
45assert(isKnownV5SectionID(Kind));
46returnstatic_cast<uint32_t>(Kind);
47 }
48assert(IndexVersion == 2);
49switch (Kind) {
50#define CASE(S,T) \
51 case DW_SECT_##S: \
52 return static_cast<uint32_t>(DWARFSectionKindV2::DW_SECT_##T)
53CASE(INFO, INFO);
54CASE(EXT_TYPES,TYPES);
55CASE(ABBREV, ABBREV);
56CASE(LINE, LINE);
57CASE(EXT_LOC, LOC);
58CASE(STR_OFFSETS, STR_OFFSETS);
59CASE(EXT_MACINFO, MACINFO);
60CASE(MACRO, MACRO);
61#undef CASE
62default:
63// All other section kinds have no corresponding values in v2 indexes.
64llvm_unreachable("Invalid DWARFSectionKind");
65 }
66}
67
68DWARFSectionKindllvm::deserializeSectionKind(uint32_tValue,
69unsigned IndexVersion) {
70if (IndexVersion == 5)
71returnisKnownV5SectionID(Value)
72 ?static_cast<DWARFSectionKind>(Value)
73 :DW_SECT_EXT_unknown;
74assert(IndexVersion == 2);
75switch (static_cast<DWARFSectionKindV2>(Value)) {
76#define CASE(S,T) \
77 case DWARFSectionKindV2::DW_SECT_##S: \
78 return DW_SECT_##T
79CASE(INFO, INFO);
80CASE(TYPES, EXT_TYPES);
81CASE(ABBREV, ABBREV);
82CASE(LINE, LINE);
83CASE(LOC, EXT_LOC);
84CASE(STR_OFFSETS, STR_OFFSETS);
85CASE(MACINFO, EXT_MACINFO);
86CASE(MACRO, MACRO);
87#undef CASE
88 }
89returnDW_SECT_EXT_unknown;
90}
91
92bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,
93uint64_t *OffsetPtr) {
94constuint64_t BeginOffset = *OffsetPtr;
95if (!IndexData.isValidOffsetForDataOfSize(*OffsetPtr, 16))
96returnfalse;
97// GCC Debug Fission defines the version as an unsigned 32-bit field
98// with value of 2, https://gcc.gnu.org/wiki/DebugFissionDWP.
99// DWARFv5 defines the same space as an uhalf version field with value of 5
100// and a 2 bytes long padding, see Section 7.3.5.3.
101 Version = IndexData.getU32(OffsetPtr);
102if (Version != 2) {
103 *OffsetPtr = BeginOffset;
104 Version = IndexData.getU16(OffsetPtr);
105if (Version != 5)
106returnfalse;
107 *OffsetPtr += 2;// Skip padding.
108 }
109 NumColumns = IndexData.getU32(OffsetPtr);
110 NumUnits = IndexData.getU32(OffsetPtr);
111 NumBuckets = IndexData.getU32(OffsetPtr);
112returntrue;
113}
114
115void DWARFUnitIndex::Header::dump(raw_ostream &OS) const{
116OS <<format("version = %u, units = %u, slots = %u\n\n", Version, NumUnits, NumBuckets);
117}
118
119boolDWARFUnitIndex::parse(DataExtractor IndexData) {
120bool b = parseImpl(IndexData);
121if (!b) {
122// Make sure we don't try to dump anything
123 Header.NumBuckets = 0;
124// Release any partially initialized data.
125 ColumnKinds.reset();
126 Rows.reset();
127 }
128return b;
129}
130
131bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {
132uint64_tOffset = 0;
133if (!Header.parse(IndexData, &Offset))
134returnfalse;
135
136// Fix InfoColumnKind: in DWARFv5, type units are in .debug_info.dwo.
137if (Header.Version == 5)
138 InfoColumnKind = DW_SECT_INFO;
139
140if (!IndexData.isValidOffsetForDataOfSize(
141Offset, Header.NumBuckets * (8 + 4) +
142 (2 * Header.NumUnits + 1) * 4 * Header.NumColumns))
143returnfalse;
144
145 Rows = std::make_unique<Entry[]>(Header.NumBuckets);
146auto Contribs =
147 std::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);
148 ColumnKinds = std::make_unique<DWARFSectionKind[]>(Header.NumColumns);
149 RawSectionIds = std::make_unique<uint32_t[]>(Header.NumColumns);
150
151// Read Hash Table of Signatures
152for (unsigned i = 0; i != Header.NumBuckets; ++i)
153 Rows[i].Signature = IndexData.getU64(&Offset);
154
155// Read Parallel Table of Indexes
156for (unsigned i = 0; i != Header.NumBuckets; ++i) {
157auto Index = IndexData.getU32(&Offset);
158if (!Index)
159continue;
160 Rows[i].Index =this;
161 Rows[i].Contributions =
162 std::make_unique<Entry::SectionContribution[]>(Header.NumColumns);
163 Contribs[Index - 1] = Rows[i].Contributions.get();
164 }
165
166// Read the Column Headers
167for (unsigned i = 0; i != Header.NumColumns; ++i) {
168 RawSectionIds[i] = IndexData.getU32(&Offset);
169 ColumnKinds[i] =deserializeSectionKind(RawSectionIds[i], Header.Version);
170if (ColumnKinds[i] == InfoColumnKind) {
171if (InfoColumn != -1)
172returnfalse;
173 InfoColumn = i;
174 }
175 }
176
177if (InfoColumn == -1)
178returnfalse;
179
180// Read Table of Section Offsets
181for (unsigned i = 0; i != Header.NumUnits; ++i) {
182auto *Contrib = Contribs[i];
183for (unsigned i = 0; i != Header.NumColumns; ++i)
184 Contrib[i].setOffset(IndexData.getU32(&Offset));
185 }
186
187// Read Table of Section Sizes
188for (unsigned i = 0; i != Header.NumUnits; ++i) {
189auto *Contrib = Contribs[i];
190for (unsigned i = 0; i != Header.NumColumns; ++i)
191 Contrib[i].setLength(IndexData.getU32(&Offset));
192 }
193
194returntrue;
195}
196
197StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {
198switch (DS) {
199#define HANDLE_DW_SECT(ID, NAME) \
200 case DW_SECT_##NAME: \
201 return #NAME;
202#include "llvm/BinaryFormat/Dwarf.def"
203caseDW_SECT_EXT_TYPES:
204return"TYPES";
205caseDW_SECT_EXT_LOC:
206return"LOC";
207caseDW_SECT_EXT_MACINFO:
208return"MACINFO";
209caseDW_SECT_EXT_unknown:
210returnStringRef();
211 }
212llvm_unreachable("Unknown DWARFSectionKind");
213}
214
215voidDWARFUnitIndex::dump(raw_ostream &OS) const{
216if (!*this)
217return;
218
219 Header.dump(OS);
220OS <<"Index Signature ";
221for (unsigned i = 0; i != Header.NumColumns; ++i) {
222DWARFSectionKind Kind = ColumnKinds[i];
223StringRefName = getColumnHeader(Kind);
224if (!Name.empty())
225OS <<' '
226 <<left_justify(Name,
227 Kind == DWARFSectionKind::DW_SECT_INFO ? 40 : 24);
228else
229OS <<format(" Unknown: %-15" PRIu32, RawSectionIds[i]);
230 }
231OS <<"\n----- ------------------";
232for (unsigned i = 0; i != Header.NumColumns; ++i) {
233DWARFSectionKind Kind = ColumnKinds[i];
234if (Kind == DWARFSectionKind::DW_SECT_INFO ||
235 Kind ==DWARFSectionKind::DW_SECT_EXT_TYPES)
236OS <<" ----------------------------------------";
237else
238OS <<" ------------------------";
239 }
240OS <<'\n';
241for (unsigned i = 0; i != Header.NumBuckets; ++i) {
242auto &Row = Rows[i];
243if (auto *Contribs = Row.Contributions.get()) {
244OS <<format("%5u 0x%016" PRIx64" ", i + 1, Row.Signature);
245for (unsigned i = 0; i != Header.NumColumns; ++i) {
246auto &Contrib = Contribs[i];
247DWARFSectionKind Kind = ColumnKinds[i];
248if (Kind == DWARFSectionKind::DW_SECT_INFO ||
249 Kind ==DWARFSectionKind::DW_SECT_EXT_TYPES)
250OS <<format("[0x%016" PRIx64", 0x%016" PRIx64") ",
251 Contrib.getOffset(),
252 Contrib.getOffset() + Contrib.getLength());
253else
254OS <<format("[0x%08" PRIx32", 0x%08" PRIx32") ",
255 Contrib.getOffset32(),
256 Contrib.getOffset32() + Contrib.getLength32());
257 }
258OS <<'\n';
259 }
260 }
261}
262
263constDWARFUnitIndex::Entry::SectionContribution *
264DWARFUnitIndex::Entry::getContribution(DWARFSectionKind Sec) const{
265uint32_t i = 0;
266for (; i != Index->Header.NumColumns; ++i)
267if (Index->ColumnKinds[i] == Sec)
268return &Contributions[i];
269returnnullptr;
270}
271
272DWARFUnitIndex::Entry::SectionContribution &
273DWARFUnitIndex::Entry::getContribution() {
274return Contributions[Index->InfoColumn];
275}
276
277constDWARFUnitIndex::Entry::SectionContribution *
278DWARFUnitIndex::Entry::getContribution() const{
279return &Contributions[Index->InfoColumn];
280}
281
282constDWARFUnitIndex::Entry *
283DWARFUnitIndex::getFromOffset(uint64_tOffset) const{
284if (OffsetLookup.empty()) {
285for (uint32_t i = 0; i != Header.NumBuckets; ++i)
286if (Rows[i].Contributions)
287 OffsetLookup.push_back(&Rows[i]);
288llvm::sort(OffsetLookup, [&](Entry *E1,Entry *E2) {
289return E1->Contributions[InfoColumn].getOffset() <
290 E2->Contributions[InfoColumn].getOffset();
291 });
292 }
293autoI =partition_point(OffsetLookup, [&](Entry *E2) {
294return E2->Contributions[InfoColumn].getOffset() <=Offset;
295 });
296if (I == OffsetLookup.begin())
297returnnullptr;
298 --I;
299constauto *E = *I;
300constauto &InfoContrib = E->Contributions[InfoColumn];
301if ((InfoContrib.getOffset() + InfoContrib.getLength()) <=Offset)
302returnnullptr;
303return E;
304}
305
306constDWARFUnitIndex::Entry *DWARFUnitIndex::getFromHash(uint64_t S) const{
307uint64_t Mask = Header.NumBuckets - 1;
308
309autoH = S & Mask;
310auto HP = ((S >> 32) & Mask) | 1;
311// The spec says "while 0 is a valid hash value, the row index in a used slot
312// will always be non-zero". Loop until we find a match or an empty slot.
313while (Rows[H].getSignature() != S && Rows[H].Index !=nullptr)
314H = (H + HP) & Mask;
315
316// If the slot is empty, we don't care whether the signature matches (it could
317// be zero and still match the zeros in the empty slot).
318if (Rows[H].Index ==nullptr)
319returnnullptr;
320
321return &Rows[H];
322}
isKnownV5SectionID
constexpr bool isKnownV5SectionID(uint32_t ID)
Definition:DWARFUnitIndex.cpp:37
CASE
#define CASE(S, T)
DWARFUnitIndex.h
DataExtractor.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Format.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
H
#define H(x, y, z)
Definition:MD5.cpp:57
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
StringRef.h
getSignature
static std::string getSignature(FunctionType *FTy)
Definition:WebAssemblyLowerEmscriptenEHSjLj.cpp:427
TYPES
#define TYPES
Definition:X86DisassemblerDecoderCommon.h:489
llvm::DWARFUnitIndex::Entry::SectionContribution
Definition:DWARFUnitIndex.h:113
llvm::DWARFUnitIndex::Entry
Definition:DWARFUnitIndex.h:111
llvm::DWARFUnitIndex::Entry::getContribution
const SectionContribution * getContribution() const
Definition:DWARFUnitIndex.cpp:278
llvm::DWARFUnitIndex::dump
void dump(raw_ostream &OS) const
Definition:DWARFUnitIndex.cpp:215
llvm::DWARFUnitIndex::parse
bool parse(DataExtractor IndexData)
Definition:DWARFUnitIndex.cpp:119
llvm::DWARFUnitIndex::getFromHash
const Entry * getFromHash(uint64_t Offset) const
Definition:DWARFUnitIndex.cpp:306
llvm::DWARFUnitIndex::getFromOffset
const Entry * getFromOffset(uint64_t Offset) const
Definition:DWARFUnitIndex.cpp:283
llvm::DataExtractor
Definition:DataExtractor.h:41
llvm::DataExtractor::getU32
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
Definition:DataExtractor.cpp:108
llvm::DataExtractor::getU16
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Definition:DataExtractor.cpp:93
llvm::DataExtractor::getU64
uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
Definition:DataExtractor.cpp:117
llvm::DataExtractor::isValidOffsetForDataOfSize
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Definition:DataExtractor.h:672
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
uint32_t
uint64_t
unsigned
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::partition_point
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition:STLExtras.h:2050
llvm::deserializeSectionKind
DWARFSectionKind deserializeSectionKind(uint32_t Value, unsigned IndexVersion)
Convert a value read from an index section to the internal representation.
Definition:DWARFUnitIndex.cpp:68
llvm::DWARFSectionKind
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
Definition:DWARFUnitIndex.h:56
llvm::DW_SECT_EXT_LOC
@ DW_SECT_EXT_LOC
Definition:DWARFUnitIndex.h:63
llvm::DW_SECT_EXT_unknown
@ DW_SECT_EXT_unknown
Denotes a value read from an index section that does not correspond to any of the supported standards...
Definition:DWARFUnitIndex.h:59
llvm::DW_SECT_EXT_TYPES
@ DW_SECT_EXT_TYPES
Definition:DWARFUnitIndex.h:62
llvm::DW_SECT_EXT_MACINFO
@ DW_SECT_EXT_MACINFO
Definition:DWARFUnitIndex.h:64
llvm::serializeSectionKind
uint32_t serializeSectionKind(DWARFSectionKind Kind, unsigned IndexVersion)
Convert the internal value for a section kind to an on-disk value.
Definition:DWARFUnitIndex.cpp:42
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition:STLExtras.h:1664
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition:Format.h:125
llvm::left_justify
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition:Format.h:146
raw_ostream.h

Generated on Thu Jul 17 2025 11:56:10 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp