|
| 1 | +//===- bolt/Core/DebugNames.h - Debug names support ---*- C++ |
| 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 declaration of classes required for generation of |
| 10 | +// .debug_names section. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef BOLT_CORE_DEBUG_NAMES_H |
| 15 | +#define BOLT_CORE_DEBUG_NAMES_H |
| 16 | + |
| 17 | +#include "DebugData.h" |
| 18 | +#include "llvm/CodeGen/AccelTable.h" |
| 19 | + |
| 20 | +namespace llvm { |
| 21 | +namespace bolt { |
| 22 | +class BOLTDWARF5AccelTableData : public DWARF5AccelTableData { |
| 23 | +public: |
| 24 | + BOLTDWARF5AccelTableData(const uint64_t DieOffset, |
| 25 | + const std::optional<uint64_t> DefiningParentOffset, |
| 26 | + const unsigned DieTag, const unsigned UnitID, |
| 27 | + const bool IsTU, |
| 28 | + const std::optional<unsigned> SecondUnitID) |
| 29 | + : DWARF5AccelTableData(DieOffset, DefiningParentOffset, DieTag, UnitID, |
| 30 | + IsTU), |
| 31 | + SecondUnitID(SecondUnitID) {} |
| 32 | + |
| 33 | + uint64_t getDieOffset() const { return DWARF5AccelTableData::getDieOffset(); } |
| 34 | + unsigned getDieTag() const { return DWARF5AccelTableData::getDieTag(); } |
| 35 | + unsigned getUnitID() const { return DWARF5AccelTableData::getUnitID(); } |
| 36 | + bool isTU() const { return DWARF5AccelTableData::isTU(); } |
| 37 | + std::optional<unsigned> getSecondUnitID() const { return SecondUnitID; } |
| 38 | + |
| 39 | +private: |
| 40 | + std::optional<unsigned> SecondUnitID; |
| 41 | +}; |
| 42 | + |
| 43 | +class DWARF5AcceleratorTable { |
| 44 | +public: |
| 45 | + DWARF5AcceleratorTable(const bool CreateDebugNames, BinaryContext &BC, |
| 46 | + DebugStrWriter &MainBinaryStrWriter); |
| 47 | + ~DWARF5AcceleratorTable() { |
| 48 | + for (DebugNamesAbbrev *Abbrev : AbbreviationsVector) |
| 49 | + Abbrev->~DebugNamesAbbrev(); |
| 50 | + } |
| 51 | + /// Add DWARF5 Accelerator table entry. |
| 52 | + /// Input is DWARFUnit being processed, DIE that belongs to it, and potential |
| 53 | + /// SkeletonCU if the Unit comes from a DWO section. |
| 54 | + void addAccelTableEntry(DWARFUnit &Unit, const DIE &Die, |
| 55 | + const std::optional<uint64_t> &DWOID); |
| 56 | + /// Set current unit being processed. |
| 57 | + void setCurrentUnit(DWARFUnit &Unit, const uint64_t UnitStartOffset); |
| 58 | + /// Emit Accelerator table. |
| 59 | + void emitAccelTable(); |
| 60 | + /// Returns true if the table was crated. |
| 61 | + bool isCreated() const { return NeedToCreate; } |
| 62 | + /// Returns buffer containing the accelerator table. |
| 63 | + std::unique_ptr<DebugBufferVector> releaseBuffer() { |
| 64 | + return std::move(FullTableBuffer); |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + BinaryContext &BC; |
| 69 | + bool NeedToCreate = false; |
| 70 | + BumpPtrAllocator Allocator; |
| 71 | + DebugStrWriter &MainBinaryStrWriter; |
| 72 | + StringRef StrSection; |
| 73 | + uint64_t CurrentUnitOffset = 0; |
| 74 | + const DWARFUnit *CurrentUnit = nullptr; |
| 75 | + std::unordered_map<uint32_t, uint32_t> AbbrevTagToIndexMap; |
| 76 | + |
| 77 | + /// Represents a group of entries with identical name (and hence, hash value). |
| 78 | + struct HashData { |
| 79 | + uint64_t StrOffset; |
| 80 | + uint32_t HashValue; |
| 81 | + uint32_t EntryOffset; |
| 82 | + std::vector<BOLTDWARF5AccelTableData *> Values; |
| 83 | + }; |
| 84 | + using HashList = std::vector<HashData *>; |
| 85 | + using BucketList = std::vector<HashList>; |
| 86 | + /// Contains all the offsets of CUs. |
| 87 | + SmallVector<uint32_t, 1> CUList; |
| 88 | + /// Contains all the offsets of local TUs. |
| 89 | + SmallVector<uint32_t, 1> LocalTUList; |
| 90 | + /// Contains all the type hashes for split dwarf TUs. |
| 91 | + SmallVector<uint64_t, 1> ForeignTUList; |
| 92 | + using StringEntries = |
| 93 | + MapVector<std::string, HashData, llvm::StringMap<unsigned>>; |
| 94 | + StringEntries Entries; |
| 95 | + /// FoldingSet that uniques the abbreviations. |
| 96 | + FoldingSet<DebugNamesAbbrev> AbbreviationsSet; |
| 97 | + /// Vector containing DebugNames abbreviations for iteration in order. |
| 98 | + SmallVector<DebugNamesAbbrev *, 5> AbbreviationsVector; |
| 99 | + /// The bump allocator to use when creating DIEAbbrev objects in the uniqued |
| 100 | + /// storage container. |
| 101 | + BumpPtrAllocator Alloc; |
| 102 | + uint32_t BucketCount = 0; |
| 103 | + uint32_t UniqueHashCount = 0; |
| 104 | + uint32_t AbbrevTableSize = 0; |
| 105 | + uint32_t CUIndexEncodingSize = 4; |
| 106 | + uint32_t TUIndexEncodingSize = 4; |
| 107 | + uint32_t AugmentationStringSize = 0; |
| 108 | + dwarf::Form CUIndexForm = dwarf::DW_FORM_data4; |
| 109 | + dwarf::Form TUIndexForm = dwarf::DW_FORM_data4; |
| 110 | + |
| 111 | + BucketList Buckets; |
| 112 | + |
| 113 | + std::unique_ptr<DebugBufferVector> FullTableBuffer; |
| 114 | + std::unique_ptr<raw_svector_ostream> FullTableStream; |
| 115 | + std::unique_ptr<DebugBufferVector> StrBuffer; |
| 116 | + std::unique_ptr<raw_svector_ostream> StrStream; |
| 117 | + std::unique_ptr<DebugBufferVector> EntriesBuffer; |
| 118 | + std::unique_ptr<raw_svector_ostream> Entriestream; |
| 119 | + std::unique_ptr<DebugBufferVector> AugStringBuffer; |
| 120 | + std::unique_ptr<raw_svector_ostream> AugStringtream; |
| 121 | + llvm::DenseMap<llvm::hash_code, uint64_t> StrCacheToOffsetMap; |
| 122 | + // Contains DWO ID to CUList Index. |
| 123 | + llvm::DenseMap<uint64_t, uint32_t> CUOffsetsToPatch; |
| 124 | + /// Adds Unit to either CUList, LocalTUList or ForeignTUList. |
| 125 | + /// Input Unit being processed, and DWO ID if Unit is being processed comes |
| 126 | + /// from a DWO section. |
| 127 | + void addUnit(DWARFUnit &Unit, const std::optional<uint64_t> &DWOID); |
| 128 | + /// Returns number of buckets in .debug_name table. |
| 129 | + ArrayRef<HashList> getBuckets() const { return Buckets; } |
| 130 | + /// Get encoding for a given attribute. |
| 131 | + std::optional<DWARF5AccelTable::UnitIndexAndEncoding> |
| 132 | + getIndexForEntry(const BOLTDWARF5AccelTableData &Value) const; |
| 133 | + /// Get encoding for a given attribute for second index. |
| 134 | + /// Returns nullopt if there is no second index. |
| 135 | + std::optional<DWARF5AccelTable::UnitIndexAndEncoding> |
| 136 | + getSecondIndexForEntry(const BOLTDWARF5AccelTableData &Value) const; |
| 137 | + /// Uniquify Entries. |
| 138 | + void finalize(); |
| 139 | + /// Computes bucket count. |
| 140 | + void computeBucketCount(); |
| 141 | + /// Populate Abbreviations Map. |
| 142 | + void populateAbbrevsMap(); |
| 143 | + /// Write Entries. |
| 144 | + void writeEntries(); |
| 145 | + /// Write an Entry. |
| 146 | + void writeEntry(const BOLTDWARF5AccelTableData &Entry); |
| 147 | + /// Write augmentation_string for BOLT. |
| 148 | + void writeAugmentationString(); |
| 149 | + /// Emit out Header for DWARF5 Accelerator table. |
| 150 | + void emitHeader() const; |
| 151 | + /// Emit out CU list. |
| 152 | + void emitCUList() const; |
| 153 | + /// Emit out TU List. Combination of LocalTUList and ForeignTUList. |
| 154 | + void emitTUList() const; |
| 155 | + /// Emit buckets. |
| 156 | + void emitBuckets() const; |
| 157 | + /// Emit hashes for hash table. |
| 158 | + void emitHashes() const; |
| 159 | + /// Emit string offsets for hash table. |
| 160 | + void emitStringOffsets() const; |
| 161 | + /// Emit Entry Offsets for hash table. |
| 162 | + void emitOffsets() const; |
| 163 | + /// Emit abbreviation table. |
| 164 | + void emitAbbrevs(); |
| 165 | + /// Emit entries. |
| 166 | + void emitData(); |
| 167 | + /// Emit augmentation string. |
| 168 | + void emitAugmentationString() const; |
| 169 | +}; |
| 170 | +} // namespace bolt |
| 171 | +} // namespace llvm |
| 172 | +#endif |
0 commit comments