LLVM 22.0.0git
TargetLoweringObjectFile.h
Go to the documentation of this file.
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
18#include "llvm/MC/MCRegister.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
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:
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 /// Emit pseudo_probe_desc metadata.
97 void emitPseudoProbeDescMetadata(MCStreamer &Streamer, Module &M) const;
98
99 /// Process linker options metadata and emit platform-specific bits.
100 virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const {}
101
102 /// Get the module-level metadata that the platform cares about.
103 virtual void getModuleMetadata(Module &M) {}
104
105 /// Given a constant with the SectionKind, return a section that it should be
106 /// placed in.
107 virtual MCSection *getSectionForConstant(const DataLayout &DL,
108 SectionKind Kind, const Constant *C,
109 Align &Alignment) const;
110
111 /// Similar to the function above, but append \p SectionSuffix to the section
112 /// name.
113 virtual MCSection *getSectionForConstant(const DataLayout &DL,
114 SectionKind Kind, const Constant *C,
115 Align &Alignment,
116 StringRef SectionSuffix) const;
117
118 virtual MCSection *
119 getSectionForMachineBasicBlock(const Function &F,
120 const MachineBasicBlock &MBB,
121 const TargetMachine &TM) const;
122
123 virtual MCSection *
124 getUniqueSectionForFunction(const Function &F,
125 const TargetMachine &TM) const;
126
127 /// Classify the specified global variable into a set of target independent
128 /// categories embodied in SectionKind.
129 static SectionKind getKindForGlobal(const GlobalObject *GO,
130 const TargetMachine &TM);
131
132 /// This method computes the appropriate section to emit the specified global
133 /// variable or function definition. This should not be passed external (or
134 /// available externally) globals.
135 MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
136 const TargetMachine &TM) const;
137
138 /// This method computes the appropriate section to emit the specified global
139 /// variable or function definition. This should not be passed external (or
140 /// available externally) globals.
141 MCSection *SectionForGlobal(const GlobalObject *GO,
142 const TargetMachine &TM) const;
143
144 virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName,
145 const GlobalValue *GV,
146 const TargetMachine &TM) const;
147
148 virtual MCSection *getSectionForJumpTable(const Function &F,
149 const TargetMachine &TM) const;
150 virtual MCSection *
151 getSectionForJumpTable(const Function &F, const TargetMachine &TM,
152 const MachineJumpTableEntry *JTE) const;
153
154 virtual MCSection *getSectionForLSDA(const Function &, const MCSymbol &,
155 const TargetMachine &) const {
156 return LSDASection;
157 }
158
159 virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
160 const Function &F) const;
161
162 /// Targets should implement this method to assign a section to globals with
163 /// an explicit section specfied. The implementation of this method can
164 /// assume that GO->hasSection() is true.
165 virtual MCSection *
167 const TargetMachine &TM) const = 0;
168
169 /// Return an MCExpr to use for a reference to the specified global variable
170 /// from exception handling information.
171 virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
172 unsigned Encoding,
173 const TargetMachine &TM,
175 MCStreamer &Streamer) const;
176
177 /// Return the MCSymbol for a private symbol with global value name as its
178 /// base, with the specified suffix.
179 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
180 StringRef Suffix,
181 const TargetMachine &TM) const;
182
183 // The symbol that gets passed to .cfi_personality.
184 virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
185 const TargetMachine &TM,
186 MachineModuleInfo *MMI) const;
187
188 unsigned getPersonalityEncoding() const { return PersonalityEncoding; }
189 unsigned getLSDAEncoding() const { return LSDAEncoding; }
190 unsigned getTTypeEncoding() const { return TTypeEncoding; }
191 unsigned getCallSiteEncoding() const;
192
193 const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
194 MCStreamer &Streamer) const;
195
196 virtual MCSection *getStaticCtorSection(unsigned Priority,
197 const MCSymbol *KeySym) const {
198 return StaticCtorSection;
199 }
200
201 virtual MCSection *getStaticDtorSection(unsigned Priority,
202 const MCSymbol *KeySym) const {
203 return StaticDtorSection;
204 }
205
206 /// Create a symbol reference to describe the given TLS variable when
207 /// emitting the address in debug info.
208 virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;
209
211 const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend,
212 std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
213 return nullptr;
214 }
215
216 /// Target supports a PC-relative relocation that references the PLT of a
217 /// function.
218 bool hasPLTPCRelative() const { return PLTPCRelativeSpecifier; }
219
221 const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend,
222 std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
223 return nullptr;
224 }
225
226 /// Target supports replacing a data "PC"-relative access to a symbol
227 /// through another symbol, by accessing the later via a GOT entry instead?
229 return SupportIndirectSymViaGOTPCRel;
230 }
231
232 /// Target GOT "PC"-relative relocation supports encoding an additional
233 /// binary expression with an offset?
235 return SupportGOTPCRelWithOffset;
236 }
237
238 /// Target supports TLS offset relocation in debug section?
240 return SupportDebugThreadLocalLocation;
241 }
242
243 /// Returns the register used as static base in RWPI variants.
244 virtual MCRegister getStaticBase() const { return MCRegister::NoRegister; }
245
246 /// Get the target specific RWPI relocation.
247 virtual const MCExpr *getIndirectSymViaRWPI(const MCSymbol *Sym) const {
248 return nullptr;
249 }
250
251 /// Get the target specific PC relative GOT entry relocation
253 const MCSymbol *Sym,
254 const MCValue &MV,
255 int64_t Offset,
257 MCStreamer &Streamer) const {
258 return nullptr;
259 }
260
261 /// If supported, return the section to use for the llvm.commandline
262 /// metadata. Otherwise, return nullptr.
264 return nullptr;
265 }
266
267 /// On targets that use separate function descriptor symbols, return a section
268 /// for the descriptor given its symbol. Use only with defined functions.
269 virtual MCSection *
271 const TargetMachine &TM) const {
272 return nullptr;
273 }
274
275 /// On targets that support TOC entries, return a section for the entry given
276 /// the symbol it refers to.
277 /// TODO: Implement this interface for existing ELF targets.
279 const TargetMachine &TM) const {
280 return nullptr;
281 }
282
283 /// On targets that associate external references with a section, return such
284 /// a section for the given external global.
285 virtual MCSection *
287 const TargetMachine &TM) const {
288 return nullptr;
289 }
290
291 /// Targets that have a special convention for their symbols could use
292 /// this hook to return a specialized symbol.
294 const TargetMachine &TM) const {
295 return nullptr;
296 }
297
298 /// If supported, return the function entry point symbol.
299 /// Otherwise, returns nullptr.
300 /// Func must be a function or an alias which has a function as base object.
302 const TargetMachine &TM) const {
303 return nullptr;
304 }
305
306protected:
308 SectionKind Kind,
309 const TargetMachine &TM) const = 0;
310};
311
312} // end namespace llvm
313
314#endif // LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Align
uint64_t Offset
Definition: ELF_riscv.cpp:478
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
Value * RHS
Value * LHS
This is an important base class in LLVM.
Definition: Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
Streaming machine code generation interface.
Definition: MCStreamer.h:220
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:190
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
This class contains meta information specific to a module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
virtual const MCExpr * lowerDSOLocalEquivalent(const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend, std::optional< int64_t > PCRelativeOffset, const TargetMachine &TM) const
virtual MCRegister getStaticBase() const
Returns the register used as static base in RWPI variants.
virtual MCSection * getSectionForCommandLines() const
If supported, return the section to use for the llvm.commandline metadata.
virtual MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) const
virtual const MCExpr * getIndirectSymViaGOTPCRel(const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const
Get the target specific PC relative GOT entry relocation.
TargetLoweringObjectFile & operator=(const TargetLoweringObjectFile &)=delete
virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const
Emit the module-level metadata that the platform cares about.
virtual MCSection * getSectionForExternalReference(const GlobalObject *GO, const TargetMachine &TM) const
On targets that associate external references with a section, return such a section for the given ext...
bool hasPLTPCRelative() const
Target supports a PC-relative relocation that references the PLT of a function.
virtual void getModuleMetadata(Module &M)
Get the module-level metadata that the platform cares about.
bool supportDebugThreadLocalLocation() const
Target supports TLS offset relocation in debug section?
virtual MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const =0
virtual const MCExpr * getIndirectSymViaRWPI(const MCSymbol *Sym) const
Get the target specific RWPI relocation.
virtual const MCExpr * lowerRelativeReference(const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend, std::optional< int64_t > PCRelativeOffset, const TargetMachine &TM) const
virtual MCSection * getStaticCtorSection(unsigned Priority, const MCSymbol *KeySym) const
virtual MCSection * getSectionForTOCEntry(const MCSymbol *S, const TargetMachine &TM) const
On targets that support TOC entries, return a section for the entry given the symbol it refers to.
bool supportGOTPCRelWithOffset() const
Target GOT "PC"-relative relocation supports encoding an additional binary expression with an offset?
virtual MCSection * getSectionForLSDA(const Function &, const MCSymbol &, const TargetMachine &) const
bool supportIndirectSymViaGOTPCRel() const
Target supports replacing a data "PC"-relative access to a symbol through another symbol,...
virtual MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const =0
Targets should implement this method to assign a section to globals with an explicit section specfied...
virtual MCSymbol * getFunctionEntryPointSymbol(const GlobalValue *Func, const TargetMachine &TM) const
If supported, return the function entry point symbol.
virtual MCSection * getSectionForFunctionDescriptor(const Function *F, const TargetMachine &TM) const
On targets that use separate function descriptor symbols, return a section for the descriptor given i...
virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const
Process linker options metadata and emit platform-specific bits.
TargetLoweringObjectFile(const TargetLoweringObjectFile &)=delete
virtual MCSymbol * getTargetSymbol(const GlobalValue *GV, const TargetMachine &TM) const
Targets that have a special convention for their symbols could use this hook to return a specialized ...
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:83
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
MachineJumpTableEntry - One jump table in the jump table info.