LLVM API Documentation
00001 //===-- MipsTargetObjectFile.cpp - Mips object files ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "MipsTargetObjectFile.h" 00011 #include "llvm/DerivedTypes.h" 00012 #include "llvm/GlobalVariable.h" 00013 #include "llvm/MC/MCSectionELF.h" 00014 #include "llvm/Target/TargetData.h" 00015 #include "llvm/Target/TargetMachine.h" 00016 #include "llvm/Support/CommandLine.h" 00017 using namespace llvm; 00018 00019 static cl::opt<unsigned> 00020 SSThreshold("mips-ssection-threshold", cl::Hidden, 00021 cl::desc("Small data and bss section threshold size (default=8)"), 00022 cl::init(8)); 00023 00024 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){ 00025 TargetLoweringObjectFileELF::Initialize(Ctx, TM); 00026 00027 SmallDataSection = 00028 getELFSection(".sdata", MCSectionELF::SHT_PROGBITS, 00029 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, 00030 SectionKind::getDataRel()); 00031 00032 SmallBSSSection = 00033 getELFSection(".sbss", MCSectionELF::SHT_NOBITS, 00034 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, 00035 SectionKind::getBSS()); 00036 00037 } 00038 00039 // A address must be loaded from a small section if its size is less than the 00040 // small section size threshold. Data in this section must be addressed using 00041 // gp_rel operator. 00042 static bool IsInSmallSection(uint64_t Size) { 00043 return Size > 0 && Size <= SSThreshold; 00044 } 00045 00046 bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV, 00047 const TargetMachine &TM) const { 00048 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) 00049 return false; 00050 00051 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM)); 00052 } 00053 00054 /// IsGlobalInSmallSection - Return true if this global address should be 00055 /// placed into small data/bss section. 00056 bool MipsTargetObjectFile:: 00057 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, 00058 SectionKind Kind) const { 00059 // Only global variables, not functions. 00060 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); 00061 if (!GVA) 00062 return false; 00063 00064 // We can only do this for datarel or BSS objects for now. 00065 if (!Kind.isBSS() && !Kind.isDataRel()) 00066 return false; 00067 00068 // If this is a internal constant string, there is a special 00069 // section for it, but not in small data/bss. 00070 if (Kind.isMergeable1ByteCString()) 00071 return false; 00072 00073 const Type *Ty = GV->getType()->getElementType(); 00074 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty)); 00075 } 00076 00077 00078 00079 const MCSection *MipsTargetObjectFile:: 00080 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 00081 Mangler *Mang, const TargetMachine &TM) const { 00082 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" 00083 // sections? 00084 00085 // Handle Small Section classification here. 00086 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind)) 00087 return SmallBSSSection; 00088 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind)) 00089 return SmallDataSection; 00090 00091 // Otherwise, we work the same as ELF. 00092 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM); 00093 }