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