1 //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MipsTargetObjectFile.h"
11 #include "MipsSubtarget.h"
12 #include "MipsTargetMachine.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 using namespace llvm;
22 
23 static cl::opt<unsigned>
24 SSThreshold("mips-ssection-threshold", cl::Hidden,
25             cl::desc("Small data and bss section threshold size (default=8)"),
26             cl::init(8));
27 
28 static cl::opt<bool>
29 LocalSData("mlocal-sdata", cl::Hidden,
30            cl::desc("MIPS: Use gp_rel for object-local data."),
31            cl::init(true));
32 
33 static cl::opt<bool>
34 ExternSData("mextern-sdata", cl::Hidden,
35             cl::desc("MIPS: Use gp_rel for data that is not defined by the "
36                      "current object."),
37             cl::init(true));
38 
39 static cl::opt<bool>
40 EmbeddedData("membedded-data", cl::Hidden,
41              cl::desc("MIPS: Try to allocate variables in the following"
42                       " sections if possible: .rodata, .sdata, .data ."),
43              cl::init(false));
44 
45 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
46   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
47   InitializeELF(TM.Options.UseInitArray);
48 
49   SmallDataSection = getContext().getELFSection(
50       ".sdata", ELF::SHT_PROGBITS,
51       ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
52 
53   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
54                                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
55                                                    ELF::SHF_MIPS_GPREL);
56   this->TM = &static_cast<const MipsTargetMachine &>(TM);
57 }
58 
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
61 // gp_rel operator.
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;
66 }
67 
68 /// Return true if this global address should be placed into small data/bss
69 /// section.
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
74   // definitions.
75   if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
76     return IsGlobalInSmallSectionImpl(GO, TM);
77 
78   return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
79 }
80 
81 /// Return true if this global address should be placed into small data/bss
82 /// section.
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() ||
88           Kind.isReadOnly());
89 }
90 
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
93 /// kind.
94 bool MipsTargetObjectFile::
95 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
96                            const TargetMachine &TM) const {
97   const MipsSubtarget &Subtarget =
98       *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
99 
100   // Return if small section is not available.
101   if (!Subtarget.useSmallSection())
102     return false;
103 
104   // Only global variables, not functions.
105   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
106   if (!GVA)
107     return false;
108 
109   // Enforce -mlocal-sdata.
110   if (!LocalSData && GVA->hasLocalLinkage())
111     return false;
112 
113   // Enforce -mextern-sdata.
114   if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
115                        GVA->hasCommonLinkage()))
116     return false;
117 
118   // Enforce -membedded-data.
119   if (EmbeddedData && GVA->isConstant())
120     return false;
121 
122   Type *Ty = GVA->getValueType();
123   return IsInSmallSection(
124       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
125 }
126 
127 MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
128     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
129   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
130   // sections?
131 
132   // Handle Small Section classification here.
133   if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
134     return SmallBSSSection;
135   if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
136     return SmallDataSection;
137   if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
138     return SmallDataSection;
139 
140   // Otherwise, we work the same as ELF.
141   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
142 }
143 
144 /// Return true if this constant should be placed into small data section.
145 bool MipsTargetObjectFile::IsConstantInSmallSection(
146     const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
147   return (static_cast<const MipsTargetMachine &>(TM)
148               .getSubtargetImpl()
149               ->useSmallSection() &&
150           LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
151 }
152 
153 /// Return true if this constant should be placed into small data section.
154 MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
155                                                        SectionKind Kind,
156                                                        const Constant *C,
157                                                        unsigned &Align) const {
158   if (IsConstantInSmallSection(DL, C, *TM))
159     return SmallDataSection;
160 
161   // Otherwise, we work the same as ELF.
162   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
163 }
164 
165 const MCExpr *
166 MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
167   const MCExpr *Expr =
168       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
169   return MCBinaryExpr::createAdd(
170       Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
171 }
172