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 "llvm/DerivedTypes.h" 13 #include "llvm/GlobalVariable.h" 14 #include "llvm/MC/MCContext.h" 15 #include "llvm/MC/MCSectionELF.h" 16 #include "llvm/Target/TargetData.h" 17 #include "llvm/Target/TargetMachine.h" 18 #include "llvm/Support/CommandLine.h" 19 using namespace llvm; 20 21 static cl::opt<unsigned> 22 SSThreshold("mips-ssection-threshold", cl::Hidden, 23 cl::desc("Small data and bss section threshold size (default=8)"), 24 cl::init(8)); 25 26 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){ 27 TargetLoweringObjectFileELF::Initialize(Ctx, TM); 28 29 SmallDataSection = 30 getContext().getELFSection(".sdata", MCSectionELF::SHT_PROGBITS, 31 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, 32 SectionKind::getDataRel()); 33 34 SmallBSSSection = 35 getContext().getELFSection(".sbss", MCSectionELF::SHT_NOBITS, 36 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, 37 SectionKind::getBSS()); 38 39 } 40 41 // A address must be loaded from a small section if its size is less than the 42 // small section size threshold. Data in this section must be addressed using 43 // gp_rel operator. 44 static bool IsInSmallSection(uint64_t Size) { 45 return Size > 0 && Size <= SSThreshold; 46 } 47 48 bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV, 49 const TargetMachine &TM) const { 50 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) 51 return false; 52 53 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM)); 54 } 55 56 /// IsGlobalInSmallSection - Return true if this global address should be 57 /// placed into small data/bss section. 58 bool MipsTargetObjectFile:: 59 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, 60 SectionKind Kind) const { 61 62 // Only use small section for non linux targets. 63 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); 64 if (Subtarget.isLinux()) 65 return false; 66 67 // Only global variables, not functions. 68 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); 69 if (!GVA) 70 return false; 71 72 // We can only do this for datarel or BSS objects for now. 73 if (!Kind.isBSS() && !Kind.isDataRel()) 74 return false; 75 76 // If this is a internal constant string, there is a special 77 // section for it, but not in small data/bss. 78 if (Kind.isMergeable1ByteCString()) 79 return false; 80 81 const Type *Ty = GV->getType()->getElementType(); 82 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty)); 83 } 84 85 86 87 const MCSection *MipsTargetObjectFile:: 88 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 89 Mangler *Mang, const TargetMachine &TM) const { 90 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" 91 // sections? 92 93 // Handle Small Section classification here. 94 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind)) 95 return SmallBSSSection; 96 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind)) 97 return SmallDataSection; 98 99 // Otherwise, we work the same as ELF. 100 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM); 101 } 102