1 //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements classes used to handle lowerings specific to common
10 // object file formats.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/BinaryFormat/COFF.h"
21 #include "llvm/BinaryFormat/Dwarf.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/BinaryFormat/MachO.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
26 #include "llvm/IR/Comdat.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalObject.h"
33 #include "llvm/IR/GlobalValue.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Mangler.h"
36 #include "llvm/IR/Metadata.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/MC/MCContext.h"
41 #include "llvm/MC/MCExpr.h"
42 #include "llvm/MC/MCSectionCOFF.h"
43 #include "llvm/MC/MCSectionELF.h"
44 #include "llvm/MC/MCSectionMachO.h"
45 #include "llvm/MC/MCSectionWasm.h"
46 #include "llvm/MC/MCSectionXCOFF.h"
47 #include "llvm/MC/MCStreamer.h"
48 #include "llvm/MC/MCSymbol.h"
49 #include "llvm/MC/MCSymbolELF.h"
50 #include "llvm/MC/MCValue.h"
51 #include "llvm/MC/SectionKind.h"
52 #include "llvm/ProfileData/InstrProf.h"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CodeGen.h"
55 #include "llvm/Support/Format.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Target/TargetMachine.h"
63 using namespace dwarf
;
65 static void GetObjCImageInfo(Module
&M
, unsigned &Version
, unsigned &Flags
,
67 SmallVector
<Module::ModuleFlagEntry
, 8> ModuleFlags
;
68 M
.getModuleFlagsMetadata(ModuleFlags
);
70 for (const auto &MFE
: ModuleFlags
) {
71 // Ignore flags with 'Require' behaviour.
72 if (MFE
.Behavior
== Module::Require
)
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();
90 //===----------------------------------------------------------------------===//
92 //===----------------------------------------------------------------------===//
94 void TargetLoweringObjectFileELF::Initialize(MCContext
&Ctx
,
95 const TargetMachine
&TgtM
) {
96 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
99 CodeModel::Model CM
= TgtM
.getCodeModel();
101 switch (TgtM
.getTargetTriple().getArch()) {
105 case Triple::thumbeb
:
106 if (Ctx
.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM
)
108 // Fallthrough if not using EHABI
112 PersonalityEncoding
= isPositionIndependent()
113 ? dwarf::DW_EH_PE_indirect
|
114 dwarf::DW_EH_PE_pcrel
|
115 dwarf::DW_EH_PE_sdata4
116 : dwarf::DW_EH_PE_absptr
;
117 LSDAEncoding
= isPositionIndependent()
118 ? dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
119 : dwarf::DW_EH_PE_absptr
;
120 TTypeEncoding
= isPositionIndependent()
121 ? dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
122 dwarf::DW_EH_PE_sdata4
123 : dwarf::DW_EH_PE_absptr
;
126 if (isPositionIndependent()) {
127 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
128 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
129 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
130 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
|
131 (CM
== CodeModel::Small
132 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
133 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
134 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
135 ? dwarf::DW_EH_PE_sdata8
: dwarf::DW_EH_PE_sdata4
);
137 PersonalityEncoding
=
138 (CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
139 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
140 LSDAEncoding
= (CM
== CodeModel::Small
)
141 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
142 TTypeEncoding
= (CM
== CodeModel::Small
)
143 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
146 case Triple::hexagon
:
147 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
148 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
149 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
150 if (isPositionIndependent()) {
151 PersonalityEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
152 LSDAEncoding
|= dwarf::DW_EH_PE_pcrel
;
153 TTypeEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
156 case Triple::aarch64
:
157 case Triple::aarch64_be
:
158 // The small model guarantees static code/data size < 4GB, but not where it
159 // will be in memory. Most of these could end up >2GB away so even a signed
160 // pc-relative 32-bit address is insufficient, theoretically.
161 if (isPositionIndependent()) {
162 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
163 dwarf::DW_EH_PE_sdata8
;
164 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
;
165 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
166 dwarf::DW_EH_PE_sdata8
;
168 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
169 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
170 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
174 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
175 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
176 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
181 case Triple::mips64el
:
182 // MIPS uses indirect pointer to refer personality functions and types, so
183 // that the eh_frame section can be read-only. DW.ref.personality will be
184 // generated for relocation.
185 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
;
186 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
187 // identify N64 from just a triple.
188 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
189 dwarf::DW_EH_PE_sdata4
;
190 // We don't support PC-relative LSDA references in GAS so we use the default
191 // DW_EH_PE_absptr for those.
193 // FreeBSD must be explicit about the data size and using pcrel since it's
194 // assembler/linker won't do the automatic conversion that the Linux tools
196 if (TgtM
.getTargetTriple().isOSFreeBSD()) {
197 PersonalityEncoding
|= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
198 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
202 case Triple::ppc64le
:
203 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
204 dwarf::DW_EH_PE_udata8
;
205 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata8
;
206 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
207 dwarf::DW_EH_PE_udata8
;
209 case Triple::sparcel
:
211 if (isPositionIndependent()) {
212 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
213 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
214 dwarf::DW_EH_PE_sdata4
;
215 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
216 dwarf::DW_EH_PE_sdata4
;
218 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
219 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
220 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
222 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
224 case Triple::riscv32
:
225 case Triple::riscv64
:
226 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
227 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
228 dwarf::DW_EH_PE_sdata4
;
229 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
230 dwarf::DW_EH_PE_sdata4
;
231 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
233 case Triple::sparcv9
:
234 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
235 if (isPositionIndependent()) {
236 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
237 dwarf::DW_EH_PE_sdata4
;
238 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
239 dwarf::DW_EH_PE_sdata4
;
241 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
242 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
245 case Triple::systemz
:
246 // All currently-defined code models guarantee that 4-byte PC-relative
247 // values will be in range.
248 if (isPositionIndependent()) {
249 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
250 dwarf::DW_EH_PE_sdata4
;
251 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
252 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
253 dwarf::DW_EH_PE_sdata4
;
255 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
256 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
257 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
265 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer
&Streamer
,
267 auto &C
= getContext();
269 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
270 auto *S
= C
.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS
,
273 Streamer
.SwitchSection(S
);
275 for (const auto &Operand
: LinkerOptions
->operands()) {
276 if (cast
<MDNode
>(Operand
)->getNumOperands() != 2)
277 report_fatal_error("invalid llvm.linker.options");
278 for (const auto &Option
: cast
<MDNode
>(Operand
)->operands()) {
279 Streamer
.EmitBytes(cast
<MDString
>(Option
)->getString());
280 Streamer
.EmitIntValue(0, 1);
285 if (NamedMDNode
*DependentLibraries
= M
.getNamedMetadata("llvm.dependent-libraries")) {
286 auto *S
= C
.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES
,
287 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1, "");
289 Streamer
.SwitchSection(S
);
291 for (const auto &Operand
: DependentLibraries
->operands()) {
293 cast
<MDString
>(cast
<MDNode
>(Operand
)->getOperand(0))->getString());
294 Streamer
.EmitIntValue(0, 1);
298 unsigned Version
= 0;
302 GetObjCImageInfo(M
, Version
, Flags
, Section
);
303 if (!Section
.empty()) {
304 auto *S
= C
.getELFSection(Section
, ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
305 Streamer
.SwitchSection(S
);
306 Streamer
.EmitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
307 Streamer
.EmitIntValue(Version
, 4);
308 Streamer
.EmitIntValue(Flags
, 4);
309 Streamer
.AddBlankLine();
312 SmallVector
<Module::ModuleFlagEntry
, 8> ModuleFlags
;
313 M
.getModuleFlagsMetadata(ModuleFlags
);
315 MDNode
*CFGProfile
= nullptr;
317 for (const auto &MFE
: ModuleFlags
) {
318 StringRef Key
= MFE
.Key
->getString();
319 if (Key
== "CG Profile") {
320 CFGProfile
= cast
<MDNode
>(MFE
.Val
);
328 auto GetSym
= [this](const MDOperand
&MDO
) -> MCSymbol
* {
331 auto V
= cast
<ValueAsMetadata
>(MDO
);
332 const Function
*F
= cast
<Function
>(V
->getValue());
333 return TM
->getSymbol(F
);
336 for (const auto &Edge
: CFGProfile
->operands()) {
337 MDNode
*E
= cast
<MDNode
>(Edge
);
338 const MCSymbol
*From
= GetSym(E
->getOperand(0));
339 const MCSymbol
*To
= GetSym(E
->getOperand(1));
340 // Skip null functions. This can happen if functions are dead stripped after
341 // the CGProfile pass has been run.
344 uint64_t Count
= cast
<ConstantAsMetadata
>(E
->getOperand(2))
348 Streamer
.emitCGProfileEntry(
349 MCSymbolRefExpr::create(From
, MCSymbolRefExpr::VK_None
, C
),
350 MCSymbolRefExpr::create(To
, MCSymbolRefExpr::VK_None
, C
), Count
);
354 MCSymbol
*TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
355 const GlobalValue
*GV
, const TargetMachine
&TM
,
356 MachineModuleInfo
*MMI
) const {
357 unsigned Encoding
= getPersonalityEncoding();
358 if ((Encoding
& 0x80) == DW_EH_PE_indirect
)
359 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
360 TM
.getSymbol(GV
)->getName());
361 if ((Encoding
& 0x70) == DW_EH_PE_absptr
)
362 return TM
.getSymbol(GV
);
363 report_fatal_error("We do not support this DWARF encoding yet!");
366 void TargetLoweringObjectFileELF::emitPersonalityValue(
367 MCStreamer
&Streamer
, const DataLayout
&DL
, const MCSymbol
*Sym
) const {
368 SmallString
<64> NameData("DW.ref.");
369 NameData
+= Sym
->getName();
371 cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(NameData
));
372 Streamer
.EmitSymbolAttribute(Label
, MCSA_Hidden
);
373 Streamer
.EmitSymbolAttribute(Label
, MCSA_Weak
);
374 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::SHF_GROUP
;
375 MCSection
*Sec
= getContext().getELFNamedSection(".data", Label
->getName(),
376 ELF::SHT_PROGBITS
, Flags
, 0);
377 unsigned Size
= DL
.getPointerSize();
378 Streamer
.SwitchSection(Sec
);
379 Streamer
.EmitValueToAlignment(DL
.getPointerABIAlignment(0));
380 Streamer
.EmitSymbolAttribute(Label
, MCSA_ELF_TypeObject
);
381 const MCExpr
*E
= MCConstantExpr::create(Size
, getContext());
382 Streamer
.emitELFSize(Label
, E
);
383 Streamer
.EmitLabel(Label
);
385 Streamer
.EmitSymbolValue(Sym
, Size
);
388 const MCExpr
*TargetLoweringObjectFileELF::getTTypeGlobalReference(
389 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
390 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
391 if (Encoding
& DW_EH_PE_indirect
) {
392 MachineModuleInfoELF
&ELFMMI
= MMI
->getObjFileInfo
<MachineModuleInfoELF
>();
394 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, ".DW.stub", TM
);
396 // Add information about the stub reference to ELFMMI so that the stub
397 // gets emitted by the asmprinter.
398 MachineModuleInfoImpl::StubValueTy
&StubSym
= ELFMMI
.getGVStubEntry(SSym
);
399 if (!StubSym
.getPointer()) {
400 MCSymbol
*Sym
= TM
.getSymbol(GV
);
401 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
404 return TargetLoweringObjectFile::
405 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
406 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
409 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
413 static SectionKind
getELFKindForNamedSection(StringRef Name
, SectionKind K
) {
414 // N.B.: The defaults used in here are not the same ones used in MC.
415 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
416 // both gas and MC will produce a section with no flags. Given
417 // section(".eh_frame") gcc will produce:
419 // .section .eh_frame,"a",@progbits
421 if (Name
== getInstrProfSectionName(IPSK_covmap
, Triple::ELF
,
422 /*AddSegmentInfo=*/false))
423 return SectionKind::getMetadata();
425 if (Name
.empty() || Name
[0] != '.') return K
;
427 // Default implementation based on some magic section names.
428 if (Name
== ".bss" ||
429 Name
.startswith(".bss.") ||
430 Name
.startswith(".gnu.linkonce.b.") ||
431 Name
.startswith(".llvm.linkonce.b.") ||
433 Name
.startswith(".sbss.") ||
434 Name
.startswith(".gnu.linkonce.sb.") ||
435 Name
.startswith(".llvm.linkonce.sb."))
436 return SectionKind::getBSS();
438 if (Name
== ".tdata" ||
439 Name
.startswith(".tdata.") ||
440 Name
.startswith(".gnu.linkonce.td.") ||
441 Name
.startswith(".llvm.linkonce.td."))
442 return SectionKind::getThreadData();
444 if (Name
== ".tbss" ||
445 Name
.startswith(".tbss.") ||
446 Name
.startswith(".gnu.linkonce.tb.") ||
447 Name
.startswith(".llvm.linkonce.tb."))
448 return SectionKind::getThreadBSS();
453 static unsigned getELFSectionType(StringRef Name
, SectionKind K
) {
454 // Use SHT_NOTE for section whose name starts with ".note" to allow
455 // emitting ELF notes from C variable declaration.
456 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
457 if (Name
.startswith(".note"))
458 return ELF::SHT_NOTE
;
460 if (Name
== ".init_array")
461 return ELF::SHT_INIT_ARRAY
;
463 if (Name
== ".fini_array")
464 return ELF::SHT_FINI_ARRAY
;
466 if (Name
== ".preinit_array")
467 return ELF::SHT_PREINIT_ARRAY
;
469 if (K
.isBSS() || K
.isThreadBSS())
470 return ELF::SHT_NOBITS
;
472 return ELF::SHT_PROGBITS
;
475 static unsigned getELFSectionFlags(SectionKind K
) {
479 Flags
|= ELF::SHF_ALLOC
;
482 Flags
|= ELF::SHF_EXECINSTR
;
484 if (K
.isExecuteOnly())
485 Flags
|= ELF::SHF_ARM_PURECODE
;
488 Flags
|= ELF::SHF_WRITE
;
490 if (K
.isThreadLocal())
491 Flags
|= ELF::SHF_TLS
;
493 if (K
.isMergeableCString() || K
.isMergeableConst())
494 Flags
|= ELF::SHF_MERGE
;
496 if (K
.isMergeableCString())
497 Flags
|= ELF::SHF_STRINGS
;
502 static const Comdat
*getELFComdat(const GlobalValue
*GV
) {
503 const Comdat
*C
= GV
->getComdat();
507 if (C
->getSelectionKind() != Comdat::Any
)
508 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
509 C
->getName() + "' cannot be lowered.");
514 static const MCSymbolELF
*getAssociatedSymbol(const GlobalObject
*GO
,
515 const TargetMachine
&TM
) {
516 MDNode
*MD
= GO
->getMetadata(LLVMContext::MD_associated
);
520 const MDOperand
&Op
= MD
->getOperand(0);
524 auto *VM
= dyn_cast
<ValueAsMetadata
>(Op
);
526 report_fatal_error("MD_associated operand is not ValueAsMetadata");
528 auto *OtherGV
= dyn_cast
<GlobalValue
>(VM
->getValue());
529 return OtherGV
? dyn_cast
<MCSymbolELF
>(TM
.getSymbol(OtherGV
)) : nullptr;
532 static unsigned getEntrySizeForKind(SectionKind Kind
) {
533 if (Kind
.isMergeable1ByteCString())
535 else if (Kind
.isMergeable2ByteCString())
537 else if (Kind
.isMergeable4ByteCString())
539 else if (Kind
.isMergeableConst4())
541 else if (Kind
.isMergeableConst8())
543 else if (Kind
.isMergeableConst16())
545 else if (Kind
.isMergeableConst32())
548 // We shouldn't have mergeable C strings or mergeable constants that we
549 // didn't handle above.
550 assert(!Kind
.isMergeableCString() && "unknown string width");
551 assert(!Kind
.isMergeableConst() && "unknown data width");
556 MCSection
*TargetLoweringObjectFileELF::getExplicitSectionGlobal(
557 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
558 StringRef SectionName
= GO
->getSection();
560 // Check if '#pragma clang section' name is applicable.
561 // Note that pragma directive overrides -ffunction-section, -fdata-section
562 // and so section name is exactly as user specified and not uniqued.
563 const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(GO
);
564 if (GV
&& GV
->hasImplicitSection()) {
565 auto Attrs
= GV
->getAttributes();
566 if (Attrs
.hasAttribute("bss-section") && Kind
.isBSS()) {
567 SectionName
= Attrs
.getAttribute("bss-section").getValueAsString();
568 } else if (Attrs
.hasAttribute("rodata-section") && Kind
.isReadOnly()) {
569 SectionName
= Attrs
.getAttribute("rodata-section").getValueAsString();
570 } else if (Attrs
.hasAttribute("data-section") && Kind
.isData()) {
571 SectionName
= Attrs
.getAttribute("data-section").getValueAsString();
574 const Function
*F
= dyn_cast
<Function
>(GO
);
575 if (F
&& F
->hasFnAttribute("implicit-section-name")) {
576 SectionName
= F
->getFnAttribute("implicit-section-name").getValueAsString();
579 // Infer section flags from the section name if we can.
580 Kind
= getELFKindForNamedSection(SectionName
, Kind
);
582 StringRef Group
= "";
583 unsigned Flags
= getELFSectionFlags(Kind
);
584 if (const Comdat
*C
= getELFComdat(GO
)) {
585 Group
= C
->getName();
586 Flags
|= ELF::SHF_GROUP
;
589 // A section can have at most one associated section. Put each global with
590 // MD_associated in a unique section.
591 unsigned UniqueID
= MCContext::GenericSectionID
;
592 const MCSymbolELF
*AssociatedSymbol
= getAssociatedSymbol(GO
, TM
);
593 if (AssociatedSymbol
) {
594 UniqueID
= NextUniqueID
++;
595 Flags
|= ELF::SHF_LINK_ORDER
;
598 MCSectionELF
*Section
= getContext().getELFSection(
599 SectionName
, getELFSectionType(SectionName
, Kind
), Flags
,
600 getEntrySizeForKind(Kind
), Group
, UniqueID
, AssociatedSymbol
);
601 // Make sure that we did not get some other section with incompatible sh_link.
602 // This should not be possible due to UniqueID code above.
603 assert(Section
->getAssociatedSymbol() == AssociatedSymbol
&&
604 "Associated symbol mismatch between sections");
608 /// Return the section prefix name used by options FunctionsSections and
610 static StringRef
getSectionPrefixForGlobal(SectionKind Kind
) {
613 if (Kind
.isReadOnly())
617 if (Kind
.isThreadData())
619 if (Kind
.isThreadBSS())
623 assert(Kind
.isReadOnlyWithRel() && "Unknown section kind");
624 return ".data.rel.ro";
627 static MCSectionELF
*selectELFSectionForGlobal(
628 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
629 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned Flags
,
630 unsigned *NextUniqueID
, const MCSymbolELF
*AssociatedSymbol
) {
632 StringRef Group
= "";
633 if (const Comdat
*C
= getELFComdat(GO
)) {
634 Flags
|= ELF::SHF_GROUP
;
635 Group
= C
->getName();
638 // Get the section entry size based on the kind.
639 unsigned EntrySize
= getEntrySizeForKind(Kind
);
641 SmallString
<128> Name
;
642 if (Kind
.isMergeableCString()) {
643 // We also need alignment here.
644 // FIXME: this is getting the alignment of the character, not the
645 // alignment of the global!
646 unsigned Align
= GO
->getParent()->getDataLayout().getPreferredAlignment(
647 cast
<GlobalVariable
>(GO
));
649 std::string SizeSpec
= ".rodata.str" + utostr(EntrySize
) + ".";
650 Name
= SizeSpec
+ utostr(Align
);
651 } else if (Kind
.isMergeableConst()) {
652 Name
= ".rodata.cst";
653 Name
+= utostr(EntrySize
);
655 Name
= getSectionPrefixForGlobal(Kind
);
658 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
659 const auto &OptionalPrefix
= F
->getSectionPrefix();
661 Name
+= *OptionalPrefix
;
664 unsigned UniqueID
= MCContext::GenericSectionID
;
665 if (EmitUniqueSection
) {
666 if (TM
.getUniqueSectionNames()) {
668 TM
.getNameWithPrefix(Name
, GO
, Mang
, true /*MayAlwaysUsePrivate*/);
670 UniqueID
= *NextUniqueID
;
674 // Use 0 as the unique ID for execute-only text.
675 if (Kind
.isExecuteOnly())
677 return Ctx
.getELFSection(Name
, getELFSectionType(Name
, Kind
), Flags
,
678 EntrySize
, Group
, UniqueID
, AssociatedSymbol
);
681 MCSection
*TargetLoweringObjectFileELF::SelectSectionForGlobal(
682 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
683 unsigned Flags
= getELFSectionFlags(Kind
);
685 // If we have -ffunction-section or -fdata-section then we should emit the
686 // global value to a uniqued section specifically for it.
687 bool EmitUniqueSection
= false;
688 if (!(Flags
& ELF::SHF_MERGE
) && !Kind
.isCommon()) {
690 EmitUniqueSection
= TM
.getFunctionSections();
692 EmitUniqueSection
= TM
.getDataSections();
694 EmitUniqueSection
|= GO
->hasComdat();
696 const MCSymbolELF
*AssociatedSymbol
= getAssociatedSymbol(GO
, TM
);
697 if (AssociatedSymbol
) {
698 EmitUniqueSection
= true;
699 Flags
|= ELF::SHF_LINK_ORDER
;
702 MCSectionELF
*Section
= selectELFSectionForGlobal(
703 getContext(), GO
, Kind
, getMangler(), TM
, EmitUniqueSection
, Flags
,
704 &NextUniqueID
, AssociatedSymbol
);
705 assert(Section
->getAssociatedSymbol() == AssociatedSymbol
);
709 MCSection
*TargetLoweringObjectFileELF::getSectionForJumpTable(
710 const Function
&F
, const TargetMachine
&TM
) const {
711 // If the function can be removed, produce a unique section so that
712 // the table doesn't prevent the removal.
713 const Comdat
*C
= F
.getComdat();
714 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
715 if (!EmitUniqueSection
)
716 return ReadOnlySection
;
718 return selectELFSectionForGlobal(getContext(), &F
, SectionKind::getReadOnly(),
719 getMangler(), TM
, EmitUniqueSection
,
720 ELF::SHF_ALLOC
, &NextUniqueID
,
721 /* AssociatedSymbol */ nullptr);
724 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
725 bool UsesLabelDifference
, const Function
&F
) const {
726 // We can always create relative relocations, so use another section
727 // that can be marked non-executable.
731 /// Given a mergeable constant with the specified size and relocation
732 /// information, return a section that it should be placed in.
733 MCSection
*TargetLoweringObjectFileELF::getSectionForConstant(
734 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
735 unsigned &Align
) const {
736 if (Kind
.isMergeableConst4() && MergeableConst4Section
)
737 return MergeableConst4Section
;
738 if (Kind
.isMergeableConst8() && MergeableConst8Section
)
739 return MergeableConst8Section
;
740 if (Kind
.isMergeableConst16() && MergeableConst16Section
)
741 return MergeableConst16Section
;
742 if (Kind
.isMergeableConst32() && MergeableConst32Section
)
743 return MergeableConst32Section
;
744 if (Kind
.isReadOnly())
745 return ReadOnlySection
;
747 assert(Kind
.isReadOnlyWithRel() && "Unknown section kind");
748 return DataRelROSection
;
751 static MCSectionELF
*getStaticStructorSection(MCContext
&Ctx
, bool UseInitArray
,
752 bool IsCtor
, unsigned Priority
,
753 const MCSymbol
*KeySym
) {
756 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
;
757 StringRef COMDAT
= KeySym
? KeySym
->getName() : "";
760 Flags
|= ELF::SHF_GROUP
;
764 Type
= ELF::SHT_INIT_ARRAY
;
765 Name
= ".init_array";
767 Type
= ELF::SHT_FINI_ARRAY
;
768 Name
= ".fini_array";
770 if (Priority
!= 65535) {
772 Name
+= utostr(Priority
);
775 // The default scheme is .ctor / .dtor, so we have to invert the priority
781 if (Priority
!= 65535)
782 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
783 Type
= ELF::SHT_PROGBITS
;
786 return Ctx
.getELFSection(Name
, Type
, Flags
, 0, COMDAT
);
789 MCSection
*TargetLoweringObjectFileELF::getStaticCtorSection(
790 unsigned Priority
, const MCSymbol
*KeySym
) const {
791 return getStaticStructorSection(getContext(), UseInitArray
, true, Priority
,
795 MCSection
*TargetLoweringObjectFileELF::getStaticDtorSection(
796 unsigned Priority
, const MCSymbol
*KeySym
) const {
797 return getStaticStructorSection(getContext(), UseInitArray
, false, Priority
,
801 const MCExpr
*TargetLoweringObjectFileELF::lowerRelativeReference(
802 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
803 const TargetMachine
&TM
) const {
804 // We may only use a PLT-relative relocation to refer to unnamed_addr
806 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
809 // Basic sanity checks.
810 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
811 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
812 RHS
->isThreadLocal())
815 return MCBinaryExpr::createSub(
816 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), PLTRelativeVariantKind
,
818 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
821 MCSection
*TargetLoweringObjectFileELF::getSectionForCommandLines() const {
822 // Use ".GCC.command.line" since this feature is to support clang's
823 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
825 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS
,
826 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1, "");
830 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_
) {
831 UseInitArray
= UseInitArray_
;
832 MCContext
&Ctx
= getContext();
834 StaticCtorSection
= Ctx
.getELFSection(".ctors", ELF::SHT_PROGBITS
,
835 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
837 StaticDtorSection
= Ctx
.getELFSection(".dtors", ELF::SHT_PROGBITS
,
838 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
842 StaticCtorSection
= Ctx
.getELFSection(".init_array", ELF::SHT_INIT_ARRAY
,
843 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
844 StaticDtorSection
= Ctx
.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY
,
845 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
848 //===----------------------------------------------------------------------===//
850 //===----------------------------------------------------------------------===//
852 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
853 : TargetLoweringObjectFile() {
854 SupportIndirectSymViaGOTPCRel
= true;
857 void TargetLoweringObjectFileMachO::Initialize(MCContext
&Ctx
,
858 const TargetMachine
&TM
) {
859 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
860 if (TM
.getRelocationModel() == Reloc::Static
) {
861 StaticCtorSection
= Ctx
.getMachOSection("__TEXT", "__constructor", 0,
862 SectionKind::getData());
863 StaticDtorSection
= Ctx
.getMachOSection("__TEXT", "__destructor", 0,
864 SectionKind::getData());
866 StaticCtorSection
= Ctx
.getMachOSection("__DATA", "__mod_init_func",
867 MachO::S_MOD_INIT_FUNC_POINTERS
,
868 SectionKind::getData());
869 StaticDtorSection
= Ctx
.getMachOSection("__DATA", "__mod_term_func",
870 MachO::S_MOD_TERM_FUNC_POINTERS
,
871 SectionKind::getData());
874 PersonalityEncoding
=
875 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
876 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
;
878 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
881 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer
&Streamer
,
883 // Emit the linker options if present.
884 if (auto *LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
885 for (const auto &Option
: LinkerOptions
->operands()) {
886 SmallVector
<std::string
, 4> StrOptions
;
887 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands())
888 StrOptions
.push_back(cast
<MDString
>(Piece
)->getString());
889 Streamer
.EmitLinkerOptions(StrOptions
);
893 unsigned VersionVal
= 0;
894 unsigned ImageInfoFlags
= 0;
895 StringRef SectionVal
;
897 GetObjCImageInfo(M
, VersionVal
, ImageInfoFlags
, SectionVal
);
899 // The section is mandatory. If we don't have it, then we don't have GC info.
900 if (SectionVal
.empty())
903 StringRef Segment
, Section
;
904 unsigned TAA
= 0, StubSize
= 0;
906 std::string ErrorCode
=
907 MCSectionMachO::ParseSectionSpecifier(SectionVal
, Segment
, Section
,
908 TAA
, TAAParsed
, StubSize
);
909 if (!ErrorCode
.empty())
910 // If invalid, report the error with report_fatal_error.
911 report_fatal_error("Invalid section specifier '" + Section
+ "': " +
915 MCSectionMachO
*S
= getContext().getMachOSection(
916 Segment
, Section
, TAA
, StubSize
, SectionKind::getData());
917 Streamer
.SwitchSection(S
);
918 Streamer
.EmitLabel(getContext().
919 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
920 Streamer
.EmitIntValue(VersionVal
, 4);
921 Streamer
.EmitIntValue(ImageInfoFlags
, 4);
922 Streamer
.AddBlankLine();
925 static void checkMachOComdat(const GlobalValue
*GV
) {
926 const Comdat
*C
= GV
->getComdat();
930 report_fatal_error("MachO doesn't support COMDATs, '" + C
->getName() +
931 "' cannot be lowered.");
934 MCSection
*TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
935 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
936 // Parse the section specifier and create it if valid.
937 StringRef Segment
, Section
;
938 unsigned TAA
= 0, StubSize
= 0;
941 checkMachOComdat(GO
);
943 std::string ErrorCode
=
944 MCSectionMachO::ParseSectionSpecifier(GO
->getSection(), Segment
, Section
,
945 TAA
, TAAParsed
, StubSize
);
946 if (!ErrorCode
.empty()) {
947 // If invalid, report the error with report_fatal_error.
948 report_fatal_error("Global variable '" + GO
->getName() +
949 "' has an invalid section specifier '" +
950 GO
->getSection() + "': " + ErrorCode
+ ".");
955 getContext().getMachOSection(Segment
, Section
, TAA
, StubSize
, Kind
);
957 // If TAA wasn't set by ParseSectionSpecifier() above,
958 // use the value returned by getMachOSection() as a default.
960 TAA
= S
->getTypeAndAttributes();
962 // Okay, now that we got the section, verify that the TAA & StubSize agree.
963 // If the user declared multiple globals with different section flags, we need
964 // to reject it here.
965 if (S
->getTypeAndAttributes() != TAA
|| S
->getStubSize() != StubSize
) {
966 // If invalid, report the error with report_fatal_error.
967 report_fatal_error("Global variable '" + GO
->getName() +
968 "' section type or attributes does not match previous"
969 " section specifier");
975 MCSection
*TargetLoweringObjectFileMachO::SelectSectionForGlobal(
976 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
977 checkMachOComdat(GO
);
979 // Handle thread local data.
980 if (Kind
.isThreadBSS()) return TLSBSSSection
;
981 if (Kind
.isThreadData()) return TLSDataSection
;
984 return GO
->isWeakForLinker() ? TextCoalSection
: TextSection
;
986 // If this is weak/linkonce, put this in a coalescable section, either in text
987 // or data depending on if it is writable.
988 if (GO
->isWeakForLinker()) {
989 if (Kind
.isReadOnly())
990 return ConstTextCoalSection
;
991 if (Kind
.isReadOnlyWithRel())
992 return ConstDataCoalSection
;
993 return DataCoalSection
;
996 // FIXME: Alignment check should be handled by section classifier.
997 if (Kind
.isMergeable1ByteCString() &&
998 GO
->getParent()->getDataLayout().getPreferredAlignment(
999 cast
<GlobalVariable
>(GO
)) < 32)
1000 return CStringSection
;
1002 // Do not put 16-bit arrays in the UString section if they have an
1003 // externally visible label, this runs into issues with certain linker
1005 if (Kind
.isMergeable2ByteCString() && !GO
->hasExternalLinkage() &&
1006 GO
->getParent()->getDataLayout().getPreferredAlignment(
1007 cast
<GlobalVariable
>(GO
)) < 32)
1008 return UStringSection
;
1010 // With MachO only variables whose corresponding symbol starts with 'l' or
1011 // 'L' can be merged, so we only try merging GVs with private linkage.
1012 if (GO
->hasPrivateLinkage() && Kind
.isMergeableConst()) {
1013 if (Kind
.isMergeableConst4())
1014 return FourByteConstantSection
;
1015 if (Kind
.isMergeableConst8())
1016 return EightByteConstantSection
;
1017 if (Kind
.isMergeableConst16())
1018 return SixteenByteConstantSection
;
1021 // Otherwise, if it is readonly, but not something we can specially optimize,
1022 // just drop it in .const.
1023 if (Kind
.isReadOnly())
1024 return ReadOnlySection
;
1026 // If this is marked const, put it into a const section. But if the dynamic
1027 // linker needs to write to it, put it in the data segment.
1028 if (Kind
.isReadOnlyWithRel())
1029 return ConstDataSection
;
1031 // Put zero initialized globals with strong external linkage in the
1032 // DATA, __common section with the .zerofill directive.
1033 if (Kind
.isBSSExtern())
1034 return DataCommonSection
;
1036 // Put zero initialized globals with local linkage in __DATA,__bss directive
1037 // with the .zerofill directive (aka .lcomm).
1038 if (Kind
.isBSSLocal())
1039 return DataBSSSection
;
1041 // Otherwise, just drop the variable in the normal data section.
1045 MCSection
*TargetLoweringObjectFileMachO::getSectionForConstant(
1046 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1047 unsigned &Align
) const {
1048 // If this constant requires a relocation, we have to put it in the data
1049 // segment, not in the text segment.
1050 if (Kind
.isData() || Kind
.isReadOnlyWithRel())
1051 return ConstDataSection
;
1053 if (Kind
.isMergeableConst4())
1054 return FourByteConstantSection
;
1055 if (Kind
.isMergeableConst8())
1056 return EightByteConstantSection
;
1057 if (Kind
.isMergeableConst16())
1058 return SixteenByteConstantSection
;
1059 return ReadOnlySection
; // .const
1062 const MCExpr
*TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1063 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
1064 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1065 // The mach-o version of this method defaults to returning a stub reference.
1067 if (Encoding
& DW_EH_PE_indirect
) {
1068 MachineModuleInfoMachO
&MachOMMI
=
1069 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1071 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1073 // Add information about the stub reference to MachOMMI so that the stub
1074 // gets emitted by the asmprinter.
1075 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1076 if (!StubSym
.getPointer()) {
1077 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1078 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1081 return TargetLoweringObjectFile::
1082 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
1083 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
1086 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
1090 MCSymbol
*TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1091 const GlobalValue
*GV
, const TargetMachine
&TM
,
1092 MachineModuleInfo
*MMI
) const {
1093 // The mach-o version of this method defaults to returning a stub reference.
1094 MachineModuleInfoMachO
&MachOMMI
=
1095 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1097 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1099 // Add information about the stub reference to MachOMMI so that the stub
1100 // gets emitted by the asmprinter.
1101 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1102 if (!StubSym
.getPointer()) {
1103 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1104 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1110 const MCExpr
*TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1111 const GlobalValue
*GV
, const MCSymbol
*Sym
, const MCValue
&MV
,
1112 int64_t Offset
, MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1113 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1114 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1115 // through a non_lazy_ptr stub instead. One advantage is that it allows the
1116 // computation of deltas to final external symbols. Example:
1122 // .long _extgotequiv-_delta
1124 // is transformed to:
1127 // .long L_extfoo$non_lazy_ptr-(_delta+0)
1129 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
1130 // L_extfoo$non_lazy_ptr:
1131 // .indirect_symbol _extfoo
1134 // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1135 // may point to both local (same translation unit) and global (other
1136 // translation units) symbols. Example:
1138 // .section __DATA,__pointers,non_lazy_symbol_pointers
1140 // .indirect_symbol _myGlobal
1143 // .indirect_symbol _myLocal
1146 // If the symbol is local, instead of the symbol's index, the assembler
1147 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1148 // Then the linker will notice the constant in the table and will look at the
1149 // content of the symbol.
1150 MachineModuleInfoMachO
&MachOMMI
=
1151 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1152 MCContext
&Ctx
= getContext();
1154 // The offset must consider the original displacement from the base symbol
1155 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1156 Offset
= -MV
.getConstant();
1157 const MCSymbol
*BaseSym
= &MV
.getSymB()->getSymbol();
1159 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1160 // non_lazy_ptr stubs.
1161 SmallString
<128> Name
;
1162 StringRef Suffix
= "$non_lazy_ptr";
1163 Name
+= MMI
->getModule()->getDataLayout().getPrivateGlobalPrefix();
1164 Name
+= Sym
->getName();
1166 MCSymbol
*Stub
= Ctx
.getOrCreateSymbol(Name
);
1168 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(Stub
);
1170 if (!StubSym
.getPointer())
1171 StubSym
= MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol
*>(Sym
),
1172 !GV
->hasLocalLinkage());
1174 const MCExpr
*BSymExpr
=
1175 MCSymbolRefExpr::create(BaseSym
, MCSymbolRefExpr::VK_None
, Ctx
);
1177 MCSymbolRefExpr::create(Stub
, MCSymbolRefExpr::VK_None
, Ctx
);
1180 return MCBinaryExpr::createSub(LHS
, BSymExpr
, Ctx
);
1183 MCBinaryExpr::createAdd(BSymExpr
, MCConstantExpr::create(Offset
, Ctx
), Ctx
);
1184 return MCBinaryExpr::createSub(LHS
, RHS
, Ctx
);
1187 static bool canUsePrivateLabel(const MCAsmInfo
&AsmInfo
,
1188 const MCSection
&Section
) {
1189 if (!AsmInfo
.isSectionAtomizableBySymbols(Section
))
1192 // If it is not dead stripped, it is safe to use private labels.
1193 const MCSectionMachO
&SMO
= cast
<MCSectionMachO
>(Section
);
1194 if (SMO
.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP
))
1200 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1201 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1202 const TargetMachine
&TM
) const {
1203 bool CannotUsePrivateLabel
= true;
1204 if (auto *GO
= GV
->getBaseObject()) {
1205 SectionKind GOKind
= TargetLoweringObjectFile::getKindForGlobal(GO
, TM
);
1206 const MCSection
*TheSection
= SectionForGlobal(GO
, GOKind
, TM
);
1207 CannotUsePrivateLabel
=
1208 !canUsePrivateLabel(*TM
.getMCAsmInfo(), *TheSection
);
1210 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1213 //===----------------------------------------------------------------------===//
1215 //===----------------------------------------------------------------------===//
1218 getCOFFSectionFlags(SectionKind K
, const TargetMachine
&TM
) {
1220 bool isThumb
= TM
.getTargetTriple().getArch() == Triple::thumb
;
1224 COFF::IMAGE_SCN_MEM_DISCARDABLE
;
1225 else if (K
.isText())
1227 COFF::IMAGE_SCN_MEM_EXECUTE
|
1228 COFF::IMAGE_SCN_MEM_READ
|
1229 COFF::IMAGE_SCN_CNT_CODE
|
1230 (isThumb
? COFF::IMAGE_SCN_MEM_16BIT
: (COFF::SectionCharacteristics
)0);
1233 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
1234 COFF::IMAGE_SCN_MEM_READ
|
1235 COFF::IMAGE_SCN_MEM_WRITE
;
1236 else if (K
.isThreadLocal())
1238 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1239 COFF::IMAGE_SCN_MEM_READ
|
1240 COFF::IMAGE_SCN_MEM_WRITE
;
1241 else if (K
.isReadOnly() || K
.isReadOnlyWithRel())
1243 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1244 COFF::IMAGE_SCN_MEM_READ
;
1245 else if (K
.isWriteable())
1247 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1248 COFF::IMAGE_SCN_MEM_READ
|
1249 COFF::IMAGE_SCN_MEM_WRITE
;
1254 static const GlobalValue
*getComdatGVForCOFF(const GlobalValue
*GV
) {
1255 const Comdat
*C
= GV
->getComdat();
1256 assert(C
&& "expected GV to have a Comdat!");
1258 StringRef ComdatGVName
= C
->getName();
1259 const GlobalValue
*ComdatGV
= GV
->getParent()->getNamedValue(ComdatGVName
);
1261 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1262 "' does not exist.");
1264 if (ComdatGV
->getComdat() != C
)
1265 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1266 "' is not a key for its COMDAT.");
1271 static int getSelectionForCOFF(const GlobalValue
*GV
) {
1272 if (const Comdat
*C
= GV
->getComdat()) {
1273 const GlobalValue
*ComdatKey
= getComdatGVForCOFF(GV
);
1274 if (const auto *GA
= dyn_cast
<GlobalAlias
>(ComdatKey
))
1275 ComdatKey
= GA
->getBaseObject();
1276 if (ComdatKey
== GV
) {
1277 switch (C
->getSelectionKind()) {
1279 return COFF::IMAGE_COMDAT_SELECT_ANY
;
1280 case Comdat::ExactMatch
:
1281 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH
;
1282 case Comdat::Largest
:
1283 return COFF::IMAGE_COMDAT_SELECT_LARGEST
;
1284 case Comdat::NoDuplicates
:
1285 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1286 case Comdat::SameSize
:
1287 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE
;
1290 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
1296 MCSection
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1297 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1299 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1300 StringRef Name
= GO
->getSection();
1301 StringRef COMDATSymName
= "";
1302 if (GO
->hasComdat()) {
1303 Selection
= getSelectionForCOFF(GO
);
1304 const GlobalValue
*ComdatGV
;
1305 if (Selection
== COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1306 ComdatGV
= getComdatGVForCOFF(GO
);
1310 if (!ComdatGV
->hasPrivateLinkage()) {
1311 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1312 COMDATSymName
= Sym
->getName();
1313 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1319 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, COMDATSymName
,
1323 static StringRef
getCOFFSectionNameForUniqueGlobal(SectionKind Kind
) {
1328 if (Kind
.isThreadLocal())
1330 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1335 MCSection
*TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1336 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1337 // If we have -ffunction-sections then we should emit the global value to a
1338 // uniqued section specifically for it.
1339 bool EmitUniquedSection
;
1341 EmitUniquedSection
= TM
.getFunctionSections();
1343 EmitUniquedSection
= TM
.getDataSections();
1345 if ((EmitUniquedSection
&& !Kind
.isCommon()) || GO
->hasComdat()) {
1346 SmallString
<256> Name
= getCOFFSectionNameForUniqueGlobal(Kind
);
1348 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1350 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1351 int Selection
= getSelectionForCOFF(GO
);
1353 Selection
= COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1354 const GlobalValue
*ComdatGV
;
1355 if (GO
->hasComdat())
1356 ComdatGV
= getComdatGVForCOFF(GO
);
1360 unsigned UniqueID
= MCContext::GenericSectionID
;
1361 if (EmitUniquedSection
)
1362 UniqueID
= NextUniqueID
++;
1364 if (!ComdatGV
->hasPrivateLinkage()) {
1365 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1366 StringRef COMDATSymName
= Sym
->getName();
1368 // Append "$symbol" to the section name *before* IR-level mangling is
1369 // applied when targetting mingw. This is what GCC does, and the ld.bfd
1370 // COFF linker will not properly handle comdats otherwise.
1371 if (getTargetTriple().isWindowsGNUEnvironment())
1372 raw_svector_ostream(Name
) << '$' << ComdatGV
->getName();
1374 return getContext().getCOFFSection(Name
, Characteristics
, Kind
,
1375 COMDATSymName
, Selection
, UniqueID
);
1377 SmallString
<256> TmpData
;
1378 getMangler().getNameWithPrefix(TmpData
, GO
, /*CannotUsePrivateLabel=*/true);
1379 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, TmpData
,
1380 Selection
, UniqueID
);
1387 if (Kind
.isThreadLocal())
1388 return TLSDataSection
;
1390 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1391 return ReadOnlySection
;
1393 // Note: we claim that common symbols are put in BSSSection, but they are
1394 // really emitted with the magic .comm directive, which creates a symbol table
1395 // entry but not a section.
1396 if (Kind
.isBSS() || Kind
.isCommon())
1402 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1403 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1404 const TargetMachine
&TM
) const {
1405 bool CannotUsePrivateLabel
= false;
1406 if (GV
->hasPrivateLinkage() &&
1407 ((isa
<Function
>(GV
) && TM
.getFunctionSections()) ||
1408 (isa
<GlobalVariable
>(GV
) && TM
.getDataSections())))
1409 CannotUsePrivateLabel
= true;
1411 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1414 MCSection
*TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1415 const Function
&F
, const TargetMachine
&TM
) const {
1416 // If the function can be removed, produce a unique section so that
1417 // the table doesn't prevent the removal.
1418 const Comdat
*C
= F
.getComdat();
1419 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
1420 if (!EmitUniqueSection
)
1421 return ReadOnlySection
;
1423 // FIXME: we should produce a symbol for F instead.
1424 if (F
.hasPrivateLinkage())
1425 return ReadOnlySection
;
1427 MCSymbol
*Sym
= TM
.getSymbol(&F
);
1428 StringRef COMDATSymName
= Sym
->getName();
1430 SectionKind Kind
= SectionKind::getReadOnly();
1431 StringRef SecName
= getCOFFSectionNameForUniqueGlobal(Kind
);
1432 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1433 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1434 unsigned UniqueID
= NextUniqueID
++;
1436 return getContext().getCOFFSection(
1437 SecName
, Characteristics
, Kind
, COMDATSymName
,
1438 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
, UniqueID
);
1441 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer
&Streamer
,
1443 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1444 // Emit the linker options to the linker .drectve section. According to the
1445 // spec, this section is a space-separated string containing flags for
1447 MCSection
*Sec
= getDrectveSection();
1448 Streamer
.SwitchSection(Sec
);
1449 for (const auto &Option
: LinkerOptions
->operands()) {
1450 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands()) {
1451 // Lead with a space for consistency with our dllexport implementation.
1452 std::string
Directive(" ");
1453 Directive
.append(cast
<MDString
>(Piece
)->getString());
1454 Streamer
.EmitBytes(Directive
);
1459 unsigned Version
= 0;
1463 GetObjCImageInfo(M
, Version
, Flags
, Section
);
1464 if (Section
.empty())
1467 auto &C
= getContext();
1468 auto *S
= C
.getCOFFSection(
1469 Section
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1470 SectionKind::getReadOnly());
1471 Streamer
.SwitchSection(S
);
1472 Streamer
.EmitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1473 Streamer
.EmitIntValue(Version
, 4);
1474 Streamer
.EmitIntValue(Flags
, 4);
1475 Streamer
.AddBlankLine();
1478 void TargetLoweringObjectFileCOFF::Initialize(MCContext
&Ctx
,
1479 const TargetMachine
&TM
) {
1480 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1481 const Triple
&T
= TM
.getTargetTriple();
1482 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1484 Ctx
.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1485 COFF::IMAGE_SCN_MEM_READ
,
1486 SectionKind::getReadOnly());
1488 Ctx
.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1489 COFF::IMAGE_SCN_MEM_READ
,
1490 SectionKind::getReadOnly());
1492 StaticCtorSection
= Ctx
.getCOFFSection(
1493 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1494 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1495 SectionKind::getData());
1496 StaticDtorSection
= Ctx
.getCOFFSection(
1497 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1498 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1499 SectionKind::getData());
1503 static MCSectionCOFF
*getCOFFStaticStructorSection(MCContext
&Ctx
,
1504 const Triple
&T
, bool IsCtor
,
1506 const MCSymbol
*KeySym
,
1507 MCSectionCOFF
*Default
) {
1508 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1509 // If the priority is the default, use .CRT$XCU, possibly associative.
1510 if (Priority
== 65535)
1511 return Ctx
.getAssociativeCOFFSection(Default
, KeySym
, 0);
1513 // Otherwise, we need to compute a new section name. Low priorities should
1514 // run earlier. The linker will sort sections ASCII-betically, and we need a
1515 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1516 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1517 // low priorities need to sort before 'L', since the CRT uses that
1518 // internally, so we use ".CRT$XCA00001" for them.
1519 SmallString
<24> Name
;
1520 raw_svector_ostream
OS(Name
);
1521 OS
<< ".CRT$X" << (IsCtor
? "C" : "T") <<
1522 (Priority
< 200 ? 'A' : 'T') << format("%05u", Priority
);
1523 MCSectionCOFF
*Sec
= Ctx
.getCOFFSection(
1524 Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1525 SectionKind::getReadOnly());
1526 return Ctx
.getAssociativeCOFFSection(Sec
, KeySym
, 0);
1529 std::string Name
= IsCtor
? ".ctors" : ".dtors";
1530 if (Priority
!= 65535)
1531 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1533 return Ctx
.getAssociativeCOFFSection(
1534 Ctx
.getCOFFSection(Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1535 COFF::IMAGE_SCN_MEM_READ
|
1536 COFF::IMAGE_SCN_MEM_WRITE
,
1537 SectionKind::getData()),
1541 MCSection
*TargetLoweringObjectFileCOFF::getStaticCtorSection(
1542 unsigned Priority
, const MCSymbol
*KeySym
) const {
1543 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1545 cast
<MCSectionCOFF
>(StaticCtorSection
));
1548 MCSection
*TargetLoweringObjectFileCOFF::getStaticDtorSection(
1549 unsigned Priority
, const MCSymbol
*KeySym
) const {
1550 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1552 cast
<MCSectionCOFF
>(StaticDtorSection
));
1555 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1556 raw_ostream
&OS
, const GlobalValue
*GV
) const {
1557 emitLinkerFlagsForGlobalCOFF(OS
, GV
, getTargetTriple(), getMangler());
1560 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1561 raw_ostream
&OS
, const GlobalValue
*GV
) const {
1562 emitLinkerFlagsForUsedCOFF(OS
, GV
, getTargetTriple(), getMangler());
1565 const MCExpr
*TargetLoweringObjectFileCOFF::lowerRelativeReference(
1566 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1567 const TargetMachine
&TM
) const {
1568 const Triple
&T
= TM
.getTargetTriple();
1569 if (T
.isOSCygMing())
1572 // Our symbols should exist in address space zero, cowardly no-op if
1574 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1575 RHS
->getType()->getPointerAddressSpace() != 0)
1578 // Both ptrtoint instructions must wrap global objects:
1579 // - Only global variables are eligible for image relative relocations.
1580 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1581 // We expect __ImageBase to be a global variable without a section, externally
1584 // It should look something like this: @__ImageBase = external constant i8
1585 if (!isa
<GlobalObject
>(LHS
) || !isa
<GlobalVariable
>(RHS
) ||
1586 LHS
->isThreadLocal() || RHS
->isThreadLocal() ||
1587 RHS
->getName() != "__ImageBase" || !RHS
->hasExternalLinkage() ||
1588 cast
<GlobalVariable
>(RHS
)->hasInitializer() || RHS
->hasSection())
1591 return MCSymbolRefExpr::create(TM
.getSymbol(LHS
),
1592 MCSymbolRefExpr::VK_COFF_IMGREL32
,
1596 static std::string
APIntToHexString(const APInt
&AI
) {
1597 unsigned Width
= (AI
.getBitWidth() / 8) * 2;
1598 std::string HexString
= AI
.toString(16, /*Signed=*/false);
1599 transform(HexString
.begin(), HexString
.end(), HexString
.begin(), tolower
);
1600 unsigned Size
= HexString
.size();
1601 assert(Width
>= Size
&& "hex string is too large!");
1602 HexString
.insert(HexString
.begin(), Width
- Size
, '0');
1607 static std::string
scalarConstantToHexString(const Constant
*C
) {
1608 Type
*Ty
= C
->getType();
1609 if (isa
<UndefValue
>(C
)) {
1610 return APIntToHexString(APInt::getNullValue(Ty
->getPrimitiveSizeInBits()));
1611 } else if (const auto *CFP
= dyn_cast
<ConstantFP
>(C
)) {
1612 return APIntToHexString(CFP
->getValueAPF().bitcastToAPInt());
1613 } else if (const auto *CI
= dyn_cast
<ConstantInt
>(C
)) {
1614 return APIntToHexString(CI
->getValue());
1616 unsigned NumElements
;
1617 if (isa
<VectorType
>(Ty
))
1618 NumElements
= Ty
->getVectorNumElements();
1620 NumElements
= Ty
->getArrayNumElements();
1621 std::string HexString
;
1622 for (int I
= NumElements
- 1, E
= -1; I
!= E
; --I
)
1623 HexString
+= scalarConstantToHexString(C
->getAggregateElement(I
));
1628 MCSection
*TargetLoweringObjectFileCOFF::getSectionForConstant(
1629 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1630 unsigned &Align
) const {
1631 if (Kind
.isMergeableConst() && C
&&
1632 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1633 // This creates comdat sections with the given symbol name, but unless
1634 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1635 // will be created with a null storage class, which makes GNU binutils
1637 const unsigned Characteristics
= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1638 COFF::IMAGE_SCN_MEM_READ
|
1639 COFF::IMAGE_SCN_LNK_COMDAT
;
1640 std::string COMDATSymName
;
1641 if (Kind
.isMergeableConst4()) {
1643 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1646 } else if (Kind
.isMergeableConst8()) {
1648 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1651 } else if (Kind
.isMergeableConst16()) {
1652 // FIXME: These may not be appropriate for non-x86 architectures.
1654 COMDATSymName
= "__xmm@" + scalarConstantToHexString(C
);
1657 } else if (Kind
.isMergeableConst32()) {
1659 COMDATSymName
= "__ymm@" + scalarConstantToHexString(C
);
1664 if (!COMDATSymName
.empty())
1665 return getContext().getCOFFSection(".rdata", Characteristics
, Kind
,
1667 COFF::IMAGE_COMDAT_SELECT_ANY
);
1670 return TargetLoweringObjectFile::getSectionForConstant(DL
, Kind
, C
, Align
);
1674 //===----------------------------------------------------------------------===//
1676 //===----------------------------------------------------------------------===//
1678 static const Comdat
*getWasmComdat(const GlobalValue
*GV
) {
1679 const Comdat
*C
= GV
->getComdat();
1683 if (C
->getSelectionKind() != Comdat::Any
)
1684 report_fatal_error("WebAssembly COMDATs only support "
1685 "SelectionKind::Any, '" + C
->getName() + "' cannot be "
1691 static SectionKind
getWasmKindForNamedSection(StringRef Name
, SectionKind K
) {
1692 // If we're told we have function data, then use that.
1694 return SectionKind::getText();
1696 // Otherwise, ignore whatever section type the generic impl detected and use
1697 // a plain data section.
1698 return SectionKind::getData();
1701 MCSection
*TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1702 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1703 // We don't support explict section names for functions in the wasm object
1704 // format. Each function has to be in its own unique section.
1705 if (isa
<Function
>(GO
)) {
1706 return SelectSectionForGlobal(GO
, Kind
, TM
);
1709 StringRef Name
= GO
->getSection();
1711 Kind
= getWasmKindForNamedSection(Name
, Kind
);
1713 StringRef Group
= "";
1714 if (const Comdat
*C
= getWasmComdat(GO
)) {
1715 Group
= C
->getName();
1718 MCSectionWasm
* Section
=
1719 getContext().getWasmSection(Name
, Kind
, Group
,
1720 MCContext::GenericSectionID
);
1725 static MCSectionWasm
*selectWasmSectionForGlobal(
1726 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
1727 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned *NextUniqueID
) {
1728 StringRef Group
= "";
1729 if (const Comdat
*C
= getWasmComdat(GO
)) {
1730 Group
= C
->getName();
1733 bool UniqueSectionNames
= TM
.getUniqueSectionNames();
1734 SmallString
<128> Name
= getSectionPrefixForGlobal(Kind
);
1736 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
1737 const auto &OptionalPrefix
= F
->getSectionPrefix();
1739 Name
+= *OptionalPrefix
;
1742 if (EmitUniqueSection
&& UniqueSectionNames
) {
1743 Name
.push_back('.');
1744 TM
.getNameWithPrefix(Name
, GO
, Mang
, true);
1746 unsigned UniqueID
= MCContext::GenericSectionID
;
1747 if (EmitUniqueSection
&& !UniqueSectionNames
) {
1748 UniqueID
= *NextUniqueID
;
1752 return Ctx
.getWasmSection(Name
, Kind
, Group
, UniqueID
);
1755 MCSection
*TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1756 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1758 if (Kind
.isCommon())
1759 report_fatal_error("mergable sections not supported yet on wasm");
1761 // If we have -ffunction-section or -fdata-section then we should emit the
1762 // global value to a uniqued section specifically for it.
1763 bool EmitUniqueSection
= false;
1765 EmitUniqueSection
= TM
.getFunctionSections();
1767 EmitUniqueSection
= TM
.getDataSections();
1768 EmitUniqueSection
|= GO
->hasComdat();
1770 return selectWasmSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
1771 EmitUniqueSection
, &NextUniqueID
);
1774 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1775 bool UsesLabelDifference
, const Function
&F
) const {
1776 // We can always create relative relocations, so use another section
1777 // that can be marked non-executable.
1781 const MCExpr
*TargetLoweringObjectFileWasm::lowerRelativeReference(
1782 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1783 const TargetMachine
&TM
) const {
1784 // We may only use a PLT-relative relocation to refer to unnamed_addr
1786 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
1789 // Basic sanity checks.
1790 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1791 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
1792 RHS
->isThreadLocal())
1795 return MCBinaryExpr::createSub(
1796 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), MCSymbolRefExpr::VK_None
,
1798 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
1801 void TargetLoweringObjectFileWasm::InitializeWasm() {
1803 getContext().getWasmSection(".init_array", SectionKind::getData());
1805 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
1806 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
1807 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
1810 MCSection
*TargetLoweringObjectFileWasm::getStaticCtorSection(
1811 unsigned Priority
, const MCSymbol
*KeySym
) const {
1812 return Priority
== UINT16_MAX
?
1814 getContext().getWasmSection(".init_array." + utostr(Priority
),
1815 SectionKind::getData());
1818 MCSection
*TargetLoweringObjectFileWasm::getStaticDtorSection(
1819 unsigned Priority
, const MCSymbol
*KeySym
) const {
1820 llvm_unreachable("@llvm.global_dtors should have been lowered already");
1824 //===----------------------------------------------------------------------===//
1826 //===----------------------------------------------------------------------===//
1827 MCSection
*TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
1828 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1829 report_fatal_error("XCOFF explicit sections not yet implemented.");
1832 MCSection
*TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
1833 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1834 assert(!TM
.getFunctionSections() && !TM
.getDataSections() &&
1835 "XCOFF unique sections not yet implemented.");
1837 // Common symbols go into a csect with matching name which will get mapped
1838 // into the .bss section.
1839 if (Kind
.isBSSLocal() || Kind
.isCommon()) {
1840 SmallString
<128> Name
;
1841 getNameWithPrefix(Name
, GO
, TM
);
1842 XCOFF::StorageClass SC
=
1843 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GO
);
1844 return getContext().getXCOFFSection(
1845 Name
, Kind
.isBSSLocal() ? XCOFF::XMC_BS
: XCOFF::XMC_RW
, XCOFF::XTY_CM
,
1846 SC
, Kind
, /* BeginSymbolName */ nullptr);
1855 report_fatal_error("XCOFF other section types not yet implemented.");
1858 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
1859 bool UsesLabelDifference
, const Function
&F
) const {
1860 report_fatal_error("TLOF XCOFF not yet implemented.");
1863 void TargetLoweringObjectFileXCOFF::Initialize(MCContext
&Ctx
,
1864 const TargetMachine
&TgtM
) {
1865 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
1867 PersonalityEncoding
= 0;
1871 MCSection
*TargetLoweringObjectFileXCOFF::getStaticCtorSection(
1872 unsigned Priority
, const MCSymbol
*KeySym
) const {
1873 report_fatal_error("XCOFF ctor section not yet implemented.");
1876 MCSection
*TargetLoweringObjectFileXCOFF::getStaticDtorSection(
1877 unsigned Priority
, const MCSymbol
*KeySym
) const {
1878 report_fatal_error("XCOFF dtor section not yet implemented.");
1881 const MCExpr
*TargetLoweringObjectFileXCOFF::lowerRelativeReference(
1882 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1883 const TargetMachine
&TM
) const {
1884 report_fatal_error("XCOFF not yet implemented.");
1887 XCOFF::StorageClass
TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(
1888 const GlobalObject
*GO
) {
1889 switch (GO
->getLinkage()) {
1890 case GlobalValue::InternalLinkage
:
1891 return XCOFF::C_HIDEXT
;
1892 case GlobalValue::ExternalLinkage
:
1893 case GlobalValue::CommonLinkage
:
1894 return XCOFF::C_EXT
;
1895 case GlobalValue::ExternalWeakLinkage
:
1896 return XCOFF::C_WEAKEXT
;
1899 "Unhandled linkage when mapping linkage to StorageClass.");