[AsmPrinter] Access pointers to globals via pcrel GOT entries
[oota-llvm.git] / include / llvm / Target / TargetLoweringObjectFile.h
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 ConstantExpr;
33   class GlobalValue;
34   class TargetMachine;
35
36 class TargetLoweringObjectFile : public MCObjectFileInfo {
37   MCContext *Ctx;
38   const DataLayout *DL;
39
40   TargetLoweringObjectFile(
41     const TargetLoweringObjectFile&) = delete;
42   void operator=(const TargetLoweringObjectFile&) = delete;
43
44 protected:
45   bool SupportIndirectSymViaGOTPCRel;
46
47 public:
48   MCContext &getContext() const { return *Ctx; }
49
50   TargetLoweringObjectFile() : MCObjectFileInfo(), Ctx(nullptr), DL(nullptr),
51                                SupportIndirectSymViaGOTPCRel(false) {}
52
53   virtual ~TargetLoweringObjectFile();
54
55   /// This method must be called before any actual lowering is done.  This
56   /// specifies the current context for codegen, and gives the lowering
57   /// implementations a chance to set up their default sections.
58   virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
59
60   virtual void emitPersonalityValue(MCStreamer &Streamer,
61                                     const TargetMachine &TM,
62                                     const MCSymbol *Sym) const;
63
64   /// Extract the dependent library name from a linker option string. Returns
65   /// StringRef() if the option does not specify a library.
66   virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const {
67     return StringRef();
68   }
69
70   /// Emit the module flags that the platform cares about.
71   virtual void emitModuleFlags(MCStreamer &Streamer,
72                                ArrayRef<Module::ModuleFlagEntry> Flags,
73                                Mangler &Mang, const TargetMachine &TM) const {}
74
75   /// Given a constant with the SectionKind, return a section that it should be
76   /// placed in.
77   virtual const MCSection *getSectionForConstant(SectionKind Kind,
78                                                  const Constant *C) const;
79
80   /// Classify the specified global variable into a set of target independent
81   /// categories embodied in SectionKind.
82   static SectionKind getKindForGlobal(const GlobalValue *GV,
83                                       const TargetMachine &TM);
84
85   /// This method computes the appropriate section to emit the specified global
86   /// variable or function definition. This should not be passed external (or
87   /// available externally) globals.
88   const MCSection *SectionForGlobal(const GlobalValue *GV,
89                                     SectionKind Kind, Mangler &Mang,
90                                     const TargetMachine &TM) const;
91
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   const MCSection *SectionForGlobal(const GlobalValue *GV,
96                                     Mangler &Mang,
97                                     const TargetMachine &TM) const {
98     return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM);
99   }
100
101   virtual const MCSection *
102   getSectionForJumpTable(const Function &F, Mangler &Mang,
103                          const TargetMachine &TM) const;
104
105   virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
106                                                    const Function &F) const;
107
108   /// Targets should implement this method to assign a section to globals with
109   /// an explicit section specfied. The implementation of this method can
110   /// assume that GV->hasSection() is true.
111   virtual const MCSection *
112   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
113                            Mangler &Mang, const TargetMachine &TM) const = 0;
114
115   /// Allow the target to completely override section assignment of a global.
116   virtual const MCSection *getSpecialCasedSectionGlobals(const GlobalValue *GV,
117                                                          SectionKind Kind,
118                                                          Mangler &Mang) const {
119     return nullptr;
120   }
121
122   /// Return an MCExpr to use for a reference to the specified global variable
123   /// from exception handling information.
124   virtual const MCExpr *
125   getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
126                           Mangler &Mang, const TargetMachine &TM,
127                           MachineModuleInfo *MMI, MCStreamer &Streamer) const;
128
129   /// Return the MCSymbol for a private symbol with global value name as its
130   /// base, with the specified suffix.
131   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
132                                          StringRef Suffix, Mangler &Mang,
133                                          const TargetMachine &TM) const;
134
135   // The symbol that gets passed to .cfi_personality.
136   virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
137                                             Mangler &Mang,
138                                             const TargetMachine &TM,
139                                             MachineModuleInfo *MMI) const;
140
141   const MCExpr *
142   getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
143                     MCStreamer &Streamer) const;
144
145   virtual const MCSection *getStaticCtorSection(unsigned Priority,
146                                                 const MCSymbol *KeySym) const {
147     return StaticCtorSection;
148   }
149
150   virtual const MCSection *getStaticDtorSection(unsigned Priority,
151                                                 const MCSymbol *KeySym) const {
152     return StaticDtorSection;
153   }
154
155   /// \brief Create a symbol reference to describe the given TLS variable when
156   /// emitting the address in debug info.
157   virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
158
159   virtual const MCExpr *
160   getExecutableRelativeSymbol(const ConstantExpr *CE, Mangler &Mang,
161                               const TargetMachine &TM) const {
162     return nullptr;
163   }
164
165   /// \brief Target supports replacing a data "PC"-relative access to a symbol
166   /// through another symbol, by accessing the later via a GOT entry instead?
167   bool supportIndirectSymViaGOTPCRel() const {
168     return SupportIndirectSymViaGOTPCRel;
169   }
170
171   /// \brief Get the target specific PC relative GOT entry relocation
172   virtual const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
173                                                   int64_t Offset) const {
174     return nullptr;
175   }
176
177 protected:
178   virtual const MCSection *
179   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
180                          Mangler &Mang, const TargetMachine &TM) const = 0;
181 };
182
183 } // end namespace llvm
184
185 #endif