LLVM  4.0.0
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/StringExtras.h"
18 #include "llvm/ADT/Triple.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"
37 #include "llvm/Support/COFF.h"
38 #include "llvm/Support/Dwarf.h"
39 #include "llvm/Support/ELF.h"
45 using namespace llvm;
46 using namespace dwarf;
47 
48 //===----------------------------------------------------------------------===//
49 // ELF
50 //===----------------------------------------------------------------------===//
51 
53  const GlobalValue *GV, const TargetMachine &TM,
54  MachineModuleInfo *MMI) const {
55  unsigned Encoding = getPersonalityEncoding();
56  if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
57  return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
58  TM.getSymbol(GV)->getName());
59  if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
60  return TM.getSymbol(GV);
61  report_fatal_error("We do not support this DWARF encoding yet!");
62 }
63 
65  MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
66  SmallString<64> NameData("DW.ref.");
67  NameData += Sym->getName();
68  MCSymbolELF *Label =
69  cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
70  Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
71  Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
73  MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
75  unsigned Size = DL.getPointerSize();
76  Streamer.SwitchSection(Sec);
79  const MCExpr *E = MCConstantExpr::create(Size, getContext());
80  Streamer.emitELFSize(Label, E);
81  Streamer.EmitLabel(Label);
82 
83  Streamer.EmitSymbolValue(Sym, Size);
84 }
85 
87  const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
88  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
89 
90  if (Encoding & dwarf::DW_EH_PE_indirect) {
92 
93  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
94 
95  // Add information about the stub reference to ELFMMI so that the stub
96  // gets emitted by the asmprinter.
97  MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
98  if (!StubSym.getPointer()) {
99  MCSymbol *Sym = TM.getSymbol(GV);
101  }
102 
104  getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
105  Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
106  }
107 
109  MMI, Streamer);
110 }
111 
112 static SectionKind
114  // N.B.: The defaults used in here are no the same ones used in MC.
115  // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
116  // both gas and MC will produce a section with no flags. Given
117  // section(".eh_frame") gcc will produce:
118  //
119  // .section .eh_frame,"a",@progbits
120 
121  if (Name == getInstrProfCoverageSectionName(false))
122  return SectionKind::getMetadata();
123 
124  if (Name.empty() || Name[0] != '.') return K;
125 
126  // Some lame default implementation based on some magic section names.
127  if (Name == ".bss" ||
128  Name.startswith(".bss.") ||
129  Name.startswith(".gnu.linkonce.b.") ||
130  Name.startswith(".llvm.linkonce.b.") ||
131  Name == ".sbss" ||
132  Name.startswith(".sbss.") ||
133  Name.startswith(".gnu.linkonce.sb.") ||
134  Name.startswith(".llvm.linkonce.sb."))
135  return SectionKind::getBSS();
136 
137  if (Name == ".tdata" ||
138  Name.startswith(".tdata.") ||
139  Name.startswith(".gnu.linkonce.td.") ||
140  Name.startswith(".llvm.linkonce.td."))
142 
143  if (Name == ".tbss" ||
144  Name.startswith(".tbss.") ||
145  Name.startswith(".gnu.linkonce.tb.") ||
146  Name.startswith(".llvm.linkonce.tb."))
147  return SectionKind::getThreadBSS();
148 
149  return K;
150 }
151 
152 
154  // Use SHT_NOTE for section whose name starts with ".note" to allow
155  // emitting ELF notes from C variable declaration.
156  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
157  if (Name.startswith(".note"))
158  return ELF::SHT_NOTE;
159 
160  if (Name == ".init_array")
161  return ELF::SHT_INIT_ARRAY;
162 
163  if (Name == ".fini_array")
164  return ELF::SHT_FINI_ARRAY;
165 
166  if (Name == ".preinit_array")
167  return ELF::SHT_PREINIT_ARRAY;
168 
169  if (K.isBSS() || K.isThreadBSS())
170  return ELF::SHT_NOBITS;
171 
172  return ELF::SHT_PROGBITS;
173 }
174 
175 static unsigned getELFSectionFlags(SectionKind K) {
176  unsigned Flags = 0;
177 
178  if (!K.isMetadata())
179  Flags |= ELF::SHF_ALLOC;
180 
181  if (K.isText())
182  Flags |= ELF::SHF_EXECINSTR;
183 
184  if (K.isExecuteOnly())
185  Flags |= ELF::SHF_ARM_PURECODE;
186 
187  if (K.isWriteable())
188  Flags |= ELF::SHF_WRITE;
189 
190  if (K.isThreadLocal())
191  Flags |= ELF::SHF_TLS;
192 
193  if (K.isMergeableCString() || K.isMergeableConst())
194  Flags |= ELF::SHF_MERGE;
195 
196  if (K.isMergeableCString())
197  Flags |= ELF::SHF_STRINGS;
198 
199  return Flags;
200 }
201 
202 static const Comdat *getELFComdat(const GlobalValue *GV) {
203  const Comdat *C = GV->getComdat();
204  if (!C)
205  return nullptr;
206 
207  if (C->getSelectionKind() != Comdat::Any)
208  report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
209  C->getName() + "' cannot be lowered.");
210 
211  return C;
212 }
213 
215  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
217 
218  // Infer section flags from the section name if we can.
219  Kind = getELFKindForNamedSection(SectionName, Kind);
220 
221  StringRef Group = "";
222  unsigned Flags = getELFSectionFlags(Kind);
223  if (const Comdat *C = getELFComdat(GO)) {
224  Group = C->getName();
225  Flags |= ELF::SHF_GROUP;
226  }
227  return getContext().getELFSection(SectionName,
228  getELFSectionType(SectionName, Kind), Flags,
229  /*EntrySize=*/0, Group);
230 }
231 
232 /// Return the section prefix name used by options FunctionsSections and
233 /// DataSections.
235  if (Kind.isText())
236  return ".text";
237  if (Kind.isReadOnly())
238  return ".rodata";
239  if (Kind.isBSS())
240  return ".bss";
241  if (Kind.isThreadData())
242  return ".tdata";
243  if (Kind.isThreadBSS())
244  return ".tbss";
245  if (Kind.isData())
246  return ".data";
247  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
248  return ".data.rel.ro";
249 }
250 
251 static MCSectionELF *
253  SectionKind Kind, Mangler &Mang,
254  const TargetMachine &TM, bool EmitUniqueSection,
255  unsigned Flags, unsigned *NextUniqueID) {
256  unsigned EntrySize = 0;
257  if (Kind.isMergeableCString()) {
258  if (Kind.isMergeable2ByteCString()) {
259  EntrySize = 2;
260  } else if (Kind.isMergeable4ByteCString()) {
261  EntrySize = 4;
262  } else {
263  EntrySize = 1;
264  assert(Kind.isMergeable1ByteCString() && "unknown string width");
265  }
266  } else if (Kind.isMergeableConst()) {
267  if (Kind.isMergeableConst4()) {
268  EntrySize = 4;
269  } else if (Kind.isMergeableConst8()) {
270  EntrySize = 8;
271  } else if (Kind.isMergeableConst16()) {
272  EntrySize = 16;
273  } else {
274  assert(Kind.isMergeableConst32() && "unknown data width");
275  EntrySize = 32;
276  }
277  }
278 
279  StringRef Group = "";
280  if (const Comdat *C = getELFComdat(GO)) {
281  Flags |= ELF::SHF_GROUP;
282  Group = C->getName();
283  }
284 
287  if (Kind.isMergeableCString()) {
288  // We also need alignment here.
289  // FIXME: this is getting the alignment of the character, not the
290  // alignment of the global!
291  unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
292  cast<GlobalVariable>(GO));
293 
294  std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
295  Name = SizeSpec + utostr(Align);
296  } else if (Kind.isMergeableConst()) {
297  Name = ".rodata.cst";
298  Name += utostr(EntrySize);
299  } else {
300  Name = getSectionPrefixForGlobal(Kind);
301  }
302 
303  if (const auto *F = dyn_cast<Function>(GO)) {
304  const auto &OptionalPrefix = F->getSectionPrefix();
305  if (OptionalPrefix)
306  Name += *OptionalPrefix;
307  }
308 
309  if (EmitUniqueSection && UniqueSectionNames) {
310  Name.push_back('.');
311  TM.getNameWithPrefix(Name, GO, Mang, true);
312  }
313  unsigned UniqueID = MCContext::GenericSectionID;
314  if (EmitUniqueSection && !UniqueSectionNames) {
315  UniqueID = *NextUniqueID;
316  (*NextUniqueID)++;
317  }
318  // Use 0 as the unique ID for execute-only text
319  if (Kind.isExecuteOnly())
320  UniqueID = 0;
321  return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
322  EntrySize, Group, UniqueID);
323 }
324 
326  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
327  unsigned Flags = getELFSectionFlags(Kind);
328 
329  // If we have -ffunction-section or -fdata-section then we should emit the
330  // global value to a uniqued section specifically for it.
331  bool EmitUniqueSection = false;
332  if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
333  if (Kind.isText())
334  EmitUniqueSection = TM.getFunctionSections();
335  else
336  EmitUniqueSection = TM.getDataSections();
337  }
338  EmitUniqueSection |= GO->hasComdat();
339 
340  return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
341  EmitUniqueSection, Flags, &NextUniqueID);
342 }
343 
345  const Function &F, const TargetMachine &TM) const {
346  // If the function can be removed, produce a unique section so that
347  // the table doesn't prevent the removal.
348  const Comdat *C = F.getComdat();
349  bool EmitUniqueSection = TM.getFunctionSections() || C;
350  if (!EmitUniqueSection)
351  return ReadOnlySection;
352 
353  return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
354  getMangler(), TM, EmitUniqueSection, ELF::SHF_ALLOC,
355  &NextUniqueID);
356 }
357 
359  bool UsesLabelDifference, const Function &F) const {
360  // We can always create relative relocations, so use another section
361  // that can be marked non-executable.
362  return false;
363 }
364 
365 /// Given a mergeable constant with the specified size and relocation
366 /// information, return a section that it should be placed in.
368  const DataLayout &DL, SectionKind Kind, const Constant *C,
369  unsigned &Align) const {
370  if (Kind.isMergeableConst4() && MergeableConst4Section)
371  return MergeableConst4Section;
372  if (Kind.isMergeableConst8() && MergeableConst8Section)
373  return MergeableConst8Section;
374  if (Kind.isMergeableConst16() && MergeableConst16Section)
375  return MergeableConst16Section;
376  if (Kind.isMergeableConst32() && MergeableConst32Section)
377  return MergeableConst32Section;
378  if (Kind.isReadOnly())
379  return ReadOnlySection;
380 
381  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
382  return DataRelROSection;
383 }
384 
385 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
386  bool IsCtor, unsigned Priority,
387  const MCSymbol *KeySym) {
388  std::string Name;
389  unsigned Type;
390  unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
391  StringRef COMDAT = KeySym ? KeySym->getName() : "";
392 
393  if (KeySym)
394  Flags |= ELF::SHF_GROUP;
395 
396  if (UseInitArray) {
397  if (IsCtor) {
398  Type = ELF::SHT_INIT_ARRAY;
399  Name = ".init_array";
400  } else {
401  Type = ELF::SHT_FINI_ARRAY;
402  Name = ".fini_array";
403  }
404  if (Priority != 65535) {
405  Name += '.';
406  Name += utostr(Priority);
407  }
408  } else {
409  // The default scheme is .ctor / .dtor, so we have to invert the priority
410  // numbering.
411  if (IsCtor)
412  Name = ".ctors";
413  else
414  Name = ".dtors";
415  if (Priority != 65535) {
416  Name += '.';
417  Name += utostr(65535 - Priority);
418  }
419  Type = ELF::SHT_PROGBITS;
420  }
421 
422  return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
423 }
424 
426  unsigned Priority, const MCSymbol *KeySym) const {
427  return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
428  KeySym);
429 }
430 
432  unsigned Priority, const MCSymbol *KeySym) const {
433  return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
434  KeySym);
435 }
436 
438  const GlobalValue *LHS, const GlobalValue *RHS,
439  const TargetMachine &TM) const {
440  // We may only use a PLT-relative relocation to refer to unnamed_addr
441  // functions.
442  if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
443  return nullptr;
444 
445  // Basic sanity checks.
446  if (LHS->getType()->getPointerAddressSpace() != 0 ||
447  RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
448  RHS->isThreadLocal())
449  return nullptr;
450 
452  MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
453  getContext()),
454  MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
455 }
456 
457 void
459  UseInitArray = UseInitArray_;
460  MCContext &Ctx = getContext();
461  if (!UseInitArray) {
462  StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
464 
465  StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
467  return;
468  }
469 
470  StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
472  StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
474 }
475 
476 //===----------------------------------------------------------------------===//
477 // MachO
478 //===----------------------------------------------------------------------===//
479 
483 }
484 
486  const TargetMachine &TM) {
488  if (TM.getRelocationModel() == Reloc::Static) {
489  StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
491  StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
493  } else {
494  StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
497  StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
500  }
501 }
502 
503 /// emitModuleFlags - Perform code emission for module flags.
505  MCStreamer &Streamer, ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
506  const TargetMachine &TM) const {
507  unsigned VersionVal = 0;
508  unsigned ImageInfoFlags = 0;
509  MDNode *LinkerOptions = nullptr;
510  StringRef SectionVal;
511 
512  for (const auto &MFE : ModuleFlags) {
513  // Ignore flags with 'Require' behavior.
514  if (MFE.Behavior == Module::Require)
515  continue;
516 
517  StringRef Key = MFE.Key->getString();
518  Metadata *Val = MFE.Val;
519 
520  if (Key == "Objective-C Image Info Version") {
521  VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
522  } else if (Key == "Objective-C Garbage Collection" ||
523  Key == "Objective-C GC Only" ||
524  Key == "Objective-C Is Simulated" ||
525  Key == "Objective-C Class Properties" ||
526  Key == "Objective-C Image Swift Version") {
527  ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
528  } else if (Key == "Objective-C Image Info Section") {
529  SectionVal = cast<MDString>(Val)->getString();
530  } else if (Key == "Linker Options") {
531  LinkerOptions = cast<MDNode>(Val);
532  }
533  }
534 
535  // Emit the linker options if present.
536  if (LinkerOptions) {
537  for (const auto &Option : LinkerOptions->operands()) {
538  SmallVector<std::string, 4> StrOptions;
539  for (const auto &Piece : cast<MDNode>(Option)->operands())
540  StrOptions.push_back(cast<MDString>(Piece)->getString());
541  Streamer.EmitLinkerOptions(StrOptions);
542  }
543  }
544 
545  // The section is mandatory. If we don't have it, then we don't have GC info.
546  if (SectionVal.empty()) return;
547 
548  StringRef Segment, Section;
549  unsigned TAA = 0, StubSize = 0;
550  bool TAAParsed;
551  std::string ErrorCode =
552  MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
553  TAA, TAAParsed, StubSize);
554  if (!ErrorCode.empty())
555  // If invalid, report the error with report_fatal_error.
556  report_fatal_error("Invalid section specifier '" + Section + "': " +
557  ErrorCode + ".");
558 
559  // Get the section.
561  Segment, Section, TAA, StubSize, SectionKind::getData());
562  Streamer.SwitchSection(S);
563  Streamer.EmitLabel(getContext().
564  getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
565  Streamer.EmitIntValue(VersionVal, 4);
566  Streamer.EmitIntValue(ImageInfoFlags, 4);
567  Streamer.AddBlankLine();
568 }
569 
570 static void checkMachOComdat(const GlobalValue *GV) {
571  const Comdat *C = GV->getComdat();
572  if (!C)
573  return;
574 
575  report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
576  "' cannot be lowered.");
577 }
578 
580  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
581  // Parse the section specifier and create it if valid.
582  StringRef Segment, Section;
583  unsigned TAA = 0, StubSize = 0;
584  bool TAAParsed;
585 
586  checkMachOComdat(GO);
587 
588  std::string ErrorCode =
590  TAA, TAAParsed, StubSize);
591  if (!ErrorCode.empty()) {
592  // If invalid, report the error with report_fatal_error.
593  report_fatal_error("Global variable '" + GO->getName() +
594  "' has an invalid section specifier '" +
595  GO->getSection() + "': " + ErrorCode + ".");
596  }
597 
598  // Get the section.
599  MCSectionMachO *S =
600  getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
601 
602  // If TAA wasn't set by ParseSectionSpecifier() above,
603  // use the value returned by getMachOSection() as a default.
604  if (!TAAParsed)
605  TAA = S->getTypeAndAttributes();
606 
607  // Okay, now that we got the section, verify that the TAA & StubSize agree.
608  // If the user declared multiple globals with different section flags, we need
609  // to reject it here.
610  if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
611  // If invalid, report the error with report_fatal_error.
612  report_fatal_error("Global variable '" + GO->getName() +
613  "' section type or attributes does not match previous"
614  " section specifier");
615  }
616 
617  return S;
618 }
619 
621  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
622  checkMachOComdat(GO);
623 
624  // Handle thread local data.
625  if (Kind.isThreadBSS()) return TLSBSSSection;
626  if (Kind.isThreadData()) return TLSDataSection;
627 
628  if (Kind.isText())
630 
631  // If this is weak/linkonce, put this in a coalescable section, either in text
632  // or data depending on if it is writable.
633  if (GO->isWeakForLinker()) {
634  if (Kind.isReadOnly())
635  return ConstTextCoalSection;
636  return DataCoalSection;
637  }
638 
639  // FIXME: Alignment check should be handled by section classifier.
640  if (Kind.isMergeable1ByteCString() &&
642  cast<GlobalVariable>(GO)) < 32)
643  return CStringSection;
644 
645  // Do not put 16-bit arrays in the UString section if they have an
646  // externally visible label, this runs into issues with certain linker
647  // versions.
648  if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
650  cast<GlobalVariable>(GO)) < 32)
651  return UStringSection;
652 
653  // With MachO only variables whose corresponding symbol starts with 'l' or
654  // 'L' can be merged, so we only try merging GVs with private linkage.
655  if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
656  if (Kind.isMergeableConst4())
658  if (Kind.isMergeableConst8())
660  if (Kind.isMergeableConst16())
662  }
663 
664  // Otherwise, if it is readonly, but not something we can specially optimize,
665  // just drop it in .const.
666  if (Kind.isReadOnly())
667  return ReadOnlySection;
668 
669  // If this is marked const, put it into a const section. But if the dynamic
670  // linker needs to write to it, put it in the data segment.
671  if (Kind.isReadOnlyWithRel())
672  return ConstDataSection;
673 
674  // Put zero initialized globals with strong external linkage in the
675  // DATA, __common section with the .zerofill directive.
676  if (Kind.isBSSExtern())
677  return DataCommonSection;
678 
679  // Put zero initialized globals with local linkage in __DATA,__bss directive
680  // with the .zerofill directive (aka .lcomm).
681  if (Kind.isBSSLocal())
682  return DataBSSSection;
683 
684  // Otherwise, just drop the variable in the normal data section.
685  return DataSection;
686 }
687 
689  const DataLayout &DL, SectionKind Kind, const Constant *C,
690  unsigned &Align) const {
691  // If this constant requires a relocation, we have to put it in the data
692  // segment, not in the text segment.
693  if (Kind.isData() || Kind.isReadOnlyWithRel())
694  return ConstDataSection;
695 
696  if (Kind.isMergeableConst4())
698  if (Kind.isMergeableConst8())
700  if (Kind.isMergeableConst16())
702  return ReadOnlySection; // .const
703 }
704 
706  const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
707  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
708  // The mach-o version of this method defaults to returning a stub reference.
709 
710  if (Encoding & DW_EH_PE_indirect) {
711  MachineModuleInfoMachO &MachOMMI =
713 
714  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
715 
716  // Add information about the stub reference to MachOMMI so that the stub
717  // gets emitted by the asmprinter.
718  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
719  if (!StubSym.getPointer()) {
720  MCSymbol *Sym = TM.getSymbol(GV);
722  }
723 
726  Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
727  }
728 
730  MMI, Streamer);
731 }
732 
734  const GlobalValue *GV, const TargetMachine &TM,
735  MachineModuleInfo *MMI) const {
736  // The mach-o version of this method defaults to returning a stub reference.
737  MachineModuleInfoMachO &MachOMMI =
739 
740  MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
741 
742  // Add information about the stub reference to MachOMMI so that the stub
743  // gets emitted by the asmprinter.
744  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
745  if (!StubSym.getPointer()) {
746  MCSymbol *Sym = TM.getSymbol(GV);
748  }
749 
750  return SSym;
751 }
752 
754  const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
755  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
756  // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
757  // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
758  // through a non_lazy_ptr stub instead. One advantage is that it allows the
759  // computation of deltas to final external symbols. Example:
760  //
761  // _extgotequiv:
762  // .long _extfoo
763  //
764  // _delta:
765  // .long _extgotequiv-_delta
766  //
767  // is transformed to:
768  //
769  // _delta:
770  // .long L_extfoo$non_lazy_ptr-(_delta+0)
771  //
772  // .section __IMPORT,__pointers,non_lazy_symbol_pointers
773  // L_extfoo$non_lazy_ptr:
774  // .indirect_symbol _extfoo
775  // .long 0
776  //
777  MachineModuleInfoMachO &MachOMMI =
779  MCContext &Ctx = getContext();
780 
781  // The offset must consider the original displacement from the base symbol
782  // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
783  Offset = -MV.getConstant();
784  const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
785 
786  // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
787  // non_lazy_ptr stubs.
789  StringRef Suffix = "$non_lazy_ptr";
790  Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
791  Name += Sym->getName();
792  Name += Suffix;
793  MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
794 
795  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
796  if (!StubSym.getPointer())
797  StubSym = MachineModuleInfoImpl::
798  StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
799 
800  const MCExpr *BSymExpr =
802  const MCExpr *LHS =
804 
805  if (!Offset)
806  return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
807 
808  const MCExpr *RHS =
809  MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
810  return MCBinaryExpr::createSub(LHS, RHS, Ctx);
811 }
812 
813 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
814  const MCSection &Section) {
815  if (!AsmInfo.isSectionAtomizableBySymbols(Section))
816  return true;
817 
818  // If it is not dead stripped, it is safe to use private labels.
819  const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
821  return true;
822 
823  return false;
824 }
825 
827  SmallVectorImpl<char> &OutName, const GlobalValue *GV,
828  const TargetMachine &TM) const {
829  bool CannotUsePrivateLabel = true;
830  if (auto *GO = GV->getBaseObject()) {
832  const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
833  CannotUsePrivateLabel =
834  !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
835  }
836  getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
837 }
838 
839 //===----------------------------------------------------------------------===//
840 // COFF
841 //===----------------------------------------------------------------------===//
842 
843 static unsigned
845  unsigned Flags = 0;
846  bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
847 
848  if (K.isMetadata())
849  Flags |=
851  else if (K.isText())
852  Flags |=
857  else if (K.isBSS())
858  Flags |=
862  else if (K.isThreadLocal())
863  Flags |=
867  else if (K.isReadOnly() || K.isReadOnlyWithRel())
868  Flags |=
871  else if (K.isWriteable())
872  Flags |=
876 
877  return Flags;
878 }
879 
880 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
881  const Comdat *C = GV->getComdat();
882  assert(C && "expected GV to have a Comdat!");
883 
884  StringRef ComdatGVName = C->getName();
885  const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
886  if (!ComdatGV)
887  report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
888  "' does not exist.");
889 
890  if (ComdatGV->getComdat() != C)
891  report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
892  "' is not a key for its COMDAT.");
893 
894  return ComdatGV;
895 }
896 
897 static int getSelectionForCOFF(const GlobalValue *GV) {
898  if (const Comdat *C = GV->getComdat()) {
899  const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
900  if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
901  ComdatKey = GA->getBaseObject();
902  if (ComdatKey == GV) {
903  switch (C->getSelectionKind()) {
904  case Comdat::Any:
906  case Comdat::ExactMatch:
908  case Comdat::Largest:
912  case Comdat::SameSize:
914  }
915  } else {
917  }
918  }
919  return 0;
920 }
921 
923  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
924  int Selection = 0;
925  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
926  StringRef Name = GO->getSection();
927  StringRef COMDATSymName = "";
928  if (GO->hasComdat()) {
929  Selection = getSelectionForCOFF(GO);
930  const GlobalValue *ComdatGV;
931  if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
932  ComdatGV = getComdatGVForCOFF(GO);
933  else
934  ComdatGV = GO;
935 
936  if (!ComdatGV->hasPrivateLinkage()) {
937  MCSymbol *Sym = TM.getSymbol(ComdatGV);
938  COMDATSymName = Sym->getName();
939  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
940  } else {
941  Selection = 0;
942  }
943  }
944 
945  return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
946  Selection);
947 }
948 
950  if (Kind.isText())
951  return ".text";
952  if (Kind.isBSS())
953  return ".bss";
954  if (Kind.isThreadLocal())
955  return ".tls$";
956  if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
957  return ".rdata";
958  return ".data";
959 }
960 
962  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
963  // If we have -ffunction-sections then we should emit the global value to a
964  // uniqued section specifically for it.
965  bool EmitUniquedSection;
966  if (Kind.isText())
967  EmitUniquedSection = TM.getFunctionSections();
968  else
969  EmitUniquedSection = TM.getDataSections();
970 
971  if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
972  const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
973  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
974 
975  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
976  int Selection = getSelectionForCOFF(GO);
977  if (!Selection)
979  const GlobalValue *ComdatGV;
980  if (GO->hasComdat())
981  ComdatGV = getComdatGVForCOFF(GO);
982  else
983  ComdatGV = GO;
984 
985  unsigned UniqueID = MCContext::GenericSectionID;
986  if (EmitUniquedSection)
987  UniqueID = NextUniqueID++;
988 
989  if (!ComdatGV->hasPrivateLinkage()) {
990  MCSymbol *Sym = TM.getSymbol(ComdatGV);
991  StringRef COMDATSymName = Sym->getName();
992  return getContext().getCOFFSection(Name, Characteristics, Kind,
993  COMDATSymName, Selection, UniqueID);
994  } else {
995  SmallString<256> TmpData;
996  getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
997  return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
998  Selection, UniqueID);
999  }
1000  }
1001 
1002  if (Kind.isText())
1003  return TextSection;
1004 
1005  if (Kind.isThreadLocal())
1006  return TLSDataSection;
1007 
1008  if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1009  return ReadOnlySection;
1010 
1011  // Note: we claim that common symbols are put in BSSSection, but they are
1012  // really emitted with the magic .comm directive, which creates a symbol table
1013  // entry but not a section.
1014  if (Kind.isBSS() || Kind.isCommon())
1015  return BSSSection;
1016 
1017  return DataSection;
1018 }
1019 
1021  SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1022  const TargetMachine &TM) const {
1023  bool CannotUsePrivateLabel = false;
1024  if (GV->hasPrivateLinkage() &&
1025  ((isa<Function>(GV) && TM.getFunctionSections()) ||
1026  (isa<GlobalVariable>(GV) && TM.getDataSections())))
1027  CannotUsePrivateLabel = true;
1028 
1029  getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1030 }
1031 
1033  const Function &F, const TargetMachine &TM) const {
1034  // If the function can be removed, produce a unique section so that
1035  // the table doesn't prevent the removal.
1036  const Comdat *C = F.getComdat();
1037  bool EmitUniqueSection = TM.getFunctionSections() || C;
1038  if (!EmitUniqueSection)
1039  return ReadOnlySection;
1040 
1041  // FIXME: we should produce a symbol for F instead.
1042  if (F.hasPrivateLinkage())
1043  return ReadOnlySection;
1044 
1045  MCSymbol *Sym = TM.getSymbol(&F);
1046  StringRef COMDATSymName = Sym->getName();
1047 
1049  const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1050  unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1051  Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1052  unsigned UniqueID = NextUniqueID++;
1053 
1054  return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1056 }
1057 
1059  MCStreamer &Streamer, ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
1060  const TargetMachine &TM) const {
1061  MDNode *LinkerOptions = nullptr;
1062 
1063  for (const auto &MFE : ModuleFlags) {
1064  StringRef Key = MFE.Key->getString();
1065  if (Key == "Linker Options")
1066  LinkerOptions = cast<MDNode>(MFE.Val);
1067  }
1068 
1069  if (LinkerOptions) {
1070  // Emit the linker options to the linker .drectve section. According to the
1071  // spec, this section is a space-separated string containing flags for
1072  // linker.
1073  MCSection *Sec = getDrectveSection();
1074  Streamer.SwitchSection(Sec);
1075  for (const auto &Option : LinkerOptions->operands()) {
1076  for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1077  // Lead with a space for consistency with our dllexport implementation.
1078  std::string Directive(" ");
1079  Directive.append(cast<MDString>(Piece)->getString());
1080  Streamer.EmitBytes(Directive);
1081  }
1082  }
1083  }
1084 }
1085 
1087  const TargetMachine &TM) {
1089  const Triple &T = TM.getTargetTriple();
1099  } else {
1108  }
1109 }
1110 
1112  unsigned Priority, const MCSymbol *KeySym) const {
1114  cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
1115 }
1116 
1118  unsigned Priority, const MCSymbol *KeySym) const {
1120  cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
1121 }
1122 
1124  raw_ostream &OS, const GlobalValue *GV) const {
1125  if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1126  return;
1127 
1128  const Triple &TT = getTargetTriple();
1129 
1131  OS << " /EXPORT:";
1132  else
1133  OS << " -export:";
1134 
1136  std::string Flag;
1137  raw_string_ostream FlagOS(Flag);
1138  getMangler().getNameWithPrefix(FlagOS, GV, false);
1139  FlagOS.flush();
1140  if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
1141  OS << Flag.substr(1);
1142  else
1143  OS << Flag;
1144  } else {
1145  getMangler().getNameWithPrefix(OS, GV, false);
1146  }
1147 
1148  if (!GV->getValueType()->isFunctionTy()) {
1150  OS << ",DATA";
1151  else
1152  OS << ",data";
1153  }
1154 }
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const override
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
void push_back(const T &Elt)
Definition: SmallVector.h:211
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static SectionKind getData()
Definition: SectionKind.h:202
bool isMergeableConst() const
Definition: SectionKind.h:136
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:284
bool hasComdat() const
Definition: GlobalObject.h:91
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...
This represents a section on a Mach-O system (used by Mac OS X).
MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const override
const MCSymbol & getSymbol() const
Definition: MCExpr.h:311
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
MCSection * getStaticCtorSection(unsigned Priority, const MCSymbol *KeySym) const override
cl::opt< bool > UniqueSectionNames("unique-section-names", cl::desc("Give unique names to every section"), cl::init(true))
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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:120
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
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:289
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
bool isBSSExtern() const
Definition: SectionKind.h:162
MCSymbol * getSymbol(const GlobalValue *GV) const
MCSection * TextSection
Section directive for standard text.
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:807
Type * getValueType() const
Definition: GlobalValue.h:261
static void checkMachOComdat(const GlobalValue *GV)
bool isMergeable2ByteCString() const
Definition: SectionKind.h:133
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const override
bool isBSS() const
Definition: SectionKind.h:160
bool isReadOnlyWithRel() const
Definition: SectionKind.h:168
The data referenced by the COMDAT must be the same size.
Definition: Comdat.h:36
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...
Metadata node.
Definition: Metadata.h:830
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:117
void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const override
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.
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.
static bool isThumb(const MCSubtargetInfo &STI)
bool isMergeableCString() const
Definition: SectionKind.h:128
char getGlobalPrefix() const
Definition: DataLayout.h:270
MCSectionCOFF * getCOFFSection(StringRef Section, unsigned Characteristics, SectionKind Kind, StringRef COMDATSymName, int Selection, unsigned UniqueID=GenericSectionID, const char *BeginSymName=nullptr)
Definition: MCContext.cpp:396
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
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:444
const Triple & getTargetTriple() const
Adds a requirement that another module flag be present and have a specified value after linking is pe...
Definition: Module.h:116
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:250
static SectionKind getBSS()
Definition: SectionKind.h:198
struct fuzzer::@269 Flags
unsigned getPointerABIAlignment(unsigned AS=0) const
Layout pointer alignment FIXME: The defaults need to be removed once all of the backends/clients are ...
Definition: DataLayout.cpp:590
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
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...
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
const MCExpr * getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, MCStreamer &Streamer) const
const Module * getModule() const
StringRef getInstrProfCoverageSectionName(bool AddSegment)
Return the name of the section containing function coverage mapping data.
Definition: InstrProf.h:84
The linker may choose any COMDAT.
Definition: Comdat.h:32
MCSection * EightByteConstantSection
S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for termination.
StringRef getName() const
Definition: Comdat.cpp:22
MCSection * TLSDataSection
Section directive for Thread Local data. ELF, MachO and COFF.
bool hasPrivateLinkage() const
Definition: GlobalValue.h:414
static const GlobalValue * getComdatGVForCOFF(const GlobalValue *GV)
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
bool isWriteable() const
Definition: SectionKind.h:145
Context object for machine code objects.
Definition: MCContext.h:51
bool isMergeable4ByteCString() const
Definition: SectionKind.h:134
bool isMergeableConst32() const
Definition: SectionKind.h:143
#define F(x, y, z)
Definition: MD5.cpp:51
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:264
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:497
PointerTy getPointer() const
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:274
MCSection * StaticDtorSection
This section contains the static destructor pointer list.
MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) const override
S_ATTR_NO_DEAD_STRIP - No dead stripping.
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:81
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:79
unsigned getStubSize() const
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:270
static SectionKind getThreadData()
Definition: SectionKind.h:197
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:429
const Triple & getTargetTriple() const
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:85
MCSection * DataSection
Section directive for standard data.
bool isMergeableConst16() const
Definition: SectionKind.h:142
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const override
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:349
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:57
.hidden (ELF)
Definition: MCDirectives.h:31
bool isMetadata() const
Definition: SectionKind.h:117
No other Module may specify this COMDAT.
Definition: Comdat.h:35
Streaming machine code generation interface.
Definition: MCStreamer.h:161
static std::string ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
Parse the section specifier indicated by "Spec".
bool isWindowsGNUEnvironment() const
Definition: Triple.h:524
PointerIntPair - This class implements a pair of a pointer and small integer.
bool isText() const
Definition: SectionKind.h:119
const Comdat * getComdat() const
Definition: GlobalObject.h:92
This is an important base class in LLVM.
Definition: Constant.h:42
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:829
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...
bool hasAttribute(unsigned Value) const
bool isThreadData() const
Definition: SectionKind.h:154
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K)
bool isMergeable1ByteCString() const
Definition: SectionKind.h:132
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:817
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
uint32_t Offset
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
MCSection * getDrectveSection() const
SelectionKind getSelectionKind() const
Definition: Comdat.h:42
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:33
bool isExecuteOnly() const
Definition: SectionKind.h:121
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:232
bool isBSSLocal() const
Definition: SectionKind.h:161
bool getFunctionSections() const
Return true if functions should be emitted into their own section, corresponding to -ffunction-sectio...
bool isThreadBSS() const
Definition: SectionKind.h:153
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
StubValueTy & getGVStubEntry(MCSymbol *Sym)
static const char * getCOFFSectionNameForUniqueGlobal(SectionKind Kind)
StubValueTy & getGVStubEntry(MCSymbol *Sym)
MCSection * getStaticDtorSection(unsigned Priority, const MCSymbol *KeySym) 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...
bool isThreadLocal() const
Definition: SectionKind.h:149
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:767
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool getDataSections() const
Return true if data objects should be emitted into their own section, corresponds to -fdata-sections...
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 hasExternalLinkage() const
Definition: GlobalValue.h:401
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:187
Comdat * getComdat()
Definition: Globals.cpp:155
static SectionKind getThreadBSS()
Definition: SectionKind.h:196
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:204
virtual void EmitLabel(MCSymbol *Symbol)
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:293
static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, const MCSection &Section)
Module.h This file contains the declarations for the Module class.
static SectionKind getMetadata()
Definition: SectionKind.h:179
static const Comdat * getELFComdat(const GlobalValue *GV)
The linker will choose the largest COMDAT.
Definition: Comdat.h:34
bool getUniqueSectionNames() const
bool isMergeableConst8() const
Definition: SectionKind.h:141
static MCSectionELF * selectELFSectionForGlobal(MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, unsigned *NextUniqueID)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Pass this value as the UniqueID during section creation to get the generic section with the given nam...
Definition: MCContext.h:324
bool isData() const
Definition: SectionKind.h:166
const GlobalObject * getBaseObject() const
Definition: GlobalValue.h:517
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
static StringRef getSectionPrefixForGlobal(SectionKind Kind)
Return the section prefix name used by options FunctionsSections and DataSections.
MCSection * FourByteConstantSection
MachineModuleInfoELF - This is a MachineModuleInfoImpl implementation for ELF targets.
const MCExpr * lowerRelativeReference(const GlobalValue *LHS, const GlobalValue *RHS, const TargetMachine &TM) const override
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:199
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:405
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:188
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:108
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
bool isWindowsCygwinEnvironment() const
Definition: Triple.h:520
static int getSelectionForCOFF(const GlobalValue *GV)
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:270
virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value)
Emit an ELF .size directive.
Definition: MCStreamer.cpp:800
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:30
const char SectionName[]
Definition: AMDGPUPTNote.h:24
op_range operands() const
Definition: Metadata.h:1032
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
void emitModuleFlags(MCStreamer &Streamer, ArrayRef< Module::ModuleFlagEntry > ModuleFlags, const TargetMachine &TM) const override
Emit Obj-C garbage collection and linker options.
bool isCommon() const
Definition: SectionKind.h:164
bool hasLocalLinkage() const
Definition: GlobalValue.h:415
const unsigned Kind
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...
MCSection * TLSBSSSection
Section directive for Thread Local uninitialized data.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int64_t getConstant() const
Definition: MCValue.h:46
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:463
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:341
unsigned getTypeAndAttributes() const
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:537
bool isMergeableConst4() const
Definition: SectionKind.h:140
bool isKnownWindowsMSVCEnvironment() const
Checks if the environment is MSVC.
Definition: Triple.h:508
bool isWindowsItaniumEnvironment() const
Definition: Triple.h:516
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...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Primary interface to the complete machine description for the target machine.
MCSection * BSSSection
Section that is default initialized to zero.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
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...
void emitModuleFlags(MCStreamer &Streamer, ArrayRef< Module::ModuleFlagEntry > ModuleFlags, const TargetMachine &TM) const override
Emit the module flags that specify the garbage collection information.
S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for initialization.
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:608
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:479
Root of the metadata hierarchy.
Definition: Metadata.h:55
static SectionKind getReadOnly()
Definition: SectionKind.h:182
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...
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:93
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149
bool isReadOnly() const
Definition: SectionKind.h:123
This class contains meta information specific to a module.
This file describes how to lower LLVM code to machine code.