LLVM  7.0.0svn
TargetLoweringObjectFileImpl.cpp
Go to the documentation of this file.
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 
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"
23 #include "llvm/BinaryFormat/ELF.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"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CodeGen.h"
55 #include "llvm/Support/Format.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) {
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 
95  Module &M) const {
96  auto &C = getContext();
97 
98  if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
99  auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
101 
102  Streamer.SwitchSection(S);
103 
104  for (const auto &Operand : LinkerOptions->operands()) {
105  if (cast<MDNode>(Operand)->getNumOperands() != 2)
106  report_fatal_error("invalid llvm.linker.options");
107  for (const auto &Option : cast<MDNode>(Operand)->operands()) {
108  Streamer.EmitBytes(cast<MDString>(Option)->getString());
109  Streamer.EmitIntValue(0, 1);
110  }
111  }
112  }
113 
114  unsigned Version = 0;
115  unsigned Flags = 0;
117 
118  GetObjCImageInfo(M, Version, Flags, Section);
119  if (Section.empty())
120  return;
121 
122  auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
123  Streamer.SwitchSection(S);
124  Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
125  Streamer.EmitIntValue(Version, 4);
126  Streamer.EmitIntValue(Flags, 4);
127  Streamer.AddBlankLine();
128 }
129 
131  const GlobalValue *GV, const TargetMachine &TM,
132  MachineModuleInfo *MMI) const {
133  unsigned Encoding = getPersonalityEncoding();
134  if ((Encoding & 0x80) == DW_EH_PE_indirect)
135  return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
136  TM.getSymbol(GV)->getName());
137  if ((Encoding & 0x70) == DW_EH_PE_absptr)
138  return TM.getSymbol(GV);
139  report_fatal_error("We do not support this DWARF encoding yet!");
140 }
141 
143  MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
144  SmallString<64> NameData("DW.ref.");
145  NameData += Sym->getName();
146  MCSymbolELF *Label =
147  cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
148  Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
149  Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
150  unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
151  MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
152  ELF::SHT_PROGBITS, Flags, 0);
153  unsigned Size = DL.getPointerSize();
154  Streamer.SwitchSection(Sec);
156  Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
157  const MCExpr *E = MCConstantExpr::create(Size, getContext());
158  Streamer.emitELFSize(Label, E);
159  Streamer.EmitLabel(Label);
160 
161  Streamer.EmitSymbolValue(Sym, Size);
162 }
163 
165  const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
166  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
167  if (Encoding & DW_EH_PE_indirect) {
169 
170  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
171 
172  // Add information about the stub reference to ELFMMI so that the stub
173  // gets emitted by the asmprinter.
174  MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
175  if (!StubSym.getPointer()) {
176  MCSymbol *Sym = TM.getSymbol(GV);
178  }
179 
181  getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
182  Encoding & ~DW_EH_PE_indirect, Streamer);
183  }
184 
186  MMI, Streamer);
187 }
188 
190  // N.B.: The defaults used in here are not the same ones used in MC.
191  // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
192  // both gas and MC will produce a section with no flags. Given
193  // section(".eh_frame") gcc will produce:
194  //
195  // .section .eh_frame,"a",@progbits
196 
197  if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
198  /*AddSegmentInfo=*/false))
199  return SectionKind::getMetadata();
200 
201  if (Name.empty() || Name[0] != '.') return K;
202 
203  // Default implementation based on some magic section names.
204  if (Name == ".bss" ||
205  Name.startswith(".bss.") ||
206  Name.startswith(".gnu.linkonce.b.") ||
207  Name.startswith(".llvm.linkonce.b.") ||
208  Name == ".sbss" ||
209  Name.startswith(".sbss.") ||
210  Name.startswith(".gnu.linkonce.sb.") ||
211  Name.startswith(".llvm.linkonce.sb."))
212  return SectionKind::getBSS();
213 
214  if (Name == ".tdata" ||
215  Name.startswith(".tdata.") ||
216  Name.startswith(".gnu.linkonce.td.") ||
217  Name.startswith(".llvm.linkonce.td."))
219 
220  if (Name == ".tbss" ||
221  Name.startswith(".tbss.") ||
222  Name.startswith(".gnu.linkonce.tb.") ||
223  Name.startswith(".llvm.linkonce.tb."))
224  return SectionKind::getThreadBSS();
225 
226  return K;
227 }
228 
230  // Use SHT_NOTE for section whose name starts with ".note" to allow
231  // emitting ELF notes from C variable declaration.
232  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
233  if (Name.startswith(".note"))
234  return ELF::SHT_NOTE;
235 
236  if (Name == ".init_array")
237  return ELF::SHT_INIT_ARRAY;
238 
239  if (Name == ".fini_array")
240  return ELF::SHT_FINI_ARRAY;
241 
242  if (Name == ".preinit_array")
243  return ELF::SHT_PREINIT_ARRAY;
244 
245  if (K.isBSS() || K.isThreadBSS())
246  return ELF::SHT_NOBITS;
247 
248  return ELF::SHT_PROGBITS;
249 }
250 
251 static unsigned getELFSectionFlags(SectionKind K) {
252  unsigned Flags = 0;
253 
254  if (!K.isMetadata())
255  Flags |= ELF::SHF_ALLOC;
256 
257  if (K.isText())
258  Flags |= ELF::SHF_EXECINSTR;
259 
260  if (K.isExecuteOnly())
261  Flags |= ELF::SHF_ARM_PURECODE;
262 
263  if (K.isWriteable())
264  Flags |= ELF::SHF_WRITE;
265 
266  if (K.isThreadLocal())
267  Flags |= ELF::SHF_TLS;
268 
269  if (K.isMergeableCString() || K.isMergeableConst())
270  Flags |= ELF::SHF_MERGE;
271 
272  if (K.isMergeableCString())
273  Flags |= ELF::SHF_STRINGS;
274 
275  return Flags;
276 }
277 
278 static const Comdat *getELFComdat(const GlobalValue *GV) {
279  const Comdat *C = GV->getComdat();
280  if (!C)
281  return nullptr;
282 
283  if (C->getSelectionKind() != Comdat::Any)
284  report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
285  C->getName() + "' cannot be lowered.");
286 
287  return C;
288 }
289 
291  const TargetMachine &TM) {
293  if (!MD)
294  return nullptr;
295 
296  const MDOperand &Op = MD->getOperand(0);
297  if (!Op.get())
298  return nullptr;
299 
300  auto *VM = dyn_cast<ValueAsMetadata>(Op);
301  if (!VM)
302  report_fatal_error("MD_associated operand is not ValueAsMetadata");
303 
304  GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
305  return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
306 }
307 
309  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
311 
312  // Check if '#pragma clang section' name is applicable.
313  // Note that pragma directive overrides -ffunction-section, -fdata-section
314  // and so section name is exactly as user specified and not uniqued.
315  const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
316  if (GV && GV->hasImplicitSection()) {
317  auto Attrs = GV->getAttributes();
318  if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
319  SectionName = Attrs.getAttribute("bss-section").getValueAsString();
320  } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
321  SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
322  } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
323  SectionName = Attrs.getAttribute("data-section").getValueAsString();
324  }
325  }
326  const Function *F = dyn_cast<Function>(GO);
327  if (F && F->hasFnAttribute("implicit-section-name")) {
328  SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
329  }
330 
331  // Infer section flags from the section name if we can.
332  Kind = getELFKindForNamedSection(SectionName, Kind);
333 
334  StringRef Group = "";
335  unsigned Flags = getELFSectionFlags(Kind);
336  if (const Comdat *C = getELFComdat(GO)) {
337  Group = C->getName();
338  Flags |= ELF::SHF_GROUP;
339  }
340 
341  // A section can have at most one associated section. Put each global with
342  // MD_associated in a unique section.
343  unsigned UniqueID = MCContext::GenericSectionID;
344  const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
345  if (AssociatedSymbol) {
346  UniqueID = NextUniqueID++;
347  Flags |= ELF::SHF_LINK_ORDER;
348  }
349 
350  MCSectionELF *Section = getContext().getELFSection(
351  SectionName, getELFSectionType(SectionName, Kind), Flags,
352  /*EntrySize=*/0, Group, UniqueID, AssociatedSymbol);
353  // Make sure that we did not get some other section with incompatible sh_link.
354  // This should not be possible due to UniqueID code above.
355  assert(Section->getAssociatedSymbol() == AssociatedSymbol);
356  return Section;
357 }
358 
359 /// Return the section prefix name used by options FunctionsSections and
360 /// DataSections.
362  if (Kind.isText())
363  return ".text";
364  if (Kind.isReadOnly())
365  return ".rodata";
366  if (Kind.isBSS())
367  return ".bss";
368  if (Kind.isThreadData())
369  return ".tdata";
370  if (Kind.isThreadBSS())
371  return ".tbss";
372  if (Kind.isData())
373  return ".data";
374  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
375  return ".data.rel.ro";
376 }
377 
379  MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
380  const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
381  unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
382  unsigned EntrySize = 0;
383  if (Kind.isMergeableCString()) {
384  if (Kind.isMergeable2ByteCString()) {
385  EntrySize = 2;
386  } else if (Kind.isMergeable4ByteCString()) {
387  EntrySize = 4;
388  } else {
389  EntrySize = 1;
390  assert(Kind.isMergeable1ByteCString() && "unknown string width");
391  }
392  } else if (Kind.isMergeableConst()) {
393  if (Kind.isMergeableConst4()) {
394  EntrySize = 4;
395  } else if (Kind.isMergeableConst8()) {
396  EntrySize = 8;
397  } else if (Kind.isMergeableConst16()) {
398  EntrySize = 16;
399  } else {
400  assert(Kind.isMergeableConst32() && "unknown data width");
401  EntrySize = 32;
402  }
403  }
404 
405  StringRef Group = "";
406  if (const Comdat *C = getELFComdat(GO)) {
407  Flags |= ELF::SHF_GROUP;
408  Group = C->getName();
409  }
410 
411  bool UniqueSectionNames = TM.getUniqueSectionNames();
413  if (Kind.isMergeableCString()) {
414  // We also need alignment here.
415  // FIXME: this is getting the alignment of the character, not the
416  // alignment of the global!
418  cast<GlobalVariable>(GO));
419 
420  std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
421  Name = SizeSpec + utostr(Align);
422  } else if (Kind.isMergeableConst()) {
423  Name = ".rodata.cst";
424  Name += utostr(EntrySize);
425  } else {
426  Name = getSectionPrefixForGlobal(Kind);
427  }
428 
429  if (const auto *F = dyn_cast<Function>(GO)) {
430  const auto &OptionalPrefix = F->getSectionPrefix();
431  if (OptionalPrefix)
432  Name += *OptionalPrefix;
433  }
434 
435  if (EmitUniqueSection && UniqueSectionNames) {
436  Name.push_back('.');
437  TM.getNameWithPrefix(Name, GO, Mang, true);
438  }
439  unsigned UniqueID = MCContext::GenericSectionID;
440  if (EmitUniqueSection && !UniqueSectionNames) {
441  UniqueID = *NextUniqueID;
442  (*NextUniqueID)++;
443  }
444  // Use 0 as the unique ID for execute-only text
445  if (Kind.isExecuteOnly())
446  UniqueID = 0;
447  return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
448  EntrySize, Group, UniqueID, AssociatedSymbol);
449 }
450 
452  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
453  unsigned Flags = getELFSectionFlags(Kind);
454 
455  // If we have -ffunction-section or -fdata-section then we should emit the
456  // global value to a uniqued section specifically for it.
457  bool EmitUniqueSection = false;
458  if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
459  if (Kind.isText())
460  EmitUniqueSection = TM.getFunctionSections();
461  else
462  EmitUniqueSection = TM.getDataSections();
463  }
464  EmitUniqueSection |= GO->hasComdat();
465 
466  const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
467  if (AssociatedSymbol) {
468  EmitUniqueSection = true;
469  Flags |= ELF::SHF_LINK_ORDER;
470  }
471 
473  getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
474  &NextUniqueID, AssociatedSymbol);
475  assert(Section->getAssociatedSymbol() == AssociatedSymbol);
476  return Section;
477 }
478 
480  const Function &F, const TargetMachine &TM) const {
481  // If the function can be removed, produce a unique section so that
482  // the table doesn't prevent the removal.
483  const Comdat *C = F.getComdat();
484  bool EmitUniqueSection = TM.getFunctionSections() || C;
485  if (!EmitUniqueSection)
486  return ReadOnlySection;
487 
488  return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
489  getMangler(), TM, EmitUniqueSection,
490  ELF::SHF_ALLOC, &NextUniqueID,
491  /* AssociatedSymbol */ nullptr);
492 }
493 
495  bool UsesLabelDifference, const Function &F) const {
496  // We can always create relative relocations, so use another section
497  // that can be marked non-executable.
498  return false;
499 }
500 
501 /// Given a mergeable constant with the specified size and relocation
502 /// information, return a section that it should be placed in.
504  const DataLayout &DL, SectionKind Kind, const Constant *C,
505  unsigned &Align) const {
506  if (Kind.isMergeableConst4() && MergeableConst4Section)
507  return MergeableConst4Section;
508  if (Kind.isMergeableConst8() && MergeableConst8Section)
509  return MergeableConst8Section;
510  if (Kind.isMergeableConst16() && MergeableConst16Section)
511  return MergeableConst16Section;
512  if (Kind.isMergeableConst32() && MergeableConst32Section)
513  return MergeableConst32Section;
514  if (Kind.isReadOnly())
515  return ReadOnlySection;
516 
517  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
518  return DataRelROSection;
519 }
520 
521 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
522  bool IsCtor, unsigned Priority,
523  const MCSymbol *KeySym) {
524  std::string Name;
525  unsigned Type;
526  unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
527  StringRef COMDAT = KeySym ? KeySym->getName() : "";
528 
529  if (KeySym)
530  Flags |= ELF::SHF_GROUP;
531 
532  if (UseInitArray) {
533  if (IsCtor) {
534  Type = ELF::SHT_INIT_ARRAY;
535  Name = ".init_array";
536  } else {
537  Type = ELF::SHT_FINI_ARRAY;
538  Name = ".fini_array";
539  }
540  if (Priority != 65535) {
541  Name += '.';
542  Name += utostr(Priority);
543  }
544  } else {
545  // The default scheme is .ctor / .dtor, so we have to invert the priority
546  // numbering.
547  if (IsCtor)
548  Name = ".ctors";
549  else
550  Name = ".dtors";
551  if (Priority != 65535)
552  raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
553  Type = ELF::SHT_PROGBITS;
554  }
555 
556  return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
557 }
558 
560  unsigned Priority, const MCSymbol *KeySym) const {
561  return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
562  KeySym);
563 }
564 
566  unsigned Priority, const MCSymbol *KeySym) const {
567  return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
568  KeySym);
569 }
570 
572  const GlobalValue *LHS, const GlobalValue *RHS,
573  const TargetMachine &TM) const {
574  // We may only use a PLT-relative relocation to refer to unnamed_addr
575  // functions.
576  if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
577  return nullptr;
578 
579  // Basic sanity checks.
580  if (LHS->getType()->getPointerAddressSpace() != 0 ||
581  RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
582  RHS->isThreadLocal())
583  return nullptr;
584 
586  MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
587  getContext()),
588  MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
589 }
590 
591 void
593  UseInitArray = UseInitArray_;
594  MCContext &Ctx = getContext();
595  if (!UseInitArray) {
596  StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
598 
599  StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
601  return;
602  }
603 
604  StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
606  StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
608 }
609 
610 //===----------------------------------------------------------------------===//
611 // MachO
612 //===----------------------------------------------------------------------===//
613 
617 }
618 
620  const TargetMachine &TM) {
622  if (TM.getRelocationModel() == Reloc::Static) {
623  StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
625  StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
627  } else {
628  StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
631  StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
634  }
635 }
636 
638  Module &M) const {
639  // Emit the linker options if present.
640  if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
641  for (const auto &Option : LinkerOptions->operands()) {
642  SmallVector<std::string, 4> StrOptions;
643  for (const auto &Piece : cast<MDNode>(Option)->operands())
644  StrOptions.push_back(cast<MDString>(Piece)->getString());
645  Streamer.EmitLinkerOptions(StrOptions);
646  }
647  }
648 
649  unsigned VersionVal = 0;
650  unsigned ImageInfoFlags = 0;
651  StringRef SectionVal;
652 
653  GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
654 
655  // The section is mandatory. If we don't have it, then we don't have GC info.
656  if (SectionVal.empty())
657  return;
658 
659  StringRef Segment, Section;
660  unsigned TAA = 0, StubSize = 0;
661  bool TAAParsed;
662  std::string ErrorCode =
663  MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
664  TAA, TAAParsed, StubSize);
665  if (!ErrorCode.empty())
666  // If invalid, report the error with report_fatal_error.
667  report_fatal_error("Invalid section specifier '" + Section + "': " +
668  ErrorCode + ".");
669 
670  // Get the section.
672  Segment, Section, TAA, StubSize, SectionKind::getData());
673  Streamer.SwitchSection(S);
674  Streamer.EmitLabel(getContext().
675  getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
676  Streamer.EmitIntValue(VersionVal, 4);
677  Streamer.EmitIntValue(ImageInfoFlags, 4);
678  Streamer.AddBlankLine();
679 }
680 
681 static void checkMachOComdat(const GlobalValue *GV) {
682  const Comdat *C = GV->getComdat();
683  if (!C)
684  return;
685 
686  report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
687  "' cannot be lowered.");
688 }
689 
691  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
692  // Parse the section specifier and create it if valid.
693  StringRef Segment, Section;
694  unsigned TAA = 0, StubSize = 0;
695  bool TAAParsed;
696 
697  checkMachOComdat(GO);
698 
699  std::string ErrorCode =
701  TAA, TAAParsed, StubSize);
702  if (!ErrorCode.empty()) {
703  // If invalid, report the error with report_fatal_error.
704  report_fatal_error("Global variable '" + GO->getName() +
705  "' has an invalid section specifier '" +
706  GO->getSection() + "': " + ErrorCode + ".");
707  }
708 
709  // Get the section.
710  MCSectionMachO *S =
711  getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
712 
713  // If TAA wasn't set by ParseSectionSpecifier() above,
714  // use the value returned by getMachOSection() as a default.
715  if (!TAAParsed)
716  TAA = S->getTypeAndAttributes();
717 
718  // Okay, now that we got the section, verify that the TAA & StubSize agree.
719  // If the user declared multiple globals with different section flags, we need
720  // to reject it here.
721  if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
722  // If invalid, report the error with report_fatal_error.
723  report_fatal_error("Global variable '" + GO->getName() +
724  "' section type or attributes does not match previous"
725  " section specifier");
726  }
727 
728  return S;
729 }
730 
732  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
733  checkMachOComdat(GO);
734 
735  // Handle thread local data.
736  if (Kind.isThreadBSS()) return TLSBSSSection;
737  if (Kind.isThreadData()) return TLSDataSection;
738 
739  if (Kind.isText())
741 
742  // If this is weak/linkonce, put this in a coalescable section, either in text
743  // or data depending on if it is writable.
744  if (GO->isWeakForLinker()) {
745  if (Kind.isReadOnly())
746  return ConstTextCoalSection;
747  if (Kind.isReadOnlyWithRel())
748  return ConstDataCoalSection;
749  return DataCoalSection;
750  }
751 
752  // FIXME: Alignment check should be handled by section classifier.
753  if (Kind.isMergeable1ByteCString() &&
755  cast<GlobalVariable>(GO)) < 32)
756  return CStringSection;
757 
758  // Do not put 16-bit arrays in the UString section if they have an
759  // externally visible label, this runs into issues with certain linker
760  // versions.
761  if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
763  cast<GlobalVariable>(GO)) < 32)
764  return UStringSection;
765 
766  // With MachO only variables whose corresponding symbol starts with 'l' or
767  // 'L' can be merged, so we only try merging GVs with private linkage.
768  if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
769  if (Kind.isMergeableConst4())
771  if (Kind.isMergeableConst8())
773  if (Kind.isMergeableConst16())
775  }
776 
777  // Otherwise, if it is readonly, but not something we can specially optimize,
778  // just drop it in .const.
779  if (Kind.isReadOnly())
780  return ReadOnlySection;
781 
782  // If this is marked const, put it into a const section. But if the dynamic
783  // linker needs to write to it, put it in the data segment.
784  if (Kind.isReadOnlyWithRel())
785  return ConstDataSection;
786 
787  // Put zero initialized globals with strong external linkage in the
788  // DATA, __common section with the .zerofill directive.
789  if (Kind.isBSSExtern())
790  return DataCommonSection;
791 
792  // Put zero initialized globals with local linkage in __DATA,__bss directive
793  // with the .zerofill directive (aka .lcomm).
794  if (Kind.isBSSLocal())
795  return DataBSSSection;
796 
797  // Otherwise, just drop the variable in the normal data section.
798  return DataSection;
799 }
800 
802  const DataLayout &DL, SectionKind Kind, const Constant *C,
803  unsigned &Align) const {
804  // If this constant requires a relocation, we have to put it in the data
805  // segment, not in the text segment.
806  if (Kind.isData() || Kind.isReadOnlyWithRel())
807  return ConstDataSection;
808 
809  if (Kind.isMergeableConst4())
811  if (Kind.isMergeableConst8())
813  if (Kind.isMergeableConst16())
815  return ReadOnlySection; // .const
816 }
817 
819  const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
820  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
821  // The mach-o version of this method defaults to returning a stub reference.
822 
823  if (Encoding & DW_EH_PE_indirect) {
824  MachineModuleInfoMachO &MachOMMI =
826 
827  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
828 
829  // Add information about the stub reference to MachOMMI so that the stub
830  // gets emitted by the asmprinter.
831  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
832  if (!StubSym.getPointer()) {
833  MCSymbol *Sym = TM.getSymbol(GV);
835  }
836 
839  Encoding & ~DW_EH_PE_indirect, Streamer);
840  }
841 
843  MMI, Streamer);
844 }
845 
847  const GlobalValue *GV, const TargetMachine &TM,
848  MachineModuleInfo *MMI) const {
849  // The mach-o version of this method defaults to returning a stub reference.
850  MachineModuleInfoMachO &MachOMMI =
852 
853  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
854 
855  // Add information about the stub reference to MachOMMI so that the stub
856  // gets emitted by the asmprinter.
857  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
858  if (!StubSym.getPointer()) {
859  MCSymbol *Sym = TM.getSymbol(GV);
861  }
862 
863  return SSym;
864 }
865 
867  const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
868  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
869  // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
870  // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
871  // through a non_lazy_ptr stub instead. One advantage is that it allows the
872  // computation of deltas to final external symbols. Example:
873  //
874  // _extgotequiv:
875  // .long _extfoo
876  //
877  // _delta:
878  // .long _extgotequiv-_delta
879  //
880  // is transformed to:
881  //
882  // _delta:
883  // .long L_extfoo$non_lazy_ptr-(_delta+0)
884  //
885  // .section __IMPORT,__pointers,non_lazy_symbol_pointers
886  // L_extfoo$non_lazy_ptr:
887  // .indirect_symbol _extfoo
888  // .long 0
889  //
890  MachineModuleInfoMachO &MachOMMI =
892  MCContext &Ctx = getContext();
893 
894  // The offset must consider the original displacement from the base symbol
895  // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
896  Offset = -MV.getConstant();
897  const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
898 
899  // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
900  // non_lazy_ptr stubs.
902  StringRef Suffix = "$non_lazy_ptr";
903  Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
904  Name += Sym->getName();
905  Name += Suffix;
906  MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
907 
908  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
909  if (!StubSym.getPointer())
910  StubSym = MachineModuleInfoImpl::
911  StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
912 
913  const MCExpr *BSymExpr =
915  const MCExpr *LHS =
917 
918  if (!Offset)
919  return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
920 
921  const MCExpr *RHS =
922  MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
923  return MCBinaryExpr::createSub(LHS, RHS, Ctx);
924 }
925 
926 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
927  const MCSection &Section) {
928  if (!AsmInfo.isSectionAtomizableBySymbols(Section))
929  return true;
930 
931  // If it is not dead stripped, it is safe to use private labels.
932  const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
934  return true;
935 
936  return false;
937 }
938 
940  SmallVectorImpl<char> &OutName, const GlobalValue *GV,
941  const TargetMachine &TM) const {
942  bool CannotUsePrivateLabel = true;
943  if (auto *GO = GV->getBaseObject()) {
945  const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
946  CannotUsePrivateLabel =
947  !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
948  }
949  getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
950 }
951 
952 //===----------------------------------------------------------------------===//
953 // COFF
954 //===----------------------------------------------------------------------===//
955 
956 static unsigned
958  unsigned Flags = 0;
959  bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
960 
961  if (K.isMetadata())
962  Flags |=
964  else if (K.isText())
965  Flags |=
970  else if (K.isBSS())
971  Flags |=
975  else if (K.isThreadLocal())
976  Flags |=
980  else if (K.isReadOnly() || K.isReadOnlyWithRel())
981  Flags |=
984  else if (K.isWriteable())
985  Flags |=
989 
990  return Flags;
991 }
992 
993 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
994  const Comdat *C = GV->getComdat();
995  assert(C && "expected GV to have a Comdat!");
996 
997  StringRef ComdatGVName = C->getName();
998  const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
999  if (!ComdatGV)
1000  report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1001  "' does not exist.");
1002 
1003  if (ComdatGV->getComdat() != C)
1004  report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1005  "' is not a key for its COMDAT.");
1006 
1007  return ComdatGV;
1008 }
1009 
1010 static int getSelectionForCOFF(const GlobalValue *GV) {
1011  if (const Comdat *C = GV->getComdat()) {
1012  const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1013  if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1014  ComdatKey = GA->getBaseObject();
1015  if (ComdatKey == GV) {
1016  switch (C->getSelectionKind()) {
1017  case Comdat::Any:
1019  case Comdat::ExactMatch:
1021  case Comdat::Largest:
1023  case Comdat::NoDuplicates:
1025  case Comdat::SameSize:
1027  }
1028  } else {
1030  }
1031  }
1032  return 0;
1033 }
1034 
1036  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1037  int Selection = 0;
1038  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1039  StringRef Name = GO->getSection();
1040  StringRef COMDATSymName = "";
1041  if (GO->hasComdat()) {
1042  Selection = getSelectionForCOFF(GO);
1043  const GlobalValue *ComdatGV;
1044  if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1045  ComdatGV = getComdatGVForCOFF(GO);
1046  else
1047  ComdatGV = GO;
1048 
1049  if (!ComdatGV->hasPrivateLinkage()) {
1050  MCSymbol *Sym = TM.getSymbol(ComdatGV);
1051  COMDATSymName = Sym->getName();
1052  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1053  } else {
1054  Selection = 0;
1055  }
1056  }
1057 
1058  return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1059  Selection);
1060 }
1061 
1063  if (Kind.isText())
1064  return ".text";
1065  if (Kind.isBSS())
1066  return ".bss";
1067  if (Kind.isThreadLocal())
1068  return ".tls$";
1069  if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1070  return ".rdata";
1071  return ".data";
1072 }
1073 
1075  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1076  // If we have -ffunction-sections then we should emit the global value to a
1077  // uniqued section specifically for it.
1078  bool EmitUniquedSection;
1079  if (Kind.isText())
1080  EmitUniquedSection = TM.getFunctionSections();
1081  else
1082  EmitUniquedSection = TM.getDataSections();
1083 
1084  if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
1085  const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1086  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1087 
1088  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1089  int Selection = getSelectionForCOFF(GO);
1090  if (!Selection)
1092  const GlobalValue *ComdatGV;
1093  if (GO->hasComdat())
1094  ComdatGV = getComdatGVForCOFF(GO);
1095  else
1096  ComdatGV = GO;
1097 
1098  unsigned UniqueID = MCContext::GenericSectionID;
1099  if (EmitUniquedSection)
1100  UniqueID = NextUniqueID++;
1101 
1102  if (!ComdatGV->hasPrivateLinkage()) {
1103  MCSymbol *Sym = TM.getSymbol(ComdatGV);
1104  StringRef COMDATSymName = Sym->getName();
1105  return getContext().getCOFFSection(Name, Characteristics, Kind,
1106  COMDATSymName, Selection, UniqueID);
1107  } else {
1108  SmallString<256> TmpData;
1109  getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
1110  return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
1111  Selection, UniqueID);
1112  }
1113  }
1114 
1115  if (Kind.isText())
1116  return TextSection;
1117 
1118  if (Kind.isThreadLocal())
1119  return TLSDataSection;
1120 
1121  if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1122  return ReadOnlySection;
1123 
1124  // Note: we claim that common symbols are put in BSSSection, but they are
1125  // really emitted with the magic .comm directive, which creates a symbol table
1126  // entry but not a section.
1127  if (Kind.isBSS() || Kind.isCommon())
1128  return BSSSection;
1129 
1130  return DataSection;
1131 }
1132 
1134  SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1135  const TargetMachine &TM) const {
1136  bool CannotUsePrivateLabel = false;
1137  if (GV->hasPrivateLinkage() &&
1138  ((isa<Function>(GV) && TM.getFunctionSections()) ||
1139  (isa<GlobalVariable>(GV) && TM.getDataSections())))
1140  CannotUsePrivateLabel = true;
1141 
1142  getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1143 }
1144 
1146  const Function &F, const TargetMachine &TM) const {
1147  // If the function can be removed, produce a unique section so that
1148  // the table doesn't prevent the removal.
1149  const Comdat *C = F.getComdat();
1150  bool EmitUniqueSection = TM.getFunctionSections() || C;
1151  if (!EmitUniqueSection)
1152  return ReadOnlySection;
1153 
1154  // FIXME: we should produce a symbol for F instead.
1155  if (F.hasPrivateLinkage())
1156  return ReadOnlySection;
1157 
1158  MCSymbol *Sym = TM.getSymbol(&F);
1159  StringRef COMDATSymName = Sym->getName();
1160 
1162  const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1163  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1164  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1165  unsigned UniqueID = NextUniqueID++;
1166 
1167  return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1169 }
1170 
1172  Module &M) const {
1173  if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1174  // Emit the linker options to the linker .drectve section. According to the
1175  // spec, this section is a space-separated string containing flags for
1176  // linker.
1177  MCSection *Sec = getDrectveSection();
1178  Streamer.SwitchSection(Sec);
1179  for (const auto &Option : LinkerOptions->operands()) {
1180  for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1181  // Lead with a space for consistency with our dllexport implementation.
1182  std::string Directive(" ");
1183  Directive.append(cast<MDString>(Piece)->getString());
1184  Streamer.EmitBytes(Directive);
1185  }
1186  }
1187  }
1188 
1189  unsigned Version = 0;
1190  unsigned Flags = 0;
1192 
1193  GetObjCImageInfo(M, Version, Flags, Section);
1194  if (Section.empty())
1195  return;
1196 
1197  auto &C = getContext();
1198  auto *S = C.getCOFFSection(
1201  Streamer.SwitchSection(S);
1202  Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1203  Streamer.EmitIntValue(Version, 4);
1204  Streamer.EmitIntValue(Flags, 4);
1205  Streamer.AddBlankLine();
1206 }
1207 
1209  const TargetMachine &TM) {
1211  const Triple &T = TM.getTargetTriple();
1221  } else {
1230  }
1231 }
1232 
1234  const Triple &T, bool IsCtor,
1235  unsigned Priority,
1236  const MCSymbol *KeySym,
1239  return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1240 
1241  std::string Name = IsCtor ? ".ctors" : ".dtors";
1242  if (Priority != 65535)
1243  raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1244 
1245  return Ctx.getAssociativeCOFFSection(
1250  KeySym, 0);
1251 }
1252 
1254  unsigned Priority, const MCSymbol *KeySym) const {
1256  Priority, KeySym,
1257  cast<MCSectionCOFF>(StaticCtorSection));
1258 }
1259 
1261  unsigned Priority, const MCSymbol *KeySym) const {
1263  Priority, KeySym,
1264  cast<MCSectionCOFF>(StaticDtorSection));
1265 }
1266 
1268  raw_ostream &OS, const GlobalValue *GV) const {
1270 }
1271 
1273  raw_ostream &OS, const GlobalValue *GV) const {
1275 }
1276 
1278  const GlobalValue *LHS, const GlobalValue *RHS,
1279  const TargetMachine &TM) const {
1280  const Triple &T = TM.getTargetTriple();
1281  if (!T.isKnownWindowsMSVCEnvironment() &&
1284  return nullptr;
1285 
1286  // Our symbols should exist in address space zero, cowardly no-op if
1287  // otherwise.
1288  if (LHS->getType()->getPointerAddressSpace() != 0 ||
1289  RHS->getType()->getPointerAddressSpace() != 0)
1290  return nullptr;
1291 
1292  // Both ptrtoint instructions must wrap global objects:
1293  // - Only global variables are eligible for image relative relocations.
1294  // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1295  // We expect __ImageBase to be a global variable without a section, externally
1296  // defined.
1297  //
1298  // It should look something like this: @__ImageBase = external constant i8
1299  if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
1300  LHS->isThreadLocal() || RHS->isThreadLocal() ||
1301  RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
1302  cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
1303  return nullptr;
1304 
1305  return MCSymbolRefExpr::create(TM.getSymbol(LHS),
1307  getContext());
1308 }
1309 
1310 static std::string APIntToHexString(const APInt &AI) {
1311  unsigned Width = (AI.getBitWidth() / 8) * 2;
1312  std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
1313  unsigned Size = HexString.size();
1314  assert(Width >= Size && "hex string is too large!");
1315  HexString.insert(HexString.begin(), Width - Size, '0');
1316 
1317  return HexString;
1318 }
1319 
1320 static std::string scalarConstantToHexString(const Constant *C) {
1321  Type *Ty = C->getType();
1322  if (isa<UndefValue>(C)) {
1324  } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
1325  return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
1326  } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
1327  return APIntToHexString(CI->getValue());
1328  } else {
1329  unsigned NumElements;
1330  if (isa<VectorType>(Ty))
1331  NumElements = Ty->getVectorNumElements();
1332  else
1333  NumElements = Ty->getArrayNumElements();
1334  std::string HexString;
1335  for (int I = NumElements - 1, E = -1; I != E; --I)
1337  return HexString;
1338  }
1339 }
1340 
1342  const DataLayout &DL, SectionKind Kind, const Constant *C,
1343  unsigned &Align) const {
1344  if (Kind.isMergeableConst() && C) {
1348  std::string COMDATSymName;
1349  if (Kind.isMergeableConst4()) {
1350  if (Align <= 4) {
1351  COMDATSymName = "__real@" + scalarConstantToHexString(C);
1352  Align = 4;
1353  }
1354  } else if (Kind.isMergeableConst8()) {
1355  if (Align <= 8) {
1356  COMDATSymName = "__real@" + scalarConstantToHexString(C);
1357  Align = 8;
1358  }
1359  } else if (Kind.isMergeableConst16()) {
1360  // FIXME: These may not be appropriate for non-x86 architectures.
1361  if (Align <= 16) {
1362  COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
1363  Align = 16;
1364  }
1365  } else if (Kind.isMergeableConst32()) {
1366  if (Align <= 32) {
1367  COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
1368  Align = 32;
1369  }
1370  }
1371 
1372  if (!COMDATSymName.empty())
1373  return getContext().getCOFFSection(".rdata", Characteristics, Kind,
1374  COMDATSymName,
1376  }
1377 
1378  return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
1379 }
1380 
1381 
1382 //===----------------------------------------------------------------------===//
1383 // Wasm
1384 //===----------------------------------------------------------------------===//
1385 
1386 static const Comdat *getWasmComdat(const GlobalValue *GV) {
1387  const Comdat *C = GV->getComdat();
1388  if (!C)
1389  return nullptr;
1390 
1391  if (C->getSelectionKind() != Comdat::Any)
1392  report_fatal_error("WebAssembly COMDATs only support "
1393  "SelectionKind::Any, '" + C->getName() + "' cannot be "
1394  "lowered.");
1395 
1396  return C;
1397 }
1398 
1400  // If we're told we have function data, then use that.
1401  if (K.isText())
1402  return SectionKind::getText();
1403 
1404  // Otherwise, ignore whatever section type the generic impl detected and use
1405  // a plain data section.
1406  return SectionKind::getData();
1407 }
1408 
1410  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1411  // We don't support explict section names for functions in the wasm object
1412  // format. Each function has to be in its own unique section.
1413  if (isa<Function>(GO)) {
1414  return SelectSectionForGlobal(GO, Kind, TM);
1415  }
1416 
1417  StringRef Name = GO->getSection();
1418 
1419  Kind = getWasmKindForNamedSection(Name, Kind);
1420 
1421  StringRef Group = "";
1422  if (const Comdat *C = getWasmComdat(GO)) {
1423  Group = C->getName();
1424  }
1425 
1426  return getContext().getWasmSection(Name, Kind, Group,
1428 }
1429 
1431  MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
1432  const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
1433  StringRef Group = "";
1434  if (const Comdat *C = getWasmComdat(GO)) {
1435  Group = C->getName();
1436  }
1437 
1438  bool UniqueSectionNames = TM.getUniqueSectionNames();
1440 
1441  if (const auto *F = dyn_cast<Function>(GO)) {
1442  const auto &OptionalPrefix = F->getSectionPrefix();
1443  if (OptionalPrefix)
1444  Name += *OptionalPrefix;
1445  }
1446 
1447  if (EmitUniqueSection && UniqueSectionNames) {
1448  Name.push_back('.');
1449  TM.getNameWithPrefix(Name, GO, Mang, true);
1450  }
1451  unsigned UniqueID = MCContext::GenericSectionID;
1452  if (EmitUniqueSection && !UniqueSectionNames) {
1453  UniqueID = *NextUniqueID;
1454  (*NextUniqueID)++;
1455  }
1456  return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
1457 }
1458 
1460  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1461 
1462  if (Kind.isCommon())
1463  report_fatal_error("mergable sections not supported yet on wasm");
1464 
1465  // If we have -ffunction-section or -fdata-section then we should emit the
1466  // global value to a uniqued section specifically for it.
1467  bool EmitUniqueSection = false;
1468  if (Kind.isText())
1469  EmitUniqueSection = TM.getFunctionSections();
1470  else
1471  EmitUniqueSection = TM.getDataSections();
1472  EmitUniqueSection |= GO->hasComdat();
1473 
1474  return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
1475  EmitUniqueSection, &NextUniqueID);
1476 }
1477 
1479  bool UsesLabelDifference, const Function &F) const {
1480  // We can always create relative relocations, so use another section
1481  // that can be marked non-executable.
1482  return false;
1483 }
1484 
1486  const GlobalValue *LHS, const GlobalValue *RHS,
1487  const TargetMachine &TM) const {
1488  // We may only use a PLT-relative relocation to refer to unnamed_addr
1489  // functions.
1490  if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1491  return nullptr;
1492 
1493  // Basic sanity checks.
1494  if (LHS->getType()->getPointerAddressSpace() != 0 ||
1495  RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1496  RHS->isThreadLocal())
1497  return nullptr;
1498 
1499  return MCBinaryExpr::createSub(
1501  getContext()),
1503 }
1504 
1507  getContext().getWasmSection(".init_array", SectionKind::getData());
1508 }
1509 
1511  unsigned Priority, const MCSymbol *KeySym) const {
1512  return Priority == UINT16_MAX ?
1514  getContext().getWasmSection(".init_array." + utostr(Priority),
1516 }
1517 
1519  unsigned Priority, const MCSymbol *KeySym) const {
1520  llvm_unreachable("@llvm.global_dtors should have been lowered already");
1521  return nullptr;
1522 }
const MCExpr * lowerRelativeReference(const GlobalValue *LHS, const GlobalValue *RHS, const TargetMachine &TM) const override
AttributeSet getAttributes() const
Return the attribute set for this global.
uint64_t CallInst * C
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const override
bool isBSSExtern() const
Definition: SectionKind.h:162
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:90
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void push_back(const T &Elt)
Definition: SmallVector.h:213
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override
Emit Obj-C garbage collection and linker options.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:709
static SectionKind getData()
Definition: SectionKind.h:202
static MCSectionCOFF * getCOFFStaticStructorSection(MCContext &Ctx, const Triple &T, bool IsCtor, unsigned Priority, const MCSymbol *KeySym, MCSectionCOFF *Default)
static MCSectionELF * selectELFSectionForGlobal(MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol)
bool isThreadData() const
Definition: SectionKind.h:154
bool hasLocalLinkage() const
Definition: GlobalValue.h:435
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
bool hasAttribute(unsigned Value) const
This represents a section on a Mach-O system (used by Mac OS X).
MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const override
MCSection * getStaticCtorSection(unsigned Priority, const MCSymbol *KeySym) const override
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:321
bool hasPrivateLinkage() const
Definition: GlobalValue.h:434
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
PointerTy getPointer() const
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:147
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
This represents an "assembler immediate".
Definition: MCValue.h:40
bool isWriteable() const
Definition: SectionKind.h:145
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:294
static std::string scalarConstantToHexString(const Constant *C)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void emitLinkerFlagsForGlobal(raw_ostream &OS, const GlobalValue *GV) const override
virtual void AddBlankLine()
AddBlankLine - Emit a blank line to a .s file to pretty it up.
Definition: MCStreamer.h:324
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const override
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * SixteenByteConstantSection
MCSection * TextSection
Section directive for standard text.
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override
Emit Obj-C garbage collection and linker options.
MCSection * StaticCtorSection
This section contains the static constructor pointer list.
MCSection * ConstTextCoalSection
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
Definition: MCStreamer.cpp:954
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override
Emit the module flags that specify the garbage collection information.
static void checkMachOComdat(const GlobalValue *GV)
This file contains the declarations for metadata subclasses.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const override
MCSectionWasm * getWasmSection(const Twine &Section, SectionKind K)
Definition: MCContext.h:455
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
bool isMergeableConst8() const
Definition: SectionKind.h:141
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:307
The data referenced by the COMDAT must be the same size.
Definition: Comdat.h:39
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
bool isMergeableCString() const
Definition: SectionKind.h:128
const MCExpr * getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, MCStreamer &Streamer) const
Metadata node.
Definition: Metadata.h:862
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:165
void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const override
F(f)
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a constant with the SectionKind, return a section that it should be placed in.
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const override
const MCExpr * getIndirectSymViaGOTPCRel(const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Get MachO PC relative GOT entry relocation.
unsigned getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
Definition: DataLayout.cpp:611
static bool isThumb(const MCSubtargetInfo &STI)
unsigned getTypeAndAttributes() const
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1493
MCSectionCOFF * getCOFFSection(StringRef Section, unsigned Characteristics, SectionKind Kind, StringRef COMDATSymName, int Selection, unsigned UniqueID=GenericSectionID, const char *BeginSymName=nullptr)
Definition: MCContext.cpp:422
bool isMergeable2ByteCString() const
Definition: SectionKind.h:133
MCSectionCOFF * getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym, unsigned UniqueID=GenericSectionID)
Gets or creates a section equivalent to Sec that is associated with the section containing KeySym...
Definition: MCContext.cpp:470
Adds a requirement that another module flag be present and have a specified value after linking is pe...
Definition: Module.h:127
This represents a section on Windows.
Definition: MCSectionCOFF.h:27
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
static MCSectionWasm * selectWasmSectionForGlobal(MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID)
bool hasSection() const
Definition: GlobalValue.h:269
int64_t getConstant() const
Definition: MCValue.h:47
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:49
A tuple of MDNodes.
Definition: Metadata.h:1323
static SectionKind getBSS()
Definition: SectionKind.h:198
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
uint64_t getArrayNumElements() const
Definition: DerivedTypes.h:388
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
bool isKnownWindowsMSVCEnvironment() const
Checks if the environment is MSVC.
Definition: Triple.h:528
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1444
The linker may choose any COMDAT.
Definition: Comdat.h:35
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
const MCExpr * lowerRelativeReference(const GlobalValue *LHS, const GlobalValue *RHS, const TargetMachine &TM) const override
MCSection * EightByteConstantSection
S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for termination.
Definition: MachO.h:143
MCSection * TLSDataSection
Section directive for Thread Local data. ELF, MachO, COFF, and Wasm.
static const GlobalValue * getComdatGVForCOFF(const GlobalValue *GV)
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Context object for machine code objects.
Definition: MCContext.h:63
bool hasExternalLinkage() const
Definition: GlobalValue.h:421
Key
PAL metadata keys.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
bool isText() const
Definition: SectionKind.h:119
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:544
MCSectionMachO * getMachOSection(StringRef Segment, StringRef Section, unsigned TypeAndAttributes, unsigned Reserved2, SectionKind K, const char *BeginSymName=nullptr)
Return the MCSection for the specified mach-o section.
Definition: MCContext.cpp:273
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:283
static std::string APIntToHexString(const APInt &AI)
MCSection * StaticDtorSection
This section contains the static destructor pointer list.
MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) const override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, StringRef &Section)
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:242
MCSection * ConstDataCoalSection
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:112
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
bool isBSSLocal() const
Definition: SectionKind.h:161
bool isMergeableConst32() const
Definition: SectionKind.h:143
bool hasImplicitSection() const
Check if section name is present.
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:337
static SectionKind getThreadData()
Definition: SectionKind.h:197
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:459
bool isWindowsItaniumEnvironment() const
Definition: Triple.h:536
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:112
bool isWindowsCoreCLREnvironment() const
Definition: Triple.h:532
Pass this value as the UniqueID during section creation to get the generic section with the given nam...
Definition: MCContext.h:373
MCSection * DataSection
Section directive for standard data.
bool getDataSections() const
Return true if data objects should be emitted into their own section, corresponds to -fdata-sections...
SectionCharacteristics
Definition: COFF.h:284
bool isReadOnlyWithRel() const
Definition: SectionKind.h:168
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const override
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:328
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:369
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool getFunctionSections() const
Return true if functions should be emitted into their own section, corresponding to -ffunction-sectio...
.hidden (ELF)
Definition: MCDirectives.h:31
void getModuleFlagsMetadata(SmallVectorImpl< ModuleFlagEntry > &Flags) const
Returns the module flags in the provided vector.
Definition: Module.cpp:282
static const Comdat * getWasmComdat(const GlobalValue *GV)
No other Module may specify this COMDAT.
Definition: Comdat.h:38
Streaming machine code generation interface.
Definition: MCStreamer.h:183
StringRef getName() const
Definition: Comdat.cpp:27
static std::string ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
Parse the section specifier indicated by "Spec".
PointerIntPair - This class implements a pair of a pointer and small integer.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a mergeable constant with the specified size and relocation information, return a section that ...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:629
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:976
This represents a section on wasm.
Definition: MCSectionWasm.h:28
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Return an MCExpr to use for a reference to the specified type info global variable from exception han...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K)
virtual MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const
Given a constant with the SectionKind, return a section that it should be placed in.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
MCSymbol * getSymbol(const GlobalValue *GV) const
MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) const override
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:74
MCSymbol * getSymbolWithGlobalValueBase(const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const
Return the MCSymbol for a private symbol with global value name as its base, with the specified suffi...
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:964
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition: Mangler.cpp:185
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
const Triple & getTargetTriple() const
void emitLinkerFlagsForUsed(raw_ostream &OS, const GlobalValue *GV) const override
virtual const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const
Return an MCExpr to use for a reference to the specified global variable from exception handling info...
AMDGPU DAG DAG Pattern Instruction Selection
bool isMergeableConst16() const
Definition: SectionKind.h:142
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a constant with the SectionKind, return a section that it should be placed in.
Ty & getObjFileInfo()
Keep track of various per-function pieces of information for backends that would like to do so...
The data referenced by the COMDAT must be the same.
Definition: Comdat.h:36
StubValueTy & getGVStubEntry(MCSymbol *Sym)
static const char * getCOFFSectionNameForUniqueGlobal(SectionKind Kind)
StubValueTy & getGVStubEntry(MCSymbol *Sym)
MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) const override
bool isThreadBSS() const
Definition: SectionKind.h:153
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCSection * getDrectveSection() const
bool isBSS() const
Definition: SectionKind.h:160
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, const Function &F) const override
bool isMergeableConst4() const
Definition: SectionKind.h:140
static const MCSymbolELF * getAssociatedSymbol(const GlobalObject *GO, const TargetMachine &TM)
static SectionKind getThreadBSS()
Definition: SectionKind.h:196
static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, const MCSection &Section)
const MCSymbol & getSymbol() const
Definition: MCExpr.h:334
bool isMergeable4ByteCString() const
Definition: SectionKind.h:134
bool isCommon() const
Definition: SectionKind.h:164
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
static SectionKind getMetadata()
Definition: SectionKind.h:179
bool isMetadata() const
Definition: SectionKind.h:117
const Triple & getTargetTriple() const
static const Comdat * getELFComdat(const GlobalValue *GV)
The linker will choose the largest COMDAT.
Definition: Comdat.h:37
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:199
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:203
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:810
This file contains constants used for implementing Dwarf debug support.
const Comdat * getComdat() const
Definition: Globals.cpp:166
Class for arbitrary precision integers.
Definition: APInt.h:69
static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K)
static StringRef getSectionPrefixForGlobal(SectionKind Kind)
Return the section prefix name used by options FunctionsSections and DataSections.
MCSection * FourByteConstantSection
bool hasComdat() const
Definition: GlobalObject.h:100
bool isReadOnly() const
Definition: SectionKind.h:123
MachineModuleInfoELF - This is a MachineModuleInfoImpl implementation for ELF targets.
const Comdat * getComdat() const
Definition: GlobalObject.h:101
bool isMergeableConst() const
Definition: SectionKind.h:136
const MCExpr * lowerRelativeReference(const GlobalValue *LHS, const GlobalValue *RHS, const TargetMachine &TM) const override
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:212
MCSection * getStaticCtorSection(unsigned Priority, const MCSymbol *KeySym) const override
virtual void EmitLinkerOptions(ArrayRef< std::string > Kind)
Emit the given list Options of strings as linker options into the output.
Definition: MCStreamer.h:440
bool getUniqueSectionNames() const
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
MCSection * getStaticCtorSection(unsigned Priority, const MCSymbol *KeySym) const override
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
Definition: APInt.h:475
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
static int getSelectionForCOFF(const GlobalValue *GV)
#define I(x, y, z)
Definition: MD5.cpp:58
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:323
virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value)
Emit an ELF .size directive.
Definition: MCStreamer.cpp:945
static unsigned getCOFFSectionFlags(SectionKind K, const TargetMachine &TM)
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:28
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Type * getValueType() const
Definition: GlobalValue.h:275
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &T, Mangler &M)
Definition: Mangler.cpp:216
const Module * getModule() const
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
const GlobalObject * getBaseObject() const
Definition: Globals.cpp:256
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
const unsigned Kind
MCSection * TLSBSSSection
Section directive for Thread Local uninitialized data.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:477
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:390
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
static MCSectionELF * getStaticStructorSection(MCContext &Ctx, bool UseInitArray, bool IsCtor, unsigned Priority, const MCSymbol *KeySym)
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
bool isData() const
Definition: SectionKind.h:166
bool isThreadLocal() const
Definition: SectionKind.h:149
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool isMergeable1ByteCString() const
Definition: SectionKind.h:132
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:322
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:317
Metadata * get() const
Definition: Metadata.h:720
bool isExecuteOnly() const
Definition: SectionKind.h:121
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:112
MCSection * BSSSection
Section that is default initialized to zero.
bool isThreadLocal() const
If the value is "Thread Local", its value isn&#39;t shared by the threads.
Definition: GlobalValue.h:246
const MCSymbol * getAssociatedSymbol() const
Definition: MCSectionELF.h:90
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
S_ATTR_NO_DEAD_STRIP - No dead stripping.
Definition: MachO.h:190
MCSection * ReadOnlySection
Section that is readonly and can contain arbitrary initialized data.
static unsigned getELFSectionFlags(SectionKind K)
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
static APInt getNullValue(unsigned numBits)
Get the &#39;0&#39; value.
Definition: APInt.h:562
S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for initialization.
Definition: MachO.h:140
static SectionKind getReadOnly()
Definition: SectionKind.h:182
const uint64_t Version
Definition: InstrProf.h:895
static unsigned getELFSectionType(StringRef Name, SectionKind K)
This class can be derived from and used by targets to hold private target-specific information for ea...
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:273
This class contains meta information specific to a module.
std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:105
SelectionKind getSelectionKind() const
Definition: Comdat.h:45
static SectionKind getText()
Definition: SectionKind.h:180
bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, const Function &F) const override