TargetLoweringObjectFile.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements classes used to handle lowerings specific to common
  10. // object file formats.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
  14. #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
  15. #include "llvm/MC/MCObjectFileInfo.h"
  16. #include <cstdint>
  17. namespace llvm {
  18. class Constant;
  19. class DataLayout;
  20. class Function;
  21. class GlobalObject;
  22. class GlobalValue;
  23. class MachineBasicBlock;
  24. class MachineModuleInfo;
  25. class Mangler;
  26. class MCContext;
  27. class MCExpr;
  28. class MCSection;
  29. class MCSymbol;
  30. class MCSymbolRefExpr;
  31. class MCStreamer;
  32. class MCValue;
  33. class Module;
  34. class SectionKind;
  35. class StringRef;
  36. class TargetMachine;
  37. class DSOLocalEquivalent;
  38. class TargetLoweringObjectFile : public MCObjectFileInfo {
  39. /// Name-mangler for global names.
  40. Mangler *Mang = nullptr;
  41. protected:
  42. bool SupportIndirectSymViaGOTPCRel = false;
  43. bool SupportGOTPCRelWithOffset = true;
  44. bool SupportDebugThreadLocalLocation = true;
  45. bool SupportDSOLocalEquivalentLowering = false;
  46. /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
  47. /// for EH.
  48. unsigned PersonalityEncoding = 0;
  49. unsigned LSDAEncoding = 0;
  50. unsigned TTypeEncoding = 0;
  51. unsigned CallSiteEncoding = 0;
  52. /// This section contains the static constructor pointer list.
  53. MCSection *StaticCtorSection = nullptr;
  54. /// This section contains the static destructor pointer list.
  55. MCSection *StaticDtorSection = nullptr;
  56. const TargetMachine *TM = nullptr;
  57. public:
  58. TargetLoweringObjectFile() = default;
  59. TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
  60. TargetLoweringObjectFile &
  61. operator=(const TargetLoweringObjectFile &) = delete;
  62. virtual ~TargetLoweringObjectFile();
  63. Mangler &getMangler() const { return *Mang; }
  64. /// This method must be called before any actual lowering is done. This
  65. /// specifies the current context for codegen, and gives the lowering
  66. /// implementations a chance to set up their default sections.
  67. virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
  68. virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
  69. const MCSymbol *Sym) const;
  70. /// Emit the module-level metadata that the platform cares about.
  71. virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
  72. /// Emit Call Graph Profile metadata.
  73. void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
  74. /// Get the module-level metadata that the platform cares about.
  75. virtual void getModuleMetadata(Module &M) {}
  76. /// Given a constant with the SectionKind, return a section that it should be
  77. /// placed in.
  78. virtual MCSection *getSectionForConstant(const DataLayout &DL,
  79. SectionKind Kind, const Constant *C,
  80. Align &Alignment) const;
  81. virtual MCSection *
  82. getSectionForMachineBasicBlock(const Function &F,
  83. const MachineBasicBlock &MBB,
  84. const TargetMachine &TM) const;
  85. virtual MCSection *
  86. getUniqueSectionForFunction(const Function &F,
  87. const TargetMachine &TM) const;
  88. /// Classify the specified global variable into a set of target independent
  89. /// categories embodied in SectionKind.
  90. static SectionKind getKindForGlobal(const GlobalObject *GO,
  91. const TargetMachine &TM);
  92. /// This method computes the appropriate section to emit the specified global
  93. /// variable or function definition. This should not be passed external (or
  94. /// available externally) globals.
  95. MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
  96. const TargetMachine &TM) const;
  97. /// This method computes the appropriate section to emit the specified global
  98. /// variable or function definition. This should not be passed external (or
  99. /// available externally) globals.
  100. MCSection *SectionForGlobal(const GlobalObject *GO,
  101. const TargetMachine &TM) const;
  102. virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
  103. const GlobalValue *GV,
  104. const TargetMachine &TM) const;
  105. virtual MCSection *getSectionForJumpTable(const Function &F,
  106. const TargetMachine &TM) const;
  107. virtual MCSection *getSectionForLSDA(const Function &, const MCSymbol &,
  108. const TargetMachine &) const {
  109. return LSDASection;
  110. }
  111. virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
  112. const Function &F) const;
  113. /// Targets should implement this method to assign a section to globals with
  114. /// an explicit section specfied. The implementation of this method can
  115. /// assume that GO->hasSection() is true.
  116. virtual MCSection *
  117. getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
  118. const TargetMachine &TM) const = 0;
  119. /// Return an MCExpr to use for a reference to the specified global variable
  120. /// from exception handling information.
  121. virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
  122. unsigned Encoding,
  123. const TargetMachine &TM,
  124. MachineModuleInfo *MMI,
  125. MCStreamer &Streamer) const;
  126. /// Return the MCSymbol for a private symbol with global value name as its
  127. /// base, with the specified suffix.
  128. MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
  129. StringRef Suffix,
  130. const TargetMachine &TM) const;
  131. // The symbol that gets passed to .cfi_personality.
  132. virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
  133. const TargetMachine &TM,
  134. MachineModuleInfo *MMI) const;
  135. unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
  136. unsigned getLSDAEncoding() const { return LSDAEncoding; }
  137. unsigned getTTypeEncoding() const { return TTypeEncoding; }
  138. unsigned getCallSiteEncoding() const;
  139. const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
  140. MCStreamer &Streamer) const;
  141. virtual MCSection *getStaticCtorSection(unsigned Priority,
  142. const MCSymbol *KeySym) const {
  143. return StaticCtorSection;
  144. }
  145. virtual MCSection *getStaticDtorSection(unsigned Priority,
  146. const MCSymbol *KeySym) const {
  147. return StaticDtorSection;
  148. }
  149. /// Create a symbol reference to describe the given TLS variable when
  150. /// emitting the address in debug info.
  151. virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
  152. virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
  153. const GlobalValue *RHS,
  154. const TargetMachine &TM) const {
  155. return nullptr;
  156. }
  157. /// Target supports a native lowering of a dso_local_equivalent constant
  158. /// without needing to replace it with equivalent IR.
  159. bool supportDSOLocalEquivalentLowering() const {
  160. return SupportDSOLocalEquivalentLowering;
  161. }
  162. virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
  163. const TargetMachine &TM) const {
  164. return nullptr;
  165. }
  166. /// Target supports replacing a data "PC"-relative access to a symbol
  167. /// through another symbol, by accessing the later via a GOT entry instead?
  168. bool supportIndirectSymViaGOTPCRel() const {
  169. return SupportIndirectSymViaGOTPCRel;
  170. }
  171. /// Target GOT "PC"-relative relocation supports encoding an additional
  172. /// binary expression with an offset?
  173. bool supportGOTPCRelWithOffset() const {
  174. return SupportGOTPCRelWithOffset;
  175. }
  176. /// Target supports TLS offset relocation in debug section?
  177. bool supportDebugThreadLocalLocation() const {
  178. return SupportDebugThreadLocalLocation;
  179. }
  180. /// Get the target specific PC relative GOT entry relocation
  181. virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
  182. const MCSymbol *Sym,
  183. const MCValue &MV,
  184. int64_t Offset,
  185. MachineModuleInfo *MMI,
  186. MCStreamer &Streamer) const {
  187. return nullptr;
  188. }
  189. /// If supported, return the section to use for the llvm.commandline
  190. /// metadata. Otherwise, return nullptr.
  191. virtual MCSection *getSectionForCommandLines() const {
  192. return nullptr;
  193. }
  194. /// On targets that use separate function descriptor symbols, return a section
  195. /// for the descriptor given its symbol. Use only with defined functions.
  196. virtual MCSection *
  197. getSectionForFunctionDescriptor(const Function *F,
  198. const TargetMachine &TM) const {
  199. return nullptr;
  200. }
  201. /// On targets that support TOC entries, return a section for the entry given
  202. /// the symbol it refers to.
  203. /// TODO: Implement this interface for existing ELF targets.
  204. virtual MCSection *getSectionForTOCEntry(const MCSymbol *S,
  205. const TargetMachine &TM) const {
  206. return nullptr;
  207. }
  208. /// On targets that associate external references with a section, return such
  209. /// a section for the given external global.
  210. virtual MCSection *
  211. getSectionForExternalReference(const GlobalObject *GO,
  212. const TargetMachine &TM) const {
  213. return nullptr;
  214. }
  215. /// Targets that have a special convention for their symbols could use
  216. /// this hook to return a specialized symbol.
  217. virtual MCSymbol *getTargetSymbol(const GlobalValue *GV,
  218. const TargetMachine &TM) const {
  219. return nullptr;
  220. }
  221. /// If supported, return the function entry point symbol.
  222. /// Otherwise, returns nulltpr.
  223. /// Func must be a function or an alias which has a function as base object.
  224. virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
  225. const TargetMachine &TM) const {
  226. return nullptr;
  227. }
  228. protected:
  229. virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
  230. SectionKind Kind,
  231. const TargetMachine &TM) const = 0;
  232. };
  233. } // end namespace llvm
  234. #endif // LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H