1//===- llvm/CodeGen/AsmPrinter/AccelTable.cpp - Accelerator Tables --------===//
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// This file contains support for writing accelerator tables.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/AccelTable.h"
14#include "DwarfCompileUnit.h"
15#include "DwarfUnit.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/BinaryFormat/Dwarf.h"
20#include "llvm/CodeGen/AsmPrinter.h"
21#include "llvm/CodeGen/DIE.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSymbol.h"
24#include "llvm/Support/LEB128.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/TargetLoweringObjectFile.h"
27#include <cstddef>
28#include <cstdint>
29#include <limits>
30#include <vector>
31
32using namespace llvm;
33
34void AccelTableBase::computeBucketCount() {
35 SmallVector<uint32_t, 0> Uniques;
36 Uniques.reserve(N: Entries.size());
37 for (const auto &E : Entries)
38 Uniques.push_back(Elt: E.second.HashValue);
39 llvm::sort(C&: Uniques);
40 UniqueHashCount = llvm::unique(R&: Uniques) - Uniques.begin();
41 BucketCount = dwarf::getDebugNamesBucketCount(UniqueHashCount);
42}
43
44void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) {
45 // Create the individual hash data outputs.
46 for (auto &E : Entries) {
47 // Unique the entries.
48 llvm::stable_sort(Range&: E.second.Values,
49 C: [](const AccelTableData *A, const AccelTableData *B) {
50 return *A < *B;
51 });
52 E.second.Values.erase(first: llvm::unique(R&: E.second.Values), last: E.second.Values.end());
53 }
54
55 // Figure out how many buckets we need, then compute the bucket contents and
56 // the final ordering. The hashes and offsets can be emitted by walking these
57 // data structures. We add temporary symbols to the data so they can be
58 // referenced when emitting the offsets.
59 computeBucketCount();
60
61 // Compute bucket contents and final ordering.
62 Buckets.resize(new_size: BucketCount);
63 for (auto &E : Entries) {
64 uint32_t Bucket = E.second.HashValue % BucketCount;
65 Buckets[Bucket].push_back(x: &E.second);
66 E.second.Sym = Asm->createTempSymbol(Name: Prefix);
67 }
68
69 // Sort the contents of the buckets by hash value so that hash collisions end
70 // up together. Stable sort makes testing easier and doesn't cost much more.
71 for (auto &Bucket : Buckets)
72 llvm::stable_sort(Range&: Bucket, C: [](HashData *LHS, HashData *RHS) {
73 return LHS->HashValue < RHS->HashValue;
74 });
75}
76
77namespace {
78/// Base class for writing out Accelerator tables. It holds the common
79/// functionality for the two Accelerator table types.
80class AccelTableWriter {
81protected:
82 AsmPrinter *const Asm; ///< Destination.
83 const AccelTableBase &Contents; ///< Data to emit.
84
85 /// Controls whether to emit duplicate hash and offset table entries for names
86 /// with identical hashes. Apple tables don't emit duplicate entries, DWARF v5
87 /// tables do.
88 const bool SkipIdenticalHashes;
89
90 void emitHashes() const;
91
92 /// Emit offsets to lists of entries with identical names. The offsets are
93 /// relative to the Base argument.
94 void emitOffsets(const MCSymbol *Base) const;
95
96public:
97 AccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
98 bool SkipIdenticalHashes)
99 : Asm(Asm), Contents(Contents), SkipIdenticalHashes(SkipIdenticalHashes) {
100 }
101};
102
103class AppleAccelTableWriter : public AccelTableWriter {
104 using Atom = AppleAccelTableData::Atom;
105
106 /// The fixed header of an Apple Accelerator Table.
107 struct Header {
108 uint32_t Magic = MagicHash;
109 uint16_t Version = 1;
110 uint16_t HashFunction = dwarf::DW_hash_function_djb;
111 uint32_t BucketCount;
112 uint32_t HashCount;
113 uint32_t HeaderDataLength;
114
115 /// 'HASH' magic value to detect endianness.
116 static const uint32_t MagicHash = 0x48415348;
117
118 Header(uint32_t BucketCount, uint32_t UniqueHashCount, uint32_t DataLength)
119 : BucketCount(BucketCount), HashCount(UniqueHashCount),
120 HeaderDataLength(DataLength) {}
121
122 void emit(AsmPrinter *Asm) const;
123#ifndef NDEBUG
124 void print(raw_ostream &OS) const;
125 void dump() const { print(dbgs()); }
126#endif
127 };
128
129 /// The HeaderData describes the structure of an Apple accelerator table
130 /// through a list of Atoms.
131 struct HeaderData {
132 /// In the case of data that is referenced via DW_FORM_ref_* the offset
133 /// base is used to describe the offset for all forms in the list of atoms.
134 uint32_t DieOffsetBase;
135
136 const SmallVector<Atom, 4> Atoms;
137
138 HeaderData(ArrayRef<Atom> AtomList, uint32_t Offset = 0)
139 : DieOffsetBase(Offset), Atoms(AtomList) {}
140
141 void emit(AsmPrinter *Asm) const;
142#ifndef NDEBUG
143 void print(raw_ostream &OS) const;
144 void dump() const { print(dbgs()); }
145#endif
146 };
147
148 Header Header;
149 HeaderData HeaderData;
150 const MCSymbol *SecBegin;
151
152 void emitBuckets() const;
153 void emitData() const;
154
155public:
156 AppleAccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
157 ArrayRef<Atom> Atoms, const MCSymbol *SecBegin)
158 : AccelTableWriter(Asm, Contents, true),
159 Header(Contents.getBucketCount(), Contents.getUniqueHashCount(),
160 8 + (Atoms.size() * 4)),
161 HeaderData(Atoms), SecBegin(SecBegin) {}
162
163 void emit() const;
164
165#ifndef NDEBUG
166 void print(raw_ostream &OS) const;
167 void dump() const { print(dbgs()); }
168#endif
169};
170
171/// Class responsible for emitting a DWARF v5 Accelerator Table. The only
172/// public function is emit(), which performs the actual emission.
173///
174/// A callback abstracts the logic to provide a CU index for a given entry.
175class Dwarf5AccelTableWriter : public AccelTableWriter {
176 struct Header {
177 uint16_t Version = 5;
178 uint16_t Padding = 0;
179 uint32_t CompUnitCount;
180 uint32_t LocalTypeUnitCount = 0;
181 uint32_t ForeignTypeUnitCount = 0;
182 uint32_t BucketCount = 0;
183 uint32_t NameCount = 0;
184 uint32_t AbbrevTableSize = 0;
185 uint32_t AugmentationStringSize = sizeof(AugmentationString);
186 char AugmentationString[8] = {'L', 'L', 'V', 'M', '0', '7', '0', '0'};
187
188 Header(uint32_t CompUnitCount, uint32_t LocalTypeUnitCount,
189 uint32_t ForeignTypeUnitCount, uint32_t BucketCount,
190 uint32_t NameCount)
191 : CompUnitCount(CompUnitCount), LocalTypeUnitCount(LocalTypeUnitCount),
192 ForeignTypeUnitCount(ForeignTypeUnitCount), BucketCount(BucketCount),
193 NameCount(NameCount) {}
194
195 void emit(Dwarf5AccelTableWriter &Ctx);
196 };
197
198 Header Header;
199 /// FoldingSet that uniques the abbreviations.
200 FoldingSet<DebugNamesAbbrev> AbbreviationsSet;
201 /// Vector containing DebugNames abbreviations for iteration in order.
202 SmallVector<DebugNamesAbbrev *, 5> AbbreviationsVector;
203 /// The bump allocator to use when creating DIEAbbrev objects in the uniqued
204 /// storage container.
205 BumpPtrAllocator Alloc;
206 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits;
207 ArrayRef<std::variant<MCSymbol *, uint64_t>> TypeUnits;
208 llvm::function_ref<std::optional<DWARF5AccelTable::UnitIndexAndEncoding>(
209 const DWARF5AccelTableData &)>
210 getIndexForEntry;
211 MCSymbol *ContributionEnd = nullptr;
212 MCSymbol *AbbrevStart = Asm->createTempSymbol(Name: "names_abbrev_start");
213 MCSymbol *AbbrevEnd = Asm->createTempSymbol(Name: "names_abbrev_end");
214 MCSymbol *EntryPool = Asm->createTempSymbol(Name: "names_entries");
215 // Indicates if this module is built with Split Dwarf enabled.
216 bool IsSplitDwarf = false;
217 /// Stores the DIE offsets which are indexed by this table.
218 DenseSet<OffsetAndUnitID> IndexedOffsets;
219
220 void populateAbbrevsMap();
221
222 void emitCUList() const;
223 void emitTUList() const;
224 void emitBuckets() const;
225 void emitStringOffsets() const;
226 void emitAbbrevs() const;
227 void emitEntry(
228 const DWARF5AccelTableData &Entry,
229 const DenseMap<OffsetAndUnitID, uint64_t> &DIEOffsetToAccelEntryOffset);
230 uint64_t getEntrySize(const DWARF5AccelTableData &Entry) const;
231 void emitData();
232
233public:
234 Dwarf5AccelTableWriter(
235 AsmPrinter *Asm, const AccelTableBase &Contents,
236 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits,
237 ArrayRef<std::variant<MCSymbol *, uint64_t>> TypeUnits,
238 llvm::function_ref<std::optional<DWARF5AccelTable::UnitIndexAndEncoding>(
239 const DWARF5AccelTableData &)>
240 getIndexForEntry,
241 bool IsSplitDwarf);
242 ~Dwarf5AccelTableWriter() {
243 for (DebugNamesAbbrev *Abbrev : AbbreviationsVector)
244 Abbrev->~DebugNamesAbbrev();
245 }
246 void emit();
247};
248} // namespace
249
250void AccelTableWriter::emitHashes() const {
251 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
252 unsigned BucketIdx = 0;
253 for (const auto &Bucket : Contents.getBuckets()) {
254 for (const auto &Hash : Bucket) {
255 uint32_t HashValue = Hash->HashValue;
256 if (SkipIdenticalHashes && PrevHash == HashValue)
257 continue;
258 Asm->OutStreamer->AddComment(T: "Hash in Bucket " + Twine(BucketIdx));
259 Asm->emitInt32(Value: HashValue);
260 PrevHash = HashValue;
261 }
262 BucketIdx++;
263 }
264}
265
266void AccelTableWriter::emitOffsets(const MCSymbol *Base) const {
267 const auto &Buckets = Contents.getBuckets();
268 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
269 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
270 for (auto *Hash : Buckets[i]) {
271 uint32_t HashValue = Hash->HashValue;
272 if (SkipIdenticalHashes && PrevHash == HashValue)
273 continue;
274 PrevHash = HashValue;
275 Asm->OutStreamer->AddComment(T: "Offset in Bucket " + Twine(i));
276 Asm->emitLabelDifference(Hi: Hash->Sym, Lo: Base, Size: Asm->getDwarfOffsetByteSize());
277 }
278 }
279}
280
281void AppleAccelTableWriter::Header::emit(AsmPrinter *Asm) const {
282 Asm->OutStreamer->AddComment(T: "Header Magic");
283 Asm->emitInt32(Value: Magic);
284 Asm->OutStreamer->AddComment(T: "Header Version");
285 Asm->emitInt16(Value: Version);
286 Asm->OutStreamer->AddComment(T: "Header Hash Function");
287 Asm->emitInt16(Value: HashFunction);
288 Asm->OutStreamer->AddComment(T: "Header Bucket Count");
289 Asm->emitInt32(Value: BucketCount);
290 Asm->OutStreamer->AddComment(T: "Header Hash Count");
291 Asm->emitInt32(Value: HashCount);
292 Asm->OutStreamer->AddComment(T: "Header Data Length");
293 Asm->emitInt32(Value: HeaderDataLength);
294}
295
296void AppleAccelTableWriter::HeaderData::emit(AsmPrinter *Asm) const {
297 Asm->OutStreamer->AddComment(T: "HeaderData Die Offset Base");
298 Asm->emitInt32(Value: DieOffsetBase);
299 Asm->OutStreamer->AddComment(T: "HeaderData Atom Count");
300 Asm->emitInt32(Value: Atoms.size());
301
302 for (const Atom &A : Atoms) {
303 Asm->OutStreamer->AddComment(T: dwarf::AtomTypeString(Atom: A.Type));
304 Asm->emitInt16(Value: A.Type);
305 Asm->OutStreamer->AddComment(T: dwarf::FormEncodingString(Encoding: A.Form));
306 Asm->emitInt16(Value: A.Form);
307 }
308}
309
310void AppleAccelTableWriter::emitBuckets() const {
311 const auto &Buckets = Contents.getBuckets();
312 unsigned index = 0;
313 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
314 Asm->OutStreamer->AddComment(T: "Bucket " + Twine(i));
315 if (!Buckets[i].empty())
316 Asm->emitInt32(Value: index);
317 else
318 Asm->emitInt32(Value: std::numeric_limits<uint32_t>::max());
319 // Buckets point in the list of hashes, not to the data. Do not increment
320 // the index multiple times in case of hash collisions.
321 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
322 for (auto *HD : Buckets[i]) {
323 uint32_t HashValue = HD->HashValue;
324 if (PrevHash != HashValue)
325 ++index;
326 PrevHash = HashValue;
327 }
328 }
329}
330
331void AppleAccelTableWriter::emitData() const {
332 const auto &Buckets = Contents.getBuckets();
333 for (const AccelTableBase::HashList &Bucket : Buckets) {
334 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
335 for (const auto &Hash : Bucket) {
336 // Terminate the previous entry if there is no hash collision with the
337 // current one.
338 if (PrevHash != std::numeric_limits<uint64_t>::max() &&
339 PrevHash != Hash->HashValue)
340 Asm->emitInt32(Value: 0);
341 // Remember to emit the label for our offset.
342 Asm->OutStreamer->emitLabel(Symbol: Hash->Sym);
343 Asm->OutStreamer->AddComment(T: Hash->Name.getString());
344 Asm->emitDwarfStringOffset(S: Hash->Name);
345 Asm->OutStreamer->AddComment(T: "Num DIEs");
346 Asm->emitInt32(Value: Hash->Values.size());
347 for (const auto *V : Hash->getValues<const AppleAccelTableData *>())
348 V->emit(Asm);
349 PrevHash = Hash->HashValue;
350 }
351 // Emit the final end marker for the bucket.
352 if (!Bucket.empty())
353 Asm->emitInt32(Value: 0);
354 }
355}
356
357void AppleAccelTableWriter::emit() const {
358 Header.emit(Asm);
359 HeaderData.emit(Asm);
360 emitBuckets();
361 emitHashes();
362 emitOffsets(Base: SecBegin);
363 emitData();
364}
365
366DWARF5AccelTableData::DWARF5AccelTableData(const DIE &Die,
367 const uint32_t UnitID,
368 const bool IsTU)
369 : OffsetVal(&Die), DieTag(Die.getTag()), AbbrevNumber(0), IsTU(IsTU),
370 UnitID(UnitID) {}
371
372void Dwarf5AccelTableWriter::Header::emit(Dwarf5AccelTableWriter &Ctx) {
373 assert(CompUnitCount > 0 && "Index must have at least one CU.");
374
375 AsmPrinter *Asm = Ctx.Asm;
376 Ctx.ContributionEnd =
377 Asm->emitDwarfUnitLength(Prefix: "names", Comment: "Header: unit length");
378 Asm->OutStreamer->AddComment(T: "Header: version");
379 Asm->emitInt16(Value: Version);
380 Asm->OutStreamer->AddComment(T: "Header: padding");
381 Asm->emitInt16(Value: Padding);
382 Asm->OutStreamer->AddComment(T: "Header: compilation unit count");
383 Asm->emitInt32(Value: CompUnitCount);
384 Asm->OutStreamer->AddComment(T: "Header: local type unit count");
385 Asm->emitInt32(Value: LocalTypeUnitCount);
386 Asm->OutStreamer->AddComment(T: "Header: foreign type unit count");
387 Asm->emitInt32(Value: ForeignTypeUnitCount);
388 Asm->OutStreamer->AddComment(T: "Header: bucket count");
389 Asm->emitInt32(Value: BucketCount);
390 Asm->OutStreamer->AddComment(T: "Header: name count");
391 Asm->emitInt32(Value: NameCount);
392 Asm->OutStreamer->AddComment(T: "Header: abbreviation table size");
393 Asm->emitLabelDifference(Hi: Ctx.AbbrevEnd, Lo: Ctx.AbbrevStart, Size: sizeof(uint32_t));
394 Asm->OutStreamer->AddComment(T: "Header: augmentation string size");
395 assert(AugmentationStringSize % 4 == 0);
396 Asm->emitInt32(Value: AugmentationStringSize);
397 Asm->OutStreamer->AddComment(T: "Header: augmentation string");
398 Asm->OutStreamer->emitBytes(Data: {AugmentationString, AugmentationStringSize});
399}
400
401std::optional<uint64_t>
402DWARF5AccelTableData::getDefiningParentDieOffset(const DIE &Die) {
403 if (auto *Parent = Die.getParent();
404 Parent && !Parent->findAttribute(Attribute: dwarf::Attribute::DW_AT_declaration))
405 return Parent->getOffset();
406 return {};
407}
408
409static std::optional<dwarf::Form>
410getFormForIdxParent(const DenseSet<OffsetAndUnitID> &IndexedOffsets,
411 std::optional<OffsetAndUnitID> ParentOffset) {
412 // No parent information
413 if (!ParentOffset)
414 return std::nullopt;
415 // Parent is indexed by this table.
416 if (IndexedOffsets.contains(V: *ParentOffset))
417 return dwarf::Form::DW_FORM_ref4;
418 // Parent is not indexed by this table.
419 return dwarf::Form::DW_FORM_flag_present;
420}
421
422void DebugNamesAbbrev::Profile(FoldingSetNodeID &ID) const {
423 ID.AddInteger(I: DieTag);
424 for (const DebugNamesAbbrev::AttributeEncoding &Enc : AttrVect) {
425 ID.AddInteger(I: Enc.Index);
426 ID.AddInteger(I: Enc.Form);
427 }
428}
429
430void Dwarf5AccelTableWriter::populateAbbrevsMap() {
431 for (auto &Bucket : Contents.getBuckets()) {
432 for (auto *Hash : Bucket) {
433 for (auto *Value : Hash->getValues<DWARF5AccelTableData *>()) {
434 std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
435 getIndexForEntry(*Value);
436 std::optional<dwarf::Form> MaybeParentForm = getFormForIdxParent(
437 IndexedOffsets, ParentOffset: Value->getParentDieOffsetAndUnitID());
438 DebugNamesAbbrev Abbrev(Value->getDieTag());
439 if (EntryRet)
440 Abbrev.addAttribute(Attr: EntryRet->Encoding);
441 Abbrev.addAttribute(Attr: {.Index: dwarf::DW_IDX_die_offset, .Form: dwarf::DW_FORM_ref4});
442 if (MaybeParentForm)
443 Abbrev.addAttribute(Attr: {.Index: dwarf::DW_IDX_parent, .Form: *MaybeParentForm});
444 FoldingSetNodeID ID;
445 Abbrev.Profile(ID);
446 void *InsertPos;
447 if (DebugNamesAbbrev *Existing =
448 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
449 Value->setAbbrevNumber(Existing->getNumber());
450 continue;
451 }
452 DebugNamesAbbrev *NewAbbrev =
453 new (Alloc) DebugNamesAbbrev(std::move(Abbrev));
454 AbbreviationsVector.push_back(Elt: NewAbbrev);
455 NewAbbrev->setNumber(AbbreviationsVector.size());
456 AbbreviationsSet.InsertNode(N: NewAbbrev, InsertPos);
457 Value->setAbbrevNumber(NewAbbrev->getNumber());
458 }
459 }
460 }
461}
462
463void Dwarf5AccelTableWriter::emitCUList() const {
464 for (const auto &CU : enumerate(First: CompUnits)) {
465 Asm->OutStreamer->AddComment(T: "Compilation unit " + Twine(CU.index()));
466 if (std::holds_alternative<MCSymbol *>(v: CU.value()))
467 Asm->emitDwarfSymbolReference(Label: std::get<MCSymbol *>(v: CU.value()));
468 else
469 Asm->emitDwarfLengthOrOffset(Value: std::get<uint64_t>(v: CU.value()));
470 }
471}
472
473void Dwarf5AccelTableWriter::emitTUList() const {
474 for (const auto &TU : enumerate(First: TypeUnits)) {
475 Asm->OutStreamer->AddComment(T: "Type unit " + Twine(TU.index()));
476 if (std::holds_alternative<MCSymbol *>(v: TU.value()))
477 Asm->emitDwarfSymbolReference(Label: std::get<MCSymbol *>(v: TU.value()));
478 else if (IsSplitDwarf)
479 Asm->emitInt64(Value: std::get<uint64_t>(v: TU.value()));
480 else
481 Asm->emitDwarfLengthOrOffset(Value: std::get<uint64_t>(v: TU.value()));
482 }
483}
484
485void Dwarf5AccelTableWriter::emitBuckets() const {
486 uint32_t Index = 1;
487 for (const auto &Bucket : enumerate(First: Contents.getBuckets())) {
488 Asm->OutStreamer->AddComment(T: "Bucket " + Twine(Bucket.index()));
489 Asm->emitInt32(Value: Bucket.value().empty() ? 0 : Index);
490 Index += Bucket.value().size();
491 }
492}
493
494void Dwarf5AccelTableWriter::emitStringOffsets() const {
495 for (const auto &Bucket : enumerate(First: Contents.getBuckets())) {
496 for (auto *Hash : Bucket.value()) {
497 DwarfStringPoolEntryRef String = Hash->Name;
498 Asm->OutStreamer->AddComment(T: "String in Bucket " + Twine(Bucket.index()) +
499 ": " + String.getString());
500 Asm->emitDwarfStringOffset(S: String);
501 }
502 }
503}
504
505void Dwarf5AccelTableWriter::emitAbbrevs() const {
506 Asm->OutStreamer->emitLabel(Symbol: AbbrevStart);
507 for (const DebugNamesAbbrev *Abbrev : AbbreviationsVector) {
508 Asm->OutStreamer->AddComment(T: "Abbrev code");
509 Asm->emitULEB128(Value: Abbrev->getNumber());
510 Asm->OutStreamer->AddComment(T: dwarf::TagString(Tag: Abbrev->getDieTag()));
511 Asm->emitULEB128(Value: Abbrev->getDieTag());
512 for (const DebugNamesAbbrev::AttributeEncoding &AttrEnc :
513 Abbrev->getAttributes()) {
514 Asm->emitULEB128(Value: AttrEnc.Index, Desc: dwarf::IndexString(Idx: AttrEnc.Index).data());
515 Asm->emitULEB128(Value: AttrEnc.Form,
516 Desc: dwarf::FormEncodingString(Encoding: AttrEnc.Form).data());
517 }
518 Asm->emitULEB128(Value: 0, Desc: "End of abbrev");
519 Asm->emitULEB128(Value: 0, Desc: "End of abbrev");
520 }
521 Asm->emitULEB128(Value: 0, Desc: "End of abbrev list");
522 Asm->OutStreamer->emitLabel(Symbol: AbbrevEnd);
523}
524
525void Dwarf5AccelTableWriter::emitEntry(
526 const DWARF5AccelTableData &Entry,
527 const DenseMap<OffsetAndUnitID, uint64_t> &DIEOffsetToAccelEntryOffset) {
528 unsigned AbbrevIndex = Entry.getAbbrevNumber() - 1;
529 assert(AbbrevIndex < AbbreviationsVector.size() &&
530 "Entry abbrev index is outside of abbreviations vector range.");
531 DebugNamesAbbrev *Abbrev = AbbreviationsVector[AbbrevIndex];
532 std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
533 getIndexForEntry(Entry);
534 std::optional<OffsetAndUnitID> MaybeParentOffset =
535 Entry.getParentDieOffsetAndUnitID();
536
537 Asm->emitULEB128(Value: Entry.getAbbrevNumber(), Desc: "Abbreviation code");
538
539 for (const DebugNamesAbbrev::AttributeEncoding &AttrEnc :
540 Abbrev->getAttributes()) {
541 Asm->OutStreamer->AddComment(T: dwarf::IndexString(Idx: AttrEnc.Index));
542 switch (AttrEnc.Index) {
543 case dwarf::DW_IDX_compile_unit:
544 case dwarf::DW_IDX_type_unit: {
545 DIEInteger ID(EntryRet->Index);
546 ID.emitValue(Asm, Form: AttrEnc.Form);
547 break;
548 }
549 case dwarf::DW_IDX_die_offset:
550 assert(AttrEnc.Form == dwarf::DW_FORM_ref4);
551 Asm->emitInt32(Value: Entry.getDieOffset());
552 break;
553 case dwarf::DW_IDX_parent: {
554 if (AttrEnc.Form == dwarf::Form::DW_FORM_flag_present)
555 break;
556 auto It = DIEOffsetToAccelEntryOffset.find(Val: *MaybeParentOffset);
557 assert(It != DIEOffsetToAccelEntryOffset.end());
558 Asm->emitInt32(Value: It->second);
559 break;
560 }
561 default:
562 llvm_unreachable("Unexpected index attribute!");
563 }
564 }
565}
566
567uint64_t
568Dwarf5AccelTableWriter::getEntrySize(const DWARF5AccelTableData &Entry) const {
569 unsigned AbbrevIndex = Entry.getAbbrevNumber() - 1;
570 assert(AbbrevIndex < AbbreviationsVector.size());
571 DebugNamesAbbrev *Abbrev = AbbreviationsVector[AbbrevIndex];
572 uint64_t Size = getULEB128Size(Value: Entry.getAbbrevNumber());
573 std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
574 getIndexForEntry(Entry);
575 for (const auto &AttrEnc : Abbrev->getAttributes()) {
576 switch (AttrEnc.Index) {
577 case dwarf::DW_IDX_compile_unit:
578 case dwarf::DW_IDX_type_unit:
579 Size += DIEInteger(EntryRet->Index)
580 .sizeOf(FormParams: Asm->getDwarfFormParams(), Form: AttrEnc.Form);
581 break;
582 case dwarf::DW_IDX_die_offset:
583 Size += 4;
584 break;
585 case dwarf::DW_IDX_parent:
586 if (AttrEnc.Form != dwarf::Form::DW_FORM_flag_present)
587 Size += 4;
588 break;
589 default:
590 llvm_unreachable("Unexpected index attribute!");
591 }
592 }
593 return Size;
594}
595
596void Dwarf5AccelTableWriter::emitData() {
597 // Pre-compute entry pool offsets for DW_IDX_parent references.
598 DenseMap<OffsetAndUnitID, uint64_t> DIEOffsetToAccelEntryOffset;
599 uint64_t Offset = 0;
600 for (auto &Bucket : Contents.getBuckets()) {
601 for (auto *Hash : Bucket) {
602 for (const auto *Value : Hash->getValues<DWARF5AccelTableData *>()) {
603 DIEOffsetToAccelEntryOffset.try_emplace(Key: Value->getDieOffsetAndUnitID(),
604 Args&: Offset);
605 Offset += getEntrySize(Entry: *Value);
606 }
607 Offset += 1; // End of list
608 }
609 }
610
611 Asm->OutStreamer->emitLabel(Symbol: EntryPool);
612 for (auto &Bucket : Contents.getBuckets()) {
613 for (auto *Hash : Bucket) {
614 // Remember to emit the label for our offset.
615 Asm->OutStreamer->emitLabel(Symbol: Hash->Sym);
616 for (const auto *Value : Hash->getValues<DWARF5AccelTableData *>())
617 emitEntry(Entry: *Value, DIEOffsetToAccelEntryOffset);
618 Asm->OutStreamer->AddComment(T: "End of list: " + Hash->Name.getString());
619 Asm->emitInt8(Value: 0);
620 }
621 }
622}
623
624Dwarf5AccelTableWriter::Dwarf5AccelTableWriter(
625 AsmPrinter *Asm, const AccelTableBase &Contents,
626 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits,
627 ArrayRef<std::variant<MCSymbol *, uint64_t>> TypeUnits,
628 llvm::function_ref<std::optional<DWARF5AccelTable::UnitIndexAndEncoding>(
629 const DWARF5AccelTableData &)>
630 getIndexForEntry,
631 bool IsSplitDwarf)
632 : AccelTableWriter(Asm, Contents, false),
633 Header(CompUnits.size(), IsSplitDwarf ? 0 : TypeUnits.size(),
634 IsSplitDwarf ? TypeUnits.size() : 0, Contents.getBucketCount(),
635 Contents.getUniqueNameCount()),
636 CompUnits(CompUnits), TypeUnits(TypeUnits),
637 getIndexForEntry(std::move(getIndexForEntry)),
638 IsSplitDwarf(IsSplitDwarf) {
639
640 for (auto &Bucket : Contents.getBuckets())
641 for (auto *Hash : Bucket)
642 for (auto *Value : Hash->getValues<DWARF5AccelTableData *>())
643 IndexedOffsets.insert(V: Value->getDieOffsetAndUnitID());
644
645 populateAbbrevsMap();
646}
647
648void Dwarf5AccelTableWriter::emit() {
649 Header.emit(Ctx&: *this);
650 emitCUList();
651 emitTUList();
652 emitBuckets();
653 emitHashes();
654 emitStringOffsets();
655 emitOffsets(Base: EntryPool);
656 emitAbbrevs();
657 emitData();
658 Asm->OutStreamer->emitValueToAlignment(Alignment: Align(4), Fill: 0);
659 Asm->OutStreamer->emitLabel(Symbol: ContributionEnd);
660}
661
662void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
663 StringRef Prefix, const MCSymbol *SecBegin,
664 ArrayRef<AppleAccelTableData::Atom> Atoms) {
665 Contents.finalize(Asm, Prefix);
666 AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit();
667}
668
669void llvm::emitDWARF5AccelTable(
670 AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD,
671 ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
672 TUVectorTy TUSymbols = Contents.getTypeUnitsSymbols();
673 std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits;
674 std::vector<std::variant<MCSymbol *, uint64_t>> TypeUnits;
675 SmallVector<unsigned, 1> CUIndex(CUs.size());
676 DenseMap<unsigned, unsigned> TUIndex(TUSymbols.size());
677 int CUCount = 0;
678 int TUCount = 0;
679 for (const auto &CU : enumerate(First&: CUs)) {
680 switch (CU.value()->getCUNode()->getNameTableKind()) {
681 case DICompileUnit::DebugNameTableKind::Default:
682 case DICompileUnit::DebugNameTableKind::Apple:
683 break;
684 default:
685 continue;
686 }
687 CUIndex[CU.index()] = CUCount++;
688 assert(CU.index() == CU.value()->getUniqueID());
689 const DwarfCompileUnit *MainCU =
690 DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get();
691 CompUnits.push_back(x: MainCU->getLabelBegin());
692 }
693
694 for (const auto &TU : TUSymbols) {
695 TUIndex[TU.UniqueID] = TUCount++;
696 if (DD.useSplitDwarf())
697 TypeUnits.push_back(x: std::get<uint64_t>(v: TU.LabelOrSignature));
698 else
699 TypeUnits.push_back(x: std::get<MCSymbol *>(v: TU.LabelOrSignature));
700 }
701
702 if (CompUnits.empty())
703 return;
704
705 Asm->OutStreamer->switchSection(
706 Section: Asm->getObjFileLowering().getDwarfDebugNamesSection());
707
708 Contents.finalize(Asm, Prefix: "names");
709 dwarf::Form CUIndexForm =
710 DIEInteger::BestForm(/*IsSigned*/ false, Int: CompUnits.size() - 1);
711 dwarf::Form TUIndexForm =
712 DIEInteger::BestForm(/*IsSigned*/ false, Int: TypeUnits.size() - 1);
713 Dwarf5AccelTableWriter(
714 Asm, Contents, CompUnits, TypeUnits,
715 [&](const DWARF5AccelTableData &Entry)
716 -> std::optional<DWARF5AccelTable::UnitIndexAndEncoding> {
717 if (Entry.isTU())
718 return {{.Index: TUIndex[Entry.getUnitID()],
719 .Encoding: {.Index: dwarf::DW_IDX_type_unit, .Form: TUIndexForm}}};
720 if (CUIndex.size() > 1)
721 return {{.Index: CUIndex[Entry.getUnitID()],
722 .Encoding: {.Index: dwarf::DW_IDX_compile_unit, .Form: CUIndexForm}}};
723 return std::nullopt;
724 },
725 DD.useSplitDwarf())
726 .emit();
727}
728
729void DWARF5AccelTable::addTypeUnitSymbol(DwarfTypeUnit &U) {
730 TUSymbolsOrHashes.push_back(Elt: {.LabelOrSignature: U.getLabelBegin(), .UniqueID: U.getUniqueID()});
731}
732
733void DWARF5AccelTable::addTypeUnitSignature(DwarfTypeUnit &U) {
734 TUSymbolsOrHashes.push_back(Elt: {.LabelOrSignature: U.getTypeSignature(), .UniqueID: U.getUniqueID()});
735}
736
737void llvm::emitDWARF5AccelTable(
738 AsmPrinter *Asm, DWARF5AccelTable &Contents,
739 ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
740 llvm::function_ref<std::optional<DWARF5AccelTable::UnitIndexAndEncoding>(
741 const DWARF5AccelTableData &)>
742 getIndexForEntry) {
743 std::vector<std::variant<MCSymbol *, uint64_t>> TypeUnits;
744 Contents.finalize(Asm, Prefix: "names");
745 Dwarf5AccelTableWriter(Asm, Contents, CUs, TypeUnits, getIndexForEntry, false)
746 .emit();
747}
748
749void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const {
750 assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
751 "The section offset exceeds the limit.");
752 Asm->emitInt32(Value: Die.getDebugSectionOffset());
753}
754
755void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const {
756 assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
757 "The section offset exceeds the limit.");
758 Asm->emitInt32(Value: Die.getDebugSectionOffset());
759 Asm->emitInt16(Value: Die.getTag());
760 Asm->emitInt8(Value: 0);
761}
762
763void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const {
764 Asm->emitInt32(Value: Offset);
765}
766
767void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const {
768 Asm->emitInt32(Value: Offset);
769 Asm->emitInt16(Value: Tag);
770 Asm->emitInt8(Value: ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation
771 : 0);
772 Asm->emitInt32(Value: QualifiedNameHash);
773}
774
775#ifndef NDEBUG
776void AppleAccelTableWriter::Header::print(raw_ostream &OS) const {
777 OS << "Magic: " << format("0x%x", Magic) << "\n"
778 << "Version: " << Version << "\n"
779 << "Hash Function: " << HashFunction << "\n"
780 << "Bucket Count: " << BucketCount << "\n"
781 << "Header Data Length: " << HeaderDataLength << "\n";
782}
783
784void AppleAccelTableData::Atom::print(raw_ostream &OS) const {
785 OS << "Type: " << dwarf::AtomTypeString(Type) << "\n"
786 << "Form: " << dwarf::FormEncodingString(Form) << "\n";
787}
788
789void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const {
790 OS << "DIE Offset Base: " << DieOffsetBase << "\n";
791 for (auto Atom : Atoms)
792 Atom.print(OS);
793}
794
795void AppleAccelTableWriter::print(raw_ostream &OS) const {
796 Header.print(OS);
797 HeaderData.print(OS);
798 Contents.print(OS);
799 SecBegin->print(OS, nullptr);
800}
801
802void AccelTableBase::HashData::print(raw_ostream &OS) const {
803 OS << "Name: " << Name.getString() << "\n";
804 OS << " Hash Value: " << format("0x%x", HashValue) << "\n";
805 OS << " Symbol: ";
806 if (Sym)
807 OS << *Sym;
808 else
809 OS << "<none>";
810 OS << "\n";
811 for (auto *Value : Values)
812 Value->print(OS);
813}
814
815void AccelTableBase::print(raw_ostream &OS) const {
816 // Print Content.
817 OS << "Entries: \n";
818 for (const auto &[Name, Data] : Entries) {
819 OS << "Name: " << Name << "\n";
820 for (auto *V : Data.Values)
821 V->print(OS);
822 }
823
824 OS << "Buckets and Hashes: \n";
825 for (const auto &Bucket : Buckets)
826 for (const auto &Hash : Bucket)
827 Hash->print(OS);
828
829 OS << "Data: \n";
830 for (const auto &E : Entries)
831 E.second.print(OS);
832}
833
834void DWARF5AccelTableData::print(raw_ostream &OS) const {
835 OS << " Offset: " << getDieOffset() << "\n";
836 OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
837}
838
839void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
840 OS << " Offset: " << Die.getOffset() << "\n";
841}
842
843void AppleAccelTableTypeData::print(raw_ostream &OS) const {
844 OS << " Offset: " << Die.getOffset() << "\n";
845 OS << " Tag: " << dwarf::TagString(Die.getTag()) << "\n";
846}
847
848void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const {
849 OS << " Static Offset: " << Offset << "\n";
850}
851
852void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const {
853 OS << " Static Offset: " << Offset << "\n";
854 OS << " QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n";
855 OS << " Tag: " << dwarf::TagString(Tag) << "\n";
856 OS << " ObjCClassIsImplementation: "
857 << (ObjCClassIsImplementation ? "true" : "false");
858 OS << "\n";
859}
860#endif
861