blob: 79df9a05148418f4faf67e6fa4d95173f5fc74f4 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
Chris Lattner68535f72009-08-13 06:28:06 +00002//
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//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Chris Lattner68535f72009-08-13 06:28:06 +00009
10#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000011#include "MipsSubtarget.h"
Eric Christopher948bdf92015-03-21 03:13:05 +000012#include "MipsTargetMachine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/DataLayout.h"
14#include "llvm/IR/DerivedTypes.h"
15#include "llvm/IR/GlobalVariable.h"
Chris Lattner80c34592010-04-08 21:34:17 +000016#include "llvm/MC/MCContext.h"
Chris Lattner68535f72009-08-13 06:28:06 +000017#include "llvm/MC/MCSectionELF.h"
Chris Lattner68535f72009-08-13 06:28:06 +000018#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner68535f72009-08-13 06:28:06 +000021using namespace llvm;
22
23static cl::opt<unsigned>
24SSThreshold("mips-ssection-threshold", cl::Hidden,
25 cl::desc("Small data and bss section threshold size (default=8)"),
26 cl::init(8));
27
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000028static cl::opt<bool>
29LocalSData("mlocal-sdata", cl::Hidden,
30 cl::desc("MIPS: Use gp_rel for object-local data."),
31 cl::init(true));
32
33static cl::opt<bool>
34ExternSData("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
Chris Lattner68535f72009-08-13 06:28:06 +000039void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
40 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Logan Chieneeaaf652012-09-05 06:17:17 +000041 InitializeELF(TM.Options.UseInitArray);
Che-Liang Chioubb033892010-09-28 09:55:24 +000042
Rafael Espindolaba31e272015-01-29 17:33:21 +000043 SmallDataSection = getContext().getELFSection(
44 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
Che-Liang Chioubb033892010-09-28 09:55:24 +000045
Rafael Espindolaba31e272015-01-29 17:33:21 +000046 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
47 ELF::SHF_WRITE | ELF::SHF_ALLOC);
Daniel Sanders8ef465f2015-05-27 08:44:01 +000048 this->TM = &static_cast<const MipsTargetMachine &>(TM);
Chris Lattner68535f72009-08-13 06:28:06 +000049}
50
Che-Liang Chioubb033892010-09-28 09:55:24 +000051// 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
Chris Lattner68535f72009-08-13 06:28:06 +000053// gp_rel operator.
54static bool IsInSmallSection(uint64_t Size) {
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000055 // gcc has traditionally not treated zero-sized objects as small data, so this
56 // is effectively part of the ABI.
Chris Lattner68535f72009-08-13 06:28:06 +000057 return Size > 0 && Size <= SSThreshold;
58}
59
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000060/// Return true if this global address should be placed into small data/bss
61/// section.
62bool MipsTargetObjectFile::
63IsGlobalInSmallSection(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
66 // definitions.
Chris Lattner68535f72009-08-13 06:28:06 +000067 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000068 return IsGlobalInSmallSectionImpl(GV, TM);
Che-Liang Chioubb033892010-09-28 09:55:24 +000069
Chris Lattner68535f72009-08-13 06:28:06 +000070 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
71}
72
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000073/// Return true if this global address should be placed into small data/bss
74/// section.
Chris Lattner68535f72009-08-13 06:28:06 +000075bool MipsTargetObjectFile::
76IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
77 SectionKind Kind) const {
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000078 return (IsGlobalInSmallSectionImpl(GV, TM) &&
Rafael Espindola449711c2015-11-18 06:02:15 +000079 (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000080}
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000081
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000082/// 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
84/// kind.
85bool MipsTargetObjectFile::
86IsGlobalInSmallSectionImpl(const GlobalValue *GV,
87 const TargetMachine &TM) const {
Eric Christopher948bdf92015-03-21 03:13:05 +000088 const MipsSubtarget &Subtarget =
89 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
Akira Hatanakaad495022012-08-22 03:18:13 +000090
91 // Return if small section is not available.
92 if (!Subtarget.useSmallSection())
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000093 return false;
94
Chris Lattner68535f72009-08-13 06:28:06 +000095 // Only global variables, not functions.
96 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
97 if (!GVA)
98 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +000099
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000100 // Enforce -mlocal-sdata.
101 if (!LocalSData && GV->hasLocalLinkage())
Chris Lattner68535f72009-08-13 06:28:06 +0000102 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000103
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000104 // Enforce -mextern-sdata.
105 if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
106 GV->hasCommonLinkage()))
Chris Lattner68535f72009-08-13 06:28:06 +0000107 return false;
108
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000109 Type *Ty = GV->getValueType();
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000110 return IsInSmallSection(
111 GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
Chris Lattner68535f72009-08-13 06:28:06 +0000112}
113
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000114MCSection *
115MipsTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
116 SectionKind Kind, Mangler &Mang,
117 const TargetMachine &TM) const {
Chris Lattner68535f72009-08-13 06:28:06 +0000118 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
119 // sections?
Che-Liang Chioubb033892010-09-28 09:55:24 +0000120
Chris Lattner68535f72009-08-13 06:28:06 +0000121 // Handle Small Section classification here.
122 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
123 return SmallBSSSection;
Rafael Espindola449711c2015-11-18 06:02:15 +0000124 if (Kind.isData() && IsGlobalInSmallSection(GV, TM, Kind))
Chris Lattner68535f72009-08-13 06:28:06 +0000125 return SmallDataSection;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000126
Chris Lattner68535f72009-08-13 06:28:06 +0000127 // Otherwise, we work the same as ELF.
Akira Hatanakae2489122011-04-15 21:51:11 +0000128 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattner68535f72009-08-13 06:28:06 +0000129}
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000130
131/// Return true if this constant should be placed into small data section.
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000132bool MipsTargetObjectFile::IsConstantInSmallSection(
133 const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
Eric Christopher948bdf92015-03-21 03:13:05 +0000134 return (static_cast<const MipsTargetMachine &>(TM)
135 .getSubtargetImpl()
136 ->useSmallSection() &&
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000137 LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000138}
139
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000140/// Return true if this constant should be placed into small data section.
141MCSection *MipsTargetObjectFile::getSectionForConstant(
142 const DataLayout &DL, SectionKind Kind, const Constant *C) const {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000143 if (IsConstantInSmallSection(DL, C, *TM))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000144 return SmallDataSection;
145
146 // Otherwise, we work the same as ELF.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000147 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000148}