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
);
47 InitializeELF(TM
.Options
.UseInitArray
);
49 SmallDataSection
= getContext().getELFSection(
50 ".sdata", ELF::SHT_PROGBITS
,
51 ELF::SHF_WRITE
| ELF::SHF_ALLOC
| ELF::SHF_MIPS_GPREL
);
53 SmallBSSSection
= getContext().getELFSection(".sbss", ELF::SHT_NOBITS
,
54 ELF::SHF_WRITE
| ELF::SHF_ALLOC
|
56 this->TM
= &static_cast<const MipsTargetMachine
&>(TM
);
59 // A address must be loaded from a small section if its size is less than the
60 // small section size threshold. Data in this section must be addressed using
62 static bool IsInSmallSection(uint64_t Size
) {
63 // gcc has traditionally not treated zero-sized objects as small data, so this
64 // is effectively part of the ABI.
65 return Size
> 0 && Size
<= SSThreshold
;
68 /// Return true if this global address should be placed into small data/bss
70 bool MipsTargetObjectFile::IsGlobalInSmallSection(
71 const GlobalObject
*GO
, const TargetMachine
&TM
) const {
72 // We first check the case where global is a declaration, because finding
73 // section kind using getKindForGlobal() is only allowed for global
75 if (GO
->isDeclaration() || GO
->hasAvailableExternallyLinkage())
76 return IsGlobalInSmallSectionImpl(GO
, TM
);
78 return IsGlobalInSmallSection(GO
, TM
, getKindForGlobal(GO
, TM
));
81 /// Return true if this global address should be placed into small data/bss
83 bool MipsTargetObjectFile::
84 IsGlobalInSmallSection(const GlobalObject
*GO
, const TargetMachine
&TM
,
85 SectionKind Kind
) const {
86 return IsGlobalInSmallSectionImpl(GO
, TM
) &&
87 (Kind
.isData() || Kind
.isBSS() || Kind
.isCommon() ||
91 /// Return true if this global address should be placed into small data/bss
92 /// section. This method does all the work, except for checking the section
94 bool MipsTargetObjectFile::
95 IsGlobalInSmallSectionImpl(const GlobalObject
*GO
,
96 const TargetMachine
&TM
) const {
97 const MipsSubtarget
&Subtarget
=
98 *static_cast<const MipsTargetMachine
&>(TM
).getSubtargetImpl();
100 // Return if small section is not available.
101 if (!Subtarget
.useSmallSection())
104 // Only global variables, not functions.
105 const GlobalVariable
*GVA
= dyn_cast
<GlobalVariable
>(GO
);
109 // If the variable has an explicit section, it is placed in that section but
110 // it's addressing mode may change.
111 if (GVA
->hasSection()) {
112 StringRef Section
= GVA
->getSection();
114 // Explicitly placing any variable in the small data section overrides
115 // the global -G value.
116 if (Section
== ".sdata" || Section
== ".sbss")
119 // Otherwise reject accessing it through the gp pointer. There are some
120 // historic cases which GCC doesn't appear to respect any more. These
121 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
125 // Enforce -mlocal-sdata.
126 if (!LocalSData
&& GVA
->hasLocalLinkage())
129 // Enforce -mextern-sdata.
130 if (!ExternSData
&& ((GVA
->hasExternalLinkage() && GVA
->isDeclaration()) ||
131 GVA
->hasCommonLinkage()))
134 // Enforce -membedded-data.
135 if (EmbeddedData
&& GVA
->isConstant())
138 Type
*Ty
= GVA
->getValueType();
140 // It is possible that the type of the global is unsized, i.e. a declaration
141 // of a extern struct. In this case don't presume it is in the small data
142 // section. This happens e.g. when building the FreeBSD kernel.
146 return IsInSmallSection(
147 GVA
->getParent()->getDataLayout().getTypeAllocSize(Ty
));
150 MCSection
*MipsTargetObjectFile::SelectSectionForGlobal(
151 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
152 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
155 // Handle Small Section classification here.
156 if (Kind
.isBSS() && IsGlobalInSmallSection(GO
, TM
, Kind
))
157 return SmallBSSSection
;
158 if (Kind
.isData() && IsGlobalInSmallSection(GO
, TM
, Kind
))
159 return SmallDataSection
;
160 if (Kind
.isReadOnly() && IsGlobalInSmallSection(GO
, TM
, Kind
))
161 return SmallDataSection
;
163 // Otherwise, we work the same as ELF.
164 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO
, Kind
, TM
);
167 /// Return true if this constant should be placed into small data section.
168 bool MipsTargetObjectFile::IsConstantInSmallSection(
169 const DataLayout
&DL
, const Constant
*CN
, const TargetMachine
&TM
) const {
170 return (static_cast<const MipsTargetMachine
&>(TM
)
172 ->useSmallSection() &&
173 LocalSData
&& IsInSmallSection(DL
.getTypeAllocSize(CN
->getType())));
176 /// Return true if this constant should be placed into small data section.
177 MCSection
*MipsTargetObjectFile::getSectionForConstant(const DataLayout
&DL
,
180 unsigned &Align
) const {
181 if (IsConstantInSmallSection(DL
, C
, *TM
))
182 return SmallDataSection
;
184 // Otherwise, we work the same as ELF.
185 return TargetLoweringObjectFileELF::getSectionForConstant(DL
, Kind
, C
, Align
);
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());