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/IntervalMap.h" 13 #include "llvm/CodeGen/DIE.h" 14 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 15 #include "llvm/Support/DataExtractor.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; PatchLocationPatchLocation33 PatchLocation(DIE::value_iterator I) : I(I) {} 34 setPatchLocation35 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 getPatchLocation42 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 CompileUnit(DWARFUnit & OrigUnit,unsigned ID,bool CanUseODR,StringRef ClangModuleName)79 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, 80 StringRef ClangModuleName) 81 : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc), 82 ClangModuleName(ClangModuleName) { 83 Info.resize(OrigUnit.getNumDIEs()); 84 85 auto CUDie = OrigUnit.getUnitDIE(false); 86 if (!CUDie) { 87 HasODR = false; 88 return; 89 } 90 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language))) 91 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus || 92 *Lang == dwarf::DW_LANG_C_plus_plus_03 || 93 *Lang == dwarf::DW_LANG_C_plus_plus_11 || 94 *Lang == dwarf::DW_LANG_C_plus_plus_14 || 95 *Lang == dwarf::DW_LANG_ObjC_plus_plus); 96 else 97 HasODR = false; 98 } 99 getOrigUnit()100 DWARFUnit &getOrigUnit() const { return OrigUnit; } 101 getUniqueID()102 unsigned getUniqueID() const { return ID; } 103 createOutputDIE()104 void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); } 105 getOutputUnitDIE()106 DIE *getOutputUnitDIE() const { 107 if (NewUnit) 108 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie(); 109 return nullptr; 110 } 111 hasODR()112 bool hasODR() const { return HasODR; } isClangModule()113 bool isClangModule() const { return !ClangModuleName.empty(); } 114 uint16_t getLanguage(); 115 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef. 116 StringRef getSysRoot(); 117 getClangModuleName()118 const std::string &getClangModuleName() const { return ClangModuleName; } 119 getInfo(unsigned Idx)120 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } getInfo(unsigned Idx)121 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } 122 getInfo(const DWARFDie & Die)123 DIEInfo &getInfo(const DWARFDie &Die) { 124 unsigned Idx = getOrigUnit().getDIEIndex(Die); 125 return Info[Idx]; 126 } 127 getStartOffset()128 uint64_t getStartOffset() const { return StartOffset; } getNextUnitOffset()129 uint64_t getNextUnitOffset() const { return NextUnitOffset; } setStartOffset(uint64_t DebugInfoSize)130 void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; } 131 getLowPc()132 uint64_t getLowPc() const { return LowPc; } getHighPc()133 uint64_t getHighPc() const { return HighPc; } hasLabelAt(uint64_t Addr)134 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); } 135 getUnitRangesAttribute()136 Optional<PatchLocation> getUnitRangesAttribute() const { 137 return UnitRangeAttribute; 138 } 139 getFunctionRanges()140 const FunctionIntervals &getFunctionRanges() const { return Ranges; } 141 getRangesAttributes()142 const std::vector<PatchLocation> &getRangesAttributes() const { 143 return RangeAttributes; 144 } 145 146 const std::vector<std::pair<PatchLocation, int64_t>> & getLocationAttributes()147 getLocationAttributes() const { 148 return LocationAttributes; 149 } 150 setHasInterestingContent()151 void setHasInterestingContent() { HasInterestingContent = true; } hasInterestingContent()152 bool hasInterestingContent() { return HasInterestingContent; } 153 154 /// Mark every DIE in this unit as kept. This function also 155 /// marks variables as InDebugMap so that they appear in the 156 /// reconstructed accelerator tables. 157 void markEverythingAsKept(); 158 159 /// Compute the end offset for this unit. Must be called after the CU's DIEs 160 /// have been cloned. \returns the next unit offset (which is also the 161 /// current debug_info section size). 162 uint64_t computeNextUnitOffset(uint16_t DwarfVersion); 163 164 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p 165 /// Attr. The attribute should be fixed up later to point to the absolute 166 /// offset of \p Die in the debug_info section or to the canonical offset of 167 /// \p Ctxt if it is non-null. 168 void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, 169 DeclContext *Ctxt, PatchLocation Attr); 170 171 /// Apply all fixups recorded by noteForwardReference(). 172 void fixupForwardReferences(); 173 174 /// Add the low_pc of a label that is relocated by applying 175 /// offset \p PCOffset. 176 void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset); 177 178 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying 179 /// offset \p PCOffset. 180 void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset); 181 182 /// Keep track of a DW_AT_range attribute that we will need to patch up later. 183 void noteRangeAttribute(const DIE &Die, PatchLocation Attr); 184 185 /// Keep track of a location attribute pointing to a location list in the 186 /// debug_loc section. 187 void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset); 188 189 /// Add a name accelerator entry for \a Die with \a Name. 190 void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name); 191 192 /// Add a name accelerator entry for \a Die with \a Name. 193 void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 194 bool SkipPubnamesSection = false); 195 196 /// Add various accelerator entries for \p Die with \p Name which is stored 197 /// in the string table at \p Offset. \p Name must be an Objective-C 198 /// selector. 199 void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 200 bool SkipPubnamesSection = false); 201 202 /// Add a type accelerator entry for \p Die with \p Name which is stored in 203 /// the string table at \p Offset. 204 void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, 205 bool ObjcClassImplementation, 206 uint32_t QualifiedNameHash); 207 208 struct AccelInfo { 209 /// Name of the entry. 210 DwarfStringPoolEntryRef Name; 211 212 /// DIE this entry describes. 213 const DIE *Die; 214 215 /// Hash of the fully qualified name. 216 uint32_t QualifiedNameHash; 217 218 /// Emit this entry only in the apple_* sections. 219 bool SkipPubSection; 220 221 /// Is this an ObjC class implementation? 222 bool ObjcClassImplementation; 223 224 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, 225 bool SkipPubSection = false) NameAccelInfo226 : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {} 227 AccelInfoAccelInfo228 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, 229 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation) 230 : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash), 231 SkipPubSection(false), 232 ObjcClassImplementation(ObjCClassIsImplementation) {} 233 }; 234 getPubnames()235 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; } getPubtypes()236 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; } getNamespaces()237 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; } getObjC()238 const std::vector<AccelInfo> &getObjC() const { return ObjC; } 239 getLabelBegin()240 MCSymbol *getLabelBegin() { return LabelBegin; } setLabelBegin(MCSymbol * S)241 void setLabelBegin(MCSymbol *S) { LabelBegin = S; } 242 243 private: 244 DWARFUnit &OrigUnit; 245 unsigned ID; 246 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. 247 Optional<BasicDIEUnit> NewUnit; 248 MCSymbol *LabelBegin = nullptr; 249 250 uint64_t StartOffset; 251 uint64_t NextUnitOffset; 252 253 uint64_t LowPc = std::numeric_limits<uint64_t>::max(); 254 uint64_t HighPc = 0; 255 256 /// A list of attributes to fixup with the absolute offset of 257 /// a DIE in the debug_info section. 258 /// 259 /// The offsets for the attributes in this array couldn't be set while 260 /// cloning because for cross-cu forward references the target DIE's offset 261 /// isn't known you emit the reference attribute. 262 std::vector< 263 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>> 264 ForwardDIEReferences; 265 266 FunctionIntervals::Allocator RangeAlloc; 267 268 /// The ranges in that interval map are the PC ranges for 269 /// functions in this unit, associated with the PC offset to apply 270 /// to the addresses to get the linked address. 271 FunctionIntervals Ranges; 272 273 /// The DW_AT_low_pc of each DW_TAG_label. 274 SmallDenseMap<uint64_t, uint64_t, 1> Labels; 275 276 /// DW_AT_ranges attributes to patch after we have gathered 277 /// all the unit's function addresses. 278 /// @{ 279 std::vector<PatchLocation> RangeAttributes; 280 Optional<PatchLocation> UnitRangeAttribute; 281 /// @} 282 283 /// Location attributes that need to be transferred from the 284 /// original debug_loc section to the liked one. They are stored 285 /// along with the PC offset that is to be applied to their 286 /// function's address. 287 std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes; 288 289 /// Accelerator entries for the unit, both for the pub* 290 /// sections and the apple* ones. 291 /// @{ 292 std::vector<AccelInfo> Pubnames; 293 std::vector<AccelInfo> Pubtypes; 294 std::vector<AccelInfo> Namespaces; 295 std::vector<AccelInfo> ObjC; 296 /// @} 297 298 /// Is this unit subject to the ODR rule? 299 bool HasODR; 300 301 /// Did a DIE actually contain a valid reloc? 302 bool HasInterestingContent; 303 304 /// The DW_AT_language of this unit. 305 uint16_t Language = 0; 306 307 /// The DW_AT_LLVM_sysroot of this unit. 308 std::string SysRoot; 309 310 /// If this is a Clang module, this holds the module's name. 311 std::string ClangModuleName; 312 }; 313 314 } // end namespace llvm 315 316 #endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H 317