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 "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/BinaryFormat/Dwarf.h" 18 #include "llvm/CodeGen/AsmPrinter.h" 19 #include "llvm/CodeGen/DIE.h" 20 #include "llvm/MC/MCStreamer.h" 21 #include "llvm/MC/MCSymbol.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Target/TargetLoweringObjectFile.h" 24 #include <algorithm> 25 #include <cstddef> 26 #include <cstdint> 27 #include <limits> 28 #include <vector> 29 30 using namespace llvm; 31 32 void AccelTableBase::computeBucketCount() { 33 // First get the number of unique hashes. 34 std::vector<uint32_t> Uniques; 35 Uniques.reserve(Entries.size()); 36 for (const auto &E : Entries) 37 Uniques.push_back(E.second.HashValue); 38 array_pod_sort(Uniques.begin(), Uniques.end()); 39 std::vector<uint32_t>::iterator P = 40 std::unique(Uniques.begin(), Uniques.end()); 41 42 UniqueHashCount = std::distance(Uniques.begin(), P); 43 44 if (UniqueHashCount > 1024) 45 BucketCount = UniqueHashCount / 4; 46 else if (UniqueHashCount > 16) 47 BucketCount = UniqueHashCount / 2; 48 else 49 BucketCount = std::max<uint32_t>(UniqueHashCount, 1); 50 } 51 52 void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) { 53 // Create the individual hash data outputs. 54 for (auto &E : Entries) { 55 // Unique the entries. 56 llvm::stable_sort(E.second.Values, 57 [](const AccelTableData *A, const AccelTableData *B) { 58 return *A < *B; 59 }); 60 E.second.Values.erase( 61 std::unique(E.second.Values.begin(), E.second.Values.end()), 62 E.second.Values.end()); 63 } 64 65 // Figure out how many buckets we need, then compute the bucket contents and 66 // the final ordering. The hashes and offsets can be emitted by walking these 67 // data structures. We add temporary symbols to the data so they can be 68 // referenced when emitting the offsets. 69 computeBucketCount(); 70 71 // Compute bucket contents and final ordering. 72 Buckets.resize(BucketCount); 73 for (auto &E : Entries) { 74 uint32_t Bucket = E.second.HashValue % BucketCount; 75 Buckets[Bucket].push_back(&E.second); 76 E.second.Sym = Asm->createTempSymbol(Prefix); 77 } 78 79 // Sort the contents of the buckets by hash value so that hash collisions end 80 // up together. Stable sort makes testing easier and doesn't cost much more. 81 for (auto &Bucket : Buckets) 82 llvm::stable_sort(Bucket, [](HashData *LHS, HashData *RHS) { 83 return LHS->HashValue < RHS->HashValue; 84 }); 85 } 86 87 namespace { 88 /// Base class for writing out Accelerator tables. It holds the common 89 /// functionality for the two Accelerator table types. 90 class AccelTableWriter { 91 protected: 92 AsmPrinter *const Asm; ///< Destination. 93 const AccelTableBase &Contents; ///< Data to emit. 94 95 /// Controls whether to emit duplicate hash and offset table entries for names 96 /// with identical hashes. Apple tables don't emit duplicate entries, DWARF v5 97 /// tables do. 98 const bool SkipIdenticalHashes; 99 100 void emitHashes() const; 101 102 /// Emit offsets to lists of entries with identical names. The offsets are 103 /// relative to the Base argument. 104 void emitOffsets(const MCSymbol *Base) const; 105 106 public: 107 AccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents, 108 bool SkipIdenticalHashes) 109 : Asm(Asm), Contents(Contents), SkipIdenticalHashes(SkipIdenticalHashes) { 110 } 111 }; 112 113 class AppleAccelTableWriter : public AccelTableWriter { 114 using Atom = AppleAccelTableData::Atom; 115 116 /// The fixed header of an Apple Accelerator Table. 117 struct Header { 118 uint32_t Magic = MagicHash; 119 uint16_t Version = 1; 120 uint16_t HashFunction = dwarf::DW_hash_function_djb; 121 uint32_t BucketCount; 122 uint32_t HashCount; 123 uint32_t HeaderDataLength; 124 125 /// 'HASH' magic value to detect endianness. 126 static const uint32_t MagicHash = 0x48415348; 127 128 Header(uint32_t BucketCount, uint32_t UniqueHashCount, uint32_t DataLength) 129 : BucketCount(BucketCount), HashCount(UniqueHashCount), 130 HeaderDataLength(DataLength) {} 131 132 void emit(AsmPrinter *Asm) const; 133 #ifndef NDEBUG 134 void print(raw_ostream &OS) const; 135 void dump() const { print(dbgs()); } 136 #endif 137 }; 138 139 /// The HeaderData describes the structure of an Apple accelerator table 140 /// through a list of Atoms. 141 struct HeaderData { 142 /// In the case of data that is referenced via DW_FORM_ref_* the offset 143 /// base is used to describe the offset for all forms in the list of atoms. 144 uint32_t DieOffsetBase; 145 146 const SmallVector<Atom, 4> Atoms; 147 148 HeaderData(ArrayRef<Atom> AtomList, uint32_t Offset = 0) 149 : DieOffsetBase(Offset), Atoms(AtomList.begin(), AtomList.end()) {} 150 151 void emit(AsmPrinter *Asm) const; 152 #ifndef NDEBUG 153 void print(raw_ostream &OS) const; 154 void dump() const { print(dbgs()); } 155 #endif 156 }; 157 158 Header Header; 159 HeaderData HeaderData; 160 const MCSymbol *SecBegin; 161 162 void emitBuckets() const; 163 void emitData() const; 164 165 public: 166 AppleAccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents, 167 ArrayRef<Atom> Atoms, const MCSymbol *SecBegin) 168 : AccelTableWriter(Asm, Contents, true), 169 Header(Contents.getBucketCount(), Contents.getUniqueHashCount(), 170 8 + (Atoms.size() * 4)), 171 HeaderData(Atoms), SecBegin(SecBegin) {} 172 173 void emit() const; 174 175 #ifndef NDEBUG 176 void print(raw_ostream &OS) const; 177 void dump() const { print(dbgs()); } 178 #endif 179 }; 180 181 /// Class responsible for emitting a DWARF v5 Accelerator Table. The only 182 /// public function is emit(), which performs the actual emission. 183 /// 184 /// The class is templated in its data type. This allows us to emit both dyamic 185 /// and static data entries. A callback abstract the logic to provide a CU 186 /// index for a given entry, which is different per data type, but identical 187 /// for every entry in the same table. 188 template <typename DataT> 189 class Dwarf5AccelTableWriter : public AccelTableWriter { 190 struct Header { 191 uint16_t Version = 5; 192 uint16_t Padding = 0; 193 uint32_t CompUnitCount; 194 uint32_t LocalTypeUnitCount = 0; 195 uint32_t ForeignTypeUnitCount = 0; 196 uint32_t BucketCount = 0; 197 uint32_t NameCount = 0; 198 uint32_t AbbrevTableSize = 0; 199 uint32_t AugmentationStringSize = sizeof(AugmentationString); 200 char AugmentationString[8] = {'L', 'L', 'V', 'M', '0', '7', '0', '0'}; 201 202 Header(uint32_t CompUnitCount, uint32_t BucketCount, uint32_t NameCount) 203 : CompUnitCount(CompUnitCount), BucketCount(BucketCount), 204 NameCount(NameCount) {} 205 206 void emit(Dwarf5AccelTableWriter &Ctx); 207 }; 208 struct AttributeEncoding { 209 dwarf::Index Index; 210 dwarf::Form Form; 211 }; 212 213 Header Header; 214 DenseMap<uint32_t, SmallVector<AttributeEncoding, 2>> Abbreviations; 215 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits; 216 llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry; 217 MCSymbol *ContributionEnd = nullptr; 218 MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start"); 219 MCSymbol *AbbrevEnd = Asm->createTempSymbol("names_abbrev_end"); 220 MCSymbol *EntryPool = Asm->createTempSymbol("names_entries"); 221 222 DenseSet<uint32_t> getUniqueTags() const; 223 224 // Right now, we emit uniform attributes for all tags. 225 SmallVector<AttributeEncoding, 2> getUniformAttributes() const; 226 227 void emitCUList() const; 228 void emitBuckets() const; 229 void emitStringOffsets() const; 230 void emitAbbrevs() const; 231 void emitEntry(const DataT &Entry) const; 232 void emitData() const; 233 234 public: 235 Dwarf5AccelTableWriter( 236 AsmPrinter *Asm, const AccelTableBase &Contents, 237 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits, 238 llvm::function_ref<unsigned(const DataT &)> GetCUIndexForEntry); 239 240 void emit(); 241 }; 242 } // namespace 243 244 void AccelTableWriter::emitHashes() const { 245 uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); 246 unsigned BucketIdx = 0; 247 for (const auto &Bucket : Contents.getBuckets()) { 248 for (const auto &Hash : Bucket) { 249 uint32_t HashValue = Hash->HashValue; 250 if (SkipIdenticalHashes && PrevHash == HashValue) 251 continue; 252 Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(BucketIdx)); 253 Asm->emitInt32(HashValue); 254 PrevHash = HashValue; 255 } 256 BucketIdx++; 257 } 258 } 259 260 void AccelTableWriter::emitOffsets(const MCSymbol *Base) const { 261 const auto &Buckets = Contents.getBuckets(); 262 uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); 263 for (size_t i = 0, e = Buckets.size(); i < e; ++i) { 264 for (auto *Hash : Buckets[i]) { 265 uint32_t HashValue = Hash->HashValue; 266 if (SkipIdenticalHashes && PrevHash == HashValue) 267 continue; 268 PrevHash = HashValue; 269 Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i)); 270 Asm->emitLabelDifference(Hash->Sym, Base, Asm->getDwarfOffsetByteSize()); 271 } 272 } 273 } 274 275 void AppleAccelTableWriter::Header::emit(AsmPrinter *Asm) const { 276 Asm->OutStreamer->AddComment("Header Magic"); 277 Asm->emitInt32(Magic); 278 Asm->OutStreamer->AddComment("Header Version"); 279 Asm->emitInt16(Version); 280 Asm->OutStreamer->AddComment("Header Hash Function"); 281 Asm->emitInt16(HashFunction); 282 Asm->OutStreamer->AddComment("Header Bucket Count"); 283 Asm->emitInt32(BucketCount); 284 Asm->OutStreamer->AddComment("Header Hash Count"); 285 Asm->emitInt32(HashCount); 286 Asm->OutStreamer->AddComment("Header Data Length"); 287 Asm->emitInt32(HeaderDataLength); 288 } 289 290 void AppleAccelTableWriter::HeaderData::emit(AsmPrinter *Asm) const { 291 Asm->OutStreamer->AddComment("HeaderData Die Offset Base"); 292 Asm->emitInt32(DieOffsetBase); 293 Asm->OutStreamer->AddComment("HeaderData Atom Count"); 294 Asm->emitInt32(Atoms.size()); 295 296 for (const Atom &A : Atoms) { 297 Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.Type)); 298 Asm->emitInt16(A.Type); 299 Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.Form)); 300 Asm->emitInt16(A.Form); 301 } 302 } 303 304 void AppleAccelTableWriter::emitBuckets() const { 305 const auto &Buckets = Contents.getBuckets(); 306 unsigned index = 0; 307 for (size_t i = 0, e = Buckets.size(); i < e; ++i) { 308 Asm->OutStreamer->AddComment("Bucket " + Twine(i)); 309 if (!Buckets[i].empty()) 310 Asm->emitInt32(index); 311 else 312 Asm->emitInt32(std::numeric_limits<uint32_t>::max()); 313 // Buckets point in the list of hashes, not to the data. Do not increment 314 // the index multiple times in case of hash collisions. 315 uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); 316 for (auto *HD : Buckets[i]) { 317 uint32_t HashValue = HD->HashValue; 318 if (PrevHash != HashValue) 319 ++index; 320 PrevHash = HashValue; 321 } 322 } 323 } 324 325 void AppleAccelTableWriter::emitData() const { 326 const auto &Buckets = Contents.getBuckets(); 327 for (const AccelTableBase::HashList &Bucket : Buckets) { 328 uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); 329 for (const auto &Hash : Bucket) { 330 // Terminate the previous entry if there is no hash collision with the 331 // current one. 332 if (PrevHash != std::numeric_limits<uint64_t>::max() && 333 PrevHash != Hash->HashValue) 334 Asm->emitInt32(0); 335 // Remember to emit the label for our offset. 336 Asm->OutStreamer->emitLabel(Hash->Sym); 337 Asm->OutStreamer->AddComment(Hash->Name.getString()); 338 Asm->emitDwarfStringOffset(Hash->Name); 339 Asm->OutStreamer->AddComment("Num DIEs"); 340 Asm->emitInt32(Hash->Values.size()); 341 for (const auto *V : Hash->Values) 342 static_cast<const AppleAccelTableData *>(V)->emit(Asm); 343 PrevHash = Hash->HashValue; 344 } 345 // Emit the final end marker for the bucket. 346 if (!Bucket.empty()) 347 Asm->emitInt32(0); 348 } 349 } 350 351 void AppleAccelTableWriter::emit() const { 352 Header.emit(Asm); 353 HeaderData.emit(Asm); 354 emitBuckets(); 355 emitHashes(); 356 emitOffsets(SecBegin); 357 emitData(); 358 } 359 360 template <typename DataT> 361 void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) { 362 assert(CompUnitCount > 0 && "Index must have at least one CU."); 363 364 AsmPrinter *Asm = Ctx.Asm; 365 Ctx.ContributionEnd = 366 Asm->emitDwarfUnitLength("names", "Header: unit length"); 367 Asm->OutStreamer->AddComment("Header: version"); 368 Asm->emitInt16(Version); 369 Asm->OutStreamer->AddComment("Header: padding"); 370 Asm->emitInt16(Padding); 371 Asm->OutStreamer->AddComment("Header: compilation unit count"); 372 Asm->emitInt32(CompUnitCount); 373 Asm->OutStreamer->AddComment("Header: local type unit count"); 374 Asm->emitInt32(LocalTypeUnitCount); 375 Asm->OutStreamer->AddComment("Header: foreign type unit count"); 376 Asm->emitInt32(ForeignTypeUnitCount); 377 Asm->OutStreamer->AddComment("Header: bucket count"); 378 Asm->emitInt32(BucketCount); 379 Asm->OutStreamer->AddComment("Header: name count"); 380 Asm->emitInt32(NameCount); 381 Asm->OutStreamer->AddComment("Header: abbreviation table size"); 382 Asm->emitLabelDifference(Ctx.AbbrevEnd, Ctx.AbbrevStart, sizeof(uint32_t)); 383 Asm->OutStreamer->AddComment("Header: augmentation string size"); 384 assert(AugmentationStringSize % 4 == 0); 385 Asm->emitInt32(AugmentationStringSize); 386 Asm->OutStreamer->AddComment("Header: augmentation string"); 387 Asm->OutStreamer->emitBytes({AugmentationString, AugmentationStringSize}); 388 } 389 390 template <typename DataT> 391 DenseSet<uint32_t> Dwarf5AccelTableWriter<DataT>::getUniqueTags() const { 392 DenseSet<uint32_t> UniqueTags; 393 for (auto &Bucket : Contents.getBuckets()) { 394 for (auto *Hash : Bucket) { 395 for (auto *Value : Hash->Values) { 396 unsigned Tag = static_cast<const DataT *>(Value)->getDieTag(); 397 UniqueTags.insert(Tag); 398 } 399 } 400 } 401 return UniqueTags; 402 } 403 404 template <typename DataT> 405 SmallVector<typename Dwarf5AccelTableWriter<DataT>::AttributeEncoding, 2> 406 Dwarf5AccelTableWriter<DataT>::getUniformAttributes() const { 407 SmallVector<AttributeEncoding, 2> UA; 408 if (CompUnits.size() > 1) { 409 size_t LargestCUIndex = CompUnits.size() - 1; 410 dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex); 411 UA.push_back({dwarf::DW_IDX_compile_unit, Form}); 412 } 413 UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4}); 414 return UA; 415 } 416 417 template <typename DataT> 418 void Dwarf5AccelTableWriter<DataT>::emitCUList() const { 419 for (const auto &CU : enumerate(CompUnits)) { 420 Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index())); 421 if (std::holds_alternative<MCSymbol *>(CU.value())) 422 Asm->emitDwarfSymbolReference(std::get<MCSymbol *>(CU.value())); 423 else 424 Asm->emitDwarfLengthOrOffset(std::get<uint64_t>(CU.value())); 425 } 426 } 427 428 template <typename DataT> 429 void Dwarf5AccelTableWriter<DataT>::emitBuckets() const { 430 uint32_t Index = 1; 431 for (const auto &Bucket : enumerate(Contents.getBuckets())) { 432 Asm->OutStreamer->AddComment("Bucket " + Twine(Bucket.index())); 433 Asm->emitInt32(Bucket.value().empty() ? 0 : Index); 434 Index += Bucket.value().size(); 435 } 436 } 437 438 template <typename DataT> 439 void Dwarf5AccelTableWriter<DataT>::emitStringOffsets() const { 440 for (const auto &Bucket : enumerate(Contents.getBuckets())) { 441 for (auto *Hash : Bucket.value()) { 442 DwarfStringPoolEntryRef String = Hash->Name; 443 Asm->OutStreamer->AddComment("String in Bucket " + Twine(Bucket.index()) + 444 ": " + String.getString()); 445 Asm->emitDwarfStringOffset(String); 446 } 447 } 448 } 449 450 template <typename DataT> 451 void Dwarf5AccelTableWriter<DataT>::emitAbbrevs() const { 452 Asm->OutStreamer->emitLabel(AbbrevStart); 453 for (const auto &Abbrev : Abbreviations) { 454 Asm->OutStreamer->AddComment("Abbrev code"); 455 assert(Abbrev.first != 0); 456 Asm->emitULEB128(Abbrev.first); 457 Asm->OutStreamer->AddComment(dwarf::TagString(Abbrev.first)); 458 Asm->emitULEB128(Abbrev.first); 459 for (const auto &AttrEnc : Abbrev.second) { 460 Asm->emitULEB128(AttrEnc.Index, dwarf::IndexString(AttrEnc.Index).data()); 461 Asm->emitULEB128(AttrEnc.Form, 462 dwarf::FormEncodingString(AttrEnc.Form).data()); 463 } 464 Asm->emitULEB128(0, "End of abbrev"); 465 Asm->emitULEB128(0, "End of abbrev"); 466 } 467 Asm->emitULEB128(0, "End of abbrev list"); 468 Asm->OutStreamer->emitLabel(AbbrevEnd); 469 } 470 471 template <typename DataT> 472 void Dwarf5AccelTableWriter<DataT>::emitEntry(const DataT &Entry) const { 473 auto AbbrevIt = Abbreviations.find(Entry.getDieTag()); 474 assert(AbbrevIt != Abbreviations.end() && 475 "Why wasn't this abbrev generated?"); 476 477 Asm->emitULEB128(AbbrevIt->first, "Abbreviation code"); 478 for (const auto &AttrEnc : AbbrevIt->second) { 479 Asm->OutStreamer->AddComment(dwarf::IndexString(AttrEnc.Index)); 480 switch (AttrEnc.Index) { 481 case dwarf::DW_IDX_compile_unit: { 482 DIEInteger ID(getCUIndexForEntry(Entry)); 483 ID.emitValue(Asm, AttrEnc.Form); 484 break; 485 } 486 case dwarf::DW_IDX_die_offset: 487 assert(AttrEnc.Form == dwarf::DW_FORM_ref4); 488 Asm->emitInt32(Entry.getDieOffset()); 489 break; 490 default: 491 llvm_unreachable("Unexpected index attribute!"); 492 } 493 } 494 } 495 496 template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emitData() const { 497 Asm->OutStreamer->emitLabel(EntryPool); 498 for (auto &Bucket : Contents.getBuckets()) { 499 for (auto *Hash : Bucket) { 500 // Remember to emit the label for our offset. 501 Asm->OutStreamer->emitLabel(Hash->Sym); 502 for (const auto *Value : Hash->Values) 503 emitEntry(*static_cast<const DataT *>(Value)); 504 Asm->OutStreamer->AddComment("End of list: " + Hash->Name.getString()); 505 Asm->emitInt8(0); 506 } 507 } 508 } 509 510 template <typename DataT> 511 Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter( 512 AsmPrinter *Asm, const AccelTableBase &Contents, 513 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits, 514 llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry) 515 : AccelTableWriter(Asm, Contents, false), 516 Header(CompUnits.size(), Contents.getBucketCount(), 517 Contents.getUniqueNameCount()), 518 CompUnits(CompUnits), getCUIndexForEntry(std::move(getCUIndexForEntry)) { 519 DenseSet<uint32_t> UniqueTags = getUniqueTags(); 520 SmallVector<AttributeEncoding, 2> UniformAttributes = getUniformAttributes(); 521 522 Abbreviations.reserve(UniqueTags.size()); 523 for (uint32_t Tag : UniqueTags) 524 Abbreviations.try_emplace(Tag, UniformAttributes); 525 } 526 527 template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() { 528 Header.emit(*this); 529 emitCUList(); 530 emitBuckets(); 531 emitHashes(); 532 emitStringOffsets(); 533 emitOffsets(EntryPool); 534 emitAbbrevs(); 535 emitData(); 536 Asm->OutStreamer->emitValueToAlignment(Align(4), 0); 537 Asm->OutStreamer->emitLabel(ContributionEnd); 538 } 539 540 void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents, 541 StringRef Prefix, const MCSymbol *SecBegin, 542 ArrayRef<AppleAccelTableData::Atom> Atoms) { 543 Contents.finalize(Asm, Prefix); 544 AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit(); 545 } 546 547 void llvm::emitDWARF5AccelTable( 548 AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents, 549 const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) { 550 std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits; 551 SmallVector<unsigned, 1> CUIndex(CUs.size()); 552 int Count = 0; 553 for (const auto &CU : enumerate(CUs)) { 554 switch (CU.value()->getCUNode()->getNameTableKind()) { 555 case DICompileUnit::DebugNameTableKind::Default: 556 case DICompileUnit::DebugNameTableKind::Apple: 557 break; 558 default: 559 continue; 560 } 561 CUIndex[CU.index()] = Count++; 562 assert(CU.index() == CU.value()->getUniqueID()); 563 const DwarfCompileUnit *MainCU = 564 DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get(); 565 CompUnits.push_back(MainCU->getLabelBegin()); 566 } 567 568 if (CompUnits.empty()) 569 return; 570 571 Asm->OutStreamer->switchSection( 572 Asm->getObjFileLowering().getDwarfDebugNamesSection()); 573 574 Contents.finalize(Asm, "names"); 575 Dwarf5AccelTableWriter<DWARF5AccelTableData>( 576 Asm, Contents, CompUnits, 577 [&](const DWARF5AccelTableData &Entry) { 578 const DIE *CUDie = Entry.getDie().getUnitDie(); 579 return CUIndex[DD.lookupCU(CUDie)->getUniqueID()]; 580 }) 581 .emit(); 582 } 583 584 void llvm::emitDWARF5AccelTable( 585 AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents, 586 ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs, 587 llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)> 588 getCUIndexForEntry) { 589 Contents.finalize(Asm, "names"); 590 Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs, 591 getCUIndexForEntry) 592 .emit(); 593 } 594 595 void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const { 596 assert(Die.getDebugSectionOffset() <= UINT32_MAX && 597 "The section offset exceeds the limit."); 598 Asm->emitInt32(Die.getDebugSectionOffset()); 599 } 600 601 void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const { 602 assert(Die.getDebugSectionOffset() <= UINT32_MAX && 603 "The section offset exceeds the limit."); 604 Asm->emitInt32(Die.getDebugSectionOffset()); 605 Asm->emitInt16(Die.getTag()); 606 Asm->emitInt8(0); 607 } 608 609 void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const { 610 Asm->emitInt32(Offset); 611 } 612 613 void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const { 614 Asm->emitInt32(Offset); 615 Asm->emitInt16(Tag); 616 Asm->emitInt8(ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation 617 : 0); 618 Asm->emitInt32(QualifiedNameHash); 619 } 620 621 constexpr AppleAccelTableData::Atom AppleAccelTableTypeData::Atoms[]; 622 constexpr AppleAccelTableData::Atom AppleAccelTableOffsetData::Atoms[]; 623 constexpr AppleAccelTableData::Atom AppleAccelTableStaticOffsetData::Atoms[]; 624 constexpr AppleAccelTableData::Atom AppleAccelTableStaticTypeData::Atoms[]; 625 626 #ifndef NDEBUG 627 void AppleAccelTableWriter::Header::print(raw_ostream &OS) const { 628 OS << "Magic: " << format("0x%x", Magic) << "\n" 629 << "Version: " << Version << "\n" 630 << "Hash Function: " << HashFunction << "\n" 631 << "Bucket Count: " << BucketCount << "\n" 632 << "Header Data Length: " << HeaderDataLength << "\n"; 633 } 634 635 void AppleAccelTableData::Atom::print(raw_ostream &OS) const { 636 OS << "Type: " << dwarf::AtomTypeString(Type) << "\n" 637 << "Form: " << dwarf::FormEncodingString(Form) << "\n"; 638 } 639 640 void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const { 641 OS << "DIE Offset Base: " << DieOffsetBase << "\n"; 642 for (auto Atom : Atoms) 643 Atom.print(OS); 644 } 645 646 void AppleAccelTableWriter::print(raw_ostream &OS) const { 647 Header.print(OS); 648 HeaderData.print(OS); 649 Contents.print(OS); 650 SecBegin->print(OS, nullptr); 651 } 652 653 void AccelTableBase::HashData::print(raw_ostream &OS) const { 654 OS << "Name: " << Name.getString() << "\n"; 655 OS << " Hash Value: " << format("0x%x", HashValue) << "\n"; 656 OS << " Symbol: "; 657 if (Sym) 658 OS << *Sym; 659 else 660 OS << "<none>"; 661 OS << "\n"; 662 for (auto *Value : Values) 663 Value->print(OS); 664 } 665 666 void AccelTableBase::print(raw_ostream &OS) const { 667 // Print Content. 668 OS << "Entries: \n"; 669 for (const auto &[Name, Data] : Entries) { 670 OS << "Name: " << Name << "\n"; 671 for (auto *V : Data.Values) 672 V->print(OS); 673 } 674 675 OS << "Buckets and Hashes: \n"; 676 for (const auto &Bucket : Buckets) 677 for (const auto &Hash : Bucket) 678 Hash->print(OS); 679 680 OS << "Data: \n"; 681 for (const auto &E : Entries) 682 E.second.print(OS); 683 } 684 685 void DWARF5AccelTableData::print(raw_ostream &OS) const { 686 OS << " Offset: " << getDieOffset() << "\n"; 687 OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n"; 688 } 689 690 void DWARF5AccelTableStaticData::print(raw_ostream &OS) const { 691 OS << " Offset: " << getDieOffset() << "\n"; 692 OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n"; 693 } 694 695 void AppleAccelTableOffsetData::print(raw_ostream &OS) const { 696 OS << " Offset: " << Die.getOffset() << "\n"; 697 } 698 699 void AppleAccelTableTypeData::print(raw_ostream &OS) const { 700 OS << " Offset: " << Die.getOffset() << "\n"; 701 OS << " Tag: " << dwarf::TagString(Die.getTag()) << "\n"; 702 } 703 704 void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const { 705 OS << " Static Offset: " << Offset << "\n"; 706 } 707 708 void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const { 709 OS << " Static Offset: " << Offset << "\n"; 710 OS << " QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n"; 711 OS << " Tag: " << dwarf::TagString(Tag) << "\n"; 712 OS << " ObjCClassIsImplementation: " 713 << (ObjCClassIsImplementation ? "true" : "false"); 714 OS << "\n"; 715 } 716 #endif 717