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