blob: ff27ceaeac359ff219dc9bedf71b72eb6c2e8ab4 [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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include "llvm/MC/MCObjectFileInfo.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#include <cstdint>
19
20namespace llvm {
21
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020022class Constant;
23class DataLayout;
24class Function;
25class GlobalObject;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026class GlobalValue;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020027class MachineBasicBlock;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010028class MachineModuleInfo;
29class Mangler;
30class MCContext;
31class MCExpr;
32class MCSection;
33class MCSymbol;
34class MCSymbolRefExpr;
35class MCStreamer;
36class MCValue;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020037class Module;
38class SectionKind;
39class StringRef;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040class TargetMachine;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041class DSOLocalEquivalent;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042
43class TargetLoweringObjectFile : public MCObjectFileInfo {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 /// Name-mangler for global names.
45 Mangler *Mang = nullptr;
46
47protected:
48 bool SupportIndirectSymViaGOTPCRel = false;
49 bool SupportGOTPCRelWithOffset = true;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 bool SupportDebugThreadLocalLocation = true;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020051 bool SupportDSOLocalEquivalentLowering = false;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052
53 /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
54 /// for EH.
55 unsigned PersonalityEncoding = 0;
56 unsigned LSDAEncoding = 0;
57 unsigned TTypeEncoding = 0;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020058 unsigned CallSiteEncoding = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059
60 /// This section contains the static constructor pointer list.
61 MCSection *StaticCtorSection = nullptr;
62
63 /// This section contains the static destructor pointer list.
64 MCSection *StaticDtorSection = nullptr;
65
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020066 const TargetMachine *TM = nullptr;
67
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068public:
69 TargetLoweringObjectFile() = default;
70 TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
71 TargetLoweringObjectFile &
72 operator=(const TargetLoweringObjectFile &) = delete;
73 virtual ~TargetLoweringObjectFile();
74
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 Mangler &getMangler() const { return *Mang; }
76
77 /// This method must be called before any actual lowering is done. This
78 /// specifies the current context for codegen, and gives the lowering
79 /// implementations a chance to set up their default sections.
80 virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
81
82 virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
83 const MCSymbol *Sym) const;
84
85 /// Emit the module-level metadata that the platform cares about.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020088 /// Emit Call Graph Profile metadata.
89 void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
90
Andrew Walbran3d2c1972020-04-07 12:24:26 +010091 /// Get the module-level metadata that the platform cares about.
92 virtual void getModuleMetadata(Module &M) {}
93
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 /// Given a constant with the SectionKind, return a section that it should be
95 /// placed in.
96 virtual MCSection *getSectionForConstant(const DataLayout &DL,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020097 SectionKind Kind, const Constant *C,
98 Align &Alignment) const;
99
100 virtual MCSection *
101 getSectionForMachineBasicBlock(const Function &F,
102 const MachineBasicBlock &MBB,
103 const TargetMachine &TM) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104
105 /// Classify the specified global variable into a set of target independent
106 /// categories embodied in SectionKind.
107 static SectionKind getKindForGlobal(const GlobalObject *GO,
108 const TargetMachine &TM);
109
110 /// This method computes the appropriate section to emit the specified global
111 /// variable or function definition. This should not be passed external (or
112 /// available externally) globals.
113 MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
114 const TargetMachine &TM) const;
115
116 /// This method computes the appropriate section to emit the specified global
117 /// variable or function definition. This should not be passed external (or
118 /// available externally) globals.
119 MCSection *SectionForGlobal(const GlobalObject *GO,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200120 const TargetMachine &TM) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121
122 virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
123 const GlobalValue *GV,
124 const TargetMachine &TM) const;
125
126 virtual MCSection *getSectionForJumpTable(const Function &F,
127 const TargetMachine &TM) const;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200128 virtual MCSection *getSectionForLSDA(const Function &F,
129 const TargetMachine &TM) const {
130 return LSDASection;
131 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132
133 virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
134 const Function &F) const;
135
136 /// Targets should implement this method to assign a section to globals with
137 /// an explicit section specfied. The implementation of this method can
138 /// assume that GO->hasSection() is true.
139 virtual MCSection *
140 getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
141 const TargetMachine &TM) const = 0;
142
143 /// Return an MCExpr to use for a reference to the specified global variable
144 /// from exception handling information.
145 virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
146 unsigned Encoding,
147 const TargetMachine &TM,
148 MachineModuleInfo *MMI,
149 MCStreamer &Streamer) const;
150
151 /// Return the MCSymbol for a private symbol with global value name as its
152 /// base, with the specified suffix.
153 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
154 StringRef Suffix,
155 const TargetMachine &TM) const;
156
157 // The symbol that gets passed to .cfi_personality.
158 virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
159 const TargetMachine &TM,
160 MachineModuleInfo *MMI) const;
161
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100162 unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
163 unsigned getLSDAEncoding() const { return LSDAEncoding; }
164 unsigned getTTypeEncoding() const { return TTypeEncoding; }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200165 unsigned getCallSiteEncoding() const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100166
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
168 MCStreamer &Streamer) const;
169
170 virtual MCSection *getStaticCtorSection(unsigned Priority,
171 const MCSymbol *KeySym) const {
172 return StaticCtorSection;
173 }
174
175 virtual MCSection *getStaticDtorSection(unsigned Priority,
176 const MCSymbol *KeySym) const {
177 return StaticDtorSection;
178 }
179
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100180 /// Create a symbol reference to describe the given TLS variable when
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100181 /// emitting the address in debug info.
182 virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
183
184 virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
185 const GlobalValue *RHS,
186 const TargetMachine &TM) const {
187 return nullptr;
188 }
189
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200190 /// Target supports a native lowering of a dso_local_equivalent constant
191 /// without needing to replace it with equivalent IR.
192 bool supportDSOLocalEquivalentLowering() const {
193 return SupportDSOLocalEquivalentLowering;
194 }
195
196 virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
197 const TargetMachine &TM) const {
198 return nullptr;
199 }
200
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100201 /// Target supports replacing a data "PC"-relative access to a symbol
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100202 /// through another symbol, by accessing the later via a GOT entry instead?
203 bool supportIndirectSymViaGOTPCRel() const {
204 return SupportIndirectSymViaGOTPCRel;
205 }
206
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100207 /// Target GOT "PC"-relative relocation supports encoding an additional
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 /// binary expression with an offset?
209 bool supportGOTPCRelWithOffset() const {
210 return SupportGOTPCRelWithOffset;
211 }
212
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100213 /// Target supports TLS offset relocation in debug section?
214 bool supportDebugThreadLocalLocation() const {
215 return SupportDebugThreadLocalLocation;
216 }
217
218 /// Get the target specific PC relative GOT entry relocation
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200219 virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
220 const MCSymbol *Sym,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100221 const MCValue &MV,
222 int64_t Offset,
223 MachineModuleInfo *MMI,
224 MCStreamer &Streamer) const {
225 return nullptr;
226 }
227
Andrew Walbran16937d02019-10-22 13:54:20 +0100228 /// If supported, return the section to use for the llvm.commandline
229 /// metadata. Otherwise, return nullptr.
230 virtual MCSection *getSectionForCommandLines() const {
231 return nullptr;
232 }
233
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200234 /// On targets that use separate function descriptor symbols, return a section
235 /// for the descriptor given its symbol. Use only with defined functions.
236 virtual MCSection *
237 getSectionForFunctionDescriptor(const Function *F,
238 const TargetMachine &TM) const {
239 return nullptr;
240 }
241
242 /// On targets that support TOC entries, return a section for the entry given
243 /// the symbol it refers to.
244 /// TODO: Implement this interface for existing ELF targets.
245 virtual MCSection *getSectionForTOCEntry(const MCSymbol *S,
246 const TargetMachine &TM) const {
247 return nullptr;
248 }
249
250 /// On targets that associate external references with a section, return such
251 /// a section for the given external global.
252 virtual MCSection *
253 getSectionForExternalReference(const GlobalObject *GO,
254 const TargetMachine &TM) const {
255 return nullptr;
256 }
257
258 /// Targets that have a special convention for their symbols could use
259 /// this hook to return a specialized symbol.
260 virtual MCSymbol *getTargetSymbol(const GlobalValue *GV,
261 const TargetMachine &TM) const {
262 return nullptr;
263 }
264
265 /// If supported, return the function entry point symbol.
266 /// Otherwise, returns nulltpr.
267 /// Func must be a function or an alias which has a function as base object.
268 virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
269 const TargetMachine &TM) const {
270 return nullptr;
271 }
272
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100273protected:
274 virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
275 SectionKind Kind,
276 const TargetMachine &TM) const = 0;
277};
278
279} // end namespace llvm
280
281#endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H