DWARFLinkerCompileUnit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /// Mark every DIE in this unit as kept. This function also
  122. /// marks variables as InDebugMap so that they appear in the
  123. /// reconstructed accelerator tables.
  124. void markEverythingAsKept();
  125. /// Compute the end offset for this unit. Must be called after the CU's DIEs
  126. /// have been cloned. \returns the next unit offset (which is also the
  127. /// current debug_info section size).
  128. uint64_t computeNextUnitOffset(uint16_t DwarfVersion);
  129. /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
  130. /// Attr. The attribute should be fixed up later to point to the absolute
  131. /// offset of \p Die in the debug_info section or to the canonical offset of
  132. /// \p Ctxt if it is non-null.
  133. void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
  134. DeclContext *Ctxt, PatchLocation Attr);
  135. /// Apply all fixups recorded by noteForwardReference().
  136. void fixupForwardReferences();
  137. /// Add the low_pc of a label that is relocated by applying
  138. /// offset \p PCOffset.
  139. void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
  140. /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
  141. /// offset \p PCOffset.
  142. void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
  143. /// Keep track of a DW_AT_range attribute that we will need to patch up later.
  144. void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
  145. /// Keep track of a location attribute pointing to a location list in the
  146. /// debug_loc section.
  147. void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
  148. /// Add a name accelerator entry for \a Die with \a Name.
  149. void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
  150. /// Add a name accelerator entry for \a Die with \a Name.
  151. void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
  152. bool SkipPubnamesSection = false);
  153. /// Add various accelerator entries for \p Die with \p Name which is stored
  154. /// in the string table at \p Offset. \p Name must be an Objective-C
  155. /// selector.
  156. void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
  157. bool SkipPubnamesSection = false);
  158. /// Add a type accelerator entry for \p Die with \p Name which is stored in
  159. /// the string table at \p Offset.
  160. void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
  161. bool ObjcClassImplementation,
  162. uint32_t QualifiedNameHash);
  163. struct AccelInfo {
  164. /// Name of the entry.
  165. DwarfStringPoolEntryRef Name;
  166. /// DIE this entry describes.
  167. const DIE *Die;
  168. /// Hash of the fully qualified name.
  169. uint32_t QualifiedNameHash;
  170. /// Emit this entry only in the apple_* sections.
  171. bool SkipPubSection;
  172. /// Is this an ObjC class implementation?
  173. bool ObjcClassImplementation;
  174. AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
  175. bool SkipPubSection = false)
  176. : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
  177. AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
  178. uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
  179. : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
  180. SkipPubSection(false),
  181. ObjcClassImplementation(ObjCClassIsImplementation) {}
  182. };
  183. const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
  184. const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
  185. const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
  186. const std::vector<AccelInfo> &getObjC() const { return ObjC; }
  187. MCSymbol *getLabelBegin() { return LabelBegin; }
  188. void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
  189. private:
  190. DWARFUnit &OrigUnit;
  191. unsigned ID;
  192. std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
  193. Optional<BasicDIEUnit> NewUnit;
  194. MCSymbol *LabelBegin = nullptr;
  195. uint64_t StartOffset;
  196. uint64_t NextUnitOffset;
  197. uint64_t LowPc = std::numeric_limits<uint64_t>::max();
  198. uint64_t HighPc = 0;
  199. /// A list of attributes to fixup with the absolute offset of
  200. /// a DIE in the debug_info section.
  201. ///
  202. /// The offsets for the attributes in this array couldn't be set while
  203. /// cloning because for cross-cu forward references the target DIE's offset
  204. /// isn't known you emit the reference attribute.
  205. std::vector<
  206. std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
  207. ForwardDIEReferences;
  208. FunctionIntervals::Allocator RangeAlloc;
  209. /// The ranges in that interval map are the PC ranges for
  210. /// functions in this unit, associated with the PC offset to apply
  211. /// to the addresses to get the linked address.
  212. FunctionIntervals Ranges;
  213. /// The DW_AT_low_pc of each DW_TAG_label.
  214. SmallDenseMap<uint64_t, uint64_t, 1> Labels;
  215. /// DW_AT_ranges attributes to patch after we have gathered
  216. /// all the unit's function addresses.
  217. /// @{
  218. std::vector<PatchLocation> RangeAttributes;
  219. Optional<PatchLocation> UnitRangeAttribute;
  220. /// @}
  221. /// Location attributes that need to be transferred from the
  222. /// original debug_loc section to the liked one. They are stored
  223. /// along with the PC offset that is to be applied to their
  224. /// function's address.
  225. std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
  226. /// Accelerator entries for the unit, both for the pub*
  227. /// sections and the apple* ones.
  228. /// @{
  229. std::vector<AccelInfo> Pubnames;
  230. std::vector<AccelInfo> Pubtypes;
  231. std::vector<AccelInfo> Namespaces;
  232. std::vector<AccelInfo> ObjC;
  233. /// @}
  234. /// Is this unit subject to the ODR rule?
  235. bool HasODR;
  236. /// The DW_AT_language of this unit.
  237. uint16_t Language = 0;
  238. /// The DW_AT_LLVM_sysroot of this unit.
  239. std::string SysRoot;
  240. /// If this is a Clang module, this holds the module's name.
  241. std::string ClangModuleName;
  242. };
  243. } // end namespace llvm
  244. #endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
  245. #ifdef __GNUC__
  246. #pragma GCC diagnostic pop
  247. #endif