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