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