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

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