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_CODEGEN_TARGETLOWERINGOBJECTFILE_H
16 #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/SectionKind.h"
23 #include <cstdint>
24 
25 namespace llvm {
26 
27 class GlobalValue;
28 class MachineModuleInfo;
29 class Mangler;
30 class MCContext;
31 class MCExpr;
32 class MCSection;
33 class MCSymbol;
34 class MCSymbolRefExpr;
35 class MCStreamer;
36 class MCValue;
37 class TargetMachine;
38 
39 class TargetLoweringObjectFile : public MCObjectFileInfo {
40   MCContext *Ctx = nullptr;
41 
42   /// Name-mangler for global names.
43   Mangler *Mang = nullptr;
44 
45 protected:
46   bool SupportIndirectSymViaGOTPCRel = false;
47   bool SupportGOTPCRelWithOffset = true;
48   bool SupportDebugThreadLocalLocation = true;
49 
50   /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
51   /// for EH.
52   unsigned PersonalityEncoding = 0;
53   unsigned LSDAEncoding = 0;
54   unsigned TTypeEncoding = 0;
55 
56   /// This section contains the static constructor pointer list.
57   MCSection *StaticCtorSection = nullptr;
58 
59   /// This section contains the static destructor pointer list.
60   MCSection *StaticDtorSection = nullptr;
61 
62 public:
63   TargetLoweringObjectFile() = default;
64   TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
65   TargetLoweringObjectFile &
66   operator=(const TargetLoweringObjectFile &) = delete;
67   virtual ~TargetLoweringObjectFile();
68 
getContext()69   MCContext &getContext() const { return *Ctx; }
getMangler()70   Mangler &getMangler() const { return *Mang; }
71 
72   /// This method must be called before any actual lowering is done.  This
73   /// specifies the current context for codegen, and gives the lowering
74   /// implementations a chance to set up their default sections.
75   virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
76 
77   virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
78                                     const MCSymbol *Sym) const;
79 
80   /// Emit the module-level metadata that the platform cares about.
emitModuleMetadata(MCStreamer & Streamer,Module & M)81   virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
82 
83   /// Given a constant with the SectionKind, return a section that it should be
84   /// placed in.
85   virtual MCSection *getSectionForConstant(const DataLayout &DL,
86                                            SectionKind Kind,
87                                            const Constant *C,
88                                            unsigned &Align) const;
89 
90   /// Classify the specified global variable into a set of target independent
91   /// categories embodied in SectionKind.
92   static SectionKind getKindForGlobal(const GlobalObject *GO,
93                                       const TargetMachine &TM);
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.
98   MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
99                               const TargetMachine &TM) const;
100 
101   /// This method computes the appropriate section to emit the specified global
102   /// variable or function definition. This should not be passed external (or
103   /// available externally) globals.
SectionForGlobal(const GlobalObject * GO,const TargetMachine & TM)104   MCSection *SectionForGlobal(const GlobalObject *GO,
105                               const TargetMachine &TM) const {
106     return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
107   }
108 
109   virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
110                                  const GlobalValue *GV,
111                                  const TargetMachine &TM) const;
112 
113   virtual MCSection *getSectionForJumpTable(const Function &F,
114                                             const TargetMachine &TM) const;
115 
116   virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
117                                                    const Function &F) const;
118 
119   /// Targets should implement this method to assign a section to globals with
120   /// an explicit section specfied. The implementation of this method can
121   /// assume that GO->hasSection() is true.
122   virtual MCSection *
123   getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
124                            const TargetMachine &TM) const = 0;
125 
126   /// Return an MCExpr to use for a reference to the specified global variable
127   /// from exception handling information.
128   virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
129                                                 unsigned Encoding,
130                                                 const TargetMachine &TM,
131                                                 MachineModuleInfo *MMI,
132                                                 MCStreamer &Streamer) const;
133 
134   /// Return the MCSymbol for a private symbol with global value name as its
135   /// base, with the specified suffix.
136   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
137                                          StringRef Suffix,
138                                          const TargetMachine &TM) const;
139 
140   // The symbol that gets passed to .cfi_personality.
141   virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
142                                             const TargetMachine &TM,
143                                             MachineModuleInfo *MMI) const;
144 
getPersonalityEncoding()145   unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
getLSDAEncoding()146   unsigned getLSDAEncoding() const { return LSDAEncoding; }
getTTypeEncoding()147   unsigned getTTypeEncoding() const { return TTypeEncoding; }
148 
149   const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
150                                   MCStreamer &Streamer) const;
151 
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym)152   virtual MCSection *getStaticCtorSection(unsigned Priority,
153                                           const MCSymbol *KeySym) const {
154     return StaticCtorSection;
155   }
156 
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym)157   virtual MCSection *getStaticDtorSection(unsigned Priority,
158                                           const MCSymbol *KeySym) const {
159     return StaticDtorSection;
160   }
161 
162   /// Create a symbol reference to describe the given TLS variable when
163   /// emitting the address in debug info.
164   virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
165 
lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM)166   virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
167                                                const GlobalValue *RHS,
168                                                const TargetMachine &TM) const {
169     return nullptr;
170   }
171 
172   /// Target supports replacing a data "PC"-relative access to a symbol
173   /// through another symbol, by accessing the later via a GOT entry instead?
supportIndirectSymViaGOTPCRel()174   bool supportIndirectSymViaGOTPCRel() const {
175     return SupportIndirectSymViaGOTPCRel;
176   }
177 
178   /// Target GOT "PC"-relative relocation supports encoding an additional
179   /// binary expression with an offset?
supportGOTPCRelWithOffset()180   bool supportGOTPCRelWithOffset() const {
181     return SupportGOTPCRelWithOffset;
182   }
183 
184   /// Target supports TLS offset relocation in debug section?
supportDebugThreadLocalLocation()185   bool supportDebugThreadLocalLocation() const {
186     return SupportDebugThreadLocalLocation;
187   }
188 
189   /// Get the target specific PC relative GOT entry relocation
getIndirectSymViaGOTPCRel(const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer)190   virtual const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym,
191                                                   const MCValue &MV,
192                                                   int64_t Offset,
193                                                   MachineModuleInfo *MMI,
194                                                   MCStreamer &Streamer) const {
195     return nullptr;
196   }
197 
emitLinkerFlagsForGlobal(raw_ostream & OS,const GlobalValue * GV)198   virtual void emitLinkerFlagsForGlobal(raw_ostream &OS,
199                                         const GlobalValue *GV) const {}
200 
emitLinkerFlagsForUsed(raw_ostream & OS,const GlobalValue * GV)201   virtual void emitLinkerFlagsForUsed(raw_ostream &OS,
202                                       const GlobalValue *GV) const {}
203 
204   /// If supported, return the section to use for the llvm.commandline
205   /// metadata. Otherwise, return nullptr.
getSectionForCommandLines()206   virtual MCSection *getSectionForCommandLines() const {
207     return nullptr;
208   }
209 
210 protected:
211   virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
212                                             SectionKind Kind,
213                                             const TargetMachine &TM) const = 0;
214 };
215 
216 } // end namespace llvm
217 
218 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H
219