blob: 0852b5a18c68f534921905b178239eec1a41b15b [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
Chris Lattner68535f72009-08-13 06:28:06 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner68535f72009-08-13 06:28:06 +00006//
Akira Hatanakae2489122011-04-15 21:51:11 +00007//===----------------------------------------------------------------------===//
Chris Lattner68535f72009-08-13 06:28:06 +00008
9#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000010#include "MipsSubtarget.h"
Eric Christopher948bdf92015-03-21 03:13:05 +000011#include "MipsTargetMachine.h"
Simon Atanasyanf76884b2018-12-03 21:54:43 +000012#include "MCTargetDesc/MipsMCExpr.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/ELF.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/DerivedTypes.h"
16#include "llvm/IR/GlobalVariable.h"
Chris Lattner80c34592010-04-08 21:34:17 +000017#include "llvm/MC/MCContext.h"
Chris Lattner68535f72009-08-13 06:28:06 +000018#include "llvm/MC/MCSectionELF.h"
Chris Lattner68535f72009-08-13 06:28:06 +000019#include "llvm/Support/CommandLine.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
Simon Dardis0310eb72017-07-21 17:19:00 +000039static cl::opt<bool>
40EmbeddedData("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
Chris Lattner68535f72009-08-13 06:28:06 +000045void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
46 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Logan Chieneeaaf652012-09-05 06:17:17 +000047 InitializeELF(TM.Options.UseInitArray);
Che-Liang Chioubb033892010-09-28 09:55:24 +000048
Rafael Espindolaba31e272015-01-29 17:33:21 +000049 SmallDataSection = getContext().getELFSection(
Simon Atanasyane7741262016-02-03 11:50:22 +000050 ".sdata", ELF::SHT_PROGBITS,
51 ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
Che-Liang Chioubb033892010-09-28 09:55:24 +000052
Rafael Espindolaba31e272015-01-29 17:33:21 +000053 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
Simon Atanasyane7741262016-02-03 11:50:22 +000054 ELF::SHF_WRITE | ELF::SHF_ALLOC |
55 ELF::SHF_MIPS_GPREL);
Daniel Sanders8ef465f2015-05-27 08:44:01 +000056 this->TM = &static_cast<const MipsTargetMachine &>(TM);
Chris Lattner68535f72009-08-13 06:28:06 +000057}
58
Che-Liang Chioubb033892010-09-28 09:55:24 +000059// 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
Chris Lattner68535f72009-08-13 06:28:06 +000061// gp_rel operator.
62static bool IsInSmallSection(uint64_t Size) {
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000063 // gcc has traditionally not treated zero-sized objects as small data, so this
64 // is effectively part of the ABI.
Chris Lattner68535f72009-08-13 06:28:06 +000065 return Size > 0 && Size <= SSThreshold;
66}
67
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000068/// Return true if this global address should be placed into small data/bss
69/// section.
Peter Collingbourne67335642016-10-24 19:23:39 +000070bool MipsTargetObjectFile::IsGlobalInSmallSection(
71 const GlobalObject *GO, const TargetMachine &TM) const {
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000072 // We first check the case where global is a declaration, because finding
73 // section kind using getKindForGlobal() is only allowed for global
74 // definitions.
Peter Collingbourne67335642016-10-24 19:23:39 +000075 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
76 return IsGlobalInSmallSectionImpl(GO, TM);
Che-Liang Chioubb033892010-09-28 09:55:24 +000077
Peter Collingbourne67335642016-10-24 19:23:39 +000078 return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
Chris Lattner68535f72009-08-13 06:28:06 +000079}
80
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000081/// Return true if this global address should be placed into small data/bss
82/// section.
Chris Lattner68535f72009-08-13 06:28:06 +000083bool MipsTargetObjectFile::
Peter Collingbourne67335642016-10-24 19:23:39 +000084IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
Chris Lattner68535f72009-08-13 06:28:06 +000085 SectionKind Kind) const {
Simon Dardis0310eb72017-07-21 17:19:00 +000086 return IsGlobalInSmallSectionImpl(GO, TM) &&
87 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
88 Kind.isReadOnly());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000089}
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000090
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000091/// 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.
94bool MipsTargetObjectFile::
Peter Collingbourne67335642016-10-24 19:23:39 +000095IsGlobalInSmallSectionImpl(const GlobalObject *GO,
Sasa Stankovicb38db1e2014-11-06 13:20:12 +000096 const TargetMachine &TM) const {
Eric Christopher948bdf92015-03-21 03:13:05 +000097 const MipsSubtarget &Subtarget =
98 *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
Akira Hatanakaad495022012-08-22 03:18:13 +000099
100 // Return if small section is not available.
101 if (!Subtarget.useSmallSection())
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +0000102 return false;
103
Chris Lattner68535f72009-08-13 06:28:06 +0000104 // Only global variables, not functions.
Peter Collingbourne67335642016-10-24 19:23:39 +0000105 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
Chris Lattner68535f72009-08-13 06:28:06 +0000106 if (!GVA)
107 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000108
Simon Dardis9c5d64b2017-08-16 12:18:04 +0000109 // 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();
113
114 // Explicitly placing any variable in the small data section overrides
115 // the global -G value.
116 if (Section == ".sdata" || Section == ".sbss")
117 return true;
118
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.
122 return false;
123 }
124
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000125 // Enforce -mlocal-sdata.
Peter Collingbourne67335642016-10-24 19:23:39 +0000126 if (!LocalSData && GVA->hasLocalLinkage())
Chris Lattner68535f72009-08-13 06:28:06 +0000127 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000128
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000129 // Enforce -mextern-sdata.
Peter Collingbourne67335642016-10-24 19:23:39 +0000130 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
131 GVA->hasCommonLinkage()))
Chris Lattner68535f72009-08-13 06:28:06 +0000132 return false;
133
Simon Dardis0310eb72017-07-21 17:19:00 +0000134 // Enforce -membedded-data.
135 if (EmbeddedData && GVA->isConstant())
136 return false;
137
Peter Collingbourne67335642016-10-24 19:23:39 +0000138 Type *Ty = GVA->getValueType();
Alexander Richardson1f9636f2018-01-26 15:56:14 +0000139
140 // It is possible that the type of the global is unsized, i.e. a declaration
141 // of a extern struct. In this case don't presume it is in the small data
142 // section. This happens e.g. when building the FreeBSD kernel.
143 if (!Ty->isSized())
144 return false;
145
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000146 return IsInSmallSection(
Peter Collingbourne67335642016-10-24 19:23:39 +0000147 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
Chris Lattner68535f72009-08-13 06:28:06 +0000148}
149
Eric Christopher4367c7f2016-09-16 07:33:15 +0000150MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000151 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Chris Lattner68535f72009-08-13 06:28:06 +0000152 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
153 // sections?
Che-Liang Chioubb033892010-09-28 09:55:24 +0000154
Chris Lattner68535f72009-08-13 06:28:06 +0000155 // Handle Small Section classification here.
Peter Collingbourne67335642016-10-24 19:23:39 +0000156 if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
Chris Lattner68535f72009-08-13 06:28:06 +0000157 return SmallBSSSection;
Peter Collingbourne67335642016-10-24 19:23:39 +0000158 if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
Chris Lattner68535f72009-08-13 06:28:06 +0000159 return SmallDataSection;
Simon Dardis0310eb72017-07-21 17:19:00 +0000160 if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
161 return SmallDataSection;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000162
Chris Lattner68535f72009-08-13 06:28:06 +0000163 // Otherwise, we work the same as ELF.
Peter Collingbourne67335642016-10-24 19:23:39 +0000164 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Chris Lattner68535f72009-08-13 06:28:06 +0000165}
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000166
167/// Return true if this constant should be placed into small data section.
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000168bool MipsTargetObjectFile::IsConstantInSmallSection(
169 const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
Eric Christopher948bdf92015-03-21 03:13:05 +0000170 return (static_cast<const MipsTargetMachine &>(TM)
171 .getSubtargetImpl()
172 ->useSmallSection() &&
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000173 LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000174}
175
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000176/// Return true if this constant should be placed into small data section.
David Majnemer78f46be2016-02-21 01:40:04 +0000177MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
178 SectionKind Kind,
179 const Constant *C,
180 unsigned &Align) const {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000181 if (IsConstantInSmallSection(DL, C, *TM))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000182 return SmallDataSection;
183
184 // Otherwise, we work the same as ELF.
David Majnemer78f46be2016-02-21 01:40:04 +0000185 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +0000186}
Petar Jovanovicdbb39352017-01-20 17:53:30 +0000187
188const MCExpr *
189MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
190 const MCExpr *Expr =
191 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Simon Atanasyanf76884b2018-12-03 21:54:43 +0000192 Expr = MCBinaryExpr::createAdd(
Petar Jovanovicdbb39352017-01-20 17:53:30 +0000193 Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
Simon Atanasyanf76884b2018-12-03 21:54:43 +0000194 return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());
Petar Jovanovicdbb39352017-01-20 17:53:30 +0000195}