TargetLoweringObjectFileImpl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- llvm/CodeGen/TargetLoweringObjectFileImpl.h - Object Info --*- 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. //
  14. // This file implements classes used to handle lowerings specific to common
  15. // object file formats.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
  19. #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/BinaryFormat/XCOFF.h"
  22. #include "llvm/Target/TargetLoweringObjectFile.h"
  23. namespace llvm {
  24. class GlobalValue;
  25. class MachineModuleInfo;
  26. class MachineFunction;
  27. class MCContext;
  28. class MCExpr;
  29. class MCSection;
  30. class MCSymbol;
  31. class Module;
  32. class TargetMachine;
  33. class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
  34. bool UseInitArray = false;
  35. mutable unsigned NextUniqueID = 1; // ID 0 is reserved for execute-only sections
  36. SmallPtrSet<GlobalObject *, 2> Used;
  37. protected:
  38. MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =
  39. MCSymbolRefExpr::VK_None;
  40. public:
  41. TargetLoweringObjectFileELF();
  42. ~TargetLoweringObjectFileELF() override = default;
  43. void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
  44. void getModuleMetadata(Module &M) override;
  45. /// Emit Obj-C garbage collection and linker options.
  46. void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
  47. void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
  48. const MCSymbol *Sym) const override;
  49. /// Given a constant with the SectionKind, return a section that it should be
  50. /// placed in.
  51. MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
  52. const Constant *C,
  53. Align &Alignment) const override;
  54. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  55. const TargetMachine &TM) const override;
  56. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  57. const TargetMachine &TM) const override;
  58. MCSection *getSectionForJumpTable(const Function &F,
  59. const TargetMachine &TM) const override;
  60. MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
  61. const TargetMachine &TM) const override;
  62. MCSection *
  63. getSectionForMachineBasicBlock(const Function &F,
  64. const MachineBasicBlock &MBB,
  65. const TargetMachine &TM) const override;
  66. MCSection *
  67. getUniqueSectionForFunction(const Function &F,
  68. const TargetMachine &TM) const override;
  69. bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
  70. const Function &F) const override;
  71. /// Return an MCExpr to use for a reference to the specified type info global
  72. /// variable from exception handling information.
  73. const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
  74. unsigned Encoding,
  75. const TargetMachine &TM,
  76. MachineModuleInfo *MMI,
  77. MCStreamer &Streamer) const override;
  78. // The symbol that gets passed to .cfi_personality.
  79. MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
  80. const TargetMachine &TM,
  81. MachineModuleInfo *MMI) const override;
  82. void InitializeELF(bool UseInitArray_);
  83. MCSection *getStaticCtorSection(unsigned Priority,
  84. const MCSymbol *KeySym) const override;
  85. MCSection *getStaticDtorSection(unsigned Priority,
  86. const MCSymbol *KeySym) const override;
  87. const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  88. const GlobalValue *RHS,
  89. const TargetMachine &TM) const override;
  90. const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
  91. const TargetMachine &TM) const override;
  92. MCSection *getSectionForCommandLines() const override;
  93. };
  94. class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
  95. public:
  96. TargetLoweringObjectFileMachO();
  97. ~TargetLoweringObjectFileMachO() override = default;
  98. void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
  99. /// Emit the module flags that specify the garbage collection information.
  100. void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
  101. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  102. const TargetMachine &TM) const override;
  103. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  104. const TargetMachine &TM) const override;
  105. MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
  106. const Constant *C,
  107. Align &Alignment) const override;
  108. /// The mach-o version of this method defaults to returning a stub reference.
  109. const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
  110. unsigned Encoding,
  111. const TargetMachine &TM,
  112. MachineModuleInfo *MMI,
  113. MCStreamer &Streamer) const override;
  114. // The symbol that gets passed to .cfi_personality.
  115. MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
  116. const TargetMachine &TM,
  117. MachineModuleInfo *MMI) const override;
  118. /// Get MachO PC relative GOT entry relocation
  119. const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
  120. const MCSymbol *Sym,
  121. const MCValue &MV, int64_t Offset,
  122. MachineModuleInfo *MMI,
  123. MCStreamer &Streamer) const override;
  124. void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  125. const TargetMachine &TM) const override;
  126. };
  127. class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
  128. mutable unsigned NextUniqueID = 0;
  129. const TargetMachine *TM = nullptr;
  130. public:
  131. ~TargetLoweringObjectFileCOFF() override = default;
  132. void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
  133. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  134. const TargetMachine &TM) const override;
  135. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  136. const TargetMachine &TM) const override;
  137. void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  138. const TargetMachine &TM) const override;
  139. MCSection *getSectionForJumpTable(const Function &F,
  140. const TargetMachine &TM) const override;
  141. /// Emit Obj-C garbage collection and linker options.
  142. void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
  143. MCSection *getStaticCtorSection(unsigned Priority,
  144. const MCSymbol *KeySym) const override;
  145. MCSection *getStaticDtorSection(unsigned Priority,
  146. const MCSymbol *KeySym) const override;
  147. const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  148. const GlobalValue *RHS,
  149. const TargetMachine &TM) const override;
  150. /// Given a mergeable constant with the specified size and relocation
  151. /// information, return a section that it should be placed in.
  152. MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
  153. const Constant *C,
  154. Align &Alignment) const override;
  155. private:
  156. void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const;
  157. };
  158. class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
  159. mutable unsigned NextUniqueID = 0;
  160. public:
  161. TargetLoweringObjectFileWasm() = default;
  162. ~TargetLoweringObjectFileWasm() override = default;
  163. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  164. const TargetMachine &TM) const override;
  165. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  166. const TargetMachine &TM) const override;
  167. bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
  168. const Function &F) const override;
  169. void InitializeWasm();
  170. MCSection *getStaticCtorSection(unsigned Priority,
  171. const MCSymbol *KeySym) const override;
  172. MCSection *getStaticDtorSection(unsigned Priority,
  173. const MCSymbol *KeySym) const override;
  174. const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  175. const GlobalValue *RHS,
  176. const TargetMachine &TM) const override;
  177. };
  178. class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
  179. public:
  180. TargetLoweringObjectFileXCOFF() = default;
  181. ~TargetLoweringObjectFileXCOFF() override = default;
  182. static bool ShouldEmitEHBlock(const MachineFunction *MF);
  183. static bool ShouldSetSSPCanaryBitInTB(const MachineFunction *MF);
  184. static MCSymbol *getEHInfoTableSymbol(const MachineFunction *MF);
  185. void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
  186. bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
  187. const Function &F) const override;
  188. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  189. const TargetMachine &TM) const override;
  190. MCSection *getStaticCtorSection(unsigned Priority,
  191. const MCSymbol *KeySym) const override;
  192. MCSection *getStaticDtorSection(unsigned Priority,
  193. const MCSymbol *KeySym) const override;
  194. const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  195. const GlobalValue *RHS,
  196. const TargetMachine &TM) const override;
  197. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  198. const TargetMachine &TM) const override;
  199. MCSection *getSectionForJumpTable(const Function &F,
  200. const TargetMachine &TM) const override;
  201. /// Given a constant with the SectionKind, return a section that it should be
  202. /// placed in.
  203. MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
  204. const Constant *C,
  205. Align &Alignment) const override;
  206. static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV);
  207. MCSection *
  208. getSectionForFunctionDescriptor(const Function *F,
  209. const TargetMachine &TM) const override;
  210. MCSection *getSectionForTOCEntry(const MCSymbol *Sym,
  211. const TargetMachine &TM) const override;
  212. /// For external functions, this will always return a function descriptor
  213. /// csect.
  214. MCSection *
  215. getSectionForExternalReference(const GlobalObject *GO,
  216. const TargetMachine &TM) const override;
  217. /// For functions, this will always return a function descriptor symbol.
  218. MCSymbol *getTargetSymbol(const GlobalValue *GV,
  219. const TargetMachine &TM) const override;
  220. MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
  221. const TargetMachine &TM) const override;
  222. };
  223. class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
  224. public:
  225. TargetLoweringObjectFileGOFF();
  226. ~TargetLoweringObjectFileGOFF() override = default;
  227. MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  228. const TargetMachine &TM) const override;
  229. MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  230. const TargetMachine &TM) const override;
  231. };
  232. } // end namespace llvm
  233. #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
  234. #ifdef __GNUC__
  235. #pragma GCC diagnostic pop
  236. #endif