LLVM 24.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
11#include "MipsSubtarget.h"
12#include "MipsTargetMachine.h"
14#include "llvm/IR/DataLayout.h"
16#include "llvm/MC/MCContext.h"
19using namespace llvm;
20
33
34// A address must be loaded from a small section if its size is less than the
35// small section size threshold. Data in this section must be addressed using
36// gp_rel operator.
37static bool isInSmallSection(const MipsOptions &CLOpts, uint64_t Size) {
38 // gcc has traditionally not treated zero-sized objects as small data, so this
39 // is effectively part of the ABI.
40 return Size > 0 && Size <= CLOpts.ssection_threshold;
41}
42
43/// Return true if this global address should be placed into small data/bss
44/// section.
45bool MipsTargetObjectFile::IsGlobalInSmallSection(
46 const GlobalObject *GO) const {
47 // We first check the case where global is a declaration, because finding
48 // section kind using getKindForGlobal() is only allowed for global
49 // definitions.
51 return IsGlobalInSmallSectionImpl(GO);
52
53 return IsGlobalInSmallSection(GO, getKindForGlobal(GO, *TM));
54}
55
56/// Return true if this global address should be placed into small data/bss
57/// section.
58bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalObject *GO,
59 SectionKind Kind) const {
60 return IsGlobalInSmallSectionImpl(GO) &&
61 (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
62 Kind.isReadOnly());
63}
64
65/// Return true if this global address should be placed into small data/bss
66/// section. This method does all the work, except for checking the section
67/// kind.
68bool MipsTargetObjectFile::IsGlobalInSmallSectionImpl(
69 const GlobalObject *GO) const {
70 const MipsOptions &CLOpts =
71 static_cast<const MipsTargetMachine *>(TM)->getCLOpts();
72 // Only global variables, not functions.
74 if (!GVA)
75 return false;
76
77 // If the variable has an explicit section, it is placed in that section but
78 // it's addressing mode may change.
79 if (GVA->hasSection()) {
80 StringRef Section = GVA->getSection();
81
82 // Explicitly placing any variable in the small data section overrides
83 // the global -G value.
84 if (Section == ".sdata" || Section == ".sbss")
85 return true;
86
87 // Otherwise reject accessing it through the gp pointer. There are some
88 // historic cases which GCC doesn't appear to respect any more. These
89 // are .lit4, .lit8 and .srdata. For the moment reject these as well.
90 return false;
91 }
92
93 // Implicit global placement follows the default subtarget, independently of
94 // the features of the functions that reference the global. Explicit small
95 // sections above also allow GP-relative accesses from functions that enable
96 // them when the default subtarget does not.
97 if (!UseSmallSection)
98 return false;
99
100 // Enforce -mlocal-sdata.
101 if (!CLOpts.mlocal_sdata && GVA->hasLocalLinkage())
102 return false;
103
104 // Enforce -mextern-sdata.
105 if (!CLOpts.mextern_sdata &&
106 ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
107 GVA->hasCommonLinkage()))
108 return false;
109
110 // Enforce -membedded-data.
111 if (CLOpts.membedded_data && GVA->isConstant())
112 return false;
113
114 Type *Ty = GVA->getValueType();
115
116 // It is possible that the type of the global is unsized, i.e. a declaration
117 // of a extern struct. In this case don't presume it is in the small data
118 // section. This happens e.g. when building the FreeBSD kernel.
119 if (!Ty->isSized())
120 return false;
121
122 return isInSmallSection(CLOpts, GVA->getDataLayout().getTypeAllocSize(Ty));
123}
124
126 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
127 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
128 // sections?
129
130 // Handle Small Section classification here.
131 if (IsGlobalInSmallSection(GO, Kind)) {
132 if (Kind.isBSS())
133 return SmallBSSSection;
134 if (Kind.isData() || Kind.isReadOnly())
135 return SmallDataSection;
136 }
137
138 // Otherwise, we work the same as ELF.
140}
141
142/// Return true if this constant should be placed into small data section.
144 const Constant *CN,
145 const Function *F) const {
146 bool UseSmallSection =
147 F ? TM->getSubtarget<MipsSubtarget>(*F).useSmallSection()
148 : this->UseSmallSection;
149 const MipsOptions &CLOpts =
150 static_cast<const MipsTargetMachine *>(TM)->getCLOpts();
151 return UseSmallSection && CN && CLOpts.mlocal_sdata &&
152 isInSmallSection(CLOpts, DL.getTypeAllocSize(CN->getType()));
153}
154
155/// Return true if this constant should be placed into small data section.
157 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
158 const Function *F) const {
160 return SmallDataSection;
161
162 // Otherwise, we work the same as ELF.
164 Alignment, F);
165}
166
167const MCExpr *
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool isInSmallSection(uint64_t Size)
#define F(x, y, z)
Definition MD5.cpp:54
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
StringRef getSection() const
Get the custom section of this global if it has one.
bool hasSection() const
Check if this global has a custom object file section.
bool hasExternalLinkage() const
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:408
bool hasLocalLinkage() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:205
bool hasCommonLinkage() const
bool hasAvailableExternallyLinkage() const
Type * getValueType() const
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:550
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool useSmallSection() const
bool IsConstantInSmallSection(const DataLayout &DL, const Constant *CN, const Function *F) const
Return true if this constant should be placed into small data section.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
void initialize(MCContext &Ctx, const TargetMachine &TM, const Module &M) override
This method must be called before any actual lowering is done.
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment, const Function *F) const override
Return true if this constant should be placed into small data section.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition SectionKind.h:22
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment, const Function *F) const override
Given a constant with the SectionKind, return a section that it should be placed in.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
void initialize(MCContext &Ctx, const TargetMachine &TM, const Module &M) override
This method must be called before any actual lowering is done.
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Primary interface to the complete machine description for the target machine.
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
@ SHF_ALLOC
Definition ELF.h:1259
@ SHF_MIPS_GPREL
Definition ELF.h:1342
@ SHF_WRITE
Definition ELF.h:1256
@ SHT_PROGBITS
Definition ELF.h:1157
@ SHT_NOBITS
Definition ELF.h:1164
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39