LCOV - code coverage report
Current view: top level - lib/CodeGen - TargetLoweringObjectFileImpl.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 643 739 87.0 %
Date: 2018-10-20 13:21:21 Functions: 54 64 84.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements classes used to handle lowerings specific to common
      11             : // object file formats.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
      16             : #include "llvm/ADT/SmallString.h"
      17             : #include "llvm/ADT/SmallVector.h"
      18             : #include "llvm/ADT/StringExtras.h"
      19             : #include "llvm/ADT/StringRef.h"
      20             : #include "llvm/ADT/Triple.h"
      21             : #include "llvm/BinaryFormat/COFF.h"
      22             : #include "llvm/BinaryFormat/Dwarf.h"
      23             : #include "llvm/BinaryFormat/ELF.h"
      24             : #include "llvm/BinaryFormat/MachO.h"
      25             : #include "llvm/CodeGen/MachineModuleInfo.h"
      26             : #include "llvm/CodeGen/MachineModuleInfoImpls.h"
      27             : #include "llvm/IR/Comdat.h"
      28             : #include "llvm/IR/Constants.h"
      29             : #include "llvm/IR/DataLayout.h"
      30             : #include "llvm/IR/DerivedTypes.h"
      31             : #include "llvm/IR/Function.h"
      32             : #include "llvm/IR/GlobalAlias.h"
      33             : #include "llvm/IR/GlobalObject.h"
      34             : #include "llvm/IR/GlobalValue.h"
      35             : #include "llvm/IR/GlobalVariable.h"
      36             : #include "llvm/IR/Mangler.h"
      37             : #include "llvm/IR/Metadata.h"
      38             : #include "llvm/IR/Module.h"
      39             : #include "llvm/IR/Type.h"
      40             : #include "llvm/MC/MCAsmInfo.h"
      41             : #include "llvm/MC/MCContext.h"
      42             : #include "llvm/MC/MCExpr.h"
      43             : #include "llvm/MC/MCSectionCOFF.h"
      44             : #include "llvm/MC/MCSectionELF.h"
      45             : #include "llvm/MC/MCSectionMachO.h"
      46             : #include "llvm/MC/MCSectionWasm.h"
      47             : #include "llvm/MC/MCStreamer.h"
      48             : #include "llvm/MC/MCSymbol.h"
      49             : #include "llvm/MC/MCSymbolELF.h"
      50             : #include "llvm/MC/MCValue.h"
      51             : #include "llvm/MC/SectionKind.h"
      52             : #include "llvm/ProfileData/InstrProf.h"
      53             : #include "llvm/Support/Casting.h"
      54             : #include "llvm/Support/CodeGen.h"
      55             : #include "llvm/Support/Format.h"
      56             : #include "llvm/Support/ErrorHandling.h"
      57             : #include "llvm/Support/raw_ostream.h"
      58             : #include "llvm/Target/TargetMachine.h"
      59             : #include <cassert>
      60             : #include <string>
      61             : 
      62             : using namespace llvm;
      63             : using namespace dwarf;
      64             : 
      65       26581 : static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
      66             :                              StringRef &Section) {
      67             :   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
      68       26581 :   M.getModuleFlagsMetadata(ModuleFlags);
      69             : 
      70       36259 :   for (const auto &MFE: ModuleFlags) {
      71             :     // Ignore flags with 'Require' behaviour.
      72        9678 :     if (MFE.Behavior == Module::Require)
      73           0 :       continue;
      74             : 
      75        9678 :     StringRef Key = MFE.Key->getString();
      76             :     if (Key == "Objective-C Image Info Version") {
      77         112 :       Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
      78             :     } else if (Key == "Objective-C Garbage Collection" ||
      79             :                Key == "Objective-C GC Only" ||
      80             :                Key == "Objective-C Is Simulated" ||
      81             :                Key == "Objective-C Class Properties" ||
      82             :                Key == "Objective-C Image Swift Version") {
      83         224 :       Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
      84             :     } else if (Key == "Objective-C Image Info Section") {
      85          56 :       Section = cast<MDString>(MFE.Val)->getString();
      86             :     }
      87             :   }
      88       26581 : }
      89             : 
      90             : //===----------------------------------------------------------------------===//
      91             : //                                  ELF
      92             : //===----------------------------------------------------------------------===//
      93             : 
      94       23536 : void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
      95             :                                              const TargetMachine &TgtM) {
      96       23536 :   TargetLoweringObjectFile::Initialize(Ctx, TgtM);
      97       23536 :   TM = &TgtM;
      98             : 
      99       23536 :   CodeModel::Model CM = TgtM.getCodeModel();
     100             : 
     101       23536 :   switch (TgtM.getTargetTriple().getArch()) {
     102        1974 :   case Triple::arm:
     103             :   case Triple::armeb:
     104             :   case Triple::thumb:
     105             :   case Triple::thumbeb:
     106        1974 :     if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
     107             :       break;
     108             :     // Fallthrough if not using EHABI
     109             :     LLVM_FALLTHROUGH;
     110             :   case Triple::ppc:
     111             :   case Triple::x86:
     112        3774 :     PersonalityEncoding = isPositionIndependent()
     113        1887 :                               ? dwarf::DW_EH_PE_indirect |
     114             :                                     dwarf::DW_EH_PE_pcrel |
     115             :                                     dwarf::DW_EH_PE_sdata4
     116             :                               : dwarf::DW_EH_PE_absptr;
     117        1887 :     LSDAEncoding = isPositionIndependent()
     118        1887 :                        ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
     119             :                        : dwarf::DW_EH_PE_absptr;
     120        1887 :     TTypeEncoding = isPositionIndependent()
     121             :                         ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     122             :                               dwarf::DW_EH_PE_sdata4
     123             :                         : dwarf::DW_EH_PE_absptr;
     124        1887 :     break;
     125       11215 :   case Triple::x86_64:
     126       11215 :     if (isPositionIndependent()) {
     127        1582 :       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     128         791 :         ((CM == CodeModel::Small || CM == CodeModel::Medium)
     129             :          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
     130         791 :       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
     131             :         (CM == CodeModel::Small
     132             :          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
     133         799 :       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     134             :         ((CM == CodeModel::Small || CM == CodeModel::Medium)
     135             :          ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
     136             :     } else {
     137       10424 :       PersonalityEncoding =
     138       10424 :         (CM == CodeModel::Small || CM == CodeModel::Medium)
     139       10424 :         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
     140       10424 :       LSDAEncoding = (CM == CodeModel::Small)
     141       10424 :         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
     142       10424 :       TTypeEncoding = (CM == CodeModel::Small)
     143             :         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
     144             :     }
     145             :     break;
     146         900 :   case Triple::hexagon:
     147         900 :     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     148         900 :     LSDAEncoding = dwarf::DW_EH_PE_absptr;
     149         900 :     TTypeEncoding = dwarf::DW_EH_PE_absptr;
     150         900 :     if (isPositionIndependent()) {
     151          16 :       PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
     152          16 :       LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
     153          16 :       TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
     154             :     }
     155             :     break;
     156         866 :   case Triple::aarch64:
     157             :   case Triple::aarch64_be:
     158             :     // The small model guarantees static code/data size < 4GB, but not where it
     159             :     // will be in memory. Most of these could end up >2GB away so even a signed
     160             :     // pc-relative 32-bit address is insufficient, theoretically.
     161         866 :     if (isPositionIndependent()) {
     162          39 :       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     163             :         dwarf::DW_EH_PE_sdata8;
     164          39 :       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
     165          39 :       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     166             :         dwarf::DW_EH_PE_sdata8;
     167             :     } else {
     168         827 :       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     169         827 :       LSDAEncoding = dwarf::DW_EH_PE_absptr;
     170         827 :       TTypeEncoding = dwarf::DW_EH_PE_absptr;
     171             :     }
     172             :     break;
     173          21 :   case Triple::lanai:
     174          21 :     LSDAEncoding = dwarf::DW_EH_PE_absptr;
     175          21 :     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     176          21 :     TTypeEncoding = dwarf::DW_EH_PE_absptr;
     177          21 :     break;
     178        1895 :   case Triple::mips:
     179             :   case Triple::mipsel:
     180             :   case Triple::mips64:
     181             :   case Triple::mips64el:
     182             :     // MIPS uses indirect pointer to refer personality functions and types, so
     183             :     // that the eh_frame section can be read-only. DW.ref.personality will be
     184             :     // generated for relocation.
     185        1895 :     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
     186             :     // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
     187             :     //        identify N64 from just a triple.
     188        1895 :     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     189             :                     dwarf::DW_EH_PE_sdata4;
     190             :     // We don't support PC-relative LSDA references in GAS so we use the default
     191             :     // DW_EH_PE_absptr for those.
     192             : 
     193             :     // FreeBSD must be explicit about the data size and using pcrel since it's
     194             :     // assembler/linker won't do the automatic conversion that the Linux tools
     195             :     // do.
     196        1895 :     if (TgtM.getTargetTriple().isOSFreeBSD()) {
     197           5 :       PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     198           5 :       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     199             :     }
     200             :     break;
     201        1199 :   case Triple::ppc64:
     202             :   case Triple::ppc64le:
     203        1199 :     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     204             :       dwarf::DW_EH_PE_udata8;
     205        1199 :     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
     206        1199 :     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     207             :       dwarf::DW_EH_PE_udata8;
     208        1199 :     break;
     209         137 :   case Triple::sparcel:
     210             :   case Triple::sparc:
     211         137 :     if (isPositionIndependent()) {
     212          10 :       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     213          10 :       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     214             :         dwarf::DW_EH_PE_sdata4;
     215          10 :       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     216             :         dwarf::DW_EH_PE_sdata4;
     217             :     } else {
     218         127 :       LSDAEncoding = dwarf::DW_EH_PE_absptr;
     219         127 :       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     220         127 :       TTypeEncoding = dwarf::DW_EH_PE_absptr;
     221             :     }
     222             :     break;
     223          58 :   case Triple::sparcv9:
     224          58 :     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     225          58 :     if (isPositionIndependent()) {
     226          10 :       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     227             :         dwarf::DW_EH_PE_sdata4;
     228          10 :       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     229             :         dwarf::DW_EH_PE_sdata4;
     230             :     } else {
     231          48 :       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     232          48 :       TTypeEncoding = dwarf::DW_EH_PE_absptr;
     233             :     }
     234             :     break;
     235         889 :   case Triple::systemz:
     236             :     // All currently-defined code models guarantee that 4-byte PC-relative
     237             :     // values will be in range.
     238         889 :     if (isPositionIndependent()) {
     239          12 :       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     240             :         dwarf::DW_EH_PE_sdata4;
     241          12 :       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     242          12 :       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
     243             :         dwarf::DW_EH_PE_sdata4;
     244             :     } else {
     245         877 :       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
     246         877 :       LSDAEncoding = dwarf::DW_EH_PE_absptr;
     247         877 :       TTypeEncoding = dwarf::DW_EH_PE_absptr;
     248             :     }
     249             :     break;
     250             :   default:
     251             :     break;
     252             :   }
     253       23536 : }
     254             : 
     255       23354 : void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
     256             :                                                      Module &M) const {
     257       23354 :   auto &C = getContext();
     258             : 
     259       23354 :   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
     260          33 :     auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
     261             :                               ELF::SHF_EXCLUDE);
     262             : 
     263          33 :     Streamer.SwitchSection(S);
     264             : 
     265          69 :     for (const auto &Operand : LinkerOptions->operands()) {
     266          36 :       if (cast<MDNode>(Operand)->getNumOperands() != 2)
     267           0 :         report_fatal_error("invalid llvm.linker.options");
     268         108 :       for (const auto &Option : cast<MDNode>(Operand)->operands()) {
     269         144 :         Streamer.EmitBytes(cast<MDString>(Option)->getString());
     270          72 :         Streamer.EmitIntValue(0, 1);
     271             :       }
     272             :     }
     273             :   }
     274             : 
     275       23354 :   unsigned Version = 0;
     276       23354 :   unsigned Flags = 0;
     277       23354 :   StringRef Section;
     278             : 
     279       23354 :   GetObjCImageInfo(M, Version, Flags, Section);
     280       23354 :   if (!Section.empty()) {
     281          28 :     auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
     282          28 :     Streamer.SwitchSection(S);
     283          56 :     Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
     284          28 :     Streamer.EmitIntValue(Version, 4);
     285          28 :     Streamer.EmitIntValue(Flags, 4);
     286          28 :     Streamer.AddBlankLine();
     287             :   }
     288             : 
     289             :   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
     290       23354 :   M.getModuleFlagsMetadata(ModuleFlags);
     291             : 
     292             :   MDNode *CFGProfile = nullptr;
     293             : 
     294       31812 :   for (const auto &MFE : ModuleFlags) {
     295        8461 :     StringRef Key = MFE.Key->getString();
     296             :     if (Key == "CG Profile") {
     297           3 :       CFGProfile = cast<MDNode>(MFE.Val);
     298           3 :       break;
     299             :     }
     300             :   }
     301             : 
     302       23354 :   if (!CFGProfile)
     303             :     return;
     304             : 
     305             :   auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
     306          18 :     if (!MDO)
     307             :       return nullptr;
     308             :     auto V = cast<ValueAsMetadata>(MDO);
     309          16 :     const Function *F = cast<Function>(V->getValue());
     310          16 :     return TM->getSymbol(F);
     311             :   };
     312             : 
     313          12 :   for (const auto &Edge : CFGProfile->operands()) {
     314             :     MDNode *E = cast<MDNode>(Edge);
     315             :     const MCSymbol *From = GetSym(E->getOperand(0));
     316             :     const MCSymbol *To = GetSym(E->getOperand(1));
     317             :     // Skip null functions. This can happen if functions are dead stripped after
     318             :     // the CGProfile pass has been run.
     319           9 :     if (!From || !To)
     320             :       continue;
     321             :     uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
     322             :                          ->getValue()
     323           7 :                          ->getUniqueInteger()
     324             :                          .getZExtValue();
     325           7 :     Streamer.emitCGProfileEntry(
     326           7 :         MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
     327           7 :         MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
     328             :   }
     329             : }
     330             : 
     331       46641 : MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
     332             :     const GlobalValue *GV, const TargetMachine &TM,
     333             :     MachineModuleInfo *MMI) const {
     334       46641 :   unsigned Encoding = getPersonalityEncoding();
     335       46641 :   if ((Encoding & 0x80) == DW_EH_PE_indirect)
     336        2975 :     return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
     337        8925 :                                           TM.getSymbol(GV)->getName());
     338       43666 :   if ((Encoding & 0x70) == DW_EH_PE_absptr)
     339       43666 :     return TM.getSymbol(GV);
     340           0 :   report_fatal_error("We do not support this DWARF encoding yet!");
     341             : }
     342             : 
     343         124 : void TargetLoweringObjectFileELF::emitPersonalityValue(
     344             :     MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
     345             :   SmallString<64> NameData("DW.ref.");
     346             :   NameData += Sym->getName();
     347             :   MCSymbolELF *Label =
     348         248 :       cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
     349         124 :   Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
     350         124 :   Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
     351             :   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
     352         372 :   MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
     353             :                                                    ELF::SHT_PROGBITS, Flags, 0);
     354         124 :   unsigned Size = DL.getPointerSize();
     355         124 :   Streamer.SwitchSection(Sec);
     356         124 :   Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0));
     357         124 :   Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
     358         124 :   const MCExpr *E = MCConstantExpr::create(Size, getContext());
     359         124 :   Streamer.emitELFSize(Label, E);
     360         124 :   Streamer.EmitLabel(Label);
     361             : 
     362         124 :   Streamer.EmitSymbolValue(Sym, Size);
     363         124 : }
     364             : 
     365       11546 : const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
     366             :     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
     367             :     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
     368       11546 :   if (Encoding & DW_EH_PE_indirect) {
     369          49 :     MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
     370             : 
     371          98 :     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
     372             : 
     373             :     // Add information about the stub reference to ELFMMI so that the stub
     374             :     // gets emitted by the asmprinter.
     375          49 :     MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
     376          98 :     if (!StubSym.getPointer()) {
     377          27 :       MCSymbol *Sym = TM.getSymbol(GV);
     378          54 :       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
     379             :     }
     380             : 
     381             :     return TargetLoweringObjectFile::
     382          49 :       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
     383          49 :                         Encoding & ~DW_EH_PE_indirect, Streamer);
     384             :   }
     385             : 
     386       11497 :   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
     387       11497 :                                                            MMI, Streamer);
     388             : }
     389             : 
     390        3698 : static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
     391             :   // N.B.: The defaults used in here are not the same ones used in MC.
     392             :   // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
     393             :   // both gas and MC will produce a section with no flags. Given
     394             :   // section(".eh_frame") gcc will produce:
     395             :   //
     396             :   //   .section   .eh_frame,"a",@progbits
     397             : 
     398        7396 :   if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
     399             :                                       /*AddSegmentInfo=*/false))
     400             :     return SectionKind::getMetadata();
     401             : 
     402        3697 :   if (Name.empty() || Name[0] != '.') return K;
     403             : 
     404             :   // Default implementation based on some magic section names.
     405             :   if (Name == ".bss" ||
     406             :       Name.startswith(".bss.") ||
     407             :       Name.startswith(".gnu.linkonce.b.") ||
     408             :       Name.startswith(".llvm.linkonce.b.") ||
     409             :       Name == ".sbss" ||
     410             :       Name.startswith(".sbss.") ||
     411             :       Name.startswith(".gnu.linkonce.sb.") ||
     412             :       Name.startswith(".llvm.linkonce.sb."))
     413             :     return SectionKind::getBSS();
     414             : 
     415             :   if (Name == ".tdata" ||
     416             :       Name.startswith(".tdata.") ||
     417             :       Name.startswith(".gnu.linkonce.td.") ||
     418             :       Name.startswith(".llvm.linkonce.td."))
     419             :     return SectionKind::getThreadData();
     420             : 
     421             :   if (Name == ".tbss" ||
     422             :       Name.startswith(".tbss.") ||
     423             :       Name.startswith(".gnu.linkonce.tb.") ||
     424             :       Name.startswith(".llvm.linkonce.tb."))
     425             :     return SectionKind::getThreadBSS();
     426             : 
     427        3497 :   return K;
     428             : }
     429             : 
     430           0 : static unsigned getELFSectionType(StringRef Name, SectionKind K) {
     431             :   // Use SHT_NOTE for section whose name starts with ".note" to allow
     432             :   // emitting ELF notes from C variable declaration.
     433             :   // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
     434             :   if (Name.startswith(".note"))
     435           0 :     return ELF::SHT_NOTE;
     436             : 
     437             :   if (Name == ".init_array")
     438           0 :     return ELF::SHT_INIT_ARRAY;
     439             : 
     440             :   if (Name == ".fini_array")
     441           0 :     return ELF::SHT_FINI_ARRAY;
     442             : 
     443             :   if (Name == ".preinit_array")
     444           0 :     return ELF::SHT_PREINIT_ARRAY;
     445             : 
     446           0 :   if (K.isBSS() || K.isThreadBSS())
     447           0 :     return ELF::SHT_NOBITS;
     448             : 
     449             :   return ELF::SHT_PROGBITS;
     450             : }
     451             : 
     452           0 : static unsigned getELFSectionFlags(SectionKind K) {
     453             :   unsigned Flags = 0;
     454             : 
     455           0 :   if (!K.isMetadata())
     456             :     Flags |= ELF::SHF_ALLOC;
     457             : 
     458           0 :   if (K.isText())
     459           0 :     Flags |= ELF::SHF_EXECINSTR;
     460             : 
     461           0 :   if (K.isExecuteOnly())
     462           0 :     Flags |= ELF::SHF_ARM_PURECODE;
     463             : 
     464             :   if (K.isWriteable())
     465           0 :     Flags |= ELF::SHF_WRITE;
     466             : 
     467           0 :   if (K.isThreadLocal())
     468           0 :     Flags |= ELF::SHF_TLS;
     469             : 
     470           0 :   if (K.isMergeableCString() || K.isMergeableConst())
     471           0 :     Flags |= ELF::SHF_MERGE;
     472             : 
     473           0 :   if (K.isMergeableCString())
     474           0 :     Flags |= ELF::SHF_STRINGS;
     475             : 
     476           0 :   return Flags;
     477             : }
     478             : 
     479      649412 : static const Comdat *getELFComdat(const GlobalValue *GV) {
     480      649412 :   const Comdat *C = GV->getComdat();
     481      649412 :   if (!C)
     482             :     return nullptr;
     483             : 
     484      215441 :   if (C->getSelectionKind() != Comdat::Any)
     485           0 :     report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
     486           0 :                        C->getName() + "' cannot be lowered.");
     487             : 
     488             :   return C;
     489             : }
     490             : 
     491      647892 : static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO,
     492             :                                               const TargetMachine &TM) {
     493      647892 :   MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
     494      647892 :   if (!MD)
     495             :     return nullptr;
     496             : 
     497             :   const MDOperand &Op = MD->getOperand(0);
     498          22 :   if (!Op.get())
     499             :     return nullptr;
     500             : 
     501             :   auto *VM = dyn_cast<ValueAsMetadata>(Op);
     502             :   if (!VM)
     503           0 :     report_fatal_error("MD_associated operand is not ValueAsMetadata");
     504             : 
     505          20 :   GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
     506          18 :   return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
     507             : }
     508             : 
     509           0 : static unsigned getEntrySizeForKind(SectionKind Kind) {
     510      649412 :   if (Kind.isMergeable1ByteCString())
     511           0 :     return 1;
     512      464011 :   else if (Kind.isMergeable2ByteCString())
     513           0 :     return 2;
     514      463806 :   else if (Kind.isMergeable4ByteCString())
     515           0 :     return 4;
     516      462731 :   else if (Kind.isMergeableConst4())
     517           0 :     return 4;
     518      462030 :   else if (Kind.isMergeableConst8())
     519           0 :     return 8;
     520      461011 :   else if (Kind.isMergeableConst16())
     521           0 :     return 16;
     522      458938 :   else if (Kind.isMergeableConst32())
     523           0 :     return 32;
     524             :   else {
     525             :     // We shouldn't have mergeable C strings or mergeable constants that we
     526             :     // didn't handle above.
     527             :     assert(!Kind.isMergeableCString() && "unknown string width");
     528             :     assert(!Kind.isMergeableConst() && "unknown data width");
     529           0 :     return 0;
     530             :   }
     531             : }
     532             : 
     533        3698 : MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
     534             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
     535        3698 :   StringRef SectionName = GO->getSection();
     536             : 
     537             :   // Check if '#pragma clang section' name is applicable.
     538             :   // Note that pragma directive overrides -ffunction-section, -fdata-section
     539             :   // and so section name is exactly as user specified and not uniqued.
     540             :   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
     541         170 :   if (GV && GV->hasImplicitSection()) {
     542          13 :     auto Attrs = GV->getAttributes();
     543          13 :     if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
     544           7 :       SectionName = Attrs.getAttribute("bss-section").getValueAsString();
     545          12 :     } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
     546           3 :       SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
     547           3 :     } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
     548           3 :       SectionName = Attrs.getAttribute("data-section").getValueAsString();
     549             :     }
     550             :   }
     551             :   const Function *F = dyn_cast<Function>(GO);
     552        3528 :   if (F && F->hasFnAttribute("implicit-section-name")) {
     553           2 :     SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
     554             :   }
     555             : 
     556             :   // Infer section flags from the section name if we can.
     557        3698 :   Kind = getELFKindForNamedSection(SectionName, Kind);
     558             : 
     559             :   StringRef Group = "";
     560        3698 :   unsigned Flags = getELFSectionFlags(Kind);
     561        3698 :   if (const Comdat *C = getELFComdat(GO)) {
     562           3 :     Group = C->getName();
     563           3 :     Flags |= ELF::SHF_GROUP;
     564             :   }
     565             : 
     566             :   // A section can have at most one associated section. Put each global with
     567             :   // MD_associated in a unique section.
     568             :   unsigned UniqueID = MCContext::GenericSectionID;
     569        3698 :   const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
     570        3698 :   if (AssociatedSymbol) {
     571           6 :     UniqueID = NextUniqueID++;
     572           6 :     Flags |= ELF::SHF_LINK_ORDER;
     573             :   }
     574             : 
     575       11088 :   MCSectionELF *Section = getContext().getELFSection(
     576             :       SectionName, getELFSectionType(SectionName, Kind), Flags,
     577             :       getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
     578             :   // Make sure that we did not get some other section with incompatible sh_link.
     579             :   // This should not be possible due to UniqueID code above.
     580             :   assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
     581             :          "Associated symbol mismatch between sections");
     582        3698 :   return Section;
     583             : }
     584             : 
     585             : /// Return the section prefix name used by options FunctionsSections and
     586             : /// DataSections.
     587           0 : static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
     588           0 :   if (Kind.isText())
     589           0 :     return ".text";
     590             :   if (Kind.isReadOnly())
     591           0 :     return ".rodata";
     592           0 :   if (Kind.isBSS())
     593           0 :     return ".bss";
     594           0 :   if (Kind.isThreadData())
     595           0 :     return ".tdata";
     596           0 :   if (Kind.isThreadBSS())
     597           0 :     return ".tbss";
     598           0 :   if (Kind.isData())
     599           0 :     return ".data";
     600             :   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
     601           0 :   return ".data.rel.ro";
     602             : }
     603             : 
     604      645714 : static MCSectionELF *selectELFSectionForGlobal(
     605             :     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
     606             :     const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
     607             :     unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
     608             : 
     609             :   StringRef Group = "";
     610      645714 :   if (const Comdat *C = getELFComdat(GO)) {
     611      215438 :     Flags |= ELF::SHF_GROUP;
     612      215438 :     Group = C->getName();
     613             :   }
     614             : 
     615             :   // Get the section entry size based on the kind.
     616             :   unsigned EntrySize = getEntrySizeForKind(Kind);
     617             : 
     618             :   SmallString<128> Name;
     619      645714 :   if (Kind.isMergeableCString()) {
     620             :     // We also need alignment here.
     621             :     // FIXME: this is getting the alignment of the character, not the
     622             :     // alignment of the global!
     623      186676 :     unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
     624             :         cast<GlobalVariable>(GO));
     625             : 
     626      373352 :     std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
     627      373352 :     Name = SizeSpec + utostr(Align);
     628      459038 :   } else if (Kind.isMergeableConst()) {
     629             :     Name = ".rodata.cst";
     630        8592 :     Name += utostr(EntrySize);
     631             :   } else {
     632      454742 :     Name = getSectionPrefixForGlobal(Kind);
     633             :   }
     634             : 
     635             :   if (const auto *F = dyn_cast<Function>(GO)) {
     636      377478 :     const auto &OptionalPrefix = F->getSectionPrefix();
     637      377478 :     if (OptionalPrefix)
     638             :       Name += *OptionalPrefix;
     639             :   }
     640             : 
     641             :   unsigned UniqueID = MCContext::GenericSectionID;
     642      645714 :   if (EmitUniqueSection) {
     643      243771 :     if (TM.getUniqueSectionNames()) {
     644      243764 :       Name.push_back('.');
     645      243764 :       TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/);
     646             :     } else {
     647           7 :       UniqueID = *NextUniqueID;
     648           7 :       (*NextUniqueID)++;
     649             :     }
     650             :   }
     651             :   // Use 0 as the unique ID for execute-only text.
     652      645714 :   if (Kind.isExecuteOnly())
     653             :     UniqueID = 0;
     654     1291428 :   return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
     655      645714 :                            EntrySize, Group, UniqueID, AssociatedSymbol);
     656             : }
     657             : 
     658      644194 : MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
     659             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
     660      644194 :   unsigned Flags = getELFSectionFlags(Kind);
     661             : 
     662             :   // If we have -ffunction-section or -fdata-section then we should emit the
     663             :   // global value to a uniqued section specifically for it.
     664             :   bool EmitUniqueSection = false;
     665      644194 :   if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
     666      453222 :     if (Kind.isText())
     667             :       EmitUniqueSection = TM.getFunctionSections();
     668             :     else
     669             :       EmitUniqueSection = TM.getDataSections();
     670             :   }
     671      644194 :   EmitUniqueSection |= GO->hasComdat();
     672             : 
     673      644194 :   const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
     674      644194 :   if (AssociatedSymbol) {
     675             :     EmitUniqueSection = true;
     676          12 :     Flags |= ELF::SHF_LINK_ORDER;
     677             :   }
     678             : 
     679      644194 :   MCSectionELF *Section = selectELFSectionForGlobal(
     680             :       getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
     681             :       &NextUniqueID, AssociatedSymbol);
     682             :   assert(Section->getAssociatedSymbol() == AssociatedSymbol);
     683      644193 :   return Section;
     684             : }
     685             : 
     686        2079 : MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
     687             :     const Function &F, const TargetMachine &TM) const {
     688             :   // If the function can be removed, produce a unique section so that
     689             :   // the table doesn't prevent the removal.
     690        2079 :   const Comdat *C = F.getComdat();
     691        2079 :   bool EmitUniqueSection = TM.getFunctionSections() || C;
     692             :   if (!EmitUniqueSection)
     693         559 :     return ReadOnlySection;
     694             : 
     695        1520 :   return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
     696             :                                    getMangler(), TM, EmitUniqueSection,
     697             :                                    ELF::SHF_ALLOC, &NextUniqueID,
     698        1520 :                                    /* AssociatedSymbol */ nullptr);
     699             : }
     700             : 
     701        2073 : bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
     702             :     bool UsesLabelDifference, const Function &F) const {
     703             :   // We can always create relative relocations, so use another section
     704             :   // that can be marked non-executable.
     705        2073 :   return false;
     706             : }
     707             : 
     708             : /// Given a mergeable constant with the specified size and relocation
     709             : /// information, return a section that it should be placed in.
     710       29009 : MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
     711             :     const DataLayout &DL, SectionKind Kind, const Constant *C,
     712             :     unsigned &Align) const {
     713       29009 :   if (Kind.isMergeableConst4() && MergeableConst4Section)
     714             :     return MergeableConst4Section;
     715       24199 :   if (Kind.isMergeableConst8() && MergeableConst8Section)
     716             :     return MergeableConst8Section;
     717       19026 :   if (Kind.isMergeableConst16() && MergeableConst16Section)
     718             :     return MergeableConst16Section;
     719        5485 :   if (Kind.isMergeableConst32() && MergeableConst32Section)
     720             :     return MergeableConst32Section;
     721             :   if (Kind.isReadOnly())
     722        1274 :     return ReadOnlySection;
     723             : 
     724             :   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
     725          17 :   return DataRelROSection;
     726             : }
     727             : 
     728         473 : static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
     729             :                                               bool IsCtor, unsigned Priority,
     730             :                                               const MCSymbol *KeySym) {
     731             :   std::string Name;
     732             :   unsigned Type;
     733             :   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
     734         495 :   StringRef COMDAT = KeySym ? KeySym->getName() : "";
     735             : 
     736         473 :   if (KeySym)
     737             :     Flags |= ELF::SHF_GROUP;
     738             : 
     739         473 :   if (UseInitArray) {
     740         445 :     if (IsCtor) {
     741             :       Type = ELF::SHT_INIT_ARRAY;
     742             :       Name = ".init_array";
     743             :     } else {
     744             :       Type = ELF::SHT_FINI_ARRAY;
     745             :       Name = ".fini_array";
     746             :     }
     747         445 :     if (Priority != 65535) {
     748             :       Name += '.';
     749         160 :       Name += utostr(Priority);
     750             :     }
     751             :   } else {
     752             :     // The default scheme is .ctor / .dtor, so we have to invert the priority
     753             :     // numbering.
     754          28 :     if (IsCtor)
     755             :       Name = ".ctors";
     756             :     else
     757             :       Name = ".dtors";
     758          28 :     if (Priority != 65535)
     759          36 :       raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
     760             :     Type = ELF::SHT_PROGBITS;
     761             :   }
     762             : 
     763         473 :   return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
     764             : }
     765             : 
     766         465 : MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
     767             :     unsigned Priority, const MCSymbol *KeySym) const {
     768         465 :   return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
     769         465 :                                   KeySym);
     770             : }
     771             : 
     772           8 : MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
     773             :     unsigned Priority, const MCSymbol *KeySym) const {
     774           8 :   return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
     775           8 :                                   KeySym);
     776             : }
     777             : 
     778          11 : const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
     779             :     const GlobalValue *LHS, const GlobalValue *RHS,
     780             :     const TargetMachine &TM) const {
     781             :   // We may only use a PLT-relative relocation to refer to unnamed_addr
     782             :   // functions.
     783          11 :   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
     784             :     return nullptr;
     785             : 
     786             :   // Basic sanity checks.
     787          12 :   if (LHS->getType()->getPointerAddressSpace() != 0 ||
     788          12 :       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
     789             :       RHS->isThreadLocal())
     790             :     return nullptr;
     791             : 
     792           6 :   return MCBinaryExpr::createSub(
     793          12 :       MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
     794           6 :                               getContext()),
     795           6 :       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
     796             : }
     797             : 
     798             : void
     799       16344 : TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
     800       16344 :   UseInitArray = UseInitArray_;
     801       16344 :   MCContext &Ctx = getContext();
     802       16344 :   if (!UseInitArray) {
     803         270 :     StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
     804             :                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
     805             : 
     806         270 :     StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
     807             :                                           ELF::SHF_ALLOC | ELF::SHF_WRITE);
     808         270 :     return;
     809             :   }
     810             : 
     811       16074 :   StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
     812             :                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
     813       16074 :   StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
     814             :                                         ELF::SHF_WRITE | ELF::SHF_ALLOC);
     815             : }
     816             : 
     817             : //===----------------------------------------------------------------------===//
     818             : //                                 MachO
     819             : //===----------------------------------------------------------------------===//
     820             : 
     821        4989 : TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
     822        4989 :   : TargetLoweringObjectFile() {
     823        4989 :   SupportIndirectSymViaGOTPCRel = true;
     824        4989 : }
     825             : 
     826        2479 : void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
     827             :                                                const TargetMachine &TM) {
     828        2479 :   TargetLoweringObjectFile::Initialize(Ctx, TM);
     829        2479 :   if (TM.getRelocationModel() == Reloc::Static) {
     830          48 :     StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
     831             :                                             SectionKind::getData());
     832          48 :     StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
     833             :                                             SectionKind::getData());
     834             :   } else {
     835        2431 :     StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
     836             :                                             MachO::S_MOD_INIT_FUNC_POINTERS,
     837             :                                             SectionKind::getData());
     838        2431 :     StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
     839             :                                             MachO::S_MOD_TERM_FUNC_POINTERS,
     840             :                                             SectionKind::getData());
     841             :   }
     842             : 
     843        2479 :   PersonalityEncoding =
     844             :       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     845        2479 :   LSDAEncoding = dwarf::DW_EH_PE_pcrel;
     846        2479 :   TTypeEncoding =
     847             :       dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
     848        2479 : }
     849             : 
     850        2467 : void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
     851             :                                                        Module &M) const {
     852             :   // Emit the linker options if present.
     853        2467 :   if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
     854          11 :     for (const auto &Option : LinkerOptions->operands()) {
     855           6 :       SmallVector<std::string, 4> StrOptions;
     856          14 :       for (const auto &Piece : cast<MDNode>(Option)->operands())
     857          24 :         StrOptions.push_back(cast<MDString>(Piece)->getString());
     858          12 :       Streamer.EmitLinkerOptions(StrOptions);
     859             :     }
     860             :   }
     861             : 
     862        2467 :   unsigned VersionVal = 0;
     863        2467 :   unsigned ImageInfoFlags = 0;
     864        2467 :   StringRef SectionVal;
     865             : 
     866        2467 :   GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
     867             : 
     868             :   // The section is mandatory. If we don't have it, then we don't have GC info.
     869        2467 :   if (SectionVal.empty())
     870        2442 :     return;
     871             : 
     872          25 :   StringRef Segment, Section;
     873          25 :   unsigned TAA = 0, StubSize = 0;
     874             :   bool TAAParsed;
     875             :   std::string ErrorCode =
     876             :     MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
     877          25 :                                           TAA, TAAParsed, StubSize);
     878          25 :   if (!ErrorCode.empty())
     879             :     // If invalid, report the error with report_fatal_error.
     880           0 :     report_fatal_error("Invalid section specifier '" + Section + "': " +
     881             :                        ErrorCode + ".");
     882             : 
     883             :   // Get the section.
     884          25 :   MCSectionMachO *S = getContext().getMachOSection(
     885             :       Segment, Section, TAA, StubSize, SectionKind::getData());
     886          25 :   Streamer.SwitchSection(S);
     887          25 :   Streamer.EmitLabel(getContext().
     888          25 :                      getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
     889          25 :   Streamer.EmitIntValue(VersionVal, 4);
     890          25 :   Streamer.EmitIntValue(ImageInfoFlags, 4);
     891          25 :   Streamer.AddBlankLine();
     892             : }
     893             : 
     894       20826 : static void checkMachOComdat(const GlobalValue *GV) {
     895       20826 :   const Comdat *C = GV->getComdat();
     896       20826 :   if (!C)
     897       20825 :     return;
     898             : 
     899           1 :   report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
     900             :                      "' cannot be lowered.");
     901             : }
     902             : 
     903         816 : MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
     904             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
     905             :   // Parse the section specifier and create it if valid.
     906         816 :   StringRef Segment, Section;
     907         816 :   unsigned TAA = 0, StubSize = 0;
     908             :   bool TAAParsed;
     909             : 
     910         816 :   checkMachOComdat(GO);
     911             : 
     912             :   std::string ErrorCode =
     913             :     MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
     914         816 :                                           TAA, TAAParsed, StubSize);
     915         816 :   if (!ErrorCode.empty()) {
     916             :     // If invalid, report the error with report_fatal_error.
     917           0 :     report_fatal_error("Global variable '" + GO->getName() +
     918             :                        "' has an invalid section specifier '" +
     919           0 :                        GO->getSection() + "': " + ErrorCode + ".");
     920             :   }
     921             : 
     922             :   // Get the section.
     923             :   MCSectionMachO *S =
     924         816 :       getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
     925             : 
     926             :   // If TAA wasn't set by ParseSectionSpecifier() above,
     927             :   // use the value returned by getMachOSection() as a default.
     928         816 :   if (!TAAParsed)
     929         192 :     TAA = S->getTypeAndAttributes();
     930             : 
     931             :   // Okay, now that we got the section, verify that the TAA & StubSize agree.
     932             :   // If the user declared multiple globals with different section flags, we need
     933             :   // to reject it here.
     934         816 :   if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
     935             :     // If invalid, report the error with report_fatal_error.
     936           0 :     report_fatal_error("Global variable '" + GO->getName() +
     937             :                        "' section type or attributes does not match previous"
     938             :                        " section specifier");
     939             :   }
     940             : 
     941         816 :   return S;
     942             : }
     943             : 
     944       20010 : MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
     945             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
     946       20010 :   checkMachOComdat(GO);
     947             : 
     948             :   // Handle thread local data.
     949       20009 :   if (Kind.isThreadBSS()) return TLSBSSSection;
     950       19932 :   if (Kind.isThreadData()) return TLSDataSection;
     951             : 
     952       19922 :   if (Kind.isText())
     953       18678 :     return GO->isWeakForLinker() ? TextCoalSection : TextSection;
     954             : 
     955             :   // If this is weak/linkonce, put this in a coalescable section, either in text
     956             :   // or data depending on if it is writable.
     957             :   if (GO->isWeakForLinker()) {
     958             :     if (Kind.isReadOnly())
     959          27 :       return ConstTextCoalSection;
     960          63 :     if (Kind.isReadOnlyWithRel())
     961           7 :       return ConstDataCoalSection;
     962          56 :     return DataCoalSection;
     963             :   }
     964             : 
     965             :   // FIXME: Alignment check should be handled by section classifier.
     966        1534 :   if (Kind.isMergeable1ByteCString() &&
     967         380 :       GO->getParent()->getDataLayout().getPreferredAlignment(
     968             :           cast<GlobalVariable>(GO)) < 32)
     969         380 :     return CStringSection;
     970             : 
     971             :   // Do not put 16-bit arrays in the UString section if they have an
     972             :   // externally visible label, this runs into issues with certain linker
     973             :   // versions.
     974         778 :   if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
     975           4 :       GO->getParent()->getDataLayout().getPreferredAlignment(
     976             :           cast<GlobalVariable>(GO)) < 32)
     977           4 :     return UStringSection;
     978             : 
     979             :   // With MachO only variables whose corresponding symbol starts with 'l' or
     980             :   // 'L' can be merged, so we only try merging GVs with private linkage.
     981         770 :   if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
     982          35 :     if (Kind.isMergeableConst4())
     983           3 :       return FourByteConstantSection;
     984          32 :     if (Kind.isMergeableConst8())
     985           6 :       return EightByteConstantSection;
     986          26 :     if (Kind.isMergeableConst16())
     987          23 :       return SixteenByteConstantSection;
     988             :   }
     989             : 
     990             :   // Otherwise, if it is readonly, but not something we can specially optimize,
     991             :   // just drop it in .const.
     992             :   if (Kind.isReadOnly())
     993         216 :     return ReadOnlySection;
     994             : 
     995             :   // If this is marked const, put it into a const section.  But if the dynamic
     996             :   // linker needs to write to it, put it in the data segment.
     997         522 :   if (Kind.isReadOnlyWithRel())
     998          72 :     return ConstDataSection;
     999             : 
    1000             :   // Put zero initialized globals with strong external linkage in the
    1001             :   // DATA, __common section with the .zerofill directive.
    1002         450 :   if (Kind.isBSSExtern())
    1003         168 :     return DataCommonSection;
    1004             : 
    1005             :   // Put zero initialized globals with local linkage in __DATA,__bss directive
    1006             :   // with the .zerofill directive (aka .lcomm).
    1007         282 :   if (Kind.isBSSLocal())
    1008         142 :     return DataBSSSection;
    1009             : 
    1010             :   // Otherwise, just drop the variable in the normal data section.
    1011         140 :   return DataSection;
    1012             : }
    1013             : 
    1014        1073 : MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
    1015             :     const DataLayout &DL, SectionKind Kind, const Constant *C,
    1016             :     unsigned &Align) const {
    1017             :   // If this constant requires a relocation, we have to put it in the data
    1018             :   // segment, not in the text segment.
    1019        1073 :   if (Kind.isData() || Kind.isReadOnlyWithRel())
    1020           0 :     return ConstDataSection;
    1021             : 
    1022        1073 :   if (Kind.isMergeableConst4())
    1023         235 :     return FourByteConstantSection;
    1024         838 :   if (Kind.isMergeableConst8())
    1025         111 :     return EightByteConstantSection;
    1026         727 :   if (Kind.isMergeableConst16())
    1027         524 :     return SixteenByteConstantSection;
    1028         203 :   return ReadOnlySection;  // .const
    1029             : }
    1030             : 
    1031           8 : const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
    1032             :     const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
    1033             :     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
    1034             :   // The mach-o version of this method defaults to returning a stub reference.
    1035             : 
    1036           8 :   if (Encoding & DW_EH_PE_indirect) {
    1037             :     MachineModuleInfoMachO &MachOMMI =
    1038           8 :       MMI->getObjFileInfo<MachineModuleInfoMachO>();
    1039             : 
    1040          16 :     MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
    1041             : 
    1042             :     // Add information about the stub reference to MachOMMI so that the stub
    1043             :     // gets emitted by the asmprinter.
    1044           8 :     MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
    1045          16 :     if (!StubSym.getPointer()) {
    1046           7 :       MCSymbol *Sym = TM.getSymbol(GV);
    1047          14 :       StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
    1048             :     }
    1049             : 
    1050             :     return TargetLoweringObjectFile::
    1051           8 :       getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
    1052           8 :                         Encoding & ~DW_EH_PE_indirect, Streamer);
    1053             :   }
    1054             : 
    1055           0 :   return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
    1056           0 :                                                            MMI, Streamer);
    1057             : }
    1058             : 
    1059          23 : MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
    1060             :     const GlobalValue *GV, const TargetMachine &TM,
    1061             :     MachineModuleInfo *MMI) const {
    1062             :   // The mach-o version of this method defaults to returning a stub reference.
    1063             :   MachineModuleInfoMachO &MachOMMI =
    1064          23 :     MMI->getObjFileInfo<MachineModuleInfoMachO>();
    1065             : 
    1066          46 :   MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
    1067             : 
    1068             :   // Add information about the stub reference to MachOMMI so that the stub
    1069             :   // gets emitted by the asmprinter.
    1070          23 :   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
    1071          46 :   if (!StubSym.getPointer()) {
    1072          14 :     MCSymbol *Sym = TM.getSymbol(GV);
    1073          28 :     StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
    1074             :   }
    1075             : 
    1076          23 :   return SSym;
    1077             : }
    1078             : 
    1079          10 : const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
    1080             :     const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
    1081             :     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
    1082             :   // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
    1083             :   // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
    1084             :   // through a non_lazy_ptr stub instead. One advantage is that it allows the
    1085             :   // computation of deltas to final external symbols. Example:
    1086             :   //
    1087             :   //    _extgotequiv:
    1088             :   //       .long   _extfoo
    1089             :   //
    1090             :   //    _delta:
    1091             :   //       .long   _extgotequiv-_delta
    1092             :   //
    1093             :   // is transformed to:
    1094             :   //
    1095             :   //    _delta:
    1096             :   //       .long   L_extfoo$non_lazy_ptr-(_delta+0)
    1097             :   //
    1098             :   //       .section        __IMPORT,__pointers,non_lazy_symbol_pointers
    1099             :   //    L_extfoo$non_lazy_ptr:
    1100             :   //       .indirect_symbol        _extfoo
    1101             :   //       .long   0
    1102             :   //
    1103             :   MachineModuleInfoMachO &MachOMMI =
    1104          10 :     MMI->getObjFileInfo<MachineModuleInfoMachO>();
    1105          10 :   MCContext &Ctx = getContext();
    1106             : 
    1107             :   // The offset must consider the original displacement from the base symbol
    1108             :   // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
    1109          10 :   Offset = -MV.getConstant();
    1110          10 :   const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
    1111             : 
    1112             :   // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
    1113             :   // non_lazy_ptr stubs.
    1114             :   SmallString<128> Name;
    1115             :   StringRef Suffix = "$non_lazy_ptr";
    1116          10 :   Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
    1117             :   Name += Sym->getName();
    1118             :   Name += Suffix;
    1119          10 :   MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
    1120             : 
    1121          10 :   MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
    1122          20 :   if (!StubSym.getPointer())
    1123           4 :     StubSym = MachineModuleInfoImpl::
    1124             :       StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
    1125             : 
    1126             :   const MCExpr *BSymExpr =
    1127          10 :     MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
    1128             :   const MCExpr *LHS =
    1129          10 :     MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
    1130             : 
    1131          10 :   if (!Offset)
    1132           2 :     return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
    1133             : 
    1134             :   const MCExpr *RHS =
    1135           8 :     MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
    1136           8 :   return MCBinaryExpr::createSub(LHS, RHS, Ctx);
    1137             : }
    1138             : 
    1139             : static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
    1140             :                                const MCSection &Section) {
    1141        1019 :   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
    1142             :     return true;
    1143             : 
    1144             :   // If it is not dead stripped, it is safe to use private labels.
    1145             :   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
    1146         706 :   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
    1147             :     return true;
    1148             : 
    1149             :   return false;
    1150             : }
    1151             : 
    1152        1019 : void TargetLoweringObjectFileMachO::getNameWithPrefix(
    1153             :     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
    1154             :     const TargetMachine &TM) const {
    1155             :   bool CannotUsePrivateLabel = true;
    1156        1019 :   if (auto *GO = GV->getBaseObject()) {
    1157        1019 :     SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
    1158        1019 :     const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
    1159        1019 :     CannotUsePrivateLabel =
    1160             :         !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
    1161             :   }
    1162        1019 :   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
    1163        1019 : }
    1164             : 
    1165             : //===----------------------------------------------------------------------===//
    1166             : //                                  COFF
    1167             : //===----------------------------------------------------------------------===//
    1168             : 
    1169             : static unsigned
    1170           0 : getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
    1171             :   unsigned Flags = 0;
    1172           0 :   bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
    1173             : 
    1174           0 :   if (K.isMetadata())
    1175             :     Flags |=
    1176             :       COFF::IMAGE_SCN_MEM_DISCARDABLE;
    1177           0 :   else if (K.isText())
    1178             :     Flags |=
    1179             :       COFF::IMAGE_SCN_MEM_EXECUTE |
    1180             :       COFF::IMAGE_SCN_MEM_READ |
    1181           0 :       COFF::IMAGE_SCN_CNT_CODE |
    1182             :       (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
    1183           0 :   else if (K.isBSS())
    1184             :     Flags |=
    1185             :       COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
    1186             :       COFF::IMAGE_SCN_MEM_READ |
    1187             :       COFF::IMAGE_SCN_MEM_WRITE;
    1188           0 :   else if (K.isThreadLocal())
    1189             :     Flags |=
    1190             :       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1191             :       COFF::IMAGE_SCN_MEM_READ |
    1192             :       COFF::IMAGE_SCN_MEM_WRITE;
    1193           0 :   else if (K.isReadOnly() || K.isReadOnlyWithRel())
    1194             :     Flags |=
    1195             :       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1196             :       COFF::IMAGE_SCN_MEM_READ;
    1197             :   else if (K.isWriteable())
    1198             :     Flags |=
    1199             :       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1200             :       COFF::IMAGE_SCN_MEM_READ |
    1201             :       COFF::IMAGE_SCN_MEM_WRITE;
    1202             : 
    1203           0 :   return Flags;
    1204             : }
    1205             : 
    1206         449 : static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
    1207         449 :   const Comdat *C = GV->getComdat();
    1208             :   assert(C && "expected GV to have a Comdat!");
    1209             : 
    1210         449 :   StringRef ComdatGVName = C->getName();
    1211         449 :   const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
    1212         449 :   if (!ComdatGV)
    1213           1 :     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
    1214             :                        "' does not exist.");
    1215             : 
    1216         448 :   if (ComdatGV->getComdat() != C)
    1217           1 :     report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
    1218             :                        "' is not a key for its COMDAT.");
    1219             : 
    1220         447 :   return ComdatGV;
    1221             : }
    1222             : 
    1223         306 : static int getSelectionForCOFF(const GlobalValue *GV) {
    1224         306 :   if (const Comdat *C = GV->getComdat()) {
    1225         227 :     const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
    1226             :     if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
    1227          20 :       ComdatKey = GA->getBaseObject();
    1228         225 :     if (ComdatKey == GV) {
    1229         216 :       switch (C->getSelectionKind()) {
    1230         191 :       case Comdat::Any:
    1231         191 :         return COFF::IMAGE_COMDAT_SELECT_ANY;
    1232           1 :       case Comdat::ExactMatch:
    1233           1 :         return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
    1234          21 :       case Comdat::Largest:
    1235          21 :         return COFF::IMAGE_COMDAT_SELECT_LARGEST;
    1236           1 :       case Comdat::NoDuplicates:
    1237           1 :         return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
    1238           2 :       case Comdat::SameSize:
    1239           2 :         return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
    1240             :       }
    1241             :     } else {
    1242             :       return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
    1243             :     }
    1244             :   }
    1245             :   return 0;
    1246             : }
    1247             : 
    1248          23 : MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
    1249             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
    1250             :   int Selection = 0;
    1251          23 :   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
    1252          23 :   StringRef Name = GO->getSection();
    1253             :   StringRef COMDATSymName = "";
    1254          23 :   if (GO->hasComdat()) {
    1255           3 :     Selection = getSelectionForCOFF(GO);
    1256             :     const GlobalValue *ComdatGV;
    1257           3 :     if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
    1258           0 :       ComdatGV = getComdatGVForCOFF(GO);
    1259             :     else
    1260             :       ComdatGV = GO;
    1261             : 
    1262           3 :     if (!ComdatGV->hasPrivateLinkage()) {
    1263           3 :       MCSymbol *Sym = TM.getSymbol(ComdatGV);
    1264           3 :       COMDATSymName = Sym->getName();
    1265           3 :       Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
    1266             :     } else {
    1267             :       Selection = 0;
    1268             :     }
    1269             :   }
    1270             : 
    1271          23 :   return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
    1272          23 :                                      Selection);
    1273             : }
    1274             : 
    1275           0 : static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
    1276           0 :   if (Kind.isText())
    1277           0 :     return ".text";
    1278           0 :   if (Kind.isBSS())
    1279           0 :     return ".bss";
    1280           0 :   if (Kind.isThreadLocal())
    1281           0 :     return ".tls$";
    1282           0 :   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
    1283           0 :     return ".rdata";
    1284           0 :   return ".data";
    1285             : }
    1286             : 
    1287        3467 : MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
    1288             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
    1289             :   // If we have -ffunction-sections then we should emit the global value to a
    1290             :   // uniqued section specifically for it.
    1291             :   bool EmitUniquedSection;
    1292        3467 :   if (Kind.isText())
    1293             :     EmitUniquedSection = TM.getFunctionSections();
    1294             :   else
    1295             :     EmitUniquedSection = TM.getDataSections();
    1296             : 
    1297        3467 :   if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
    1298         303 :     SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
    1299             : 
    1300         303 :     unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
    1301             : 
    1302         303 :     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
    1303         303 :     int Selection = getSelectionForCOFF(GO);
    1304         301 :     if (!Selection)
    1305             :       Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
    1306             :     const GlobalValue *ComdatGV;
    1307         301 :     if (GO->hasComdat())
    1308         222 :       ComdatGV = getComdatGVForCOFF(GO);
    1309             :     else
    1310             :       ComdatGV = GO;
    1311             : 
    1312             :     unsigned UniqueID = MCContext::GenericSectionID;
    1313         301 :     if (EmitUniquedSection)
    1314          83 :       UniqueID = NextUniqueID++;
    1315             : 
    1316         301 :     if (!ComdatGV->hasPrivateLinkage()) {
    1317         299 :       MCSymbol *Sym = TM.getSymbol(ComdatGV);
    1318         299 :       StringRef COMDATSymName = Sym->getName();
    1319             : 
    1320             :       // Append "$symbol" to the section name when targetting mingw. The ld.bfd
    1321             :       // COFF linker will not properly handle comdats otherwise.
    1322             :       if (getTargetTriple().isWindowsGNUEnvironment())
    1323          20 :         raw_svector_ostream(Name) << '$' << COMDATSymName;
    1324             : 
    1325         598 :       return getContext().getCOFFSection(Name, Characteristics, Kind,
    1326             :                                          COMDATSymName, Selection, UniqueID);
    1327             :     } else {
    1328             :       SmallString<256> TmpData;
    1329           2 :       getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
    1330           4 :       return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
    1331             :                                          Selection, UniqueID);
    1332             :     }
    1333             :   }
    1334             : 
    1335        3164 :   if (Kind.isText())
    1336        2801 :     return TextSection;
    1337             : 
    1338         363 :   if (Kind.isThreadLocal())
    1339          37 :     return TLSDataSection;
    1340             : 
    1341         209 :   if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
    1342         120 :     return ReadOnlySection;
    1343             : 
    1344             :   // Note: we claim that common symbols are put in BSSSection, but they are
    1345             :   // really emitted with the magic .comm directive, which creates a symbol table
    1346             :   // entry but not a section.
    1347         206 :   if (Kind.isBSS() || Kind.isCommon())
    1348         120 :     return BSSSection;
    1349             : 
    1350          86 :   return DataSection;
    1351             : }
    1352             : 
    1353         351 : void TargetLoweringObjectFileCOFF::getNameWithPrefix(
    1354             :     SmallVectorImpl<char> &OutName, const GlobalValue *GV,
    1355             :     const TargetMachine &TM) const {
    1356             :   bool CannotUsePrivateLabel = false;
    1357         351 :   if (GV->hasPrivateLinkage() &&
    1358         351 :       ((isa<Function>(GV) && TM.getFunctionSections()) ||
    1359         335 :        (isa<GlobalVariable>(GV) && TM.getDataSections())))
    1360             :     CannotUsePrivateLabel = true;
    1361             : 
    1362         351 :   getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
    1363         351 : }
    1364             : 
    1365           9 : MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
    1366             :     const Function &F, const TargetMachine &TM) const {
    1367             :   // If the function can be removed, produce a unique section so that
    1368             :   // the table doesn't prevent the removal.
    1369           9 :   const Comdat *C = F.getComdat();
    1370           9 :   bool EmitUniqueSection = TM.getFunctionSections() || C;
    1371             :   if (!EmitUniqueSection)
    1372           5 :     return ReadOnlySection;
    1373             : 
    1374             :   // FIXME: we should produce a symbol for F instead.
    1375           4 :   if (F.hasPrivateLinkage())
    1376           0 :     return ReadOnlySection;
    1377             : 
    1378           4 :   MCSymbol *Sym = TM.getSymbol(&F);
    1379           4 :   StringRef COMDATSymName = Sym->getName();
    1380             : 
    1381           4 :   SectionKind Kind = SectionKind::getReadOnly();
    1382           4 :   StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
    1383           4 :   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
    1384           4 :   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
    1385           4 :   unsigned UniqueID = NextUniqueID++;
    1386             : 
    1387           4 :   return getContext().getCOFFSection(
    1388             :       SecName, Characteristics, Kind, COMDATSymName,
    1389           4 :       COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
    1390             : }
    1391             : 
    1392         760 : void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
    1393             :                                                       Module &M) const {
    1394         760 :   if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
    1395             :     // Emit the linker options to the linker .drectve section.  According to the
    1396             :     // spec, this section is a space-separated string containing flags for
    1397             :     // linker.
    1398          30 :     MCSection *Sec = getDrectveSection();
    1399          30 :     Streamer.SwitchSection(Sec);
    1400          85 :     for (const auto &Option : LinkerOptions->operands()) {
    1401         111 :       for (const auto &Piece : cast<MDNode>(Option)->operands()) {
    1402             :         // Lead with a space for consistency with our dllexport implementation.
    1403          56 :         std::string Directive(" ");
    1404          56 :         Directive.append(cast<MDString>(Piece)->getString());
    1405         112 :         Streamer.EmitBytes(Directive);
    1406             :       }
    1407             :     }
    1408             :   }
    1409             : 
    1410         760 :   unsigned Version = 0;
    1411         760 :   unsigned Flags = 0;
    1412         760 :   StringRef Section;
    1413             : 
    1414         760 :   GetObjCImageInfo(M, Version, Flags, Section);
    1415         760 :   if (Section.empty())
    1416         757 :     return;
    1417             : 
    1418           3 :   auto &C = getContext();
    1419           3 :   auto *S = C.getCOFFSection(
    1420             :       Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
    1421             :       SectionKind::getReadOnly());
    1422           3 :   Streamer.SwitchSection(S);
    1423           6 :   Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
    1424           3 :   Streamer.EmitIntValue(Version, 4);
    1425           3 :   Streamer.EmitIntValue(Flags, 4);
    1426           3 :   Streamer.AddBlankLine();
    1427             : }
    1428             : 
    1429         763 : void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
    1430             :                                               const TargetMachine &TM) {
    1431         763 :   TargetLoweringObjectFile::Initialize(Ctx, TM);
    1432             :   const Triple &T = TM.getTargetTriple();
    1433             :   if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
    1434         635 :     StaticCtorSection =
    1435         635 :         Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1436             :                                            COFF::IMAGE_SCN_MEM_READ,
    1437             :                            SectionKind::getReadOnly());
    1438         635 :     StaticDtorSection =
    1439         635 :         Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1440             :                                            COFF::IMAGE_SCN_MEM_READ,
    1441             :                            SectionKind::getReadOnly());
    1442             :   } else {
    1443         128 :     StaticCtorSection = Ctx.getCOFFSection(
    1444             :         ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1445             :                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
    1446             :         SectionKind::getData());
    1447         128 :     StaticDtorSection = Ctx.getCOFFSection(
    1448             :         ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1449             :                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
    1450             :         SectionKind::getData());
    1451             :   }
    1452         763 : }
    1453             : 
    1454          30 : static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
    1455             :                                                    const Triple &T, bool IsCtor,
    1456             :                                                    unsigned Priority,
    1457             :                                                    const MCSymbol *KeySym,
    1458             :                                                    MCSectionCOFF *Default) {
    1459             :   if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
    1460             :     // If the priority is the default, use .CRT$XCU, possibly associative.
    1461          19 :     if (Priority == 65535)
    1462          13 :       return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
    1463             : 
    1464             :     // Otherwise, we need to compute a new section name. Low priorities should
    1465             :     // run earlier. The linker will sort sections ASCII-betically, and we need a
    1466             :     // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
    1467             :     // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
    1468             :     // low priorities need to sort before 'L', since the CRT uses that
    1469             :     // internally, so we use ".CRT$XCA00001" for them.
    1470             :     SmallString<24> Name;
    1471             :     raw_svector_ostream OS(Name);
    1472          12 :     OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
    1473           6 :     MCSectionCOFF *Sec = Ctx.getCOFFSection(
    1474             :         Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
    1475             :         SectionKind::getReadOnly());
    1476           6 :     return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
    1477             :   }
    1478             : 
    1479          13 :   std::string Name = IsCtor ? ".ctors" : ".dtors";
    1480          11 :   if (Priority != 65535)
    1481           4 :     raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
    1482             : 
    1483          11 :   return Ctx.getAssociativeCOFFSection(
    1484             :       Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1485             :                                    COFF::IMAGE_SCN_MEM_READ |
    1486             :                                    COFF::IMAGE_SCN_MEM_WRITE,
    1487             :                          SectionKind::getData()),
    1488             :       KeySym, 0);
    1489             : }
    1490             : 
    1491          25 : MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
    1492             :     unsigned Priority, const MCSymbol *KeySym) const {
    1493          25 :   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
    1494             :                                       Priority, KeySym,
    1495          25 :                                       cast<MCSectionCOFF>(StaticCtorSection));
    1496             : }
    1497             : 
    1498           5 : MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
    1499             :     unsigned Priority, const MCSymbol *KeySym) const {
    1500           5 :   return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
    1501             :                                       Priority, KeySym,
    1502           5 :                                       cast<MCSectionCOFF>(StaticDtorSection));
    1503             : }
    1504             : 
    1505        5813 : void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
    1506             :     raw_ostream &OS, const GlobalValue *GV) const {
    1507       11626 :   emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
    1508        5813 : }
    1509             : 
    1510           8 : void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
    1511             :     raw_ostream &OS, const GlobalValue *GV) const {
    1512          16 :   emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
    1513           8 : }
    1514             : 
    1515          91 : const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
    1516             :     const GlobalValue *LHS, const GlobalValue *RHS,
    1517             :     const TargetMachine &TM) const {
    1518             :   const Triple &T = TM.getTargetTriple();
    1519             :   if (!T.isKnownWindowsMSVCEnvironment() &&
    1520             :       !T.isWindowsItaniumEnvironment() &&
    1521             :       !T.isWindowsCoreCLREnvironment())
    1522             :     return nullptr;
    1523             : 
    1524             :   // Our symbols should exist in address space zero, cowardly no-op if
    1525             :   // otherwise.
    1526         182 :   if (LHS->getType()->getPointerAddressSpace() != 0 ||
    1527          91 :       RHS->getType()->getPointerAddressSpace() != 0)
    1528             :     return nullptr;
    1529             : 
    1530             :   // Both ptrtoint instructions must wrap global objects:
    1531             :   // - Only global variables are eligible for image relative relocations.
    1532             :   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
    1533             :   // We expect __ImageBase to be a global variable without a section, externally
    1534             :   // defined.
    1535             :   //
    1536             :   // It should look something like this: @__ImageBase = external constant i8
    1537          91 :   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
    1538          91 :       LHS->isThreadLocal() || RHS->isThreadLocal() ||
    1539         263 :       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
    1540          86 :       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
    1541           5 :     return nullptr;
    1542             : 
    1543          86 :   return MCSymbolRefExpr::create(TM.getSymbol(LHS),
    1544             :                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
    1545          86 :                                  getContext());
    1546             : }
    1547             : 
    1548         486 : static std::string APIntToHexString(const APInt &AI) {
    1549         486 :   unsigned Width = (AI.getBitWidth() / 8) * 2;
    1550         486 :   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
    1551         486 :   unsigned Size = HexString.size();
    1552             :   assert(Width >= Size && "hex string is too large!");
    1553         486 :   HexString.insert(HexString.begin(), Width - Size, '0');
    1554             : 
    1555         486 :   return HexString;
    1556             : }
    1557             : 
    1558         541 : static std::string scalarConstantToHexString(const Constant *C) {
    1559         541 :   Type *Ty = C->getType();
    1560         541 :   if (isa<UndefValue>(C)) {
    1561          12 :     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
    1562             :   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
    1563         468 :     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
    1564             :   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
    1565         246 :     return APIntToHexString(CI->getValue());
    1566             :   } else {
    1567             :     unsigned NumElements;
    1568          55 :     if (isa<VectorType>(Ty))
    1569             :       NumElements = Ty->getVectorNumElements();
    1570             :     else
    1571           3 :       NumElements = Ty->getArrayNumElements();
    1572             :     std::string HexString;
    1573         351 :     for (int I = NumElements - 1, E = -1; I != E; --I)
    1574         592 :       HexString += scalarConstantToHexString(C->getAggregateElement(I));
    1575             :     return HexString;
    1576             :   }
    1577             : }
    1578             : 
    1579         266 : MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
    1580             :     const DataLayout &DL, SectionKind Kind, const Constant *C,
    1581             :     unsigned &Align) const {
    1582         266 :   if (Kind.isMergeableConst() && C &&
    1583         263 :       getContext().getAsmInfo()->hasCOFFComdatConstants()) {
    1584             :     // This creates comdat sections with the given symbol name, but unless
    1585             :     // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
    1586             :     // will be created with a null storage class, which makes GNU binutils
    1587             :     // error out.
    1588             :     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    1589             :                                      COFF::IMAGE_SCN_MEM_READ |
    1590             :                                      COFF::IMAGE_SCN_LNK_COMDAT;
    1591             :     std::string COMDATSymName;
    1592         245 :     if (Kind.isMergeableConst4()) {
    1593         105 :       if (Align <= 4) {
    1594         210 :         COMDATSymName = "__real@" + scalarConstantToHexString(C);
    1595         105 :         Align = 4;
    1596             :       }
    1597         140 :     } else if (Kind.isMergeableConst8()) {
    1598          88 :       if (Align <= 8) {
    1599         176 :         COMDATSymName = "__real@" + scalarConstantToHexString(C);
    1600          88 :         Align = 8;
    1601             :       }
    1602          52 :     } else if (Kind.isMergeableConst16()) {
    1603             :       // FIXME: These may not be appropriate for non-x86 architectures.
    1604          48 :       if (Align <= 16) {
    1605          96 :         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
    1606          48 :         Align = 16;
    1607             :       }
    1608             :     } else if (Kind.isMergeableConst32()) {
    1609           4 :       if (Align <= 32) {
    1610           8 :         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
    1611           4 :         Align = 32;
    1612             :       }
    1613             :     }
    1614             : 
    1615         245 :     if (!COMDATSymName.empty())
    1616         490 :       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
    1617             :                                          COMDATSymName,
    1618             :                                          COFF::IMAGE_COMDAT_SELECT_ANY);
    1619             :   }
    1620             : 
    1621          21 :   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
    1622             : }
    1623             : 
    1624             : 
    1625             : //===----------------------------------------------------------------------===//
    1626             : //                                  Wasm
    1627             : //===----------------------------------------------------------------------===//
    1628             : 
    1629        3166 : static const Comdat *getWasmComdat(const GlobalValue *GV) {
    1630        3166 :   const Comdat *C = GV->getComdat();
    1631        3166 :   if (!C)
    1632             :     return nullptr;
    1633             : 
    1634           9 :   if (C->getSelectionKind() != Comdat::Any)
    1635           0 :     report_fatal_error("WebAssembly COMDATs only support "
    1636           0 :                        "SelectionKind::Any, '" + C->getName() + "' cannot be "
    1637             :                        "lowered.");
    1638             : 
    1639             :   return C;
    1640             : }
    1641             : 
    1642           0 : static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
    1643             :   // If we're told we have function data, then use that.
    1644           3 :   if (K.isText())
    1645             :     return SectionKind::getText();
    1646             : 
    1647             :   // Otherwise, ignore whatever section type the generic impl detected and use
    1648             :   // a plain data section.
    1649             :   return SectionKind::getData();
    1650             : }
    1651             : 
    1652           6 : MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
    1653             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
    1654             :   // We don't support explict section names for functions in the wasm object
    1655             :   // format.  Each function has to be in its own unique section.
    1656           6 :   if (isa<Function>(GO)) {
    1657           3 :     return SelectSectionForGlobal(GO, Kind, TM);
    1658             :   }
    1659             : 
    1660           3 :   StringRef Name = GO->getSection();
    1661             : 
    1662             :   Kind = getWasmKindForNamedSection(Name, Kind);
    1663             : 
    1664             :   StringRef Group = "";
    1665           3 :   if (const Comdat *C = getWasmComdat(GO)) {
    1666           0 :     Group = C->getName();
    1667             :   }
    1668             : 
    1669           3 :   return getContext().getWasmSection(Name, Kind, Group,
    1670             :                                      MCContext::GenericSectionID);
    1671             : }
    1672             : 
    1673        3163 : static MCSectionWasm *selectWasmSectionForGlobal(
    1674             :     MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
    1675             :     const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
    1676             :   StringRef Group = "";
    1677        3163 :   if (const Comdat *C = getWasmComdat(GO)) {
    1678           9 :     Group = C->getName();
    1679             :   }
    1680             : 
    1681             :   bool UniqueSectionNames = TM.getUniqueSectionNames();
    1682        3163 :   SmallString<128> Name = getSectionPrefixForGlobal(Kind);
    1683             : 
    1684             :   if (const auto *F = dyn_cast<Function>(GO)) {
    1685        2982 :     const auto &OptionalPrefix = F->getSectionPrefix();
    1686        2982 :     if (OptionalPrefix)
    1687             :       Name += *OptionalPrefix;
    1688             :   }
    1689             : 
    1690        3163 :   if (EmitUniqueSection && UniqueSectionNames) {
    1691        3163 :     Name.push_back('.');
    1692        3163 :     TM.getNameWithPrefix(Name, GO, Mang, true);
    1693             :   }
    1694             :   unsigned UniqueID = MCContext::GenericSectionID;
    1695        3163 :   if (EmitUniqueSection && !UniqueSectionNames) {
    1696           0 :     UniqueID = *NextUniqueID;
    1697           0 :     (*NextUniqueID)++;
    1698             :   }
    1699        3163 :   return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
    1700             : }
    1701             : 
    1702        3163 : MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
    1703             :     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
    1704             : 
    1705        3163 :   if (Kind.isCommon())
    1706           0 :     report_fatal_error("mergable sections not supported yet on wasm");
    1707             : 
    1708             :   // If we have -ffunction-section or -fdata-section then we should emit the
    1709             :   // global value to a uniqued section specifically for it.
    1710             :   bool EmitUniqueSection = false;
    1711        3163 :   if (Kind.isText())
    1712             :     EmitUniqueSection = TM.getFunctionSections();
    1713             :   else
    1714             :     EmitUniqueSection = TM.getDataSections();
    1715        3163 :   EmitUniqueSection |= GO->hasComdat();
    1716             : 
    1717        3163 :   return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
    1718        3163 :                                     EmitUniqueSection, &NextUniqueID);
    1719             : }
    1720             : 
    1721           0 : bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
    1722             :     bool UsesLabelDifference, const Function &F) const {
    1723             :   // We can always create relative relocations, so use another section
    1724             :   // that can be marked non-executable.
    1725           0 :   return false;
    1726             : }
    1727             : 
    1728           0 : const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
    1729             :     const GlobalValue *LHS, const GlobalValue *RHS,
    1730             :     const TargetMachine &TM) const {
    1731             :   // We may only use a PLT-relative relocation to refer to unnamed_addr
    1732             :   // functions.
    1733           0 :   if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
    1734             :     return nullptr;
    1735             : 
    1736             :   // Basic sanity checks.
    1737           0 :   if (LHS->getType()->getPointerAddressSpace() != 0 ||
    1738           0 :       RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
    1739             :       RHS->isThreadLocal())
    1740             :     return nullptr;
    1741             : 
    1742           0 :   return MCBinaryExpr::createSub(
    1743           0 :       MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
    1744           0 :                               getContext()),
    1745           0 :       MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
    1746             : }
    1747             : 
    1748         305 : void TargetLoweringObjectFileWasm::InitializeWasm() {
    1749         305 :   StaticCtorSection =
    1750         305 :       getContext().getWasmSection(".init_array", SectionKind::getData());
    1751         305 : }
    1752             : 
    1753          21 : MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
    1754             :     unsigned Priority, const MCSymbol *KeySym) const {
    1755          21 :   return Priority == UINT16_MAX ?
    1756             :          StaticCtorSection :
    1757          57 :          getContext().getWasmSection(".init_array." + utostr(Priority),
    1758          21 :                                      SectionKind::getData());
    1759             : }
    1760             : 
    1761           0 : MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
    1762             :     unsigned Priority, const MCSymbol *KeySym) const {
    1763           0 :   llvm_unreachable("@llvm.global_dtors should have been lowered already");
    1764             :   return nullptr;
    1765             : }

Generated by: LCOV version 1.13