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