xref: /src/contrib/llvm-project/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
163faed5bSDimitry Andric //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
259850d08SRoman Divacky //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
659850d08SRoman Divacky //
759850d08SRoman Divacky //===----------------------------------------------------------------------===//
859850d08SRoman Divacky 
959850d08SRoman Divacky #include "MipsTargetObjectFile.h"
107d453863SRoman Divacky #include "MipsSubtarget.h"
115a5ac124SDimitry Andric #include "MipsTargetMachine.h"
12d8e91e46SDimitry Andric #include "MCTargetDesc/MipsMCExpr.h"
137ab83427SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
144a16efa3SDimitry Andric #include "llvm/IR/DataLayout.h"
154a16efa3SDimitry Andric #include "llvm/IR/DerivedTypes.h"
164a16efa3SDimitry Andric #include "llvm/IR/GlobalVariable.h"
17d7f7719eSRoman Divacky #include "llvm/MC/MCContext.h"
1859850d08SRoman Divacky #include "llvm/MC/MCSectionELF.h"
1959850d08SRoman Divacky #include "llvm/Support/CommandLine.h"
204a16efa3SDimitry Andric #include "llvm/Target/TargetMachine.h"
2159850d08SRoman Divacky using namespace llvm;
2259850d08SRoman Divacky 
2359850d08SRoman Divacky static cl::opt<unsigned>
2459850d08SRoman Divacky SSThreshold("mips-ssection-threshold", cl::Hidden,
2559850d08SRoman Divacky             cl::desc("Small data and bss section threshold size (default=8)"),
2659850d08SRoman Divacky             cl::init(8));
2759850d08SRoman Divacky 
2867c32a98SDimitry Andric static cl::opt<bool>
2967c32a98SDimitry Andric LocalSData("mlocal-sdata", cl::Hidden,
3067c32a98SDimitry Andric            cl::desc("MIPS: Use gp_rel for object-local data."),
3167c32a98SDimitry Andric            cl::init(true));
3267c32a98SDimitry Andric 
3367c32a98SDimitry Andric static cl::opt<bool>
3467c32a98SDimitry Andric ExternSData("mextern-sdata", cl::Hidden,
3567c32a98SDimitry Andric             cl::desc("MIPS: Use gp_rel for data that is not defined by the "
3667c32a98SDimitry Andric                      "current object."),
3767c32a98SDimitry Andric             cl::init(true));
3867c32a98SDimitry Andric 
39044eb2f6SDimitry Andric static cl::opt<bool>
40044eb2f6SDimitry Andric EmbeddedData("membedded-data", cl::Hidden,
41044eb2f6SDimitry Andric              cl::desc("MIPS: Try to allocate variables in the following"
42044eb2f6SDimitry Andric                       " sections if possible: .rodata, .sdata, .data ."),
43044eb2f6SDimitry Andric              cl::init(false));
44044eb2f6SDimitry Andric 
Initialize(MCContext & Ctx,const TargetMachine & TM)4559850d08SRoman Divacky void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
4659850d08SRoman Divacky   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
4759850d08SRoman Divacky 
485a5ac124SDimitry Andric   SmallDataSection = getContext().getELFSection(
4901095a5dSDimitry Andric       ".sdata", ELF::SHT_PROGBITS,
5001095a5dSDimitry Andric       ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
5159850d08SRoman Divacky 
525a5ac124SDimitry Andric   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
5301095a5dSDimitry Andric                                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
5401095a5dSDimitry Andric                                                    ELF::SHF_MIPS_GPREL);
555a5ac124SDimitry Andric   this->TM = &static_cast<const MipsTargetMachine &>(TM);
5659850d08SRoman Divacky }
5759850d08SRoman Divacky 
5859850d08SRoman Divacky // A address must be loaded from a small section if its size is less than the
5959850d08SRoman Divacky // small section size threshold. Data in this section must be addressed using
6059850d08SRoman Divacky // gp_rel operator.
IsInSmallSection(uint64_t Size)6159850d08SRoman Divacky static bool IsInSmallSection(uint64_t Size) {
6267c32a98SDimitry Andric   // gcc has traditionally not treated zero-sized objects as small data, so this
6367c32a98SDimitry Andric   // is effectively part of the ABI.
6459850d08SRoman Divacky   return Size > 0 && Size <= SSThreshold;
6559850d08SRoman Divacky }
6659850d08SRoman Divacky 
6767c32a98SDimitry Andric /// Return true if this global address should be placed into small data/bss
6867c32a98SDimitry Andric /// section.
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const69b915e9e0SDimitry Andric bool MipsTargetObjectFile::IsGlobalInSmallSection(
70b915e9e0SDimitry Andric     const GlobalObject *GO, const TargetMachine &TM) const {
7167c32a98SDimitry Andric   // We first check the case where global is a declaration, because finding
7267c32a98SDimitry Andric   // section kind using getKindForGlobal() is only allowed for global
7367c32a98SDimitry Andric   // definitions.
74b915e9e0SDimitry Andric   if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
75b915e9e0SDimitry Andric     return IsGlobalInSmallSectionImpl(GO, TM);
7659850d08SRoman Divacky 
77b915e9e0SDimitry Andric   return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
7859850d08SRoman Divacky }
7959850d08SRoman Divacky 
8067c32a98SDimitry Andric /// Return true if this global address should be placed into small data/bss
8167c32a98SDimitry Andric /// section.
8259850d08SRoman Divacky bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM,SectionKind Kind) const83b915e9e0SDimitry Andric IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
8459850d08SRoman Divacky                        SectionKind Kind) const {
85044eb2f6SDimitry Andric   return IsGlobalInSmallSectionImpl(GO, TM) &&
86044eb2f6SDimitry Andric          (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
87044eb2f6SDimitry Andric           Kind.isReadOnly());
8867c32a98SDimitry Andric }
897d453863SRoman Divacky 
9067c32a98SDimitry Andric /// Return true if this global address should be placed into small data/bss
9167c32a98SDimitry Andric /// section. This method does all the work, except for checking the section
9267c32a98SDimitry Andric /// kind.
9367c32a98SDimitry Andric bool MipsTargetObjectFile::
IsGlobalInSmallSectionImpl(const GlobalObject * GO,const TargetMachine & TM) const94b915e9e0SDimitry Andric IsGlobalInSmallSectionImpl(const GlobalObject *GO,
9567c32a98SDimitry Andric                            const TargetMachine &TM) const {
965a5ac124SDimitry Andric   const MipsSubtarget &Subtarget =
975a5ac124SDimitry Andric       *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
98522600a2SDimitry Andric 
99522600a2SDimitry Andric   // Return if small section is not available.
100522600a2SDimitry Andric   if (!Subtarget.useSmallSection())
1017d453863SRoman Divacky     return false;
1027d453863SRoman Divacky 
10359850d08SRoman Divacky   // Only global variables, not functions.
104b915e9e0SDimitry Andric   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
10559850d08SRoman Divacky   if (!GVA)
10659850d08SRoman Divacky     return false;
10759850d08SRoman Divacky 
108044eb2f6SDimitry Andric   // If the variable has an explicit section, it is placed in that section but
109044eb2f6SDimitry Andric   // it's addressing mode may change.
110044eb2f6SDimitry Andric   if (GVA->hasSection()) {
111044eb2f6SDimitry Andric     StringRef Section = GVA->getSection();
112044eb2f6SDimitry Andric 
113044eb2f6SDimitry Andric     // Explicitly placing any variable in the small data section overrides
114044eb2f6SDimitry Andric     // the global -G value.
115044eb2f6SDimitry Andric     if (Section == ".sdata" || Section == ".sbss")
116044eb2f6SDimitry Andric       return true;
117044eb2f6SDimitry Andric 
118044eb2f6SDimitry Andric     // Otherwise reject accessing it through the gp pointer. There are some
119044eb2f6SDimitry Andric     // historic cases which GCC doesn't appear to respect any more. These
120044eb2f6SDimitry Andric     // are .lit4, .lit8 and .srdata. For the moment reject these as well.
121044eb2f6SDimitry Andric     return false;
122044eb2f6SDimitry Andric   }
123044eb2f6SDimitry Andric 
12467c32a98SDimitry Andric   // Enforce -mlocal-sdata.
125b915e9e0SDimitry Andric   if (!LocalSData && GVA->hasLocalLinkage())
12659850d08SRoman Divacky     return false;
12759850d08SRoman Divacky 
12867c32a98SDimitry Andric   // Enforce -mextern-sdata.
129b915e9e0SDimitry Andric   if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
130b915e9e0SDimitry Andric                        GVA->hasCommonLinkage()))
13159850d08SRoman Divacky     return false;
13259850d08SRoman Divacky 
133044eb2f6SDimitry Andric   // Enforce -membedded-data.
134044eb2f6SDimitry Andric   if (EmbeddedData && GVA->isConstant())
135044eb2f6SDimitry Andric     return false;
136044eb2f6SDimitry Andric 
137b915e9e0SDimitry Andric   Type *Ty = GVA->getValueType();
138eb11fae6SDimitry Andric 
139eb11fae6SDimitry Andric   // It is possible that the type of the global is unsized, i.e. a declaration
140eb11fae6SDimitry Andric   // of a extern struct. In this case don't presume it is in the small data
141eb11fae6SDimitry Andric   // section. This happens e.g. when building the FreeBSD kernel.
142eb11fae6SDimitry Andric   if (!Ty->isSized())
143eb11fae6SDimitry Andric     return false;
144eb11fae6SDimitry Andric 
145dd58ef01SDimitry Andric   return IsInSmallSection(
146ac9a064cSDimitry Andric       GVA->getDataLayout().getTypeAllocSize(Ty));
14759850d08SRoman Divacky }
14859850d08SRoman Divacky 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const149b915e9e0SDimitry Andric MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
150b915e9e0SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
15159850d08SRoman Divacky   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
15259850d08SRoman Divacky   // sections?
15359850d08SRoman Divacky 
15459850d08SRoman Divacky   // Handle Small Section classification here.
155b915e9e0SDimitry Andric   if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
15659850d08SRoman Divacky     return SmallBSSSection;
157b915e9e0SDimitry Andric   if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
15859850d08SRoman Divacky     return SmallDataSection;
159044eb2f6SDimitry Andric   if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
160044eb2f6SDimitry Andric     return SmallDataSection;
16159850d08SRoman Divacky 
16259850d08SRoman Divacky   // Otherwise, we work the same as ELF.
163b915e9e0SDimitry Andric   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
16459850d08SRoman Divacky }
16567c32a98SDimitry Andric 
16667c32a98SDimitry Andric /// Return true if this constant should be placed into small data section.
IsConstantInSmallSection(const DataLayout & DL,const Constant * CN,const TargetMachine & TM) const167dd58ef01SDimitry Andric bool MipsTargetObjectFile::IsConstantInSmallSection(
168dd58ef01SDimitry Andric     const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
1695a5ac124SDimitry Andric   return (static_cast<const MipsTargetMachine &>(TM)
1705a5ac124SDimitry Andric               .getSubtargetImpl()
1715a5ac124SDimitry Andric               ->useSmallSection() &&
172dd58ef01SDimitry Andric           LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
17367c32a98SDimitry Andric }
17467c32a98SDimitry Andric 
175dd58ef01SDimitry Andric /// Return true if this constant should be placed into small data section.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const17601095a5dSDimitry Andric MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
17701095a5dSDimitry Andric                                                        SectionKind Kind,
17801095a5dSDimitry Andric                                                        const Constant *C,
179cfca06d7SDimitry Andric                                                        Align &Alignment) const {
180dd58ef01SDimitry Andric   if (IsConstantInSmallSection(DL, C, *TM))
18167c32a98SDimitry Andric     return SmallDataSection;
18267c32a98SDimitry Andric 
18367c32a98SDimitry Andric   // Otherwise, we work the same as ELF.
184cfca06d7SDimitry Andric   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
185cfca06d7SDimitry Andric                                                             Alignment);
18667c32a98SDimitry Andric }
187823f87a1SDimitry Andric 
188823f87a1SDimitry Andric const MCExpr *
getDebugThreadLocalSymbol(const MCSymbol * Sym) const189823f87a1SDimitry Andric MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
190823f87a1SDimitry Andric   const MCExpr *Expr =
191823f87a1SDimitry Andric       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
192d8e91e46SDimitry Andric   Expr = MCBinaryExpr::createAdd(
193823f87a1SDimitry Andric       Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
194d8e91e46SDimitry Andric   return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());
195823f87a1SDimitry Andric }
196