1 //===-- MipsTargetObjectFile.cpp - Mips object files ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MipsTargetObjectFile.h"
11 #include "MipsSubtarget.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/MC/MCSectionELF.h"
15 #include "llvm/Target/TargetData.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Support/CommandLine.h"
20 static cl::opt<unsigned>
21 SSThreshold("mips-ssection-threshold", cl::Hidden,
22 cl::desc("Small data and bss section threshold size (default=8)"),
25 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
26 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
29 getELFSection(".sdata", MCSectionELF::SHT_PROGBITS,
30 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
31 SectionKind::getDataRel());
34 getELFSection(".sbss", MCSectionELF::SHT_NOBITS,
35 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
36 SectionKind::getBSS());
40 // A address must be loaded from a small section if its size is less than the
41 // small section size threshold. Data in this section must be addressed using
43 static bool IsInSmallSection(uint64_t Size) {
44 return Size > 0 && Size <= SSThreshold;
47 bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
48 const TargetMachine &TM) const {
49 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
52 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
55 /// IsGlobalInSmallSection - Return true if this global address should be
56 /// placed into small data/bss section.
57 bool MipsTargetObjectFile::
58 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
59 SectionKind Kind) const {
61 // Only use small section for non linux targets.
62 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
63 if (Subtarget.isLinux())
66 // Only global variables, not functions.
67 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
71 // We can only do this for datarel or BSS objects for now.
72 if (!Kind.isBSS() && !Kind.isDataRel())
75 // If this is a internal constant string, there is a special
76 // section for it, but not in small data/bss.
77 if (Kind.isMergeable1ByteCString())
80 const Type *Ty = GV->getType()->getElementType();
81 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
86 const MCSection *MipsTargetObjectFile::
87 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
88 Mangler *Mang, const TargetMachine &TM) const {
89 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
92 // Handle Small Section classification here.
93 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
94 return SmallBSSSection;
95 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
96 return SmallDataSection;
98 // Otherwise, we work the same as ELF.
99 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);