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