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