• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16 #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/SectionKind.h"
22 
23 namespace llvm {
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 ConstantExpr;
34   class GlobalValue;
35   class TargetMachine;
36 
37 class TargetLoweringObjectFile : public MCObjectFileInfo {
38   MCContext *Ctx;
39 
40   TargetLoweringObjectFile(
41     const TargetLoweringObjectFile&) = delete;
42   void operator=(const TargetLoweringObjectFile&) = delete;
43 
44 protected:
45   const DataLayout *DL;
46   bool SupportIndirectSymViaGOTPCRel;
47   bool SupportGOTPCRelWithOffset;
48 
49 public:
getContext()50   MCContext &getContext() const { return *Ctx; }
51 
TargetLoweringObjectFile()52   TargetLoweringObjectFile() : MCObjectFileInfo(), Ctx(nullptr), DL(nullptr),
53                                SupportIndirectSymViaGOTPCRel(false),
54                                SupportGOTPCRelWithOffset(true) {}
55 
56   virtual ~TargetLoweringObjectFile();
57 
58   /// This method must be called before any actual lowering is done.  This
59   /// specifies the current context for codegen, and gives the lowering
60   /// implementations a chance to set up their default sections.
61   virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
62 
63   virtual void emitPersonalityValue(MCStreamer &Streamer,
64                                     const TargetMachine &TM,
65                                     const MCSymbol *Sym) const;
66 
67   /// Extract the dependent library name from a linker option string. Returns
68   /// StringRef() if the option does not specify a library.
getDepLibFromLinkerOpt(StringRef LinkerOption)69   virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const {
70     return StringRef();
71   }
72 
73   /// Emit the module flags that the platform cares about.
emitModuleFlags(MCStreamer & Streamer,ArrayRef<Module::ModuleFlagEntry> Flags,Mangler & Mang,const TargetMachine & TM)74   virtual void emitModuleFlags(MCStreamer &Streamer,
75                                ArrayRef<Module::ModuleFlagEntry> Flags,
76                                Mangler &Mang, const TargetMachine &TM) const {}
77 
78   /// Given a constant with the SectionKind, return a section that it should be
79   /// placed in.
80   virtual const MCSection *getSectionForConstant(SectionKind Kind,
81                                                  const Constant *C) const;
82 
83   /// Classify the specified global variable into a set of target independent
84   /// categories embodied in SectionKind.
85   static SectionKind getKindForGlobal(const GlobalValue *GV,
86                                       const TargetMachine &TM);
87 
88   /// This method computes the appropriate section to emit the specified global
89   /// variable or function definition. This should not be passed external (or
90   /// available externally) globals.
91   const MCSection *SectionForGlobal(const GlobalValue *GV,
92                                     SectionKind Kind, Mangler &Mang,
93                                     const TargetMachine &TM) const;
94 
95   /// This method computes the appropriate section to emit the specified global
96   /// variable or function definition. This should not be passed external (or
97   /// available externally) globals.
SectionForGlobal(const GlobalValue * GV,Mangler & Mang,const TargetMachine & TM)98   const MCSection *SectionForGlobal(const GlobalValue *GV,
99                                     Mangler &Mang,
100                                     const TargetMachine &TM) const {
101     return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM);
102   }
103 
104   virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
105                                  const GlobalValue *GV,
106                                  bool CannotUsePrivateLabel, Mangler &Mang,
107                                  const TargetMachine &TM) const;
108 
109   virtual const MCSection *
110   getSectionForJumpTable(const Function &F, Mangler &Mang,
111                          const TargetMachine &TM) const;
112 
113   virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
114                                                    const Function &F) const;
115 
116   /// Targets should implement this method to assign a section to globals with
117   /// an explicit section specfied. The implementation of this method can
118   /// assume that GV->hasSection() is true.
119   virtual const MCSection *
120   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
121                            Mangler &Mang, const TargetMachine &TM) const = 0;
122 
123   /// Allow the target to completely override section assignment of a global.
getSpecialCasedSectionGlobals(const GlobalValue * GV,SectionKind Kind,Mangler & Mang)124   virtual const MCSection *getSpecialCasedSectionGlobals(const GlobalValue *GV,
125                                                          SectionKind Kind,
126                                                          Mangler &Mang) const {
127     return nullptr;
128   }
129 
130   /// Return an MCExpr to use for a reference to the specified global variable
131   /// from exception handling information.
132   virtual const MCExpr *
133   getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
134                           Mangler &Mang, const TargetMachine &TM,
135                           MachineModuleInfo *MMI, MCStreamer &Streamer) const;
136 
137   /// Return the MCSymbol for a private symbol with global value name as its
138   /// base, with the specified suffix.
139   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
140                                          StringRef Suffix, Mangler &Mang,
141                                          const TargetMachine &TM) const;
142 
143   // The symbol that gets passed to .cfi_personality.
144   virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
145                                             Mangler &Mang,
146                                             const TargetMachine &TM,
147                                             MachineModuleInfo *MMI) const;
148 
149   const MCExpr *
150   getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
151                     MCStreamer &Streamer) const;
152 
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym)153   virtual const MCSection *getStaticCtorSection(unsigned Priority,
154                                                 const MCSymbol *KeySym) const {
155     return StaticCtorSection;
156   }
157 
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym)158   virtual const MCSection *getStaticDtorSection(unsigned Priority,
159                                                 const MCSymbol *KeySym) const {
160     return StaticDtorSection;
161   }
162 
163   /// \brief Create a symbol reference to describe the given TLS variable when
164   /// emitting the address in debug info.
165   virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
166 
167   virtual const MCExpr *
getExecutableRelativeSymbol(const ConstantExpr * CE,Mangler & Mang,const TargetMachine & TM)168   getExecutableRelativeSymbol(const ConstantExpr *CE, Mangler &Mang,
169                               const TargetMachine &TM) const {
170     return nullptr;
171   }
172 
173   /// \brief Target supports replacing a data "PC"-relative access to a symbol
174   /// through another symbol, by accessing the later via a GOT entry instead?
supportIndirectSymViaGOTPCRel()175   bool supportIndirectSymViaGOTPCRel() const {
176     return SupportIndirectSymViaGOTPCRel;
177   }
178 
179   /// \brief Target GOT "PC"-relative relocation supports encoding an additional
180   /// binary expression with an offset?
supportGOTPCRelWithOffset()181   bool supportGOTPCRelWithOffset() const {
182     return SupportGOTPCRelWithOffset;
183   }
184 
185   /// \brief Get the target specific PC relative GOT entry relocation
getIndirectSymViaGOTPCRel(const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer)186   virtual const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
187                                                   const MCValue &MV,
188                                                   int64_t Offset,
189                                                   MachineModuleInfo *MMI,
190                                                   MCStreamer &Streamer) const {
191     return nullptr;
192   }
193 
194 protected:
195   virtual const MCSection *
196   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
197                          Mangler &Mang, const TargetMachine &TM) const = 0;
198 };
199 
200 } // end namespace llvm
201 
202 #endif
203