1//===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements classes used to handle lowerings specific to common
10// object file formats.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
15#define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16
17#include "llvm/MC/MCObjectFileInfo.h"
18#include "llvm/MC/MCRegister.h"
19#include "llvm/Support/Compiler.h"
20#include <cstdint>
21
22namespace llvm {
23
24struct Align;
25struct MachineJumpTableEntry;
26class Constant;
27class DataLayout;
28class Function;
29class GlobalObject;
30class GlobalValue;
31class MachineBasicBlock;
32class MachineModuleInfo;
33class Mangler;
34class MCContext;
35class MCExpr;
36class MCSection;
37class MCSymbol;
38class MCSymbolRefExpr;
39class MCStreamer;
40class MCValue;
41class Module;
42class SectionKind;
43class StringRef;
44class TargetMachine;
45class DSOLocalEquivalent;
46
47class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
48 /// Name-mangler for global names.
49 Mangler *Mang = nullptr;
50
51protected:
52 bool SupportIndirectSymViaGOTPCRel = false;
53 bool SupportGOTPCRelWithOffset = true;
54 bool SupportDebugThreadLocalLocation = true;
55 uint32_t PLTPCRelativeSpecifier = 0;
56
57 /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values
58 /// for EH.
59 unsigned PersonalityEncoding = 0;
60 unsigned LSDAEncoding = 0;
61 unsigned TTypeEncoding = 0;
62 unsigned CallSiteEncoding = 0;
63
64 /// This section contains the static constructor pointer list.
65 MCSection *StaticCtorSection = nullptr;
66
67 /// This section contains the static destructor pointer list.
68 MCSection *StaticDtorSection = nullptr;
69
70 const TargetMachine *TM = nullptr;
71
72public:
73 TargetLoweringObjectFile() = default;
74 TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
75 TargetLoweringObjectFile &
76 operator=(const TargetLoweringObjectFile &) = delete;
77 virtual ~TargetLoweringObjectFile();
78
79 Mangler &getMangler() const { return *Mang; }
80
81 /// This method must be called before any actual lowering is done. This
82 /// specifies the current context for codegen, and gives the lowering
83 /// implementations a chance to set up their default sections.
84 virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
85
86 virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
87 const MCSymbol *Sym,
88 const MachineModuleInfo *MMI) const;
89
90 /// Emit the module-level metadata that the platform cares about.
91 virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
92
93 /// Emit Call Graph Profile metadata.
94 void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
95
96 /// Process linker options metadata and emit platform-specific bits.
97 virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const {}
98
99 /// Get the module-level metadata that the platform cares about.
100 virtual void getModuleMetadata(Module &M) {}
101
102 /// Given a constant with the SectionKind, return a section that it should be
103 /// placed in.
104 virtual MCSection *getSectionForConstant(const DataLayout &DL,
105 SectionKind Kind, const Constant *C,
106 Align &Alignment) const;
107
108 /// Similar to the function above, but append \p SectionSuffix to the section
109 /// name.
110 virtual MCSection *getSectionForConstant(const DataLayout &DL,
111 SectionKind Kind, const Constant *C,
112 Align &Alignment,
113 StringRef SectionSuffix) const;
114
115 virtual MCSection *
116 getSectionForMachineBasicBlock(const Function &F,
117 const MachineBasicBlock &MBB,
118 const TargetMachine &TM) const;
119
120 virtual MCSection *
121 getUniqueSectionForFunction(const Function &F,
122 const TargetMachine &TM) const;
123
124 /// Classify the specified global variable into a set of target independent
125 /// categories embodied in SectionKind.
126 static SectionKind getKindForGlobal(const GlobalObject *GO,
127 const TargetMachine &TM);
128
129 /// This method computes the appropriate section to emit the specified global
130 /// variable or function definition. This should not be passed external (or
131 /// available externally) globals.
132 MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
133 const TargetMachine &TM) const;
134
135 /// This method computes the appropriate section to emit the specified global
136 /// variable or function definition. This should not be passed external (or
137 /// available externally) globals.
138 MCSection *SectionForGlobal(const GlobalObject *GO,
139 const TargetMachine &TM) const;
140
141 virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
142 const GlobalValue *GV,
143 const TargetMachine &TM) const;
144
145 virtual MCSection *getSectionForJumpTable(const Function &F,
146 const TargetMachine &TM) const;
147 virtual MCSection *
148 getSectionForJumpTable(const Function &F, const TargetMachine &TM,
149 const MachineJumpTableEntry *JTE) const;
150
151 virtual MCSection *getSectionForLSDA(const Function &, const MCSymbol &,
152 const TargetMachine &) const {
153 return LSDASection;
154 }
155
156 virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
157 const Function &F) const;
158
159 /// Targets should implement this method to assign a section to globals with
160 /// an explicit section specfied. The implementation of this method can
161 /// assume that GO->hasSection() is true.
162 virtual MCSection *
163 getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
164 const TargetMachine &TM) const = 0;
165
166 /// Return an MCExpr to use for a reference to the specified global variable
167 /// from exception handling information.
168 virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
169 unsigned Encoding,
170 const TargetMachine &TM,
171 MachineModuleInfo *MMI,
172 MCStreamer &Streamer) const;
173
174 /// Return the MCSymbol for a private symbol with global value name as its
175 /// base, with the specified suffix.
176 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
177 StringRef Suffix,
178 const TargetMachine &TM) const;
179
180 // The symbol that gets passed to .cfi_personality.
181 virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
182 const TargetMachine &TM,
183 MachineModuleInfo *MMI) const;
184
185 unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
186 unsigned getLSDAEncoding() const { return LSDAEncoding; }
187 unsigned getTTypeEncoding() const { return TTypeEncoding; }
188 unsigned getCallSiteEncoding() const;
189
190 const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
191 MCStreamer &Streamer) const;
192
193 virtual MCSection *getStaticCtorSection(unsigned Priority,
194 const MCSymbol *KeySym) const {
195 return StaticCtorSection;
196 }
197
198 virtual MCSection *getStaticDtorSection(unsigned Priority,
199 const MCSymbol *KeySym) const {
200 return StaticDtorSection;
201 }
202
203 /// Create a symbol reference to describe the given TLS variable when
204 /// emitting the address in debug info.
205 virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
206
207 virtual const MCExpr *lowerRelativeReference(
208 const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend,
209 std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
210 return nullptr;
211 }
212
213 /// Target supports a PC-relative relocation that references the PLT of a
214 /// function.
215 bool hasPLTPCRelative() const { return PLTPCRelativeSpecifier; }
216
217 virtual const MCExpr *lowerDSOLocalEquivalent(
218 const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend,
219 std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
220 return nullptr;
221 }
222
223 /// Target supports replacing a data "PC"-relative access to a symbol
224 /// through another symbol, by accessing the later via a GOT entry instead?
225 bool supportIndirectSymViaGOTPCRel() const {
226 return SupportIndirectSymViaGOTPCRel;
227 }
228
229 /// Target GOT "PC"-relative relocation supports encoding an additional
230 /// binary expression with an offset?
231 bool supportGOTPCRelWithOffset() const {
232 return SupportGOTPCRelWithOffset;
233 }
234
235 /// Target supports TLS offset relocation in debug section?
236 bool supportDebugThreadLocalLocation() const {
237 return SupportDebugThreadLocalLocation;
238 }
239
240 /// Returns the register used as static base in RWPI variants.
241 virtual MCRegister getStaticBase() const { return MCRegister::NoRegister; }
242
243 /// Get the target specific RWPI relocation.
244 virtual const MCExpr *getIndirectSymViaRWPI(const MCSymbol *Sym) const {
245 return nullptr;
246 }
247
248 /// Get the target specific PC relative GOT entry relocation
249 virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
250 const MCSymbol *Sym,
251 const MCValue &MV,
252 int64_t Offset,
253 MachineModuleInfo *MMI,
254 MCStreamer &Streamer) const {
255 return nullptr;
256 }
257
258 /// If supported, return the section to use for the llvm.commandline
259 /// metadata. Otherwise, return nullptr.
260 virtual MCSection *getSectionForCommandLines() const {
261 return nullptr;
262 }
263
264 /// On targets that use separate function descriptor symbols, return a section
265 /// for the descriptor given its symbol. Use only with defined functions.
266 virtual MCSection *
267 getSectionForFunctionDescriptor(const Function *F,
268 const TargetMachine &TM) const {
269 return nullptr;
270 }
271
272 /// On targets that support TOC entries, return a section for the entry given
273 /// the symbol it refers to.
274 /// TODO: Implement this interface for existing ELF targets.
275 virtual MCSection *getSectionForTOCEntry(const MCSymbol *S,
276 const TargetMachine &TM) const {
277 return nullptr;
278 }
279
280 /// On targets that associate external references with a section, return such
281 /// a section for the given external global.
282 virtual MCSection *
283 getSectionForExternalReference(const GlobalObject *GO,
284 const TargetMachine &TM) const {
285 return nullptr;
286 }
287
288 /// Targets that have a special convention for their symbols could use
289 /// this hook to return a specialized symbol.
290 virtual MCSymbol *getTargetSymbol(const GlobalValue *GV,
291 const TargetMachine &TM) const {
292 return nullptr;
293 }
294
295 /// If supported, return the function entry point symbol.
296 /// Otherwise, returns nullptr.
297 /// Func must be a function or an alias which has a function as base object.
298 virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
299 const TargetMachine &TM) const {
300 return nullptr;
301 }
302
303protected:
304 virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
305 SectionKind Kind,
306 const TargetMachine &TM) const = 0;
307};
308
309} // end namespace llvm
310
311#endif // LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
312