1*0b57cec5SDimitry Andric //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "MipsTargetObjectFile.h"
10*0b57cec5SDimitry Andric #include "MipsSubtarget.h"
11*0b57cec5SDimitry Andric #include "MipsTargetMachine.h"
12*0b57cec5SDimitry Andric #include "MCTargetDesc/MipsMCExpr.h"
13*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
14*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
17*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
18*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
20*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric static cl::opt<unsigned>
24*0b57cec5SDimitry Andric SSThreshold("mips-ssection-threshold", cl::Hidden,
25*0b57cec5SDimitry Andric             cl::desc("Small data and bss section threshold size (default=8)"),
26*0b57cec5SDimitry Andric             cl::init(8));
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric static cl::opt<bool>
29*0b57cec5SDimitry Andric LocalSData("mlocal-sdata", cl::Hidden,
30*0b57cec5SDimitry Andric            cl::desc("MIPS: Use gp_rel for object-local data."),
31*0b57cec5SDimitry Andric            cl::init(true));
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric static cl::opt<bool>
34*0b57cec5SDimitry Andric ExternSData("mextern-sdata", cl::Hidden,
35*0b57cec5SDimitry Andric             cl::desc("MIPS: Use gp_rel for data that is not defined by the "
36*0b57cec5SDimitry Andric                      "current object."),
37*0b57cec5SDimitry Andric             cl::init(true));
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric static cl::opt<bool>
40*0b57cec5SDimitry Andric EmbeddedData("membedded-data", cl::Hidden,
41*0b57cec5SDimitry Andric              cl::desc("MIPS: Try to allocate variables in the following"
42*0b57cec5SDimitry Andric                       " sections if possible: .rodata, .sdata, .data ."),
43*0b57cec5SDimitry Andric              cl::init(false));
44*0b57cec5SDimitry Andric 
Initialize(MCContext & Ctx,const TargetMachine & TM)45*0b57cec5SDimitry Andric void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
46*0b57cec5SDimitry Andric   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric   SmallDataSection = getContext().getELFSection(
49*0b57cec5SDimitry Andric       ".sdata", ELF::SHT_PROGBITS,
50*0b57cec5SDimitry Andric       ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
53*0b57cec5SDimitry Andric                                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
54*0b57cec5SDimitry Andric                                                    ELF::SHF_MIPS_GPREL);
55*0b57cec5SDimitry Andric   this->TM = &static_cast<const MipsTargetMachine &>(TM);
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric // A address must be loaded from a small section if its size is less than the
59*0b57cec5SDimitry Andric // small section size threshold. Data in this section must be addressed using
60*0b57cec5SDimitry Andric // gp_rel operator.
IsInSmallSection(uint64_t Size)61*0b57cec5SDimitry Andric static bool IsInSmallSection(uint64_t Size) {
62*0b57cec5SDimitry Andric   // gcc has traditionally not treated zero-sized objects as small data, so this
63*0b57cec5SDimitry Andric   // is effectively part of the ABI.
64*0b57cec5SDimitry Andric   return Size > 0 && Size <= SSThreshold;
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss
68*0b57cec5SDimitry Andric /// section.
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const69*0b57cec5SDimitry Andric bool MipsTargetObjectFile::IsGlobalInSmallSection(
70*0b57cec5SDimitry Andric     const GlobalObject *GO, const TargetMachine &TM) const {
71*0b57cec5SDimitry Andric   // We first check the case where global is a declaration, because finding
72*0b57cec5SDimitry Andric   // section kind using getKindForGlobal() is only allowed for global
73*0b57cec5SDimitry Andric   // definitions.
74*0b57cec5SDimitry Andric   if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
75*0b57cec5SDimitry Andric     return IsGlobalInSmallSectionImpl(GO, TM);
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric   return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss
81*0b57cec5SDimitry Andric /// section.
82*0b57cec5SDimitry Andric bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM,SectionKind Kind) const83*0b57cec5SDimitry Andric IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
84*0b57cec5SDimitry Andric                        SectionKind Kind) const {
85*0b57cec5SDimitry Andric   return IsGlobalInSmallSectionImpl(GO, TM) &&
86*0b57cec5SDimitry Andric          (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
87*0b57cec5SDimitry Andric           Kind.isReadOnly());
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric /// Return true if this global address should be placed into small data/bss
91*0b57cec5SDimitry Andric /// section. This method does all the work, except for checking the section
92*0b57cec5SDimitry Andric /// kind.
93*0b57cec5SDimitry Andric bool MipsTargetObjectFile::
IsGlobalInSmallSectionImpl(const GlobalObject * GO,const TargetMachine & TM) const94*0b57cec5SDimitry Andric IsGlobalInSmallSectionImpl(const GlobalObject *GO,
95*0b57cec5SDimitry Andric                            const TargetMachine &TM) const {
96*0b57cec5SDimitry Andric   const MipsSubtarget &Subtarget =
97*0b57cec5SDimitry Andric       *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric   // Return if small section is not available.
100*0b57cec5SDimitry Andric   if (!Subtarget.useSmallSection())
101*0b57cec5SDimitry Andric     return false;
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric   // Only global variables, not functions.
104*0b57cec5SDimitry Andric   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
105*0b57cec5SDimitry Andric   if (!GVA)
106*0b57cec5SDimitry Andric     return false;
107*0b57cec5SDimitry Andric 
108*0b57cec5SDimitry Andric   // If the variable has an explicit section, it is placed in that section but
109*0b57cec5SDimitry Andric   // it's addressing mode may change.
110*0b57cec5SDimitry Andric   if (GVA->hasSection()) {
111*0b57cec5SDimitry Andric     StringRef Section = GVA->getSection();
112*0b57cec5SDimitry Andric 
113*0b57cec5SDimitry Andric     // Explicitly placing any variable in the small data section overrides
114*0b57cec5SDimitry Andric     // the global -G value.
115*0b57cec5SDimitry Andric     if (Section == ".sdata" || Section == ".sbss")
116*0b57cec5SDimitry Andric       return true;
117*0b57cec5SDimitry Andric 
118*0b57cec5SDimitry Andric     // Otherwise reject accessing it through the gp pointer. There are some
119*0b57cec5SDimitry Andric     // historic cases which GCC doesn't appear to respect any more. These
120*0b57cec5SDimitry Andric     // are .lit4, .lit8 and .srdata. For the moment reject these as well.
121*0b57cec5SDimitry Andric     return false;
122*0b57cec5SDimitry Andric   }
123*0b57cec5SDimitry Andric 
124*0b57cec5SDimitry Andric   // Enforce -mlocal-sdata.
125*0b57cec5SDimitry Andric   if (!LocalSData && GVA->hasLocalLinkage())
126*0b57cec5SDimitry Andric     return false;
127*0b57cec5SDimitry Andric 
128*0b57cec5SDimitry Andric   // Enforce -mextern-sdata.
129*0b57cec5SDimitry Andric   if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
130*0b57cec5SDimitry Andric                        GVA->hasCommonLinkage()))
131*0b57cec5SDimitry Andric     return false;
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric   // Enforce -membedded-data.
134*0b57cec5SDimitry Andric   if (EmbeddedData && GVA->isConstant())
135*0b57cec5SDimitry Andric     return false;
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric   Type *Ty = GVA->getValueType();
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric   // It is possible that the type of the global is unsized, i.e. a declaration
140*0b57cec5SDimitry Andric   // of a extern struct. In this case don't presume it is in the small data
141*0b57cec5SDimitry Andric   // section. This happens e.g. when building the FreeBSD kernel.
142*0b57cec5SDimitry Andric   if (!Ty->isSized())
143*0b57cec5SDimitry Andric     return false;
144*0b57cec5SDimitry Andric 
145*0b57cec5SDimitry Andric   return IsInSmallSection(
146*0b57cec5SDimitry Andric       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
147*0b57cec5SDimitry Andric }
148*0b57cec5SDimitry Andric 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const149*0b57cec5SDimitry Andric MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
150*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
151*0b57cec5SDimitry Andric   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
152*0b57cec5SDimitry Andric   // sections?
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric   // Handle Small Section classification here.
155*0b57cec5SDimitry Andric   if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
156*0b57cec5SDimitry Andric     return SmallBSSSection;
157*0b57cec5SDimitry Andric   if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
158*0b57cec5SDimitry Andric     return SmallDataSection;
159*0b57cec5SDimitry Andric   if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
160*0b57cec5SDimitry Andric     return SmallDataSection;
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric   // Otherwise, we work the same as ELF.
163*0b57cec5SDimitry Andric   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
164*0b57cec5SDimitry Andric }
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric /// Return true if this constant should be placed into small data section.
IsConstantInSmallSection(const DataLayout & DL,const Constant * CN,const TargetMachine & TM) const167*0b57cec5SDimitry Andric bool MipsTargetObjectFile::IsConstantInSmallSection(
168*0b57cec5SDimitry Andric     const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
169*0b57cec5SDimitry Andric   return (static_cast<const MipsTargetMachine &>(TM)
170*0b57cec5SDimitry Andric               .getSubtargetImpl()
171*0b57cec5SDimitry Andric               ->useSmallSection() &&
172*0b57cec5SDimitry Andric           LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
173*0b57cec5SDimitry Andric }
174*0b57cec5SDimitry Andric 
175*0b57cec5SDimitry Andric /// Return true if this constant should be placed into small data section.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const176*0b57cec5SDimitry Andric MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
177*0b57cec5SDimitry Andric                                                        SectionKind Kind,
178*0b57cec5SDimitry Andric                                                        const Constant *C,
179*0b57cec5SDimitry Andric                                                        Align &Alignment) const {
180*0b57cec5SDimitry Andric   if (IsConstantInSmallSection(DL, C, *TM))
181*0b57cec5SDimitry Andric     return SmallDataSection;
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric   // Otherwise, we work the same as ELF.
184*0b57cec5SDimitry Andric   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
185*0b57cec5SDimitry Andric                                                             Alignment);
186*0b57cec5SDimitry Andric }
187*0b57cec5SDimitry Andric 
188*0b57cec5SDimitry Andric const MCExpr *
getDebugThreadLocalSymbol(const MCSymbol * Sym) const189*0b57cec5SDimitry Andric MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
190*0b57cec5SDimitry Andric   const MCExpr *Expr =
191*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
192*0b57cec5SDimitry Andric   Expr = MCBinaryExpr::createAdd(
193*0b57cec5SDimitry Andric       Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
194*0b57cec5SDimitry Andric   return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());
195*0b57cec5SDimitry Andric }
196