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