TargetLoweringObjectFileImpl.h 13 KB

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