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