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 SupportDSOLocalEquivalentLowering
= true;
115 void TargetLoweringObjectFileELF::Initialize(MCContext
&Ctx
,
116 const TargetMachine
&TgtM
) {
117 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
119 CodeModel::Model CM
= TgtM
.getCodeModel();
120 InitializeELF(TgtM
.Options
.UseInitArray
);
122 switch (TgtM
.getTargetTriple().getArch()) {
126 case Triple::thumbeb
:
127 if (Ctx
.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM
)
129 // Fallthrough if not using EHABI
134 PersonalityEncoding
= isPositionIndependent()
135 ? dwarf::DW_EH_PE_indirect
|
136 dwarf::DW_EH_PE_pcrel
|
137 dwarf::DW_EH_PE_sdata4
138 : dwarf::DW_EH_PE_absptr
;
139 LSDAEncoding
= isPositionIndependent()
140 ? dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
141 : dwarf::DW_EH_PE_absptr
;
142 TTypeEncoding
= isPositionIndependent()
143 ? dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
144 dwarf::DW_EH_PE_sdata4
145 : dwarf::DW_EH_PE_absptr
;
148 if (isPositionIndependent()) {
149 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
150 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
151 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
152 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
|
153 (CM
== CodeModel::Small
154 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
155 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
156 ((CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
157 ? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8
);
159 PersonalityEncoding
=
160 (CM
== CodeModel::Small
|| CM
== CodeModel::Medium
)
161 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
162 LSDAEncoding
= (CM
== CodeModel::Small
)
163 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
164 TTypeEncoding
= (CM
== CodeModel::Small
)
165 ? dwarf::DW_EH_PE_udata4
: dwarf::DW_EH_PE_absptr
;
168 case Triple::hexagon
:
169 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
170 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
171 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
172 if (isPositionIndependent()) {
173 PersonalityEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
174 LSDAEncoding
|= dwarf::DW_EH_PE_pcrel
;
175 TTypeEncoding
|= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
;
178 case Triple::aarch64
:
179 case Triple::aarch64_be
:
180 case Triple::aarch64_32
:
181 // The small model guarantees static code/data size < 4GB, but not where it
182 // will be in memory. Most of these could end up >2GB away so even a signed
183 // pc-relative 32-bit address is insufficient, theoretically.
184 if (isPositionIndependent()) {
185 // ILP32 uses sdata4 instead of sdata8
186 if (TgtM
.getTargetTriple().getEnvironment() == Triple::GNUILP32
) {
187 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
188 dwarf::DW_EH_PE_sdata4
;
189 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
190 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
191 dwarf::DW_EH_PE_sdata4
;
193 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
194 dwarf::DW_EH_PE_sdata8
;
195 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
;
196 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
197 dwarf::DW_EH_PE_sdata8
;
200 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
201 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
202 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
206 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
207 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
208 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
213 case Triple::mips64el
:
214 // MIPS uses indirect pointer to refer personality functions and types, so
215 // that the eh_frame section can be read-only. DW.ref.personality will be
216 // generated for relocation.
217 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
;
218 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
219 // identify N64 from just a triple.
220 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
221 dwarf::DW_EH_PE_sdata4
;
222 // We don't support PC-relative LSDA references in GAS so we use the default
223 // DW_EH_PE_absptr for those.
225 // FreeBSD must be explicit about the data size and using pcrel since it's
226 // assembler/linker won't do the automatic conversion that the Linux tools
228 if (TgtM
.getTargetTriple().isOSFreeBSD()) {
229 PersonalityEncoding
|= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
230 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
234 case Triple::ppc64le
:
235 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
236 dwarf::DW_EH_PE_udata8
;
237 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata8
;
238 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
239 dwarf::DW_EH_PE_udata8
;
241 case Triple::sparcel
:
243 if (isPositionIndependent()) {
244 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
245 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
246 dwarf::DW_EH_PE_sdata4
;
247 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
248 dwarf::DW_EH_PE_sdata4
;
250 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
251 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
252 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
254 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
256 case Triple::riscv32
:
257 case Triple::riscv64
:
258 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
259 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
260 dwarf::DW_EH_PE_sdata4
;
261 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
262 dwarf::DW_EH_PE_sdata4
;
263 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
265 case Triple::sparcv9
:
266 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
267 if (isPositionIndependent()) {
268 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
269 dwarf::DW_EH_PE_sdata4
;
270 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
271 dwarf::DW_EH_PE_sdata4
;
273 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
274 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
277 case Triple::systemz
:
278 // All currently-defined code models guarantee that 4-byte PC-relative
279 // values will be in range.
280 if (isPositionIndependent()) {
281 PersonalityEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
282 dwarf::DW_EH_PE_sdata4
;
283 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
284 TTypeEncoding
= dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
|
285 dwarf::DW_EH_PE_sdata4
;
287 PersonalityEncoding
= dwarf::DW_EH_PE_absptr
;
288 LSDAEncoding
= dwarf::DW_EH_PE_absptr
;
289 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
297 void TargetLoweringObjectFileELF::getModuleMetadata(Module
&M
) {
298 SmallVector
<GlobalValue
*, 4> Vec
;
299 collectUsedGlobalVariables(M
, Vec
, false);
300 for (GlobalValue
*GV
: Vec
)
301 if (auto *GO
= dyn_cast
<GlobalObject
>(GV
))
305 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer
&Streamer
,
307 auto &C
= getContext();
309 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
310 auto *S
= C
.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS
,
313 Streamer
.switchSection(S
);
315 for (const auto *Operand
: LinkerOptions
->operands()) {
316 if (cast
<MDNode
>(Operand
)->getNumOperands() != 2)
317 report_fatal_error("invalid llvm.linker.options");
318 for (const auto &Option
: cast
<MDNode
>(Operand
)->operands()) {
319 Streamer
.emitBytes(cast
<MDString
>(Option
)->getString());
320 Streamer
.emitInt8(0);
325 if (NamedMDNode
*DependentLibraries
= M
.getNamedMetadata("llvm.dependent-libraries")) {
326 auto *S
= C
.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES
,
327 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1);
329 Streamer
.switchSection(S
);
331 for (const auto *Operand
: DependentLibraries
->operands()) {
333 cast
<MDString
>(cast
<MDNode
>(Operand
)->getOperand(0))->getString());
334 Streamer
.emitInt8(0);
338 if (NamedMDNode
*FuncInfo
= M
.getNamedMetadata(PseudoProbeDescMetadataName
)) {
339 // Emit a descriptor for every function including functions that have an
340 // available external linkage. We may not want this for imported functions
341 // that has code in another thinLTO module but we don't have a good way to
342 // tell them apart from inline functions defined in header files. Therefore
343 // we put each descriptor in a separate comdat section and rely on the
344 // linker to deduplicate.
345 for (const auto *Operand
: FuncInfo
->operands()) {
346 const auto *MD
= cast
<MDNode
>(Operand
);
347 auto *GUID
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(0));
348 auto *Hash
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(1));
349 auto *Name
= cast
<MDString
>(MD
->getOperand(2));
350 auto *S
= C
.getObjectFileInfo()->getPseudoProbeDescSection(
351 TM
->getFunctionSections() ? Name
->getString() : StringRef());
353 Streamer
.switchSection(S
);
354 Streamer
.emitInt64(GUID
->getZExtValue());
355 Streamer
.emitInt64(Hash
->getZExtValue());
356 Streamer
.emitULEB128IntValue(Name
->getString().size());
357 Streamer
.emitBytes(Name
->getString());
361 unsigned Version
= 0;
365 GetObjCImageInfo(M
, Version
, Flags
, Section
);
366 if (!Section
.empty()) {
367 auto *S
= C
.getELFSection(Section
, ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
368 Streamer
.switchSection(S
);
369 Streamer
.emitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
370 Streamer
.emitInt32(Version
);
371 Streamer
.emitInt32(Flags
);
372 Streamer
.addBlankLine();
375 emitCGProfileMetadata(Streamer
, M
);
378 MCSymbol
*TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
379 const GlobalValue
*GV
, const TargetMachine
&TM
,
380 MachineModuleInfo
*MMI
) const {
381 unsigned Encoding
= getPersonalityEncoding();
382 if ((Encoding
& 0x80) == DW_EH_PE_indirect
)
383 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
384 TM
.getSymbol(GV
)->getName());
385 if ((Encoding
& 0x70) == DW_EH_PE_absptr
)
386 return TM
.getSymbol(GV
);
387 report_fatal_error("We do not support this DWARF encoding yet!");
390 void TargetLoweringObjectFileELF::emitPersonalityValue(
391 MCStreamer
&Streamer
, const DataLayout
&DL
, const MCSymbol
*Sym
) const {
392 SmallString
<64> NameData("DW.ref.");
393 NameData
+= Sym
->getName();
395 cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(NameData
));
396 Streamer
.emitSymbolAttribute(Label
, MCSA_Hidden
);
397 Streamer
.emitSymbolAttribute(Label
, MCSA_Weak
);
398 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::SHF_GROUP
;
399 MCSection
*Sec
= getContext().getELFNamedSection(".data", Label
->getName(),
400 ELF::SHT_PROGBITS
, Flags
, 0);
401 unsigned Size
= DL
.getPointerSize();
402 Streamer
.switchSection(Sec
);
403 Streamer
.emitValueToAlignment(DL
.getPointerABIAlignment(0).value());
404 Streamer
.emitSymbolAttribute(Label
, MCSA_ELF_TypeObject
);
405 const MCExpr
*E
= MCConstantExpr::create(Size
, getContext());
406 Streamer
.emitELFSize(Label
, E
);
407 Streamer
.emitLabel(Label
);
409 Streamer
.emitSymbolValue(Sym
, Size
);
412 const MCExpr
*TargetLoweringObjectFileELF::getTTypeGlobalReference(
413 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
414 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
415 if (Encoding
& DW_EH_PE_indirect
) {
416 MachineModuleInfoELF
&ELFMMI
= MMI
->getObjFileInfo
<MachineModuleInfoELF
>();
418 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, ".DW.stub", TM
);
420 // Add information about the stub reference to ELFMMI so that the stub
421 // gets emitted by the asmprinter.
422 MachineModuleInfoImpl::StubValueTy
&StubSym
= ELFMMI
.getGVStubEntry(SSym
);
423 if (!StubSym
.getPointer()) {
424 MCSymbol
*Sym
= TM
.getSymbol(GV
);
425 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
428 return TargetLoweringObjectFile::
429 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
430 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
433 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
437 static SectionKind
getELFKindForNamedSection(StringRef Name
, SectionKind K
) {
438 // N.B.: The defaults used in here are not the same ones used in MC.
439 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
440 // both gas and MC will produce a section with no flags. Given
441 // section(".eh_frame") gcc will produce:
443 // .section .eh_frame,"a",@progbits
445 if (Name
== getInstrProfSectionName(IPSK_covmap
, Triple::ELF
,
446 /*AddSegmentInfo=*/false) ||
447 Name
== getInstrProfSectionName(IPSK_covfun
, Triple::ELF
,
448 /*AddSegmentInfo=*/false) ||
449 Name
== ".llvmbc" || Name
== ".llvmcmd")
450 return SectionKind::getMetadata();
452 if (Name
.empty() || Name
[0] != '.') return K
;
454 // Default implementation based on some magic section names.
455 if (Name
== ".bss" ||
456 Name
.startswith(".bss.") ||
457 Name
.startswith(".gnu.linkonce.b.") ||
458 Name
.startswith(".llvm.linkonce.b.") ||
460 Name
.startswith(".sbss.") ||
461 Name
.startswith(".gnu.linkonce.sb.") ||
462 Name
.startswith(".llvm.linkonce.sb."))
463 return SectionKind::getBSS();
465 if (Name
== ".tdata" ||
466 Name
.startswith(".tdata.") ||
467 Name
.startswith(".gnu.linkonce.td.") ||
468 Name
.startswith(".llvm.linkonce.td."))
469 return SectionKind::getThreadData();
471 if (Name
== ".tbss" ||
472 Name
.startswith(".tbss.") ||
473 Name
.startswith(".gnu.linkonce.tb.") ||
474 Name
.startswith(".llvm.linkonce.tb."))
475 return SectionKind::getThreadBSS();
480 static bool hasPrefix(StringRef SectionName
, StringRef Prefix
) {
481 return SectionName
.consume_front(Prefix
) &&
482 (SectionName
.empty() || SectionName
[0] == '.');
485 static unsigned getELFSectionType(StringRef Name
, SectionKind K
) {
486 // Use SHT_NOTE for section whose name starts with ".note" to allow
487 // emitting ELF notes from C variable declaration.
488 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
489 if (Name
.startswith(".note"))
490 return ELF::SHT_NOTE
;
492 if (hasPrefix(Name
, ".init_array"))
493 return ELF::SHT_INIT_ARRAY
;
495 if (hasPrefix(Name
, ".fini_array"))
496 return ELF::SHT_FINI_ARRAY
;
498 if (hasPrefix(Name
, ".preinit_array"))
499 return ELF::SHT_PREINIT_ARRAY
;
501 if (hasPrefix(Name
, ".llvm.offloading"))
502 return ELF::SHT_LLVM_OFFLOADING
;
504 if (K
.isBSS() || K
.isThreadBSS())
505 return ELF::SHT_NOBITS
;
507 return ELF::SHT_PROGBITS
;
510 static unsigned getELFSectionFlags(SectionKind K
) {
513 if (!K
.isMetadata() && !K
.isExclude())
514 Flags
|= ELF::SHF_ALLOC
;
517 Flags
|= ELF::SHF_EXCLUDE
;
520 Flags
|= ELF::SHF_EXECINSTR
;
522 if (K
.isExecuteOnly())
523 Flags
|= ELF::SHF_ARM_PURECODE
;
526 Flags
|= ELF::SHF_WRITE
;
528 if (K
.isThreadLocal())
529 Flags
|= ELF::SHF_TLS
;
531 if (K
.isMergeableCString() || K
.isMergeableConst())
532 Flags
|= ELF::SHF_MERGE
;
534 if (K
.isMergeableCString())
535 Flags
|= ELF::SHF_STRINGS
;
540 static const Comdat
*getELFComdat(const GlobalValue
*GV
) {
541 const Comdat
*C
= GV
->getComdat();
545 if (C
->getSelectionKind() != Comdat::Any
&&
546 C
->getSelectionKind() != Comdat::NoDeduplicate
)
547 report_fatal_error("ELF COMDATs only support SelectionKind::Any and "
548 "SelectionKind::NoDeduplicate, '" +
549 C
->getName() + "' cannot be lowered.");
554 static const MCSymbolELF
*getLinkedToSymbol(const GlobalObject
*GO
,
555 const TargetMachine
&TM
) {
556 MDNode
*MD
= GO
->getMetadata(LLVMContext::MD_associated
);
560 const MDOperand
&Op
= MD
->getOperand(0);
564 auto *VM
= dyn_cast
<ValueAsMetadata
>(Op
);
566 report_fatal_error("MD_associated operand is not ValueAsMetadata");
568 auto *OtherGV
= dyn_cast
<GlobalValue
>(VM
->getValue());
569 return OtherGV
? dyn_cast
<MCSymbolELF
>(TM
.getSymbol(OtherGV
)) : nullptr;
572 static unsigned getEntrySizeForKind(SectionKind Kind
) {
573 if (Kind
.isMergeable1ByteCString())
575 else if (Kind
.isMergeable2ByteCString())
577 else if (Kind
.isMergeable4ByteCString())
579 else if (Kind
.isMergeableConst4())
581 else if (Kind
.isMergeableConst8())
583 else if (Kind
.isMergeableConst16())
585 else if (Kind
.isMergeableConst32())
588 // We shouldn't have mergeable C strings or mergeable constants that we
589 // didn't handle above.
590 assert(!Kind
.isMergeableCString() && "unknown string width");
591 assert(!Kind
.isMergeableConst() && "unknown data width");
596 /// Return the section prefix name used by options FunctionsSections and
598 static StringRef
getSectionPrefixForGlobal(SectionKind Kind
) {
601 if (Kind
.isReadOnly())
605 if (Kind
.isThreadData())
607 if (Kind
.isThreadBSS())
611 if (Kind
.isReadOnlyWithRel())
612 return ".data.rel.ro";
613 llvm_unreachable("Unknown section kind");
616 static SmallString
<128>
617 getELFSectionNameForGlobal(const GlobalObject
*GO
, SectionKind Kind
,
618 Mangler
&Mang
, const TargetMachine
&TM
,
619 unsigned EntrySize
, bool UniqueSectionName
) {
620 SmallString
<128> Name
;
621 if (Kind
.isMergeableCString()) {
622 // We also need alignment here.
623 // FIXME: this is getting the alignment of the character, not the
624 // alignment of the global!
625 Align Alignment
= GO
->getParent()->getDataLayout().getPreferredAlign(
626 cast
<GlobalVariable
>(GO
));
628 std::string SizeSpec
= ".rodata.str" + utostr(EntrySize
) + ".";
629 Name
= SizeSpec
+ utostr(Alignment
.value());
630 } else if (Kind
.isMergeableConst()) {
631 Name
= ".rodata.cst";
632 Name
+= utostr(EntrySize
);
634 Name
= getSectionPrefixForGlobal(Kind
);
637 bool HasPrefix
= false;
638 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
639 if (Optional
<StringRef
> Prefix
= F
->getSectionPrefix()) {
640 raw_svector_ostream(Name
) << '.' << *Prefix
;
645 if (UniqueSectionName
) {
647 TM
.getNameWithPrefix(Name
, GO
, Mang
, /*MayAlwaysUsePrivate*/true);
648 } else if (HasPrefix
)
649 // For distinguishing between .text.${text-section-prefix}. (with trailing
650 // dot) and .text.${function-name}
656 class LoweringDiagnosticInfo
: public DiagnosticInfo
{
660 LoweringDiagnosticInfo(const Twine
&DiagMsg
,
661 DiagnosticSeverity Severity
= DS_Error
)
662 : DiagnosticInfo(DK_Lowering
, Severity
), Msg(DiagMsg
) {}
663 void print(DiagnosticPrinter
&DP
) const override
{ DP
<< Msg
; }
667 /// Calculate an appropriate unique ID for a section, and update Flags,
668 /// EntrySize and NextUniqueID where appropriate.
670 calcUniqueIDUpdateFlagsAndSize(const GlobalObject
*GO
, StringRef SectionName
,
671 SectionKind Kind
, const TargetMachine
&TM
,
672 MCContext
&Ctx
, Mangler
&Mang
, unsigned &Flags
,
673 unsigned &EntrySize
, unsigned &NextUniqueID
,
674 const bool Retain
, const bool ForceUnique
) {
675 // Increment uniqueID if we are forced to emit a unique section.
676 // This works perfectly fine with section attribute or pragma section as the
677 // sections with the same name are grouped together by the assembler.
679 return NextUniqueID
++;
681 // A section can have at most one associated section. Put each global with
682 // MD_associated in a unique section.
683 const bool Associated
= GO
->getMetadata(LLVMContext::MD_associated
);
685 Flags
|= ELF::SHF_LINK_ORDER
;
686 return NextUniqueID
++;
690 if (TM
.getTargetTriple().isOSSolaris())
691 Flags
|= ELF::SHF_SUNW_NODISCARD
;
692 else if (Ctx
.getAsmInfo()->useIntegratedAssembler() ||
693 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 36))
694 Flags
|= ELF::SHF_GNU_RETAIN
;
695 return NextUniqueID
++;
698 // If two symbols with differing sizes end up in the same mergeable section
699 // that section can be assigned an incorrect entry size. To avoid this we
700 // usually put symbols of the same size into distinct mergeable sections with
701 // the same name. Doing so relies on the ",unique ," assembly feature. This
702 // feature is not avalible until bintuils version 2.35
703 // (https://sourceware.org/bugzilla/show_bug.cgi?id=25380).
704 const bool SupportsUnique
= Ctx
.getAsmInfo()->useIntegratedAssembler() ||
705 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 35);
706 if (!SupportsUnique
) {
707 Flags
&= ~ELF::SHF_MERGE
;
709 return MCContext::GenericSectionID
;
712 const bool SymbolMergeable
= Flags
& ELF::SHF_MERGE
;
713 const bool SeenSectionNameBefore
=
714 Ctx
.isELFGenericMergeableSection(SectionName
);
715 // If this is the first ocurrence of this section name, treat it as the
717 if (!SymbolMergeable
&& !SeenSectionNameBefore
)
718 return MCContext::GenericSectionID
;
720 // Symbols must be placed into sections with compatible entry sizes. Generate
721 // unique sections for symbols that have not been assigned to compatible
723 const auto PreviousID
=
724 Ctx
.getELFUniqueIDForEntsize(SectionName
, Flags
, EntrySize
);
728 // If the user has specified the same section name as would be created
729 // implicitly for this symbol e.g. .rodata.str1.1, then we don't need
730 // to unique the section as the entry size for this symbol will be
731 // compatible with implicitly created sections.
732 SmallString
<128> ImplicitSectionNameStem
=
733 getELFSectionNameForGlobal(GO
, Kind
, Mang
, TM
, EntrySize
, false);
734 if (SymbolMergeable
&&
735 Ctx
.isELFImplicitMergeableSectionNamePrefix(SectionName
) &&
736 SectionName
.startswith(ImplicitSectionNameStem
))
737 return MCContext::GenericSectionID
;
739 // We have seen this section name before, but with different flags or entity
740 // size. Create a new unique ID.
741 return NextUniqueID
++;
744 static MCSection
*selectExplicitSectionGlobal(
745 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
,
746 MCContext
&Ctx
, Mangler
&Mang
, unsigned &NextUniqueID
,
747 bool Retain
, bool ForceUnique
) {
748 StringRef SectionName
= GO
->getSection();
750 // Check if '#pragma clang section' name is applicable.
751 // Note that pragma directive overrides -ffunction-section, -fdata-section
752 // and so section name is exactly as user specified and not uniqued.
753 const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(GO
);
754 if (GV
&& GV
->hasImplicitSection()) {
755 auto Attrs
= GV
->getAttributes();
756 if (Attrs
.hasAttribute("bss-section") && Kind
.isBSS()) {
757 SectionName
= Attrs
.getAttribute("bss-section").getValueAsString();
758 } else if (Attrs
.hasAttribute("rodata-section") && Kind
.isReadOnly()) {
759 SectionName
= Attrs
.getAttribute("rodata-section").getValueAsString();
760 } else if (Attrs
.hasAttribute("relro-section") && Kind
.isReadOnlyWithRel()) {
761 SectionName
= Attrs
.getAttribute("relro-section").getValueAsString();
762 } else if (Attrs
.hasAttribute("data-section") && Kind
.isData()) {
763 SectionName
= Attrs
.getAttribute("data-section").getValueAsString();
766 const Function
*F
= dyn_cast
<Function
>(GO
);
767 if (F
&& F
->hasFnAttribute("implicit-section-name")) {
768 SectionName
= F
->getFnAttribute("implicit-section-name").getValueAsString();
771 // Infer section flags from the section name if we can.
772 Kind
= getELFKindForNamedSection(SectionName
, Kind
);
774 StringRef Group
= "";
775 bool IsComdat
= false;
776 unsigned Flags
= getELFSectionFlags(Kind
);
777 if (const Comdat
*C
= getELFComdat(GO
)) {
778 Group
= C
->getName();
779 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
780 Flags
|= ELF::SHF_GROUP
;
783 unsigned EntrySize
= getEntrySizeForKind(Kind
);
784 const unsigned UniqueID
= calcUniqueIDUpdateFlagsAndSize(
785 GO
, SectionName
, Kind
, TM
, Ctx
, Mang
, Flags
, EntrySize
, NextUniqueID
,
786 Retain
, ForceUnique
);
788 const MCSymbolELF
*LinkedToSym
= getLinkedToSymbol(GO
, TM
);
789 MCSectionELF
*Section
= Ctx
.getELFSection(
790 SectionName
, getELFSectionType(SectionName
, Kind
), Flags
, EntrySize
,
791 Group
, IsComdat
, UniqueID
, LinkedToSym
);
792 // Make sure that we did not get some other section with incompatible sh_link.
793 // This should not be possible due to UniqueID code above.
794 assert(Section
->getLinkedToSymbol() == LinkedToSym
&&
795 "Associated symbol mismatch between sections");
797 if (!(Ctx
.getAsmInfo()->useIntegratedAssembler() ||
798 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 35))) {
799 // If we are using GNU as before 2.35, then this symbol might have
800 // been placed in an incompatible mergeable section. Emit an error if this
801 // is the case to avoid creating broken output.
802 if ((Section
->getFlags() & ELF::SHF_MERGE
) &&
803 (Section
->getEntrySize() != getEntrySizeForKind(Kind
)))
804 GO
->getContext().diagnose(LoweringDiagnosticInfo(
805 "Symbol '" + GO
->getName() + "' from module '" +
806 (GO
->getParent() ? GO
->getParent()->getSourceFileName() : "unknown") +
807 "' required a section with entry-size=" +
808 Twine(getEntrySizeForKind(Kind
)) + " but was placed in section '" +
809 SectionName
+ "' with entry-size=" + Twine(Section
->getEntrySize()) +
810 ": Explicit assignment by pragma or attribute of an incompatible "
811 "symbol to this section?"));
817 MCSection
*TargetLoweringObjectFileELF::getExplicitSectionGlobal(
818 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
819 return selectExplicitSectionGlobal(GO
, Kind
, TM
, getContext(), getMangler(),
820 NextUniqueID
, Used
.count(GO
),
821 /* ForceUnique = */false);
824 static MCSectionELF
*selectELFSectionForGlobal(
825 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
826 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned Flags
,
827 unsigned *NextUniqueID
, const MCSymbolELF
*AssociatedSymbol
) {
829 StringRef Group
= "";
830 bool IsComdat
= false;
831 if (const Comdat
*C
= getELFComdat(GO
)) {
832 Flags
|= ELF::SHF_GROUP
;
833 Group
= C
->getName();
834 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
837 // Get the section entry size based on the kind.
838 unsigned EntrySize
= getEntrySizeForKind(Kind
);
840 bool UniqueSectionName
= false;
841 unsigned UniqueID
= MCContext::GenericSectionID
;
842 if (EmitUniqueSection
) {
843 if (TM
.getUniqueSectionNames()) {
844 UniqueSectionName
= true;
846 UniqueID
= *NextUniqueID
;
850 SmallString
<128> Name
= getELFSectionNameForGlobal(
851 GO
, Kind
, Mang
, TM
, EntrySize
, UniqueSectionName
);
853 // Use 0 as the unique ID for execute-only text.
854 if (Kind
.isExecuteOnly())
856 return Ctx
.getELFSection(Name
, getELFSectionType(Name
, Kind
), Flags
,
857 EntrySize
, Group
, IsComdat
, UniqueID
,
861 static MCSection
*selectELFSectionForGlobal(
862 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
863 const TargetMachine
&TM
, bool Retain
, bool EmitUniqueSection
,
864 unsigned Flags
, unsigned *NextUniqueID
) {
865 const MCSymbolELF
*LinkedToSym
= getLinkedToSymbol(GO
, TM
);
867 EmitUniqueSection
= true;
868 Flags
|= ELF::SHF_LINK_ORDER
;
871 if (TM
.getTargetTriple().isOSSolaris()) {
872 EmitUniqueSection
= true;
873 Flags
|= ELF::SHF_SUNW_NODISCARD
;
874 } else if (Ctx
.getAsmInfo()->useIntegratedAssembler() ||
875 Ctx
.getAsmInfo()->binutilsIsAtLeast(2, 36)) {
876 EmitUniqueSection
= true;
877 Flags
|= ELF::SHF_GNU_RETAIN
;
881 MCSectionELF
*Section
= selectELFSectionForGlobal(
882 Ctx
, GO
, Kind
, Mang
, TM
, EmitUniqueSection
, Flags
,
883 NextUniqueID
, LinkedToSym
);
884 assert(Section
->getLinkedToSymbol() == LinkedToSym
);
888 MCSection
*TargetLoweringObjectFileELF::SelectSectionForGlobal(
889 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
890 unsigned Flags
= getELFSectionFlags(Kind
);
892 // If we have -ffunction-section or -fdata-section then we should emit the
893 // global value to a uniqued section specifically for it.
894 bool EmitUniqueSection
= false;
895 if (!(Flags
& ELF::SHF_MERGE
) && !Kind
.isCommon()) {
897 EmitUniqueSection
= TM
.getFunctionSections();
899 EmitUniqueSection
= TM
.getDataSections();
901 EmitUniqueSection
|= GO
->hasComdat();
902 return selectELFSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
903 Used
.count(GO
), EmitUniqueSection
, Flags
,
907 MCSection
*TargetLoweringObjectFileELF::getUniqueSectionForFunction(
908 const Function
&F
, const TargetMachine
&TM
) const {
909 SectionKind Kind
= SectionKind::getText();
910 unsigned Flags
= getELFSectionFlags(Kind
);
911 // If the function's section names is pre-determined via pragma or a
912 // section attribute, call selectExplicitSectionGlobal.
913 if (F
.hasSection() || F
.hasFnAttribute("implicit-section-name"))
914 return selectExplicitSectionGlobal(
915 &F
, Kind
, TM
, getContext(), getMangler(), NextUniqueID
,
916 Used
.count(&F
), /* ForceUnique = */true);
918 return selectELFSectionForGlobal(
919 getContext(), &F
, Kind
, getMangler(), TM
, Used
.count(&F
),
920 /*EmitUniqueSection=*/true, Flags
, &NextUniqueID
);
923 MCSection
*TargetLoweringObjectFileELF::getSectionForJumpTable(
924 const Function
&F
, const TargetMachine
&TM
) const {
925 // If the function can be removed, produce a unique section so that
926 // the table doesn't prevent the removal.
927 const Comdat
*C
= F
.getComdat();
928 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
929 if (!EmitUniqueSection
)
930 return ReadOnlySection
;
932 return selectELFSectionForGlobal(getContext(), &F
, SectionKind::getReadOnly(),
933 getMangler(), TM
, EmitUniqueSection
,
934 ELF::SHF_ALLOC
, &NextUniqueID
,
935 /* AssociatedSymbol */ nullptr);
938 MCSection
*TargetLoweringObjectFileELF::getSectionForLSDA(
939 const Function
&F
, const MCSymbol
&FnSym
, const TargetMachine
&TM
) const {
940 // If neither COMDAT nor function sections, use the monolithic LSDA section.
941 // Re-use this path if LSDASection is null as in the Arm EHABI.
942 if (!LSDASection
|| (!F
.hasComdat() && !TM
.getFunctionSections()))
945 const auto *LSDA
= cast
<MCSectionELF
>(LSDASection
);
946 unsigned Flags
= LSDA
->getFlags();
947 const MCSymbolELF
*LinkedToSym
= nullptr;
949 bool IsComdat
= false;
950 if (const Comdat
*C
= getELFComdat(&F
)) {
951 Flags
|= ELF::SHF_GROUP
;
952 Group
= C
->getName();
953 IsComdat
= C
->getSelectionKind() == Comdat::Any
;
955 // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36
956 // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER.
957 if (TM
.getFunctionSections() &&
958 (getContext().getAsmInfo()->useIntegratedAssembler() &&
959 getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) {
960 Flags
|= ELF::SHF_LINK_ORDER
;
961 LinkedToSym
= cast
<MCSymbolELF
>(&FnSym
);
964 // Append the function name as the suffix like GCC, assuming
965 // -funique-section-names applies to .gcc_except_table sections.
966 return getContext().getELFSection(
967 (TM
.getUniqueSectionNames() ? LSDA
->getName() + "." + F
.getName()
969 LSDA
->getType(), Flags
, 0, Group
, IsComdat
, MCSection::NonUniqueID
,
973 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
974 bool UsesLabelDifference
, const Function
&F
) const {
975 // We can always create relative relocations, so use another section
976 // that can be marked non-executable.
980 /// Given a mergeable constant with the specified size and relocation
981 /// information, return a section that it should be placed in.
982 MCSection
*TargetLoweringObjectFileELF::getSectionForConstant(
983 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
984 Align
&Alignment
) const {
985 if (Kind
.isMergeableConst4() && MergeableConst4Section
)
986 return MergeableConst4Section
;
987 if (Kind
.isMergeableConst8() && MergeableConst8Section
)
988 return MergeableConst8Section
;
989 if (Kind
.isMergeableConst16() && MergeableConst16Section
)
990 return MergeableConst16Section
;
991 if (Kind
.isMergeableConst32() && MergeableConst32Section
)
992 return MergeableConst32Section
;
993 if (Kind
.isReadOnly())
994 return ReadOnlySection
;
996 assert(Kind
.isReadOnlyWithRel() && "Unknown section kind");
997 return DataRelROSection
;
1000 /// Returns a unique section for the given machine basic block.
1001 MCSection
*TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
1002 const Function
&F
, const MachineBasicBlock
&MBB
,
1003 const TargetMachine
&TM
) const {
1004 assert(MBB
.isBeginSection() && "Basic block does not start a section!");
1005 unsigned UniqueID
= MCContext::GenericSectionID
;
1007 // For cold sections use the .text.split. prefix along with the parent
1008 // function name. All cold blocks for the same function go to the same
1009 // section. Similarly all exception blocks are grouped by symbol name
1010 // under the .text.eh prefix. For regular sections, we either use a unique
1011 // name, or a unique ID for the section.
1012 SmallString
<128> Name
;
1013 if (MBB
.getSectionID() == MBBSectionID::ColdSectionID
) {
1014 Name
+= BBSectionsColdTextPrefix
;
1015 Name
+= MBB
.getParent()->getName();
1016 } else if (MBB
.getSectionID() == MBBSectionID::ExceptionSectionID
) {
1017 Name
+= ".text.eh.";
1018 Name
+= MBB
.getParent()->getName();
1020 Name
+= MBB
.getParent()->getSection()->getName();
1021 if (TM
.getUniqueBasicBlockSectionNames()) {
1022 if (!Name
.endswith("."))
1024 Name
+= MBB
.getSymbol()->getName();
1026 UniqueID
= NextUniqueID
++;
1030 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
;
1031 std::string GroupName
;
1032 if (F
.hasComdat()) {
1033 Flags
|= ELF::SHF_GROUP
;
1034 GroupName
= F
.getComdat()->getName().str();
1036 return getContext().getELFSection(Name
, ELF::SHT_PROGBITS
, Flags
,
1037 0 /* Entry Size */, GroupName
,
1038 F
.hasComdat(), UniqueID
, nullptr);
1041 static MCSectionELF
*getStaticStructorSection(MCContext
&Ctx
, bool UseInitArray
,
1042 bool IsCtor
, unsigned Priority
,
1043 const MCSymbol
*KeySym
) {
1046 unsigned Flags
= ELF::SHF_ALLOC
| ELF::SHF_WRITE
;
1047 StringRef Comdat
= KeySym
? KeySym
->getName() : "";
1050 Flags
|= ELF::SHF_GROUP
;
1054 Type
= ELF::SHT_INIT_ARRAY
;
1055 Name
= ".init_array";
1057 Type
= ELF::SHT_FINI_ARRAY
;
1058 Name
= ".fini_array";
1060 if (Priority
!= 65535) {
1062 Name
+= utostr(Priority
);
1065 // The default scheme is .ctor / .dtor, so we have to invert the priority
1071 if (Priority
!= 65535)
1072 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1073 Type
= ELF::SHT_PROGBITS
;
1076 return Ctx
.getELFSection(Name
, Type
, Flags
, 0, Comdat
, /*IsComdat=*/true);
1079 MCSection
*TargetLoweringObjectFileELF::getStaticCtorSection(
1080 unsigned Priority
, const MCSymbol
*KeySym
) const {
1081 return getStaticStructorSection(getContext(), UseInitArray
, true, Priority
,
1085 MCSection
*TargetLoweringObjectFileELF::getStaticDtorSection(
1086 unsigned Priority
, const MCSymbol
*KeySym
) const {
1087 return getStaticStructorSection(getContext(), UseInitArray
, false, Priority
,
1091 const MCExpr
*TargetLoweringObjectFileELF::lowerRelativeReference(
1092 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1093 const TargetMachine
&TM
) const {
1094 // We may only use a PLT-relative relocation to refer to unnamed_addr
1096 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
1099 // Basic correctness checks.
1100 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1101 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
1102 RHS
->isThreadLocal())
1105 return MCBinaryExpr::createSub(
1106 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), PLTRelativeVariantKind
,
1108 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
1111 const MCExpr
*TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
1112 const DSOLocalEquivalent
*Equiv
, const TargetMachine
&TM
) const {
1113 assert(supportDSOLocalEquivalentLowering());
1115 const auto *GV
= Equiv
->getGlobalValue();
1117 // A PLT entry is not needed for dso_local globals.
1118 if (GV
->isDSOLocal() || GV
->isImplicitDSOLocal())
1119 return MCSymbolRefExpr::create(TM
.getSymbol(GV
), getContext());
1121 return MCSymbolRefExpr::create(TM
.getSymbol(GV
), PLTRelativeVariantKind
,
1125 MCSection
*TargetLoweringObjectFileELF::getSectionForCommandLines() const {
1126 // Use ".GCC.command.line" since this feature is to support clang's
1127 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
1129 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS
,
1130 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1);
1134 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_
) {
1135 UseInitArray
= UseInitArray_
;
1136 MCContext
&Ctx
= getContext();
1137 if (!UseInitArray
) {
1138 StaticCtorSection
= Ctx
.getELFSection(".ctors", ELF::SHT_PROGBITS
,
1139 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
1141 StaticDtorSection
= Ctx
.getELFSection(".dtors", ELF::SHT_PROGBITS
,
1142 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
1146 StaticCtorSection
= Ctx
.getELFSection(".init_array", ELF::SHT_INIT_ARRAY
,
1147 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
1148 StaticDtorSection
= Ctx
.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY
,
1149 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
1152 //===----------------------------------------------------------------------===//
1154 //===----------------------------------------------------------------------===//
1156 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() {
1157 SupportIndirectSymViaGOTPCRel
= true;
1160 void TargetLoweringObjectFileMachO::Initialize(MCContext
&Ctx
,
1161 const TargetMachine
&TM
) {
1162 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1163 if (TM
.getRelocationModel() == Reloc::Static
) {
1164 StaticCtorSection
= Ctx
.getMachOSection("__TEXT", "__constructor", 0,
1165 SectionKind::getData());
1166 StaticDtorSection
= Ctx
.getMachOSection("__TEXT", "__destructor", 0,
1167 SectionKind::getData());
1169 StaticCtorSection
= Ctx
.getMachOSection("__DATA", "__mod_init_func",
1170 MachO::S_MOD_INIT_FUNC_POINTERS
,
1171 SectionKind::getData());
1172 StaticDtorSection
= Ctx
.getMachOSection("__DATA", "__mod_term_func",
1173 MachO::S_MOD_TERM_FUNC_POINTERS
,
1174 SectionKind::getData());
1177 PersonalityEncoding
=
1178 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
1179 LSDAEncoding
= dwarf::DW_EH_PE_pcrel
;
1181 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
1184 MCSection
*TargetLoweringObjectFileMachO::getStaticDtorSection(
1185 unsigned Priority
, const MCSymbol
*KeySym
) const {
1186 // TODO(yln): Remove -lower-global-dtors-via-cxa-atexit fallback flag
1187 // (LowerGlobalDtorsViaCxaAtExit) and always issue a fatal error here.
1188 if (TM
->Options
.LowerGlobalDtorsViaCxaAtExit
)
1189 report_fatal_error("@llvm.global_dtors should have been lowered already");
1190 return StaticDtorSection
;
1193 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer
&Streamer
,
1195 // Emit the linker options if present.
1196 if (auto *LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1197 for (const auto *Option
: LinkerOptions
->operands()) {
1198 SmallVector
<std::string
, 4> StrOptions
;
1199 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands())
1200 StrOptions
.push_back(std::string(cast
<MDString
>(Piece
)->getString()));
1201 Streamer
.emitLinkerOptions(StrOptions
);
1205 unsigned VersionVal
= 0;
1206 unsigned ImageInfoFlags
= 0;
1207 StringRef SectionVal
;
1209 GetObjCImageInfo(M
, VersionVal
, ImageInfoFlags
, SectionVal
);
1210 emitCGProfileMetadata(Streamer
, M
);
1212 // The section is mandatory. If we don't have it, then we don't have GC info.
1213 if (SectionVal
.empty())
1216 StringRef Segment
, Section
;
1217 unsigned TAA
= 0, StubSize
= 0;
1219 if (Error E
= MCSectionMachO::ParseSectionSpecifier(
1220 SectionVal
, Segment
, Section
, TAA
, TAAParsed
, StubSize
)) {
1221 // If invalid, report the error with report_fatal_error.
1222 report_fatal_error("Invalid section specifier '" + Section
+
1223 "': " + toString(std::move(E
)) + ".");
1227 MCSectionMachO
*S
= getContext().getMachOSection(
1228 Segment
, Section
, TAA
, StubSize
, SectionKind::getData());
1229 Streamer
.switchSection(S
);
1230 Streamer
.emitLabel(getContext().
1231 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
1232 Streamer
.emitInt32(VersionVal
);
1233 Streamer
.emitInt32(ImageInfoFlags
);
1234 Streamer
.addBlankLine();
1237 static void checkMachOComdat(const GlobalValue
*GV
) {
1238 const Comdat
*C
= GV
->getComdat();
1242 report_fatal_error("MachO doesn't support COMDATs, '" + C
->getName() +
1243 "' cannot be lowered.");
1246 MCSection
*TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
1247 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1249 StringRef SectionName
= GO
->getSection();
1251 const Function
*F
= dyn_cast
<Function
>(GO
);
1252 if (F
&& F
->hasFnAttribute("implicit-section-name")) {
1253 SectionName
= F
->getFnAttribute("implicit-section-name").getValueAsString();
1256 // Parse the section specifier and create it if valid.
1257 StringRef Segment
, Section
;
1258 unsigned TAA
= 0, StubSize
= 0;
1261 checkMachOComdat(GO
);
1263 if (Error E
= MCSectionMachO::ParseSectionSpecifier(
1264 SectionName
, Segment
, Section
, TAA
, TAAParsed
, StubSize
)) {
1265 // If invalid, report the error with report_fatal_error.
1266 report_fatal_error("Global variable '" + GO
->getName() +
1267 "' has an invalid section specifier '" +
1268 GO
->getSection() + "': " + toString(std::move(E
)) + ".");
1273 getContext().getMachOSection(Segment
, Section
, TAA
, StubSize
, Kind
);
1275 // If TAA wasn't set by ParseSectionSpecifier() above,
1276 // use the value returned by getMachOSection() as a default.
1278 TAA
= S
->getTypeAndAttributes();
1280 // Okay, now that we got the section, verify that the TAA & StubSize agree.
1281 // If the user declared multiple globals with different section flags, we need
1282 // to reject it here.
1283 if (S
->getTypeAndAttributes() != TAA
|| S
->getStubSize() != StubSize
) {
1284 // If invalid, report the error with report_fatal_error.
1285 report_fatal_error("Global variable '" + GO
->getName() +
1286 "' section type or attributes does not match previous"
1287 " section specifier");
1293 MCSection
*TargetLoweringObjectFileMachO::SelectSectionForGlobal(
1294 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1295 checkMachOComdat(GO
);
1297 // Handle thread local data.
1298 if (Kind
.isThreadBSS()) return TLSBSSSection
;
1299 if (Kind
.isThreadData()) return TLSDataSection
;
1302 return GO
->isWeakForLinker() ? TextCoalSection
: TextSection
;
1304 // If this is weak/linkonce, put this in a coalescable section, either in text
1305 // or data depending on if it is writable.
1306 if (GO
->isWeakForLinker()) {
1307 if (Kind
.isReadOnly())
1308 return ConstTextCoalSection
;
1309 if (Kind
.isReadOnlyWithRel())
1310 return ConstDataCoalSection
;
1311 return DataCoalSection
;
1314 // FIXME: Alignment check should be handled by section classifier.
1315 if (Kind
.isMergeable1ByteCString() &&
1316 GO
->getParent()->getDataLayout().getPreferredAlign(
1317 cast
<GlobalVariable
>(GO
)) < Align(32))
1318 return CStringSection
;
1320 // Do not put 16-bit arrays in the UString section if they have an
1321 // externally visible label, this runs into issues with certain linker
1323 if (Kind
.isMergeable2ByteCString() && !GO
->hasExternalLinkage() &&
1324 GO
->getParent()->getDataLayout().getPreferredAlign(
1325 cast
<GlobalVariable
>(GO
)) < Align(32))
1326 return UStringSection
;
1328 // With MachO only variables whose corresponding symbol starts with 'l' or
1329 // 'L' can be merged, so we only try merging GVs with private linkage.
1330 if (GO
->hasPrivateLinkage() && Kind
.isMergeableConst()) {
1331 if (Kind
.isMergeableConst4())
1332 return FourByteConstantSection
;
1333 if (Kind
.isMergeableConst8())
1334 return EightByteConstantSection
;
1335 if (Kind
.isMergeableConst16())
1336 return SixteenByteConstantSection
;
1339 // Otherwise, if it is readonly, but not something we can specially optimize,
1340 // just drop it in .const.
1341 if (Kind
.isReadOnly())
1342 return ReadOnlySection
;
1344 // If this is marked const, put it into a const section. But if the dynamic
1345 // linker needs to write to it, put it in the data segment.
1346 if (Kind
.isReadOnlyWithRel())
1347 return ConstDataSection
;
1349 // Put zero initialized globals with strong external linkage in the
1350 // DATA, __common section with the .zerofill directive.
1351 if (Kind
.isBSSExtern())
1352 return DataCommonSection
;
1354 // Put zero initialized globals with local linkage in __DATA,__bss directive
1355 // with the .zerofill directive (aka .lcomm).
1356 if (Kind
.isBSSLocal())
1357 return DataBSSSection
;
1359 // Otherwise, just drop the variable in the normal data section.
1363 MCSection
*TargetLoweringObjectFileMachO::getSectionForConstant(
1364 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
1365 Align
&Alignment
) const {
1366 // If this constant requires a relocation, we have to put it in the data
1367 // segment, not in the text segment.
1368 if (Kind
.isData() || Kind
.isReadOnlyWithRel())
1369 return ConstDataSection
;
1371 if (Kind
.isMergeableConst4())
1372 return FourByteConstantSection
;
1373 if (Kind
.isMergeableConst8())
1374 return EightByteConstantSection
;
1375 if (Kind
.isMergeableConst16())
1376 return SixteenByteConstantSection
;
1377 return ReadOnlySection
; // .const
1380 const MCExpr
*TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1381 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
1382 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1383 // The mach-o version of this method defaults to returning a stub reference.
1385 if (Encoding
& DW_EH_PE_indirect
) {
1386 MachineModuleInfoMachO
&MachOMMI
=
1387 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1389 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1391 // Add information about the stub reference to MachOMMI so that the stub
1392 // gets emitted by the asmprinter.
1393 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1394 if (!StubSym
.getPointer()) {
1395 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1396 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1399 return TargetLoweringObjectFile::
1400 getTTypeReference(MCSymbolRefExpr::create(SSym
, getContext()),
1401 Encoding
& ~DW_EH_PE_indirect
, Streamer
);
1404 return TargetLoweringObjectFile::getTTypeGlobalReference(GV
, Encoding
, TM
,
1408 MCSymbol
*TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1409 const GlobalValue
*GV
, const TargetMachine
&TM
,
1410 MachineModuleInfo
*MMI
) const {
1411 // The mach-o version of this method defaults to returning a stub reference.
1412 MachineModuleInfoMachO
&MachOMMI
=
1413 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1415 MCSymbol
*SSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr", TM
);
1417 // Add information about the stub reference to MachOMMI so that the stub
1418 // gets emitted by the asmprinter.
1419 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(SSym
);
1420 if (!StubSym
.getPointer()) {
1421 MCSymbol
*Sym
= TM
.getSymbol(GV
);
1422 StubSym
= MachineModuleInfoImpl::StubValueTy(Sym
, !GV
->hasLocalLinkage());
1428 const MCExpr
*TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1429 const GlobalValue
*GV
, const MCSymbol
*Sym
, const MCValue
&MV
,
1430 int64_t Offset
, MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
1431 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1432 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1433 // through a non_lazy_ptr stub instead. One advantage is that it allows the
1434 // computation of deltas to final external symbols. Example:
1440 // .long _extgotequiv-_delta
1442 // is transformed to:
1445 // .long L_extfoo$non_lazy_ptr-(_delta+0)
1447 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
1448 // L_extfoo$non_lazy_ptr:
1449 // .indirect_symbol _extfoo
1452 // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1453 // may point to both local (same translation unit) and global (other
1454 // translation units) symbols. Example:
1456 // .section __DATA,__pointers,non_lazy_symbol_pointers
1458 // .indirect_symbol _myGlobal
1461 // .indirect_symbol _myLocal
1464 // If the symbol is local, instead of the symbol's index, the assembler
1465 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1466 // Then the linker will notice the constant in the table and will look at the
1467 // content of the symbol.
1468 MachineModuleInfoMachO
&MachOMMI
=
1469 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
1470 MCContext
&Ctx
= getContext();
1472 // The offset must consider the original displacement from the base symbol
1473 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1474 Offset
= -MV
.getConstant();
1475 const MCSymbol
*BaseSym
= &MV
.getSymB()->getSymbol();
1477 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1478 // non_lazy_ptr stubs.
1479 SmallString
<128> Name
;
1480 StringRef Suffix
= "$non_lazy_ptr";
1481 Name
+= MMI
->getModule()->getDataLayout().getPrivateGlobalPrefix();
1482 Name
+= Sym
->getName();
1484 MCSymbol
*Stub
= Ctx
.getOrCreateSymbol(Name
);
1486 MachineModuleInfoImpl::StubValueTy
&StubSym
= MachOMMI
.getGVStubEntry(Stub
);
1488 if (!StubSym
.getPointer())
1489 StubSym
= MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol
*>(Sym
),
1490 !GV
->hasLocalLinkage());
1492 const MCExpr
*BSymExpr
=
1493 MCSymbolRefExpr::create(BaseSym
, MCSymbolRefExpr::VK_None
, Ctx
);
1495 MCSymbolRefExpr::create(Stub
, MCSymbolRefExpr::VK_None
, Ctx
);
1498 return MCBinaryExpr::createSub(LHS
, BSymExpr
, Ctx
);
1501 MCBinaryExpr::createAdd(BSymExpr
, MCConstantExpr::create(Offset
, Ctx
), Ctx
);
1502 return MCBinaryExpr::createSub(LHS
, RHS
, Ctx
);
1505 static bool canUsePrivateLabel(const MCAsmInfo
&AsmInfo
,
1506 const MCSection
&Section
) {
1507 if (!AsmInfo
.isSectionAtomizableBySymbols(Section
))
1510 // FIXME: we should be able to use private labels for sections that can't be
1511 // dead-stripped (there's no issue with blocking atomization there), but `ld
1512 // -r` sometimes drops the no_dead_strip attribute from sections so for safety
1513 // we don't allow it.
1517 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1518 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1519 const TargetMachine
&TM
) const {
1520 bool CannotUsePrivateLabel
= true;
1521 if (auto *GO
= GV
->getAliaseeObject()) {
1522 SectionKind GOKind
= TargetLoweringObjectFile::getKindForGlobal(GO
, TM
);
1523 const MCSection
*TheSection
= SectionForGlobal(GO
, GOKind
, TM
);
1524 CannotUsePrivateLabel
=
1525 !canUsePrivateLabel(*TM
.getMCAsmInfo(), *TheSection
);
1527 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1530 //===----------------------------------------------------------------------===//
1532 //===----------------------------------------------------------------------===//
1535 getCOFFSectionFlags(SectionKind K
, const TargetMachine
&TM
) {
1537 bool isThumb
= TM
.getTargetTriple().getArch() == Triple::thumb
;
1541 COFF::IMAGE_SCN_MEM_DISCARDABLE
;
1542 else if (K
.isExclude())
1544 COFF::IMAGE_SCN_LNK_REMOVE
| COFF::IMAGE_SCN_MEM_DISCARDABLE
;
1545 else if (K
.isText())
1547 COFF::IMAGE_SCN_MEM_EXECUTE
|
1548 COFF::IMAGE_SCN_MEM_READ
|
1549 COFF::IMAGE_SCN_CNT_CODE
|
1550 (isThumb
? COFF::IMAGE_SCN_MEM_16BIT
: (COFF::SectionCharacteristics
)0);
1553 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
1554 COFF::IMAGE_SCN_MEM_READ
|
1555 COFF::IMAGE_SCN_MEM_WRITE
;
1556 else if (K
.isThreadLocal())
1558 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1559 COFF::IMAGE_SCN_MEM_READ
|
1560 COFF::IMAGE_SCN_MEM_WRITE
;
1561 else if (K
.isReadOnly() || K
.isReadOnlyWithRel())
1563 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1564 COFF::IMAGE_SCN_MEM_READ
;
1565 else if (K
.isWriteable())
1567 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1568 COFF::IMAGE_SCN_MEM_READ
|
1569 COFF::IMAGE_SCN_MEM_WRITE
;
1574 static const GlobalValue
*getComdatGVForCOFF(const GlobalValue
*GV
) {
1575 const Comdat
*C
= GV
->getComdat();
1576 assert(C
&& "expected GV to have a Comdat!");
1578 StringRef ComdatGVName
= C
->getName();
1579 const GlobalValue
*ComdatGV
= GV
->getParent()->getNamedValue(ComdatGVName
);
1581 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1582 "' does not exist.");
1584 if (ComdatGV
->getComdat() != C
)
1585 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName
+
1586 "' is not a key for its COMDAT.");
1591 static int getSelectionForCOFF(const GlobalValue
*GV
) {
1592 if (const Comdat
*C
= GV
->getComdat()) {
1593 const GlobalValue
*ComdatKey
= getComdatGVForCOFF(GV
);
1594 if (const auto *GA
= dyn_cast
<GlobalAlias
>(ComdatKey
))
1595 ComdatKey
= GA
->getAliaseeObject();
1596 if (ComdatKey
== GV
) {
1597 switch (C
->getSelectionKind()) {
1599 return COFF::IMAGE_COMDAT_SELECT_ANY
;
1600 case Comdat::ExactMatch
:
1601 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH
;
1602 case Comdat::Largest
:
1603 return COFF::IMAGE_COMDAT_SELECT_LARGEST
;
1604 case Comdat::NoDeduplicate
:
1605 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1606 case Comdat::SameSize
:
1607 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE
;
1610 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
1616 MCSection
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1617 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1619 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1620 StringRef Name
= GO
->getSection();
1621 StringRef COMDATSymName
= "";
1622 if (GO
->hasComdat()) {
1623 Selection
= getSelectionForCOFF(GO
);
1624 const GlobalValue
*ComdatGV
;
1625 if (Selection
== COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1626 ComdatGV
= getComdatGVForCOFF(GO
);
1630 if (!ComdatGV
->hasPrivateLinkage()) {
1631 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1632 COMDATSymName
= Sym
->getName();
1633 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1639 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, COMDATSymName
,
1643 static StringRef
getCOFFSectionNameForUniqueGlobal(SectionKind Kind
) {
1648 if (Kind
.isThreadLocal())
1650 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1655 MCSection
*TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1656 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
1657 // If we have -ffunction-sections then we should emit the global value to a
1658 // uniqued section specifically for it.
1659 bool EmitUniquedSection
;
1661 EmitUniquedSection
= TM
.getFunctionSections();
1663 EmitUniquedSection
= TM
.getDataSections();
1665 if ((EmitUniquedSection
&& !Kind
.isCommon()) || GO
->hasComdat()) {
1666 SmallString
<256> Name
= getCOFFSectionNameForUniqueGlobal(Kind
);
1668 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1670 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1671 int Selection
= getSelectionForCOFF(GO
);
1673 Selection
= COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
;
1674 const GlobalValue
*ComdatGV
;
1675 if (GO
->hasComdat())
1676 ComdatGV
= getComdatGVForCOFF(GO
);
1680 unsigned UniqueID
= MCContext::GenericSectionID
;
1681 if (EmitUniquedSection
)
1682 UniqueID
= NextUniqueID
++;
1684 if (!ComdatGV
->hasPrivateLinkage()) {
1685 MCSymbol
*Sym
= TM
.getSymbol(ComdatGV
);
1686 StringRef COMDATSymName
= Sym
->getName();
1688 if (const auto *F
= dyn_cast
<Function
>(GO
))
1689 if (Optional
<StringRef
> Prefix
= F
->getSectionPrefix())
1690 raw_svector_ostream(Name
) << '$' << *Prefix
;
1692 // Append "$symbol" to the section name *before* IR-level mangling is
1693 // applied when targetting mingw. This is what GCC does, and the ld.bfd
1694 // COFF linker will not properly handle comdats otherwise.
1695 if (getContext().getTargetTriple().isWindowsGNUEnvironment())
1696 raw_svector_ostream(Name
) << '$' << ComdatGV
->getName();
1698 return getContext().getCOFFSection(Name
, Characteristics
, Kind
,
1699 COMDATSymName
, Selection
, UniqueID
);
1701 SmallString
<256> TmpData
;
1702 getMangler().getNameWithPrefix(TmpData
, GO
, /*CannotUsePrivateLabel=*/true);
1703 return getContext().getCOFFSection(Name
, Characteristics
, Kind
, TmpData
,
1704 Selection
, UniqueID
);
1711 if (Kind
.isThreadLocal())
1712 return TLSDataSection
;
1714 if (Kind
.isReadOnly() || Kind
.isReadOnlyWithRel())
1715 return ReadOnlySection
;
1717 // Note: we claim that common symbols are put in BSSSection, but they are
1718 // really emitted with the magic .comm directive, which creates a symbol table
1719 // entry but not a section.
1720 if (Kind
.isBSS() || Kind
.isCommon())
1726 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1727 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
1728 const TargetMachine
&TM
) const {
1729 bool CannotUsePrivateLabel
= false;
1730 if (GV
->hasPrivateLinkage() &&
1731 ((isa
<Function
>(GV
) && TM
.getFunctionSections()) ||
1732 (isa
<GlobalVariable
>(GV
) && TM
.getDataSections())))
1733 CannotUsePrivateLabel
= true;
1735 getMangler().getNameWithPrefix(OutName
, GV
, CannotUsePrivateLabel
);
1738 MCSection
*TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1739 const Function
&F
, const TargetMachine
&TM
) const {
1740 // If the function can be removed, produce a unique section so that
1741 // the table doesn't prevent the removal.
1742 const Comdat
*C
= F
.getComdat();
1743 bool EmitUniqueSection
= TM
.getFunctionSections() || C
;
1744 if (!EmitUniqueSection
)
1745 return ReadOnlySection
;
1747 // FIXME: we should produce a symbol for F instead.
1748 if (F
.hasPrivateLinkage())
1749 return ReadOnlySection
;
1751 MCSymbol
*Sym
= TM
.getSymbol(&F
);
1752 StringRef COMDATSymName
= Sym
->getName();
1754 SectionKind Kind
= SectionKind::getReadOnly();
1755 StringRef SecName
= getCOFFSectionNameForUniqueGlobal(Kind
);
1756 unsigned Characteristics
= getCOFFSectionFlags(Kind
, TM
);
1757 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
1758 unsigned UniqueID
= NextUniqueID
++;
1760 return getContext().getCOFFSection(
1761 SecName
, Characteristics
, Kind
, COMDATSymName
,
1762 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
, UniqueID
);
1765 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer
&Streamer
,
1767 emitLinkerDirectives(Streamer
, M
);
1769 unsigned Version
= 0;
1773 GetObjCImageInfo(M
, Version
, Flags
, Section
);
1774 if (!Section
.empty()) {
1775 auto &C
= getContext();
1776 auto *S
= C
.getCOFFSection(Section
,
1777 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1778 COFF::IMAGE_SCN_MEM_READ
,
1779 SectionKind::getReadOnly());
1780 Streamer
.switchSection(S
);
1781 Streamer
.emitLabel(C
.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1782 Streamer
.emitInt32(Version
);
1783 Streamer
.emitInt32(Flags
);
1784 Streamer
.addBlankLine();
1787 emitCGProfileMetadata(Streamer
, M
);
1790 void TargetLoweringObjectFileCOFF::emitLinkerDirectives(
1791 MCStreamer
&Streamer
, Module
&M
) const {
1792 if (NamedMDNode
*LinkerOptions
= M
.getNamedMetadata("llvm.linker.options")) {
1793 // Emit the linker options to the linker .drectve section. According to the
1794 // spec, this section is a space-separated string containing flags for
1796 MCSection
*Sec
= getDrectveSection();
1797 Streamer
.switchSection(Sec
);
1798 for (const auto *Option
: LinkerOptions
->operands()) {
1799 for (const auto &Piece
: cast
<MDNode
>(Option
)->operands()) {
1800 // Lead with a space for consistency with our dllexport implementation.
1801 std::string
Directive(" ");
1802 Directive
.append(std::string(cast
<MDString
>(Piece
)->getString()));
1803 Streamer
.emitBytes(Directive
);
1808 // Emit /EXPORT: flags for each exported global as necessary.
1810 for (const GlobalValue
&GV
: M
.global_values()) {
1811 raw_string_ostream
OS(Flags
);
1812 emitLinkerFlagsForGlobalCOFF(OS
, &GV
, getContext().getTargetTriple(),
1815 if (!Flags
.empty()) {
1816 Streamer
.switchSection(getDrectveSection());
1817 Streamer
.emitBytes(Flags
);
1822 // Emit /INCLUDE: flags for each used global as necessary.
1823 if (const auto *LU
= M
.getNamedGlobal("llvm.used")) {
1824 assert(LU
->hasInitializer() && "expected llvm.used to have an initializer");
1825 assert(isa
<ArrayType
>(LU
->getValueType()) &&
1826 "expected llvm.used to be an array type");
1827 if (const auto *A
= cast
<ConstantArray
>(LU
->getInitializer())) {
1828 for (const Value
*Op
: A
->operands()) {
1829 const auto *GV
= cast
<GlobalValue
>(Op
->stripPointerCasts());
1830 // Global symbols with internal or private linkage are not visible to
1831 // the linker, and thus would cause an error when the linker tried to
1832 // preserve the symbol due to the `/include:` directive.
1833 if (GV
->hasLocalLinkage())
1836 raw_string_ostream
OS(Flags
);
1837 emitLinkerFlagsForUsedCOFF(OS
, GV
, getContext().getTargetTriple(),
1841 if (!Flags
.empty()) {
1842 Streamer
.switchSection(getDrectveSection());
1843 Streamer
.emitBytes(Flags
);
1851 void TargetLoweringObjectFileCOFF::Initialize(MCContext
&Ctx
,
1852 const TargetMachine
&TM
) {
1853 TargetLoweringObjectFile::Initialize(Ctx
, TM
);
1855 const Triple
&T
= TM
.getTargetTriple();
1856 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1858 Ctx
.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1859 COFF::IMAGE_SCN_MEM_READ
,
1860 SectionKind::getReadOnly());
1862 Ctx
.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1863 COFF::IMAGE_SCN_MEM_READ
,
1864 SectionKind::getReadOnly());
1866 StaticCtorSection
= Ctx
.getCOFFSection(
1867 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1868 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1869 SectionKind::getData());
1870 StaticDtorSection
= Ctx
.getCOFFSection(
1871 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1872 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
1873 SectionKind::getData());
1877 static MCSectionCOFF
*getCOFFStaticStructorSection(MCContext
&Ctx
,
1878 const Triple
&T
, bool IsCtor
,
1880 const MCSymbol
*KeySym
,
1881 MCSectionCOFF
*Default
) {
1882 if (T
.isWindowsMSVCEnvironment() || T
.isWindowsItaniumEnvironment()) {
1883 // If the priority is the default, use .CRT$XCU, possibly associative.
1884 if (Priority
== 65535)
1885 return Ctx
.getAssociativeCOFFSection(Default
, KeySym
, 0);
1887 // Otherwise, we need to compute a new section name. Low priorities should
1888 // run earlier. The linker will sort sections ASCII-betically, and we need a
1889 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1890 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1891 // low priorities need to sort before 'L', since the CRT uses that
1892 // internally, so we use ".CRT$XCA00001" for them. We have a contract with
1893 // the frontend that "init_seg(compiler)" corresponds to priority 200 and
1894 // "init_seg(lib)" corresponds to priority 400, and those respectively use
1895 // 'C' and 'L' without the priority suffix. Priorities between 200 and 400
1896 // use 'C' with the priority as a suffix.
1897 SmallString
<24> Name
;
1898 char LastLetter
= 'T';
1899 bool AddPrioritySuffix
= Priority
!= 200 && Priority
!= 400;
1902 else if (Priority
< 400)
1904 else if (Priority
== 400)
1906 raw_svector_ostream
OS(Name
);
1907 OS
<< ".CRT$X" << (IsCtor
? "C" : "T") << LastLetter
;
1908 if (AddPrioritySuffix
)
1909 OS
<< format("%05u", Priority
);
1910 MCSectionCOFF
*Sec
= Ctx
.getCOFFSection(
1911 Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
1912 SectionKind::getReadOnly());
1913 return Ctx
.getAssociativeCOFFSection(Sec
, KeySym
, 0);
1916 std::string Name
= IsCtor
? ".ctors" : ".dtors";
1917 if (Priority
!= 65535)
1918 raw_string_ostream(Name
) << format(".%05u", 65535 - Priority
);
1920 return Ctx
.getAssociativeCOFFSection(
1921 Ctx
.getCOFFSection(Name
, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
1922 COFF::IMAGE_SCN_MEM_READ
|
1923 COFF::IMAGE_SCN_MEM_WRITE
,
1924 SectionKind::getData()),
1928 MCSection
*TargetLoweringObjectFileCOFF::getStaticCtorSection(
1929 unsigned Priority
, const MCSymbol
*KeySym
) const {
1930 return getCOFFStaticStructorSection(
1931 getContext(), getContext().getTargetTriple(), true, Priority
, KeySym
,
1932 cast
<MCSectionCOFF
>(StaticCtorSection
));
1935 MCSection
*TargetLoweringObjectFileCOFF::getStaticDtorSection(
1936 unsigned Priority
, const MCSymbol
*KeySym
) const {
1937 return getCOFFStaticStructorSection(
1938 getContext(), getContext().getTargetTriple(), false, Priority
, KeySym
,
1939 cast
<MCSectionCOFF
>(StaticDtorSection
));
1942 const MCExpr
*TargetLoweringObjectFileCOFF::lowerRelativeReference(
1943 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
1944 const TargetMachine
&TM
) const {
1945 const Triple
&T
= TM
.getTargetTriple();
1946 if (T
.isOSCygMing())
1949 // Our symbols should exist in address space zero, cowardly no-op if
1951 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
1952 RHS
->getType()->getPointerAddressSpace() != 0)
1955 // Both ptrtoint instructions must wrap global objects:
1956 // - Only global variables are eligible for image relative relocations.
1957 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1958 // We expect __ImageBase to be a global variable without a section, externally
1961 // It should look something like this: @__ImageBase = external constant i8
1962 if (!isa
<GlobalObject
>(LHS
) || !isa
<GlobalVariable
>(RHS
) ||
1963 LHS
->isThreadLocal() || RHS
->isThreadLocal() ||
1964 RHS
->getName() != "__ImageBase" || !RHS
->hasExternalLinkage() ||
1965 cast
<GlobalVariable
>(RHS
)->hasInitializer() || RHS
->hasSection())
1968 return MCSymbolRefExpr::create(TM
.getSymbol(LHS
),
1969 MCSymbolRefExpr::VK_COFF_IMGREL32
,
1973 static std::string
APIntToHexString(const APInt
&AI
) {
1974 unsigned Width
= (AI
.getBitWidth() / 8) * 2;
1975 std::string HexString
= toString(AI
, 16, /*Signed=*/false);
1976 llvm::transform(HexString
, HexString
.begin(), tolower
);
1977 unsigned Size
= HexString
.size();
1978 assert(Width
>= Size
&& "hex string is too large!");
1979 HexString
.insert(HexString
.begin(), Width
- Size
, '0');
1984 static std::string
scalarConstantToHexString(const Constant
*C
) {
1985 Type
*Ty
= C
->getType();
1986 if (isa
<UndefValue
>(C
)) {
1987 return APIntToHexString(APInt::getZero(Ty
->getPrimitiveSizeInBits()));
1988 } else if (const auto *CFP
= dyn_cast
<ConstantFP
>(C
)) {
1989 return APIntToHexString(CFP
->getValueAPF().bitcastToAPInt());
1990 } else if (const auto *CI
= dyn_cast
<ConstantInt
>(C
)) {
1991 return APIntToHexString(CI
->getValue());
1993 unsigned NumElements
;
1994 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
1995 NumElements
= cast
<FixedVectorType
>(VTy
)->getNumElements();
1997 NumElements
= Ty
->getArrayNumElements();
1998 std::string HexString
;
1999 for (int I
= NumElements
- 1, E
= -1; I
!= E
; --I
)
2000 HexString
+= scalarConstantToHexString(C
->getAggregateElement(I
));
2005 MCSection
*TargetLoweringObjectFileCOFF::getSectionForConstant(
2006 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
2007 Align
&Alignment
) const {
2008 if (Kind
.isMergeableConst() && C
&&
2009 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
2010 // This creates comdat sections with the given symbol name, but unless
2011 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
2012 // will be created with a null storage class, which makes GNU binutils
2014 const unsigned Characteristics
= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
2015 COFF::IMAGE_SCN_MEM_READ
|
2016 COFF::IMAGE_SCN_LNK_COMDAT
;
2017 std::string COMDATSymName
;
2018 if (Kind
.isMergeableConst4()) {
2019 if (Alignment
<= 4) {
2020 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
2021 Alignment
= Align(4);
2023 } else if (Kind
.isMergeableConst8()) {
2024 if (Alignment
<= 8) {
2025 COMDATSymName
= "__real@" + scalarConstantToHexString(C
);
2026 Alignment
= Align(8);
2028 } else if (Kind
.isMergeableConst16()) {
2029 // FIXME: These may not be appropriate for non-x86 architectures.
2030 if (Alignment
<= 16) {
2031 COMDATSymName
= "__xmm@" + scalarConstantToHexString(C
);
2032 Alignment
= Align(16);
2034 } else if (Kind
.isMergeableConst32()) {
2035 if (Alignment
<= 32) {
2036 COMDATSymName
= "__ymm@" + scalarConstantToHexString(C
);
2037 Alignment
= Align(32);
2041 if (!COMDATSymName
.empty())
2042 return getContext().getCOFFSection(".rdata", Characteristics
, Kind
,
2044 COFF::IMAGE_COMDAT_SELECT_ANY
);
2047 return TargetLoweringObjectFile::getSectionForConstant(DL
, Kind
, C
,
2051 //===----------------------------------------------------------------------===//
2053 //===----------------------------------------------------------------------===//
2055 static const Comdat
*getWasmComdat(const GlobalValue
*GV
) {
2056 const Comdat
*C
= GV
->getComdat();
2060 if (C
->getSelectionKind() != Comdat::Any
)
2061 report_fatal_error("WebAssembly COMDATs only support "
2062 "SelectionKind::Any, '" + C
->getName() + "' cannot be "
2068 static unsigned getWasmSectionFlags(SectionKind K
) {
2071 if (K
.isThreadLocal())
2072 Flags
|= wasm::WASM_SEG_FLAG_TLS
;
2074 if (K
.isMergeableCString())
2075 Flags
|= wasm::WASM_SEG_FLAG_STRINGS
;
2077 // TODO(sbc): Add suport for K.isMergeableConst()
2082 MCSection
*TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
2083 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2084 // We don't support explict section names for functions in the wasm object
2085 // format. Each function has to be in its own unique section.
2086 if (isa
<Function
>(GO
)) {
2087 return SelectSectionForGlobal(GO
, Kind
, TM
);
2090 StringRef Name
= GO
->getSection();
2092 // Certain data sections we treat as named custom sections rather than
2093 // segments within the data section.
2094 // This could be avoided if all data segements (the wasm sense) were
2095 // represented as their own sections (in the llvm sense).
2096 // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
2097 if (Name
== ".llvmcmd" || Name
== ".llvmbc")
2098 Kind
= SectionKind::getMetadata();
2100 StringRef Group
= "";
2101 if (const Comdat
*C
= getWasmComdat(GO
)) {
2102 Group
= C
->getName();
2105 unsigned Flags
= getWasmSectionFlags(Kind
);
2106 MCSectionWasm
*Section
= getContext().getWasmSection(
2107 Name
, Kind
, Flags
, Group
, MCContext::GenericSectionID
);
2112 static MCSectionWasm
*selectWasmSectionForGlobal(
2113 MCContext
&Ctx
, const GlobalObject
*GO
, SectionKind Kind
, Mangler
&Mang
,
2114 const TargetMachine
&TM
, bool EmitUniqueSection
, unsigned *NextUniqueID
) {
2115 StringRef Group
= "";
2116 if (const Comdat
*C
= getWasmComdat(GO
)) {
2117 Group
= C
->getName();
2120 bool UniqueSectionNames
= TM
.getUniqueSectionNames();
2121 SmallString
<128> Name
= getSectionPrefixForGlobal(Kind
);
2123 if (const auto *F
= dyn_cast
<Function
>(GO
)) {
2124 const auto &OptionalPrefix
= F
->getSectionPrefix();
2126 raw_svector_ostream(Name
) << '.' << *OptionalPrefix
;
2129 if (EmitUniqueSection
&& UniqueSectionNames
) {
2130 Name
.push_back('.');
2131 TM
.getNameWithPrefix(Name
, GO
, Mang
, true);
2133 unsigned UniqueID
= MCContext::GenericSectionID
;
2134 if (EmitUniqueSection
&& !UniqueSectionNames
) {
2135 UniqueID
= *NextUniqueID
;
2139 unsigned Flags
= getWasmSectionFlags(Kind
);
2140 return Ctx
.getWasmSection(Name
, Kind
, Flags
, Group
, UniqueID
);
2143 MCSection
*TargetLoweringObjectFileWasm::SelectSectionForGlobal(
2144 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2146 if (Kind
.isCommon())
2147 report_fatal_error("mergable sections not supported yet on wasm");
2149 // If we have -ffunction-section or -fdata-section then we should emit the
2150 // global value to a uniqued section specifically for it.
2151 bool EmitUniqueSection
= false;
2153 EmitUniqueSection
= TM
.getFunctionSections();
2155 EmitUniqueSection
= TM
.getDataSections();
2156 EmitUniqueSection
|= GO
->hasComdat();
2158 return selectWasmSectionForGlobal(getContext(), GO
, Kind
, getMangler(), TM
,
2159 EmitUniqueSection
, &NextUniqueID
);
2162 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
2163 bool UsesLabelDifference
, const Function
&F
) const {
2164 // We can always create relative relocations, so use another section
2165 // that can be marked non-executable.
2169 const MCExpr
*TargetLoweringObjectFileWasm::lowerRelativeReference(
2170 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
2171 const TargetMachine
&TM
) const {
2172 // We may only use a PLT-relative relocation to refer to unnamed_addr
2174 if (!LHS
->hasGlobalUnnamedAddr() || !LHS
->getValueType()->isFunctionTy())
2177 // Basic correctness checks.
2178 if (LHS
->getType()->getPointerAddressSpace() != 0 ||
2179 RHS
->getType()->getPointerAddressSpace() != 0 || LHS
->isThreadLocal() ||
2180 RHS
->isThreadLocal())
2183 return MCBinaryExpr::createSub(
2184 MCSymbolRefExpr::create(TM
.getSymbol(LHS
), MCSymbolRefExpr::VK_None
,
2186 MCSymbolRefExpr::create(TM
.getSymbol(RHS
), getContext()), getContext());
2189 void TargetLoweringObjectFileWasm::InitializeWasm() {
2191 getContext().getWasmSection(".init_array", SectionKind::getData());
2193 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
2194 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
2195 TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
2198 MCSection
*TargetLoweringObjectFileWasm::getStaticCtorSection(
2199 unsigned Priority
, const MCSymbol
*KeySym
) const {
2200 return Priority
== UINT16_MAX
?
2202 getContext().getWasmSection(".init_array." + utostr(Priority
),
2203 SectionKind::getData());
2206 MCSection
*TargetLoweringObjectFileWasm::getStaticDtorSection(
2207 unsigned Priority
, const MCSymbol
*KeySym
) const {
2208 report_fatal_error("@llvm.global_dtors should have been lowered already");
2211 //===----------------------------------------------------------------------===//
2213 //===----------------------------------------------------------------------===//
2214 bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(
2215 const MachineFunction
*MF
) {
2216 if (!MF
->getLandingPads().empty())
2219 const Function
&F
= MF
->getFunction();
2220 if (!F
.hasPersonalityFn() || !F
.needsUnwindTableEntry())
2223 const GlobalValue
*Per
=
2224 dyn_cast
<GlobalValue
>(F
.getPersonalityFn()->stripPointerCasts());
2225 assert(Per
&& "Personality routine is not a GlobalValue type.");
2226 if (isNoOpWithoutInvoke(classifyEHPersonality(Per
)))
2232 bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(
2233 const MachineFunction
*MF
) {
2234 const Function
&F
= MF
->getFunction();
2235 if (!F
.hasStackProtectorFnAttr())
2237 // FIXME: check presence of canary word
2238 // There are cases that the stack protectors are not really inserted even if
2239 // the attributes are on.
2244 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction
*MF
) {
2245 return MF
->getMMI().getContext().getOrCreateSymbol(
2246 "__ehinfo." + Twine(MF
->getFunctionNumber()));
2250 TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue
*GV
,
2251 const TargetMachine
&TM
) const {
2252 // We always use a qualname symbol for a GV that represents
2253 // a declaration, a function descriptor, or a common symbol.
2254 // If a GV represents a GlobalVariable and -fdata-sections is enabled, we
2255 // also return a qualname so that a label symbol could be avoided.
2256 // It is inherently ambiguous when the GO represents the address of a
2257 // function, as the GO could either represent a function descriptor or a
2258 // function entry point. We choose to always return a function descriptor
2260 if (const GlobalObject
*GO
= dyn_cast
<GlobalObject
>(GV
)) {
2261 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
2262 if (GVar
->hasAttribute("toc-data"))
2263 return cast
<MCSectionXCOFF
>(
2264 SectionForGlobal(GVar
, SectionKind::getData(), TM
))
2265 ->getQualNameSymbol();
2267 if (GO
->isDeclarationForLinker())
2268 return cast
<MCSectionXCOFF
>(getSectionForExternalReference(GO
, TM
))
2269 ->getQualNameSymbol();
2271 SectionKind GOKind
= getKindForGlobal(GO
, TM
);
2272 if (GOKind
.isText())
2273 return cast
<MCSectionXCOFF
>(
2274 getSectionForFunctionDescriptor(cast
<Function
>(GO
), TM
))
2275 ->getQualNameSymbol();
2276 if ((TM
.getDataSections() && !GO
->hasSection()) || GO
->hasCommonLinkage() ||
2277 GOKind
.isBSSLocal() || GOKind
.isThreadBSSLocal())
2278 return cast
<MCSectionXCOFF
>(SectionForGlobal(GO
, GOKind
, TM
))
2279 ->getQualNameSymbol();
2282 // For all other cases, fall back to getSymbol to return the unqualified name.
2286 MCSection
*TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
2287 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2288 if (!GO
->hasSection())
2289 report_fatal_error("#pragma clang section is not yet supported");
2291 StringRef SectionName
= GO
->getSection();
2293 // Handle the XCOFF::TD case first, then deal with the rest.
2294 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GO
))
2295 if (GVar
->hasAttribute("toc-data"))
2296 return getContext().getXCOFFSection(
2298 XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD
, XCOFF::XTY_SD
),
2299 /* MultiSymbolsAllowed*/ true);
2301 XCOFF::StorageMappingClass MappingClass
;
2303 MappingClass
= XCOFF::XMC_PR
;
2304 else if (Kind
.isData() || Kind
.isReadOnlyWithRel() || Kind
.isBSS())
2305 MappingClass
= XCOFF::XMC_RW
;
2306 else if (Kind
.isReadOnly())
2307 MappingClass
= XCOFF::XMC_RO
;
2309 report_fatal_error("XCOFF other section types not yet implemented.");
2311 return getContext().getXCOFFSection(
2312 SectionName
, Kind
, XCOFF::CsectProperties(MappingClass
, XCOFF::XTY_SD
),
2313 /* MultiSymbolsAllowed*/ true);
2316 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForExternalReference(
2317 const GlobalObject
*GO
, const TargetMachine
&TM
) const {
2318 assert(GO
->isDeclarationForLinker() &&
2319 "Tried to get ER section for a defined global.");
2321 SmallString
<128> Name
;
2322 getNameWithPrefix(Name
, GO
, TM
);
2324 XCOFF::StorageMappingClass SMC
=
2325 isa
<Function
>(GO
) ? XCOFF::XMC_DS
: XCOFF::XMC_UA
;
2326 if (GO
->isThreadLocal())
2327 SMC
= XCOFF::XMC_UL
;
2329 // Externals go into a csect of type ER.
2330 return getContext().getXCOFFSection(
2331 Name
, SectionKind::getMetadata(),
2332 XCOFF::CsectProperties(SMC
, XCOFF::XTY_ER
));
2335 MCSection
*TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
2336 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2337 // Handle the XCOFF::TD case first, then deal with the rest.
2338 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GO
))
2339 if (GVar
->hasAttribute("toc-data")) {
2340 SmallString
<128> Name
;
2341 getNameWithPrefix(Name
, GO
, TM
);
2342 return getContext().getXCOFFSection(
2343 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_TD
, XCOFF::XTY_SD
),
2344 /* MultiSymbolsAllowed*/ true);
2347 // Common symbols go into a csect with matching name which will get mapped
2348 // into the .bss section.
2349 // Zero-initialized local TLS symbols go into a csect with matching name which
2350 // will get mapped into the .tbss section.
2351 if (Kind
.isBSSLocal() || GO
->hasCommonLinkage() || Kind
.isThreadBSSLocal()) {
2352 SmallString
<128> Name
;
2353 getNameWithPrefix(Name
, GO
, TM
);
2354 XCOFF::StorageMappingClass SMC
= Kind
.isBSSLocal() ? XCOFF::XMC_BS
2355 : Kind
.isCommon() ? XCOFF::XMC_RW
2357 return getContext().getXCOFFSection(
2358 Name
, Kind
, XCOFF::CsectProperties(SMC
, XCOFF::XTY_CM
));
2361 if (Kind
.isMergeableCString()) {
2362 Align Alignment
= GO
->getParent()->getDataLayout().getPreferredAlign(
2363 cast
<GlobalVariable
>(GO
));
2365 unsigned EntrySize
= getEntrySizeForKind(Kind
);
2366 std::string SizeSpec
= ".rodata.str" + utostr(EntrySize
) + ".";
2367 SmallString
<128> Name
;
2368 Name
= SizeSpec
+ utostr(Alignment
.value());
2370 if (TM
.getDataSections())
2371 getNameWithPrefix(Name
, GO
, TM
);
2373 return getContext().getXCOFFSection(
2374 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
),
2375 /* MultiSymbolsAllowed*/ !TM
.getDataSections());
2378 if (Kind
.isText()) {
2379 if (TM
.getFunctionSections()) {
2380 return cast
<MCSymbolXCOFF
>(getFunctionEntryPointSymbol(GO
, TM
))
2381 ->getRepresentedCsect();
2386 // TODO: We may put Kind.isReadOnlyWithRel() under option control, because
2387 // user may want to have read-only data with relocations placed into a
2388 // read-only section by the compiler.
2389 // For BSS kind, zero initialized data must be emitted to the .data section
2390 // because external linkage control sections that get mapped to the .bss
2391 // section will be linked as tentative defintions, which is only appropriate
2392 // for SectionKind::Common.
2393 if (Kind
.isData() || Kind
.isReadOnlyWithRel() || Kind
.isBSS()) {
2394 if (TM
.getDataSections()) {
2395 SmallString
<128> Name
;
2396 getNameWithPrefix(Name
, GO
, TM
);
2397 return getContext().getXCOFFSection(
2398 Name
, SectionKind::getData(),
2399 XCOFF::CsectProperties(XCOFF::XMC_RW
, XCOFF::XTY_SD
));
2404 if (Kind
.isReadOnly()) {
2405 if (TM
.getDataSections()) {
2406 SmallString
<128> Name
;
2407 getNameWithPrefix(Name
, GO
, TM
);
2408 return getContext().getXCOFFSection(
2409 Name
, SectionKind::getReadOnly(),
2410 XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
));
2412 return ReadOnlySection
;
2415 // External/weak TLS data and initialized local TLS data are not eligible
2416 // to be put into common csect. If data sections are enabled, thread
2417 // data are emitted into separate sections. Otherwise, thread data
2418 // are emitted into the .tdata section.
2419 if (Kind
.isThreadLocal()) {
2420 if (TM
.getDataSections()) {
2421 SmallString
<128> Name
;
2422 getNameWithPrefix(Name
, GO
, TM
);
2423 return getContext().getXCOFFSection(
2424 Name
, Kind
, XCOFF::CsectProperties(XCOFF::XMC_TL
, XCOFF::XTY_SD
));
2426 return TLSDataSection
;
2429 report_fatal_error("XCOFF other section types not yet implemented.");
2432 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForJumpTable(
2433 const Function
&F
, const TargetMachine
&TM
) const {
2434 assert (!F
.getComdat() && "Comdat not supported on XCOFF.");
2436 if (!TM
.getFunctionSections())
2437 return ReadOnlySection
;
2439 // If the function can be removed, produce a unique section so that
2440 // the table doesn't prevent the removal.
2441 SmallString
<128> NameStr(".rodata.jmp..");
2442 getNameWithPrefix(NameStr
, &F
, TM
);
2443 return getContext().getXCOFFSection(
2444 NameStr
, SectionKind::getReadOnly(),
2445 XCOFF::CsectProperties(XCOFF::XMC_RO
, XCOFF::XTY_SD
));
2448 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
2449 bool UsesLabelDifference
, const Function
&F
) const {
2453 /// Given a mergeable constant with the specified size and relocation
2454 /// information, return a section that it should be placed in.
2455 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForConstant(
2456 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
2457 Align
&Alignment
) const {
2458 // TODO: Enable emiting constant pool to unique sections when we support it.
2459 if (Alignment
> Align(16))
2460 report_fatal_error("Alignments greater than 16 not yet supported.");
2462 if (Alignment
== Align(8)) {
2463 assert(ReadOnly8Section
&& "Section should always be initialized.");
2464 return ReadOnly8Section
;
2467 if (Alignment
== Align(16)) {
2468 assert(ReadOnly16Section
&& "Section should always be initialized.");
2469 return ReadOnly16Section
;
2472 return ReadOnlySection
;
2475 void TargetLoweringObjectFileXCOFF::Initialize(MCContext
&Ctx
,
2476 const TargetMachine
&TgtM
) {
2477 TargetLoweringObjectFile::Initialize(Ctx
, TgtM
);
2479 dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_datarel
|
2480 (TgtM
.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4
2481 : dwarf::DW_EH_PE_sdata8
);
2482 PersonalityEncoding
= 0;
2484 CallSiteEncoding
= dwarf::DW_EH_PE_udata4
;
2486 // AIX debug for thread local location is not ready. And for integrated as
2487 // mode, the relocatable address for the thread local variable will cause
2488 // linker error. So disable the location attribute generation for thread local
2489 // variables for now.
2490 // FIXME: when TLS debug on AIX is ready, remove this setting.
2491 SupportDebugThreadLocalLocation
= false;
2494 MCSection
*TargetLoweringObjectFileXCOFF::getStaticCtorSection(
2495 unsigned Priority
, const MCSymbol
*KeySym
) const {
2496 report_fatal_error("no static constructor section on AIX");
2499 MCSection
*TargetLoweringObjectFileXCOFF::getStaticDtorSection(
2500 unsigned Priority
, const MCSymbol
*KeySym
) const {
2501 report_fatal_error("no static destructor section on AIX");
2504 const MCExpr
*TargetLoweringObjectFileXCOFF::lowerRelativeReference(
2505 const GlobalValue
*LHS
, const GlobalValue
*RHS
,
2506 const TargetMachine
&TM
) const {
2507 /* Not implemented yet, but don't crash, return nullptr. */
2512 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue
*GV
) {
2513 assert(!isa
<GlobalIFunc
>(GV
) && "GlobalIFunc is not supported on AIX.");
2515 switch (GV
->getLinkage()) {
2516 case GlobalValue::InternalLinkage
:
2517 case GlobalValue::PrivateLinkage
:
2518 return XCOFF::C_HIDEXT
;
2519 case GlobalValue::ExternalLinkage
:
2520 case GlobalValue::CommonLinkage
:
2521 case GlobalValue::AvailableExternallyLinkage
:
2522 return XCOFF::C_EXT
;
2523 case GlobalValue::ExternalWeakLinkage
:
2524 case GlobalValue::LinkOnceAnyLinkage
:
2525 case GlobalValue::LinkOnceODRLinkage
:
2526 case GlobalValue::WeakAnyLinkage
:
2527 case GlobalValue::WeakODRLinkage
:
2528 return XCOFF::C_WEAKEXT
;
2529 case GlobalValue::AppendingLinkage
:
2531 "There is no mapping that implements AppendingLinkage for XCOFF.");
2533 llvm_unreachable("Unknown linkage type!");
2536 MCSymbol
*TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol(
2537 const GlobalValue
*Func
, const TargetMachine
&TM
) const {
2538 assert((isa
<Function
>(Func
) ||
2539 (isa
<GlobalAlias
>(Func
) &&
2540 isa_and_nonnull
<Function
>(
2541 cast
<GlobalAlias
>(Func
)->getAliaseeObject()))) &&
2542 "Func must be a function or an alias which has a function as base "
2545 SmallString
<128> NameStr
;
2546 NameStr
.push_back('.');
2547 getNameWithPrefix(NameStr
, Func
, TM
);
2549 // When -function-sections is enabled and explicit section is not specified,
2550 // it's not necessary to emit function entry point label any more. We will use
2551 // function entry point csect instead. And for function delcarations, the
2552 // undefined symbols gets treated as csect with XTY_ER property.
2553 if (((TM
.getFunctionSections() && !Func
->hasSection()) ||
2554 Func
->isDeclaration()) &&
2555 isa
<Function
>(Func
)) {
2558 NameStr
, SectionKind::getText(),
2559 XCOFF::CsectProperties(XCOFF::XMC_PR
, Func
->isDeclaration()
2562 ->getQualNameSymbol();
2565 return getContext().getOrCreateSymbol(NameStr
);
2568 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor(
2569 const Function
*F
, const TargetMachine
&TM
) const {
2570 SmallString
<128> NameStr
;
2571 getNameWithPrefix(NameStr
, F
, TM
);
2572 return getContext().getXCOFFSection(
2573 NameStr
, SectionKind::getData(),
2574 XCOFF::CsectProperties(XCOFF::XMC_DS
, XCOFF::XTY_SD
));
2577 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForTOCEntry(
2578 const MCSymbol
*Sym
, const TargetMachine
&TM
) const {
2579 // Use TE storage-mapping class when large code model is enabled so that
2580 // the chance of needing -bbigtoc is decreased.
2581 return getContext().getXCOFFSection(
2582 cast
<MCSymbolXCOFF
>(Sym
)->getSymbolTableName(), SectionKind::getData(),
2583 XCOFF::CsectProperties(
2584 TM
.getCodeModel() == CodeModel::Large
? XCOFF::XMC_TE
: XCOFF::XMC_TC
,
2588 MCSection
*TargetLoweringObjectFileXCOFF::getSectionForLSDA(
2589 const Function
&F
, const MCSymbol
&FnSym
, const TargetMachine
&TM
) const {
2590 auto *LSDA
= cast
<MCSectionXCOFF
>(LSDASection
);
2591 if (TM
.getFunctionSections()) {
2592 // If option -ffunction-sections is on, append the function name to the
2593 // name of the LSDA csect so that each function has its own LSDA csect.
2594 // This helps the linker to garbage-collect EH info of unused functions.
2595 SmallString
<128> NameStr
= LSDA
->getName();
2596 raw_svector_ostream(NameStr
) << '.' << F
.getName();
2597 LSDA
= getContext().getXCOFFSection(NameStr
, LSDA
->getKind(),
2598 LSDA
->getCsectProp());
2602 //===----------------------------------------------------------------------===//
2604 //===----------------------------------------------------------------------===//
2605 TargetLoweringObjectFileGOFF::TargetLoweringObjectFileGOFF() = default;
2607 MCSection
*TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
2608 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2609 return SelectSectionForGlobal(GO
, Kind
, TM
);
2612 MCSection
*TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
2613 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
2614 auto *Symbol
= TM
.getSymbol(GO
);
2616 return getContext().getGOFFSection(Symbol
->getName(), SectionKind::getBSS(),
2619 return getContext().getObjectFileInfo()->getTextSection();