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/BinaryFormat/Wasm.h"
25 #include "llvm/CodeGen/BasicBlockSectionUtils.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
30 #include "llvm/IR/Comdat.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/DiagnosticInfo.h"
35 #include "llvm/IR/DiagnosticPrinter.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalAlias.h"
38 #include "llvm/IR/GlobalObject.h"
39 #include "llvm/IR/GlobalValue.h"
40 #include "llvm/IR/GlobalVariable.h"
41 #include "llvm/IR/Mangler.h"
42 #include "llvm/IR/Metadata.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/PseudoProbe.h"
45 #include "llvm/IR/Type.h"
46 #include "llvm/MC/MCAsmInfo.h"
47 #include "llvm/MC/MCContext.h"
48 #include "llvm/MC/MCExpr.h"
49 #include "llvm/MC/MCSectionCOFF.h"
50 #include "llvm/MC/MCSectionELF.h"
51 #include "llvm/MC/MCSectionGOFF.h"
52 #include "llvm/MC/MCSectionMachO.h"
53 #include "llvm/MC/MCSectionWasm.h"
54 #include "llvm/MC/MCSectionXCOFF.h"
55 #include "llvm/MC/MCStreamer.h"
56 #include "llvm/MC/MCSymbol.h"
57 #include "llvm/MC/MCSymbolELF.h"
58 #include "llvm/MC/MCValue.h"
59 #include "llvm/MC/SectionKind.h"
60 #include "llvm/ProfileData/InstrProf.h"
61 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/CodeGen.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/Support/Format.h"
65 #include "llvm/Support/raw_ostream.h"
66 #include "llvm/Target/TargetMachine.h"
71 using namespace dwarf
;
73 static void GetObjCImageInfo(Module
&M
, unsigned &Version
, unsigned &Flags
,
75 SmallVector
<Module::ModuleFlagEntry
, 8> ModuleFlags
;
76 M
.getModuleFlagsMetadata(ModuleFlags
);
78 for (const auto &MFE
: ModuleFlags
) {
79 // Ignore flags with 'Require' behaviour.
80 if (MFE
.Behavior
== Module::Require
)
83 StringRef Key
= MFE
.Key
->getString();
84 if (Key
== "Objective-C Image Info Version") {
85 Version
= mdconst::extract
<ConstantInt
>(MFE
.Val
)->getZExtValue();
86 } else if (Key
== "Objective-C Garbage Collection" ||
87 Key
== "Objective-C GC Only" ||
88 Key
== "Objective-C Is Simulated" ||
89 Key
== "Objective-C Class Properties" ||
90 Key
== "Objective-C Image Swift Version") {
91 Flags
|= mdconst::extract
<ConstantInt
>(MFE
.Val
)->getZExtValue();
92 } else if (Key
== "Objective-C Image Info Section") {
93 Section
= cast
<MDString
>(MFE
.Val
)->getString();
95 // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor +
96 // "Objective-C Garbage Collection".
97 else if (Key
== "Swift ABI Version") {
98 Flags
|= (mdconst::extract
<ConstantInt
>(MFE
.Val
)->getZExtValue()) << 8;
99 } else if (Key
== "Swift Major Version") {
100 Flags
|= (mdconst::extract
<ConstantInt
>(MFE
.Val
)->getZExtValue()) << 24;
101 } else if (Key
== "Swift Minor Version") {
102 Flags
|= (mdconst::extract
<ConstantInt
>(MFE
.Val
)->getZExtValue()) << 16;
107 //===----------------------------------------------------------------------===//
109 //===----------------------------------------------------------------------===//
111 TargetLoweringObjectFileELF::TargetLoweringObjectFileELF()
112 : TargetLoweringObjectFile() {
113 SupportDSOLocalEquivalentLowering
= true;
116 void TargetLoweringObjectFileELF::Initialize(MCContext
&Ctx
,
117 const TargetMachine
&TgtM
) {
118 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
120 CodeModel::Model CM
= TgtM
.getCodeModel();
121 InitializeELF(TgtM
.Options
.UseInitArray
);
123 switch (TgtM
.getTargetTriple().getArch()) {
127 case Triple::thumbeb
:
128 if (Ctx
.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM
)
130 // Fallthrough if not using EHABI
135 PersonalityEncoding
= isPositionIndependent()
136 ? dwarf::DW_EH_PE_indirect
|
137 dwarf::DW_EH_PE_pcrel
|
138 dwarf::DW_EH_PE_sdata4
139 : dwarf::DW_EH_PE_absptr
;
140 LSDAEncoding
= isPositionIndependent()
141 ? dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
142 : dwarf::DW_EH_PE_absptr
;
143 TTypeEncoding
= isPositionIndependent()
144 ? dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
145 dwarf::DW_EH_PE_sdata4
146 : dwarf::DW_EH_PE_absptr
;
149 if (isPositionIndependent()) {
150 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
151 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
152 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
153 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
|
154 (CM
== CodeModel::Small
155 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
156 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
157 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
158 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
160 PersonalityEncoding
=
161 (CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
162 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
163 LSDAEncoding
= (CM
== CodeModel::Small
)
164 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
165 TTypeEncoding
= (CM
== CodeModel::Small
)
166 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
169 case Triple::hexagon
:
170 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
171 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
172 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
173 if (isPositionIndependent()) {
174 PersonalityEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
175 LSDAEncoding
|= dwarf::DW_EH_PE_pcrel
;
176 TTypeEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
179 case Triple::aarch64
:
180 case Triple::aarch64_be
:
181 case Triple::aarch64_32
:
182 // The small model guarantees static code/data size < 4GB, but not where it
183 // will be in memory. Most of these could end up >2GB away so even a signed
184 // pc-relative 32-bit address is insufficient, theoretically.
185 if (isPositionIndependent()) {
186 // ILP32 uses sdata4 instead of sdata8
187 if (TgtM
.getTargetTriple().getEnvironment() == Triple::GNUILP32
) {
188 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
189 dwarf::DW_EH_PE_sdata4
;
190 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
191 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
192 dwarf::DW_EH_PE_sdata4
;
194 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
195 dwarf::DW_EH_PE_sdata8
;
196 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
;
197 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
198 dwarf::DW_EH_PE_sdata8
;
201 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
202 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
203 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
207 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
208 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
209 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
214 case Triple::mips64el
:
215 // MIPS uses indirect pointer to refer personality functions and types, so
216 // that the eh_frame section can be read-only. DW.ref.personality will be
217 // generated for relocation.
218 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
;
219 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
220 // identify N64 from just a triple.
221 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
222 dwarf::DW_EH_PE_sdata4
;
223 // We don't support PC-relative LSDA references in GAS so we use the default
224 // DW_EH_PE_absptr for those.
226 // FreeBSD must be explicit about the data size and using pcrel since it's
227 // assembler/linker won't do the automatic conversion that the Linux tools
229 if (TgtM
.getTargetTriple().isOSFreeBSD()) {
230 PersonalityEncoding
|= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
231 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
235 case Triple::ppc64le
:
236 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
237 dwarf::DW_EH_PE_udata8
;
238 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata8
;
239 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
240 dwarf::DW_EH_PE_udata8
;
242 case Triple::sparcel
:
244 if (isPositionIndependent()) {
245 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
246 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
247 dwarf::DW_EH_PE_sdata4
;
248 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
249 dwarf::DW_EH_PE_sdata4
;
251 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
252 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
253 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
255 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
257 case Triple::riscv32
:
258 case Triple::riscv64
:
259 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
260 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
261 dwarf::DW_EH_PE_sdata4
;
262 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
263 dwarf::DW_EH_PE_sdata4
;
264 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
266 case Triple::sparcv9
:
267 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
268 if (isPositionIndependent()) {
269 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
270 dwarf::DW_EH_PE_sdata4
;
271 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
272 dwarf::DW_EH_PE_sdata4
;
274 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
275 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
278 case Triple::systemz
:
279 // All currently-defined code models guarantee that 4-byte PC-relative
280 // values will be in range.
281 if (isPositionIndependent()) {
282 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
283 dwarf::DW_EH_PE_sdata4
;
284 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
285 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
286 dwarf::DW_EH_PE_sdata4
;
288 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
289 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
290 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
298 void TargetLoweringObjectFileELF::getModuleMetadata(Module
&M
) {
299 SmallVector
<GlobalValue
*, 4> Vec
;
300 collectUsedGlobalVariables(M
, Vec
, false);
301 for (GlobalValue
*GV
: Vec
)
302 if (auto *GO
= dyn_cast
<GlobalObject
>(GV
))
306 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer
&Streamer
,
308 auto &C
= getContext();
310 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
311 auto *S
= C
.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS
,
314 Streamer
.SwitchSection(S
);
316 for (const auto *Operand
: LinkerOptions
->operands()) {
317 if (cast
<MDNode
>(Operand
)->getNumOperands() != 2)
318 report_fatal_error("invalid llvm.linker.options");
319 for (const auto &Option
: cast
<MDNode
>(Operand
)->operands()) {
320 Streamer
.emitBytes(cast
<MDString
>(Option
)->getString());
321 Streamer
.emitInt8(0);
326 if (NamedMDNode
*DependentLibraries
= M
.getNamedMetadata("llvm.dependent-libraries")) {
327 auto *S
= C
.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES
,
328 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1);
330 Streamer
.SwitchSection(S
);
332 for (const auto *Operand
: DependentLibraries
->operands()) {
334 cast
<MDString
>(cast
<MDNode
>(Operand
)->getOperand(0))->getString());
335 Streamer
.emitInt8(0);
339 if (NamedMDNode
*FuncInfo
= M
.getNamedMetadata(PseudoProbeDescMetadataName
)) {
340 // Emit a descriptor for every function including functions that have an
341 // available external linkage. We may not want this for imported functions
342 // that has code in another thinLTO module but we don't have a good way to
343 // tell them apart from inline functions defined in header files. Therefore
344 // we put each descriptor in a separate comdat section and rely on the
345 // linker to deduplicate.
346 for (const auto *Operand
: FuncInfo
->operands()) {
347 const auto *MD
= cast
<MDNode
>(Operand
);
348 auto *GUID
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(0));
349 auto *Hash
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(1));
350 auto *Name
= cast
<MDString
>(MD
->getOperand(2));
351 auto *S
= C
.getObjectFileInfo()->getPseudoProbeDescSection(
352 TM
->getFunctionSections() ? Name
->getString() : StringRef());
354 Streamer
.SwitchSection(S
);
355 Streamer
.emitInt64(GUID
->getZExtValue());
356 Streamer
.emitInt64(Hash
->getZExtValue());
357 Streamer
.emitULEB128IntValue(Name
->getString().size());
358 Streamer
.emitBytes(Name
->getString());
362 unsigned Version
= 0;
366 GetObjCImageInfo(M
, Version
, Flags
, Section
);
367 if (!Section
.empty()) {
368 auto *S
= C
.getELFSection(Section
, ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
369 Streamer
.SwitchSection(S
);
370 Streamer
.emitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
371 Streamer
.emitInt32(Version
);
372 Streamer
.emitInt32(Flags
);
373 Streamer
.AddBlankLine();
376 emitCGProfileMetadata(Streamer
, M
);
379 MCSymbol
*TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
380 const GlobalValue
*GV
, const TargetMachine
&TM
,
381 MachineModuleInfo
*MMI
) const {
382 unsigned Encoding
= getPersonalityEncoding();
383 if ((Encoding
& 0x80) == DW_EH_PE_indirect
)
384 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
385 TM
.getSymbol(GV
)->getName());
386 if ((Encoding
& 0x70) == DW_EH_PE_absptr
)
387 return TM
.getSymbol(GV
);
388 report_fatal_error("We do not support this DWARF encoding yet!");
391 void TargetLoweringObjectFileELF::emitPersonalityValue(
392 MCStreamer
&Streamer
, const DataLayout
&DL
, const MCSymbol
*Sym
) const {
393 SmallString
<64> NameData("DW.ref.");
394 NameData
+= Sym
->getName();
396 cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(NameData
));
397 Streamer
.emitSymbolAttribute(Label
, MCSA_Hidden
);
398 Streamer
.emitSymbolAttribute(Label
, MCSA_Weak
);
399 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::SHF_GROUP
;
400 MCSection
*Sec
= getContext().getELFNamedSection(".data", Label
->getName(),
401 ELF::SHT_PROGBITS
, Flags
, 0);
402 unsigned Size
= DL
.getPointerSize();
403 Streamer
.SwitchSection(Sec
);
404 Streamer
.emitValueToAlignment(DL
.getPointerABIAlignment(0).value());
405 Streamer
.emitSymbolAttribute(Label
, MCSA_ELF_TypeObject
);
406 const MCExpr
*E
= MCConstantExpr::create(Size
, getContext());
407 Streamer
.emitELFSize(Label
, E
);
408 Streamer
.emitLabel(Label
);
410 Streamer
.emitSymbolValue(Sym
, Size
);
413 const MCExpr
*TargetLoweringObjectFileELF::getTTypeGlobalReference(
414 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
415 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
416 if (Encoding
& DW_EH_PE_indirect
) {
417 MachineModuleInfoELF
&ELFMMI
= MMI
->getObjFileInfo
<MachineModuleInfoELF
>();
419 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, ".DW.stub", TM
);
421 // Add information about the stub reference to ELFMMI so that the stub
422 // gets emitted by the asmprinter.
423 MachineModuleInfoImpl::StubValueTy
&StubSym
= ELFMMI
.getGVStubEntry(SSym
);
424 if (!StubSym
.getPointer()) {
425 MCSymbol
*Sym
= TM
.getSymbol(GV
);
426 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
429 return TargetLoweringObjectFile::
430 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
431 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
434 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
438 static SectionKind
getELFKindForNamedSection(StringRef Name
, SectionKind K
) {
439 // N.B.: The defaults used in here are not the same ones used in MC.
440 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
441 // both gas and MC will produce a section with no flags. Given
442 // section(".eh_frame") gcc will produce:
444 // .section .eh_frame,"a",@progbits
446 if (Name
== getInstrProfSectionName(IPSK_covmap
, Triple::ELF
,
447 /*AddSegmentInfo=*/false) ||
448 Name
== getInstrProfSectionName(IPSK_covfun
, Triple::ELF
,
449 /*AddSegmentInfo=*/false) ||
450 Name
== ".llvmbc" || Name
== ".llvmcmd")
451 return SectionKind::getMetadata();
453 if (Name
.empty() || Name
[0] != '.') return K
;
455 // Default implementation based on some magic section names.
456 if (Name
== ".bss" ||
457 Name
.startswith(".bss.") ||
458 Name
.startswith(".gnu.linkonce.b.") ||
459 Name
.startswith(".llvm.linkonce.b.") ||
461 Name
.startswith(".sbss.") ||
462 Name
.startswith(".gnu.linkonce.sb.") ||
463 Name
.startswith(".llvm.linkonce.sb."))
464 return SectionKind::getBSS();
466 if (Name
== ".tdata" ||
467 Name
.startswith(".tdata.") ||
468 Name
.startswith(".gnu.linkonce.td.") ||
469 Name
.startswith(".llvm.linkonce.td."))
470 return SectionKind::getThreadData();
472 if (Name
== ".tbss" ||
473 Name
.startswith(".tbss.") ||
474 Name
.startswith(".gnu.linkonce.tb.") ||
475 Name
.startswith(".llvm.linkonce.tb."))
476 return SectionKind::getThreadBSS();
481 static unsigned getELFSectionType(StringRef Name
, SectionKind K
) {
482 // Use SHT_NOTE for section whose name starts with ".note" to allow
483 // emitting ELF notes from C variable declaration.
484 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
485 if (Name
.startswith(".note"))
486 return ELF::SHT_NOTE
;
488 if (Name
== ".init_array")
489 return ELF::SHT_INIT_ARRAY
;
491 if (Name
== ".fini_array")
492 return ELF::SHT_FINI_ARRAY
;
494 if (Name
== ".preinit_array")
495 return ELF::SHT_PREINIT_ARRAY
;
497 if (K
.isBSS() || K
.isThreadBSS())
498 return ELF::SHT_NOBITS
;
500 return ELF::SHT_PROGBITS
;
503 static unsigned getELFSectionFlags(SectionKind K
) {
507 Flags
|= ELF::SHF_ALLOC
;
510 Flags
|= ELF::SHF_EXECINSTR
;
512 if (K
.isExecuteOnly())
513 Flags
|= ELF::SHF_ARM_PURECODE
;
516 Flags
|= ELF::SHF_WRITE
;
518 if (K
.isThreadLocal())
519 Flags
|= ELF::SHF_TLS
;
521 if (K
.isMergeableCString() || K
.isMergeableConst())
522 Flags
|= ELF::SHF_MERGE
;
524 if (K
.isMergeableCString())
525 Flags
|= ELF::SHF_STRINGS
;
530 static const Comdat
*getELFComdat(const GlobalValue
*GV
) {
531 const Comdat
*C
= GV
->getComdat();
535 if (C
->getSelectionKind() != Comdat::Any
&&
536 C
->getSelectionKind() != Comdat::NoDeduplicate
)
537 report_fatal_error("ELF COMDATs only support SelectionKind::Any and "
538 "SelectionKind::NoDeduplicate, '" +
539 C
->getName() + "' cannot be lowered.");
544 static const MCSymbolELF
*getLinkedToSymbol(const GlobalObject
*GO
,
545 const TargetMachine
&TM
) {
546 MDNode
*MD
= GO
->getMetadata(LLVMContext::MD_associated
);
550 const MDOperand
&Op
= MD
->getOperand(0);
554 auto *VM
= dyn_cast
<ValueAsMetadata
>(Op
);
556 report_fatal_error("MD_associated operand is not ValueAsMetadata");
558 auto *OtherGV
= dyn_cast
<GlobalValue
>(VM
->getValue());
559 return OtherGV
? dyn_cast
<MCSymbolELF
>(TM
.getSymbol(OtherGV
)) : nullptr;
562 static unsigned getEntrySizeForKind(SectionKind Kind
) {
563 if (Kind
.isMergeable1ByteCString())
565 else if (Kind
.isMergeable2ByteCString())
567 else if (Kind
.isMergeable4ByteCString())
569 else if (Kind
.isMergeableConst4())
571 else if (Kind
.isMergeableConst8())
573 else if (Kind
.isMergeableConst16())
575 else if (Kind
.isMergeableConst32())
578 // We shouldn't have mergeable C strings or mergeable constants that we
579 // didn't handle above.
580 assert(!Kind
.isMergeableCString() && "unknown string width");
581 assert(!Kind
.isMergeableConst() && "unknown data width");
586 /// Return the section prefix name used by options FunctionsSections and
588 static StringRef
getSectionPrefixForGlobal(SectionKind Kind
) {
591 if (Kind
.isReadOnly())
595 if (Kind
.isThreadData())
597 if (Kind
.isThreadBSS())
601 if (Kind
.isReadOnlyWithRel())
602 return ".data.rel.ro";
603 llvm_unreachable("Unknown section kind");
606 static SmallString
<128>
607 getELFSectionNameForGlobal(const GlobalObject
*GO
, SectionKind Kind
,
608 Mangler
&Mang
, const TargetMachine
&TM
,
609 unsigned EntrySize
, bool UniqueSectionName
) {
610 SmallString
<128> Name
;
611 if (Kind
.isMergeableCString()) {
612 // We also need alignment here.
613 // FIXME: this is getting the alignment of the character, not the
614 // alignment of the global!
615 Align Alignment
= GO
->getParent()->getDataLayout().getPreferredAlign(
616 cast
<GlobalVariable
>(GO
));
618 std::string SizeSpec
= ".rodata.str" + utostr(EntrySize
) + ".";
619 Name
= SizeSpec
+ utostr(Alignment
.value());
620 } else if (Kind
.isMergeableConst()) {
621 Name
= ".rodata.cst";
622 Name
+= utostr(EntrySize
);
624 Name
= getSectionPrefixForGlobal(Kind
);
627 bool HasPrefix
= false;
628 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
629 if (Optional
<StringRef
> Prefix
= F
->getSectionPrefix()) {
630 raw_svector_ostream(Name
) << '.' << *Prefix
;
635 if (UniqueSectionName
) {
637 TM
.getNameWithPrefix(Name
, GO
, Mang
, /*MayAlwaysUsePrivate*/true);
638 } else if (HasPrefix
)
639 // For distinguishing between .text.${text-section-prefix}. (with trailing
640 // dot) and .text.${function-name}
646 class LoweringDiagnosticInfo
: public DiagnosticInfo
{
650 LoweringDiagnosticInfo(const Twine
&DiagMsg
,
651 DiagnosticSeverity Severity
= DS_Error
)
652 : DiagnosticInfo(DK_Lowering
, Severity
), Msg(DiagMsg
) {}
653 void print(DiagnosticPrinter
&DP
) const override
{ DP
<< Msg
; }
657 /// Calculate an appropriate unique ID for a section, and update Flags,
658 /// EntrySize and NextUniqueID where appropriate.
660 calcUniqueIDUpdateFlagsAndSize(const GlobalObject
*GO
, StringRef SectionName
,
661 SectionKind Kind
, const TargetMachine
&TM
,
662 MCContext
&Ctx
, Mangler
&Mang
, unsigned &Flags
,
663 unsigned &EntrySize
, unsigned &NextUniqueID
,
664 const bool Retain
, const bool ForceUnique
) {
665 // Increment uniqueID if we are forced to emit a unique section.
666 // This works perfectly fine with section attribute or pragma section as the
667 // sections with the same name are grouped together by the assembler.
669 return NextUniqueID
++;
671 // A section can have at most one associated section. Put each global with
672 // MD_associated in a unique section.
673 const bool Associated
= GO
->getMetadata(LLVMContext::MD_associated
);
675 Flags
|= ELF::SHF_LINK_ORDER
;
676 return NextUniqueID
++;
680 if ((Ctx
.getAsmInfo()->useIntegratedAssembler() ||
681 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
682 !TM
.getTargetTriple().isOSSolaris())
683 Flags
|= ELF::SHF_GNU_RETAIN
;
684 return NextUniqueID
++;
687 // If two symbols with differing sizes end up in the same mergeable section
688 // that section can be assigned an incorrect entry size. To avoid this we
689 // usually put symbols of the same size into distinct mergeable sections with
690 // the same name. Doing so relies on the ",unique ," assembly feature. This
691 // feature is not avalible until bintuils version 2.35
692 // (https://sourceware.org/bugzilla/show_bug.cgi?id=25380).
693 const bool SupportsUnique
= Ctx
.getAsmInfo()->useIntegratedAssembler() ||
694 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 35);
695 if (!SupportsUnique
) {
696 Flags
&= ~ELF::SHF_MERGE
;
698 return MCContext::GenericSectionID
;
701 const bool SymbolMergeable
= Flags
& ELF::SHF_MERGE
;
702 const bool SeenSectionNameBefore
=
703 Ctx
.isELFGenericMergeableSection(SectionName
);
704 // If this is the first ocurrence of this section name, treat it as the
706 if (!SymbolMergeable
&& !SeenSectionNameBefore
)
707 return MCContext::GenericSectionID
;
709 // Symbols must be placed into sections with compatible entry sizes. Generate
710 // unique sections for symbols that have not been assigned to compatible
712 const auto PreviousID
=
713 Ctx
.getELFUniqueIDForEntsize(SectionName
, Flags
, EntrySize
);
717 // If the user has specified the same section name as would be created
718 // implicitly for this symbol e.g. .rodata.str1.1, then we don't need
719 // to unique the section as the entry size for this symbol will be
720 // compatible with implicitly created sections.
721 SmallString
<128> ImplicitSectionNameStem
=
722 getELFSectionNameForGlobal(GO
, Kind
, Mang
, TM
, EntrySize
, false);
723 if (SymbolMergeable
&&
724 Ctx
.isELFImplicitMergeableSectionNamePrefix(SectionName
) &&
725 SectionName
.startswith(ImplicitSectionNameStem
))
726 return MCContext::GenericSectionID
;
728 // We have seen this section name before, but with different flags or entity
729 // size. Create a new unique ID.
730 return NextUniqueID
++;
733 static MCSection
*selectExplicitSectionGlobal(
734 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
,
735 MCContext
&Ctx
, Mangler
&Mang
, unsigned &NextUniqueID
,
736 bool Retain
, bool ForceUnique
) {
737 StringRef SectionName
= GO
->getSection();
739 // Check if '#pragma clang section' name is applicable.
740 // Note that pragma directive overrides -ffunction-section, -fdata-section
741 // and so section name is exactly as user specified and not uniqued.
742 const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(GO
);
743 if (GV
&& GV
->hasImplicitSection()) {
744 auto Attrs
= GV
->getAttributes();
745 if (Attrs
.hasAttribute("bss-section") && Kind
.isBSS()) {
746 SectionName
= Attrs
.getAttribute("bss-section").getValueAsString();
747 } else if (Attrs
.hasAttribute("rodata-section") && Kind
.isReadOnly()) {
748 SectionName
= Attrs
.getAttribute("rodata-section").getValueAsString();
749 } else if (Attrs
.hasAttribute("relro-section") && Kind
.isReadOnlyWithRel()) {
750 SectionName
= Attrs
.getAttribute("relro-section").getValueAsString();
751 } else if (Attrs
.hasAttribute("data-section") && Kind
.isData()) {
752 SectionName
= Attrs
.getAttribute("data-section").getValueAsString();
755 const Function
*F
= dyn_cast
<Function
>(GO
);
756 if (F
&& F
->hasFnAttribute("implicit-section-name")) {
757 SectionName
= F
->getFnAttribute("implicit-section-name").getValueAsString();
760 // Infer section flags from the section name if we can.
761 Kind
= getELFKindForNamedSection(SectionName
, Kind
);
763 StringRef Group
= "";
764 bool IsComdat
= false;
765 unsigned Flags
= getELFSectionFlags(Kind
);
766 if (const Comdat
*C
= getELFComdat(GO
)) {
767 Group
= C
->getName();
768 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
769 Flags
|= ELF::SHF_GROUP
;
772 unsigned EntrySize
= getEntrySizeForKind(Kind
);
773 const unsigned UniqueID
= calcUniqueIDUpdateFlagsAndSize(
774 GO
, SectionName
, Kind
, TM
, Ctx
, Mang
, Flags
, EntrySize
, NextUniqueID
,
775 Retain
, ForceUnique
);
777 const MCSymbolELF
*LinkedToSym
= getLinkedToSymbol(GO
, TM
);
778 MCSectionELF
*Section
= Ctx
.getELFSection(
779 SectionName
, getELFSectionType(SectionName
, Kind
), Flags
, EntrySize
,
780 Group
, IsComdat
, UniqueID
, LinkedToSym
);
781 // Make sure that we did not get some other section with incompatible sh_link.
782 // This should not be possible due to UniqueID code above.
783 assert(Section
->getLinkedToSymbol() == LinkedToSym
&&
784 "Associated symbol mismatch between sections");
786 if (!(Ctx
.getAsmInfo()->useIntegratedAssembler() ||
787 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 35))) {
788 // If we are using GNU as before 2.35, then this symbol might have
789 // been placed in an incompatible mergeable section. Emit an error if this
790 // is the case to avoid creating broken output.
791 if ((Section
->getFlags() & ELF::SHF_MERGE
) &&
792 (Section
->getEntrySize() != getEntrySizeForKind(Kind
)))
793 GO
->getContext().diagnose(LoweringDiagnosticInfo(
794 "Symbol '" + GO
->getName() + "' from module '" +
795 (GO
->getParent() ? GO
->getParent()->getSourceFileName() : "unknown") +
796 "' required a section with entry-size=" +
797 Twine(getEntrySizeForKind(Kind
)) + " but was placed in section '" +
798 SectionName
+ "' with entry-size=" + Twine(Section
->getEntrySize()) +
799 ": Explicit assignment by pragma or attribute of an incompatible "
800 "symbol to this section?"));
806 MCSection
*TargetLoweringObjectFileELF::getExplicitSectionGlobal(
807 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
808 return selectExplicitSectionGlobal(GO
, Kind
, TM
, getContext(), getMangler(),
809 NextUniqueID
, Used
.count(GO
),
810 /* ForceUnique = */false);
813 static MCSectionELF
*selectELFSectionForGlobal(
814 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
815 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned Flags
,
816 unsigned *NextUniqueID
, const MCSymbolELF
*AssociatedSymbol
) {
818 StringRef Group
= "";
819 bool IsComdat
= false;
820 if (const Comdat
*C
= getELFComdat(GO
)) {
821 Flags
|= ELF::SHF_GROUP
;
822 Group
= C
->getName();
823 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
826 // Get the section entry size based on the kind.
827 unsigned EntrySize
= getEntrySizeForKind(Kind
);
829 bool UniqueSectionName
= false;
830 unsigned UniqueID
= MCContext::GenericSectionID
;
831 if (EmitUniqueSection
) {
832 if (TM
.getUniqueSectionNames()) {
833 UniqueSectionName
= true;
835 UniqueID
= *NextUniqueID
;
839 SmallString
<128> Name
= getELFSectionNameForGlobal(
840 GO
, Kind
, Mang
, TM
, EntrySize
, UniqueSectionName
);
842 // Use 0 as the unique ID for execute-only text.
843 if (Kind
.isExecuteOnly())
845 return Ctx
.getELFSection(Name
, getELFSectionType(Name
, Kind
), Flags
,
846 EntrySize
, Group
, IsComdat
, UniqueID
,
850 static MCSection
*selectELFSectionForGlobal(
851 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
852 const TargetMachine
&TM
, bool Retain
, bool EmitUniqueSection
,
853 unsigned Flags
, unsigned *NextUniqueID
) {
854 const MCSymbolELF
*LinkedToSym
= getLinkedToSymbol(GO
, TM
);
856 EmitUniqueSection
= true;
857 Flags
|= ELF::SHF_LINK_ORDER
;
860 (Ctx
.getAsmInfo()->useIntegratedAssembler() ||
861 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
862 !TM
.getTargetTriple().isOSSolaris()) {
863 EmitUniqueSection
= true;
864 Flags
|= ELF::SHF_GNU_RETAIN
;
867 MCSectionELF
*Section
= selectELFSectionForGlobal(
868 Ctx
, GO
, Kind
, Mang
, TM
, EmitUniqueSection
, Flags
,
869 NextUniqueID
, LinkedToSym
);
870 assert(Section
->getLinkedToSymbol() == LinkedToSym
);
874 MCSection
*TargetLoweringObjectFileELF::SelectSectionForGlobal(
875 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
876 unsigned Flags
= getELFSectionFlags(Kind
);
878 // If we have -ffunction-section or -fdata-section then we should emit the
879 // global value to a uniqued section specifically for it.
880 bool EmitUniqueSection
= false;
881 if (!(Flags
& ELF::SHF_MERGE
) && !Kind
.isCommon()) {
883 EmitUniqueSection
= TM
.getFunctionSections();
885 EmitUniqueSection
= TM
.getDataSections();
887 EmitUniqueSection
|= GO
->hasComdat();
888 return selectELFSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
889 Used
.count(GO
), EmitUniqueSection
, Flags
,
893 MCSection
*TargetLoweringObjectFileELF::getUniqueSectionForFunction(
894 const Function
&F
, const TargetMachine
&TM
) const {
895 SectionKind Kind
= SectionKind::getText();
896 unsigned Flags
= getELFSectionFlags(Kind
);
897 // If the function's section names is pre-determined via pragma or a
898 // section attribute, call selectExplicitSectionGlobal.
899 if (F
.hasSection() || F
.hasFnAttribute("implicit-section-name"))
900 return selectExplicitSectionGlobal(
901 &F
, Kind
, TM
, getContext(), getMangler(), NextUniqueID
,
902 Used
.count(&F
), /* ForceUnique = */true);
904 return selectELFSectionForGlobal(
905 getContext(), &F
, Kind
, getMangler(), TM
, Used
.count(&F
),
906 /*EmitUniqueSection=*/true, Flags
, &NextUniqueID
);
909 MCSection
*TargetLoweringObjectFileELF::getSectionForJumpTable(
910 const Function
&F
, const TargetMachine
&TM
) const {
911 // If the function can be removed, produce a unique section so that
912 // the table doesn't prevent the removal.
913 const Comdat
*C
= F
.getComdat();
914 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
915 if (!EmitUniqueSection
)
916 return ReadOnlySection
;
918 return selectELFSectionForGlobal(getContext(), &F
, SectionKind::getReadOnly(),
919 getMangler(), TM
, EmitUniqueSection
,
920 ELF::SHF_ALLOC
, &NextUniqueID
,
921 /* AssociatedSymbol */ nullptr);
924 MCSection
*TargetLoweringObjectFileELF::getSectionForLSDA(
925 const Function
&F
, const MCSymbol
&FnSym
, const TargetMachine
&TM
) const {
926 // If neither COMDAT nor function sections, use the monolithic LSDA section.
927 // Re-use this path if LSDASection is null as in the Arm EHABI.
928 if (!LSDASection
|| (!F
.hasComdat() && !TM
.getFunctionSections()))
931 const auto *LSDA
= cast
<MCSectionELF
>(LSDASection
);
932 unsigned Flags
= LSDA
->getFlags();
933 const MCSymbolELF
*LinkedToSym
= nullptr;
935 bool IsComdat
= false;
936 if (const Comdat
*C
= getELFComdat(&F
)) {
937 Flags
|= ELF::SHF_GROUP
;
938 Group
= C
->getName();
939 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
941 // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36
942 // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER.
943 if (TM
.getFunctionSections() &&
944 (getContext().getAsmInfo()->useIntegratedAssembler() &&
945 getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) {
946 Flags
|= ELF::SHF_LINK_ORDER
;
947 LinkedToSym
= cast
<MCSymbolELF
>(&FnSym
);
950 // Append the function name as the suffix like GCC, assuming
951 // -funique-section-names applies to .gcc_except_table sections.
952 return getContext().getELFSection(
953 (TM
.getUniqueSectionNames() ? LSDA
->getName() + "." + F
.getName()
955 LSDA
->getType(), Flags
, 0, Group
, IsComdat
, MCSection::NonUniqueID
,
959 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
960 bool UsesLabelDifference
, const Function
&F
) const {
961 // We can always create relative relocations, so use another section
962 // that can be marked non-executable.
966 /// Given a mergeable constant with the specified size and relocation
967 /// information, return a section that it should be placed in.
968 MCSection
*TargetLoweringObjectFileELF::getSectionForConstant(
969 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
970 Align
&Alignment
) const {
971 if (Kind
.isMergeableConst4() && MergeableConst4Section
)
972 return MergeableConst4Section
;
973 if (Kind
.isMergeableConst8() && MergeableConst8Section
)
974 return MergeableConst8Section
;
975 if (Kind
.isMergeableConst16() && MergeableConst16Section
)
976 return MergeableConst16Section
;
977 if (Kind
.isMergeableConst32() && MergeableConst32Section
)
978 return MergeableConst32Section
;
979 if (Kind
.isReadOnly())
980 return ReadOnlySection
;
982 assert(Kind
.isReadOnlyWithRel() && "Unknown section kind");
983 return DataRelROSection
;
986 /// Returns a unique section for the given machine basic block.
987 MCSection
*TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
988 const Function
&F
, const MachineBasicBlock
&MBB
,
989 const TargetMachine
&TM
) const {
990 assert(MBB
.isBeginSection() && "Basic block does not start a section!");
991 unsigned UniqueID
= MCContext::GenericSectionID
;
993 // For cold sections use the .text.split. prefix along with the parent
994 // function name. All cold blocks for the same function go to the same
995 // section. Similarly all exception blocks are grouped by symbol name
996 // under the .text.eh prefix. For regular sections, we either use a unique
997 // name, or a unique ID for the section.
998 SmallString
<128> Name
;
999 if (MBB
.getSectionID() == MBBSectionID::ColdSectionID
) {
1000 Name
+= BBSectionsColdTextPrefix
;
1001 Name
+= MBB
.getParent()->getName();
1002 } else if (MBB
.getSectionID() == MBBSectionID::ExceptionSectionID
) {
1003 Name
+= ".text.eh.";
1004 Name
+= MBB
.getParent()->getName();
1006 Name
+= MBB
.getParent()->getSection()->getName();
1007 if (TM
.getUniqueBasicBlockSectionNames()) {
1008 if (!Name
.endswith("."))
1010 Name
+= MBB
.getSymbol()->getName();
1012 UniqueID
= NextUniqueID
++;
1016 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
;
1017 std::string GroupName
;
1018 if (F
.hasComdat()) {
1019 Flags
|= ELF::SHF_GROUP
;
1020 GroupName
= F
.getComdat()->getName().str();
1022 return getContext().getELFSection(Name
, ELF::SHT_PROGBITS
, Flags
,
1023 0 /* Entry Size */, GroupName
,
1024 F
.hasComdat(), UniqueID
, nullptr);
1027 static MCSectionELF
*getStaticStructorSection(MCContext
&Ctx
, bool UseInitArray
,
1028 bool IsCtor
, unsigned Priority
,
1029 const MCSymbol
*KeySym
) {
1032 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
;
1033 StringRef Comdat
= KeySym
? KeySym
->getName() : "";
1036 Flags
|= ELF::SHF_GROUP
;
1040 Type
= ELF::SHT_INIT_ARRAY
;
1041 Name
= ".init_array";
1043 Type
= ELF::SHT_FINI_ARRAY
;
1044 Name
= ".fini_array";
1046 if (Priority
!= 65535) {
1048 Name
+= utostr(Priority
);
1051 // The default scheme is .ctor / .dtor, so we have to invert the priority
1057 if (Priority
!= 65535)
1058 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1059 Type
= ELF::SHT_PROGBITS
;
1062 return Ctx
.getELFSection(Name
, Type
, Flags
, 0, Comdat
, /*IsComdat=*/true);
1065 MCSection
*TargetLoweringObjectFileELF::getStaticCtorSection(
1066 unsigned Priority
, const MCSymbol
*KeySym
) const {
1067 return getStaticStructorSection(getContext(), UseInitArray
, true, Priority
,
1071 MCSection
*TargetLoweringObjectFileELF::getStaticDtorSection(
1072 unsigned Priority
, const MCSymbol
*KeySym
) const {
1073 return getStaticStructorSection(getContext(), UseInitArray
, false, Priority
,
1077 const MCExpr
*TargetLoweringObjectFileELF::lowerRelativeReference(
1078 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1079 const TargetMachine
&TM
) const {
1080 // We may only use a PLT-relative relocation to refer to unnamed_addr
1082 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
1085 // Basic sanity checks.
1086 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1087 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
1088 RHS
->isThreadLocal())
1091 return MCBinaryExpr::createSub(
1092 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), PLTRelativeVariantKind
,
1094 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
1097 const MCExpr
*TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
1098 const DSOLocalEquivalent
*Equiv
, const TargetMachine
&TM
) const {
1099 assert(supportDSOLocalEquivalentLowering());
1101 const auto *GV
= Equiv
->getGlobalValue();
1103 // A PLT entry is not needed for dso_local globals.
1104 if (GV
->isDSOLocal() || GV
->isImplicitDSOLocal())
1105 return MCSymbolRefExpr::create(TM
.getSymbol(GV
), getContext());
1107 return MCSymbolRefExpr::create(TM
.getSymbol(GV
), PLTRelativeVariantKind
,
1111 MCSection
*TargetLoweringObjectFileELF::getSectionForCommandLines() const {
1112 // Use ".GCC.command.line" since this feature is to support clang's
1113 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
1115 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS
,
1116 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1);
1120 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_
) {
1121 UseInitArray
= UseInitArray_
;
1122 MCContext
&Ctx
= getContext();
1123 if (!UseInitArray
) {
1124 StaticCtorSection
= Ctx
.getELFSection(".ctors", ELF::SHT_PROGBITS
,
1125 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
1127 StaticDtorSection
= Ctx
.getELFSection(".dtors", ELF::SHT_PROGBITS
,
1128 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
1132 StaticCtorSection
= Ctx
.getELFSection(".init_array", ELF::SHT_INIT_ARRAY
,
1133 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
1134 StaticDtorSection
= Ctx
.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY
,
1135 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
1138 //===----------------------------------------------------------------------===//
1140 //===----------------------------------------------------------------------===//
1142 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
1143 : TargetLoweringObjectFile() {
1144 SupportIndirectSymViaGOTPCRel
= true;
1147 void TargetLoweringObjectFileMachO::Initialize(MCContext
&Ctx
,
1148 const TargetMachine
&TM
) {
1149 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1150 if (TM
.getRelocationModel() == Reloc::Static
) {
1151 StaticCtorSection
= Ctx
.getMachOSection("__TEXT", "__constructor", 0,
1152 SectionKind::getData());
1153 StaticDtorSection
= Ctx
.getMachOSection("__TEXT", "__destructor", 0,
1154 SectionKind::getData());
1156 StaticCtorSection
= Ctx
.getMachOSection("__DATA", "__mod_init_func",
1157 MachO::S_MOD_INIT_FUNC_POINTERS
,
1158 SectionKind::getData());
1159 StaticDtorSection
= Ctx
.getMachOSection("__DATA", "__mod_term_func",
1160 MachO::S_MOD_TERM_FUNC_POINTERS
,
1161 SectionKind::getData());
1164 PersonalityEncoding
=
1165 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
1166 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
;
1168 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
1171 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer
&Streamer
,
1173 // Emit the linker options if present.
1174 if (auto *LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1175 for (const auto *Option
: LinkerOptions
->operands()) {
1176 SmallVector
<std::string
, 4> StrOptions
;
1177 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands())
1178 StrOptions
.push_back(std::string(cast
<MDString
>(Piece
)->getString()));
1179 Streamer
.emitLinkerOptions(StrOptions
);
1183 unsigned VersionVal
= 0;
1184 unsigned ImageInfoFlags
= 0;
1185 StringRef SectionVal
;
1187 GetObjCImageInfo(M
, VersionVal
, ImageInfoFlags
, SectionVal
);
1189 // The section is mandatory. If we don't have it, then we don't have GC info.
1190 if (SectionVal
.empty())
1193 StringRef Segment
, Section
;
1194 unsigned TAA
= 0, StubSize
= 0;
1196 if (Error E
= MCSectionMachO::ParseSectionSpecifier(
1197 SectionVal
, Segment
, Section
, TAA
, TAAParsed
, StubSize
)) {
1198 // If invalid, report the error with report_fatal_error.
1199 report_fatal_error("Invalid section specifier '" + Section
+
1200 "': " + toString(std::move(E
)) + ".");
1204 MCSectionMachO
*S
= getContext().getMachOSection(
1205 Segment
, Section
, TAA
, StubSize
, SectionKind::getData());
1206 Streamer
.SwitchSection(S
);
1207 Streamer
.emitLabel(getContext().
1208 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
1209 Streamer
.emitInt32(VersionVal
);
1210 Streamer
.emitInt32(ImageInfoFlags
);
1211 Streamer
.AddBlankLine();
1214 static void checkMachOComdat(const GlobalValue
*GV
) {
1215 const Comdat
*C
= GV
->getComdat();
1219 report_fatal_error("MachO doesn't support COMDATs, '" + C
->getName() +
1220 "' cannot be lowered.");
1223 MCSection
*TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
1224 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1226 StringRef SectionName
= GO
->getSection();
1228 const Function
*F
= dyn_cast
<Function
>(GO
);
1229 if (F
&& F
->hasFnAttribute("implicit-section-name")) {
1230 SectionName
= F
->getFnAttribute("implicit-section-name").getValueAsString();
1233 // Parse the section specifier and create it if valid.
1234 StringRef Segment
, Section
;
1235 unsigned TAA
= 0, StubSize
= 0;
1238 checkMachOComdat(GO
);
1240 if (Error E
= MCSectionMachO::ParseSectionSpecifier(
1241 SectionName
, Segment
, Section
, TAA
, TAAParsed
, StubSize
)) {
1242 // If invalid, report the error with report_fatal_error.
1243 report_fatal_error("Global variable '" + GO
->getName() +
1244 "' has an invalid section specifier '" +
1245 GO
->getSection() + "': " + toString(std::move(E
)) + ".");
1250 getContext().getMachOSection(Segment
, Section
, TAA
, StubSize
, Kind
);
1252 // If TAA wasn't set by ParseSectionSpecifier() above,
1253 // use the value returned by getMachOSection() as a default.
1255 TAA
= S
->getTypeAndAttributes();
1257 // Okay, now that we got the section, verify that the TAA & StubSize agree.
1258 // If the user declared multiple globals with different section flags, we need
1259 // to reject it here.
1260 if (S
->getTypeAndAttributes() != TAA
|| S
->getStubSize() != StubSize
) {
1261 // If invalid, report the error with report_fatal_error.
1262 report_fatal_error("Global variable '" + GO
->getName() +
1263 "' section type or attributes does not match previous"
1264 " section specifier");
1270 MCSection
*TargetLoweringObjectFileMachO::SelectSectionForGlobal(
1271 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1272 checkMachOComdat(GO
);
1274 // Handle thread local data.
1275 if (Kind
.isThreadBSS()) return TLSBSSSection
;
1276 if (Kind
.isThreadData()) return TLSDataSection
;
1279 return GO
->isWeakForLinker() ? TextCoalSection
: TextSection
;
1281 // If this is weak/linkonce, put this in a coalescable section, either in text
1282 // or data depending on if it is writable.
1283 if (GO
->isWeakForLinker()) {
1284 if (Kind
.isReadOnly())
1285 return ConstTextCoalSection
;
1286 if (Kind
.isReadOnlyWithRel())
1287 return ConstDataCoalSection
;
1288 return DataCoalSection
;
1291 // FIXME: Alignment check should be handled by section classifier.
1292 if (Kind
.isMergeable1ByteCString() &&
1293 GO
->getParent()->getDataLayout().getPreferredAlign(
1294 cast
<GlobalVariable
>(GO
)) < Align(32))
1295 return CStringSection
;
1297 // Do not put 16-bit arrays in the UString section if they have an
1298 // externally visible label, this runs into issues with certain linker
1300 if (Kind
.isMergeable2ByteCString() && !GO
->hasExternalLinkage() &&
1301 GO
->getParent()->getDataLayout().getPreferredAlign(
1302 cast
<GlobalVariable
>(GO
)) < Align(32))
1303 return UStringSection
;
1305 // With MachO only variables whose corresponding symbol starts with 'l' or
1306 // 'L' can be merged, so we only try merging GVs with private linkage.
1307 if (GO
->hasPrivateLinkage() && Kind
.isMergeableConst()) {
1308 if (Kind
.isMergeableConst4())
1309 return FourByteConstantSection
;
1310 if (Kind
.isMergeableConst8())
1311 return EightByteConstantSection
;
1312 if (Kind
.isMergeableConst16())
1313 return SixteenByteConstantSection
;
1316 // Otherwise, if it is readonly, but not something we can specially optimize,
1317 // just drop it in .const.
1318 if (Kind
.isReadOnly())
1319 return ReadOnlySection
;
1321 // If this is marked const, put it into a const section. But if the dynamic
1322 // linker needs to write to it, put it in the data segment.
1323 if (Kind
.isReadOnlyWithRel())
1324 return ConstDataSection
;
1326 // Put zero initialized globals with strong external linkage in the
1327 // DATA, __common section with the .zerofill directive.
1328 if (Kind
.isBSSExtern())
1329 return DataCommonSection
;
1331 // Put zero initialized globals with local linkage in __DATA,__bss directive
1332 // with the .zerofill directive (aka .lcomm).
1333 if (Kind
.isBSSLocal())
1334 return DataBSSSection
;
1336 // Otherwise, just drop the variable in the normal data section.
1340 MCSection
*TargetLoweringObjectFileMachO::getSectionForConstant(
1341 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1342 Align
&Alignment
) const {
1343 // If this constant requires a relocation, we have to put it in the data
1344 // segment, not in the text segment.
1345 if (Kind
.isData() || Kind
.isReadOnlyWithRel())
1346 return ConstDataSection
;
1348 if (Kind
.isMergeableConst4())
1349 return FourByteConstantSection
;
1350 if (Kind
.isMergeableConst8())
1351 return EightByteConstantSection
;
1352 if (Kind
.isMergeableConst16())
1353 return SixteenByteConstantSection
;
1354 return ReadOnlySection
; // .const
1357 const MCExpr
*TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1358 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
1359 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1360 // The mach-o version of this method defaults to returning a stub reference.
1362 if (Encoding
& DW_EH_PE_indirect
) {
1363 MachineModuleInfoMachO
&MachOMMI
=
1364 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1366 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1368 // Add information about the stub reference to MachOMMI so that the stub
1369 // gets emitted by the asmprinter.
1370 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1371 if (!StubSym
.getPointer()) {
1372 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1373 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1376 return TargetLoweringObjectFile::
1377 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
1378 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
1381 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
1385 MCSymbol
*TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1386 const GlobalValue
*GV
, const TargetMachine
&TM
,
1387 MachineModuleInfo
*MMI
) const {
1388 // The mach-o version of this method defaults to returning a stub reference.
1389 MachineModuleInfoMachO
&MachOMMI
=
1390 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1392 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1394 // Add information about the stub reference to MachOMMI so that the stub
1395 // gets emitted by the asmprinter.
1396 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1397 if (!StubSym
.getPointer()) {
1398 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1399 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1405 const MCExpr
*TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1406 const GlobalValue
*GV
, const MCSymbol
*Sym
, const MCValue
&MV
,
1407 int64_t Offset
, MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1408 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1409 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1410 // through a non_lazy_ptr stub instead. One advantage is that it allows the
1411 // computation of deltas to final external symbols. Example:
1417 // .long _extgotequiv-_delta
1419 // is transformed to:
1422 // .long L_extfoo$non_lazy_ptr-(_delta+0)
1424 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
1425 // L_extfoo$non_lazy_ptr:
1426 // .indirect_symbol _extfoo
1429 // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1430 // may point to both local (same translation unit) and global (other
1431 // translation units) symbols. Example:
1433 // .section __DATA,__pointers,non_lazy_symbol_pointers
1435 // .indirect_symbol _myGlobal
1438 // .indirect_symbol _myLocal
1441 // If the symbol is local, instead of the symbol's index, the assembler
1442 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1443 // Then the linker will notice the constant in the table and will look at the
1444 // content of the symbol.
1445 MachineModuleInfoMachO
&MachOMMI
=
1446 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1447 MCContext
&Ctx
= getContext();
1449 // The offset must consider the original displacement from the base symbol
1450 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1451 Offset
= -MV
.getConstant();
1452 const MCSymbol
*BaseSym
= &MV
.getSymB()->getSymbol();
1454 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1455 // non_lazy_ptr stubs.
1456 SmallString
<128> Name
;
1457 StringRef Suffix
= "$non_lazy_ptr";
1458 Name
+= MMI
->getModule()->getDataLayout().getPrivateGlobalPrefix();
1459 Name
+= Sym
->getName();
1461 MCSymbol
*Stub
= Ctx
.getOrCreateSymbol(Name
);
1463 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(Stub
);
1465 if (!StubSym
.getPointer())
1466 StubSym
= MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol
*>(Sym
),
1467 !GV
->hasLocalLinkage());
1469 const MCExpr
*BSymExpr
=
1470 MCSymbolRefExpr::create(BaseSym
, MCSymbolRefExpr::VK_None
, Ctx
);
1472 MCSymbolRefExpr::create(Stub
, MCSymbolRefExpr::VK_None
, Ctx
);
1475 return MCBinaryExpr::createSub(LHS
, BSymExpr
, Ctx
);
1478 MCBinaryExpr::createAdd(BSymExpr
, MCConstantExpr::create(Offset
, Ctx
), Ctx
);
1479 return MCBinaryExpr::createSub(LHS
, RHS
, Ctx
);
1482 static bool canUsePrivateLabel(const MCAsmInfo
&AsmInfo
,
1483 const MCSection
&Section
) {
1484 if (!AsmInfo
.isSectionAtomizableBySymbols(Section
))
1487 // FIXME: we should be able to use private labels for sections that can't be
1488 // dead-stripped (there's no issue with blocking atomization there), but `ld
1489 // -r` sometimes drops the no_dead_strip attribute from sections so for safety
1490 // we don't allow it.
1494 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1495 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1496 const TargetMachine
&TM
) const {
1497 bool CannotUsePrivateLabel
= true;
1498 if (auto *GO
= GV
->getBaseObject()) {
1499 SectionKind GOKind
= TargetLoweringObjectFile::getKindForGlobal(GO
, TM
);
1500 const MCSection
*TheSection
= SectionForGlobal(GO
, GOKind
, TM
);
1501 CannotUsePrivateLabel
=
1502 !canUsePrivateLabel(*TM
.getMCAsmInfo(), *TheSection
);
1504 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1507 //===----------------------------------------------------------------------===//
1509 //===----------------------------------------------------------------------===//
1512 getCOFFSectionFlags(SectionKind K
, const TargetMachine
&TM
) {
1514 bool isThumb
= TM
.getTargetTriple().getArch() == Triple::thumb
;
1518 COFF::IMAGE_SCN_MEM_DISCARDABLE
;
1519 else if (K
.isText())
1521 COFF::IMAGE_SCN_MEM_EXECUTE
|
1522 COFF::IMAGE_SCN_MEM_READ
|
1523 COFF::IMAGE_SCN_CNT_CODE
|
1524 (isThumb
? COFF::IMAGE_SCN_MEM_16BIT
: (COFF::SectionCharacteristics
)0);
1527 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
1528 COFF::IMAGE_SCN_MEM_READ
|
1529 COFF::IMAGE_SCN_MEM_WRITE
;
1530 else if (K
.isThreadLocal())
1532 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1533 COFF::IMAGE_SCN_MEM_READ
|
1534 COFF::IMAGE_SCN_MEM_WRITE
;
1535 else if (K
.isReadOnly() || K
.isReadOnlyWithRel())
1537 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1538 COFF::IMAGE_SCN_MEM_READ
;
1539 else if (K
.isWriteable())
1541 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1542 COFF::IMAGE_SCN_MEM_READ
|
1543 COFF::IMAGE_SCN_MEM_WRITE
;
1548 static const GlobalValue
*getComdatGVForCOFF(const GlobalValue
*GV
) {
1549 const Comdat
*C
= GV
->getComdat();
1550 assert(C
&& "expected GV to have a Comdat!");
1552 StringRef ComdatGVName
= C
->getName();
1553 const GlobalValue
*ComdatGV
= GV
->getParent()->getNamedValue(ComdatGVName
);
1555 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1556 "' does not exist.");
1558 if (ComdatGV
->getComdat() != C
)
1559 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1560 "' is not a key for its COMDAT.");
1565 static int getSelectionForCOFF(const GlobalValue
*GV
) {
1566 if (const Comdat
*C
= GV
->getComdat()) {
1567 const GlobalValue
*ComdatKey
= getComdatGVForCOFF(GV
);
1568 if (const auto *GA
= dyn_cast
<GlobalAlias
>(ComdatKey
))
1569 ComdatKey
= GA
->getBaseObject();
1570 if (ComdatKey
== GV
) {
1571 switch (C
->getSelectionKind()) {
1573 return COFF::IMAGE_COMDAT_SELECT_ANY
;
1574 case Comdat::ExactMatch
:
1575 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH
;
1576 case Comdat::Largest
:
1577 return COFF::IMAGE_COMDAT_SELECT_LARGEST
;
1578 case Comdat::NoDeduplicate
:
1579 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1580 case Comdat::SameSize
:
1581 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE
;
1584 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
1590 MCSection
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1591 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1593 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1594 StringRef Name
= GO
->getSection();
1595 StringRef COMDATSymName
= "";
1596 if (GO
->hasComdat()) {
1597 Selection
= getSelectionForCOFF(GO
);
1598 const GlobalValue
*ComdatGV
;
1599 if (Selection
== COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1600 ComdatGV
= getComdatGVForCOFF(GO
);
1604 if (!ComdatGV
->hasPrivateLinkage()) {
1605 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1606 COMDATSymName
= Sym
->getName();
1607 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1613 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, COMDATSymName
,
1617 static StringRef
getCOFFSectionNameForUniqueGlobal(SectionKind Kind
) {
1622 if (Kind
.isThreadLocal())
1624 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1629 MCSection
*TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1630 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1631 // If we have -ffunction-sections then we should emit the global value to a
1632 // uniqued section specifically for it.
1633 bool EmitUniquedSection
;
1635 EmitUniquedSection
= TM
.getFunctionSections();
1637 EmitUniquedSection
= TM
.getDataSections();
1639 if ((EmitUniquedSection
&& !Kind
.isCommon()) || GO
->hasComdat()) {
1640 SmallString
<256> Name
= getCOFFSectionNameForUniqueGlobal(Kind
);
1642 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1644 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1645 int Selection
= getSelectionForCOFF(GO
);
1647 Selection
= COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1648 const GlobalValue
*ComdatGV
;
1649 if (GO
->hasComdat())
1650 ComdatGV
= getComdatGVForCOFF(GO
);
1654 unsigned UniqueID
= MCContext::GenericSectionID
;
1655 if (EmitUniquedSection
)
1656 UniqueID
= NextUniqueID
++;
1658 if (!ComdatGV
->hasPrivateLinkage()) {
1659 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1660 StringRef COMDATSymName
= Sym
->getName();
1662 if (const auto *F
= dyn_cast
<Function
>(GO
))
1663 if (Optional
<StringRef
> Prefix
= F
->getSectionPrefix())
1664 raw_svector_ostream(Name
) << '$' << *Prefix
;
1666 // Append "$symbol" to the section name *before* IR-level mangling is
1667 // applied when targetting mingw. This is what GCC does, and the ld.bfd
1668 // COFF linker will not properly handle comdats otherwise.
1669 if (getContext().getTargetTriple().isWindowsGNUEnvironment())
1670 raw_svector_ostream(Name
) << '$' << ComdatGV
->getName();
1672 return getContext().getCOFFSection(Name
, Characteristics
, Kind
,
1673 COMDATSymName
, Selection
, UniqueID
);
1675 SmallString
<256> TmpData
;
1676 getMangler().getNameWithPrefix(TmpData
, GO
, /*CannotUsePrivateLabel=*/true);
1677 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, TmpData
,
1678 Selection
, UniqueID
);
1685 if (Kind
.isThreadLocal())
1686 return TLSDataSection
;
1688 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1689 return ReadOnlySection
;
1691 // Note: we claim that common symbols are put in BSSSection, but they are
1692 // really emitted with the magic .comm directive, which creates a symbol table
1693 // entry but not a section.
1694 if (Kind
.isBSS() || Kind
.isCommon())
1700 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1701 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1702 const TargetMachine
&TM
) const {
1703 bool CannotUsePrivateLabel
= false;
1704 if (GV
->hasPrivateLinkage() &&
1705 ((isa
<Function
>(GV
) && TM
.getFunctionSections()) ||
1706 (isa
<GlobalVariable
>(GV
) && TM
.getDataSections())))
1707 CannotUsePrivateLabel
= true;
1709 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1712 MCSection
*TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1713 const Function
&F
, const TargetMachine
&TM
) const {
1714 // If the function can be removed, produce a unique section so that
1715 // the table doesn't prevent the removal.
1716 const Comdat
*C
= F
.getComdat();
1717 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
1718 if (!EmitUniqueSection
)
1719 return ReadOnlySection
;
1721 // FIXME: we should produce a symbol for F instead.
1722 if (F
.hasPrivateLinkage())
1723 return ReadOnlySection
;
1725 MCSymbol
*Sym
= TM
.getSymbol(&F
);
1726 StringRef COMDATSymName
= Sym
->getName();
1728 SectionKind Kind
= SectionKind::getReadOnly();
1729 StringRef SecName
= getCOFFSectionNameForUniqueGlobal(Kind
);
1730 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1731 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1732 unsigned UniqueID
= NextUniqueID
++;
1734 return getContext().getCOFFSection(
1735 SecName
, Characteristics
, Kind
, COMDATSymName
,
1736 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
, UniqueID
);
1739 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer
&Streamer
,
1741 emitLinkerDirectives(Streamer
, M
);
1743 unsigned Version
= 0;
1747 GetObjCImageInfo(M
, Version
, Flags
, Section
);
1748 if (!Section
.empty()) {
1749 auto &C
= getContext();
1750 auto *S
= C
.getCOFFSection(Section
,
1751 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1752 COFF::IMAGE_SCN_MEM_READ
,
1753 SectionKind::getReadOnly());
1754 Streamer
.SwitchSection(S
);
1755 Streamer
.emitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1756 Streamer
.emitInt32(Version
);
1757 Streamer
.emitInt32(Flags
);
1758 Streamer
.AddBlankLine();
1761 emitCGProfileMetadata(Streamer
, M
);
1764 void TargetLoweringObjectFileCOFF::emitLinkerDirectives(
1765 MCStreamer
&Streamer
, Module
&M
) const {
1766 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1767 // Emit the linker options to the linker .drectve section. According to the
1768 // spec, this section is a space-separated string containing flags for
1770 MCSection
*Sec
= getDrectveSection();
1771 Streamer
.SwitchSection(Sec
);
1772 for (const auto *Option
: LinkerOptions
->operands()) {
1773 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands()) {
1774 // Lead with a space for consistency with our dllexport implementation.
1775 std::string
Directive(" ");
1776 Directive
.append(std::string(cast
<MDString
>(Piece
)->getString()));
1777 Streamer
.emitBytes(Directive
);
1782 // Emit /EXPORT: flags for each exported global as necessary.
1784 for (const GlobalValue
&GV
: M
.global_values()) {
1785 raw_string_ostream
OS(Flags
);
1786 emitLinkerFlagsForGlobalCOFF(OS
, &GV
, getContext().getTargetTriple(),
1789 if (!Flags
.empty()) {
1790 Streamer
.SwitchSection(getDrectveSection());
1791 Streamer
.emitBytes(Flags
);
1796 // Emit /INCLUDE: flags for each used global as necessary.
1797 if (const auto *LU
= M
.getNamedGlobal("llvm.used")) {
1798 assert(LU
->hasInitializer() && "expected llvm.used to have an initializer");
1799 assert(isa
<ArrayType
>(LU
->getValueType()) &&
1800 "expected llvm.used to be an array type");
1801 if (const auto *A
= cast
<ConstantArray
>(LU
->getInitializer())) {
1802 for (const Value
*Op
: A
->operands()) {
1803 const auto *GV
= cast
<GlobalValue
>(Op
->stripPointerCasts());
1804 // Global symbols with internal or private linkage are not visible to
1805 // the linker, and thus would cause an error when the linker tried to
1806 // preserve the symbol due to the `/include:` directive.
1807 if (GV
->hasLocalLinkage())
1810 raw_string_ostream
OS(Flags
);
1811 emitLinkerFlagsForUsedCOFF(OS
, GV
, getContext().getTargetTriple(),
1815 if (!Flags
.empty()) {
1816 Streamer
.SwitchSection(getDrectveSection());
1817 Streamer
.emitBytes(Flags
);
1825 void TargetLoweringObjectFileCOFF::Initialize(MCContext
&Ctx
,
1826 const TargetMachine
&TM
) {
1827 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1829 const Triple
&T
= TM
.getTargetTriple();
1830 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1832 Ctx
.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1833 COFF::IMAGE_SCN_MEM_READ
,
1834 SectionKind::getReadOnly());
1836 Ctx
.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1837 COFF::IMAGE_SCN_MEM_READ
,
1838 SectionKind::getReadOnly());
1840 StaticCtorSection
= Ctx
.getCOFFSection(
1841 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1842 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1843 SectionKind::getData());
1844 StaticDtorSection
= Ctx
.getCOFFSection(
1845 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1846 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1847 SectionKind::getData());
1851 static MCSectionCOFF
*getCOFFStaticStructorSection(MCContext
&Ctx
,
1852 const Triple
&T
, bool IsCtor
,
1854 const MCSymbol
*KeySym
,
1855 MCSectionCOFF
*Default
) {
1856 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1857 // If the priority is the default, use .CRT$XCU, possibly associative.
1858 if (Priority
== 65535)
1859 return Ctx
.getAssociativeCOFFSection(Default
, KeySym
, 0);
1861 // Otherwise, we need to compute a new section name. Low priorities should
1862 // run earlier. The linker will sort sections ASCII-betically, and we need a
1863 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1864 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1865 // low priorities need to sort before 'L', since the CRT uses that
1866 // internally, so we use ".CRT$XCA00001" for them.
1867 SmallString
<24> Name
;
1868 raw_svector_ostream
OS(Name
);
1869 OS
<< ".CRT$X" << (IsCtor
? "C" : "T") <<
1870 (Priority
< 200 ? 'A' : 'T') << format("%05u", Priority
);
1871 MCSectionCOFF
*Sec
= Ctx
.getCOFFSection(
1872 Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1873 SectionKind::getReadOnly());
1874 return Ctx
.getAssociativeCOFFSection(Sec
, KeySym
, 0);
1877 std::string Name
= IsCtor
? ".ctors" : ".dtors";
1878 if (Priority
!= 65535)
1879 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1881 return Ctx
.getAssociativeCOFFSection(
1882 Ctx
.getCOFFSection(Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1883 COFF::IMAGE_SCN_MEM_READ
|
1884 COFF::IMAGE_SCN_MEM_WRITE
,
1885 SectionKind::getData()),
1889 MCSection
*TargetLoweringObjectFileCOFF::getStaticCtorSection(
1890 unsigned Priority
, const MCSymbol
*KeySym
) const {
1891 return getCOFFStaticStructorSection(
1892 getContext(), getContext().getTargetTriple(), true, Priority
, KeySym
,
1893 cast
<MCSectionCOFF
>(StaticCtorSection
));
1896 MCSection
*TargetLoweringObjectFileCOFF::getStaticDtorSection(
1897 unsigned Priority
, const MCSymbol
*KeySym
) const {
1898 return getCOFFStaticStructorSection(
1899 getContext(), getContext().getTargetTriple(), false, Priority
, KeySym
,
1900 cast
<MCSectionCOFF
>(StaticDtorSection
));
1903 const MCExpr
*TargetLoweringObjectFileCOFF::lowerRelativeReference(
1904 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1905 const TargetMachine
&TM
) const {
1906 const Triple
&T
= TM
.getTargetTriple();
1907 if (T
.isOSCygMing())
1910 // Our symbols should exist in address space zero, cowardly no-op if
1912 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1913 RHS
->getType()->getPointerAddressSpace() != 0)
1916 // Both ptrtoint instructions must wrap global objects:
1917 // - Only global variables are eligible for image relative relocations.
1918 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1919 // We expect __ImageBase to be a global variable without a section, externally
1922 // It should look something like this: @__ImageBase = external constant i8
1923 if (!isa
<GlobalObject
>(LHS
) || !isa
<GlobalVariable
>(RHS
) ||
1924 LHS
->isThreadLocal() || RHS
->isThreadLocal() ||
1925 RHS
->getName() != "__ImageBase" || !RHS
->hasExternalLinkage() ||
1926 cast
<GlobalVariable
>(RHS
)->hasInitializer() || RHS
->hasSection())
1929 return MCSymbolRefExpr::create(TM
.getSymbol(LHS
),
1930 MCSymbolRefExpr::VK_COFF_IMGREL32
,
1934 static std::string
APIntToHexString(const APInt
&AI
) {
1935 unsigned Width
= (AI
.getBitWidth() / 8) * 2;
1936 std::string HexString
= toString(AI
, 16, /*Signed=*/false);
1937 llvm::transform(HexString
, HexString
.begin(), tolower
);
1938 unsigned Size
= HexString
.size();
1939 assert(Width
>= Size
&& "hex string is too large!");
1940 HexString
.insert(HexString
.begin(), Width
- Size
, '0');
1945 static std::string
scalarConstantToHexString(const Constant
*C
) {
1946 Type
*Ty
= C
->getType();
1947 if (isa
<UndefValue
>(C
)) {
1948 return APIntToHexString(APInt::getNullValue(Ty
->getPrimitiveSizeInBits()));
1949 } else if (const auto *CFP
= dyn_cast
<ConstantFP
>(C
)) {
1950 return APIntToHexString(CFP
->getValueAPF().bitcastToAPInt());
1951 } else if (const auto *CI
= dyn_cast
<ConstantInt
>(C
)) {
1952 return APIntToHexString(CI
->getValue());
1954 unsigned NumElements
;
1955 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
1956 NumElements
= cast
<FixedVectorType
>(VTy
)->getNumElements();
1958 NumElements
= Ty
->getArrayNumElements();
1959 std::string HexString
;
1960 for (int I
= NumElements
- 1, E
= -1; I
!= E
; --I
)
1961 HexString
+= scalarConstantToHexString(C
->getAggregateElement(I
));
1966 MCSection
*TargetLoweringObjectFileCOFF::getSectionForConstant(
1967 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1968 Align
&Alignment
) const {
1969 if (Kind
.isMergeableConst() && C
&&
1970 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1971 // This creates comdat sections with the given symbol name, but unless
1972 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1973 // will be created with a null storage class, which makes GNU binutils
1975 const unsigned Characteristics
= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1976 COFF::IMAGE_SCN_MEM_READ
|
1977 COFF::IMAGE_SCN_LNK_COMDAT
;
1978 std::string COMDATSymName
;
1979 if (Kind
.isMergeableConst4()) {
1980 if (Alignment
<= 4) {
1981 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1982 Alignment
= Align(4);
1984 } else if (Kind
.isMergeableConst8()) {
1985 if (Alignment
<= 8) {
1986 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
1987 Alignment
= Align(8);
1989 } else if (Kind
.isMergeableConst16()) {
1990 // FIXME: These may not be appropriate for non-x86 architectures.
1991 if (Alignment
<= 16) {
1992 COMDATSymName
= "__xmm@" + scalarConstantToHexString(C
);
1993 Alignment
= Align(16);
1995 } else if (Kind
.isMergeableConst32()) {
1996 if (Alignment
<= 32) {
1997 COMDATSymName
= "__ymm@" + scalarConstantToHexString(C
);
1998 Alignment
= Align(32);
2002 if (!COMDATSymName
.empty())
2003 return getContext().getCOFFSection(".rdata", Characteristics
, Kind
,
2005 COFF::IMAGE_COMDAT_SELECT_ANY
);
2008 return TargetLoweringObjectFile::getSectionForConstant(DL
, Kind
, C
,
2012 //===----------------------------------------------------------------------===//
2014 //===----------------------------------------------------------------------===//
2016 static const Comdat
*getWasmComdat(const GlobalValue
*GV
) {
2017 const Comdat
*C
= GV
->getComdat();
2021 if (C
->getSelectionKind() != Comdat::Any
)
2022 report_fatal_error("WebAssembly COMDATs only support "
2023 "SelectionKind::Any, '" + C
->getName() + "' cannot be "
2029 static unsigned getWasmSectionFlags(SectionKind K
) {
2032 if (K
.isThreadLocal())
2033 Flags
|= wasm::WASM_SEG_FLAG_TLS
;
2035 if (K
.isMergeableCString())
2036 Flags
|= wasm::WASM_SEG_FLAG_STRINGS
;
2038 // TODO(sbc): Add suport for K.isMergeableConst()
2043 MCSection
*TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
2044 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2045 // We don't support explict section names for functions in the wasm object
2046 // format. Each function has to be in its own unique section.
2047 if (isa
<Function
>(GO
)) {
2048 return SelectSectionForGlobal(GO
, Kind
, TM
);
2051 StringRef Name
= GO
->getSection();
2053 // Certain data sections we treat as named custom sections rather than
2054 // segments within the data section.
2055 // This could be avoided if all data segements (the wasm sense) were
2056 // represented as their own sections (in the llvm sense).
2057 // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
2058 if (Name
== ".llvmcmd" || Name
== ".llvmbc")
2059 Kind
= SectionKind::getMetadata();
2061 StringRef Group
= "";
2062 if (const Comdat
*C
= getWasmComdat(GO
)) {
2063 Group
= C
->getName();
2066 unsigned Flags
= getWasmSectionFlags(Kind
);
2067 MCSectionWasm
*Section
= getContext().getWasmSection(
2068 Name
, Kind
, Flags
, Group
, MCContext::GenericSectionID
);
2073 static MCSectionWasm
*selectWasmSectionForGlobal(
2074 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
2075 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned *NextUniqueID
) {
2076 StringRef Group
= "";
2077 if (const Comdat
*C
= getWasmComdat(GO
)) {
2078 Group
= C
->getName();
2081 bool UniqueSectionNames
= TM
.getUniqueSectionNames();
2082 SmallString
<128> Name
= getSectionPrefixForGlobal(Kind
);
2084 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
2085 const auto &OptionalPrefix
= F
->getSectionPrefix();
2087 raw_svector_ostream(Name
) << '.' << *OptionalPrefix
;
2090 if (EmitUniqueSection
&& UniqueSectionNames
) {
2091 Name
.push_back('.');
2092 TM
.getNameWithPrefix(Name
, GO
, Mang
, true);
2094 unsigned UniqueID
= MCContext::GenericSectionID
;
2095 if (EmitUniqueSection
&& !UniqueSectionNames
) {
2096 UniqueID
= *NextUniqueID
;
2100 unsigned Flags
= getWasmSectionFlags(Kind
);
2101 return Ctx
.getWasmSection(Name
, Kind
, Flags
, Group
, UniqueID
);
2104 MCSection
*TargetLoweringObjectFileWasm::SelectSectionForGlobal(
2105 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2107 if (Kind
.isCommon())
2108 report_fatal_error("mergable sections not supported yet on wasm");
2110 // If we have -ffunction-section or -fdata-section then we should emit the
2111 // global value to a uniqued section specifically for it.
2112 bool EmitUniqueSection
= false;
2114 EmitUniqueSection
= TM
.getFunctionSections();
2116 EmitUniqueSection
= TM
.getDataSections();
2117 EmitUniqueSection
|= GO
->hasComdat();
2119 return selectWasmSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
2120 EmitUniqueSection
, &NextUniqueID
);
2123 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
2124 bool UsesLabelDifference
, const Function
&F
) const {
2125 // We can always create relative relocations, so use another section
2126 // that can be marked non-executable.
2130 const MCExpr
*TargetLoweringObjectFileWasm::lowerRelativeReference(
2131 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
2132 const TargetMachine
&TM
) const {
2133 // We may only use a PLT-relative relocation to refer to unnamed_addr
2135 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
2138 // Basic sanity checks.
2139 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
2140 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
2141 RHS
->isThreadLocal())
2144 return MCBinaryExpr::createSub(
2145 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), MCSymbolRefExpr::VK_None
,
2147 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
2150 void TargetLoweringObjectFileWasm::InitializeWasm() {
2152 getContext().getWasmSection(".init_array", SectionKind::getData());
2154 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
2155 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
2156 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
2159 MCSection
*TargetLoweringObjectFileWasm::getStaticCtorSection(
2160 unsigned Priority
, const MCSymbol
*KeySym
) const {
2161 return Priority
== UINT16_MAX
?
2163 getContext().getWasmSection(".init_array." + utostr(Priority
),
2164 SectionKind::getData());
2167 MCSection
*TargetLoweringObjectFileWasm::getStaticDtorSection(
2168 unsigned Priority
, const MCSymbol
*KeySym
) const {
2169 llvm_unreachable("@llvm.global_dtors should have been lowered already");
2173 //===----------------------------------------------------------------------===//
2175 //===----------------------------------------------------------------------===//
2176 bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(
2177 const MachineFunction
*MF
) {
2178 if (!MF
->getLandingPads().empty())
2181 const Function
&F
= MF
->getFunction();
2182 if (!F
.hasPersonalityFn() || !F
.needsUnwindTableEntry())
2185 const GlobalValue
*Per
=
2186 dyn_cast
<GlobalValue
>(F
.getPersonalityFn()->stripPointerCasts());
2187 assert(Per
&& "Personality routine is not a GlobalValue type.");
2188 if (isNoOpWithoutInvoke(classifyEHPersonality(Per
)))
2194 bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(
2195 const MachineFunction
*MF
) {
2196 const Function
&F
= MF
->getFunction();
2197 if (!F
.hasStackProtectorFnAttr())
2199 // FIXME: check presence of canary word
2200 // There are cases that the stack protectors are not really inserted even if
2201 // the attributes are on.
2206 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction
*MF
) {
2207 return MF
->getMMI().getContext().getOrCreateSymbol(
2208 "__ehinfo." + Twine(MF
->getFunctionNumber()));
2212 TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue
*GV
,
2213 const TargetMachine
&TM
) const {
2214 // We always use a qualname symbol for a GV that represents
2215 // a declaration, a function descriptor, or a common symbol.
2216 // If a GV represents a GlobalVariable and -fdata-sections is enabled, we
2217 // also return a qualname so that a label symbol could be avoided.
2218 // It is inherently ambiguous when the GO represents the address of a
2219 // function, as the GO could either represent a function descriptor or a
2220 // function entry point. We choose to always return a function descriptor
2222 if (const GlobalObject
*GO
= dyn_cast
<GlobalObject
>(GV
)) {
2223 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
2224 if (GVar
->hasAttribute("toc-data"))
2225 return cast
<MCSectionXCOFF
>(
2226 SectionForGlobal(GVar
, SectionKind::getData(), TM
))
2227 ->getQualNameSymbol();
2229 if (GO
->isDeclarationForLinker())
2230 return cast
<MCSectionXCOFF
>(getSectionForExternalReference(GO
, TM
))
2231 ->getQualNameSymbol();
2233 SectionKind GOKind
= getKindForGlobal(GO
, TM
);
2234 if (GOKind
.isText())
2235 return cast
<MCSectionXCOFF
>(
2236 getSectionForFunctionDescriptor(cast
<Function
>(GO
), TM
))
2237 ->getQualNameSymbol();
2238 if ((TM
.getDataSections() && !GO
->hasSection()) || GO
->hasCommonLinkage() ||
2239 GOKind
.isBSSLocal() || GOKind
.isThreadBSSLocal())
2240 return cast
<MCSectionXCOFF
>(SectionForGlobal(GO
, GOKind
, TM
))
2241 ->getQualNameSymbol();
2244 // For all other cases, fall back to getSymbol to return the unqualified name.
2248 MCSection
*TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
2249 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2250 if (!GO
->hasSection())
2251 report_fatal_error("#pragma clang section is not yet supported");
2253 StringRef SectionName
= GO
->getSection();
2255 // Handle the XCOFF::TD case first, then deal with the rest.
2256 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GO
))
2257 if (GVar
->hasAttribute("toc-data"))
2258 return getContext().getXCOFFSection(
2260 XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD
, XCOFF::XTY_SD
),
2261 /* MultiSymbolsAllowed*/ true);
2263 XCOFF::StorageMappingClass MappingClass
;
2265 MappingClass
= XCOFF::XMC_PR
;
2266 else if (Kind
.isData() || Kind
.isReadOnlyWithRel() || Kind
.isBSS())
2267 MappingClass
= XCOFF::XMC_RW
;
2268 else if (Kind
.isReadOnly())
2269 MappingClass
= XCOFF::XMC_RO
;
2271 report_fatal_error("XCOFF other section types not yet implemented.");
2273 return getContext().getXCOFFSection(
2274 SectionName
, Kind
, XCOFF::CsectProperties(MappingClass
, XCOFF::XTY_SD
),
2275 /* MultiSymbolsAllowed*/ true);
2278 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForExternalReference(
2279 const GlobalObject
*GO
, const TargetMachine
&TM
) const {
2280 assert(GO
->isDeclarationForLinker() &&
2281 "Tried to get ER section for a defined global.");
2283 SmallString
<128> Name
;
2284 getNameWithPrefix(Name
, GO
, TM
);
2286 XCOFF::StorageMappingClass SMC
=
2287 isa
<Function
>(GO
) ? XCOFF::XMC_DS
: XCOFF::XMC_UA
;
2288 if (GO
->isThreadLocal())
2289 SMC
= XCOFF::XMC_UL
;
2291 // Externals go into a csect of type ER.
2292 return getContext().getXCOFFSection(
2293 Name
, SectionKind::getMetadata(),
2294 XCOFF::CsectProperties(SMC
, XCOFF::XTY_ER
));
2297 MCSection
*TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
2298 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2299 // Handle the XCOFF::TD case first, then deal with the rest.
2300 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GO
))
2301 if (GVar
->hasAttribute("toc-data")) {
2302 SmallString
<128> Name
;
2303 getNameWithPrefix(Name
, GO
, TM
);
2304 return getContext().getXCOFFSection(
2305 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_TD
, XCOFF::XTY_SD
),
2306 /* MultiSymbolsAllowed*/ true);
2309 // Common symbols go into a csect with matching name which will get mapped
2310 // into the .bss section.
2311 // Zero-initialized local TLS symbols go into a csect with matching name which
2312 // will get mapped into the .tbss section.
2313 if (Kind
.isBSSLocal() || GO
->hasCommonLinkage() || Kind
.isThreadBSSLocal()) {
2314 SmallString
<128> Name
;
2315 getNameWithPrefix(Name
, GO
, TM
);
2316 XCOFF::StorageMappingClass SMC
= Kind
.isBSSLocal() ? XCOFF::XMC_BS
2317 : Kind
.isCommon() ? XCOFF::XMC_RW
2319 return getContext().getXCOFFSection(
2320 Name
, Kind
, XCOFF::CsectProperties(SMC
, XCOFF::XTY_CM
));
2323 if (Kind
.isMergeableCString()) {
2324 Align Alignment
= GO
->getParent()->getDataLayout().getPreferredAlign(
2325 cast
<GlobalVariable
>(GO
));
2327 unsigned EntrySize
= getEntrySizeForKind(Kind
);
2328 std::string SizeSpec
= ".rodata.str" + utostr(EntrySize
) + ".";
2329 SmallString
<128> Name
;
2330 Name
= SizeSpec
+ utostr(Alignment
.value());
2332 if (TM
.getDataSections())
2333 getNameWithPrefix(Name
, GO
, TM
);
2335 return getContext().getXCOFFSection(
2336 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
),
2337 /* MultiSymbolsAllowed*/ !TM
.getDataSections());
2340 if (Kind
.isText()) {
2341 if (TM
.getFunctionSections()) {
2342 return cast
<MCSymbolXCOFF
>(getFunctionEntryPointSymbol(GO
, TM
))
2343 ->getRepresentedCsect();
2348 // TODO: We may put Kind.isReadOnlyWithRel() under option control, because
2349 // user may want to have read-only data with relocations placed into a
2350 // read-only section by the compiler.
2351 // For BSS kind, zero initialized data must be emitted to the .data section
2352 // because external linkage control sections that get mapped to the .bss
2353 // section will be linked as tentative defintions, which is only appropriate
2354 // for SectionKind::Common.
2355 if (Kind
.isData() || Kind
.isReadOnlyWithRel() || Kind
.isBSS()) {
2356 if (TM
.getDataSections()) {
2357 SmallString
<128> Name
;
2358 getNameWithPrefix(Name
, GO
, TM
);
2359 return getContext().getXCOFFSection(
2360 Name
, SectionKind::getData(),
2361 XCOFF::CsectProperties(XCOFF::XMC_RW
, XCOFF::XTY_SD
));
2366 if (Kind
.isReadOnly()) {
2367 if (TM
.getDataSections()) {
2368 SmallString
<128> Name
;
2369 getNameWithPrefix(Name
, GO
, TM
);
2370 return getContext().getXCOFFSection(
2371 Name
, SectionKind::getReadOnly(),
2372 XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
));
2374 return ReadOnlySection
;
2377 // External/weak TLS data and initialized local TLS data are not eligible
2378 // to be put into common csect. If data sections are enabled, thread
2379 // data are emitted into separate sections. Otherwise, thread data
2380 // are emitted into the .tdata section.
2381 if (Kind
.isThreadLocal()) {
2382 if (TM
.getDataSections()) {
2383 SmallString
<128> Name
;
2384 getNameWithPrefix(Name
, GO
, TM
);
2385 return getContext().getXCOFFSection(
2386 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_TL
, XCOFF::XTY_SD
));
2388 return TLSDataSection
;
2391 report_fatal_error("XCOFF other section types not yet implemented.");
2394 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForJumpTable(
2395 const Function
&F
, const TargetMachine
&TM
) const {
2396 assert (!F
.getComdat() && "Comdat not supported on XCOFF.");
2398 if (!TM
.getFunctionSections())
2399 return ReadOnlySection
;
2401 // If the function can be removed, produce a unique section so that
2402 // the table doesn't prevent the removal.
2403 SmallString
<128> NameStr(".rodata.jmp..");
2404 getNameWithPrefix(NameStr
, &F
, TM
);
2405 return getContext().getXCOFFSection(
2406 NameStr
, SectionKind::getReadOnly(),
2407 XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
));
2410 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
2411 bool UsesLabelDifference
, const Function
&F
) const {
2415 /// Given a mergeable constant with the specified size and relocation
2416 /// information, return a section that it should be placed in.
2417 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForConstant(
2418 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
2419 Align
&Alignment
) const {
2420 // TODO: Enable emiting constant pool to unique sections when we support it.
2421 if (Alignment
> Align(16))
2422 report_fatal_error("Alignments greater than 16 not yet supported.");
2424 if (Alignment
== Align(8)) {
2425 assert(ReadOnly8Section
&& "Section should always be initialized.");
2426 return ReadOnly8Section
;
2429 if (Alignment
== Align(16)) {
2430 assert(ReadOnly16Section
&& "Section should always be initialized.");
2431 return ReadOnly16Section
;
2434 return ReadOnlySection
;
2437 void TargetLoweringObjectFileXCOFF::Initialize(MCContext
&Ctx
,
2438 const TargetMachine
&TgtM
) {
2439 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
2441 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_datarel
|
2442 (TgtM
.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4
2443 : dwarf::DW_EH_PE_sdata8
);
2444 PersonalityEncoding
= 0;
2446 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
2449 MCSection
*TargetLoweringObjectFileXCOFF::getStaticCtorSection(
2450 unsigned Priority
, const MCSymbol
*KeySym
) const {
2451 report_fatal_error("no static constructor section on AIX");
2454 MCSection
*TargetLoweringObjectFileXCOFF::getStaticDtorSection(
2455 unsigned Priority
, const MCSymbol
*KeySym
) const {
2456 report_fatal_error("no static destructor section on AIX");
2459 const MCExpr
*TargetLoweringObjectFileXCOFF::lowerRelativeReference(
2460 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
2461 const TargetMachine
&TM
) const {
2462 /* Not implemented yet, but don't crash, return nullptr. */
2467 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue
*GV
) {
2468 assert(!isa
<GlobalIFunc
>(GV
) && "GlobalIFunc is not supported on AIX.");
2470 switch (GV
->getLinkage()) {
2471 case GlobalValue::InternalLinkage
:
2472 case GlobalValue::PrivateLinkage
:
2473 return XCOFF::C_HIDEXT
;
2474 case GlobalValue::ExternalLinkage
:
2475 case GlobalValue::CommonLinkage
:
2476 case GlobalValue::AvailableExternallyLinkage
:
2477 return XCOFF::C_EXT
;
2478 case GlobalValue::ExternalWeakLinkage
:
2479 case GlobalValue::LinkOnceAnyLinkage
:
2480 case GlobalValue::LinkOnceODRLinkage
:
2481 case GlobalValue::WeakAnyLinkage
:
2482 case GlobalValue::WeakODRLinkage
:
2483 return XCOFF::C_WEAKEXT
;
2484 case GlobalValue::AppendingLinkage
:
2486 "There is no mapping that implements AppendingLinkage for XCOFF.");
2488 llvm_unreachable("Unknown linkage type!");
2491 MCSymbol
*TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol(
2492 const GlobalValue
*Func
, const TargetMachine
&TM
) const {
2494 (isa
<Function
>(Func
) ||
2495 (isa
<GlobalAlias
>(Func
) &&
2496 isa_and_nonnull
<Function
>(cast
<GlobalAlias
>(Func
)->getBaseObject()))) &&
2497 "Func must be a function or an alias which has a function as base "
2500 SmallString
<128> NameStr
;
2501 NameStr
.push_back('.');
2502 getNameWithPrefix(NameStr
, Func
, TM
);
2504 // When -function-sections is enabled and explicit section is not specified,
2505 // it's not necessary to emit function entry point label any more. We will use
2506 // function entry point csect instead. And for function delcarations, the
2507 // undefined symbols gets treated as csect with XTY_ER property.
2508 if (((TM
.getFunctionSections() && !Func
->hasSection()) ||
2509 Func
->isDeclaration()) &&
2510 isa
<Function
>(Func
)) {
2513 NameStr
, SectionKind::getText(),
2514 XCOFF::CsectProperties(XCOFF::XMC_PR
, Func
->isDeclaration()
2517 ->getQualNameSymbol();
2520 return getContext().getOrCreateSymbol(NameStr
);
2523 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor(
2524 const Function
*F
, const TargetMachine
&TM
) const {
2525 SmallString
<128> NameStr
;
2526 getNameWithPrefix(NameStr
, F
, TM
);
2527 return getContext().getXCOFFSection(
2528 NameStr
, SectionKind::getData(),
2529 XCOFF::CsectProperties(XCOFF::XMC_DS
, XCOFF::XTY_SD
));
2532 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForTOCEntry(
2533 const MCSymbol
*Sym
, const TargetMachine
&TM
) const {
2534 // Use TE storage-mapping class when large code model is enabled so that
2535 // the chance of needing -bbigtoc is decreased.
2536 return getContext().getXCOFFSection(
2537 cast
<MCSymbolXCOFF
>(Sym
)->getSymbolTableName(), SectionKind::getData(),
2538 XCOFF::CsectProperties(
2539 TM
.getCodeModel() == CodeModel::Large
? XCOFF::XMC_TE
: XCOFF::XMC_TC
,
2543 //===----------------------------------------------------------------------===//
2545 //===----------------------------------------------------------------------===//
2546 TargetLoweringObjectFileGOFF::TargetLoweringObjectFileGOFF()
2547 : TargetLoweringObjectFile() {}
2549 MCSection
*TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
2550 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2551 return SelectSectionForGlobal(GO
, Kind
, TM
);
2554 MCSection
*TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
2555 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2556 auto *Symbol
= TM
.getSymbol(GO
);
2558 return getContext().getGOFFSection(Symbol
->getName(),
2559 SectionKind::getBSS());
2561 return getContext().getObjectFileInfo()->getTextSection();