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