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