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&) LLVM_DELETED_FUNCTION;
42   void operator=(const TargetLoweringObjectFile&) LLVM_DELETED_FUNCTION;
43 
44 public:
getContext()45   MCContext &getContext() const { return *Ctx; }
46 
TargetLoweringObjectFile()47   TargetLoweringObjectFile() : MCObjectFileInfo(), Ctx(nullptr), DL(nullptr) {}
48 
49   virtual ~TargetLoweringObjectFile();
50 
51   /// This method must be called before any actual lowering is done.  This
52   /// specifies the current context for codegen, and gives the lowering
53   /// implementations a chance to set up their default sections.
54   virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
55 
56   virtual void emitPersonalityValue(MCStreamer &Streamer,
57                                     const TargetMachine &TM,
58                                     const MCSymbol *Sym) const;
59 
60   /// Extract the dependent library name from a linker option string. Returns
61   /// StringRef() if the option does not specify a library.
getDepLibFromLinkerOpt(StringRef LinkerOption)62   virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const {
63     return StringRef();
64   }
65 
66   /// Emit the module flags that the platform cares about.
emitModuleFlags(MCStreamer & Streamer,ArrayRef<Module::ModuleFlagEntry> Flags,Mangler & Mang,const TargetMachine & TM)67   virtual void emitModuleFlags(MCStreamer &Streamer,
68                                ArrayRef<Module::ModuleFlagEntry> Flags,
69                                Mangler &Mang, const TargetMachine &TM) const {}
70 
71   /// Given a constant with the SectionKind, return a section that it should be
72   /// placed in.
73   virtual const MCSection *getSectionForConstant(SectionKind Kind,
74                                                  const Constant *C) const;
75 
76   /// Classify the specified global variable into a set of target independent
77   /// categories embodied in SectionKind.
78   static SectionKind getKindForGlobal(const GlobalValue *GV,
79                                       const TargetMachine &TM);
80 
81   /// This method computes the appropriate section to emit the specified global
82   /// variable or function definition. This should not be passed external (or
83   /// available externally) globals.
84   const MCSection *SectionForGlobal(const GlobalValue *GV,
85                                     SectionKind Kind, Mangler &Mang,
86                                     const TargetMachine &TM) const;
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.
SectionForGlobal(const GlobalValue * GV,Mangler & Mang,const TargetMachine & TM)91   const MCSection *SectionForGlobal(const GlobalValue *GV,
92                                     Mangler &Mang,
93                                     const TargetMachine &TM) const {
94     return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM);
95   }
96 
97   /// Targets should implement this method to assign a section to globals with
98   /// an explicit section specfied. The implementation of this method can
99   /// assume that GV->hasSection() is true.
100   virtual const MCSection *
101   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
102                            Mangler &Mang, const TargetMachine &TM) const = 0;
103 
104   /// Allow the target to completely override section assignment of a global.
getSpecialCasedSectionGlobals(const GlobalValue * GV,SectionKind Kind,Mangler & Mang)105   virtual const MCSection *getSpecialCasedSectionGlobals(const GlobalValue *GV,
106                                                          SectionKind Kind,
107                                                          Mangler &Mang) const {
108     return nullptr;
109   }
110 
111   /// Return an MCExpr to use for a reference to the specified global variable
112   /// from exception handling information.
113   virtual const MCExpr *
114   getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
115                           Mangler &Mang, const TargetMachine &TM,
116                           MachineModuleInfo *MMI, MCStreamer &Streamer) const;
117 
118   /// Return the MCSymbol for a private symbol with global value name as its
119   /// base, with the specified suffix.
120   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
121                                          StringRef Suffix, Mangler &Mang,
122                                          const TargetMachine &TM) const;
123 
124   // The symbol that gets passed to .cfi_personality.
125   virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
126                                             Mangler &Mang,
127                                             const TargetMachine &TM,
128                                             MachineModuleInfo *MMI) const;
129 
130   const MCExpr *
131   getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
132                     MCStreamer &Streamer) const;
133 
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym)134   virtual const MCSection *getStaticCtorSection(unsigned Priority,
135                                                 const MCSymbol *KeySym) const {
136     return StaticCtorSection;
137   }
138 
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym)139   virtual const MCSection *getStaticDtorSection(unsigned Priority,
140                                                 const MCSymbol *KeySym) const {
141     return StaticDtorSection;
142   }
143 
144   /// \brief Create a symbol reference to describe the given TLS variable when
145   /// emitting the address in debug info.
146   virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
147 
148   virtual const MCExpr *
getExecutableRelativeSymbol(const ConstantExpr * CE,Mangler & Mang,const TargetMachine & TM)149   getExecutableRelativeSymbol(const ConstantExpr *CE, Mangler &Mang,
150                               const TargetMachine &TM) const {
151     return nullptr;
152   }
153 
154 protected:
155   virtual const MCSection *
156   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
157                          Mangler &Mang, const TargetMachine &TM) const = 0;
158 };
159 
160 } // end namespace llvm
161 
162 #endif
163