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