Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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 "MCTargetDesc/MipsMCExpr.h"
11#include "MipsSubtarget.h"
12#include "MipsTargetMachine.h"
13#include "llvm/BinaryFormat/ELF.h"
14#include "llvm/IR/DataLayout.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/Target/TargetMachine.h"
20using namespacellvm;
21
22staticcl::opt<unsigned>
23SSThreshold("mips-ssection-threshold",cl::Hidden,
24cl::desc("Small data and bss section threshold size (default=8)"),
25cl::init(8));
26
27staticcl::opt<bool>
28LocalSData("mlocal-sdata",cl::Hidden,
29cl::desc("MIPS: Use gp_rel for object-local data."),
30cl::init(true));
31
32staticcl::opt<bool>
33ExternSData("mextern-sdata",cl::Hidden,
34cl::desc("MIPS: Use gp_rel for data that is not defined by the "
35"current object."),
36cl::init(true));
37
38staticcl::opt<bool>
39EmbeddedData("membedded-data",cl::Hidden,
40cl::desc("MIPS: Try to allocate variables in the following"
41" sections if possible: .rodata, .sdata, .data ."),
42cl::init(false));
43
44voidMipsTargetObjectFile::Initialize(MCContext &Ctx,constTargetMachine &TM){
45TargetLoweringObjectFileELF::Initialize(Ctx, TM);
46
47 SmallDataSection =getContext().getELFSection(
48".sdata",ELF::SHT_PROGBITS,
49ELF::SHF_WRITE |ELF::SHF_ALLOC |ELF::SHF_MIPS_GPREL);
50
51 SmallBSSSection =getContext().getELFSection(".sbss",ELF::SHT_NOBITS,
52ELF::SHF_WRITE |ELF::SHF_ALLOC |
53ELF::SHF_MIPS_GPREL);
54 this->TM = &static_cast<constMipsTargetMachine &>(TM);
55}
56
57// A address must be loaded from a small section if its size is less than the
58// small section size threshold. Data in this section must be addressed using
59// gp_rel operator.
60staticboolIsInSmallSection(uint64_tSize) {
61// gcc has traditionally not treated zero-sized objects as small data, so this
62// is effectively part of the ABI.
63returnSize > 0 &&Size <=SSThreshold;
64}
65
66/// Return true if this global address should be placed into small data/bss
67/// section.
68bool MipsTargetObjectFile::IsGlobalInSmallSection(
69constGlobalObject *GO,constTargetMachine &TM) const{
70// We first check the case where global is a declaration, because finding
71// section kind using getKindForGlobal() is only allowed for global
72// definitions.
73if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
74return IsGlobalInSmallSectionImpl(GO, TM);
75
76return IsGlobalInSmallSection(GO, TM,getKindForGlobal(GO, TM));
77}
78
79/// Return true if this global address should be placed into small data/bss
80/// section.
81bool MipsTargetObjectFile::
82IsGlobalInSmallSection(constGlobalObject *GO,constTargetMachine &TM,
83SectionKind Kind) const{
84return IsGlobalInSmallSectionImpl(GO, TM) &&
85 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
86 Kind.isReadOnly());
87}
88
89/// Return true if this global address should be placed into small data/bss
90/// section. This method does all the work, except for checking the section
91/// kind.
92bool MipsTargetObjectFile::
93IsGlobalInSmallSectionImpl(constGlobalObject *GO,
94constTargetMachine &TM) const{
95constMipsSubtarget &Subtarget =
96 *static_cast<constMipsTargetMachine &>(TM).getSubtargetImpl();
97
98// Return if small section is not available.
99if (!Subtarget.useSmallSection())
100returnfalse;
101
102// Only global variables, not functions.
103constGlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
104if (!GVA)
105returnfalse;
106
107// If the variable has an explicit section, it is placed in that section but
108// it's addressing mode may change.
109if (GVA->hasSection()) {
110StringRef Section = GVA->getSection();
111
112// Explicitly placing any variable in the small data section overrides
113// the global -G value.
114if (Section ==".sdata" || Section ==".sbss")
115returntrue;
116
117// Otherwise reject accessing it through the gp pointer. There are some
118// historic cases which GCC doesn't appear to respect any more. These
119// are .lit4, .lit8 and .srdata. For the moment reject these as well.
120returnfalse;
121 }
122
123// Enforce -mlocal-sdata.
124if (!LocalSData && GVA->hasLocalLinkage())
125returnfalse;
126
127// Enforce -mextern-sdata.
128if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
129 GVA->hasCommonLinkage()))
130returnfalse;
131
132// Enforce -membedded-data.
133if (EmbeddedData && GVA->isConstant())
134returnfalse;
135
136Type *Ty = GVA->getValueType();
137
138// It is possible that the type of the global is unsized, i.e. a declaration
139// of a extern struct. In this case don't presume it is in the small data
140// section. This happens e.g. when building the FreeBSD kernel.
141if (!Ty->isSized())
142returnfalse;
143
144returnIsInSmallSection(
145 GVA->getDataLayout().getTypeAllocSize(Ty));
146}
147
148MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
149constGlobalObject *GO,SectionKind Kind,constTargetMachine &TM) const{
150// TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
151// sections?
152
153// Handle Small Section classification here.
154if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
155return SmallBSSSection;
156if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
157return SmallDataSection;
158if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
159return SmallDataSection;
160
161// Otherwise, we work the same as ELF.
162returnTargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
163}
164
165/// Return true if this constant should be placed into small data section.
166boolMipsTargetObjectFile::IsConstantInSmallSection(
167constDataLayout &DL,constConstant *CN,constTargetMachine &TM) const{
168return (static_cast<constMipsTargetMachine &>(TM)
169 .getSubtargetImpl()
170 ->useSmallSection() &&
171LocalSData &&IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
172}
173
174/// Return true if this constant should be placed into small data section.
175MCSection *MipsTargetObjectFile::getSectionForConstant(constDataLayout &DL,
176SectionKind Kind,
177constConstant *C,
178Align &Alignment) const{
179if (IsConstantInSmallSection(DL,C, *TM))
180return SmallDataSection;
181
182// Otherwise, we work the same as ELF.
183returnTargetLoweringObjectFileELF::getSectionForConstant(DL, Kind,C,
184 Alignment);
185}
186
187constMCExpr *
188MipsTargetObjectFile::getDebugThreadLocalSymbol(constMCSymbol *Sym) const{
189constMCExpr *Expr =
190MCSymbolRefExpr::create(Sym,MCSymbolRefExpr::VK_None,getContext());
191 Expr =MCBinaryExpr::createAdd(
192 Expr,MCConstantExpr::create(0x8000,getContext()),getContext());
193returnMipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr,getContext());
194}
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
ELF.h
CommandLine.h
DataLayout.h
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Sym
Symbol * Sym
Definition:ELF_riscv.cpp:479
GlobalVariable.h
SSThreshold
static cl::opt< unsigned > SSThreshold("lanai-ssection-threshold", cl::Hidden, cl::desc("Small data and bss section threshold size (default=0)"), cl::init(0))
MCContext.h
MCSectionELF.h
MipsMCExpr.h
MipsSubtarget.h
MipsTargetMachine.h
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))
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))
LocalSData
static cl::opt< bool > LocalSData("mlocal-sdata", cl::Hidden, cl::desc("MIPS: Use gp_rel for object-local data."), cl::init(true))
IsInSmallSection
static bool IsInSmallSection(uint64_t Size)
Definition:MipsTargetObjectFile.cpp:60
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))
MipsTargetObjectFile.h
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition:DataLayout.h:63
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:457
llvm::GlobalObject
Definition:GlobalObject.h:27
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition:GlobalObject.h:117
llvm::GlobalObject::hasSection
bool hasSection() const
Check if this global has a custom object file section.
Definition:GlobalObject.h:109
llvm::GlobalValue::hasExternalLinkage
bool hasExternalLinkage() const
Definition:GlobalValue.h:512
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:296
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition:GlobalValue.h:529
llvm::GlobalValue::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition:Globals.cpp:130
llvm::GlobalValue::hasCommonLinkage
bool hasCommonLinkage() const
Definition:GlobalValue.h:533
llvm::GlobalValue::hasAvailableExternallyLinkage
bool hasAvailableExternallyLinkage() const
Definition:GlobalValue.h:513
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition:GlobalValue.h:297
llvm::GlobalVariable
Definition:GlobalVariable.h:39
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:173
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition:MCExpr.h:537
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition:MCExpr.cpp:222
llvm::MCContext
Context object for machine code objects.
Definition:MCContext.h:83
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition:MCContext.h:551
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition:MCExpr.h:34
llvm::MCObjectFileInfo::getContext
MCContext & getContext() const
Definition:MCObjectFileInfo.h:252
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition:MCSection.h:36
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition:MCExpr.h:195
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition:MCExpr.h:398
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition:MCSymbol.h:41
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:23
llvm::MipsSubtarget
Definition:MipsSubtarget.h:37
llvm::MipsSubtarget::useSmallSection
bool useSmallSection() const
Definition:MipsSubtarget.h:336
llvm::MipsTargetMachine
Definition:MipsTargetMachine.h:27
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:175
llvm::MipsTargetObjectFile::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition:MipsTargetObjectFile.cpp:148
llvm::MipsTargetObjectFile::getDebugThreadLocalSymbol
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
Definition:MipsTargetObjectFile.cpp:188
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:166
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:44
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition:SectionKind.h:22
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
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:121
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:1027
llvm::TargetLoweringObjectFileELF::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition:TargetLoweringObjectFileImpl.cpp:933
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:199
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition:TargetMachine.h:77
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
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:310
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::cl::opt
Definition:CommandLine.h:1423
uint64_t
TargetMachine.h
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition:ELF.h:1098
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition:ELF.h:1105
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition:ELF.h:1198
llvm::ELF::SHF_MIPS_GPREL
@ SHF_MIPS_GPREL
Definition:ELF.h:1281
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition:ELF.h:1195
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition:Alignment.h:39
llvm::cl::desc
Definition:CommandLine.h:409

Generated on Fri Jul 18 2025 14:09:39 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp