1 //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MipsTargetObjectFile.h"
11 #include "MipsSubtarget.h"
12 #include "MipsTargetMachine.h"
13 #include "MCTargetDesc/MipsMCExpr.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Target/TargetMachine.h"
24 static cl::opt<unsigned>
25 SSThreshold("mips-ssection-threshold", cl::Hidden,
26 cl::desc("Small data and bss section threshold size (default=8)"),
30 LocalSData("mlocal-sdata", cl::Hidden,
31 cl::desc("MIPS: Use gp_rel for object-local data."),
35 ExternSData("mextern-sdata", cl::Hidden,
36 cl::desc("MIPS: Use gp_rel for data that is not defined by the "
41 EmbeddedData("membedded-data", cl::Hidden,
42 cl::desc("MIPS: Try to allocate variables in the following"
43 " sections if possible: .rodata, .sdata, .data ."),
46 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
47 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
48 InitializeELF(TM.Options.UseInitArray);
50 SmallDataSection = getContext().getELFSection(
51 ".sdata", ELF::SHT_PROGBITS,
52 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
54 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
55 ELF::SHF_WRITE | ELF::SHF_ALLOC |
57 this->TM = &static_cast<const MipsTargetMachine &>(TM);
60 // A address must be loaded from a small section if its size is less than the
61 // small section size threshold. Data in this section must be addressed using
63 static bool IsInSmallSection(uint64_t Size) {
64 // gcc has traditionally not treated zero-sized objects as small data, so this
65 // is effectively part of the ABI.
66 return Size > 0 && Size <= SSThreshold;
69 /// Return true if this global address should be placed into small data/bss
71 bool MipsTargetObjectFile::IsGlobalInSmallSection(
72 const GlobalObject *GO, const TargetMachine &TM) const {
73 // We first check the case where global is a declaration, because finding
74 // section kind using getKindForGlobal() is only allowed for global
76 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
77 return IsGlobalInSmallSectionImpl(GO, TM);
79 return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
82 /// Return true if this global address should be placed into small data/bss
84 bool MipsTargetObjectFile::
85 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
86 SectionKind Kind) const {
87 return IsGlobalInSmallSectionImpl(GO, TM) &&
88 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
92 /// Return true if this global address should be placed into small data/bss
93 /// section. This method does all the work, except for checking the section
95 bool MipsTargetObjectFile::
96 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
97 const TargetMachine &TM) const {
98 const MipsSubtarget &Subtarget =
99 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
101 // Return if small section is not available.
102 if (!Subtarget.useSmallSection())
105 // Only global variables, not functions.
106 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
110 // If the variable has an explicit section, it is placed in that section but
111 // it's addressing mode may change.
112 if (GVA->hasSection()) {
113 StringRef Section = GVA->getSection();
115 // Explicitly placing any variable in the small data section overrides
116 // the global -G value.
117 if (Section == ".sdata" || Section == ".sbss")
120 // Otherwise reject accessing it through the gp pointer. There are some
121 // historic cases which GCC doesn't appear to respect any more. These
122 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
126 // Enforce -mlocal-sdata.
127 if (!LocalSData && GVA->hasLocalLinkage())
130 // Enforce -mextern-sdata.
131 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
132 GVA->hasCommonLinkage()))
135 // Enforce -membedded-data.
136 if (EmbeddedData && GVA->isConstant())
139 Type *Ty = GVA->getValueType();
141 // It is possible that the type of the global is unsized, i.e. a declaration
142 // of a extern struct. In this case don't presume it is in the small data
143 // section. This happens e.g. when building the FreeBSD kernel.
147 return IsInSmallSection(
148 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
151 MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
152 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
153 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
156 // Handle Small Section classification here.
157 if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
158 return SmallBSSSection;
159 if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
160 return SmallDataSection;
161 if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
162 return SmallDataSection;
164 // Otherwise, we work the same as ELF.
165 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
168 /// Return true if this constant should be placed into small data section.
169 bool MipsTargetObjectFile::IsConstantInSmallSection(
170 const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
171 return (static_cast<const MipsTargetMachine &>(TM)
173 ->useSmallSection() &&
174 LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
177 /// Return true if this constant should be placed into small data section.
178 MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
181 unsigned &Align) const {
182 if (IsConstantInSmallSection(DL, C, *TM))
183 return SmallDataSection;
185 // Otherwise, we work the same as ELF.
186 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
190 MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
192 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
193 Expr = MCBinaryExpr::createAdd(
194 Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
195 return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());