1dff0c46cSDimitry Andric //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky
10f22ef01cSRoman Divacky #include "MipsTargetObjectFile.h"
11f22ef01cSRoman Divacky #include "MipsSubtarget.h"
12ff0cc061SDimitry Andric #include "MipsTargetMachine.h"
13*b5893f02SDimitry Andric #include "MCTargetDesc/MipsMCExpr.h"
14db17bf38SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
15139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
16139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
17139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
18f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h"
19f22ef01cSRoman Divacky #include "llvm/MC/MCSectionELF.h"
20f22ef01cSRoman Divacky #include "llvm/Support/CommandLine.h"
21139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h"
22f22ef01cSRoman Divacky using namespace llvm;
23f22ef01cSRoman Divacky
24f22ef01cSRoman Divacky static cl::opt<unsigned>
25f22ef01cSRoman Divacky SSThreshold("mips-ssection-threshold", cl::Hidden,
26f22ef01cSRoman Divacky cl::desc("Small data and bss section threshold size (default=8)"),
27f22ef01cSRoman Divacky cl::init(8));
28f22ef01cSRoman Divacky
2939d628a0SDimitry Andric static cl::opt<bool>
3039d628a0SDimitry Andric LocalSData("mlocal-sdata", cl::Hidden,
3139d628a0SDimitry Andric cl::desc("MIPS: Use gp_rel for object-local data."),
3239d628a0SDimitry Andric cl::init(true));
3339d628a0SDimitry Andric
3439d628a0SDimitry Andric static cl::opt<bool>
3539d628a0SDimitry Andric ExternSData("mextern-sdata", cl::Hidden,
3639d628a0SDimitry Andric cl::desc("MIPS: Use gp_rel for data that is not defined by the "
3739d628a0SDimitry Andric "current object."),
3839d628a0SDimitry Andric cl::init(true));
3939d628a0SDimitry Andric
402cab237bSDimitry Andric static cl::opt<bool>
412cab237bSDimitry Andric EmbeddedData("membedded-data", cl::Hidden,
422cab237bSDimitry Andric cl::desc("MIPS: Try to allocate variables in the following"
432cab237bSDimitry Andric " sections if possible: .rodata, .sdata, .data ."),
442cab237bSDimitry Andric cl::init(false));
452cab237bSDimitry Andric
Initialize(MCContext & Ctx,const TargetMachine & TM)46f22ef01cSRoman Divacky void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
47f22ef01cSRoman Divacky TargetLoweringObjectFileELF::Initialize(Ctx, TM);
483861d79fSDimitry Andric InitializeELF(TM.Options.UseInitArray);
49f22ef01cSRoman Divacky
50ff0cc061SDimitry Andric SmallDataSection = getContext().getELFSection(
513ca95b02SDimitry Andric ".sdata", ELF::SHT_PROGBITS,
523ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
53f22ef01cSRoman Divacky
54ff0cc061SDimitry Andric SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
553ca95b02SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC |
563ca95b02SDimitry Andric ELF::SHF_MIPS_GPREL);
57ff0cc061SDimitry Andric this->TM = &static_cast<const MipsTargetMachine &>(TM);
58f22ef01cSRoman Divacky }
59f22ef01cSRoman Divacky
60f22ef01cSRoman Divacky // A address must be loaded from a small section if its size is less than the
61f22ef01cSRoman Divacky // small section size threshold. Data in this section must be addressed using
62f22ef01cSRoman Divacky // gp_rel operator.
IsInSmallSection(uint64_t Size)63f22ef01cSRoman Divacky static bool IsInSmallSection(uint64_t Size) {
6439d628a0SDimitry Andric // gcc has traditionally not treated zero-sized objects as small data, so this
6539d628a0SDimitry Andric // is effectively part of the ABI.
66f22ef01cSRoman Divacky return Size > 0 && Size <= SSThreshold;
67f22ef01cSRoman Divacky }
68f22ef01cSRoman Divacky
6939d628a0SDimitry Andric /// Return true if this global address should be placed into small data/bss
7039d628a0SDimitry Andric /// section.
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const71d88c1a5aSDimitry Andric bool MipsTargetObjectFile::IsGlobalInSmallSection(
72d88c1a5aSDimitry Andric const GlobalObject *GO, const TargetMachine &TM) const {
7339d628a0SDimitry Andric // We first check the case where global is a declaration, because finding
7439d628a0SDimitry Andric // section kind using getKindForGlobal() is only allowed for global
7539d628a0SDimitry Andric // definitions.
76d88c1a5aSDimitry Andric if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
77d88c1a5aSDimitry Andric return IsGlobalInSmallSectionImpl(GO, TM);
78f22ef01cSRoman Divacky
79d88c1a5aSDimitry Andric return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
80f22ef01cSRoman Divacky }
81f22ef01cSRoman Divacky
8239d628a0SDimitry Andric /// Return true if this global address should be placed into small data/bss
8339d628a0SDimitry Andric /// section.
84f22ef01cSRoman Divacky bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM,SectionKind Kind) const85d88c1a5aSDimitry Andric IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
86f22ef01cSRoman Divacky SectionKind Kind) const {
872cab237bSDimitry Andric return IsGlobalInSmallSectionImpl(GO, TM) &&
882cab237bSDimitry Andric (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
892cab237bSDimitry Andric Kind.isReadOnly());
9039d628a0SDimitry Andric }
91f22ef01cSRoman Divacky
9239d628a0SDimitry Andric /// Return true if this global address should be placed into small data/bss
9339d628a0SDimitry Andric /// section. This method does all the work, except for checking the section
9439d628a0SDimitry Andric /// kind.
9539d628a0SDimitry Andric bool MipsTargetObjectFile::
IsGlobalInSmallSectionImpl(const GlobalObject * GO,const TargetMachine & TM) const96d88c1a5aSDimitry Andric IsGlobalInSmallSectionImpl(const GlobalObject *GO,
9739d628a0SDimitry Andric const TargetMachine &TM) const {
98ff0cc061SDimitry Andric const MipsSubtarget &Subtarget =
99ff0cc061SDimitry Andric *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
1003861d79fSDimitry Andric
1013861d79fSDimitry Andric // Return if small section is not available.
1023861d79fSDimitry Andric if (!Subtarget.useSmallSection())
103f22ef01cSRoman Divacky return false;
104f22ef01cSRoman Divacky
105f22ef01cSRoman Divacky // Only global variables, not functions.
106d88c1a5aSDimitry Andric const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
107f22ef01cSRoman Divacky if (!GVA)
108f22ef01cSRoman Divacky return false;
109f22ef01cSRoman Divacky
1102cab237bSDimitry Andric // If the variable has an explicit section, it is placed in that section but
1112cab237bSDimitry Andric // it's addressing mode may change.
1122cab237bSDimitry Andric if (GVA->hasSection()) {
1132cab237bSDimitry Andric StringRef Section = GVA->getSection();
1142cab237bSDimitry Andric
1152cab237bSDimitry Andric // Explicitly placing any variable in the small data section overrides
1162cab237bSDimitry Andric // the global -G value.
1172cab237bSDimitry Andric if (Section == ".sdata" || Section == ".sbss")
1182cab237bSDimitry Andric return true;
1192cab237bSDimitry Andric
1202cab237bSDimitry Andric // Otherwise reject accessing it through the gp pointer. There are some
1212cab237bSDimitry Andric // historic cases which GCC doesn't appear to respect any more. These
1222cab237bSDimitry Andric // are .lit4, .lit8 and .srdata. For the moment reject these as well.
1232cab237bSDimitry Andric return false;
1242cab237bSDimitry Andric }
1252cab237bSDimitry Andric
12639d628a0SDimitry Andric // Enforce -mlocal-sdata.
127d88c1a5aSDimitry Andric if (!LocalSData && GVA->hasLocalLinkage())
128f22ef01cSRoman Divacky return false;
129f22ef01cSRoman Divacky
13039d628a0SDimitry Andric // Enforce -mextern-sdata.
131d88c1a5aSDimitry Andric if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
132d88c1a5aSDimitry Andric GVA->hasCommonLinkage()))
133f22ef01cSRoman Divacky return false;
134f22ef01cSRoman Divacky
1352cab237bSDimitry Andric // Enforce -membedded-data.
1362cab237bSDimitry Andric if (EmbeddedData && GVA->isConstant())
1372cab237bSDimitry Andric return false;
1382cab237bSDimitry Andric
139d88c1a5aSDimitry Andric Type *Ty = GVA->getValueType();
14007577dfeSDimitry Andric
14107577dfeSDimitry Andric // It is possible that the type of the global is unsized, i.e. a declaration
14207577dfeSDimitry Andric // of a extern struct. In this case don't presume it is in the small data
14307577dfeSDimitry Andric // section. This happens e.g. when building the FreeBSD kernel.
14407577dfeSDimitry Andric if (!Ty->isSized())
14507577dfeSDimitry Andric return false;
14607577dfeSDimitry Andric
1477d523365SDimitry Andric return IsInSmallSection(
148d88c1a5aSDimitry Andric GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
149f22ef01cSRoman Divacky }
150f22ef01cSRoman Divacky
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const151d88c1a5aSDimitry Andric MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
152d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
153f22ef01cSRoman Divacky // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
154f22ef01cSRoman Divacky // sections?
155f22ef01cSRoman Divacky
156f22ef01cSRoman Divacky // Handle Small Section classification here.
157d88c1a5aSDimitry Andric if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
158f22ef01cSRoman Divacky return SmallBSSSection;
159d88c1a5aSDimitry Andric if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
160f22ef01cSRoman Divacky return SmallDataSection;
1612cab237bSDimitry Andric if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
1622cab237bSDimitry Andric return SmallDataSection;
163f22ef01cSRoman Divacky
164f22ef01cSRoman Divacky // Otherwise, we work the same as ELF.
165d88c1a5aSDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
166f22ef01cSRoman Divacky }
16739d628a0SDimitry Andric
16839d628a0SDimitry Andric /// Return true if this constant should be placed into small data section.
IsConstantInSmallSection(const DataLayout & DL,const Constant * CN,const TargetMachine & TM) const1697d523365SDimitry Andric bool MipsTargetObjectFile::IsConstantInSmallSection(
1707d523365SDimitry Andric const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
171ff0cc061SDimitry Andric return (static_cast<const MipsTargetMachine &>(TM)
172ff0cc061SDimitry Andric .getSubtargetImpl()
173ff0cc061SDimitry Andric ->useSmallSection() &&
1747d523365SDimitry Andric LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
17539d628a0SDimitry Andric }
17639d628a0SDimitry Andric
1777d523365SDimitry Andric /// Return true if this constant should be placed into small data section.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,unsigned & Align) const1783ca95b02SDimitry Andric MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
1793ca95b02SDimitry Andric SectionKind Kind,
1803ca95b02SDimitry Andric const Constant *C,
1813ca95b02SDimitry Andric unsigned &Align) const {
1827d523365SDimitry Andric if (IsConstantInSmallSection(DL, C, *TM))
18339d628a0SDimitry Andric return SmallDataSection;
18439d628a0SDimitry Andric
18539d628a0SDimitry Andric // Otherwise, we work the same as ELF.
1863ca95b02SDimitry Andric return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
18739d628a0SDimitry Andric }
188077e1117SDimitry Andric
189077e1117SDimitry Andric const MCExpr *
getDebugThreadLocalSymbol(const MCSymbol * Sym) const190077e1117SDimitry Andric MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
191077e1117SDimitry Andric const MCExpr *Expr =
192077e1117SDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
193*b5893f02SDimitry Andric Expr = MCBinaryExpr::createAdd(
194077e1117SDimitry Andric Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
195*b5893f02SDimitry Andric return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());
196077e1117SDimitry Andric }
197