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 DWARF5AccelTableData::DWARF5AccelTableData(const DIE &Die, 361 const DwarfCompileUnit &CU) 362 : OffsetVal(&Die), DieTag(Die.getTag()), UnitID(CU.getUniqueID()) {} 363 364 template <typename DataT> 365 void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) { 366 assert(CompUnitCount > 0 && "Index must have at least one CU."); 367 368 AsmPrinter *Asm = Ctx.Asm; 369 Ctx.ContributionEnd = 370 Asm->emitDwarfUnitLength("names", "Header: unit length"); 371 Asm->OutStreamer->AddComment("Header: version"); 372 Asm->emitInt16(Version); 373 Asm->OutStreamer->AddComment("Header: padding"); 374 Asm->emitInt16(Padding); 375 Asm->OutStreamer->AddComment("Header: compilation unit count"); 376 Asm->emitInt32(CompUnitCount); 377 Asm->OutStreamer->AddComment("Header: local type unit count"); 378 Asm->emitInt32(LocalTypeUnitCount); 379 Asm->OutStreamer->AddComment("Header: foreign type unit count"); 380 Asm->emitInt32(ForeignTypeUnitCount); 381 Asm->OutStreamer->AddComment("Header: bucket count"); 382 Asm->emitInt32(BucketCount); 383 Asm->OutStreamer->AddComment("Header: name count"); 384 Asm->emitInt32(NameCount); 385 Asm->OutStreamer->AddComment("Header: abbreviation table size"); 386 Asm->emitLabelDifference(Ctx.AbbrevEnd, Ctx.AbbrevStart, sizeof(uint32_t)); 387 Asm->OutStreamer->AddComment("Header: augmentation string size"); 388 assert(AugmentationStringSize % 4 == 0); 389 Asm->emitInt32(AugmentationStringSize); 390 Asm->OutStreamer->AddComment("Header: augmentation string"); 391 Asm->OutStreamer->emitBytes({AugmentationString, AugmentationStringSize}); 392 } 393 394 template <typename DataT> 395 DenseSet<uint32_t> Dwarf5AccelTableWriter<DataT>::getUniqueTags() const { 396 DenseSet<uint32_t> UniqueTags; 397 for (auto &Bucket : Contents.getBuckets()) { 398 for (auto *Hash : Bucket) { 399 for (auto *Value : Hash->Values) { 400 unsigned Tag = static_cast<const DataT *>(Value)->getDieTag(); 401 UniqueTags.insert(Tag); 402 } 403 } 404 } 405 return UniqueTags; 406 } 407 408 template <typename DataT> 409 SmallVector<typename Dwarf5AccelTableWriter<DataT>::AttributeEncoding, 2> 410 Dwarf5AccelTableWriter<DataT>::getUniformAttributes() const { 411 SmallVector<AttributeEncoding, 2> UA; 412 if (CompUnits.size() > 1) { 413 size_t LargestCUIndex = CompUnits.size() - 1; 414 dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex); 415 UA.push_back({dwarf::DW_IDX_compile_unit, Form}); 416 } 417 UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4}); 418 return UA; 419 } 420 421 template <typename DataT> 422 void Dwarf5AccelTableWriter<DataT>::emitCUList() const { 423 for (const auto &CU : enumerate(CompUnits)) { 424 Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index())); 425 if (std::holds_alternative<MCSymbol *>(CU.value())) 426 Asm->emitDwarfSymbolReference(std::get<MCSymbol *>(CU.value())); 427 else 428 Asm->emitDwarfLengthOrOffset(std::get<uint64_t>(CU.value())); 429 } 430 } 431 432 template <typename DataT> 433 void Dwarf5AccelTableWriter<DataT>::emitBuckets() const { 434 uint32_t Index = 1; 435 for (const auto &Bucket : enumerate(Contents.getBuckets())) { 436 Asm->OutStreamer->AddComment("Bucket " + Twine(Bucket.index())); 437 Asm->emitInt32(Bucket.value().empty() ? 0 : Index); 438 Index += Bucket.value().size(); 439 } 440 } 441 442 template <typename DataT> 443 void Dwarf5AccelTableWriter<DataT>::emitStringOffsets() const { 444 for (const auto &Bucket : enumerate(Contents.getBuckets())) { 445 for (auto *Hash : Bucket.value()) { 446 DwarfStringPoolEntryRef String = Hash->Name; 447 Asm->OutStreamer->AddComment("String in Bucket " + Twine(Bucket.index()) + 448 ": " + String.getString()); 449 Asm->emitDwarfStringOffset(String); 450 } 451 } 452 } 453 454 template <typename DataT> 455 void Dwarf5AccelTableWriter<DataT>::emitAbbrevs() const { 456 Asm->OutStreamer->emitLabel(AbbrevStart); 457 for (const auto &Abbrev : Abbreviations) { 458 Asm->OutStreamer->AddComment("Abbrev code"); 459 assert(Abbrev.first != 0); 460 Asm->emitULEB128(Abbrev.first); 461 Asm->OutStreamer->AddComment(dwarf::TagString(Abbrev.first)); 462 Asm->emitULEB128(Abbrev.first); 463 for (const auto &AttrEnc : Abbrev.second) { 464 Asm->emitULEB128(AttrEnc.Index, dwarf::IndexString(AttrEnc.Index).data()); 465 Asm->emitULEB128(AttrEnc.Form, 466 dwarf::FormEncodingString(AttrEnc.Form).data()); 467 } 468 Asm->emitULEB128(0, "End of abbrev"); 469 Asm->emitULEB128(0, "End of abbrev"); 470 } 471 Asm->emitULEB128(0, "End of abbrev list"); 472 Asm->OutStreamer->emitLabel(AbbrevEnd); 473 } 474 475 template <typename DataT> 476 void Dwarf5AccelTableWriter<DataT>::emitEntry(const DataT &Entry) const { 477 auto AbbrevIt = Abbreviations.find(Entry.getDieTag()); 478 assert(AbbrevIt != Abbreviations.end() && 479 "Why wasn't this abbrev generated?"); 480 481 Asm->emitULEB128(AbbrevIt->first, "Abbreviation code"); 482 for (const auto &AttrEnc : AbbrevIt->second) { 483 Asm->OutStreamer->AddComment(dwarf::IndexString(AttrEnc.Index)); 484 switch (AttrEnc.Index) { 485 case dwarf::DW_IDX_compile_unit: { 486 DIEInteger ID(getCUIndexForEntry(Entry)); 487 ID.emitValue(Asm, AttrEnc.Form); 488 break; 489 } 490 case dwarf::DW_IDX_die_offset: 491 assert(AttrEnc.Form == dwarf::DW_FORM_ref4); 492 Asm->emitInt32(Entry.getDieOffset()); 493 break; 494 default: 495 llvm_unreachable("Unexpected index attribute!"); 496 } 497 } 498 } 499 500 template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emitData() const { 501 Asm->OutStreamer->emitLabel(EntryPool); 502 for (auto &Bucket : Contents.getBuckets()) { 503 for (auto *Hash : Bucket) { 504 // Remember to emit the label for our offset. 505 Asm->OutStreamer->emitLabel(Hash->Sym); 506 for (const auto *Value : Hash->Values) 507 emitEntry(*static_cast<const DataT *>(Value)); 508 Asm->OutStreamer->AddComment("End of list: " + Hash->Name.getString()); 509 Asm->emitInt8(0); 510 } 511 } 512 } 513 514 template <typename DataT> 515 Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter( 516 AsmPrinter *Asm, const AccelTableBase &Contents, 517 ArrayRef<std::variant<MCSymbol *, uint64_t>> CompUnits, 518 llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry) 519 : AccelTableWriter(Asm, Contents, false), 520 Header(CompUnits.size(), Contents.getBucketCount(), 521 Contents.getUniqueNameCount()), 522 CompUnits(CompUnits), getCUIndexForEntry(std::move(getCUIndexForEntry)) { 523 DenseSet<uint32_t> UniqueTags = getUniqueTags(); 524 SmallVector<AttributeEncoding, 2> UniformAttributes = getUniformAttributes(); 525 526 Abbreviations.reserve(UniqueTags.size()); 527 for (uint32_t Tag : UniqueTags) 528 Abbreviations.try_emplace(Tag, UniformAttributes); 529 } 530 531 template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() { 532 Header.emit(*this); 533 emitCUList(); 534 emitBuckets(); 535 emitHashes(); 536 emitStringOffsets(); 537 emitOffsets(EntryPool); 538 emitAbbrevs(); 539 emitData(); 540 Asm->OutStreamer->emitValueToAlignment(Align(4), 0); 541 Asm->OutStreamer->emitLabel(ContributionEnd); 542 } 543 544 void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents, 545 StringRef Prefix, const MCSymbol *SecBegin, 546 ArrayRef<AppleAccelTableData::Atom> Atoms) { 547 Contents.finalize(Asm, Prefix); 548 AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit(); 549 } 550 551 void llvm::emitDWARF5AccelTable( 552 AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD, 553 ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) { 554 std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits; 555 SmallVector<unsigned, 1> CUIndex(CUs.size()); 556 int Count = 0; 557 for (const auto &CU : enumerate(CUs)) { 558 switch (CU.value()->getCUNode()->getNameTableKind()) { 559 case DICompileUnit::DebugNameTableKind::Default: 560 case DICompileUnit::DebugNameTableKind::Apple: 561 break; 562 default: 563 continue; 564 } 565 CUIndex[CU.index()] = Count++; 566 assert(CU.index() == CU.value()->getUniqueID()); 567 const DwarfCompileUnit *MainCU = 568 DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get(); 569 CompUnits.push_back(MainCU->getLabelBegin()); 570 } 571 572 if (CompUnits.empty()) 573 return; 574 575 Asm->OutStreamer->switchSection( 576 Asm->getObjFileLowering().getDwarfDebugNamesSection()); 577 578 Contents.finalize(Asm, "names"); 579 Dwarf5AccelTableWriter<DWARF5AccelTableData>( 580 Asm, Contents, CompUnits, 581 [&](const DWARF5AccelTableData &Entry) { 582 return CUIndex[Entry.getUnitID()]; 583 }) 584 .emit(); 585 } 586 587 void llvm::emitDWARF5AccelTable( 588 AsmPrinter *Asm, DWARF5AccelTable &Contents, 589 ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs, 590 llvm::function_ref<unsigned(const DWARF5AccelTableData &)> 591 getCUIndexForEntry) { 592 Contents.finalize(Asm, "names"); 593 Dwarf5AccelTableWriter<DWARF5AccelTableData>(Asm, Contents, CUs, 594 getCUIndexForEntry) 595 .emit(); 596 } 597 598 void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const { 599 assert(Die.getDebugSectionOffset() <= UINT32_MAX && 600 "The section offset exceeds the limit."); 601 Asm->emitInt32(Die.getDebugSectionOffset()); 602 } 603 604 void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const { 605 assert(Die.getDebugSectionOffset() <= UINT32_MAX && 606 "The section offset exceeds the limit."); 607 Asm->emitInt32(Die.getDebugSectionOffset()); 608 Asm->emitInt16(Die.getTag()); 609 Asm->emitInt8(0); 610 } 611 612 void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const { 613 Asm->emitInt32(Offset); 614 } 615 616 void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const { 617 Asm->emitInt32(Offset); 618 Asm->emitInt16(Tag); 619 Asm->emitInt8(ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation 620 : 0); 621 Asm->emitInt32(QualifiedNameHash); 622 } 623 624 constexpr AppleAccelTableData::Atom AppleAccelTableTypeData::Atoms[]; 625 constexpr AppleAccelTableData::Atom AppleAccelTableOffsetData::Atoms[]; 626 constexpr AppleAccelTableData::Atom AppleAccelTableStaticOffsetData::Atoms[]; 627 constexpr AppleAccelTableData::Atom AppleAccelTableStaticTypeData::Atoms[]; 628 629 #ifndef NDEBUG 630 void AppleAccelTableWriter::Header::print(raw_ostream &OS) const { 631 OS << "Magic: " << format("0x%x", Magic) << "\n" 632 << "Version: " << Version << "\n" 633 << "Hash Function: " << HashFunction << "\n" 634 << "Bucket Count: " << BucketCount << "\n" 635 << "Header Data Length: " << HeaderDataLength << "\n"; 636 } 637 638 void AppleAccelTableData::Atom::print(raw_ostream &OS) const { 639 OS << "Type: " << dwarf::AtomTypeString(Type) << "\n" 640 << "Form: " << dwarf::FormEncodingString(Form) << "\n"; 641 } 642 643 void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const { 644 OS << "DIE Offset Base: " << DieOffsetBase << "\n"; 645 for (auto Atom : Atoms) 646 Atom.print(OS); 647 } 648 649 void AppleAccelTableWriter::print(raw_ostream &OS) const { 650 Header.print(OS); 651 HeaderData.print(OS); 652 Contents.print(OS); 653 SecBegin->print(OS, nullptr); 654 } 655 656 void AccelTableBase::HashData::print(raw_ostream &OS) const { 657 OS << "Name: " << Name.getString() << "\n"; 658 OS << " Hash Value: " << format("0x%x", HashValue) << "\n"; 659 OS << " Symbol: "; 660 if (Sym) 661 OS << *Sym; 662 else 663 OS << "<none>"; 664 OS << "\n"; 665 for (auto *Value : Values) 666 Value->print(OS); 667 } 668 669 void AccelTableBase::print(raw_ostream &OS) const { 670 // Print Content. 671 OS << "Entries: \n"; 672 for (const auto &[Name, Data] : Entries) { 673 OS << "Name: " << Name << "\n"; 674 for (auto *V : Data.Values) 675 V->print(OS); 676 } 677 678 OS << "Buckets and Hashes: \n"; 679 for (const auto &Bucket : Buckets) 680 for (const auto &Hash : Bucket) 681 Hash->print(OS); 682 683 OS << "Data: \n"; 684 for (const auto &E : Entries) 685 E.second.print(OS); 686 } 687 688 void DWARF5AccelTableData::print(raw_ostream &OS) const { 689 OS << " Offset: " << getDieOffset() << "\n"; 690 OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n"; 691 } 692 693 void AppleAccelTableOffsetData::print(raw_ostream &OS) const { 694 OS << " Offset: " << Die.getOffset() << "\n"; 695 } 696 697 void AppleAccelTableTypeData::print(raw_ostream &OS) const { 698 OS << " Offset: " << Die.getOffset() << "\n"; 699 OS << " Tag: " << dwarf::TagString(Die.getTag()) << "\n"; 700 } 701 702 void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const { 703 OS << " Static Offset: " << Offset << "\n"; 704 } 705 706 void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const { 707 OS << " Static Offset: " << Offset << "\n"; 708 OS << " QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n"; 709 OS << " Tag: " << dwarf::TagString(Tag) << "\n"; 710 OS << " ObjCClassIsImplementation: " 711 << (ObjCClassIsImplementation ? "true" : "false"); 712 OS << "\n"; 713 } 714 #endif 715