1*0b57cec5SDimitry Andric //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "MipsTargetObjectFile.h" 10*0b57cec5SDimitry Andric #include "MipsSubtarget.h" 11*0b57cec5SDimitry Andric #include "MipsTargetMachine.h" 12*0b57cec5SDimitry Andric #include "MCTargetDesc/MipsMCExpr.h" 13*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h" 14*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 15*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 16*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 17*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 18*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h" 19*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 20*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 21*0b57cec5SDimitry Andric using namespace llvm; 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric static cl::opt<unsigned> 24*0b57cec5SDimitry Andric SSThreshold("mips-ssection-threshold", cl::Hidden, 25*0b57cec5SDimitry Andric cl::desc("Small data and bss section threshold size (default=8)"), 26*0b57cec5SDimitry Andric cl::init(8)); 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric static cl::opt<bool> 29*0b57cec5SDimitry Andric LocalSData("mlocal-sdata", cl::Hidden, 30*0b57cec5SDimitry Andric cl::desc("MIPS: Use gp_rel for object-local data."), 31*0b57cec5SDimitry Andric cl::init(true)); 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric static cl::opt<bool> 34*0b57cec5SDimitry Andric ExternSData("mextern-sdata", cl::Hidden, 35*0b57cec5SDimitry Andric cl::desc("MIPS: Use gp_rel for data that is not defined by the " 36*0b57cec5SDimitry Andric "current object."), 37*0b57cec5SDimitry Andric cl::init(true)); 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric static cl::opt<bool> 40*0b57cec5SDimitry Andric EmbeddedData("membedded-data", cl::Hidden, 41*0b57cec5SDimitry Andric cl::desc("MIPS: Try to allocate variables in the following" 42*0b57cec5SDimitry Andric " sections if possible: .rodata, .sdata, .data ."), 43*0b57cec5SDimitry Andric cl::init(false)); 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){ 46*0b57cec5SDimitry Andric TargetLoweringObjectFileELF::Initialize(Ctx, TM); 47*0b57cec5SDimitry Andric InitializeELF(TM.Options.UseInitArray); 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric SmallDataSection = getContext().getELFSection( 50*0b57cec5SDimitry Andric ".sdata", ELF::SHT_PROGBITS, 51*0b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL); 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, 54*0b57cec5SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC | 55*0b57cec5SDimitry Andric ELF::SHF_MIPS_GPREL); 56*0b57cec5SDimitry Andric this->TM = &static_cast<const MipsTargetMachine &>(TM); 57*0b57cec5SDimitry Andric } 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric // A address must be loaded from a small section if its size is less than the 60*0b57cec5SDimitry Andric // small section size threshold. Data in this section must be addressed using 61*0b57cec5SDimitry Andric // gp_rel operator. 62*0b57cec5SDimitry Andric static bool IsInSmallSection(uint64_t Size) { 63*0b57cec5SDimitry Andric // gcc has traditionally not treated zero-sized objects as small data, so this 64*0b57cec5SDimitry Andric // is effectively part of the ABI. 65*0b57cec5SDimitry Andric return Size > 0 && Size <= SSThreshold; 66*0b57cec5SDimitry Andric } 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss 69*0b57cec5SDimitry Andric /// section. 70*0b57cec5SDimitry Andric bool MipsTargetObjectFile::IsGlobalInSmallSection( 71*0b57cec5SDimitry Andric const GlobalObject *GO, const TargetMachine &TM) const { 72*0b57cec5SDimitry Andric // We first check the case where global is a declaration, because finding 73*0b57cec5SDimitry Andric // section kind using getKindForGlobal() is only allowed for global 74*0b57cec5SDimitry Andric // definitions. 75*0b57cec5SDimitry Andric if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage()) 76*0b57cec5SDimitry Andric return IsGlobalInSmallSectionImpl(GO, TM); 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM)); 79*0b57cec5SDimitry Andric } 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss 82*0b57cec5SDimitry Andric /// section. 83*0b57cec5SDimitry Andric bool MipsTargetObjectFile:: 84*0b57cec5SDimitry Andric IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM, 85*0b57cec5SDimitry Andric SectionKind Kind) const { 86*0b57cec5SDimitry Andric return IsGlobalInSmallSectionImpl(GO, TM) && 87*0b57cec5SDimitry Andric (Kind.isData() || Kind.isBSS() || Kind.isCommon() || 88*0b57cec5SDimitry Andric Kind.isReadOnly()); 89*0b57cec5SDimitry Andric } 90*0b57cec5SDimitry Andric 91*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss 92*0b57cec5SDimitry Andric /// section. This method does all the work, except for checking the section 93*0b57cec5SDimitry Andric /// kind. 94*0b57cec5SDimitry Andric bool MipsTargetObjectFile:: 95*0b57cec5SDimitry Andric IsGlobalInSmallSectionImpl(const GlobalObject *GO, 96*0b57cec5SDimitry Andric const TargetMachine &TM) const { 97*0b57cec5SDimitry Andric const MipsSubtarget &Subtarget = 98*0b57cec5SDimitry Andric *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl(); 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric // Return if small section is not available. 101*0b57cec5SDimitry Andric if (!Subtarget.useSmallSection()) 102*0b57cec5SDimitry Andric return false; 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric // Only global variables, not functions. 105*0b57cec5SDimitry Andric const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO); 106*0b57cec5SDimitry Andric if (!GVA) 107*0b57cec5SDimitry Andric return false; 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric // If the variable has an explicit section, it is placed in that section but 110*0b57cec5SDimitry Andric // it's addressing mode may change. 111*0b57cec5SDimitry Andric if (GVA->hasSection()) { 112*0b57cec5SDimitry Andric StringRef Section = GVA->getSection(); 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric // Explicitly placing any variable in the small data section overrides 115*0b57cec5SDimitry Andric // the global -G value. 116*0b57cec5SDimitry Andric if (Section == ".sdata" || Section == ".sbss") 117*0b57cec5SDimitry Andric return true; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric // Otherwise reject accessing it through the gp pointer. There are some 120*0b57cec5SDimitry Andric // historic cases which GCC doesn't appear to respect any more. These 121*0b57cec5SDimitry Andric // are .lit4, .lit8 and .srdata. For the moment reject these as well. 122*0b57cec5SDimitry Andric return false; 123*0b57cec5SDimitry Andric } 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric // Enforce -mlocal-sdata. 126*0b57cec5SDimitry Andric if (!LocalSData && GVA->hasLocalLinkage()) 127*0b57cec5SDimitry Andric return false; 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric // Enforce -mextern-sdata. 130*0b57cec5SDimitry Andric if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) || 131*0b57cec5SDimitry Andric GVA->hasCommonLinkage())) 132*0b57cec5SDimitry Andric return false; 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric // Enforce -membedded-data. 135*0b57cec5SDimitry Andric if (EmbeddedData && GVA->isConstant()) 136*0b57cec5SDimitry Andric return false; 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric Type *Ty = GVA->getValueType(); 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric // It is possible that the type of the global is unsized, i.e. a declaration 141*0b57cec5SDimitry Andric // of a extern struct. In this case don't presume it is in the small data 142*0b57cec5SDimitry Andric // section. This happens e.g. when building the FreeBSD kernel. 143*0b57cec5SDimitry Andric if (!Ty->isSized()) 144*0b57cec5SDimitry Andric return false; 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric return IsInSmallSection( 147*0b57cec5SDimitry Andric GVA->getParent()->getDataLayout().getTypeAllocSize(Ty)); 148*0b57cec5SDimitry Andric } 149*0b57cec5SDimitry Andric 150*0b57cec5SDimitry Andric MCSection *MipsTargetObjectFile::SelectSectionForGlobal( 151*0b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 152*0b57cec5SDimitry Andric // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" 153*0b57cec5SDimitry Andric // sections? 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric // Handle Small Section classification here. 156*0b57cec5SDimitry Andric if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind)) 157*0b57cec5SDimitry Andric return SmallBSSSection; 158*0b57cec5SDimitry Andric if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind)) 159*0b57cec5SDimitry Andric return SmallDataSection; 160*0b57cec5SDimitry Andric if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind)) 161*0b57cec5SDimitry Andric return SmallDataSection; 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric // Otherwise, we work the same as ELF. 164*0b57cec5SDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric /// Return true if this constant should be placed into small data section. 168*0b57cec5SDimitry Andric bool MipsTargetObjectFile::IsConstantInSmallSection( 169*0b57cec5SDimitry Andric const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const { 170*0b57cec5SDimitry Andric return (static_cast<const MipsTargetMachine &>(TM) 171*0b57cec5SDimitry Andric .getSubtargetImpl() 172*0b57cec5SDimitry Andric ->useSmallSection() && 173*0b57cec5SDimitry Andric LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType()))); 174*0b57cec5SDimitry Andric } 175*0b57cec5SDimitry Andric 176*0b57cec5SDimitry Andric /// Return true if this constant should be placed into small data section. 177*0b57cec5SDimitry Andric MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL, 178*0b57cec5SDimitry Andric SectionKind Kind, 179*0b57cec5SDimitry Andric const Constant *C, 180*0b57cec5SDimitry Andric unsigned &Align) const { 181*0b57cec5SDimitry Andric if (IsConstantInSmallSection(DL, C, *TM)) 182*0b57cec5SDimitry Andric return SmallDataSection; 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric // Otherwise, we work the same as ELF. 185*0b57cec5SDimitry Andric return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align); 186*0b57cec5SDimitry Andric } 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric const MCExpr * 189*0b57cec5SDimitry Andric MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { 190*0b57cec5SDimitry Andric const MCExpr *Expr = 191*0b57cec5SDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 192*0b57cec5SDimitry Andric Expr = MCBinaryExpr::createAdd( 193*0b57cec5SDimitry Andric Expr, MCConstantExpr::create(0x8000, getContext()), getContext()); 194*0b57cec5SDimitry Andric return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext()); 195*0b57cec5SDimitry Andric } 196