2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===//
8 #include "LanaiTargetObjectFile.h"
10 #include "LanaiSubtarget.h"
11 #include "LanaiTargetMachine.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Target/TargetMachine.h"
23 static cl::opt
<unsigned> SSThreshold(
24 "lanai-ssection-threshold", cl::Hidden
,
25 cl::desc("Small data and bss section threshold size (default=0)"),
28 void LanaiTargetObjectFile::Initialize(MCContext
&Ctx
,
29 const TargetMachine
&TM
) {
30 TargetLoweringObjectFileELF::Initialize(Ctx
, TM
);
31 InitializeELF(TM
.Options
.UseInitArray
);
33 SmallDataSection
= getContext().getELFSection(
34 ".sdata", ELF::SHT_PROGBITS
, ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
35 SmallBSSSection
= getContext().getELFSection(".sbss", ELF::SHT_NOBITS
,
36 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
39 // A address must be loaded from a small section if its size is less than the
40 // small section size threshold. Data in this section must be addressed using
42 static bool isInSmallSection(uint64_t Size
) {
43 // gcc has traditionally not treated zero-sized objects as small data, so this
44 // is effectively part of the ABI.
45 return Size
> 0 && Size
<= SSThreshold
;
48 // Return true if this global address should be placed into small data/bss
50 bool LanaiTargetObjectFile::isGlobalInSmallSection(
51 const GlobalObject
*GO
, const TargetMachine
&TM
) const {
52 if (GO
== nullptr) return TM
.getCodeModel() == CodeModel::Small
;
54 // We first check the case where global is a declaration, because finding
55 // section kind using getKindForGlobal() is only allowed for global
57 if (GO
->isDeclaration() || GO
->hasAvailableExternallyLinkage())
58 return isGlobalInSmallSectionImpl(GO
, TM
);
60 return isGlobalInSmallSection(GO
, TM
, getKindForGlobal(GO
, TM
));
63 // Return true if this global address should be placed into small data/bss
65 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject
*GO
,
66 const TargetMachine
&TM
,
67 SectionKind Kind
) const {
68 return isGlobalInSmallSectionImpl(GO
, TM
);
71 // Return true if this global address should be placed into small data/bss
72 // section. This method does all the work, except for checking the section
74 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
75 const GlobalObject
*GO
, const TargetMachine
&TM
) const {
76 const auto *GVA
= dyn_cast
<GlobalVariable
>(GO
);
78 // If not a GlobalVariable, only consider the code model.
79 if (!GVA
) return TM
.getCodeModel() == CodeModel::Small
;
81 // Global values placed in sections starting with .ldata do not fit in
82 // 21-bits, so always use large memory access for them. FIXME: This is a
83 // workaround for a tool limitation.
84 if (GVA
->getSection().startswith(".ldata"))
87 if (TM
.getCodeModel() == CodeModel::Small
)
90 if (GVA
->hasLocalLinkage())
93 if (((GVA
->hasExternalLinkage() && GVA
->isDeclaration()) ||
94 GVA
->hasCommonLinkage()))
97 Type
*Ty
= GVA
->getValueType();
98 return isInSmallSection(
99 GVA
->getParent()->getDataLayout().getTypeAllocSize(Ty
));
102 MCSection
*LanaiTargetObjectFile::SelectSectionForGlobal(
103 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
104 // Handle Small Section classification here.
105 if (Kind
.isBSS() && isGlobalInSmallSection(GO
, TM
, Kind
))
106 return SmallBSSSection
;
107 if (Kind
.isData() && isGlobalInSmallSection(GO
, TM
, Kind
))
108 return SmallDataSection
;
110 // Otherwise, we work the same as ELF.
111 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO
, Kind
, TM
);
114 /// Return true if this constant should be placed into small data section.
115 bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout
&DL
,
116 const Constant
*CN
) const {
117 return isInSmallSection(DL
.getTypeAllocSize(CN
->getType()));
120 MCSection
*LanaiTargetObjectFile::getSectionForConstant(const DataLayout
&DL
,
123 unsigned &Align
) const {
124 if (isConstantInSmallSection(DL
, C
))
125 return SmallDataSection
;
127 // Otherwise, we work the same as ELF.
128 return TargetLoweringObjectFileELF::getSectionForConstant(DL
, Kind
, C
, Align
);