blob: 31e08b7d1e63418788efff34fb3866dd1b3ddaf6 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//==- llvm/CodeGen/TargetLoweringObjectFileImpl.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_TARGETLOWERINGOBJECTFILEIMPL_H
15#define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
16
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020017#include "llvm/BinaryFormat/XCOFF.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#include "llvm/Target/TargetLoweringObjectFile.h"
19
20namespace llvm {
21
22class GlobalValue;
23class MachineModuleInfo;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020024class MachineFunction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025class MCContext;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020026class MCExpr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027class MCSection;
28class MCSymbol;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020029class Module;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030class TargetMachine;
31
32class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
33 bool UseInitArray = false;
34 mutable unsigned NextUniqueID = 1; // ID 0 is reserved for execute-only sections
35
36protected:
37 MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =
38 MCSymbolRefExpr::VK_None;
39
40public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 TargetLoweringObjectFileELF();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 ~TargetLoweringObjectFileELF() override = default;
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 /// Emit Obj-C garbage collection and linker options.
47 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
48
49 void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 const MCSymbol *Sym) const override;
51
52 /// Given a constant with the SectionKind, return a section that it should be
53 /// placed in.
54 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
55 const Constant *C,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020056 Align &Alignment) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057
58 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
59 const TargetMachine &TM) const override;
60
61 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
62 const TargetMachine &TM) const override;
63
64 MCSection *getSectionForJumpTable(const Function &F,
65 const TargetMachine &TM) const override;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020066 MCSection *getSectionForLSDA(const Function &F,
67 const TargetMachine &TM) const override;
68
69 MCSection *
70 getSectionForMachineBasicBlock(const Function &F,
71 const MachineBasicBlock &MBB,
72 const TargetMachine &TM) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073
74 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
75 const Function &F) const override;
76
77 /// Return an MCExpr to use for a reference to the specified type info global
78 /// variable from exception handling information.
79 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
80 unsigned Encoding,
81 const TargetMachine &TM,
82 MachineModuleInfo *MMI,
83 MCStreamer &Streamer) const override;
84
85 // The symbol that gets passed to .cfi_personality.
86 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
87 const TargetMachine &TM,
88 MachineModuleInfo *MMI) const override;
89
90 void InitializeELF(bool UseInitArray_);
91 MCSection *getStaticCtorSection(unsigned Priority,
92 const MCSymbol *KeySym) const override;
93 MCSection *getStaticDtorSection(unsigned Priority,
94 const MCSymbol *KeySym) const override;
95
96 const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
97 const GlobalValue *RHS,
98 const TargetMachine &TM) const override;
Andrew Walbran16937d02019-10-22 13:54:20 +010099
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200100 const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
101 const TargetMachine &TM) const override;
102
Andrew Walbran16937d02019-10-22 13:54:20 +0100103 MCSection *getSectionForCommandLines() const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104};
105
106class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
107public:
108 TargetLoweringObjectFileMachO();
109 ~TargetLoweringObjectFileMachO() override = default;
110
111 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
112
113 /// Emit the module flags that specify the garbage collection information.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115
116 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
117 const TargetMachine &TM) const override;
118
119 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
120 const TargetMachine &TM) const override;
121
122 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
123 const Constant *C,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200124 Align &Alignment) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125
126 /// The mach-o version of this method defaults to returning a stub reference.
127 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
128 unsigned Encoding,
129 const TargetMachine &TM,
130 MachineModuleInfo *MMI,
131 MCStreamer &Streamer) const override;
132
133 // The symbol that gets passed to .cfi_personality.
134 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
135 const TargetMachine &TM,
136 MachineModuleInfo *MMI) const override;
137
138 /// Get MachO PC relative GOT entry relocation
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200139 const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
140 const MCSymbol *Sym,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100141 const MCValue &MV, int64_t Offset,
142 MachineModuleInfo *MMI,
143 MCStreamer &Streamer) const override;
144
145 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
146 const TargetMachine &TM) const override;
147};
148
149class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
150 mutable unsigned NextUniqueID = 0;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200151 const TargetMachine *TM = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152
153public:
154 ~TargetLoweringObjectFileCOFF() override = default;
155
156 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
157 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
158 const TargetMachine &TM) const override;
159
160 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
161 const TargetMachine &TM) const override;
162
163 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
164 const TargetMachine &TM) const override;
165
166 MCSection *getSectionForJumpTable(const Function &F,
167 const TargetMachine &TM) const override;
168
169 /// Emit Obj-C garbage collection and linker options.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100170 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100171
172 MCSection *getStaticCtorSection(unsigned Priority,
173 const MCSymbol *KeySym) const override;
174 MCSection *getStaticDtorSection(unsigned Priority,
175 const MCSymbol *KeySym) const override;
176
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100177 const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
178 const GlobalValue *RHS,
179 const TargetMachine &TM) const override;
180
181 /// Given a mergeable constant with the specified size and relocation
182 /// information, return a section that it should be placed in.
183 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
184 const Constant *C,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185 Align &Alignment) const override;
186
187private:
188 void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100189};
190
191class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
192 mutable unsigned NextUniqueID = 0;
193
194public:
195 TargetLoweringObjectFileWasm() = default;
196 ~TargetLoweringObjectFileWasm() override = default;
197
198 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
199 const TargetMachine &TM) const override;
200
201 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
202 const TargetMachine &TM) const override;
203
204 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
205 const Function &F) const override;
206
207 void InitializeWasm();
208 MCSection *getStaticCtorSection(unsigned Priority,
209 const MCSymbol *KeySym) const override;
210 MCSection *getStaticDtorSection(unsigned Priority,
211 const MCSymbol *KeySym) const override;
212
213 const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
214 const GlobalValue *RHS,
215 const TargetMachine &TM) const override;
216};
217
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200218class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
219public:
220 TargetLoweringObjectFileXCOFF() = default;
221 ~TargetLoweringObjectFileXCOFF() override = default;
222
223 static bool ShouldEmitEHBlock(const MachineFunction *MF);
224
225 static MCSymbol *getEHInfoTableSymbol(const MachineFunction *MF);
226
227 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
228
229 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
230 const Function &F) const override;
231
232 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
233 const TargetMachine &TM) const override;
234
235 MCSection *getStaticCtorSection(unsigned Priority,
236 const MCSymbol *KeySym) const override;
237 MCSection *getStaticDtorSection(unsigned Priority,
238 const MCSymbol *KeySym) const override;
239
240 const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
241 const GlobalValue *RHS,
242 const TargetMachine &TM) const override;
243
244 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
245 const TargetMachine &TM) const override;
246
247 MCSection *getSectionForJumpTable(const Function &F,
248 const TargetMachine &TM) const override;
249
250 /// Given a constant with the SectionKind, return a section that it should be
251 /// placed in.
252 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
253 const Constant *C,
254 Align &Alignment) const override;
255
256 static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV);
257
258 MCSection *
259 getSectionForFunctionDescriptor(const Function *F,
260 const TargetMachine &TM) const override;
261 MCSection *getSectionForTOCEntry(const MCSymbol *Sym,
262 const TargetMachine &TM) const override;
263
264 /// For external functions, this will always return a function descriptor
265 /// csect.
266 MCSection *
267 getSectionForExternalReference(const GlobalObject *GO,
268 const TargetMachine &TM) const override;
269
270 /// For functions, this will always return a function descriptor symbol.
271 MCSymbol *getTargetSymbol(const GlobalValue *GV,
272 const TargetMachine &TM) const override;
273
274 MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
275 const TargetMachine &TM) const override;
276};
277
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100278} // end namespace llvm
279
280#endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H