1 //===- DWARFLinkerCompileUnit.h ---------------------------------*- 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 #ifndef LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H 10 #define LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/IntervalMap.h" 14 #include "llvm/CodeGen/DIE.h" 15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 16 17 namespace llvm { 18 19 class DeclContext; 20 21 template <typename KeyT, typename ValT> 22 using HalfOpenIntervalMap = 23 IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize, 24 IntervalMapHalfOpenInfo<KeyT>>; 25 26 using FunctionIntervals = HalfOpenIntervalMap<uint64_t, int64_t>; 27 28 // FIXME: Delete this structure. 29 struct PatchLocation { 30 DIE::value_iterator I; 31 32 PatchLocation() = default; 33 PatchLocation(DIE::value_iterator I) : I(I) {} 34 35 void set(uint64_t New) const { 36 assert(I); 37 const auto &Old = *I; 38 assert(Old.getType() == DIEValue::isInteger); 39 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New)); 40 } 41 42 uint64_t get() const { 43 assert(I); 44 return I->getDIEInteger().getValue(); 45 } 46 }; 47 48 /// Stores all information relating to a compile unit, be it in its original 49 /// instance in the object file to its brand new cloned and generated DIE tree. 50 class CompileUnit { 51 public: 52 /// Information gathered about a DIE in the object file. 53 struct DIEInfo { 54 /// Address offset to apply to the described entity. 55 int64_t AddrAdjust; 56 57 /// ODR Declaration context. 58 DeclContext *Ctxt; 59 60 /// Cloned version of that DIE. 61 DIE *Clone; 62 63 /// The index of this DIE's parent. 64 uint32_t ParentIdx; 65 66 /// Is the DIE part of the linked output? 67 bool Keep : 1; 68 69 /// Was this DIE's entity found in the map? 70 bool InDebugMap : 1; 71 72 /// Is this a pure forward declaration we can strip? 73 bool Prune : 1; 74 75 /// Does DIE transitively refer an incomplete decl? 76 bool Incomplete : 1; 77 78 /// Is DIE in the clang module scope? 79 bool InModuleScope : 1; 80 81 /// Is ODR marking done? 82 bool ODRMarkingDone : 1; 83 }; 84 85 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, 86 StringRef ClangModuleName) 87 : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc), 88 ClangModuleName(ClangModuleName) { 89 Info.resize(OrigUnit.getNumDIEs()); 90 91 auto CUDie = OrigUnit.getUnitDIE(false); 92 if (!CUDie) { 93 HasODR = false; 94 return; 95 } 96 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language))) 97 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus || 98 *Lang == dwarf::DW_LANG_C_plus_plus_03 || 99 *Lang == dwarf::DW_LANG_C_plus_plus_11 || 100 *Lang == dwarf::DW_LANG_C_plus_plus_14 || 101 *Lang == dwarf::DW_LANG_ObjC_plus_plus); 102 else 103 HasODR = false; 104 } 105 106 DWARFUnit &getOrigUnit() const { return OrigUnit; } 107 108 unsigned getUniqueID() const { return ID; } 109 110 void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); } 111 112 DIE *getOutputUnitDIE() const { 113 if (NewUnit) 114 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie(); 115 return nullptr; 116 } 117 118 bool hasODR() const { return HasODR; } 119 bool isClangModule() const { return !ClangModuleName.empty(); } 120 uint16_t getLanguage(); 121 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef. 122 StringRef getSysRoot(); 123 124 const std::string &getClangModuleName() const { return ClangModuleName; } 125 126 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } 127 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } 128 129 DIEInfo &getInfo(const DWARFDie &Die) { 130 unsigned Idx = getOrigUnit().getDIEIndex(Die); 131 return Info[Idx]; 132 } 133 134 uint64_t getStartOffset() const { return StartOffset; } 135 uint64_t getNextUnitOffset() const { return NextUnitOffset; } 136 void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; } 137 138 uint64_t getLowPc() const { return LowPc; } 139 uint64_t getHighPc() const { return HighPc; } 140 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); } 141 142 Optional<PatchLocation> getUnitRangesAttribute() const { 143 return UnitRangeAttribute; 144 } 145 146 const FunctionIntervals &getFunctionRanges() const { return Ranges; } 147 148 const std::vector<PatchLocation> &getRangesAttributes() const { 149 return RangeAttributes; 150 } 151 152 const std::vector<std::pair<PatchLocation, int64_t>> & 153 getLocationAttributes() const { 154 return LocationAttributes; 155 } 156 157 /// Mark every DIE in this unit as kept. This function also 158 /// marks variables as InDebugMap so that they appear in the 159 /// reconstructed accelerator tables. 160 void markEverythingAsKept(); 161 162 /// Compute the end offset for this unit. Must be called after the CU's DIEs 163 /// have been cloned. \returns the next unit offset (which is also the 164 /// current debug_info section size). 165 uint64_t computeNextUnitOffset(uint16_t DwarfVersion); 166 167 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p 168 /// Attr. The attribute should be fixed up later to point to the absolute 169 /// offset of \p Die in the debug_info section or to the canonical offset of 170 /// \p Ctxt if it is non-null. 171 void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, 172 DeclContext *Ctxt, PatchLocation Attr); 173 174 /// Apply all fixups recorded by noteForwardReference(). 175 void fixupForwardReferences(); 176 177 /// Add the low_pc of a label that is relocated by applying 178 /// offset \p PCOffset. 179 void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset); 180 181 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying 182 /// offset \p PCOffset. 183 void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset); 184 185 /// Check whether specified address range \p LowPC \p HighPC 186 /// overlaps with existing function ranges. 187 bool overlapsWithFunctionRanges(uint64_t LowPC, uint64_t HighPC); 188 189 /// Keep track of a DW_AT_range attribute that we will need to patch up later. 190 void noteRangeAttribute(const DIE &Die, PatchLocation Attr); 191 192 /// Keep track of a location attribute pointing to a location list in the 193 /// debug_loc section. 194 void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset); 195 196 /// Add a name accelerator entry for \a Die with \a Name. 197 void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name); 198 199 /// Add a name accelerator entry for \a Die with \a Name. 200 void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 201 bool SkipPubnamesSection = false); 202 203 /// Add various accelerator entries for \p Die with \p Name which is stored 204 /// in the string table at \p Offset. \p Name must be an Objective-C 205 /// selector. 206 void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 207 bool SkipPubnamesSection = false); 208 209 /// Add a type accelerator entry for \p Die with \p Name which is stored in 210 /// the string table at \p Offset. 211 void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 212 bool ObjcClassImplementation, 213 uint32_t QualifiedNameHash); 214 215 struct AccelInfo { 216 /// Name of the entry. 217 DwarfStringPoolEntryRef Name; 218 219 /// DIE this entry describes. 220 const DIE *Die; 221 222 /// Hash of the fully qualified name. 223 uint32_t QualifiedNameHash; 224 225 /// Emit this entry only in the apple_* sections. 226 bool SkipPubSection; 227 228 /// Is this an ObjC class implementation? 229 bool ObjcClassImplementation; 230 231 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, 232 bool SkipPubSection = false) 233 : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {} 234 235 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, 236 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation) 237 : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash), 238 SkipPubSection(false), 239 ObjcClassImplementation(ObjCClassIsImplementation) {} 240 }; 241 242 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; } 243 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; } 244 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; } 245 const std::vector<AccelInfo> &getObjC() const { return ObjC; } 246 247 MCSymbol *getLabelBegin() { return LabelBegin; } 248 void setLabelBegin(MCSymbol *S) { LabelBegin = S; } 249 250 private: 251 DWARFUnit &OrigUnit; 252 unsigned ID; 253 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. 254 Optional<BasicDIEUnit> NewUnit; 255 MCSymbol *LabelBegin = nullptr; 256 257 uint64_t StartOffset; 258 uint64_t NextUnitOffset; 259 260 uint64_t LowPc = std::numeric_limits<uint64_t>::max(); 261 uint64_t HighPc = 0; 262 263 /// A list of attributes to fixup with the absolute offset of 264 /// a DIE in the debug_info section. 265 /// 266 /// The offsets for the attributes in this array couldn't be set while 267 /// cloning because for cross-cu forward references the target DIE's offset 268 /// isn't known you emit the reference attribute. 269 std::vector< 270 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>> 271 ForwardDIEReferences; 272 273 FunctionIntervals::Allocator RangeAlloc; 274 275 /// The ranges in that interval map are the PC ranges for 276 /// functions in this unit, associated with the PC offset to apply 277 /// to the addresses to get the linked address. 278 FunctionIntervals Ranges; 279 280 /// The DW_AT_low_pc of each DW_TAG_label. 281 SmallDenseMap<uint64_t, uint64_t, 1> Labels; 282 283 /// DW_AT_ranges attributes to patch after we have gathered 284 /// all the unit's function addresses. 285 /// @{ 286 std::vector<PatchLocation> RangeAttributes; 287 Optional<PatchLocation> UnitRangeAttribute; 288 /// @} 289 290 /// Location attributes that need to be transferred from the 291 /// original debug_loc section to the liked one. They are stored 292 /// along with the PC offset that is to be applied to their 293 /// function's address. 294 std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes; 295 296 /// Accelerator entries for the unit, both for the pub* 297 /// sections and the apple* ones. 298 /// @{ 299 std::vector<AccelInfo> Pubnames; 300 std::vector<AccelInfo> Pubtypes; 301 std::vector<AccelInfo> Namespaces; 302 std::vector<AccelInfo> ObjC; 303 /// @} 304 305 /// Is this unit subject to the ODR rule? 306 bool HasODR; 307 308 /// The DW_AT_language of this unit. 309 uint16_t Language = 0; 310 311 /// The DW_AT_LLVM_sysroot of this unit. 312 std::string SysRoot; 313 314 /// If this is a Clang module, this holds the module's name. 315 std::string ClangModuleName; 316 }; 317 318 } // end namespace llvm 319 320 #endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H 321