10b57cec5SDimitry Andric //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements classes used to handle lowerings specific to common 100b57cec5SDimitry Andric // object file formats. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 160b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 180b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 200b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h" 210b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 220b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h" 230b57cec5SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 24*5f7ddb14SDimitry Andric #include "llvm/BinaryFormat/Wasm.h" 25af732203SDimitry Andric #include "llvm/CodeGen/BasicBlockSectionUtils.h" 265ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 275ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h" 300b57cec5SDimitry Andric #include "llvm/IR/Comdat.h" 310b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 320b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 330b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 345ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 355ffd83dbSDimitry Andric #include "llvm/IR/DiagnosticPrinter.h" 360b57cec5SDimitry Andric #include "llvm/IR/Function.h" 370b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 380b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h" 390b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 400b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 410b57cec5SDimitry Andric #include "llvm/IR/Mangler.h" 420b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 430b57cec5SDimitry Andric #include "llvm/IR/Module.h" 44af732203SDimitry Andric #include "llvm/IR/PseudoProbe.h" 450b57cec5SDimitry Andric #include "llvm/IR/Type.h" 460b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 470b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 480b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h" 490b57cec5SDimitry Andric #include "llvm/MC/MCSectionCOFF.h" 500b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h" 51*5f7ddb14SDimitry Andric #include "llvm/MC/MCSectionGOFF.h" 520b57cec5SDimitry Andric #include "llvm/MC/MCSectionMachO.h" 530b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h" 548bcb0991SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h" 550b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 560b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 570b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h" 580b57cec5SDimitry Andric #include "llvm/MC/MCValue.h" 590b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 600b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h" 610b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 620b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 630b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 645ffd83dbSDimitry Andric #include "llvm/Support/Format.h" 650b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 660b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 670b57cec5SDimitry Andric #include <cassert> 680b57cec5SDimitry Andric #include <string> 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric using namespace llvm; 710b57cec5SDimitry Andric using namespace dwarf; 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, 740b57cec5SDimitry Andric StringRef &Section) { 750b57cec5SDimitry Andric SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 760b57cec5SDimitry Andric M.getModuleFlagsMetadata(ModuleFlags); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric for (const auto &MFE: ModuleFlags) { 790b57cec5SDimitry Andric // Ignore flags with 'Require' behaviour. 800b57cec5SDimitry Andric if (MFE.Behavior == Module::Require) 810b57cec5SDimitry Andric continue; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric StringRef Key = MFE.Key->getString(); 840b57cec5SDimitry Andric if (Key == "Objective-C Image Info Version") { 850b57cec5SDimitry Andric Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 860b57cec5SDimitry Andric } else if (Key == "Objective-C Garbage Collection" || 870b57cec5SDimitry Andric Key == "Objective-C GC Only" || 880b57cec5SDimitry Andric Key == "Objective-C Is Simulated" || 890b57cec5SDimitry Andric Key == "Objective-C Class Properties" || 900b57cec5SDimitry Andric Key == "Objective-C Image Swift Version") { 910b57cec5SDimitry Andric Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 920b57cec5SDimitry Andric } else if (Key == "Objective-C Image Info Section") { 930b57cec5SDimitry Andric Section = cast<MDString>(MFE.Val)->getString(); 940b57cec5SDimitry Andric } 955ffd83dbSDimitry Andric // Backend generates L_OBJC_IMAGE_INFO from Swift ABI version + major + minor + 965ffd83dbSDimitry Andric // "Objective-C Garbage Collection". 975ffd83dbSDimitry Andric else if (Key == "Swift ABI Version") { 985ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 8; 995ffd83dbSDimitry Andric } else if (Key == "Swift Major Version") { 1005ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 24; 1015ffd83dbSDimitry Andric } else if (Key == "Swift Minor Version") { 1025ffd83dbSDimitry Andric Flags |= (mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue()) << 16; 1035ffd83dbSDimitry Andric } 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1080b57cec5SDimitry Andric // ELF 1090b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1100b57cec5SDimitry Andric 111af732203SDimitry Andric TargetLoweringObjectFileELF::TargetLoweringObjectFileELF() 112af732203SDimitry Andric : TargetLoweringObjectFile() { 113af732203SDimitry Andric SupportDSOLocalEquivalentLowering = true; 114af732203SDimitry Andric } 115af732203SDimitry Andric 1160b57cec5SDimitry Andric void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 1170b57cec5SDimitry Andric const TargetMachine &TgtM) { 1180b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TgtM); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric CodeModel::Model CM = TgtM.getCodeModel(); 1215ffd83dbSDimitry Andric InitializeELF(TgtM.Options.UseInitArray); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric switch (TgtM.getTargetTriple().getArch()) { 1240b57cec5SDimitry Andric case Triple::arm: 1250b57cec5SDimitry Andric case Triple::armeb: 1260b57cec5SDimitry Andric case Triple::thumb: 1270b57cec5SDimitry Andric case Triple::thumbeb: 1280b57cec5SDimitry Andric if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) 1290b57cec5SDimitry Andric break; 1300b57cec5SDimitry Andric // Fallthrough if not using EHABI 1310b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1320b57cec5SDimitry Andric case Triple::ppc: 133af732203SDimitry Andric case Triple::ppcle: 1340b57cec5SDimitry Andric case Triple::x86: 1350b57cec5SDimitry Andric PersonalityEncoding = isPositionIndependent() 1360b57cec5SDimitry Andric ? dwarf::DW_EH_PE_indirect | 1370b57cec5SDimitry Andric dwarf::DW_EH_PE_pcrel | 1380b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4 1390b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 1400b57cec5SDimitry Andric LSDAEncoding = isPositionIndependent() 1410b57cec5SDimitry Andric ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 1420b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 1430b57cec5SDimitry Andric TTypeEncoding = isPositionIndependent() 1440b57cec5SDimitry Andric ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 1450b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4 1460b57cec5SDimitry Andric : dwarf::DW_EH_PE_absptr; 1470b57cec5SDimitry Andric break; 1480b57cec5SDimitry Andric case Triple::x86_64: 1490b57cec5SDimitry Andric if (isPositionIndependent()) { 1500b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 1510b57cec5SDimitry Andric ((CM == CodeModel::Small || CM == CodeModel::Medium) 1520b57cec5SDimitry Andric ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 1530b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | 1540b57cec5SDimitry Andric (CM == CodeModel::Small 1550b57cec5SDimitry Andric ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 1560b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 1570b57cec5SDimitry Andric ((CM == CodeModel::Small || CM == CodeModel::Medium) 158*5f7ddb14SDimitry Andric ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 1590b57cec5SDimitry Andric } else { 1600b57cec5SDimitry Andric PersonalityEncoding = 1610b57cec5SDimitry Andric (CM == CodeModel::Small || CM == CodeModel::Medium) 1620b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 1630b57cec5SDimitry Andric LSDAEncoding = (CM == CodeModel::Small) 1640b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 1650b57cec5SDimitry Andric TTypeEncoding = (CM == CodeModel::Small) 1660b57cec5SDimitry Andric ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric break; 1690b57cec5SDimitry Andric case Triple::hexagon: 1700b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 1710b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 1720b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 1730b57cec5SDimitry Andric if (isPositionIndependent()) { 1740b57cec5SDimitry Andric PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 1750b57cec5SDimitry Andric LSDAEncoding |= dwarf::DW_EH_PE_pcrel; 1760b57cec5SDimitry Andric TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric break; 1790b57cec5SDimitry Andric case Triple::aarch64: 1800b57cec5SDimitry Andric case Triple::aarch64_be: 1818bcb0991SDimitry Andric case Triple::aarch64_32: 1820b57cec5SDimitry Andric // The small model guarantees static code/data size < 4GB, but not where it 1830b57cec5SDimitry Andric // will be in memory. Most of these could end up >2GB away so even a signed 1840b57cec5SDimitry Andric // pc-relative 32-bit address is insufficient, theoretically. 1850b57cec5SDimitry Andric if (isPositionIndependent()) { 186af732203SDimitry Andric // ILP32 uses sdata4 instead of sdata8 187af732203SDimitry Andric if (TgtM.getTargetTriple().getEnvironment() == Triple::GNUILP32) { 188af732203SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 189af732203SDimitry Andric dwarf::DW_EH_PE_sdata4; 190af732203SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 191af732203SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 192af732203SDimitry Andric dwarf::DW_EH_PE_sdata4; 193af732203SDimitry Andric } else { 1940b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 1950b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata8; 1960b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8; 1970b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 1980b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata8; 199af732203SDimitry Andric } 2000b57cec5SDimitry Andric } else { 2010b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 2020b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 2030b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric break; 2060b57cec5SDimitry Andric case Triple::lanai: 2070b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 2080b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 2090b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2100b57cec5SDimitry Andric break; 2110b57cec5SDimitry Andric case Triple::mips: 2120b57cec5SDimitry Andric case Triple::mipsel: 2130b57cec5SDimitry Andric case Triple::mips64: 2140b57cec5SDimitry Andric case Triple::mips64el: 2150b57cec5SDimitry Andric // MIPS uses indirect pointer to refer personality functions and types, so 2160b57cec5SDimitry Andric // that the eh_frame section can be read-only. DW.ref.personality will be 2170b57cec5SDimitry Andric // generated for relocation. 2180b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect; 2190b57cec5SDimitry Andric // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't 2200b57cec5SDimitry Andric // identify N64 from just a triple. 2210b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2220b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2230b57cec5SDimitry Andric // We don't support PC-relative LSDA references in GAS so we use the default 2240b57cec5SDimitry Andric // DW_EH_PE_absptr for those. 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric // FreeBSD must be explicit about the data size and using pcrel since it's 2270b57cec5SDimitry Andric // assembler/linker won't do the automatic conversion that the Linux tools 2280b57cec5SDimitry Andric // do. 2290b57cec5SDimitry Andric if (TgtM.getTargetTriple().isOSFreeBSD()) { 2300b57cec5SDimitry Andric PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2310b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric break; 2340b57cec5SDimitry Andric case Triple::ppc64: 2350b57cec5SDimitry Andric case Triple::ppc64le: 2360b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2370b57cec5SDimitry Andric dwarf::DW_EH_PE_udata8; 2380b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; 2390b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2400b57cec5SDimitry Andric dwarf::DW_EH_PE_udata8; 2410b57cec5SDimitry Andric break; 2420b57cec5SDimitry Andric case Triple::sparcel: 2430b57cec5SDimitry Andric case Triple::sparc: 2440b57cec5SDimitry Andric if (isPositionIndependent()) { 2450b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2460b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2470b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2480b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2490b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2500b57cec5SDimitry Andric } else { 2510b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 2520b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 2530b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 2560b57cec5SDimitry Andric break; 2570b57cec5SDimitry Andric case Triple::riscv32: 2580b57cec5SDimitry Andric case Triple::riscv64: 2590b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2600b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2610b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2620b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2630b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2640b57cec5SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 2650b57cec5SDimitry Andric break; 2660b57cec5SDimitry Andric case Triple::sparcv9: 2670b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2680b57cec5SDimitry Andric if (isPositionIndependent()) { 2690b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2700b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2710b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2720b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2730b57cec5SDimitry Andric } else { 2740b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 2750b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric break; 2780b57cec5SDimitry Andric case Triple::systemz: 2790b57cec5SDimitry Andric // All currently-defined code models guarantee that 4-byte PC-relative 2800b57cec5SDimitry Andric // values will be in range. 2810b57cec5SDimitry Andric if (isPositionIndependent()) { 2820b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2830b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2840b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 2850b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 2860b57cec5SDimitry Andric dwarf::DW_EH_PE_sdata4; 2870b57cec5SDimitry Andric } else { 2880b57cec5SDimitry Andric PersonalityEncoding = dwarf::DW_EH_PE_absptr; 2890b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_absptr; 2900b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric break; 2930b57cec5SDimitry Andric default: 2940b57cec5SDimitry Andric break; 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 298*5f7ddb14SDimitry Andric void TargetLoweringObjectFileELF::getModuleMetadata(Module &M) { 299*5f7ddb14SDimitry Andric SmallVector<GlobalValue *, 4> Vec; 300*5f7ddb14SDimitry Andric collectUsedGlobalVariables(M, Vec, false); 301*5f7ddb14SDimitry Andric for (GlobalValue *GV : Vec) 302*5f7ddb14SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(GV)) 303*5f7ddb14SDimitry Andric Used.insert(GO); 304*5f7ddb14SDimitry Andric } 305*5f7ddb14SDimitry Andric 3060b57cec5SDimitry Andric void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, 3070b57cec5SDimitry Andric Module &M) const { 3080b57cec5SDimitry Andric auto &C = getContext(); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 3110b57cec5SDimitry Andric auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS, 3120b57cec5SDimitry Andric ELF::SHF_EXCLUDE); 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric Streamer.SwitchSection(S); 3150b57cec5SDimitry Andric 316480093f4SDimitry Andric for (const auto *Operand : LinkerOptions->operands()) { 3170b57cec5SDimitry Andric if (cast<MDNode>(Operand)->getNumOperands() != 2) 3180b57cec5SDimitry Andric report_fatal_error("invalid llvm.linker.options"); 3190b57cec5SDimitry Andric for (const auto &Option : cast<MDNode>(Operand)->operands()) { 3205ffd83dbSDimitry Andric Streamer.emitBytes(cast<MDString>(Option)->getString()); 3215ffd83dbSDimitry Andric Streamer.emitInt8(0); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) { 3270b57cec5SDimitry Andric auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES, 328*5f7ddb14SDimitry Andric ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric Streamer.SwitchSection(S); 3310b57cec5SDimitry Andric 332480093f4SDimitry Andric for (const auto *Operand : DependentLibraries->operands()) { 3335ffd83dbSDimitry Andric Streamer.emitBytes( 3340b57cec5SDimitry Andric cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString()); 3355ffd83dbSDimitry Andric Streamer.emitInt8(0); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 339af732203SDimitry Andric if (NamedMDNode *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName)) { 340af732203SDimitry Andric // Emit a descriptor for every function including functions that have an 341af732203SDimitry Andric // available external linkage. We may not want this for imported functions 342af732203SDimitry Andric // that has code in another thinLTO module but we don't have a good way to 343af732203SDimitry Andric // tell them apart from inline functions defined in header files. Therefore 344af732203SDimitry Andric // we put each descriptor in a separate comdat section and rely on the 345af732203SDimitry Andric // linker to deduplicate. 346af732203SDimitry Andric for (const auto *Operand : FuncInfo->operands()) { 347af732203SDimitry Andric const auto *MD = cast<MDNode>(Operand); 348af732203SDimitry Andric auto *GUID = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0)); 349af732203SDimitry Andric auto *Hash = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); 350af732203SDimitry Andric auto *Name = cast<MDString>(MD->getOperand(2)); 351af732203SDimitry Andric auto *S = C.getObjectFileInfo()->getPseudoProbeDescSection( 352af732203SDimitry Andric TM->getFunctionSections() ? Name->getString() : StringRef()); 353af732203SDimitry Andric 354af732203SDimitry Andric Streamer.SwitchSection(S); 355af732203SDimitry Andric Streamer.emitInt64(GUID->getZExtValue()); 356af732203SDimitry Andric Streamer.emitInt64(Hash->getZExtValue()); 357af732203SDimitry Andric Streamer.emitULEB128IntValue(Name->getString().size()); 358af732203SDimitry Andric Streamer.emitBytes(Name->getString()); 359af732203SDimitry Andric } 360af732203SDimitry Andric } 361af732203SDimitry Andric 3620b57cec5SDimitry Andric unsigned Version = 0; 3630b57cec5SDimitry Andric unsigned Flags = 0; 3640b57cec5SDimitry Andric StringRef Section; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric GetObjCImageInfo(M, Version, Flags, Section); 3670b57cec5SDimitry Andric if (!Section.empty()) { 3680b57cec5SDimitry Andric auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 3690b57cec5SDimitry Andric Streamer.SwitchSection(S); 3705ffd83dbSDimitry Andric Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 3715ffd83dbSDimitry Andric Streamer.emitInt32(Version); 3725ffd83dbSDimitry Andric Streamer.emitInt32(Flags); 3730b57cec5SDimitry Andric Streamer.AddBlankLine(); 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 376af732203SDimitry Andric emitCGProfileMetadata(Streamer, M); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( 3800b57cec5SDimitry Andric const GlobalValue *GV, const TargetMachine &TM, 3810b57cec5SDimitry Andric MachineModuleInfo *MMI) const { 3820b57cec5SDimitry Andric unsigned Encoding = getPersonalityEncoding(); 3830b57cec5SDimitry Andric if ((Encoding & 0x80) == DW_EH_PE_indirect) 3840b57cec5SDimitry Andric return getContext().getOrCreateSymbol(StringRef("DW.ref.") + 3850b57cec5SDimitry Andric TM.getSymbol(GV)->getName()); 3860b57cec5SDimitry Andric if ((Encoding & 0x70) == DW_EH_PE_absptr) 3870b57cec5SDimitry Andric return TM.getSymbol(GV); 3880b57cec5SDimitry Andric report_fatal_error("We do not support this DWARF encoding yet!"); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric void TargetLoweringObjectFileELF::emitPersonalityValue( 3920b57cec5SDimitry Andric MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { 3930b57cec5SDimitry Andric SmallString<64> NameData("DW.ref."); 3940b57cec5SDimitry Andric NameData += Sym->getName(); 3950b57cec5SDimitry Andric MCSymbolELF *Label = 3960b57cec5SDimitry Andric cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); 3975ffd83dbSDimitry Andric Streamer.emitSymbolAttribute(Label, MCSA_Hidden); 3985ffd83dbSDimitry Andric Streamer.emitSymbolAttribute(Label, MCSA_Weak); 3990b57cec5SDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; 4000b57cec5SDimitry Andric MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(), 4010b57cec5SDimitry Andric ELF::SHT_PROGBITS, Flags, 0); 4020b57cec5SDimitry Andric unsigned Size = DL.getPointerSize(); 4030b57cec5SDimitry Andric Streamer.SwitchSection(Sec); 4045ffd83dbSDimitry Andric Streamer.emitValueToAlignment(DL.getPointerABIAlignment(0).value()); 4055ffd83dbSDimitry Andric Streamer.emitSymbolAttribute(Label, MCSA_ELF_TypeObject); 4060b57cec5SDimitry Andric const MCExpr *E = MCConstantExpr::create(Size, getContext()); 4070b57cec5SDimitry Andric Streamer.emitELFSize(Label, E); 4085ffd83dbSDimitry Andric Streamer.emitLabel(Label); 4090b57cec5SDimitry Andric 4105ffd83dbSDimitry Andric Streamer.emitSymbolValue(Sym, Size); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( 4140b57cec5SDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 4150b57cec5SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 4160b57cec5SDimitry Andric if (Encoding & DW_EH_PE_indirect) { 4170b57cec5SDimitry Andric MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Add information about the stub reference to ELFMMI so that the stub 4220b57cec5SDimitry Andric // gets emitted by the asmprinter. 4230b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 4240b57cec5SDimitry Andric if (!StubSym.getPointer()) { 4250b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(GV); 4260b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric return TargetLoweringObjectFile:: 4300b57cec5SDimitry Andric getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 4310b57cec5SDimitry Andric Encoding & ~DW_EH_PE_indirect, Streamer); 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 4350b57cec5SDimitry Andric MMI, Streamer); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { 4390b57cec5SDimitry Andric // N.B.: The defaults used in here are not the same ones used in MC. 4400b57cec5SDimitry Andric // We follow gcc, MC follows gas. For example, given ".section .eh_frame", 4410b57cec5SDimitry Andric // both gas and MC will produce a section with no flags. Given 4420b57cec5SDimitry Andric // section(".eh_frame") gcc will produce: 4430b57cec5SDimitry Andric // 4440b57cec5SDimitry Andric // .section .eh_frame,"a",@progbits 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, 4475ffd83dbSDimitry Andric /*AddSegmentInfo=*/false) || 4485ffd83dbSDimitry Andric Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF, 449af732203SDimitry Andric /*AddSegmentInfo=*/false) || 450af732203SDimitry Andric Name == ".llvmbc" || Name == ".llvmcmd") 4510b57cec5SDimitry Andric return SectionKind::getMetadata(); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric if (Name.empty() || Name[0] != '.') return K; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric // Default implementation based on some magic section names. 4560b57cec5SDimitry Andric if (Name == ".bss" || 4570b57cec5SDimitry Andric Name.startswith(".bss.") || 4580b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.b.") || 4590b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.b.") || 4600b57cec5SDimitry Andric Name == ".sbss" || 4610b57cec5SDimitry Andric Name.startswith(".sbss.") || 4620b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.sb.") || 4630b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.sb.")) 4640b57cec5SDimitry Andric return SectionKind::getBSS(); 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric if (Name == ".tdata" || 4670b57cec5SDimitry Andric Name.startswith(".tdata.") || 4680b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.td.") || 4690b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.td.")) 4700b57cec5SDimitry Andric return SectionKind::getThreadData(); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric if (Name == ".tbss" || 4730b57cec5SDimitry Andric Name.startswith(".tbss.") || 4740b57cec5SDimitry Andric Name.startswith(".gnu.linkonce.tb.") || 4750b57cec5SDimitry Andric Name.startswith(".llvm.linkonce.tb.")) 4760b57cec5SDimitry Andric return SectionKind::getThreadBSS(); 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric return K; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric static unsigned getELFSectionType(StringRef Name, SectionKind K) { 4820b57cec5SDimitry Andric // Use SHT_NOTE for section whose name starts with ".note" to allow 4830b57cec5SDimitry Andric // emitting ELF notes from C variable declaration. 4840b57cec5SDimitry Andric // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 4850b57cec5SDimitry Andric if (Name.startswith(".note")) 4860b57cec5SDimitry Andric return ELF::SHT_NOTE; 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric if (Name == ".init_array") 4890b57cec5SDimitry Andric return ELF::SHT_INIT_ARRAY; 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric if (Name == ".fini_array") 4920b57cec5SDimitry Andric return ELF::SHT_FINI_ARRAY; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric if (Name == ".preinit_array") 4950b57cec5SDimitry Andric return ELF::SHT_PREINIT_ARRAY; 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric if (K.isBSS() || K.isThreadBSS()) 4980b57cec5SDimitry Andric return ELF::SHT_NOBITS; 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric return ELF::SHT_PROGBITS; 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric static unsigned getELFSectionFlags(SectionKind K) { 5040b57cec5SDimitry Andric unsigned Flags = 0; 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric if (!K.isMetadata()) 5070b57cec5SDimitry Andric Flags |= ELF::SHF_ALLOC; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric if (K.isText()) 5100b57cec5SDimitry Andric Flags |= ELF::SHF_EXECINSTR; 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric if (K.isExecuteOnly()) 5130b57cec5SDimitry Andric Flags |= ELF::SHF_ARM_PURECODE; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric if (K.isWriteable()) 5160b57cec5SDimitry Andric Flags |= ELF::SHF_WRITE; 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric if (K.isThreadLocal()) 5190b57cec5SDimitry Andric Flags |= ELF::SHF_TLS; 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric if (K.isMergeableCString() || K.isMergeableConst()) 5220b57cec5SDimitry Andric Flags |= ELF::SHF_MERGE; 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric if (K.isMergeableCString()) 5250b57cec5SDimitry Andric Flags |= ELF::SHF_STRINGS; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric return Flags; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric static const Comdat *getELFComdat(const GlobalValue *GV) { 5310b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 5320b57cec5SDimitry Andric if (!C) 5330b57cec5SDimitry Andric return nullptr; 5340b57cec5SDimitry Andric 535*5f7ddb14SDimitry Andric if (C->getSelectionKind() != Comdat::Any && 536*5f7ddb14SDimitry Andric C->getSelectionKind() != Comdat::NoDeduplicate) 537*5f7ddb14SDimitry Andric report_fatal_error("ELF COMDATs only support SelectionKind::Any and " 538*5f7ddb14SDimitry Andric "SelectionKind::NoDeduplicate, '" + 5390b57cec5SDimitry Andric C->getName() + "' cannot be lowered."); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric return C; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5445ffd83dbSDimitry Andric static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO, 5450b57cec5SDimitry Andric const TargetMachine &TM) { 5460b57cec5SDimitry Andric MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); 5470b57cec5SDimitry Andric if (!MD) 5480b57cec5SDimitry Andric return nullptr; 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric const MDOperand &Op = MD->getOperand(0); 5510b57cec5SDimitry Andric if (!Op.get()) 5520b57cec5SDimitry Andric return nullptr; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric auto *VM = dyn_cast<ValueAsMetadata>(Op); 5550b57cec5SDimitry Andric if (!VM) 5560b57cec5SDimitry Andric report_fatal_error("MD_associated operand is not ValueAsMetadata"); 5570b57cec5SDimitry Andric 5588bcb0991SDimitry Andric auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue()); 5598bcb0991SDimitry Andric return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr; 5600b57cec5SDimitry Andric } 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric static unsigned getEntrySizeForKind(SectionKind Kind) { 5630b57cec5SDimitry Andric if (Kind.isMergeable1ByteCString()) 5640b57cec5SDimitry Andric return 1; 5650b57cec5SDimitry Andric else if (Kind.isMergeable2ByteCString()) 5660b57cec5SDimitry Andric return 2; 5670b57cec5SDimitry Andric else if (Kind.isMergeable4ByteCString()) 5680b57cec5SDimitry Andric return 4; 5690b57cec5SDimitry Andric else if (Kind.isMergeableConst4()) 5700b57cec5SDimitry Andric return 4; 5710b57cec5SDimitry Andric else if (Kind.isMergeableConst8()) 5720b57cec5SDimitry Andric return 8; 5730b57cec5SDimitry Andric else if (Kind.isMergeableConst16()) 5740b57cec5SDimitry Andric return 16; 5750b57cec5SDimitry Andric else if (Kind.isMergeableConst32()) 5760b57cec5SDimitry Andric return 32; 5770b57cec5SDimitry Andric else { 5780b57cec5SDimitry Andric // We shouldn't have mergeable C strings or mergeable constants that we 5790b57cec5SDimitry Andric // didn't handle above. 5800b57cec5SDimitry Andric assert(!Kind.isMergeableCString() && "unknown string width"); 5810b57cec5SDimitry Andric assert(!Kind.isMergeableConst() && "unknown data width"); 5820b57cec5SDimitry Andric return 0; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric 5865ffd83dbSDimitry Andric /// Return the section prefix name used by options FunctionsSections and 5875ffd83dbSDimitry Andric /// DataSections. 5885ffd83dbSDimitry Andric static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 5895ffd83dbSDimitry Andric if (Kind.isText()) 5905ffd83dbSDimitry Andric return ".text"; 5915ffd83dbSDimitry Andric if (Kind.isReadOnly()) 5925ffd83dbSDimitry Andric return ".rodata"; 5935ffd83dbSDimitry Andric if (Kind.isBSS()) 5945ffd83dbSDimitry Andric return ".bss"; 5955ffd83dbSDimitry Andric if (Kind.isThreadData()) 5965ffd83dbSDimitry Andric return ".tdata"; 5975ffd83dbSDimitry Andric if (Kind.isThreadBSS()) 5985ffd83dbSDimitry Andric return ".tbss"; 5995ffd83dbSDimitry Andric if (Kind.isData()) 6005ffd83dbSDimitry Andric return ".data"; 6015ffd83dbSDimitry Andric if (Kind.isReadOnlyWithRel()) 6025ffd83dbSDimitry Andric return ".data.rel.ro"; 6035ffd83dbSDimitry Andric llvm_unreachable("Unknown section kind"); 6045ffd83dbSDimitry Andric } 6055ffd83dbSDimitry Andric 6065ffd83dbSDimitry Andric static SmallString<128> 6075ffd83dbSDimitry Andric getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind, 6085ffd83dbSDimitry Andric Mangler &Mang, const TargetMachine &TM, 6095ffd83dbSDimitry Andric unsigned EntrySize, bool UniqueSectionName) { 6105ffd83dbSDimitry Andric SmallString<128> Name; 6115ffd83dbSDimitry Andric if (Kind.isMergeableCString()) { 6125ffd83dbSDimitry Andric // We also need alignment here. 6135ffd83dbSDimitry Andric // FIXME: this is getting the alignment of the character, not the 6145ffd83dbSDimitry Andric // alignment of the global! 6155ffd83dbSDimitry Andric Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 6165ffd83dbSDimitry Andric cast<GlobalVariable>(GO)); 6175ffd83dbSDimitry Andric 6185ffd83dbSDimitry Andric std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 6195ffd83dbSDimitry Andric Name = SizeSpec + utostr(Alignment.value()); 6205ffd83dbSDimitry Andric } else if (Kind.isMergeableConst()) { 6215ffd83dbSDimitry Andric Name = ".rodata.cst"; 6225ffd83dbSDimitry Andric Name += utostr(EntrySize); 6235ffd83dbSDimitry Andric } else { 6245ffd83dbSDimitry Andric Name = getSectionPrefixForGlobal(Kind); 6255ffd83dbSDimitry Andric } 6265ffd83dbSDimitry Andric 6275ffd83dbSDimitry Andric bool HasPrefix = false; 6285ffd83dbSDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) { 6295ffd83dbSDimitry Andric if (Optional<StringRef> Prefix = F->getSectionPrefix()) { 630af732203SDimitry Andric raw_svector_ostream(Name) << '.' << *Prefix; 6315ffd83dbSDimitry Andric HasPrefix = true; 6325ffd83dbSDimitry Andric } 6335ffd83dbSDimitry Andric } 6345ffd83dbSDimitry Andric 6355ffd83dbSDimitry Andric if (UniqueSectionName) { 6365ffd83dbSDimitry Andric Name.push_back('.'); 6375ffd83dbSDimitry Andric TM.getNameWithPrefix(Name, GO, Mang, /*MayAlwaysUsePrivate*/true); 6385ffd83dbSDimitry Andric } else if (HasPrefix) 639*5f7ddb14SDimitry Andric // For distinguishing between .text.${text-section-prefix}. (with trailing 640*5f7ddb14SDimitry Andric // dot) and .text.${function-name} 6415ffd83dbSDimitry Andric Name.push_back('.'); 6425ffd83dbSDimitry Andric return Name; 6435ffd83dbSDimitry Andric } 6445ffd83dbSDimitry Andric 6455ffd83dbSDimitry Andric namespace { 6465ffd83dbSDimitry Andric class LoweringDiagnosticInfo : public DiagnosticInfo { 6475ffd83dbSDimitry Andric const Twine &Msg; 6485ffd83dbSDimitry Andric 6495ffd83dbSDimitry Andric public: 6505ffd83dbSDimitry Andric LoweringDiagnosticInfo(const Twine &DiagMsg, 6515ffd83dbSDimitry Andric DiagnosticSeverity Severity = DS_Error) 6525ffd83dbSDimitry Andric : DiagnosticInfo(DK_Lowering, Severity), Msg(DiagMsg) {} 6535ffd83dbSDimitry Andric void print(DiagnosticPrinter &DP) const override { DP << Msg; } 6545ffd83dbSDimitry Andric }; 6555ffd83dbSDimitry Andric } 6565ffd83dbSDimitry Andric 657*5f7ddb14SDimitry Andric /// Calculate an appropriate unique ID for a section, and update Flags, 658*5f7ddb14SDimitry Andric /// EntrySize and NextUniqueID where appropriate. 659*5f7ddb14SDimitry Andric static unsigned 660*5f7ddb14SDimitry Andric calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName, 661*5f7ddb14SDimitry Andric SectionKind Kind, const TargetMachine &TM, 662*5f7ddb14SDimitry Andric MCContext &Ctx, Mangler &Mang, unsigned &Flags, 663*5f7ddb14SDimitry Andric unsigned &EntrySize, unsigned &NextUniqueID, 664*5f7ddb14SDimitry Andric const bool Retain, const bool ForceUnique) { 665*5f7ddb14SDimitry Andric // Increment uniqueID if we are forced to emit a unique section. 666*5f7ddb14SDimitry Andric // This works perfectly fine with section attribute or pragma section as the 667*5f7ddb14SDimitry Andric // sections with the same name are grouped together by the assembler. 668*5f7ddb14SDimitry Andric if (ForceUnique) 669*5f7ddb14SDimitry Andric return NextUniqueID++; 670*5f7ddb14SDimitry Andric 671*5f7ddb14SDimitry Andric // A section can have at most one associated section. Put each global with 672*5f7ddb14SDimitry Andric // MD_associated in a unique section. 673*5f7ddb14SDimitry Andric const bool Associated = GO->getMetadata(LLVMContext::MD_associated); 674*5f7ddb14SDimitry Andric if (Associated) { 675*5f7ddb14SDimitry Andric Flags |= ELF::SHF_LINK_ORDER; 676*5f7ddb14SDimitry Andric return NextUniqueID++; 677*5f7ddb14SDimitry Andric } 678*5f7ddb14SDimitry Andric 679*5f7ddb14SDimitry Andric if (Retain) { 680*5f7ddb14SDimitry Andric if (Ctx.getAsmInfo()->useIntegratedAssembler() || 681*5f7ddb14SDimitry Andric Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) 682*5f7ddb14SDimitry Andric Flags |= ELF::SHF_GNU_RETAIN; 683*5f7ddb14SDimitry Andric return NextUniqueID++; 684*5f7ddb14SDimitry Andric } 685*5f7ddb14SDimitry Andric 686*5f7ddb14SDimitry Andric // If two symbols with differing sizes end up in the same mergeable section 687*5f7ddb14SDimitry Andric // that section can be assigned an incorrect entry size. To avoid this we 688*5f7ddb14SDimitry Andric // usually put symbols of the same size into distinct mergeable sections with 689*5f7ddb14SDimitry Andric // the same name. Doing so relies on the ",unique ," assembly feature. This 690*5f7ddb14SDimitry Andric // feature is not avalible until bintuils version 2.35 691*5f7ddb14SDimitry Andric // (https://sourceware.org/bugzilla/show_bug.cgi?id=25380). 692*5f7ddb14SDimitry Andric const bool SupportsUnique = Ctx.getAsmInfo()->useIntegratedAssembler() || 693*5f7ddb14SDimitry Andric Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35); 694*5f7ddb14SDimitry Andric if (!SupportsUnique) { 695*5f7ddb14SDimitry Andric Flags &= ~ELF::SHF_MERGE; 696*5f7ddb14SDimitry Andric EntrySize = 0; 697*5f7ddb14SDimitry Andric return MCContext::GenericSectionID; 698*5f7ddb14SDimitry Andric } 699*5f7ddb14SDimitry Andric 700*5f7ddb14SDimitry Andric const bool SymbolMergeable = Flags & ELF::SHF_MERGE; 701*5f7ddb14SDimitry Andric const bool SeenSectionNameBefore = 702*5f7ddb14SDimitry Andric Ctx.isELFGenericMergeableSection(SectionName); 703*5f7ddb14SDimitry Andric // If this is the first ocurrence of this section name, treat it as the 704*5f7ddb14SDimitry Andric // generic section 705*5f7ddb14SDimitry Andric if (!SymbolMergeable && !SeenSectionNameBefore) 706*5f7ddb14SDimitry Andric return MCContext::GenericSectionID; 707*5f7ddb14SDimitry Andric 708*5f7ddb14SDimitry Andric // Symbols must be placed into sections with compatible entry sizes. Generate 709*5f7ddb14SDimitry Andric // unique sections for symbols that have not been assigned to compatible 710*5f7ddb14SDimitry Andric // sections. 711*5f7ddb14SDimitry Andric const auto PreviousID = 712*5f7ddb14SDimitry Andric Ctx.getELFUniqueIDForEntsize(SectionName, Flags, EntrySize); 713*5f7ddb14SDimitry Andric if (PreviousID) 714*5f7ddb14SDimitry Andric return *PreviousID; 715*5f7ddb14SDimitry Andric 716*5f7ddb14SDimitry Andric // If the user has specified the same section name as would be created 717*5f7ddb14SDimitry Andric // implicitly for this symbol e.g. .rodata.str1.1, then we don't need 718*5f7ddb14SDimitry Andric // to unique the section as the entry size for this symbol will be 719*5f7ddb14SDimitry Andric // compatible with implicitly created sections. 720*5f7ddb14SDimitry Andric SmallString<128> ImplicitSectionNameStem = 721*5f7ddb14SDimitry Andric getELFSectionNameForGlobal(GO, Kind, Mang, TM, EntrySize, false); 722*5f7ddb14SDimitry Andric if (SymbolMergeable && 723*5f7ddb14SDimitry Andric Ctx.isELFImplicitMergeableSectionNamePrefix(SectionName) && 724*5f7ddb14SDimitry Andric SectionName.startswith(ImplicitSectionNameStem)) 725*5f7ddb14SDimitry Andric return MCContext::GenericSectionID; 726*5f7ddb14SDimitry Andric 727*5f7ddb14SDimitry Andric // We have seen this section name before, but with different flags or entity 728*5f7ddb14SDimitry Andric // size. Create a new unique ID. 729*5f7ddb14SDimitry Andric return NextUniqueID++; 730*5f7ddb14SDimitry Andric } 731*5f7ddb14SDimitry Andric 732*5f7ddb14SDimitry Andric static MCSection *selectExplicitSectionGlobal( 733*5f7ddb14SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM, 734*5f7ddb14SDimitry Andric MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID, 735*5f7ddb14SDimitry Andric bool Retain, bool ForceUnique) { 7360b57cec5SDimitry Andric StringRef SectionName = GO->getSection(); 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric // Check if '#pragma clang section' name is applicable. 7390b57cec5SDimitry Andric // Note that pragma directive overrides -ffunction-section, -fdata-section 7400b57cec5SDimitry Andric // and so section name is exactly as user specified and not uniqued. 7410b57cec5SDimitry Andric const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); 7420b57cec5SDimitry Andric if (GV && GV->hasImplicitSection()) { 7430b57cec5SDimitry Andric auto Attrs = GV->getAttributes(); 7440b57cec5SDimitry Andric if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { 7450b57cec5SDimitry Andric SectionName = Attrs.getAttribute("bss-section").getValueAsString(); 7460b57cec5SDimitry Andric } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { 7470b57cec5SDimitry Andric SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); 7488bcb0991SDimitry Andric } else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) { 7498bcb0991SDimitry Andric SectionName = Attrs.getAttribute("relro-section").getValueAsString(); 7500b57cec5SDimitry Andric } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { 7510b57cec5SDimitry Andric SectionName = Attrs.getAttribute("data-section").getValueAsString(); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric const Function *F = dyn_cast<Function>(GO); 7550b57cec5SDimitry Andric if (F && F->hasFnAttribute("implicit-section-name")) { 7560b57cec5SDimitry Andric SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // Infer section flags from the section name if we can. 7600b57cec5SDimitry Andric Kind = getELFKindForNamedSection(SectionName, Kind); 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric StringRef Group = ""; 763*5f7ddb14SDimitry Andric bool IsComdat = false; 7640b57cec5SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 7650b57cec5SDimitry Andric if (const Comdat *C = getELFComdat(GO)) { 7660b57cec5SDimitry Andric Group = C->getName(); 767*5f7ddb14SDimitry Andric IsComdat = C->getSelectionKind() == Comdat::Any; 7680b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric 7715ffd83dbSDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 772*5f7ddb14SDimitry Andric const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize( 773*5f7ddb14SDimitry Andric GO, SectionName, Kind, TM, Ctx, Mang, Flags, EntrySize, NextUniqueID, 774*5f7ddb14SDimitry Andric Retain, ForceUnique); 7755ffd83dbSDimitry Andric 7765ffd83dbSDimitry Andric const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); 777*5f7ddb14SDimitry Andric MCSectionELF *Section = Ctx.getELFSection( 778*5f7ddb14SDimitry Andric SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize, 779*5f7ddb14SDimitry Andric Group, IsComdat, UniqueID, LinkedToSym); 7800b57cec5SDimitry Andric // Make sure that we did not get some other section with incompatible sh_link. 7810b57cec5SDimitry Andric // This should not be possible due to UniqueID code above. 7825ffd83dbSDimitry Andric assert(Section->getLinkedToSymbol() == LinkedToSym && 7830b57cec5SDimitry Andric "Associated symbol mismatch between sections"); 7845ffd83dbSDimitry Andric 785*5f7ddb14SDimitry Andric if (!(Ctx.getAsmInfo()->useIntegratedAssembler() || 786*5f7ddb14SDimitry Andric Ctx.getAsmInfo()->binutilsIsAtLeast(2, 35))) { 787af732203SDimitry Andric // If we are using GNU as before 2.35, then this symbol might have 7885ffd83dbSDimitry Andric // been placed in an incompatible mergeable section. Emit an error if this 7895ffd83dbSDimitry Andric // is the case to avoid creating broken output. 7905ffd83dbSDimitry Andric if ((Section->getFlags() & ELF::SHF_MERGE) && 7915ffd83dbSDimitry Andric (Section->getEntrySize() != getEntrySizeForKind(Kind))) 7925ffd83dbSDimitry Andric GO->getContext().diagnose(LoweringDiagnosticInfo( 7935ffd83dbSDimitry Andric "Symbol '" + GO->getName() + "' from module '" + 7945ffd83dbSDimitry Andric (GO->getParent() ? GO->getParent()->getSourceFileName() : "unknown") + 7955ffd83dbSDimitry Andric "' required a section with entry-size=" + 7965ffd83dbSDimitry Andric Twine(getEntrySizeForKind(Kind)) + " but was placed in section '" + 7975ffd83dbSDimitry Andric SectionName + "' with entry-size=" + Twine(Section->getEntrySize()) + 7985ffd83dbSDimitry Andric ": Explicit assignment by pragma or attribute of an incompatible " 7995ffd83dbSDimitry Andric "symbol to this section?")); 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric 8025ffd83dbSDimitry Andric return Section; 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 805*5f7ddb14SDimitry Andric MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( 806*5f7ddb14SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 807*5f7ddb14SDimitry Andric return selectExplicitSectionGlobal(GO, Kind, TM, getContext(), getMangler(), 808*5f7ddb14SDimitry Andric NextUniqueID, Used.count(GO), 809*5f7ddb14SDimitry Andric /* ForceUnique = */false); 810*5f7ddb14SDimitry Andric } 811*5f7ddb14SDimitry Andric 8120b57cec5SDimitry Andric static MCSectionELF *selectELFSectionForGlobal( 8130b57cec5SDimitry Andric MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 8140b57cec5SDimitry Andric const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, 8150b57cec5SDimitry Andric unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric StringRef Group = ""; 818*5f7ddb14SDimitry Andric bool IsComdat = false; 8190b57cec5SDimitry Andric if (const Comdat *C = getELFComdat(GO)) { 8200b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 8210b57cec5SDimitry Andric Group = C->getName(); 822*5f7ddb14SDimitry Andric IsComdat = C->getSelectionKind() == Comdat::Any; 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric // Get the section entry size based on the kind. 8260b57cec5SDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 8270b57cec5SDimitry Andric 8285ffd83dbSDimitry Andric bool UniqueSectionName = false; 8290b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 8300b57cec5SDimitry Andric if (EmitUniqueSection) { 8310b57cec5SDimitry Andric if (TM.getUniqueSectionNames()) { 8325ffd83dbSDimitry Andric UniqueSectionName = true; 8330b57cec5SDimitry Andric } else { 8340b57cec5SDimitry Andric UniqueID = *NextUniqueID; 8350b57cec5SDimitry Andric (*NextUniqueID)++; 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric } 8385ffd83dbSDimitry Andric SmallString<128> Name = getELFSectionNameForGlobal( 8395ffd83dbSDimitry Andric GO, Kind, Mang, TM, EntrySize, UniqueSectionName); 8405ffd83dbSDimitry Andric 8410b57cec5SDimitry Andric // Use 0 as the unique ID for execute-only text. 8420b57cec5SDimitry Andric if (Kind.isExecuteOnly()) 8430b57cec5SDimitry Andric UniqueID = 0; 8440b57cec5SDimitry Andric return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 845*5f7ddb14SDimitry Andric EntrySize, Group, IsComdat, UniqueID, 846*5f7ddb14SDimitry Andric AssociatedSymbol); 847*5f7ddb14SDimitry Andric } 848*5f7ddb14SDimitry Andric 849*5f7ddb14SDimitry Andric static MCSection *selectELFSectionForGlobal( 850*5f7ddb14SDimitry Andric MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 851*5f7ddb14SDimitry Andric const TargetMachine &TM, bool Retain, bool EmitUniqueSection, 852*5f7ddb14SDimitry Andric unsigned Flags, unsigned *NextUniqueID) { 853*5f7ddb14SDimitry Andric const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); 854*5f7ddb14SDimitry Andric if (LinkedToSym) { 855*5f7ddb14SDimitry Andric EmitUniqueSection = true; 856*5f7ddb14SDimitry Andric Flags |= ELF::SHF_LINK_ORDER; 857*5f7ddb14SDimitry Andric } 858*5f7ddb14SDimitry Andric if (Retain && (Ctx.getAsmInfo()->useIntegratedAssembler() || 859*5f7ddb14SDimitry Andric Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36))) { 860*5f7ddb14SDimitry Andric EmitUniqueSection = true; 861*5f7ddb14SDimitry Andric Flags |= ELF::SHF_GNU_RETAIN; 862*5f7ddb14SDimitry Andric } 863*5f7ddb14SDimitry Andric 864*5f7ddb14SDimitry Andric MCSectionELF *Section = selectELFSectionForGlobal( 865*5f7ddb14SDimitry Andric Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags, 866*5f7ddb14SDimitry Andric NextUniqueID, LinkedToSym); 867*5f7ddb14SDimitry Andric assert(Section->getLinkedToSymbol() == LinkedToSym); 868*5f7ddb14SDimitry Andric return Section; 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 8720b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 8730b57cec5SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the 8760b57cec5SDimitry Andric // global value to a uniqued section specifically for it. 8770b57cec5SDimitry Andric bool EmitUniqueSection = false; 8780b57cec5SDimitry Andric if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 8790b57cec5SDimitry Andric if (Kind.isText()) 8800b57cec5SDimitry Andric EmitUniqueSection = TM.getFunctionSections(); 8810b57cec5SDimitry Andric else 8820b57cec5SDimitry Andric EmitUniqueSection = TM.getDataSections(); 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric EmitUniqueSection |= GO->hasComdat(); 885*5f7ddb14SDimitry Andric return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 886*5f7ddb14SDimitry Andric Used.count(GO), EmitUniqueSection, Flags, 887*5f7ddb14SDimitry Andric &NextUniqueID); 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric 890*5f7ddb14SDimitry Andric MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction( 891*5f7ddb14SDimitry Andric const Function &F, const TargetMachine &TM) const { 892*5f7ddb14SDimitry Andric SectionKind Kind = SectionKind::getText(); 893*5f7ddb14SDimitry Andric unsigned Flags = getELFSectionFlags(Kind); 894*5f7ddb14SDimitry Andric // If the function's section names is pre-determined via pragma or a 895*5f7ddb14SDimitry Andric // section attribute, call selectExplicitSectionGlobal. 896*5f7ddb14SDimitry Andric if (F.hasSection() || F.hasFnAttribute("implicit-section-name")) 897*5f7ddb14SDimitry Andric return selectExplicitSectionGlobal( 898*5f7ddb14SDimitry Andric &F, Kind, TM, getContext(), getMangler(), NextUniqueID, 899*5f7ddb14SDimitry Andric Used.count(&F), /* ForceUnique = */true); 900*5f7ddb14SDimitry Andric else 901*5f7ddb14SDimitry Andric return selectELFSectionForGlobal( 902*5f7ddb14SDimitry Andric getContext(), &F, Kind, getMangler(), TM, Used.count(&F), 903*5f7ddb14SDimitry Andric /*EmitUniqueSection=*/true, Flags, &NextUniqueID); 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 9070b57cec5SDimitry Andric const Function &F, const TargetMachine &TM) const { 9080b57cec5SDimitry Andric // If the function can be removed, produce a unique section so that 9090b57cec5SDimitry Andric // the table doesn't prevent the removal. 9100b57cec5SDimitry Andric const Comdat *C = F.getComdat(); 9110b57cec5SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 9120b57cec5SDimitry Andric if (!EmitUniqueSection) 9130b57cec5SDimitry Andric return ReadOnlySection; 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 9160b57cec5SDimitry Andric getMangler(), TM, EmitUniqueSection, 9170b57cec5SDimitry Andric ELF::SHF_ALLOC, &NextUniqueID, 9180b57cec5SDimitry Andric /* AssociatedSymbol */ nullptr); 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 921*5f7ddb14SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForLSDA( 922*5f7ddb14SDimitry Andric const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const { 923af732203SDimitry Andric // If neither COMDAT nor function sections, use the monolithic LSDA section. 924af732203SDimitry Andric // Re-use this path if LSDASection is null as in the Arm EHABI. 925af732203SDimitry Andric if (!LSDASection || (!F.hasComdat() && !TM.getFunctionSections())) 926af732203SDimitry Andric return LSDASection; 927af732203SDimitry Andric 928af732203SDimitry Andric const auto *LSDA = cast<MCSectionELF>(LSDASection); 929af732203SDimitry Andric unsigned Flags = LSDA->getFlags(); 930*5f7ddb14SDimitry Andric const MCSymbolELF *LinkedToSym = nullptr; 931af732203SDimitry Andric StringRef Group; 932*5f7ddb14SDimitry Andric bool IsComdat = false; 933*5f7ddb14SDimitry Andric if (const Comdat *C = getELFComdat(&F)) { 934af732203SDimitry Andric Flags |= ELF::SHF_GROUP; 935*5f7ddb14SDimitry Andric Group = C->getName(); 936*5f7ddb14SDimitry Andric IsComdat = C->getSelectionKind() == Comdat::Any; 937*5f7ddb14SDimitry Andric } 938*5f7ddb14SDimitry Andric // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36 939*5f7ddb14SDimitry Andric // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER. 940*5f7ddb14SDimitry Andric if (TM.getFunctionSections() && 941*5f7ddb14SDimitry Andric (getContext().getAsmInfo()->useIntegratedAssembler() && 942*5f7ddb14SDimitry Andric getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) { 943*5f7ddb14SDimitry Andric Flags |= ELF::SHF_LINK_ORDER; 944*5f7ddb14SDimitry Andric LinkedToSym = cast<MCSymbolELF>(&FnSym); 945af732203SDimitry Andric } 946af732203SDimitry Andric 947af732203SDimitry Andric // Append the function name as the suffix like GCC, assuming 948af732203SDimitry Andric // -funique-section-names applies to .gcc_except_table sections. 949*5f7ddb14SDimitry Andric return getContext().getELFSection( 950*5f7ddb14SDimitry Andric (TM.getUniqueSectionNames() ? LSDA->getName() + "." + F.getName() 951*5f7ddb14SDimitry Andric : LSDA->getName()), 952*5f7ddb14SDimitry Andric LSDA->getType(), Flags, 0, Group, IsComdat, MCSection::NonUniqueID, 953*5f7ddb14SDimitry Andric LinkedToSym); 954af732203SDimitry Andric } 955af732203SDimitry Andric 9560b57cec5SDimitry Andric bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 9570b57cec5SDimitry Andric bool UsesLabelDifference, const Function &F) const { 9580b57cec5SDimitry Andric // We can always create relative relocations, so use another section 9590b57cec5SDimitry Andric // that can be marked non-executable. 9600b57cec5SDimitry Andric return false; 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric /// Given a mergeable constant with the specified size and relocation 9640b57cec5SDimitry Andric /// information, return a section that it should be placed in. 9650b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 9660b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 9675ffd83dbSDimitry Andric Align &Alignment) const { 9680b57cec5SDimitry Andric if (Kind.isMergeableConst4() && MergeableConst4Section) 9690b57cec5SDimitry Andric return MergeableConst4Section; 9700b57cec5SDimitry Andric if (Kind.isMergeableConst8() && MergeableConst8Section) 9710b57cec5SDimitry Andric return MergeableConst8Section; 9720b57cec5SDimitry Andric if (Kind.isMergeableConst16() && MergeableConst16Section) 9730b57cec5SDimitry Andric return MergeableConst16Section; 9740b57cec5SDimitry Andric if (Kind.isMergeableConst32() && MergeableConst32Section) 9750b57cec5SDimitry Andric return MergeableConst32Section; 9760b57cec5SDimitry Andric if (Kind.isReadOnly()) 9770b57cec5SDimitry Andric return ReadOnlySection; 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 9800b57cec5SDimitry Andric return DataRelROSection; 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric 9835ffd83dbSDimitry Andric /// Returns a unique section for the given machine basic block. 9845ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock( 9855ffd83dbSDimitry Andric const Function &F, const MachineBasicBlock &MBB, 9865ffd83dbSDimitry Andric const TargetMachine &TM) const { 9875ffd83dbSDimitry Andric assert(MBB.isBeginSection() && "Basic block does not start a section!"); 9885ffd83dbSDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 9895ffd83dbSDimitry Andric 990af732203SDimitry Andric // For cold sections use the .text.split. prefix along with the parent 9915ffd83dbSDimitry Andric // function name. All cold blocks for the same function go to the same 9925ffd83dbSDimitry Andric // section. Similarly all exception blocks are grouped by symbol name 9935ffd83dbSDimitry Andric // under the .text.eh prefix. For regular sections, we either use a unique 9945ffd83dbSDimitry Andric // name, or a unique ID for the section. 9955ffd83dbSDimitry Andric SmallString<128> Name; 9965ffd83dbSDimitry Andric if (MBB.getSectionID() == MBBSectionID::ColdSectionID) { 997af732203SDimitry Andric Name += BBSectionsColdTextPrefix; 9985ffd83dbSDimitry Andric Name += MBB.getParent()->getName(); 9995ffd83dbSDimitry Andric } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) { 10005ffd83dbSDimitry Andric Name += ".text.eh."; 10015ffd83dbSDimitry Andric Name += MBB.getParent()->getName(); 10025ffd83dbSDimitry Andric } else { 10035ffd83dbSDimitry Andric Name += MBB.getParent()->getSection()->getName(); 10045ffd83dbSDimitry Andric if (TM.getUniqueBasicBlockSectionNames()) { 1005*5f7ddb14SDimitry Andric if (!Name.endswith(".")) 10065ffd83dbSDimitry Andric Name += "."; 10075ffd83dbSDimitry Andric Name += MBB.getSymbol()->getName(); 10085ffd83dbSDimitry Andric } else { 10095ffd83dbSDimitry Andric UniqueID = NextUniqueID++; 10105ffd83dbSDimitry Andric } 10115ffd83dbSDimitry Andric } 10125ffd83dbSDimitry Andric 10135ffd83dbSDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; 1014af732203SDimitry Andric std::string GroupName; 10155ffd83dbSDimitry Andric if (F.hasComdat()) { 10165ffd83dbSDimitry Andric Flags |= ELF::SHF_GROUP; 10175ffd83dbSDimitry Andric GroupName = F.getComdat()->getName().str(); 10185ffd83dbSDimitry Andric } 10195ffd83dbSDimitry Andric return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 1020*5f7ddb14SDimitry Andric 0 /* Entry Size */, GroupName, 1021*5f7ddb14SDimitry Andric F.hasComdat(), UniqueID, nullptr); 10225ffd83dbSDimitry Andric } 10235ffd83dbSDimitry Andric 10240b57cec5SDimitry Andric static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 10250b57cec5SDimitry Andric bool IsCtor, unsigned Priority, 10260b57cec5SDimitry Andric const MCSymbol *KeySym) { 10270b57cec5SDimitry Andric std::string Name; 10280b57cec5SDimitry Andric unsigned Type; 10290b57cec5SDimitry Andric unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 1030*5f7ddb14SDimitry Andric StringRef Comdat = KeySym ? KeySym->getName() : ""; 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric if (KeySym) 10330b57cec5SDimitry Andric Flags |= ELF::SHF_GROUP; 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric if (UseInitArray) { 10360b57cec5SDimitry Andric if (IsCtor) { 10370b57cec5SDimitry Andric Type = ELF::SHT_INIT_ARRAY; 10380b57cec5SDimitry Andric Name = ".init_array"; 10390b57cec5SDimitry Andric } else { 10400b57cec5SDimitry Andric Type = ELF::SHT_FINI_ARRAY; 10410b57cec5SDimitry Andric Name = ".fini_array"; 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric if (Priority != 65535) { 10440b57cec5SDimitry Andric Name += '.'; 10450b57cec5SDimitry Andric Name += utostr(Priority); 10460b57cec5SDimitry Andric } 10470b57cec5SDimitry Andric } else { 10480b57cec5SDimitry Andric // The default scheme is .ctor / .dtor, so we have to invert the priority 10490b57cec5SDimitry Andric // numbering. 10500b57cec5SDimitry Andric if (IsCtor) 10510b57cec5SDimitry Andric Name = ".ctors"; 10520b57cec5SDimitry Andric else 10530b57cec5SDimitry Andric Name = ".dtors"; 10540b57cec5SDimitry Andric if (Priority != 65535) 10550b57cec5SDimitry Andric raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 10560b57cec5SDimitry Andric Type = ELF::SHT_PROGBITS; 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 1059*5f7ddb14SDimitry Andric return Ctx.getELFSection(Name, Type, Flags, 0, Comdat, /*IsComdat=*/true); 10600b57cec5SDimitry Andric } 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 10630b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 10640b57cec5SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 10650b57cec5SDimitry Andric KeySym); 10660b57cec5SDimitry Andric } 10670b57cec5SDimitry Andric 10680b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 10690b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 10700b57cec5SDimitry Andric return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 10710b57cec5SDimitry Andric KeySym); 10720b57cec5SDimitry Andric } 10730b57cec5SDimitry Andric 10740b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 10750b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 10760b57cec5SDimitry Andric const TargetMachine &TM) const { 10770b57cec5SDimitry Andric // We may only use a PLT-relative relocation to refer to unnamed_addr 10780b57cec5SDimitry Andric // functions. 10790b57cec5SDimitry Andric if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 10800b57cec5SDimitry Andric return nullptr; 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric // Basic sanity checks. 10830b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 10840b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 10850b57cec5SDimitry Andric RHS->isThreadLocal()) 10860b57cec5SDimitry Andric return nullptr; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric return MCBinaryExpr::createSub( 10890b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, 10900b57cec5SDimitry Andric getContext()), 10910b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric 1094af732203SDimitry Andric const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent( 1095af732203SDimitry Andric const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const { 1096af732203SDimitry Andric assert(supportDSOLocalEquivalentLowering()); 1097af732203SDimitry Andric 1098af732203SDimitry Andric const auto *GV = Equiv->getGlobalValue(); 1099af732203SDimitry Andric 1100af732203SDimitry Andric // A PLT entry is not needed for dso_local globals. 1101af732203SDimitry Andric if (GV->isDSOLocal() || GV->isImplicitDSOLocal()) 1102af732203SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); 1103af732203SDimitry Andric 1104af732203SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeVariantKind, 1105af732203SDimitry Andric getContext()); 1106af732203SDimitry Andric } 1107af732203SDimitry Andric 11080b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { 11090b57cec5SDimitry Andric // Use ".GCC.command.line" since this feature is to support clang's 11100b57cec5SDimitry Andric // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the 11110b57cec5SDimitry Andric // same name. 11120b57cec5SDimitry Andric return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, 1113*5f7ddb14SDimitry Andric ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); 11140b57cec5SDimitry Andric } 11150b57cec5SDimitry Andric 11160b57cec5SDimitry Andric void 11170b57cec5SDimitry Andric TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 11180b57cec5SDimitry Andric UseInitArray = UseInitArray_; 11190b57cec5SDimitry Andric MCContext &Ctx = getContext(); 11200b57cec5SDimitry Andric if (!UseInitArray) { 11210b57cec5SDimitry Andric StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, 11220b57cec5SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_WRITE); 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, 11250b57cec5SDimitry Andric ELF::SHF_ALLOC | ELF::SHF_WRITE); 11260b57cec5SDimitry Andric return; 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 11300b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC); 11310b57cec5SDimitry Andric StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 11320b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC); 11330b57cec5SDimitry Andric } 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 11360b57cec5SDimitry Andric // MachO 11370b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 11380b57cec5SDimitry Andric 11390b57cec5SDimitry Andric TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 11400b57cec5SDimitry Andric : TargetLoweringObjectFile() { 11410b57cec5SDimitry Andric SupportIndirectSymViaGOTPCRel = true; 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric 11440b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 11450b57cec5SDimitry Andric const TargetMachine &TM) { 11460b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TM); 11470b57cec5SDimitry Andric if (TM.getRelocationModel() == Reloc::Static) { 11480b57cec5SDimitry Andric StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, 11490b57cec5SDimitry Andric SectionKind::getData()); 11500b57cec5SDimitry Andric StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, 11510b57cec5SDimitry Andric SectionKind::getData()); 11520b57cec5SDimitry Andric } else { 11530b57cec5SDimitry Andric StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", 11540b57cec5SDimitry Andric MachO::S_MOD_INIT_FUNC_POINTERS, 11550b57cec5SDimitry Andric SectionKind::getData()); 11560b57cec5SDimitry Andric StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", 11570b57cec5SDimitry Andric MachO::S_MOD_TERM_FUNC_POINTERS, 11580b57cec5SDimitry Andric SectionKind::getData()); 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric PersonalityEncoding = 11620b57cec5SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 11630b57cec5SDimitry Andric LSDAEncoding = dwarf::DW_EH_PE_pcrel; 11640b57cec5SDimitry Andric TTypeEncoding = 11650b57cec5SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, 11690b57cec5SDimitry Andric Module &M) const { 11700b57cec5SDimitry Andric // Emit the linker options if present. 11710b57cec5SDimitry Andric if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1172480093f4SDimitry Andric for (const auto *Option : LinkerOptions->operands()) { 11730b57cec5SDimitry Andric SmallVector<std::string, 4> StrOptions; 11740b57cec5SDimitry Andric for (const auto &Piece : cast<MDNode>(Option)->operands()) 11755ffd83dbSDimitry Andric StrOptions.push_back(std::string(cast<MDString>(Piece)->getString())); 11765ffd83dbSDimitry Andric Streamer.emitLinkerOptions(StrOptions); 11770b57cec5SDimitry Andric } 11780b57cec5SDimitry Andric } 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric unsigned VersionVal = 0; 11810b57cec5SDimitry Andric unsigned ImageInfoFlags = 0; 11820b57cec5SDimitry Andric StringRef SectionVal; 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric // The section is mandatory. If we don't have it, then we don't have GC info. 11870b57cec5SDimitry Andric if (SectionVal.empty()) 11880b57cec5SDimitry Andric return; 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andric StringRef Segment, Section; 11910b57cec5SDimitry Andric unsigned TAA = 0, StubSize = 0; 11920b57cec5SDimitry Andric bool TAAParsed; 1193*5f7ddb14SDimitry Andric if (Error E = MCSectionMachO::ParseSectionSpecifier( 1194*5f7ddb14SDimitry Andric SectionVal, Segment, Section, TAA, TAAParsed, StubSize)) { 11950b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 1196*5f7ddb14SDimitry Andric report_fatal_error("Invalid section specifier '" + Section + 1197*5f7ddb14SDimitry Andric "': " + toString(std::move(E)) + "."); 1198*5f7ddb14SDimitry Andric } 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric // Get the section. 12010b57cec5SDimitry Andric MCSectionMachO *S = getContext().getMachOSection( 12020b57cec5SDimitry Andric Segment, Section, TAA, StubSize, SectionKind::getData()); 12030b57cec5SDimitry Andric Streamer.SwitchSection(S); 12045ffd83dbSDimitry Andric Streamer.emitLabel(getContext(). 12050b57cec5SDimitry Andric getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 12065ffd83dbSDimitry Andric Streamer.emitInt32(VersionVal); 12075ffd83dbSDimitry Andric Streamer.emitInt32(ImageInfoFlags); 12080b57cec5SDimitry Andric Streamer.AddBlankLine(); 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric static void checkMachOComdat(const GlobalValue *GV) { 12120b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 12130b57cec5SDimitry Andric if (!C) 12140b57cec5SDimitry Andric return; 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 12170b57cec5SDimitry Andric "' cannot be lowered."); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 12210b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1222*5f7ddb14SDimitry Andric 1223*5f7ddb14SDimitry Andric StringRef SectionName = GO->getSection(); 1224*5f7ddb14SDimitry Andric 1225*5f7ddb14SDimitry Andric const Function *F = dyn_cast<Function>(GO); 1226*5f7ddb14SDimitry Andric if (F && F->hasFnAttribute("implicit-section-name")) { 1227*5f7ddb14SDimitry Andric SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); 1228*5f7ddb14SDimitry Andric } 1229*5f7ddb14SDimitry Andric 12300b57cec5SDimitry Andric // Parse the section specifier and create it if valid. 12310b57cec5SDimitry Andric StringRef Segment, Section; 12320b57cec5SDimitry Andric unsigned TAA = 0, StubSize = 0; 12330b57cec5SDimitry Andric bool TAAParsed; 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric checkMachOComdat(GO); 12360b57cec5SDimitry Andric 1237*5f7ddb14SDimitry Andric if (Error E = MCSectionMachO::ParseSectionSpecifier( 1238*5f7ddb14SDimitry Andric SectionName, Segment, Section, TAA, TAAParsed, StubSize)) { 12390b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 12400b57cec5SDimitry Andric report_fatal_error("Global variable '" + GO->getName() + 12410b57cec5SDimitry Andric "' has an invalid section specifier '" + 1242*5f7ddb14SDimitry Andric GO->getSection() + "': " + toString(std::move(E)) + "."); 12430b57cec5SDimitry Andric } 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric // Get the section. 12460b57cec5SDimitry Andric MCSectionMachO *S = 12470b57cec5SDimitry Andric getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric // If TAA wasn't set by ParseSectionSpecifier() above, 12500b57cec5SDimitry Andric // use the value returned by getMachOSection() as a default. 12510b57cec5SDimitry Andric if (!TAAParsed) 12520b57cec5SDimitry Andric TAA = S->getTypeAndAttributes(); 12530b57cec5SDimitry Andric 12540b57cec5SDimitry Andric // Okay, now that we got the section, verify that the TAA & StubSize agree. 12550b57cec5SDimitry Andric // If the user declared multiple globals with different section flags, we need 12560b57cec5SDimitry Andric // to reject it here. 12570b57cec5SDimitry Andric if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 12580b57cec5SDimitry Andric // If invalid, report the error with report_fatal_error. 12590b57cec5SDimitry Andric report_fatal_error("Global variable '" + GO->getName() + 12600b57cec5SDimitry Andric "' section type or attributes does not match previous" 12610b57cec5SDimitry Andric " section specifier"); 12620b57cec5SDimitry Andric } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric return S; 12650b57cec5SDimitry Andric } 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 12680b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 12690b57cec5SDimitry Andric checkMachOComdat(GO); 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric // Handle thread local data. 12720b57cec5SDimitry Andric if (Kind.isThreadBSS()) return TLSBSSSection; 12730b57cec5SDimitry Andric if (Kind.isThreadData()) return TLSDataSection; 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric if (Kind.isText()) 12760b57cec5SDimitry Andric return GO->isWeakForLinker() ? TextCoalSection : TextSection; 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric // If this is weak/linkonce, put this in a coalescable section, either in text 12790b57cec5SDimitry Andric // or data depending on if it is writable. 12800b57cec5SDimitry Andric if (GO->isWeakForLinker()) { 12810b57cec5SDimitry Andric if (Kind.isReadOnly()) 12820b57cec5SDimitry Andric return ConstTextCoalSection; 12830b57cec5SDimitry Andric if (Kind.isReadOnlyWithRel()) 12840b57cec5SDimitry Andric return ConstDataCoalSection; 12850b57cec5SDimitry Andric return DataCoalSection; 12860b57cec5SDimitry Andric } 12870b57cec5SDimitry Andric 12880b57cec5SDimitry Andric // FIXME: Alignment check should be handled by section classifier. 12890b57cec5SDimitry Andric if (Kind.isMergeable1ByteCString() && 12905ffd83dbSDimitry Andric GO->getParent()->getDataLayout().getPreferredAlign( 12915ffd83dbSDimitry Andric cast<GlobalVariable>(GO)) < Align(32)) 12920b57cec5SDimitry Andric return CStringSection; 12930b57cec5SDimitry Andric 12940b57cec5SDimitry Andric // Do not put 16-bit arrays in the UString section if they have an 12950b57cec5SDimitry Andric // externally visible label, this runs into issues with certain linker 12960b57cec5SDimitry Andric // versions. 12970b57cec5SDimitry Andric if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && 12985ffd83dbSDimitry Andric GO->getParent()->getDataLayout().getPreferredAlign( 12995ffd83dbSDimitry Andric cast<GlobalVariable>(GO)) < Align(32)) 13000b57cec5SDimitry Andric return UStringSection; 13010b57cec5SDimitry Andric 13020b57cec5SDimitry Andric // With MachO only variables whose corresponding symbol starts with 'l' or 13030b57cec5SDimitry Andric // 'L' can be merged, so we only try merging GVs with private linkage. 13040b57cec5SDimitry Andric if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { 13050b57cec5SDimitry Andric if (Kind.isMergeableConst4()) 13060b57cec5SDimitry Andric return FourByteConstantSection; 13070b57cec5SDimitry Andric if (Kind.isMergeableConst8()) 13080b57cec5SDimitry Andric return EightByteConstantSection; 13090b57cec5SDimitry Andric if (Kind.isMergeableConst16()) 13100b57cec5SDimitry Andric return SixteenByteConstantSection; 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andric // Otherwise, if it is readonly, but not something we can specially optimize, 13140b57cec5SDimitry Andric // just drop it in .const. 13150b57cec5SDimitry Andric if (Kind.isReadOnly()) 13160b57cec5SDimitry Andric return ReadOnlySection; 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric // If this is marked const, put it into a const section. But if the dynamic 13190b57cec5SDimitry Andric // linker needs to write to it, put it in the data segment. 13200b57cec5SDimitry Andric if (Kind.isReadOnlyWithRel()) 13210b57cec5SDimitry Andric return ConstDataSection; 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric // Put zero initialized globals with strong external linkage in the 13240b57cec5SDimitry Andric // DATA, __common section with the .zerofill directive. 13250b57cec5SDimitry Andric if (Kind.isBSSExtern()) 13260b57cec5SDimitry Andric return DataCommonSection; 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric // Put zero initialized globals with local linkage in __DATA,__bss directive 13290b57cec5SDimitry Andric // with the .zerofill directive (aka .lcomm). 13300b57cec5SDimitry Andric if (Kind.isBSSLocal()) 13310b57cec5SDimitry Andric return DataBSSSection; 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric // Otherwise, just drop the variable in the normal data section. 13340b57cec5SDimitry Andric return DataSection; 13350b57cec5SDimitry Andric } 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 13380b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 13395ffd83dbSDimitry Andric Align &Alignment) const { 13400b57cec5SDimitry Andric // If this constant requires a relocation, we have to put it in the data 13410b57cec5SDimitry Andric // segment, not in the text segment. 13420b57cec5SDimitry Andric if (Kind.isData() || Kind.isReadOnlyWithRel()) 13430b57cec5SDimitry Andric return ConstDataSection; 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andric if (Kind.isMergeableConst4()) 13460b57cec5SDimitry Andric return FourByteConstantSection; 13470b57cec5SDimitry Andric if (Kind.isMergeableConst8()) 13480b57cec5SDimitry Andric return EightByteConstantSection; 13490b57cec5SDimitry Andric if (Kind.isMergeableConst16()) 13500b57cec5SDimitry Andric return SixteenByteConstantSection; 13510b57cec5SDimitry Andric return ReadOnlySection; // .const 13520b57cec5SDimitry Andric } 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 13550b57cec5SDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 13560b57cec5SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 13570b57cec5SDimitry Andric // The mach-o version of this method defaults to returning a stub reference. 13580b57cec5SDimitry Andric 13590b57cec5SDimitry Andric if (Encoding & DW_EH_PE_indirect) { 13600b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 13610b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 13620b57cec5SDimitry Andric 13630b57cec5SDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric // Add information about the stub reference to MachOMMI so that the stub 13660b57cec5SDimitry Andric // gets emitted by the asmprinter. 13670b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 13680b57cec5SDimitry Andric if (!StubSym.getPointer()) { 13690b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(GV); 13700b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric return TargetLoweringObjectFile:: 13740b57cec5SDimitry Andric getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 13750b57cec5SDimitry Andric Encoding & ~DW_EH_PE_indirect, Streamer); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric 13780b57cec5SDimitry Andric return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 13790b57cec5SDimitry Andric MMI, Streamer); 13800b57cec5SDimitry Andric } 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 13830b57cec5SDimitry Andric const GlobalValue *GV, const TargetMachine &TM, 13840b57cec5SDimitry Andric MachineModuleInfo *MMI) const { 13850b57cec5SDimitry Andric // The mach-o version of this method defaults to returning a stub reference. 13860b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 13870b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 13880b57cec5SDimitry Andric 13890b57cec5SDimitry Andric MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 13900b57cec5SDimitry Andric 13910b57cec5SDimitry Andric // Add information about the stub reference to MachOMMI so that the stub 13920b57cec5SDimitry Andric // gets emitted by the asmprinter. 13930b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 13940b57cec5SDimitry Andric if (!StubSym.getPointer()) { 13950b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(GV); 13960b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 13970b57cec5SDimitry Andric } 13980b57cec5SDimitry Andric 13990b57cec5SDimitry Andric return SSym; 14000b57cec5SDimitry Andric } 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 14038bcb0991SDimitry Andric const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, 14048bcb0991SDimitry Andric int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { 14050b57cec5SDimitry Andric // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 14060b57cec5SDimitry Andric // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 14070b57cec5SDimitry Andric // through a non_lazy_ptr stub instead. One advantage is that it allows the 14080b57cec5SDimitry Andric // computation of deltas to final external symbols. Example: 14090b57cec5SDimitry Andric // 14100b57cec5SDimitry Andric // _extgotequiv: 14110b57cec5SDimitry Andric // .long _extfoo 14120b57cec5SDimitry Andric // 14130b57cec5SDimitry Andric // _delta: 14140b57cec5SDimitry Andric // .long _extgotequiv-_delta 14150b57cec5SDimitry Andric // 14160b57cec5SDimitry Andric // is transformed to: 14170b57cec5SDimitry Andric // 14180b57cec5SDimitry Andric // _delta: 14190b57cec5SDimitry Andric // .long L_extfoo$non_lazy_ptr-(_delta+0) 14200b57cec5SDimitry Andric // 14210b57cec5SDimitry Andric // .section __IMPORT,__pointers,non_lazy_symbol_pointers 14220b57cec5SDimitry Andric // L_extfoo$non_lazy_ptr: 14230b57cec5SDimitry Andric // .indirect_symbol _extfoo 14240b57cec5SDimitry Andric // .long 0 14250b57cec5SDimitry Andric // 14260b57cec5SDimitry Andric // The indirect symbol table (and sections of non_lazy_symbol_pointers type) 14270b57cec5SDimitry Andric // may point to both local (same translation unit) and global (other 14280b57cec5SDimitry Andric // translation units) symbols. Example: 14290b57cec5SDimitry Andric // 14300b57cec5SDimitry Andric // .section __DATA,__pointers,non_lazy_symbol_pointers 14310b57cec5SDimitry Andric // L1: 14320b57cec5SDimitry Andric // .indirect_symbol _myGlobal 14330b57cec5SDimitry Andric // .long 0 14340b57cec5SDimitry Andric // L2: 14350b57cec5SDimitry Andric // .indirect_symbol _myLocal 14360b57cec5SDimitry Andric // .long _myLocal 14370b57cec5SDimitry Andric // 14380b57cec5SDimitry Andric // If the symbol is local, instead of the symbol's index, the assembler 14390b57cec5SDimitry Andric // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. 14400b57cec5SDimitry Andric // Then the linker will notice the constant in the table and will look at the 14410b57cec5SDimitry Andric // content of the symbol. 14420b57cec5SDimitry Andric MachineModuleInfoMachO &MachOMMI = 14430b57cec5SDimitry Andric MMI->getObjFileInfo<MachineModuleInfoMachO>(); 14440b57cec5SDimitry Andric MCContext &Ctx = getContext(); 14450b57cec5SDimitry Andric 14460b57cec5SDimitry Andric // The offset must consider the original displacement from the base symbol 14470b57cec5SDimitry Andric // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 14480b57cec5SDimitry Andric Offset = -MV.getConstant(); 14490b57cec5SDimitry Andric const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 14500b57cec5SDimitry Andric 14510b57cec5SDimitry Andric // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 14520b57cec5SDimitry Andric // non_lazy_ptr stubs. 14530b57cec5SDimitry Andric SmallString<128> Name; 14540b57cec5SDimitry Andric StringRef Suffix = "$non_lazy_ptr"; 14550b57cec5SDimitry Andric Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 14560b57cec5SDimitry Andric Name += Sym->getName(); 14570b57cec5SDimitry Andric Name += Suffix; 14580b57cec5SDimitry Andric MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 14590b57cec5SDimitry Andric 14600b57cec5SDimitry Andric MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 14618bcb0991SDimitry Andric 14628bcb0991SDimitry Andric if (!StubSym.getPointer()) 14630b57cec5SDimitry Andric StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), 14648bcb0991SDimitry Andric !GV->hasLocalLinkage()); 14650b57cec5SDimitry Andric 14660b57cec5SDimitry Andric const MCExpr *BSymExpr = 14670b57cec5SDimitry Andric MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 14680b57cec5SDimitry Andric const MCExpr *LHS = 14690b57cec5SDimitry Andric MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 14700b57cec5SDimitry Andric 14710b57cec5SDimitry Andric if (!Offset) 14720b57cec5SDimitry Andric return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 14730b57cec5SDimitry Andric 14740b57cec5SDimitry Andric const MCExpr *RHS = 14750b57cec5SDimitry Andric MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 14760b57cec5SDimitry Andric return MCBinaryExpr::createSub(LHS, RHS, Ctx); 14770b57cec5SDimitry Andric } 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 14800b57cec5SDimitry Andric const MCSection &Section) { 14810b57cec5SDimitry Andric if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 14820b57cec5SDimitry Andric return true; 14830b57cec5SDimitry Andric 1484*5f7ddb14SDimitry Andric // FIXME: we should be able to use private labels for sections that can't be 1485*5f7ddb14SDimitry Andric // dead-stripped (there's no issue with blocking atomization there), but `ld 1486*5f7ddb14SDimitry Andric // -r` sometimes drops the no_dead_strip attribute from sections so for safety 1487*5f7ddb14SDimitry Andric // we don't allow it. 14880b57cec5SDimitry Andric return false; 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric 14910b57cec5SDimitry Andric void TargetLoweringObjectFileMachO::getNameWithPrefix( 14920b57cec5SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 14930b57cec5SDimitry Andric const TargetMachine &TM) const { 14940b57cec5SDimitry Andric bool CannotUsePrivateLabel = true; 14950b57cec5SDimitry Andric if (auto *GO = GV->getBaseObject()) { 14960b57cec5SDimitry Andric SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); 14970b57cec5SDimitry Andric const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); 14980b57cec5SDimitry Andric CannotUsePrivateLabel = 14990b57cec5SDimitry Andric !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 15000b57cec5SDimitry Andric } 15010b57cec5SDimitry Andric getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 15020b57cec5SDimitry Andric } 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15050b57cec5SDimitry Andric // COFF 15060b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15070b57cec5SDimitry Andric 15080b57cec5SDimitry Andric static unsigned 15090b57cec5SDimitry Andric getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { 15100b57cec5SDimitry Andric unsigned Flags = 0; 15110b57cec5SDimitry Andric bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric if (K.isMetadata()) 15140b57cec5SDimitry Andric Flags |= 15150b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_DISCARDABLE; 15160b57cec5SDimitry Andric else if (K.isText()) 15170b57cec5SDimitry Andric Flags |= 15180b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_EXECUTE | 15190b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 15200b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_CODE | 15210b57cec5SDimitry Andric (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); 15220b57cec5SDimitry Andric else if (K.isBSS()) 15230b57cec5SDimitry Andric Flags |= 15240b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 15250b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 15260b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 15270b57cec5SDimitry Andric else if (K.isThreadLocal()) 15280b57cec5SDimitry Andric Flags |= 15290b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 15300b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 15310b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 15320b57cec5SDimitry Andric else if (K.isReadOnly() || K.isReadOnlyWithRel()) 15330b57cec5SDimitry Andric Flags |= 15340b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 15350b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ; 15360b57cec5SDimitry Andric else if (K.isWriteable()) 15370b57cec5SDimitry Andric Flags |= 15380b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 15390b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 15400b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE; 15410b57cec5SDimitry Andric 15420b57cec5SDimitry Andric return Flags; 15430b57cec5SDimitry Andric } 15440b57cec5SDimitry Andric 15450b57cec5SDimitry Andric static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 15460b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 15470b57cec5SDimitry Andric assert(C && "expected GV to have a Comdat!"); 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andric StringRef ComdatGVName = C->getName(); 15500b57cec5SDimitry Andric const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 15510b57cec5SDimitry Andric if (!ComdatGV) 15520b57cec5SDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 15530b57cec5SDimitry Andric "' does not exist."); 15540b57cec5SDimitry Andric 15550b57cec5SDimitry Andric if (ComdatGV->getComdat() != C) 15560b57cec5SDimitry Andric report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 15570b57cec5SDimitry Andric "' is not a key for its COMDAT."); 15580b57cec5SDimitry Andric 15590b57cec5SDimitry Andric return ComdatGV; 15600b57cec5SDimitry Andric } 15610b57cec5SDimitry Andric 15620b57cec5SDimitry Andric static int getSelectionForCOFF(const GlobalValue *GV) { 15630b57cec5SDimitry Andric if (const Comdat *C = GV->getComdat()) { 15640b57cec5SDimitry Andric const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 15650b57cec5SDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 15660b57cec5SDimitry Andric ComdatKey = GA->getBaseObject(); 15670b57cec5SDimitry Andric if (ComdatKey == GV) { 15680b57cec5SDimitry Andric switch (C->getSelectionKind()) { 15690b57cec5SDimitry Andric case Comdat::Any: 15700b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ANY; 15710b57cec5SDimitry Andric case Comdat::ExactMatch: 15720b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 15730b57cec5SDimitry Andric case Comdat::Largest: 15740b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_LARGEST; 1575*5f7ddb14SDimitry Andric case Comdat::NoDeduplicate: 15760b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 15770b57cec5SDimitry Andric case Comdat::SameSize: 15780b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 15790b57cec5SDimitry Andric } 15800b57cec5SDimitry Andric } else { 15810b57cec5SDimitry Andric return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 15820b57cec5SDimitry Andric } 15830b57cec5SDimitry Andric } 15840b57cec5SDimitry Andric return 0; 15850b57cec5SDimitry Andric } 15860b57cec5SDimitry Andric 15870b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 15880b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 15890b57cec5SDimitry Andric int Selection = 0; 15900b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 15910b57cec5SDimitry Andric StringRef Name = GO->getSection(); 15920b57cec5SDimitry Andric StringRef COMDATSymName = ""; 15930b57cec5SDimitry Andric if (GO->hasComdat()) { 15940b57cec5SDimitry Andric Selection = getSelectionForCOFF(GO); 15950b57cec5SDimitry Andric const GlobalValue *ComdatGV; 15960b57cec5SDimitry Andric if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 15970b57cec5SDimitry Andric ComdatGV = getComdatGVForCOFF(GO); 15980b57cec5SDimitry Andric else 15990b57cec5SDimitry Andric ComdatGV = GO; 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 16020b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV); 16030b57cec5SDimitry Andric COMDATSymName = Sym->getName(); 16040b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 16050b57cec5SDimitry Andric } else { 16060b57cec5SDimitry Andric Selection = 0; 16070b57cec5SDimitry Andric } 16080b57cec5SDimitry Andric } 16090b57cec5SDimitry Andric 16100b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 16110b57cec5SDimitry Andric Selection); 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 16150b57cec5SDimitry Andric if (Kind.isText()) 16160b57cec5SDimitry Andric return ".text"; 16170b57cec5SDimitry Andric if (Kind.isBSS()) 16180b57cec5SDimitry Andric return ".bss"; 16190b57cec5SDimitry Andric if (Kind.isThreadLocal()) 16200b57cec5SDimitry Andric return ".tls$"; 16210b57cec5SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 16220b57cec5SDimitry Andric return ".rdata"; 16230b57cec5SDimitry Andric return ".data"; 16240b57cec5SDimitry Andric } 16250b57cec5SDimitry Andric 16260b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 16270b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 16280b57cec5SDimitry Andric // If we have -ffunction-sections then we should emit the global value to a 16290b57cec5SDimitry Andric // uniqued section specifically for it. 16300b57cec5SDimitry Andric bool EmitUniquedSection; 16310b57cec5SDimitry Andric if (Kind.isText()) 16320b57cec5SDimitry Andric EmitUniquedSection = TM.getFunctionSections(); 16330b57cec5SDimitry Andric else 16340b57cec5SDimitry Andric EmitUniquedSection = TM.getDataSections(); 16350b57cec5SDimitry Andric 16360b57cec5SDimitry Andric if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { 16370b57cec5SDimitry Andric SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); 16380b57cec5SDimitry Andric 16390b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 16420b57cec5SDimitry Andric int Selection = getSelectionForCOFF(GO); 16430b57cec5SDimitry Andric if (!Selection) 16440b57cec5SDimitry Andric Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 16450b57cec5SDimitry Andric const GlobalValue *ComdatGV; 16460b57cec5SDimitry Andric if (GO->hasComdat()) 16470b57cec5SDimitry Andric ComdatGV = getComdatGVForCOFF(GO); 16480b57cec5SDimitry Andric else 16490b57cec5SDimitry Andric ComdatGV = GO; 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 16520b57cec5SDimitry Andric if (EmitUniquedSection) 16530b57cec5SDimitry Andric UniqueID = NextUniqueID++; 16540b57cec5SDimitry Andric 16550b57cec5SDimitry Andric if (!ComdatGV->hasPrivateLinkage()) { 16560b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(ComdatGV); 16570b57cec5SDimitry Andric StringRef COMDATSymName = Sym->getName(); 16580b57cec5SDimitry Andric 1659af732203SDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) 1660af732203SDimitry Andric if (Optional<StringRef> Prefix = F->getSectionPrefix()) 1661af732203SDimitry Andric raw_svector_ostream(Name) << '$' << *Prefix; 1662af732203SDimitry Andric 16630b57cec5SDimitry Andric // Append "$symbol" to the section name *before* IR-level mangling is 16640b57cec5SDimitry Andric // applied when targetting mingw. This is what GCC does, and the ld.bfd 16650b57cec5SDimitry Andric // COFF linker will not properly handle comdats otherwise. 1666*5f7ddb14SDimitry Andric if (getContext().getTargetTriple().isWindowsGNUEnvironment()) 16670b57cec5SDimitry Andric raw_svector_ostream(Name) << '$' << ComdatGV->getName(); 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, 16700b57cec5SDimitry Andric COMDATSymName, Selection, UniqueID); 16710b57cec5SDimitry Andric } else { 16720b57cec5SDimitry Andric SmallString<256> TmpData; 16730b57cec5SDimitry Andric getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); 16740b57cec5SDimitry Andric return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 16750b57cec5SDimitry Andric Selection, UniqueID); 16760b57cec5SDimitry Andric } 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric 16790b57cec5SDimitry Andric if (Kind.isText()) 16800b57cec5SDimitry Andric return TextSection; 16810b57cec5SDimitry Andric 16820b57cec5SDimitry Andric if (Kind.isThreadLocal()) 16830b57cec5SDimitry Andric return TLSDataSection; 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 16860b57cec5SDimitry Andric return ReadOnlySection; 16870b57cec5SDimitry Andric 16880b57cec5SDimitry Andric // Note: we claim that common symbols are put in BSSSection, but they are 16890b57cec5SDimitry Andric // really emitted with the magic .comm directive, which creates a symbol table 16900b57cec5SDimitry Andric // entry but not a section. 16910b57cec5SDimitry Andric if (Kind.isBSS() || Kind.isCommon()) 16920b57cec5SDimitry Andric return BSSSection; 16930b57cec5SDimitry Andric 16940b57cec5SDimitry Andric return DataSection; 16950b57cec5SDimitry Andric } 16960b57cec5SDimitry Andric 16970b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::getNameWithPrefix( 16980b57cec5SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 16990b57cec5SDimitry Andric const TargetMachine &TM) const { 17000b57cec5SDimitry Andric bool CannotUsePrivateLabel = false; 17010b57cec5SDimitry Andric if (GV->hasPrivateLinkage() && 17020b57cec5SDimitry Andric ((isa<Function>(GV) && TM.getFunctionSections()) || 17030b57cec5SDimitry Andric (isa<GlobalVariable>(GV) && TM.getDataSections()))) 17040b57cec5SDimitry Andric CannotUsePrivateLabel = true; 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 17070b57cec5SDimitry Andric } 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 17100b57cec5SDimitry Andric const Function &F, const TargetMachine &TM) const { 17110b57cec5SDimitry Andric // If the function can be removed, produce a unique section so that 17120b57cec5SDimitry Andric // the table doesn't prevent the removal. 17130b57cec5SDimitry Andric const Comdat *C = F.getComdat(); 17140b57cec5SDimitry Andric bool EmitUniqueSection = TM.getFunctionSections() || C; 17150b57cec5SDimitry Andric if (!EmitUniqueSection) 17160b57cec5SDimitry Andric return ReadOnlySection; 17170b57cec5SDimitry Andric 17180b57cec5SDimitry Andric // FIXME: we should produce a symbol for F instead. 17190b57cec5SDimitry Andric if (F.hasPrivateLinkage()) 17200b57cec5SDimitry Andric return ReadOnlySection; 17210b57cec5SDimitry Andric 17220b57cec5SDimitry Andric MCSymbol *Sym = TM.getSymbol(&F); 17230b57cec5SDimitry Andric StringRef COMDATSymName = Sym->getName(); 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric SectionKind Kind = SectionKind::getReadOnly(); 17260b57cec5SDimitry Andric StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); 17270b57cec5SDimitry Andric unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 17280b57cec5SDimitry Andric Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 17290b57cec5SDimitry Andric unsigned UniqueID = NextUniqueID++; 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric return getContext().getCOFFSection( 17320b57cec5SDimitry Andric SecName, Characteristics, Kind, COMDATSymName, 17330b57cec5SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); 17340b57cec5SDimitry Andric } 17350b57cec5SDimitry Andric 17360b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, 17370b57cec5SDimitry Andric Module &M) const { 1738af732203SDimitry Andric emitLinkerDirectives(Streamer, M); 1739af732203SDimitry Andric 1740af732203SDimitry Andric unsigned Version = 0; 1741af732203SDimitry Andric unsigned Flags = 0; 1742af732203SDimitry Andric StringRef Section; 1743af732203SDimitry Andric 1744af732203SDimitry Andric GetObjCImageInfo(M, Version, Flags, Section); 1745af732203SDimitry Andric if (!Section.empty()) { 1746af732203SDimitry Andric auto &C = getContext(); 1747af732203SDimitry Andric auto *S = C.getCOFFSection(Section, 1748af732203SDimitry Andric COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1749af732203SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 1750af732203SDimitry Andric SectionKind::getReadOnly()); 1751af732203SDimitry Andric Streamer.SwitchSection(S); 1752af732203SDimitry Andric Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 1753af732203SDimitry Andric Streamer.emitInt32(Version); 1754af732203SDimitry Andric Streamer.emitInt32(Flags); 1755af732203SDimitry Andric Streamer.AddBlankLine(); 1756af732203SDimitry Andric } 1757af732203SDimitry Andric 1758af732203SDimitry Andric emitCGProfileMetadata(Streamer, M); 1759af732203SDimitry Andric } 1760af732203SDimitry Andric 1761af732203SDimitry Andric void TargetLoweringObjectFileCOFF::emitLinkerDirectives( 1762af732203SDimitry Andric MCStreamer &Streamer, Module &M) const { 17630b57cec5SDimitry Andric if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 17640b57cec5SDimitry Andric // Emit the linker options to the linker .drectve section. According to the 17650b57cec5SDimitry Andric // spec, this section is a space-separated string containing flags for 17660b57cec5SDimitry Andric // linker. 17670b57cec5SDimitry Andric MCSection *Sec = getDrectveSection(); 17680b57cec5SDimitry Andric Streamer.SwitchSection(Sec); 1769480093f4SDimitry Andric for (const auto *Option : LinkerOptions->operands()) { 17700b57cec5SDimitry Andric for (const auto &Piece : cast<MDNode>(Option)->operands()) { 17710b57cec5SDimitry Andric // Lead with a space for consistency with our dllexport implementation. 17720b57cec5SDimitry Andric std::string Directive(" "); 17735ffd83dbSDimitry Andric Directive.append(std::string(cast<MDString>(Piece)->getString())); 17745ffd83dbSDimitry Andric Streamer.emitBytes(Directive); 17750b57cec5SDimitry Andric } 17760b57cec5SDimitry Andric } 17770b57cec5SDimitry Andric } 17780b57cec5SDimitry Andric 1779af732203SDimitry Andric // Emit /EXPORT: flags for each exported global as necessary. 1780af732203SDimitry Andric std::string Flags; 1781af732203SDimitry Andric for (const GlobalValue &GV : M.global_values()) { 1782af732203SDimitry Andric raw_string_ostream OS(Flags); 1783*5f7ddb14SDimitry Andric emitLinkerFlagsForGlobalCOFF(OS, &GV, getContext().getTargetTriple(), 1784*5f7ddb14SDimitry Andric getMangler()); 1785af732203SDimitry Andric OS.flush(); 1786af732203SDimitry Andric if (!Flags.empty()) { 1787af732203SDimitry Andric Streamer.SwitchSection(getDrectveSection()); 1788af732203SDimitry Andric Streamer.emitBytes(Flags); 1789af732203SDimitry Andric } 1790af732203SDimitry Andric Flags.clear(); 1791af732203SDimitry Andric } 17920b57cec5SDimitry Andric 1793af732203SDimitry Andric // Emit /INCLUDE: flags for each used global as necessary. 1794af732203SDimitry Andric if (const auto *LU = M.getNamedGlobal("llvm.used")) { 1795af732203SDimitry Andric assert(LU->hasInitializer() && "expected llvm.used to have an initializer"); 1796af732203SDimitry Andric assert(isa<ArrayType>(LU->getValueType()) && 1797af732203SDimitry Andric "expected llvm.used to be an array type"); 1798af732203SDimitry Andric if (const auto *A = cast<ConstantArray>(LU->getInitializer())) { 1799af732203SDimitry Andric for (const Value *Op : A->operands()) { 1800af732203SDimitry Andric const auto *GV = cast<GlobalValue>(Op->stripPointerCasts()); 1801af732203SDimitry Andric // Global symbols with internal or private linkage are not visible to 1802af732203SDimitry Andric // the linker, and thus would cause an error when the linker tried to 1803af732203SDimitry Andric // preserve the symbol due to the `/include:` directive. 1804af732203SDimitry Andric if (GV->hasLocalLinkage()) 1805af732203SDimitry Andric continue; 18060b57cec5SDimitry Andric 1807af732203SDimitry Andric raw_string_ostream OS(Flags); 1808*5f7ddb14SDimitry Andric emitLinkerFlagsForUsedCOFF(OS, GV, getContext().getTargetTriple(), 1809*5f7ddb14SDimitry Andric getMangler()); 1810af732203SDimitry Andric OS.flush(); 1811af732203SDimitry Andric 1812af732203SDimitry Andric if (!Flags.empty()) { 1813af732203SDimitry Andric Streamer.SwitchSection(getDrectveSection()); 1814af732203SDimitry Andric Streamer.emitBytes(Flags); 1815af732203SDimitry Andric } 1816af732203SDimitry Andric Flags.clear(); 1817af732203SDimitry Andric } 1818af732203SDimitry Andric } 1819af732203SDimitry Andric } 18200b57cec5SDimitry Andric } 18210b57cec5SDimitry Andric 18220b57cec5SDimitry Andric void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 18230b57cec5SDimitry Andric const TargetMachine &TM) { 18240b57cec5SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TM); 1825af732203SDimitry Andric this->TM = &TM; 18260b57cec5SDimitry Andric const Triple &T = TM.getTargetTriple(); 18270b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 18280b57cec5SDimitry Andric StaticCtorSection = 18290b57cec5SDimitry Andric Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 18300b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 18310b57cec5SDimitry Andric SectionKind::getReadOnly()); 18320b57cec5SDimitry Andric StaticDtorSection = 18330b57cec5SDimitry Andric Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 18340b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ, 18350b57cec5SDimitry Andric SectionKind::getReadOnly()); 18360b57cec5SDimitry Andric } else { 18370b57cec5SDimitry Andric StaticCtorSection = Ctx.getCOFFSection( 18380b57cec5SDimitry Andric ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 18390b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 18400b57cec5SDimitry Andric SectionKind::getData()); 18410b57cec5SDimitry Andric StaticDtorSection = Ctx.getCOFFSection( 18420b57cec5SDimitry Andric ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 18430b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 18440b57cec5SDimitry Andric SectionKind::getData()); 18450b57cec5SDimitry Andric } 18460b57cec5SDimitry Andric } 18470b57cec5SDimitry Andric 18480b57cec5SDimitry Andric static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 18490b57cec5SDimitry Andric const Triple &T, bool IsCtor, 18500b57cec5SDimitry Andric unsigned Priority, 18510b57cec5SDimitry Andric const MCSymbol *KeySym, 18520b57cec5SDimitry Andric MCSectionCOFF *Default) { 18530b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 18540b57cec5SDimitry Andric // If the priority is the default, use .CRT$XCU, possibly associative. 18550b57cec5SDimitry Andric if (Priority == 65535) 18560b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 18570b57cec5SDimitry Andric 18580b57cec5SDimitry Andric // Otherwise, we need to compute a new section name. Low priorities should 18590b57cec5SDimitry Andric // run earlier. The linker will sort sections ASCII-betically, and we need a 18600b57cec5SDimitry Andric // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 18610b57cec5SDimitry Andric // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 18620b57cec5SDimitry Andric // low priorities need to sort before 'L', since the CRT uses that 18630b57cec5SDimitry Andric // internally, so we use ".CRT$XCA00001" for them. 18640b57cec5SDimitry Andric SmallString<24> Name; 18650b57cec5SDimitry Andric raw_svector_ostream OS(Name); 18668bcb0991SDimitry Andric OS << ".CRT$X" << (IsCtor ? "C" : "T") << 18678bcb0991SDimitry Andric (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 18680b57cec5SDimitry Andric MCSectionCOFF *Sec = Ctx.getCOFFSection( 18690b57cec5SDimitry Andric Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 18700b57cec5SDimitry Andric SectionKind::getReadOnly()); 18710b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 18720b57cec5SDimitry Andric } 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric std::string Name = IsCtor ? ".ctors" : ".dtors"; 18750b57cec5SDimitry Andric if (Priority != 65535) 18760b57cec5SDimitry Andric raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 18770b57cec5SDimitry Andric 18780b57cec5SDimitry Andric return Ctx.getAssociativeCOFFSection( 18790b57cec5SDimitry Andric Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 18800b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 18810b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_WRITE, 18820b57cec5SDimitry Andric SectionKind::getData()), 18830b57cec5SDimitry Andric KeySym, 0); 18840b57cec5SDimitry Andric } 18850b57cec5SDimitry Andric 18860b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 18870b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 1888*5f7ddb14SDimitry Andric return getCOFFStaticStructorSection( 1889*5f7ddb14SDimitry Andric getContext(), getContext().getTargetTriple(), true, Priority, KeySym, 18900b57cec5SDimitry Andric cast<MCSectionCOFF>(StaticCtorSection)); 18910b57cec5SDimitry Andric } 18920b57cec5SDimitry Andric 18930b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 18940b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 1895*5f7ddb14SDimitry Andric return getCOFFStaticStructorSection( 1896*5f7ddb14SDimitry Andric getContext(), getContext().getTargetTriple(), false, Priority, KeySym, 18970b57cec5SDimitry Andric cast<MCSectionCOFF>(StaticDtorSection)); 18980b57cec5SDimitry Andric } 18990b57cec5SDimitry Andric 19000b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 19010b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 19020b57cec5SDimitry Andric const TargetMachine &TM) const { 19030b57cec5SDimitry Andric const Triple &T = TM.getTargetTriple(); 19040b57cec5SDimitry Andric if (T.isOSCygMing()) 19050b57cec5SDimitry Andric return nullptr; 19060b57cec5SDimitry Andric 19070b57cec5SDimitry Andric // Our symbols should exist in address space zero, cowardly no-op if 19080b57cec5SDimitry Andric // otherwise. 19090b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 19100b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0) 19110b57cec5SDimitry Andric return nullptr; 19120b57cec5SDimitry Andric 19130b57cec5SDimitry Andric // Both ptrtoint instructions must wrap global objects: 19140b57cec5SDimitry Andric // - Only global variables are eligible for image relative relocations. 19150b57cec5SDimitry Andric // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 19160b57cec5SDimitry Andric // We expect __ImageBase to be a global variable without a section, externally 19170b57cec5SDimitry Andric // defined. 19180b57cec5SDimitry Andric // 19190b57cec5SDimitry Andric // It should look something like this: @__ImageBase = external constant i8 19200b57cec5SDimitry Andric if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 19210b57cec5SDimitry Andric LHS->isThreadLocal() || RHS->isThreadLocal() || 19220b57cec5SDimitry Andric RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 19230b57cec5SDimitry Andric cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 19240b57cec5SDimitry Andric return nullptr; 19250b57cec5SDimitry Andric 19260b57cec5SDimitry Andric return MCSymbolRefExpr::create(TM.getSymbol(LHS), 19270b57cec5SDimitry Andric MCSymbolRefExpr::VK_COFF_IMGREL32, 19280b57cec5SDimitry Andric getContext()); 19290b57cec5SDimitry Andric } 19300b57cec5SDimitry Andric 19310b57cec5SDimitry Andric static std::string APIntToHexString(const APInt &AI) { 19320b57cec5SDimitry Andric unsigned Width = (AI.getBitWidth() / 8) * 2; 1933*5f7ddb14SDimitry Andric std::string HexString = toString(AI, 16, /*Signed=*/false); 19345ffd83dbSDimitry Andric llvm::transform(HexString, HexString.begin(), tolower); 19350b57cec5SDimitry Andric unsigned Size = HexString.size(); 19360b57cec5SDimitry Andric assert(Width >= Size && "hex string is too large!"); 19370b57cec5SDimitry Andric HexString.insert(HexString.begin(), Width - Size, '0'); 19380b57cec5SDimitry Andric 19390b57cec5SDimitry Andric return HexString; 19400b57cec5SDimitry Andric } 19410b57cec5SDimitry Andric 19420b57cec5SDimitry Andric static std::string scalarConstantToHexString(const Constant *C) { 19430b57cec5SDimitry Andric Type *Ty = C->getType(); 19440b57cec5SDimitry Andric if (isa<UndefValue>(C)) { 19450b57cec5SDimitry Andric return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 19460b57cec5SDimitry Andric } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 19470b57cec5SDimitry Andric return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 19480b57cec5SDimitry Andric } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 19490b57cec5SDimitry Andric return APIntToHexString(CI->getValue()); 19500b57cec5SDimitry Andric } else { 19510b57cec5SDimitry Andric unsigned NumElements; 19525ffd83dbSDimitry Andric if (auto *VTy = dyn_cast<VectorType>(Ty)) 19535ffd83dbSDimitry Andric NumElements = cast<FixedVectorType>(VTy)->getNumElements(); 19540b57cec5SDimitry Andric else 19550b57cec5SDimitry Andric NumElements = Ty->getArrayNumElements(); 19560b57cec5SDimitry Andric std::string HexString; 19570b57cec5SDimitry Andric for (int I = NumElements - 1, E = -1; I != E; --I) 19580b57cec5SDimitry Andric HexString += scalarConstantToHexString(C->getAggregateElement(I)); 19590b57cec5SDimitry Andric return HexString; 19600b57cec5SDimitry Andric } 19610b57cec5SDimitry Andric } 19620b57cec5SDimitry Andric 19630b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 19640b57cec5SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 19655ffd83dbSDimitry Andric Align &Alignment) const { 19660b57cec5SDimitry Andric if (Kind.isMergeableConst() && C && 19670b57cec5SDimitry Andric getContext().getAsmInfo()->hasCOFFComdatConstants()) { 19680b57cec5SDimitry Andric // This creates comdat sections with the given symbol name, but unless 19690b57cec5SDimitry Andric // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 19700b57cec5SDimitry Andric // will be created with a null storage class, which makes GNU binutils 19710b57cec5SDimitry Andric // error out. 19720b57cec5SDimitry Andric const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 19730b57cec5SDimitry Andric COFF::IMAGE_SCN_MEM_READ | 19740b57cec5SDimitry Andric COFF::IMAGE_SCN_LNK_COMDAT; 19750b57cec5SDimitry Andric std::string COMDATSymName; 19760b57cec5SDimitry Andric if (Kind.isMergeableConst4()) { 19775ffd83dbSDimitry Andric if (Alignment <= 4) { 19780b57cec5SDimitry Andric COMDATSymName = "__real@" + scalarConstantToHexString(C); 19795ffd83dbSDimitry Andric Alignment = Align(4); 19800b57cec5SDimitry Andric } 19810b57cec5SDimitry Andric } else if (Kind.isMergeableConst8()) { 19825ffd83dbSDimitry Andric if (Alignment <= 8) { 19830b57cec5SDimitry Andric COMDATSymName = "__real@" + scalarConstantToHexString(C); 19845ffd83dbSDimitry Andric Alignment = Align(8); 19850b57cec5SDimitry Andric } 19860b57cec5SDimitry Andric } else if (Kind.isMergeableConst16()) { 19870b57cec5SDimitry Andric // FIXME: These may not be appropriate for non-x86 architectures. 19885ffd83dbSDimitry Andric if (Alignment <= 16) { 19890b57cec5SDimitry Andric COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 19905ffd83dbSDimitry Andric Alignment = Align(16); 19910b57cec5SDimitry Andric } 19920b57cec5SDimitry Andric } else if (Kind.isMergeableConst32()) { 19935ffd83dbSDimitry Andric if (Alignment <= 32) { 19940b57cec5SDimitry Andric COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 19955ffd83dbSDimitry Andric Alignment = Align(32); 19960b57cec5SDimitry Andric } 19970b57cec5SDimitry Andric } 19980b57cec5SDimitry Andric 19990b57cec5SDimitry Andric if (!COMDATSymName.empty()) 20000b57cec5SDimitry Andric return getContext().getCOFFSection(".rdata", Characteristics, Kind, 20010b57cec5SDimitry Andric COMDATSymName, 20020b57cec5SDimitry Andric COFF::IMAGE_COMDAT_SELECT_ANY); 20030b57cec5SDimitry Andric } 20040b57cec5SDimitry Andric 20055ffd83dbSDimitry Andric return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, 20065ffd83dbSDimitry Andric Alignment); 20070b57cec5SDimitry Andric } 20080b57cec5SDimitry Andric 20090b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 20100b57cec5SDimitry Andric // Wasm 20110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 20120b57cec5SDimitry Andric 20130b57cec5SDimitry Andric static const Comdat *getWasmComdat(const GlobalValue *GV) { 20140b57cec5SDimitry Andric const Comdat *C = GV->getComdat(); 20150b57cec5SDimitry Andric if (!C) 20160b57cec5SDimitry Andric return nullptr; 20170b57cec5SDimitry Andric 20180b57cec5SDimitry Andric if (C->getSelectionKind() != Comdat::Any) 20190b57cec5SDimitry Andric report_fatal_error("WebAssembly COMDATs only support " 20200b57cec5SDimitry Andric "SelectionKind::Any, '" + C->getName() + "' cannot be " 20210b57cec5SDimitry Andric "lowered."); 20220b57cec5SDimitry Andric 20230b57cec5SDimitry Andric return C; 20240b57cec5SDimitry Andric } 20250b57cec5SDimitry Andric 2026*5f7ddb14SDimitry Andric static unsigned getWasmSectionFlags(SectionKind K) { 2027*5f7ddb14SDimitry Andric unsigned Flags = 0; 2028*5f7ddb14SDimitry Andric 2029*5f7ddb14SDimitry Andric if (K.isThreadLocal()) 2030*5f7ddb14SDimitry Andric Flags |= wasm::WASM_SEG_FLAG_TLS; 2031*5f7ddb14SDimitry Andric 2032*5f7ddb14SDimitry Andric if (K.isMergeableCString()) 2033*5f7ddb14SDimitry Andric Flags |= wasm::WASM_SEG_FLAG_STRINGS; 2034*5f7ddb14SDimitry Andric 2035*5f7ddb14SDimitry Andric // TODO(sbc): Add suport for K.isMergeableConst() 2036*5f7ddb14SDimitry Andric 2037*5f7ddb14SDimitry Andric return Flags; 2038*5f7ddb14SDimitry Andric } 2039*5f7ddb14SDimitry Andric 20400b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 20410b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 20420b57cec5SDimitry Andric // We don't support explict section names for functions in the wasm object 20430b57cec5SDimitry Andric // format. Each function has to be in its own unique section. 20440b57cec5SDimitry Andric if (isa<Function>(GO)) { 20450b57cec5SDimitry Andric return SelectSectionForGlobal(GO, Kind, TM); 20460b57cec5SDimitry Andric } 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric StringRef Name = GO->getSection(); 20490b57cec5SDimitry Andric 20505ffd83dbSDimitry Andric // Certain data sections we treat as named custom sections rather than 20515ffd83dbSDimitry Andric // segments within the data section. 20525ffd83dbSDimitry Andric // This could be avoided if all data segements (the wasm sense) were 20535ffd83dbSDimitry Andric // represented as their own sections (in the llvm sense). 20545ffd83dbSDimitry Andric // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 20555ffd83dbSDimitry Andric if (Name == ".llvmcmd" || Name == ".llvmbc") 20565ffd83dbSDimitry Andric Kind = SectionKind::getMetadata(); 20570b57cec5SDimitry Andric 20580b57cec5SDimitry Andric StringRef Group = ""; 20590b57cec5SDimitry Andric if (const Comdat *C = getWasmComdat(GO)) { 20600b57cec5SDimitry Andric Group = C->getName(); 20610b57cec5SDimitry Andric } 20620b57cec5SDimitry Andric 2063*5f7ddb14SDimitry Andric unsigned Flags = getWasmSectionFlags(Kind); 2064*5f7ddb14SDimitry Andric MCSectionWasm *Section = getContext().getWasmSection( 2065*5f7ddb14SDimitry Andric Name, Kind, Flags, Group, MCContext::GenericSectionID); 20660b57cec5SDimitry Andric 20670b57cec5SDimitry Andric return Section; 20680b57cec5SDimitry Andric } 20690b57cec5SDimitry Andric 20700b57cec5SDimitry Andric static MCSectionWasm *selectWasmSectionForGlobal( 20710b57cec5SDimitry Andric MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 20720b57cec5SDimitry Andric const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 20730b57cec5SDimitry Andric StringRef Group = ""; 20740b57cec5SDimitry Andric if (const Comdat *C = getWasmComdat(GO)) { 20750b57cec5SDimitry Andric Group = C->getName(); 20760b57cec5SDimitry Andric } 20770b57cec5SDimitry Andric 20780b57cec5SDimitry Andric bool UniqueSectionNames = TM.getUniqueSectionNames(); 20790b57cec5SDimitry Andric SmallString<128> Name = getSectionPrefixForGlobal(Kind); 20800b57cec5SDimitry Andric 20810b57cec5SDimitry Andric if (const auto *F = dyn_cast<Function>(GO)) { 20820b57cec5SDimitry Andric const auto &OptionalPrefix = F->getSectionPrefix(); 20830b57cec5SDimitry Andric if (OptionalPrefix) 2084af732203SDimitry Andric raw_svector_ostream(Name) << '.' << *OptionalPrefix; 20850b57cec5SDimitry Andric } 20860b57cec5SDimitry Andric 20870b57cec5SDimitry Andric if (EmitUniqueSection && UniqueSectionNames) { 20880b57cec5SDimitry Andric Name.push_back('.'); 20890b57cec5SDimitry Andric TM.getNameWithPrefix(Name, GO, Mang, true); 20900b57cec5SDimitry Andric } 20910b57cec5SDimitry Andric unsigned UniqueID = MCContext::GenericSectionID; 20920b57cec5SDimitry Andric if (EmitUniqueSection && !UniqueSectionNames) { 20930b57cec5SDimitry Andric UniqueID = *NextUniqueID; 20940b57cec5SDimitry Andric (*NextUniqueID)++; 20950b57cec5SDimitry Andric } 20960b57cec5SDimitry Andric 2097*5f7ddb14SDimitry Andric unsigned Flags = getWasmSectionFlags(Kind); 2098*5f7ddb14SDimitry Andric return Ctx.getWasmSection(Name, Kind, Flags, Group, UniqueID); 20990b57cec5SDimitry Andric } 21000b57cec5SDimitry Andric 21010b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 21020b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 21030b57cec5SDimitry Andric 21040b57cec5SDimitry Andric if (Kind.isCommon()) 21050b57cec5SDimitry Andric report_fatal_error("mergable sections not supported yet on wasm"); 21060b57cec5SDimitry Andric 21070b57cec5SDimitry Andric // If we have -ffunction-section or -fdata-section then we should emit the 21080b57cec5SDimitry Andric // global value to a uniqued section specifically for it. 21090b57cec5SDimitry Andric bool EmitUniqueSection = false; 21100b57cec5SDimitry Andric if (Kind.isText()) 21110b57cec5SDimitry Andric EmitUniqueSection = TM.getFunctionSections(); 21120b57cec5SDimitry Andric else 21130b57cec5SDimitry Andric EmitUniqueSection = TM.getDataSections(); 21140b57cec5SDimitry Andric EmitUniqueSection |= GO->hasComdat(); 21150b57cec5SDimitry Andric 21160b57cec5SDimitry Andric return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 21170b57cec5SDimitry Andric EmitUniqueSection, &NextUniqueID); 21180b57cec5SDimitry Andric } 21190b57cec5SDimitry Andric 21200b57cec5SDimitry Andric bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 21210b57cec5SDimitry Andric bool UsesLabelDifference, const Function &F) const { 21220b57cec5SDimitry Andric // We can always create relative relocations, so use another section 21230b57cec5SDimitry Andric // that can be marked non-executable. 21240b57cec5SDimitry Andric return false; 21250b57cec5SDimitry Andric } 21260b57cec5SDimitry Andric 21270b57cec5SDimitry Andric const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 21280b57cec5SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 21290b57cec5SDimitry Andric const TargetMachine &TM) const { 21300b57cec5SDimitry Andric // We may only use a PLT-relative relocation to refer to unnamed_addr 21310b57cec5SDimitry Andric // functions. 21320b57cec5SDimitry Andric if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 21330b57cec5SDimitry Andric return nullptr; 21340b57cec5SDimitry Andric 21350b57cec5SDimitry Andric // Basic sanity checks. 21360b57cec5SDimitry Andric if (LHS->getType()->getPointerAddressSpace() != 0 || 21370b57cec5SDimitry Andric RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 21380b57cec5SDimitry Andric RHS->isThreadLocal()) 21390b57cec5SDimitry Andric return nullptr; 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric return MCBinaryExpr::createSub( 21420b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 21430b57cec5SDimitry Andric getContext()), 21440b57cec5SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 21450b57cec5SDimitry Andric } 21460b57cec5SDimitry Andric 21470b57cec5SDimitry Andric void TargetLoweringObjectFileWasm::InitializeWasm() { 21480b57cec5SDimitry Andric StaticCtorSection = 21490b57cec5SDimitry Andric getContext().getWasmSection(".init_array", SectionKind::getData()); 21500b57cec5SDimitry Andric 21510b57cec5SDimitry Andric // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 21520b57cec5SDimitry Andric // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 21530b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_absptr; 21540b57cec5SDimitry Andric } 21550b57cec5SDimitry Andric 21560b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 21570b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 21580b57cec5SDimitry Andric return Priority == UINT16_MAX ? 21590b57cec5SDimitry Andric StaticCtorSection : 21600b57cec5SDimitry Andric getContext().getWasmSection(".init_array." + utostr(Priority), 21610b57cec5SDimitry Andric SectionKind::getData()); 21620b57cec5SDimitry Andric } 21630b57cec5SDimitry Andric 21640b57cec5SDimitry Andric MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 21650b57cec5SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 21660b57cec5SDimitry Andric llvm_unreachable("@llvm.global_dtors should have been lowered already"); 21670b57cec5SDimitry Andric return nullptr; 21680b57cec5SDimitry Andric } 21698bcb0991SDimitry Andric 21708bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 21718bcb0991SDimitry Andric // XCOFF 21728bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 2173af732203SDimitry Andric bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock( 2174af732203SDimitry Andric const MachineFunction *MF) { 2175af732203SDimitry Andric if (!MF->getLandingPads().empty()) 2176af732203SDimitry Andric return true; 2177af732203SDimitry Andric 2178af732203SDimitry Andric const Function &F = MF->getFunction(); 2179af732203SDimitry Andric if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry()) 2180af732203SDimitry Andric return false; 2181af732203SDimitry Andric 2182*5f7ddb14SDimitry Andric const GlobalValue *Per = 2183*5f7ddb14SDimitry Andric dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts()); 2184*5f7ddb14SDimitry Andric assert(Per && "Personality routine is not a GlobalValue type."); 2185af732203SDimitry Andric if (isNoOpWithoutInvoke(classifyEHPersonality(Per))) 2186af732203SDimitry Andric return false; 2187af732203SDimitry Andric 2188af732203SDimitry Andric return true; 2189af732203SDimitry Andric } 2190af732203SDimitry Andric 2191*5f7ddb14SDimitry Andric bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB( 2192*5f7ddb14SDimitry Andric const MachineFunction *MF) { 2193*5f7ddb14SDimitry Andric const Function &F = MF->getFunction(); 2194*5f7ddb14SDimitry Andric if (!F.hasStackProtectorFnAttr()) 2195*5f7ddb14SDimitry Andric return false; 2196*5f7ddb14SDimitry Andric // FIXME: check presence of canary word 2197*5f7ddb14SDimitry Andric // There are cases that the stack protectors are not really inserted even if 2198*5f7ddb14SDimitry Andric // the attributes are on. 2199*5f7ddb14SDimitry Andric return true; 2200*5f7ddb14SDimitry Andric } 2201*5f7ddb14SDimitry Andric 2202af732203SDimitry Andric MCSymbol * 2203af732203SDimitry Andric TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { 2204af732203SDimitry Andric return MF->getMMI().getContext().getOrCreateSymbol( 2205af732203SDimitry Andric "__ehinfo." + Twine(MF->getFunctionNumber())); 2206af732203SDimitry Andric } 2207af732203SDimitry Andric 22085ffd83dbSDimitry Andric MCSymbol * 22095ffd83dbSDimitry Andric TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV, 22105ffd83dbSDimitry Andric const TargetMachine &TM) const { 22115ffd83dbSDimitry Andric // We always use a qualname symbol for a GV that represents 22125ffd83dbSDimitry Andric // a declaration, a function descriptor, or a common symbol. 2213af732203SDimitry Andric // If a GV represents a GlobalVariable and -fdata-sections is enabled, we 2214af732203SDimitry Andric // also return a qualname so that a label symbol could be avoided. 22155ffd83dbSDimitry Andric // It is inherently ambiguous when the GO represents the address of a 22165ffd83dbSDimitry Andric // function, as the GO could either represent a function descriptor or a 22175ffd83dbSDimitry Andric // function entry point. We choose to always return a function descriptor 22185ffd83dbSDimitry Andric // here. 22195ffd83dbSDimitry Andric if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) { 2220*5f7ddb14SDimitry Andric if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) 2221*5f7ddb14SDimitry Andric if (GVar->hasAttribute("toc-data")) 2222*5f7ddb14SDimitry Andric return cast<MCSectionXCOFF>( 2223*5f7ddb14SDimitry Andric SectionForGlobal(GVar, SectionKind::getData(), TM)) 2224*5f7ddb14SDimitry Andric ->getQualNameSymbol(); 2225*5f7ddb14SDimitry Andric 22265ffd83dbSDimitry Andric if (GO->isDeclarationForLinker()) 22275ffd83dbSDimitry Andric return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM)) 22285ffd83dbSDimitry Andric ->getQualNameSymbol(); 22295ffd83dbSDimitry Andric 22305ffd83dbSDimitry Andric SectionKind GOKind = getKindForGlobal(GO, TM); 22315ffd83dbSDimitry Andric if (GOKind.isText()) 22325ffd83dbSDimitry Andric return cast<MCSectionXCOFF>( 22335ffd83dbSDimitry Andric getSectionForFunctionDescriptor(cast<Function>(GO), TM)) 22345ffd83dbSDimitry Andric ->getQualNameSymbol(); 2235*5f7ddb14SDimitry Andric if ((TM.getDataSections() && !GO->hasSection()) || GO->hasCommonLinkage() || 2236*5f7ddb14SDimitry Andric GOKind.isBSSLocal() || GOKind.isThreadBSSLocal()) 22375ffd83dbSDimitry Andric return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM)) 22385ffd83dbSDimitry Andric ->getQualNameSymbol(); 22395ffd83dbSDimitry Andric } 22405ffd83dbSDimitry Andric 22415ffd83dbSDimitry Andric // For all other cases, fall back to getSymbol to return the unqualified name. 22425ffd83dbSDimitry Andric return nullptr; 22435ffd83dbSDimitry Andric } 22445ffd83dbSDimitry Andric 22458bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 22468bcb0991SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2247af732203SDimitry Andric if (!GO->hasSection()) 2248af732203SDimitry Andric report_fatal_error("#pragma clang section is not yet supported"); 2249af732203SDimitry Andric 2250af732203SDimitry Andric StringRef SectionName = GO->getSection(); 2251*5f7ddb14SDimitry Andric 2252*5f7ddb14SDimitry Andric // Handle the XCOFF::TD case first, then deal with the rest. 2253*5f7ddb14SDimitry Andric if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) 2254*5f7ddb14SDimitry Andric if (GVar->hasAttribute("toc-data")) 2255*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2256*5f7ddb14SDimitry Andric SectionName, Kind, 2257*5f7ddb14SDimitry Andric XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD), 2258*5f7ddb14SDimitry Andric /* MultiSymbolsAllowed*/ true); 2259*5f7ddb14SDimitry Andric 2260af732203SDimitry Andric XCOFF::StorageMappingClass MappingClass; 2261af732203SDimitry Andric if (Kind.isText()) 2262af732203SDimitry Andric MappingClass = XCOFF::XMC_PR; 2263af732203SDimitry Andric else if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) 2264af732203SDimitry Andric MappingClass = XCOFF::XMC_RW; 2265af732203SDimitry Andric else if (Kind.isReadOnly()) 2266af732203SDimitry Andric MappingClass = XCOFF::XMC_RO; 2267af732203SDimitry Andric else 2268af732203SDimitry Andric report_fatal_error("XCOFF other section types not yet implemented."); 2269af732203SDimitry Andric 2270*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2271*5f7ddb14SDimitry Andric SectionName, Kind, XCOFF::CsectProperties(MappingClass, XCOFF::XTY_SD), 2272*5f7ddb14SDimitry Andric /* MultiSymbolsAllowed*/ true); 22738bcb0991SDimitry Andric } 22748bcb0991SDimitry Andric 22755ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( 22765ffd83dbSDimitry Andric const GlobalObject *GO, const TargetMachine &TM) const { 22775ffd83dbSDimitry Andric assert(GO->isDeclarationForLinker() && 22785ffd83dbSDimitry Andric "Tried to get ER section for a defined global."); 22795ffd83dbSDimitry Andric 22805ffd83dbSDimitry Andric SmallString<128> Name; 22815ffd83dbSDimitry Andric getNameWithPrefix(Name, GO, TM); 22825ffd83dbSDimitry Andric 2283*5f7ddb14SDimitry Andric XCOFF::StorageMappingClass SMC = 2284*5f7ddb14SDimitry Andric isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA; 2285*5f7ddb14SDimitry Andric if (GO->isThreadLocal()) 2286*5f7ddb14SDimitry Andric SMC = XCOFF::XMC_UL; 2287*5f7ddb14SDimitry Andric 22885ffd83dbSDimitry Andric // Externals go into a csect of type ER. 22895ffd83dbSDimitry Andric return getContext().getXCOFFSection( 2290*5f7ddb14SDimitry Andric Name, SectionKind::getMetadata(), 2291*5f7ddb14SDimitry Andric XCOFF::CsectProperties(SMC, XCOFF::XTY_ER)); 22925ffd83dbSDimitry Andric } 22935ffd83dbSDimitry Andric 22948bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 22958bcb0991SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2296*5f7ddb14SDimitry Andric // Handle the XCOFF::TD case first, then deal with the rest. 2297*5f7ddb14SDimitry Andric if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) 2298*5f7ddb14SDimitry Andric if (GVar->hasAttribute("toc-data")) { 22998bcb0991SDimitry Andric SmallString<128> Name; 23008bcb0991SDimitry Andric getNameWithPrefix(Name, GO, TM); 23018bcb0991SDimitry Andric return getContext().getXCOFFSection( 2302*5f7ddb14SDimitry Andric Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, XCOFF::XTY_SD), 2303*5f7ddb14SDimitry Andric /* MultiSymbolsAllowed*/ true); 2304*5f7ddb14SDimitry Andric } 2305*5f7ddb14SDimitry Andric 2306*5f7ddb14SDimitry Andric // Common symbols go into a csect with matching name which will get mapped 2307*5f7ddb14SDimitry Andric // into the .bss section. 2308*5f7ddb14SDimitry Andric // Zero-initialized local TLS symbols go into a csect with matching name which 2309*5f7ddb14SDimitry Andric // will get mapped into the .tbss section. 2310*5f7ddb14SDimitry Andric if (Kind.isBSSLocal() || GO->hasCommonLinkage() || Kind.isThreadBSSLocal()) { 2311*5f7ddb14SDimitry Andric SmallString<128> Name; 2312*5f7ddb14SDimitry Andric getNameWithPrefix(Name, GO, TM); 2313*5f7ddb14SDimitry Andric XCOFF::StorageMappingClass SMC = Kind.isBSSLocal() ? XCOFF::XMC_BS 2314*5f7ddb14SDimitry Andric : Kind.isCommon() ? XCOFF::XMC_RW 2315*5f7ddb14SDimitry Andric : XCOFF::XMC_UL; 2316*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2317*5f7ddb14SDimitry Andric Name, Kind, XCOFF::CsectProperties(SMC, XCOFF::XTY_CM)); 23188bcb0991SDimitry Andric } 23198bcb0991SDimitry Andric 2320480093f4SDimitry Andric if (Kind.isMergeableCString()) { 23215ffd83dbSDimitry Andric Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 2322480093f4SDimitry Andric cast<GlobalVariable>(GO)); 2323480093f4SDimitry Andric 2324480093f4SDimitry Andric unsigned EntrySize = getEntrySizeForKind(Kind); 2325480093f4SDimitry Andric std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 2326480093f4SDimitry Andric SmallString<128> Name; 23275ffd83dbSDimitry Andric Name = SizeSpec + utostr(Alignment.value()); 2328480093f4SDimitry Andric 2329af732203SDimitry Andric if (TM.getDataSections()) 2330af732203SDimitry Andric getNameWithPrefix(Name, GO, TM); 2331af732203SDimitry Andric 2332480093f4SDimitry Andric return getContext().getXCOFFSection( 2333*5f7ddb14SDimitry Andric Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD), 2334af732203SDimitry Andric /* MultiSymbolsAllowed*/ !TM.getDataSections()); 2335480093f4SDimitry Andric } 2336480093f4SDimitry Andric 2337af732203SDimitry Andric if (Kind.isText()) { 2338af732203SDimitry Andric if (TM.getFunctionSections()) { 2339af732203SDimitry Andric return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM)) 2340af732203SDimitry Andric ->getRepresentedCsect(); 2341af732203SDimitry Andric } 23428bcb0991SDimitry Andric return TextSection; 2343af732203SDimitry Andric } 23448bcb0991SDimitry Andric 2345af732203SDimitry Andric // TODO: We may put Kind.isReadOnlyWithRel() under option control, because 2346af732203SDimitry Andric // user may want to have read-only data with relocations placed into a 2347af732203SDimitry Andric // read-only section by the compiler. 2348af732203SDimitry Andric // For BSS kind, zero initialized data must be emitted to the .data section 2349af732203SDimitry Andric // because external linkage control sections that get mapped to the .bss 2350af732203SDimitry Andric // section will be linked as tentative defintions, which is only appropriate 2351af732203SDimitry Andric // for SectionKind::Common. 2352af732203SDimitry Andric if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) { 2353af732203SDimitry Andric if (TM.getDataSections()) { 2354af732203SDimitry Andric SmallString<128> Name; 2355af732203SDimitry Andric getNameWithPrefix(Name, GO, TM); 2356*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2357*5f7ddb14SDimitry Andric Name, SectionKind::getData(), 2358*5f7ddb14SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)); 2359af732203SDimitry Andric } 23608bcb0991SDimitry Andric return DataSection; 2361af732203SDimitry Andric } 23628bcb0991SDimitry Andric 2363af732203SDimitry Andric if (Kind.isReadOnly()) { 2364af732203SDimitry Andric if (TM.getDataSections()) { 2365af732203SDimitry Andric SmallString<128> Name; 2366af732203SDimitry Andric getNameWithPrefix(Name, GO, TM); 2367*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2368*5f7ddb14SDimitry Andric Name, SectionKind::getReadOnly(), 2369*5f7ddb14SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2370af732203SDimitry Andric } 2371480093f4SDimitry Andric return ReadOnlySection; 2372af732203SDimitry Andric } 2373480093f4SDimitry Andric 2374*5f7ddb14SDimitry Andric // External/weak TLS data and initialized local TLS data are not eligible 2375*5f7ddb14SDimitry Andric // to be put into common csect. If data sections are enabled, thread 2376*5f7ddb14SDimitry Andric // data are emitted into separate sections. Otherwise, thread data 2377*5f7ddb14SDimitry Andric // are emitted into the .tdata section. 2378*5f7ddb14SDimitry Andric if (Kind.isThreadLocal()) { 2379*5f7ddb14SDimitry Andric if (TM.getDataSections()) { 2380*5f7ddb14SDimitry Andric SmallString<128> Name; 2381*5f7ddb14SDimitry Andric getNameWithPrefix(Name, GO, TM); 2382*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2383*5f7ddb14SDimitry Andric Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TL, XCOFF::XTY_SD)); 2384*5f7ddb14SDimitry Andric } 2385*5f7ddb14SDimitry Andric return TLSDataSection; 2386*5f7ddb14SDimitry Andric } 2387*5f7ddb14SDimitry Andric 23888bcb0991SDimitry Andric report_fatal_error("XCOFF other section types not yet implemented."); 23898bcb0991SDimitry Andric } 23908bcb0991SDimitry Andric 2391480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable( 2392480093f4SDimitry Andric const Function &F, const TargetMachine &TM) const { 2393480093f4SDimitry Andric assert (!F.getComdat() && "Comdat not supported on XCOFF."); 2394af732203SDimitry Andric 2395af732203SDimitry Andric if (!TM.getFunctionSections()) 2396480093f4SDimitry Andric return ReadOnlySection; 2397af732203SDimitry Andric 2398af732203SDimitry Andric // If the function can be removed, produce a unique section so that 2399af732203SDimitry Andric // the table doesn't prevent the removal. 2400af732203SDimitry Andric SmallString<128> NameStr(".rodata.jmp.."); 2401af732203SDimitry Andric getNameWithPrefix(NameStr, &F, TM); 2402*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2403*5f7ddb14SDimitry Andric NameStr, SectionKind::getReadOnly(), 2404*5f7ddb14SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2405480093f4SDimitry Andric } 2406480093f4SDimitry Andric 24078bcb0991SDimitry Andric bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 24088bcb0991SDimitry Andric bool UsesLabelDifference, const Function &F) const { 2409480093f4SDimitry Andric return false; 2410480093f4SDimitry Andric } 2411480093f4SDimitry Andric 2412480093f4SDimitry Andric /// Given a mergeable constant with the specified size and relocation 2413480093f4SDimitry Andric /// information, return a section that it should be placed in. 2414480093f4SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant( 2415480093f4SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 24165ffd83dbSDimitry Andric Align &Alignment) const { 2417480093f4SDimitry Andric //TODO: Enable emiting constant pool to unique sections when we support it. 2418480093f4SDimitry Andric return ReadOnlySection; 24198bcb0991SDimitry Andric } 24208bcb0991SDimitry Andric 24218bcb0991SDimitry Andric void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 24228bcb0991SDimitry Andric const TargetMachine &TgtM) { 24238bcb0991SDimitry Andric TargetLoweringObjectFile::Initialize(Ctx, TgtM); 2424af732203SDimitry Andric TTypeEncoding = 2425af732203SDimitry Andric dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel | 2426af732203SDimitry Andric (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4 2427af732203SDimitry Andric : dwarf::DW_EH_PE_sdata8); 24288bcb0991SDimitry Andric PersonalityEncoding = 0; 24298bcb0991SDimitry Andric LSDAEncoding = 0; 2430af732203SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_udata4; 24318bcb0991SDimitry Andric } 24328bcb0991SDimitry Andric 24338bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 24348bcb0991SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2435af732203SDimitry Andric report_fatal_error("no static constructor section on AIX"); 24368bcb0991SDimitry Andric } 24378bcb0991SDimitry Andric 24388bcb0991SDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 24398bcb0991SDimitry Andric unsigned Priority, const MCSymbol *KeySym) const { 2440af732203SDimitry Andric report_fatal_error("no static destructor section on AIX"); 24418bcb0991SDimitry Andric } 24428bcb0991SDimitry Andric 24438bcb0991SDimitry Andric const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 24448bcb0991SDimitry Andric const GlobalValue *LHS, const GlobalValue *RHS, 24458bcb0991SDimitry Andric const TargetMachine &TM) const { 24468bcb0991SDimitry Andric report_fatal_error("XCOFF not yet implemented."); 24478bcb0991SDimitry Andric } 24488bcb0991SDimitry Andric 2449af732203SDimitry Andric XCOFF::StorageClass 2450af732203SDimitry Andric TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) { 2451af732203SDimitry Andric assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX."); 2452af732203SDimitry Andric 2453af732203SDimitry Andric switch (GV->getLinkage()) { 24548bcb0991SDimitry Andric case GlobalValue::InternalLinkage: 2455480093f4SDimitry Andric case GlobalValue::PrivateLinkage: 24568bcb0991SDimitry Andric return XCOFF::C_HIDEXT; 24578bcb0991SDimitry Andric case GlobalValue::ExternalLinkage: 24588bcb0991SDimitry Andric case GlobalValue::CommonLinkage: 24595ffd83dbSDimitry Andric case GlobalValue::AvailableExternallyLinkage: 24608bcb0991SDimitry Andric return XCOFF::C_EXT; 24618bcb0991SDimitry Andric case GlobalValue::ExternalWeakLinkage: 24625ffd83dbSDimitry Andric case GlobalValue::LinkOnceAnyLinkage: 24635ffd83dbSDimitry Andric case GlobalValue::LinkOnceODRLinkage: 24645ffd83dbSDimitry Andric case GlobalValue::WeakAnyLinkage: 24655ffd83dbSDimitry Andric case GlobalValue::WeakODRLinkage: 24668bcb0991SDimitry Andric return XCOFF::C_WEAKEXT; 24675ffd83dbSDimitry Andric case GlobalValue::AppendingLinkage: 24688bcb0991SDimitry Andric report_fatal_error( 24695ffd83dbSDimitry Andric "There is no mapping that implements AppendingLinkage for XCOFF."); 24708bcb0991SDimitry Andric } 24715ffd83dbSDimitry Andric llvm_unreachable("Unknown linkage type!"); 24725ffd83dbSDimitry Andric } 24735ffd83dbSDimitry Andric 24745ffd83dbSDimitry Andric MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol( 2475af732203SDimitry Andric const GlobalValue *Func, const TargetMachine &TM) const { 2476af732203SDimitry Andric assert( 2477af732203SDimitry Andric (isa<Function>(Func) || 2478af732203SDimitry Andric (isa<GlobalAlias>(Func) && 2479af732203SDimitry Andric isa_and_nonnull<Function>(cast<GlobalAlias>(Func)->getBaseObject()))) && 2480af732203SDimitry Andric "Func must be a function or an alias which has a function as base " 2481af732203SDimitry Andric "object."); 2482af732203SDimitry Andric 24835ffd83dbSDimitry Andric SmallString<128> NameStr; 24845ffd83dbSDimitry Andric NameStr.push_back('.'); 2485af732203SDimitry Andric getNameWithPrefix(NameStr, Func, TM); 2486af732203SDimitry Andric 2487af732203SDimitry Andric // When -function-sections is enabled and explicit section is not specified, 2488af732203SDimitry Andric // it's not necessary to emit function entry point label any more. We will use 2489af732203SDimitry Andric // function entry point csect instead. And for function delcarations, the 2490af732203SDimitry Andric // undefined symbols gets treated as csect with XTY_ER property. 2491af732203SDimitry Andric if (((TM.getFunctionSections() && !Func->hasSection()) || 2492af732203SDimitry Andric Func->isDeclaration()) && 2493af732203SDimitry Andric isa<Function>(Func)) { 2494af732203SDimitry Andric return getContext() 2495*5f7ddb14SDimitry Andric .getXCOFFSection( 2496*5f7ddb14SDimitry Andric NameStr, SectionKind::getText(), 2497*5f7ddb14SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_PR, Func->isDeclaration() 2498*5f7ddb14SDimitry Andric ? XCOFF::XTY_ER 2499*5f7ddb14SDimitry Andric : XCOFF::XTY_SD)) 2500af732203SDimitry Andric ->getQualNameSymbol(); 2501af732203SDimitry Andric } 2502af732203SDimitry Andric 25035ffd83dbSDimitry Andric return getContext().getOrCreateSymbol(NameStr); 25045ffd83dbSDimitry Andric } 25055ffd83dbSDimitry Andric 25065ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( 25075ffd83dbSDimitry Andric const Function *F, const TargetMachine &TM) const { 25085ffd83dbSDimitry Andric SmallString<128> NameStr; 25095ffd83dbSDimitry Andric getNameWithPrefix(NameStr, F, TM); 2510*5f7ddb14SDimitry Andric return getContext().getXCOFFSection( 2511*5f7ddb14SDimitry Andric NameStr, SectionKind::getData(), 2512*5f7ddb14SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_DS, XCOFF::XTY_SD)); 25135ffd83dbSDimitry Andric } 25145ffd83dbSDimitry Andric 25155ffd83dbSDimitry Andric MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( 2516af732203SDimitry Andric const MCSymbol *Sym, const TargetMachine &TM) const { 2517af732203SDimitry Andric // Use TE storage-mapping class when large code model is enabled so that 2518af732203SDimitry Andric // the chance of needing -bbigtoc is decreased. 25195ffd83dbSDimitry Andric return getContext().getXCOFFSection( 2520*5f7ddb14SDimitry Andric cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(), 2521*5f7ddb14SDimitry Andric XCOFF::CsectProperties( 2522af732203SDimitry Andric TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE : XCOFF::XMC_TC, 2523*5f7ddb14SDimitry Andric XCOFF::XTY_SD)); 2524*5f7ddb14SDimitry Andric } 2525*5f7ddb14SDimitry Andric 2526*5f7ddb14SDimitry Andric //===----------------------------------------------------------------------===// 2527*5f7ddb14SDimitry Andric // GOFF 2528*5f7ddb14SDimitry Andric //===----------------------------------------------------------------------===// 2529*5f7ddb14SDimitry Andric TargetLoweringObjectFileGOFF::TargetLoweringObjectFileGOFF() 2530*5f7ddb14SDimitry Andric : TargetLoweringObjectFile() {} 2531*5f7ddb14SDimitry Andric 2532*5f7ddb14SDimitry Andric MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal( 2533*5f7ddb14SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2534*5f7ddb14SDimitry Andric return SelectSectionForGlobal(GO, Kind, TM); 2535*5f7ddb14SDimitry Andric } 2536*5f7ddb14SDimitry Andric 2537*5f7ddb14SDimitry Andric MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal( 2538*5f7ddb14SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2539*5f7ddb14SDimitry Andric auto *Symbol = TM.getSymbol(GO); 2540*5f7ddb14SDimitry Andric if (Kind.isBSS()) 2541*5f7ddb14SDimitry Andric return getContext().getGOFFSection(Symbol->getName(), 2542*5f7ddb14SDimitry Andric SectionKind::getBSS()); 2543*5f7ddb14SDimitry Andric 2544*5f7ddb14SDimitry Andric return getContext().getObjectFileInfo()->getTextSection(); 25458bcb0991SDimitry Andric } 2546