1*9880d681SAndroid Build Coastguard Worker //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "MipsTargetObjectFile.h"
11*9880d681SAndroid Build Coastguard Worker #include "MipsSubtarget.h"
12*9880d681SAndroid Build Coastguard Worker #include "MipsTargetMachine.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSectionELF.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ELF.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
24*9880d681SAndroid Build Coastguard Worker SSThreshold("mips-ssection-threshold", cl::Hidden,
25*9880d681SAndroid Build Coastguard Worker cl::desc("Small data and bss section threshold size (default=8)"),
26*9880d681SAndroid Build Coastguard Worker cl::init(8));
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
29*9880d681SAndroid Build Coastguard Worker LocalSData("mlocal-sdata", cl::Hidden,
30*9880d681SAndroid Build Coastguard Worker cl::desc("MIPS: Use gp_rel for object-local data."),
31*9880d681SAndroid Build Coastguard Worker cl::init(true));
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
34*9880d681SAndroid Build Coastguard Worker ExternSData("mextern-sdata", cl::Hidden,
35*9880d681SAndroid Build Coastguard Worker cl::desc("MIPS: Use gp_rel for data that is not defined by the "
36*9880d681SAndroid Build Coastguard Worker "current object."),
37*9880d681SAndroid Build Coastguard Worker cl::init(true));
38*9880d681SAndroid Build Coastguard Worker
Initialize(MCContext & Ctx,const TargetMachine & TM)39*9880d681SAndroid Build Coastguard Worker void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
40*9880d681SAndroid Build Coastguard Worker TargetLoweringObjectFileELF::Initialize(Ctx, TM);
41*9880d681SAndroid Build Coastguard Worker InitializeELF(TM.Options.UseInitArray);
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker SmallDataSection = getContext().getELFSection(
44*9880d681SAndroid Build Coastguard Worker ".sdata", ELF::SHT_PROGBITS,
45*9880d681SAndroid Build Coastguard Worker ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
48*9880d681SAndroid Build Coastguard Worker ELF::SHF_WRITE | ELF::SHF_ALLOC |
49*9880d681SAndroid Build Coastguard Worker ELF::SHF_MIPS_GPREL);
50*9880d681SAndroid Build Coastguard Worker this->TM = &static_cast<const MipsTargetMachine &>(TM);
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker // A address must be loaded from a small section if its size is less than the
54*9880d681SAndroid Build Coastguard Worker // small section size threshold. Data in this section must be addressed using
55*9880d681SAndroid Build Coastguard Worker // gp_rel operator.
IsInSmallSection(uint64_t Size)56*9880d681SAndroid Build Coastguard Worker static bool IsInSmallSection(uint64_t Size) {
57*9880d681SAndroid Build Coastguard Worker // gcc has traditionally not treated zero-sized objects as small data, so this
58*9880d681SAndroid Build Coastguard Worker // is effectively part of the ABI.
59*9880d681SAndroid Build Coastguard Worker return Size > 0 && Size <= SSThreshold;
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker /// Return true if this global address should be placed into small data/bss
63*9880d681SAndroid Build Coastguard Worker /// section.
64*9880d681SAndroid Build Coastguard Worker bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM) const65*9880d681SAndroid Build Coastguard Worker IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
66*9880d681SAndroid Build Coastguard Worker // We first check the case where global is a declaration, because finding
67*9880d681SAndroid Build Coastguard Worker // section kind using getKindForGlobal() is only allowed for global
68*9880d681SAndroid Build Coastguard Worker // definitions.
69*9880d681SAndroid Build Coastguard Worker if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
70*9880d681SAndroid Build Coastguard Worker return IsGlobalInSmallSectionImpl(GV, TM);
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker /// Return true if this global address should be placed into small data/bss
76*9880d681SAndroid Build Coastguard Worker /// section.
77*9880d681SAndroid Build Coastguard Worker bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM,SectionKind Kind) const78*9880d681SAndroid Build Coastguard Worker IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
79*9880d681SAndroid Build Coastguard Worker SectionKind Kind) const {
80*9880d681SAndroid Build Coastguard Worker return (IsGlobalInSmallSectionImpl(GV, TM) &&
81*9880d681SAndroid Build Coastguard Worker (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker /// Return true if this global address should be placed into small data/bss
85*9880d681SAndroid Build Coastguard Worker /// section. This method does all the work, except for checking the section
86*9880d681SAndroid Build Coastguard Worker /// kind.
87*9880d681SAndroid Build Coastguard Worker bool MipsTargetObjectFile::
IsGlobalInSmallSectionImpl(const GlobalValue * GV,const TargetMachine & TM) const88*9880d681SAndroid Build Coastguard Worker IsGlobalInSmallSectionImpl(const GlobalValue *GV,
89*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM) const {
90*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &Subtarget =
91*9880d681SAndroid Build Coastguard Worker *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker // Return if small section is not available.
94*9880d681SAndroid Build Coastguard Worker if (!Subtarget.useSmallSection())
95*9880d681SAndroid Build Coastguard Worker return false;
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker // Only global variables, not functions.
98*9880d681SAndroid Build Coastguard Worker const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
99*9880d681SAndroid Build Coastguard Worker if (!GVA)
100*9880d681SAndroid Build Coastguard Worker return false;
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker // Enforce -mlocal-sdata.
103*9880d681SAndroid Build Coastguard Worker if (!LocalSData && GV->hasLocalLinkage())
104*9880d681SAndroid Build Coastguard Worker return false;
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker // Enforce -mextern-sdata.
107*9880d681SAndroid Build Coastguard Worker if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
108*9880d681SAndroid Build Coastguard Worker GV->hasCommonLinkage()))
109*9880d681SAndroid Build Coastguard Worker return false;
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker Type *Ty = GV->getValueType();
112*9880d681SAndroid Build Coastguard Worker return IsInSmallSection(
113*9880d681SAndroid Build Coastguard Worker GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
114*9880d681SAndroid Build Coastguard Worker }
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker MCSection *
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const117*9880d681SAndroid Build Coastguard Worker MipsTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
118*9880d681SAndroid Build Coastguard Worker SectionKind Kind, Mangler &Mang,
119*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM) const {
120*9880d681SAndroid Build Coastguard Worker // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
121*9880d681SAndroid Build Coastguard Worker // sections?
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker // Handle Small Section classification here.
124*9880d681SAndroid Build Coastguard Worker if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
125*9880d681SAndroid Build Coastguard Worker return SmallBSSSection;
126*9880d681SAndroid Build Coastguard Worker if (Kind.isData() && IsGlobalInSmallSection(GV, TM, Kind))
127*9880d681SAndroid Build Coastguard Worker return SmallDataSection;
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker // Otherwise, we work the same as ELF.
130*9880d681SAndroid Build Coastguard Worker return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
131*9880d681SAndroid Build Coastguard Worker }
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker /// Return true if this constant should be placed into small data section.
IsConstantInSmallSection(const DataLayout & DL,const Constant * CN,const TargetMachine & TM) const134*9880d681SAndroid Build Coastguard Worker bool MipsTargetObjectFile::IsConstantInSmallSection(
135*9880d681SAndroid Build Coastguard Worker const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
136*9880d681SAndroid Build Coastguard Worker return (static_cast<const MipsTargetMachine &>(TM)
137*9880d681SAndroid Build Coastguard Worker .getSubtargetImpl()
138*9880d681SAndroid Build Coastguard Worker ->useSmallSection() &&
139*9880d681SAndroid Build Coastguard Worker LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
140*9880d681SAndroid Build Coastguard Worker }
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker /// Return true if this constant should be placed into small data section.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,unsigned & Align) const143*9880d681SAndroid Build Coastguard Worker MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
144*9880d681SAndroid Build Coastguard Worker SectionKind Kind,
145*9880d681SAndroid Build Coastguard Worker const Constant *C,
146*9880d681SAndroid Build Coastguard Worker unsigned &Align) const {
147*9880d681SAndroid Build Coastguard Worker if (IsConstantInSmallSection(DL, C, *TM))
148*9880d681SAndroid Build Coastguard Worker return SmallDataSection;
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker // Otherwise, we work the same as ELF.
151*9880d681SAndroid Build Coastguard Worker return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
152*9880d681SAndroid Build Coastguard Worker }
153