1 //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements classes used to handle lowerings specific to common
10 // object file formats.
11 //
12 //===----------------------------------------------------------------------===//
13
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/BinaryFormat/COFF.h"
20 #include "llvm/BinaryFormat/Dwarf.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/BinaryFormat/MachO.h"
23 #include "llvm/BinaryFormat/Wasm.h"
24 #include "llvm/CodeGen/BasicBlockSectionUtils.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
29 #include "llvm/IR/Comdat.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/DiagnosticInfo.h"
34 #include "llvm/IR/DiagnosticPrinter.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/GlobalAlias.h"
37 #include "llvm/IR/GlobalObject.h"
38 #include "llvm/IR/GlobalValue.h"
39 #include "llvm/IR/GlobalVariable.h"
40 #include "llvm/IR/Mangler.h"
41 #include "llvm/IR/Metadata.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/PseudoProbe.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/MC/MCAsmInfo.h"
46 #include "llvm/MC/MCAsmInfoDarwin.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/Base64.h"
62 #include "llvm/Support/Casting.h"
63 #include "llvm/Support/CodeGen.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/Format.h"
66 #include "llvm/Support/raw_ostream.h"
67 #include "llvm/Target/TargetMachine.h"
68 #include "llvm/TargetParser/Triple.h"
69 #include <cassert>
70 #include <string>
71
72 using namespace llvm;
73 using namespace dwarf;
74
75 static cl::opt<bool> JumpTableInFunctionSection(
76 "jumptable-in-function-section", cl::Hidden, cl::init(false),
77 cl::desc("Putting Jump Table in function section"));
78
GetObjCImageInfo(Module & M,unsigned & Version,unsigned & Flags,StringRef & Section)79 static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
80 StringRef &Section) {
81 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
82 M.getModuleFlagsMetadata(ModuleFlags);
83
84 for (const auto &MFE: ModuleFlags) {
85 // Ignore flags with 'Require' behaviour.
86 if (MFE.Behavior == Module::Require)
87 continue;
88
89 StringRef Key = MFE.Key->getString();
90 if (Key == "Objective-C Image Info Version") {
91 Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
92 } else if (Key == "Objective-C Garbage Collection" ||
93 Key == "Objective-C GC Only" ||
94 Key == "Objective-C Is Simulated" ||
95 Key == "Objective-C Class Properties" ||
96 Key == "Objective-C Image Swift Version") {
97 Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
98 } else if (Key == "Objective-C Image Info Section") {
99 Section = cast<MDString>(MFE.Val)->getString();
100 }
101 // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor +
102 // "Objective-C Garbage Collection".
103 else if (Key == "Swift ABI Version") {
104 Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 8;
105 } else if (Key == "Swift Major Version") {
106 Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 24;
107 } else if (Key == "Swift Minor Version") {
108 Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 16;
109 }
110 }
111 }
112
113 //===----------------------------------------------------------------------===//
114 // ELF
115 //===----------------------------------------------------------------------===//
116
TargetLoweringObjectFileELF()117 TargetLoweringObjectFileELF::TargetLoweringObjectFileELF() {
118 SupportDSOLocalEquivalentLowering = true;
119 }
120
Initialize(MCContext & Ctx,const TargetMachine & TgtM)121 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
122 const TargetMachine &TgtM) {
123 TargetLoweringObjectFile::Initialize(Ctx, TgtM);
124
125 CodeModel::Model CM = TgtM.getCodeModel();
126 InitializeELF(TgtM.Options.UseInitArray);
127
128 switch (TgtM.getTargetTriple().getArch()) {
129 case Triple::arm:
130 case Triple::armeb:
131 case Triple::thumb:
132 case Triple::thumbeb:
133 if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
134 break;
135 // Fallthrough if not using EHABI
136 [[fallthrough]];
137 case Triple::ppc:
138 case Triple::ppcle:
139 case Triple::x86:
140 PersonalityEncoding = isPositionIndependent()
141 ? dwarf::DW_EH_PE_indirect |
142 dwarf::DW_EH_PE_pcrel |
143 dwarf::DW_EH_PE_sdata4
144 : dwarf::DW_EH_PE_absptr;
145 LSDAEncoding = isPositionIndependent()
146 ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
147 : dwarf::DW_EH_PE_absptr;
148 TTypeEncoding = isPositionIndependent()
149 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
150 dwarf::DW_EH_PE_sdata4
151 : dwarf::DW_EH_PE_absptr;
152 break;
153 case Triple::x86_64:
154 if (isPositionIndependent()) {
155 PersonalityEncoding = 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);
158 LSDAEncoding = dwarf::DW_EH_PE_pcrel |
159 (CM == CodeModel::Small
160 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
161 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
162 ((CM == CodeModel::Small || CM == CodeModel::Medium)
163 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
164 } else {
165 PersonalityEncoding =
166 (CM == CodeModel::Small || CM == CodeModel::Medium)
167 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
168 LSDAEncoding = (CM == CodeModel::Small)
169 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
170 TTypeEncoding = (CM == CodeModel::Small)
171 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
172 }
173 break;
174 case Triple::hexagon:
175 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
176 LSDAEncoding = dwarf::DW_EH_PE_absptr;
177 TTypeEncoding = dwarf::DW_EH_PE_absptr;
178 if (isPositionIndependent()) {
179 PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
180 LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
181 TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
182 }
183 break;
184 case Triple::aarch64:
185 case Triple::aarch64_be:
186 case Triple::aarch64_32:
187 // The small model guarantees static code/data size < 4GB, but not where it
188 // will be in memory. Most of these could end up >2GB away so even a signed
189 // pc-relative 32-bit address is insufficient, theoretically.
190 //
191 // Use DW_EH_PE_indirect even for -fno-pic to avoid copy relocations.
192 LSDAEncoding = dwarf::DW_EH_PE_pcrel |
193 (TgtM.getTargetTriple().getEnvironment() == Triple::GNUILP32
194 ? dwarf::DW_EH_PE_sdata4
195 : dwarf::DW_EH_PE_sdata8);
196 PersonalityEncoding = LSDAEncoding | dwarf::DW_EH_PE_indirect;
197 TTypeEncoding = LSDAEncoding | dwarf::DW_EH_PE_indirect;
198 break;
199 case Triple::lanai:
200 LSDAEncoding = dwarf::DW_EH_PE_absptr;
201 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
202 TTypeEncoding = dwarf::DW_EH_PE_absptr;
203 break;
204 case Triple::mips:
205 case Triple::mipsel:
206 case Triple::mips64:
207 case Triple::mips64el:
208 // MIPS uses indirect pointer to refer personality functions and types, so
209 // that the eh_frame section can be read-only. DW.ref.personality will be
210 // generated for relocation.
211 PersonalityEncoding = dwarf::DW_EH_PE_indirect;
212 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
213 // identify N64 from just a triple.
214 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
215 dwarf::DW_EH_PE_sdata4;
216
217 // FreeBSD must be explicit about the data size and using pcrel since it's
218 // assembler/linker won't do the automatic conversion that the Linux tools
219 // do.
220 if (isPositionIndependent() || TgtM.getTargetTriple().isOSFreeBSD()) {
221 PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
222 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
223 }
224 break;
225 case Triple::ppc64:
226 case Triple::ppc64le:
227 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
228 dwarf::DW_EH_PE_udata8;
229 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
230 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
231 dwarf::DW_EH_PE_udata8;
232 break;
233 case Triple::sparcel:
234 case Triple::sparc:
235 if (isPositionIndependent()) {
236 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
237 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
238 dwarf::DW_EH_PE_sdata4;
239 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
240 dwarf::DW_EH_PE_sdata4;
241 } else {
242 LSDAEncoding = dwarf::DW_EH_PE_absptr;
243 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
244 TTypeEncoding = dwarf::DW_EH_PE_absptr;
245 }
246 CallSiteEncoding = dwarf::DW_EH_PE_udata4;
247 break;
248 case Triple::riscv32:
249 case Triple::riscv64:
250 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
251 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
252 dwarf::DW_EH_PE_sdata4;
253 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
254 dwarf::DW_EH_PE_sdata4;
255 CallSiteEncoding = dwarf::DW_EH_PE_udata4;
256 break;
257 case Triple::sparcv9:
258 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
259 if (isPositionIndependent()) {
260 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
261 dwarf::DW_EH_PE_sdata4;
262 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
263 dwarf::DW_EH_PE_sdata4;
264 } else {
265 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
266 TTypeEncoding = dwarf::DW_EH_PE_absptr;
267 }
268 break;
269 case Triple::systemz:
270 // All currently-defined code models guarantee that 4-byte PC-relative
271 // values will be in range.
272 if (isPositionIndependent()) {
273 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
274 dwarf::DW_EH_PE_sdata4;
275 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
276 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
277 dwarf::DW_EH_PE_sdata4;
278 } else {
279 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
280 LSDAEncoding = dwarf::DW_EH_PE_absptr;
281 TTypeEncoding = dwarf::DW_EH_PE_absptr;
282 }
283 break;
284 case Triple::loongarch32:
285 case Triple::loongarch64:
286 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
287 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
288 dwarf::DW_EH_PE_sdata4;
289 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
290 dwarf::DW_EH_PE_sdata4;
291 break;
292 default:
293 break;
294 }
295 }
296
getModuleMetadata(Module & M)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))
302 Used.insert(GO);
303 }
304
emitModuleMetadata(MCStreamer & Streamer,Module & M) const305 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
306 Module &M) const {
307 auto &C = getContext();
308
309 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
310 auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
311 ELF::SHF_EXCLUDE);
312
313 Streamer.switchSection(S);
314
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);
321 }
322 }
323 }
324
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);
328
329 Streamer.switchSection(S);
330
331 for (const auto *Operand : DependentLibraries->operands()) {
332 Streamer.emitBytes(
333 cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString());
334 Streamer.emitInt8(0);
335 }
336 }
337
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());
352
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());
358 }
359 }
360
361 if (NamedMDNode *LLVMStats = M.getNamedMetadata("llvm.stats")) {
362 // Emit the metadata for llvm statistics into .llvm_stats section, which is
363 // formatted as a list of key/value pair, the value is base64 encoded.
364 auto *S = C.getObjectFileInfo()->getLLVMStatsSection();
365 Streamer.switchSection(S);
366 for (const auto *Operand : LLVMStats->operands()) {
367 const auto *MD = cast<MDNode>(Operand);
368 assert(MD->getNumOperands() % 2 == 0 &&
369 ("Operand num should be even for a list of key/value pair"));
370 for (size_t I = 0; I < MD->getNumOperands(); I += 2) {
371 // Encode the key string size.
372 auto *Key = cast<MDString>(MD->getOperand(I));
373 Streamer.emitULEB128IntValue(Key->getString().size());
374 Streamer.emitBytes(Key->getString());
375 // Encode the value into a Base64 string.
376 std::string Value = encodeBase64(
377 Twine(mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1))
378 ->getZExtValue())
379 .str());
380 Streamer.emitULEB128IntValue(Value.size());
381 Streamer.emitBytes(Value);
382 }
383 }
384 }
385
386 unsigned Version = 0;
387 unsigned Flags = 0;
388 StringRef Section;
389
390 GetObjCImageInfo(M, Version, Flags, Section);
391 if (!Section.empty()) {
392 auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
393 Streamer.switchSection(S);
394 Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
395 Streamer.emitInt32(Version);
396 Streamer.emitInt32(Flags);
397 Streamer.addBlankLine();
398 }
399
400 emitCGProfileMetadata(Streamer, M);
401 }
402
getCFIPersonalitySymbol(const GlobalValue * GV,const TargetMachine & TM,MachineModuleInfo * MMI) const403 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
404 const GlobalValue *GV, const TargetMachine &TM,
405 MachineModuleInfo *MMI) const {
406 unsigned Encoding = getPersonalityEncoding();
407 if ((Encoding & 0x80) == DW_EH_PE_indirect)
408 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
409 TM.getSymbol(GV)->getName());
410 if ((Encoding & 0x70) == DW_EH_PE_absptr)
411 return TM.getSymbol(GV);
412 report_fatal_error("We do not support this DWARF encoding yet!");
413 }
414
emitPersonalityValue(MCStreamer & Streamer,const DataLayout & DL,const MCSymbol * Sym) const415 void TargetLoweringObjectFileELF::emitPersonalityValue(
416 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
417 SmallString<64> NameData("DW.ref.");
418 NameData += Sym->getName();
419 MCSymbolELF *Label =
420 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
421 Streamer.emitSymbolAttribute(Label, MCSA_Hidden);
422 Streamer.emitSymbolAttribute(Label, MCSA_Weak);
423 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
424 MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
425 ELF::SHT_PROGBITS, Flags, 0);
426 unsigned Size = DL.getPointerSize();
427 Streamer.switchSection(Sec);
428 Streamer.emitValueToAlignment(DL.getPointerABIAlignment(0));
429 Streamer.emitSymbolAttribute(Label, MCSA_ELF_TypeObject);
430 const MCExpr *E = MCConstantExpr::create(Size, getContext());
431 Streamer.emitELFSize(Label, E);
432 Streamer.emitLabel(Label);
433
434 Streamer.emitSymbolValue(Sym, Size);
435 }
436
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const437 const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
438 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
439 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
440 if (Encoding & DW_EH_PE_indirect) {
441 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
442
443 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
444
445 // Add information about the stub reference to ELFMMI so that the stub
446 // gets emitted by the asmprinter.
447 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
448 if (!StubSym.getPointer()) {
449 MCSymbol *Sym = TM.getSymbol(GV);
450 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
451 }
452
453 return TargetLoweringObjectFile::
454 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
455 Encoding & ~DW_EH_PE_indirect, Streamer);
456 }
457
458 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
459 MMI, Streamer);
460 }
461
getELFKindForNamedSection(StringRef Name,SectionKind K)462 static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
463 // N.B.: The defaults used in here are not the same ones used in MC.
464 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
465 // both gas and MC will produce a section with no flags. Given
466 // section(".eh_frame") gcc will produce:
467 //
468 // .section .eh_frame,"a",@progbits
469
470 if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
471 /*AddSegmentInfo=*/false) ||
472 Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF,
473 /*AddSegmentInfo=*/false) ||
474 Name == getInstrProfSectionName(IPSK_covdata, Triple::ELF,
475 /*AddSegmentInfo=*/false) ||
476 Name == getInstrProfSectionName(IPSK_covname, Triple::ELF,
477 /*AddSegmentInfo=*/false) ||
478 Name == ".llvmbc" || Name == ".llvmcmd")
479 return SectionKind::getMetadata();
480
481 if (!Name.starts_with(".")) return K;
482
483 // Default implementation based on some magic section names.
484 if (Name == ".bss" || Name.starts_with(".bss.") ||
485 Name.starts_with(".gnu.linkonce.b.") ||
486 Name.starts_with(".llvm.linkonce.b.") || Name == ".sbss" ||
487 Name.starts_with(".sbss.") || Name.starts_with(".gnu.linkonce.sb.") ||
488 Name.starts_with(".llvm.linkonce.sb."))
489 return SectionKind::getBSS();
490
491 if (Name == ".tdata" || Name.starts_with(".tdata.") ||
492 Name.starts_with(".gnu.linkonce.td.") ||
493 Name.starts_with(".llvm.linkonce.td."))
494 return SectionKind::getThreadData();
495
496 if (Name == ".tbss" || Name.starts_with(".tbss.") ||
497 Name.starts_with(".gnu.linkonce.tb.") ||
498 Name.starts_with(".llvm.linkonce.tb."))
499 return SectionKind::getThreadBSS();
500
501 return K;
502 }
503
hasPrefix(StringRef SectionName,StringRef Prefix)504 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
505 return SectionName.consume_front(Prefix) &&
506 (SectionName.empty() || SectionName[0] == '.');
507 }
508
getELFSectionType(StringRef Name,SectionKind K)509 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
510 // Use SHT_NOTE for section whose name starts with ".note" to allow
511 // emitting ELF notes from C variable declaration.
512 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
513 if (Name.starts_with(".note"))
514 return ELF::SHT_NOTE;
515
516 if (hasPrefix(Name, ".init_array"))
517 return ELF::SHT_INIT_ARRAY;
518
519 if (hasPrefix(Name, ".fini_array"))
520 return ELF::SHT_FINI_ARRAY;
521
522 if (hasPrefix(Name, ".preinit_array"))
523 return ELF::SHT_PREINIT_ARRAY;
524
525 if (hasPrefix(Name, ".llvm.offloading"))
526 return ELF::SHT_LLVM_OFFLOADING;
527 if (Name == ".llvm.lto")
528 return ELF::SHT_LLVM_LTO;
529
530 if (K.isBSS() || K.isThreadBSS())
531 return ELF::SHT_NOBITS;
532
533 return ELF::SHT_PROGBITS;
534 }
535
getELFSectionFlags(SectionKind K)536 static unsigned getELFSectionFlags(SectionKind K) {
537 unsigned Flags = 0;
538
539 if (!K.isMetadata() && !K.isExclude())
540 Flags |= ELF::SHF_ALLOC;
541
542 if (K.isExclude())
543 Flags |= ELF::SHF_EXCLUDE;
544
545 if (K.isText())
546 Flags |= ELF::SHF_EXECINSTR;
547
548 if (K.isExecuteOnly())
549 Flags |= ELF::SHF_ARM_PURECODE;
550
551 if (K.isWriteable())
552 Flags |= ELF::SHF_WRITE;
553
554 if (K.isThreadLocal())
555 Flags |= ELF::SHF_TLS;
556
557 if (K.isMergeableCString() || K.isMergeableConst())
558 Flags |= ELF::SHF_MERGE;
559
560 if (K.isMergeableCString())
561 Flags |= ELF::SHF_STRINGS;
562
563 return Flags;
564 }
565
getELFComdat(const GlobalValue * GV)566 static const Comdat *getELFComdat(const GlobalValue *GV) {
567 const Comdat *C = GV->getComdat();
568 if (!C)
569 return nullptr;
570
571 if (C->getSelectionKind() != Comdat::Any &&
572 C->getSelectionKind() != Comdat::NoDeduplicate)
573 report_fatal_error("ELF COMDATs only support SelectionKind::Any and "
574 "SelectionKind::NoDeduplicate, '" +
575 C->getName() + "' cannot be lowered.");
576
577 return C;
578 }
579
getLinkedToSymbol(const GlobalObject * GO,const TargetMachine & TM)580 static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO,
581 const TargetMachine &TM) {
582 MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
583 if (!MD)
584 return nullptr;
585
586 auto *VM = cast<ValueAsMetadata>(MD->getOperand(0).get());
587 auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue());
588 return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr;
589 }
590
getEntrySizeForKind(SectionKind Kind)591 static unsigned getEntrySizeForKind(SectionKind Kind) {
592 if (Kind.isMergeable1ByteCString())
593 return 1;
594 else if (Kind.isMergeable2ByteCString())
595 return 2;
596 else if (Kind.isMergeable4ByteCString())
597 return 4;
598 else if (Kind.isMergeableConst4())
599 return 4;
600 else if (Kind.isMergeableConst8())
601 return 8;
602 else if (Kind.isMergeableConst16())
603 return 16;
604 else if (Kind.isMergeableConst32())
605 return 32;
606 else {
607 // We shouldn't have mergeable C strings or mergeable constants that we
608 // didn't handle above.
609 assert(!Kind.isMergeableCString() && "unknown string width");
610 assert(!Kind.isMergeableConst() && "unknown data width");
611 return 0;
612 }
613 }
614
615 /// Return the section prefix name used by options FunctionsSections and
616 /// DataSections.
getSectionPrefixForGlobal(SectionKind Kind,bool IsLarge)617 static StringRef getSectionPrefixForGlobal(SectionKind Kind, bool IsLarge) {
618 if (Kind.isText())
619 return IsLarge ? ".ltext" : ".text";
620 if (Kind.isReadOnly())
621 return IsLarge ? ".lrodata" : ".rodata";
622 if (Kind.isBSS())
623 return IsLarge ? ".lbss" : ".bss";
624 if (Kind.isThreadData())
625 return ".tdata";
626 if (Kind.isThreadBSS())
627 return ".tbss";
628 if (Kind.isData())
629 return IsLarge ? ".ldata" : ".data";
630 if (Kind.isReadOnlyWithRel())
631 return IsLarge ? ".ldata.rel.ro" : ".data.rel.ro";
632 llvm_unreachable("Unknown section kind");
633 }
634
635 static SmallString<128>
getELFSectionNameForGlobal(const GlobalObject * GO,SectionKind Kind,Mangler & Mang,const TargetMachine & TM,unsigned EntrySize,bool UniqueSectionName)636 getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
637 Mangler &Mang, const TargetMachine &TM,
638 unsigned EntrySize, bool UniqueSectionName) {
639 SmallString<128> Name =
640 getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO));
641 if (Kind.isMergeableCString()) {
642 // We also need alignment here.
643 // FIXME: this is getting the alignment of the character, not the
644 // alignment of the global!
645 Align Alignment = GO->getDataLayout().getPreferredAlign(
646 cast<GlobalVariable>(GO));
647
648 Name += ".str";
649 Name += utostr(EntrySize);
650 Name += ".";
651 Name += utostr(Alignment.value());
652 } else if (Kind.isMergeableConst()) {
653 Name += ".cst";
654 Name += utostr(EntrySize);
655 }
656
657 bool HasPrefix = false;
658 if (const auto *F = dyn_cast<Function>(GO)) {
659 if (std::optional<StringRef> Prefix = F->getSectionPrefix()) {
660 raw_svector_ostream(Name) << '.' << *Prefix;
661 HasPrefix = true;
662 }
663 }
664
665 if (UniqueSectionName) {
666 Name.push_back('.');
667 TM.getNameWithPrefix(Name, GO, Mang, /*MayAlwaysUsePrivate*/true);
668 } else if (HasPrefix)
669 // For distinguishing between .text.${text-section-prefix}. (with trailing
670 // dot) and .text.${function-name}
671 Name.push_back('.');
672 return Name;
673 }
674
675 namespace {
676 class LoweringDiagnosticInfo : public DiagnosticInfo {
677 const Twine &Msg;
678
679 public:
LoweringDiagnosticInfo(const Twine & DiagMsg,DiagnosticSeverity Severity=DS_Error)680 LoweringDiagnosticInfo(const Twine &DiagMsg,
681 DiagnosticSeverity Severity = DS_Error)
682 : DiagnosticInfo(DK_Lowering, Severity), Msg(DiagMsg) {}
print(DiagnosticPrinter & DP) const683 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
684 };
685 }
686
687 /// Calculate an appropriate unique ID for a section, and update Flags,
688 /// EntrySize and NextUniqueID where appropriate.
689 static unsigned
calcUniqueIDUpdateFlagsAndSize(const GlobalObject * GO,StringRef SectionName,SectionKind Kind,const TargetMachine & TM,MCContext & Ctx,Mangler & Mang,unsigned & Flags,unsigned & EntrySize,unsigned & NextUniqueID,const bool Retain,const bool ForceUnique)690 calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
691 SectionKind Kind, const TargetMachine &TM,
692 MCContext &Ctx, Mangler &Mang, unsigned &Flags,
693 unsigned &EntrySize, unsigned &NextUniqueID,
694 const bool Retain, const bool ForceUnique) {
695 // Increment uniqueID if we are forced to emit a unique section.
696 // This works perfectly fine with section attribute or pragma section as the
697 // sections with the same name are grouped together by the assembler.
698 if (ForceUnique)
699 return NextUniqueID++;
700
701 // A section can have at most one associated section. Put each global with
702 // MD_associated in a unique section.
703 const bool Associated = GO->getMetadata(LLVMContext::MD_associated);
704 if (Associated) {
705 Flags |= ELF::SHF_LINK_ORDER;
706 return NextUniqueID++;
707 }
708
709 if (Retain) {
710 if (TM.getTargetTriple().isOSSolaris())
711 Flags |= ELF::SHF_SUNW_NODISCARD;
712 else if (Ctx.getAsmInfo()->useIntegratedAssembler() ||
713 Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36))
714 Flags |= ELF::SHF_GNU_RETAIN;
715 return NextUniqueID++;
716 }
717
718 // If two symbols with differing sizes end up in the same mergeable section
719 // that section can be assigned an incorrect entry size. To avoid this we
720 // usually put symbols of the same size into distinct mergeable sections with
721 // the same name. Doing so relies on the ",unique ," assembly feature. This
722 // feature is not avalible until bintuils version 2.35
723 // (https://sourceware.org/bugzilla/show_bug.cgi?id=25380).
724 const bool SupportsUnique = Ctx.getAsmInfo()->useIntegratedAssembler() ||
725 Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35);
726 if (!SupportsUnique) {
727 Flags &= ~ELF::SHF_MERGE;
728 EntrySize = 0;
729 return MCContext::GenericSectionID;
730 }
731
732 const bool SymbolMergeable = Flags & ELF::SHF_MERGE;
733 const bool SeenSectionNameBefore =
734 Ctx.isELFGenericMergeableSection(SectionName);
735 // If this is the first ocurrence of this section name, treat it as the
736 // generic section
737 if (!SymbolMergeable && !SeenSectionNameBefore) {
738 if (TM.getSeparateNamedSections())
739 return NextUniqueID++;
740 else
741 return MCContext::GenericSectionID;
742 }
743
744 // Symbols must be placed into sections with compatible entry sizes. Generate
745 // unique sections for symbols that have not been assigned to compatible
746 // sections.
747 const auto PreviousID =
748 Ctx.getELFUniqueIDForEntsize(SectionName, Flags, EntrySize);
749 if (PreviousID && (!TM.getSeparateNamedSections() ||
750 *PreviousID == MCContext::GenericSectionID))
751 return *PreviousID;
752
753 // If the user has specified the same section name as would be created
754 // implicitly for this symbol e.g. .rodata.str1.1, then we don't need
755 // to unique the section as the entry size for this symbol will be
756 // compatible with implicitly created sections.
757 SmallString<128> ImplicitSectionNameStem =
758 getELFSectionNameForGlobal(GO, Kind, Mang, TM, EntrySize, false);
759 if (SymbolMergeable &&
760 Ctx.isELFImplicitMergeableSectionNamePrefix(SectionName) &&
761 SectionName.starts_with(ImplicitSectionNameStem))
762 return MCContext::GenericSectionID;
763
764 // We have seen this section name before, but with different flags or entity
765 // size. Create a new unique ID.
766 return NextUniqueID++;
767 }
768
769 static std::tuple<StringRef, bool, unsigned>
getGlobalObjectInfo(const GlobalObject * GO,const TargetMachine & TM)770 getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
771 StringRef Group = "";
772 bool IsComdat = false;
773 unsigned Flags = 0;
774 if (const Comdat *C = getELFComdat(GO)) {
775 Flags |= ELF::SHF_GROUP;
776 Group = C->getName();
777 IsComdat = C->getSelectionKind() == Comdat::Any;
778 }
779 if (TM.isLargeGlobalValue(GO))
780 Flags |= ELF::SHF_X86_64_LARGE;
781 return {Group, IsComdat, Flags};
782 }
783
selectExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM,MCContext & Ctx,Mangler & Mang,unsigned & NextUniqueID,bool Retain,bool ForceUnique)784 static MCSection *selectExplicitSectionGlobal(
785 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
786 MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
787 bool Retain, bool ForceUnique) {
788 StringRef SectionName = GO->getSection();
789
790 // Check if '#pragma clang section' name is applicable.
791 // Note that pragma directive overrides -ffunction-section, -fdata-section
792 // and so section name is exactly as user specified and not uniqued.
793 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
794 if (GV && GV->hasImplicitSection()) {
795 auto Attrs = GV->getAttributes();
796 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
797 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
798 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
799 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
800 } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
801 SectionName = Attrs.getAttribute("relro-section").getValueAsString();
802 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
803 SectionName = Attrs.getAttribute("data-section").getValueAsString();
804 }
805 }
806
807 // Infer section flags from the section name if we can.
808 Kind = getELFKindForNamedSection(SectionName, Kind);
809
810 unsigned Flags = getELFSectionFlags(Kind);
811 auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
812 Flags |= ExtraFlags;
813
814 unsigned EntrySize = getEntrySizeForKind(Kind);
815 const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize(
816 GO, SectionName, Kind, TM, Ctx, Mang, Flags, EntrySize, NextUniqueID,
817 Retain, ForceUnique);
818
819 const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
820 MCSectionELF *Section = Ctx.getELFSection(
821 SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize,
822 Group, IsComdat, UniqueID, LinkedToSym);
823 // Make sure that we did not get some other section with incompatible sh_link.
824 // This should not be possible due to UniqueID code above.
825 assert(Section->getLinkedToSymbol() == LinkedToSym &&
826 "Associated symbol mismatch between sections");
827
828 if (!(Ctx.getAsmInfo()->useIntegratedAssembler() ||
829 Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35))) {
830 // If we are using GNU as before 2.35, then this symbol might have
831 // been placed in an incompatible mergeable section. Emit an error if this
832 // is the case to avoid creating broken output.
833 if ((Section->getFlags() & ELF::SHF_MERGE) &&
834 (Section->getEntrySize() != getEntrySizeForKind(Kind)))
835 GO->getContext().diagnose(LoweringDiagnosticInfo(
836 "Symbol '" + GO->getName() + "' from module '" +
837 (GO->getParent() ? GO->getParent()->getSourceFileName() : "unknown") +
838 "' required a section with entry-size=" +
839 Twine(getEntrySizeForKind(Kind)) + " but was placed in section '" +
840 SectionName + "' with entry-size=" + Twine(Section->getEntrySize()) +
841 ": Explicit assignment by pragma or attribute of an incompatible "
842 "symbol to this section?"));
843 }
844
845 return Section;
846 }
847
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const848 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
849 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
850 return selectExplicitSectionGlobal(GO, Kind, TM, getContext(), getMangler(),
851 NextUniqueID, Used.count(GO),
852 /* ForceUnique = */false);
853 }
854
selectELFSectionForGlobal(MCContext & Ctx,const GlobalObject * GO,SectionKind Kind,Mangler & Mang,const TargetMachine & TM,bool EmitUniqueSection,unsigned Flags,unsigned * NextUniqueID,const MCSymbolELF * AssociatedSymbol)855 static MCSectionELF *selectELFSectionForGlobal(
856 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
857 const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
858 unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
859
860 auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
861 Flags |= ExtraFlags;
862
863 // Get the section entry size based on the kind.
864 unsigned EntrySize = getEntrySizeForKind(Kind);
865
866 bool UniqueSectionName = false;
867 unsigned UniqueID = MCContext::GenericSectionID;
868 if (EmitUniqueSection) {
869 if (TM.getUniqueSectionNames()) {
870 UniqueSectionName = true;
871 } else {
872 UniqueID = *NextUniqueID;
873 (*NextUniqueID)++;
874 }
875 }
876 SmallString<128> Name = getELFSectionNameForGlobal(
877 GO, Kind, Mang, TM, EntrySize, UniqueSectionName);
878
879 // Use 0 as the unique ID for execute-only text.
880 if (Kind.isExecuteOnly())
881 UniqueID = 0;
882 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
883 EntrySize, Group, IsComdat, UniqueID,
884 AssociatedSymbol);
885 }
886
selectELFSectionForGlobal(MCContext & Ctx,const GlobalObject * GO,SectionKind Kind,Mangler & Mang,const TargetMachine & TM,bool Retain,bool EmitUniqueSection,unsigned Flags,unsigned * NextUniqueID)887 static MCSection *selectELFSectionForGlobal(
888 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
889 const TargetMachine &TM, bool Retain, bool EmitUniqueSection,
890 unsigned Flags, unsigned *NextUniqueID) {
891 const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
892 if (LinkedToSym) {
893 EmitUniqueSection = true;
894 Flags |= ELF::SHF_LINK_ORDER;
895 }
896 if (Retain) {
897 if (TM.getTargetTriple().isOSSolaris()) {
898 EmitUniqueSection = true;
899 Flags |= ELF::SHF_SUNW_NODISCARD;
900 } else if (Ctx.getAsmInfo()->useIntegratedAssembler() ||
901 Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) {
902 EmitUniqueSection = true;
903 Flags |= ELF::SHF_GNU_RETAIN;
904 }
905 }
906
907 MCSectionELF *Section = selectELFSectionForGlobal(
908 Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags,
909 NextUniqueID, LinkedToSym);
910 assert(Section->getLinkedToSymbol() == LinkedToSym);
911 return Section;
912 }
913
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const914 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
915 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
916 unsigned Flags = getELFSectionFlags(Kind);
917
918 // If we have -ffunction-section or -fdata-section then we should emit the
919 // global value to a uniqued section specifically for it.
920 bool EmitUniqueSection = false;
921 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
922 if (Kind.isText())
923 EmitUniqueSection = TM.getFunctionSections();
924 else
925 EmitUniqueSection = TM.getDataSections();
926 }
927 EmitUniqueSection |= GO->hasComdat();
928 return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
929 Used.count(GO), EmitUniqueSection, Flags,
930 &NextUniqueID);
931 }
932
getUniqueSectionForFunction(const Function & F,const TargetMachine & TM) const933 MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
934 const Function &F, const TargetMachine &TM) const {
935 SectionKind Kind = SectionKind::getText();
936 unsigned Flags = getELFSectionFlags(Kind);
937 // If the function's section names is pre-determined via pragma or a
938 // section attribute, call selectExplicitSectionGlobal.
939 if (F.hasSection())
940 return selectExplicitSectionGlobal(
941 &F, Kind, TM, getContext(), getMangler(), NextUniqueID,
942 Used.count(&F), /* ForceUnique = */true);
943 else
944 return selectELFSectionForGlobal(
945 getContext(), &F, Kind, getMangler(), TM, Used.count(&F),
946 /*EmitUniqueSection=*/true, Flags, &NextUniqueID);
947 }
948
getSectionForJumpTable(const Function & F,const TargetMachine & TM) const949 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
950 const Function &F, const TargetMachine &TM) const {
951 // If the function can be removed, produce a unique section so that
952 // the table doesn't prevent the removal.
953 const Comdat *C = F.getComdat();
954 bool EmitUniqueSection = TM.getFunctionSections() || C;
955 if (!EmitUniqueSection)
956 return ReadOnlySection;
957
958 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
959 getMangler(), TM, EmitUniqueSection,
960 ELF::SHF_ALLOC, &NextUniqueID,
961 /* AssociatedSymbol */ nullptr);
962 }
963
getSectionForLSDA(const Function & F,const MCSymbol & FnSym,const TargetMachine & TM) const964 MCSection *TargetLoweringObjectFileELF::getSectionForLSDA(
965 const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
966 // If neither COMDAT nor function sections, use the monolithic LSDA section.
967 // Re-use this path if LSDASection is null as in the Arm EHABI.
968 if (!LSDASection || (!F.hasComdat() && !TM.getFunctionSections()))
969 return LSDASection;
970
971 const auto *LSDA = cast<MCSectionELF>(LSDASection);
972 unsigned Flags = LSDA->getFlags();
973 const MCSymbolELF *LinkedToSym = nullptr;
974 StringRef Group;
975 bool IsComdat = false;
976 if (const Comdat *C = getELFComdat(&F)) {
977 Flags |= ELF::SHF_GROUP;
978 Group = C->getName();
979 IsComdat = C->getSelectionKind() == Comdat::Any;
980 }
981 // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36
982 // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER.
983 if (TM.getFunctionSections() &&
984 (getContext().getAsmInfo()->useIntegratedAssembler() &&
985 getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) {
986 Flags |= ELF::SHF_LINK_ORDER;
987 LinkedToSym = cast<MCSymbolELF>(&FnSym);
988 }
989
990 // Append the function name as the suffix like GCC, assuming
991 // -funique-section-names applies to .gcc_except_table sections.
992 return getContext().getELFSection(
993 (TM.getUniqueSectionNames() ? LSDA->getName() + "." + F.getName()
994 : LSDA->getName()),
995 LSDA->getType(), Flags, 0, Group, IsComdat, MCSection::NonUniqueID,
996 LinkedToSym);
997 }
998
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const999 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
1000 bool UsesLabelDifference, const Function &F) const {
1001 // We can always create relative relocations, so use another section
1002 // that can be marked non-executable.
1003 return false;
1004 }
1005
1006 /// Given a mergeable constant with the specified size and relocation
1007 /// information, return a section that it should be placed in.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const1008 MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
1009 const DataLayout &DL, SectionKind Kind, const Constant *C,
1010 Align &Alignment) const {
1011 if (Kind.isMergeableConst4() && MergeableConst4Section)
1012 return MergeableConst4Section;
1013 if (Kind.isMergeableConst8() && MergeableConst8Section)
1014 return MergeableConst8Section;
1015 if (Kind.isMergeableConst16() && MergeableConst16Section)
1016 return MergeableConst16Section;
1017 if (Kind.isMergeableConst32() && MergeableConst32Section)
1018 return MergeableConst32Section;
1019 if (Kind.isReadOnly())
1020 return ReadOnlySection;
1021
1022 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
1023 return DataRelROSection;
1024 }
1025
1026 /// Returns a unique section for the given machine basic block.
getSectionForMachineBasicBlock(const Function & F,const MachineBasicBlock & MBB,const TargetMachine & TM) const1027 MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
1028 const Function &F, const MachineBasicBlock &MBB,
1029 const TargetMachine &TM) const {
1030 assert(MBB.isBeginSection() && "Basic block does not start a section!");
1031 unsigned UniqueID = MCContext::GenericSectionID;
1032
1033 // For cold sections use the .text.split. prefix along with the parent
1034 // function name. All cold blocks for the same function go to the same
1035 // section. Similarly all exception blocks are grouped by symbol name
1036 // under the .text.eh prefix. For regular sections, we either use a unique
1037 // name, or a unique ID for the section.
1038 SmallString<128> Name;
1039 StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
1040 if (FunctionSectionName == ".text" ||
1041 FunctionSectionName.starts_with(".text.")) {
1042 // Function is in a regular .text section.
1043 StringRef FunctionName = MBB.getParent()->getName();
1044 if (MBB.getSectionID() == MBBSectionID::ColdSectionID) {
1045 Name += BBSectionsColdTextPrefix;
1046 Name += FunctionName;
1047 } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
1048 Name += ".text.eh.";
1049 Name += FunctionName;
1050 } else {
1051 Name += FunctionSectionName;
1052 if (TM.getUniqueBasicBlockSectionNames()) {
1053 if (!Name.ends_with("."))
1054 Name += ".";
1055 Name += MBB.getSymbol()->getName();
1056 } else {
1057 UniqueID = NextUniqueID++;
1058 }
1059 }
1060 } else {
1061 // If the original function has a custom non-dot-text section, then emit
1062 // all basic block sections into that section too, each with a unique id.
1063 Name = FunctionSectionName;
1064 UniqueID = NextUniqueID++;
1065 }
1066
1067 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
1068 std::string GroupName;
1069 if (F.hasComdat()) {
1070 Flags |= ELF::SHF_GROUP;
1071 GroupName = F.getComdat()->getName().str();
1072 }
1073 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags,
1074 0 /* Entry Size */, GroupName,
1075 F.hasComdat(), UniqueID, nullptr);
1076 }
1077
getStaticStructorSection(MCContext & Ctx,bool UseInitArray,bool IsCtor,unsigned Priority,const MCSymbol * KeySym)1078 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
1079 bool IsCtor, unsigned Priority,
1080 const MCSymbol *KeySym) {
1081 std::string Name;
1082 unsigned Type;
1083 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
1084 StringRef Comdat = KeySym ? KeySym->getName() : "";
1085
1086 if (KeySym)
1087 Flags |= ELF::SHF_GROUP;
1088
1089 if (UseInitArray) {
1090 if (IsCtor) {
1091 Type = ELF::SHT_INIT_ARRAY;
1092 Name = ".init_array";
1093 } else {
1094 Type = ELF::SHT_FINI_ARRAY;
1095 Name = ".fini_array";
1096 }
1097 if (Priority != 65535) {
1098 Name += '.';
1099 Name += utostr(Priority);
1100 }
1101 } else {
1102 // The default scheme is .ctor / .dtor, so we have to invert the priority
1103 // numbering.
1104 if (IsCtor)
1105 Name = ".ctors";
1106 else
1107 Name = ".dtors";
1108 if (Priority != 65535)
1109 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1110 Type = ELF::SHT_PROGBITS;
1111 }
1112
1113 return Ctx.getELFSection(Name, Type, Flags, 0, Comdat, /*IsComdat=*/true);
1114 }
1115
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym) const1116 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
1117 unsigned Priority, const MCSymbol *KeySym) const {
1118 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
1119 KeySym);
1120 }
1121
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym) const1122 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
1123 unsigned Priority, const MCSymbol *KeySym) const {
1124 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
1125 KeySym);
1126 }
1127
lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM) const1128 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
1129 const GlobalValue *LHS, const GlobalValue *RHS,
1130 const TargetMachine &TM) const {
1131 // We may only use a PLT-relative relocation to refer to unnamed_addr
1132 // functions.
1133 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1134 return nullptr;
1135
1136 // Basic correctness checks.
1137 if (LHS->getType()->getPointerAddressSpace() != 0 ||
1138 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1139 RHS->isThreadLocal())
1140 return nullptr;
1141
1142 return MCBinaryExpr::createSub(
1143 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
1144 getContext()),
1145 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
1146 }
1147
lowerDSOLocalEquivalent(const DSOLocalEquivalent * Equiv,const TargetMachine & TM) const1148 const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
1149 const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const {
1150 assert(supportDSOLocalEquivalentLowering());
1151
1152 const auto *GV = Equiv->getGlobalValue();
1153
1154 // A PLT entry is not needed for dso_local globals.
1155 if (GV->isDSOLocal() || GV->isImplicitDSOLocal())
1156 return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
1157
1158 return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeVariantKind,
1159 getContext());
1160 }
1161
getSectionForCommandLines() const1162 MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {
1163 // Use ".GCC.command.line" since this feature is to support clang's
1164 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
1165 // same name.
1166 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS,
1167 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
1168 }
1169
1170 void
InitializeELF(bool UseInitArray_)1171 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
1172 UseInitArray = UseInitArray_;
1173 MCContext &Ctx = getContext();
1174 if (!UseInitArray) {
1175 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
1176 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1177
1178 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
1179 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1180 return;
1181 }
1182
1183 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
1184 ELF::SHF_WRITE | ELF::SHF_ALLOC);
1185 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
1186 ELF::SHF_WRITE | ELF::SHF_ALLOC);
1187 }
1188
1189 //===----------------------------------------------------------------------===//
1190 // MachO
1191 //===----------------------------------------------------------------------===//
1192
TargetLoweringObjectFileMachO()1193 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() {
1194 SupportIndirectSymViaGOTPCRel = true;
1195 }
1196
Initialize(MCContext & Ctx,const TargetMachine & TM)1197 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
1198 const TargetMachine &TM) {
1199 TargetLoweringObjectFile::Initialize(Ctx, TM);
1200 if (TM.getRelocationModel() == Reloc::Static) {
1201 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
1202 SectionKind::getData());
1203 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
1204 SectionKind::getData());
1205 } else {
1206 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
1207 MachO::S_MOD_INIT_FUNC_POINTERS,
1208 SectionKind::getData());
1209 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
1210 MachO::S_MOD_TERM_FUNC_POINTERS,
1211 SectionKind::getData());
1212 }
1213
1214 PersonalityEncoding =
1215 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
1216 LSDAEncoding = dwarf::DW_EH_PE_pcrel;
1217 TTypeEncoding =
1218 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
1219 }
1220
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym) const1221 MCSection *TargetLoweringObjectFileMachO::getStaticDtorSection(
1222 unsigned Priority, const MCSymbol *KeySym) const {
1223 return StaticDtorSection;
1224 // In userspace, we lower global destructors via atexit(), but kernel/kext
1225 // environments do not provide this function so we still need to support the
1226 // legacy way here.
1227 // See the -disable-atexit-based-global-dtor-lowering CodeGen flag for more
1228 // context.
1229 }
1230
emitModuleMetadata(MCStreamer & Streamer,Module & M) const1231 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
1232 Module &M) const {
1233 // Emit the linker options if present.
1234 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1235 for (const auto *Option : LinkerOptions->operands()) {
1236 SmallVector<std::string, 4> StrOptions;
1237 for (const auto &Piece : cast<MDNode>(Option)->operands())
1238 StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
1239 Streamer.emitLinkerOptions(StrOptions);
1240 }
1241 }
1242
1243 unsigned VersionVal = 0;
1244 unsigned ImageInfoFlags = 0;
1245 StringRef SectionVal;
1246
1247 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
1248 emitCGProfileMetadata(Streamer, M);
1249
1250 // The section is mandatory. If we don't have it, then we don't have GC info.
1251 if (SectionVal.empty())
1252 return;
1253
1254 StringRef Segment, Section;
1255 unsigned TAA = 0, StubSize = 0;
1256 bool TAAParsed;
1257 if (Error E = MCSectionMachO::ParseSectionSpecifier(
1258 SectionVal, Segment, Section, TAA, TAAParsed, StubSize)) {
1259 // If invalid, report the error with report_fatal_error.
1260 report_fatal_error("Invalid section specifier '" + Section +
1261 "': " + toString(std::move(E)) + ".");
1262 }
1263
1264 // Get the section.
1265 MCSectionMachO *S = getContext().getMachOSection(
1266 Segment, Section, TAA, StubSize, SectionKind::getData());
1267 Streamer.switchSection(S);
1268 Streamer.emitLabel(getContext().
1269 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
1270 Streamer.emitInt32(VersionVal);
1271 Streamer.emitInt32(ImageInfoFlags);
1272 Streamer.addBlankLine();
1273 }
1274
checkMachOComdat(const GlobalValue * GV)1275 static void checkMachOComdat(const GlobalValue *GV) {
1276 const Comdat *C = GV->getComdat();
1277 if (!C)
1278 return;
1279
1280 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
1281 "' cannot be lowered.");
1282 }
1283
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1284 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
1285 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1286
1287 StringRef SectionName = GO->getSection();
1288
1289 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1290 if (GV && GV->hasImplicitSection()) {
1291 auto Attrs = GV->getAttributes();
1292 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
1293 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
1294 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
1295 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
1296 } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
1297 SectionName = Attrs.getAttribute("relro-section").getValueAsString();
1298 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
1299 SectionName = Attrs.getAttribute("data-section").getValueAsString();
1300 }
1301 }
1302
1303 // Parse the section specifier and create it if valid.
1304 StringRef Segment, Section;
1305 unsigned TAA = 0, StubSize = 0;
1306 bool TAAParsed;
1307
1308 checkMachOComdat(GO);
1309
1310 if (Error E = MCSectionMachO::ParseSectionSpecifier(
1311 SectionName, Segment, Section, TAA, TAAParsed, StubSize)) {
1312 // If invalid, report the error with report_fatal_error.
1313 report_fatal_error("Global variable '" + GO->getName() +
1314 "' has an invalid section specifier '" +
1315 GO->getSection() + "': " + toString(std::move(E)) + ".");
1316 }
1317
1318 // Get the section.
1319 MCSectionMachO *S =
1320 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
1321
1322 // If TAA wasn't set by ParseSectionSpecifier() above,
1323 // use the value returned by getMachOSection() as a default.
1324 if (!TAAParsed)
1325 TAA = S->getTypeAndAttributes();
1326
1327 // Okay, now that we got the section, verify that the TAA & StubSize agree.
1328 // If the user declared multiple globals with different section flags, we need
1329 // to reject it here.
1330 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
1331 // If invalid, report the error with report_fatal_error.
1332 report_fatal_error("Global variable '" + GO->getName() +
1333 "' section type or attributes does not match previous"
1334 " section specifier");
1335 }
1336
1337 return S;
1338 }
1339
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1340 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
1341 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1342 checkMachOComdat(GO);
1343
1344 // Handle thread local data.
1345 if (Kind.isThreadBSS()) return TLSBSSSection;
1346 if (Kind.isThreadData()) return TLSDataSection;
1347
1348 if (Kind.isText())
1349 return GO->isWeakForLinker() ? TextCoalSection : TextSection;
1350
1351 // If this is weak/linkonce, put this in a coalescable section, either in text
1352 // or data depending on if it is writable.
1353 if (GO->isWeakForLinker()) {
1354 if (Kind.isReadOnly())
1355 return ConstTextCoalSection;
1356 if (Kind.isReadOnlyWithRel())
1357 return ConstDataCoalSection;
1358 return DataCoalSection;
1359 }
1360
1361 // FIXME: Alignment check should be handled by section classifier.
1362 if (Kind.isMergeable1ByteCString() &&
1363 GO->getDataLayout().getPreferredAlign(
1364 cast<GlobalVariable>(GO)) < Align(32))
1365 return CStringSection;
1366
1367 // Do not put 16-bit arrays in the UString section if they have an
1368 // externally visible label, this runs into issues with certain linker
1369 // versions.
1370 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
1371 GO->getDataLayout().getPreferredAlign(
1372 cast<GlobalVariable>(GO)) < Align(32))
1373 return UStringSection;
1374
1375 // With MachO only variables whose corresponding symbol starts with 'l' or
1376 // 'L' can be merged, so we only try merging GVs with private linkage.
1377 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
1378 if (Kind.isMergeableConst4())
1379 return FourByteConstantSection;
1380 if (Kind.isMergeableConst8())
1381 return EightByteConstantSection;
1382 if (Kind.isMergeableConst16())
1383 return SixteenByteConstantSection;
1384 }
1385
1386 // Otherwise, if it is readonly, but not something we can specially optimize,
1387 // just drop it in .const.
1388 if (Kind.isReadOnly())
1389 return ReadOnlySection;
1390
1391 // If this is marked const, put it into a const section. But if the dynamic
1392 // linker needs to write to it, put it in the data segment.
1393 if (Kind.isReadOnlyWithRel())
1394 return ConstDataSection;
1395
1396 // Put zero initialized globals with strong external linkage in the
1397 // DATA, __common section with the .zerofill directive.
1398 if (Kind.isBSSExtern())
1399 return DataCommonSection;
1400
1401 // Put zero initialized globals with local linkage in __DATA,__bss directive
1402 // with the .zerofill directive (aka .lcomm).
1403 if (Kind.isBSSLocal())
1404 return DataBSSSection;
1405
1406 // Otherwise, just drop the variable in the normal data section.
1407 return DataSection;
1408 }
1409
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const1410 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
1411 const DataLayout &DL, SectionKind Kind, const Constant *C,
1412 Align &Alignment) const {
1413 // If this constant requires a relocation, we have to put it in the data
1414 // segment, not in the text segment.
1415 if (Kind.isData() || Kind.isReadOnlyWithRel())
1416 return ConstDataSection;
1417
1418 if (Kind.isMergeableConst4())
1419 return FourByteConstantSection;
1420 if (Kind.isMergeableConst8())
1421 return EightByteConstantSection;
1422 if (Kind.isMergeableConst16())
1423 return SixteenByteConstantSection;
1424 return ReadOnlySection; // .const
1425 }
1426
getSectionForCommandLines() const1427 MCSection *TargetLoweringObjectFileMachO::getSectionForCommandLines() const {
1428 return getContext().getMachOSection("__TEXT", "__command_line", 0,
1429 SectionKind::getReadOnly());
1430 }
1431
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const1432 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1433 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
1434 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1435 // The mach-o version of this method defaults to returning a stub reference.
1436
1437 if (Encoding & DW_EH_PE_indirect) {
1438 MachineModuleInfoMachO &MachOMMI =
1439 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1440
1441 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1442
1443 // Add information about the stub reference to MachOMMI so that the stub
1444 // gets emitted by the asmprinter.
1445 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1446 if (!StubSym.getPointer()) {
1447 MCSymbol *Sym = TM.getSymbol(GV);
1448 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1449 }
1450
1451 return TargetLoweringObjectFile::
1452 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
1453 Encoding & ~DW_EH_PE_indirect, Streamer);
1454 }
1455
1456 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
1457 MMI, Streamer);
1458 }
1459
getCFIPersonalitySymbol(const GlobalValue * GV,const TargetMachine & TM,MachineModuleInfo * MMI) const1460 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1461 const GlobalValue *GV, const TargetMachine &TM,
1462 MachineModuleInfo *MMI) const {
1463 // The mach-o version of this method defaults to returning a stub reference.
1464 MachineModuleInfoMachO &MachOMMI =
1465 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1466
1467 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1468
1469 // Add information about the stub reference to MachOMMI so that the stub
1470 // gets emitted by the asmprinter.
1471 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1472 if (!StubSym.getPointer()) {
1473 MCSymbol *Sym = TM.getSymbol(GV);
1474 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1475 }
1476
1477 return SSym;
1478 }
1479
getIndirectSymViaGOTPCRel(const GlobalValue * GV,const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer) const1480 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1481 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
1482 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1483 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1484 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1485 // through a non_lazy_ptr stub instead. One advantage is that it allows the
1486 // computation of deltas to final external symbols. Example:
1487 //
1488 // _extgotequiv:
1489 // .long _extfoo
1490 //
1491 // _delta:
1492 // .long _extgotequiv-_delta
1493 //
1494 // is transformed to:
1495 //
1496 // _delta:
1497 // .long L_extfoo$non_lazy_ptr-(_delta+0)
1498 //
1499 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
1500 // L_extfoo$non_lazy_ptr:
1501 // .indirect_symbol _extfoo
1502 // .long 0
1503 //
1504 // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1505 // may point to both local (same translation unit) and global (other
1506 // translation units) symbols. Example:
1507 //
1508 // .section __DATA,__pointers,non_lazy_symbol_pointers
1509 // L1:
1510 // .indirect_symbol _myGlobal
1511 // .long 0
1512 // L2:
1513 // .indirect_symbol _myLocal
1514 // .long _myLocal
1515 //
1516 // If the symbol is local, instead of the symbol's index, the assembler
1517 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1518 // Then the linker will notice the constant in the table and will look at the
1519 // content of the symbol.
1520 MachineModuleInfoMachO &MachOMMI =
1521 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1522 MCContext &Ctx = getContext();
1523
1524 // The offset must consider the original displacement from the base symbol
1525 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1526 Offset = -MV.getConstant();
1527 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
1528
1529 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1530 // non_lazy_ptr stubs.
1531 SmallString<128> Name;
1532 StringRef Suffix = "$non_lazy_ptr";
1533 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
1534 Name += Sym->getName();
1535 Name += Suffix;
1536 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
1537
1538 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
1539
1540 if (!StubSym.getPointer())
1541 StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
1542 !GV->hasLocalLinkage());
1543
1544 const MCExpr *BSymExpr =
1545 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
1546 const MCExpr *LHS =
1547 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
1548
1549 if (!Offset)
1550 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
1551
1552 const MCExpr *RHS =
1553 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
1554 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
1555 }
1556
canUsePrivateLabel(const MCAsmInfo & AsmInfo,const MCSection & Section)1557 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
1558 const MCSection &Section) {
1559 if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Section))
1560 return true;
1561
1562 // FIXME: we should be able to use private labels for sections that can't be
1563 // dead-stripped (there's no issue with blocking atomization there), but `ld
1564 // -r` sometimes drops the no_dead_strip attribute from sections so for safety
1565 // we don't allow it.
1566 return false;
1567 }
1568
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,const TargetMachine & TM) const1569 void TargetLoweringObjectFileMachO::getNameWithPrefix(
1570 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1571 const TargetMachine &TM) const {
1572 bool CannotUsePrivateLabel = true;
1573 if (auto *GO = GV->getAliaseeObject()) {
1574 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
1575 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
1576 CannotUsePrivateLabel =
1577 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
1578 }
1579 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1580 }
1581
1582 //===----------------------------------------------------------------------===//
1583 // COFF
1584 //===----------------------------------------------------------------------===//
1585
1586 static unsigned
getCOFFSectionFlags(SectionKind K,const TargetMachine & TM)1587 getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
1588 unsigned Flags = 0;
1589 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
1590
1591 if (K.isMetadata())
1592 Flags |=
1593 COFF::IMAGE_SCN_MEM_DISCARDABLE;
1594 else if (K.isExclude())
1595 Flags |=
1596 COFF::IMAGE_SCN_LNK_REMOVE | COFF::IMAGE_SCN_MEM_DISCARDABLE;
1597 else if (K.isText())
1598 Flags |=
1599 COFF::IMAGE_SCN_MEM_EXECUTE |
1600 COFF::IMAGE_SCN_MEM_READ |
1601 COFF::IMAGE_SCN_CNT_CODE |
1602 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
1603 else if (K.isBSS())
1604 Flags |=
1605 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1606 COFF::IMAGE_SCN_MEM_READ |
1607 COFF::IMAGE_SCN_MEM_WRITE;
1608 else if (K.isThreadLocal())
1609 Flags |=
1610 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1611 COFF::IMAGE_SCN_MEM_READ |
1612 COFF::IMAGE_SCN_MEM_WRITE;
1613 else if (K.isReadOnly() || K.isReadOnlyWithRel())
1614 Flags |=
1615 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1616 COFF::IMAGE_SCN_MEM_READ;
1617 else if (K.isWriteable())
1618 Flags |=
1619 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1620 COFF::IMAGE_SCN_MEM_READ |
1621 COFF::IMAGE_SCN_MEM_WRITE;
1622
1623 return Flags;
1624 }
1625
getComdatGVForCOFF(const GlobalValue * GV)1626 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
1627 const Comdat *C = GV->getComdat();
1628 assert(C && "expected GV to have a Comdat!");
1629
1630 StringRef ComdatGVName = C->getName();
1631 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
1632 if (!ComdatGV)
1633 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1634 "' does not exist.");
1635
1636 if (ComdatGV->getComdat() != C)
1637 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1638 "' is not a key for its COMDAT.");
1639
1640 return ComdatGV;
1641 }
1642
getSelectionForCOFF(const GlobalValue * GV)1643 static int getSelectionForCOFF(const GlobalValue *GV) {
1644 if (const Comdat *C = GV->getComdat()) {
1645 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1646 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1647 ComdatKey = GA->getAliaseeObject();
1648 if (ComdatKey == GV) {
1649 switch (C->getSelectionKind()) {
1650 case Comdat::Any:
1651 return COFF::IMAGE_COMDAT_SELECT_ANY;
1652 case Comdat::ExactMatch:
1653 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
1654 case Comdat::Largest:
1655 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
1656 case Comdat::NoDeduplicate:
1657 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1658 case Comdat::SameSize:
1659 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
1660 }
1661 } else {
1662 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1663 }
1664 }
1665 return 0;
1666 }
1667
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1668 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1669 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1670 StringRef Name = GO->getSection();
1671 if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
1672 /*AddSegmentInfo=*/false) ||
1673 Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
1674 /*AddSegmentInfo=*/false) ||
1675 Name == getInstrProfSectionName(IPSK_covdata, Triple::COFF,
1676 /*AddSegmentInfo=*/false) ||
1677 Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
1678 /*AddSegmentInfo=*/false))
1679 Kind = SectionKind::getMetadata();
1680 int Selection = 0;
1681 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1682 StringRef COMDATSymName = "";
1683 if (GO->hasComdat()) {
1684 Selection = getSelectionForCOFF(GO);
1685 const GlobalValue *ComdatGV;
1686 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1687 ComdatGV = getComdatGVForCOFF(GO);
1688 else
1689 ComdatGV = GO;
1690
1691 if (!ComdatGV->hasPrivateLinkage()) {
1692 MCSymbol *Sym = TM.getSymbol(ComdatGV);
1693 COMDATSymName = Sym->getName();
1694 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1695 } else {
1696 Selection = 0;
1697 }
1698 }
1699
1700 return getContext().getCOFFSection(Name, Characteristics, COMDATSymName,
1701 Selection);
1702 }
1703
getCOFFSectionNameForUniqueGlobal(SectionKind Kind)1704 static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
1705 if (Kind.isText())
1706 return ".text";
1707 if (Kind.isBSS())
1708 return ".bss";
1709 if (Kind.isThreadLocal())
1710 return ".tls$";
1711 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1712 return ".rdata";
1713 return ".data";
1714 }
1715
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1716 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1717 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1718 // If we have -ffunction-sections then we should emit the global value to a
1719 // uniqued section specifically for it.
1720 bool EmitUniquedSection;
1721 if (Kind.isText())
1722 EmitUniquedSection = TM.getFunctionSections();
1723 else
1724 EmitUniquedSection = TM.getDataSections();
1725
1726 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
1727 SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
1728
1729 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1730
1731 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1732 int Selection = getSelectionForCOFF(GO);
1733 if (!Selection)
1734 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1735 const GlobalValue *ComdatGV;
1736 if (GO->hasComdat())
1737 ComdatGV = getComdatGVForCOFF(GO);
1738 else
1739 ComdatGV = GO;
1740
1741 unsigned UniqueID = MCContext::GenericSectionID;
1742 if (EmitUniquedSection)
1743 UniqueID = NextUniqueID++;
1744
1745 if (!ComdatGV->hasPrivateLinkage()) {
1746 MCSymbol *Sym = TM.getSymbol(ComdatGV);
1747 StringRef COMDATSymName = Sym->getName();
1748
1749 if (const auto *F = dyn_cast<Function>(GO))
1750 if (std::optional<StringRef> Prefix = F->getSectionPrefix())
1751 raw_svector_ostream(Name) << '$' << *Prefix;
1752
1753 // Append "$symbol" to the section name *before* IR-level mangling is
1754 // applied when targetting mingw. This is what GCC does, and the ld.bfd
1755 // COFF linker will not properly handle comdats otherwise.
1756 if (getContext().getTargetTriple().isWindowsGNUEnvironment())
1757 raw_svector_ostream(Name) << '$' << ComdatGV->getName();
1758
1759 return getContext().getCOFFSection(Name, Characteristics, COMDATSymName,
1760 Selection, UniqueID);
1761 } else {
1762 SmallString<256> TmpData;
1763 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
1764 return getContext().getCOFFSection(Name, Characteristics, TmpData,
1765 Selection, UniqueID);
1766 }
1767 }
1768
1769 if (Kind.isText())
1770 return TextSection;
1771
1772 if (Kind.isThreadLocal())
1773 return TLSDataSection;
1774
1775 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1776 return ReadOnlySection;
1777
1778 // Note: we claim that common symbols are put in BSSSection, but they are
1779 // really emitted with the magic .comm directive, which creates a symbol table
1780 // entry but not a section.
1781 if (Kind.isBSS() || Kind.isCommon())
1782 return BSSSection;
1783
1784 return DataSection;
1785 }
1786
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,const TargetMachine & TM) const1787 void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1788 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1789 const TargetMachine &TM) const {
1790 bool CannotUsePrivateLabel = false;
1791 if (GV->hasPrivateLinkage() &&
1792 ((isa<Function>(GV) && TM.getFunctionSections()) ||
1793 (isa<GlobalVariable>(GV) && TM.getDataSections())))
1794 CannotUsePrivateLabel = true;
1795
1796 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1797 }
1798
getSectionForJumpTable(const Function & F,const TargetMachine & TM) const1799 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1800 const Function &F, const TargetMachine &TM) const {
1801 // If the function can be removed, produce a unique section so that
1802 // the table doesn't prevent the removal.
1803 const Comdat *C = F.getComdat();
1804 bool EmitUniqueSection = TM.getFunctionSections() || C;
1805 if (!EmitUniqueSection)
1806 return ReadOnlySection;
1807
1808 // FIXME: we should produce a symbol for F instead.
1809 if (F.hasPrivateLinkage())
1810 return ReadOnlySection;
1811
1812 MCSymbol *Sym = TM.getSymbol(&F);
1813 StringRef COMDATSymName = Sym->getName();
1814
1815 SectionKind Kind = SectionKind::getReadOnly();
1816 StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
1817 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1818 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1819 unsigned UniqueID = NextUniqueID++;
1820
1821 return getContext().getCOFFSection(SecName, Characteristics, COMDATSymName,
1822 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE,
1823 UniqueID);
1824 }
1825
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const1826 bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection(
1827 bool UsesLabelDifference, const Function &F) const {
1828 if (TM->getTargetTriple().getArch() == Triple::x86_64) {
1829 if (!JumpTableInFunctionSection) {
1830 // We can always create relative relocations, so use another section
1831 // that can be marked non-executable.
1832 return false;
1833 }
1834 }
1835 return TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
1836 UsesLabelDifference, F);
1837 }
1838
emitModuleMetadata(MCStreamer & Streamer,Module & M) const1839 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
1840 Module &M) const {
1841 emitLinkerDirectives(Streamer, M);
1842
1843 unsigned Version = 0;
1844 unsigned Flags = 0;
1845 StringRef Section;
1846
1847 GetObjCImageInfo(M, Version, Flags, Section);
1848 if (!Section.empty()) {
1849 auto &C = getContext();
1850 auto *S = C.getCOFFSection(Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1851 COFF::IMAGE_SCN_MEM_READ);
1852 Streamer.switchSection(S);
1853 Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1854 Streamer.emitInt32(Version);
1855 Streamer.emitInt32(Flags);
1856 Streamer.addBlankLine();
1857 }
1858
1859 emitCGProfileMetadata(Streamer, M);
1860 }
1861
emitLinkerDirectives(MCStreamer & Streamer,Module & M) const1862 void TargetLoweringObjectFileCOFF::emitLinkerDirectives(
1863 MCStreamer &Streamer, Module &M) const {
1864 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1865 // Emit the linker options to the linker .drectve section. According to the
1866 // spec, this section is a space-separated string containing flags for
1867 // linker.
1868 MCSection *Sec = getDrectveSection();
1869 Streamer.switchSection(Sec);
1870 for (const auto *Option : LinkerOptions->operands()) {
1871 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1872 // Lead with a space for consistency with our dllexport implementation.
1873 std::string Directive(" ");
1874 Directive.append(std::string(cast<MDString>(Piece)->getString()));
1875 Streamer.emitBytes(Directive);
1876 }
1877 }
1878 }
1879
1880 // Emit /EXPORT: flags for each exported global as necessary.
1881 std::string Flags;
1882 for (const GlobalValue &GV : M.global_values()) {
1883 raw_string_ostream OS(Flags);
1884 emitLinkerFlagsForGlobalCOFF(OS, &GV, getContext().getTargetTriple(),
1885 getMangler());
1886 OS.flush();
1887 if (!Flags.empty()) {
1888 Streamer.switchSection(getDrectveSection());
1889 Streamer.emitBytes(Flags);
1890 }
1891 Flags.clear();
1892 }
1893
1894 // Emit /INCLUDE: flags for each used global as necessary.
1895 if (const auto *LU = M.getNamedGlobal("llvm.used")) {
1896 assert(LU->hasInitializer() && "expected llvm.used to have an initializer");
1897 assert(isa<ArrayType>(LU->getValueType()) &&
1898 "expected llvm.used to be an array type");
1899 if (const auto *A = cast<ConstantArray>(LU->getInitializer())) {
1900 for (const Value *Op : A->operands()) {
1901 const auto *GV = cast<GlobalValue>(Op->stripPointerCasts());
1902 // Global symbols with internal or private linkage are not visible to
1903 // the linker, and thus would cause an error when the linker tried to
1904 // preserve the symbol due to the `/include:` directive.
1905 if (GV->hasLocalLinkage())
1906 continue;
1907
1908 raw_string_ostream OS(Flags);
1909 emitLinkerFlagsForUsedCOFF(OS, GV, getContext().getTargetTriple(),
1910 getMangler());
1911 OS.flush();
1912
1913 if (!Flags.empty()) {
1914 Streamer.switchSection(getDrectveSection());
1915 Streamer.emitBytes(Flags);
1916 }
1917 Flags.clear();
1918 }
1919 }
1920 }
1921 }
1922
Initialize(MCContext & Ctx,const TargetMachine & TM)1923 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1924 const TargetMachine &TM) {
1925 TargetLoweringObjectFile::Initialize(Ctx, TM);
1926 this->TM = &TM;
1927 const Triple &T = TM.getTargetTriple();
1928 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1929 StaticCtorSection =
1930 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1931 COFF::IMAGE_SCN_MEM_READ);
1932 StaticDtorSection =
1933 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1934 COFF::IMAGE_SCN_MEM_READ);
1935 } else {
1936 StaticCtorSection = Ctx.getCOFFSection(
1937 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1938 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE);
1939 StaticDtorSection = Ctx.getCOFFSection(
1940 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1941 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE);
1942 }
1943 }
1944
getCOFFStaticStructorSection(MCContext & Ctx,const Triple & T,bool IsCtor,unsigned Priority,const MCSymbol * KeySym,MCSectionCOFF * Default)1945 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
1946 const Triple &T, bool IsCtor,
1947 unsigned Priority,
1948 const MCSymbol *KeySym,
1949 MCSectionCOFF *Default) {
1950 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1951 // If the priority is the default, use .CRT$XCU, possibly associative.
1952 if (Priority == 65535)
1953 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1954
1955 // Otherwise, we need to compute a new section name. Low priorities should
1956 // run earlier. The linker will sort sections ASCII-betically, and we need a
1957 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1958 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1959 // low priorities need to sort before 'L', since the CRT uses that
1960 // internally, so we use ".CRT$XCA00001" for them. We have a contract with
1961 // the frontend that "init_seg(compiler)" corresponds to priority 200 and
1962 // "init_seg(lib)" corresponds to priority 400, and those respectively use
1963 // 'C' and 'L' without the priority suffix. Priorities between 200 and 400
1964 // use 'C' with the priority as a suffix.
1965 SmallString<24> Name;
1966 char LastLetter = 'T';
1967 bool AddPrioritySuffix = Priority != 200 && Priority != 400;
1968 if (Priority < 200)
1969 LastLetter = 'A';
1970 else if (Priority < 400)
1971 LastLetter = 'C';
1972 else if (Priority == 400)
1973 LastLetter = 'L';
1974 raw_svector_ostream OS(Name);
1975 OS << ".CRT$X" << (IsCtor ? "C" : "T") << LastLetter;
1976 if (AddPrioritySuffix)
1977 OS << format("%05u", Priority);
1978 MCSectionCOFF *Sec = Ctx.getCOFFSection(
1979 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ);
1980 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
1981 }
1982
1983 std::string Name = IsCtor ? ".ctors" : ".dtors";
1984 if (Priority != 65535)
1985 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1986
1987 return Ctx.getAssociativeCOFFSection(
1988 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1989 COFF::IMAGE_SCN_MEM_READ |
1990 COFF::IMAGE_SCN_MEM_WRITE),
1991 KeySym, 0);
1992 }
1993
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym) const1994 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
1995 unsigned Priority, const MCSymbol *KeySym) const {
1996 return getCOFFStaticStructorSection(
1997 getContext(), getContext().getTargetTriple(), true, Priority, KeySym,
1998 cast<MCSectionCOFF>(StaticCtorSection));
1999 }
2000
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym) const2001 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
2002 unsigned Priority, const MCSymbol *KeySym) const {
2003 return getCOFFStaticStructorSection(
2004 getContext(), getContext().getTargetTriple(), false, Priority, KeySym,
2005 cast<MCSectionCOFF>(StaticDtorSection));
2006 }
2007
lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM) const2008 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
2009 const GlobalValue *LHS, const GlobalValue *RHS,
2010 const TargetMachine &TM) const {
2011 const Triple &T = TM.getTargetTriple();
2012 if (T.isOSCygMing())
2013 return nullptr;
2014
2015 // Our symbols should exist in address space zero, cowardly no-op if
2016 // otherwise.
2017 if (LHS->getType()->getPointerAddressSpace() != 0 ||
2018 RHS->getType()->getPointerAddressSpace() != 0)
2019 return nullptr;
2020
2021 // Both ptrtoint instructions must wrap global objects:
2022 // - Only global variables are eligible for image relative relocations.
2023 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
2024 // We expect __ImageBase to be a global variable without a section, externally
2025 // defined.
2026 //
2027 // It should look something like this: @__ImageBase = external constant i8
2028 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
2029 LHS->isThreadLocal() || RHS->isThreadLocal() ||
2030 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
2031 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
2032 return nullptr;
2033
2034 return MCSymbolRefExpr::create(TM.getSymbol(LHS),
2035 MCSymbolRefExpr::VK_COFF_IMGREL32,
2036 getContext());
2037 }
2038
APIntToHexString(const APInt & AI)2039 static std::string APIntToHexString(const APInt &AI) {
2040 unsigned Width = (AI.getBitWidth() / 8) * 2;
2041 std::string HexString = toString(AI, 16, /*Signed=*/false);
2042 llvm::transform(HexString, HexString.begin(), tolower);
2043 unsigned Size = HexString.size();
2044 assert(Width >= Size && "hex string is too large!");
2045 HexString.insert(HexString.begin(), Width - Size, '0');
2046
2047 return HexString;
2048 }
2049
scalarConstantToHexString(const Constant * C)2050 static std::string scalarConstantToHexString(const Constant *C) {
2051 Type *Ty = C->getType();
2052 if (isa<UndefValue>(C)) {
2053 return APIntToHexString(APInt::getZero(Ty->getPrimitiveSizeInBits()));
2054 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
2055 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
2056 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
2057 return APIntToHexString(CI->getValue());
2058 } else {
2059 unsigned NumElements;
2060 if (auto *VTy = dyn_cast<VectorType>(Ty))
2061 NumElements = cast<FixedVectorType>(VTy)->getNumElements();
2062 else
2063 NumElements = Ty->getArrayNumElements();
2064 std::string HexString;
2065 for (int I = NumElements - 1, E = -1; I != E; --I)
2066 HexString += scalarConstantToHexString(C->getAggregateElement(I));
2067 return HexString;
2068 }
2069 }
2070
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const2071 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
2072 const DataLayout &DL, SectionKind Kind, const Constant *C,
2073 Align &Alignment) const {
2074 if (Kind.isMergeableConst() && C &&
2075 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
2076 // This creates comdat sections with the given symbol name, but unless
2077 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
2078 // will be created with a null storage class, which makes GNU binutils
2079 // error out.
2080 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
2081 COFF::IMAGE_SCN_MEM_READ |
2082 COFF::IMAGE_SCN_LNK_COMDAT;
2083 std::string COMDATSymName;
2084 if (Kind.isMergeableConst4()) {
2085 if (Alignment <= 4) {
2086 COMDATSymName = "__real@" + scalarConstantToHexString(C);
2087 Alignment = Align(4);
2088 }
2089 } else if (Kind.isMergeableConst8()) {
2090 if (Alignment <= 8) {
2091 COMDATSymName = "__real@" + scalarConstantToHexString(C);
2092 Alignment = Align(8);
2093 }
2094 } else if (Kind.isMergeableConst16()) {
2095 // FIXME: These may not be appropriate for non-x86 architectures.
2096 if (Alignment <= 16) {
2097 COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
2098 Alignment = Align(16);
2099 }
2100 } else if (Kind.isMergeableConst32()) {
2101 if (Alignment <= 32) {
2102 COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
2103 Alignment = Align(32);
2104 }
2105 }
2106
2107 if (!COMDATSymName.empty())
2108 return getContext().getCOFFSection(".rdata", Characteristics,
2109 COMDATSymName,
2110 COFF::IMAGE_COMDAT_SELECT_ANY);
2111 }
2112
2113 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C,
2114 Alignment);
2115 }
2116
2117 //===----------------------------------------------------------------------===//
2118 // Wasm
2119 //===----------------------------------------------------------------------===//
2120
getWasmComdat(const GlobalValue * GV)2121 static const Comdat *getWasmComdat(const GlobalValue *GV) {
2122 const Comdat *C = GV->getComdat();
2123 if (!C)
2124 return nullptr;
2125
2126 if (C->getSelectionKind() != Comdat::Any)
2127 report_fatal_error("WebAssembly COMDATs only support "
2128 "SelectionKind::Any, '" + C->getName() + "' cannot be "
2129 "lowered.");
2130
2131 return C;
2132 }
2133
getWasmSectionFlags(SectionKind K,bool Retain)2134 static unsigned getWasmSectionFlags(SectionKind K, bool Retain) {
2135 unsigned Flags = 0;
2136
2137 if (K.isThreadLocal())
2138 Flags |= wasm::WASM_SEG_FLAG_TLS;
2139
2140 if (K.isMergeableCString())
2141 Flags |= wasm::WASM_SEG_FLAG_STRINGS;
2142
2143 if (Retain)
2144 Flags |= wasm::WASM_SEG_FLAG_RETAIN;
2145
2146 // TODO(sbc): Add suport for K.isMergeableConst()
2147
2148 return Flags;
2149 }
2150
getModuleMetadata(Module & M)2151 void TargetLoweringObjectFileWasm::getModuleMetadata(Module &M) {
2152 SmallVector<GlobalValue *, 4> Vec;
2153 collectUsedGlobalVariables(M, Vec, false);
2154 for (GlobalValue *GV : Vec)
2155 if (auto *GO = dyn_cast<GlobalObject>(GV))
2156 Used.insert(GO);
2157 }
2158
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2159 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
2160 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2161 // We don't support explict section names for functions in the wasm object
2162 // format. Each function has to be in its own unique section.
2163 if (isa<Function>(GO)) {
2164 return SelectSectionForGlobal(GO, Kind, TM);
2165 }
2166
2167 StringRef Name = GO->getSection();
2168
2169 // Certain data sections we treat as named custom sections rather than
2170 // segments within the data section.
2171 // This could be avoided if all data segements (the wasm sense) were
2172 // represented as their own sections (in the llvm sense).
2173 // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
2174 if (Name == ".llvmcmd" || Name == ".llvmbc")
2175 Kind = SectionKind::getMetadata();
2176
2177 StringRef Group = "";
2178 if (const Comdat *C = getWasmComdat(GO)) {
2179 Group = C->getName();
2180 }
2181
2182 unsigned Flags = getWasmSectionFlags(Kind, Used.count(GO));
2183 MCSectionWasm *Section = getContext().getWasmSection(
2184 Name, Kind, Flags, Group, MCContext::GenericSectionID);
2185
2186 return Section;
2187 }
2188
2189 static MCSectionWasm *
selectWasmSectionForGlobal(MCContext & Ctx,const GlobalObject * GO,SectionKind Kind,Mangler & Mang,const TargetMachine & TM,bool EmitUniqueSection,unsigned * NextUniqueID,bool Retain)2190 selectWasmSectionForGlobal(MCContext &Ctx, const GlobalObject *GO,
2191 SectionKind Kind, Mangler &Mang,
2192 const TargetMachine &TM, bool EmitUniqueSection,
2193 unsigned *NextUniqueID, bool Retain) {
2194 StringRef Group = "";
2195 if (const Comdat *C = getWasmComdat(GO)) {
2196 Group = C->getName();
2197 }
2198
2199 bool UniqueSectionNames = TM.getUniqueSectionNames();
2200 SmallString<128> Name = getSectionPrefixForGlobal(Kind, /*IsLarge=*/false);
2201
2202 if (const auto *F = dyn_cast<Function>(GO)) {
2203 const auto &OptionalPrefix = F->getSectionPrefix();
2204 if (OptionalPrefix)
2205 raw_svector_ostream(Name) << '.' << *OptionalPrefix;
2206 }
2207
2208 if (EmitUniqueSection && UniqueSectionNames) {
2209 Name.push_back('.');
2210 TM.getNameWithPrefix(Name, GO, Mang, true);
2211 }
2212 unsigned UniqueID = MCContext::GenericSectionID;
2213 if (EmitUniqueSection && !UniqueSectionNames) {
2214 UniqueID = *NextUniqueID;
2215 (*NextUniqueID)++;
2216 }
2217
2218 unsigned Flags = getWasmSectionFlags(Kind, Retain);
2219 return Ctx.getWasmSection(Name, Kind, Flags, Group, UniqueID);
2220 }
2221
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2222 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
2223 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2224
2225 if (Kind.isCommon())
2226 report_fatal_error("mergable sections not supported yet on wasm");
2227
2228 // If we have -ffunction-section or -fdata-section then we should emit the
2229 // global value to a uniqued section specifically for it.
2230 bool EmitUniqueSection = false;
2231 if (Kind.isText())
2232 EmitUniqueSection = TM.getFunctionSections();
2233 else
2234 EmitUniqueSection = TM.getDataSections();
2235 EmitUniqueSection |= GO->hasComdat();
2236 bool Retain = Used.count(GO);
2237 EmitUniqueSection |= Retain;
2238
2239 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
2240 EmitUniqueSection, &NextUniqueID, Retain);
2241 }
2242
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const2243 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
2244 bool UsesLabelDifference, const Function &F) const {
2245 // We can always create relative relocations, so use another section
2246 // that can be marked non-executable.
2247 return false;
2248 }
2249
lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM) const2250 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
2251 const GlobalValue *LHS, const GlobalValue *RHS,
2252 const TargetMachine &TM) const {
2253 // We may only use a PLT-relative relocation to refer to unnamed_addr
2254 // functions.
2255 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
2256 return nullptr;
2257
2258 // Basic correctness checks.
2259 if (LHS->getType()->getPointerAddressSpace() != 0 ||
2260 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
2261 RHS->isThreadLocal())
2262 return nullptr;
2263
2264 return MCBinaryExpr::createSub(
2265 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
2266 getContext()),
2267 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
2268 }
2269
InitializeWasm()2270 void TargetLoweringObjectFileWasm::InitializeWasm() {
2271 StaticCtorSection =
2272 getContext().getWasmSection(".init_array", SectionKind::getData());
2273
2274 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
2275 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
2276 TTypeEncoding = dwarf::DW_EH_PE_absptr;
2277 }
2278
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym) const2279 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
2280 unsigned Priority, const MCSymbol *KeySym) const {
2281 return Priority == UINT16_MAX ?
2282 StaticCtorSection :
2283 getContext().getWasmSection(".init_array." + utostr(Priority),
2284 SectionKind::getData());
2285 }
2286
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym) const2287 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
2288 unsigned Priority, const MCSymbol *KeySym) const {
2289 report_fatal_error("@llvm.global_dtors should have been lowered already");
2290 }
2291
2292 //===----------------------------------------------------------------------===//
2293 // XCOFF
2294 //===----------------------------------------------------------------------===//
ShouldEmitEHBlock(const MachineFunction * MF)2295 bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(
2296 const MachineFunction *MF) {
2297 if (!MF->getLandingPads().empty())
2298 return true;
2299
2300 const Function &F = MF->getFunction();
2301 if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry())
2302 return false;
2303
2304 const GlobalValue *Per =
2305 dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
2306 assert(Per && "Personality routine is not a GlobalValue type.");
2307 if (isNoOpWithoutInvoke(classifyEHPersonality(Per)))
2308 return false;
2309
2310 return true;
2311 }
2312
ShouldSetSSPCanaryBitInTB(const MachineFunction * MF)2313 bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(
2314 const MachineFunction *MF) {
2315 const Function &F = MF->getFunction();
2316 if (!F.hasStackProtectorFnAttr())
2317 return false;
2318 // FIXME: check presence of canary word
2319 // There are cases that the stack protectors are not really inserted even if
2320 // the attributes are on.
2321 return true;
2322 }
2323
2324 MCSymbol *
getEHInfoTableSymbol(const MachineFunction * MF)2325 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) {
2326 MCSymbol *EHInfoSym = MF->getContext().getOrCreateSymbol(
2327 "__ehinfo." + Twine(MF->getFunctionNumber()));
2328 cast<MCSymbolXCOFF>(EHInfoSym)->setEHInfo();
2329 return EHInfoSym;
2330 }
2331
2332 MCSymbol *
getTargetSymbol(const GlobalValue * GV,const TargetMachine & TM) const2333 TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV,
2334 const TargetMachine &TM) const {
2335 // We always use a qualname symbol for a GV that represents
2336 // a declaration, a function descriptor, or a common symbol.
2337 // If a GV represents a GlobalVariable and -fdata-sections is enabled, we
2338 // also return a qualname so that a label symbol could be avoided.
2339 // It is inherently ambiguous when the GO represents the address of a
2340 // function, as the GO could either represent a function descriptor or a
2341 // function entry point. We choose to always return a function descriptor
2342 // here.
2343 if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) {
2344 if (GO->isDeclarationForLinker())
2345 return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM))
2346 ->getQualNameSymbol();
2347
2348 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
2349 if (GVar->hasAttribute("toc-data"))
2350 return cast<MCSectionXCOFF>(
2351 SectionForGlobal(GVar, SectionKind::getData(), TM))
2352 ->getQualNameSymbol();
2353
2354 SectionKind GOKind = getKindForGlobal(GO, TM);
2355 if (GOKind.isText())
2356 return cast<MCSectionXCOFF>(
2357 getSectionForFunctionDescriptor(cast<Function>(GO), TM))
2358 ->getQualNameSymbol();
2359 if ((TM.getDataSections() && !GO->hasSection()) || GO->hasCommonLinkage() ||
2360 GOKind.isBSSLocal() || GOKind.isThreadBSSLocal())
2361 return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM))
2362 ->getQualNameSymbol();
2363 }
2364
2365 // For all other cases, fall back to getSymbol to return the unqualified name.
2366 return nullptr;
2367 }
2368
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2369 MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
2370 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2371 if (!GO->hasSection())
2372 report_fatal_error("#pragma clang section is not yet supported");
2373
2374 StringRef SectionName = GO->getSection();
2375
2376 // Handle the XCOFF::TD case first, then deal with the rest.
2377 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
2378 if (GVar->hasAttribute("toc-data"))
2379 return getContext().getXCOFFSection(
2380 SectionName, Kind,
2381 XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD),
2382 /* MultiSymbolsAllowed*/ true);
2383
2384 XCOFF::StorageMappingClass MappingClass;
2385 if (Kind.isText())
2386 MappingClass = XCOFF::XMC_PR;
2387 else if (Kind.isData() || Kind.isBSS())
2388 MappingClass = XCOFF::XMC_RW;
2389 else if (Kind.isReadOnlyWithRel())
2390 MappingClass =
2391 TM.Options.XCOFFReadOnlyPointers ? XCOFF::XMC_RO : XCOFF::XMC_RW;
2392 else if (Kind.isReadOnly())
2393 MappingClass = XCOFF::XMC_RO;
2394 else
2395 report_fatal_error("XCOFF other section types not yet implemented.");
2396
2397 return getContext().getXCOFFSection(
2398 SectionName, Kind, XCOFF::CsectProperties(MappingClass, XCOFF::XTY_SD),
2399 /* MultiSymbolsAllowed*/ true);
2400 }
2401
getSectionForExternalReference(const GlobalObject * GO,const TargetMachine & TM) const2402 MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference(
2403 const GlobalObject *GO, const TargetMachine &TM) const {
2404 assert(GO->isDeclarationForLinker() &&
2405 "Tried to get ER section for a defined global.");
2406
2407 SmallString<128> Name;
2408 getNameWithPrefix(Name, GO, TM);
2409
2410 // AIX TLS local-dynamic does not need the external reference for the
2411 // "_$TLSML" symbol.
2412 if (GO->getThreadLocalMode() == GlobalVariable::LocalDynamicTLSModel &&
2413 GO->hasName() && GO->getName() == "_$TLSML") {
2414 return getContext().getXCOFFSection(
2415 Name, SectionKind::getData(),
2416 XCOFF::CsectProperties(XCOFF::XMC_TC, XCOFF::XTY_SD));
2417 }
2418
2419 XCOFF::StorageMappingClass SMC =
2420 isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA;
2421 if (GO->isThreadLocal())
2422 SMC = XCOFF::XMC_UL;
2423
2424 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
2425 if (GVar->hasAttribute("toc-data"))
2426 SMC = XCOFF::XMC_TD;
2427
2428 // Externals go into a csect of type ER.
2429 return getContext().getXCOFFSection(
2430 Name, SectionKind::getMetadata(),
2431 XCOFF::CsectProperties(SMC, XCOFF::XTY_ER));
2432 }
2433
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2434 MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
2435 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2436 // Handle the XCOFF::TD case first, then deal with the rest.
2437 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
2438 if (GVar->hasAttribute("toc-data")) {
2439 SmallString<128> Name;
2440 getNameWithPrefix(Name, GO, TM);
2441 XCOFF::SymbolType symType =
2442 GO->hasCommonLinkage() ? XCOFF::XTY_CM : XCOFF::XTY_SD;
2443 return getContext().getXCOFFSection(
2444 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, symType),
2445 /* MultiSymbolsAllowed*/ true);
2446 }
2447
2448 // Common symbols go into a csect with matching name which will get mapped
2449 // into the .bss section.
2450 // Zero-initialized local TLS symbols go into a csect with matching name which
2451 // will get mapped into the .tbss section.
2452 if (Kind.isBSSLocal() || GO->hasCommonLinkage() || Kind.isThreadBSSLocal()) {
2453 SmallString<128> Name;
2454 getNameWithPrefix(Name, GO, TM);
2455 XCOFF::StorageMappingClass SMC = Kind.isBSSLocal() ? XCOFF::XMC_BS
2456 : Kind.isCommon() ? XCOFF::XMC_RW
2457 : XCOFF::XMC_UL;
2458 return getContext().getXCOFFSection(
2459 Name, Kind, XCOFF::CsectProperties(SMC, XCOFF::XTY_CM));
2460 }
2461
2462 if (Kind.isText()) {
2463 if (TM.getFunctionSections()) {
2464 return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM))
2465 ->getRepresentedCsect();
2466 }
2467 return TextSection;
2468 }
2469
2470 if (TM.Options.XCOFFReadOnlyPointers && Kind.isReadOnlyWithRel()) {
2471 if (!TM.getDataSections())
2472 report_fatal_error(
2473 "ReadOnlyPointers is supported only if data sections is turned on");
2474
2475 SmallString<128> Name;
2476 getNameWithPrefix(Name, GO, TM);
2477 return getContext().getXCOFFSection(
2478 Name, SectionKind::getReadOnly(),
2479 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD));
2480 }
2481
2482 // For BSS kind, zero initialized data must be emitted to the .data section
2483 // because external linkage control sections that get mapped to the .bss
2484 // section will be linked as tentative defintions, which is only appropriate
2485 // for SectionKind::Common.
2486 if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) {
2487 if (TM.getDataSections()) {
2488 SmallString<128> Name;
2489 getNameWithPrefix(Name, GO, TM);
2490 return getContext().getXCOFFSection(
2491 Name, SectionKind::getData(),
2492 XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD));
2493 }
2494 return DataSection;
2495 }
2496
2497 if (Kind.isReadOnly()) {
2498 if (TM.getDataSections()) {
2499 SmallString<128> Name;
2500 getNameWithPrefix(Name, GO, TM);
2501 return getContext().getXCOFFSection(
2502 Name, SectionKind::getReadOnly(),
2503 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD));
2504 }
2505 return ReadOnlySection;
2506 }
2507
2508 // External/weak TLS data and initialized local TLS data are not eligible
2509 // to be put into common csect. If data sections are enabled, thread
2510 // data are emitted into separate sections. Otherwise, thread data
2511 // are emitted into the .tdata section.
2512 if (Kind.isThreadLocal()) {
2513 if (TM.getDataSections()) {
2514 SmallString<128> Name;
2515 getNameWithPrefix(Name, GO, TM);
2516 return getContext().getXCOFFSection(
2517 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TL, XCOFF::XTY_SD));
2518 }
2519 return TLSDataSection;
2520 }
2521
2522 report_fatal_error("XCOFF other section types not yet implemented.");
2523 }
2524
getSectionForJumpTable(const Function & F,const TargetMachine & TM) const2525 MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable(
2526 const Function &F, const TargetMachine &TM) const {
2527 assert (!F.getComdat() && "Comdat not supported on XCOFF.");
2528
2529 if (!TM.getFunctionSections())
2530 return ReadOnlySection;
2531
2532 // If the function can be removed, produce a unique section so that
2533 // the table doesn't prevent the removal.
2534 SmallString<128> NameStr(".rodata.jmp..");
2535 getNameWithPrefix(NameStr, &F, TM);
2536 return getContext().getXCOFFSection(
2537 NameStr, SectionKind::getReadOnly(),
2538 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD));
2539 }
2540
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const2541 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
2542 bool UsesLabelDifference, const Function &F) const {
2543 return false;
2544 }
2545
2546 /// Given a mergeable constant with the specified size and relocation
2547 /// information, return a section that it should be placed in.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const2548 MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant(
2549 const DataLayout &DL, SectionKind Kind, const Constant *C,
2550 Align &Alignment) const {
2551 // TODO: Enable emiting constant pool to unique sections when we support it.
2552 if (Alignment > Align(16))
2553 report_fatal_error("Alignments greater than 16 not yet supported.");
2554
2555 if (Alignment == Align(8)) {
2556 assert(ReadOnly8Section && "Section should always be initialized.");
2557 return ReadOnly8Section;
2558 }
2559
2560 if (Alignment == Align(16)) {
2561 assert(ReadOnly16Section && "Section should always be initialized.");
2562 return ReadOnly16Section;
2563 }
2564
2565 return ReadOnlySection;
2566 }
2567
Initialize(MCContext & Ctx,const TargetMachine & TgtM)2568 void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx,
2569 const TargetMachine &TgtM) {
2570 TargetLoweringObjectFile::Initialize(Ctx, TgtM);
2571 TTypeEncoding =
2572 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel |
2573 (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4
2574 : dwarf::DW_EH_PE_sdata8);
2575 PersonalityEncoding = 0;
2576 LSDAEncoding = 0;
2577 CallSiteEncoding = dwarf::DW_EH_PE_udata4;
2578
2579 // AIX debug for thread local location is not ready. And for integrated as
2580 // mode, the relocatable address for the thread local variable will cause
2581 // linker error. So disable the location attribute generation for thread local
2582 // variables for now.
2583 // FIXME: when TLS debug on AIX is ready, remove this setting.
2584 SupportDebugThreadLocalLocation = false;
2585 }
2586
getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym) const2587 MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection(
2588 unsigned Priority, const MCSymbol *KeySym) const {
2589 report_fatal_error("no static constructor section on AIX");
2590 }
2591
getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym) const2592 MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection(
2593 unsigned Priority, const MCSymbol *KeySym) const {
2594 report_fatal_error("no static destructor section on AIX");
2595 }
2596
lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM) const2597 const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference(
2598 const GlobalValue *LHS, const GlobalValue *RHS,
2599 const TargetMachine &TM) const {
2600 /* Not implemented yet, but don't crash, return nullptr. */
2601 return nullptr;
2602 }
2603
2604 XCOFF::StorageClass
getStorageClassForGlobal(const GlobalValue * GV)2605 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) {
2606 assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX.");
2607
2608 switch (GV->getLinkage()) {
2609 case GlobalValue::InternalLinkage:
2610 case GlobalValue::PrivateLinkage:
2611 return XCOFF::C_HIDEXT;
2612 case GlobalValue::ExternalLinkage:
2613 case GlobalValue::CommonLinkage:
2614 case GlobalValue::AvailableExternallyLinkage:
2615 return XCOFF::C_EXT;
2616 case GlobalValue::ExternalWeakLinkage:
2617 case GlobalValue::LinkOnceAnyLinkage:
2618 case GlobalValue::LinkOnceODRLinkage:
2619 case GlobalValue::WeakAnyLinkage:
2620 case GlobalValue::WeakODRLinkage:
2621 return XCOFF::C_WEAKEXT;
2622 case GlobalValue::AppendingLinkage:
2623 report_fatal_error(
2624 "There is no mapping that implements AppendingLinkage for XCOFF.");
2625 }
2626 llvm_unreachable("Unknown linkage type!");
2627 }
2628
getFunctionEntryPointSymbol(const GlobalValue * Func,const TargetMachine & TM) const2629 MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol(
2630 const GlobalValue *Func, const TargetMachine &TM) const {
2631 assert((isa<Function>(Func) ||
2632 (isa<GlobalAlias>(Func) &&
2633 isa_and_nonnull<Function>(
2634 cast<GlobalAlias>(Func)->getAliaseeObject()))) &&
2635 "Func must be a function or an alias which has a function as base "
2636 "object.");
2637
2638 SmallString<128> NameStr;
2639 NameStr.push_back('.');
2640 getNameWithPrefix(NameStr, Func, TM);
2641
2642 // When -function-sections is enabled and explicit section is not specified,
2643 // it's not necessary to emit function entry point label any more. We will use
2644 // function entry point csect instead. And for function delcarations, the
2645 // undefined symbols gets treated as csect with XTY_ER property.
2646 if (((TM.getFunctionSections() && !Func->hasSection()) ||
2647 Func->isDeclarationForLinker()) &&
2648 isa<Function>(Func)) {
2649 return getContext()
2650 .getXCOFFSection(
2651 NameStr, SectionKind::getText(),
2652 XCOFF::CsectProperties(XCOFF::XMC_PR, Func->isDeclarationForLinker()
2653 ? XCOFF::XTY_ER
2654 : XCOFF::XTY_SD))
2655 ->getQualNameSymbol();
2656 }
2657
2658 return getContext().getOrCreateSymbol(NameStr);
2659 }
2660
getSectionForFunctionDescriptor(const Function * F,const TargetMachine & TM) const2661 MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor(
2662 const Function *F, const TargetMachine &TM) const {
2663 SmallString<128> NameStr;
2664 getNameWithPrefix(NameStr, F, TM);
2665 return getContext().getXCOFFSection(
2666 NameStr, SectionKind::getData(),
2667 XCOFF::CsectProperties(XCOFF::XMC_DS, XCOFF::XTY_SD));
2668 }
2669
getSectionForTOCEntry(const MCSymbol * Sym,const TargetMachine & TM) const2670 MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry(
2671 const MCSymbol *Sym, const TargetMachine &TM) const {
2672 const XCOFF::StorageMappingClass SMC = [](const MCSymbol *Sym,
2673 const TargetMachine &TM) {
2674 const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(Sym);
2675
2676 // The "_$TLSML" symbol for TLS local-dynamic mode requires XMC_TC,
2677 // otherwise the AIX assembler will complain.
2678 if (XSym->getSymbolTableName() == "_$TLSML")
2679 return XCOFF::XMC_TC;
2680
2681 // Use large code model toc entries for ehinfo symbols as they are
2682 // never referenced directly. The runtime loads their TOC entry
2683 // addresses from the trace-back table.
2684 if (XSym->isEHInfo())
2685 return XCOFF::XMC_TE;
2686
2687 // If the symbol does not have a code model specified use the module value.
2688 if (!XSym->hasPerSymbolCodeModel())
2689 return TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE
2690 : XCOFF::XMC_TC;
2691
2692 return XSym->getPerSymbolCodeModel() == MCSymbolXCOFF::CM_Large
2693 ? XCOFF::XMC_TE
2694 : XCOFF::XMC_TC;
2695 }(Sym, TM);
2696
2697 return getContext().getXCOFFSection(
2698 cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(),
2699 XCOFF::CsectProperties(SMC, XCOFF::XTY_SD));
2700 }
2701
getSectionForLSDA(const Function & F,const MCSymbol & FnSym,const TargetMachine & TM) const2702 MCSection *TargetLoweringObjectFileXCOFF::getSectionForLSDA(
2703 const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
2704 auto *LSDA = cast<MCSectionXCOFF>(LSDASection);
2705 if (TM.getFunctionSections()) {
2706 // If option -ffunction-sections is on, append the function name to the
2707 // name of the LSDA csect so that each function has its own LSDA csect.
2708 // This helps the linker to garbage-collect EH info of unused functions.
2709 SmallString<128> NameStr = LSDA->getName();
2710 raw_svector_ostream(NameStr) << '.' << F.getName();
2711 LSDA = getContext().getXCOFFSection(NameStr, LSDA->getKind(),
2712 LSDA->getCsectProp());
2713 }
2714 return LSDA;
2715 }
2716 //===----------------------------------------------------------------------===//
2717 // GOFF
2718 //===----------------------------------------------------------------------===//
2719 TargetLoweringObjectFileGOFF::TargetLoweringObjectFileGOFF() = default;
2720
getExplicitSectionGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2721 MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
2722 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2723 return SelectSectionForGlobal(GO, Kind, TM);
2724 }
2725
getSectionForLSDA(const Function & F,const MCSymbol & FnSym,const TargetMachine & TM) const2726 MCSection *TargetLoweringObjectFileGOFF::getSectionForLSDA(
2727 const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
2728 std::string Name = ".gcc_exception_table." + F.getName().str();
2729 return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr, 0);
2730 }
2731
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const2732 MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
2733 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2734 auto *Symbol = TM.getSymbol(GO);
2735 if (Kind.isBSS())
2736 return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS(),
2737 nullptr, 0);
2738
2739 return getContext().getObjectFileInfo()->getTextSection();
2740 }
2741