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