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 "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"
23 static cl::opt<unsigned>
24 SSThreshold("mips-ssection-threshold", cl::Hidden,
25 cl::desc("Small data and bss section threshold size (default=8)"),
29 LocalSData("mlocal-sdata", cl::Hidden,
30 cl::desc("MIPS: Use gp_rel for object-local data."),
34 ExternSData("mextern-sdata", cl::Hidden,
35 cl::desc("MIPS: Use gp_rel for data that is not defined by the "
40 EmbeddedData("membedded-data", cl::Hidden,
41 cl::desc("MIPS: Try to allocate variables in the following"
42 " sections if possible: .rodata, .sdata, .data ."),
45 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
46 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
47 InitializeELF(TM.Options.UseInitArray);
49 SmallDataSection = getContext().getELFSection(
50 ".sdata", ELF::SHT_PROGBITS,
51 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
53 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
54 ELF::SHF_WRITE | ELF::SHF_ALLOC |
56 this->TM = &static_cast<const MipsTargetMachine &>(TM);
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
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;
68 /// Return true if this global address should be placed into small data/bss
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
75 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
76 return IsGlobalInSmallSectionImpl(GO, TM);
78 return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
81 /// Return true if this global address should be placed into small data/bss
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() ||
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
94 bool MipsTargetObjectFile::
95 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
96 const TargetMachine &TM) const {
97 const MipsSubtarget &Subtarget =
98 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
100 // Return if small section is not available.
101 if (!Subtarget.useSmallSection())
104 // Only global variables, not functions.
105 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
109 // If the variable has an explicit section, it is placed in that section but
110 // it's addressing mode may change.
111 if (GVA->hasSection()) {
112 StringRef Section = GVA->getSection();
114 // Explicitly placing any variable in the small data section overrides
115 // the global -G value.
116 if (Section == ".sdata" || Section == ".sbss")
119 // Otherwise reject accessing it through the gp pointer. There are some
120 // historic cases which GCC doesn't appear to respect any more. These
121 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
125 // Enforce -mlocal-sdata.
126 if (!LocalSData && GVA->hasLocalLinkage())
129 // Enforce -mextern-sdata.
130 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
131 GVA->hasCommonLinkage()))
134 // Enforce -membedded-data.
135 if (EmbeddedData && GVA->isConstant())
138 Type *Ty = GVA->getValueType();
139 return IsInSmallSection(
140 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
143 MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
144 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
145 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
148 // Handle Small Section classification here.
149 if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
150 return SmallBSSSection;
151 if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
152 return SmallDataSection;
153 if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
154 return SmallDataSection;
156 // Otherwise, we work the same as ELF.
157 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
160 /// Return true if this constant should be placed into small data section.
161 bool MipsTargetObjectFile::IsConstantInSmallSection(
162 const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
163 return (static_cast<const MipsTargetMachine &>(TM)
165 ->useSmallSection() &&
166 LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
169 /// Return true if this constant should be placed into small data section.
170 MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
173 unsigned &Align) const {
174 if (IsConstantInSmallSection(DL, C, *TM))
175 return SmallDataSection;
177 // Otherwise, we work the same as ELF.
178 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
182 MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
184 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
185 return MCBinaryExpr::createAdd(
186 Expr, MCConstantExpr::create(0x8000, getContext()), getContext());