1//===- DWARFAcceleratorTable.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/DWARFAcceleratorTable.h"
10
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/BinaryFormat/Dwarf.h"
13#include "llvm/Support/Compiler.h"
14#include "llvm/Support/DJB.h"
15#include "llvm/Support/Errc.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/ScopedPrinter.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cstddef>
20#include <cstdint>
21#include <utility>
22
23using namespace llvm;
24
25namespace {
26struct Atom {
27 unsigned Value;
28};
29
30static raw_ostream &operator<<(raw_ostream &OS, const Atom &A) {
31 StringRef Str = dwarf::AtomTypeString(Atom: A.Value);
32 if (!Str.empty())
33 return OS << Str;
34 return OS << "DW_ATOM_unknown_" << formatv(Fmt: "{0:x-}", Vals: A.Value);
35}
36} // namespace
37
38static Atom formatAtom(unsigned Atom) { return {.Value: Atom}; }
39
40DWARFAcceleratorTable::~DWARFAcceleratorTable() = default;
41
42Error AppleAcceleratorTable::extract() {
43 uint64_t Offset = 0;
44
45 // Check that we can at least read the header.
46 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength) + 4))
47 return createStringError(EC: errc::illegal_byte_sequence,
48 S: "Section too small: cannot read header.");
49
50 Hdr.Magic = AccelSection.getU32(offset_ptr: &Offset);
51 Hdr.Version = AccelSection.getU16(offset_ptr: &Offset);
52 Hdr.HashFunction = AccelSection.getU16(offset_ptr: &Offset);
53 Hdr.BucketCount = AccelSection.getU32(offset_ptr: &Offset);
54 Hdr.HashCount = AccelSection.getU32(offset_ptr: &Offset);
55 Hdr.HeaderDataLength = AccelSection.getU32(offset_ptr: &Offset);
56 FormParams = {.Version: Hdr.Version, .AddrSize: 0, .Format: dwarf::DwarfFormat::DWARF32};
57
58 // Check that we can read all the hashes and offsets from the
59 // section (see SourceLevelDebugging.md for the structure of the index).
60 if (!AccelSection.isValidOffset(offset: getIthBucketBase(I: Hdr.BucketCount - 1)))
61 return createStringError(
62 EC: errc::illegal_byte_sequence,
63 S: "Section too small: cannot read buckets and hashes.");
64
65 HdrData.DIEOffsetBase = AccelSection.getU32(offset_ptr: &Offset);
66 uint32_t NumAtoms = AccelSection.getU32(offset_ptr: &Offset);
67
68 HashDataEntryLength = 0;
69 auto MakeUnsupportedFormError = [](dwarf::Form Form) {
70 return createStringError(EC: errc::not_supported,
71 S: "Unsupported form:" +
72 dwarf::FormEncodingString(Encoding: Form));
73 };
74
75 for (unsigned i = 0; i < NumAtoms; ++i) {
76 uint16_t AtomType = AccelSection.getU16(offset_ptr: &Offset);
77 auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(offset_ptr: &Offset));
78 HdrData.Atoms.push_back(Elt: std::make_pair(x&: AtomType, y&: AtomForm));
79
80 std::optional<uint8_t> FormSize =
81 dwarf::getFixedFormByteSize(Form: AtomForm, Params: FormParams);
82 if (!FormSize)
83 return MakeUnsupportedFormError(AtomForm);
84 HashDataEntryLength += *FormSize;
85 }
86
87 IsValid = true;
88 return Error::success();
89}
90
91uint32_t AppleAcceleratorTable::getNumBuckets() const {
92 return Hdr.BucketCount;
93}
94uint32_t AppleAcceleratorTable::getNumHashes() const { return Hdr.HashCount; }
95uint32_t AppleAcceleratorTable::getSizeHdr() const { return sizeof(Hdr); }
96uint32_t AppleAcceleratorTable::getHeaderDataLength() const {
97 return Hdr.HeaderDataLength;
98}
99
100ArrayRef<std::pair<AppleAcceleratorTable::HeaderData::AtomType,
101 AppleAcceleratorTable::HeaderData::Form>>
102AppleAcceleratorTable::getAtomsDesc() {
103 return HdrData.Atoms;
104}
105
106bool AppleAcceleratorTable::validateForms() {
107 for (auto Atom : getAtomsDesc()) {
108 DWARFFormValue FormValue(Atom.second);
109 switch (Atom.first) {
110 case dwarf::DW_ATOM_die_offset:
111 case dwarf::DW_ATOM_die_tag:
112 case dwarf::DW_ATOM_type_flags:
113 if ((!FormValue.isFormClass(FC: DWARFFormValue::FC_Constant) &&
114 !FormValue.isFormClass(FC: DWARFFormValue::FC_Flag)) ||
115 FormValue.getForm() == dwarf::DW_FORM_sdata)
116 return false;
117 break;
118 default:
119 break;
120 }
121 }
122 return true;
123}
124
125std::pair<uint64_t, dwarf::Tag>
126AppleAcceleratorTable::readAtoms(uint64_t *HashDataOffset) {
127 uint64_t DieOffset = dwarf::DW_INVALID_OFFSET;
128 dwarf::Tag DieTag = dwarf::DW_TAG_null;
129
130 for (auto Atom : getAtomsDesc()) {
131 DWARFFormValue FormValue(Atom.second);
132 FormValue.extractValue(Data: AccelSection, OffsetPtr: HashDataOffset, FormParams);
133 switch (Atom.first) {
134 case dwarf::DW_ATOM_die_offset:
135 DieOffset = *FormValue.getAsUnsignedConstant();
136 break;
137 case dwarf::DW_ATOM_die_tag:
138 DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant();
139 break;
140 default:
141 break;
142 }
143 }
144 return {DieOffset, DieTag};
145}
146
147void AppleAcceleratorTable::Header::dump(ScopedPrinter &W) const {
148 DictScope HeaderScope(W, "Header");
149 W.printHex(Label: "Magic", Value: Magic);
150 W.printHex(Label: "Version", Value: Version);
151 W.printHex(Label: "Hash function", Value: HashFunction);
152 W.printNumber(Label: "Bucket count", Value: BucketCount);
153 W.printNumber(Label: "Hashes count", Value: HashCount);
154 W.printNumber(Label: "HeaderData length", Value: HeaderDataLength);
155}
156
157std::optional<uint64_t> AppleAcceleratorTable::HeaderData::extractOffset(
158 std::optional<DWARFFormValue> Value) const {
159 if (!Value)
160 return std::nullopt;
161
162 switch (Value->getForm()) {
163 case dwarf::DW_FORM_ref1:
164 case dwarf::DW_FORM_ref2:
165 case dwarf::DW_FORM_ref4:
166 case dwarf::DW_FORM_ref8:
167 case dwarf::DW_FORM_ref_udata:
168 return Value->getRawUValue() + DIEOffsetBase;
169 default:
170 return Value->getAsSectionOffset();
171 }
172}
173
174bool AppleAcceleratorTable::dumpName(ScopedPrinter &W,
175 SmallVectorImpl<DWARFFormValue> &AtomForms,
176 uint64_t *DataOffset) const {
177 uint64_t NameOffset = *DataOffset;
178 if (!AccelSection.isValidOffsetForDataOfSize(offset: *DataOffset, length: 4)) {
179 W.printString(Value: "Incorrectly terminated list.");
180 return false;
181 }
182 uint64_t StringOffset = AccelSection.getRelocatedValue(Size: 4, Off: DataOffset);
183 if (!StringOffset)
184 return false; // End of list
185
186 DictScope NameScope(W, ("Name@0x" + Twine::utohexstr(Val: NameOffset)).str());
187 W.startLine() << formatv(Fmt: "String: {0:x8}", Vals&: StringOffset);
188 W.getOStream() << " \"" << StringSection.getCStr(OffsetPtr: &StringOffset) << "\"\n";
189
190 unsigned NumData = AccelSection.getU32(offset_ptr: DataOffset);
191 for (unsigned Data = 0; Data < NumData; ++Data) {
192 ListScope DataScope(W, ("Data " + Twine(Data)).str());
193 unsigned i = 0;
194 for (auto &Atom : AtomForms) {
195 W.startLine() << formatv(Fmt: "Atom[{0}]: ", Vals&: i);
196 if (Atom.extractValue(Data: AccelSection, OffsetPtr: DataOffset, FormParams)) {
197 Atom.dump(OS&: W.getOStream());
198 if (std::optional<uint64_t> Val = Atom.getAsUnsignedConstant()) {
199 StringRef Str = dwarf::AtomValueString(Atom: HdrData.Atoms[i].first, Val: *Val);
200 if (!Str.empty())
201 W.getOStream() << " (" << Str << ")";
202 }
203 } else
204 W.getOStream() << "Error extracting the value";
205 W.getOStream() << "\n";
206 i++;
207 }
208 }
209 return true; // more entries follow
210}
211
212LLVM_DUMP_METHOD void AppleAcceleratorTable::dump(raw_ostream &OS) const {
213 if (!IsValid)
214 return;
215
216 ScopedPrinter W(OS);
217
218 Hdr.dump(W);
219
220 W.printNumber(Label: "DIE offset base", Value: HdrData.DIEOffsetBase);
221 W.printNumber(Label: "Number of atoms", Value: uint64_t(HdrData.Atoms.size()));
222 W.printNumber(Label: "Size of each hash data entry", Value: getHashDataEntryLength());
223 SmallVector<DWARFFormValue, 3> AtomForms;
224 {
225 ListScope AtomsScope(W, "Atoms");
226 unsigned i = 0;
227 for (const auto &Atom : HdrData.Atoms) {
228 DictScope AtomScope(W, ("Atom " + Twine(i++)).str());
229 W.startLine() << "Type: " << formatAtom(Atom: Atom.first) << '\n';
230 W.startLine() << "Form: " << formatv(Fmt: "{0}", Vals: Atom.second) << '\n';
231 AtomForms.push_back(Elt: DWARFFormValue(Atom.second));
232 }
233 }
234
235 // Now go through the actual tables and dump them.
236 uint64_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
237 uint64_t HashesBase = Offset + Hdr.BucketCount * 4;
238 uint64_t OffsetsBase = HashesBase + Hdr.HashCount * 4;
239
240 for (unsigned Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) {
241 unsigned Index = AccelSection.getU32(offset_ptr: &Offset);
242
243 ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str());
244 if (Index == UINT32_MAX) {
245 W.printString(Value: "EMPTY");
246 continue;
247 }
248
249 for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) {
250 uint64_t HashOffset = HashesBase + HashIdx*4;
251 uint64_t OffsetsOffset = OffsetsBase + HashIdx*4;
252 uint32_t Hash = AccelSection.getU32(offset_ptr: &HashOffset);
253
254 if (Hash % Hdr.BucketCount != Bucket)
255 break;
256
257 uint64_t DataOffset = AccelSection.getU32(offset_ptr: &OffsetsOffset);
258 ListScope HashScope(W, ("Hash 0x" + Twine::utohexstr(Val: Hash)).str());
259 if (!AccelSection.isValidOffset(offset: DataOffset)) {
260 W.printString(Value: "Invalid section offset");
261 continue;
262 }
263 while (dumpName(W, AtomForms, DataOffset: &DataOffset))
264 /*empty*/;
265 }
266 }
267}
268
269AppleAcceleratorTable::Entry::Entry(const AppleAcceleratorTable &Table)
270 : Table(Table) {
271 Values.reserve(N: Table.HdrData.Atoms.size());
272 for (const auto &Atom : Table.HdrData.Atoms)
273 Values.push_back(Elt: DWARFFormValue(Atom.second));
274}
275
276void AppleAcceleratorTable::Entry::extract(uint64_t *Offset) {
277 for (auto &FormValue : Values)
278 FormValue.extractValue(Data: Table.AccelSection, OffsetPtr: Offset, FormParams: Table.FormParams);
279}
280
281std::optional<DWARFFormValue>
282AppleAcceleratorTable::Entry::lookup(HeaderData::AtomType AtomToFind) const {
283 for (auto [Atom, FormValue] : zip_equal(t: Table.HdrData.Atoms, u: Values))
284 if (Atom.first == AtomToFind)
285 return FormValue;
286 return std::nullopt;
287}
288
289std::optional<uint64_t>
290AppleAcceleratorTable::Entry::getDIESectionOffset() const {
291 return Table.HdrData.extractOffset(Value: lookup(AtomToFind: dwarf::DW_ATOM_die_offset));
292}
293
294std::optional<uint64_t> AppleAcceleratorTable::Entry::getCUOffset() const {
295 return Table.HdrData.extractOffset(Value: lookup(AtomToFind: dwarf::DW_ATOM_cu_offset));
296}
297
298std::optional<dwarf::Tag> AppleAcceleratorTable::Entry::getTag() const {
299 std::optional<DWARFFormValue> Tag = lookup(AtomToFind: dwarf::DW_ATOM_die_tag);
300 if (!Tag)
301 return std::nullopt;
302 if (std::optional<uint64_t> Value = Tag->getAsUnsignedConstant())
303 return dwarf::Tag(*Value);
304 return std::nullopt;
305}
306
307AppleAcceleratorTable::SameNameIterator::SameNameIterator(
308 const AppleAcceleratorTable &AccelTable, uint64_t DataOffset)
309 : Current(AccelTable), Offset(DataOffset) {}
310
311void AppleAcceleratorTable::Iterator::prepareNextEntryOrEnd() {
312 if (NumEntriesToCome == 0)
313 prepareNextStringOrEnd();
314 if (isEnd())
315 return;
316 uint64_t OffsetCopy = Offset;
317 Current.BaseEntry.extract(Offset: &OffsetCopy);
318 NumEntriesToCome--;
319 Offset += getTable().getHashDataEntryLength();
320}
321
322void AppleAcceleratorTable::Iterator::prepareNextStringOrEnd() {
323 const AppleAcceleratorTable &Table = getTable();
324 if (Offset == 0) {
325 // Always start looking for strings using a valid offset from the Offsets
326 // table. Entries are not always consecutive.
327 std::optional<uint64_t> OptOffset = Table.readIthOffset(I: OffsetIdx++);
328 if (!OptOffset)
329 return setToEnd();
330 Offset = *OptOffset;
331 }
332 std::optional<uint32_t> StrOffset = Table.readStringOffsetAt(Offset);
333 if (!StrOffset)
334 return setToEnd();
335
336 // A zero denotes the end of the collision list. Skip to the next offset
337 // in the offsets table by setting the Offset to zero so we will grab the
338 // next offset from the offsets table.
339 if (*StrOffset == 0) {
340 Offset = 0;
341 return prepareNextStringOrEnd();
342 }
343 Current.StrOffset = *StrOffset;
344
345 std::optional<uint32_t> MaybeNumEntries = Table.readU32FromAccel(Offset);
346 if (!MaybeNumEntries || *MaybeNumEntries == 0)
347 return setToEnd();
348 NumEntriesToCome = *MaybeNumEntries;
349}
350
351AppleAcceleratorTable::Iterator::Iterator(const AppleAcceleratorTable &Table,
352 bool SetEnd)
353 : Current(Table), Offset(0), NumEntriesToCome(0) {
354 if (SetEnd)
355 setToEnd();
356 else
357 prepareNextEntryOrEnd();
358}
359
360iterator_range<AppleAcceleratorTable::SameNameIterator>
361AppleAcceleratorTable::equal_range(StringRef Key) const {
362 const auto EmptyRange =
363 make_range(x: SameNameIterator(*this, 0), y: SameNameIterator(*this, 0));
364 if (!IsValid)
365 return EmptyRange;
366
367 // Find the bucket.
368 uint32_t SearchHash = djbHash(Buffer: Key);
369 uint32_t BucketIdx = hashToBucketIdx(Hash: SearchHash);
370 std::optional<uint32_t> HashIdx = idxOfHashInBucket(HashToFind: SearchHash, BucketIdx);
371 if (!HashIdx)
372 return EmptyRange;
373
374 std::optional<uint64_t> MaybeDataOffset = readIthOffset(I: *HashIdx);
375 if (!MaybeDataOffset)
376 return EmptyRange;
377
378 uint64_t DataOffset = *MaybeDataOffset;
379 if (DataOffset >= AccelSection.size())
380 return EmptyRange;
381
382 std::optional<uint32_t> StrOffset = readStringOffsetAt(Offset&: DataOffset);
383 // Valid input and still have strings in this hash.
384 while (StrOffset && *StrOffset) {
385 std::optional<StringRef> MaybeStr = readStringFromStrSection(StringSectionOffset: *StrOffset);
386 std::optional<uint32_t> NumEntries = this->readU32FromAccel(Offset&: DataOffset);
387 if (!MaybeStr || !NumEntries)
388 return EmptyRange;
389 uint64_t EndOffset = DataOffset + *NumEntries * getHashDataEntryLength();
390 if (Key == *MaybeStr)
391 return make_range(x: {*this, DataOffset},
392 y: SameNameIterator{*this, EndOffset});
393 DataOffset = EndOffset;
394 StrOffset = readStringOffsetAt(Offset&: DataOffset);
395 }
396
397 return EmptyRange;
398}
399
400std::optional<uint32_t>
401AppleAcceleratorTable::idxOfHashInBucket(uint32_t HashToFind,
402 uint32_t BucketIdx) const {
403 std::optional<uint32_t> HashStartIdx = readIthBucket(I: BucketIdx);
404 if (!HashStartIdx)
405 return std::nullopt;
406
407 for (uint32_t HashIdx = *HashStartIdx; HashIdx < getNumHashes(); HashIdx++) {
408 std::optional<uint32_t> MaybeHash = readIthHash(I: HashIdx);
409 if (!MaybeHash || !wouldHashBeInBucket(Hash: *MaybeHash, BucketIdx))
410 break;
411 if (*MaybeHash == HashToFind)
412 return HashIdx;
413 }
414 return std::nullopt;
415}
416
417std::optional<StringRef> AppleAcceleratorTable::readStringFromStrSection(
418 uint64_t StringSectionOffset) const {
419 Error E = Error::success();
420 StringRef Str = StringSection.getCStrRef(OffsetPtr: &StringSectionOffset, Err: &E);
421 if (E) {
422 consumeError(Err: std::move(E));
423 return std::nullopt;
424 }
425 return Str;
426}
427
428std::optional<uint32_t>
429AppleAcceleratorTable::readU32FromAccel(uint64_t &Offset,
430 bool UseRelocation) const {
431 Error E = Error::success();
432 uint32_t Data = UseRelocation
433 ? AccelSection.getRelocatedValue(Size: 4, Off: &Offset, SectionIndex: nullptr, Err: &E)
434 : AccelSection.getU32(offset_ptr: &Offset, Err: &E);
435 if (E) {
436 consumeError(Err: std::move(E));
437 return std::nullopt;
438 }
439 return Data;
440}
441
442void DWARFDebugNames::Header::dump(ScopedPrinter &W) const {
443 DictScope HeaderScope(W, "Header");
444 W.printHex(Label: "Length", Value: UnitLength);
445 W.printString(Label: "Format", Value: dwarf::FormatString(Format));
446 W.printNumber(Label: "Version", Value: Version);
447 W.printNumber(Label: "CU count", Value: CompUnitCount);
448 W.printNumber(Label: "Local TU count", Value: LocalTypeUnitCount);
449 W.printNumber(Label: "Foreign TU count", Value: ForeignTypeUnitCount);
450 W.printNumber(Label: "Bucket count", Value: BucketCount);
451 W.printNumber(Label: "Name count", Value: NameCount);
452 W.printHex(Label: "Abbreviations table size", Value: AbbrevTableSize);
453 W.startLine() << "Augmentation: '" << AugmentationString << "'\n";
454}
455
456Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS,
457 uint64_t *Offset) {
458 auto HeaderError = [Offset = *Offset](Error E) {
459 return createStringError(EC: errc::illegal_byte_sequence,
460 Fmt: "parsing .debug_names header at 0x%" PRIx64 ": %s",
461 Vals: Offset, Vals: toString(E: std::move(E)).c_str());
462 };
463
464 DataExtractor::Cursor C(*Offset);
465 std::tie(args&: UnitLength, args&: Format) = AS.getInitialLength(C);
466
467 Version = AS.getU16(C);
468 AS.skip(C, Length: 2); // padding
469 CompUnitCount = AS.getU32(C);
470 LocalTypeUnitCount = AS.getU32(C);
471 ForeignTypeUnitCount = AS.getU32(C);
472 BucketCount = AS.getU32(C);
473 NameCount = AS.getU32(C);
474 AbbrevTableSize = AS.getU32(C);
475 AugmentationStringSize = alignTo(Value: AS.getU32(C), Align: 4);
476
477 if (!C)
478 return HeaderError(C.takeError());
479
480 if (!AS.isValidOffsetForDataOfSize(offset: C.tell(), length: AugmentationStringSize))
481 return HeaderError(createStringError(EC: errc::illegal_byte_sequence,
482 S: "cannot read header augmentation"));
483 AugmentationString.resize(N: AugmentationStringSize);
484 AS.getU8(C, Dst: reinterpret_cast<uint8_t *>(AugmentationString.data()),
485 Count: AugmentationStringSize);
486 *Offset = C.tell();
487 return C.takeError();
488}
489
490void DWARFDebugNames::Abbrev::dump(ScopedPrinter &W) const {
491 DictScope AbbrevScope(W, ("Abbreviation 0x" + Twine::utohexstr(Val: Code)).str());
492 W.startLine() << formatv(Fmt: "Tag: {0}\n", Vals: Tag);
493
494 for (const auto &Attr : Attributes)
495 W.startLine() << formatv(Fmt: "{0}: {1}\n", Vals: Attr.Index, Vals: Attr.Form);
496}
497
498static constexpr DWARFDebugNames::AttributeEncoding sentinelAttrEnc() {
499 return {dwarf::Index(0), dwarf::Form(0)};
500}
501
502static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE) {
503 return AE == sentinelAttrEnc();
504}
505
506static DWARFDebugNames::Abbrev sentinelAbbrev() {
507 return DWARFDebugNames::Abbrev(0, dwarf::Tag(0), 0, {});
508}
509
510static bool isSentinel(const DWARFDebugNames::Abbrev &Abbr) {
511 return Abbr.Code == 0;
512}
513
514Expected<DWARFDebugNames::AttributeEncoding>
515DWARFDebugNames::NameIndex::extractAttributeEncoding(uint64_t *Offset) {
516 if (*Offset >= Offsets.EntriesBase) {
517 return createStringError(EC: errc::illegal_byte_sequence,
518 S: "Incorrectly terminated abbreviation table.");
519 }
520
521 uint32_t Index = Section.AccelSection.getULEB128(offset_ptr: Offset);
522 uint32_t Form = Section.AccelSection.getULEB128(offset_ptr: Offset);
523 return AttributeEncoding(dwarf::Index(Index), dwarf::Form(Form));
524}
525
526Expected<std::vector<DWARFDebugNames::AttributeEncoding>>
527DWARFDebugNames::NameIndex::extractAttributeEncodings(uint64_t *Offset) {
528 std::vector<AttributeEncoding> Result;
529 for (;;) {
530 auto AttrEncOr = extractAttributeEncoding(Offset);
531 if (!AttrEncOr)
532 return AttrEncOr.takeError();
533 if (isSentinel(AE: *AttrEncOr))
534 return std::move(Result);
535
536 Result.emplace_back(args&: *AttrEncOr);
537 }
538}
539
540Expected<DWARFDebugNames::Abbrev>
541DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
542 if (*Offset >= Offsets.EntriesBase) {
543 return createStringError(EC: errc::illegal_byte_sequence,
544 S: "Incorrectly terminated abbreviation table.");
545 }
546 const uint64_t AbbrevOffset = *Offset;
547 uint32_t Code = Section.AccelSection.getULEB128(offset_ptr: Offset);
548 if (Code == 0)
549 return sentinelAbbrev();
550
551 uint32_t Tag = Section.AccelSection.getULEB128(offset_ptr: Offset);
552 auto AttrEncOr = extractAttributeEncodings(Offset);
553 if (!AttrEncOr)
554 return AttrEncOr.takeError();
555 return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
556}
557
558DWARFDebugNames::DWARFDebugNamesOffsets
559dwarf::findDebugNamesOffsets(uint64_t EndOfHeaderOffset,
560 const DWARFDebugNames::Header &Hdr) {
561 uint64_t DwarfSize = getDwarfOffsetByteSize(Format: Hdr.Format);
562 DWARFDebugNames::DWARFDebugNamesOffsets Ret;
563 Ret.CUsBase = EndOfHeaderOffset;
564 Ret.BucketsBase = Ret.CUsBase + Hdr.CompUnitCount * DwarfSize +
565 Hdr.LocalTypeUnitCount * DwarfSize +
566 Hdr.ForeignTypeUnitCount * 8;
567 Ret.HashesBase = Ret.BucketsBase + Hdr.BucketCount * 4;
568 Ret.StringOffsetsBase =
569 Ret.HashesBase + (Hdr.BucketCount > 0 ? Hdr.NameCount * 4 : 0);
570 Ret.EntryOffsetsBase = Ret.StringOffsetsBase + Hdr.NameCount * DwarfSize;
571 Ret.EntriesBase =
572 Ret.EntryOffsetsBase + Hdr.NameCount * DwarfSize + Hdr.AbbrevTableSize;
573 return Ret;
574}
575
576Error DWARFDebugNames::NameIndex::extract() {
577 const DWARFDataExtractor &AS = Section.AccelSection;
578 uint64_t EndOfHeaderOffset = Base;
579 if (Error E = Hdr.extract(AS, Offset: &EndOfHeaderOffset))
580 return E;
581
582 const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Format: Hdr.Format);
583 Offsets = dwarf::findDebugNamesOffsets(EndOfHeaderOffset, Hdr);
584
585 uint64_t Offset =
586 Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
587
588 if (!AS.isValidOffsetForDataOfSize(offset: Offset, length: Hdr.AbbrevTableSize))
589 return createStringError(EC: errc::illegal_byte_sequence,
590 S: "Section too small: cannot read abbreviations.");
591
592 Offsets.EntriesBase = Offset + Hdr.AbbrevTableSize;
593
594 for (;;) {
595 auto AbbrevOr = extractAbbrev(Offset: &Offset);
596 if (!AbbrevOr)
597 return AbbrevOr.takeError();
598 if (isSentinel(Abbr: *AbbrevOr))
599 return Error::success();
600
601 if (!Abbrevs.insert(V: std::move(*AbbrevOr)).second)
602 return createStringError(EC: errc::invalid_argument,
603 S: "Duplicate abbreviation code.");
604 }
605}
606
607DWARFDebugNames::Entry::Entry(const NameIndex &NameIdx, const Abbrev &Abbr)
608 : NameIdx(&NameIdx), Abbr(&Abbr) {
609 // This merely creates form values. It is up to the caller
610 // (NameIndex::getEntry) to populate them.
611 Values.reserve(N: Abbr.Attributes.size());
612 for (const auto &Attr : Abbr.Attributes)
613 Values.emplace_back(Args: Attr.Form);
614}
615
616std::optional<DWARFFormValue>
617DWARFDebugNames::Entry::lookup(dwarf::Index Index) const {
618 assert(Abbr->Attributes.size() == Values.size());
619 for (auto Tuple : zip_first(t: Abbr->Attributes, u: Values)) {
620 if (std::get<0>(t&: Tuple).Index == Index)
621 return std::get<1>(t&: Tuple);
622 }
623 return std::nullopt;
624}
625
626bool DWARFDebugNames::Entry::hasParentInformation() const {
627 return lookup(Index: dwarf::DW_IDX_parent).has_value();
628}
629
630std::optional<uint64_t> DWARFDebugNames::Entry::getDIEUnitOffset() const {
631 if (std::optional<DWARFFormValue> Off = lookup(Index: dwarf::DW_IDX_die_offset))
632 return Off->getAsReferenceUVal();
633 return std::nullopt;
634}
635
636std::optional<uint64_t> DWARFDebugNames::Entry::getRelatedCUIndex() const {
637 // Return the DW_IDX_compile_unit attribute value if it is specified.
638 if (std::optional<DWARFFormValue> Off = lookup(Index: dwarf::DW_IDX_compile_unit))
639 return Off->getAsUnsignedConstant();
640 // In a per-CU index, the entries without a DW_IDX_compile_unit attribute
641 // implicitly refer to the single CU.
642 if (NameIdx->getCUCount() == 1)
643 return 0;
644 return std::nullopt;
645}
646
647std::optional<uint64_t> DWARFDebugNames::Entry::getCUIndex() const {
648 // Return the DW_IDX_compile_unit attribute value but only if we don't have a
649 // DW_IDX_type_unit attribute. Use Entry::getRelatedCUIndex() to get the
650 // associated CU index if this behaviour is not desired.
651 if (lookup(Index: dwarf::DW_IDX_type_unit).has_value())
652 return std::nullopt;
653 return getRelatedCUIndex();
654}
655
656std::optional<uint64_t> DWARFDebugNames::Entry::getCUOffset() const {
657 std::optional<uint64_t> Index = getCUIndex();
658 if (!Index || *Index >= NameIdx->getCUCount())
659 return std::nullopt;
660 return NameIdx->getCUOffset(CU: *Index);
661}
662
663std::optional<uint64_t> DWARFDebugNames::Entry::getRelatedCUOffset() const {
664 std::optional<uint64_t> Index = getRelatedCUIndex();
665 if (!Index || *Index >= NameIdx->getCUCount())
666 return std::nullopt;
667 return NameIdx->getCUOffset(CU: *Index);
668}
669
670std::optional<uint64_t> DWARFDebugNames::Entry::getLocalTUOffset() const {
671 std::optional<uint64_t> Index = getTUIndex();
672 if (!Index || *Index >= NameIdx->getLocalTUCount())
673 return std::nullopt;
674 return NameIdx->getLocalTUOffset(TU: *Index);
675}
676
677std::optional<uint64_t>
678DWARFDebugNames::Entry::getForeignTUTypeSignature() const {
679 std::optional<uint64_t> Index = getTUIndex();
680 const uint32_t NumLocalTUs = NameIdx->getLocalTUCount();
681 if (!Index || *Index < NumLocalTUs)
682 return std::nullopt; // Invalid TU index or TU index is for a local TU
683 // The foreign TU index is the TU index minus the number of local TUs.
684 const uint64_t ForeignTUIndex = *Index - NumLocalTUs;
685 if (ForeignTUIndex >= NameIdx->getForeignTUCount())
686 return std::nullopt; // Invalid foreign TU index.
687 return NameIdx->getForeignTUSignature(TU: ForeignTUIndex);
688}
689
690std::optional<uint64_t> DWARFDebugNames::Entry::getTUIndex() const {
691 if (std::optional<DWARFFormValue> Off = lookup(Index: dwarf::DW_IDX_type_unit))
692 return Off->getAsUnsignedConstant();
693 return std::nullopt;
694}
695
696Expected<std::optional<DWARFDebugNames::Entry>>
697DWARFDebugNames::Entry::getParentDIEEntry() const {
698 // The offset of the accelerator table entry for the parent.
699 std::optional<DWARFFormValue> ParentEntryOff = lookup(Index: dwarf::DW_IDX_parent);
700 assert(ParentEntryOff.has_value() && "hasParentInformation() must be called");
701
702 if (ParentEntryOff->getForm() == dwarf::Form::DW_FORM_flag_present)
703 return std::nullopt;
704 return NameIdx->getEntryAtRelativeOffset(Offset: ParentEntryOff->getRawUValue());
705}
706
707void DWARFDebugNames::Entry::dumpParentIdx(
708 ScopedPrinter &W, const DWARFFormValue &FormValue) const {
709 Expected<std::optional<Entry>> ParentEntry = getParentDIEEntry();
710 if (!ParentEntry) {
711 W.getOStream() << "<invalid offset data>";
712 consumeError(Err: ParentEntry.takeError());
713 return;
714 }
715
716 if (!ParentEntry->has_value()) {
717 W.getOStream() << "<parent not indexed>";
718 return;
719 }
720
721 auto AbsoluteOffset = NameIdx->Offsets.EntriesBase + FormValue.getRawUValue();
722 W.getOStream() << "Entry @ 0x" + Twine::utohexstr(Val: AbsoluteOffset);
723}
724
725void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const {
726 W.startLine() << formatv(Fmt: "Abbrev: {0:x}\n", Vals: Abbr->Code);
727 W.startLine() << formatv(Fmt: "Tag: {0}\n", Vals: Abbr->Tag);
728 assert(Abbr->Attributes.size() == Values.size());
729 for (auto Tuple : zip_first(t: Abbr->Attributes, u: Values)) {
730 auto Index = std::get<0>(t&: Tuple).Index;
731 W.startLine() << formatv(Fmt: "{0}: ", Vals&: Index);
732
733 auto FormValue = std::get<1>(t&: Tuple);
734 if (Index == dwarf::Index::DW_IDX_parent)
735 dumpParentIdx(W, FormValue);
736 else
737 FormValue.dump(OS&: W.getOStream());
738 W.getOStream() << '\n';
739 }
740}
741
742char DWARFDebugNames::SentinelError::ID;
743std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const {
744 return inconvertibleErrorCode();
745}
746
747uint64_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const {
748 assert(CU < Hdr.CompUnitCount);
749 const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Format: Hdr.Format);
750 uint64_t Offset = Offsets.CUsBase + SectionOffsetSize * CU;
751 return Section.AccelSection.getRelocatedValue(Size: SectionOffsetSize, Off: &Offset);
752}
753
754uint64_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const {
755 assert(TU < Hdr.LocalTypeUnitCount);
756 const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Format: Hdr.Format);
757 uint64_t Offset =
758 Offsets.CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
759 return Section.AccelSection.getRelocatedValue(Size: SectionOffsetSize, Off: &Offset);
760}
761
762uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const {
763 assert(TU < Hdr.ForeignTypeUnitCount);
764 const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Format: Hdr.Format);
765 uint64_t Offset =
766 Offsets.CUsBase +
767 SectionOffsetSize * (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) + 8 * TU;
768 return Section.AccelSection.getU64(offset_ptr: &Offset);
769}
770
771Expected<DWARFDebugNames::Entry>
772DWARFDebugNames::NameIndex::getEntry(uint64_t *Offset) const {
773 const DWARFDataExtractor &AS = Section.AccelSection;
774 if (!AS.isValidOffset(offset: *Offset))
775 return createStringError(EC: errc::illegal_byte_sequence,
776 S: "Incorrectly terminated entry list.");
777
778 uint32_t AbbrevCode = AS.getULEB128(offset_ptr: Offset);
779 if (AbbrevCode == 0)
780 return make_error<SentinelError>();
781
782 const auto AbbrevIt = Abbrevs.find_as(Val: AbbrevCode);
783 if (AbbrevIt == Abbrevs.end())
784 return createStringError(EC: errc::invalid_argument, S: "Invalid abbreviation.");
785
786 Entry E(*this, *AbbrevIt);
787
788 dwarf::FormParams FormParams = {.Version: Hdr.Version, .AddrSize: 0, .Format: Hdr.Format};
789 for (auto &Value : E.Values) {
790 if (!Value.extractValue(Data: AS, OffsetPtr: Offset, FormParams))
791 return createStringError(EC: errc::io_error,
792 S: "Error extracting index attribute values.");
793 }
794 return std::move(E);
795}
796
797DWARFDebugNames::NameTableEntry
798DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const {
799 assert(0 < Index && Index <= Hdr.NameCount);
800 const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Format: Hdr.Format);
801 uint64_t StringOffsetOffset =
802 Offsets.StringOffsetsBase + SectionOffsetSize * (Index - 1);
803 uint64_t EntryOffsetOffset =
804 Offsets.EntryOffsetsBase + SectionOffsetSize * (Index - 1);
805 const DWARFDataExtractor &AS = Section.AccelSection;
806
807 uint64_t StringOffset =
808 AS.getRelocatedValue(Size: SectionOffsetSize, Off: &StringOffsetOffset);
809 uint64_t EntryOffset = AS.getUnsigned(offset_ptr: &EntryOffsetOffset, byte_size: SectionOffsetSize);
810 EntryOffset += Offsets.EntriesBase;
811 return {Section.StringSection, Index, StringOffset, EntryOffset};
812}
813
814uint32_t
815DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const {
816 assert(Bucket < Hdr.BucketCount);
817 uint64_t BucketOffset = Offsets.BucketsBase + 4 * Bucket;
818 return Section.AccelSection.getU32(offset_ptr: &BucketOffset);
819}
820
821uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const {
822 assert(0 < Index && Index <= Hdr.NameCount);
823 uint64_t HashOffset = Offsets.HashesBase + 4 * (Index - 1);
824 return Section.AccelSection.getU32(offset_ptr: &HashOffset);
825}
826
827// Returns true if we should continue scanning for entries, false if this is the
828// last (sentinel) entry). In case of a parsing error we also return false, as
829// it's not possible to recover this entry list (but the other lists may still
830// parse OK).
831bool DWARFDebugNames::NameIndex::dumpEntry(ScopedPrinter &W,
832 uint64_t *Offset) const {
833 uint64_t EntryId = *Offset;
834 auto EntryOr = getEntry(Offset);
835 if (!EntryOr) {
836 handleAllErrors(E: EntryOr.takeError(), Handlers: [](const SentinelError &) {},
837 Handlers: [&W](const ErrorInfoBase &EI) { EI.log(OS&: W.startLine()); });
838 return false;
839 }
840
841 DictScope EntryScope(W, ("Entry @ 0x" + Twine::utohexstr(Val: EntryId)).str());
842 EntryOr->dump(W);
843 return true;
844}
845
846void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W,
847 const NameTableEntry &NTE,
848 std::optional<uint32_t> Hash) const {
849 DictScope NameScope(W, ("Name " + Twine(NTE.getIndex())).str());
850 if (Hash)
851 W.printHex(Label: "Hash", Value: *Hash);
852
853 W.startLine() << formatv(Fmt: "String: {0:x8}", Vals: NTE.getStringOffset());
854 W.getOStream() << " \"" << NTE.getString() << "\"\n";
855
856 uint64_t EntryOffset = NTE.getEntryOffset();
857 while (dumpEntry(W, Offset: &EntryOffset))
858 /*empty*/;
859}
860
861void DWARFDebugNames::NameIndex::dumpCUs(ScopedPrinter &W) const {
862 ListScope CUScope(W, "Compilation Unit offsets");
863 for (uint32_t CU = 0; CU < Hdr.CompUnitCount; ++CU)
864 W.startLine() << formatv(Fmt: "CU[{0}]: {1:x8}\n", Vals&: CU, Vals: getCUOffset(CU));
865}
866
867void DWARFDebugNames::NameIndex::dumpLocalTUs(ScopedPrinter &W) const {
868 if (Hdr.LocalTypeUnitCount == 0)
869 return;
870
871 ListScope TUScope(W, "Local Type Unit offsets");
872 for (uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU)
873 W.startLine() << formatv(Fmt: "LocalTU[{0}]: {1:x8}\n", Vals&: TU,
874 Vals: getLocalTUOffset(TU));
875}
876
877void DWARFDebugNames::NameIndex::dumpForeignTUs(ScopedPrinter &W) const {
878 if (Hdr.ForeignTypeUnitCount == 0)
879 return;
880
881 ListScope TUScope(W, "Foreign Type Unit signatures");
882 for (uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) {
883 W.startLine() << formatv(Fmt: "ForeignTU[{0}]: {1:x16}\n", Vals&: TU,
884 Vals: getForeignTUSignature(TU));
885 }
886}
887
888void DWARFDebugNames::NameIndex::dumpAbbreviations(ScopedPrinter &W) const {
889 ListScope AbbrevsScope(W, "Abbreviations");
890 std::vector<const Abbrev *> AbbrevsVect;
891 for (const DWARFDebugNames::Abbrev &Abbr : Abbrevs)
892 AbbrevsVect.push_back(x: &Abbr);
893 llvm::sort(C&: AbbrevsVect, Comp: [](const Abbrev *LHS, const Abbrev *RHS) {
894 return LHS->AbbrevOffset < RHS->AbbrevOffset;
895 });
896 for (const DWARFDebugNames::Abbrev *Abbr : AbbrevsVect)
897 Abbr->dump(W);
898}
899
900void DWARFDebugNames::NameIndex::dumpBucket(ScopedPrinter &W,
901 uint32_t Bucket) const {
902 ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str());
903 uint32_t Index = getBucketArrayEntry(Bucket);
904 if (Index == 0) {
905 W.printString(Value: "EMPTY");
906 return;
907 }
908 if (Index > Hdr.NameCount) {
909 W.printString(Value: "Name index is invalid");
910 return;
911 }
912
913 for (; Index <= Hdr.NameCount; ++Index) {
914 uint32_t Hash = getHashArrayEntry(Index);
915 if (Hash % Hdr.BucketCount != Bucket)
916 break;
917
918 dumpName(W, NTE: getNameTableEntry(Index), Hash);
919 }
920}
921
922LLVM_DUMP_METHOD void DWARFDebugNames::NameIndex::dump(ScopedPrinter &W) const {
923 DictScope UnitScope(W, ("Name Index @ 0x" + Twine::utohexstr(Val: Base)).str());
924 Hdr.dump(W);
925 dumpCUs(W);
926 dumpLocalTUs(W);
927 dumpForeignTUs(W);
928 dumpAbbreviations(W);
929
930 if (Hdr.BucketCount > 0) {
931 for (uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket)
932 dumpBucket(W, Bucket);
933 return;
934 }
935
936 W.startLine() << "Hash table not present\n";
937 for (const NameTableEntry &NTE : *this)
938 dumpName(W, NTE, Hash: std::nullopt);
939}
940
941Error DWARFDebugNames::extract() {
942 uint64_t Offset = 0;
943 while (AccelSection.isValidOffset(offset: Offset)) {
944 NameIndex Next(*this, Offset);
945 if (Error E = Next.extract())
946 return E;
947 Offset = Next.getNextUnitOffset();
948 NameIndices.push_back(Elt: std::move(Next));
949 }
950 return Error::success();
951}
952
953iterator_range<DWARFDebugNames::ValueIterator>
954DWARFDebugNames::NameIndex::equal_range(StringRef Key) const {
955 return make_range(x: ValueIterator(*this, Key), y: ValueIterator());
956}
957
958LLVM_DUMP_METHOD void DWARFDebugNames::dump(raw_ostream &OS) const {
959 ScopedPrinter W(OS);
960 for (const NameIndex &NI : NameIndices)
961 NI.dump(W);
962}
963
964std::optional<uint64_t>
965DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() {
966 const Header &Hdr = CurrentIndex->Hdr;
967 if (Hdr.BucketCount == 0) {
968 // No Hash Table, We need to search through all names in the Name Index.
969 for (const NameTableEntry &NTE : *CurrentIndex) {
970 if (NTE.sameNameAs(Target: Key))
971 return NTE.getEntryOffset();
972 }
973 return std::nullopt;
974 }
975
976 // The Name Index has a Hash Table, so use that to speed up the search.
977 // Compute the Key Hash, if it has not been done already.
978 if (!Hash)
979 Hash = caseFoldingDjbHash(Buffer: Key);
980 uint32_t Bucket = *Hash % Hdr.BucketCount;
981 uint32_t Index = CurrentIndex->getBucketArrayEntry(Bucket);
982 if (Index == 0)
983 return std::nullopt; // Empty bucket
984
985 for (; Index <= Hdr.NameCount; ++Index) {
986 uint32_t HashAtIndex = CurrentIndex->getHashArrayEntry(Index);
987 if (HashAtIndex % Hdr.BucketCount != Bucket)
988 return std::nullopt; // End of bucket
989 // Only compare names if the hashes match.
990 if (HashAtIndex != Hash)
991 continue;
992
993 NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index);
994 if (NTE.sameNameAs(Target: Key))
995 return NTE.getEntryOffset();
996 }
997 return std::nullopt;
998}
999
1000bool DWARFDebugNames::ValueIterator::getEntryAtCurrentOffset() {
1001 auto EntryOr = CurrentIndex->getEntry(Offset: &DataOffset);
1002 if (!EntryOr) {
1003 consumeError(Err: EntryOr.takeError());
1004 return false;
1005 }
1006 CurrentEntry = std::move(*EntryOr);
1007 return true;
1008}
1009
1010bool DWARFDebugNames::ValueIterator::findInCurrentIndex() {
1011 std::optional<uint64_t> Offset = findEntryOffsetInCurrentIndex();
1012 if (!Offset)
1013 return false;
1014 DataOffset = *Offset;
1015 return getEntryAtCurrentOffset();
1016}
1017
1018void DWARFDebugNames::ValueIterator::searchFromStartOfCurrentIndex() {
1019 for (const NameIndex *End = CurrentIndex->Section.NameIndices.end();
1020 CurrentIndex != End; ++CurrentIndex) {
1021 if (findInCurrentIndex())
1022 return;
1023 }
1024 setEnd();
1025}
1026
1027void DWARFDebugNames::ValueIterator::next() {
1028 assert(CurrentIndex && "Incrementing an end() iterator?");
1029
1030 // First try the next entry in the current Index.
1031 if (getEntryAtCurrentOffset())
1032 return;
1033
1034 // If we're a local iterator or we have reached the last Index, we're done.
1035 if (IsLocal || CurrentIndex == &CurrentIndex->Section.NameIndices.back()) {
1036 setEnd();
1037 return;
1038 }
1039
1040 // Otherwise, try the next index.
1041 ++CurrentIndex;
1042 searchFromStartOfCurrentIndex();
1043}
1044
1045DWARFDebugNames::ValueIterator::ValueIterator(const DWARFDebugNames &AccelTable,
1046 StringRef Key)
1047 : CurrentIndex(AccelTable.NameIndices.begin()), IsLocal(false),
1048 Key(std::string(Key)) {
1049 searchFromStartOfCurrentIndex();
1050}
1051
1052DWARFDebugNames::ValueIterator::ValueIterator(
1053 const DWARFDebugNames::NameIndex &NI, StringRef Key)
1054 : CurrentIndex(&NI), IsLocal(true), Key(std::string(Key)) {
1055 if (!findInCurrentIndex())
1056 setEnd();
1057}
1058
1059iterator_range<DWARFDebugNames::ValueIterator>
1060DWARFDebugNames::equal_range(StringRef Key) const {
1061 if (NameIndices.empty())
1062 return make_range(x: ValueIterator(), y: ValueIterator());
1063 return make_range(x: ValueIterator(*this, Key), y: ValueIterator());
1064}
1065
1066const DWARFDebugNames::NameIndex *
1067DWARFDebugNames::getCUOrTUNameIndex(uint64_t UnitOffset) {
1068 if (UnitOffsetToNameIndex.size() == 0 && NameIndices.size() > 0) {
1069 for (const auto &NI : *this) {
1070 for (uint32_t CU = 0; CU < NI.getCUCount(); ++CU)
1071 UnitOffsetToNameIndex.try_emplace(Key: NI.getCUOffset(CU), Args: &NI);
1072 for (uint32_t TU = 0; TU < NI.getLocalTUCount(); ++TU)
1073 UnitOffsetToNameIndex.try_emplace(Key: NI.getLocalTUOffset(TU), Args: &NI);
1074 }
1075 }
1076 return UnitOffsetToNameIndex.lookup(Val: UnitOffset);
1077}
1078
1079static bool isObjCSelector(StringRef Name) {
1080 return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
1081 (Name[1] == '[');
1082}
1083
1084std::optional<ObjCSelectorNames> llvm::getObjCNamesIfSelector(StringRef Name) {
1085 if (!isObjCSelector(Name))
1086 return std::nullopt;
1087 // "-[Atom setMass:]"
1088 StringRef ClassNameStart(Name.drop_front(N: 2));
1089 size_t FirstSpace = ClassNameStart.find(C: ' ');
1090 if (FirstSpace == StringRef::npos)
1091 return std::nullopt;
1092
1093 StringRef SelectorStart = ClassNameStart.drop_front(N: FirstSpace + 1);
1094 if (!SelectorStart.size())
1095 return std::nullopt;
1096
1097 ObjCSelectorNames Ans;
1098 Ans.ClassName = ClassNameStart.take_front(N: FirstSpace);
1099 Ans.Selector = SelectorStart.drop_back(); // drop ']';
1100
1101 // "-[Class(Category) selector :withArg ...]"
1102 if (Ans.ClassName.back() == ')') {
1103 size_t OpenParens = Ans.ClassName.find(C: '(');
1104 if (OpenParens != StringRef::npos) {
1105 Ans.ClassNameNoCategory = Ans.ClassName.take_front(N: OpenParens);
1106
1107 Ans.MethodNameNoCategory = Name.take_front(N: OpenParens + 2);
1108 // FIXME: The missing space here may be a bug, but dsymutil-classic also
1109 // does it this way.
1110 append_range(C&: *Ans.MethodNameNoCategory, R&: SelectorStart);
1111 }
1112 }
1113 return Ans;
1114}
1115
1116std::optional<StringRef> llvm::StripTemplateParameters(StringRef Name) {
1117 // We are looking for template parameters to strip from Name. e.g.
1118 //
1119 // operator<<B>
1120 //
1121 // We look for > at the end but if it does not contain any < then we
1122 // have something like operator>>. We check for the operator<=> case.
1123 if (!Name.ends_with(Suffix: ">") || Name.count(Str: "<") == 0 || Name.ends_with(Suffix: "<=>"))
1124 return {};
1125
1126 // How many < until we have the start of the template parameters.
1127 size_t NumLeftAnglesToSkip = 1;
1128
1129 // If we have operator<=> then we need to skip its < as well.
1130 NumLeftAnglesToSkip += Name.count(Str: "<=>");
1131
1132 size_t RightAngleCount = Name.count(C: '>');
1133 size_t LeftAngleCount = Name.count(C: '<');
1134
1135 // If we have more < than > we have operator< or operator<<
1136 // we to account for their < as well.
1137 if (LeftAngleCount > RightAngleCount)
1138 NumLeftAnglesToSkip += LeftAngleCount - RightAngleCount;
1139
1140 size_t StartOfTemplate = 0;
1141 while (NumLeftAnglesToSkip--)
1142 StartOfTemplate = Name.find(C: '<', From: StartOfTemplate) + 1;
1143
1144 StringRef Result = Name.substr(Start: 0, N: StartOfTemplate - 1);
1145 if (Result.empty())
1146 return std::nullopt;
1147 return Result;
1148}
1149