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" 24e8d8bef9SDimitry Andric #include "llvm/CodeGen/BasicBlockSectionUtils.h" 255ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 265ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/Comdat.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 335ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 345ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 40*0b57cec5SDimitry Andric #include "llvm/IR/Mangler.h" 41*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 42*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 43e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h" 44*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 45*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 46*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 47*0b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h" 48*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionCOFF.h" 49*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h" 50*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionMachO.h" 51*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h" 528bcb0991SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h" 53*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 54*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 55*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h" 56*0b57cec5SDimitry Andric #include "llvm/MC/MCValue.h" 57*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 58*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h" 59*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 60*0b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 61*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 625ffd83dbSDimitry Andric #include "llvm/Support/Format.h" 63*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 64*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 65*0b57cec5SDimitry Andric #include <cassert> 66*0b57cec5SDimitry Andric #include <string> 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric using namespace llvm; 69*0b57cec5SDimitry Andric using namespace dwarf; 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, 72*0b57cec5SDimitry Andric StringRef &Section) { 73*0b57cec5SDimitry Andric SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 74*0b57cec5SDimitry Andric M.getModuleFlagsMetadata(ModuleFlags); 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric for (const auto &MFE: ModuleFlags) { 77*0b57cec5SDimitry Andric // Ignore flags with 'Require' behaviour. 78*0b57cec5SDimitry Andric if (MFE.Behavior == Module::Require) 79*0b57cec5SDimitry Andric continue; 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric StringRef Key = MFE.Key->getString(); 82*0b57cec5SDimitry Andric if (Key == "Objective-C Image Info Version") { 83*0b57cec5SDimitry Andric Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 84*0b57cec5SDimitry Andric } else if (Key == "Objective-C Garbage Collection" || 85*0b57cec5SDimitry Andric Key == "Objective-C GC Only" || 86*0b57cec5SDimitry Andric Key == "Objective-C Is Simulated" || 87*0b57cec5SDimitry Andric Key == "Objective-C Class Properties" || 88*0b57cec5SDimitry Andric Key == "Objective-C Image Swift Version") { 89*0b57cec5SDimitry Andric Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 90*0b57cec5SDimitry Andric } else if (Key == "Objective-C Image Info Section") { 91*0b57cec5SDimitry Andric Section = cast<MDString>(MFE.Val)->getString(); 92*0b57cec5SDimitry Andric } 935ffd83dbSDimitry Andric // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor + 945ffd83dbSDimitry Andric // "Objective-C Garbage Collection". 955ffd83dbSDimitry Andric else if (Key == "Swift ABI Version") { 965ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 8; 975ffd83dbSDimitry Andric } else if (Key == "Swift Major Version") { 985ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 24; 995ffd83dbSDimitry Andric } else if (Key == "Swift Minor Version") { 1005ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 16; 1015ffd83dbSDimitry Andric } 102*0b57cec5SDimitry Andric } 103*0b57cec5SDimitry Andric } 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 106*0b57cec5SDimitry Andric // ELF 107*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 108*0b57cec5SDimitry Andric 109e8d8bef9SDimitry Andric TargetLoweringObjectFileELF::TargetLoweringObjectFileELF() 110e8d8bef9SDimitry Andric : TargetLoweringObjectFile() { 111e8d8bef9SDimitry Andric SupportDSOLocalEquivalentLowering = true; 112e8d8bef9SDimitry Andric } 113e8d8bef9SDimitry Andric 114*0b57cec5SDimitry Andric void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 115*0b57cec5SDimitry Andric const TargetMachine &TgtM) { 116*0b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TgtM); 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric CodeModel::Model CM = TgtM.getCodeModel(); 1195ffd83dbSDimitry Andric InitializeELF(TgtM.Options.UseInitArray); 120*0b57cec5SDimitry Andric 121*0b57cec5SDimitry Andric switch (TgtM.getTargetTriple().getArch()) { 122*0b57cec5SDimitry Andric case Triple::arm: 123*0b57cec5SDimitry Andric case Triple::armeb: 124*0b57cec5SDimitry Andric case Triple::thumb: 125*0b57cec5SDimitry Andric case Triple::thumbeb: 126*0b57cec5SDimitry Andric if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) 127*0b57cec5SDimitry Andric break; 128*0b57cec5SDimitry Andric // Fallthrough if not using EHABI 129*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 130*0b57cec5SDimitry Andric case Triple::ppc: 131e8d8bef9SDimitry Andric case Triple::ppcle: 132*0b57cec5SDimitry Andric case Triple::x86: 133*0b57cec5SDimitry Andric PersonalityEncoding = isPositionIndependent() 134*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_indirect | 135*0b57cec5SDimitry Andric dwarf::DW_EH_PE_pcrel | 136*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4 137*0b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 138*0b57cec5SDimitry Andric LSDAEncoding = isPositionIndependent() 139*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 140*0b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 141*0b57cec5SDimitry Andric TTypeEncoding = isPositionIndependent() 142*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 143*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4 144*0b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 145*0b57cec5SDimitry Andric break; 146*0b57cec5SDimitry Andric case Triple::x86_64: 147*0b57cec5SDimitry Andric if (isPositionIndependent()) { 148*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 149*0b57cec5SDimitry Andric ((CM == CodeModel::Small || CM == CodeModel::Medium) 150*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 151*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | 152*0b57cec5SDimitry Andric (CM == CodeModel::Small 153*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 154*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 155*0b57cec5SDimitry Andric ((CM == CodeModel::Small || CM == CodeModel::Medium) 156*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); 157*0b57cec5SDimitry Andric } else { 158*0b57cec5SDimitry Andric PersonalityEncoding = 159*0b57cec5SDimitry Andric (CM == CodeModel::Small || CM == CodeModel::Medium) 160*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 161*0b57cec5SDimitry Andric LSDAEncoding = (CM == CodeModel::Small) 162*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 163*0b57cec5SDimitry Andric TTypeEncoding = (CM == CodeModel::Small) 164*0b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric break; 167*0b57cec5SDimitry Andric case Triple::hexagon: 168*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 169*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 170*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 171*0b57cec5SDimitry Andric if (isPositionIndependent()) { 172*0b57cec5SDimitry Andric PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 173*0b57cec5SDimitry Andric LSDAEncoding |= dwarf::DW_EH_PE_pcrel; 174*0b57cec5SDimitry Andric TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 175*0b57cec5SDimitry Andric } 176*0b57cec5SDimitry Andric break; 177*0b57cec5SDimitry Andric case Triple::aarch64: 178*0b57cec5SDimitry Andric case Triple::aarch64_be: 1798bcb0991SDimitry Andric case Triple::aarch64_32: 180*0b57cec5SDimitry Andric // The small model guarantees static code/data size < 4GB, but not where it 181*0b57cec5SDimitry Andric // will be in memory. Most of these could end up >2GB away so even a signed 182*0b57cec5SDimitry Andric // pc-relative 32-bit address is insufficient, theoretically. 183*0b57cec5SDimitry Andric if (isPositionIndependent()) { 184e8d8bef9SDimitry Andric // ILP32 uses sdata4 instead of sdata8 185e8d8bef9SDimitry Andric if (TgtM.getTargetTriple().getEnvironment() == Triple::GNUILP32) { 186e8d8bef9SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 187e8d8bef9SDimitry Andric dwarf::DW_EH_PE_sdata4; 188e8d8bef9SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 189e8d8bef9SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 190e8d8bef9SDimitry Andric dwarf::DW_EH_PE_sdata4; 191e8d8bef9SDimitry Andric } else { 192*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 193*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata8; 194*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8; 195*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 196*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata8; 197e8d8bef9SDimitry Andric } 198*0b57cec5SDimitry Andric } else { 199*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 200*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 201*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 202*0b57cec5SDimitry Andric } 203*0b57cec5SDimitry Andric break; 204*0b57cec5SDimitry Andric case Triple::lanai: 205*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 206*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 207*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 208*0b57cec5SDimitry Andric break; 209*0b57cec5SDimitry Andric case Triple::mips: 210*0b57cec5SDimitry Andric case Triple::mipsel: 211*0b57cec5SDimitry Andric case Triple::mips64: 212*0b57cec5SDimitry Andric case Triple::mips64el: 213*0b57cec5SDimitry Andric // MIPS uses indirect pointer to refer personality functions and types, so 214*0b57cec5SDimitry Andric // that the eh_frame section can be read-only. DW.ref.personality will be 215*0b57cec5SDimitry Andric // generated for relocation. 216*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect; 217*0b57cec5SDimitry Andric // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't 218*0b57cec5SDimitry Andric // identify N64 from just a triple. 219*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 220*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 221*0b57cec5SDimitry Andric // We don't support PC-relative LSDA references in GAS so we use the default 222*0b57cec5SDimitry Andric // DW_EH_PE_absptr for those. 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric // FreeBSD must be explicit about the data size and using pcrel since it's 225*0b57cec5SDimitry Andric // assembler/linker won't do the automatic conversion that the Linux tools 226*0b57cec5SDimitry Andric // do. 227*0b57cec5SDimitry Andric if (TgtM.getTargetTriple().isOSFreeBSD()) { 228*0b57cec5SDimitry Andric PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 229*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 230*0b57cec5SDimitry Andric } 231*0b57cec5SDimitry Andric break; 232*0b57cec5SDimitry Andric case Triple::ppc64: 233*0b57cec5SDimitry Andric case Triple::ppc64le: 234*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 235*0b57cec5SDimitry Andric dwarf::DW_EH_PE_udata8; 236*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; 237*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 238*0b57cec5SDimitry Andric dwarf::DW_EH_PE_udata8; 239*0b57cec5SDimitry Andric break; 240*0b57cec5SDimitry Andric case Triple::sparcel: 241*0b57cec5SDimitry Andric case Triple::sparc: 242*0b57cec5SDimitry Andric if (isPositionIndependent()) { 243*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 244*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 245*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 246*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 247*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 248*0b57cec5SDimitry Andric } else { 249*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 250*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 251*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 252*0b57cec5SDimitry Andric } 253*0b57cec5SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 254*0b57cec5SDimitry Andric break; 255*0b57cec5SDimitry Andric case Triple::riscv32: 256*0b57cec5SDimitry Andric case Triple::riscv64: 257*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 258*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 259*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 260*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 261*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 262*0b57cec5SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 263*0b57cec5SDimitry Andric break; 264*0b57cec5SDimitry Andric case Triple::sparcv9: 265*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 266*0b57cec5SDimitry Andric if (isPositionIndependent()) { 267*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 268*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 269*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 270*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 271*0b57cec5SDimitry Andric } else { 272*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 273*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 274*0b57cec5SDimitry Andric } 275*0b57cec5SDimitry Andric break; 276*0b57cec5SDimitry Andric case Triple::systemz: 277*0b57cec5SDimitry Andric // All currently-defined code models guarantee that 4-byte PC-relative 278*0b57cec5SDimitry Andric // values will be in range. 279*0b57cec5SDimitry Andric if (isPositionIndependent()) { 280*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 281*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 282*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 283*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 284*0b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 285*0b57cec5SDimitry Andric } else { 286*0b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 287*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 288*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric break; 291*0b57cec5SDimitry Andric default: 292*0b57cec5SDimitry Andric break; 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric } 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, 297*0b57cec5SDimitry Andric Module &M) const { 298*0b57cec5SDimitry Andric auto &C = getContext(); 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 301*0b57cec5SDimitry Andric auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS, 302*0b57cec5SDimitry Andric ELF::SHF_EXCLUDE); 303*0b57cec5SDimitry Andric 304*0b57cec5SDimitry Andric Streamer.SwitchSection(S); 305*0b57cec5SDimitry Andric 306480093f4SDimitry Andric for (const auto *Operand : LinkerOptions->operands()) { 307*0b57cec5SDimitry Andric if (cast<MDNode>(Operand)->getNumOperands() != 2) 308*0b57cec5SDimitry Andric report_fatal_error("invalid llvm.linker.options"); 309*0b57cec5SDimitry Andric for (const auto &Option : cast<MDNode>(Operand)->operands()) { 3105ffd83dbSDimitry Andric Streamer.emitBytes(cast<MDString>(Option)->getString()); 3115ffd83dbSDimitry Andric Streamer.emitInt8(0); 312*0b57cec5SDimitry Andric } 313*0b57cec5SDimitry Andric } 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) { 317*0b57cec5SDimitry Andric auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES, 318*0b57cec5SDimitry Andric ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric Streamer.SwitchSection(S); 321*0b57cec5SDimitry Andric 322480093f4SDimitry Andric for (const auto *Operand : DependentLibraries->operands()) { 3235ffd83dbSDimitry Andric Streamer.emitBytes( 324*0b57cec5SDimitry Andric cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString()); 3255ffd83dbSDimitry Andric Streamer.emitInt8(0); 326*0b57cec5SDimitry Andric } 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric 329e8d8bef9SDimitry Andric if (NamedMDNode *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName)) { 330e8d8bef9SDimitry Andric // Emit a descriptor for every function including functions that have an 331e8d8bef9SDimitry Andric // available external linkage. We may not want this for imported functions 332e8d8bef9SDimitry Andric // that has code in another thinLTO module but we don't have a good way to 333e8d8bef9SDimitry Andric // tell them apart from inline functions defined in header files. Therefore 334e8d8bef9SDimitry Andric // we put each descriptor in a separate comdat section and rely on the 335e8d8bef9SDimitry Andric // linker to deduplicate. 336e8d8bef9SDimitry Andric for (const auto *Operand : FuncInfo->operands()) { 337e8d8bef9SDimitry Andric const auto *MD = cast<MDNode>(Operand); 338e8d8bef9SDimitry Andric auto *GUID = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0)); 339e8d8bef9SDimitry Andric auto *Hash = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); 340e8d8bef9SDimitry Andric auto *Name = cast<MDString>(MD->getOperand(2)); 341e8d8bef9SDimitry Andric auto *S = C.getObjectFileInfo()->getPseudoProbeDescSection( 342e8d8bef9SDimitry Andric TM->getFunctionSections() ? Name->getString() : StringRef()); 343e8d8bef9SDimitry Andric 344e8d8bef9SDimitry Andric Streamer.SwitchSection(S); 345e8d8bef9SDimitry Andric Streamer.emitInt64(GUID->getZExtValue()); 346e8d8bef9SDimitry Andric Streamer.emitInt64(Hash->getZExtValue()); 347e8d8bef9SDimitry Andric Streamer.emitULEB128IntValue(Name->getString().size()); 348e8d8bef9SDimitry Andric Streamer.emitBytes(Name->getString()); 349e8d8bef9SDimitry Andric } 350e8d8bef9SDimitry Andric } 351e8d8bef9SDimitry Andric 352*0b57cec5SDimitry Andric unsigned Version = 0; 353*0b57cec5SDimitry Andric unsigned Flags = 0; 354*0b57cec5SDimitry Andric StringRef Section; 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric GetObjCImageInfo(M, Version, Flags, Section); 357*0b57cec5SDimitry Andric if (!Section.empty()) { 358*0b57cec5SDimitry Andric auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 359*0b57cec5SDimitry Andric Streamer.SwitchSection(S); 3605ffd83dbSDimitry Andric Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 3615ffd83dbSDimitry Andric Streamer.emitInt32(Version); 3625ffd83dbSDimitry Andric Streamer.emitInt32(Flags); 363*0b57cec5SDimitry Andric Streamer.AddBlankLine(); 364*0b57cec5SDimitry Andric } 365*0b57cec5SDimitry Andric 366e8d8bef9SDimitry Andric emitCGProfileMetadata(Streamer, M); 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, 439e8d8bef9SDimitry Andric /*AddSegmentInfo=*/false) || 440e8d8bef9SDimitry Andric Name == ".llvmbc" || Name == ".llvmcmd") 441*0b57cec5SDimitry Andric return SectionKind::getMetadata(); 442*0b57cec5SDimitry Andric 443*0b57cec5SDimitry Andric if (Name.empty() || Name[0] != '.') return K; 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric // Default implementation based on some magic section names. 446*0b57cec5SDimitry Andric if (Name == ".bss" || 447*0b57cec5SDimitry Andric Name.startswith(".bss.") || 448*0b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.b.") || 449*0b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.b.") || 450*0b57cec5SDimitry Andric Name == ".sbss" || 451*0b57cec5SDimitry Andric Name.startswith(".sbss.") || 452*0b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.sb.") || 453*0b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.sb.")) 454*0b57cec5SDimitry Andric return SectionKind::getBSS(); 455*0b57cec5SDimitry Andric 456*0b57cec5SDimitry Andric if (Name == ".tdata" || 457*0b57cec5SDimitry Andric Name.startswith(".tdata.") || 458*0b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.td.") || 459*0b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.td.")) 460*0b57cec5SDimitry Andric return SectionKind::getThreadData(); 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric if (Name == ".tbss" || 463*0b57cec5SDimitry Andric Name.startswith(".tbss.") || 464*0b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.tb.") || 465*0b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.tb.")) 466*0b57cec5SDimitry Andric return SectionKind::getThreadBSS(); 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric return K; 469*0b57cec5SDimitry Andric } 470*0b57cec5SDimitry Andric 471*0b57cec5SDimitry Andric static unsigned getELFSectionType(StringRef Name, SectionKind K) { 472*0b57cec5SDimitry Andric // Use SHT_NOTE for section whose name starts with ".note" to allow 473*0b57cec5SDimitry Andric // emitting ELF notes from C variable declaration. 474*0b57cec5SDimitry Andric // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 475*0b57cec5SDimitry Andric if (Name.startswith(".note")) 476*0b57cec5SDimitry Andric return ELF::SHT_NOTE; 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric if (Name == ".init_array") 479*0b57cec5SDimitry Andric return ELF::SHT_INIT_ARRAY; 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric if (Name == ".fini_array") 482*0b57cec5SDimitry Andric return ELF::SHT_FINI_ARRAY; 483*0b57cec5SDimitry Andric 484*0b57cec5SDimitry Andric if (Name == ".preinit_array") 485*0b57cec5SDimitry Andric return ELF::SHT_PREINIT_ARRAY; 486*0b57cec5SDimitry Andric 487*0b57cec5SDimitry Andric if (K.isBSS() || K.isThreadBSS()) 488*0b57cec5SDimitry Andric return ELF::SHT_NOBITS; 489*0b57cec5SDimitry Andric 490*0b57cec5SDimitry Andric return ELF::SHT_PROGBITS; 491*0b57cec5SDimitry Andric } 492*0b57cec5SDimitry Andric 493*0b57cec5SDimitry Andric static unsigned getELFSectionFlags(SectionKind K) { 494*0b57cec5SDimitry Andric unsigned Flags = 0; 495*0b57cec5SDimitry Andric 496*0b57cec5SDimitry Andric if (!K.isMetadata()) 497*0b57cec5SDimitry Andric Flags |= ELF::SHF_ALLOC; 498*0b57cec5SDimitry Andric 499*0b57cec5SDimitry Andric if (K.isText()) 500*0b57cec5SDimitry Andric Flags |= ELF::SHF_EXECINSTR; 501*0b57cec5SDimitry Andric 502*0b57cec5SDimitry Andric if (K.isExecuteOnly()) 503*0b57cec5SDimitry Andric Flags |= ELF::SHF_ARM_PURECODE; 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric if (K.isWriteable()) 506*0b57cec5SDimitry Andric Flags |= ELF::SHF_WRITE; 507*0b57cec5SDimitry Andric 508*0b57cec5SDimitry Andric if (K.isThreadLocal()) 509*0b57cec5SDimitry Andric Flags |= ELF::SHF_TLS; 510*0b57cec5SDimitry Andric 511*0b57cec5SDimitry Andric if (K.isMergeableCString() || K.isMergeableConst()) 512*0b57cec5SDimitry Andric Flags |= ELF::SHF_MERGE; 513*0b57cec5SDimitry Andric 514*0b57cec5SDimitry Andric if (K.isMergeableCString()) 515*0b57cec5SDimitry Andric Flags |= ELF::SHF_STRINGS; 516*0b57cec5SDimitry Andric 517*0b57cec5SDimitry Andric return Flags; 518*0b57cec5SDimitry Andric } 519*0b57cec5SDimitry Andric 520*0b57cec5SDimitry Andric static const Comdat *getELFComdat(const GlobalValue *GV) { 521*0b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 522*0b57cec5SDimitry Andric if (!C) 523*0b57cec5SDimitry Andric return nullptr; 524*0b57cec5SDimitry Andric 525*0b57cec5SDimitry Andric if (C->getSelectionKind() != Comdat::Any) 526*0b57cec5SDimitry Andric report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + 527*0b57cec5SDimitry Andric C->getName() + "' cannot be lowered."); 528*0b57cec5SDimitry Andric 529*0b57cec5SDimitry Andric return C; 530*0b57cec5SDimitry Andric } 531*0b57cec5SDimitry Andric 5325ffd83dbSDimitry Andric static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO, 533*0b57cec5SDimitry Andric const TargetMachine &TM) { 534*0b57cec5SDimitry Andric MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); 535*0b57cec5SDimitry Andric if (!MD) 536*0b57cec5SDimitry Andric return nullptr; 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric const MDOperand &Op = MD->getOperand(0); 539*0b57cec5SDimitry Andric if (!Op.get()) 540*0b57cec5SDimitry Andric return nullptr; 541*0b57cec5SDimitry Andric 542*0b57cec5SDimitry Andric auto *VM = dyn_cast<ValueAsMetadata>(Op); 543*0b57cec5SDimitry Andric if (!VM) 544*0b57cec5SDimitry Andric report_fatal_error("MD_associated operand is not ValueAsMetadata"); 545*0b57cec5SDimitry Andric 5468bcb0991SDimitry Andric auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue()); 5478bcb0991SDimitry Andric return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr; 548*0b57cec5SDimitry Andric } 549*0b57cec5SDimitry Andric 550*0b57cec5SDimitry Andric static unsigned getEntrySizeForKind(SectionKind Kind) { 551*0b57cec5SDimitry Andric if (Kind.isMergeable1ByteCString()) 552*0b57cec5SDimitry Andric return 1; 553*0b57cec5SDimitry Andric else if (Kind.isMergeable2ByteCString()) 554*0b57cec5SDimitry Andric return 2; 555*0b57cec5SDimitry Andric else if (Kind.isMergeable4ByteCString()) 556*0b57cec5SDimitry Andric return 4; 557*0b57cec5SDimitry Andric else if (Kind.isMergeableConst4()) 558*0b57cec5SDimitry Andric return 4; 559*0b57cec5SDimitry Andric else if (Kind.isMergeableConst8()) 560*0b57cec5SDimitry Andric return 8; 561*0b57cec5SDimitry Andric else if (Kind.isMergeableConst16()) 562*0b57cec5SDimitry Andric return 16; 563*0b57cec5SDimitry Andric else if (Kind.isMergeableConst32()) 564*0b57cec5SDimitry Andric return 32; 565*0b57cec5SDimitry Andric else { 566*0b57cec5SDimitry Andric // We shouldn't have mergeable C strings or mergeable constants that we 567*0b57cec5SDimitry Andric // didn't handle above. 568*0b57cec5SDimitry Andric assert(!Kind.isMergeableCString() && "unknown string width"); 569*0b57cec5SDimitry Andric assert(!Kind.isMergeableConst() && "unknown data width"); 570*0b57cec5SDimitry Andric return 0; 571*0b57cec5SDimitry Andric } 572*0b57cec5SDimitry Andric } 573*0b57cec5SDimitry Andric 5745ffd83dbSDimitry Andric /// Return the section prefix name used by options FunctionsSections and 5755ffd83dbSDimitry Andric /// DataSections. 5765ffd83dbSDimitry Andric static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 5775ffd83dbSDimitry Andric if (Kind.isText()) 5785ffd83dbSDimitry Andric return ".text"; 5795ffd83dbSDimitry Andric if (Kind.isReadOnly()) 5805ffd83dbSDimitry Andric return ".rodata"; 5815ffd83dbSDimitry Andric if (Kind.isBSS()) 5825ffd83dbSDimitry Andric return ".bss"; 5835ffd83dbSDimitry Andric if (Kind.isThreadData()) 5845ffd83dbSDimitry Andric return ".tdata"; 5855ffd83dbSDimitry Andric if (Kind.isThreadBSS()) 5865ffd83dbSDimitry Andric return ".tbss"; 5875ffd83dbSDimitry Andric if (Kind.isData()) 5885ffd83dbSDimitry Andric return ".data"; 5895ffd83dbSDimitry Andric if (Kind.isReadOnlyWithRel()) 5905ffd83dbSDimitry Andric return ".data.rel.ro"; 5915ffd83dbSDimitry Andric llvm_unreachable("Unknown section kind"); 5925ffd83dbSDimitry Andric } 5935ffd83dbSDimitry Andric 5945ffd83dbSDimitry Andric static SmallString<128> 5955ffd83dbSDimitry Andric getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind, 5965ffd83dbSDimitry Andric Mangler &Mang, const TargetMachine &TM, 5975ffd83dbSDimitry Andric unsigned EntrySize, bool UniqueSectionName) { 5985ffd83dbSDimitry Andric SmallString<128> Name; 5995ffd83dbSDimitry Andric if (Kind.isMergeableCString()) { 6005ffd83dbSDimitry Andric // We also need alignment here. 6015ffd83dbSDimitry Andric // FIXME: this is getting the alignment of the character, not the 6025ffd83dbSDimitry Andric // alignment of the global! 6035ffd83dbSDimitry Andric Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 6045ffd83dbSDimitry Andric cast<GlobalVariable>(GO)); 6055ffd83dbSDimitry Andric 6065ffd83dbSDimitry Andric std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 6075ffd83dbSDimitry Andric Name = SizeSpec + utostr(Alignment.value()); 6085ffd83dbSDimitry Andric } else if (Kind.isMergeableConst()) { 6095ffd83dbSDimitry Andric Name = ".rodata.cst"; 6105ffd83dbSDimitry Andric Name += utostr(EntrySize); 6115ffd83dbSDimitry Andric } else { 6125ffd83dbSDimitry Andric Name = getSectionPrefixForGlobal(Kind); 6135ffd83dbSDimitry Andric } 6145ffd83dbSDimitry Andric 6155ffd83dbSDimitry Andric bool HasPrefix = false; 6165ffd83dbSDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) { 6175ffd83dbSDimitry Andric if (Optional<StringRef> Prefix = F->getSectionPrefix()) { 618e8d8bef9SDimitry Andric raw_svector_ostream(Name) << '.' << *Prefix; 6195ffd83dbSDimitry Andric HasPrefix = true; 6205ffd83dbSDimitry Andric } 6215ffd83dbSDimitry Andric } 6225ffd83dbSDimitry Andric 6235ffd83dbSDimitry Andric if (UniqueSectionName) { 6245ffd83dbSDimitry Andric Name.push_back('.'); 6255ffd83dbSDimitry Andric TM.getNameWithPrefix(Name, GO, Mang, /*MayAlwaysUsePrivate*/true); 6265ffd83dbSDimitry Andric } else if (HasPrefix) 6275ffd83dbSDimitry Andric Name.push_back('.'); 6285ffd83dbSDimitry Andric return Name; 6295ffd83dbSDimitry Andric } 6305ffd83dbSDimitry Andric 6315ffd83dbSDimitry Andric namespace { 6325ffd83dbSDimitry Andric class LoweringDiagnosticInfo : public DiagnosticInfo { 6335ffd83dbSDimitry Andric const Twine &Msg; 6345ffd83dbSDimitry Andric 6355ffd83dbSDimitry Andric public: 6365ffd83dbSDimitry Andric LoweringDiagnosticInfo(const Twine &DiagMsg, 6375ffd83dbSDimitry Andric DiagnosticSeverity Severity = DS_Error) 6385ffd83dbSDimitry Andric : DiagnosticInfo(DK_Lowering, Severity), Msg(DiagMsg) {} 6395ffd83dbSDimitry Andric void print(DiagnosticPrinter &DP) const override { DP << Msg; } 6405ffd83dbSDimitry Andric }; 6415ffd83dbSDimitry Andric } 6425ffd83dbSDimitry Andric 643*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( 644*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 645*0b57cec5SDimitry Andric StringRef SectionName = GO->getSection(); 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric // Check if '#pragma clang section' name is applicable. 648*0b57cec5SDimitry Andric // Note that pragma directive overrides -ffunction-section, -fdata-section 649*0b57cec5SDimitry Andric // and so section name is exactly as user specified and not uniqued. 650*0b57cec5SDimitry Andric const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); 651*0b57cec5SDimitry Andric if (GV && GV->hasImplicitSection()) { 652*0b57cec5SDimitry Andric auto Attrs = GV->getAttributes(); 653*0b57cec5SDimitry Andric if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { 654*0b57cec5SDimitry Andric SectionName = Attrs.getAttribute("bss-section").getValueAsString(); 655*0b57cec5SDimitry Andric } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { 656*0b57cec5SDimitry Andric SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); 6578bcb0991SDimitry Andric } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) { 6588bcb0991SDimitry Andric SectionName = Attrs.getAttribute("relro-section").getValueAsString(); 659*0b57cec5SDimitry Andric } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { 660*0b57cec5SDimitry Andric SectionName = Attrs.getAttribute("data-section").getValueAsString(); 661*0b57cec5SDimitry Andric } 662*0b57cec5SDimitry Andric } 663*0b57cec5SDimitry Andric const Function *F = dyn_cast<Function>(GO); 664*0b57cec5SDimitry Andric if (F && F->hasFnAttribute("implicit-section-name")) { 665*0b57cec5SDimitry Andric SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); 666*0b57cec5SDimitry Andric } 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric // Infer section flags from the section name if we can. 669*0b57cec5SDimitry Andric Kind = getELFKindForNamedSection(SectionName, Kind); 670*0b57cec5SDimitry Andric 671*0b57cec5SDimitry Andric StringRef Group = ""; 672*0b57cec5SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 673*0b57cec5SDimitry Andric if (const Comdat *C = getELFComdat(GO)) { 674*0b57cec5SDimitry Andric Group = C->getName(); 675*0b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 676*0b57cec5SDimitry Andric } 677*0b57cec5SDimitry Andric 6785ffd83dbSDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 6795ffd83dbSDimitry Andric 680*0b57cec5SDimitry Andric // A section can have at most one associated section. Put each global with 681*0b57cec5SDimitry Andric // MD_associated in a unique section. 682*0b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 6835ffd83dbSDimitry Andric const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); 684e8d8bef9SDimitry Andric if (GO->getMetadata(LLVMContext::MD_associated)) { 685*0b57cec5SDimitry Andric UniqueID = NextUniqueID++; 686*0b57cec5SDimitry Andric Flags |= ELF::SHF_LINK_ORDER; 6875ffd83dbSDimitry Andric } else { 688e8d8bef9SDimitry Andric if (getContext().getAsmInfo()->useIntegratedAssembler() || 689e8d8bef9SDimitry Andric getContext().getAsmInfo()->binutilsIsAtLeast(2, 35)) { 6905ffd83dbSDimitry Andric // Symbols must be placed into sections with compatible entry 6915ffd83dbSDimitry Andric // sizes. Generate unique sections for symbols that have not 6925ffd83dbSDimitry Andric // been assigned to compatible sections. 6935ffd83dbSDimitry Andric if (Flags & ELF::SHF_MERGE) { 6945ffd83dbSDimitry Andric auto maybeID = getContext().getELFUniqueIDForEntsize(SectionName, Flags, 6955ffd83dbSDimitry Andric EntrySize); 6965ffd83dbSDimitry Andric if (maybeID) 6975ffd83dbSDimitry Andric UniqueID = *maybeID; 6985ffd83dbSDimitry Andric else { 6995ffd83dbSDimitry Andric // If the user has specified the same section name as would be created 7005ffd83dbSDimitry Andric // implicitly for this symbol e.g. .rodata.str1.1, then we don't need 7015ffd83dbSDimitry Andric // to unique the section as the entry size for this symbol will be 7025ffd83dbSDimitry Andric // compatible with implicitly created sections. 7035ffd83dbSDimitry Andric SmallString<128> ImplicitSectionNameStem = getELFSectionNameForGlobal( 7045ffd83dbSDimitry Andric GO, Kind, getMangler(), TM, EntrySize, false); 7055ffd83dbSDimitry Andric if (!(getContext().isELFImplicitMergeableSectionNamePrefix( 7065ffd83dbSDimitry Andric SectionName) && 7075ffd83dbSDimitry Andric SectionName.startswith(ImplicitSectionNameStem))) 7085ffd83dbSDimitry Andric UniqueID = NextUniqueID++; 7095ffd83dbSDimitry Andric } 7105ffd83dbSDimitry Andric } else { 7115ffd83dbSDimitry Andric // We need to unique the section if the user has explicity 7125ffd83dbSDimitry Andric // assigned a non-mergeable symbol to a section name for 7135ffd83dbSDimitry Andric // a generic mergeable section. 7145ffd83dbSDimitry Andric if (getContext().isELFGenericMergeableSection(SectionName)) { 7155ffd83dbSDimitry Andric auto maybeID = getContext().getELFUniqueIDForEntsize( 7165ffd83dbSDimitry Andric SectionName, Flags, EntrySize); 7175ffd83dbSDimitry Andric UniqueID = maybeID ? *maybeID : NextUniqueID++; 7185ffd83dbSDimitry Andric } 7195ffd83dbSDimitry Andric } 7205ffd83dbSDimitry Andric } else { 7215ffd83dbSDimitry Andric // If two symbols with differing sizes end up in the same mergeable 7225ffd83dbSDimitry Andric // section that section can be assigned an incorrect entry size. To avoid 7235ffd83dbSDimitry Andric // this we usually put symbols of the same size into distinct mergeable 7245ffd83dbSDimitry Andric // sections with the same name. Doing so relies on the ",unique ," 7255ffd83dbSDimitry Andric // assembly feature. This feature is not avalible until bintuils 7265ffd83dbSDimitry Andric // version 2.35 (https://sourceware.org/bugzilla/show_bug.cgi?id=25380). 7275ffd83dbSDimitry Andric Flags &= ~ELF::SHF_MERGE; 7285ffd83dbSDimitry Andric EntrySize = 0; 7295ffd83dbSDimitry Andric } 730*0b57cec5SDimitry Andric } 731*0b57cec5SDimitry Andric 732*0b57cec5SDimitry Andric MCSectionELF *Section = getContext().getELFSection( 733*0b57cec5SDimitry Andric SectionName, getELFSectionType(SectionName, Kind), Flags, 7345ffd83dbSDimitry Andric EntrySize, Group, UniqueID, LinkedToSym); 735*0b57cec5SDimitry Andric // Make sure that we did not get some other section with incompatible sh_link. 736*0b57cec5SDimitry Andric // This should not be possible due to UniqueID code above. 7375ffd83dbSDimitry Andric assert(Section->getLinkedToSymbol() == LinkedToSym && 738*0b57cec5SDimitry Andric "Associated symbol mismatch between sections"); 7395ffd83dbSDimitry Andric 740e8d8bef9SDimitry Andric if (!(getContext().getAsmInfo()->useIntegratedAssembler() || 741e8d8bef9SDimitry Andric getContext().getAsmInfo()->binutilsIsAtLeast(2, 35))) { 742e8d8bef9SDimitry Andric // If we are using GNU as before 2.35, then this symbol might have 7435ffd83dbSDimitry Andric // been placed in an incompatible mergeable section. Emit an error if this 7445ffd83dbSDimitry Andric // is the case to avoid creating broken output. 7455ffd83dbSDimitry Andric if ((Section->getFlags() & ELF::SHF_MERGE) && 7465ffd83dbSDimitry Andric (Section->getEntrySize() != getEntrySizeForKind(Kind))) 7475ffd83dbSDimitry Andric GO->getContext().diagnose(LoweringDiagnosticInfo( 7485ffd83dbSDimitry Andric "Symbol '" + GO->getName() + "' from module '" + 7495ffd83dbSDimitry Andric (GO->getParent() ? GO->getParent()->getSourceFileName() : "unknown") + 7505ffd83dbSDimitry Andric "' required a section with entry-size=" + 7515ffd83dbSDimitry Andric Twine(getEntrySizeForKind(Kind)) + " but was placed in section '" + 7525ffd83dbSDimitry Andric SectionName + "' with entry-size=" + Twine(Section->getEntrySize()) + 7535ffd83dbSDimitry Andric ": Explicit assignment by pragma or attribute of an incompatible " 7545ffd83dbSDimitry Andric "symbol to this section?")); 755*0b57cec5SDimitry Andric } 756*0b57cec5SDimitry Andric 7575ffd83dbSDimitry Andric return Section; 758*0b57cec5SDimitry Andric } 759*0b57cec5SDimitry Andric 760*0b57cec5SDimitry Andric static MCSectionELF *selectELFSectionForGlobal( 761*0b57cec5SDimitry Andric MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 762*0b57cec5SDimitry Andric const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, 763*0b57cec5SDimitry Andric unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { 764*0b57cec5SDimitry Andric 765*0b57cec5SDimitry Andric StringRef Group = ""; 766*0b57cec5SDimitry Andric if (const Comdat *C = getELFComdat(GO)) { 767*0b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 768*0b57cec5SDimitry Andric Group = C->getName(); 769*0b57cec5SDimitry Andric } 770*0b57cec5SDimitry Andric 771*0b57cec5SDimitry Andric // Get the section entry size based on the kind. 772*0b57cec5SDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 773*0b57cec5SDimitry Andric 7745ffd83dbSDimitry Andric bool UniqueSectionName = false; 775*0b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 776*0b57cec5SDimitry Andric if (EmitUniqueSection) { 777*0b57cec5SDimitry Andric if (TM.getUniqueSectionNames()) { 7785ffd83dbSDimitry Andric UniqueSectionName = true; 779*0b57cec5SDimitry Andric } else { 780*0b57cec5SDimitry Andric UniqueID = *NextUniqueID; 781*0b57cec5SDimitry Andric (*NextUniqueID)++; 782*0b57cec5SDimitry Andric } 783*0b57cec5SDimitry Andric } 7845ffd83dbSDimitry Andric SmallString<128> Name = getELFSectionNameForGlobal( 7855ffd83dbSDimitry Andric GO, Kind, Mang, TM, EntrySize, UniqueSectionName); 7865ffd83dbSDimitry Andric 787*0b57cec5SDimitry Andric // Use 0 as the unique ID for execute-only text. 788*0b57cec5SDimitry Andric if (Kind.isExecuteOnly()) 789*0b57cec5SDimitry Andric UniqueID = 0; 790*0b57cec5SDimitry Andric return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 791*0b57cec5SDimitry Andric EntrySize, Group, UniqueID, AssociatedSymbol); 792*0b57cec5SDimitry Andric } 793*0b57cec5SDimitry Andric 794*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 795*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 796*0b57cec5SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 797*0b57cec5SDimitry Andric 798*0b57cec5SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the 799*0b57cec5SDimitry Andric // global value to a uniqued section specifically for it. 800*0b57cec5SDimitry Andric bool EmitUniqueSection = false; 801*0b57cec5SDimitry Andric if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 802*0b57cec5SDimitry Andric if (Kind.isText()) 803*0b57cec5SDimitry Andric EmitUniqueSection = TM.getFunctionSections(); 804*0b57cec5SDimitry Andric else 805*0b57cec5SDimitry Andric EmitUniqueSection = TM.getDataSections(); 806*0b57cec5SDimitry Andric } 807*0b57cec5SDimitry Andric EmitUniqueSection |= GO->hasComdat(); 808*0b57cec5SDimitry Andric 8095ffd83dbSDimitry Andric const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); 8105ffd83dbSDimitry Andric if (LinkedToSym) { 811*0b57cec5SDimitry Andric EmitUniqueSection = true; 812*0b57cec5SDimitry Andric Flags |= ELF::SHF_LINK_ORDER; 813*0b57cec5SDimitry Andric } 814*0b57cec5SDimitry Andric 815*0b57cec5SDimitry Andric MCSectionELF *Section = selectELFSectionForGlobal( 816*0b57cec5SDimitry Andric getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, 8175ffd83dbSDimitry Andric &NextUniqueID, LinkedToSym); 8185ffd83dbSDimitry Andric assert(Section->getLinkedToSymbol() == LinkedToSym); 819*0b57cec5SDimitry Andric return Section; 820*0b57cec5SDimitry Andric } 821*0b57cec5SDimitry Andric 822*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 823*0b57cec5SDimitry Andric const Function &F, const TargetMachine &TM) const { 824*0b57cec5SDimitry Andric // If the function can be removed, produce a unique section so that 825*0b57cec5SDimitry Andric // the table doesn't prevent the removal. 826*0b57cec5SDimitry Andric const Comdat *C = F.getComdat(); 827*0b57cec5SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 828*0b57cec5SDimitry Andric if (!EmitUniqueSection) 829*0b57cec5SDimitry Andric return ReadOnlySection; 830*0b57cec5SDimitry Andric 831*0b57cec5SDimitry Andric return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 832*0b57cec5SDimitry Andric getMangler(), TM, EmitUniqueSection, 833*0b57cec5SDimitry Andric ELF::SHF_ALLOC, &NextUniqueID, 834*0b57cec5SDimitry Andric /* AssociatedSymbol */ nullptr); 835*0b57cec5SDimitry Andric } 836*0b57cec5SDimitry Andric 837e8d8bef9SDimitry Andric MCSection * 838e8d8bef9SDimitry Andric TargetLoweringObjectFileELF::getSectionForLSDA(const Function &F, 839e8d8bef9SDimitry Andric const TargetMachine &TM) const { 840e8d8bef9SDimitry Andric // If neither COMDAT nor function sections, use the monolithic LSDA section. 841e8d8bef9SDimitry Andric // Re-use this path if LSDASection is null as in the Arm EHABI. 842e8d8bef9SDimitry Andric if (!LSDASection || (!F.hasComdat() && !TM.getFunctionSections())) 843e8d8bef9SDimitry Andric return LSDASection; 844e8d8bef9SDimitry Andric 845e8d8bef9SDimitry Andric const auto *LSDA = cast<MCSectionELF>(LSDASection); 846e8d8bef9SDimitry Andric unsigned Flags = LSDA->getFlags(); 847e8d8bef9SDimitry Andric StringRef Group; 848e8d8bef9SDimitry Andric if (F.hasComdat()) { 849e8d8bef9SDimitry Andric Group = F.getComdat()->getName(); 850e8d8bef9SDimitry Andric Flags |= ELF::SHF_GROUP; 851e8d8bef9SDimitry Andric } 852e8d8bef9SDimitry Andric 853e8d8bef9SDimitry Andric // Append the function name as the suffix like GCC, assuming 854e8d8bef9SDimitry Andric // -funique-section-names applies to .gcc_except_table sections. 855e8d8bef9SDimitry Andric if (TM.getUniqueSectionNames()) 856e8d8bef9SDimitry Andric return getContext().getELFSection(LSDA->getName() + "." + F.getName(), 857e8d8bef9SDimitry Andric LSDA->getType(), Flags, 0, Group, 858e8d8bef9SDimitry Andric MCSection::NonUniqueID, nullptr); 859e8d8bef9SDimitry Andric 860e8d8bef9SDimitry Andric // Allocate a unique ID if function sections && (integrated assembler or GNU 861e8d8bef9SDimitry Andric // as>=2.35). Note we could use SHF_LINK_ORDER to facilitate --gc-sections but 862e8d8bef9SDimitry Andric // that would require that we know the linker is a modern LLD (12.0 or later). 863e8d8bef9SDimitry Andric // GNU ld as of 2.35 does not support mixed SHF_LINK_ORDER & 864e8d8bef9SDimitry Andric // non-SHF_LINK_ORDER components in an output section 865e8d8bef9SDimitry Andric // https://sourceware.org/bugzilla/show_bug.cgi?id=26256 866e8d8bef9SDimitry Andric unsigned ID = TM.getFunctionSections() && 867e8d8bef9SDimitry Andric getContext().getAsmInfo()->useIntegratedAssembler() 868e8d8bef9SDimitry Andric ? NextUniqueID++ 869e8d8bef9SDimitry Andric : MCSection::NonUniqueID; 870e8d8bef9SDimitry Andric return getContext().getELFSection(LSDA->getName(), LSDA->getType(), Flags, 0, 871e8d8bef9SDimitry Andric Group, ID, nullptr); 872e8d8bef9SDimitry Andric } 873e8d8bef9SDimitry Andric 874*0b57cec5SDimitry Andric bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 875*0b57cec5SDimitry Andric bool UsesLabelDifference, const Function &F) const { 876*0b57cec5SDimitry Andric // We can always create relative relocations, so use another section 877*0b57cec5SDimitry Andric // that can be marked non-executable. 878*0b57cec5SDimitry Andric return false; 879*0b57cec5SDimitry Andric } 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric /// Given a mergeable constant with the specified size and relocation 882*0b57cec5SDimitry Andric /// information, return a section that it should be placed in. 883*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 884*0b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 8855ffd83dbSDimitry Andric Align &Alignment) const { 886*0b57cec5SDimitry Andric if (Kind.isMergeableConst4() && MergeableConst4Section) 887*0b57cec5SDimitry Andric return MergeableConst4Section; 888*0b57cec5SDimitry Andric if (Kind.isMergeableConst8() && MergeableConst8Section) 889*0b57cec5SDimitry Andric return MergeableConst8Section; 890*0b57cec5SDimitry Andric if (Kind.isMergeableConst16() && MergeableConst16Section) 891*0b57cec5SDimitry Andric return MergeableConst16Section; 892*0b57cec5SDimitry Andric if (Kind.isMergeableConst32() && MergeableConst32Section) 893*0b57cec5SDimitry Andric return MergeableConst32Section; 894*0b57cec5SDimitry Andric if (Kind.isReadOnly()) 895*0b57cec5SDimitry Andric return ReadOnlySection; 896*0b57cec5SDimitry Andric 897*0b57cec5SDimitry Andric assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 898*0b57cec5SDimitry Andric return DataRelROSection; 899*0b57cec5SDimitry Andric } 900*0b57cec5SDimitry Andric 9015ffd83dbSDimitry Andric /// Returns a unique section for the given machine basic block. 9025ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock( 9035ffd83dbSDimitry Andric const Function &F, const MachineBasicBlock &MBB, 9045ffd83dbSDimitry Andric const TargetMachine &TM) const { 9055ffd83dbSDimitry Andric assert(MBB.isBeginSection() && "Basic block does not start a section!"); 9065ffd83dbSDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 9075ffd83dbSDimitry Andric 908e8d8bef9SDimitry Andric // For cold sections use the .text.split. prefix along with the parent 9095ffd83dbSDimitry Andric // function name. All cold blocks for the same function go to the same 9105ffd83dbSDimitry Andric // section. Similarly all exception blocks are grouped by symbol name 9115ffd83dbSDimitry Andric // under the .text.eh prefix. For regular sections, we either use a unique 9125ffd83dbSDimitry Andric // name, or a unique ID for the section. 9135ffd83dbSDimitry Andric SmallString<128> Name; 9145ffd83dbSDimitry Andric if (MBB.getSectionID() == MBBSectionID::ColdSectionID) { 915e8d8bef9SDimitry Andric Name += BBSectionsColdTextPrefix; 9165ffd83dbSDimitry Andric Name += MBB.getParent()->getName(); 9175ffd83dbSDimitry Andric } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) { 9185ffd83dbSDimitry Andric Name += ".text.eh."; 9195ffd83dbSDimitry Andric Name += MBB.getParent()->getName(); 9205ffd83dbSDimitry Andric } else { 9215ffd83dbSDimitry Andric Name += MBB.getParent()->getSection()->getName(); 9225ffd83dbSDimitry Andric if (TM.getUniqueBasicBlockSectionNames()) { 9235ffd83dbSDimitry Andric Name += "."; 9245ffd83dbSDimitry Andric Name += MBB.getSymbol()->getName(); 9255ffd83dbSDimitry Andric } else { 9265ffd83dbSDimitry Andric UniqueID = NextUniqueID++; 9275ffd83dbSDimitry Andric } 9285ffd83dbSDimitry Andric } 9295ffd83dbSDimitry Andric 9305ffd83dbSDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; 931e8d8bef9SDimitry Andric std::string GroupName; 9325ffd83dbSDimitry Andric if (F.hasComdat()) { 9335ffd83dbSDimitry Andric Flags |= ELF::SHF_GROUP; 9345ffd83dbSDimitry Andric GroupName = F.getComdat()->getName().str(); 9355ffd83dbSDimitry Andric } 9365ffd83dbSDimitry Andric return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 9375ffd83dbSDimitry Andric 0 /* Entry Size */, GroupName, UniqueID, 9385ffd83dbSDimitry Andric nullptr); 9395ffd83dbSDimitry Andric } 9405ffd83dbSDimitry Andric 941*0b57cec5SDimitry Andric static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 942*0b57cec5SDimitry Andric bool IsCtor, unsigned Priority, 943*0b57cec5SDimitry Andric const MCSymbol *KeySym) { 944*0b57cec5SDimitry Andric std::string Name; 945*0b57cec5SDimitry Andric unsigned Type; 946*0b57cec5SDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 947*0b57cec5SDimitry Andric StringRef COMDAT = KeySym ? KeySym->getName() : ""; 948*0b57cec5SDimitry Andric 949*0b57cec5SDimitry Andric if (KeySym) 950*0b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 951*0b57cec5SDimitry Andric 952*0b57cec5SDimitry Andric if (UseInitArray) { 953*0b57cec5SDimitry Andric if (IsCtor) { 954*0b57cec5SDimitry Andric Type = ELF::SHT_INIT_ARRAY; 955*0b57cec5SDimitry Andric Name = ".init_array"; 956*0b57cec5SDimitry Andric } else { 957*0b57cec5SDimitry Andric Type = ELF::SHT_FINI_ARRAY; 958*0b57cec5SDimitry Andric Name = ".fini_array"; 959*0b57cec5SDimitry Andric } 960*0b57cec5SDimitry Andric if (Priority != 65535) { 961*0b57cec5SDimitry Andric Name += '.'; 962*0b57cec5SDimitry Andric Name += utostr(Priority); 963*0b57cec5SDimitry Andric } 964*0b57cec5SDimitry Andric } else { 965*0b57cec5SDimitry Andric // The default scheme is .ctor / .dtor, so we have to invert the priority 966*0b57cec5SDimitry Andric // numbering. 967*0b57cec5SDimitry Andric if (IsCtor) 968*0b57cec5SDimitry Andric Name = ".ctors"; 969*0b57cec5SDimitry Andric else 970*0b57cec5SDimitry Andric Name = ".dtors"; 971*0b57cec5SDimitry Andric if (Priority != 65535) 972*0b57cec5SDimitry Andric raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 973*0b57cec5SDimitry Andric Type = ELF::SHT_PROGBITS; 974*0b57cec5SDimitry Andric } 975*0b57cec5SDimitry Andric 976*0b57cec5SDimitry Andric return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 977*0b57cec5SDimitry Andric } 978*0b57cec5SDimitry Andric 979*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 980*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 981*0b57cec5SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 982*0b57cec5SDimitry Andric KeySym); 983*0b57cec5SDimitry Andric } 984*0b57cec5SDimitry Andric 985*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 986*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 987*0b57cec5SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 988*0b57cec5SDimitry Andric KeySym); 989*0b57cec5SDimitry Andric } 990*0b57cec5SDimitry Andric 991*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 992*0b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 993*0b57cec5SDimitry Andric const TargetMachine &TM) const { 994*0b57cec5SDimitry Andric // We may only use a PLT-relative relocation to refer to unnamed_addr 995*0b57cec5SDimitry Andric // functions. 996*0b57cec5SDimitry Andric if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 997*0b57cec5SDimitry Andric return nullptr; 998*0b57cec5SDimitry Andric 999*0b57cec5SDimitry Andric // Basic sanity checks. 1000*0b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 1001*0b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 1002*0b57cec5SDimitry Andric RHS->isThreadLocal()) 1003*0b57cec5SDimitry Andric return nullptr; 1004*0b57cec5SDimitry Andric 1005*0b57cec5SDimitry Andric return MCBinaryExpr::createSub( 1006*0b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, 1007*0b57cec5SDimitry Andric getContext()), 1008*0b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 1009*0b57cec5SDimitry Andric } 1010*0b57cec5SDimitry Andric 1011e8d8bef9SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent( 1012e8d8bef9SDimitry Andric const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const { 1013e8d8bef9SDimitry Andric assert(supportDSOLocalEquivalentLowering()); 1014e8d8bef9SDimitry Andric 1015e8d8bef9SDimitry Andric const auto *GV = Equiv->getGlobalValue(); 1016e8d8bef9SDimitry Andric 1017e8d8bef9SDimitry Andric // A PLT entry is not needed for dso_local globals. 1018e8d8bef9SDimitry Andric if (GV->isDSOLocal() || GV->isImplicitDSOLocal()) 1019e8d8bef9SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); 1020e8d8bef9SDimitry Andric 1021e8d8bef9SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeVariantKind, 1022e8d8bef9SDimitry Andric getContext()); 1023e8d8bef9SDimitry Andric } 1024e8d8bef9SDimitry Andric 1025*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { 1026*0b57cec5SDimitry Andric // Use ".GCC.command.line" since this feature is to support clang's 1027*0b57cec5SDimitry Andric // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the 1028*0b57cec5SDimitry Andric // same name. 1029*0b57cec5SDimitry Andric return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, 1030*0b57cec5SDimitry Andric ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 1031*0b57cec5SDimitry Andric } 1032*0b57cec5SDimitry Andric 1033*0b57cec5SDimitry Andric void 1034*0b57cec5SDimitry Andric TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 1035*0b57cec5SDimitry Andric UseInitArray = UseInitArray_; 1036*0b57cec5SDimitry Andric MCContext &Ctx = getContext(); 1037*0b57cec5SDimitry Andric if (!UseInitArray) { 1038*0b57cec5SDimitry Andric StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, 1039*0b57cec5SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_WRITE); 1040*0b57cec5SDimitry Andric 1041*0b57cec5SDimitry Andric StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, 1042*0b57cec5SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_WRITE); 1043*0b57cec5SDimitry Andric return; 1044*0b57cec5SDimitry Andric } 1045*0b57cec5SDimitry Andric 1046*0b57cec5SDimitry Andric StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 1047*0b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC); 1048*0b57cec5SDimitry Andric StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 1049*0b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC); 1050*0b57cec5SDimitry Andric } 1051*0b57cec5SDimitry Andric 1052*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1053*0b57cec5SDimitry Andric // MachO 1054*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1055*0b57cec5SDimitry Andric 1056*0b57cec5SDimitry Andric TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 1057*0b57cec5SDimitry Andric : TargetLoweringObjectFile() { 1058*0b57cec5SDimitry Andric SupportIndirectSymViaGOTPCRel = true; 1059*0b57cec5SDimitry Andric } 1060*0b57cec5SDimitry Andric 1061*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 1062*0b57cec5SDimitry Andric const TargetMachine &TM) { 1063*0b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TM); 1064*0b57cec5SDimitry Andric if (TM.getRelocationModel() == Reloc::Static) { 1065*0b57cec5SDimitry Andric StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, 1066*0b57cec5SDimitry Andric SectionKind::getData()); 1067*0b57cec5SDimitry Andric StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, 1068*0b57cec5SDimitry Andric SectionKind::getData()); 1069*0b57cec5SDimitry Andric } else { 1070*0b57cec5SDimitry Andric StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", 1071*0b57cec5SDimitry Andric MachO::S_MOD_INIT_FUNC_POINTERS, 1072*0b57cec5SDimitry Andric SectionKind::getData()); 1073*0b57cec5SDimitry Andric StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", 1074*0b57cec5SDimitry Andric MachO::S_MOD_TERM_FUNC_POINTERS, 1075*0b57cec5SDimitry Andric SectionKind::getData()); 1076*0b57cec5SDimitry Andric } 1077*0b57cec5SDimitry Andric 1078*0b57cec5SDimitry Andric PersonalityEncoding = 1079*0b57cec5SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 1080*0b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel; 1081*0b57cec5SDimitry Andric TTypeEncoding = 1082*0b57cec5SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 1083*0b57cec5SDimitry Andric } 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, 1086*0b57cec5SDimitry Andric Module &M) const { 1087*0b57cec5SDimitry Andric // Emit the linker options if present. 1088*0b57cec5SDimitry Andric if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1089480093f4SDimitry Andric for (const auto *Option : LinkerOptions->operands()) { 1090*0b57cec5SDimitry Andric SmallVector<std::string, 4> StrOptions; 1091*0b57cec5SDimitry Andric for (const auto &Piece : cast<MDNode>(Option)->operands()) 10925ffd83dbSDimitry Andric StrOptions.push_back(std::string(cast<MDString>(Piece)->getString())); 10935ffd83dbSDimitry Andric Streamer.emitLinkerOptions(StrOptions); 1094*0b57cec5SDimitry Andric } 1095*0b57cec5SDimitry Andric } 1096*0b57cec5SDimitry Andric 1097*0b57cec5SDimitry Andric unsigned VersionVal = 0; 1098*0b57cec5SDimitry Andric unsigned ImageInfoFlags = 0; 1099*0b57cec5SDimitry Andric StringRef SectionVal; 1100*0b57cec5SDimitry Andric 1101*0b57cec5SDimitry Andric GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric // The section is mandatory. If we don't have it, then we don't have GC info. 1104*0b57cec5SDimitry Andric if (SectionVal.empty()) 1105*0b57cec5SDimitry Andric return; 1106*0b57cec5SDimitry Andric 1107*0b57cec5SDimitry Andric StringRef Segment, Section; 1108*0b57cec5SDimitry Andric unsigned TAA = 0, StubSize = 0; 1109*0b57cec5SDimitry Andric bool TAAParsed; 1110*0b57cec5SDimitry Andric std::string ErrorCode = 1111*0b57cec5SDimitry Andric MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 1112*0b57cec5SDimitry Andric TAA, TAAParsed, StubSize); 1113*0b57cec5SDimitry Andric if (!ErrorCode.empty()) 1114*0b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 1115*0b57cec5SDimitry Andric report_fatal_error("Invalid section specifier '" + Section + "': " + 1116*0b57cec5SDimitry Andric ErrorCode + "."); 1117*0b57cec5SDimitry Andric 1118*0b57cec5SDimitry Andric // Get the section. 1119*0b57cec5SDimitry Andric MCSectionMachO *S = getContext().getMachOSection( 1120*0b57cec5SDimitry Andric Segment, Section, TAA, StubSize, SectionKind::getData()); 1121*0b57cec5SDimitry Andric Streamer.SwitchSection(S); 11225ffd83dbSDimitry Andric Streamer.emitLabel(getContext(). 1123*0b57cec5SDimitry Andric getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 11245ffd83dbSDimitry Andric Streamer.emitInt32(VersionVal); 11255ffd83dbSDimitry Andric Streamer.emitInt32(ImageInfoFlags); 1126*0b57cec5SDimitry Andric Streamer.AddBlankLine(); 1127*0b57cec5SDimitry Andric } 1128*0b57cec5SDimitry Andric 1129*0b57cec5SDimitry Andric static void checkMachOComdat(const GlobalValue *GV) { 1130*0b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 1131*0b57cec5SDimitry Andric if (!C) 1132*0b57cec5SDimitry Andric return; 1133*0b57cec5SDimitry Andric 1134*0b57cec5SDimitry Andric report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 1135*0b57cec5SDimitry Andric "' cannot be lowered."); 1136*0b57cec5SDimitry Andric } 1137*0b57cec5SDimitry Andric 1138*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 1139*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1140*0b57cec5SDimitry Andric // Parse the section specifier and create it if valid. 1141*0b57cec5SDimitry Andric StringRef Segment, Section; 1142*0b57cec5SDimitry Andric unsigned TAA = 0, StubSize = 0; 1143*0b57cec5SDimitry Andric bool TAAParsed; 1144*0b57cec5SDimitry Andric 1145*0b57cec5SDimitry Andric checkMachOComdat(GO); 1146*0b57cec5SDimitry Andric 1147*0b57cec5SDimitry Andric std::string ErrorCode = 1148*0b57cec5SDimitry Andric MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section, 1149*0b57cec5SDimitry Andric TAA, TAAParsed, StubSize); 1150*0b57cec5SDimitry Andric if (!ErrorCode.empty()) { 1151*0b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 1152*0b57cec5SDimitry Andric report_fatal_error("Global variable '" + GO->getName() + 1153*0b57cec5SDimitry Andric "' has an invalid section specifier '" + 1154*0b57cec5SDimitry Andric GO->getSection() + "': " + ErrorCode + "."); 1155*0b57cec5SDimitry Andric } 1156*0b57cec5SDimitry Andric 1157*0b57cec5SDimitry Andric // Get the section. 1158*0b57cec5SDimitry Andric MCSectionMachO *S = 1159*0b57cec5SDimitry Andric getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 1160*0b57cec5SDimitry Andric 1161*0b57cec5SDimitry Andric // If TAA wasn't set by ParseSectionSpecifier() above, 1162*0b57cec5SDimitry Andric // use the value returned by getMachOSection() as a default. 1163*0b57cec5SDimitry Andric if (!TAAParsed) 1164*0b57cec5SDimitry Andric TAA = S->getTypeAndAttributes(); 1165*0b57cec5SDimitry Andric 1166*0b57cec5SDimitry Andric // Okay, now that we got the section, verify that the TAA & StubSize agree. 1167*0b57cec5SDimitry Andric // If the user declared multiple globals with different section flags, we need 1168*0b57cec5SDimitry Andric // to reject it here. 1169*0b57cec5SDimitry Andric if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 1170*0b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 1171*0b57cec5SDimitry Andric report_fatal_error("Global variable '" + GO->getName() + 1172*0b57cec5SDimitry Andric "' section type or attributes does not match previous" 1173*0b57cec5SDimitry Andric " section specifier"); 1174*0b57cec5SDimitry Andric } 1175*0b57cec5SDimitry Andric 1176*0b57cec5SDimitry Andric return S; 1177*0b57cec5SDimitry Andric } 1178*0b57cec5SDimitry Andric 1179*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 1180*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1181*0b57cec5SDimitry Andric checkMachOComdat(GO); 1182*0b57cec5SDimitry Andric 1183*0b57cec5SDimitry Andric // Handle thread local data. 1184*0b57cec5SDimitry Andric if (Kind.isThreadBSS()) return TLSBSSSection; 1185*0b57cec5SDimitry Andric if (Kind.isThreadData()) return TLSDataSection; 1186*0b57cec5SDimitry Andric 1187*0b57cec5SDimitry Andric if (Kind.isText()) 1188*0b57cec5SDimitry Andric return GO->isWeakForLinker() ? TextCoalSection : TextSection; 1189*0b57cec5SDimitry Andric 1190*0b57cec5SDimitry Andric // If this is weak/linkonce, put this in a coalescable section, either in text 1191*0b57cec5SDimitry Andric // or data depending on if it is writable. 1192*0b57cec5SDimitry Andric if (GO->isWeakForLinker()) { 1193*0b57cec5SDimitry Andric if (Kind.isReadOnly()) 1194*0b57cec5SDimitry Andric return ConstTextCoalSection; 1195*0b57cec5SDimitry Andric if (Kind.isReadOnlyWithRel()) 1196*0b57cec5SDimitry Andric return ConstDataCoalSection; 1197*0b57cec5SDimitry Andric return DataCoalSection; 1198*0b57cec5SDimitry Andric } 1199*0b57cec5SDimitry Andric 1200*0b57cec5SDimitry Andric // FIXME: Alignment check should be handled by section classifier. 1201*0b57cec5SDimitry Andric if (Kind.isMergeable1ByteCString() && 12025ffd83dbSDimitry Andric GO->getParent()->getDataLayout().getPreferredAlign( 12035ffd83dbSDimitry Andric cast<GlobalVariable>(GO)) < Align(32)) 1204*0b57cec5SDimitry Andric return CStringSection; 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric // Do not put 16-bit arrays in the UString section if they have an 1207*0b57cec5SDimitry Andric // externally visible label, this runs into issues with certain linker 1208*0b57cec5SDimitry Andric // versions. 1209*0b57cec5SDimitry Andric if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && 12105ffd83dbSDimitry Andric GO->getParent()->getDataLayout().getPreferredAlign( 12115ffd83dbSDimitry Andric cast<GlobalVariable>(GO)) < Align(32)) 1212*0b57cec5SDimitry Andric return UStringSection; 1213*0b57cec5SDimitry Andric 1214*0b57cec5SDimitry Andric // With MachO only variables whose corresponding symbol starts with 'l' or 1215*0b57cec5SDimitry Andric // 'L' can be merged, so we only try merging GVs with private linkage. 1216*0b57cec5SDimitry Andric if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { 1217*0b57cec5SDimitry Andric if (Kind.isMergeableConst4()) 1218*0b57cec5SDimitry Andric return FourByteConstantSection; 1219*0b57cec5SDimitry Andric if (Kind.isMergeableConst8()) 1220*0b57cec5SDimitry Andric return EightByteConstantSection; 1221*0b57cec5SDimitry Andric if (Kind.isMergeableConst16()) 1222*0b57cec5SDimitry Andric return SixteenByteConstantSection; 1223*0b57cec5SDimitry Andric } 1224*0b57cec5SDimitry Andric 1225*0b57cec5SDimitry Andric // Otherwise, if it is readonly, but not something we can specially optimize, 1226*0b57cec5SDimitry Andric // just drop it in .const. 1227*0b57cec5SDimitry Andric if (Kind.isReadOnly()) 1228*0b57cec5SDimitry Andric return ReadOnlySection; 1229*0b57cec5SDimitry Andric 1230*0b57cec5SDimitry Andric // If this is marked const, put it into a const section. But if the dynamic 1231*0b57cec5SDimitry Andric // linker needs to write to it, put it in the data segment. 1232*0b57cec5SDimitry Andric if (Kind.isReadOnlyWithRel()) 1233*0b57cec5SDimitry Andric return ConstDataSection; 1234*0b57cec5SDimitry Andric 1235*0b57cec5SDimitry Andric // Put zero initialized globals with strong external linkage in the 1236*0b57cec5SDimitry Andric // DATA, __common section with the .zerofill directive. 1237*0b57cec5SDimitry Andric if (Kind.isBSSExtern()) 1238*0b57cec5SDimitry Andric return DataCommonSection; 1239*0b57cec5SDimitry Andric 1240*0b57cec5SDimitry Andric // Put zero initialized globals with local linkage in __DATA,__bss directive 1241*0b57cec5SDimitry Andric // with the .zerofill directive (aka .lcomm). 1242*0b57cec5SDimitry Andric if (Kind.isBSSLocal()) 1243*0b57cec5SDimitry Andric return DataBSSSection; 1244*0b57cec5SDimitry Andric 1245*0b57cec5SDimitry Andric // Otherwise, just drop the variable in the normal data section. 1246*0b57cec5SDimitry Andric return DataSection; 1247*0b57cec5SDimitry Andric } 1248*0b57cec5SDimitry Andric 1249*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 1250*0b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 12515ffd83dbSDimitry Andric Align &Alignment) const { 1252*0b57cec5SDimitry Andric // If this constant requires a relocation, we have to put it in the data 1253*0b57cec5SDimitry Andric // segment, not in the text segment. 1254*0b57cec5SDimitry Andric if (Kind.isData() || Kind.isReadOnlyWithRel()) 1255*0b57cec5SDimitry Andric return ConstDataSection; 1256*0b57cec5SDimitry Andric 1257*0b57cec5SDimitry Andric if (Kind.isMergeableConst4()) 1258*0b57cec5SDimitry Andric return FourByteConstantSection; 1259*0b57cec5SDimitry Andric if (Kind.isMergeableConst8()) 1260*0b57cec5SDimitry Andric return EightByteConstantSection; 1261*0b57cec5SDimitry Andric if (Kind.isMergeableConst16()) 1262*0b57cec5SDimitry Andric return SixteenByteConstantSection; 1263*0b57cec5SDimitry Andric return ReadOnlySection; // .const 1264*0b57cec5SDimitry Andric } 1265*0b57cec5SDimitry Andric 1266*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 1267*0b57cec5SDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 1268*0b57cec5SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1269*0b57cec5SDimitry Andric // The mach-o version of this method defaults to returning a stub reference. 1270*0b57cec5SDimitry Andric 1271*0b57cec5SDimitry Andric if (Encoding & DW_EH_PE_indirect) { 1272*0b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 1273*0b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1274*0b57cec5SDimitry Andric 1275*0b57cec5SDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1276*0b57cec5SDimitry Andric 1277*0b57cec5SDimitry Andric // Add information about the stub reference to MachOMMI so that the stub 1278*0b57cec5SDimitry Andric // gets emitted by the asmprinter. 1279*0b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1280*0b57cec5SDimitry Andric if (!StubSym.getPointer()) { 1281*0b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(GV); 1282*0b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1283*0b57cec5SDimitry Andric } 1284*0b57cec5SDimitry Andric 1285*0b57cec5SDimitry Andric return TargetLoweringObjectFile:: 1286*0b57cec5SDimitry Andric getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 1287*0b57cec5SDimitry Andric Encoding & ~DW_EH_PE_indirect, Streamer); 1288*0b57cec5SDimitry Andric } 1289*0b57cec5SDimitry Andric 1290*0b57cec5SDimitry Andric return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 1291*0b57cec5SDimitry Andric MMI, Streamer); 1292*0b57cec5SDimitry Andric } 1293*0b57cec5SDimitry Andric 1294*0b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 1295*0b57cec5SDimitry Andric const GlobalValue *GV, const TargetMachine &TM, 1296*0b57cec5SDimitry Andric MachineModuleInfo *MMI) const { 1297*0b57cec5SDimitry Andric // The mach-o version of this method defaults to returning a stub reference. 1298*0b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 1299*0b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1300*0b57cec5SDimitry Andric 1301*0b57cec5SDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric // Add information about the stub reference to MachOMMI so that the stub 1304*0b57cec5SDimitry Andric // gets emitted by the asmprinter. 1305*0b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1306*0b57cec5SDimitry Andric if (!StubSym.getPointer()) { 1307*0b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(GV); 1308*0b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1309*0b57cec5SDimitry Andric } 1310*0b57cec5SDimitry Andric 1311*0b57cec5SDimitry Andric return SSym; 1312*0b57cec5SDimitry Andric } 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 13158bcb0991SDimitry Andric const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, 13168bcb0991SDimitry Andric int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1317*0b57cec5SDimitry Andric // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 1318*0b57cec5SDimitry Andric // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 1319*0b57cec5SDimitry Andric // through a non_lazy_ptr stub instead. One advantage is that it allows the 1320*0b57cec5SDimitry Andric // computation of deltas to final external symbols. Example: 1321*0b57cec5SDimitry Andric // 1322*0b57cec5SDimitry Andric // _extgotequiv: 1323*0b57cec5SDimitry Andric // .long _extfoo 1324*0b57cec5SDimitry Andric // 1325*0b57cec5SDimitry Andric // _delta: 1326*0b57cec5SDimitry Andric // .long _extgotequiv-_delta 1327*0b57cec5SDimitry Andric // 1328*0b57cec5SDimitry Andric // is transformed to: 1329*0b57cec5SDimitry Andric // 1330*0b57cec5SDimitry Andric // _delta: 1331*0b57cec5SDimitry Andric // .long L_extfoo$non_lazy_ptr-(_delta+0) 1332*0b57cec5SDimitry Andric // 1333*0b57cec5SDimitry Andric // .section __IMPORT,__pointers,non_lazy_symbol_pointers 1334*0b57cec5SDimitry Andric // L_extfoo$non_lazy_ptr: 1335*0b57cec5SDimitry Andric // .indirect_symbol _extfoo 1336*0b57cec5SDimitry Andric // .long 0 1337*0b57cec5SDimitry Andric // 1338*0b57cec5SDimitry Andric // The indirect symbol table (and sections of non_lazy_symbol_pointers type) 1339*0b57cec5SDimitry Andric // may point to both local (same translation unit) and global (other 1340*0b57cec5SDimitry Andric // translation units) symbols. Example: 1341*0b57cec5SDimitry Andric // 1342*0b57cec5SDimitry Andric // .section __DATA,__pointers,non_lazy_symbol_pointers 1343*0b57cec5SDimitry Andric // L1: 1344*0b57cec5SDimitry Andric // .indirect_symbol _myGlobal 1345*0b57cec5SDimitry Andric // .long 0 1346*0b57cec5SDimitry Andric // L2: 1347*0b57cec5SDimitry Andric // .indirect_symbol _myLocal 1348*0b57cec5SDimitry Andric // .long _myLocal 1349*0b57cec5SDimitry Andric // 1350*0b57cec5SDimitry Andric // If the symbol is local, instead of the symbol's index, the assembler 1351*0b57cec5SDimitry Andric // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. 1352*0b57cec5SDimitry Andric // Then the linker will notice the constant in the table and will look at the 1353*0b57cec5SDimitry Andric // content of the symbol. 1354*0b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 1355*0b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1356*0b57cec5SDimitry Andric MCContext &Ctx = getContext(); 1357*0b57cec5SDimitry Andric 1358*0b57cec5SDimitry Andric // The offset must consider the original displacement from the base symbol 1359*0b57cec5SDimitry Andric // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 1360*0b57cec5SDimitry Andric Offset = -MV.getConstant(); 1361*0b57cec5SDimitry Andric const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 1362*0b57cec5SDimitry Andric 1363*0b57cec5SDimitry Andric // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 1364*0b57cec5SDimitry Andric // non_lazy_ptr stubs. 1365*0b57cec5SDimitry Andric SmallString<128> Name; 1366*0b57cec5SDimitry Andric StringRef Suffix = "$non_lazy_ptr"; 1367*0b57cec5SDimitry Andric Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 1368*0b57cec5SDimitry Andric Name += Sym->getName(); 1369*0b57cec5SDimitry Andric Name += Suffix; 1370*0b57cec5SDimitry Andric MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 1371*0b57cec5SDimitry Andric 1372*0b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 13738bcb0991SDimitry Andric 13748bcb0991SDimitry Andric if (!StubSym.getPointer()) 1375*0b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), 13768bcb0991SDimitry Andric !GV->hasLocalLinkage()); 1377*0b57cec5SDimitry Andric 1378*0b57cec5SDimitry Andric const MCExpr *BSymExpr = 1379*0b57cec5SDimitry Andric MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 1380*0b57cec5SDimitry Andric const MCExpr *LHS = 1381*0b57cec5SDimitry Andric MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 1382*0b57cec5SDimitry Andric 1383*0b57cec5SDimitry Andric if (!Offset) 1384*0b57cec5SDimitry Andric return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 1385*0b57cec5SDimitry Andric 1386*0b57cec5SDimitry Andric const MCExpr *RHS = 1387*0b57cec5SDimitry Andric MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 1388*0b57cec5SDimitry Andric return MCBinaryExpr::createSub(LHS, RHS, Ctx); 1389*0b57cec5SDimitry Andric } 1390*0b57cec5SDimitry Andric 1391*0b57cec5SDimitry Andric static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 1392*0b57cec5SDimitry Andric const MCSection &Section) { 1393*0b57cec5SDimitry Andric if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 1394*0b57cec5SDimitry Andric return true; 1395*0b57cec5SDimitry Andric 1396*0b57cec5SDimitry Andric // If it is not dead stripped, it is safe to use private labels. 1397*0b57cec5SDimitry Andric const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 1398*0b57cec5SDimitry Andric if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 1399*0b57cec5SDimitry Andric return true; 1400*0b57cec5SDimitry Andric 1401*0b57cec5SDimitry Andric return false; 1402*0b57cec5SDimitry Andric } 1403*0b57cec5SDimitry Andric 1404*0b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::getNameWithPrefix( 1405*0b57cec5SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1406*0b57cec5SDimitry Andric const TargetMachine &TM) const { 1407*0b57cec5SDimitry Andric bool CannotUsePrivateLabel = true; 1408*0b57cec5SDimitry Andric if (auto *GO = GV->getBaseObject()) { 1409*0b57cec5SDimitry Andric SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); 1410*0b57cec5SDimitry Andric const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); 1411*0b57cec5SDimitry Andric CannotUsePrivateLabel = 1412*0b57cec5SDimitry Andric !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 1413*0b57cec5SDimitry Andric } 1414*0b57cec5SDimitry Andric getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1415*0b57cec5SDimitry Andric } 1416*0b57cec5SDimitry Andric 1417*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1418*0b57cec5SDimitry Andric // COFF 1419*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric static unsigned 1422*0b57cec5SDimitry Andric getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { 1423*0b57cec5SDimitry Andric unsigned Flags = 0; 1424*0b57cec5SDimitry Andric bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; 1425*0b57cec5SDimitry Andric 1426*0b57cec5SDimitry Andric if (K.isMetadata()) 1427*0b57cec5SDimitry Andric Flags |= 1428*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_DISCARDABLE; 1429*0b57cec5SDimitry Andric else if (K.isText()) 1430*0b57cec5SDimitry Andric Flags |= 1431*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_EXECUTE | 1432*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1433*0b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_CODE | 1434*0b57cec5SDimitry Andric (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); 1435*0b57cec5SDimitry Andric else if (K.isBSS()) 1436*0b57cec5SDimitry Andric Flags |= 1437*0b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 1438*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1439*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 1440*0b57cec5SDimitry Andric else if (K.isThreadLocal()) 1441*0b57cec5SDimitry Andric Flags |= 1442*0b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1443*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1444*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 1445*0b57cec5SDimitry Andric else if (K.isReadOnly() || K.isReadOnlyWithRel()) 1446*0b57cec5SDimitry Andric Flags |= 1447*0b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1448*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ; 1449*0b57cec5SDimitry Andric else if (K.isWriteable()) 1450*0b57cec5SDimitry Andric Flags |= 1451*0b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1452*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1453*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric return Flags; 1456*0b57cec5SDimitry Andric } 1457*0b57cec5SDimitry Andric 1458*0b57cec5SDimitry Andric static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 1459*0b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 1460*0b57cec5SDimitry Andric assert(C && "expected GV to have a Comdat!"); 1461*0b57cec5SDimitry Andric 1462*0b57cec5SDimitry Andric StringRef ComdatGVName = C->getName(); 1463*0b57cec5SDimitry Andric const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 1464*0b57cec5SDimitry Andric if (!ComdatGV) 1465*0b57cec5SDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1466*0b57cec5SDimitry Andric "' does not exist."); 1467*0b57cec5SDimitry Andric 1468*0b57cec5SDimitry Andric if (ComdatGV->getComdat() != C) 1469*0b57cec5SDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1470*0b57cec5SDimitry Andric "' is not a key for its COMDAT."); 1471*0b57cec5SDimitry Andric 1472*0b57cec5SDimitry Andric return ComdatGV; 1473*0b57cec5SDimitry Andric } 1474*0b57cec5SDimitry Andric 1475*0b57cec5SDimitry Andric static int getSelectionForCOFF(const GlobalValue *GV) { 1476*0b57cec5SDimitry Andric if (const Comdat *C = GV->getComdat()) { 1477*0b57cec5SDimitry Andric const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 1478*0b57cec5SDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 1479*0b57cec5SDimitry Andric ComdatKey = GA->getBaseObject(); 1480*0b57cec5SDimitry Andric if (ComdatKey == GV) { 1481*0b57cec5SDimitry Andric switch (C->getSelectionKind()) { 1482*0b57cec5SDimitry Andric case Comdat::Any: 1483*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ANY; 1484*0b57cec5SDimitry Andric case Comdat::ExactMatch: 1485*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 1486*0b57cec5SDimitry Andric case Comdat::Largest: 1487*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_LARGEST; 1488*0b57cec5SDimitry Andric case Comdat::NoDuplicates: 1489*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1490*0b57cec5SDimitry Andric case Comdat::SameSize: 1491*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 1492*0b57cec5SDimitry Andric } 1493*0b57cec5SDimitry Andric } else { 1494*0b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 1495*0b57cec5SDimitry Andric } 1496*0b57cec5SDimitry Andric } 1497*0b57cec5SDimitry Andric return 0; 1498*0b57cec5SDimitry Andric } 1499*0b57cec5SDimitry Andric 1500*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 1501*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1502*0b57cec5SDimitry Andric int Selection = 0; 1503*0b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1504*0b57cec5SDimitry Andric StringRef Name = GO->getSection(); 1505*0b57cec5SDimitry Andric StringRef COMDATSymName = ""; 1506*0b57cec5SDimitry Andric if (GO->hasComdat()) { 1507*0b57cec5SDimitry Andric Selection = getSelectionForCOFF(GO); 1508*0b57cec5SDimitry Andric const GlobalValue *ComdatGV; 1509*0b57cec5SDimitry Andric if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 1510*0b57cec5SDimitry Andric ComdatGV = getComdatGVForCOFF(GO); 1511*0b57cec5SDimitry Andric else 1512*0b57cec5SDimitry Andric ComdatGV = GO; 1513*0b57cec5SDimitry Andric 1514*0b57cec5SDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 1515*0b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV); 1516*0b57cec5SDimitry Andric COMDATSymName = Sym->getName(); 1517*0b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1518*0b57cec5SDimitry Andric } else { 1519*0b57cec5SDimitry Andric Selection = 0; 1520*0b57cec5SDimitry Andric } 1521*0b57cec5SDimitry Andric } 1522*0b57cec5SDimitry Andric 1523*0b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1524*0b57cec5SDimitry Andric Selection); 1525*0b57cec5SDimitry Andric } 1526*0b57cec5SDimitry Andric 1527*0b57cec5SDimitry Andric static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 1528*0b57cec5SDimitry Andric if (Kind.isText()) 1529*0b57cec5SDimitry Andric return ".text"; 1530*0b57cec5SDimitry Andric if (Kind.isBSS()) 1531*0b57cec5SDimitry Andric return ".bss"; 1532*0b57cec5SDimitry Andric if (Kind.isThreadLocal()) 1533*0b57cec5SDimitry Andric return ".tls$"; 1534*0b57cec5SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1535*0b57cec5SDimitry Andric return ".rdata"; 1536*0b57cec5SDimitry Andric return ".data"; 1537*0b57cec5SDimitry Andric } 1538*0b57cec5SDimitry Andric 1539*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 1540*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1541*0b57cec5SDimitry Andric // If we have -ffunction-sections then we should emit the global value to a 1542*0b57cec5SDimitry Andric // uniqued section specifically for it. 1543*0b57cec5SDimitry Andric bool EmitUniquedSection; 1544*0b57cec5SDimitry Andric if (Kind.isText()) 1545*0b57cec5SDimitry Andric EmitUniquedSection = TM.getFunctionSections(); 1546*0b57cec5SDimitry Andric else 1547*0b57cec5SDimitry Andric EmitUniquedSection = TM.getDataSections(); 1548*0b57cec5SDimitry Andric 1549*0b57cec5SDimitry Andric if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { 1550*0b57cec5SDimitry Andric SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); 1551*0b57cec5SDimitry Andric 1552*0b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1553*0b57cec5SDimitry Andric 1554*0b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1555*0b57cec5SDimitry Andric int Selection = getSelectionForCOFF(GO); 1556*0b57cec5SDimitry Andric if (!Selection) 1557*0b57cec5SDimitry Andric Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1558*0b57cec5SDimitry Andric const GlobalValue *ComdatGV; 1559*0b57cec5SDimitry Andric if (GO->hasComdat()) 1560*0b57cec5SDimitry Andric ComdatGV = getComdatGVForCOFF(GO); 1561*0b57cec5SDimitry Andric else 1562*0b57cec5SDimitry Andric ComdatGV = GO; 1563*0b57cec5SDimitry Andric 1564*0b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 1565*0b57cec5SDimitry Andric if (EmitUniquedSection) 1566*0b57cec5SDimitry Andric UniqueID = NextUniqueID++; 1567*0b57cec5SDimitry Andric 1568*0b57cec5SDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 1569*0b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV); 1570*0b57cec5SDimitry Andric StringRef COMDATSymName = Sym->getName(); 1571*0b57cec5SDimitry Andric 1572e8d8bef9SDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) 1573e8d8bef9SDimitry Andric if (Optional<StringRef> Prefix = F->getSectionPrefix()) 1574e8d8bef9SDimitry Andric raw_svector_ostream(Name) << '$' << *Prefix; 1575e8d8bef9SDimitry Andric 1576*0b57cec5SDimitry Andric // Append "$symbol" to the section name *before* IR-level mangling is 1577*0b57cec5SDimitry Andric // applied when targetting mingw. This is what GCC does, and the ld.bfd 1578*0b57cec5SDimitry Andric // COFF linker will not properly handle comdats otherwise. 1579*0b57cec5SDimitry Andric if (getTargetTriple().isWindowsGNUEnvironment()) 1580*0b57cec5SDimitry Andric raw_svector_ostream(Name) << '$' << ComdatGV->getName(); 1581*0b57cec5SDimitry Andric 1582*0b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, 1583*0b57cec5SDimitry Andric COMDATSymName, Selection, UniqueID); 1584*0b57cec5SDimitry Andric } else { 1585*0b57cec5SDimitry Andric SmallString<256> TmpData; 1586*0b57cec5SDimitry Andric getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); 1587*0b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 1588*0b57cec5SDimitry Andric Selection, UniqueID); 1589*0b57cec5SDimitry Andric } 1590*0b57cec5SDimitry Andric } 1591*0b57cec5SDimitry Andric 1592*0b57cec5SDimitry Andric if (Kind.isText()) 1593*0b57cec5SDimitry Andric return TextSection; 1594*0b57cec5SDimitry Andric 1595*0b57cec5SDimitry Andric if (Kind.isThreadLocal()) 1596*0b57cec5SDimitry Andric return TLSDataSection; 1597*0b57cec5SDimitry Andric 1598*0b57cec5SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1599*0b57cec5SDimitry Andric return ReadOnlySection; 1600*0b57cec5SDimitry Andric 1601*0b57cec5SDimitry Andric // Note: we claim that common symbols are put in BSSSection, but they are 1602*0b57cec5SDimitry Andric // really emitted with the magic .comm directive, which creates a symbol table 1603*0b57cec5SDimitry Andric // entry but not a section. 1604*0b57cec5SDimitry Andric if (Kind.isBSS() || Kind.isCommon()) 1605*0b57cec5SDimitry Andric return BSSSection; 1606*0b57cec5SDimitry Andric 1607*0b57cec5SDimitry Andric return DataSection; 1608*0b57cec5SDimitry Andric } 1609*0b57cec5SDimitry Andric 1610*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1611*0b57cec5SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1612*0b57cec5SDimitry Andric const TargetMachine &TM) const { 1613*0b57cec5SDimitry Andric bool CannotUsePrivateLabel = false; 1614*0b57cec5SDimitry Andric if (GV->hasPrivateLinkage() && 1615*0b57cec5SDimitry Andric ((isa<Function>(GV) && TM.getFunctionSections()) || 1616*0b57cec5SDimitry Andric (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1617*0b57cec5SDimitry Andric CannotUsePrivateLabel = true; 1618*0b57cec5SDimitry Andric 1619*0b57cec5SDimitry Andric getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1620*0b57cec5SDimitry Andric } 1621*0b57cec5SDimitry Andric 1622*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1623*0b57cec5SDimitry Andric const Function &F, const TargetMachine &TM) const { 1624*0b57cec5SDimitry Andric // If the function can be removed, produce a unique section so that 1625*0b57cec5SDimitry Andric // the table doesn't prevent the removal. 1626*0b57cec5SDimitry Andric const Comdat *C = F.getComdat(); 1627*0b57cec5SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 1628*0b57cec5SDimitry Andric if (!EmitUniqueSection) 1629*0b57cec5SDimitry Andric return ReadOnlySection; 1630*0b57cec5SDimitry Andric 1631*0b57cec5SDimitry Andric // FIXME: we should produce a symbol for F instead. 1632*0b57cec5SDimitry Andric if (F.hasPrivateLinkage()) 1633*0b57cec5SDimitry Andric return ReadOnlySection; 1634*0b57cec5SDimitry Andric 1635*0b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(&F); 1636*0b57cec5SDimitry Andric StringRef COMDATSymName = Sym->getName(); 1637*0b57cec5SDimitry Andric 1638*0b57cec5SDimitry Andric SectionKind Kind = SectionKind::getReadOnly(); 1639*0b57cec5SDimitry Andric StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); 1640*0b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1641*0b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1642*0b57cec5SDimitry Andric unsigned UniqueID = NextUniqueID++; 1643*0b57cec5SDimitry Andric 1644*0b57cec5SDimitry Andric return getContext().getCOFFSection( 1645*0b57cec5SDimitry Andric SecName, Characteristics, Kind, COMDATSymName, 1646*0b57cec5SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); 1647*0b57cec5SDimitry Andric } 1648*0b57cec5SDimitry Andric 1649*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, 1650*0b57cec5SDimitry Andric Module &M) const { 1651e8d8bef9SDimitry Andric emitLinkerDirectives(Streamer, M); 1652e8d8bef9SDimitry Andric 1653e8d8bef9SDimitry Andric unsigned Version = 0; 1654e8d8bef9SDimitry Andric unsigned Flags = 0; 1655e8d8bef9SDimitry Andric StringRef Section; 1656e8d8bef9SDimitry Andric 1657e8d8bef9SDimitry Andric GetObjCImageInfo(M, Version, Flags, Section); 1658e8d8bef9SDimitry Andric if (!Section.empty()) { 1659e8d8bef9SDimitry Andric auto &C = getContext(); 1660e8d8bef9SDimitry Andric auto *S = C.getCOFFSection(Section, 1661e8d8bef9SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1662e8d8bef9SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 1663e8d8bef9SDimitry Andric SectionKind::getReadOnly()); 1664e8d8bef9SDimitry Andric Streamer.SwitchSection(S); 1665e8d8bef9SDimitry Andric Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 1666e8d8bef9SDimitry Andric Streamer.emitInt32(Version); 1667e8d8bef9SDimitry Andric Streamer.emitInt32(Flags); 1668e8d8bef9SDimitry Andric Streamer.AddBlankLine(); 1669e8d8bef9SDimitry Andric } 1670e8d8bef9SDimitry Andric 1671e8d8bef9SDimitry Andric emitCGProfileMetadata(Streamer, M); 1672e8d8bef9SDimitry Andric } 1673e8d8bef9SDimitry Andric 1674e8d8bef9SDimitry Andric void TargetLoweringObjectFileCOFF::emitLinkerDirectives( 1675e8d8bef9SDimitry Andric MCStreamer &Streamer, Module &M) const { 1676*0b57cec5SDimitry Andric if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1677*0b57cec5SDimitry Andric // Emit the linker options to the linker .drectve section. According to the 1678*0b57cec5SDimitry Andric // spec, this section is a space-separated string containing flags for 1679*0b57cec5SDimitry Andric // linker. 1680*0b57cec5SDimitry Andric MCSection *Sec = getDrectveSection(); 1681*0b57cec5SDimitry Andric Streamer.SwitchSection(Sec); 1682480093f4SDimitry Andric for (const auto *Option : LinkerOptions->operands()) { 1683*0b57cec5SDimitry Andric for (const auto &Piece : cast<MDNode>(Option)->operands()) { 1684*0b57cec5SDimitry Andric // Lead with a space for consistency with our dllexport implementation. 1685*0b57cec5SDimitry Andric std::string Directive(" "); 16865ffd83dbSDimitry Andric Directive.append(std::string(cast<MDString>(Piece)->getString())); 16875ffd83dbSDimitry Andric Streamer.emitBytes(Directive); 1688*0b57cec5SDimitry Andric } 1689*0b57cec5SDimitry Andric } 1690*0b57cec5SDimitry Andric } 1691*0b57cec5SDimitry Andric 1692e8d8bef9SDimitry Andric // Emit /EXPORT: flags for each exported global as necessary. 1693e8d8bef9SDimitry Andric std::string Flags; 1694e8d8bef9SDimitry Andric for (const GlobalValue &GV : M.global_values()) { 1695e8d8bef9SDimitry Andric raw_string_ostream OS(Flags); 1696e8d8bef9SDimitry Andric emitLinkerFlagsForGlobalCOFF(OS, &GV, getTargetTriple(), getMangler()); 1697e8d8bef9SDimitry Andric OS.flush(); 1698e8d8bef9SDimitry Andric if (!Flags.empty()) { 1699e8d8bef9SDimitry Andric Streamer.SwitchSection(getDrectveSection()); 1700e8d8bef9SDimitry Andric Streamer.emitBytes(Flags); 1701e8d8bef9SDimitry Andric } 1702e8d8bef9SDimitry Andric Flags.clear(); 1703e8d8bef9SDimitry Andric } 1704*0b57cec5SDimitry Andric 1705e8d8bef9SDimitry Andric // Emit /INCLUDE: flags for each used global as necessary. 1706e8d8bef9SDimitry Andric if (const auto *LU = M.getNamedGlobal("llvm.used")) { 1707e8d8bef9SDimitry Andric assert(LU->hasInitializer() && "expected llvm.used to have an initializer"); 1708e8d8bef9SDimitry Andric assert(isa<ArrayType>(LU->getValueType()) && 1709e8d8bef9SDimitry Andric "expected llvm.used to be an array type"); 1710e8d8bef9SDimitry Andric if (const auto *A = cast<ConstantArray>(LU->getInitializer())) { 1711e8d8bef9SDimitry Andric for (const Value *Op : A->operands()) { 1712e8d8bef9SDimitry Andric const auto *GV = cast<GlobalValue>(Op->stripPointerCasts()); 1713e8d8bef9SDimitry Andric // Global symbols with internal or private linkage are not visible to 1714e8d8bef9SDimitry Andric // the linker, and thus would cause an error when the linker tried to 1715e8d8bef9SDimitry Andric // preserve the symbol due to the `/include:` directive. 1716e8d8bef9SDimitry Andric if (GV->hasLocalLinkage()) 1717e8d8bef9SDimitry Andric continue; 1718*0b57cec5SDimitry Andric 1719e8d8bef9SDimitry Andric raw_string_ostream OS(Flags); 1720e8d8bef9SDimitry Andric emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); 1721e8d8bef9SDimitry Andric OS.flush(); 1722e8d8bef9SDimitry Andric 1723e8d8bef9SDimitry Andric if (!Flags.empty()) { 1724e8d8bef9SDimitry Andric Streamer.SwitchSection(getDrectveSection()); 1725e8d8bef9SDimitry Andric Streamer.emitBytes(Flags); 1726e8d8bef9SDimitry Andric } 1727e8d8bef9SDimitry Andric Flags.clear(); 1728e8d8bef9SDimitry Andric } 1729e8d8bef9SDimitry Andric } 1730e8d8bef9SDimitry Andric } 1731*0b57cec5SDimitry Andric } 1732*0b57cec5SDimitry Andric 1733*0b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 1734*0b57cec5SDimitry Andric const TargetMachine &TM) { 1735*0b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TM); 1736e8d8bef9SDimitry Andric this->TM = &TM; 1737*0b57cec5SDimitry Andric const Triple &T = TM.getTargetTriple(); 1738*0b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1739*0b57cec5SDimitry Andric StaticCtorSection = 1740*0b57cec5SDimitry Andric Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1741*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 1742*0b57cec5SDimitry Andric SectionKind::getReadOnly()); 1743*0b57cec5SDimitry Andric StaticDtorSection = 1744*0b57cec5SDimitry Andric Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1745*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 1746*0b57cec5SDimitry Andric SectionKind::getReadOnly()); 1747*0b57cec5SDimitry Andric } else { 1748*0b57cec5SDimitry Andric StaticCtorSection = Ctx.getCOFFSection( 1749*0b57cec5SDimitry Andric ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1750*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1751*0b57cec5SDimitry Andric SectionKind::getData()); 1752*0b57cec5SDimitry Andric StaticDtorSection = Ctx.getCOFFSection( 1753*0b57cec5SDimitry Andric ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1754*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1755*0b57cec5SDimitry Andric SectionKind::getData()); 1756*0b57cec5SDimitry Andric } 1757*0b57cec5SDimitry Andric } 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 1760*0b57cec5SDimitry Andric const Triple &T, bool IsCtor, 1761*0b57cec5SDimitry Andric unsigned Priority, 1762*0b57cec5SDimitry Andric const MCSymbol *KeySym, 1763*0b57cec5SDimitry Andric MCSectionCOFF *Default) { 1764*0b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1765*0b57cec5SDimitry Andric // If the priority is the default, use .CRT$XCU, possibly associative. 1766*0b57cec5SDimitry Andric if (Priority == 65535) 1767*0b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 1768*0b57cec5SDimitry Andric 1769*0b57cec5SDimitry Andric // Otherwise, we need to compute a new section name. Low priorities should 1770*0b57cec5SDimitry Andric // run earlier. The linker will sort sections ASCII-betically, and we need a 1771*0b57cec5SDimitry Andric // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 1772*0b57cec5SDimitry Andric // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 1773*0b57cec5SDimitry Andric // low priorities need to sort before 'L', since the CRT uses that 1774*0b57cec5SDimitry Andric // internally, so we use ".CRT$XCA00001" for them. 1775*0b57cec5SDimitry Andric SmallString<24> Name; 1776*0b57cec5SDimitry Andric raw_svector_ostream OS(Name); 17778bcb0991SDimitry Andric OS << ".CRT$X" << (IsCtor ? "C" : "T") << 17788bcb0991SDimitry Andric (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 1779*0b57cec5SDimitry Andric MCSectionCOFF *Sec = Ctx.getCOFFSection( 1780*0b57cec5SDimitry Andric Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1781*0b57cec5SDimitry Andric SectionKind::getReadOnly()); 1782*0b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 1783*0b57cec5SDimitry Andric } 1784*0b57cec5SDimitry Andric 1785*0b57cec5SDimitry Andric std::string Name = IsCtor ? ".ctors" : ".dtors"; 1786*0b57cec5SDimitry Andric if (Priority != 65535) 1787*0b57cec5SDimitry Andric raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1788*0b57cec5SDimitry Andric 1789*0b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection( 1790*0b57cec5SDimitry Andric Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1791*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1792*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE, 1793*0b57cec5SDimitry Andric SectionKind::getData()), 1794*0b57cec5SDimitry Andric KeySym, 0); 1795*0b57cec5SDimitry Andric } 1796*0b57cec5SDimitry Andric 1797*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1798*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 1799*0b57cec5SDimitry Andric return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, 1800*0b57cec5SDimitry Andric Priority, KeySym, 1801*0b57cec5SDimitry Andric cast<MCSectionCOFF>(StaticCtorSection)); 1802*0b57cec5SDimitry Andric } 1803*0b57cec5SDimitry Andric 1804*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1805*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 1806*0b57cec5SDimitry Andric return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, 1807*0b57cec5SDimitry Andric Priority, KeySym, 1808*0b57cec5SDimitry Andric cast<MCSectionCOFF>(StaticDtorSection)); 1809*0b57cec5SDimitry Andric } 1810*0b57cec5SDimitry Andric 1811*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 1812*0b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 1813*0b57cec5SDimitry Andric const TargetMachine &TM) const { 1814*0b57cec5SDimitry Andric const Triple &T = TM.getTargetTriple(); 1815*0b57cec5SDimitry Andric if (T.isOSCygMing()) 1816*0b57cec5SDimitry Andric return nullptr; 1817*0b57cec5SDimitry Andric 1818*0b57cec5SDimitry Andric // Our symbols should exist in address space zero, cowardly no-op if 1819*0b57cec5SDimitry Andric // otherwise. 1820*0b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 1821*0b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0) 1822*0b57cec5SDimitry Andric return nullptr; 1823*0b57cec5SDimitry Andric 1824*0b57cec5SDimitry Andric // Both ptrtoint instructions must wrap global objects: 1825*0b57cec5SDimitry Andric // - Only global variables are eligible for image relative relocations. 1826*0b57cec5SDimitry Andric // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 1827*0b57cec5SDimitry Andric // We expect __ImageBase to be a global variable without a section, externally 1828*0b57cec5SDimitry Andric // defined. 1829*0b57cec5SDimitry Andric // 1830*0b57cec5SDimitry Andric // It should look something like this: @__ImageBase = external constant i8 1831*0b57cec5SDimitry Andric if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 1832*0b57cec5SDimitry Andric LHS->isThreadLocal() || RHS->isThreadLocal() || 1833*0b57cec5SDimitry Andric RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 1834*0b57cec5SDimitry Andric cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 1835*0b57cec5SDimitry Andric return nullptr; 1836*0b57cec5SDimitry Andric 1837*0b57cec5SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(LHS), 1838*0b57cec5SDimitry Andric MCSymbolRefExpr::VK_COFF_IMGREL32, 1839*0b57cec5SDimitry Andric getContext()); 1840*0b57cec5SDimitry Andric } 1841*0b57cec5SDimitry Andric 1842*0b57cec5SDimitry Andric static std::string APIntToHexString(const APInt &AI) { 1843*0b57cec5SDimitry Andric unsigned Width = (AI.getBitWidth() / 8) * 2; 18448bcb0991SDimitry Andric std::string HexString = AI.toString(16, /*Signed=*/false); 18455ffd83dbSDimitry Andric llvm::transform(HexString, HexString.begin(), tolower); 1846*0b57cec5SDimitry Andric unsigned Size = HexString.size(); 1847*0b57cec5SDimitry Andric assert(Width >= Size && "hex string is too large!"); 1848*0b57cec5SDimitry Andric HexString.insert(HexString.begin(), Width - Size, '0'); 1849*0b57cec5SDimitry Andric 1850*0b57cec5SDimitry Andric return HexString; 1851*0b57cec5SDimitry Andric } 1852*0b57cec5SDimitry Andric 1853*0b57cec5SDimitry Andric static std::string scalarConstantToHexString(const Constant *C) { 1854*0b57cec5SDimitry Andric Type *Ty = C->getType(); 1855*0b57cec5SDimitry Andric if (isa<UndefValue>(C)) { 1856*0b57cec5SDimitry Andric return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 1857*0b57cec5SDimitry Andric } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 1858*0b57cec5SDimitry Andric return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 1859*0b57cec5SDimitry Andric } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 1860*0b57cec5SDimitry Andric return APIntToHexString(CI->getValue()); 1861*0b57cec5SDimitry Andric } else { 1862*0b57cec5SDimitry Andric unsigned NumElements; 18635ffd83dbSDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty)) 18645ffd83dbSDimitry Andric NumElements = cast<FixedVectorType>(VTy)->getNumElements(); 1865*0b57cec5SDimitry Andric else 1866*0b57cec5SDimitry Andric NumElements = Ty->getArrayNumElements(); 1867*0b57cec5SDimitry Andric std::string HexString; 1868*0b57cec5SDimitry Andric for (int I = NumElements - 1, E = -1; I != E; --I) 1869*0b57cec5SDimitry Andric HexString += scalarConstantToHexString(C->getAggregateElement(I)); 1870*0b57cec5SDimitry Andric return HexString; 1871*0b57cec5SDimitry Andric } 1872*0b57cec5SDimitry Andric } 1873*0b57cec5SDimitry Andric 1874*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 1875*0b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 18765ffd83dbSDimitry Andric Align &Alignment) const { 1877*0b57cec5SDimitry Andric if (Kind.isMergeableConst() && C && 1878*0b57cec5SDimitry Andric getContext().getAsmInfo()->hasCOFFComdatConstants()) { 1879*0b57cec5SDimitry Andric // This creates comdat sections with the given symbol name, but unless 1880*0b57cec5SDimitry Andric // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 1881*0b57cec5SDimitry Andric // will be created with a null storage class, which makes GNU binutils 1882*0b57cec5SDimitry Andric // error out. 1883*0b57cec5SDimitry Andric const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1884*0b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 1885*0b57cec5SDimitry Andric COFF::IMAGE_SCN_LNK_COMDAT; 1886*0b57cec5SDimitry Andric std::string COMDATSymName; 1887*0b57cec5SDimitry Andric if (Kind.isMergeableConst4()) { 18885ffd83dbSDimitry Andric if (Alignment <= 4) { 1889*0b57cec5SDimitry Andric COMDATSymName = "__real@" + scalarConstantToHexString(C); 18905ffd83dbSDimitry Andric Alignment = Align(4); 1891*0b57cec5SDimitry Andric } 1892*0b57cec5SDimitry Andric } else if (Kind.isMergeableConst8()) { 18935ffd83dbSDimitry Andric if (Alignment <= 8) { 1894*0b57cec5SDimitry Andric COMDATSymName = "__real@" + scalarConstantToHexString(C); 18955ffd83dbSDimitry Andric Alignment = Align(8); 1896*0b57cec5SDimitry Andric } 1897*0b57cec5SDimitry Andric } else if (Kind.isMergeableConst16()) { 1898*0b57cec5SDimitry Andric // FIXME: These may not be appropriate for non-x86 architectures. 18995ffd83dbSDimitry Andric if (Alignment <= 16) { 1900*0b57cec5SDimitry Andric COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 19015ffd83dbSDimitry Andric Alignment = Align(16); 1902*0b57cec5SDimitry Andric } 1903*0b57cec5SDimitry Andric } else if (Kind.isMergeableConst32()) { 19045ffd83dbSDimitry Andric if (Alignment <= 32) { 1905*0b57cec5SDimitry Andric COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 19065ffd83dbSDimitry Andric Alignment = Align(32); 1907*0b57cec5SDimitry Andric } 1908*0b57cec5SDimitry Andric } 1909*0b57cec5SDimitry Andric 1910*0b57cec5SDimitry Andric if (!COMDATSymName.empty()) 1911*0b57cec5SDimitry Andric return getContext().getCOFFSection(".rdata", Characteristics, Kind, 1912*0b57cec5SDimitry Andric COMDATSymName, 1913*0b57cec5SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ANY); 1914*0b57cec5SDimitry Andric } 1915*0b57cec5SDimitry Andric 19165ffd83dbSDimitry Andric return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, 19175ffd83dbSDimitry Andric Alignment); 1918*0b57cec5SDimitry Andric } 1919*0b57cec5SDimitry Andric 1920*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1921*0b57cec5SDimitry Andric // Wasm 1922*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1923*0b57cec5SDimitry Andric 1924*0b57cec5SDimitry Andric static const Comdat *getWasmComdat(const GlobalValue *GV) { 1925*0b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 1926*0b57cec5SDimitry Andric if (!C) 1927*0b57cec5SDimitry Andric return nullptr; 1928*0b57cec5SDimitry Andric 1929*0b57cec5SDimitry Andric if (C->getSelectionKind() != Comdat::Any) 1930*0b57cec5SDimitry Andric report_fatal_error("WebAssembly COMDATs only support " 1931*0b57cec5SDimitry Andric "SelectionKind::Any, '" + C->getName() + "' cannot be " 1932*0b57cec5SDimitry Andric "lowered."); 1933*0b57cec5SDimitry Andric 1934*0b57cec5SDimitry Andric return C; 1935*0b57cec5SDimitry Andric } 1936*0b57cec5SDimitry Andric 1937*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 1938*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1939*0b57cec5SDimitry Andric // We don't support explict section names for functions in the wasm object 1940*0b57cec5SDimitry Andric // format. Each function has to be in its own unique section. 1941*0b57cec5SDimitry Andric if (isa<Function>(GO)) { 1942*0b57cec5SDimitry Andric return SelectSectionForGlobal(GO, Kind, TM); 1943*0b57cec5SDimitry Andric } 1944*0b57cec5SDimitry Andric 1945*0b57cec5SDimitry Andric StringRef Name = GO->getSection(); 1946*0b57cec5SDimitry Andric 19475ffd83dbSDimitry Andric // Certain data sections we treat as named custom sections rather than 19485ffd83dbSDimitry Andric // segments within the data section. 19495ffd83dbSDimitry Andric // This could be avoided if all data segements (the wasm sense) were 19505ffd83dbSDimitry Andric // represented as their own sections (in the llvm sense). 19515ffd83dbSDimitry Andric // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 19525ffd83dbSDimitry Andric if (Name == ".llvmcmd" || Name == ".llvmbc") 19535ffd83dbSDimitry Andric Kind = SectionKind::getMetadata(); 1954*0b57cec5SDimitry Andric 1955*0b57cec5SDimitry Andric StringRef Group = ""; 1956*0b57cec5SDimitry Andric if (const Comdat *C = getWasmComdat(GO)) { 1957*0b57cec5SDimitry Andric Group = C->getName(); 1958*0b57cec5SDimitry Andric } 1959*0b57cec5SDimitry Andric 1960*0b57cec5SDimitry Andric MCSectionWasm* Section = 1961*0b57cec5SDimitry Andric getContext().getWasmSection(Name, Kind, Group, 1962*0b57cec5SDimitry Andric MCContext::GenericSectionID); 1963*0b57cec5SDimitry Andric 1964*0b57cec5SDimitry Andric return Section; 1965*0b57cec5SDimitry Andric } 1966*0b57cec5SDimitry Andric 1967*0b57cec5SDimitry Andric static MCSectionWasm *selectWasmSectionForGlobal( 1968*0b57cec5SDimitry Andric MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 1969*0b57cec5SDimitry Andric const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 1970*0b57cec5SDimitry Andric StringRef Group = ""; 1971*0b57cec5SDimitry Andric if (const Comdat *C = getWasmComdat(GO)) { 1972*0b57cec5SDimitry Andric Group = C->getName(); 1973*0b57cec5SDimitry Andric } 1974*0b57cec5SDimitry Andric 1975*0b57cec5SDimitry Andric bool UniqueSectionNames = TM.getUniqueSectionNames(); 1976*0b57cec5SDimitry Andric SmallString<128> Name = getSectionPrefixForGlobal(Kind); 1977*0b57cec5SDimitry Andric 1978*0b57cec5SDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) { 1979*0b57cec5SDimitry Andric const auto &OptionalPrefix = F->getSectionPrefix(); 1980*0b57cec5SDimitry Andric if (OptionalPrefix) 1981e8d8bef9SDimitry Andric raw_svector_ostream(Name) << '.' << *OptionalPrefix; 1982*0b57cec5SDimitry Andric } 1983*0b57cec5SDimitry Andric 1984*0b57cec5SDimitry Andric if (EmitUniqueSection && UniqueSectionNames) { 1985*0b57cec5SDimitry Andric Name.push_back('.'); 1986*0b57cec5SDimitry Andric TM.getNameWithPrefix(Name, GO, Mang, true); 1987*0b57cec5SDimitry Andric } 1988*0b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 1989*0b57cec5SDimitry Andric if (EmitUniqueSection && !UniqueSectionNames) { 1990*0b57cec5SDimitry Andric UniqueID = *NextUniqueID; 1991*0b57cec5SDimitry Andric (*NextUniqueID)++; 1992*0b57cec5SDimitry Andric } 1993*0b57cec5SDimitry Andric 1994*0b57cec5SDimitry Andric return Ctx.getWasmSection(Name, Kind, Group, UniqueID); 1995*0b57cec5SDimitry Andric } 1996*0b57cec5SDimitry Andric 1997*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 1998*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1999*0b57cec5SDimitry Andric 2000*0b57cec5SDimitry Andric if (Kind.isCommon()) 2001*0b57cec5SDimitry Andric report_fatal_error("mergable sections not supported yet on wasm"); 2002*0b57cec5SDimitry Andric 2003*0b57cec5SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the 2004*0b57cec5SDimitry Andric // global value to a uniqued section specifically for it. 2005*0b57cec5SDimitry Andric bool EmitUniqueSection = false; 2006*0b57cec5SDimitry Andric if (Kind.isText()) 2007*0b57cec5SDimitry Andric EmitUniqueSection = TM.getFunctionSections(); 2008*0b57cec5SDimitry Andric else 2009*0b57cec5SDimitry Andric EmitUniqueSection = TM.getDataSections(); 2010*0b57cec5SDimitry Andric EmitUniqueSection |= GO->hasComdat(); 2011*0b57cec5SDimitry Andric 2012*0b57cec5SDimitry Andric return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 2013*0b57cec5SDimitry Andric EmitUniqueSection, &NextUniqueID); 2014*0b57cec5SDimitry Andric } 2015*0b57cec5SDimitry Andric 2016*0b57cec5SDimitry Andric bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 2017*0b57cec5SDimitry Andric bool UsesLabelDifference, const Function &F) const { 2018*0b57cec5SDimitry Andric // We can always create relative relocations, so use another section 2019*0b57cec5SDimitry Andric // that can be marked non-executable. 2020*0b57cec5SDimitry Andric return false; 2021*0b57cec5SDimitry Andric } 2022*0b57cec5SDimitry Andric 2023*0b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 2024*0b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 2025*0b57cec5SDimitry Andric const TargetMachine &TM) const { 2026*0b57cec5SDimitry Andric // We may only use a PLT-relative relocation to refer to unnamed_addr 2027*0b57cec5SDimitry Andric // functions. 2028*0b57cec5SDimitry Andric if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 2029*0b57cec5SDimitry Andric return nullptr; 2030*0b57cec5SDimitry Andric 2031*0b57cec5SDimitry Andric // Basic sanity checks. 2032*0b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 2033*0b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 2034*0b57cec5SDimitry Andric RHS->isThreadLocal()) 2035*0b57cec5SDimitry Andric return nullptr; 2036*0b57cec5SDimitry Andric 2037*0b57cec5SDimitry Andric return MCBinaryExpr::createSub( 2038*0b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 2039*0b57cec5SDimitry Andric getContext()), 2040*0b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 2041*0b57cec5SDimitry Andric } 2042*0b57cec5SDimitry Andric 2043*0b57cec5SDimitry Andric void TargetLoweringObjectFileWasm::InitializeWasm() { 2044*0b57cec5SDimitry Andric StaticCtorSection = 2045*0b57cec5SDimitry Andric getContext().getWasmSection(".init_array", SectionKind::getData()); 2046*0b57cec5SDimitry Andric 2047*0b57cec5SDimitry Andric // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 2048*0b57cec5SDimitry Andric // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 2049*0b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2050*0b57cec5SDimitry Andric } 2051*0b57cec5SDimitry Andric 2052*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 2053*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2054*0b57cec5SDimitry Andric return Priority == UINT16_MAX ? 2055*0b57cec5SDimitry Andric StaticCtorSection : 2056*0b57cec5SDimitry Andric getContext().getWasmSection(".init_array." + utostr(Priority), 2057*0b57cec5SDimitry Andric SectionKind::getData()); 2058*0b57cec5SDimitry Andric } 2059*0b57cec5SDimitry Andric 2060*0b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 2061*0b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2062*0b57cec5SDimitry Andric llvm_unreachable("@llvm.global_dtors should have been lowered already"); 2063*0b57cec5SDimitry Andric return nullptr; 2064*0b57cec5SDimitry Andric } 20658bcb0991SDimitry Andric 20668bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 20678bcb0991SDimitry Andric // XCOFF 20688bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 2069e8d8bef9SDimitry Andric bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock( 2070e8d8bef9SDimitry Andric const MachineFunction *MF) { 2071e8d8bef9SDimitry Andric if (!MF->getLandingPads().empty()) 2072e8d8bef9SDimitry Andric return true; 2073e8d8bef9SDimitry Andric 2074e8d8bef9SDimitry Andric const Function &F = MF->getFunction(); 2075e8d8bef9SDimitry Andric if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry()) 2076e8d8bef9SDimitry Andric return false; 2077e8d8bef9SDimitry Andric 2078e8d8bef9SDimitry Andric const Function *Per = 2079e8d8bef9SDimitry Andric dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); 2080e8d8bef9SDimitry Andric if (isNoOpWithoutInvoke(classifyEHPersonality(Per))) 2081e8d8bef9SDimitry Andric return false; 2082e8d8bef9SDimitry Andric 2083e8d8bef9SDimitry Andric return true; 2084e8d8bef9SDimitry Andric } 2085e8d8bef9SDimitry Andric 2086e8d8bef9SDimitry Andric MCSymbol * 2087e8d8bef9SDimitry Andric TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { 2088e8d8bef9SDimitry Andric return MF->getMMI().getContext().getOrCreateSymbol( 2089e8d8bef9SDimitry Andric "__ehinfo." + Twine(MF->getFunctionNumber())); 2090e8d8bef9SDimitry Andric } 2091e8d8bef9SDimitry Andric 20925ffd83dbSDimitry Andric MCSymbol * 20935ffd83dbSDimitry Andric TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV, 20945ffd83dbSDimitry Andric const TargetMachine &TM) const { 20955ffd83dbSDimitry Andric // We always use a qualname symbol for a GV that represents 20965ffd83dbSDimitry Andric // a declaration, a function descriptor, or a common symbol. 2097e8d8bef9SDimitry Andric // If a GV represents a GlobalVariable and -fdata-sections is enabled, we 2098e8d8bef9SDimitry Andric // also return a qualname so that a label symbol could be avoided. 20995ffd83dbSDimitry Andric // It is inherently ambiguous when the GO represents the address of a 21005ffd83dbSDimitry Andric // function, as the GO could either represent a function descriptor or a 21015ffd83dbSDimitry Andric // function entry point. We choose to always return a function descriptor 21025ffd83dbSDimitry Andric // here. 21035ffd83dbSDimitry Andric if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) { 21045ffd83dbSDimitry Andric if (GO->isDeclarationForLinker()) 21055ffd83dbSDimitry Andric return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM)) 21065ffd83dbSDimitry Andric ->getQualNameSymbol(); 21075ffd83dbSDimitry Andric 21085ffd83dbSDimitry Andric SectionKind GOKind = getKindForGlobal(GO, TM); 21095ffd83dbSDimitry Andric if (GOKind.isText()) 21105ffd83dbSDimitry Andric return cast<MCSectionXCOFF>( 21115ffd83dbSDimitry Andric getSectionForFunctionDescriptor(cast<Function>(GO), TM)) 21125ffd83dbSDimitry Andric ->getQualNameSymbol(); 2113e8d8bef9SDimitry Andric if ((TM.getDataSections() && !GO->hasSection()) || GOKind.isCommon() || 2114e8d8bef9SDimitry Andric GOKind.isBSSLocal()) 21155ffd83dbSDimitry Andric return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM)) 21165ffd83dbSDimitry Andric ->getQualNameSymbol(); 21175ffd83dbSDimitry Andric } 21185ffd83dbSDimitry Andric 21195ffd83dbSDimitry Andric // For all other cases, fall back to getSymbol to return the unqualified name. 21205ffd83dbSDimitry Andric return nullptr; 21215ffd83dbSDimitry Andric } 21225ffd83dbSDimitry Andric 21238bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 21248bcb0991SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2125e8d8bef9SDimitry Andric if (!GO->hasSection()) 2126e8d8bef9SDimitry Andric report_fatal_error("#pragma clang section is not yet supported"); 2127e8d8bef9SDimitry Andric 2128e8d8bef9SDimitry Andric StringRef SectionName = GO->getSection(); 2129e8d8bef9SDimitry Andric XCOFF::StorageMappingClass MappingClass; 2130e8d8bef9SDimitry Andric if (Kind.isText()) 2131e8d8bef9SDimitry Andric MappingClass = XCOFF::XMC_PR; 2132e8d8bef9SDimitry Andric else if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) 2133e8d8bef9SDimitry Andric MappingClass = XCOFF::XMC_RW; 2134e8d8bef9SDimitry Andric else if (Kind.isReadOnly()) 2135e8d8bef9SDimitry Andric MappingClass = XCOFF::XMC_RO; 2136e8d8bef9SDimitry Andric else 2137e8d8bef9SDimitry Andric report_fatal_error("XCOFF other section types not yet implemented."); 2138e8d8bef9SDimitry Andric 2139e8d8bef9SDimitry Andric return getContext().getXCOFFSection(SectionName, MappingClass, XCOFF::XTY_SD, 2140e8d8bef9SDimitry Andric Kind, /* MultiSymbolsAllowed*/ true); 21418bcb0991SDimitry Andric } 21428bcb0991SDimitry Andric 21435ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( 21445ffd83dbSDimitry Andric const GlobalObject *GO, const TargetMachine &TM) const { 21455ffd83dbSDimitry Andric assert(GO->isDeclarationForLinker() && 21465ffd83dbSDimitry Andric "Tried to get ER section for a defined global."); 21475ffd83dbSDimitry Andric 21485ffd83dbSDimitry Andric SmallString<128> Name; 21495ffd83dbSDimitry Andric getNameWithPrefix(Name, GO, TM); 21505ffd83dbSDimitry Andric 21515ffd83dbSDimitry Andric // Externals go into a csect of type ER. 21525ffd83dbSDimitry Andric return getContext().getXCOFFSection( 21535ffd83dbSDimitry Andric Name, isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA, XCOFF::XTY_ER, 2154e8d8bef9SDimitry Andric SectionKind::getMetadata()); 21555ffd83dbSDimitry Andric } 21565ffd83dbSDimitry Andric 21578bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 21588bcb0991SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 21598bcb0991SDimitry Andric // Common symbols go into a csect with matching name which will get mapped 21608bcb0991SDimitry Andric // into the .bss section. 21618bcb0991SDimitry Andric if (Kind.isBSSLocal() || Kind.isCommon()) { 21628bcb0991SDimitry Andric SmallString<128> Name; 21638bcb0991SDimitry Andric getNameWithPrefix(Name, GO, TM); 21648bcb0991SDimitry Andric return getContext().getXCOFFSection( 21658bcb0991SDimitry Andric Name, Kind.isBSSLocal() ? XCOFF::XMC_BS : XCOFF::XMC_RW, XCOFF::XTY_CM, 2166e8d8bef9SDimitry Andric Kind); 21678bcb0991SDimitry Andric } 21688bcb0991SDimitry Andric 2169480093f4SDimitry Andric if (Kind.isMergeableCString()) { 21705ffd83dbSDimitry Andric Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 2171480093f4SDimitry Andric cast<GlobalVariable>(GO)); 2172480093f4SDimitry Andric 2173480093f4SDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 2174480093f4SDimitry Andric std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 2175480093f4SDimitry Andric SmallString<128> Name; 21765ffd83dbSDimitry Andric Name = SizeSpec + utostr(Alignment.value()); 2177480093f4SDimitry Andric 2178e8d8bef9SDimitry Andric if (TM.getDataSections()) 2179e8d8bef9SDimitry Andric getNameWithPrefix(Name, GO, TM); 2180e8d8bef9SDimitry Andric 2181480093f4SDimitry Andric return getContext().getXCOFFSection( 2182e8d8bef9SDimitry Andric Name, XCOFF::XMC_RO, XCOFF::XTY_SD, Kind, 2183e8d8bef9SDimitry Andric /* MultiSymbolsAllowed*/ !TM.getDataSections()); 2184480093f4SDimitry Andric } 2185480093f4SDimitry Andric 2186e8d8bef9SDimitry Andric if (Kind.isText()) { 2187e8d8bef9SDimitry Andric if (TM.getFunctionSections()) { 2188e8d8bef9SDimitry Andric return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM)) 2189e8d8bef9SDimitry Andric ->getRepresentedCsect(); 2190e8d8bef9SDimitry Andric } 21918bcb0991SDimitry Andric return TextSection; 2192e8d8bef9SDimitry Andric } 21938bcb0991SDimitry Andric 2194e8d8bef9SDimitry Andric // TODO: We may put Kind.isReadOnlyWithRel() under option control, because 2195e8d8bef9SDimitry Andric // user may want to have read-only data with relocations placed into a 2196e8d8bef9SDimitry Andric // read-only section by the compiler. 2197e8d8bef9SDimitry Andric // For BSS kind, zero initialized data must be emitted to the .data section 2198e8d8bef9SDimitry Andric // because external linkage control sections that get mapped to the .bss 2199e8d8bef9SDimitry Andric // section will be linked as tentative defintions, which is only appropriate 2200e8d8bef9SDimitry Andric // for SectionKind::Common. 2201e8d8bef9SDimitry Andric if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) { 2202e8d8bef9SDimitry Andric if (TM.getDataSections()) { 2203e8d8bef9SDimitry Andric SmallString<128> Name; 2204e8d8bef9SDimitry Andric getNameWithPrefix(Name, GO, TM); 2205e8d8bef9SDimitry Andric return getContext().getXCOFFSection(Name, XCOFF::XMC_RW, XCOFF::XTY_SD, 2206e8d8bef9SDimitry Andric SectionKind::getData()); 2207e8d8bef9SDimitry Andric } 22088bcb0991SDimitry Andric return DataSection; 2209e8d8bef9SDimitry Andric } 22108bcb0991SDimitry Andric 2211e8d8bef9SDimitry Andric if (Kind.isReadOnly()) { 2212e8d8bef9SDimitry Andric if (TM.getDataSections()) { 2213e8d8bef9SDimitry Andric SmallString<128> Name; 2214e8d8bef9SDimitry Andric getNameWithPrefix(Name, GO, TM); 2215e8d8bef9SDimitry Andric return getContext().getXCOFFSection(Name, XCOFF::XMC_RO, XCOFF::XTY_SD, 2216e8d8bef9SDimitry Andric SectionKind::getReadOnly()); 2217e8d8bef9SDimitry Andric } 2218480093f4SDimitry Andric return ReadOnlySection; 2219e8d8bef9SDimitry Andric } 2220480093f4SDimitry Andric 22218bcb0991SDimitry Andric report_fatal_error("XCOFF other section types not yet implemented."); 22228bcb0991SDimitry Andric } 22238bcb0991SDimitry Andric 2224480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable( 2225480093f4SDimitry Andric const Function &F, const TargetMachine &TM) const { 2226480093f4SDimitry Andric assert (!F.getComdat() && "Comdat not supported on XCOFF."); 2227e8d8bef9SDimitry Andric 2228e8d8bef9SDimitry Andric if (!TM.getFunctionSections()) 2229480093f4SDimitry Andric return ReadOnlySection; 2230e8d8bef9SDimitry Andric 2231e8d8bef9SDimitry Andric // If the function can be removed, produce a unique section so that 2232e8d8bef9SDimitry Andric // the table doesn't prevent the removal. 2233e8d8bef9SDimitry Andric SmallString<128> NameStr(".rodata.jmp.."); 2234e8d8bef9SDimitry Andric getNameWithPrefix(NameStr, &F, TM); 2235e8d8bef9SDimitry Andric return getContext().getXCOFFSection(NameStr, XCOFF::XMC_RO, XCOFF::XTY_SD, 2236e8d8bef9SDimitry Andric SectionKind::getReadOnly()); 2237480093f4SDimitry Andric } 2238480093f4SDimitry Andric 22398bcb0991SDimitry Andric bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 22408bcb0991SDimitry Andric bool UsesLabelDifference, const Function &F) const { 2241480093f4SDimitry Andric return false; 2242480093f4SDimitry Andric } 2243480093f4SDimitry Andric 2244480093f4SDimitry Andric /// Given a mergeable constant with the specified size and relocation 2245480093f4SDimitry Andric /// information, return a section that it should be placed in. 2246480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant( 2247480093f4SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 22485ffd83dbSDimitry Andric Align &Alignment) const { 2249480093f4SDimitry Andric //TODO: Enable emiting constant pool to unique sections when we support it. 2250480093f4SDimitry Andric return ReadOnlySection; 22518bcb0991SDimitry Andric } 22528bcb0991SDimitry Andric 22538bcb0991SDimitry Andric void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 22548bcb0991SDimitry Andric const TargetMachine &TgtM) { 22558bcb0991SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TgtM); 2256e8d8bef9SDimitry Andric TTypeEncoding = 2257e8d8bef9SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel | 2258e8d8bef9SDimitry Andric (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4 2259e8d8bef9SDimitry Andric : dwarf::DW_EH_PE_sdata8); 22608bcb0991SDimitry Andric PersonalityEncoding = 0; 22618bcb0991SDimitry Andric LSDAEncoding = 0; 2262e8d8bef9SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 22638bcb0991SDimitry Andric } 22648bcb0991SDimitry Andric 22658bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 22668bcb0991SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2267e8d8bef9SDimitry Andric report_fatal_error("no static constructor section on AIX"); 22688bcb0991SDimitry Andric } 22698bcb0991SDimitry Andric 22708bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 22718bcb0991SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2272e8d8bef9SDimitry Andric report_fatal_error("no static destructor section on AIX"); 22738bcb0991SDimitry Andric } 22748bcb0991SDimitry Andric 22758bcb0991SDimitry Andric const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 22768bcb0991SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 22778bcb0991SDimitry Andric const TargetMachine &TM) const { 22788bcb0991SDimitry Andric report_fatal_error("XCOFF not yet implemented."); 22798bcb0991SDimitry Andric } 22808bcb0991SDimitry Andric 2281e8d8bef9SDimitry Andric XCOFF::StorageClass 2282e8d8bef9SDimitry Andric TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) { 2283e8d8bef9SDimitry Andric assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX."); 2284e8d8bef9SDimitry Andric 2285e8d8bef9SDimitry Andric switch (GV->getLinkage()) { 22868bcb0991SDimitry Andric case GlobalValue::InternalLinkage: 2287480093f4SDimitry Andric case GlobalValue::PrivateLinkage: 22888bcb0991SDimitry Andric return XCOFF::C_HIDEXT; 22898bcb0991SDimitry Andric case GlobalValue::ExternalLinkage: 22908bcb0991SDimitry Andric case GlobalValue::CommonLinkage: 22915ffd83dbSDimitry Andric case GlobalValue::AvailableExternallyLinkage: 22928bcb0991SDimitry Andric return XCOFF::C_EXT; 22938bcb0991SDimitry Andric case GlobalValue::ExternalWeakLinkage: 22945ffd83dbSDimitry Andric case GlobalValue::LinkOnceAnyLinkage: 22955ffd83dbSDimitry Andric case GlobalValue::LinkOnceODRLinkage: 22965ffd83dbSDimitry Andric case GlobalValue::WeakAnyLinkage: 22975ffd83dbSDimitry Andric case GlobalValue::WeakODRLinkage: 22988bcb0991SDimitry Andric return XCOFF::C_WEAKEXT; 22995ffd83dbSDimitry Andric case GlobalValue::AppendingLinkage: 23008bcb0991SDimitry Andric report_fatal_error( 23015ffd83dbSDimitry Andric "There is no mapping that implements AppendingLinkage for XCOFF."); 23028bcb0991SDimitry Andric } 23035ffd83dbSDimitry Andric llvm_unreachable("Unknown linkage type!"); 23045ffd83dbSDimitry Andric } 23055ffd83dbSDimitry Andric 23065ffd83dbSDimitry Andric MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol( 2307e8d8bef9SDimitry Andric const GlobalValue *Func, const TargetMachine &TM) const { 2308e8d8bef9SDimitry Andric assert( 2309e8d8bef9SDimitry Andric (isa<Function>(Func) || 2310e8d8bef9SDimitry Andric (isa<GlobalAlias>(Func) && 2311e8d8bef9SDimitry Andric isa_and_nonnull<Function>(cast<GlobalAlias>(Func)->getBaseObject()))) && 2312e8d8bef9SDimitry Andric "Func must be a function or an alias which has a function as base " 2313e8d8bef9SDimitry Andric "object."); 2314e8d8bef9SDimitry Andric 23155ffd83dbSDimitry Andric SmallString<128> NameStr; 23165ffd83dbSDimitry Andric NameStr.push_back('.'); 2317e8d8bef9SDimitry Andric getNameWithPrefix(NameStr, Func, TM); 2318e8d8bef9SDimitry Andric 2319e8d8bef9SDimitry Andric // When -function-sections is enabled and explicit section is not specified, 2320e8d8bef9SDimitry Andric // it's not necessary to emit function entry point label any more. We will use 2321e8d8bef9SDimitry Andric // function entry point csect instead. And for function delcarations, the 2322e8d8bef9SDimitry Andric // undefined symbols gets treated as csect with XTY_ER property. 2323e8d8bef9SDimitry Andric if (((TM.getFunctionSections() && !Func->hasSection()) || 2324e8d8bef9SDimitry Andric Func->isDeclaration()) && 2325e8d8bef9SDimitry Andric isa<Function>(Func)) { 2326e8d8bef9SDimitry Andric return getContext() 2327e8d8bef9SDimitry Andric .getXCOFFSection(NameStr, XCOFF::XMC_PR, 2328e8d8bef9SDimitry Andric Func->isDeclaration() ? XCOFF::XTY_ER : XCOFF::XTY_SD, 2329e8d8bef9SDimitry Andric SectionKind::getText()) 2330e8d8bef9SDimitry Andric ->getQualNameSymbol(); 2331e8d8bef9SDimitry Andric } 2332e8d8bef9SDimitry Andric 23335ffd83dbSDimitry Andric return getContext().getOrCreateSymbol(NameStr); 23345ffd83dbSDimitry Andric } 23355ffd83dbSDimitry Andric 23365ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( 23375ffd83dbSDimitry Andric const Function *F, const TargetMachine &TM) const { 23385ffd83dbSDimitry Andric SmallString<128> NameStr; 23395ffd83dbSDimitry Andric getNameWithPrefix(NameStr, F, TM); 23405ffd83dbSDimitry Andric return getContext().getXCOFFSection(NameStr, XCOFF::XMC_DS, XCOFF::XTY_SD, 23415ffd83dbSDimitry Andric SectionKind::getData()); 23425ffd83dbSDimitry Andric } 23435ffd83dbSDimitry Andric 23445ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( 2345e8d8bef9SDimitry Andric const MCSymbol *Sym, const TargetMachine &TM) const { 2346e8d8bef9SDimitry Andric // Use TE storage-mapping class when large code model is enabled so that 2347e8d8bef9SDimitry Andric // the chance of needing -bbigtoc is decreased. 23485ffd83dbSDimitry Andric return getContext().getXCOFFSection( 2349e8d8bef9SDimitry Andric cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), 2350e8d8bef9SDimitry Andric TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE : XCOFF::XMC_TC, 2351e8d8bef9SDimitry Andric XCOFF::XTY_SD, SectionKind::getData()); 23528bcb0991SDimitry Andric } 2353