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 MCSymbol
*Sym
, const MCValue
&MV
, int64_t Offset
,
1112 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
);
1169 if (!StubSym
.getPointer()) {
1170 bool IsIndirectLocal
= Sym
->isDefined() && !Sym
->isExternal();
1171 // With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
1172 StubSym
= MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol
*>(Sym
),
1176 const MCExpr
*BSymExpr
=
1177 MCSymbolRefExpr::create(BaseSym
, MCSymbolRefExpr::VK_None
, Ctx
);
1179 MCSymbolRefExpr::create(Stub
, MCSymbolRefExpr::VK_None
, Ctx
);
1182 return MCBinaryExpr::createSub(LHS
, BSymExpr
, Ctx
);
1185 MCBinaryExpr::createAdd(BSymExpr
, MCConstantExpr::create(Offset
, Ctx
), Ctx
);
1186 return MCBinaryExpr::createSub(LHS
, RHS
, Ctx
);
1189 static bool canUsePrivateLabel(const MCAsmInfo
&AsmInfo
,
1190 const MCSection
&Section
) {
1191 if (!AsmInfo
.isSectionAtomizableBySymbols(Section
))
1194 // If it is not dead stripped, it is safe to use private labels.
1195 const MCSectionMachO
&SMO
= cast
<MCSectionMachO
>(Section
);
1196 if (SMO
.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP
))
1202 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1203 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1204 const TargetMachine
&TM
) const {
1205 bool CannotUsePrivateLabel
= true;
1206 if (auto *GO
= GV
->getBaseObject()) {
1207 SectionKind GOKind
= TargetLoweringObjectFile::getKindForGlobal(GO
, TM
);
1208 const MCSection
*TheSection
= SectionForGlobal(GO
, GOKind
, TM
);
1209 CannotUsePrivateLabel
=
1210 !canUsePrivateLabel(*TM
.getMCAsmInfo(), *TheSection
);
1212 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1215 //===----------------------------------------------------------------------===//
1217 //===----------------------------------------------------------------------===//
1220 getCOFFSectionFlags(SectionKind K
, const TargetMachine
&TM
) {
1222 bool isThumb
= TM
.getTargetTriple().getArch() == Triple::thumb
;
1226 COFF::IMAGE_SCN_MEM_DISCARDABLE
;
1227 else if (K
.isText())
1229 COFF::IMAGE_SCN_MEM_EXECUTE
|
1230 COFF::IMAGE_SCN_MEM_READ
|
1231 COFF::IMAGE_SCN_CNT_CODE
|
1232 (isThumb
? COFF::IMAGE_SCN_MEM_16BIT
: (COFF::SectionCharacteristics
)0);
1235 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
1236 COFF::IMAGE_SCN_MEM_READ
|
1237 COFF::IMAGE_SCN_MEM_WRITE
;
1238 else if (K
.isThreadLocal())
1240 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1241 COFF::IMAGE_SCN_MEM_READ
|
1242 COFF::IMAGE_SCN_MEM_WRITE
;
1243 else if (K
.isReadOnly() || K
.isReadOnlyWithRel())
1245 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1246 COFF::IMAGE_SCN_MEM_READ
;
1247 else if (K
.isWriteable())
1249 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1250 COFF::IMAGE_SCN_MEM_READ
|
1251 COFF::IMAGE_SCN_MEM_WRITE
;
1256 static const GlobalValue
*getComdatGVForCOFF(const GlobalValue
*GV
) {
1257 const Comdat
*C
= GV
->getComdat();
1258 assert(C
&& "expected GV to have a Comdat!");
1260 StringRef ComdatGVName
= C
->getName();
1261 const GlobalValue
*ComdatGV
= GV
->getParent()->getNamedValue(ComdatGVName
);
1263 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1264 "' does not exist.");
1266 if (ComdatGV
->getComdat() != C
)
1267 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1268 "' is not a key for its COMDAT.");
1273 static int getSelectionForCOFF(const GlobalValue
*GV
) {
1274 if (const Comdat
*C
= GV
->getComdat()) {
1275 const GlobalValue
*ComdatKey
= getComdatGVForCOFF(GV
);
1276 if (const auto *GA
= dyn_cast
<GlobalAlias
>(ComdatKey
))
1277 ComdatKey
= GA
->getBaseObject();
1278 if (ComdatKey
== GV
) {
1279 switch (C
->getSelectionKind()) {
1281 return COFF::IMAGE_COMDAT_SELECT_ANY
;
1282 case Comdat::ExactMatch
:
1283 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH
;
1284 case Comdat::Largest
:
1285 return COFF::IMAGE_COMDAT_SELECT_LARGEST
;
1286 case Comdat::NoDuplicates
:
1287 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1288 case Comdat::SameSize
:
1289 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE
;
1292 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
1298 MCSection
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1299 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1301 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1302 StringRef Name
= GO
->getSection();
1303 StringRef COMDATSymName
= "";
1304 if (GO
->hasComdat()) {
1305 Selection
= getSelectionForCOFF(GO
);
1306 const GlobalValue
*ComdatGV
;
1307 if (Selection
== COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1308 ComdatGV
= getComdatGVForCOFF(GO
);
1312 if (!ComdatGV
->hasPrivateLinkage()) {
1313 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1314 COMDATSymName
= Sym
->getName();
1315 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1321 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, COMDATSymName
,
1325 static StringRef
getCOFFSectionNameForUniqueGlobal(SectionKind Kind
) {
1330 if (Kind
.isThreadLocal())
1332 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1337 MCSection
*TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1338 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1339 // If we have -ffunction-sections then we should emit the global value to a
1340 // uniqued section specifically for it.
1341 bool EmitUniquedSection
;
1343 EmitUniquedSection
= TM
.getFunctionSections();
1345 EmitUniquedSection
= TM
.getDataSections();
1347 if ((EmitUniquedSection
&& !Kind
.isCommon()) || GO
->hasComdat()) {
1348 SmallString
<256> Name
= getCOFFSectionNameForUniqueGlobal(Kind
);
1350 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1352 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1353 int Selection
= getSelectionForCOFF(GO
);
1355 Selection
= COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1356 const GlobalValue
*ComdatGV
;
1357 if (GO
->hasComdat())
1358 ComdatGV
= getComdatGVForCOFF(GO
);
1362 unsigned UniqueID
= MCContext::GenericSectionID
;
1363 if (EmitUniquedSection
)
1364 UniqueID
= NextUniqueID
++;
1366 if (!ComdatGV
->hasPrivateLinkage()) {
1367 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1368 StringRef COMDATSymName
= Sym
->getName();
1370 // Append "$symbol" to the section name *before* IR-level mangling is
1371 // applied when targetting mingw. This is what GCC does, and the ld.bfd
1372 // COFF linker will not properly handle comdats otherwise.
1373 if (getTargetTriple().isWindowsGNUEnvironment())
1374 raw_svector_ostream(Name
) << '$' << ComdatGV
->getName();
1376 return getContext().getCOFFSection(Name
, Characteristics
, Kind
,
1377 COMDATSymName
, Selection
, UniqueID
);
1379 SmallString
<256> TmpData
;
1380 getMangler().getNameWithPrefix(TmpData
, GO
, /*CannotUsePrivateLabel=*/true);
1381 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, TmpData
,
1382 Selection
, UniqueID
);
1389 if (Kind
.isThreadLocal())
1390 return TLSDataSection
;
1392 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1393 return ReadOnlySection
;
1395 // Note: we claim that common symbols are put in BSSSection, but they are
1396 // really emitted with the magic .comm directive, which creates a symbol table
1397 // entry but not a section.
1398 if (Kind
.isBSS() || Kind
.isCommon())
1404 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1405 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1406 const TargetMachine
&TM
) const {
1407 bool CannotUsePrivateLabel
= false;
1408 if (GV
->hasPrivateLinkage() &&
1409 ((isa
<Function
>(GV
) && TM
.getFunctionSections()) ||
1410 (isa
<GlobalVariable
>(GV
) && TM
.getDataSections())))
1411 CannotUsePrivateLabel
= true;
1413 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1416 MCSection
*TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1417 const Function
&F
, const TargetMachine
&TM
) const {
1418 // If the function can be removed, produce a unique section so that
1419 // the table doesn't prevent the removal.
1420 const Comdat
*C
= F
.getComdat();
1421 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
1422 if (!EmitUniqueSection
)
1423 return ReadOnlySection
;
1425 // FIXME: we should produce a symbol for F instead.
1426 if (F
.hasPrivateLinkage())
1427 return ReadOnlySection
;
1429 MCSymbol
*Sym
= TM
.getSymbol(&F
);
1430 StringRef COMDATSymName
= Sym
->getName();
1432 SectionKind Kind
= SectionKind::getReadOnly();
1433 StringRef SecName
= getCOFFSectionNameForUniqueGlobal(Kind
);
1434 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1435 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1436 unsigned UniqueID
= NextUniqueID
++;
1438 return getContext().getCOFFSection(
1439 SecName
, Characteristics
, Kind
, COMDATSymName
,
1440 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
, UniqueID
);
1443 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer
&Streamer
,
1445 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1446 // Emit the linker options to the linker .drectve section. According to the
1447 // spec, this section is a space-separated string containing flags for
1449 MCSection
*Sec
= getDrectveSection();
1450 Streamer
.SwitchSection(Sec
);
1451 for (const auto &Option
: LinkerOptions
->operands()) {
1452 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands()) {
1453 // Lead with a space for consistency with our dllexport implementation.
1454 std::string
Directive(" ");
1455 Directive
.append(cast
<MDString
>(Piece
)->getString());
1456 Streamer
.EmitBytes(Directive
);
1461 unsigned Version
= 0;
1465 GetObjCImageInfo(M
, Version
, Flags
, Section
);
1466 if (Section
.empty())
1469 auto &C
= getContext();
1470 auto *S
= C
.getCOFFSection(
1471 Section
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1472 SectionKind::getReadOnly());
1473 Streamer
.SwitchSection(S
);
1474 Streamer
.EmitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1475 Streamer
.EmitIntValue(Version
, 4);
1476 Streamer
.EmitIntValue(Flags
, 4);
1477 Streamer
.AddBlankLine();
1480 void TargetLoweringObjectFileCOFF::Initialize(MCContext
&Ctx
,
1481 const TargetMachine
&TM
) {
1482 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1483 const Triple
&T
= TM
.getTargetTriple();
1484 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1486 Ctx
.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1487 COFF::IMAGE_SCN_MEM_READ
,
1488 SectionKind::getReadOnly());
1490 Ctx
.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1491 COFF::IMAGE_SCN_MEM_READ
,
1492 SectionKind::getReadOnly());
1494 StaticCtorSection
= Ctx
.getCOFFSection(
1495 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1496 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1497 SectionKind::getData());
1498 StaticDtorSection
= Ctx
.getCOFFSection(
1499 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1500 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1501 SectionKind::getData());
1505 static MCSectionCOFF
*getCOFFStaticStructorSection(MCContext
&Ctx
,
1506 const Triple
&T
, bool IsCtor
,
1508 const MCSymbol
*KeySym
,
1509 MCSectionCOFF
*Default
) {
1510 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1511 // If the priority is the default, use .CRT$XCU, possibly associative.
1512 if (Priority
== 65535)
1513 return Ctx
.getAssociativeCOFFSection(Default
, KeySym
, 0);
1515 // Otherwise, we need to compute a new section name. Low priorities should
1516 // run earlier. The linker will sort sections ASCII-betically, and we need a
1517 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1518 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1519 // low priorities need to sort before 'L', since the CRT uses that
1520 // internally, so we use ".CRT$XCA00001" for them.
1521 SmallString
<24> Name
;
1522 raw_svector_ostream
OS(Name
);
1523 OS
<< ".CRT$X" << (IsCtor
? "C" : "T") <<
1524 (Priority
< 200 ? 'A' : 'T') << format("%05u", Priority
);
1525 MCSectionCOFF
*Sec
= Ctx
.getCOFFSection(
1526 Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1527 SectionKind::getReadOnly());
1528 return Ctx
.getAssociativeCOFFSection(Sec
, KeySym
, 0);
1531 std::string Name
= IsCtor
? ".ctors" : ".dtors";
1532 if (Priority
!= 65535)
1533 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1535 return Ctx
.getAssociativeCOFFSection(
1536 Ctx
.getCOFFSection(Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1537 COFF::IMAGE_SCN_MEM_READ
|
1538 COFF::IMAGE_SCN_MEM_WRITE
,
1539 SectionKind::getData()),
1543 MCSection
*TargetLoweringObjectFileCOFF::getStaticCtorSection(
1544 unsigned Priority
, const MCSymbol
*KeySym
) const {
1545 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1547 cast
<MCSectionCOFF
>(StaticCtorSection
));
1550 MCSection
*TargetLoweringObjectFileCOFF::getStaticDtorSection(
1551 unsigned Priority
, const MCSymbol
*KeySym
) const {
1552 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1554 cast
<MCSectionCOFF
>(StaticDtorSection
));
1557 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1558 raw_ostream
&OS
, const GlobalValue
*GV
) const {
1559 emitLinkerFlagsForGlobalCOFF(OS
, GV
, getTargetTriple(), getMangler());
1562 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1563 raw_ostream
&OS
, const GlobalValue
*GV
) const {
1564 emitLinkerFlagsForUsedCOFF(OS
, GV
, getTargetTriple(), getMangler());
1567 const MCExpr
*TargetLoweringObjectFileCOFF::lowerRelativeReference(
1568 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1569 const TargetMachine
&TM
) const {
1570 const Triple
&T
= TM
.getTargetTriple();
1571 if (T
.isOSCygMing())
1574 // Our symbols should exist in address space zero, cowardly no-op if
1576 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1577 RHS
->getType()->getPointerAddressSpace() != 0)
1580 // Both ptrtoint instructions must wrap global objects:
1581 // - Only global variables are eligible for image relative relocations.
1582 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1583 // We expect __ImageBase to be a global variable without a section, externally
1586 // It should look something like this: @__ImageBase = external constant i8
1587 if (!isa
<GlobalObject
>(LHS
) || !isa
<GlobalVariable
>(RHS
) ||
1588 LHS
->isThreadLocal() || RHS
->isThreadLocal() ||
1589 RHS
->getName() != "__ImageBase" || !RHS
->hasExternalLinkage() ||
1590 cast
<GlobalVariable
>(RHS
)->hasInitializer() || RHS
->hasSection())
1593 return MCSymbolRefExpr::create(TM
.getSymbol(LHS
),
1594 MCSymbolRefExpr::VK_COFF_IMGREL32
,
1598 static std::string
APIntToHexString(const APInt
&AI
) {
1599 unsigned Width
= (AI
.getBitWidth() / 8) * 2;
1600 std::string HexString
= utohexstr(AI
.getLimitedValue(), /*LowerCase=*/true);
1601 unsigned Size
= HexString
.size();
1602 assert(Width
>= Size
&& "hex string is too large!");
1603 HexString
.insert(HexString
.begin(), Width
- Size
, '0');
1608 static std::string
scalarConstantToHexString(const Constant
*C
) {
1609 Type
*Ty
= C
->getType();
1610 if (isa
<UndefValue
>(C
)) {
1611 return APIntToHexString(APInt::getNullValue(Ty
->getPrimitiveSizeInBits()));
1612 } else if (const auto *CFP
= dyn_cast
<ConstantFP
>(C
)) {
1613 return APIntToHexString(CFP
->getValueAPF().bitcastToAPInt());
1614 } else if (const auto *CI
= dyn_cast
<ConstantInt
>(C
)) {
1615 return APIntToHexString(CI
->getValue());
1617 unsigned NumElements
;
1618 if (isa
<VectorType
>(Ty
))
1619 NumElements
= Ty
->getVectorNumElements();
1621 NumElements
= Ty
->getArrayNumElements();
1622 std::string HexString
;
1623 for (int I
= NumElements
- 1, E
= -1; I
!= E
; --I
)
1624 HexString
+= scalarConstantToHexString(C
->getAggregateElement(I
));
1629 MCSection
*TargetLoweringObjectFileCOFF::getSectionForConstant(
1630 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1631 unsigned &Align
) const {
1632 if (Kind
.isMergeableConst() && C
&&
1633 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1634 // This creates comdat sections with the given symbol name, but unless
1635 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1636 // will be created with a null storage class, which makes GNU binutils
1638 const unsigned Characteristics
= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1639 COFF::IMAGE_SCN_MEM_READ
|
1640 COFF::IMAGE_SCN_LNK_COMDAT
;
1641 std::string COMDATSymName
;
1642 if (Kind
.isMergeableConst4()) {
1644 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1647 } else if (Kind
.isMergeableConst8()) {
1649 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1652 } else if (Kind
.isMergeableConst16()) {
1653 // FIXME: These may not be appropriate for non-x86 architectures.
1655 COMDATSymName
= "__xmm@" + scalarConstantToHexString(C
);
1658 } else if (Kind
.isMergeableConst32()) {
1660 COMDATSymName
= "__ymm@" + scalarConstantToHexString(C
);
1665 if (!COMDATSymName
.empty())
1666 return getContext().getCOFFSection(".rdata", Characteristics
, Kind
,
1668 COFF::IMAGE_COMDAT_SELECT_ANY
);
1671 return TargetLoweringObjectFile::getSectionForConstant(DL
, Kind
, C
, Align
);
1675 //===----------------------------------------------------------------------===//
1677 //===----------------------------------------------------------------------===//
1679 static const Comdat
*getWasmComdat(const GlobalValue
*GV
) {
1680 const Comdat
*C
= GV
->getComdat();
1684 if (C
->getSelectionKind() != Comdat::Any
)
1685 report_fatal_error("WebAssembly COMDATs only support "
1686 "SelectionKind::Any, '" + C
->getName() + "' cannot be "
1692 static SectionKind
getWasmKindForNamedSection(StringRef Name
, SectionKind K
) {
1693 // If we're told we have function data, then use that.
1695 return SectionKind::getText();
1697 // Otherwise, ignore whatever section type the generic impl detected and use
1698 // a plain data section.
1699 return SectionKind::getData();
1702 MCSection
*TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1703 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1704 // We don't support explict section names for functions in the wasm object
1705 // format. Each function has to be in its own unique section.
1706 if (isa
<Function
>(GO
)) {
1707 return SelectSectionForGlobal(GO
, Kind
, TM
);
1710 StringRef Name
= GO
->getSection();
1712 Kind
= getWasmKindForNamedSection(Name
, Kind
);
1714 StringRef Group
= "";
1715 if (const Comdat
*C
= getWasmComdat(GO
)) {
1716 Group
= C
->getName();
1719 MCSectionWasm
* Section
=
1720 getContext().getWasmSection(Name
, Kind
, Group
,
1721 MCContext::GenericSectionID
);
1726 static MCSectionWasm
*selectWasmSectionForGlobal(
1727 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
1728 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned *NextUniqueID
) {
1729 StringRef Group
= "";
1730 if (const Comdat
*C
= getWasmComdat(GO
)) {
1731 Group
= C
->getName();
1734 bool UniqueSectionNames
= TM
.getUniqueSectionNames();
1735 SmallString
<128> Name
= getSectionPrefixForGlobal(Kind
);
1737 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
1738 const auto &OptionalPrefix
= F
->getSectionPrefix();
1740 Name
+= *OptionalPrefix
;
1743 if (EmitUniqueSection
&& UniqueSectionNames
) {
1744 Name
.push_back('.');
1745 TM
.getNameWithPrefix(Name
, GO
, Mang
, true);
1747 unsigned UniqueID
= MCContext::GenericSectionID
;
1748 if (EmitUniqueSection
&& !UniqueSectionNames
) {
1749 UniqueID
= *NextUniqueID
;
1753 return Ctx
.getWasmSection(Name
, Kind
, Group
, UniqueID
);
1756 MCSection
*TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1757 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1759 if (Kind
.isCommon())
1760 report_fatal_error("mergable sections not supported yet on wasm");
1762 // If we have -ffunction-section or -fdata-section then we should emit the
1763 // global value to a uniqued section specifically for it.
1764 bool EmitUniqueSection
= false;
1766 EmitUniqueSection
= TM
.getFunctionSections();
1768 EmitUniqueSection
= TM
.getDataSections();
1769 EmitUniqueSection
|= GO
->hasComdat();
1771 return selectWasmSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
1772 EmitUniqueSection
, &NextUniqueID
);
1775 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1776 bool UsesLabelDifference
, const Function
&F
) const {
1777 // We can always create relative relocations, so use another section
1778 // that can be marked non-executable.
1782 const MCExpr
*TargetLoweringObjectFileWasm::lowerRelativeReference(
1783 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1784 const TargetMachine
&TM
) const {
1785 // We may only use a PLT-relative relocation to refer to unnamed_addr
1787 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
1790 // Basic sanity checks.
1791 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1792 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
1793 RHS
->isThreadLocal())
1796 return MCBinaryExpr::createSub(
1797 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), MCSymbolRefExpr::VK_None
,
1799 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
1802 void TargetLoweringObjectFileWasm::InitializeWasm() {
1804 getContext().getWasmSection(".init_array", SectionKind::getData());
1806 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
1807 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
1808 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
1811 MCSection
*TargetLoweringObjectFileWasm::getStaticCtorSection(
1812 unsigned Priority
, const MCSymbol
*KeySym
) const {
1813 return Priority
== UINT16_MAX
?
1815 getContext().getWasmSection(".init_array." + utostr(Priority
),
1816 SectionKind::getData());
1819 MCSection
*TargetLoweringObjectFileWasm::getStaticDtorSection(
1820 unsigned Priority
, const MCSymbol
*KeySym
) const {
1821 llvm_unreachable("@llvm.global_dtors should have been lowered already");
1825 //===----------------------------------------------------------------------===//
1827 //===----------------------------------------------------------------------===//
1828 MCSection
*TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
1829 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1830 report_fatal_error("XCOFF explicit sections not yet implemented.");
1833 MCSection
*TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
1834 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1835 assert(!TM
.getFunctionSections() && !TM
.getDataSections() &&
1836 "XCOFF unique sections not yet implemented.");
1838 // Common symbols go into a csect with matching name which will get mapped
1839 // into the .bss section.
1840 if (Kind
.isBSSLocal() || Kind
.isCommon()) {
1841 SmallString
<128> Name
;
1842 getNameWithPrefix(Name
, GO
, TM
);
1843 XCOFF::StorageClass SC
=
1844 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GO
);
1845 return getContext().getXCOFFSection(
1846 Name
, Kind
.isBSSLocal() ? XCOFF::XMC_BS
: XCOFF::XMC_RW
, XCOFF::XTY_CM
,
1847 SC
, Kind
, /* BeginSymbolName */ nullptr);
1853 report_fatal_error("XCOFF other section types not yet implemented.");
1856 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
1857 bool UsesLabelDifference
, const Function
&F
) const {
1858 report_fatal_error("TLOF XCOFF not yet implemented.");
1861 void TargetLoweringObjectFileXCOFF::Initialize(MCContext
&Ctx
,
1862 const TargetMachine
&TgtM
) {
1863 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
1865 PersonalityEncoding
= 0;
1869 MCSection
*TargetLoweringObjectFileXCOFF::getStaticCtorSection(
1870 unsigned Priority
, const MCSymbol
*KeySym
) const {
1871 report_fatal_error("XCOFF ctor section not yet implemented.");
1874 MCSection
*TargetLoweringObjectFileXCOFF::getStaticDtorSection(
1875 unsigned Priority
, const MCSymbol
*KeySym
) const {
1876 report_fatal_error("XCOFF dtor section not yet implemented.");
1879 const MCExpr
*TargetLoweringObjectFileXCOFF::lowerRelativeReference(
1880 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1881 const TargetMachine
&TM
) const {
1882 report_fatal_error("XCOFF not yet implemented.");
1885 XCOFF::StorageClass
TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(
1886 const GlobalObject
*GO
) {
1887 switch (GO
->getLinkage()) {
1888 case GlobalValue::InternalLinkage
:
1889 return XCOFF::C_HIDEXT
;
1890 case GlobalValue::ExternalLinkage
:
1891 case GlobalValue::CommonLinkage
:
1892 return XCOFF::C_EXT
;
1893 case GlobalValue::ExternalWeakLinkage
:
1894 return XCOFF::C_WEAKEXT
;
1897 "Unhandled linkage when mapping linkage to StorageClass.");