xref: /llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (revision 2f07c2e9d9cc66c5c560a28ae7a706756efce2c4)
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/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
26 #include "llvm/IR/Comdat.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalObject.h"
33 #include "llvm/IR/GlobalValue.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Mangler.h"
36 #include "llvm/IR/Metadata.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/MC/MCContext.h"
41 #include "llvm/MC/MCExpr.h"
42 #include "llvm/MC/MCSectionCOFF.h"
43 #include "llvm/MC/MCSectionELF.h"
44 #include "llvm/MC/MCSectionMachO.h"
45 #include "llvm/MC/MCSectionWasm.h"
46 #include "llvm/MC/MCStreamer.h"
47 #include "llvm/MC/MCSymbol.h"
48 #include "llvm/MC/MCSymbolELF.h"
49 #include "llvm/MC/MCValue.h"
50 #include "llvm/MC/SectionKind.h"
51 #include "llvm/ProfileData/InstrProf.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CodeGen.h"
54 #include "llvm/Support/Format.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include "llvm/Target/TargetMachine.h"
58 #include <cassert>
59 #include <string>
60 
61 using namespace llvm;
62 using namespace dwarf;
63 
64 static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
65                              StringRef &Section) {
66   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
67   M.getModuleFlagsMetadata(ModuleFlags);
68 
69   for (const auto &MFE: ModuleFlags) {
70     // Ignore flags with 'Require' behaviour.
71     if (MFE.Behavior == Module::Require)
72       continue;
73 
74     StringRef Key = MFE.Key->getString();
75     if (Key == "Objective-C Image Info Version") {
76       Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
77     } else if (Key == "Objective-C Garbage Collection" ||
78                Key == "Objective-C GC Only" ||
79                Key == "Objective-C Is Simulated" ||
80                Key == "Objective-C Class Properties" ||
81                Key == "Objective-C Image Swift Version") {
82       Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
83     } else if (Key == "Objective-C Image Info Section") {
84       Section = cast<MDString>(MFE.Val)->getString();
85     }
86   }
87 }
88 
89 //===----------------------------------------------------------------------===//
90 //                                  ELF
91 //===----------------------------------------------------------------------===//
92 
93 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
94                                              const TargetMachine &TgtM) {
95   TargetLoweringObjectFile::Initialize(Ctx, TgtM);
96   TM = &TgtM;
97 
98   CodeModel::Model CM = TgtM.getCodeModel();
99 
100   switch (TgtM.getTargetTriple().getArch()) {
101   case Triple::arm:
102   case Triple::armeb:
103   case Triple::thumb:
104   case Triple::thumbeb:
105     if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
106       break;
107     // Fallthrough if not using EHABI
108     LLVM_FALLTHROUGH;
109   case Triple::ppc:
110   case Triple::x86:
111     PersonalityEncoding = isPositionIndependent()
112                               ? dwarf::DW_EH_PE_indirect |
113                                     dwarf::DW_EH_PE_pcrel |
114                                     dwarf::DW_EH_PE_sdata4
115                               : dwarf::DW_EH_PE_absptr;
116     LSDAEncoding = isPositionIndependent()
117                        ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
118                        : dwarf::DW_EH_PE_absptr;
119     TTypeEncoding = isPositionIndependent()
120                         ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
121                               dwarf::DW_EH_PE_sdata4
122                         : dwarf::DW_EH_PE_absptr;
123     break;
124   case Triple::x86_64:
125     if (isPositionIndependent()) {
126       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
127         ((CM == CodeModel::Small || CM == CodeModel::Medium)
128          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
129       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
130         (CM == CodeModel::Small
131          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
132       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
133         ((CM == CodeModel::Small || CM == CodeModel::Medium)
134          ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
135     } else {
136       PersonalityEncoding =
137         (CM == CodeModel::Small || CM == CodeModel::Medium)
138         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
139       LSDAEncoding = (CM == CodeModel::Small)
140         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
141       TTypeEncoding = (CM == CodeModel::Small)
142         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
143     }
144     break;
145   case Triple::hexagon:
146     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
147     LSDAEncoding = dwarf::DW_EH_PE_absptr;
148     TTypeEncoding = dwarf::DW_EH_PE_absptr;
149     if (isPositionIndependent()) {
150       PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
151       LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
152       TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
153     }
154     break;
155   case Triple::aarch64:
156   case Triple::aarch64_be:
157     // The small model guarantees static code/data size < 4GB, but not where it
158     // will be in memory. Most of these could end up >2GB away so even a signed
159     // pc-relative 32-bit address is insufficient, theoretically.
160     if (isPositionIndependent()) {
161       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
162         dwarf::DW_EH_PE_sdata8;
163       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
164       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
165         dwarf::DW_EH_PE_sdata8;
166     } else {
167       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
168       LSDAEncoding = dwarf::DW_EH_PE_absptr;
169       TTypeEncoding = dwarf::DW_EH_PE_absptr;
170     }
171     break;
172   case Triple::lanai:
173     LSDAEncoding = dwarf::DW_EH_PE_absptr;
174     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
175     TTypeEncoding = dwarf::DW_EH_PE_absptr;
176     break;
177   case Triple::mips:
178   case Triple::mipsel:
179   case Triple::mips64:
180   case Triple::mips64el:
181     // MIPS uses indirect pointer to refer personality functions and types, so
182     // that the eh_frame section can be read-only. DW.ref.personality will be
183     // generated for relocation.
184     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
185     // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
186     //        identify N64 from just a triple.
187     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
188                     dwarf::DW_EH_PE_sdata4;
189     // We don't support PC-relative LSDA references in GAS so we use the default
190     // DW_EH_PE_absptr for those.
191 
192     // FreeBSD must be explicit about the data size and using pcrel since it's
193     // assembler/linker won't do the automatic conversion that the Linux tools
194     // do.
195     if (TgtM.getTargetTriple().isOSFreeBSD()) {
196       PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
197       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
198     }
199     break;
200   case Triple::ppc64:
201   case Triple::ppc64le:
202     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
203       dwarf::DW_EH_PE_udata8;
204     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
205     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
206       dwarf::DW_EH_PE_udata8;
207     break;
208   case Triple::sparcel:
209   case Triple::sparc:
210     if (isPositionIndependent()) {
211       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
212       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
213         dwarf::DW_EH_PE_sdata4;
214       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
215         dwarf::DW_EH_PE_sdata4;
216     } else {
217       LSDAEncoding = dwarf::DW_EH_PE_absptr;
218       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
219       TTypeEncoding = dwarf::DW_EH_PE_absptr;
220     }
221     break;
222   case Triple::sparcv9:
223     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
224     if (isPositionIndependent()) {
225       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
226         dwarf::DW_EH_PE_sdata4;
227       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
228         dwarf::DW_EH_PE_sdata4;
229     } else {
230       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
231       TTypeEncoding = dwarf::DW_EH_PE_absptr;
232     }
233     break;
234   case Triple::systemz:
235     // All currently-defined code models guarantee that 4-byte PC-relative
236     // values will be in range.
237     if (isPositionIndependent()) {
238       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
239         dwarf::DW_EH_PE_sdata4;
240       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
241       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
242         dwarf::DW_EH_PE_sdata4;
243     } else {
244       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
245       LSDAEncoding = dwarf::DW_EH_PE_absptr;
246       TTypeEncoding = dwarf::DW_EH_PE_absptr;
247     }
248     break;
249   default:
250     break;
251   }
252 }
253 
254 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
255                                                      Module &M) const {
256   auto &C = getContext();
257 
258   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
259     auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
260                               ELF::SHF_EXCLUDE);
261 
262     Streamer.SwitchSection(S);
263 
264     for (const auto &Operand : LinkerOptions->operands()) {
265       if (cast<MDNode>(Operand)->getNumOperands() != 2)
266         report_fatal_error("invalid llvm.linker.options");
267       for (const auto &Option : cast<MDNode>(Operand)->operands()) {
268         Streamer.EmitBytes(cast<MDString>(Option)->getString());
269         Streamer.EmitIntValue(0, 1);
270       }
271     }
272   }
273 
274   if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) {
275     auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES,
276                               ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
277 
278     Streamer.SwitchSection(S);
279 
280     for (const auto &Operand : DependentLibraries->operands()) {
281       Streamer.EmitBytes(
282           cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString());
283       Streamer.EmitIntValue(0, 1);
284     }
285   }
286 
287   unsigned Version = 0;
288   unsigned Flags = 0;
289   StringRef Section;
290 
291   GetObjCImageInfo(M, Version, Flags, Section);
292   if (!Section.empty()) {
293     auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
294     Streamer.SwitchSection(S);
295     Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
296     Streamer.EmitIntValue(Version, 4);
297     Streamer.EmitIntValue(Flags, 4);
298     Streamer.AddBlankLine();
299   }
300 
301   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
302   M.getModuleFlagsMetadata(ModuleFlags);
303 
304   MDNode *CFGProfile = nullptr;
305 
306   for (const auto &MFE : ModuleFlags) {
307     StringRef Key = MFE.Key->getString();
308     if (Key == "CG Profile") {
309       CFGProfile = cast<MDNode>(MFE.Val);
310       break;
311     }
312   }
313 
314   if (!CFGProfile)
315     return;
316 
317   auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
318     if (!MDO)
319       return nullptr;
320     auto V = cast<ValueAsMetadata>(MDO);
321     const Function *F = cast<Function>(V->getValue());
322     return TM->getSymbol(F);
323   };
324 
325   for (const auto &Edge : CFGProfile->operands()) {
326     MDNode *E = cast<MDNode>(Edge);
327     const MCSymbol *From = GetSym(E->getOperand(0));
328     const MCSymbol *To = GetSym(E->getOperand(1));
329     // Skip null functions. This can happen if functions are dead stripped after
330     // the CGProfile pass has been run.
331     if (!From || !To)
332       continue;
333     uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
334                          ->getValue()
335                          ->getUniqueInteger()
336                          .getZExtValue();
337     Streamer.emitCGProfileEntry(
338         MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
339         MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
340   }
341 }
342 
343 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
344     const GlobalValue *GV, const TargetMachine &TM,
345     MachineModuleInfo *MMI) const {
346   unsigned Encoding = getPersonalityEncoding();
347   if ((Encoding & 0x80) == DW_EH_PE_indirect)
348     return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
349                                           TM.getSymbol(GV)->getName());
350   if ((Encoding & 0x70) == DW_EH_PE_absptr)
351     return TM.getSymbol(GV);
352   report_fatal_error("We do not support this DWARF encoding yet!");
353 }
354 
355 void TargetLoweringObjectFileELF::emitPersonalityValue(
356     MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
357   SmallString<64> NameData("DW.ref.");
358   NameData += Sym->getName();
359   MCSymbolELF *Label =
360       cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
361   Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
362   Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
363   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
364   MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
365                                                    ELF::SHT_PROGBITS, Flags, 0);
366   unsigned Size = DL.getPointerSize();
367   Streamer.SwitchSection(Sec);
368   Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0));
369   Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
370   const MCExpr *E = MCConstantExpr::create(Size, getContext());
371   Streamer.emitELFSize(Label, E);
372   Streamer.EmitLabel(Label);
373 
374   Streamer.EmitSymbolValue(Sym, Size);
375 }
376 
377 const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
378     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
379     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
380   if (Encoding & DW_EH_PE_indirect) {
381     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
382 
383     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
384 
385     // Add information about the stub reference to ELFMMI so that the stub
386     // gets emitted by the asmprinter.
387     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
388     if (!StubSym.getPointer()) {
389       MCSymbol *Sym = TM.getSymbol(GV);
390       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
391     }
392 
393     return TargetLoweringObjectFile::
394       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
395                         Encoding & ~DW_EH_PE_indirect, Streamer);
396   }
397 
398   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
399                                                            MMI, Streamer);
400 }
401 
402 static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
403   // N.B.: The defaults used in here are not the same ones used in MC.
404   // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
405   // both gas and MC will produce a section with no flags. Given
406   // section(".eh_frame") gcc will produce:
407   //
408   //   .section   .eh_frame,"a",@progbits
409 
410   if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
411                                       /*AddSegmentInfo=*/false))
412     return SectionKind::getMetadata();
413 
414   if (Name.empty() || Name[0] != '.') return K;
415 
416   // Default implementation based on some magic section names.
417   if (Name == ".bss" ||
418       Name.startswith(".bss.") ||
419       Name.startswith(".gnu.linkonce.b.") ||
420       Name.startswith(".llvm.linkonce.b.") ||
421       Name == ".sbss" ||
422       Name.startswith(".sbss.") ||
423       Name.startswith(".gnu.linkonce.sb.") ||
424       Name.startswith(".llvm.linkonce.sb."))
425     return SectionKind::getBSS();
426 
427   if (Name == ".tdata" ||
428       Name.startswith(".tdata.") ||
429       Name.startswith(".gnu.linkonce.td.") ||
430       Name.startswith(".llvm.linkonce.td."))
431     return SectionKind::getThreadData();
432 
433   if (Name == ".tbss" ||
434       Name.startswith(".tbss.") ||
435       Name.startswith(".gnu.linkonce.tb.") ||
436       Name.startswith(".llvm.linkonce.tb."))
437     return SectionKind::getThreadBSS();
438 
439   return K;
440 }
441 
442 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
443   // Use SHT_NOTE for section whose name starts with ".note" to allow
444   // emitting ELF notes from C variable declaration.
445   // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
446   if (Name.startswith(".note"))
447     return ELF::SHT_NOTE;
448 
449   if (Name == ".init_array")
450     return ELF::SHT_INIT_ARRAY;
451 
452   if (Name == ".fini_array")
453     return ELF::SHT_FINI_ARRAY;
454 
455   if (Name == ".preinit_array")
456     return ELF::SHT_PREINIT_ARRAY;
457 
458   if (K.isBSS() || K.isThreadBSS())
459     return ELF::SHT_NOBITS;
460 
461   return ELF::SHT_PROGBITS;
462 }
463 
464 static unsigned getELFSectionFlags(SectionKind K) {
465   unsigned Flags = 0;
466 
467   if (!K.isMetadata())
468     Flags |= ELF::SHF_ALLOC;
469 
470   if (K.isText())
471     Flags |= ELF::SHF_EXECINSTR;
472 
473   if (K.isExecuteOnly())
474     Flags |= ELF::SHF_ARM_PURECODE;
475 
476   if (K.isWriteable())
477     Flags |= ELF::SHF_WRITE;
478 
479   if (K.isThreadLocal())
480     Flags |= ELF::SHF_TLS;
481 
482   if (K.isMergeableCString() || K.isMergeableConst())
483     Flags |= ELF::SHF_MERGE;
484 
485   if (K.isMergeableCString())
486     Flags |= ELF::SHF_STRINGS;
487 
488   return Flags;
489 }
490 
491 static const Comdat *getELFComdat(const GlobalValue *GV) {
492   const Comdat *C = GV->getComdat();
493   if (!C)
494     return nullptr;
495 
496   if (C->getSelectionKind() != Comdat::Any)
497     report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
498                        C->getName() + "' cannot be lowered.");
499 
500   return C;
501 }
502 
503 static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO,
504                                               const TargetMachine &TM) {
505   MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
506   if (!MD)
507     return nullptr;
508 
509   const MDOperand &Op = MD->getOperand(0);
510   if (!Op.get())
511     return nullptr;
512 
513   auto *VM = dyn_cast<ValueAsMetadata>(Op);
514   if (!VM)
515     report_fatal_error("MD_associated operand is not ValueAsMetadata");
516 
517   GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
518   return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
519 }
520 
521 static unsigned getEntrySizeForKind(SectionKind Kind) {
522   if (Kind.isMergeable1ByteCString())
523     return 1;
524   else if (Kind.isMergeable2ByteCString())
525     return 2;
526   else if (Kind.isMergeable4ByteCString())
527     return 4;
528   else if (Kind.isMergeableConst4())
529     return 4;
530   else if (Kind.isMergeableConst8())
531     return 8;
532   else if (Kind.isMergeableConst16())
533     return 16;
534   else if (Kind.isMergeableConst32())
535     return 32;
536   else {
537     // We shouldn't have mergeable C strings or mergeable constants that we
538     // didn't handle above.
539     assert(!Kind.isMergeableCString() && "unknown string width");
540     assert(!Kind.isMergeableConst() && "unknown data width");
541     return 0;
542   }
543 }
544 
545 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
546     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
547   StringRef SectionName = GO->getSection();
548 
549   // Check if '#pragma clang section' name is applicable.
550   // Note that pragma directive overrides -ffunction-section, -fdata-section
551   // and so section name is exactly as user specified and not uniqued.
552   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
553   if (GV && GV->hasImplicitSection()) {
554     auto Attrs = GV->getAttributes();
555     if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
556       SectionName = Attrs.getAttribute("bss-section").getValueAsString();
557     } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
558       SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
559     } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
560       SectionName = Attrs.getAttribute("data-section").getValueAsString();
561     }
562   }
563   const Function *F = dyn_cast<Function>(GO);
564   if (F && F->hasFnAttribute("implicit-section-name")) {
565     SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
566   }
567 
568   // Infer section flags from the section name if we can.
569   Kind = getELFKindForNamedSection(SectionName, Kind);
570 
571   StringRef Group = "";
572   unsigned Flags = getELFSectionFlags(Kind);
573   if (const Comdat *C = getELFComdat(GO)) {
574     Group = C->getName();
575     Flags |= ELF::SHF_GROUP;
576   }
577 
578   // A section can have at most one associated section. Put each global with
579   // MD_associated in a unique section.
580   unsigned UniqueID = MCContext::GenericSectionID;
581   const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
582   if (AssociatedSymbol) {
583     UniqueID = NextUniqueID++;
584     Flags |= ELF::SHF_LINK_ORDER;
585   }
586 
587   MCSectionELF *Section = getContext().getELFSection(
588       SectionName, getELFSectionType(SectionName, Kind), Flags,
589       getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
590   // Make sure that we did not get some other section with incompatible sh_link.
591   // This should not be possible due to UniqueID code above.
592   assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
593          "Associated symbol mismatch between sections");
594   return Section;
595 }
596 
597 /// Return the section prefix name used by options FunctionsSections and
598 /// DataSections.
599 static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
600   if (Kind.isText())
601     return ".text";
602   if (Kind.isReadOnly())
603     return ".rodata";
604   if (Kind.isBSS())
605     return ".bss";
606   if (Kind.isThreadData())
607     return ".tdata";
608   if (Kind.isThreadBSS())
609     return ".tbss";
610   if (Kind.isData())
611     return ".data";
612   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
613   return ".data.rel.ro";
614 }
615 
616 static MCSectionELF *selectELFSectionForGlobal(
617     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
618     const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
619     unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
620 
621   StringRef Group = "";
622   if (const Comdat *C = getELFComdat(GO)) {
623     Flags |= ELF::SHF_GROUP;
624     Group = C->getName();
625   }
626 
627   // Get the section entry size based on the kind.
628   unsigned EntrySize = getEntrySizeForKind(Kind);
629 
630   SmallString<128> Name;
631   if (Kind.isMergeableCString()) {
632     // We also need alignment here.
633     // FIXME: this is getting the alignment of the character, not the
634     // alignment of the global!
635     unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
636         cast<GlobalVariable>(GO));
637 
638     std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
639     Name = SizeSpec + utostr(Align);
640   } else if (Kind.isMergeableConst()) {
641     Name = ".rodata.cst";
642     Name += utostr(EntrySize);
643   } else {
644     Name = getSectionPrefixForGlobal(Kind);
645   }
646 
647   if (const auto *F = dyn_cast<Function>(GO)) {
648     const auto &OptionalPrefix = F->getSectionPrefix();
649     if (OptionalPrefix)
650       Name += *OptionalPrefix;
651   }
652 
653   unsigned UniqueID = MCContext::GenericSectionID;
654   if (EmitUniqueSection) {
655     if (TM.getUniqueSectionNames()) {
656       Name.push_back('.');
657       TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/);
658     } else {
659       UniqueID = *NextUniqueID;
660       (*NextUniqueID)++;
661     }
662   }
663   // Use 0 as the unique ID for execute-only text.
664   if (Kind.isExecuteOnly())
665     UniqueID = 0;
666   return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
667                            EntrySize, Group, UniqueID, AssociatedSymbol);
668 }
669 
670 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
671     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
672   unsigned Flags = getELFSectionFlags(Kind);
673 
674   // If we have -ffunction-section or -fdata-section then we should emit the
675   // global value to a uniqued section specifically for it.
676   bool EmitUniqueSection = false;
677   if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
678     if (Kind.isText())
679       EmitUniqueSection = TM.getFunctionSections();
680     else
681       EmitUniqueSection = TM.getDataSections();
682   }
683   EmitUniqueSection |= GO->hasComdat();
684 
685   const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
686   if (AssociatedSymbol) {
687     EmitUniqueSection = true;
688     Flags |= ELF::SHF_LINK_ORDER;
689   }
690 
691   MCSectionELF *Section = selectELFSectionForGlobal(
692       getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
693       &NextUniqueID, AssociatedSymbol);
694   assert(Section->getAssociatedSymbol() == AssociatedSymbol);
695   return Section;
696 }
697 
698 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
699     const Function &F, const TargetMachine &TM) const {
700   // If the function can be removed, produce a unique section so that
701   // the table doesn't prevent the removal.
702   const Comdat *C = F.getComdat();
703   bool EmitUniqueSection = TM.getFunctionSections() || C;
704   if (!EmitUniqueSection)
705     return ReadOnlySection;
706 
707   return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
708                                    getMangler(), TM, EmitUniqueSection,
709                                    ELF::SHF_ALLOC, &NextUniqueID,
710                                    /* AssociatedSymbol */ nullptr);
711 }
712 
713 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
714     bool UsesLabelDifference, const Function &F) const {
715   // We can always create relative relocations, so use another section
716   // that can be marked non-executable.
717   return false;
718 }
719 
720 /// Given a mergeable constant with the specified size and relocation
721 /// information, return a section that it should be placed in.
722 MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
723     const DataLayout &DL, SectionKind Kind, const Constant *C,
724     unsigned &Align) const {
725   if (Kind.isMergeableConst4() && MergeableConst4Section)
726     return MergeableConst4Section;
727   if (Kind.isMergeableConst8() && MergeableConst8Section)
728     return MergeableConst8Section;
729   if (Kind.isMergeableConst16() && MergeableConst16Section)
730     return MergeableConst16Section;
731   if (Kind.isMergeableConst32() && MergeableConst32Section)
732     return MergeableConst32Section;
733   if (Kind.isReadOnly())
734     return ReadOnlySection;
735 
736   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
737   return DataRelROSection;
738 }
739 
740 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
741                                               bool IsCtor, unsigned Priority,
742                                               const MCSymbol *KeySym) {
743   std::string Name;
744   unsigned Type;
745   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
746   StringRef COMDAT = KeySym ? KeySym->getName() : "";
747 
748   if (KeySym)
749     Flags |= ELF::SHF_GROUP;
750 
751   if (UseInitArray) {
752     if (IsCtor) {
753       Type = ELF::SHT_INIT_ARRAY;
754       Name = ".init_array";
755     } else {
756       Type = ELF::SHT_FINI_ARRAY;
757       Name = ".fini_array";
758     }
759     if (Priority != 65535) {
760       Name += '.';
761       Name += utostr(Priority);
762     }
763   } else {
764     // The default scheme is .ctor / .dtor, so we have to invert the priority
765     // numbering.
766     if (IsCtor)
767       Name = ".ctors";
768     else
769       Name = ".dtors";
770     if (Priority != 65535)
771       raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
772     Type = ELF::SHT_PROGBITS;
773   }
774 
775   return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
776 }
777 
778 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
779     unsigned Priority, const MCSymbol *KeySym) const {
780   return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
781                                   KeySym);
782 }
783 
784 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
785     unsigned Priority, const MCSymbol *KeySym) const {
786   return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
787                                   KeySym);
788 }
789 
790 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
791     const GlobalValue *LHS, const GlobalValue *RHS,
792     const TargetMachine &TM) const {
793   // We may only use a PLT-relative relocation to refer to unnamed_addr
794   // functions.
795   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
796     return nullptr;
797 
798   // Basic sanity checks.
799   if (LHS->getType()->getPointerAddressSpace() != 0 ||
800       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
801       RHS->isThreadLocal())
802     return nullptr;
803 
804   return MCBinaryExpr::createSub(
805       MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
806                               getContext()),
807       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
808 }
809 
810 MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {
811   // Use ".GCC.command.line" since this feature is to support clang's
812   // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
813   // same name.
814   return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS,
815                                     ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
816 }
817 
818 void
819 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
820   UseInitArray = UseInitArray_;
821   MCContext &Ctx = getContext();
822   if (!UseInitArray) {
823     StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
824                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
825 
826     StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
827                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
828     return;
829   }
830 
831   StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
832                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
833   StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
834                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
835 }
836 
837 //===----------------------------------------------------------------------===//
838 //                                 MachO
839 //===----------------------------------------------------------------------===//
840 
841 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
842   : TargetLoweringObjectFile() {
843   SupportIndirectSymViaGOTPCRel = true;
844 }
845 
846 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
847                                                const TargetMachine &TM) {
848   TargetLoweringObjectFile::Initialize(Ctx, TM);
849   if (TM.getRelocationModel() == Reloc::Static) {
850     StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
851                                             SectionKind::getData());
852     StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
853                                             SectionKind::getData());
854   } else {
855     StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
856                                             MachO::S_MOD_INIT_FUNC_POINTERS,
857                                             SectionKind::getData());
858     StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
859                                             MachO::S_MOD_TERM_FUNC_POINTERS,
860                                             SectionKind::getData());
861   }
862 
863   PersonalityEncoding =
864       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
865   LSDAEncoding = dwarf::DW_EH_PE_pcrel;
866   TTypeEncoding =
867       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
868 }
869 
870 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
871                                                        Module &M) const {
872   // Emit the linker options if present.
873   if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
874     for (const auto &Option : LinkerOptions->operands()) {
875       SmallVector<std::string, 4> StrOptions;
876       for (const auto &Piece : cast<MDNode>(Option)->operands())
877         StrOptions.push_back(cast<MDString>(Piece)->getString());
878       Streamer.EmitLinkerOptions(StrOptions);
879     }
880   }
881 
882   unsigned VersionVal = 0;
883   unsigned ImageInfoFlags = 0;
884   StringRef SectionVal;
885 
886   GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
887 
888   // The section is mandatory. If we don't have it, then we don't have GC info.
889   if (SectionVal.empty())
890     return;
891 
892   StringRef Segment, Section;
893   unsigned TAA = 0, StubSize = 0;
894   bool TAAParsed;
895   std::string ErrorCode =
896     MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
897                                           TAA, TAAParsed, StubSize);
898   if (!ErrorCode.empty())
899     // If invalid, report the error with report_fatal_error.
900     report_fatal_error("Invalid section specifier '" + Section + "': " +
901                        ErrorCode + ".");
902 
903   // Get the section.
904   MCSectionMachO *S = getContext().getMachOSection(
905       Segment, Section, TAA, StubSize, SectionKind::getData());
906   Streamer.SwitchSection(S);
907   Streamer.EmitLabel(getContext().
908                      getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
909   Streamer.EmitIntValue(VersionVal, 4);
910   Streamer.EmitIntValue(ImageInfoFlags, 4);
911   Streamer.AddBlankLine();
912 }
913 
914 static void checkMachOComdat(const GlobalValue *GV) {
915   const Comdat *C = GV->getComdat();
916   if (!C)
917     return;
918 
919   report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
920                      "' cannot be lowered.");
921 }
922 
923 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
924     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
925   // Parse the section specifier and create it if valid.
926   StringRef Segment, Section;
927   unsigned TAA = 0, StubSize = 0;
928   bool TAAParsed;
929 
930   checkMachOComdat(GO);
931 
932   std::string ErrorCode =
933     MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
934                                           TAA, TAAParsed, StubSize);
935   if (!ErrorCode.empty()) {
936     // If invalid, report the error with report_fatal_error.
937     report_fatal_error("Global variable '" + GO->getName() +
938                        "' has an invalid section specifier '" +
939                        GO->getSection() + "': " + ErrorCode + ".");
940   }
941 
942   // Get the section.
943   MCSectionMachO *S =
944       getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
945 
946   // If TAA wasn't set by ParseSectionSpecifier() above,
947   // use the value returned by getMachOSection() as a default.
948   if (!TAAParsed)
949     TAA = S->getTypeAndAttributes();
950 
951   // Okay, now that we got the section, verify that the TAA & StubSize agree.
952   // If the user declared multiple globals with different section flags, we need
953   // to reject it here.
954   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
955     // If invalid, report the error with report_fatal_error.
956     report_fatal_error("Global variable '" + GO->getName() +
957                        "' section type or attributes does not match previous"
958                        " section specifier");
959   }
960 
961   return S;
962 }
963 
964 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
965     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
966   checkMachOComdat(GO);
967 
968   // Handle thread local data.
969   if (Kind.isThreadBSS()) return TLSBSSSection;
970   if (Kind.isThreadData()) return TLSDataSection;
971 
972   if (Kind.isText())
973     return GO->isWeakForLinker() ? TextCoalSection : TextSection;
974 
975   // If this is weak/linkonce, put this in a coalescable section, either in text
976   // or data depending on if it is writable.
977   if (GO->isWeakForLinker()) {
978     if (Kind.isReadOnly())
979       return ConstTextCoalSection;
980     if (Kind.isReadOnlyWithRel())
981       return ConstDataCoalSection;
982     return DataCoalSection;
983   }
984 
985   // FIXME: Alignment check should be handled by section classifier.
986   if (Kind.isMergeable1ByteCString() &&
987       GO->getParent()->getDataLayout().getPreferredAlignment(
988           cast<GlobalVariable>(GO)) < 32)
989     return CStringSection;
990 
991   // Do not put 16-bit arrays in the UString section if they have an
992   // externally visible label, this runs into issues with certain linker
993   // versions.
994   if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
995       GO->getParent()->getDataLayout().getPreferredAlignment(
996           cast<GlobalVariable>(GO)) < 32)
997     return UStringSection;
998 
999   // With MachO only variables whose corresponding symbol starts with 'l' or
1000   // 'L' can be merged, so we only try merging GVs with private linkage.
1001   if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
1002     if (Kind.isMergeableConst4())
1003       return FourByteConstantSection;
1004     if (Kind.isMergeableConst8())
1005       return EightByteConstantSection;
1006     if (Kind.isMergeableConst16())
1007       return SixteenByteConstantSection;
1008   }
1009 
1010   // Otherwise, if it is readonly, but not something we can specially optimize,
1011   // just drop it in .const.
1012   if (Kind.isReadOnly())
1013     return ReadOnlySection;
1014 
1015   // If this is marked const, put it into a const section.  But if the dynamic
1016   // linker needs to write to it, put it in the data segment.
1017   if (Kind.isReadOnlyWithRel())
1018     return ConstDataSection;
1019 
1020   // Put zero initialized globals with strong external linkage in the
1021   // DATA, __common section with the .zerofill directive.
1022   if (Kind.isBSSExtern())
1023     return DataCommonSection;
1024 
1025   // Put zero initialized globals with local linkage in __DATA,__bss directive
1026   // with the .zerofill directive (aka .lcomm).
1027   if (Kind.isBSSLocal())
1028     return DataBSSSection;
1029 
1030   // Otherwise, just drop the variable in the normal data section.
1031   return DataSection;
1032 }
1033 
1034 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
1035     const DataLayout &DL, SectionKind Kind, const Constant *C,
1036     unsigned &Align) const {
1037   // If this constant requires a relocation, we have to put it in the data
1038   // segment, not in the text segment.
1039   if (Kind.isData() || Kind.isReadOnlyWithRel())
1040     return ConstDataSection;
1041 
1042   if (Kind.isMergeableConst4())
1043     return FourByteConstantSection;
1044   if (Kind.isMergeableConst8())
1045     return EightByteConstantSection;
1046   if (Kind.isMergeableConst16())
1047     return SixteenByteConstantSection;
1048   return ReadOnlySection;  // .const
1049 }
1050 
1051 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1052     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
1053     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1054   // The mach-o version of this method defaults to returning a stub reference.
1055 
1056   if (Encoding & DW_EH_PE_indirect) {
1057     MachineModuleInfoMachO &MachOMMI =
1058       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1059 
1060     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1061 
1062     // Add information about the stub reference to MachOMMI so that the stub
1063     // gets emitted by the asmprinter.
1064     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1065     if (!StubSym.getPointer()) {
1066       MCSymbol *Sym = TM.getSymbol(GV);
1067       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1068     }
1069 
1070     return TargetLoweringObjectFile::
1071       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
1072                         Encoding & ~DW_EH_PE_indirect, Streamer);
1073   }
1074 
1075   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
1076                                                            MMI, Streamer);
1077 }
1078 
1079 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1080     const GlobalValue *GV, const TargetMachine &TM,
1081     MachineModuleInfo *MMI) const {
1082   // The mach-o version of this method defaults to returning a stub reference.
1083   MachineModuleInfoMachO &MachOMMI =
1084     MMI->getObjFileInfo<MachineModuleInfoMachO>();
1085 
1086   MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1087 
1088   // Add information about the stub reference to MachOMMI so that the stub
1089   // gets emitted by the asmprinter.
1090   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1091   if (!StubSym.getPointer()) {
1092     MCSymbol *Sym = TM.getSymbol(GV);
1093     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1094   }
1095 
1096   return SSym;
1097 }
1098 
1099 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1100     const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
1101     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1102   // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1103   // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1104   // through a non_lazy_ptr stub instead. One advantage is that it allows the
1105   // computation of deltas to final external symbols. Example:
1106   //
1107   //    _extgotequiv:
1108   //       .long   _extfoo
1109   //
1110   //    _delta:
1111   //       .long   _extgotequiv-_delta
1112   //
1113   // is transformed to:
1114   //
1115   //    _delta:
1116   //       .long   L_extfoo$non_lazy_ptr-(_delta+0)
1117   //
1118   //       .section        __IMPORT,__pointers,non_lazy_symbol_pointers
1119   //    L_extfoo$non_lazy_ptr:
1120   //       .indirect_symbol        _extfoo
1121   //       .long   0
1122   //
1123   // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1124   // may point to both local (same translation unit) and global (other
1125   // translation units) symbols. Example:
1126   //
1127   // .section __DATA,__pointers,non_lazy_symbol_pointers
1128   // L1:
1129   //    .indirect_symbol _myGlobal
1130   //    .long 0
1131   // L2:
1132   //    .indirect_symbol _myLocal
1133   //    .long _myLocal
1134   //
1135   // If the symbol is local, instead of the symbol's index, the assembler
1136   // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1137   // Then the linker will notice the constant in the table and will look at the
1138   // content of the symbol.
1139   MachineModuleInfoMachO &MachOMMI =
1140     MMI->getObjFileInfo<MachineModuleInfoMachO>();
1141   MCContext &Ctx = getContext();
1142 
1143   // The offset must consider the original displacement from the base symbol
1144   // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1145   Offset = -MV.getConstant();
1146   const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
1147 
1148   // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1149   // non_lazy_ptr stubs.
1150   SmallString<128> Name;
1151   StringRef Suffix = "$non_lazy_ptr";
1152   Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
1153   Name += Sym->getName();
1154   Name += Suffix;
1155   MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
1156 
1157   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
1158   if (!StubSym.getPointer()) {
1159     bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal();
1160     // With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
1161     StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
1162                                                  !IsIndirectLocal);
1163   }
1164 
1165   const MCExpr *BSymExpr =
1166     MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
1167   const MCExpr *LHS =
1168     MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
1169 
1170   if (!Offset)
1171     return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
1172 
1173   const MCExpr *RHS =
1174     MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
1175   return MCBinaryExpr::createSub(LHS, RHS, Ctx);
1176 }
1177 
1178 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
1179                                const MCSection &Section) {
1180   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
1181     return true;
1182 
1183   // If it is not dead stripped, it is safe to use private labels.
1184   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
1185   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
1186     return true;
1187 
1188   return false;
1189 }
1190 
1191 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1192     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1193     const TargetMachine &TM) const {
1194   bool CannotUsePrivateLabel = true;
1195   if (auto *GO = GV->getBaseObject()) {
1196     SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
1197     const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
1198     CannotUsePrivateLabel =
1199         !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
1200   }
1201   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1202 }
1203 
1204 //===----------------------------------------------------------------------===//
1205 //                                  COFF
1206 //===----------------------------------------------------------------------===//
1207 
1208 static unsigned
1209 getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
1210   unsigned Flags = 0;
1211   bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
1212 
1213   if (K.isMetadata())
1214     Flags |=
1215       COFF::IMAGE_SCN_MEM_DISCARDABLE;
1216   else if (K.isText())
1217     Flags |=
1218       COFF::IMAGE_SCN_MEM_EXECUTE |
1219       COFF::IMAGE_SCN_MEM_READ |
1220       COFF::IMAGE_SCN_CNT_CODE |
1221       (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
1222   else if (K.isBSS())
1223     Flags |=
1224       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1225       COFF::IMAGE_SCN_MEM_READ |
1226       COFF::IMAGE_SCN_MEM_WRITE;
1227   else if (K.isThreadLocal())
1228     Flags |=
1229       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1230       COFF::IMAGE_SCN_MEM_READ |
1231       COFF::IMAGE_SCN_MEM_WRITE;
1232   else if (K.isReadOnly() || K.isReadOnlyWithRel())
1233     Flags |=
1234       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1235       COFF::IMAGE_SCN_MEM_READ;
1236   else if (K.isWriteable())
1237     Flags |=
1238       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1239       COFF::IMAGE_SCN_MEM_READ |
1240       COFF::IMAGE_SCN_MEM_WRITE;
1241 
1242   return Flags;
1243 }
1244 
1245 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
1246   const Comdat *C = GV->getComdat();
1247   assert(C && "expected GV to have a Comdat!");
1248 
1249   StringRef ComdatGVName = C->getName();
1250   const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
1251   if (!ComdatGV)
1252     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1253                        "' does not exist.");
1254 
1255   if (ComdatGV->getComdat() != C)
1256     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1257                        "' is not a key for its COMDAT.");
1258 
1259   return ComdatGV;
1260 }
1261 
1262 static int getSelectionForCOFF(const GlobalValue *GV) {
1263   if (const Comdat *C = GV->getComdat()) {
1264     const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1265     if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1266       ComdatKey = GA->getBaseObject();
1267     if (ComdatKey == GV) {
1268       switch (C->getSelectionKind()) {
1269       case Comdat::Any:
1270         return COFF::IMAGE_COMDAT_SELECT_ANY;
1271       case Comdat::ExactMatch:
1272         return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
1273       case Comdat::Largest:
1274         return COFF::IMAGE_COMDAT_SELECT_LARGEST;
1275       case Comdat::NoDuplicates:
1276         return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1277       case Comdat::SameSize:
1278         return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
1279       }
1280     } else {
1281       return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1282     }
1283   }
1284   return 0;
1285 }
1286 
1287 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1288     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1289   int Selection = 0;
1290   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1291   StringRef Name = GO->getSection();
1292   StringRef COMDATSymName = "";
1293   if (GO->hasComdat()) {
1294     Selection = getSelectionForCOFF(GO);
1295     const GlobalValue *ComdatGV;
1296     if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1297       ComdatGV = getComdatGVForCOFF(GO);
1298     else
1299       ComdatGV = GO;
1300 
1301     if (!ComdatGV->hasPrivateLinkage()) {
1302       MCSymbol *Sym = TM.getSymbol(ComdatGV);
1303       COMDATSymName = Sym->getName();
1304       Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1305     } else {
1306       Selection = 0;
1307     }
1308   }
1309 
1310   return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1311                                      Selection);
1312 }
1313 
1314 static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
1315   if (Kind.isText())
1316     return ".text";
1317   if (Kind.isBSS())
1318     return ".bss";
1319   if (Kind.isThreadLocal())
1320     return ".tls$";
1321   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1322     return ".rdata";
1323   return ".data";
1324 }
1325 
1326 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1327     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1328   // If we have -ffunction-sections then we should emit the global value to a
1329   // uniqued section specifically for it.
1330   bool EmitUniquedSection;
1331   if (Kind.isText())
1332     EmitUniquedSection = TM.getFunctionSections();
1333   else
1334     EmitUniquedSection = TM.getDataSections();
1335 
1336   if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
1337     SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
1338 
1339     unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1340 
1341     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1342     int Selection = getSelectionForCOFF(GO);
1343     if (!Selection)
1344       Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1345     const GlobalValue *ComdatGV;
1346     if (GO->hasComdat())
1347       ComdatGV = getComdatGVForCOFF(GO);
1348     else
1349       ComdatGV = GO;
1350 
1351     unsigned UniqueID = MCContext::GenericSectionID;
1352     if (EmitUniquedSection)
1353       UniqueID = NextUniqueID++;
1354 
1355     if (!ComdatGV->hasPrivateLinkage()) {
1356       MCSymbol *Sym = TM.getSymbol(ComdatGV);
1357       StringRef COMDATSymName = Sym->getName();
1358 
1359       // Append "$symbol" to the section name *before* IR-level mangling is
1360       // applied when targetting mingw. This is what GCC does, and the ld.bfd
1361       // COFF linker will not properly handle comdats otherwise.
1362       if (getTargetTriple().isWindowsGNUEnvironment())
1363         raw_svector_ostream(Name) << '$' << ComdatGV->getName();
1364 
1365       return getContext().getCOFFSection(Name, Characteristics, Kind,
1366                                          COMDATSymName, Selection, UniqueID);
1367     } else {
1368       SmallString<256> TmpData;
1369       getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
1370       return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
1371                                          Selection, UniqueID);
1372     }
1373   }
1374 
1375   if (Kind.isText())
1376     return TextSection;
1377 
1378   if (Kind.isThreadLocal())
1379     return TLSDataSection;
1380 
1381   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1382     return ReadOnlySection;
1383 
1384   // Note: we claim that common symbols are put in BSSSection, but they are
1385   // really emitted with the magic .comm directive, which creates a symbol table
1386   // entry but not a section.
1387   if (Kind.isBSS() || Kind.isCommon())
1388     return BSSSection;
1389 
1390   return DataSection;
1391 }
1392 
1393 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1394     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1395     const TargetMachine &TM) const {
1396   bool CannotUsePrivateLabel = false;
1397   if (GV->hasPrivateLinkage() &&
1398       ((isa<Function>(GV) && TM.getFunctionSections()) ||
1399        (isa<GlobalVariable>(GV) && TM.getDataSections())))
1400     CannotUsePrivateLabel = true;
1401 
1402   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1403 }
1404 
1405 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1406     const Function &F, const TargetMachine &TM) const {
1407   // If the function can be removed, produce a unique section so that
1408   // the table doesn't prevent the removal.
1409   const Comdat *C = F.getComdat();
1410   bool EmitUniqueSection = TM.getFunctionSections() || C;
1411   if (!EmitUniqueSection)
1412     return ReadOnlySection;
1413 
1414   // FIXME: we should produce a symbol for F instead.
1415   if (F.hasPrivateLinkage())
1416     return ReadOnlySection;
1417 
1418   MCSymbol *Sym = TM.getSymbol(&F);
1419   StringRef COMDATSymName = Sym->getName();
1420 
1421   SectionKind Kind = SectionKind::getReadOnly();
1422   StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
1423   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1424   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1425   unsigned UniqueID = NextUniqueID++;
1426 
1427   return getContext().getCOFFSection(
1428       SecName, Characteristics, Kind, COMDATSymName,
1429       COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
1430 }
1431 
1432 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
1433                                                       Module &M) const {
1434   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1435     // Emit the linker options to the linker .drectve section.  According to the
1436     // spec, this section is a space-separated string containing flags for
1437     // linker.
1438     MCSection *Sec = getDrectveSection();
1439     Streamer.SwitchSection(Sec);
1440     for (const auto &Option : LinkerOptions->operands()) {
1441       for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1442         // Lead with a space for consistency with our dllexport implementation.
1443         std::string Directive(" ");
1444         Directive.append(cast<MDString>(Piece)->getString());
1445         Streamer.EmitBytes(Directive);
1446       }
1447     }
1448   }
1449 
1450   unsigned Version = 0;
1451   unsigned Flags = 0;
1452   StringRef Section;
1453 
1454   GetObjCImageInfo(M, Version, Flags, Section);
1455   if (Section.empty())
1456     return;
1457 
1458   auto &C = getContext();
1459   auto *S = C.getCOFFSection(
1460       Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1461       SectionKind::getReadOnly());
1462   Streamer.SwitchSection(S);
1463   Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1464   Streamer.EmitIntValue(Version, 4);
1465   Streamer.EmitIntValue(Flags, 4);
1466   Streamer.AddBlankLine();
1467 }
1468 
1469 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1470                                               const TargetMachine &TM) {
1471   TargetLoweringObjectFile::Initialize(Ctx, TM);
1472   const Triple &T = TM.getTargetTriple();
1473   if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1474     StaticCtorSection =
1475         Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1476                                            COFF::IMAGE_SCN_MEM_READ,
1477                            SectionKind::getReadOnly());
1478     StaticDtorSection =
1479         Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1480                                            COFF::IMAGE_SCN_MEM_READ,
1481                            SectionKind::getReadOnly());
1482   } else {
1483     StaticCtorSection = Ctx.getCOFFSection(
1484         ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1485                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1486         SectionKind::getData());
1487     StaticDtorSection = Ctx.getCOFFSection(
1488         ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1489                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1490         SectionKind::getData());
1491   }
1492 }
1493 
1494 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
1495                                                    const Triple &T, bool IsCtor,
1496                                                    unsigned Priority,
1497                                                    const MCSymbol *KeySym,
1498                                                    MCSectionCOFF *Default) {
1499   if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1500     // If the priority is the default, use .CRT$XCU, possibly associative.
1501     if (Priority == 65535)
1502       return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1503 
1504     // Otherwise, we need to compute a new section name. Low priorities should
1505     // run earlier. The linker will sort sections ASCII-betically, and we need a
1506     // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1507     // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1508     // low priorities need to sort before 'L', since the CRT uses that
1509     // internally, so we use ".CRT$XCA00001" for them.
1510     SmallString<24> Name;
1511     raw_svector_ostream OS(Name);
1512     OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
1513     MCSectionCOFF *Sec = Ctx.getCOFFSection(
1514         Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1515         SectionKind::getReadOnly());
1516     return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
1517   }
1518 
1519   std::string Name = IsCtor ? ".ctors" : ".dtors";
1520   if (Priority != 65535)
1521     raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1522 
1523   return Ctx.getAssociativeCOFFSection(
1524       Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1525                                    COFF::IMAGE_SCN_MEM_READ |
1526                                    COFF::IMAGE_SCN_MEM_WRITE,
1527                          SectionKind::getData()),
1528       KeySym, 0);
1529 }
1530 
1531 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
1532     unsigned Priority, const MCSymbol *KeySym) const {
1533   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1534                                       Priority, KeySym,
1535                                       cast<MCSectionCOFF>(StaticCtorSection));
1536 }
1537 
1538 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
1539     unsigned Priority, const MCSymbol *KeySym) const {
1540   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1541                                       Priority, KeySym,
1542                                       cast<MCSectionCOFF>(StaticDtorSection));
1543 }
1544 
1545 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1546     raw_ostream &OS, const GlobalValue *GV) const {
1547   emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
1548 }
1549 
1550 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1551     raw_ostream &OS, const GlobalValue *GV) const {
1552   emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
1553 }
1554 
1555 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
1556     const GlobalValue *LHS, const GlobalValue *RHS,
1557     const TargetMachine &TM) const {
1558   const Triple &T = TM.getTargetTriple();
1559   if (T.isOSCygMing())
1560     return nullptr;
1561 
1562   // Our symbols should exist in address space zero, cowardly no-op if
1563   // otherwise.
1564   if (LHS->getType()->getPointerAddressSpace() != 0 ||
1565       RHS->getType()->getPointerAddressSpace() != 0)
1566     return nullptr;
1567 
1568   // Both ptrtoint instructions must wrap global objects:
1569   // - Only global variables are eligible for image relative relocations.
1570   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1571   // We expect __ImageBase to be a global variable without a section, externally
1572   // defined.
1573   //
1574   // It should look something like this: @__ImageBase = external constant i8
1575   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
1576       LHS->isThreadLocal() || RHS->isThreadLocal() ||
1577       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
1578       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
1579     return nullptr;
1580 
1581   return MCSymbolRefExpr::create(TM.getSymbol(LHS),
1582                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
1583                                  getContext());
1584 }
1585 
1586 static std::string APIntToHexString(const APInt &AI) {
1587   unsigned Width = (AI.getBitWidth() / 8) * 2;
1588   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
1589   unsigned Size = HexString.size();
1590   assert(Width >= Size && "hex string is too large!");
1591   HexString.insert(HexString.begin(), Width - Size, '0');
1592 
1593   return HexString;
1594 }
1595 
1596 static std::string scalarConstantToHexString(const Constant *C) {
1597   Type *Ty = C->getType();
1598   if (isa<UndefValue>(C)) {
1599     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
1600   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
1601     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
1602   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
1603     return APIntToHexString(CI->getValue());
1604   } else {
1605     unsigned NumElements;
1606     if (isa<VectorType>(Ty))
1607       NumElements = Ty->getVectorNumElements();
1608     else
1609       NumElements = Ty->getArrayNumElements();
1610     std::string HexString;
1611     for (int I = NumElements - 1, E = -1; I != E; --I)
1612       HexString += scalarConstantToHexString(C->getAggregateElement(I));
1613     return HexString;
1614   }
1615 }
1616 
1617 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
1618     const DataLayout &DL, SectionKind Kind, const Constant *C,
1619     unsigned &Align) const {
1620   if (Kind.isMergeableConst() && C &&
1621       getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1622     // This creates comdat sections with the given symbol name, but unless
1623     // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1624     // will be created with a null storage class, which makes GNU binutils
1625     // error out.
1626     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1627                                      COFF::IMAGE_SCN_MEM_READ |
1628                                      COFF::IMAGE_SCN_LNK_COMDAT;
1629     std::string COMDATSymName;
1630     if (Kind.isMergeableConst4()) {
1631       if (Align <= 4) {
1632         COMDATSymName = "__real@" + scalarConstantToHexString(C);
1633         Align = 4;
1634       }
1635     } else if (Kind.isMergeableConst8()) {
1636       if (Align <= 8) {
1637         COMDATSymName = "__real@" + scalarConstantToHexString(C);
1638         Align = 8;
1639       }
1640     } else if (Kind.isMergeableConst16()) {
1641       // FIXME: These may not be appropriate for non-x86 architectures.
1642       if (Align <= 16) {
1643         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
1644         Align = 16;
1645       }
1646     } else if (Kind.isMergeableConst32()) {
1647       if (Align <= 32) {
1648         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
1649         Align = 32;
1650       }
1651     }
1652 
1653     if (!COMDATSymName.empty())
1654       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
1655                                          COMDATSymName,
1656                                          COFF::IMAGE_COMDAT_SELECT_ANY);
1657   }
1658 
1659   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
1660 }
1661 
1662 
1663 //===----------------------------------------------------------------------===//
1664 //                                  Wasm
1665 //===----------------------------------------------------------------------===//
1666 
1667 static const Comdat *getWasmComdat(const GlobalValue *GV) {
1668   const Comdat *C = GV->getComdat();
1669   if (!C)
1670     return nullptr;
1671 
1672   if (C->getSelectionKind() != Comdat::Any)
1673     report_fatal_error("WebAssembly COMDATs only support "
1674                        "SelectionKind::Any, '" + C->getName() + "' cannot be "
1675                        "lowered.");
1676 
1677   return C;
1678 }
1679 
1680 static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
1681   // If we're told we have function data, then use that.
1682   if (K.isText())
1683     return SectionKind::getText();
1684 
1685   // Otherwise, ignore whatever section type the generic impl detected and use
1686   // a plain data section.
1687   return SectionKind::getData();
1688 }
1689 
1690 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1691     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1692   // We don't support explict section names for functions in the wasm object
1693   // format.  Each function has to be in its own unique section.
1694   if (isa<Function>(GO)) {
1695     return SelectSectionForGlobal(GO, Kind, TM);
1696   }
1697 
1698   StringRef Name = GO->getSection();
1699 
1700   Kind = getWasmKindForNamedSection(Name, Kind);
1701 
1702   StringRef Group = "";
1703   if (const Comdat *C = getWasmComdat(GO)) {
1704     Group = C->getName();
1705   }
1706 
1707   MCSectionWasm* Section =
1708       getContext().getWasmSection(Name, Kind, Group,
1709                                   MCContext::GenericSectionID);
1710 
1711   return Section;
1712 }
1713 
1714 static MCSectionWasm *selectWasmSectionForGlobal(
1715     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
1716     const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
1717   StringRef Group = "";
1718   if (const Comdat *C = getWasmComdat(GO)) {
1719     Group = C->getName();
1720   }
1721 
1722   bool UniqueSectionNames = TM.getUniqueSectionNames();
1723   SmallString<128> Name = getSectionPrefixForGlobal(Kind);
1724 
1725   if (const auto *F = dyn_cast<Function>(GO)) {
1726     const auto &OptionalPrefix = F->getSectionPrefix();
1727     if (OptionalPrefix)
1728       Name += *OptionalPrefix;
1729   }
1730 
1731   if (EmitUniqueSection && UniqueSectionNames) {
1732     Name.push_back('.');
1733     TM.getNameWithPrefix(Name, GO, Mang, true);
1734   }
1735   unsigned UniqueID = MCContext::GenericSectionID;
1736   if (EmitUniqueSection && !UniqueSectionNames) {
1737     UniqueID = *NextUniqueID;
1738     (*NextUniqueID)++;
1739   }
1740 
1741   return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
1742 }
1743 
1744 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1745     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1746 
1747   if (Kind.isCommon())
1748     report_fatal_error("mergable sections not supported yet on wasm");
1749 
1750   // If we have -ffunction-section or -fdata-section then we should emit the
1751   // global value to a uniqued section specifically for it.
1752   bool EmitUniqueSection = false;
1753   if (Kind.isText())
1754     EmitUniqueSection = TM.getFunctionSections();
1755   else
1756     EmitUniqueSection = TM.getDataSections();
1757   EmitUniqueSection |= GO->hasComdat();
1758 
1759   return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
1760                                     EmitUniqueSection, &NextUniqueID);
1761 }
1762 
1763 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1764     bool UsesLabelDifference, const Function &F) const {
1765   // We can always create relative relocations, so use another section
1766   // that can be marked non-executable.
1767   return false;
1768 }
1769 
1770 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
1771     const GlobalValue *LHS, const GlobalValue *RHS,
1772     const TargetMachine &TM) const {
1773   // We may only use a PLT-relative relocation to refer to unnamed_addr
1774   // functions.
1775   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1776     return nullptr;
1777 
1778   // Basic sanity checks.
1779   if (LHS->getType()->getPointerAddressSpace() != 0 ||
1780       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1781       RHS->isThreadLocal())
1782     return nullptr;
1783 
1784   return MCBinaryExpr::createSub(
1785       MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
1786                               getContext()),
1787       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
1788 }
1789 
1790 void TargetLoweringObjectFileWasm::InitializeWasm() {
1791   StaticCtorSection =
1792       getContext().getWasmSection(".init_array", SectionKind::getData());
1793 
1794   // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
1795   // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
1796   TTypeEncoding = dwarf::DW_EH_PE_absptr;
1797 }
1798 
1799 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
1800     unsigned Priority, const MCSymbol *KeySym) const {
1801   return Priority == UINT16_MAX ?
1802          StaticCtorSection :
1803          getContext().getWasmSection(".init_array." + utostr(Priority),
1804                                      SectionKind::getData());
1805 }
1806 
1807 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
1808     unsigned Priority, const MCSymbol *KeySym) const {
1809   llvm_unreachable("@llvm.global_dtors should have been lowered already");
1810   return nullptr;
1811 }
1812