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/IR/DataLayout.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ELF.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 "
39 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
40 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
41 InitializeELF(TM.Options.UseInitArray);
43 SmallDataSection = getContext().getELFSection(
44 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
46 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
47 ELF::SHF_WRITE | ELF::SHF_ALLOC);
48 this->TM = &static_cast<const MipsTargetMachine &>(TM);
51 // A address must be loaded from a small section if its size is less than the
52 // small section size threshold. Data in this section must be addressed using
54 static bool IsInSmallSection(uint64_t Size) {
55 // gcc has traditionally not treated zero-sized objects as small data, so this
56 // is effectively part of the ABI.
57 return Size > 0 && Size <= SSThreshold;
60 /// Return true if this global address should be placed into small data/bss
62 bool MipsTargetObjectFile::
63 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
64 // We first check the case where global is a declaration, because finding
65 // section kind using getKindForGlobal() is only allowed for global
67 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
68 return IsGlobalInSmallSectionImpl(GV, TM);
70 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
73 /// Return true if this global address should be placed into small data/bss
75 bool MipsTargetObjectFile::
76 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
77 SectionKind Kind) const {
78 return (IsGlobalInSmallSectionImpl(GV, TM) &&
79 (Kind.isDataRel() || Kind.isBSS() || Kind.isCommon()));
82 /// Return true if this global address should be placed into small data/bss
83 /// section. This method does all the work, except for checking the section
85 bool MipsTargetObjectFile::
86 IsGlobalInSmallSectionImpl(const GlobalValue *GV,
87 const TargetMachine &TM) const {
88 const MipsSubtarget &Subtarget =
89 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
91 // Return if small section is not available.
92 if (!Subtarget.useSmallSection())
95 // Only global variables, not functions.
96 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
100 // Enforce -mlocal-sdata.
101 if (!LocalSData && GV->hasLocalLinkage())
104 // Enforce -mextern-sdata.
105 if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
106 GV->hasCommonLinkage()))
109 Type *Ty = GV->getType()->getElementType();
110 return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
113 const MCSection *MipsTargetObjectFile::
114 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
115 Mangler &Mang, const TargetMachine &TM) const {
116 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
119 // Handle Small Section classification here.
120 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
121 return SmallBSSSection;
122 if (Kind.isDataRel() && IsGlobalInSmallSection(GV, TM, Kind))
123 return SmallDataSection;
125 // Otherwise, we work the same as ELF.
126 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
129 /// Return true if this constant should be placed into small data section.
130 bool MipsTargetObjectFile::
131 IsConstantInSmallSection(const Constant *CN, const TargetMachine &TM) const {
132 return (static_cast<const MipsTargetMachine &>(TM)
134 ->useSmallSection() &&
135 LocalSData && IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(
139 const MCSection *MipsTargetObjectFile::
140 getSectionForConstant(SectionKind Kind, const Constant *C) const {
141 if (IsConstantInSmallSection(C, *TM))
142 return SmallDataSection;
144 // Otherwise, we work the same as ELF.
145 return TargetLoweringObjectFileELF::getSectionForConstant(Kind, C);