/src/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===// |
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 | | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
15 | | #include "llvm/ADT/SmallString.h" |
16 | | #include "llvm/ADT/SmallVector.h" |
17 | | #include "llvm/ADT/StringExtras.h" |
18 | | #include "llvm/ADT/StringRef.h" |
19 | | #include "llvm/BinaryFormat/COFF.h" |
20 | | #include "llvm/BinaryFormat/Dwarf.h" |
21 | | #include "llvm/BinaryFormat/ELF.h" |
22 | | #include "llvm/BinaryFormat/MachO.h" |
23 | | #include "llvm/BinaryFormat/Wasm.h" |
24 | | #include "llvm/CodeGen/BasicBlockSectionUtils.h" |
25 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
26 | | #include "llvm/CodeGen/MachineFunction.h" |
27 | | #include "llvm/CodeGen/MachineModuleInfo.h" |
28 | | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
29 | | #include "llvm/IR/Comdat.h" |
30 | | #include "llvm/IR/Constants.h" |
31 | | #include "llvm/IR/DataLayout.h" |
32 | | #include "llvm/IR/DerivedTypes.h" |
33 | | #include "llvm/IR/DiagnosticInfo.h" |
34 | | #include "llvm/IR/DiagnosticPrinter.h" |
35 | | #include "llvm/IR/Function.h" |
36 | | #include "llvm/IR/GlobalAlias.h" |
37 | | #include "llvm/IR/GlobalObject.h" |
38 | | #include "llvm/IR/GlobalValue.h" |
39 | | #include "llvm/IR/GlobalVariable.h" |
40 | | #include "llvm/IR/Mangler.h" |
41 | | #include "llvm/IR/Metadata.h" |
42 | | #include "llvm/IR/Module.h" |
43 | | #include "llvm/IR/PseudoProbe.h" |
44 | | #include "llvm/IR/Type.h" |
45 | | #include "llvm/MC/MCAsmInfo.h" |
46 | | #include "llvm/MC/MCContext.h" |
47 | | #include "llvm/MC/MCExpr.h" |
48 | | #include "llvm/MC/MCSectionCOFF.h" |
49 | | #include "llvm/MC/MCSectionELF.h" |
50 | | #include "llvm/MC/MCSectionGOFF.h" |
51 | | #include "llvm/MC/MCSectionMachO.h" |
52 | | #include "llvm/MC/MCSectionWasm.h" |
53 | | #include "llvm/MC/MCSectionXCOFF.h" |
54 | | #include "llvm/MC/MCStreamer.h" |
55 | | #include "llvm/MC/MCSymbol.h" |
56 | | #include "llvm/MC/MCSymbolELF.h" |
57 | | #include "llvm/MC/MCValue.h" |
58 | | #include "llvm/MC/SectionKind.h" |
59 | | #include "llvm/ProfileData/InstrProf.h" |
60 | | #include "llvm/Support/Base64.h" |
61 | | #include "llvm/Support/Casting.h" |
62 | | #include "llvm/Support/CodeGen.h" |
63 | | #include "llvm/Support/ErrorHandling.h" |
64 | | #include "llvm/Support/Format.h" |
65 | | #include "llvm/Support/raw_ostream.h" |
66 | | #include "llvm/Target/TargetMachine.h" |
67 | | #include "llvm/TargetParser/Triple.h" |
68 | | #include <cassert> |
69 | | #include <string> |
70 | | |
71 | | using namespace llvm; |
72 | | using namespace dwarf; |
73 | | |
74 | | static cl::opt<bool> JumpTableInFunctionSection( |
75 | | "jumptable-in-function-section", cl::Hidden, cl::init(false), |
76 | | cl::desc("Putting Jump Table in function section")); |
77 | | |
78 | | static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, |
79 | 33.5k | StringRef &Section) { |
80 | 33.5k | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
81 | 33.5k | M.getModuleFlagsMetadata(ModuleFlags); |
82 | | |
83 | 33.5k | for (const auto &MFE: ModuleFlags) { |
84 | | // Ignore flags with 'Require' behaviour. |
85 | 2.47k | if (MFE.Behavior == Module::Require) |
86 | 0 | continue; |
87 | | |
88 | 2.47k | StringRef Key = MFE.Key->getString(); |
89 | 2.47k | if (Key == "Objective-C Image Info Version") { |
90 | 0 | Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
91 | 2.47k | } else if (Key == "Objective-C Garbage Collection" || |
92 | 2.47k | Key == "Objective-C GC Only" || |
93 | 2.47k | Key == "Objective-C Is Simulated" || |
94 | 2.47k | Key == "Objective-C Class Properties" || |
95 | 2.47k | Key == "Objective-C Image Swift Version") { |
96 | 0 | Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
97 | 2.47k | } else if (Key == "Objective-C Image Info Section") { |
98 | 0 | Section = cast<MDString>(MFE.Val)->getString(); |
99 | 0 | } |
100 | | // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor + |
101 | | // "Objective-C Garbage Collection". |
102 | 2.47k | else if (Key == "Swift ABI Version") { |
103 | 0 | Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 8; |
104 | 2.47k | } else if (Key == "Swift Major Version") { |
105 | 0 | Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 24; |
106 | 2.47k | } else if (Key == "Swift Minor Version") { |
107 | 0 | Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 16; |
108 | 0 | } |
109 | 2.47k | } |
110 | 33.5k | } |
111 | | |
112 | | //===----------------------------------------------------------------------===// |
113 | | // ELF |
114 | | //===----------------------------------------------------------------------===// |
115 | | |
116 | 58 | TargetLoweringObjectFileELF::TargetLoweringObjectFileELF() { |
117 | 58 | SupportDSOLocalEquivalentLowering = true; |
118 | 58 | } |
119 | | |
120 | | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
121 | 33.5k | const TargetMachine &TgtM) { |
122 | 33.5k | TargetLoweringObjectFile::Initialize(Ctx, TgtM); |
123 | | |
124 | 33.5k | CodeModel::Model CM = TgtM.getCodeModel(); |
125 | 33.5k | InitializeELF(TgtM.Options.UseInitArray); |
126 | | |
127 | 33.5k | switch (TgtM.getTargetTriple().getArch()) { |
128 | 2.47k | case Triple::arm: |
129 | 2.47k | case Triple::armeb: |
130 | 2.47k | case Triple::thumb: |
131 | 2.47k | case Triple::thumbeb: |
132 | 2.47k | if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) |
133 | 2.47k | break; |
134 | | // Fallthrough if not using EHABI |
135 | 2.47k | [[fallthrough]]; |
136 | 0 | case Triple::ppc: |
137 | 0 | case Triple::ppcle: |
138 | 0 | case Triple::x86: |
139 | 0 | PersonalityEncoding = isPositionIndependent() |
140 | 0 | ? dwarf::DW_EH_PE_indirect | |
141 | 0 | dwarf::DW_EH_PE_pcrel | |
142 | 0 | dwarf::DW_EH_PE_sdata4 |
143 | 0 | : dwarf::DW_EH_PE_absptr; |
144 | 0 | LSDAEncoding = isPositionIndependent() |
145 | 0 | ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
146 | 0 | : dwarf::DW_EH_PE_absptr; |
147 | 0 | TTypeEncoding = isPositionIndependent() |
148 | 0 | ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
149 | 0 | dwarf::DW_EH_PE_sdata4 |
150 | 0 | : dwarf::DW_EH_PE_absptr; |
151 | 0 | break; |
152 | 662 | case Triple::x86_64: |
153 | 662 | if (isPositionIndependent()) { |
154 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
155 | 0 | ((CM == CodeModel::Small || CM == CodeModel::Medium) |
156 | 0 | ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); |
157 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | |
158 | 0 | (CM == CodeModel::Small |
159 | 0 | ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); |
160 | 0 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
161 | 0 | ((CM == CodeModel::Small || CM == CodeModel::Medium) |
162 | 0 | ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); |
163 | 662 | } else { |
164 | 662 | PersonalityEncoding = |
165 | 662 | (CM == CodeModel::Small || CM == CodeModel::Medium) |
166 | 662 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
167 | 662 | LSDAEncoding = (CM == CodeModel::Small) |
168 | 662 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
169 | 662 | TTypeEncoding = (CM == CodeModel::Small) |
170 | 662 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
171 | 662 | } |
172 | 662 | break; |
173 | 8.48k | case Triple::hexagon: |
174 | 8.48k | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
175 | 8.48k | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
176 | 8.48k | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
177 | 8.48k | if (isPositionIndependent()) { |
178 | 0 | PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; |
179 | 0 | LSDAEncoding |= dwarf::DW_EH_PE_pcrel; |
180 | 0 | TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; |
181 | 0 | } |
182 | 8.48k | break; |
183 | 4.38k | case Triple::aarch64: |
184 | 4.38k | case Triple::aarch64_be: |
185 | 4.38k | case Triple::aarch64_32: |
186 | | // The small model guarantees static code/data size < 4GB, but not where it |
187 | | // will be in memory. Most of these could end up >2GB away so even a signed |
188 | | // pc-relative 32-bit address is insufficient, theoretically. |
189 | | // |
190 | | // Use DW_EH_PE_indirect even for -fno-pic to avoid copy relocations. |
191 | 4.38k | LSDAEncoding = dwarf::DW_EH_PE_pcrel | |
192 | 4.38k | (TgtM.getTargetTriple().getEnvironment() == Triple::GNUILP32 |
193 | 4.38k | ? dwarf::DW_EH_PE_sdata4 |
194 | 4.38k | : dwarf::DW_EH_PE_sdata8); |
195 | 4.38k | PersonalityEncoding = LSDAEncoding | dwarf::DW_EH_PE_indirect; |
196 | 4.38k | TTypeEncoding = LSDAEncoding | dwarf::DW_EH_PE_indirect; |
197 | 4.38k | break; |
198 | 0 | case Triple::lanai: |
199 | 0 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
200 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
201 | 0 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
202 | 0 | break; |
203 | 0 | case Triple::mips: |
204 | 0 | case Triple::mipsel: |
205 | 71 | case Triple::mips64: |
206 | 71 | case Triple::mips64el: |
207 | | // MIPS uses indirect pointer to refer personality functions and types, so |
208 | | // that the eh_frame section can be read-only. DW.ref.personality will be |
209 | | // generated for relocation. |
210 | 71 | PersonalityEncoding = dwarf::DW_EH_PE_indirect; |
211 | | // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't |
212 | | // identify N64 from just a triple. |
213 | 71 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
214 | 71 | dwarf::DW_EH_PE_sdata4; |
215 | | // We don't support PC-relative LSDA references in GAS so we use the default |
216 | | // DW_EH_PE_absptr for those. |
217 | | |
218 | | // FreeBSD must be explicit about the data size and using pcrel since it's |
219 | | // assembler/linker won't do the automatic conversion that the Linux tools |
220 | | // do. |
221 | 71 | if (TgtM.getTargetTriple().isOSFreeBSD()) { |
222 | 0 | PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
223 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
224 | 0 | } |
225 | 71 | break; |
226 | 8.28k | case Triple::ppc64: |
227 | 8.28k | case Triple::ppc64le: |
228 | 8.28k | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
229 | 8.28k | dwarf::DW_EH_PE_udata8; |
230 | 8.28k | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; |
231 | 8.28k | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
232 | 8.28k | dwarf::DW_EH_PE_udata8; |
233 | 8.28k | break; |
234 | 0 | case Triple::sparcel: |
235 | 0 | case Triple::sparc: |
236 | 0 | if (isPositionIndependent()) { |
237 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
238 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
239 | 0 | dwarf::DW_EH_PE_sdata4; |
240 | 0 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
241 | 0 | dwarf::DW_EH_PE_sdata4; |
242 | 0 | } else { |
243 | 0 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
244 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
245 | 0 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
246 | 0 | } |
247 | 0 | CallSiteEncoding = dwarf::DW_EH_PE_udata4; |
248 | 0 | break; |
249 | 0 | case Triple::riscv32: |
250 | 6.24k | case Triple::riscv64: |
251 | 6.24k | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
252 | 6.24k | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
253 | 6.24k | dwarf::DW_EH_PE_sdata4; |
254 | 6.24k | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
255 | 6.24k | dwarf::DW_EH_PE_sdata4; |
256 | 6.24k | CallSiteEncoding = dwarf::DW_EH_PE_udata4; |
257 | 6.24k | break; |
258 | 0 | case Triple::sparcv9: |
259 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
260 | 0 | if (isPositionIndependent()) { |
261 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
262 | 0 | dwarf::DW_EH_PE_sdata4; |
263 | 0 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
264 | 0 | dwarf::DW_EH_PE_sdata4; |
265 | 0 | } else { |
266 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
267 | 0 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
268 | 0 | } |
269 | 0 | break; |
270 | 0 | case Triple::systemz: |
271 | | // All currently-defined code models guarantee that 4-byte PC-relative |
272 | | // values will be in range. |
273 | 0 | if (isPositionIndependent()) { |
274 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
275 | 0 | dwarf::DW_EH_PE_sdata4; |
276 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
277 | 0 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
278 | 0 | dwarf::DW_EH_PE_sdata4; |
279 | 0 | } else { |
280 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
281 | 0 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
282 | 0 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
283 | 0 | } |
284 | 0 | break; |
285 | 0 | case Triple::loongarch32: |
286 | 0 | case Triple::loongarch64: |
287 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
288 | 0 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
289 | 0 | dwarf::DW_EH_PE_sdata4; |
290 | 0 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
291 | 0 | dwarf::DW_EH_PE_sdata4; |
292 | 0 | break; |
293 | 2.95k | default: |
294 | 2.95k | break; |
295 | 33.5k | } |
296 | 33.5k | } |
297 | | |
298 | 33.5k | void TargetLoweringObjectFileELF::getModuleMetadata(Module &M) { |
299 | 33.5k | SmallVector<GlobalValue *, 4> Vec; |
300 | 33.5k | collectUsedGlobalVariables(M, Vec, false); |
301 | 33.5k | for (GlobalValue *GV : Vec) |
302 | 0 | if (auto *GO = dyn_cast<GlobalObject>(GV)) |
303 | 0 | Used.insert(GO); |
304 | 33.5k | } |
305 | | |
306 | | void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, |
307 | 33.5k | Module &M) const { |
308 | 33.5k | auto &C = getContext(); |
309 | | |
310 | 33.5k | if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
311 | 0 | auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS, |
312 | 0 | ELF::SHF_EXCLUDE); |
313 | |
|
314 | 0 | Streamer.switchSection(S); |
315 | |
|
316 | 0 | for (const auto *Operand : LinkerOptions->operands()) { |
317 | 0 | if (cast<MDNode>(Operand)->getNumOperands() != 2) |
318 | 0 | report_fatal_error("invalid llvm.linker.options"); |
319 | 0 | for (const auto &Option : cast<MDNode>(Operand)->operands()) { |
320 | 0 | Streamer.emitBytes(cast<MDString>(Option)->getString()); |
321 | 0 | Streamer.emitInt8(0); |
322 | 0 | } |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | 33.5k | if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) { |
327 | 0 | auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES, |
328 | 0 | ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); |
329 | |
|
330 | 0 | Streamer.switchSection(S); |
331 | |
|
332 | 0 | for (const auto *Operand : DependentLibraries->operands()) { |
333 | 0 | Streamer.emitBytes( |
334 | 0 | cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString()); |
335 | 0 | Streamer.emitInt8(0); |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | 33.5k | if (NamedMDNode *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName)) { |
340 | | // Emit a descriptor for every function including functions that have an |
341 | | // available external linkage. We may not want this for imported functions |
342 | | // that has code in another thinLTO module but we don't have a good way to |
343 | | // tell them apart from inline functions defined in header files. Therefore |
344 | | // we put each descriptor in a separate comdat section and rely on the |
345 | | // linker to deduplicate. |
346 | 0 | for (const auto *Operand : FuncInfo->operands()) { |
347 | 0 | const auto *MD = cast<MDNode>(Operand); |
348 | 0 | auto *GUID = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0)); |
349 | 0 | auto *Hash = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); |
350 | 0 | auto *Name = cast<MDString>(MD->getOperand(2)); |
351 | 0 | auto *S = C.getObjectFileInfo()->getPseudoProbeDescSection( |
352 | 0 | TM->getFunctionSections() ? Name->getString() : StringRef()); |
353 | |
|
354 | 0 | Streamer.switchSection(S); |
355 | 0 | Streamer.emitInt64(GUID->getZExtValue()); |
356 | 0 | Streamer.emitInt64(Hash->getZExtValue()); |
357 | 0 | Streamer.emitULEB128IntValue(Name->getString().size()); |
358 | 0 | Streamer.emitBytes(Name->getString()); |
359 | 0 | } |
360 | 0 | } |
361 | | |
362 | 33.5k | if (NamedMDNode *LLVMStats = M.getNamedMetadata("llvm.stats")) { |
363 | | // Emit the metadata for llvm statistics into .llvm_stats section, which is |
364 | | // formatted as a list of key/value pair, the value is base64 encoded. |
365 | 0 | auto *S = C.getObjectFileInfo()->getLLVMStatsSection(); |
366 | 0 | Streamer.switchSection(S); |
367 | 0 | for (const auto *Operand : LLVMStats->operands()) { |
368 | 0 | const auto *MD = cast<MDNode>(Operand); |
369 | 0 | assert(MD->getNumOperands() % 2 == 0 && |
370 | 0 | ("Operand num should be even for a list of key/value pair")); |
371 | 0 | for (size_t I = 0; I < MD->getNumOperands(); I += 2) { |
372 | | // Encode the key string size. |
373 | 0 | auto *Key = cast<MDString>(MD->getOperand(I)); |
374 | 0 | Streamer.emitULEB128IntValue(Key->getString().size()); |
375 | 0 | Streamer.emitBytes(Key->getString()); |
376 | | // Encode the value into a Base64 string. |
377 | 0 | std::string Value = encodeBase64( |
378 | 0 | Twine(mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1)) |
379 | 0 | ->getZExtValue()) |
380 | 0 | .str()); |
381 | 0 | Streamer.emitULEB128IntValue(Value.size()); |
382 | 0 | Streamer.emitBytes(Value); |
383 | 0 | } |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | 33.5k | unsigned Version = 0; |
388 | 33.5k | unsigned Flags = 0; |
389 | 33.5k | StringRef Section; |
390 | | |
391 | 33.5k | GetObjCImageInfo(M, Version, Flags, Section); |
392 | 33.5k | if (!Section.empty()) { |
393 | 0 | auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); |
394 | 0 | Streamer.switchSection(S); |
395 | 0 | Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
396 | 0 | Streamer.emitInt32(Version); |
397 | 0 | Streamer.emitInt32(Flags); |
398 | 0 | Streamer.addBlankLine(); |
399 | 0 | } |
400 | | |
401 | 33.5k | emitCGProfileMetadata(Streamer, M); |
402 | 33.5k | } |
403 | | |
404 | | MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( |
405 | | const GlobalValue *GV, const TargetMachine &TM, |
406 | 136 | MachineModuleInfo *MMI) const { |
407 | 136 | unsigned Encoding = getPersonalityEncoding(); |
408 | 136 | if ((Encoding & 0x80) == DW_EH_PE_indirect) |
409 | 118 | return getContext().getOrCreateSymbol(StringRef("DW.ref.") + |
410 | 118 | TM.getSymbol(GV)->getName()); |
411 | 18 | if ((Encoding & 0x70) == DW_EH_PE_absptr) |
412 | 18 | return TM.getSymbol(GV); |
413 | 0 | report_fatal_error("We do not support this DWARF encoding yet!"); |
414 | 0 | } |
415 | | |
416 | | void TargetLoweringObjectFileELF::emitPersonalityValue( |
417 | 89 | MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { |
418 | 89 | SmallString<64> NameData("DW.ref."); |
419 | 89 | NameData += Sym->getName(); |
420 | 89 | MCSymbolELF *Label = |
421 | 89 | cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); |
422 | 89 | Streamer.emitSymbolAttribute(Label, MCSA_Hidden); |
423 | 89 | Streamer.emitSymbolAttribute(Label, MCSA_Weak); |
424 | 89 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
425 | 89 | MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(), |
426 | 89 | ELF::SHT_PROGBITS, Flags, 0); |
427 | 89 | unsigned Size = DL.getPointerSize(); |
428 | 89 | Streamer.switchSection(Sec); |
429 | 89 | Streamer.emitValueToAlignment(DL.getPointerABIAlignment(0)); |
430 | 89 | Streamer.emitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
431 | 89 | const MCExpr *E = MCConstantExpr::create(Size, getContext()); |
432 | 89 | Streamer.emitELFSize(Label, E); |
433 | 89 | Streamer.emitLabel(Label); |
434 | | |
435 | 89 | Streamer.emitSymbolValue(Sym, Size); |
436 | 89 | } |
437 | | |
438 | | const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( |
439 | | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
440 | 7 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
441 | 7 | if (Encoding & DW_EH_PE_indirect) { |
442 | 7 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
443 | | |
444 | 7 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM); |
445 | | |
446 | | // Add information about the stub reference to ELFMMI so that the stub |
447 | | // gets emitted by the asmprinter. |
448 | 7 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
449 | 7 | if (!StubSym.getPointer()) { |
450 | 7 | MCSymbol *Sym = TM.getSymbol(GV); |
451 | 7 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
452 | 7 | } |
453 | | |
454 | 7 | return TargetLoweringObjectFile:: |
455 | 7 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
456 | 7 | Encoding & ~DW_EH_PE_indirect, Streamer); |
457 | 7 | } |
458 | | |
459 | 0 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
460 | 0 | MMI, Streamer); |
461 | 7 | } |
462 | | |
463 | 3 | static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { |
464 | | // N.B.: The defaults used in here are not the same ones used in MC. |
465 | | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
466 | | // both gas and MC will produce a section with no flags. Given |
467 | | // section(".eh_frame") gcc will produce: |
468 | | // |
469 | | // .section .eh_frame,"a",@progbits |
470 | | |
471 | 3 | if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, |
472 | 3 | /*AddSegmentInfo=*/false) || |
473 | 3 | Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF, |
474 | 3 | /*AddSegmentInfo=*/false) || |
475 | 3 | Name == getInstrProfSectionName(IPSK_covdata, Triple::ELF, |
476 | 3 | /*AddSegmentInfo=*/false) || |
477 | 3 | Name == getInstrProfSectionName(IPSK_covname, Triple::ELF, |
478 | 3 | /*AddSegmentInfo=*/false) || |
479 | 3 | Name == ".llvmbc" || Name == ".llvmcmd") |
480 | 0 | return SectionKind::getMetadata(); |
481 | | |
482 | 3 | if (Name.empty() || Name[0] != '.') return K; |
483 | | |
484 | | // Default implementation based on some magic section names. |
485 | 1 | if (Name == ".bss" || Name.starts_with(".bss.") || |
486 | 1 | Name.starts_with(".gnu.linkonce.b.") || |
487 | 1 | Name.starts_with(".llvm.linkonce.b.") || Name == ".sbss" || |
488 | 1 | Name.starts_with(".sbss.") || Name.starts_with(".gnu.linkonce.sb.") || |
489 | 1 | Name.starts_with(".llvm.linkonce.sb.")) |
490 | 0 | return SectionKind::getBSS(); |
491 | | |
492 | 1 | if (Name == ".tdata" || Name.starts_with(".tdata.") || |
493 | 1 | Name.starts_with(".gnu.linkonce.td.") || |
494 | 1 | Name.starts_with(".llvm.linkonce.td.")) |
495 | 0 | return SectionKind::getThreadData(); |
496 | | |
497 | 1 | if (Name == ".tbss" || Name.starts_with(".tbss.") || |
498 | 1 | Name.starts_with(".gnu.linkonce.tb.") || |
499 | 1 | Name.starts_with(".llvm.linkonce.tb.")) |
500 | 0 | return SectionKind::getThreadBSS(); |
501 | | |
502 | 1 | return K; |
503 | 1 | } |
504 | | |
505 | 813k | static bool hasPrefix(StringRef SectionName, StringRef Prefix) { |
506 | 813k | return SectionName.consume_front(Prefix) && |
507 | 813k | (SectionName.empty() || SectionName[0] == '.'); |
508 | 813k | } |
509 | | |
510 | 203k | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
511 | | // Use SHT_NOTE for section whose name starts with ".note" to allow |
512 | | // emitting ELF notes from C variable declaration. |
513 | | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 |
514 | 203k | if (Name.starts_with(".note")) |
515 | 0 | return ELF::SHT_NOTE; |
516 | | |
517 | 203k | if (hasPrefix(Name, ".init_array")) |
518 | 0 | return ELF::SHT_INIT_ARRAY; |
519 | | |
520 | 203k | if (hasPrefix(Name, ".fini_array")) |
521 | 0 | return ELF::SHT_FINI_ARRAY; |
522 | | |
523 | 203k | if (hasPrefix(Name, ".preinit_array")) |
524 | 0 | return ELF::SHT_PREINIT_ARRAY; |
525 | | |
526 | 203k | if (hasPrefix(Name, ".llvm.offloading")) |
527 | 0 | return ELF::SHT_LLVM_OFFLOADING; |
528 | | |
529 | 203k | if (K.isBSS() || K.isThreadBSS()) |
530 | 27.8k | return ELF::SHT_NOBITS; |
531 | | |
532 | 175k | return ELF::SHT_PROGBITS; |
533 | 203k | } |
534 | | |
535 | 203k | static unsigned getELFSectionFlags(SectionKind K) { |
536 | 203k | unsigned Flags = 0; |
537 | | |
538 | 203k | if (!K.isMetadata() && !K.isExclude()) |
539 | 203k | Flags |= ELF::SHF_ALLOC; |
540 | | |
541 | 203k | if (K.isExclude()) |
542 | 0 | Flags |= ELF::SHF_EXCLUDE; |
543 | | |
544 | 203k | if (K.isText()) |
545 | 121k | Flags |= ELF::SHF_EXECINSTR; |
546 | | |
547 | 203k | if (K.isExecuteOnly()) |
548 | 0 | Flags |= ELF::SHF_ARM_PURECODE; |
549 | | |
550 | 203k | if (K.isWriteable()) |
551 | 80.5k | Flags |= ELF::SHF_WRITE; |
552 | | |
553 | 203k | if (K.isThreadLocal()) |
554 | 92 | Flags |= ELF::SHF_TLS; |
555 | | |
556 | 203k | if (K.isMergeableCString() || K.isMergeableConst()) |
557 | 478 | Flags |= ELF::SHF_MERGE; |
558 | | |
559 | 203k | if (K.isMergeableCString()) |
560 | 255 | Flags |= ELF::SHF_STRINGS; |
561 | | |
562 | 203k | return Flags; |
563 | 203k | } |
564 | | |
565 | 203k | static const Comdat *getELFComdat(const GlobalValue *GV) { |
566 | 203k | const Comdat *C = GV->getComdat(); |
567 | 203k | if (!C) |
568 | 203k | return nullptr; |
569 | | |
570 | 36 | if (C->getSelectionKind() != Comdat::Any && |
571 | 36 | C->getSelectionKind() != Comdat::NoDeduplicate) |
572 | 0 | report_fatal_error("ELF COMDATs only support SelectionKind::Any and " |
573 | 0 | "SelectionKind::NoDeduplicate, '" + |
574 | 0 | C->getName() + "' cannot be lowered."); |
575 | | |
576 | 36 | return C; |
577 | 203k | } |
578 | | |
579 | | static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO, |
580 | 203k | const TargetMachine &TM) { |
581 | 203k | MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); |
582 | 203k | if (!MD) |
583 | 203k | return nullptr; |
584 | | |
585 | 0 | auto *VM = cast<ValueAsMetadata>(MD->getOperand(0).get()); |
586 | 0 | auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue()); |
587 | 0 | return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr; |
588 | 203k | } |
589 | | |
590 | 203k | static unsigned getEntrySizeForKind(SectionKind Kind) { |
591 | 203k | if (Kind.isMergeable1ByteCString()) |
592 | 255 | return 1; |
593 | 203k | else if (Kind.isMergeable2ByteCString()) |
594 | 0 | return 2; |
595 | 203k | else if (Kind.isMergeable4ByteCString()) |
596 | 0 | return 4; |
597 | 203k | else if (Kind.isMergeableConst4()) |
598 | 60 | return 4; |
599 | 203k | else if (Kind.isMergeableConst8()) |
600 | 119 | return 8; |
601 | 203k | else if (Kind.isMergeableConst16()) |
602 | 33 | return 16; |
603 | 202k | else if (Kind.isMergeableConst32()) |
604 | 11 | return 32; |
605 | 202k | else { |
606 | | // We shouldn't have mergeable C strings or mergeable constants that we |
607 | | // didn't handle above. |
608 | 202k | assert(!Kind.isMergeableCString() && "unknown string width"); |
609 | 0 | assert(!Kind.isMergeableConst() && "unknown data width"); |
610 | 0 | return 0; |
611 | 202k | } |
612 | 203k | } |
613 | | |
614 | | /// Return the section prefix name used by options FunctionsSections and |
615 | | /// DataSections. |
616 | 209k | static StringRef getSectionPrefixForGlobal(SectionKind Kind, bool IsLarge) { |
617 | 209k | if (Kind.isText()) |
618 | 128k | return IsLarge ? ".ltext" : ".text"; |
619 | 81.7k | if (Kind.isReadOnly()) |
620 | 1.08k | return IsLarge ? ".lrodata" : ".rodata"; |
621 | 80.6k | if (Kind.isBSS()) |
622 | 27.7k | return IsLarge ? ".lbss" : ".bss"; |
623 | 52.8k | if (Kind.isThreadData()) |
624 | 0 | return ".tdata"; |
625 | 52.8k | if (Kind.isThreadBSS()) |
626 | 92 | return ".tbss"; |
627 | 52.7k | if (Kind.isData()) |
628 | 52.7k | return IsLarge ? ".ldata" : ".data"; |
629 | 23 | if (Kind.isReadOnlyWithRel()) |
630 | 23 | return IsLarge ? ".ldata.rel.ro" : ".data.rel.ro"; |
631 | 0 | llvm_unreachable("Unknown section kind"); |
632 | 0 | } |
633 | | |
634 | | static SmallString<128> |
635 | | getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind, |
636 | | Mangler &Mang, const TargetMachine &TM, |
637 | 203k | unsigned EntrySize, bool UniqueSectionName) { |
638 | 203k | SmallString<128> Name; |
639 | 203k | if (Kind.isMergeableCString()) { |
640 | | // We also need alignment here. |
641 | | // FIXME: this is getting the alignment of the character, not the |
642 | | // alignment of the global! |
643 | 255 | Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( |
644 | 255 | cast<GlobalVariable>(GO)); |
645 | | |
646 | 255 | std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; |
647 | 255 | Name = SizeSpec + utostr(Alignment.value()); |
648 | 203k | } else if (Kind.isMergeableConst()) { |
649 | 223 | Name = ".rodata.cst"; |
650 | 223 | Name += utostr(EntrySize); |
651 | 202k | } else { |
652 | 202k | Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO)); |
653 | 202k | } |
654 | | |
655 | 203k | bool HasPrefix = false; |
656 | 203k | if (const auto *F = dyn_cast<Function>(GO)) { |
657 | 121k | if (std::optional<StringRef> Prefix = F->getSectionPrefix()) { |
658 | 7 | raw_svector_ostream(Name) << '.' << *Prefix; |
659 | 7 | HasPrefix = true; |
660 | 7 | } |
661 | 121k | } |
662 | | |
663 | 203k | if (UniqueSectionName) { |
664 | 21 | Name.push_back('.'); |
665 | 21 | TM.getNameWithPrefix(Name, GO, Mang, /*MayAlwaysUsePrivate*/true); |
666 | 203k | } else if (HasPrefix) |
667 | | // For distinguishing between .text.${text-section-prefix}. (with trailing |
668 | | // dot) and .text.${function-name} |
669 | 7 | Name.push_back('.'); |
670 | 203k | return Name; |
671 | 203k | } |
672 | | |
673 | | namespace { |
674 | | class LoweringDiagnosticInfo : public DiagnosticInfo { |
675 | | const Twine &Msg; |
676 | | |
677 | | public: |
678 | | LoweringDiagnosticInfo(const Twine &DiagMsg, |
679 | | DiagnosticSeverity Severity = DS_Error) |
680 | 0 | : DiagnosticInfo(DK_Lowering, Severity), Msg(DiagMsg) {} |
681 | 0 | void print(DiagnosticPrinter &DP) const override { DP << Msg; } |
682 | | }; |
683 | | } |
684 | | |
685 | | /// Calculate an appropriate unique ID for a section, and update Flags, |
686 | | /// EntrySize and NextUniqueID where appropriate. |
687 | | static unsigned |
688 | | calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName, |
689 | | SectionKind Kind, const TargetMachine &TM, |
690 | | MCContext &Ctx, Mangler &Mang, unsigned &Flags, |
691 | | unsigned &EntrySize, unsigned &NextUniqueID, |
692 | 3 | const bool Retain, const bool ForceUnique) { |
693 | | // Increment uniqueID if we are forced to emit a unique section. |
694 | | // This works perfectly fine with section attribute or pragma section as the |
695 | | // sections with the same name are grouped together by the assembler. |
696 | 3 | if (ForceUnique) |
697 | 0 | return NextUniqueID++; |
698 | | |
699 | | // A section can have at most one associated section. Put each global with |
700 | | // MD_associated in a unique section. |
701 | 3 | const bool Associated = GO->getMetadata(LLVMContext::MD_associated); |
702 | 3 | if (Associated) { |
703 | 0 | Flags |= ELF::SHF_LINK_ORDER; |
704 | 0 | return NextUniqueID++; |
705 | 0 | } |
706 | | |
707 | 3 | if (Retain) { |
708 | 0 | if (TM.getTargetTriple().isOSSolaris()) |
709 | 0 | Flags |= ELF::SHF_SUNW_NODISCARD; |
710 | 0 | else if (Ctx.getAsmInfo()->useIntegratedAssembler() || |
711 | 0 | Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) |
712 | 0 | Flags |= ELF::SHF_GNU_RETAIN; |
713 | 0 | return NextUniqueID++; |
714 | 0 | } |
715 | | |
716 | | // If two symbols with differing sizes end up in the same mergeable section |
717 | | // that section can be assigned an incorrect entry size. To avoid this we |
718 | | // usually put symbols of the same size into distinct mergeable sections with |
719 | | // the same name. Doing so relies on the ",unique ," assembly feature. This |
720 | | // feature is not avalible until bintuils version 2.35 |
721 | | // (https://sourceware.org/bugzilla/show_bug.cgi?id=25380). |
722 | 3 | const bool SupportsUnique = Ctx.getAsmInfo()->useIntegratedAssembler() || |
723 | 3 | Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35); |
724 | 3 | if (!SupportsUnique) { |
725 | 0 | Flags &= ~ELF::SHF_MERGE; |
726 | 0 | EntrySize = 0; |
727 | 0 | return MCContext::GenericSectionID; |
728 | 0 | } |
729 | | |
730 | 3 | const bool SymbolMergeable = Flags & ELF::SHF_MERGE; |
731 | 3 | const bool SeenSectionNameBefore = |
732 | 3 | Ctx.isELFGenericMergeableSection(SectionName); |
733 | | // If this is the first ocurrence of this section name, treat it as the |
734 | | // generic section |
735 | 3 | if (!SymbolMergeable && !SeenSectionNameBefore) |
736 | 3 | return MCContext::GenericSectionID; |
737 | | |
738 | | // Symbols must be placed into sections with compatible entry sizes. Generate |
739 | | // unique sections for symbols that have not been assigned to compatible |
740 | | // sections. |
741 | 0 | const auto PreviousID = |
742 | 0 | Ctx.getELFUniqueIDForEntsize(SectionName, Flags, EntrySize); |
743 | 0 | if (PreviousID) |
744 | 0 | return *PreviousID; |
745 | | |
746 | | // If the user has specified the same section name as would be created |
747 | | // implicitly for this symbol e.g. .rodata.str1.1, then we don't need |
748 | | // to unique the section as the entry size for this symbol will be |
749 | | // compatible with implicitly created sections. |
750 | 0 | SmallString<128> ImplicitSectionNameStem = |
751 | 0 | getELFSectionNameForGlobal(GO, Kind, Mang, TM, EntrySize, false); |
752 | 0 | if (SymbolMergeable && |
753 | 0 | Ctx.isELFImplicitMergeableSectionNamePrefix(SectionName) && |
754 | 0 | SectionName.starts_with(ImplicitSectionNameStem)) |
755 | 0 | return MCContext::GenericSectionID; |
756 | | |
757 | | // We have seen this section name before, but with different flags or entity |
758 | | // size. Create a new unique ID. |
759 | 0 | return NextUniqueID++; |
760 | 0 | } |
761 | | |
762 | | static std::tuple<StringRef, bool, unsigned> |
763 | 203k | getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) { |
764 | 203k | StringRef Group = ""; |
765 | 203k | bool IsComdat = false; |
766 | 203k | unsigned Flags = 0; |
767 | 203k | if (const Comdat *C = getELFComdat(GO)) { |
768 | 21 | Flags |= ELF::SHF_GROUP; |
769 | 21 | Group = C->getName(); |
770 | 21 | IsComdat = C->getSelectionKind() == Comdat::Any; |
771 | 21 | } |
772 | 203k | if (TM.isLargeGlobalValue(GO)) |
773 | 0 | Flags |= ELF::SHF_X86_64_LARGE; |
774 | 203k | return {Group, IsComdat, Flags}; |
775 | 203k | } |
776 | | |
777 | | static MCSection *selectExplicitSectionGlobal( |
778 | | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM, |
779 | | MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID, |
780 | 3 | bool Retain, bool ForceUnique) { |
781 | 3 | StringRef SectionName = GO->getSection(); |
782 | | |
783 | | // Check if '#pragma clang section' name is applicable. |
784 | | // Note that pragma directive overrides -ffunction-section, -fdata-section |
785 | | // and so section name is exactly as user specified and not uniqued. |
786 | 3 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); |
787 | 3 | if (GV && GV->hasImplicitSection()) { |
788 | 0 | auto Attrs = GV->getAttributes(); |
789 | 0 | if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { |
790 | 0 | SectionName = Attrs.getAttribute("bss-section").getValueAsString(); |
791 | 0 | } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { |
792 | 0 | SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); |
793 | 0 | } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) { |
794 | 0 | SectionName = Attrs.getAttribute("relro-section").getValueAsString(); |
795 | 0 | } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { |
796 | 0 | SectionName = Attrs.getAttribute("data-section").getValueAsString(); |
797 | 0 | } |
798 | 0 | } |
799 | 3 | const Function *F = dyn_cast<Function>(GO); |
800 | 3 | if (F && F->hasFnAttribute("implicit-section-name")) { |
801 | 0 | SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); |
802 | 0 | } |
803 | | |
804 | | // Infer section flags from the section name if we can. |
805 | 3 | Kind = getELFKindForNamedSection(SectionName, Kind); |
806 | | |
807 | 3 | unsigned Flags = getELFSectionFlags(Kind); |
808 | 3 | auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM); |
809 | 3 | Flags |= ExtraFlags; |
810 | | |
811 | 3 | unsigned EntrySize = getEntrySizeForKind(Kind); |
812 | 3 | const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize( |
813 | 3 | GO, SectionName, Kind, TM, Ctx, Mang, Flags, EntrySize, NextUniqueID, |
814 | 3 | Retain, ForceUnique); |
815 | | |
816 | 3 | const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); |
817 | 3 | MCSectionELF *Section = Ctx.getELFSection( |
818 | 3 | SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize, |
819 | 3 | Group, IsComdat, UniqueID, LinkedToSym); |
820 | | // Make sure that we did not get some other section with incompatible sh_link. |
821 | | // This should not be possible due to UniqueID code above. |
822 | 3 | assert(Section->getLinkedToSymbol() == LinkedToSym && |
823 | 3 | "Associated symbol mismatch between sections"); |
824 | | |
825 | 3 | if (!(Ctx.getAsmInfo()->useIntegratedAssembler() || |
826 | 3 | Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35))) { |
827 | | // If we are using GNU as before 2.35, then this symbol might have |
828 | | // been placed in an incompatible mergeable section. Emit an error if this |
829 | | // is the case to avoid creating broken output. |
830 | 0 | if ((Section->getFlags() & ELF::SHF_MERGE) && |
831 | 0 | (Section->getEntrySize() != getEntrySizeForKind(Kind))) |
832 | 0 | GO->getContext().diagnose(LoweringDiagnosticInfo( |
833 | 0 | "Symbol '" + GO->getName() + "' from module '" + |
834 | 0 | (GO->getParent() ? GO->getParent()->getSourceFileName() : "unknown") + |
835 | 0 | "' required a section with entry-size=" + |
836 | 0 | Twine(getEntrySizeForKind(Kind)) + " but was placed in section '" + |
837 | 0 | SectionName + "' with entry-size=" + Twine(Section->getEntrySize()) + |
838 | 0 | ": Explicit assignment by pragma or attribute of an incompatible " |
839 | 0 | "symbol to this section?")); |
840 | 0 | } |
841 | | |
842 | 3 | return Section; |
843 | 3 | } |
844 | | |
845 | | MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( |
846 | 3 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
847 | 3 | return selectExplicitSectionGlobal(GO, Kind, TM, getContext(), getMangler(), |
848 | 3 | NextUniqueID, Used.count(GO), |
849 | 3 | /* ForceUnique = */false); |
850 | 3 | } |
851 | | |
852 | | static MCSectionELF *selectELFSectionForGlobal( |
853 | | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
854 | | const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, |
855 | 203k | unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { |
856 | | |
857 | 203k | auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM); |
858 | 203k | Flags |= ExtraFlags; |
859 | | |
860 | | // Get the section entry size based on the kind. |
861 | 203k | unsigned EntrySize = getEntrySizeForKind(Kind); |
862 | | |
863 | 203k | bool UniqueSectionName = false; |
864 | 203k | unsigned UniqueID = MCContext::GenericSectionID; |
865 | 203k | if (EmitUniqueSection) { |
866 | 21 | if (TM.getUniqueSectionNames()) { |
867 | 21 | UniqueSectionName = true; |
868 | 21 | } else { |
869 | 0 | UniqueID = *NextUniqueID; |
870 | 0 | (*NextUniqueID)++; |
871 | 0 | } |
872 | 21 | } |
873 | 203k | SmallString<128> Name = getELFSectionNameForGlobal( |
874 | 203k | GO, Kind, Mang, TM, EntrySize, UniqueSectionName); |
875 | | |
876 | | // Use 0 as the unique ID for execute-only text. |
877 | 203k | if (Kind.isExecuteOnly()) |
878 | 0 | UniqueID = 0; |
879 | 203k | return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, |
880 | 203k | EntrySize, Group, IsComdat, UniqueID, |
881 | 203k | AssociatedSymbol); |
882 | 203k | } |
883 | | |
884 | | static MCSection *selectELFSectionForGlobal( |
885 | | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
886 | | const TargetMachine &TM, bool Retain, bool EmitUniqueSection, |
887 | 203k | unsigned Flags, unsigned *NextUniqueID) { |
888 | 203k | const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); |
889 | 203k | if (LinkedToSym) { |
890 | 0 | EmitUniqueSection = true; |
891 | 0 | Flags |= ELF::SHF_LINK_ORDER; |
892 | 0 | } |
893 | 203k | if (Retain) { |
894 | 0 | if (TM.getTargetTriple().isOSSolaris()) { |
895 | 0 | EmitUniqueSection = true; |
896 | 0 | Flags |= ELF::SHF_SUNW_NODISCARD; |
897 | 0 | } else if (Ctx.getAsmInfo()->useIntegratedAssembler() || |
898 | 0 | Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) { |
899 | 0 | EmitUniqueSection = true; |
900 | 0 | Flags |= ELF::SHF_GNU_RETAIN; |
901 | 0 | } |
902 | 0 | } |
903 | | |
904 | 203k | MCSectionELF *Section = selectELFSectionForGlobal( |
905 | 203k | Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags, |
906 | 203k | NextUniqueID, LinkedToSym); |
907 | 203k | assert(Section->getLinkedToSymbol() == LinkedToSym); |
908 | 0 | return Section; |
909 | 203k | } |
910 | | |
911 | | MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( |
912 | 203k | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
913 | 203k | unsigned Flags = getELFSectionFlags(Kind); |
914 | | |
915 | | // If we have -ffunction-section or -fdata-section then we should emit the |
916 | | // global value to a uniqued section specifically for it. |
917 | 203k | bool EmitUniqueSection = false; |
918 | 203k | if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { |
919 | 202k | if (Kind.isText()) |
920 | 121k | EmitUniqueSection = TM.getFunctionSections(); |
921 | 81.6k | else |
922 | 81.6k | EmitUniqueSection = TM.getDataSections(); |
923 | 202k | } |
924 | 203k | EmitUniqueSection |= GO->hasComdat(); |
925 | 203k | return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, |
926 | 203k | Used.count(GO), EmitUniqueSection, Flags, |
927 | 203k | &NextUniqueID); |
928 | 203k | } |
929 | | |
930 | | MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction( |
931 | 0 | const Function &F, const TargetMachine &TM) const { |
932 | 0 | SectionKind Kind = SectionKind::getText(); |
933 | 0 | unsigned Flags = getELFSectionFlags(Kind); |
934 | | // If the function's section names is pre-determined via pragma or a |
935 | | // section attribute, call selectExplicitSectionGlobal. |
936 | 0 | if (F.hasSection() || F.hasFnAttribute("implicit-section-name")) |
937 | 0 | return selectExplicitSectionGlobal( |
938 | 0 | &F, Kind, TM, getContext(), getMangler(), NextUniqueID, |
939 | 0 | Used.count(&F), /* ForceUnique = */true); |
940 | 0 | else |
941 | 0 | return selectELFSectionForGlobal( |
942 | 0 | getContext(), &F, Kind, getMangler(), TM, Used.count(&F), |
943 | 0 | /*EmitUniqueSection=*/true, Flags, &NextUniqueID); |
944 | 0 | } |
945 | | |
946 | | MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( |
947 | 95 | const Function &F, const TargetMachine &TM) const { |
948 | | // If the function can be removed, produce a unique section so that |
949 | | // the table doesn't prevent the removal. |
950 | 95 | const Comdat *C = F.getComdat(); |
951 | 95 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
952 | 95 | if (!EmitUniqueSection) |
953 | 95 | return ReadOnlySection; |
954 | | |
955 | 0 | return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), |
956 | 0 | getMangler(), TM, EmitUniqueSection, |
957 | 0 | ELF::SHF_ALLOC, &NextUniqueID, |
958 | 0 | /* AssociatedSymbol */ nullptr); |
959 | 95 | } |
960 | | |
961 | | MCSection *TargetLoweringObjectFileELF::getSectionForLSDA( |
962 | 136 | const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const { |
963 | | // If neither COMDAT nor function sections, use the monolithic LSDA section. |
964 | | // Re-use this path if LSDASection is null as in the Arm EHABI. |
965 | 136 | if (!LSDASection || (!F.hasComdat() && !TM.getFunctionSections())) |
966 | 121 | return LSDASection; |
967 | | |
968 | 15 | const auto *LSDA = cast<MCSectionELF>(LSDASection); |
969 | 15 | unsigned Flags = LSDA->getFlags(); |
970 | 15 | const MCSymbolELF *LinkedToSym = nullptr; |
971 | 15 | StringRef Group; |
972 | 15 | bool IsComdat = false; |
973 | 15 | if (const Comdat *C = getELFComdat(&F)) { |
974 | 15 | Flags |= ELF::SHF_GROUP; |
975 | 15 | Group = C->getName(); |
976 | 15 | IsComdat = C->getSelectionKind() == Comdat::Any; |
977 | 15 | } |
978 | | // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36 |
979 | | // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER. |
980 | 15 | if (TM.getFunctionSections() && |
981 | 15 | (getContext().getAsmInfo()->useIntegratedAssembler() && |
982 | 0 | getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) { |
983 | 0 | Flags |= ELF::SHF_LINK_ORDER; |
984 | 0 | LinkedToSym = cast<MCSymbolELF>(&FnSym); |
985 | 0 | } |
986 | | |
987 | | // Append the function name as the suffix like GCC, assuming |
988 | | // -funique-section-names applies to .gcc_except_table sections. |
989 | 15 | return getContext().getELFSection( |
990 | 15 | (TM.getUniqueSectionNames() ? LSDA->getName() + "." + F.getName() |
991 | 15 | : LSDA->getName()), |
992 | 15 | LSDA->getType(), Flags, 0, Group, IsComdat, MCSection::NonUniqueID, |
993 | 15 | LinkedToSym); |
994 | 136 | } |
995 | | |
996 | | bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( |
997 | 87 | bool UsesLabelDifference, const Function &F) const { |
998 | | // We can always create relative relocations, so use another section |
999 | | // that can be marked non-executable. |
1000 | 87 | return false; |
1001 | 87 | } |
1002 | | |
1003 | | /// Given a mergeable constant with the specified size and relocation |
1004 | | /// information, return a section that it should be placed in. |
1005 | | MCSection *TargetLoweringObjectFileELF::getSectionForConstant( |
1006 | | const DataLayout &DL, SectionKind Kind, const Constant *C, |
1007 | 16.8k | Align &Alignment) const { |
1008 | 16.8k | if (Kind.isMergeableConst4() && MergeableConst4Section) |
1009 | 7.89k | return MergeableConst4Section; |
1010 | 8.99k | if (Kind.isMergeableConst8() && MergeableConst8Section) |
1011 | 2.80k | return MergeableConst8Section; |
1012 | 6.19k | if (Kind.isMergeableConst16() && MergeableConst16Section) |
1013 | 6.13k | return MergeableConst16Section; |
1014 | 58 | if (Kind.isMergeableConst32() && MergeableConst32Section) |
1015 | 2 | return MergeableConst32Section; |
1016 | 56 | if (Kind.isReadOnly()) |
1017 | 56 | return ReadOnlySection; |
1018 | | |
1019 | 0 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
1020 | 0 | return DataRelROSection; |
1021 | 56 | } |
1022 | | |
1023 | | /// Returns a unique section for the given machine basic block. |
1024 | | MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock( |
1025 | | const Function &F, const MachineBasicBlock &MBB, |
1026 | 0 | const TargetMachine &TM) const { |
1027 | 0 | assert(MBB.isBeginSection() && "Basic block does not start a section!"); |
1028 | 0 | unsigned UniqueID = MCContext::GenericSectionID; |
1029 | | |
1030 | | // For cold sections use the .text.split. prefix along with the parent |
1031 | | // function name. All cold blocks for the same function go to the same |
1032 | | // section. Similarly all exception blocks are grouped by symbol name |
1033 | | // under the .text.eh prefix. For regular sections, we either use a unique |
1034 | | // name, or a unique ID for the section. |
1035 | 0 | SmallString<128> Name; |
1036 | 0 | StringRef FunctionSectionName = MBB.getParent()->getSection()->getName(); |
1037 | 0 | if (FunctionSectionName.equals(".text") || |
1038 | 0 | FunctionSectionName.starts_with(".text.")) { |
1039 | | // Function is in a regular .text section. |
1040 | 0 | StringRef FunctionName = MBB.getParent()->getName(); |
1041 | 0 | if (MBB.getSectionID() == MBBSectionID::ColdSectionID) { |
1042 | 0 | Name += BBSectionsColdTextPrefix; |
1043 | 0 | Name += FunctionName; |
1044 | 0 | } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) { |
1045 | 0 | Name += ".text.eh."; |
1046 | 0 | Name += FunctionName; |
1047 | 0 | } else { |
1048 | 0 | Name += FunctionSectionName; |
1049 | 0 | if (TM.getUniqueBasicBlockSectionNames()) { |
1050 | 0 | if (!Name.ends_with(".")) |
1051 | 0 | Name += "."; |
1052 | 0 | Name += MBB.getSymbol()->getName(); |
1053 | 0 | } else { |
1054 | 0 | UniqueID = NextUniqueID++; |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | } else { |
1058 | | // If the original function has a custom non-dot-text section, then emit |
1059 | | // all basic block sections into that section too, each with a unique id. |
1060 | 0 | Name = FunctionSectionName; |
1061 | 0 | UniqueID = NextUniqueID++; |
1062 | 0 | } |
1063 | |
|
1064 | 0 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; |
1065 | 0 | std::string GroupName; |
1066 | 0 | if (F.hasComdat()) { |
1067 | 0 | Flags |= ELF::SHF_GROUP; |
1068 | 0 | GroupName = F.getComdat()->getName().str(); |
1069 | 0 | } |
1070 | 0 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, |
1071 | 0 | 0 /* Entry Size */, GroupName, |
1072 | 0 | F.hasComdat(), UniqueID, nullptr); |
1073 | 0 | } |
1074 | | |
1075 | | static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, |
1076 | | bool IsCtor, unsigned Priority, |
1077 | 0 | const MCSymbol *KeySym) { |
1078 | 0 | std::string Name; |
1079 | 0 | unsigned Type; |
1080 | 0 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; |
1081 | 0 | StringRef Comdat = KeySym ? KeySym->getName() : ""; |
1082 | |
|
1083 | 0 | if (KeySym) |
1084 | 0 | Flags |= ELF::SHF_GROUP; |
1085 | |
|
1086 | 0 | if (UseInitArray) { |
1087 | 0 | if (IsCtor) { |
1088 | 0 | Type = ELF::SHT_INIT_ARRAY; |
1089 | 0 | Name = ".init_array"; |
1090 | 0 | } else { |
1091 | 0 | Type = ELF::SHT_FINI_ARRAY; |
1092 | 0 | Name = ".fini_array"; |
1093 | 0 | } |
1094 | 0 | if (Priority != 65535) { |
1095 | 0 | Name += '.'; |
1096 | 0 | Name += utostr(Priority); |
1097 | 0 | } |
1098 | 0 | } else { |
1099 | | // The default scheme is .ctor / .dtor, so we have to invert the priority |
1100 | | // numbering. |
1101 | 0 | if (IsCtor) |
1102 | 0 | Name = ".ctors"; |
1103 | 0 | else |
1104 | 0 | Name = ".dtors"; |
1105 | 0 | if (Priority != 65535) |
1106 | 0 | raw_string_ostream(Name) << format(".%05u", 65535 - Priority); |
1107 | 0 | Type = ELF::SHT_PROGBITS; |
1108 | 0 | } |
1109 | |
|
1110 | 0 | return Ctx.getELFSection(Name, Type, Flags, 0, Comdat, /*IsComdat=*/true); |
1111 | 0 | } |
1112 | | |
1113 | | MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( |
1114 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
1115 | 0 | return getStaticStructorSection(getContext(), UseInitArray, true, Priority, |
1116 | 0 | KeySym); |
1117 | 0 | } |
1118 | | |
1119 | | MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( |
1120 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
1121 | 0 | return getStaticStructorSection(getContext(), UseInitArray, false, Priority, |
1122 | 0 | KeySym); |
1123 | 0 | } |
1124 | | |
1125 | | const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( |
1126 | | const GlobalValue *LHS, const GlobalValue *RHS, |
1127 | 0 | const TargetMachine &TM) const { |
1128 | | // We may only use a PLT-relative relocation to refer to unnamed_addr |
1129 | | // functions. |
1130 | 0 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
1131 | 0 | return nullptr; |
1132 | | |
1133 | | // Basic correctness checks. |
1134 | 0 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
1135 | 0 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
1136 | 0 | RHS->isThreadLocal()) |
1137 | 0 | return nullptr; |
1138 | | |
1139 | 0 | return MCBinaryExpr::createSub( |
1140 | 0 | MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, |
1141 | 0 | getContext()), |
1142 | 0 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
1143 | 0 | } |
1144 | | |
1145 | | const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent( |
1146 | 0 | const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const { |
1147 | 0 | assert(supportDSOLocalEquivalentLowering()); |
1148 | | |
1149 | 0 | const auto *GV = Equiv->getGlobalValue(); |
1150 | | |
1151 | | // A PLT entry is not needed for dso_local globals. |
1152 | 0 | if (GV->isDSOLocal() || GV->isImplicitDSOLocal()) |
1153 | 0 | return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); |
1154 | | |
1155 | 0 | return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeVariantKind, |
1156 | 0 | getContext()); |
1157 | 0 | } |
1158 | | |
1159 | 33.5k | MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { |
1160 | | // Use ".GCC.command.line" since this feature is to support clang's |
1161 | | // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the |
1162 | | // same name. |
1163 | 33.5k | return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, |
1164 | 33.5k | ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); |
1165 | 33.5k | } |
1166 | | |
1167 | | void |
1168 | 38.5k | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
1169 | 38.5k | UseInitArray = UseInitArray_; |
1170 | 38.5k | MCContext &Ctx = getContext(); |
1171 | 38.5k | if (!UseInitArray) { |
1172 | 0 | StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, |
1173 | 0 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
1174 | |
|
1175 | 0 | StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, |
1176 | 0 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
1177 | 0 | return; |
1178 | 0 | } |
1179 | | |
1180 | 38.5k | StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, |
1181 | 38.5k | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
1182 | 38.5k | StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, |
1183 | 38.5k | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
1184 | 38.5k | } |
1185 | | |
1186 | | //===----------------------------------------------------------------------===// |
1187 | | // MachO |
1188 | | //===----------------------------------------------------------------------===// |
1189 | | |
1190 | 0 | TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() { |
1191 | 0 | SupportIndirectSymViaGOTPCRel = true; |
1192 | 0 | } |
1193 | | |
1194 | | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
1195 | 0 | const TargetMachine &TM) { |
1196 | 0 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
1197 | 0 | if (TM.getRelocationModel() == Reloc::Static) { |
1198 | 0 | StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, |
1199 | 0 | SectionKind::getData()); |
1200 | 0 | StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, |
1201 | 0 | SectionKind::getData()); |
1202 | 0 | } else { |
1203 | 0 | StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", |
1204 | 0 | MachO::S_MOD_INIT_FUNC_POINTERS, |
1205 | 0 | SectionKind::getData()); |
1206 | 0 | StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", |
1207 | 0 | MachO::S_MOD_TERM_FUNC_POINTERS, |
1208 | 0 | SectionKind::getData()); |
1209 | 0 | } |
1210 | |
|
1211 | 0 | PersonalityEncoding = |
1212 | 0 | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
1213 | 0 | LSDAEncoding = dwarf::DW_EH_PE_pcrel; |
1214 | 0 | TTypeEncoding = |
1215 | 0 | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
1216 | 0 | } |
1217 | | |
1218 | | MCSection *TargetLoweringObjectFileMachO::getStaticDtorSection( |
1219 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
1220 | 0 | return StaticDtorSection; |
1221 | | // In userspace, we lower global destructors via atexit(), but kernel/kext |
1222 | | // environments do not provide this function so we still need to support the |
1223 | | // legacy way here. |
1224 | | // See the -disable-atexit-based-global-dtor-lowering CodeGen flag for more |
1225 | | // context. |
1226 | 0 | } |
1227 | | |
1228 | | void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, |
1229 | 0 | Module &M) const { |
1230 | | // Emit the linker options if present. |
1231 | 0 | if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
1232 | 0 | for (const auto *Option : LinkerOptions->operands()) { |
1233 | 0 | SmallVector<std::string, 4> StrOptions; |
1234 | 0 | for (const auto &Piece : cast<MDNode>(Option)->operands()) |
1235 | 0 | StrOptions.push_back(std::string(cast<MDString>(Piece)->getString())); |
1236 | 0 | Streamer.emitLinkerOptions(StrOptions); |
1237 | 0 | } |
1238 | 0 | } |
1239 | |
|
1240 | 0 | unsigned VersionVal = 0; |
1241 | 0 | unsigned ImageInfoFlags = 0; |
1242 | 0 | StringRef SectionVal; |
1243 | |
|
1244 | 0 | GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); |
1245 | 0 | emitCGProfileMetadata(Streamer, M); |
1246 | | |
1247 | | // The section is mandatory. If we don't have it, then we don't have GC info. |
1248 | 0 | if (SectionVal.empty()) |
1249 | 0 | return; |
1250 | | |
1251 | 0 | StringRef Segment, Section; |
1252 | 0 | unsigned TAA = 0, StubSize = 0; |
1253 | 0 | bool TAAParsed; |
1254 | 0 | if (Error E = MCSectionMachO::ParseSectionSpecifier( |
1255 | 0 | SectionVal, Segment, Section, TAA, TAAParsed, StubSize)) { |
1256 | | // If invalid, report the error with report_fatal_error. |
1257 | 0 | report_fatal_error("Invalid section specifier '" + Section + |
1258 | 0 | "': " + toString(std::move(E)) + "."); |
1259 | 0 | } |
1260 | | |
1261 | | // Get the section. |
1262 | 0 | MCSectionMachO *S = getContext().getMachOSection( |
1263 | 0 | Segment, Section, TAA, StubSize, SectionKind::getData()); |
1264 | 0 | Streamer.switchSection(S); |
1265 | 0 | Streamer.emitLabel(getContext(). |
1266 | 0 | getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
1267 | 0 | Streamer.emitInt32(VersionVal); |
1268 | 0 | Streamer.emitInt32(ImageInfoFlags); |
1269 | 0 | Streamer.addBlankLine(); |
1270 | 0 | } |
1271 | | |
1272 | 0 | static void checkMachOComdat(const GlobalValue *GV) { |
1273 | 0 | const Comdat *C = GV->getComdat(); |
1274 | 0 | if (!C) |
1275 | 0 | return; |
1276 | | |
1277 | 0 | report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + |
1278 | 0 | "' cannot be lowered."); |
1279 | 0 | } |
1280 | | |
1281 | | MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( |
1282 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
1283 | |
|
1284 | 0 | StringRef SectionName = GO->getSection(); |
1285 | |
|
1286 | 0 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); |
1287 | 0 | if (GV && GV->hasImplicitSection()) { |
1288 | 0 | auto Attrs = GV->getAttributes(); |
1289 | 0 | if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { |
1290 | 0 | SectionName = Attrs.getAttribute("bss-section").getValueAsString(); |
1291 | 0 | } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { |
1292 | 0 | SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); |
1293 | 0 | } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) { |
1294 | 0 | SectionName = Attrs.getAttribute("relro-section").getValueAsString(); |
1295 | 0 | } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { |
1296 | 0 | SectionName = Attrs.getAttribute("data-section").getValueAsString(); |
1297 | 0 | } |
1298 | 0 | } |
1299 | |
|
1300 | 0 | const Function *F = dyn_cast<Function>(GO); |
1301 | 0 | if (F && F->hasFnAttribute("implicit-section-name")) { |
1302 | 0 | SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); |
1303 | 0 | } |
1304 | | |
1305 | | // Parse the section specifier and create it if valid. |
1306 | 0 | StringRef Segment, Section; |
1307 | 0 | unsigned TAA = 0, StubSize = 0; |
1308 | 0 | bool TAAParsed; |
1309 | |
|
1310 | 0 | checkMachOComdat(GO); |
1311 | |
|
1312 | 0 | if (Error E = MCSectionMachO::ParseSectionSpecifier( |
1313 | 0 | SectionName, Segment, Section, TAA, TAAParsed, StubSize)) { |
1314 | | // If invalid, report the error with report_fatal_error. |
1315 | 0 | report_fatal_error("Global variable '" + GO->getName() + |
1316 | 0 | "' has an invalid section specifier '" + |
1317 | 0 | GO->getSection() + "': " + toString(std::move(E)) + "."); |
1318 | 0 | } |
1319 | | |
1320 | | // Get the section. |
1321 | 0 | MCSectionMachO *S = |
1322 | 0 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
1323 | | |
1324 | | // If TAA wasn't set by ParseSectionSpecifier() above, |
1325 | | // use the value returned by getMachOSection() as a default. |
1326 | 0 | if (!TAAParsed) |
1327 | 0 | TAA = S->getTypeAndAttributes(); |
1328 | | |
1329 | | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
1330 | | // If the user declared multiple globals with different section flags, we need |
1331 | | // to reject it here. |
1332 | 0 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
1333 | | // If invalid, report the error with report_fatal_error. |
1334 | 0 | report_fatal_error("Global variable '" + GO->getName() + |
1335 | 0 | "' section type or attributes does not match previous" |
1336 | 0 | " section specifier"); |
1337 | 0 | } |
1338 | |
|
1339 | 0 | return S; |
1340 | 0 | } |
1341 | | |
1342 | | MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( |
1343 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
1344 | 0 | checkMachOComdat(GO); |
1345 | | |
1346 | | // Handle thread local data. |
1347 | 0 | if (Kind.isThreadBSS()) return TLSBSSSection; |
1348 | 0 | if (Kind.isThreadData()) return TLSDataSection; |
1349 | | |
1350 | 0 | if (Kind.isText()) |
1351 | 0 | return GO->isWeakForLinker() ? TextCoalSection : TextSection; |
1352 | | |
1353 | | // If this is weak/linkonce, put this in a coalescable section, either in text |
1354 | | // or data depending on if it is writable. |
1355 | 0 | if (GO->isWeakForLinker()) { |
1356 | 0 | if (Kind.isReadOnly()) |
1357 | 0 | return ConstTextCoalSection; |
1358 | 0 | if (Kind.isReadOnlyWithRel()) |
1359 | 0 | return ConstDataCoalSection; |
1360 | 0 | return DataCoalSection; |
1361 | 0 | } |
1362 | | |
1363 | | // FIXME: Alignment check should be handled by section classifier. |
1364 | 0 | if (Kind.isMergeable1ByteCString() && |
1365 | 0 | GO->getParent()->getDataLayout().getPreferredAlign( |
1366 | 0 | cast<GlobalVariable>(GO)) < Align(32)) |
1367 | 0 | return CStringSection; |
1368 | | |
1369 | | // Do not put 16-bit arrays in the UString section if they have an |
1370 | | // externally visible label, this runs into issues with certain linker |
1371 | | // versions. |
1372 | 0 | if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && |
1373 | 0 | GO->getParent()->getDataLayout().getPreferredAlign( |
1374 | 0 | cast<GlobalVariable>(GO)) < Align(32)) |
1375 | 0 | return UStringSection; |
1376 | | |
1377 | | // With MachO only variables whose corresponding symbol starts with 'l' or |
1378 | | // 'L' can be merged, so we only try merging GVs with private linkage. |
1379 | 0 | if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { |
1380 | 0 | if (Kind.isMergeableConst4()) |
1381 | 0 | return FourByteConstantSection; |
1382 | 0 | if (Kind.isMergeableConst8()) |
1383 | 0 | return EightByteConstantSection; |
1384 | 0 | if (Kind.isMergeableConst16()) |
1385 | 0 | return SixteenByteConstantSection; |
1386 | 0 | } |
1387 | | |
1388 | | // Otherwise, if it is readonly, but not something we can specially optimize, |
1389 | | // just drop it in .const. |
1390 | 0 | if (Kind.isReadOnly()) |
1391 | 0 | return ReadOnlySection; |
1392 | | |
1393 | | // If this is marked const, put it into a const section. But if the dynamic |
1394 | | // linker needs to write to it, put it in the data segment. |
1395 | 0 | if (Kind.isReadOnlyWithRel()) |
1396 | 0 | return ConstDataSection; |
1397 | | |
1398 | | // Put zero initialized globals with strong external linkage in the |
1399 | | // DATA, __common section with the .zerofill directive. |
1400 | 0 | if (Kind.isBSSExtern()) |
1401 | 0 | return DataCommonSection; |
1402 | | |
1403 | | // Put zero initialized globals with local linkage in __DATA,__bss directive |
1404 | | // with the .zerofill directive (aka .lcomm). |
1405 | 0 | if (Kind.isBSSLocal()) |
1406 | 0 | return DataBSSSection; |
1407 | | |
1408 | | // Otherwise, just drop the variable in the normal data section. |
1409 | 0 | return DataSection; |
1410 | 0 | } |
1411 | | |
1412 | | MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( |
1413 | | const DataLayout &DL, SectionKind Kind, const Constant *C, |
1414 | 0 | Align &Alignment) const { |
1415 | | // If this constant requires a relocation, we have to put it in the data |
1416 | | // segment, not in the text segment. |
1417 | 0 | if (Kind.isData() || Kind.isReadOnlyWithRel()) |
1418 | 0 | return ConstDataSection; |
1419 | | |
1420 | 0 | if (Kind.isMergeableConst4()) |
1421 | 0 | return FourByteConstantSection; |
1422 | 0 | if (Kind.isMergeableConst8()) |
1423 | 0 | return EightByteConstantSection; |
1424 | 0 | if (Kind.isMergeableConst16()) |
1425 | 0 | return SixteenByteConstantSection; |
1426 | 0 | return ReadOnlySection; // .const |
1427 | 0 | } |
1428 | | |
1429 | 0 | MCSection *TargetLoweringObjectFileMachO::getSectionForCommandLines() const { |
1430 | 0 | return getContext().getMachOSection("__TEXT", "__command_line", 0, |
1431 | 0 | SectionKind::getReadOnly()); |
1432 | 0 | } |
1433 | | |
1434 | | const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
1435 | | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
1436 | 0 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
1437 | | // The mach-o version of this method defaults to returning a stub reference. |
1438 | |
|
1439 | 0 | if (Encoding & DW_EH_PE_indirect) { |
1440 | 0 | MachineModuleInfoMachO &MachOMMI = |
1441 | 0 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
1442 | |
|
1443 | 0 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
1444 | | |
1445 | | // Add information about the stub reference to MachOMMI so that the stub |
1446 | | // gets emitted by the asmprinter. |
1447 | 0 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
1448 | 0 | if (!StubSym.getPointer()) { |
1449 | 0 | MCSymbol *Sym = TM.getSymbol(GV); |
1450 | 0 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
1451 | 0 | } |
1452 | |
|
1453 | 0 | return TargetLoweringObjectFile:: |
1454 | 0 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
1455 | 0 | Encoding & ~DW_EH_PE_indirect, Streamer); |
1456 | 0 | } |
1457 | | |
1458 | 0 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
1459 | 0 | MMI, Streamer); |
1460 | 0 | } |
1461 | | |
1462 | | MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( |
1463 | | const GlobalValue *GV, const TargetMachine &TM, |
1464 | 0 | MachineModuleInfo *MMI) const { |
1465 | | // The mach-o version of this method defaults to returning a stub reference. |
1466 | 0 | MachineModuleInfoMachO &MachOMMI = |
1467 | 0 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
1468 | |
|
1469 | 0 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
1470 | | |
1471 | | // Add information about the stub reference to MachOMMI so that the stub |
1472 | | // gets emitted by the asmprinter. |
1473 | 0 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
1474 | 0 | if (!StubSym.getPointer()) { |
1475 | 0 | MCSymbol *Sym = TM.getSymbol(GV); |
1476 | 0 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
1477 | 0 | } |
1478 | |
|
1479 | 0 | return SSym; |
1480 | 0 | } |
1481 | | |
1482 | | const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( |
1483 | | const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, |
1484 | 0 | int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
1485 | | // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation |
1486 | | // as 64-bit do, we replace the GOT equivalent by accessing the final symbol |
1487 | | // through a non_lazy_ptr stub instead. One advantage is that it allows the |
1488 | | // computation of deltas to final external symbols. Example: |
1489 | | // |
1490 | | // _extgotequiv: |
1491 | | // .long _extfoo |
1492 | | // |
1493 | | // _delta: |
1494 | | // .long _extgotequiv-_delta |
1495 | | // |
1496 | | // is transformed to: |
1497 | | // |
1498 | | // _delta: |
1499 | | // .long L_extfoo$non_lazy_ptr-(_delta+0) |
1500 | | // |
1501 | | // .section __IMPORT,__pointers,non_lazy_symbol_pointers |
1502 | | // L_extfoo$non_lazy_ptr: |
1503 | | // .indirect_symbol _extfoo |
1504 | | // .long 0 |
1505 | | // |
1506 | | // The indirect symbol table (and sections of non_lazy_symbol_pointers type) |
1507 | | // may point to both local (same translation unit) and global (other |
1508 | | // translation units) symbols. Example: |
1509 | | // |
1510 | | // .section __DATA,__pointers,non_lazy_symbol_pointers |
1511 | | // L1: |
1512 | | // .indirect_symbol _myGlobal |
1513 | | // .long 0 |
1514 | | // L2: |
1515 | | // .indirect_symbol _myLocal |
1516 | | // .long _myLocal |
1517 | | // |
1518 | | // If the symbol is local, instead of the symbol's index, the assembler |
1519 | | // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. |
1520 | | // Then the linker will notice the constant in the table and will look at the |
1521 | | // content of the symbol. |
1522 | 0 | MachineModuleInfoMachO &MachOMMI = |
1523 | 0 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
1524 | 0 | MCContext &Ctx = getContext(); |
1525 | | |
1526 | | // The offset must consider the original displacement from the base symbol |
1527 | | // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. |
1528 | 0 | Offset = -MV.getConstant(); |
1529 | 0 | const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); |
1530 | | |
1531 | | // Access the final symbol via sym$non_lazy_ptr and generate the appropriated |
1532 | | // non_lazy_ptr stubs. |
1533 | 0 | SmallString<128> Name; |
1534 | 0 | StringRef Suffix = "$non_lazy_ptr"; |
1535 | 0 | Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); |
1536 | 0 | Name += Sym->getName(); |
1537 | 0 | Name += Suffix; |
1538 | 0 | MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); |
1539 | |
|
1540 | 0 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); |
1541 | |
|
1542 | 0 | if (!StubSym.getPointer()) |
1543 | 0 | StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), |
1544 | 0 | !GV->hasLocalLinkage()); |
1545 | |
|
1546 | 0 | const MCExpr *BSymExpr = |
1547 | 0 | MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); |
1548 | 0 | const MCExpr *LHS = |
1549 | 0 | MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); |
1550 | |
|
1551 | 0 | if (!Offset) |
1552 | 0 | return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); |
1553 | | |
1554 | 0 | const MCExpr *RHS = |
1555 | 0 | MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); |
1556 | 0 | return MCBinaryExpr::createSub(LHS, RHS, Ctx); |
1557 | 0 | } |
1558 | | |
1559 | | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
1560 | 0 | const MCSection &Section) { |
1561 | 0 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
1562 | 0 | return true; |
1563 | | |
1564 | | // FIXME: we should be able to use private labels for sections that can't be |
1565 | | // dead-stripped (there's no issue with blocking atomization there), but `ld |
1566 | | // -r` sometimes drops the no_dead_strip attribute from sections so for safety |
1567 | | // we don't allow it. |
1568 | 0 | return false; |
1569 | 0 | } |
1570 | | |
1571 | | void TargetLoweringObjectFileMachO::getNameWithPrefix( |
1572 | | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
1573 | 0 | const TargetMachine &TM) const { |
1574 | 0 | bool CannotUsePrivateLabel = true; |
1575 | 0 | if (auto *GO = GV->getAliaseeObject()) { |
1576 | 0 | SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); |
1577 | 0 | const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); |
1578 | 0 | CannotUsePrivateLabel = |
1579 | 0 | !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); |
1580 | 0 | } |
1581 | 0 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
1582 | 0 | } |
1583 | | |
1584 | | //===----------------------------------------------------------------------===// |
1585 | | // COFF |
1586 | | //===----------------------------------------------------------------------===// |
1587 | | |
1588 | | static unsigned |
1589 | 0 | getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { |
1590 | 0 | unsigned Flags = 0; |
1591 | 0 | bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; |
1592 | |
|
1593 | 0 | if (K.isMetadata()) |
1594 | 0 | Flags |= |
1595 | 0 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
1596 | 0 | else if (K.isExclude()) |
1597 | 0 | Flags |= |
1598 | 0 | COFF::IMAGE_SCN_LNK_REMOVE | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
1599 | 0 | else if (K.isText()) |
1600 | 0 | Flags |= |
1601 | 0 | COFF::IMAGE_SCN_MEM_EXECUTE | |
1602 | 0 | COFF::IMAGE_SCN_MEM_READ | |
1603 | 0 | COFF::IMAGE_SCN_CNT_CODE | |
1604 | 0 | (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); |
1605 | 0 | else if (K.isBSS()) |
1606 | 0 | Flags |= |
1607 | 0 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
1608 | 0 | COFF::IMAGE_SCN_MEM_READ | |
1609 | 0 | COFF::IMAGE_SCN_MEM_WRITE; |
1610 | 0 | else if (K.isThreadLocal()) |
1611 | 0 | Flags |= |
1612 | 0 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1613 | 0 | COFF::IMAGE_SCN_MEM_READ | |
1614 | 0 | COFF::IMAGE_SCN_MEM_WRITE; |
1615 | 0 | else if (K.isReadOnly() || K.isReadOnlyWithRel()) |
1616 | 0 | Flags |= |
1617 | 0 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1618 | 0 | COFF::IMAGE_SCN_MEM_READ; |
1619 | 0 | else if (K.isWriteable()) |
1620 | 0 | Flags |= |
1621 | 0 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1622 | 0 | COFF::IMAGE_SCN_MEM_READ | |
1623 | 0 | COFF::IMAGE_SCN_MEM_WRITE; |
1624 | |
|
1625 | 0 | return Flags; |
1626 | 0 | } |
1627 | | |
1628 | 0 | static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { |
1629 | 0 | const Comdat *C = GV->getComdat(); |
1630 | 0 | assert(C && "expected GV to have a Comdat!"); |
1631 | | |
1632 | 0 | StringRef ComdatGVName = C->getName(); |
1633 | 0 | const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); |
1634 | 0 | if (!ComdatGV) |
1635 | 0 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
1636 | 0 | "' does not exist."); |
1637 | |
|
1638 | 0 | if (ComdatGV->getComdat() != C) |
1639 | 0 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
1640 | 0 | "' is not a key for its COMDAT."); |
1641 | |
|
1642 | 0 | return ComdatGV; |
1643 | 0 | } |
1644 | | |
1645 | 0 | static int getSelectionForCOFF(const GlobalValue *GV) { |
1646 | 0 | if (const Comdat *C = GV->getComdat()) { |
1647 | 0 | const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); |
1648 | 0 | if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) |
1649 | 0 | ComdatKey = GA->getAliaseeObject(); |
1650 | 0 | if (ComdatKey == GV) { |
1651 | 0 | switch (C->getSelectionKind()) { |
1652 | 0 | case Comdat::Any: |
1653 | 0 | return COFF::IMAGE_COMDAT_SELECT_ANY; |
1654 | 0 | case Comdat::ExactMatch: |
1655 | 0 | return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; |
1656 | 0 | case Comdat::Largest: |
1657 | 0 | return COFF::IMAGE_COMDAT_SELECT_LARGEST; |
1658 | 0 | case Comdat::NoDeduplicate: |
1659 | 0 | return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
1660 | 0 | case Comdat::SameSize: |
1661 | 0 | return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; |
1662 | 0 | } |
1663 | 0 | } else { |
1664 | 0 | return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
1665 | 0 | } |
1666 | 0 | } |
1667 | 0 | return 0; |
1668 | 0 | } |
1669 | | |
1670 | | MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( |
1671 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
1672 | 0 | StringRef Name = GO->getSection(); |
1673 | 0 | if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF, |
1674 | 0 | /*AddSegmentInfo=*/false) || |
1675 | 0 | Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF, |
1676 | 0 | /*AddSegmentInfo=*/false) || |
1677 | 0 | Name == getInstrProfSectionName(IPSK_covdata, Triple::COFF, |
1678 | 0 | /*AddSegmentInfo=*/false) || |
1679 | 0 | Name == getInstrProfSectionName(IPSK_covname, Triple::COFF, |
1680 | 0 | /*AddSegmentInfo=*/false)) |
1681 | 0 | Kind = SectionKind::getMetadata(); |
1682 | 0 | int Selection = 0; |
1683 | 0 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
1684 | 0 | StringRef COMDATSymName = ""; |
1685 | 0 | if (GO->hasComdat()) { |
1686 | 0 | Selection = getSelectionForCOFF(GO); |
1687 | 0 | const GlobalValue *ComdatGV; |
1688 | 0 | if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
1689 | 0 | ComdatGV = getComdatGVForCOFF(GO); |
1690 | 0 | else |
1691 | 0 | ComdatGV = GO; |
1692 | |
|
1693 | 0 | if (!ComdatGV->hasPrivateLinkage()) { |
1694 | 0 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
1695 | 0 | COMDATSymName = Sym->getName(); |
1696 | 0 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
1697 | 0 | } else { |
1698 | 0 | Selection = 0; |
1699 | 0 | } |
1700 | 0 | } |
1701 | |
|
1702 | 0 | return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, |
1703 | 0 | Selection); |
1704 | 0 | } |
1705 | | |
1706 | 0 | static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { |
1707 | 0 | if (Kind.isText()) |
1708 | 0 | return ".text"; |
1709 | 0 | if (Kind.isBSS()) |
1710 | 0 | return ".bss"; |
1711 | 0 | if (Kind.isThreadLocal()) |
1712 | 0 | return ".tls$"; |
1713 | 0 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
1714 | 0 | return ".rdata"; |
1715 | 0 | return ".data"; |
1716 | 0 | } |
1717 | | |
1718 | | MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( |
1719 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
1720 | | // If we have -ffunction-sections then we should emit the global value to a |
1721 | | // uniqued section specifically for it. |
1722 | 0 | bool EmitUniquedSection; |
1723 | 0 | if (Kind.isText()) |
1724 | 0 | EmitUniquedSection = TM.getFunctionSections(); |
1725 | 0 | else |
1726 | 0 | EmitUniquedSection = TM.getDataSections(); |
1727 | |
|
1728 | 0 | if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { |
1729 | 0 | SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); |
1730 | |
|
1731 | 0 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
1732 | |
|
1733 | 0 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
1734 | 0 | int Selection = getSelectionForCOFF(GO); |
1735 | 0 | if (!Selection) |
1736 | 0 | Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
1737 | 0 | const GlobalValue *ComdatGV; |
1738 | 0 | if (GO->hasComdat()) |
1739 | 0 | ComdatGV = getComdatGVForCOFF(GO); |
1740 | 0 | else |
1741 | 0 | ComdatGV = GO; |
1742 | |
|
1743 | 0 | unsigned UniqueID = MCContext::GenericSectionID; |
1744 | 0 | if (EmitUniquedSection) |
1745 | 0 | UniqueID = NextUniqueID++; |
1746 | |
|
1747 | 0 | if (!ComdatGV->hasPrivateLinkage()) { |
1748 | 0 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
1749 | 0 | StringRef COMDATSymName = Sym->getName(); |
1750 | |
|
1751 | 0 | if (const auto *F = dyn_cast<Function>(GO)) |
1752 | 0 | if (std::optional<StringRef> Prefix = F->getSectionPrefix()) |
1753 | 0 | raw_svector_ostream(Name) << '$' << *Prefix; |
1754 | | |
1755 | | // Append "$symbol" to the section name *before* IR-level mangling is |
1756 | | // applied when targetting mingw. This is what GCC does, and the ld.bfd |
1757 | | // COFF linker will not properly handle comdats otherwise. |
1758 | 0 | if (getContext().getTargetTriple().isWindowsGNUEnvironment()) |
1759 | 0 | raw_svector_ostream(Name) << '$' << ComdatGV->getName(); |
1760 | |
|
1761 | 0 | return getContext().getCOFFSection(Name, Characteristics, Kind, |
1762 | 0 | COMDATSymName, Selection, UniqueID); |
1763 | 0 | } else { |
1764 | 0 | SmallString<256> TmpData; |
1765 | 0 | getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); |
1766 | 0 | return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, |
1767 | 0 | Selection, UniqueID); |
1768 | 0 | } |
1769 | 0 | } |
1770 | | |
1771 | 0 | if (Kind.isText()) |
1772 | 0 | return TextSection; |
1773 | | |
1774 | 0 | if (Kind.isThreadLocal()) |
1775 | 0 | return TLSDataSection; |
1776 | | |
1777 | 0 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
1778 | 0 | return ReadOnlySection; |
1779 | | |
1780 | | // Note: we claim that common symbols are put in BSSSection, but they are |
1781 | | // really emitted with the magic .comm directive, which creates a symbol table |
1782 | | // entry but not a section. |
1783 | 0 | if (Kind.isBSS() || Kind.isCommon()) |
1784 | 0 | return BSSSection; |
1785 | | |
1786 | 0 | return DataSection; |
1787 | 0 | } |
1788 | | |
1789 | | void TargetLoweringObjectFileCOFF::getNameWithPrefix( |
1790 | | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
1791 | 0 | const TargetMachine &TM) const { |
1792 | 0 | bool CannotUsePrivateLabel = false; |
1793 | 0 | if (GV->hasPrivateLinkage() && |
1794 | 0 | ((isa<Function>(GV) && TM.getFunctionSections()) || |
1795 | 0 | (isa<GlobalVariable>(GV) && TM.getDataSections()))) |
1796 | 0 | CannotUsePrivateLabel = true; |
1797 | |
|
1798 | 0 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
1799 | 0 | } |
1800 | | |
1801 | | MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( |
1802 | 0 | const Function &F, const TargetMachine &TM) const { |
1803 | | // If the function can be removed, produce a unique section so that |
1804 | | // the table doesn't prevent the removal. |
1805 | 0 | const Comdat *C = F.getComdat(); |
1806 | 0 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
1807 | 0 | if (!EmitUniqueSection) |
1808 | 0 | return ReadOnlySection; |
1809 | | |
1810 | | // FIXME: we should produce a symbol for F instead. |
1811 | 0 | if (F.hasPrivateLinkage()) |
1812 | 0 | return ReadOnlySection; |
1813 | | |
1814 | 0 | MCSymbol *Sym = TM.getSymbol(&F); |
1815 | 0 | StringRef COMDATSymName = Sym->getName(); |
1816 | |
|
1817 | 0 | SectionKind Kind = SectionKind::getReadOnly(); |
1818 | 0 | StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); |
1819 | 0 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
1820 | 0 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
1821 | 0 | unsigned UniqueID = NextUniqueID++; |
1822 | |
|
1823 | 0 | return getContext().getCOFFSection( |
1824 | 0 | SecName, Characteristics, Kind, COMDATSymName, |
1825 | 0 | COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); |
1826 | 0 | } |
1827 | | |
1828 | | bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection( |
1829 | 0 | bool UsesLabelDifference, const Function &F) const { |
1830 | 0 | if (TM->getTargetTriple().getArch() == Triple::x86_64) { |
1831 | 0 | if (!JumpTableInFunctionSection) { |
1832 | | // We can always create relative relocations, so use another section |
1833 | | // that can be marked non-executable. |
1834 | 0 | return false; |
1835 | 0 | } |
1836 | 0 | } |
1837 | 0 | return TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection( |
1838 | 0 | UsesLabelDifference, F); |
1839 | 0 | } |
1840 | | |
1841 | | void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, |
1842 | 0 | Module &M) const { |
1843 | 0 | emitLinkerDirectives(Streamer, M); |
1844 | |
|
1845 | 0 | unsigned Version = 0; |
1846 | 0 | unsigned Flags = 0; |
1847 | 0 | StringRef Section; |
1848 | |
|
1849 | 0 | GetObjCImageInfo(M, Version, Flags, Section); |
1850 | 0 | if (!Section.empty()) { |
1851 | 0 | auto &C = getContext(); |
1852 | 0 | auto *S = C.getCOFFSection(Section, |
1853 | 0 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1854 | 0 | COFF::IMAGE_SCN_MEM_READ, |
1855 | 0 | SectionKind::getReadOnly()); |
1856 | 0 | Streamer.switchSection(S); |
1857 | 0 | Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
1858 | 0 | Streamer.emitInt32(Version); |
1859 | 0 | Streamer.emitInt32(Flags); |
1860 | 0 | Streamer.addBlankLine(); |
1861 | 0 | } |
1862 | |
|
1863 | 0 | emitCGProfileMetadata(Streamer, M); |
1864 | 0 | } |
1865 | | |
1866 | | void TargetLoweringObjectFileCOFF::emitLinkerDirectives( |
1867 | 0 | MCStreamer &Streamer, Module &M) const { |
1868 | 0 | if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
1869 | | // Emit the linker options to the linker .drectve section. According to the |
1870 | | // spec, this section is a space-separated string containing flags for |
1871 | | // linker. |
1872 | 0 | MCSection *Sec = getDrectveSection(); |
1873 | 0 | Streamer.switchSection(Sec); |
1874 | 0 | for (const auto *Option : LinkerOptions->operands()) { |
1875 | 0 | for (const auto &Piece : cast<MDNode>(Option)->operands()) { |
1876 | | // Lead with a space for consistency with our dllexport implementation. |
1877 | 0 | std::string Directive(" "); |
1878 | 0 | Directive.append(std::string(cast<MDString>(Piece)->getString())); |
1879 | 0 | Streamer.emitBytes(Directive); |
1880 | 0 | } |
1881 | 0 | } |
1882 | 0 | } |
1883 | | |
1884 | | // Emit /EXPORT: flags for each exported global as necessary. |
1885 | 0 | std::string Flags; |
1886 | 0 | for (const GlobalValue &GV : M.global_values()) { |
1887 | 0 | raw_string_ostream OS(Flags); |
1888 | 0 | emitLinkerFlagsForGlobalCOFF(OS, &GV, getContext().getTargetTriple(), |
1889 | 0 | getMangler()); |
1890 | 0 | OS.flush(); |
1891 | 0 | if (!Flags.empty()) { |
1892 | 0 | Streamer.switchSection(getDrectveSection()); |
1893 | 0 | Streamer.emitBytes(Flags); |
1894 | 0 | } |
1895 | 0 | Flags.clear(); |
1896 | 0 | } |
1897 | | |
1898 | | // Emit /INCLUDE: flags for each used global as necessary. |
1899 | 0 | if (const auto *LU = M.getNamedGlobal("llvm.used")) { |
1900 | 0 | assert(LU->hasInitializer() && "expected llvm.used to have an initializer"); |
1901 | 0 | assert(isa<ArrayType>(LU->getValueType()) && |
1902 | 0 | "expected llvm.used to be an array type"); |
1903 | 0 | if (const auto *A = cast<ConstantArray>(LU->getInitializer())) { |
1904 | 0 | for (const Value *Op : A->operands()) { |
1905 | 0 | const auto *GV = cast<GlobalValue>(Op->stripPointerCasts()); |
1906 | | // Global symbols with internal or private linkage are not visible to |
1907 | | // the linker, and thus would cause an error when the linker tried to |
1908 | | // preserve the symbol due to the `/include:` directive. |
1909 | 0 | if (GV->hasLocalLinkage()) |
1910 | 0 | continue; |
1911 | | |
1912 | 0 | raw_string_ostream OS(Flags); |
1913 | 0 | emitLinkerFlagsForUsedCOFF(OS, GV, getContext().getTargetTriple(), |
1914 | 0 | getMangler()); |
1915 | 0 | OS.flush(); |
1916 | |
|
1917 | 0 | if (!Flags.empty()) { |
1918 | 0 | Streamer.switchSection(getDrectveSection()); |
1919 | 0 | Streamer.emitBytes(Flags); |
1920 | 0 | } |
1921 | 0 | Flags.clear(); |
1922 | 0 | } |
1923 | 0 | } |
1924 | 0 | } |
1925 | 0 | } |
1926 | | |
1927 | | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
1928 | 0 | const TargetMachine &TM) { |
1929 | 0 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
1930 | 0 | this->TM = &TM; |
1931 | 0 | const Triple &T = TM.getTargetTriple(); |
1932 | 0 | if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { |
1933 | 0 | StaticCtorSection = |
1934 | 0 | Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1935 | 0 | COFF::IMAGE_SCN_MEM_READ, |
1936 | 0 | SectionKind::getReadOnly()); |
1937 | 0 | StaticDtorSection = |
1938 | 0 | Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1939 | 0 | COFF::IMAGE_SCN_MEM_READ, |
1940 | 0 | SectionKind::getReadOnly()); |
1941 | 0 | } else { |
1942 | 0 | StaticCtorSection = Ctx.getCOFFSection( |
1943 | 0 | ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1944 | 0 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
1945 | 0 | SectionKind::getData()); |
1946 | 0 | StaticDtorSection = Ctx.getCOFFSection( |
1947 | 0 | ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1948 | 0 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
1949 | 0 | SectionKind::getData()); |
1950 | 0 | } |
1951 | 0 | } |
1952 | | |
1953 | | static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, |
1954 | | const Triple &T, bool IsCtor, |
1955 | | unsigned Priority, |
1956 | | const MCSymbol *KeySym, |
1957 | 0 | MCSectionCOFF *Default) { |
1958 | 0 | if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { |
1959 | | // If the priority is the default, use .CRT$XCU, possibly associative. |
1960 | 0 | if (Priority == 65535) |
1961 | 0 | return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); |
1962 | | |
1963 | | // Otherwise, we need to compute a new section name. Low priorities should |
1964 | | // run earlier. The linker will sort sections ASCII-betically, and we need a |
1965 | | // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we |
1966 | | // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really |
1967 | | // low priorities need to sort before 'L', since the CRT uses that |
1968 | | // internally, so we use ".CRT$XCA00001" for them. We have a contract with |
1969 | | // the frontend that "init_seg(compiler)" corresponds to priority 200 and |
1970 | | // "init_seg(lib)" corresponds to priority 400, and those respectively use |
1971 | | // 'C' and 'L' without the priority suffix. Priorities between 200 and 400 |
1972 | | // use 'C' with the priority as a suffix. |
1973 | 0 | SmallString<24> Name; |
1974 | 0 | char LastLetter = 'T'; |
1975 | 0 | bool AddPrioritySuffix = Priority != 200 && Priority != 400; |
1976 | 0 | if (Priority < 200) |
1977 | 0 | LastLetter = 'A'; |
1978 | 0 | else if (Priority < 400) |
1979 | 0 | LastLetter = 'C'; |
1980 | 0 | else if (Priority == 400) |
1981 | 0 | LastLetter = 'L'; |
1982 | 0 | raw_svector_ostream OS(Name); |
1983 | 0 | OS << ".CRT$X" << (IsCtor ? "C" : "T") << LastLetter; |
1984 | 0 | if (AddPrioritySuffix) |
1985 | 0 | OS << format("%05u", Priority); |
1986 | 0 | MCSectionCOFF *Sec = Ctx.getCOFFSection( |
1987 | 0 | Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, |
1988 | 0 | SectionKind::getReadOnly()); |
1989 | 0 | return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); |
1990 | 0 | } |
1991 | | |
1992 | 0 | std::string Name = IsCtor ? ".ctors" : ".dtors"; |
1993 | 0 | if (Priority != 65535) |
1994 | 0 | raw_string_ostream(Name) << format(".%05u", 65535 - Priority); |
1995 | |
|
1996 | 0 | return Ctx.getAssociativeCOFFSection( |
1997 | 0 | Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
1998 | 0 | COFF::IMAGE_SCN_MEM_READ | |
1999 | 0 | COFF::IMAGE_SCN_MEM_WRITE, |
2000 | 0 | SectionKind::getData()), |
2001 | 0 | KeySym, 0); |
2002 | 0 | } |
2003 | | |
2004 | | MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( |
2005 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2006 | 0 | return getCOFFStaticStructorSection( |
2007 | 0 | getContext(), getContext().getTargetTriple(), true, Priority, KeySym, |
2008 | 0 | cast<MCSectionCOFF>(StaticCtorSection)); |
2009 | 0 | } |
2010 | | |
2011 | | MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( |
2012 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2013 | 0 | return getCOFFStaticStructorSection( |
2014 | 0 | getContext(), getContext().getTargetTriple(), false, Priority, KeySym, |
2015 | 0 | cast<MCSectionCOFF>(StaticDtorSection)); |
2016 | 0 | } |
2017 | | |
2018 | | const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( |
2019 | | const GlobalValue *LHS, const GlobalValue *RHS, |
2020 | 0 | const TargetMachine &TM) const { |
2021 | 0 | const Triple &T = TM.getTargetTriple(); |
2022 | 0 | if (T.isOSCygMing()) |
2023 | 0 | return nullptr; |
2024 | | |
2025 | | // Our symbols should exist in address space zero, cowardly no-op if |
2026 | | // otherwise. |
2027 | 0 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
2028 | 0 | RHS->getType()->getPointerAddressSpace() != 0) |
2029 | 0 | return nullptr; |
2030 | | |
2031 | | // Both ptrtoint instructions must wrap global objects: |
2032 | | // - Only global variables are eligible for image relative relocations. |
2033 | | // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. |
2034 | | // We expect __ImageBase to be a global variable without a section, externally |
2035 | | // defined. |
2036 | | // |
2037 | | // It should look something like this: @__ImageBase = external constant i8 |
2038 | 0 | if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || |
2039 | 0 | LHS->isThreadLocal() || RHS->isThreadLocal() || |
2040 | 0 | RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || |
2041 | 0 | cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) |
2042 | 0 | return nullptr; |
2043 | | |
2044 | 0 | return MCSymbolRefExpr::create(TM.getSymbol(LHS), |
2045 | 0 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
2046 | 0 | getContext()); |
2047 | 0 | } |
2048 | | |
2049 | 0 | static std::string APIntToHexString(const APInt &AI) { |
2050 | 0 | unsigned Width = (AI.getBitWidth() / 8) * 2; |
2051 | 0 | std::string HexString = toString(AI, 16, /*Signed=*/false); |
2052 | 0 | llvm::transform(HexString, HexString.begin(), tolower); |
2053 | 0 | unsigned Size = HexString.size(); |
2054 | 0 | assert(Width >= Size && "hex string is too large!"); |
2055 | 0 | HexString.insert(HexString.begin(), Width - Size, '0'); |
2056 | |
|
2057 | 0 | return HexString; |
2058 | 0 | } |
2059 | | |
2060 | 0 | static std::string scalarConstantToHexString(const Constant *C) { |
2061 | 0 | Type *Ty = C->getType(); |
2062 | 0 | if (isa<UndefValue>(C)) { |
2063 | 0 | return APIntToHexString(APInt::getZero(Ty->getPrimitiveSizeInBits())); |
2064 | 0 | } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { |
2065 | 0 | return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); |
2066 | 0 | } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { |
2067 | 0 | return APIntToHexString(CI->getValue()); |
2068 | 0 | } else { |
2069 | 0 | unsigned NumElements; |
2070 | 0 | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
2071 | 0 | NumElements = cast<FixedVectorType>(VTy)->getNumElements(); |
2072 | 0 | else |
2073 | 0 | NumElements = Ty->getArrayNumElements(); |
2074 | 0 | std::string HexString; |
2075 | 0 | for (int I = NumElements - 1, E = -1; I != E; --I) |
2076 | 0 | HexString += scalarConstantToHexString(C->getAggregateElement(I)); |
2077 | 0 | return HexString; |
2078 | 0 | } |
2079 | 0 | } |
2080 | | |
2081 | | MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( |
2082 | | const DataLayout &DL, SectionKind Kind, const Constant *C, |
2083 | 0 | Align &Alignment) const { |
2084 | 0 | if (Kind.isMergeableConst() && C && |
2085 | 0 | getContext().getAsmInfo()->hasCOFFComdatConstants()) { |
2086 | | // This creates comdat sections with the given symbol name, but unless |
2087 | | // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol |
2088 | | // will be created with a null storage class, which makes GNU binutils |
2089 | | // error out. |
2090 | 0 | const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
2091 | 0 | COFF::IMAGE_SCN_MEM_READ | |
2092 | 0 | COFF::IMAGE_SCN_LNK_COMDAT; |
2093 | 0 | std::string COMDATSymName; |
2094 | 0 | if (Kind.isMergeableConst4()) { |
2095 | 0 | if (Alignment <= 4) { |
2096 | 0 | COMDATSymName = "__real@" + scalarConstantToHexString(C); |
2097 | 0 | Alignment = Align(4); |
2098 | 0 | } |
2099 | 0 | } else if (Kind.isMergeableConst8()) { |
2100 | 0 | if (Alignment <= 8) { |
2101 | 0 | COMDATSymName = "__real@" + scalarConstantToHexString(C); |
2102 | 0 | Alignment = Align(8); |
2103 | 0 | } |
2104 | 0 | } else if (Kind.isMergeableConst16()) { |
2105 | | // FIXME: These may not be appropriate for non-x86 architectures. |
2106 | 0 | if (Alignment <= 16) { |
2107 | 0 | COMDATSymName = "__xmm@" + scalarConstantToHexString(C); |
2108 | 0 | Alignment = Align(16); |
2109 | 0 | } |
2110 | 0 | } else if (Kind.isMergeableConst32()) { |
2111 | 0 | if (Alignment <= 32) { |
2112 | 0 | COMDATSymName = "__ymm@" + scalarConstantToHexString(C); |
2113 | 0 | Alignment = Align(32); |
2114 | 0 | } |
2115 | 0 | } |
2116 | |
|
2117 | 0 | if (!COMDATSymName.empty()) |
2118 | 0 | return getContext().getCOFFSection(".rdata", Characteristics, Kind, |
2119 | 0 | COMDATSymName, |
2120 | 0 | COFF::IMAGE_COMDAT_SELECT_ANY); |
2121 | 0 | } |
2122 | | |
2123 | 0 | return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, |
2124 | 0 | Alignment); |
2125 | 0 | } |
2126 | | |
2127 | | //===----------------------------------------------------------------------===// |
2128 | | // Wasm |
2129 | | //===----------------------------------------------------------------------===// |
2130 | | |
2131 | 6.83k | static const Comdat *getWasmComdat(const GlobalValue *GV) { |
2132 | 6.83k | const Comdat *C = GV->getComdat(); |
2133 | 6.83k | if (!C) |
2134 | 6.83k | return nullptr; |
2135 | | |
2136 | 0 | if (C->getSelectionKind() != Comdat::Any) |
2137 | 0 | report_fatal_error("WebAssembly COMDATs only support " |
2138 | 0 | "SelectionKind::Any, '" + C->getName() + "' cannot be " |
2139 | 0 | "lowered."); |
2140 | |
|
2141 | 0 | return C; |
2142 | 6.83k | } |
2143 | | |
2144 | 6.83k | static unsigned getWasmSectionFlags(SectionKind K) { |
2145 | 6.83k | unsigned Flags = 0; |
2146 | | |
2147 | 6.83k | if (K.isThreadLocal()) |
2148 | 0 | Flags |= wasm::WASM_SEG_FLAG_TLS; |
2149 | | |
2150 | 6.83k | if (K.isMergeableCString()) |
2151 | 50 | Flags |= wasm::WASM_SEG_FLAG_STRINGS; |
2152 | | |
2153 | | // TODO(sbc): Add suport for K.isMergeableConst() |
2154 | | |
2155 | 6.83k | return Flags; |
2156 | 6.83k | } |
2157 | | |
2158 | | MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( |
2159 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2160 | | // We don't support explict section names for functions in the wasm object |
2161 | | // format. Each function has to be in its own unique section. |
2162 | 0 | if (isa<Function>(GO)) { |
2163 | 0 | return SelectSectionForGlobal(GO, Kind, TM); |
2164 | 0 | } |
2165 | | |
2166 | 0 | StringRef Name = GO->getSection(); |
2167 | | |
2168 | | // Certain data sections we treat as named custom sections rather than |
2169 | | // segments within the data section. |
2170 | | // This could be avoided if all data segements (the wasm sense) were |
2171 | | // represented as their own sections (in the llvm sense). |
2172 | | // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 |
2173 | 0 | if (Name == ".llvmcmd" || Name == ".llvmbc") |
2174 | 0 | Kind = SectionKind::getMetadata(); |
2175 | |
|
2176 | 0 | StringRef Group = ""; |
2177 | 0 | if (const Comdat *C = getWasmComdat(GO)) { |
2178 | 0 | Group = C->getName(); |
2179 | 0 | } |
2180 | |
|
2181 | 0 | unsigned Flags = getWasmSectionFlags(Kind); |
2182 | 0 | MCSectionWasm *Section = getContext().getWasmSection( |
2183 | 0 | Name, Kind, Flags, Group, MCContext::GenericSectionID); |
2184 | |
|
2185 | 0 | return Section; |
2186 | 0 | } |
2187 | | |
2188 | | static MCSectionWasm *selectWasmSectionForGlobal( |
2189 | | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
2190 | 6.83k | const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { |
2191 | 6.83k | StringRef Group = ""; |
2192 | 6.83k | if (const Comdat *C = getWasmComdat(GO)) { |
2193 | 0 | Group = C->getName(); |
2194 | 0 | } |
2195 | | |
2196 | 6.83k | bool UniqueSectionNames = TM.getUniqueSectionNames(); |
2197 | 6.83k | SmallString<128> Name = getSectionPrefixForGlobal(Kind, /*IsLarge=*/false); |
2198 | | |
2199 | 6.83k | if (const auto *F = dyn_cast<Function>(GO)) { |
2200 | 6.72k | const auto &OptionalPrefix = F->getSectionPrefix(); |
2201 | 6.72k | if (OptionalPrefix) |
2202 | 0 | raw_svector_ostream(Name) << '.' << *OptionalPrefix; |
2203 | 6.72k | } |
2204 | | |
2205 | 6.83k | if (EmitUniqueSection && UniqueSectionNames) { |
2206 | 6.83k | Name.push_back('.'); |
2207 | 6.83k | TM.getNameWithPrefix(Name, GO, Mang, true); |
2208 | 6.83k | } |
2209 | 6.83k | unsigned UniqueID = MCContext::GenericSectionID; |
2210 | 6.83k | if (EmitUniqueSection && !UniqueSectionNames) { |
2211 | 0 | UniqueID = *NextUniqueID; |
2212 | 0 | (*NextUniqueID)++; |
2213 | 0 | } |
2214 | | |
2215 | 6.83k | unsigned Flags = getWasmSectionFlags(Kind); |
2216 | 6.83k | return Ctx.getWasmSection(Name, Kind, Flags, Group, UniqueID); |
2217 | 6.83k | } |
2218 | | |
2219 | | MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( |
2220 | 6.83k | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2221 | | |
2222 | 6.83k | if (Kind.isCommon()) |
2223 | 0 | report_fatal_error("mergable sections not supported yet on wasm"); |
2224 | | |
2225 | | // If we have -ffunction-section or -fdata-section then we should emit the |
2226 | | // global value to a uniqued section specifically for it. |
2227 | 6.83k | bool EmitUniqueSection = false; |
2228 | 6.83k | if (Kind.isText()) |
2229 | 6.72k | EmitUniqueSection = TM.getFunctionSections(); |
2230 | 107 | else |
2231 | 107 | EmitUniqueSection = TM.getDataSections(); |
2232 | 6.83k | EmitUniqueSection |= GO->hasComdat(); |
2233 | | |
2234 | 6.83k | return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, |
2235 | 6.83k | EmitUniqueSection, &NextUniqueID); |
2236 | 6.83k | } |
2237 | | |
2238 | | bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( |
2239 | 0 | bool UsesLabelDifference, const Function &F) const { |
2240 | | // We can always create relative relocations, so use another section |
2241 | | // that can be marked non-executable. |
2242 | 0 | return false; |
2243 | 0 | } |
2244 | | |
2245 | | const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( |
2246 | | const GlobalValue *LHS, const GlobalValue *RHS, |
2247 | 0 | const TargetMachine &TM) const { |
2248 | | // We may only use a PLT-relative relocation to refer to unnamed_addr |
2249 | | // functions. |
2250 | 0 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
2251 | 0 | return nullptr; |
2252 | | |
2253 | | // Basic correctness checks. |
2254 | 0 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
2255 | 0 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
2256 | 0 | RHS->isThreadLocal()) |
2257 | 0 | return nullptr; |
2258 | | |
2259 | 0 | return MCBinaryExpr::createSub( |
2260 | 0 | MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, |
2261 | 0 | getContext()), |
2262 | 0 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
2263 | 0 | } |
2264 | | |
2265 | 101 | void TargetLoweringObjectFileWasm::InitializeWasm() { |
2266 | 101 | StaticCtorSection = |
2267 | 101 | getContext().getWasmSection(".init_array", SectionKind::getData()); |
2268 | | |
2269 | | // We don't use PersonalityEncoding and LSDAEncoding because we don't emit |
2270 | | // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. |
2271 | 101 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
2272 | 101 | } |
2273 | | |
2274 | | MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( |
2275 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2276 | 0 | return Priority == UINT16_MAX ? |
2277 | 0 | StaticCtorSection : |
2278 | 0 | getContext().getWasmSection(".init_array." + utostr(Priority), |
2279 | 0 | SectionKind::getData()); |
2280 | 0 | } |
2281 | | |
2282 | | MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( |
2283 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2284 | 0 | report_fatal_error("@llvm.global_dtors should have been lowered already"); |
2285 | 0 | } |
2286 | | |
2287 | | //===----------------------------------------------------------------------===// |
2288 | | // XCOFF |
2289 | | //===----------------------------------------------------------------------===// |
2290 | | bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock( |
2291 | 0 | const MachineFunction *MF) { |
2292 | 0 | if (!MF->getLandingPads().empty()) |
2293 | 0 | return true; |
2294 | | |
2295 | 0 | const Function &F = MF->getFunction(); |
2296 | 0 | if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry()) |
2297 | 0 | return false; |
2298 | | |
2299 | 0 | const GlobalValue *Per = |
2300 | 0 | dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts()); |
2301 | 0 | assert(Per && "Personality routine is not a GlobalValue type."); |
2302 | 0 | if (isNoOpWithoutInvoke(classifyEHPersonality(Per))) |
2303 | 0 | return false; |
2304 | | |
2305 | 0 | return true; |
2306 | 0 | } |
2307 | | |
2308 | | bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB( |
2309 | 0 | const MachineFunction *MF) { |
2310 | 0 | const Function &F = MF->getFunction(); |
2311 | 0 | if (!F.hasStackProtectorFnAttr()) |
2312 | 0 | return false; |
2313 | | // FIXME: check presence of canary word |
2314 | | // There are cases that the stack protectors are not really inserted even if |
2315 | | // the attributes are on. |
2316 | 0 | return true; |
2317 | 0 | } |
2318 | | |
2319 | | MCSymbol * |
2320 | 0 | TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { |
2321 | 0 | MCSymbol *EHInfoSym = MF->getMMI().getContext().getOrCreateSymbol( |
2322 | 0 | "__ehinfo." + Twine(MF->getFunctionNumber())); |
2323 | 0 | cast<MCSymbolXCOFF>(EHInfoSym)->setEHInfo(); |
2324 | 0 | return EHInfoSym; |
2325 | 0 | } |
2326 | | |
2327 | | MCSymbol * |
2328 | | TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV, |
2329 | 0 | const TargetMachine &TM) const { |
2330 | | // We always use a qualname symbol for a GV that represents |
2331 | | // a declaration, a function descriptor, or a common symbol. |
2332 | | // If a GV represents a GlobalVariable and -fdata-sections is enabled, we |
2333 | | // also return a qualname so that a label symbol could be avoided. |
2334 | | // It is inherently ambiguous when the GO represents the address of a |
2335 | | // function, as the GO could either represent a function descriptor or a |
2336 | | // function entry point. We choose to always return a function descriptor |
2337 | | // here. |
2338 | 0 | if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) { |
2339 | 0 | if (GO->isDeclarationForLinker()) |
2340 | 0 | return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM)) |
2341 | 0 | ->getQualNameSymbol(); |
2342 | | |
2343 | 0 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) |
2344 | 0 | if (GVar->hasAttribute("toc-data")) |
2345 | 0 | return cast<MCSectionXCOFF>( |
2346 | 0 | SectionForGlobal(GVar, SectionKind::getData(), TM)) |
2347 | 0 | ->getQualNameSymbol(); |
2348 | | |
2349 | 0 | SectionKind GOKind = getKindForGlobal(GO, TM); |
2350 | 0 | if (GOKind.isText()) |
2351 | 0 | return cast<MCSectionXCOFF>( |
2352 | 0 | getSectionForFunctionDescriptor(cast<Function>(GO), TM)) |
2353 | 0 | ->getQualNameSymbol(); |
2354 | 0 | if ((TM.getDataSections() && !GO->hasSection()) || GO->hasCommonLinkage() || |
2355 | 0 | GOKind.isBSSLocal() || GOKind.isThreadBSSLocal()) |
2356 | 0 | return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM)) |
2357 | 0 | ->getQualNameSymbol(); |
2358 | 0 | } |
2359 | | |
2360 | | // For all other cases, fall back to getSymbol to return the unqualified name. |
2361 | 0 | return nullptr; |
2362 | 0 | } |
2363 | | |
2364 | | MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( |
2365 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2366 | 0 | if (!GO->hasSection()) |
2367 | 0 | report_fatal_error("#pragma clang section is not yet supported"); |
2368 | |
|
2369 | 0 | StringRef SectionName = GO->getSection(); |
2370 | | |
2371 | | // Handle the XCOFF::TD case first, then deal with the rest. |
2372 | 0 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) |
2373 | 0 | if (GVar->hasAttribute("toc-data")) |
2374 | 0 | return getContext().getXCOFFSection( |
2375 | 0 | SectionName, Kind, |
2376 | 0 | XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD), |
2377 | 0 | /* MultiSymbolsAllowed*/ true); |
2378 | | |
2379 | 0 | XCOFF::StorageMappingClass MappingClass; |
2380 | 0 | if (Kind.isText()) |
2381 | 0 | MappingClass = XCOFF::XMC_PR; |
2382 | 0 | else if (Kind.isData() || Kind.isBSS()) |
2383 | 0 | MappingClass = XCOFF::XMC_RW; |
2384 | 0 | else if (Kind.isReadOnlyWithRel()) |
2385 | 0 | MappingClass = |
2386 | 0 | TM.Options.XCOFFReadOnlyPointers ? XCOFF::XMC_RO : XCOFF::XMC_RW; |
2387 | 0 | else if (Kind.isReadOnly()) |
2388 | 0 | MappingClass = XCOFF::XMC_RO; |
2389 | 0 | else |
2390 | 0 | report_fatal_error("XCOFF other section types not yet implemented."); |
2391 | |
|
2392 | 0 | return getContext().getXCOFFSection( |
2393 | 0 | SectionName, Kind, XCOFF::CsectProperties(MappingClass, XCOFF::XTY_SD), |
2394 | 0 | /* MultiSymbolsAllowed*/ true); |
2395 | 0 | } |
2396 | | |
2397 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( |
2398 | 0 | const GlobalObject *GO, const TargetMachine &TM) const { |
2399 | 0 | assert(GO->isDeclarationForLinker() && |
2400 | 0 | "Tried to get ER section for a defined global."); |
2401 | | |
2402 | 0 | SmallString<128> Name; |
2403 | 0 | getNameWithPrefix(Name, GO, TM); |
2404 | |
|
2405 | 0 | XCOFF::StorageMappingClass SMC = |
2406 | 0 | isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA; |
2407 | 0 | if (GO->isThreadLocal()) |
2408 | 0 | SMC = XCOFF::XMC_UL; |
2409 | |
|
2410 | 0 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) |
2411 | 0 | if (GVar->hasAttribute("toc-data")) |
2412 | 0 | SMC = XCOFF::XMC_TD; |
2413 | | |
2414 | | // Externals go into a csect of type ER. |
2415 | 0 | return getContext().getXCOFFSection( |
2416 | 0 | Name, SectionKind::getMetadata(), |
2417 | 0 | XCOFF::CsectProperties(SMC, XCOFF::XTY_ER)); |
2418 | 0 | } |
2419 | | |
2420 | | MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( |
2421 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2422 | | // Handle the XCOFF::TD case first, then deal with the rest. |
2423 | 0 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) |
2424 | 0 | if (GVar->hasAttribute("toc-data")) { |
2425 | 0 | SmallString<128> Name; |
2426 | 0 | getNameWithPrefix(Name, GO, TM); |
2427 | 0 | return getContext().getXCOFFSection( |
2428 | 0 | Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, XCOFF::XTY_SD), |
2429 | 0 | /* MultiSymbolsAllowed*/ true); |
2430 | 0 | } |
2431 | | |
2432 | | // Common symbols go into a csect with matching name which will get mapped |
2433 | | // into the .bss section. |
2434 | | // Zero-initialized local TLS symbols go into a csect with matching name which |
2435 | | // will get mapped into the .tbss section. |
2436 | 0 | if (Kind.isBSSLocal() || GO->hasCommonLinkage() || Kind.isThreadBSSLocal()) { |
2437 | 0 | SmallString<128> Name; |
2438 | 0 | getNameWithPrefix(Name, GO, TM); |
2439 | 0 | XCOFF::StorageMappingClass SMC = Kind.isBSSLocal() ? XCOFF::XMC_BS |
2440 | 0 | : Kind.isCommon() ? XCOFF::XMC_RW |
2441 | 0 | : XCOFF::XMC_UL; |
2442 | 0 | return getContext().getXCOFFSection( |
2443 | 0 | Name, Kind, XCOFF::CsectProperties(SMC, XCOFF::XTY_CM)); |
2444 | 0 | } |
2445 | | |
2446 | 0 | if (Kind.isText()) { |
2447 | 0 | if (TM.getFunctionSections()) { |
2448 | 0 | return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM)) |
2449 | 0 | ->getRepresentedCsect(); |
2450 | 0 | } |
2451 | 0 | return TextSection; |
2452 | 0 | } |
2453 | | |
2454 | 0 | if (TM.Options.XCOFFReadOnlyPointers && Kind.isReadOnlyWithRel()) { |
2455 | 0 | if (!TM.getDataSections()) |
2456 | 0 | report_fatal_error( |
2457 | 0 | "ReadOnlyPointers is supported only if data sections is turned on"); |
2458 | |
|
2459 | 0 | SmallString<128> Name; |
2460 | 0 | getNameWithPrefix(Name, GO, TM); |
2461 | 0 | return getContext().getXCOFFSection( |
2462 | 0 | Name, SectionKind::getReadOnly(), |
2463 | 0 | XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); |
2464 | 0 | } |
2465 | | |
2466 | | // For BSS kind, zero initialized data must be emitted to the .data section |
2467 | | // because external linkage control sections that get mapped to the .bss |
2468 | | // section will be linked as tentative defintions, which is only appropriate |
2469 | | // for SectionKind::Common. |
2470 | 0 | if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) { |
2471 | 0 | if (TM.getDataSections()) { |
2472 | 0 | SmallString<128> Name; |
2473 | 0 | getNameWithPrefix(Name, GO, TM); |
2474 | 0 | return getContext().getXCOFFSection( |
2475 | 0 | Name, SectionKind::getData(), |
2476 | 0 | XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)); |
2477 | 0 | } |
2478 | 0 | return DataSection; |
2479 | 0 | } |
2480 | | |
2481 | 0 | if (Kind.isReadOnly()) { |
2482 | 0 | if (TM.getDataSections()) { |
2483 | 0 | SmallString<128> Name; |
2484 | 0 | getNameWithPrefix(Name, GO, TM); |
2485 | 0 | return getContext().getXCOFFSection( |
2486 | 0 | Name, SectionKind::getReadOnly(), |
2487 | 0 | XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); |
2488 | 0 | } |
2489 | 0 | return ReadOnlySection; |
2490 | 0 | } |
2491 | | |
2492 | | // External/weak TLS data and initialized local TLS data are not eligible |
2493 | | // to be put into common csect. If data sections are enabled, thread |
2494 | | // data are emitted into separate sections. Otherwise, thread data |
2495 | | // are emitted into the .tdata section. |
2496 | 0 | if (Kind.isThreadLocal()) { |
2497 | 0 | if (TM.getDataSections()) { |
2498 | 0 | SmallString<128> Name; |
2499 | 0 | getNameWithPrefix(Name, GO, TM); |
2500 | 0 | return getContext().getXCOFFSection( |
2501 | 0 | Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TL, XCOFF::XTY_SD)); |
2502 | 0 | } |
2503 | 0 | return TLSDataSection; |
2504 | 0 | } |
2505 | | |
2506 | 0 | report_fatal_error("XCOFF other section types not yet implemented."); |
2507 | 0 | } |
2508 | | |
2509 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable( |
2510 | 0 | const Function &F, const TargetMachine &TM) const { |
2511 | 0 | assert (!F.getComdat() && "Comdat not supported on XCOFF."); |
2512 | | |
2513 | 0 | if (!TM.getFunctionSections()) |
2514 | 0 | return ReadOnlySection; |
2515 | | |
2516 | | // If the function can be removed, produce a unique section so that |
2517 | | // the table doesn't prevent the removal. |
2518 | 0 | SmallString<128> NameStr(".rodata.jmp.."); |
2519 | 0 | getNameWithPrefix(NameStr, &F, TM); |
2520 | 0 | return getContext().getXCOFFSection( |
2521 | 0 | NameStr, SectionKind::getReadOnly(), |
2522 | 0 | XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); |
2523 | 0 | } |
2524 | | |
2525 | | bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( |
2526 | 0 | bool UsesLabelDifference, const Function &F) const { |
2527 | 0 | return false; |
2528 | 0 | } |
2529 | | |
2530 | | /// Given a mergeable constant with the specified size and relocation |
2531 | | /// information, return a section that it should be placed in. |
2532 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant( |
2533 | | const DataLayout &DL, SectionKind Kind, const Constant *C, |
2534 | 0 | Align &Alignment) const { |
2535 | | // TODO: Enable emiting constant pool to unique sections when we support it. |
2536 | 0 | if (Alignment > Align(16)) |
2537 | 0 | report_fatal_error("Alignments greater than 16 not yet supported."); |
2538 | |
|
2539 | 0 | if (Alignment == Align(8)) { |
2540 | 0 | assert(ReadOnly8Section && "Section should always be initialized."); |
2541 | 0 | return ReadOnly8Section; |
2542 | 0 | } |
2543 | | |
2544 | 0 | if (Alignment == Align(16)) { |
2545 | 0 | assert(ReadOnly16Section && "Section should always be initialized."); |
2546 | 0 | return ReadOnly16Section; |
2547 | 0 | } |
2548 | | |
2549 | 0 | return ReadOnlySection; |
2550 | 0 | } |
2551 | | |
2552 | | void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, |
2553 | 0 | const TargetMachine &TgtM) { |
2554 | 0 | TargetLoweringObjectFile::Initialize(Ctx, TgtM); |
2555 | 0 | TTypeEncoding = |
2556 | 0 | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel | |
2557 | 0 | (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4 |
2558 | 0 | : dwarf::DW_EH_PE_sdata8); |
2559 | 0 | PersonalityEncoding = 0; |
2560 | 0 | LSDAEncoding = 0; |
2561 | 0 | CallSiteEncoding = dwarf::DW_EH_PE_udata4; |
2562 | | |
2563 | | // AIX debug for thread local location is not ready. And for integrated as |
2564 | | // mode, the relocatable address for the thread local variable will cause |
2565 | | // linker error. So disable the location attribute generation for thread local |
2566 | | // variables for now. |
2567 | | // FIXME: when TLS debug on AIX is ready, remove this setting. |
2568 | 0 | SupportDebugThreadLocalLocation = false; |
2569 | 0 | } |
2570 | | |
2571 | | MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( |
2572 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2573 | 0 | report_fatal_error("no static constructor section on AIX"); |
2574 | 0 | } |
2575 | | |
2576 | | MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( |
2577 | 0 | unsigned Priority, const MCSymbol *KeySym) const { |
2578 | 0 | report_fatal_error("no static destructor section on AIX"); |
2579 | 0 | } |
2580 | | |
2581 | | const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( |
2582 | | const GlobalValue *LHS, const GlobalValue *RHS, |
2583 | 0 | const TargetMachine &TM) const { |
2584 | | /* Not implemented yet, but don't crash, return nullptr. */ |
2585 | 0 | return nullptr; |
2586 | 0 | } |
2587 | | |
2588 | | XCOFF::StorageClass |
2589 | 0 | TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) { |
2590 | 0 | assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX."); |
2591 | | |
2592 | 0 | switch (GV->getLinkage()) { |
2593 | 0 | case GlobalValue::InternalLinkage: |
2594 | 0 | case GlobalValue::PrivateLinkage: |
2595 | 0 | return XCOFF::C_HIDEXT; |
2596 | 0 | case GlobalValue::ExternalLinkage: |
2597 | 0 | case GlobalValue::CommonLinkage: |
2598 | 0 | case GlobalValue::AvailableExternallyLinkage: |
2599 | 0 | return XCOFF::C_EXT; |
2600 | 0 | case GlobalValue::ExternalWeakLinkage: |
2601 | 0 | case GlobalValue::LinkOnceAnyLinkage: |
2602 | 0 | case GlobalValue::LinkOnceODRLinkage: |
2603 | 0 | case GlobalValue::WeakAnyLinkage: |
2604 | 0 | case GlobalValue::WeakODRLinkage: |
2605 | 0 | return XCOFF::C_WEAKEXT; |
2606 | 0 | case GlobalValue::AppendingLinkage: |
2607 | 0 | report_fatal_error( |
2608 | 0 | "There is no mapping that implements AppendingLinkage for XCOFF."); |
2609 | 0 | } |
2610 | 0 | llvm_unreachable("Unknown linkage type!"); |
2611 | 0 | } |
2612 | | |
2613 | | MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol( |
2614 | 0 | const GlobalValue *Func, const TargetMachine &TM) const { |
2615 | 0 | assert((isa<Function>(Func) || |
2616 | 0 | (isa<GlobalAlias>(Func) && |
2617 | 0 | isa_and_nonnull<Function>( |
2618 | 0 | cast<GlobalAlias>(Func)->getAliaseeObject()))) && |
2619 | 0 | "Func must be a function or an alias which has a function as base " |
2620 | 0 | "object."); |
2621 | | |
2622 | 0 | SmallString<128> NameStr; |
2623 | 0 | NameStr.push_back('.'); |
2624 | 0 | getNameWithPrefix(NameStr, Func, TM); |
2625 | | |
2626 | | // When -function-sections is enabled and explicit section is not specified, |
2627 | | // it's not necessary to emit function entry point label any more. We will use |
2628 | | // function entry point csect instead. And for function delcarations, the |
2629 | | // undefined symbols gets treated as csect with XTY_ER property. |
2630 | 0 | if (((TM.getFunctionSections() && !Func->hasSection()) || |
2631 | 0 | Func->isDeclarationForLinker()) && |
2632 | 0 | isa<Function>(Func)) { |
2633 | 0 | return getContext() |
2634 | 0 | .getXCOFFSection( |
2635 | 0 | NameStr, SectionKind::getText(), |
2636 | 0 | XCOFF::CsectProperties(XCOFF::XMC_PR, Func->isDeclarationForLinker() |
2637 | 0 | ? XCOFF::XTY_ER |
2638 | 0 | : XCOFF::XTY_SD)) |
2639 | 0 | ->getQualNameSymbol(); |
2640 | 0 | } |
2641 | | |
2642 | 0 | return getContext().getOrCreateSymbol(NameStr); |
2643 | 0 | } |
2644 | | |
2645 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( |
2646 | 0 | const Function *F, const TargetMachine &TM) const { |
2647 | 0 | SmallString<128> NameStr; |
2648 | 0 | getNameWithPrefix(NameStr, F, TM); |
2649 | 0 | return getContext().getXCOFFSection( |
2650 | 0 | NameStr, SectionKind::getData(), |
2651 | 0 | XCOFF::CsectProperties(XCOFF::XMC_DS, XCOFF::XTY_SD)); |
2652 | 0 | } |
2653 | | |
2654 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( |
2655 | 0 | const MCSymbol *Sym, const TargetMachine &TM) const { |
2656 | | // Use TE storage-mapping class when large code model is enabled so that |
2657 | | // the chance of needing -bbigtoc is decreased. Also, the toc-entry for |
2658 | | // EH info is never referenced directly using instructions so it can be |
2659 | | // allocated with TE storage-mapping class. |
2660 | 0 | return getContext().getXCOFFSection( |
2661 | 0 | cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(), |
2662 | 0 | XCOFF::CsectProperties((TM.getCodeModel() == CodeModel::Large || |
2663 | 0 | cast<MCSymbolXCOFF>(Sym)->isEHInfo()) |
2664 | 0 | ? XCOFF::XMC_TE |
2665 | 0 | : XCOFF::XMC_TC, |
2666 | 0 | XCOFF::XTY_SD)); |
2667 | 0 | } |
2668 | | |
2669 | | MCSection *TargetLoweringObjectFileXCOFF::getSectionForLSDA( |
2670 | 0 | const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const { |
2671 | 0 | auto *LSDA = cast<MCSectionXCOFF>(LSDASection); |
2672 | 0 | if (TM.getFunctionSections()) { |
2673 | | // If option -ffunction-sections is on, append the function name to the |
2674 | | // name of the LSDA csect so that each function has its own LSDA csect. |
2675 | | // This helps the linker to garbage-collect EH info of unused functions. |
2676 | 0 | SmallString<128> NameStr = LSDA->getName(); |
2677 | 0 | raw_svector_ostream(NameStr) << '.' << F.getName(); |
2678 | 0 | LSDA = getContext().getXCOFFSection(NameStr, LSDA->getKind(), |
2679 | 0 | LSDA->getCsectProp()); |
2680 | 0 | } |
2681 | 0 | return LSDA; |
2682 | 0 | } |
2683 | | //===----------------------------------------------------------------------===// |
2684 | | // GOFF |
2685 | | //===----------------------------------------------------------------------===// |
2686 | 0 | TargetLoweringObjectFileGOFF::TargetLoweringObjectFileGOFF() = default; |
2687 | | |
2688 | | MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal( |
2689 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2690 | 0 | return SelectSectionForGlobal(GO, Kind, TM); |
2691 | 0 | } |
2692 | | |
2693 | | MCSection *TargetLoweringObjectFileGOFF::getSectionForLSDA( |
2694 | 0 | const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const { |
2695 | 0 | std::string Name = ".gcc_exception_table." + F.getName().str(); |
2696 | 0 | return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr, |
2697 | 0 | nullptr); |
2698 | 0 | } |
2699 | | |
2700 | | MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal( |
2701 | 0 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
2702 | 0 | auto *Symbol = TM.getSymbol(GO); |
2703 | 0 | if (Kind.isBSS()) |
2704 | 0 | return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS(), |
2705 | 0 | nullptr, nullptr); |
2706 | | |
2707 | 0 | return getContext().getObjectFileInfo()->getTextSection(); |
2708 | 0 | } |