xref: /llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (revision f84b6300445e726f318ecafd8fec4048a7af1d6e)
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/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCSectionCOFF.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCSectionMachO.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbolELF.h"
35 #include "llvm/MC/MCValue.h"
36 #include "llvm/ProfileData/InstrProf.h"
37 #include "llvm/ProfileData/ProfileCommon.h"
38 #include "llvm/Support/COFF.h"
39 #include "llvm/Support/Dwarf.h"
40 #include "llvm/Support/ELF.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetLowering.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetSubtargetInfo.h"
46 using namespace llvm;
47 using namespace dwarf;
48 
49 //===----------------------------------------------------------------------===//
50 //                                  ELF
51 //===----------------------------------------------------------------------===//
52 
53 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
54     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
55     MachineModuleInfo *MMI) const {
56   unsigned Encoding = getPersonalityEncoding();
57   if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
58     return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
59                                           TM.getSymbol(GV, Mang)->getName());
60   if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
61     return TM.getSymbol(GV, Mang);
62   report_fatal_error("We do not support this DWARF encoding yet!");
63 }
64 
65 void TargetLoweringObjectFileELF::emitPersonalityValue(
66     MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
67   SmallString<64> NameData("DW.ref.");
68   NameData += Sym->getName();
69   MCSymbolELF *Label =
70       cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
71   Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
72   Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
73   StringRef Prefix = ".data.";
74   NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
75   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
76   MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS,
77                                               Flags, 0, Label->getName());
78   unsigned Size = DL.getPointerSize();
79   Streamer.SwitchSection(Sec);
80   Streamer.EmitValueToAlignment(DL.getPointerABIAlignment());
81   Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
82   const MCExpr *E = MCConstantExpr::create(Size, getContext());
83   Streamer.emitELFSize(Label, E);
84   Streamer.EmitLabel(Label);
85 
86   Streamer.EmitSymbolValue(Sym, Size);
87 }
88 
89 const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
90     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
91     const TargetMachine &TM, MachineModuleInfo *MMI,
92     MCStreamer &Streamer) const {
93 
94   if (Encoding & dwarf::DW_EH_PE_indirect) {
95     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
96 
97     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
98 
99     // Add information about the stub reference to ELFMMI so that the stub
100     // gets emitted by the asmprinter.
101     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
102     if (!StubSym.getPointer()) {
103       MCSymbol *Sym = TM.getSymbol(GV, Mang);
104       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
105     }
106 
107     return TargetLoweringObjectFile::
108       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
109                         Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
110   }
111 
112   return TargetLoweringObjectFile::
113     getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
114 }
115 
116 static SectionKind
117 getELFKindForNamedSection(StringRef Name, SectionKind K) {
118   // N.B.: The defaults used in here are no the same ones used in MC.
119   // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
120   // both gas and MC will produce a section with no flags. Given
121   // section(".eh_frame") gcc will produce:
122   //
123   //   .section   .eh_frame,"a",@progbits
124 
125   if (Name == getInstrProfCoverageSectionName(false))
126     return SectionKind::getMetadata();
127 
128   if (Name.empty() || Name[0] != '.') return K;
129 
130   // Some lame default implementation based on some magic section names.
131   if (Name == ".bss" ||
132       Name.startswith(".bss.") ||
133       Name.startswith(".gnu.linkonce.b.") ||
134       Name.startswith(".llvm.linkonce.b.") ||
135       Name == ".sbss" ||
136       Name.startswith(".sbss.") ||
137       Name.startswith(".gnu.linkonce.sb.") ||
138       Name.startswith(".llvm.linkonce.sb."))
139     return SectionKind::getBSS();
140 
141   if (Name == ".tdata" ||
142       Name.startswith(".tdata.") ||
143       Name.startswith(".gnu.linkonce.td.") ||
144       Name.startswith(".llvm.linkonce.td."))
145     return SectionKind::getThreadData();
146 
147   if (Name == ".tbss" ||
148       Name.startswith(".tbss.") ||
149       Name.startswith(".gnu.linkonce.tb.") ||
150       Name.startswith(".llvm.linkonce.tb."))
151     return SectionKind::getThreadBSS();
152 
153   return K;
154 }
155 
156 
157 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
158 
159   if (Name == ".init_array")
160     return ELF::SHT_INIT_ARRAY;
161 
162   if (Name == ".fini_array")
163     return ELF::SHT_FINI_ARRAY;
164 
165   if (Name == ".preinit_array")
166     return ELF::SHT_PREINIT_ARRAY;
167 
168   if (K.isBSS() || K.isThreadBSS())
169     return ELF::SHT_NOBITS;
170 
171   return ELF::SHT_PROGBITS;
172 }
173 
174 static unsigned getELFSectionFlags(SectionKind K) {
175   unsigned Flags = 0;
176 
177   if (!K.isMetadata())
178     Flags |= ELF::SHF_ALLOC;
179 
180   if (K.isText())
181     Flags |= ELF::SHF_EXECINSTR;
182 
183   if (K.isWriteable())
184     Flags |= ELF::SHF_WRITE;
185 
186   if (K.isThreadLocal())
187     Flags |= ELF::SHF_TLS;
188 
189   if (K.isMergeableCString() || K.isMergeableConst())
190     Flags |= ELF::SHF_MERGE;
191 
192   if (K.isMergeableCString())
193     Flags |= ELF::SHF_STRINGS;
194 
195   return Flags;
196 }
197 
198 static const Comdat *getELFComdat(const GlobalValue *GV) {
199   const Comdat *C = GV->getComdat();
200   if (!C)
201     return nullptr;
202 
203   if (C->getSelectionKind() != Comdat::Any)
204     report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
205                        C->getName() + "' cannot be lowered.");
206 
207   return C;
208 }
209 
210 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
211     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
212     const TargetMachine &TM) const {
213   StringRef SectionName = GV->getSection();
214 
215   // Infer section flags from the section name if we can.
216   Kind = getELFKindForNamedSection(SectionName, Kind);
217 
218   StringRef Group = "";
219   unsigned Flags = getELFSectionFlags(Kind);
220   if (const Comdat *C = getELFComdat(GV)) {
221     Group = C->getName();
222     Flags |= ELF::SHF_GROUP;
223   }
224   return getContext().getELFSection(SectionName,
225                                     getELFSectionType(SectionName, Kind), Flags,
226                                     /*EntrySize=*/0, Group);
227 }
228 
229 /// Return the section prefix name used by options FunctionsSections and
230 /// DataSections.
231 static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
232   if (Kind.isText())
233     return ".text";
234   if (Kind.isReadOnly())
235     return ".rodata";
236   if (Kind.isBSS())
237     return ".bss";
238   if (Kind.isThreadData())
239     return ".tdata";
240   if (Kind.isThreadBSS())
241     return ".tbss";
242   if (Kind.isData())
243     return ".data";
244   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
245   return ".data.rel.ro";
246 }
247 
248 static cl::opt<bool> GroupFunctionsByHotness(
249     "group-functions-by-hotness",
250     llvm::cl::desc("Partition hot/cold functions by sections prefix"),
251     cl::init(false));
252 
253 static MCSectionELF *
254 selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV,
255                           SectionKind Kind, Mangler &Mang,
256                           const TargetMachine &TM, bool EmitUniqueSection,
257                           unsigned Flags, unsigned *NextUniqueID) {
258   unsigned EntrySize = 0;
259   if (Kind.isMergeableCString()) {
260     if (Kind.isMergeable2ByteCString()) {
261       EntrySize = 2;
262     } else if (Kind.isMergeable4ByteCString()) {
263       EntrySize = 4;
264     } else {
265       EntrySize = 1;
266       assert(Kind.isMergeable1ByteCString() && "unknown string width");
267     }
268   } else if (Kind.isMergeableConst()) {
269     if (Kind.isMergeableConst4()) {
270       EntrySize = 4;
271     } else if (Kind.isMergeableConst8()) {
272       EntrySize = 8;
273     } else if (Kind.isMergeableConst16()) {
274       EntrySize = 16;
275     } else {
276       assert(Kind.isMergeableConst32() && "unknown data width");
277       EntrySize = 32;
278     }
279   }
280 
281   StringRef Group = "";
282   if (const Comdat *C = getELFComdat(GV)) {
283     Flags |= ELF::SHF_GROUP;
284     Group = C->getName();
285   }
286 
287   bool UniqueSectionNames = TM.getUniqueSectionNames();
288   SmallString<128> Name;
289   if (Kind.isMergeableCString()) {
290     // We also need alignment here.
291     // FIXME: this is getting the alignment of the character, not the
292     // alignment of the global!
293     unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment(
294         cast<GlobalVariable>(GV));
295 
296     std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
297     Name = SizeSpec + utostr(Align);
298   } else if (Kind.isMergeableConst()) {
299     Name = ".rodata.cst";
300     Name += utostr(EntrySize);
301   } else {
302     Name = getSectionPrefixForGlobal(Kind);
303   }
304 
305   if (GroupFunctionsByHotness) {
306     if (const Function *F = dyn_cast<Function>(GV)) {
307       if (ProfileSummary::isFunctionHot(F)) {
308         Name += getHotSectionPrefix();
309       } else if (ProfileSummary::isFunctionUnlikely(F)) {
310         Name += getUnlikelySectionPrefix();
311       }
312     }
313   }
314 
315   if (EmitUniqueSection && UniqueSectionNames) {
316     Name.push_back('.');
317     TM.getNameWithPrefix(Name, GV, Mang, true);
318   }
319   unsigned UniqueID = ~0;
320   if (EmitUniqueSection && !UniqueSectionNames) {
321     UniqueID = *NextUniqueID;
322     (*NextUniqueID)++;
323   }
324   return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
325                            EntrySize, Group, UniqueID);
326 }
327 
328 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
329     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
330     const TargetMachine &TM) const {
331   unsigned Flags = getELFSectionFlags(Kind);
332 
333   // If we have -ffunction-section or -fdata-section then we should emit the
334   // global value to a uniqued section specifically for it.
335   bool EmitUniqueSection = false;
336   if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
337     if (Kind.isText())
338       EmitUniqueSection = TM.getFunctionSections();
339     else
340       EmitUniqueSection = TM.getDataSections();
341   }
342   EmitUniqueSection |= GV->hasComdat();
343 
344   return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM,
345                                    EmitUniqueSection, Flags, &NextUniqueID);
346 }
347 
348 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
349     const Function &F, Mangler &Mang, const TargetMachine &TM) const {
350   // If the function can be removed, produce a unique section so that
351   // the table doesn't prevent the removal.
352   const Comdat *C = F.getComdat();
353   bool EmitUniqueSection = TM.getFunctionSections() || C;
354   if (!EmitUniqueSection)
355     return ReadOnlySection;
356 
357   return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
358                                    Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC,
359                                    &NextUniqueID);
360 }
361 
362 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
363     bool UsesLabelDifference, const Function &F) const {
364   // We can always create relative relocations, so use another section
365   // that can be marked non-executable.
366   return false;
367 }
368 
369 /// Given a mergeable constant with the specified size and relocation
370 /// information, return a section that it should be placed in.
371 MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
372     const DataLayout &DL, SectionKind Kind, const Constant *C,
373     unsigned &Align) const {
374   if (Kind.isMergeableConst4() && MergeableConst4Section)
375     return MergeableConst4Section;
376   if (Kind.isMergeableConst8() && MergeableConst8Section)
377     return MergeableConst8Section;
378   if (Kind.isMergeableConst16() && MergeableConst16Section)
379     return MergeableConst16Section;
380   if (Kind.isMergeableConst32() && MergeableConst32Section)
381     return MergeableConst32Section;
382   if (Kind.isReadOnly())
383     return ReadOnlySection;
384 
385   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
386   return DataRelROSection;
387 }
388 
389 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
390                                               bool IsCtor, unsigned Priority,
391                                               const MCSymbol *KeySym) {
392   std::string Name;
393   unsigned Type;
394   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
395   StringRef COMDAT = KeySym ? KeySym->getName() : "";
396 
397   if (KeySym)
398     Flags |= ELF::SHF_GROUP;
399 
400   if (UseInitArray) {
401     if (IsCtor) {
402       Type = ELF::SHT_INIT_ARRAY;
403       Name = ".init_array";
404     } else {
405       Type = ELF::SHT_FINI_ARRAY;
406       Name = ".fini_array";
407     }
408     if (Priority != 65535) {
409       Name += '.';
410       Name += utostr(Priority);
411     }
412   } else {
413     // The default scheme is .ctor / .dtor, so we have to invert the priority
414     // numbering.
415     if (IsCtor)
416       Name = ".ctors";
417     else
418       Name = ".dtors";
419     if (Priority != 65535) {
420       Name += '.';
421       Name += utostr(65535 - Priority);
422     }
423     Type = ELF::SHT_PROGBITS;
424   }
425 
426   return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
427 }
428 
429 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
430     unsigned Priority, const MCSymbol *KeySym) const {
431   return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
432                                   KeySym);
433 }
434 
435 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
436     unsigned Priority, const MCSymbol *KeySym) const {
437   return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
438                                   KeySym);
439 }
440 
441 void
442 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
443   UseInitArray = UseInitArray_;
444   if (!UseInitArray)
445     return;
446 
447   StaticCtorSection = getContext().getELFSection(
448       ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
449   StaticDtorSection = getContext().getELFSection(
450       ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
451 }
452 
453 //===----------------------------------------------------------------------===//
454 //                                 MachO
455 //===----------------------------------------------------------------------===//
456 
457 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
458   : TargetLoweringObjectFile() {
459   SupportIndirectSymViaGOTPCRel = true;
460 }
461 
462 /// emitModuleFlags - Perform code emission for module flags.
463 void TargetLoweringObjectFileMachO::
464 emitModuleFlags(MCStreamer &Streamer,
465                 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
466                 Mangler &Mang, const TargetMachine &TM) const {
467   unsigned VersionVal = 0;
468   unsigned ImageInfoFlags = 0;
469   MDNode *LinkerOptions = nullptr;
470   StringRef SectionVal;
471 
472   for (ArrayRef<Module::ModuleFlagEntry>::iterator
473          i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
474     const Module::ModuleFlagEntry &MFE = *i;
475 
476     // Ignore flags with 'Require' behavior.
477     if (MFE.Behavior == Module::Require)
478       continue;
479 
480     StringRef Key = MFE.Key->getString();
481     Metadata *Val = MFE.Val;
482 
483     if (Key == "Objective-C Image Info Version") {
484       VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
485     } else if (Key == "Objective-C Garbage Collection" ||
486                Key == "Objective-C GC Only" ||
487                Key == "Objective-C Is Simulated" ||
488                Key == "Objective-C Class Properties" ||
489                Key == "Objective-C Image Swift Version") {
490       ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
491     } else if (Key == "Objective-C Image Info Section") {
492       SectionVal = cast<MDString>(Val)->getString();
493     } else if (Key == "Linker Options") {
494       LinkerOptions = cast<MDNode>(Val);
495     }
496   }
497 
498   // Emit the linker options if present.
499   if (LinkerOptions) {
500     for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
501       MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
502       SmallVector<std::string, 4> StrOptions;
503 
504       // Convert to strings.
505       for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
506         MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
507         StrOptions.push_back(MDOption->getString());
508       }
509 
510       Streamer.EmitLinkerOptions(StrOptions);
511     }
512   }
513 
514   // The section is mandatory. If we don't have it, then we don't have GC info.
515   if (SectionVal.empty()) return;
516 
517   StringRef Segment, Section;
518   unsigned TAA = 0, StubSize = 0;
519   bool TAAParsed;
520   std::string ErrorCode =
521     MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
522                                           TAA, TAAParsed, StubSize);
523   if (!ErrorCode.empty())
524     // If invalid, report the error with report_fatal_error.
525     report_fatal_error("Invalid section specifier '" + Section + "': " +
526                        ErrorCode + ".");
527 
528   // Get the section.
529   MCSectionMachO *S = getContext().getMachOSection(
530       Segment, Section, TAA, StubSize, SectionKind::getData());
531   Streamer.SwitchSection(S);
532   Streamer.EmitLabel(getContext().
533                      getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
534   Streamer.EmitIntValue(VersionVal, 4);
535   Streamer.EmitIntValue(ImageInfoFlags, 4);
536   Streamer.AddBlankLine();
537 }
538 
539 static void checkMachOComdat(const GlobalValue *GV) {
540   const Comdat *C = GV->getComdat();
541   if (!C)
542     return;
543 
544   report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
545                      "' cannot be lowered.");
546 }
547 
548 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
549     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
550     const TargetMachine &TM) const {
551   // Parse the section specifier and create it if valid.
552   StringRef Segment, Section;
553   unsigned TAA = 0, StubSize = 0;
554   bool TAAParsed;
555 
556   checkMachOComdat(GV);
557 
558   std::string ErrorCode =
559     MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
560                                           TAA, TAAParsed, StubSize);
561   if (!ErrorCode.empty()) {
562     // If invalid, report the error with report_fatal_error.
563     report_fatal_error("Global variable '" + GV->getName() +
564                        "' has an invalid section specifier '" +
565                        GV->getSection() + "': " + ErrorCode + ".");
566   }
567 
568   // Get the section.
569   MCSectionMachO *S =
570       getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
571 
572   // If TAA wasn't set by ParseSectionSpecifier() above,
573   // use the value returned by getMachOSection() as a default.
574   if (!TAAParsed)
575     TAA = S->getTypeAndAttributes();
576 
577   // Okay, now that we got the section, verify that the TAA & StubSize agree.
578   // If the user declared multiple globals with different section flags, we need
579   // to reject it here.
580   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
581     // If invalid, report the error with report_fatal_error.
582     report_fatal_error("Global variable '" + GV->getName() +
583                        "' section type or attributes does not match previous"
584                        " section specifier");
585   }
586 
587   return S;
588 }
589 
590 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
591     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
592     const TargetMachine &TM) const {
593   checkMachOComdat(GV);
594 
595   // Handle thread local data.
596   if (Kind.isThreadBSS()) return TLSBSSSection;
597   if (Kind.isThreadData()) return TLSDataSection;
598 
599   if (Kind.isText())
600     return GV->isWeakForLinker() ? TextCoalSection : TextSection;
601 
602   // If this is weak/linkonce, put this in a coalescable section, either in text
603   // or data depending on if it is writable.
604   if (GV->isWeakForLinker()) {
605     if (Kind.isReadOnly())
606       return ConstTextCoalSection;
607     return DataCoalSection;
608   }
609 
610   // FIXME: Alignment check should be handled by section classifier.
611   if (Kind.isMergeable1ByteCString() &&
612       GV->getParent()->getDataLayout().getPreferredAlignment(
613           cast<GlobalVariable>(GV)) < 32)
614     return CStringSection;
615 
616   // Do not put 16-bit arrays in the UString section if they have an
617   // externally visible label, this runs into issues with certain linker
618   // versions.
619   if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
620       GV->getParent()->getDataLayout().getPreferredAlignment(
621           cast<GlobalVariable>(GV)) < 32)
622     return UStringSection;
623 
624   // With MachO only variables whose corresponding symbol starts with 'l' or
625   // 'L' can be merged, so we only try merging GVs with private linkage.
626   if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
627     if (Kind.isMergeableConst4())
628       return FourByteConstantSection;
629     if (Kind.isMergeableConst8())
630       return EightByteConstantSection;
631     if (Kind.isMergeableConst16())
632       return SixteenByteConstantSection;
633   }
634 
635   // Otherwise, if it is readonly, but not something we can specially optimize,
636   // just drop it in .const.
637   if (Kind.isReadOnly())
638     return ReadOnlySection;
639 
640   // If this is marked const, put it into a const section.  But if the dynamic
641   // linker needs to write to it, put it in the data segment.
642   if (Kind.isReadOnlyWithRel())
643     return ConstDataSection;
644 
645   // Put zero initialized globals with strong external linkage in the
646   // DATA, __common section with the .zerofill directive.
647   if (Kind.isBSSExtern())
648     return DataCommonSection;
649 
650   // Put zero initialized globals with local linkage in __DATA,__bss directive
651   // with the .zerofill directive (aka .lcomm).
652   if (Kind.isBSSLocal())
653     return DataBSSSection;
654 
655   // Otherwise, just drop the variable in the normal data section.
656   return DataSection;
657 }
658 
659 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
660     const DataLayout &DL, SectionKind Kind, const Constant *C,
661     unsigned &Align) const {
662   // If this constant requires a relocation, we have to put it in the data
663   // segment, not in the text segment.
664   if (Kind.isData() || Kind.isReadOnlyWithRel())
665     return ConstDataSection;
666 
667   if (Kind.isMergeableConst4())
668     return FourByteConstantSection;
669   if (Kind.isMergeableConst8())
670     return EightByteConstantSection;
671   if (Kind.isMergeableConst16())
672     return SixteenByteConstantSection;
673   return ReadOnlySection;  // .const
674 }
675 
676 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
677     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
678     const TargetMachine &TM, MachineModuleInfo *MMI,
679     MCStreamer &Streamer) const {
680   // The mach-o version of this method defaults to returning a stub reference.
681 
682   if (Encoding & DW_EH_PE_indirect) {
683     MachineModuleInfoMachO &MachOMMI =
684       MMI->getObjFileInfo<MachineModuleInfoMachO>();
685 
686     MCSymbol *SSym =
687         getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
688 
689     // Add information about the stub reference to MachOMMI so that the stub
690     // gets emitted by the asmprinter.
691     MachineModuleInfoImpl::StubValueTy &StubSym =
692       GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
693                                   MachOMMI.getGVStubEntry(SSym);
694     if (!StubSym.getPointer()) {
695       MCSymbol *Sym = TM.getSymbol(GV, Mang);
696       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
697     }
698 
699     return TargetLoweringObjectFile::
700       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
701                         Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
702   }
703 
704   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
705                                                            TM, MMI, Streamer);
706 }
707 
708 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
709     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
710     MachineModuleInfo *MMI) const {
711   // The mach-o version of this method defaults to returning a stub reference.
712   MachineModuleInfoMachO &MachOMMI =
713     MMI->getObjFileInfo<MachineModuleInfoMachO>();
714 
715   MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
716 
717   // Add information about the stub reference to MachOMMI so that the stub
718   // gets emitted by the asmprinter.
719   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
720   if (!StubSym.getPointer()) {
721     MCSymbol *Sym = TM.getSymbol(GV, Mang);
722     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
723   }
724 
725   return SSym;
726 }
727 
728 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
729     const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
730     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
731   // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
732   // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
733   // through a non_lazy_ptr stub instead. One advantage is that it allows the
734   // computation of deltas to final external symbols. Example:
735   //
736   //    _extgotequiv:
737   //       .long   _extfoo
738   //
739   //    _delta:
740   //       .long   _extgotequiv-_delta
741   //
742   // is transformed to:
743   //
744   //    _delta:
745   //       .long   L_extfoo$non_lazy_ptr-(_delta+0)
746   //
747   //       .section        __IMPORT,__pointers,non_lazy_symbol_pointers
748   //    L_extfoo$non_lazy_ptr:
749   //       .indirect_symbol        _extfoo
750   //       .long   0
751   //
752   MachineModuleInfoMachO &MachOMMI =
753     MMI->getObjFileInfo<MachineModuleInfoMachO>();
754   MCContext &Ctx = getContext();
755 
756   // The offset must consider the original displacement from the base symbol
757   // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
758   Offset = -MV.getConstant();
759   const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
760 
761   // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
762   // non_lazy_ptr stubs.
763   SmallString<128> Name;
764   StringRef Suffix = "$non_lazy_ptr";
765   Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
766   Name += Sym->getName();
767   Name += Suffix;
768   MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
769 
770   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
771   if (!StubSym.getPointer())
772     StubSym = MachineModuleInfoImpl::
773       StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
774 
775   const MCExpr *BSymExpr =
776     MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
777   const MCExpr *LHS =
778     MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
779 
780   if (!Offset)
781     return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
782 
783   const MCExpr *RHS =
784     MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
785   return MCBinaryExpr::createSub(LHS, RHS, Ctx);
786 }
787 
788 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
789                                const MCSection &Section) {
790   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
791     return true;
792 
793   // If it is not dead stripped, it is safe to use private labels.
794   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
795   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
796     return true;
797 
798   return false;
799 }
800 
801 void TargetLoweringObjectFileMachO::getNameWithPrefix(
802     SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
803     const TargetMachine &TM) const {
804   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
805   const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM);
806   bool CannotUsePrivateLabel =
807       !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
808   Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
809 }
810 
811 //===----------------------------------------------------------------------===//
812 //                                  COFF
813 //===----------------------------------------------------------------------===//
814 
815 static unsigned
816 getCOFFSectionFlags(SectionKind K) {
817   unsigned Flags = 0;
818 
819   if (K.isMetadata())
820     Flags |=
821       COFF::IMAGE_SCN_MEM_DISCARDABLE;
822   else if (K.isText())
823     Flags |=
824       COFF::IMAGE_SCN_MEM_EXECUTE |
825       COFF::IMAGE_SCN_MEM_READ |
826       COFF::IMAGE_SCN_CNT_CODE;
827   else if (K.isBSS())
828     Flags |=
829       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
830       COFF::IMAGE_SCN_MEM_READ |
831       COFF::IMAGE_SCN_MEM_WRITE;
832   else if (K.isThreadLocal())
833     Flags |=
834       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835       COFF::IMAGE_SCN_MEM_READ |
836       COFF::IMAGE_SCN_MEM_WRITE;
837   else if (K.isReadOnly() || K.isReadOnlyWithRel())
838     Flags |=
839       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
840       COFF::IMAGE_SCN_MEM_READ;
841   else if (K.isWriteable())
842     Flags |=
843       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
844       COFF::IMAGE_SCN_MEM_READ |
845       COFF::IMAGE_SCN_MEM_WRITE;
846 
847   return Flags;
848 }
849 
850 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
851   const Comdat *C = GV->getComdat();
852   assert(C && "expected GV to have a Comdat!");
853 
854   StringRef ComdatGVName = C->getName();
855   const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
856   if (!ComdatGV)
857     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
858                        "' does not exist.");
859 
860   if (ComdatGV->getComdat() != C)
861     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
862                        "' is not a key for its COMDAT.");
863 
864   return ComdatGV;
865 }
866 
867 static int getSelectionForCOFF(const GlobalValue *GV) {
868   if (const Comdat *C = GV->getComdat()) {
869     const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
870     if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
871       ComdatKey = GA->getBaseObject();
872     if (ComdatKey == GV) {
873       switch (C->getSelectionKind()) {
874       case Comdat::Any:
875         return COFF::IMAGE_COMDAT_SELECT_ANY;
876       case Comdat::ExactMatch:
877         return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
878       case Comdat::Largest:
879         return COFF::IMAGE_COMDAT_SELECT_LARGEST;
880       case Comdat::NoDuplicates:
881         return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
882       case Comdat::SameSize:
883         return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
884       }
885     } else {
886       return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
887     }
888   }
889   return 0;
890 }
891 
892 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
893     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
894     const TargetMachine &TM) const {
895   int Selection = 0;
896   unsigned Characteristics = getCOFFSectionFlags(Kind);
897   StringRef Name = GV->getSection();
898   StringRef COMDATSymName = "";
899   if (GV->hasComdat()) {
900     Selection = getSelectionForCOFF(GV);
901     const GlobalValue *ComdatGV;
902     if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
903       ComdatGV = getComdatGVForCOFF(GV);
904     else
905       ComdatGV = GV;
906 
907     if (!ComdatGV->hasPrivateLinkage()) {
908       MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
909       COMDATSymName = Sym->getName();
910       Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
911     } else {
912       Selection = 0;
913     }
914   }
915   return getContext().getCOFFSection(Name,
916                                      Characteristics,
917                                      Kind,
918                                      COMDATSymName,
919                                      Selection);
920 }
921 
922 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
923   if (Kind.isText())
924     return ".text";
925   if (Kind.isBSS())
926     return ".bss";
927   if (Kind.isThreadLocal())
928     return ".tls$";
929   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
930     return ".rdata";
931   return ".data";
932 }
933 
934 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
935     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
936     const TargetMachine &TM) const {
937   // If we have -ffunction-sections then we should emit the global value to a
938   // uniqued section specifically for it.
939   bool EmitUniquedSection;
940   if (Kind.isText())
941     EmitUniquedSection = TM.getFunctionSections();
942   else
943     EmitUniquedSection = TM.getDataSections();
944 
945   if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
946     const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
947     unsigned Characteristics = getCOFFSectionFlags(Kind);
948 
949     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
950     int Selection = getSelectionForCOFF(GV);
951     if (!Selection)
952       Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
953     const GlobalValue *ComdatGV;
954     if (GV->hasComdat())
955       ComdatGV = getComdatGVForCOFF(GV);
956     else
957       ComdatGV = GV;
958 
959     if (!ComdatGV->hasPrivateLinkage()) {
960       MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
961       StringRef COMDATSymName = Sym->getName();
962       return getContext().getCOFFSection(Name, Characteristics, Kind,
963                                          COMDATSymName, Selection);
964     } else {
965       SmallString<256> TmpData;
966       Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true);
967       return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
968                                          Selection);
969     }
970   }
971 
972   if (Kind.isText())
973     return TextSection;
974 
975   if (Kind.isThreadLocal())
976     return TLSDataSection;
977 
978   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
979     return ReadOnlySection;
980 
981   // Note: we claim that common symbols are put in BSSSection, but they are
982   // really emitted with the magic .comm directive, which creates a symbol table
983   // entry but not a section.
984   if (Kind.isBSS() || Kind.isCommon())
985     return BSSSection;
986 
987   return DataSection;
988 }
989 
990 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
991     SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
992     const TargetMachine &TM) const {
993   bool CannotUsePrivateLabel = false;
994   if (GV->hasPrivateLinkage() &&
995       ((isa<Function>(GV) && TM.getFunctionSections()) ||
996        (isa<GlobalVariable>(GV) && TM.getDataSections())))
997     CannotUsePrivateLabel = true;
998 
999   Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1000 }
1001 
1002 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1003     const Function &F, Mangler &Mang, const TargetMachine &TM) const {
1004   // If the function can be removed, produce a unique section so that
1005   // the table doesn't prevent the removal.
1006   const Comdat *C = F.getComdat();
1007   bool EmitUniqueSection = TM.getFunctionSections() || C;
1008   if (!EmitUniqueSection)
1009     return ReadOnlySection;
1010 
1011   // FIXME: we should produce a symbol for F instead.
1012   if (F.hasPrivateLinkage())
1013     return ReadOnlySection;
1014 
1015   MCSymbol *Sym = TM.getSymbol(&F, Mang);
1016   StringRef COMDATSymName = Sym->getName();
1017 
1018   SectionKind Kind = SectionKind::getReadOnly();
1019   const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1020   unsigned Characteristics = getCOFFSectionFlags(Kind);
1021   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1022 
1023   return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1024                                      COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
1025 }
1026 
1027 void TargetLoweringObjectFileCOFF::
1028 emitModuleFlags(MCStreamer &Streamer,
1029                 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
1030                 Mangler &Mang, const TargetMachine &TM) const {
1031   MDNode *LinkerOptions = nullptr;
1032 
1033   // Look for the "Linker Options" flag, since it's the only one we support.
1034   for (ArrayRef<Module::ModuleFlagEntry>::iterator
1035        i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
1036     const Module::ModuleFlagEntry &MFE = *i;
1037     StringRef Key = MFE.Key->getString();
1038     Metadata *Val = MFE.Val;
1039     if (Key == "Linker Options") {
1040       LinkerOptions = cast<MDNode>(Val);
1041       break;
1042     }
1043   }
1044   if (!LinkerOptions)
1045     return;
1046 
1047   // Emit the linker options to the linker .drectve section.  According to the
1048   // spec, this section is a space-separated string containing flags for linker.
1049   MCSection *Sec = getDrectveSection();
1050   Streamer.SwitchSection(Sec);
1051   for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
1052     MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
1053     for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
1054       MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
1055       // Lead with a space for consistency with our dllexport implementation.
1056       std::string Directive(" ");
1057       Directive.append(MDOption->getString());
1058       Streamer.EmitBytes(Directive);
1059     }
1060   }
1061 }
1062 
1063 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
1064     unsigned Priority, const MCSymbol *KeySym) const {
1065   return getContext().getAssociativeCOFFSection(
1066       cast<MCSectionCOFF>(StaticCtorSection), KeySym);
1067 }
1068 
1069 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
1070     unsigned Priority, const MCSymbol *KeySym) const {
1071   return getContext().getAssociativeCOFFSection(
1072       cast<MCSectionCOFF>(StaticDtorSection), KeySym);
1073 }
1074 
1075 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1076     raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const {
1077   if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1078     return;
1079 
1080   const Triple &TT = getTargetTriple();
1081 
1082   if (TT.isKnownWindowsMSVCEnvironment())
1083     OS << " /EXPORT:";
1084   else
1085     OS << " -export:";
1086 
1087   if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1088     std::string Flag;
1089     raw_string_ostream FlagOS(Flag);
1090     Mang.getNameWithPrefix(FlagOS, GV, false);
1091     FlagOS.flush();
1092     if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
1093       OS << Flag.substr(1);
1094     else
1095       OS << Flag;
1096   } else {
1097     Mang.getNameWithPrefix(OS, GV, false);
1098   }
1099 
1100   if (!GV->getValueType()->isFunctionTy()) {
1101     if (TT.isKnownWindowsMSVCEnvironment())
1102       OS << ",DATA";
1103     else
1104       OS << ",data";
1105   }
1106 }
1107