1//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "MipsTargetObjectFile.h"
10#include "MCTargetDesc/MipsMCAsmInfo.h"
11#include "MipsSubtarget.h"
12#include "llvm/BinaryFormat/ELF.h"
13#include "llvm/IR/DataLayout.h"
14#include "llvm/IR/GlobalVariable.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCSectionELF.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Target/TargetMachine.h"
19using namespace llvm;
20
21static cl::opt<unsigned>
22SSThreshold("mips-ssection-threshold", cl::Hidden,
23 cl::desc("Small data and bss section threshold size (default=8)"),
24 cl::init(Val: 8));
25
26static cl::opt<bool>
27LocalSData("mlocal-sdata", cl::Hidden,
28 cl::desc("MIPS: Use gp_rel for object-local data."),
29 cl::init(Val: true));
30
31static cl::opt<bool>
32ExternSData("mextern-sdata", cl::Hidden,
33 cl::desc("MIPS: Use gp_rel for data that is not defined by the "
34 "current object."),
35 cl::init(Val: true));
36
37static cl::opt<bool>
38EmbeddedData("membedded-data", cl::Hidden,
39 cl::desc("MIPS: Try to allocate variables in the following"
40 " sections if possible: .rodata, .sdata, .data ."),
41 cl::init(Val: false));
42
43void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
44 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
45
46 SmallDataSection = getContext().getELFSection(
47 Section: ".sdata", Type: ELF::SHT_PROGBITS,
48 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
49
50 SmallBSSSection = getContext().getELFSection(Section: ".sbss", Type: ELF::SHT_NOBITS,
51 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC |
52 ELF::SHF_MIPS_GPREL);
53}
54
55// A address must be loaded from a small section if its size is less than the
56// small section size threshold. Data in this section must be addressed using
57// gp_rel operator.
58static bool IsInSmallSection(uint64_t Size) {
59 // gcc has traditionally not treated zero-sized objects as small data, so this
60 // is effectively part of the ABI.
61 return Size > 0 && Size <= SSThreshold;
62}
63
64/// Return true if this global address should be placed into small data/bss
65/// section.
66bool MipsTargetObjectFile::IsGlobalInSmallSection(
67 const GlobalObject *GO) const {
68 // We first check the case where global is a declaration, because finding
69 // section kind using getKindForGlobal() is only allowed for global
70 // definitions.
71 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
72 return IsGlobalInSmallSectionImpl(GO);
73
74 return IsGlobalInSmallSection(GO, Kind: getKindForGlobal(GO, TM: *TM));
75}
76
77/// Return true if this global address should be placed into small data/bss
78/// section.
79bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalObject *GO,
80 SectionKind Kind) const {
81 return IsGlobalInSmallSectionImpl(GO) &&
82 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
83 Kind.isReadOnly());
84}
85
86/// Return true if this global address should be placed into small data/bss
87/// section. This method does all the work, except for checking the section
88/// kind.
89bool MipsTargetObjectFile::IsGlobalInSmallSectionImpl(
90 const GlobalObject *GO) const {
91 // Only global variables, not functions.
92 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(Val: GO);
93 if (!GVA)
94 return false;
95
96 // If the variable has an explicit section, it is placed in that section but
97 // it's addressing mode may change.
98 if (GVA->hasSection()) {
99 StringRef Section = GVA->getSection();
100
101 // Explicitly placing any variable in the small data section overrides
102 // the global -G value.
103 if (Section == ".sdata" || Section == ".sbss")
104 return true;
105
106 // Otherwise reject accessing it through the gp pointer. There are some
107 // historic cases which GCC doesn't appear to respect any more. These
108 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
109 return false;
110 }
111
112 // Implicit global placement follows the default subtarget, independently of
113 // the features of the functions that reference the global. Explicit small
114 // sections above also allow GP-relative accesses from functions that enable
115 // them when the default subtarget does not.
116 if (!UseSmallSection)
117 return false;
118
119 // Enforce -mlocal-sdata.
120 if (!LocalSData && GVA->hasLocalLinkage())
121 return false;
122
123 // Enforce -mextern-sdata.
124 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
125 GVA->hasCommonLinkage()))
126 return false;
127
128 // Enforce -membedded-data.
129 if (EmbeddedData && GVA->isConstant())
130 return false;
131
132 Type *Ty = GVA->getValueType();
133
134 // It is possible that the type of the global is unsized, i.e. a declaration
135 // of a extern struct. In this case don't presume it is in the small data
136 // section. This happens e.g. when building the FreeBSD kernel.
137 if (!Ty->isSized())
138 return false;
139
140 return IsInSmallSection(
141 Size: GVA->getDataLayout().getTypeAllocSize(Ty));
142}
143
144MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
145 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
146 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
147 // sections?
148
149 // Handle Small Section classification here.
150 if (IsGlobalInSmallSection(GO, Kind)) {
151 if (Kind.isBSS())
152 return SmallBSSSection;
153 if (Kind.isData() || Kind.isReadOnly())
154 return SmallDataSection;
155 }
156
157 // Otherwise, we work the same as ELF.
158 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
159}
160
161/// Return true if this constant should be placed into small data section.
162bool MipsTargetObjectFile::IsConstantInSmallSection(const DataLayout &DL,
163 const Constant *CN,
164 const Function *F) const {
165 bool UseSmallSection =
166 F ? TM->getSubtarget<MipsSubtarget>(F: *F).useSmallSection()
167 : this->UseSmallSection;
168 return UseSmallSection && CN && LocalSData &&
169 IsInSmallSection(Size: DL.getTypeAllocSize(Ty: CN->getType()));
170}
171
172/// Return true if this constant should be placed into small data section.
173MCSection *MipsTargetObjectFile::getSectionForConstant(
174 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
175 const Function *F) const {
176 if (IsConstantInSmallSection(DL, CN: C, F))
177 return SmallDataSection;
178
179 // Otherwise, we work the same as ELF.
180 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
181 Alignment, F);
182}
183
184const MCExpr *
185MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
186 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext());
187 Expr = MCBinaryExpr::createAdd(
188 LHS: Expr, RHS: MCConstantExpr::create(Value: 0x8000, Ctx&: getContext()), Ctx&: getContext());
189 return MCSpecifierExpr::create(Expr, S: Mips::S_DTPREL, Ctx&: getContext());
190}
191