DWARFLinkerCompileUnit.h 11 KB

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