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