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