1*9880d681SAndroid Build Coastguard Worker //
2*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
3*9880d681SAndroid Build Coastguard Worker //
4*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
5*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
6*9880d681SAndroid Build Coastguard Worker //
7*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Worker #include "LanaiTargetObjectFile.h"
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Worker #include "LanaiSubtarget.h"
12*9880d681SAndroid Build Coastguard Worker #include "LanaiTargetMachine.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSectionELF.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ELF.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker using namespace llvm;
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> SSThreshold(
25*9880d681SAndroid Build Coastguard Worker "lanai-ssection-threshold", cl::Hidden,
26*9880d681SAndroid Build Coastguard Worker cl::desc("Small data and bss section threshold size (default=0)"),
27*9880d681SAndroid Build Coastguard Worker cl::init(0));
28*9880d681SAndroid Build Coastguard Worker
Initialize(MCContext & Ctx,const TargetMachine & TM)29*9880d681SAndroid Build Coastguard Worker void LanaiTargetObjectFile::Initialize(MCContext &Ctx,
30*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM) {
31*9880d681SAndroid Build Coastguard Worker TargetLoweringObjectFileELF::Initialize(Ctx, TM);
32*9880d681SAndroid Build Coastguard Worker InitializeELF(TM.Options.UseInitArray);
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker SmallDataSection = getContext().getELFSection(
35*9880d681SAndroid Build Coastguard Worker ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
36*9880d681SAndroid Build Coastguard Worker SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
37*9880d681SAndroid Build Coastguard Worker ELF::SHF_WRITE | ELF::SHF_ALLOC);
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker // A address must be loaded from a small section if its size is less than the
41*9880d681SAndroid Build Coastguard Worker // small section size threshold. Data in this section must be addressed using
42*9880d681SAndroid Build Coastguard Worker // gp_rel operator.
isInSmallSection(uint64_t Size)43*9880d681SAndroid Build Coastguard Worker static bool isInSmallSection(uint64_t Size) {
44*9880d681SAndroid Build Coastguard Worker // gcc has traditionally not treated zero-sized objects as small data, so this
45*9880d681SAndroid Build Coastguard Worker // is effectively part of the ABI.
46*9880d681SAndroid Build Coastguard Worker return Size > 0 && Size <= SSThreshold;
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker // Return true if this global address should be placed into small data/bss
50*9880d681SAndroid Build Coastguard Worker // section.
isGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM) const51*9880d681SAndroid Build Coastguard Worker bool LanaiTargetObjectFile::isGlobalInSmallSection(
52*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV, const TargetMachine &TM) const {
53*9880d681SAndroid Build Coastguard Worker // We first check the case where global is a declaration, because finding
54*9880d681SAndroid Build Coastguard Worker // section kind using getKindForGlobal() is only allowed for global
55*9880d681SAndroid Build Coastguard Worker // definitions.
56*9880d681SAndroid Build Coastguard Worker if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
57*9880d681SAndroid Build Coastguard Worker return isGlobalInSmallSectionImpl(GV, TM);
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker return isGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker // Return true if this global address should be placed into small data/bss
63*9880d681SAndroid Build Coastguard Worker // section.
isGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM,SectionKind Kind) const64*9880d681SAndroid Build Coastguard Worker bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalValue *GV,
65*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM,
66*9880d681SAndroid Build Coastguard Worker SectionKind Kind) const {
67*9880d681SAndroid Build Coastguard Worker return (isGlobalInSmallSectionImpl(GV, TM) &&
68*9880d681SAndroid Build Coastguard Worker (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker // Return true if this global address should be placed into small data/bss
72*9880d681SAndroid Build Coastguard Worker // section. This method does all the work, except for checking the section
73*9880d681SAndroid Build Coastguard Worker // kind.
isGlobalInSmallSectionImpl(const GlobalValue * GV,const TargetMachine & TM) const74*9880d681SAndroid Build Coastguard Worker bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
75*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV, const TargetMachine &TM) const {
76*9880d681SAndroid Build Coastguard Worker // Only global variables, not functions.
77*9880d681SAndroid Build Coastguard Worker const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
78*9880d681SAndroid Build Coastguard Worker if (!GVA)
79*9880d681SAndroid Build Coastguard Worker return false;
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker if (GV->hasLocalLinkage())
82*9880d681SAndroid Build Coastguard Worker return false;
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker if (((GV->hasExternalLinkage() && GV->isDeclaration()) ||
85*9880d681SAndroid Build Coastguard Worker GV->hasCommonLinkage()))
86*9880d681SAndroid Build Coastguard Worker return false;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker Type *Ty = GV->getType()->getElementType();
89*9880d681SAndroid Build Coastguard Worker return isInSmallSection(
90*9880d681SAndroid Build Coastguard Worker GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker MCSection *
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const94*9880d681SAndroid Build Coastguard Worker LanaiTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
95*9880d681SAndroid Build Coastguard Worker SectionKind Kind, Mangler &Mang,
96*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM) const {
97*9880d681SAndroid Build Coastguard Worker // Handle Small Section classification here.
98*9880d681SAndroid Build Coastguard Worker if (Kind.isBSS() && isGlobalInSmallSection(GV, TM, Kind))
99*9880d681SAndroid Build Coastguard Worker return SmallBSSSection;
100*9880d681SAndroid Build Coastguard Worker if (Kind.isData() && isGlobalInSmallSection(GV, TM, Kind))
101*9880d681SAndroid Build Coastguard Worker return SmallDataSection;
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker // Otherwise, we work the same as ELF.
104*9880d681SAndroid Build Coastguard Worker return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,
105*9880d681SAndroid Build Coastguard Worker TM);
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker /// Return true if this constant should be placed into small data section.
isConstantInSmallSection(const DataLayout & DL,const Constant * CN) const109*9880d681SAndroid Build Coastguard Worker bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL,
110*9880d681SAndroid Build Coastguard Worker const Constant *CN) const {
111*9880d681SAndroid Build Coastguard Worker return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,unsigned & Align) const114*9880d681SAndroid Build Coastguard Worker MCSection *LanaiTargetObjectFile::getSectionForConstant(const DataLayout &DL,
115*9880d681SAndroid Build Coastguard Worker SectionKind Kind,
116*9880d681SAndroid Build Coastguard Worker const Constant *C,
117*9880d681SAndroid Build Coastguard Worker unsigned &Align) const {
118*9880d681SAndroid Build Coastguard Worker if (isConstantInSmallSection(DL, C))
119*9880d681SAndroid Build Coastguard Worker return SmallDataSection;
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker // Otherwise, we work the same as ELF.
122*9880d681SAndroid Build Coastguard Worker return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
123*9880d681SAndroid Build Coastguard Worker }
124