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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp