LLVM  13.0.0git
MipsTargetObjectFile.cpp
Go to the documentation of this file.
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 "MipsSubtarget.h"
11 #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"
21 using namespace llvm;
22 
23 static cl::opt<unsigned>
24 SSThreshold("mips-ssection-threshold", cl::Hidden,
25  cl::desc("Small data and bss section threshold size (default=8)"),
26  cl::init(8));
27 
28 static cl::opt<bool>
29 LocalSData("mlocal-sdata", cl::Hidden,
30  cl::desc("MIPS: Use gp_rel for object-local data."),
31  cl::init(true));
32 
33 static cl::opt<bool>
34 ExternSData("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 
39 static cl::opt<bool>
40 EmbeddedData("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 
47 
48  SmallDataSection = getContext().getELFSection(
49  ".sdata", ELF::SHT_PROGBITS,
51 
52  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
55  this->TM = &static_cast<const MipsTargetMachine &>(TM);
56 }
57 
58 // A address must be loaded from a small section if its size is less than the
59 // small section size threshold. Data in this section must be addressed using
60 // gp_rel operator.
61 static bool IsInSmallSection(uint64_t Size) {
62  // gcc has traditionally not treated zero-sized objects as small data, so this
63  // is effectively part of the ABI.
64  return Size > 0 && Size <= SSThreshold;
65 }
66 
67 /// Return true if this global address should be placed into small data/bss
68 /// section.
69 bool MipsTargetObjectFile::IsGlobalInSmallSection(
70  const GlobalObject *GO, const TargetMachine &TM) const {
71  // We first check the case where global is a declaration, because finding
72  // section kind using getKindForGlobal() is only allowed for global
73  // definitions.
75  return IsGlobalInSmallSectionImpl(GO, TM);
76 
77  return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
78 }
79 
80 /// Return true if this global address should be placed into small data/bss
81 /// section.
82 bool MipsTargetObjectFile::
83 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
84  SectionKind Kind) const {
85  return IsGlobalInSmallSectionImpl(GO, TM) &&
86  (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
87  Kind.isReadOnly());
88 }
89 
90 /// Return true if this global address should be placed into small data/bss
91 /// section. This method does all the work, except for checking the section
92 /// kind.
93 bool MipsTargetObjectFile::
94 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
95  const TargetMachine &TM) const {
96  const MipsSubtarget &Subtarget =
97  *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
98 
99  // Return if small section is not available.
100  if (!Subtarget.useSmallSection())
101  return false;
102 
103  // Only global variables, not functions.
104  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
105  if (!GVA)
106  return false;
107 
108  // If the variable has an explicit section, it is placed in that section but
109  // it's addressing mode may change.
110  if (GVA->hasSection()) {
111  StringRef Section = GVA->getSection();
112 
113  // Explicitly placing any variable in the small data section overrides
114  // the global -G value.
115  if (Section == ".sdata" || Section == ".sbss")
116  return true;
117 
118  // Otherwise reject accessing it through the gp pointer. There are some
119  // historic cases which GCC doesn't appear to respect any more. These
120  // are .lit4, .lit8 and .srdata. For the moment reject these as well.
121  return false;
122  }
123 
124  // Enforce -mlocal-sdata.
125  if (!LocalSData && GVA->hasLocalLinkage())
126  return false;
127 
128  // Enforce -mextern-sdata.
129  if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
130  GVA->hasCommonLinkage()))
131  return false;
132 
133  // Enforce -membedded-data.
134  if (EmbeddedData && GVA->isConstant())
135  return false;
136 
137  Type *Ty = GVA->getValueType();
138 
139  // It is possible that the type of the global is unsized, i.e. a declaration
140  // of a extern struct. In this case don't presume it is in the small data
141  // section. This happens e.g. when building the FreeBSD kernel.
142  if (!Ty->isSized())
143  return false;
144 
145  return IsInSmallSection(
147 }
148 
150  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
151  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
152  // sections?
153 
154  // Handle Small Section classification here.
155  if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
156  return SmallBSSSection;
157  if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
158  return SmallDataSection;
159  if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
160  return SmallDataSection;
161 
162  // Otherwise, we work the same as ELF.
164 }
165 
166 /// Return true if this constant should be placed into small data section.
168  const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
169  return (static_cast<const MipsTargetMachine &>(TM)
170  .getSubtargetImpl()
171  ->useSmallSection() &&
172  LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
173 }
174 
175 /// Return true if this constant should be placed into small data section.
178  const Constant *C,
179  Align &Alignment) const {
181  return SmallDataSection;
182 
183  // Otherwise, we work the same as ELF.
185  Alignment);
186 }
187 
188 const MCExpr *
190  const MCExpr *Expr =
193  Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
195 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::MipsTargetMachine
Definition: MipsTargetMachine.h:27
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:272
ExternSData
static cl::opt< bool > ExternSData("mextern-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for data that is not defined by the " "current object."), cl::init(true))
llvm
Definition: AllocatorList.h:23
llvm::MipsTargetObjectFile::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: MipsTargetObjectFile.cpp:45
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::GlobalValue::hasCommonLinkage
bool hasCommonLinkage() const
Definition: GlobalValue.h:449
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::TargetLoweringObjectFileELF::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: TargetLoweringObjectFileImpl.cpp:870
llvm::GlobalValue::hasExternalLinkage
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
MipsTargetObjectFile.h
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:112
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
MCSectionELF.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::MipsTargetObjectFile::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: MipsTargetObjectFile.cpp:149
IsInSmallSection
static bool IsInSmallSection(uint64_t Size)
Definition: MipsTargetObjectFile.cpp:61
LocalSData
static cl::opt< bool > LocalSData("mlocal-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for object-local data."), cl::init(true))
MipsTargetMachine.h
llvm::MCObjectFileInfo::getContext
MCContext & getContext() const
Definition: MCObjectFileInfo.h:232
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:979
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:911
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
CommandLine.h
ELF.h
TargetMachine.h
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
llvm::ELF::SHF_MIPS_GPREL
@ SHF_MIPS_GPREL
Definition: ELF.h:1062
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalObject
Definition: GlobalObject.h:28
MCContext.h
SSThreshold
static cl::opt< unsigned > SSThreshold("mips-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=8)"), cl::init(8))
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:537
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
MipsMCExpr.h
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::TargetLoweringObjectFile::getKindForGlobal
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Definition: TargetLoweringObjectFile.cpp:202
llvm::MipsTargetObjectFile::getSectionForConstant
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const override
Return true if this constant should be placed into small data section.
Definition: MipsTargetObjectFile.cpp:176
llvm::cl::opt
Definition: CommandLine.h:1422
llvm::GlobalObject::hasSection
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:104
llvm::GlobalValue::hasAvailableExternallyLinkage
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:432
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:904
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
llvm::MipsTargetObjectFile::getDebugThreadLocalSymbol
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
Definition: MipsTargetObjectFile.cpp:189
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:982
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
llvm::TargetLoweringObjectFileELF::getSectionForConstant
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const override
Given a constant with the SectionKind, return a section that it should be placed in.
Definition: TargetLoweringObjectFileImpl.cpp:964
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:523
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::MipsSubtarget
Definition: MipsSubtarget.h:39
llvm::MipsMCExpr::create
static const MipsMCExpr * create(MipsExprKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: MipsMCExpr.cpp:27
llvm::MipsMCExpr::MEK_DTPREL
@ MEK_DTPREL
Definition: MipsMCExpr.h:24
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
GlobalVariable.h
llvm::TargetLoweringObjectFileELF::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: TargetLoweringObjectFileImpl.cpp:115
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:384
llvm::MipsSubtarget::useSmallSection
bool useSmallSection() const
Definition: MipsSubtarget.h:329
EmbeddedData
static cl::opt< bool > EmbeddedData("membedded-data", cl::Hidden, cl::desc("MIPS: Try to allocate variables in the following" " sections if possible: .rodata, .sdata, .data ."), cl::init(false))
llvm::GlobalVariable::isConstant
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Definition: GlobalVariable.h:153
MipsSubtarget.h
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:397
DerivedTypes.h
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::cl::desc
Definition: CommandLine.h:414
llvm::MipsTargetObjectFile::IsConstantInSmallSection
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const
Return true if this constant should be placed into small data section.
Definition: MipsTargetObjectFile.cpp:167
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::DataLayout::getTypeAllocSize
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:498