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