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