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