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