DWARFLinkerCompileUnit.h 11 KB

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