1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "LanaiTargetObjectFile.h"
10
11 #include "LanaiSubtarget.h"
12 #include "LanaiTargetMachine.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"
21
22 using namespace llvm;
23
24 static cl::opt<unsigned> SSThreshold(
25 "lanai-ssection-threshold", cl::Hidden,
26 cl::desc("Small data and bss section threshold size (default=0)"),
27 cl::init(0));
28
Initialize(MCContext & Ctx,const TargetMachine & TM)29 void LanaiTargetObjectFile::Initialize(MCContext &Ctx,
30 const TargetMachine &TM) {
31 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
32 InitializeELF(TM.Options.UseInitArray);
33
34 SmallDataSection = getContext().getELFSection(
35 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
36 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
37 ELF::SHF_WRITE | ELF::SHF_ALLOC);
38 }
39
40 // A address must be loaded from a small section if its size is less than the
41 // small section size threshold. Data in this section must be addressed using
42 // gp_rel operator.
isInSmallSection(uint64_t Size)43 static bool isInSmallSection(uint64_t Size) {
44 // gcc has traditionally not treated zero-sized objects as small data, so this
45 // is effectively part of the ABI.
46 return Size > 0 && Size <= SSThreshold;
47 }
48
49 // Return true if this global address should be placed into small data/bss
50 // section.
isGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const51 bool LanaiTargetObjectFile::isGlobalInSmallSection(
52 const GlobalObject *GO, const TargetMachine &TM) const {
53 if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small;
54
55 // We first check the case where global is a declaration, because finding
56 // section kind using getKindForGlobal() is only allowed for global
57 // definitions.
58 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
59 return isGlobalInSmallSectionImpl(GO, TM);
60
61 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
62 }
63
64 // Return true if this global address should be placed into small data/bss
65 // section.
isGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM,SectionKind Kind) const66 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
67 const TargetMachine &TM,
68 SectionKind Kind) const {
69 return isGlobalInSmallSectionImpl(GO, TM);
70 }
71
72 // Return true if this global address should be placed into small data/bss
73 // section. This method does all the work, except for checking the section
74 // kind.
isGlobalInSmallSectionImpl(const GlobalObject * GO,const TargetMachine & TM) const75 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
76 const GlobalObject *GO, const TargetMachine &TM) const {
77 const auto *GVA = dyn_cast<GlobalVariable>(GO);
78
79 // If not a GlobalVariable, only consider the code model.
80 if (!GVA) return TM.getCodeModel() == CodeModel::Small;
81
82 // Global values placed in sections starting with .ldata do not fit in
83 // 21-bits, so always use large memory access for them. FIXME: This is a
84 // workaround for a tool limitation.
85 if (GVA->getSection().startswith(".ldata"))
86 return false;
87
88 if (TM.getCodeModel() == CodeModel::Small)
89 return true;
90
91 if (GVA->hasLocalLinkage())
92 return false;
93
94 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
95 GVA->hasCommonLinkage()))
96 return false;
97
98 Type *Ty = GVA->getValueType();
99 return isInSmallSection(
100 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
101 }
102
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const103 MCSection *LanaiTargetObjectFile::SelectSectionForGlobal(
104 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
105 // Handle Small Section classification here.
106 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
107 return SmallBSSSection;
108 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
109 return SmallDataSection;
110
111 // Otherwise, we work the same as ELF.
112 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
113 }
114
115 /// Return true if this constant should be placed into small data section.
isConstantInSmallSection(const DataLayout & DL,const Constant * CN) const116 bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL,
117 const Constant *CN) const {
118 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
119 }
120
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,unsigned & Align) const121 MCSection *LanaiTargetObjectFile::getSectionForConstant(const DataLayout &DL,
122 SectionKind Kind,
123 const Constant *C,
124 unsigned &Align) const {
125 if (isConstantInSmallSection(DL, C))
126 return SmallDataSection;
127
128 // Otherwise, we work the same as ELF.
129 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
130 }
131