1*0b57cec5SDimitry Andric //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements classes used to handle lowerings specific to common
10*0b57cec5SDimitry Andric // object file formats.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
17*0b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
18*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
19*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
20*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
21*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
22*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
23*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/MachO.h"
245ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
255ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
28*0b57cec5SDimitry Andric #include "llvm/IR/Comdat.h"
29*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
30*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
31*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
325ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
335ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
34*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
35*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h"
36*0b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h"
37*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
38*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
39*0b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
40*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
41*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
42*0b57cec5SDimitry Andric #include "llvm/IR/Type.h"
43*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
44*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
45*0b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
46*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionCOFF.h"
47*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
48*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionMachO.h"
49*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h"
508bcb0991SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h"
51*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
52*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
53*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h"
54*0b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
55*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h"
56*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
57*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
58*0b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
59*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
605ffd83dbSDimitry Andric #include "llvm/Support/Format.h"
61*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
62*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
63*0b57cec5SDimitry Andric #include <cassert>
64*0b57cec5SDimitry Andric #include <string>
65*0b57cec5SDimitry Andric 
66*0b57cec5SDimitry Andric using namespace llvm;
67*0b57cec5SDimitry Andric using namespace dwarf;
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
70*0b57cec5SDimitry Andric                              StringRef &Section) {
71*0b57cec5SDimitry Andric   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
72*0b57cec5SDimitry Andric   M.getModuleFlagsMetadata(ModuleFlags);
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric   for (const auto &MFE: ModuleFlags) {
75*0b57cec5SDimitry Andric     // Ignore flags with 'Require' behaviour.
76*0b57cec5SDimitry Andric     if (MFE.Behavior == Module::Require)
77*0b57cec5SDimitry Andric       continue;
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric     StringRef Key = MFE.Key->getString();
80*0b57cec5SDimitry Andric     if (Key == "Objective-C Image Info Version") {
81*0b57cec5SDimitry Andric       Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
82*0b57cec5SDimitry Andric     } else if (Key == "Objective-C Garbage Collection" ||
83*0b57cec5SDimitry Andric                Key == "Objective-C GC Only" ||
84*0b57cec5SDimitry Andric                Key == "Objective-C Is Simulated" ||
85*0b57cec5SDimitry Andric                Key == "Objective-C Class Properties" ||
86*0b57cec5SDimitry Andric                Key == "Objective-C Image Swift Version") {
87*0b57cec5SDimitry Andric       Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
88*0b57cec5SDimitry Andric     } else if (Key == "Objective-C Image Info Section") {
89*0b57cec5SDimitry Andric       Section = cast<MDString>(MFE.Val)->getString();
90*0b57cec5SDimitry Andric     }
915ffd83dbSDimitry Andric     // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor +
925ffd83dbSDimitry Andric     // "Objective-C Garbage Collection".
935ffd83dbSDimitry Andric     else if (Key == "Swift ABI Version") {
945ffd83dbSDimitry Andric       Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 8;
955ffd83dbSDimitry Andric     } else if (Key == "Swift Major Version") {
965ffd83dbSDimitry Andric       Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 24;
975ffd83dbSDimitry Andric     } else if (Key == "Swift Minor Version") {
985ffd83dbSDimitry Andric       Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 16;
995ffd83dbSDimitry Andric     }
100*0b57cec5SDimitry Andric   }
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
104*0b57cec5SDimitry Andric //                                  ELF
105*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
108*0b57cec5SDimitry Andric                                              const TargetMachine &TgtM) {
109*0b57cec5SDimitry Andric   TargetLoweringObjectFile::Initialize(Ctx, TgtM);
110*0b57cec5SDimitry Andric   TM = &TgtM;
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   CodeModel::Model CM = TgtM.getCodeModel();
1135ffd83dbSDimitry Andric   InitializeELF(TgtM.Options.UseInitArray);
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric   switch (TgtM.getTargetTriple().getArch()) {
116*0b57cec5SDimitry Andric   case Triple::arm:
117*0b57cec5SDimitry Andric   case Triple::armeb:
118*0b57cec5SDimitry Andric   case Triple::thumb:
119*0b57cec5SDimitry Andric   case Triple::thumbeb:
120*0b57cec5SDimitry Andric     if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
121*0b57cec5SDimitry Andric       break;
122*0b57cec5SDimitry Andric     // Fallthrough if not using EHABI
123*0b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
124*0b57cec5SDimitry Andric   case Triple::ppc:
125*0b57cec5SDimitry Andric   case Triple::x86:
126*0b57cec5SDimitry Andric     PersonalityEncoding = isPositionIndependent()
127*0b57cec5SDimitry Andric                               ? dwarf::DW_EH_PE_indirect |
128*0b57cec5SDimitry Andric                                     dwarf::DW_EH_PE_pcrel |
129*0b57cec5SDimitry Andric                                     dwarf::DW_EH_PE_sdata4
130*0b57cec5SDimitry Andric                               : dwarf::DW_EH_PE_absptr;
131*0b57cec5SDimitry Andric     LSDAEncoding = isPositionIndependent()
132*0b57cec5SDimitry Andric                        ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
133*0b57cec5SDimitry Andric                        : dwarf::DW_EH_PE_absptr;
134*0b57cec5SDimitry Andric     TTypeEncoding = isPositionIndependent()
135*0b57cec5SDimitry Andric                         ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
136*0b57cec5SDimitry Andric                               dwarf::DW_EH_PE_sdata4
137*0b57cec5SDimitry Andric                         : dwarf::DW_EH_PE_absptr;
138*0b57cec5SDimitry Andric     break;
139*0b57cec5SDimitry Andric   case Triple::x86_64:
140*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
141*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
142*0b57cec5SDimitry Andric         ((CM == CodeModel::Small || CM == CodeModel::Medium)
143*0b57cec5SDimitry Andric          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
144*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
145*0b57cec5SDimitry Andric         (CM == CodeModel::Small
146*0b57cec5SDimitry Andric          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
147*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
148*0b57cec5SDimitry Andric         ((CM == CodeModel::Small || CM == CodeModel::Medium)
149*0b57cec5SDimitry Andric          ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
150*0b57cec5SDimitry Andric     } else {
151*0b57cec5SDimitry Andric       PersonalityEncoding =
152*0b57cec5SDimitry Andric         (CM == CodeModel::Small || CM == CodeModel::Medium)
153*0b57cec5SDimitry Andric         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
154*0b57cec5SDimitry Andric       LSDAEncoding = (CM == CodeModel::Small)
155*0b57cec5SDimitry Andric         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
156*0b57cec5SDimitry Andric       TTypeEncoding = (CM == CodeModel::Small)
157*0b57cec5SDimitry Andric         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
158*0b57cec5SDimitry Andric     }
159*0b57cec5SDimitry Andric     break;
160*0b57cec5SDimitry Andric   case Triple::hexagon:
161*0b57cec5SDimitry Andric     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
162*0b57cec5SDimitry Andric     LSDAEncoding = dwarf::DW_EH_PE_absptr;
163*0b57cec5SDimitry Andric     TTypeEncoding = dwarf::DW_EH_PE_absptr;
164*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
165*0b57cec5SDimitry Andric       PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
166*0b57cec5SDimitry Andric       LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
167*0b57cec5SDimitry Andric       TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
168*0b57cec5SDimitry Andric     }
169*0b57cec5SDimitry Andric     break;
170*0b57cec5SDimitry Andric   case Triple::aarch64:
171*0b57cec5SDimitry Andric   case Triple::aarch64_be:
1728bcb0991SDimitry Andric   case Triple::aarch64_32:
173*0b57cec5SDimitry Andric     // The small model guarantees static code/data size < 4GB, but not where it
174*0b57cec5SDimitry Andric     // will be in memory. Most of these could end up >2GB away so even a signed
175*0b57cec5SDimitry Andric     // pc-relative 32-bit address is insufficient, theoretically.
176*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
177*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
178*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata8;
179*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
180*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
181*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata8;
182*0b57cec5SDimitry Andric     } else {
183*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
184*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_absptr;
185*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_absptr;
186*0b57cec5SDimitry Andric     }
187*0b57cec5SDimitry Andric     break;
188*0b57cec5SDimitry Andric   case Triple::lanai:
189*0b57cec5SDimitry Andric     LSDAEncoding = dwarf::DW_EH_PE_absptr;
190*0b57cec5SDimitry Andric     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
191*0b57cec5SDimitry Andric     TTypeEncoding = dwarf::DW_EH_PE_absptr;
192*0b57cec5SDimitry Andric     break;
193*0b57cec5SDimitry Andric   case Triple::mips:
194*0b57cec5SDimitry Andric   case Triple::mipsel:
195*0b57cec5SDimitry Andric   case Triple::mips64:
196*0b57cec5SDimitry Andric   case Triple::mips64el:
197*0b57cec5SDimitry Andric     // MIPS uses indirect pointer to refer personality functions and types, so
198*0b57cec5SDimitry Andric     // that the eh_frame section can be read-only. DW.ref.personality will be
199*0b57cec5SDimitry Andric     // generated for relocation.
200*0b57cec5SDimitry Andric     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
201*0b57cec5SDimitry Andric     // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
202*0b57cec5SDimitry Andric     //        identify N64 from just a triple.
203*0b57cec5SDimitry Andric     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
204*0b57cec5SDimitry Andric                     dwarf::DW_EH_PE_sdata4;
205*0b57cec5SDimitry Andric     // We don't support PC-relative LSDA references in GAS so we use the default
206*0b57cec5SDimitry Andric     // DW_EH_PE_absptr for those.
207*0b57cec5SDimitry Andric 
208*0b57cec5SDimitry Andric     // FreeBSD must be explicit about the data size and using pcrel since it's
209*0b57cec5SDimitry Andric     // assembler/linker won't do the automatic conversion that the Linux tools
210*0b57cec5SDimitry Andric     // do.
211*0b57cec5SDimitry Andric     if (TgtM.getTargetTriple().isOSFreeBSD()) {
212*0b57cec5SDimitry Andric       PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
213*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
214*0b57cec5SDimitry Andric     }
215*0b57cec5SDimitry Andric     break;
216*0b57cec5SDimitry Andric   case Triple::ppc64:
217*0b57cec5SDimitry Andric   case Triple::ppc64le:
218*0b57cec5SDimitry Andric     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
219*0b57cec5SDimitry Andric       dwarf::DW_EH_PE_udata8;
220*0b57cec5SDimitry Andric     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
221*0b57cec5SDimitry Andric     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
222*0b57cec5SDimitry Andric       dwarf::DW_EH_PE_udata8;
223*0b57cec5SDimitry Andric     break;
224*0b57cec5SDimitry Andric   case Triple::sparcel:
225*0b57cec5SDimitry Andric   case Triple::sparc:
226*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
227*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
228*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
229*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
230*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
231*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
232*0b57cec5SDimitry Andric     } else {
233*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_absptr;
234*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
235*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_absptr;
236*0b57cec5SDimitry Andric     }
237*0b57cec5SDimitry Andric     CallSiteEncoding = dwarf::DW_EH_PE_udata4;
238*0b57cec5SDimitry Andric     break;
239*0b57cec5SDimitry Andric   case Triple::riscv32:
240*0b57cec5SDimitry Andric   case Triple::riscv64:
241*0b57cec5SDimitry Andric     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
242*0b57cec5SDimitry Andric     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
243*0b57cec5SDimitry Andric                           dwarf::DW_EH_PE_sdata4;
244*0b57cec5SDimitry Andric     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
245*0b57cec5SDimitry Andric                     dwarf::DW_EH_PE_sdata4;
246*0b57cec5SDimitry Andric     CallSiteEncoding = dwarf::DW_EH_PE_udata4;
247*0b57cec5SDimitry Andric     break;
248*0b57cec5SDimitry Andric   case Triple::sparcv9:
249*0b57cec5SDimitry Andric     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
250*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
251*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
252*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
253*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
254*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
255*0b57cec5SDimitry Andric     } else {
256*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
257*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_absptr;
258*0b57cec5SDimitry Andric     }
259*0b57cec5SDimitry Andric     break;
260*0b57cec5SDimitry Andric   case Triple::systemz:
261*0b57cec5SDimitry Andric     // All currently-defined code models guarantee that 4-byte PC-relative
262*0b57cec5SDimitry Andric     // values will be in range.
263*0b57cec5SDimitry Andric     if (isPositionIndependent()) {
264*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
265*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
266*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
267*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
268*0b57cec5SDimitry Andric         dwarf::DW_EH_PE_sdata4;
269*0b57cec5SDimitry Andric     } else {
270*0b57cec5SDimitry Andric       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
271*0b57cec5SDimitry Andric       LSDAEncoding = dwarf::DW_EH_PE_absptr;
272*0b57cec5SDimitry Andric       TTypeEncoding = dwarf::DW_EH_PE_absptr;
273*0b57cec5SDimitry Andric     }
274*0b57cec5SDimitry Andric     break;
275*0b57cec5SDimitry Andric   default:
276*0b57cec5SDimitry Andric     break;
277*0b57cec5SDimitry Andric   }
278*0b57cec5SDimitry Andric }
279*0b57cec5SDimitry Andric 
280*0b57cec5SDimitry Andric void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
281*0b57cec5SDimitry Andric                                                      Module &M) const {
282*0b57cec5SDimitry Andric   auto &C = getContext();
283*0b57cec5SDimitry Andric 
284*0b57cec5SDimitry Andric   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
285*0b57cec5SDimitry Andric     auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
286*0b57cec5SDimitry Andric                               ELF::SHF_EXCLUDE);
287*0b57cec5SDimitry Andric 
288*0b57cec5SDimitry Andric     Streamer.SwitchSection(S);
289*0b57cec5SDimitry Andric 
290480093f4SDimitry Andric     for (const auto *Operand : LinkerOptions->operands()) {
291*0b57cec5SDimitry Andric       if (cast<MDNode>(Operand)->getNumOperands() != 2)
292*0b57cec5SDimitry Andric         report_fatal_error("invalid llvm.linker.options");
293*0b57cec5SDimitry Andric       for (const auto &Option : cast<MDNode>(Operand)->operands()) {
2945ffd83dbSDimitry Andric         Streamer.emitBytes(cast<MDString>(Option)->getString());
2955ffd83dbSDimitry Andric         Streamer.emitInt8(0);
296*0b57cec5SDimitry Andric       }
297*0b57cec5SDimitry Andric     }
298*0b57cec5SDimitry Andric   }
299*0b57cec5SDimitry Andric 
300*0b57cec5SDimitry Andric   if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) {
301*0b57cec5SDimitry Andric     auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES,
302*0b57cec5SDimitry Andric                               ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
303*0b57cec5SDimitry Andric 
304*0b57cec5SDimitry Andric     Streamer.SwitchSection(S);
305*0b57cec5SDimitry Andric 
306480093f4SDimitry Andric     for (const auto *Operand : DependentLibraries->operands()) {
3075ffd83dbSDimitry Andric       Streamer.emitBytes(
308*0b57cec5SDimitry Andric           cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString());
3095ffd83dbSDimitry Andric       Streamer.emitInt8(0);
310*0b57cec5SDimitry Andric     }
311*0b57cec5SDimitry Andric   }
312*0b57cec5SDimitry Andric 
313*0b57cec5SDimitry Andric   unsigned Version = 0;
314*0b57cec5SDimitry Andric   unsigned Flags = 0;
315*0b57cec5SDimitry Andric   StringRef Section;
316*0b57cec5SDimitry Andric 
317*0b57cec5SDimitry Andric   GetObjCImageInfo(M, Version, Flags, Section);
318*0b57cec5SDimitry Andric   if (!Section.empty()) {
319*0b57cec5SDimitry Andric     auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
320*0b57cec5SDimitry Andric     Streamer.SwitchSection(S);
3215ffd83dbSDimitry Andric     Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
3225ffd83dbSDimitry Andric     Streamer.emitInt32(Version);
3235ffd83dbSDimitry Andric     Streamer.emitInt32(Flags);
324*0b57cec5SDimitry Andric     Streamer.AddBlankLine();
325*0b57cec5SDimitry Andric   }
326*0b57cec5SDimitry Andric 
327*0b57cec5SDimitry Andric   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
328*0b57cec5SDimitry Andric   M.getModuleFlagsMetadata(ModuleFlags);
329*0b57cec5SDimitry Andric 
330*0b57cec5SDimitry Andric   MDNode *CFGProfile = nullptr;
331*0b57cec5SDimitry Andric 
332*0b57cec5SDimitry Andric   for (const auto &MFE : ModuleFlags) {
333*0b57cec5SDimitry Andric     StringRef Key = MFE.Key->getString();
334*0b57cec5SDimitry Andric     if (Key == "CG Profile") {
335*0b57cec5SDimitry Andric       CFGProfile = cast<MDNode>(MFE.Val);
336*0b57cec5SDimitry Andric       break;
337*0b57cec5SDimitry Andric     }
338*0b57cec5SDimitry Andric   }
339*0b57cec5SDimitry Andric 
340*0b57cec5SDimitry Andric   if (!CFGProfile)
341*0b57cec5SDimitry Andric     return;
342*0b57cec5SDimitry Andric 
343*0b57cec5SDimitry Andric   auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
344*0b57cec5SDimitry Andric     if (!MDO)
345*0b57cec5SDimitry Andric       return nullptr;
346*0b57cec5SDimitry Andric     auto V = cast<ValueAsMetadata>(MDO);
347*0b57cec5SDimitry Andric     const Function *F = cast<Function>(V->getValue());
348*0b57cec5SDimitry Andric     return TM->getSymbol(F);
349*0b57cec5SDimitry Andric   };
350*0b57cec5SDimitry Andric 
351*0b57cec5SDimitry Andric   for (const auto &Edge : CFGProfile->operands()) {
352*0b57cec5SDimitry Andric     MDNode *E = cast<MDNode>(Edge);
353*0b57cec5SDimitry Andric     const MCSymbol *From = GetSym(E->getOperand(0));
354*0b57cec5SDimitry Andric     const MCSymbol *To = GetSym(E->getOperand(1));
355*0b57cec5SDimitry Andric     // Skip null functions. This can happen if functions are dead stripped after
356*0b57cec5SDimitry Andric     // the CGProfile pass has been run.
357*0b57cec5SDimitry Andric     if (!From || !To)
358*0b57cec5SDimitry Andric       continue;
359*0b57cec5SDimitry Andric     uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
360*0b57cec5SDimitry Andric                          ->getValue()
361*0b57cec5SDimitry Andric                          ->getUniqueInteger()
362*0b57cec5SDimitry Andric                          .getZExtValue();
363*0b57cec5SDimitry Andric     Streamer.emitCGProfileEntry(
364*0b57cec5SDimitry Andric         MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
365*0b57cec5SDimitry Andric         MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
366*0b57cec5SDimitry Andric   }
367*0b57cec5SDimitry Andric }
368*0b57cec5SDimitry Andric 
369*0b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
370*0b57cec5SDimitry Andric     const GlobalValue *GV, const TargetMachine &TM,
371*0b57cec5SDimitry Andric     MachineModuleInfo *MMI) const {
372*0b57cec5SDimitry Andric   unsigned Encoding = getPersonalityEncoding();
373*0b57cec5SDimitry Andric   if ((Encoding & 0x80) == DW_EH_PE_indirect)
374*0b57cec5SDimitry Andric     return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
375*0b57cec5SDimitry Andric                                           TM.getSymbol(GV)->getName());
376*0b57cec5SDimitry Andric   if ((Encoding & 0x70) == DW_EH_PE_absptr)
377*0b57cec5SDimitry Andric     return TM.getSymbol(GV);
378*0b57cec5SDimitry Andric   report_fatal_error("We do not support this DWARF encoding yet!");
379*0b57cec5SDimitry Andric }
380*0b57cec5SDimitry Andric 
381*0b57cec5SDimitry Andric void TargetLoweringObjectFileELF::emitPersonalityValue(
382*0b57cec5SDimitry Andric     MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
383*0b57cec5SDimitry Andric   SmallString<64> NameData("DW.ref.");
384*0b57cec5SDimitry Andric   NameData += Sym->getName();
385*0b57cec5SDimitry Andric   MCSymbolELF *Label =
386*0b57cec5SDimitry Andric       cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
3875ffd83dbSDimitry Andric   Streamer.emitSymbolAttribute(Label, MCSA_Hidden);
3885ffd83dbSDimitry Andric   Streamer.emitSymbolAttribute(Label, MCSA_Weak);
389*0b57cec5SDimitry Andric   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
390*0b57cec5SDimitry Andric   MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
391*0b57cec5SDimitry Andric                                                    ELF::SHT_PROGBITS, Flags, 0);
392*0b57cec5SDimitry Andric   unsigned Size = DL.getPointerSize();
393*0b57cec5SDimitry Andric   Streamer.SwitchSection(Sec);
3945ffd83dbSDimitry Andric   Streamer.emitValueToAlignment(DL.getPointerABIAlignment(0).value());
3955ffd83dbSDimitry Andric   Streamer.emitSymbolAttribute(Label, MCSA_ELF_TypeObject);
396*0b57cec5SDimitry Andric   const MCExpr *E = MCConstantExpr::create(Size, getContext());
397*0b57cec5SDimitry Andric   Streamer.emitELFSize(Label, E);
3985ffd83dbSDimitry Andric   Streamer.emitLabel(Label);
399*0b57cec5SDimitry Andric 
4005ffd83dbSDimitry Andric   Streamer.emitSymbolValue(Sym, Size);
401*0b57cec5SDimitry Andric }
402*0b57cec5SDimitry Andric 
403*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
404*0b57cec5SDimitry Andric     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
405*0b57cec5SDimitry Andric     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
406*0b57cec5SDimitry Andric   if (Encoding & DW_EH_PE_indirect) {
407*0b57cec5SDimitry Andric     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
408*0b57cec5SDimitry Andric 
409*0b57cec5SDimitry Andric     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
410*0b57cec5SDimitry Andric 
411*0b57cec5SDimitry Andric     // Add information about the stub reference to ELFMMI so that the stub
412*0b57cec5SDimitry Andric     // gets emitted by the asmprinter.
413*0b57cec5SDimitry Andric     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
414*0b57cec5SDimitry Andric     if (!StubSym.getPointer()) {
415*0b57cec5SDimitry Andric       MCSymbol *Sym = TM.getSymbol(GV);
416*0b57cec5SDimitry Andric       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
417*0b57cec5SDimitry Andric     }
418*0b57cec5SDimitry Andric 
419*0b57cec5SDimitry Andric     return TargetLoweringObjectFile::
420*0b57cec5SDimitry Andric       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
421*0b57cec5SDimitry Andric                         Encoding & ~DW_EH_PE_indirect, Streamer);
422*0b57cec5SDimitry Andric   }
423*0b57cec5SDimitry Andric 
424*0b57cec5SDimitry Andric   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
425*0b57cec5SDimitry Andric                                                            MMI, Streamer);
426*0b57cec5SDimitry Andric }
427*0b57cec5SDimitry Andric 
428*0b57cec5SDimitry Andric static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
429*0b57cec5SDimitry Andric   // N.B.: The defaults used in here are not the same ones used in MC.
430*0b57cec5SDimitry Andric   // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
431*0b57cec5SDimitry Andric   // both gas and MC will produce a section with no flags. Given
432*0b57cec5SDimitry Andric   // section(".eh_frame") gcc will produce:
433*0b57cec5SDimitry Andric   //
434*0b57cec5SDimitry Andric   //   .section   .eh_frame,"a",@progbits
435*0b57cec5SDimitry Andric 
436*0b57cec5SDimitry Andric   if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
4375ffd83dbSDimitry Andric                                       /*AddSegmentInfo=*/false) ||
4385ffd83dbSDimitry Andric       Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF,
439*0b57cec5SDimitry Andric                                       /*AddSegmentInfo=*/false))
440*0b57cec5SDimitry Andric     return SectionKind::getMetadata();
441*0b57cec5SDimitry Andric 
442*0b57cec5SDimitry Andric   if (Name.empty() || Name[0] != '.') return K;
443*0b57cec5SDimitry Andric 
444*0b57cec5SDimitry Andric   // Default implementation based on some magic section names.
445*0b57cec5SDimitry Andric   if (Name == ".bss" ||
446*0b57cec5SDimitry Andric       Name.startswith(".bss.") ||
447*0b57cec5SDimitry Andric       Name.startswith(".gnu.linkonce.b.") ||
448*0b57cec5SDimitry Andric       Name.startswith(".llvm.linkonce.b.") ||
449*0b57cec5SDimitry Andric       Name == ".sbss" ||
450*0b57cec5SDimitry Andric       Name.startswith(".sbss.") ||
451*0b57cec5SDimitry Andric       Name.startswith(".gnu.linkonce.sb.") ||
452*0b57cec5SDimitry Andric       Name.startswith(".llvm.linkonce.sb."))
453*0b57cec5SDimitry Andric     return SectionKind::getBSS();
454*0b57cec5SDimitry Andric 
455*0b57cec5SDimitry Andric   if (Name == ".tdata" ||
456*0b57cec5SDimitry Andric       Name.startswith(".tdata.") ||
457*0b57cec5SDimitry Andric       Name.startswith(".gnu.linkonce.td.") ||
458*0b57cec5SDimitry Andric       Name.startswith(".llvm.linkonce.td."))
459*0b57cec5SDimitry Andric     return SectionKind::getThreadData();
460*0b57cec5SDimitry Andric 
461*0b57cec5SDimitry Andric   if (Name == ".tbss" ||
462*0b57cec5SDimitry Andric       Name.startswith(".tbss.") ||
463*0b57cec5SDimitry Andric       Name.startswith(".gnu.linkonce.tb.") ||
464*0b57cec5SDimitry Andric       Name.startswith(".llvm.linkonce.tb."))
465*0b57cec5SDimitry Andric     return SectionKind::getThreadBSS();
466*0b57cec5SDimitry Andric 
467*0b57cec5SDimitry Andric   return K;
468*0b57cec5SDimitry Andric }
469*0b57cec5SDimitry Andric 
470*0b57cec5SDimitry Andric static unsigned getELFSectionType(StringRef Name, SectionKind K) {
471*0b57cec5SDimitry Andric   // Use SHT_NOTE for section whose name starts with ".note" to allow
472*0b57cec5SDimitry Andric   // emitting ELF notes from C variable declaration.
473*0b57cec5SDimitry Andric   // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
474*0b57cec5SDimitry Andric   if (Name.startswith(".note"))
475*0b57cec5SDimitry Andric     return ELF::SHT_NOTE;
476*0b57cec5SDimitry Andric 
477*0b57cec5SDimitry Andric   if (Name == ".init_array")
478*0b57cec5SDimitry Andric     return ELF::SHT_INIT_ARRAY;
479*0b57cec5SDimitry Andric 
480*0b57cec5SDimitry Andric   if (Name == ".fini_array")
481*0b57cec5SDimitry Andric     return ELF::SHT_FINI_ARRAY;
482*0b57cec5SDimitry Andric 
483*0b57cec5SDimitry Andric   if (Name == ".preinit_array")
484*0b57cec5SDimitry Andric     return ELF::SHT_PREINIT_ARRAY;
485*0b57cec5SDimitry Andric 
486*0b57cec5SDimitry Andric   if (K.isBSS() || K.isThreadBSS())
487*0b57cec5SDimitry Andric     return ELF::SHT_NOBITS;
488*0b57cec5SDimitry Andric 
489*0b57cec5SDimitry Andric   return ELF::SHT_PROGBITS;
490*0b57cec5SDimitry Andric }
491*0b57cec5SDimitry Andric 
492*0b57cec5SDimitry Andric static unsigned getELFSectionFlags(SectionKind K) {
493*0b57cec5SDimitry Andric   unsigned Flags = 0;
494*0b57cec5SDimitry Andric 
495*0b57cec5SDimitry Andric   if (!K.isMetadata())
496*0b57cec5SDimitry Andric     Flags |= ELF::SHF_ALLOC;
497*0b57cec5SDimitry Andric 
498*0b57cec5SDimitry Andric   if (K.isText())
499*0b57cec5SDimitry Andric     Flags |= ELF::SHF_EXECINSTR;
500*0b57cec5SDimitry Andric 
501*0b57cec5SDimitry Andric   if (K.isExecuteOnly())
502*0b57cec5SDimitry Andric     Flags |= ELF::SHF_ARM_PURECODE;
503*0b57cec5SDimitry Andric 
504*0b57cec5SDimitry Andric   if (K.isWriteable())
505*0b57cec5SDimitry Andric     Flags |= ELF::SHF_WRITE;
506*0b57cec5SDimitry Andric 
507*0b57cec5SDimitry Andric   if (K.isThreadLocal())
508*0b57cec5SDimitry Andric     Flags |= ELF::SHF_TLS;
509*0b57cec5SDimitry Andric 
510*0b57cec5SDimitry Andric   if (K.isMergeableCString() || K.isMergeableConst())
511*0b57cec5SDimitry Andric     Flags |= ELF::SHF_MERGE;
512*0b57cec5SDimitry Andric 
513*0b57cec5SDimitry Andric   if (K.isMergeableCString())
514*0b57cec5SDimitry Andric     Flags |= ELF::SHF_STRINGS;
515*0b57cec5SDimitry Andric 
516*0b57cec5SDimitry Andric   return Flags;
517*0b57cec5SDimitry Andric }
518*0b57cec5SDimitry Andric 
519*0b57cec5SDimitry Andric static const Comdat *getELFComdat(const GlobalValue *GV) {
520*0b57cec5SDimitry Andric   const Comdat *C = GV->getComdat();
521*0b57cec5SDimitry Andric   if (!C)
522*0b57cec5SDimitry Andric     return nullptr;
523*0b57cec5SDimitry Andric 
524*0b57cec5SDimitry Andric   if (C->getSelectionKind() != Comdat::Any)
525*0b57cec5SDimitry Andric     report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
526*0b57cec5SDimitry Andric                        C->getName() + "' cannot be lowered.");
527*0b57cec5SDimitry Andric 
528*0b57cec5SDimitry Andric   return C;
529*0b57cec5SDimitry Andric }
530*0b57cec5SDimitry Andric 
5315ffd83dbSDimitry Andric static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO,
532*0b57cec5SDimitry Andric                                             const TargetMachine &TM) {
533*0b57cec5SDimitry Andric   MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
534*0b57cec5SDimitry Andric   if (!MD)
535*0b57cec5SDimitry Andric     return nullptr;
536*0b57cec5SDimitry Andric 
537*0b57cec5SDimitry Andric   const MDOperand &Op = MD->getOperand(0);
538*0b57cec5SDimitry Andric   if (!Op.get())
539*0b57cec5SDimitry Andric     return nullptr;
540*0b57cec5SDimitry Andric 
541*0b57cec5SDimitry Andric   auto *VM = dyn_cast<ValueAsMetadata>(Op);
542*0b57cec5SDimitry Andric   if (!VM)
543*0b57cec5SDimitry Andric     report_fatal_error("MD_associated operand is not ValueAsMetadata");
544*0b57cec5SDimitry Andric 
5458bcb0991SDimitry Andric   auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue());
5468bcb0991SDimitry Andric   return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr;
547*0b57cec5SDimitry Andric }
548*0b57cec5SDimitry Andric 
549*0b57cec5SDimitry Andric static unsigned getEntrySizeForKind(SectionKind Kind) {
550*0b57cec5SDimitry Andric   if (Kind.isMergeable1ByteCString())
551*0b57cec5SDimitry Andric     return 1;
552*0b57cec5SDimitry Andric   else if (Kind.isMergeable2ByteCString())
553*0b57cec5SDimitry Andric     return 2;
554*0b57cec5SDimitry Andric   else if (Kind.isMergeable4ByteCString())
555*0b57cec5SDimitry Andric     return 4;
556*0b57cec5SDimitry Andric   else if (Kind.isMergeableConst4())
557*0b57cec5SDimitry Andric     return 4;
558*0b57cec5SDimitry Andric   else if (Kind.isMergeableConst8())
559*0b57cec5SDimitry Andric     return 8;
560*0b57cec5SDimitry Andric   else if (Kind.isMergeableConst16())
561*0b57cec5SDimitry Andric     return 16;
562*0b57cec5SDimitry Andric   else if (Kind.isMergeableConst32())
563*0b57cec5SDimitry Andric     return 32;
564*0b57cec5SDimitry Andric   else {
565*0b57cec5SDimitry Andric     // We shouldn't have mergeable C strings or mergeable constants that we
566*0b57cec5SDimitry Andric     // didn't handle above.
567*0b57cec5SDimitry Andric     assert(!Kind.isMergeableCString() && "unknown string width");
568*0b57cec5SDimitry Andric     assert(!Kind.isMergeableConst() && "unknown data width");
569*0b57cec5SDimitry Andric     return 0;
570*0b57cec5SDimitry Andric   }
571*0b57cec5SDimitry Andric }
572*0b57cec5SDimitry Andric 
5735ffd83dbSDimitry Andric /// Return the section prefix name used by options FunctionsSections and
5745ffd83dbSDimitry Andric /// DataSections.
5755ffd83dbSDimitry Andric static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
5765ffd83dbSDimitry Andric   if (Kind.isText())
5775ffd83dbSDimitry Andric     return ".text";
5785ffd83dbSDimitry Andric   if (Kind.isReadOnly())
5795ffd83dbSDimitry Andric     return ".rodata";
5805ffd83dbSDimitry Andric   if (Kind.isBSS())
5815ffd83dbSDimitry Andric     return ".bss";
5825ffd83dbSDimitry Andric   if (Kind.isThreadData())
5835ffd83dbSDimitry Andric     return ".tdata";
5845ffd83dbSDimitry Andric   if (Kind.isThreadBSS())
5855ffd83dbSDimitry Andric     return ".tbss";
5865ffd83dbSDimitry Andric   if (Kind.isData())
5875ffd83dbSDimitry Andric     return ".data";
5885ffd83dbSDimitry Andric   if (Kind.isReadOnlyWithRel())
5895ffd83dbSDimitry Andric     return ".data.rel.ro";
5905ffd83dbSDimitry Andric   llvm_unreachable("Unknown section kind");
5915ffd83dbSDimitry Andric }
5925ffd83dbSDimitry Andric 
5935ffd83dbSDimitry Andric static SmallString<128>
5945ffd83dbSDimitry Andric getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
5955ffd83dbSDimitry Andric                            Mangler &Mang, const TargetMachine &TM,
5965ffd83dbSDimitry Andric                            unsigned EntrySize, bool UniqueSectionName) {
5975ffd83dbSDimitry Andric   SmallString<128> Name;
5985ffd83dbSDimitry Andric   if (Kind.isMergeableCString()) {
5995ffd83dbSDimitry Andric     // We also need alignment here.
6005ffd83dbSDimitry Andric     // FIXME: this is getting the alignment of the character, not the
6015ffd83dbSDimitry Andric     // alignment of the global!
6025ffd83dbSDimitry Andric     Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
6035ffd83dbSDimitry Andric         cast<GlobalVariable>(GO));
6045ffd83dbSDimitry Andric 
6055ffd83dbSDimitry Andric     std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
6065ffd83dbSDimitry Andric     Name = SizeSpec + utostr(Alignment.value());
6075ffd83dbSDimitry Andric   } else if (Kind.isMergeableConst()) {
6085ffd83dbSDimitry Andric     Name = ".rodata.cst";
6095ffd83dbSDimitry Andric     Name += utostr(EntrySize);
6105ffd83dbSDimitry Andric   } else {
6115ffd83dbSDimitry Andric     Name = getSectionPrefixForGlobal(Kind);
6125ffd83dbSDimitry Andric   }
6135ffd83dbSDimitry Andric 
6145ffd83dbSDimitry Andric   bool HasPrefix = false;
6155ffd83dbSDimitry Andric   if (const auto *F = dyn_cast<Function>(GO)) {
6165ffd83dbSDimitry Andric     if (Optional<StringRef> Prefix = F->getSectionPrefix()) {
6175ffd83dbSDimitry Andric       Name += *Prefix;
6185ffd83dbSDimitry Andric       HasPrefix = true;
6195ffd83dbSDimitry Andric     }
6205ffd83dbSDimitry Andric   }
6215ffd83dbSDimitry Andric 
6225ffd83dbSDimitry Andric   if (UniqueSectionName) {
6235ffd83dbSDimitry Andric     Name.push_back('.');
6245ffd83dbSDimitry Andric     TM.getNameWithPrefix(Name, GO, Mang, /*MayAlwaysUsePrivate*/true);
6255ffd83dbSDimitry Andric   } else if (HasPrefix)
6265ffd83dbSDimitry Andric     Name.push_back('.');
6275ffd83dbSDimitry Andric   return Name;
6285ffd83dbSDimitry Andric }
6295ffd83dbSDimitry Andric 
6305ffd83dbSDimitry Andric namespace {
6315ffd83dbSDimitry Andric class LoweringDiagnosticInfo : public DiagnosticInfo {
6325ffd83dbSDimitry Andric   const Twine &Msg;
6335ffd83dbSDimitry Andric 
6345ffd83dbSDimitry Andric public:
6355ffd83dbSDimitry Andric   LoweringDiagnosticInfo(const Twine &DiagMsg,
6365ffd83dbSDimitry Andric                          DiagnosticSeverity Severity = DS_Error)
6375ffd83dbSDimitry Andric       : DiagnosticInfo(DK_Lowering, Severity), Msg(DiagMsg) {}
6385ffd83dbSDimitry Andric   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
6395ffd83dbSDimitry Andric };
6405ffd83dbSDimitry Andric }
6415ffd83dbSDimitry Andric 
642*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
643*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
644*0b57cec5SDimitry Andric   StringRef SectionName = GO->getSection();
645*0b57cec5SDimitry Andric 
646*0b57cec5SDimitry Andric   // Check if '#pragma clang section' name is applicable.
647*0b57cec5SDimitry Andric   // Note that pragma directive overrides -ffunction-section, -fdata-section
648*0b57cec5SDimitry Andric   // and so section name is exactly as user specified and not uniqued.
649*0b57cec5SDimitry Andric   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
650*0b57cec5SDimitry Andric   if (GV && GV->hasImplicitSection()) {
651*0b57cec5SDimitry Andric     auto Attrs = GV->getAttributes();
652*0b57cec5SDimitry Andric     if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
653*0b57cec5SDimitry Andric       SectionName = Attrs.getAttribute("bss-section").getValueAsString();
654*0b57cec5SDimitry Andric     } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
655*0b57cec5SDimitry Andric       SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
6568bcb0991SDimitry Andric     } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
6578bcb0991SDimitry Andric       SectionName = Attrs.getAttribute("relro-section").getValueAsString();
658*0b57cec5SDimitry Andric     } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
659*0b57cec5SDimitry Andric       SectionName = Attrs.getAttribute("data-section").getValueAsString();
660*0b57cec5SDimitry Andric     }
661*0b57cec5SDimitry Andric   }
662*0b57cec5SDimitry Andric   const Function *F = dyn_cast<Function>(GO);
663*0b57cec5SDimitry Andric   if (F && F->hasFnAttribute("implicit-section-name")) {
664*0b57cec5SDimitry Andric     SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
665*0b57cec5SDimitry Andric   }
666*0b57cec5SDimitry Andric 
667*0b57cec5SDimitry Andric   // Infer section flags from the section name if we can.
668*0b57cec5SDimitry Andric   Kind = getELFKindForNamedSection(SectionName, Kind);
669*0b57cec5SDimitry Andric 
670*0b57cec5SDimitry Andric   StringRef Group = "";
671*0b57cec5SDimitry Andric   unsigned Flags = getELFSectionFlags(Kind);
672*0b57cec5SDimitry Andric   if (const Comdat *C = getELFComdat(GO)) {
673*0b57cec5SDimitry Andric     Group = C->getName();
674*0b57cec5SDimitry Andric     Flags |= ELF::SHF_GROUP;
675*0b57cec5SDimitry Andric   }
676*0b57cec5SDimitry Andric 
6775ffd83dbSDimitry Andric   unsigned EntrySize = getEntrySizeForKind(Kind);
6785ffd83dbSDimitry Andric 
679*0b57cec5SDimitry Andric   // A section can have at most one associated section. Put each global with
680*0b57cec5SDimitry Andric   // MD_associated in a unique section.
681*0b57cec5SDimitry Andric   unsigned UniqueID = MCContext::GenericSectionID;
6825ffd83dbSDimitry Andric   const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
6835ffd83dbSDimitry Andric   if (LinkedToSym) {
684*0b57cec5SDimitry Andric     UniqueID = NextUniqueID++;
685*0b57cec5SDimitry Andric     Flags |= ELF::SHF_LINK_ORDER;
6865ffd83dbSDimitry Andric   } else {
6875ffd83dbSDimitry Andric     if (getContext().getAsmInfo()->useIntegratedAssembler()) {
6885ffd83dbSDimitry Andric       // Symbols must be placed into sections with compatible entry
6895ffd83dbSDimitry Andric       // sizes. Generate unique sections for symbols that have not
6905ffd83dbSDimitry Andric       // been assigned to compatible sections.
6915ffd83dbSDimitry Andric       if (Flags & ELF::SHF_MERGE) {
6925ffd83dbSDimitry Andric         auto maybeID = getContext().getELFUniqueIDForEntsize(SectionName, Flags,
6935ffd83dbSDimitry Andric                                                              EntrySize);
6945ffd83dbSDimitry Andric         if (maybeID)
6955ffd83dbSDimitry Andric           UniqueID = *maybeID;
6965ffd83dbSDimitry Andric         else {
6975ffd83dbSDimitry Andric           // If the user has specified the same section name as would be created
6985ffd83dbSDimitry Andric           // implicitly for this symbol e.g. .rodata.str1.1, then we don't need
6995ffd83dbSDimitry Andric           // to unique the section as the entry size for this symbol will be
7005ffd83dbSDimitry Andric           // compatible with implicitly created sections.
7015ffd83dbSDimitry Andric           SmallString<128> ImplicitSectionNameStem = getELFSectionNameForGlobal(
7025ffd83dbSDimitry Andric               GO, Kind, getMangler(), TM, EntrySize, false);
7035ffd83dbSDimitry Andric           if (!(getContext().isELFImplicitMergeableSectionNamePrefix(
7045ffd83dbSDimitry Andric                     SectionName) &&
7055ffd83dbSDimitry Andric                 SectionName.startswith(ImplicitSectionNameStem)))
7065ffd83dbSDimitry Andric             UniqueID = NextUniqueID++;
7075ffd83dbSDimitry Andric         }
7085ffd83dbSDimitry Andric       } else {
7095ffd83dbSDimitry Andric         // We need to unique the section if the user has explicity
7105ffd83dbSDimitry Andric         // assigned a non-mergeable symbol to a section name for
7115ffd83dbSDimitry Andric         // a generic mergeable section.
7125ffd83dbSDimitry Andric         if (getContext().isELFGenericMergeableSection(SectionName)) {
7135ffd83dbSDimitry Andric           auto maybeID = getContext().getELFUniqueIDForEntsize(
7145ffd83dbSDimitry Andric               SectionName, Flags, EntrySize);
7155ffd83dbSDimitry Andric           UniqueID = maybeID ? *maybeID : NextUniqueID++;
7165ffd83dbSDimitry Andric         }
7175ffd83dbSDimitry Andric       }
7185ffd83dbSDimitry Andric     } else {
7195ffd83dbSDimitry Andric       // If two symbols with differing sizes end up in the same mergeable
7205ffd83dbSDimitry Andric       // section that section can be assigned an incorrect entry size. To avoid
7215ffd83dbSDimitry Andric       // this we usually put symbols of the same size into distinct mergeable
7225ffd83dbSDimitry Andric       // sections with the same name. Doing so relies on the ",unique ,"
7235ffd83dbSDimitry Andric       // assembly feature. This feature is not avalible until bintuils
7245ffd83dbSDimitry Andric       // version 2.35 (https://sourceware.org/bugzilla/show_bug.cgi?id=25380).
7255ffd83dbSDimitry Andric       Flags &= ~ELF::SHF_MERGE;
7265ffd83dbSDimitry Andric       EntrySize = 0;
7275ffd83dbSDimitry Andric     }
728*0b57cec5SDimitry Andric   }
729*0b57cec5SDimitry Andric 
730*0b57cec5SDimitry Andric   MCSectionELF *Section = getContext().getELFSection(
731*0b57cec5SDimitry Andric       SectionName, getELFSectionType(SectionName, Kind), Flags,
7325ffd83dbSDimitry Andric       EntrySize, Group, UniqueID, LinkedToSym);
733*0b57cec5SDimitry Andric   // Make sure that we did not get some other section with incompatible sh_link.
734*0b57cec5SDimitry Andric   // This should not be possible due to UniqueID code above.
7355ffd83dbSDimitry Andric   assert(Section->getLinkedToSymbol() == LinkedToSym &&
736*0b57cec5SDimitry Andric          "Associated symbol mismatch between sections");
7375ffd83dbSDimitry Andric 
7385ffd83dbSDimitry Andric   if (!getContext().getAsmInfo()->useIntegratedAssembler()) {
7395ffd83dbSDimitry Andric     // If we are not using the integrated assembler then this symbol might have
7405ffd83dbSDimitry Andric     // been placed in an incompatible mergeable section. Emit an error if this
7415ffd83dbSDimitry Andric     // is the case to avoid creating broken output.
7425ffd83dbSDimitry Andric     if ((Section->getFlags() & ELF::SHF_MERGE) &&
7435ffd83dbSDimitry Andric         (Section->getEntrySize() != getEntrySizeForKind(Kind)))
7445ffd83dbSDimitry Andric       GO->getContext().diagnose(LoweringDiagnosticInfo(
7455ffd83dbSDimitry Andric           "Symbol '" + GO->getName() + "' from module '" +
7465ffd83dbSDimitry Andric           (GO->getParent() ? GO->getParent()->getSourceFileName() : "unknown") +
7475ffd83dbSDimitry Andric           "' required a section with entry-size=" +
7485ffd83dbSDimitry Andric           Twine(getEntrySizeForKind(Kind)) + " but was placed in section '" +
7495ffd83dbSDimitry Andric           SectionName + "' with entry-size=" + Twine(Section->getEntrySize()) +
7505ffd83dbSDimitry Andric           ": Explicit assignment by pragma or attribute of an incompatible "
7515ffd83dbSDimitry Andric           "symbol to this section?"));
752*0b57cec5SDimitry Andric   }
753*0b57cec5SDimitry Andric 
7545ffd83dbSDimitry Andric   return Section;
755*0b57cec5SDimitry Andric }
756*0b57cec5SDimitry Andric 
757*0b57cec5SDimitry Andric static MCSectionELF *selectELFSectionForGlobal(
758*0b57cec5SDimitry Andric     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
759*0b57cec5SDimitry Andric     const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
760*0b57cec5SDimitry Andric     unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
761*0b57cec5SDimitry Andric 
762*0b57cec5SDimitry Andric   StringRef Group = "";
763*0b57cec5SDimitry Andric   if (const Comdat *C = getELFComdat(GO)) {
764*0b57cec5SDimitry Andric     Flags |= ELF::SHF_GROUP;
765*0b57cec5SDimitry Andric     Group = C->getName();
766*0b57cec5SDimitry Andric   }
767*0b57cec5SDimitry Andric 
768*0b57cec5SDimitry Andric   // Get the section entry size based on the kind.
769*0b57cec5SDimitry Andric   unsigned EntrySize = getEntrySizeForKind(Kind);
770*0b57cec5SDimitry Andric 
7715ffd83dbSDimitry Andric   bool UniqueSectionName = false;
772*0b57cec5SDimitry Andric   unsigned UniqueID = MCContext::GenericSectionID;
773*0b57cec5SDimitry Andric   if (EmitUniqueSection) {
774*0b57cec5SDimitry Andric     if (TM.getUniqueSectionNames()) {
7755ffd83dbSDimitry Andric       UniqueSectionName = true;
776*0b57cec5SDimitry Andric     } else {
777*0b57cec5SDimitry Andric       UniqueID = *NextUniqueID;
778*0b57cec5SDimitry Andric       (*NextUniqueID)++;
779*0b57cec5SDimitry Andric     }
780*0b57cec5SDimitry Andric   }
7815ffd83dbSDimitry Andric   SmallString<128> Name = getELFSectionNameForGlobal(
7825ffd83dbSDimitry Andric       GO, Kind, Mang, TM, EntrySize, UniqueSectionName);
7835ffd83dbSDimitry Andric 
784*0b57cec5SDimitry Andric   // Use 0 as the unique ID for execute-only text.
785*0b57cec5SDimitry Andric   if (Kind.isExecuteOnly())
786*0b57cec5SDimitry Andric     UniqueID = 0;
787*0b57cec5SDimitry Andric   return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
788*0b57cec5SDimitry Andric                            EntrySize, Group, UniqueID, AssociatedSymbol);
789*0b57cec5SDimitry Andric }
790*0b57cec5SDimitry Andric 
791*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
792*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
793*0b57cec5SDimitry Andric   unsigned Flags = getELFSectionFlags(Kind);
794*0b57cec5SDimitry Andric 
795*0b57cec5SDimitry Andric   // If we have -ffunction-section or -fdata-section then we should emit the
796*0b57cec5SDimitry Andric   // global value to a uniqued section specifically for it.
797*0b57cec5SDimitry Andric   bool EmitUniqueSection = false;
798*0b57cec5SDimitry Andric   if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
799*0b57cec5SDimitry Andric     if (Kind.isText())
800*0b57cec5SDimitry Andric       EmitUniqueSection = TM.getFunctionSections();
801*0b57cec5SDimitry Andric     else
802*0b57cec5SDimitry Andric       EmitUniqueSection = TM.getDataSections();
803*0b57cec5SDimitry Andric   }
804*0b57cec5SDimitry Andric   EmitUniqueSection |= GO->hasComdat();
805*0b57cec5SDimitry Andric 
8065ffd83dbSDimitry Andric   const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
8075ffd83dbSDimitry Andric   if (LinkedToSym) {
808*0b57cec5SDimitry Andric     EmitUniqueSection = true;
809*0b57cec5SDimitry Andric     Flags |= ELF::SHF_LINK_ORDER;
810*0b57cec5SDimitry Andric   }
811*0b57cec5SDimitry Andric 
812*0b57cec5SDimitry Andric   MCSectionELF *Section = selectELFSectionForGlobal(
813*0b57cec5SDimitry Andric       getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
8145ffd83dbSDimitry Andric       &NextUniqueID, LinkedToSym);
8155ffd83dbSDimitry Andric   assert(Section->getLinkedToSymbol() == LinkedToSym);
816*0b57cec5SDimitry Andric   return Section;
817*0b57cec5SDimitry Andric }
818*0b57cec5SDimitry Andric 
819*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
820*0b57cec5SDimitry Andric     const Function &F, const TargetMachine &TM) const {
821*0b57cec5SDimitry Andric   // If the function can be removed, produce a unique section so that
822*0b57cec5SDimitry Andric   // the table doesn't prevent the removal.
823*0b57cec5SDimitry Andric   const Comdat *C = F.getComdat();
824*0b57cec5SDimitry Andric   bool EmitUniqueSection = TM.getFunctionSections() || C;
825*0b57cec5SDimitry Andric   if (!EmitUniqueSection)
826*0b57cec5SDimitry Andric     return ReadOnlySection;
827*0b57cec5SDimitry Andric 
828*0b57cec5SDimitry Andric   return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
829*0b57cec5SDimitry Andric                                    getMangler(), TM, EmitUniqueSection,
830*0b57cec5SDimitry Andric                                    ELF::SHF_ALLOC, &NextUniqueID,
831*0b57cec5SDimitry Andric                                    /* AssociatedSymbol */ nullptr);
832*0b57cec5SDimitry Andric }
833*0b57cec5SDimitry Andric 
834*0b57cec5SDimitry Andric bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
835*0b57cec5SDimitry Andric     bool UsesLabelDifference, const Function &F) const {
836*0b57cec5SDimitry Andric   // We can always create relative relocations, so use another section
837*0b57cec5SDimitry Andric   // that can be marked non-executable.
838*0b57cec5SDimitry Andric   return false;
839*0b57cec5SDimitry Andric }
840*0b57cec5SDimitry Andric 
841*0b57cec5SDimitry Andric /// Given a mergeable constant with the specified size and relocation
842*0b57cec5SDimitry Andric /// information, return a section that it should be placed in.
843*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
844*0b57cec5SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
8455ffd83dbSDimitry Andric     Align &Alignment) const {
846*0b57cec5SDimitry Andric   if (Kind.isMergeableConst4() && MergeableConst4Section)
847*0b57cec5SDimitry Andric     return MergeableConst4Section;
848*0b57cec5SDimitry Andric   if (Kind.isMergeableConst8() && MergeableConst8Section)
849*0b57cec5SDimitry Andric     return MergeableConst8Section;
850*0b57cec5SDimitry Andric   if (Kind.isMergeableConst16() && MergeableConst16Section)
851*0b57cec5SDimitry Andric     return MergeableConst16Section;
852*0b57cec5SDimitry Andric   if (Kind.isMergeableConst32() && MergeableConst32Section)
853*0b57cec5SDimitry Andric     return MergeableConst32Section;
854*0b57cec5SDimitry Andric   if (Kind.isReadOnly())
855*0b57cec5SDimitry Andric     return ReadOnlySection;
856*0b57cec5SDimitry Andric 
857*0b57cec5SDimitry Andric   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
858*0b57cec5SDimitry Andric   return DataRelROSection;
859*0b57cec5SDimitry Andric }
860*0b57cec5SDimitry Andric 
8615ffd83dbSDimitry Andric /// Returns a unique section for the given machine basic block.
8625ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
8635ffd83dbSDimitry Andric     const Function &F, const MachineBasicBlock &MBB,
8645ffd83dbSDimitry Andric     const TargetMachine &TM) const {
8655ffd83dbSDimitry Andric   assert(MBB.isBeginSection() && "Basic block does not start a section!");
8665ffd83dbSDimitry Andric   unsigned UniqueID = MCContext::GenericSectionID;
8675ffd83dbSDimitry Andric 
8685ffd83dbSDimitry Andric   // For cold sections use the .text.unlikely prefix along with the parent
8695ffd83dbSDimitry Andric   // function name. All cold blocks for the same function go to the same
8705ffd83dbSDimitry Andric   // section. Similarly all exception blocks are grouped by symbol name
8715ffd83dbSDimitry Andric   // under the .text.eh prefix. For regular sections, we either use a unique
8725ffd83dbSDimitry Andric   // name, or a unique ID for the section.
8735ffd83dbSDimitry Andric   SmallString<128> Name;
8745ffd83dbSDimitry Andric   if (MBB.getSectionID() == MBBSectionID::ColdSectionID) {
8755ffd83dbSDimitry Andric     Name += ".text.unlikely.";
8765ffd83dbSDimitry Andric     Name += MBB.getParent()->getName();
8775ffd83dbSDimitry Andric   } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
8785ffd83dbSDimitry Andric     Name += ".text.eh.";
8795ffd83dbSDimitry Andric     Name += MBB.getParent()->getName();
8805ffd83dbSDimitry Andric   } else {
8815ffd83dbSDimitry Andric     Name += MBB.getParent()->getSection()->getName();
8825ffd83dbSDimitry Andric     if (TM.getUniqueBasicBlockSectionNames()) {
8835ffd83dbSDimitry Andric       Name += ".";
8845ffd83dbSDimitry Andric       Name += MBB.getSymbol()->getName();
8855ffd83dbSDimitry Andric     } else {
8865ffd83dbSDimitry Andric       UniqueID = NextUniqueID++;
8875ffd83dbSDimitry Andric     }
8885ffd83dbSDimitry Andric   }
8895ffd83dbSDimitry Andric 
8905ffd83dbSDimitry Andric   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
8915ffd83dbSDimitry Andric   std::string GroupName = "";
8925ffd83dbSDimitry Andric   if (F.hasComdat()) {
8935ffd83dbSDimitry Andric     Flags |= ELF::SHF_GROUP;
8945ffd83dbSDimitry Andric     GroupName = F.getComdat()->getName().str();
8955ffd83dbSDimitry Andric   }
8965ffd83dbSDimitry Andric   return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags,
8975ffd83dbSDimitry Andric                                     0 /* Entry Size */, GroupName, UniqueID,
8985ffd83dbSDimitry Andric                                     nullptr);
8995ffd83dbSDimitry Andric }
9005ffd83dbSDimitry Andric 
901*0b57cec5SDimitry Andric static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
902*0b57cec5SDimitry Andric                                               bool IsCtor, unsigned Priority,
903*0b57cec5SDimitry Andric                                               const MCSymbol *KeySym) {
904*0b57cec5SDimitry Andric   std::string Name;
905*0b57cec5SDimitry Andric   unsigned Type;
906*0b57cec5SDimitry Andric   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
907*0b57cec5SDimitry Andric   StringRef COMDAT = KeySym ? KeySym->getName() : "";
908*0b57cec5SDimitry Andric 
909*0b57cec5SDimitry Andric   if (KeySym)
910*0b57cec5SDimitry Andric     Flags |= ELF::SHF_GROUP;
911*0b57cec5SDimitry Andric 
912*0b57cec5SDimitry Andric   if (UseInitArray) {
913*0b57cec5SDimitry Andric     if (IsCtor) {
914*0b57cec5SDimitry Andric       Type = ELF::SHT_INIT_ARRAY;
915*0b57cec5SDimitry Andric       Name = ".init_array";
916*0b57cec5SDimitry Andric     } else {
917*0b57cec5SDimitry Andric       Type = ELF::SHT_FINI_ARRAY;
918*0b57cec5SDimitry Andric       Name = ".fini_array";
919*0b57cec5SDimitry Andric     }
920*0b57cec5SDimitry Andric     if (Priority != 65535) {
921*0b57cec5SDimitry Andric       Name += '.';
922*0b57cec5SDimitry Andric       Name += utostr(Priority);
923*0b57cec5SDimitry Andric     }
924*0b57cec5SDimitry Andric   } else {
925*0b57cec5SDimitry Andric     // The default scheme is .ctor / .dtor, so we have to invert the priority
926*0b57cec5SDimitry Andric     // numbering.
927*0b57cec5SDimitry Andric     if (IsCtor)
928*0b57cec5SDimitry Andric       Name = ".ctors";
929*0b57cec5SDimitry Andric     else
930*0b57cec5SDimitry Andric       Name = ".dtors";
931*0b57cec5SDimitry Andric     if (Priority != 65535)
932*0b57cec5SDimitry Andric       raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
933*0b57cec5SDimitry Andric     Type = ELF::SHT_PROGBITS;
934*0b57cec5SDimitry Andric   }
935*0b57cec5SDimitry Andric 
936*0b57cec5SDimitry Andric   return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
937*0b57cec5SDimitry Andric }
938*0b57cec5SDimitry Andric 
939*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
940*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
941*0b57cec5SDimitry Andric   return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
942*0b57cec5SDimitry Andric                                   KeySym);
943*0b57cec5SDimitry Andric }
944*0b57cec5SDimitry Andric 
945*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
946*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
947*0b57cec5SDimitry Andric   return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
948*0b57cec5SDimitry Andric                                   KeySym);
949*0b57cec5SDimitry Andric }
950*0b57cec5SDimitry Andric 
951*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
952*0b57cec5SDimitry Andric     const GlobalValue *LHS, const GlobalValue *RHS,
953*0b57cec5SDimitry Andric     const TargetMachine &TM) const {
954*0b57cec5SDimitry Andric   // We may only use a PLT-relative relocation to refer to unnamed_addr
955*0b57cec5SDimitry Andric   // functions.
956*0b57cec5SDimitry Andric   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
957*0b57cec5SDimitry Andric     return nullptr;
958*0b57cec5SDimitry Andric 
959*0b57cec5SDimitry Andric   // Basic sanity checks.
960*0b57cec5SDimitry Andric   if (LHS->getType()->getPointerAddressSpace() != 0 ||
961*0b57cec5SDimitry Andric       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
962*0b57cec5SDimitry Andric       RHS->isThreadLocal())
963*0b57cec5SDimitry Andric     return nullptr;
964*0b57cec5SDimitry Andric 
965*0b57cec5SDimitry Andric   return MCBinaryExpr::createSub(
966*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
967*0b57cec5SDimitry Andric                               getContext()),
968*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
969*0b57cec5SDimitry Andric }
970*0b57cec5SDimitry Andric 
971*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {
972*0b57cec5SDimitry Andric   // Use ".GCC.command.line" since this feature is to support clang's
973*0b57cec5SDimitry Andric   // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
974*0b57cec5SDimitry Andric   // same name.
975*0b57cec5SDimitry Andric   return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS,
976*0b57cec5SDimitry Andric                                     ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
977*0b57cec5SDimitry Andric }
978*0b57cec5SDimitry Andric 
979*0b57cec5SDimitry Andric void
980*0b57cec5SDimitry Andric TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
981*0b57cec5SDimitry Andric   UseInitArray = UseInitArray_;
982*0b57cec5SDimitry Andric   MCContext &Ctx = getContext();
983*0b57cec5SDimitry Andric   if (!UseInitArray) {
984*0b57cec5SDimitry Andric     StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
985*0b57cec5SDimitry Andric                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
986*0b57cec5SDimitry Andric 
987*0b57cec5SDimitry Andric     StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
988*0b57cec5SDimitry Andric                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
989*0b57cec5SDimitry Andric     return;
990*0b57cec5SDimitry Andric   }
991*0b57cec5SDimitry Andric 
992*0b57cec5SDimitry Andric   StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
993*0b57cec5SDimitry Andric                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
994*0b57cec5SDimitry Andric   StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
995*0b57cec5SDimitry Andric                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
996*0b57cec5SDimitry Andric }
997*0b57cec5SDimitry Andric 
998*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
999*0b57cec5SDimitry Andric //                                 MachO
1000*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1001*0b57cec5SDimitry Andric 
1002*0b57cec5SDimitry Andric TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
1003*0b57cec5SDimitry Andric   : TargetLoweringObjectFile() {
1004*0b57cec5SDimitry Andric   SupportIndirectSymViaGOTPCRel = true;
1005*0b57cec5SDimitry Andric }
1006*0b57cec5SDimitry Andric 
1007*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
1008*0b57cec5SDimitry Andric                                                const TargetMachine &TM) {
1009*0b57cec5SDimitry Andric   TargetLoweringObjectFile::Initialize(Ctx, TM);
1010*0b57cec5SDimitry Andric   if (TM.getRelocationModel() == Reloc::Static) {
1011*0b57cec5SDimitry Andric     StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
1012*0b57cec5SDimitry Andric                                             SectionKind::getData());
1013*0b57cec5SDimitry Andric     StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
1014*0b57cec5SDimitry Andric                                             SectionKind::getData());
1015*0b57cec5SDimitry Andric   } else {
1016*0b57cec5SDimitry Andric     StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
1017*0b57cec5SDimitry Andric                                             MachO::S_MOD_INIT_FUNC_POINTERS,
1018*0b57cec5SDimitry Andric                                             SectionKind::getData());
1019*0b57cec5SDimitry Andric     StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
1020*0b57cec5SDimitry Andric                                             MachO::S_MOD_TERM_FUNC_POINTERS,
1021*0b57cec5SDimitry Andric                                             SectionKind::getData());
1022*0b57cec5SDimitry Andric   }
1023*0b57cec5SDimitry Andric 
1024*0b57cec5SDimitry Andric   PersonalityEncoding =
1025*0b57cec5SDimitry Andric       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
1026*0b57cec5SDimitry Andric   LSDAEncoding = dwarf::DW_EH_PE_pcrel;
1027*0b57cec5SDimitry Andric   TTypeEncoding =
1028*0b57cec5SDimitry Andric       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
1029*0b57cec5SDimitry Andric }
1030*0b57cec5SDimitry Andric 
1031*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
1032*0b57cec5SDimitry Andric                                                        Module &M) const {
1033*0b57cec5SDimitry Andric   // Emit the linker options if present.
1034*0b57cec5SDimitry Andric   if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1035480093f4SDimitry Andric     for (const auto *Option : LinkerOptions->operands()) {
1036*0b57cec5SDimitry Andric       SmallVector<std::string, 4> StrOptions;
1037*0b57cec5SDimitry Andric       for (const auto &Piece : cast<MDNode>(Option)->operands())
10385ffd83dbSDimitry Andric         StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
10395ffd83dbSDimitry Andric       Streamer.emitLinkerOptions(StrOptions);
1040*0b57cec5SDimitry Andric     }
1041*0b57cec5SDimitry Andric   }
1042*0b57cec5SDimitry Andric 
1043*0b57cec5SDimitry Andric   unsigned VersionVal = 0;
1044*0b57cec5SDimitry Andric   unsigned ImageInfoFlags = 0;
1045*0b57cec5SDimitry Andric   StringRef SectionVal;
1046*0b57cec5SDimitry Andric 
1047*0b57cec5SDimitry Andric   GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
1048*0b57cec5SDimitry Andric 
1049*0b57cec5SDimitry Andric   // The section is mandatory. If we don't have it, then we don't have GC info.
1050*0b57cec5SDimitry Andric   if (SectionVal.empty())
1051*0b57cec5SDimitry Andric     return;
1052*0b57cec5SDimitry Andric 
1053*0b57cec5SDimitry Andric   StringRef Segment, Section;
1054*0b57cec5SDimitry Andric   unsigned TAA = 0, StubSize = 0;
1055*0b57cec5SDimitry Andric   bool TAAParsed;
1056*0b57cec5SDimitry Andric   std::string ErrorCode =
1057*0b57cec5SDimitry Andric     MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
1058*0b57cec5SDimitry Andric                                           TAA, TAAParsed, StubSize);
1059*0b57cec5SDimitry Andric   if (!ErrorCode.empty())
1060*0b57cec5SDimitry Andric     // If invalid, report the error with report_fatal_error.
1061*0b57cec5SDimitry Andric     report_fatal_error("Invalid section specifier '" + Section + "': " +
1062*0b57cec5SDimitry Andric                        ErrorCode + ".");
1063*0b57cec5SDimitry Andric 
1064*0b57cec5SDimitry Andric   // Get the section.
1065*0b57cec5SDimitry Andric   MCSectionMachO *S = getContext().getMachOSection(
1066*0b57cec5SDimitry Andric       Segment, Section, TAA, StubSize, SectionKind::getData());
1067*0b57cec5SDimitry Andric   Streamer.SwitchSection(S);
10685ffd83dbSDimitry Andric   Streamer.emitLabel(getContext().
1069*0b57cec5SDimitry Andric                      getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
10705ffd83dbSDimitry Andric   Streamer.emitInt32(VersionVal);
10715ffd83dbSDimitry Andric   Streamer.emitInt32(ImageInfoFlags);
1072*0b57cec5SDimitry Andric   Streamer.AddBlankLine();
1073*0b57cec5SDimitry Andric }
1074*0b57cec5SDimitry Andric 
1075*0b57cec5SDimitry Andric static void checkMachOComdat(const GlobalValue *GV) {
1076*0b57cec5SDimitry Andric   const Comdat *C = GV->getComdat();
1077*0b57cec5SDimitry Andric   if (!C)
1078*0b57cec5SDimitry Andric     return;
1079*0b57cec5SDimitry Andric 
1080*0b57cec5SDimitry Andric   report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
1081*0b57cec5SDimitry Andric                      "' cannot be lowered.");
1082*0b57cec5SDimitry Andric }
1083*0b57cec5SDimitry Andric 
1084*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
1085*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1086*0b57cec5SDimitry Andric   // Parse the section specifier and create it if valid.
1087*0b57cec5SDimitry Andric   StringRef Segment, Section;
1088*0b57cec5SDimitry Andric   unsigned TAA = 0, StubSize = 0;
1089*0b57cec5SDimitry Andric   bool TAAParsed;
1090*0b57cec5SDimitry Andric 
1091*0b57cec5SDimitry Andric   checkMachOComdat(GO);
1092*0b57cec5SDimitry Andric 
1093*0b57cec5SDimitry Andric   std::string ErrorCode =
1094*0b57cec5SDimitry Andric     MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
1095*0b57cec5SDimitry Andric                                           TAA, TAAParsed, StubSize);
1096*0b57cec5SDimitry Andric   if (!ErrorCode.empty()) {
1097*0b57cec5SDimitry Andric     // If invalid, report the error with report_fatal_error.
1098*0b57cec5SDimitry Andric     report_fatal_error("Global variable '" + GO->getName() +
1099*0b57cec5SDimitry Andric                        "' has an invalid section specifier '" +
1100*0b57cec5SDimitry Andric                        GO->getSection() + "': " + ErrorCode + ".");
1101*0b57cec5SDimitry Andric   }
1102*0b57cec5SDimitry Andric 
1103*0b57cec5SDimitry Andric   // Get the section.
1104*0b57cec5SDimitry Andric   MCSectionMachO *S =
1105*0b57cec5SDimitry Andric       getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
1106*0b57cec5SDimitry Andric 
1107*0b57cec5SDimitry Andric   // If TAA wasn't set by ParseSectionSpecifier() above,
1108*0b57cec5SDimitry Andric   // use the value returned by getMachOSection() as a default.
1109*0b57cec5SDimitry Andric   if (!TAAParsed)
1110*0b57cec5SDimitry Andric     TAA = S->getTypeAndAttributes();
1111*0b57cec5SDimitry Andric 
1112*0b57cec5SDimitry Andric   // Okay, now that we got the section, verify that the TAA & StubSize agree.
1113*0b57cec5SDimitry Andric   // If the user declared multiple globals with different section flags, we need
1114*0b57cec5SDimitry Andric   // to reject it here.
1115*0b57cec5SDimitry Andric   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
1116*0b57cec5SDimitry Andric     // If invalid, report the error with report_fatal_error.
1117*0b57cec5SDimitry Andric     report_fatal_error("Global variable '" + GO->getName() +
1118*0b57cec5SDimitry Andric                        "' section type or attributes does not match previous"
1119*0b57cec5SDimitry Andric                        " section specifier");
1120*0b57cec5SDimitry Andric   }
1121*0b57cec5SDimitry Andric 
1122*0b57cec5SDimitry Andric   return S;
1123*0b57cec5SDimitry Andric }
1124*0b57cec5SDimitry Andric 
1125*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
1126*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1127*0b57cec5SDimitry Andric   checkMachOComdat(GO);
1128*0b57cec5SDimitry Andric 
1129*0b57cec5SDimitry Andric   // Handle thread local data.
1130*0b57cec5SDimitry Andric   if (Kind.isThreadBSS()) return TLSBSSSection;
1131*0b57cec5SDimitry Andric   if (Kind.isThreadData()) return TLSDataSection;
1132*0b57cec5SDimitry Andric 
1133*0b57cec5SDimitry Andric   if (Kind.isText())
1134*0b57cec5SDimitry Andric     return GO->isWeakForLinker() ? TextCoalSection : TextSection;
1135*0b57cec5SDimitry Andric 
1136*0b57cec5SDimitry Andric   // If this is weak/linkonce, put this in a coalescable section, either in text
1137*0b57cec5SDimitry Andric   // or data depending on if it is writable.
1138*0b57cec5SDimitry Andric   if (GO->isWeakForLinker()) {
1139*0b57cec5SDimitry Andric     if (Kind.isReadOnly())
1140*0b57cec5SDimitry Andric       return ConstTextCoalSection;
1141*0b57cec5SDimitry Andric     if (Kind.isReadOnlyWithRel())
1142*0b57cec5SDimitry Andric       return ConstDataCoalSection;
1143*0b57cec5SDimitry Andric     return DataCoalSection;
1144*0b57cec5SDimitry Andric   }
1145*0b57cec5SDimitry Andric 
1146*0b57cec5SDimitry Andric   // FIXME: Alignment check should be handled by section classifier.
1147*0b57cec5SDimitry Andric   if (Kind.isMergeable1ByteCString() &&
11485ffd83dbSDimitry Andric       GO->getParent()->getDataLayout().getPreferredAlign(
11495ffd83dbSDimitry Andric           cast<GlobalVariable>(GO)) < Align(32))
1150*0b57cec5SDimitry Andric     return CStringSection;
1151*0b57cec5SDimitry Andric 
1152*0b57cec5SDimitry Andric   // Do not put 16-bit arrays in the UString section if they have an
1153*0b57cec5SDimitry Andric   // externally visible label, this runs into issues with certain linker
1154*0b57cec5SDimitry Andric   // versions.
1155*0b57cec5SDimitry Andric   if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
11565ffd83dbSDimitry Andric       GO->getParent()->getDataLayout().getPreferredAlign(
11575ffd83dbSDimitry Andric           cast<GlobalVariable>(GO)) < Align(32))
1158*0b57cec5SDimitry Andric     return UStringSection;
1159*0b57cec5SDimitry Andric 
1160*0b57cec5SDimitry Andric   // With MachO only variables whose corresponding symbol starts with 'l' or
1161*0b57cec5SDimitry Andric   // 'L' can be merged, so we only try merging GVs with private linkage.
1162*0b57cec5SDimitry Andric   if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
1163*0b57cec5SDimitry Andric     if (Kind.isMergeableConst4())
1164*0b57cec5SDimitry Andric       return FourByteConstantSection;
1165*0b57cec5SDimitry Andric     if (Kind.isMergeableConst8())
1166*0b57cec5SDimitry Andric       return EightByteConstantSection;
1167*0b57cec5SDimitry Andric     if (Kind.isMergeableConst16())
1168*0b57cec5SDimitry Andric       return SixteenByteConstantSection;
1169*0b57cec5SDimitry Andric   }
1170*0b57cec5SDimitry Andric 
1171*0b57cec5SDimitry Andric   // Otherwise, if it is readonly, but not something we can specially optimize,
1172*0b57cec5SDimitry Andric   // just drop it in .const.
1173*0b57cec5SDimitry Andric   if (Kind.isReadOnly())
1174*0b57cec5SDimitry Andric     return ReadOnlySection;
1175*0b57cec5SDimitry Andric 
1176*0b57cec5SDimitry Andric   // If this is marked const, put it into a const section.  But if the dynamic
1177*0b57cec5SDimitry Andric   // linker needs to write to it, put it in the data segment.
1178*0b57cec5SDimitry Andric   if (Kind.isReadOnlyWithRel())
1179*0b57cec5SDimitry Andric     return ConstDataSection;
1180*0b57cec5SDimitry Andric 
1181*0b57cec5SDimitry Andric   // Put zero initialized globals with strong external linkage in the
1182*0b57cec5SDimitry Andric   // DATA, __common section with the .zerofill directive.
1183*0b57cec5SDimitry Andric   if (Kind.isBSSExtern())
1184*0b57cec5SDimitry Andric     return DataCommonSection;
1185*0b57cec5SDimitry Andric 
1186*0b57cec5SDimitry Andric   // Put zero initialized globals with local linkage in __DATA,__bss directive
1187*0b57cec5SDimitry Andric   // with the .zerofill directive (aka .lcomm).
1188*0b57cec5SDimitry Andric   if (Kind.isBSSLocal())
1189*0b57cec5SDimitry Andric     return DataBSSSection;
1190*0b57cec5SDimitry Andric 
1191*0b57cec5SDimitry Andric   // Otherwise, just drop the variable in the normal data section.
1192*0b57cec5SDimitry Andric   return DataSection;
1193*0b57cec5SDimitry Andric }
1194*0b57cec5SDimitry Andric 
1195*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
1196*0b57cec5SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
11975ffd83dbSDimitry Andric     Align &Alignment) const {
1198*0b57cec5SDimitry Andric   // If this constant requires a relocation, we have to put it in the data
1199*0b57cec5SDimitry Andric   // segment, not in the text segment.
1200*0b57cec5SDimitry Andric   if (Kind.isData() || Kind.isReadOnlyWithRel())
1201*0b57cec5SDimitry Andric     return ConstDataSection;
1202*0b57cec5SDimitry Andric 
1203*0b57cec5SDimitry Andric   if (Kind.isMergeableConst4())
1204*0b57cec5SDimitry Andric     return FourByteConstantSection;
1205*0b57cec5SDimitry Andric   if (Kind.isMergeableConst8())
1206*0b57cec5SDimitry Andric     return EightByteConstantSection;
1207*0b57cec5SDimitry Andric   if (Kind.isMergeableConst16())
1208*0b57cec5SDimitry Andric     return SixteenByteConstantSection;
1209*0b57cec5SDimitry Andric   return ReadOnlySection;  // .const
1210*0b57cec5SDimitry Andric }
1211*0b57cec5SDimitry Andric 
1212*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
1213*0b57cec5SDimitry Andric     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
1214*0b57cec5SDimitry Andric     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1215*0b57cec5SDimitry Andric   // The mach-o version of this method defaults to returning a stub reference.
1216*0b57cec5SDimitry Andric 
1217*0b57cec5SDimitry Andric   if (Encoding & DW_EH_PE_indirect) {
1218*0b57cec5SDimitry Andric     MachineModuleInfoMachO &MachOMMI =
1219*0b57cec5SDimitry Andric       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1220*0b57cec5SDimitry Andric 
1221*0b57cec5SDimitry Andric     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1222*0b57cec5SDimitry Andric 
1223*0b57cec5SDimitry Andric     // Add information about the stub reference to MachOMMI so that the stub
1224*0b57cec5SDimitry Andric     // gets emitted by the asmprinter.
1225*0b57cec5SDimitry Andric     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1226*0b57cec5SDimitry Andric     if (!StubSym.getPointer()) {
1227*0b57cec5SDimitry Andric       MCSymbol *Sym = TM.getSymbol(GV);
1228*0b57cec5SDimitry Andric       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1229*0b57cec5SDimitry Andric     }
1230*0b57cec5SDimitry Andric 
1231*0b57cec5SDimitry Andric     return TargetLoweringObjectFile::
1232*0b57cec5SDimitry Andric       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
1233*0b57cec5SDimitry Andric                         Encoding & ~DW_EH_PE_indirect, Streamer);
1234*0b57cec5SDimitry Andric   }
1235*0b57cec5SDimitry Andric 
1236*0b57cec5SDimitry Andric   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
1237*0b57cec5SDimitry Andric                                                            MMI, Streamer);
1238*0b57cec5SDimitry Andric }
1239*0b57cec5SDimitry Andric 
1240*0b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
1241*0b57cec5SDimitry Andric     const GlobalValue *GV, const TargetMachine &TM,
1242*0b57cec5SDimitry Andric     MachineModuleInfo *MMI) const {
1243*0b57cec5SDimitry Andric   // The mach-o version of this method defaults to returning a stub reference.
1244*0b57cec5SDimitry Andric   MachineModuleInfoMachO &MachOMMI =
1245*0b57cec5SDimitry Andric     MMI->getObjFileInfo<MachineModuleInfoMachO>();
1246*0b57cec5SDimitry Andric 
1247*0b57cec5SDimitry Andric   MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
1248*0b57cec5SDimitry Andric 
1249*0b57cec5SDimitry Andric   // Add information about the stub reference to MachOMMI so that the stub
1250*0b57cec5SDimitry Andric   // gets emitted by the asmprinter.
1251*0b57cec5SDimitry Andric   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
1252*0b57cec5SDimitry Andric   if (!StubSym.getPointer()) {
1253*0b57cec5SDimitry Andric     MCSymbol *Sym = TM.getSymbol(GV);
1254*0b57cec5SDimitry Andric     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1255*0b57cec5SDimitry Andric   }
1256*0b57cec5SDimitry Andric 
1257*0b57cec5SDimitry Andric   return SSym;
1258*0b57cec5SDimitry Andric }
1259*0b57cec5SDimitry Andric 
1260*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
12618bcb0991SDimitry Andric     const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
12628bcb0991SDimitry Andric     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
1263*0b57cec5SDimitry Andric   // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
1264*0b57cec5SDimitry Andric   // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1265*0b57cec5SDimitry Andric   // through a non_lazy_ptr stub instead. One advantage is that it allows the
1266*0b57cec5SDimitry Andric   // computation of deltas to final external symbols. Example:
1267*0b57cec5SDimitry Andric   //
1268*0b57cec5SDimitry Andric   //    _extgotequiv:
1269*0b57cec5SDimitry Andric   //       .long   _extfoo
1270*0b57cec5SDimitry Andric   //
1271*0b57cec5SDimitry Andric   //    _delta:
1272*0b57cec5SDimitry Andric   //       .long   _extgotequiv-_delta
1273*0b57cec5SDimitry Andric   //
1274*0b57cec5SDimitry Andric   // is transformed to:
1275*0b57cec5SDimitry Andric   //
1276*0b57cec5SDimitry Andric   //    _delta:
1277*0b57cec5SDimitry Andric   //       .long   L_extfoo$non_lazy_ptr-(_delta+0)
1278*0b57cec5SDimitry Andric   //
1279*0b57cec5SDimitry Andric   //       .section        __IMPORT,__pointers,non_lazy_symbol_pointers
1280*0b57cec5SDimitry Andric   //    L_extfoo$non_lazy_ptr:
1281*0b57cec5SDimitry Andric   //       .indirect_symbol        _extfoo
1282*0b57cec5SDimitry Andric   //       .long   0
1283*0b57cec5SDimitry Andric   //
1284*0b57cec5SDimitry Andric   // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1285*0b57cec5SDimitry Andric   // may point to both local (same translation unit) and global (other
1286*0b57cec5SDimitry Andric   // translation units) symbols. Example:
1287*0b57cec5SDimitry Andric   //
1288*0b57cec5SDimitry Andric   // .section __DATA,__pointers,non_lazy_symbol_pointers
1289*0b57cec5SDimitry Andric   // L1:
1290*0b57cec5SDimitry Andric   //    .indirect_symbol _myGlobal
1291*0b57cec5SDimitry Andric   //    .long 0
1292*0b57cec5SDimitry Andric   // L2:
1293*0b57cec5SDimitry Andric   //    .indirect_symbol _myLocal
1294*0b57cec5SDimitry Andric   //    .long _myLocal
1295*0b57cec5SDimitry Andric   //
1296*0b57cec5SDimitry Andric   // If the symbol is local, instead of the symbol's index, the assembler
1297*0b57cec5SDimitry Andric   // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1298*0b57cec5SDimitry Andric   // Then the linker will notice the constant in the table and will look at the
1299*0b57cec5SDimitry Andric   // content of the symbol.
1300*0b57cec5SDimitry Andric   MachineModuleInfoMachO &MachOMMI =
1301*0b57cec5SDimitry Andric     MMI->getObjFileInfo<MachineModuleInfoMachO>();
1302*0b57cec5SDimitry Andric   MCContext &Ctx = getContext();
1303*0b57cec5SDimitry Andric 
1304*0b57cec5SDimitry Andric   // The offset must consider the original displacement from the base symbol
1305*0b57cec5SDimitry Andric   // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1306*0b57cec5SDimitry Andric   Offset = -MV.getConstant();
1307*0b57cec5SDimitry Andric   const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
1308*0b57cec5SDimitry Andric 
1309*0b57cec5SDimitry Andric   // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1310*0b57cec5SDimitry Andric   // non_lazy_ptr stubs.
1311*0b57cec5SDimitry Andric   SmallString<128> Name;
1312*0b57cec5SDimitry Andric   StringRef Suffix = "$non_lazy_ptr";
1313*0b57cec5SDimitry Andric   Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
1314*0b57cec5SDimitry Andric   Name += Sym->getName();
1315*0b57cec5SDimitry Andric   Name += Suffix;
1316*0b57cec5SDimitry Andric   MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
1317*0b57cec5SDimitry Andric 
1318*0b57cec5SDimitry Andric   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
13198bcb0991SDimitry Andric 
13208bcb0991SDimitry Andric   if (!StubSym.getPointer())
1321*0b57cec5SDimitry Andric     StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
13228bcb0991SDimitry Andric                                                  !GV->hasLocalLinkage());
1323*0b57cec5SDimitry Andric 
1324*0b57cec5SDimitry Andric   const MCExpr *BSymExpr =
1325*0b57cec5SDimitry Andric     MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
1326*0b57cec5SDimitry Andric   const MCExpr *LHS =
1327*0b57cec5SDimitry Andric     MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
1328*0b57cec5SDimitry Andric 
1329*0b57cec5SDimitry Andric   if (!Offset)
1330*0b57cec5SDimitry Andric     return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
1331*0b57cec5SDimitry Andric 
1332*0b57cec5SDimitry Andric   const MCExpr *RHS =
1333*0b57cec5SDimitry Andric     MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
1334*0b57cec5SDimitry Andric   return MCBinaryExpr::createSub(LHS, RHS, Ctx);
1335*0b57cec5SDimitry Andric }
1336*0b57cec5SDimitry Andric 
1337*0b57cec5SDimitry Andric static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
1338*0b57cec5SDimitry Andric                                const MCSection &Section) {
1339*0b57cec5SDimitry Andric   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
1340*0b57cec5SDimitry Andric     return true;
1341*0b57cec5SDimitry Andric 
1342*0b57cec5SDimitry Andric   // If it is not dead stripped, it is safe to use private labels.
1343*0b57cec5SDimitry Andric   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
1344*0b57cec5SDimitry Andric   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
1345*0b57cec5SDimitry Andric     return true;
1346*0b57cec5SDimitry Andric 
1347*0b57cec5SDimitry Andric   return false;
1348*0b57cec5SDimitry Andric }
1349*0b57cec5SDimitry Andric 
1350*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::getNameWithPrefix(
1351*0b57cec5SDimitry Andric     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1352*0b57cec5SDimitry Andric     const TargetMachine &TM) const {
1353*0b57cec5SDimitry Andric   bool CannotUsePrivateLabel = true;
1354*0b57cec5SDimitry Andric   if (auto *GO = GV->getBaseObject()) {
1355*0b57cec5SDimitry Andric     SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
1356*0b57cec5SDimitry Andric     const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
1357*0b57cec5SDimitry Andric     CannotUsePrivateLabel =
1358*0b57cec5SDimitry Andric         !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
1359*0b57cec5SDimitry Andric   }
1360*0b57cec5SDimitry Andric   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1361*0b57cec5SDimitry Andric }
1362*0b57cec5SDimitry Andric 
1363*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1364*0b57cec5SDimitry Andric //                                  COFF
1365*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1366*0b57cec5SDimitry Andric 
1367*0b57cec5SDimitry Andric static unsigned
1368*0b57cec5SDimitry Andric getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
1369*0b57cec5SDimitry Andric   unsigned Flags = 0;
1370*0b57cec5SDimitry Andric   bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
1371*0b57cec5SDimitry Andric 
1372*0b57cec5SDimitry Andric   if (K.isMetadata())
1373*0b57cec5SDimitry Andric     Flags |=
1374*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_DISCARDABLE;
1375*0b57cec5SDimitry Andric   else if (K.isText())
1376*0b57cec5SDimitry Andric     Flags |=
1377*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_EXECUTE |
1378*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1379*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_CNT_CODE |
1380*0b57cec5SDimitry Andric       (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
1381*0b57cec5SDimitry Andric   else if (K.isBSS())
1382*0b57cec5SDimitry Andric     Flags |=
1383*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1384*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1385*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_WRITE;
1386*0b57cec5SDimitry Andric   else if (K.isThreadLocal())
1387*0b57cec5SDimitry Andric     Flags |=
1388*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1389*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1390*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_WRITE;
1391*0b57cec5SDimitry Andric   else if (K.isReadOnly() || K.isReadOnlyWithRel())
1392*0b57cec5SDimitry Andric     Flags |=
1393*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1394*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_READ;
1395*0b57cec5SDimitry Andric   else if (K.isWriteable())
1396*0b57cec5SDimitry Andric     Flags |=
1397*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1398*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_READ |
1399*0b57cec5SDimitry Andric       COFF::IMAGE_SCN_MEM_WRITE;
1400*0b57cec5SDimitry Andric 
1401*0b57cec5SDimitry Andric   return Flags;
1402*0b57cec5SDimitry Andric }
1403*0b57cec5SDimitry Andric 
1404*0b57cec5SDimitry Andric static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
1405*0b57cec5SDimitry Andric   const Comdat *C = GV->getComdat();
1406*0b57cec5SDimitry Andric   assert(C && "expected GV to have a Comdat!");
1407*0b57cec5SDimitry Andric 
1408*0b57cec5SDimitry Andric   StringRef ComdatGVName = C->getName();
1409*0b57cec5SDimitry Andric   const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
1410*0b57cec5SDimitry Andric   if (!ComdatGV)
1411*0b57cec5SDimitry Andric     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1412*0b57cec5SDimitry Andric                        "' does not exist.");
1413*0b57cec5SDimitry Andric 
1414*0b57cec5SDimitry Andric   if (ComdatGV->getComdat() != C)
1415*0b57cec5SDimitry Andric     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1416*0b57cec5SDimitry Andric                        "' is not a key for its COMDAT.");
1417*0b57cec5SDimitry Andric 
1418*0b57cec5SDimitry Andric   return ComdatGV;
1419*0b57cec5SDimitry Andric }
1420*0b57cec5SDimitry Andric 
1421*0b57cec5SDimitry Andric static int getSelectionForCOFF(const GlobalValue *GV) {
1422*0b57cec5SDimitry Andric   if (const Comdat *C = GV->getComdat()) {
1423*0b57cec5SDimitry Andric     const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1424*0b57cec5SDimitry Andric     if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1425*0b57cec5SDimitry Andric       ComdatKey = GA->getBaseObject();
1426*0b57cec5SDimitry Andric     if (ComdatKey == GV) {
1427*0b57cec5SDimitry Andric       switch (C->getSelectionKind()) {
1428*0b57cec5SDimitry Andric       case Comdat::Any:
1429*0b57cec5SDimitry Andric         return COFF::IMAGE_COMDAT_SELECT_ANY;
1430*0b57cec5SDimitry Andric       case Comdat::ExactMatch:
1431*0b57cec5SDimitry Andric         return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
1432*0b57cec5SDimitry Andric       case Comdat::Largest:
1433*0b57cec5SDimitry Andric         return COFF::IMAGE_COMDAT_SELECT_LARGEST;
1434*0b57cec5SDimitry Andric       case Comdat::NoDuplicates:
1435*0b57cec5SDimitry Andric         return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1436*0b57cec5SDimitry Andric       case Comdat::SameSize:
1437*0b57cec5SDimitry Andric         return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
1438*0b57cec5SDimitry Andric       }
1439*0b57cec5SDimitry Andric     } else {
1440*0b57cec5SDimitry Andric       return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1441*0b57cec5SDimitry Andric     }
1442*0b57cec5SDimitry Andric   }
1443*0b57cec5SDimitry Andric   return 0;
1444*0b57cec5SDimitry Andric }
1445*0b57cec5SDimitry Andric 
1446*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
1447*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1448*0b57cec5SDimitry Andric   int Selection = 0;
1449*0b57cec5SDimitry Andric   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1450*0b57cec5SDimitry Andric   StringRef Name = GO->getSection();
1451*0b57cec5SDimitry Andric   StringRef COMDATSymName = "";
1452*0b57cec5SDimitry Andric   if (GO->hasComdat()) {
1453*0b57cec5SDimitry Andric     Selection = getSelectionForCOFF(GO);
1454*0b57cec5SDimitry Andric     const GlobalValue *ComdatGV;
1455*0b57cec5SDimitry Andric     if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1456*0b57cec5SDimitry Andric       ComdatGV = getComdatGVForCOFF(GO);
1457*0b57cec5SDimitry Andric     else
1458*0b57cec5SDimitry Andric       ComdatGV = GO;
1459*0b57cec5SDimitry Andric 
1460*0b57cec5SDimitry Andric     if (!ComdatGV->hasPrivateLinkage()) {
1461*0b57cec5SDimitry Andric       MCSymbol *Sym = TM.getSymbol(ComdatGV);
1462*0b57cec5SDimitry Andric       COMDATSymName = Sym->getName();
1463*0b57cec5SDimitry Andric       Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1464*0b57cec5SDimitry Andric     } else {
1465*0b57cec5SDimitry Andric       Selection = 0;
1466*0b57cec5SDimitry Andric     }
1467*0b57cec5SDimitry Andric   }
1468*0b57cec5SDimitry Andric 
1469*0b57cec5SDimitry Andric   return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1470*0b57cec5SDimitry Andric                                      Selection);
1471*0b57cec5SDimitry Andric }
1472*0b57cec5SDimitry Andric 
1473*0b57cec5SDimitry Andric static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
1474*0b57cec5SDimitry Andric   if (Kind.isText())
1475*0b57cec5SDimitry Andric     return ".text";
1476*0b57cec5SDimitry Andric   if (Kind.isBSS())
1477*0b57cec5SDimitry Andric     return ".bss";
1478*0b57cec5SDimitry Andric   if (Kind.isThreadLocal())
1479*0b57cec5SDimitry Andric     return ".tls$";
1480*0b57cec5SDimitry Andric   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1481*0b57cec5SDimitry Andric     return ".rdata";
1482*0b57cec5SDimitry Andric   return ".data";
1483*0b57cec5SDimitry Andric }
1484*0b57cec5SDimitry Andric 
1485*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
1486*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1487*0b57cec5SDimitry Andric   // If we have -ffunction-sections then we should emit the global value to a
1488*0b57cec5SDimitry Andric   // uniqued section specifically for it.
1489*0b57cec5SDimitry Andric   bool EmitUniquedSection;
1490*0b57cec5SDimitry Andric   if (Kind.isText())
1491*0b57cec5SDimitry Andric     EmitUniquedSection = TM.getFunctionSections();
1492*0b57cec5SDimitry Andric   else
1493*0b57cec5SDimitry Andric     EmitUniquedSection = TM.getDataSections();
1494*0b57cec5SDimitry Andric 
1495*0b57cec5SDimitry Andric   if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
1496*0b57cec5SDimitry Andric     SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
1497*0b57cec5SDimitry Andric 
1498*0b57cec5SDimitry Andric     unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1499*0b57cec5SDimitry Andric 
1500*0b57cec5SDimitry Andric     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1501*0b57cec5SDimitry Andric     int Selection = getSelectionForCOFF(GO);
1502*0b57cec5SDimitry Andric     if (!Selection)
1503*0b57cec5SDimitry Andric       Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1504*0b57cec5SDimitry Andric     const GlobalValue *ComdatGV;
1505*0b57cec5SDimitry Andric     if (GO->hasComdat())
1506*0b57cec5SDimitry Andric       ComdatGV = getComdatGVForCOFF(GO);
1507*0b57cec5SDimitry Andric     else
1508*0b57cec5SDimitry Andric       ComdatGV = GO;
1509*0b57cec5SDimitry Andric 
1510*0b57cec5SDimitry Andric     unsigned UniqueID = MCContext::GenericSectionID;
1511*0b57cec5SDimitry Andric     if (EmitUniquedSection)
1512*0b57cec5SDimitry Andric       UniqueID = NextUniqueID++;
1513*0b57cec5SDimitry Andric 
1514*0b57cec5SDimitry Andric     if (!ComdatGV->hasPrivateLinkage()) {
1515*0b57cec5SDimitry Andric       MCSymbol *Sym = TM.getSymbol(ComdatGV);
1516*0b57cec5SDimitry Andric       StringRef COMDATSymName = Sym->getName();
1517*0b57cec5SDimitry Andric 
1518*0b57cec5SDimitry Andric       // Append "$symbol" to the section name *before* IR-level mangling is
1519*0b57cec5SDimitry Andric       // applied when targetting mingw. This is what GCC does, and the ld.bfd
1520*0b57cec5SDimitry Andric       // COFF linker will not properly handle comdats otherwise.
1521*0b57cec5SDimitry Andric       if (getTargetTriple().isWindowsGNUEnvironment())
1522*0b57cec5SDimitry Andric         raw_svector_ostream(Name) << '$' << ComdatGV->getName();
1523*0b57cec5SDimitry Andric 
1524*0b57cec5SDimitry Andric       return getContext().getCOFFSection(Name, Characteristics, Kind,
1525*0b57cec5SDimitry Andric                                          COMDATSymName, Selection, UniqueID);
1526*0b57cec5SDimitry Andric     } else {
1527*0b57cec5SDimitry Andric       SmallString<256> TmpData;
1528*0b57cec5SDimitry Andric       getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
1529*0b57cec5SDimitry Andric       return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
1530*0b57cec5SDimitry Andric                                          Selection, UniqueID);
1531*0b57cec5SDimitry Andric     }
1532*0b57cec5SDimitry Andric   }
1533*0b57cec5SDimitry Andric 
1534*0b57cec5SDimitry Andric   if (Kind.isText())
1535*0b57cec5SDimitry Andric     return TextSection;
1536*0b57cec5SDimitry Andric 
1537*0b57cec5SDimitry Andric   if (Kind.isThreadLocal())
1538*0b57cec5SDimitry Andric     return TLSDataSection;
1539*0b57cec5SDimitry Andric 
1540*0b57cec5SDimitry Andric   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1541*0b57cec5SDimitry Andric     return ReadOnlySection;
1542*0b57cec5SDimitry Andric 
1543*0b57cec5SDimitry Andric   // Note: we claim that common symbols are put in BSSSection, but they are
1544*0b57cec5SDimitry Andric   // really emitted with the magic .comm directive, which creates a symbol table
1545*0b57cec5SDimitry Andric   // entry but not a section.
1546*0b57cec5SDimitry Andric   if (Kind.isBSS() || Kind.isCommon())
1547*0b57cec5SDimitry Andric     return BSSSection;
1548*0b57cec5SDimitry Andric 
1549*0b57cec5SDimitry Andric   return DataSection;
1550*0b57cec5SDimitry Andric }
1551*0b57cec5SDimitry Andric 
1552*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::getNameWithPrefix(
1553*0b57cec5SDimitry Andric     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
1554*0b57cec5SDimitry Andric     const TargetMachine &TM) const {
1555*0b57cec5SDimitry Andric   bool CannotUsePrivateLabel = false;
1556*0b57cec5SDimitry Andric   if (GV->hasPrivateLinkage() &&
1557*0b57cec5SDimitry Andric       ((isa<Function>(GV) && TM.getFunctionSections()) ||
1558*0b57cec5SDimitry Andric        (isa<GlobalVariable>(GV) && TM.getDataSections())))
1559*0b57cec5SDimitry Andric     CannotUsePrivateLabel = true;
1560*0b57cec5SDimitry Andric 
1561*0b57cec5SDimitry Andric   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1562*0b57cec5SDimitry Andric }
1563*0b57cec5SDimitry Andric 
1564*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
1565*0b57cec5SDimitry Andric     const Function &F, const TargetMachine &TM) const {
1566*0b57cec5SDimitry Andric   // If the function can be removed, produce a unique section so that
1567*0b57cec5SDimitry Andric   // the table doesn't prevent the removal.
1568*0b57cec5SDimitry Andric   const Comdat *C = F.getComdat();
1569*0b57cec5SDimitry Andric   bool EmitUniqueSection = TM.getFunctionSections() || C;
1570*0b57cec5SDimitry Andric   if (!EmitUniqueSection)
1571*0b57cec5SDimitry Andric     return ReadOnlySection;
1572*0b57cec5SDimitry Andric 
1573*0b57cec5SDimitry Andric   // FIXME: we should produce a symbol for F instead.
1574*0b57cec5SDimitry Andric   if (F.hasPrivateLinkage())
1575*0b57cec5SDimitry Andric     return ReadOnlySection;
1576*0b57cec5SDimitry Andric 
1577*0b57cec5SDimitry Andric   MCSymbol *Sym = TM.getSymbol(&F);
1578*0b57cec5SDimitry Andric   StringRef COMDATSymName = Sym->getName();
1579*0b57cec5SDimitry Andric 
1580*0b57cec5SDimitry Andric   SectionKind Kind = SectionKind::getReadOnly();
1581*0b57cec5SDimitry Andric   StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
1582*0b57cec5SDimitry Andric   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
1583*0b57cec5SDimitry Andric   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1584*0b57cec5SDimitry Andric   unsigned UniqueID = NextUniqueID++;
1585*0b57cec5SDimitry Andric 
1586*0b57cec5SDimitry Andric   return getContext().getCOFFSection(
1587*0b57cec5SDimitry Andric       SecName, Characteristics, Kind, COMDATSymName,
1588*0b57cec5SDimitry Andric       COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
1589*0b57cec5SDimitry Andric }
1590*0b57cec5SDimitry Andric 
1591*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
1592*0b57cec5SDimitry Andric                                                       Module &M) const {
1593*0b57cec5SDimitry Andric   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1594*0b57cec5SDimitry Andric     // Emit the linker options to the linker .drectve section.  According to the
1595*0b57cec5SDimitry Andric     // spec, this section is a space-separated string containing flags for
1596*0b57cec5SDimitry Andric     // linker.
1597*0b57cec5SDimitry Andric     MCSection *Sec = getDrectveSection();
1598*0b57cec5SDimitry Andric     Streamer.SwitchSection(Sec);
1599480093f4SDimitry Andric     for (const auto *Option : LinkerOptions->operands()) {
1600*0b57cec5SDimitry Andric       for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1601*0b57cec5SDimitry Andric         // Lead with a space for consistency with our dllexport implementation.
1602*0b57cec5SDimitry Andric         std::string Directive(" ");
16035ffd83dbSDimitry Andric         Directive.append(std::string(cast<MDString>(Piece)->getString()));
16045ffd83dbSDimitry Andric         Streamer.emitBytes(Directive);
1605*0b57cec5SDimitry Andric       }
1606*0b57cec5SDimitry Andric     }
1607*0b57cec5SDimitry Andric   }
1608*0b57cec5SDimitry Andric 
1609*0b57cec5SDimitry Andric   unsigned Version = 0;
1610*0b57cec5SDimitry Andric   unsigned Flags = 0;
1611*0b57cec5SDimitry Andric   StringRef Section;
1612*0b57cec5SDimitry Andric 
1613*0b57cec5SDimitry Andric   GetObjCImageInfo(M, Version, Flags, Section);
1614*0b57cec5SDimitry Andric   if (Section.empty())
1615*0b57cec5SDimitry Andric     return;
1616*0b57cec5SDimitry Andric 
1617*0b57cec5SDimitry Andric   auto &C = getContext();
1618*0b57cec5SDimitry Andric   auto *S = C.getCOFFSection(
1619*0b57cec5SDimitry Andric       Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1620*0b57cec5SDimitry Andric       SectionKind::getReadOnly());
1621*0b57cec5SDimitry Andric   Streamer.SwitchSection(S);
16225ffd83dbSDimitry Andric   Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
16235ffd83dbSDimitry Andric   Streamer.emitInt32(Version);
16245ffd83dbSDimitry Andric   Streamer.emitInt32(Flags);
1625*0b57cec5SDimitry Andric   Streamer.AddBlankLine();
1626*0b57cec5SDimitry Andric }
1627*0b57cec5SDimitry Andric 
1628*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1629*0b57cec5SDimitry Andric                                               const TargetMachine &TM) {
1630*0b57cec5SDimitry Andric   TargetLoweringObjectFile::Initialize(Ctx, TM);
1631*0b57cec5SDimitry Andric   const Triple &T = TM.getTargetTriple();
1632*0b57cec5SDimitry Andric   if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1633*0b57cec5SDimitry Andric     StaticCtorSection =
1634*0b57cec5SDimitry Andric         Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1635*0b57cec5SDimitry Andric                                            COFF::IMAGE_SCN_MEM_READ,
1636*0b57cec5SDimitry Andric                            SectionKind::getReadOnly());
1637*0b57cec5SDimitry Andric     StaticDtorSection =
1638*0b57cec5SDimitry Andric         Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1639*0b57cec5SDimitry Andric                                            COFF::IMAGE_SCN_MEM_READ,
1640*0b57cec5SDimitry Andric                            SectionKind::getReadOnly());
1641*0b57cec5SDimitry Andric   } else {
1642*0b57cec5SDimitry Andric     StaticCtorSection = Ctx.getCOFFSection(
1643*0b57cec5SDimitry Andric         ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1644*0b57cec5SDimitry Andric                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1645*0b57cec5SDimitry Andric         SectionKind::getData());
1646*0b57cec5SDimitry Andric     StaticDtorSection = Ctx.getCOFFSection(
1647*0b57cec5SDimitry Andric         ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1648*0b57cec5SDimitry Andric                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1649*0b57cec5SDimitry Andric         SectionKind::getData());
1650*0b57cec5SDimitry Andric   }
1651*0b57cec5SDimitry Andric }
1652*0b57cec5SDimitry Andric 
1653*0b57cec5SDimitry Andric static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
1654*0b57cec5SDimitry Andric                                                    const Triple &T, bool IsCtor,
1655*0b57cec5SDimitry Andric                                                    unsigned Priority,
1656*0b57cec5SDimitry Andric                                                    const MCSymbol *KeySym,
1657*0b57cec5SDimitry Andric                                                    MCSectionCOFF *Default) {
1658*0b57cec5SDimitry Andric   if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1659*0b57cec5SDimitry Andric     // If the priority is the default, use .CRT$XCU, possibly associative.
1660*0b57cec5SDimitry Andric     if (Priority == 65535)
1661*0b57cec5SDimitry Andric       return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1662*0b57cec5SDimitry Andric 
1663*0b57cec5SDimitry Andric     // Otherwise, we need to compute a new section name. Low priorities should
1664*0b57cec5SDimitry Andric     // run earlier. The linker will sort sections ASCII-betically, and we need a
1665*0b57cec5SDimitry Andric     // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1666*0b57cec5SDimitry Andric     // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1667*0b57cec5SDimitry Andric     // low priorities need to sort before 'L', since the CRT uses that
1668*0b57cec5SDimitry Andric     // internally, so we use ".CRT$XCA00001" for them.
1669*0b57cec5SDimitry Andric     SmallString<24> Name;
1670*0b57cec5SDimitry Andric     raw_svector_ostream OS(Name);
16718bcb0991SDimitry Andric     OS << ".CRT$X" << (IsCtor ? "C" : "T") <<
16728bcb0991SDimitry Andric         (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
1673*0b57cec5SDimitry Andric     MCSectionCOFF *Sec = Ctx.getCOFFSection(
1674*0b57cec5SDimitry Andric         Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1675*0b57cec5SDimitry Andric         SectionKind::getReadOnly());
1676*0b57cec5SDimitry Andric     return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
1677*0b57cec5SDimitry Andric   }
1678*0b57cec5SDimitry Andric 
1679*0b57cec5SDimitry Andric   std::string Name = IsCtor ? ".ctors" : ".dtors";
1680*0b57cec5SDimitry Andric   if (Priority != 65535)
1681*0b57cec5SDimitry Andric     raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1682*0b57cec5SDimitry Andric 
1683*0b57cec5SDimitry Andric   return Ctx.getAssociativeCOFFSection(
1684*0b57cec5SDimitry Andric       Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1685*0b57cec5SDimitry Andric                                    COFF::IMAGE_SCN_MEM_READ |
1686*0b57cec5SDimitry Andric                                    COFF::IMAGE_SCN_MEM_WRITE,
1687*0b57cec5SDimitry Andric                          SectionKind::getData()),
1688*0b57cec5SDimitry Andric       KeySym, 0);
1689*0b57cec5SDimitry Andric }
1690*0b57cec5SDimitry Andric 
1691*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
1692*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
1693*0b57cec5SDimitry Andric   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1694*0b57cec5SDimitry Andric                                       Priority, KeySym,
1695*0b57cec5SDimitry Andric                                       cast<MCSectionCOFF>(StaticCtorSection));
1696*0b57cec5SDimitry Andric }
1697*0b57cec5SDimitry Andric 
1698*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
1699*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
1700*0b57cec5SDimitry Andric   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1701*0b57cec5SDimitry Andric                                       Priority, KeySym,
1702*0b57cec5SDimitry Andric                                       cast<MCSectionCOFF>(StaticDtorSection));
1703*0b57cec5SDimitry Andric }
1704*0b57cec5SDimitry Andric 
1705*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1706*0b57cec5SDimitry Andric     raw_ostream &OS, const GlobalValue *GV) const {
1707*0b57cec5SDimitry Andric   emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
1708*0b57cec5SDimitry Andric }
1709*0b57cec5SDimitry Andric 
1710*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1711*0b57cec5SDimitry Andric     raw_ostream &OS, const GlobalValue *GV) const {
1712*0b57cec5SDimitry Andric   emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
1713*0b57cec5SDimitry Andric }
1714*0b57cec5SDimitry Andric 
1715*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
1716*0b57cec5SDimitry Andric     const GlobalValue *LHS, const GlobalValue *RHS,
1717*0b57cec5SDimitry Andric     const TargetMachine &TM) const {
1718*0b57cec5SDimitry Andric   const Triple &T = TM.getTargetTriple();
1719*0b57cec5SDimitry Andric   if (T.isOSCygMing())
1720*0b57cec5SDimitry Andric     return nullptr;
1721*0b57cec5SDimitry Andric 
1722*0b57cec5SDimitry Andric   // Our symbols should exist in address space zero, cowardly no-op if
1723*0b57cec5SDimitry Andric   // otherwise.
1724*0b57cec5SDimitry Andric   if (LHS->getType()->getPointerAddressSpace() != 0 ||
1725*0b57cec5SDimitry Andric       RHS->getType()->getPointerAddressSpace() != 0)
1726*0b57cec5SDimitry Andric     return nullptr;
1727*0b57cec5SDimitry Andric 
1728*0b57cec5SDimitry Andric   // Both ptrtoint instructions must wrap global objects:
1729*0b57cec5SDimitry Andric   // - Only global variables are eligible for image relative relocations.
1730*0b57cec5SDimitry Andric   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1731*0b57cec5SDimitry Andric   // We expect __ImageBase to be a global variable without a section, externally
1732*0b57cec5SDimitry Andric   // defined.
1733*0b57cec5SDimitry Andric   //
1734*0b57cec5SDimitry Andric   // It should look something like this: @__ImageBase = external constant i8
1735*0b57cec5SDimitry Andric   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
1736*0b57cec5SDimitry Andric       LHS->isThreadLocal() || RHS->isThreadLocal() ||
1737*0b57cec5SDimitry Andric       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
1738*0b57cec5SDimitry Andric       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
1739*0b57cec5SDimitry Andric     return nullptr;
1740*0b57cec5SDimitry Andric 
1741*0b57cec5SDimitry Andric   return MCSymbolRefExpr::create(TM.getSymbol(LHS),
1742*0b57cec5SDimitry Andric                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
1743*0b57cec5SDimitry Andric                                  getContext());
1744*0b57cec5SDimitry Andric }
1745*0b57cec5SDimitry Andric 
1746*0b57cec5SDimitry Andric static std::string APIntToHexString(const APInt &AI) {
1747*0b57cec5SDimitry Andric   unsigned Width = (AI.getBitWidth() / 8) * 2;
17488bcb0991SDimitry Andric   std::string HexString = AI.toString(16, /*Signed=*/false);
17495ffd83dbSDimitry Andric   llvm::transform(HexString, HexString.begin(), tolower);
1750*0b57cec5SDimitry Andric   unsigned Size = HexString.size();
1751*0b57cec5SDimitry Andric   assert(Width >= Size && "hex string is too large!");
1752*0b57cec5SDimitry Andric   HexString.insert(HexString.begin(), Width - Size, '0');
1753*0b57cec5SDimitry Andric 
1754*0b57cec5SDimitry Andric   return HexString;
1755*0b57cec5SDimitry Andric }
1756*0b57cec5SDimitry Andric 
1757*0b57cec5SDimitry Andric static std::string scalarConstantToHexString(const Constant *C) {
1758*0b57cec5SDimitry Andric   Type *Ty = C->getType();
1759*0b57cec5SDimitry Andric   if (isa<UndefValue>(C)) {
1760*0b57cec5SDimitry Andric     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
1761*0b57cec5SDimitry Andric   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
1762*0b57cec5SDimitry Andric     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
1763*0b57cec5SDimitry Andric   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
1764*0b57cec5SDimitry Andric     return APIntToHexString(CI->getValue());
1765*0b57cec5SDimitry Andric   } else {
1766*0b57cec5SDimitry Andric     unsigned NumElements;
17675ffd83dbSDimitry Andric     if (auto *VTy = dyn_cast<VectorType>(Ty))
17685ffd83dbSDimitry Andric       NumElements = cast<FixedVectorType>(VTy)->getNumElements();
1769*0b57cec5SDimitry Andric     else
1770*0b57cec5SDimitry Andric       NumElements = Ty->getArrayNumElements();
1771*0b57cec5SDimitry Andric     std::string HexString;
1772*0b57cec5SDimitry Andric     for (int I = NumElements - 1, E = -1; I != E; --I)
1773*0b57cec5SDimitry Andric       HexString += scalarConstantToHexString(C->getAggregateElement(I));
1774*0b57cec5SDimitry Andric     return HexString;
1775*0b57cec5SDimitry Andric   }
1776*0b57cec5SDimitry Andric }
1777*0b57cec5SDimitry Andric 
1778*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
1779*0b57cec5SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
17805ffd83dbSDimitry Andric     Align &Alignment) const {
1781*0b57cec5SDimitry Andric   if (Kind.isMergeableConst() && C &&
1782*0b57cec5SDimitry Andric       getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1783*0b57cec5SDimitry Andric     // This creates comdat sections with the given symbol name, but unless
1784*0b57cec5SDimitry Andric     // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1785*0b57cec5SDimitry Andric     // will be created with a null storage class, which makes GNU binutils
1786*0b57cec5SDimitry Andric     // error out.
1787*0b57cec5SDimitry Andric     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1788*0b57cec5SDimitry Andric                                      COFF::IMAGE_SCN_MEM_READ |
1789*0b57cec5SDimitry Andric                                      COFF::IMAGE_SCN_LNK_COMDAT;
1790*0b57cec5SDimitry Andric     std::string COMDATSymName;
1791*0b57cec5SDimitry Andric     if (Kind.isMergeableConst4()) {
17925ffd83dbSDimitry Andric       if (Alignment <= 4) {
1793*0b57cec5SDimitry Andric         COMDATSymName = "__real@" + scalarConstantToHexString(C);
17945ffd83dbSDimitry Andric         Alignment = Align(4);
1795*0b57cec5SDimitry Andric       }
1796*0b57cec5SDimitry Andric     } else if (Kind.isMergeableConst8()) {
17975ffd83dbSDimitry Andric       if (Alignment <= 8) {
1798*0b57cec5SDimitry Andric         COMDATSymName = "__real@" + scalarConstantToHexString(C);
17995ffd83dbSDimitry Andric         Alignment = Align(8);
1800*0b57cec5SDimitry Andric       }
1801*0b57cec5SDimitry Andric     } else if (Kind.isMergeableConst16()) {
1802*0b57cec5SDimitry Andric       // FIXME: These may not be appropriate for non-x86 architectures.
18035ffd83dbSDimitry Andric       if (Alignment <= 16) {
1804*0b57cec5SDimitry Andric         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
18055ffd83dbSDimitry Andric         Alignment = Align(16);
1806*0b57cec5SDimitry Andric       }
1807*0b57cec5SDimitry Andric     } else if (Kind.isMergeableConst32()) {
18085ffd83dbSDimitry Andric       if (Alignment <= 32) {
1809*0b57cec5SDimitry Andric         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
18105ffd83dbSDimitry Andric         Alignment = Align(32);
1811*0b57cec5SDimitry Andric       }
1812*0b57cec5SDimitry Andric     }
1813*0b57cec5SDimitry Andric 
1814*0b57cec5SDimitry Andric     if (!COMDATSymName.empty())
1815*0b57cec5SDimitry Andric       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
1816*0b57cec5SDimitry Andric                                          COMDATSymName,
1817*0b57cec5SDimitry Andric                                          COFF::IMAGE_COMDAT_SELECT_ANY);
1818*0b57cec5SDimitry Andric   }
1819*0b57cec5SDimitry Andric 
18205ffd83dbSDimitry Andric   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C,
18215ffd83dbSDimitry Andric                                                          Alignment);
1822*0b57cec5SDimitry Andric }
1823*0b57cec5SDimitry Andric 
1824*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1825*0b57cec5SDimitry Andric //                                  Wasm
1826*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1827*0b57cec5SDimitry Andric 
1828*0b57cec5SDimitry Andric static const Comdat *getWasmComdat(const GlobalValue *GV) {
1829*0b57cec5SDimitry Andric   const Comdat *C = GV->getComdat();
1830*0b57cec5SDimitry Andric   if (!C)
1831*0b57cec5SDimitry Andric     return nullptr;
1832*0b57cec5SDimitry Andric 
1833*0b57cec5SDimitry Andric   if (C->getSelectionKind() != Comdat::Any)
1834*0b57cec5SDimitry Andric     report_fatal_error("WebAssembly COMDATs only support "
1835*0b57cec5SDimitry Andric                        "SelectionKind::Any, '" + C->getName() + "' cannot be "
1836*0b57cec5SDimitry Andric                        "lowered.");
1837*0b57cec5SDimitry Andric 
1838*0b57cec5SDimitry Andric   return C;
1839*0b57cec5SDimitry Andric }
1840*0b57cec5SDimitry Andric 
1841*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1842*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1843*0b57cec5SDimitry Andric   // We don't support explict section names for functions in the wasm object
1844*0b57cec5SDimitry Andric   // format.  Each function has to be in its own unique section.
1845*0b57cec5SDimitry Andric   if (isa<Function>(GO)) {
1846*0b57cec5SDimitry Andric     return SelectSectionForGlobal(GO, Kind, TM);
1847*0b57cec5SDimitry Andric   }
1848*0b57cec5SDimitry Andric 
1849*0b57cec5SDimitry Andric   StringRef Name = GO->getSection();
1850*0b57cec5SDimitry Andric 
18515ffd83dbSDimitry Andric   // Certain data sections we treat as named custom sections rather than
18525ffd83dbSDimitry Andric   // segments within the data section.
18535ffd83dbSDimitry Andric   // This could be avoided if all data segements (the wasm sense) were
18545ffd83dbSDimitry Andric   // represented as their own sections (in the llvm sense).
18555ffd83dbSDimitry Andric   // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
18565ffd83dbSDimitry Andric   if (Name == ".llvmcmd" || Name == ".llvmbc")
18575ffd83dbSDimitry Andric     Kind = SectionKind::getMetadata();
1858*0b57cec5SDimitry Andric 
1859*0b57cec5SDimitry Andric   StringRef Group = "";
1860*0b57cec5SDimitry Andric   if (const Comdat *C = getWasmComdat(GO)) {
1861*0b57cec5SDimitry Andric     Group = C->getName();
1862*0b57cec5SDimitry Andric   }
1863*0b57cec5SDimitry Andric 
1864*0b57cec5SDimitry Andric   MCSectionWasm* Section =
1865*0b57cec5SDimitry Andric       getContext().getWasmSection(Name, Kind, Group,
1866*0b57cec5SDimitry Andric                                   MCContext::GenericSectionID);
1867*0b57cec5SDimitry Andric 
1868*0b57cec5SDimitry Andric   return Section;
1869*0b57cec5SDimitry Andric }
1870*0b57cec5SDimitry Andric 
1871*0b57cec5SDimitry Andric static MCSectionWasm *selectWasmSectionForGlobal(
1872*0b57cec5SDimitry Andric     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
1873*0b57cec5SDimitry Andric     const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
1874*0b57cec5SDimitry Andric   StringRef Group = "";
1875*0b57cec5SDimitry Andric   if (const Comdat *C = getWasmComdat(GO)) {
1876*0b57cec5SDimitry Andric     Group = C->getName();
1877*0b57cec5SDimitry Andric   }
1878*0b57cec5SDimitry Andric 
1879*0b57cec5SDimitry Andric   bool UniqueSectionNames = TM.getUniqueSectionNames();
1880*0b57cec5SDimitry Andric   SmallString<128> Name = getSectionPrefixForGlobal(Kind);
1881*0b57cec5SDimitry Andric 
1882*0b57cec5SDimitry Andric   if (const auto *F = dyn_cast<Function>(GO)) {
1883*0b57cec5SDimitry Andric     const auto &OptionalPrefix = F->getSectionPrefix();
1884*0b57cec5SDimitry Andric     if (OptionalPrefix)
1885*0b57cec5SDimitry Andric       Name += *OptionalPrefix;
1886*0b57cec5SDimitry Andric   }
1887*0b57cec5SDimitry Andric 
1888*0b57cec5SDimitry Andric   if (EmitUniqueSection && UniqueSectionNames) {
1889*0b57cec5SDimitry Andric     Name.push_back('.');
1890*0b57cec5SDimitry Andric     TM.getNameWithPrefix(Name, GO, Mang, true);
1891*0b57cec5SDimitry Andric   }
1892*0b57cec5SDimitry Andric   unsigned UniqueID = MCContext::GenericSectionID;
1893*0b57cec5SDimitry Andric   if (EmitUniqueSection && !UniqueSectionNames) {
1894*0b57cec5SDimitry Andric     UniqueID = *NextUniqueID;
1895*0b57cec5SDimitry Andric     (*NextUniqueID)++;
1896*0b57cec5SDimitry Andric   }
1897*0b57cec5SDimitry Andric 
1898*0b57cec5SDimitry Andric   return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
1899*0b57cec5SDimitry Andric }
1900*0b57cec5SDimitry Andric 
1901*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1902*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1903*0b57cec5SDimitry Andric 
1904*0b57cec5SDimitry Andric   if (Kind.isCommon())
1905*0b57cec5SDimitry Andric     report_fatal_error("mergable sections not supported yet on wasm");
1906*0b57cec5SDimitry Andric 
1907*0b57cec5SDimitry Andric   // If we have -ffunction-section or -fdata-section then we should emit the
1908*0b57cec5SDimitry Andric   // global value to a uniqued section specifically for it.
1909*0b57cec5SDimitry Andric   bool EmitUniqueSection = false;
1910*0b57cec5SDimitry Andric   if (Kind.isText())
1911*0b57cec5SDimitry Andric     EmitUniqueSection = TM.getFunctionSections();
1912*0b57cec5SDimitry Andric   else
1913*0b57cec5SDimitry Andric     EmitUniqueSection = TM.getDataSections();
1914*0b57cec5SDimitry Andric   EmitUniqueSection |= GO->hasComdat();
1915*0b57cec5SDimitry Andric 
1916*0b57cec5SDimitry Andric   return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
1917*0b57cec5SDimitry Andric                                     EmitUniqueSection, &NextUniqueID);
1918*0b57cec5SDimitry Andric }
1919*0b57cec5SDimitry Andric 
1920*0b57cec5SDimitry Andric bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1921*0b57cec5SDimitry Andric     bool UsesLabelDifference, const Function &F) const {
1922*0b57cec5SDimitry Andric   // We can always create relative relocations, so use another section
1923*0b57cec5SDimitry Andric   // that can be marked non-executable.
1924*0b57cec5SDimitry Andric   return false;
1925*0b57cec5SDimitry Andric }
1926*0b57cec5SDimitry Andric 
1927*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
1928*0b57cec5SDimitry Andric     const GlobalValue *LHS, const GlobalValue *RHS,
1929*0b57cec5SDimitry Andric     const TargetMachine &TM) const {
1930*0b57cec5SDimitry Andric   // We may only use a PLT-relative relocation to refer to unnamed_addr
1931*0b57cec5SDimitry Andric   // functions.
1932*0b57cec5SDimitry Andric   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1933*0b57cec5SDimitry Andric     return nullptr;
1934*0b57cec5SDimitry Andric 
1935*0b57cec5SDimitry Andric   // Basic sanity checks.
1936*0b57cec5SDimitry Andric   if (LHS->getType()->getPointerAddressSpace() != 0 ||
1937*0b57cec5SDimitry Andric       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1938*0b57cec5SDimitry Andric       RHS->isThreadLocal())
1939*0b57cec5SDimitry Andric     return nullptr;
1940*0b57cec5SDimitry Andric 
1941*0b57cec5SDimitry Andric   return MCBinaryExpr::createSub(
1942*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
1943*0b57cec5SDimitry Andric                               getContext()),
1944*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
1945*0b57cec5SDimitry Andric }
1946*0b57cec5SDimitry Andric 
1947*0b57cec5SDimitry Andric void TargetLoweringObjectFileWasm::InitializeWasm() {
1948*0b57cec5SDimitry Andric   StaticCtorSection =
1949*0b57cec5SDimitry Andric       getContext().getWasmSection(".init_array", SectionKind::getData());
1950*0b57cec5SDimitry Andric 
1951*0b57cec5SDimitry Andric   // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
1952*0b57cec5SDimitry Andric   // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
1953*0b57cec5SDimitry Andric   TTypeEncoding = dwarf::DW_EH_PE_absptr;
1954*0b57cec5SDimitry Andric }
1955*0b57cec5SDimitry Andric 
1956*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
1957*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
1958*0b57cec5SDimitry Andric   return Priority == UINT16_MAX ?
1959*0b57cec5SDimitry Andric          StaticCtorSection :
1960*0b57cec5SDimitry Andric          getContext().getWasmSection(".init_array." + utostr(Priority),
1961*0b57cec5SDimitry Andric                                      SectionKind::getData());
1962*0b57cec5SDimitry Andric }
1963*0b57cec5SDimitry Andric 
1964*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
1965*0b57cec5SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
1966*0b57cec5SDimitry Andric   llvm_unreachable("@llvm.global_dtors should have been lowered already");
1967*0b57cec5SDimitry Andric   return nullptr;
1968*0b57cec5SDimitry Andric }
19698bcb0991SDimitry Andric 
19708bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
19718bcb0991SDimitry Andric //                                  XCOFF
19728bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
19735ffd83dbSDimitry Andric MCSymbol *
19745ffd83dbSDimitry Andric TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV,
19755ffd83dbSDimitry Andric                                                const TargetMachine &TM) const {
19765ffd83dbSDimitry Andric   if (TM.getDataSections())
19775ffd83dbSDimitry Andric     report_fatal_error("XCOFF unique data sections not yet implemented");
19785ffd83dbSDimitry Andric 
19795ffd83dbSDimitry Andric   // We always use a qualname symbol for a GV that represents
19805ffd83dbSDimitry Andric   // a declaration, a function descriptor, or a common symbol.
19815ffd83dbSDimitry Andric   // It is inherently ambiguous when the GO represents the address of a
19825ffd83dbSDimitry Andric   // function, as the GO could either represent a function descriptor or a
19835ffd83dbSDimitry Andric   // function entry point. We choose to always return a function descriptor
19845ffd83dbSDimitry Andric   // here.
19855ffd83dbSDimitry Andric   if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) {
19865ffd83dbSDimitry Andric     if (GO->isDeclarationForLinker())
19875ffd83dbSDimitry Andric       return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM))
19885ffd83dbSDimitry Andric           ->getQualNameSymbol();
19895ffd83dbSDimitry Andric 
19905ffd83dbSDimitry Andric     SectionKind GOKind = getKindForGlobal(GO, TM);
19915ffd83dbSDimitry Andric     if (GOKind.isText())
19925ffd83dbSDimitry Andric       return cast<MCSectionXCOFF>(
19935ffd83dbSDimitry Andric                  getSectionForFunctionDescriptor(cast<Function>(GO), TM))
19945ffd83dbSDimitry Andric           ->getQualNameSymbol();
19955ffd83dbSDimitry Andric     if (GOKind.isCommon() || GOKind.isBSSLocal())
19965ffd83dbSDimitry Andric       return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM))
19975ffd83dbSDimitry Andric           ->getQualNameSymbol();
19985ffd83dbSDimitry Andric   }
19995ffd83dbSDimitry Andric 
20005ffd83dbSDimitry Andric   // For all other cases, fall back to getSymbol to return the unqualified name.
20015ffd83dbSDimitry Andric   // This could change for a GV that is a GlobalVariable when we decide to
20025ffd83dbSDimitry Andric   // support -fdata-sections since we could avoid having label symbols if the
20035ffd83dbSDimitry Andric   // linkage name is applied to the csect symbol.
20045ffd83dbSDimitry Andric   return nullptr;
20055ffd83dbSDimitry Andric }
20065ffd83dbSDimitry Andric 
20078bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
20088bcb0991SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
20098bcb0991SDimitry Andric   report_fatal_error("XCOFF explicit sections not yet implemented.");
20108bcb0991SDimitry Andric }
20118bcb0991SDimitry Andric 
20125ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference(
20135ffd83dbSDimitry Andric     const GlobalObject *GO, const TargetMachine &TM) const {
20145ffd83dbSDimitry Andric   assert(GO->isDeclarationForLinker() &&
20155ffd83dbSDimitry Andric          "Tried to get ER section for a defined global.");
20165ffd83dbSDimitry Andric 
20175ffd83dbSDimitry Andric   SmallString<128> Name;
20185ffd83dbSDimitry Andric   getNameWithPrefix(Name, GO, TM);
20195ffd83dbSDimitry Andric   XCOFF::StorageClass SC =
20205ffd83dbSDimitry Andric       TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GO);
20215ffd83dbSDimitry Andric 
20225ffd83dbSDimitry Andric   // Externals go into a csect of type ER.
20235ffd83dbSDimitry Andric   return getContext().getXCOFFSection(
20245ffd83dbSDimitry Andric       Name, isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA, XCOFF::XTY_ER,
20255ffd83dbSDimitry Andric       SC, SectionKind::getMetadata());
20265ffd83dbSDimitry Andric }
20275ffd83dbSDimitry Andric 
20288bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
20298bcb0991SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
20308bcb0991SDimitry Andric   assert(!TM.getFunctionSections() && !TM.getDataSections() &&
20318bcb0991SDimitry Andric          "XCOFF unique sections not yet implemented.");
20328bcb0991SDimitry Andric 
20338bcb0991SDimitry Andric   // Common symbols go into a csect with matching name which will get mapped
20348bcb0991SDimitry Andric   // into the .bss section.
20358bcb0991SDimitry Andric   if (Kind.isBSSLocal() || Kind.isCommon()) {
20368bcb0991SDimitry Andric     SmallString<128> Name;
20378bcb0991SDimitry Andric     getNameWithPrefix(Name, GO, TM);
20388bcb0991SDimitry Andric     XCOFF::StorageClass SC =
20398bcb0991SDimitry Andric         TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GO);
20408bcb0991SDimitry Andric     return getContext().getXCOFFSection(
20418bcb0991SDimitry Andric         Name, Kind.isBSSLocal() ? XCOFF::XMC_BS : XCOFF::XMC_RW, XCOFF::XTY_CM,
20428bcb0991SDimitry Andric         SC, Kind, /* BeginSymbolName */ nullptr);
20438bcb0991SDimitry Andric   }
20448bcb0991SDimitry Andric 
2045480093f4SDimitry Andric   if (Kind.isMergeableCString()) {
20465ffd83dbSDimitry Andric     Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
2047480093f4SDimitry Andric         cast<GlobalVariable>(GO));
2048480093f4SDimitry Andric 
2049480093f4SDimitry Andric     unsigned EntrySize = getEntrySizeForKind(Kind);
2050480093f4SDimitry Andric     std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
2051480093f4SDimitry Andric     SmallString<128> Name;
20525ffd83dbSDimitry Andric     Name = SizeSpec + utostr(Alignment.value());
2053480093f4SDimitry Andric 
2054480093f4SDimitry Andric     return getContext().getXCOFFSection(
2055480093f4SDimitry Andric         Name, XCOFF::XMC_RO, XCOFF::XTY_SD,
2056480093f4SDimitry Andric         TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GO),
2057480093f4SDimitry Andric         Kind, /* BeginSymbolName */ nullptr);
2058480093f4SDimitry Andric   }
2059480093f4SDimitry Andric 
20608bcb0991SDimitry Andric   if (Kind.isText())
20618bcb0991SDimitry Andric     return TextSection;
20628bcb0991SDimitry Andric 
2063480093f4SDimitry Andric   if (Kind.isData() || Kind.isReadOnlyWithRel())
2064480093f4SDimitry Andric     // TODO: We may put this under option control, because user may want to
2065480093f4SDimitry Andric     // have read-only data with relocations placed into a read-only section by
2066480093f4SDimitry Andric     // the compiler.
20678bcb0991SDimitry Andric     return DataSection;
20688bcb0991SDimitry Andric 
2069480093f4SDimitry Andric   // Zero initialized data must be emitted to the .data section because external
2070480093f4SDimitry Andric   // linkage control sections that get mapped to the .bss section will be linked
2071480093f4SDimitry Andric   // as tentative defintions, which is only appropriate for SectionKind::Common.
2072480093f4SDimitry Andric   if (Kind.isBSS())
2073480093f4SDimitry Andric     return DataSection;
2074480093f4SDimitry Andric 
2075480093f4SDimitry Andric   if (Kind.isReadOnly())
2076480093f4SDimitry Andric     return ReadOnlySection;
2077480093f4SDimitry Andric 
20788bcb0991SDimitry Andric   report_fatal_error("XCOFF other section types not yet implemented.");
20798bcb0991SDimitry Andric }
20808bcb0991SDimitry Andric 
2081480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable(
2082480093f4SDimitry Andric     const Function &F, const TargetMachine &TM) const {
2083480093f4SDimitry Andric   assert (!TM.getFunctionSections() && "Unique sections not supported on XCOFF"
2084480093f4SDimitry Andric           " yet.");
2085480093f4SDimitry Andric   assert (!F.getComdat() && "Comdat not supported on XCOFF.");
2086480093f4SDimitry Andric   //TODO: Enable emiting jump table to unique sections when we support it.
2087480093f4SDimitry Andric   return ReadOnlySection;
2088480093f4SDimitry Andric }
2089480093f4SDimitry Andric 
20908bcb0991SDimitry Andric bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
20918bcb0991SDimitry Andric     bool UsesLabelDifference, const Function &F) const {
2092480093f4SDimitry Andric   return false;
2093480093f4SDimitry Andric }
2094480093f4SDimitry Andric 
2095480093f4SDimitry Andric /// Given a mergeable constant with the specified size and relocation
2096480093f4SDimitry Andric /// information, return a section that it should be placed in.
2097480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant(
2098480093f4SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
20995ffd83dbSDimitry Andric     Align &Alignment) const {
2100480093f4SDimitry Andric   //TODO: Enable emiting constant pool to unique sections when we support it.
2101480093f4SDimitry Andric   return ReadOnlySection;
21028bcb0991SDimitry Andric }
21038bcb0991SDimitry Andric 
21048bcb0991SDimitry Andric void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx,
21058bcb0991SDimitry Andric                                                const TargetMachine &TgtM) {
21068bcb0991SDimitry Andric   TargetLoweringObjectFile::Initialize(Ctx, TgtM);
21078bcb0991SDimitry Andric   TTypeEncoding = 0;
21088bcb0991SDimitry Andric   PersonalityEncoding = 0;
21098bcb0991SDimitry Andric   LSDAEncoding = 0;
21108bcb0991SDimitry Andric }
21118bcb0991SDimitry Andric 
21128bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection(
21138bcb0991SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
21148bcb0991SDimitry Andric   report_fatal_error("XCOFF ctor section not yet implemented.");
21158bcb0991SDimitry Andric }
21168bcb0991SDimitry Andric 
21178bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection(
21188bcb0991SDimitry Andric     unsigned Priority, const MCSymbol *KeySym) const {
21198bcb0991SDimitry Andric   report_fatal_error("XCOFF dtor section not yet implemented.");
21208bcb0991SDimitry Andric }
21218bcb0991SDimitry Andric 
21228bcb0991SDimitry Andric const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference(
21238bcb0991SDimitry Andric     const GlobalValue *LHS, const GlobalValue *RHS,
21248bcb0991SDimitry Andric     const TargetMachine &TM) const {
21258bcb0991SDimitry Andric   report_fatal_error("XCOFF not yet implemented.");
21268bcb0991SDimitry Andric }
21278bcb0991SDimitry Andric 
21288bcb0991SDimitry Andric XCOFF::StorageClass TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(
21298bcb0991SDimitry Andric     const GlobalObject *GO) {
21308bcb0991SDimitry Andric   switch (GO->getLinkage()) {
21318bcb0991SDimitry Andric   case GlobalValue::InternalLinkage:
2132480093f4SDimitry Andric   case GlobalValue::PrivateLinkage:
21338bcb0991SDimitry Andric     return XCOFF::C_HIDEXT;
21348bcb0991SDimitry Andric   case GlobalValue::ExternalLinkage:
21358bcb0991SDimitry Andric   case GlobalValue::CommonLinkage:
21365ffd83dbSDimitry Andric   case GlobalValue::AvailableExternallyLinkage:
21378bcb0991SDimitry Andric     return XCOFF::C_EXT;
21388bcb0991SDimitry Andric   case GlobalValue::ExternalWeakLinkage:
21395ffd83dbSDimitry Andric   case GlobalValue::LinkOnceAnyLinkage:
21405ffd83dbSDimitry Andric   case GlobalValue::LinkOnceODRLinkage:
21415ffd83dbSDimitry Andric   case GlobalValue::WeakAnyLinkage:
21425ffd83dbSDimitry Andric   case GlobalValue::WeakODRLinkage:
21438bcb0991SDimitry Andric     return XCOFF::C_WEAKEXT;
21445ffd83dbSDimitry Andric   case GlobalValue::AppendingLinkage:
21458bcb0991SDimitry Andric     report_fatal_error(
21465ffd83dbSDimitry Andric         "There is no mapping that implements AppendingLinkage for XCOFF.");
21478bcb0991SDimitry Andric   }
21485ffd83dbSDimitry Andric   llvm_unreachable("Unknown linkage type!");
21495ffd83dbSDimitry Andric }
21505ffd83dbSDimitry Andric 
21515ffd83dbSDimitry Andric MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol(
21525ffd83dbSDimitry Andric     const Function *F, const TargetMachine &TM) const {
21535ffd83dbSDimitry Andric   SmallString<128> NameStr;
21545ffd83dbSDimitry Andric   NameStr.push_back('.');
21555ffd83dbSDimitry Andric   getNameWithPrefix(NameStr, F, TM);
21565ffd83dbSDimitry Andric   return getContext().getOrCreateSymbol(NameStr);
21575ffd83dbSDimitry Andric }
21585ffd83dbSDimitry Andric 
21595ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor(
21605ffd83dbSDimitry Andric     const Function *F, const TargetMachine &TM) const {
21615ffd83dbSDimitry Andric   SmallString<128> NameStr;
21625ffd83dbSDimitry Andric   getNameWithPrefix(NameStr, F, TM);
21635ffd83dbSDimitry Andric   return getContext().getXCOFFSection(NameStr, XCOFF::XMC_DS, XCOFF::XTY_SD,
21645ffd83dbSDimitry Andric                                       getStorageClassForGlobal(F),
21655ffd83dbSDimitry Andric                                       SectionKind::getData());
21665ffd83dbSDimitry Andric }
21675ffd83dbSDimitry Andric 
21685ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry(
21695ffd83dbSDimitry Andric     const MCSymbol *Sym) const {
21705ffd83dbSDimitry Andric   return getContext().getXCOFFSection(
21715ffd83dbSDimitry Andric       cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), XCOFF::XMC_TC,
21725ffd83dbSDimitry Andric       XCOFF::XTY_SD, XCOFF::C_HIDEXT, SectionKind::getData());
21738bcb0991SDimitry Andric }
2174