xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "MipsTargetObjectFile.h"
107330f729Sjoerg #include "MipsSubtarget.h"
117330f729Sjoerg #include "MipsTargetMachine.h"
127330f729Sjoerg #include "MCTargetDesc/MipsMCExpr.h"
137330f729Sjoerg #include "llvm/BinaryFormat/ELF.h"
147330f729Sjoerg #include "llvm/IR/DataLayout.h"
157330f729Sjoerg #include "llvm/IR/DerivedTypes.h"
167330f729Sjoerg #include "llvm/IR/GlobalVariable.h"
177330f729Sjoerg #include "llvm/MC/MCContext.h"
187330f729Sjoerg #include "llvm/MC/MCSectionELF.h"
197330f729Sjoerg #include "llvm/Support/CommandLine.h"
207330f729Sjoerg #include "llvm/Target/TargetMachine.h"
217330f729Sjoerg using namespace llvm;
227330f729Sjoerg 
237330f729Sjoerg static cl::opt<unsigned>
247330f729Sjoerg SSThreshold("mips-ssection-threshold", cl::Hidden,
257330f729Sjoerg             cl::desc("Small data and bss section threshold size (default=8)"),
267330f729Sjoerg             cl::init(8));
277330f729Sjoerg 
287330f729Sjoerg static cl::opt<bool>
297330f729Sjoerg LocalSData("mlocal-sdata", cl::Hidden,
307330f729Sjoerg            cl::desc("MIPS: Use gp_rel for object-local data."),
317330f729Sjoerg            cl::init(true));
327330f729Sjoerg 
337330f729Sjoerg static cl::opt<bool>
347330f729Sjoerg ExternSData("mextern-sdata", cl::Hidden,
357330f729Sjoerg             cl::desc("MIPS: Use gp_rel for data that is not defined by the "
367330f729Sjoerg                      "current object."),
377330f729Sjoerg             cl::init(true));
387330f729Sjoerg 
397330f729Sjoerg static cl::opt<bool>
407330f729Sjoerg EmbeddedData("membedded-data", cl::Hidden,
417330f729Sjoerg              cl::desc("MIPS: Try to allocate variables in the following"
427330f729Sjoerg                       " sections if possible: .rodata, .sdata, .data ."),
437330f729Sjoerg              cl::init(false));
447330f729Sjoerg 
Initialize(MCContext & Ctx,const TargetMachine & TM)457330f729Sjoerg void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
467330f729Sjoerg   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
477330f729Sjoerg 
487330f729Sjoerg   SmallDataSection = getContext().getELFSection(
497330f729Sjoerg       ".sdata", ELF::SHT_PROGBITS,
507330f729Sjoerg       ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
517330f729Sjoerg 
527330f729Sjoerg   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
537330f729Sjoerg                                                ELF::SHF_WRITE | ELF::SHF_ALLOC |
547330f729Sjoerg                                                    ELF::SHF_MIPS_GPREL);
557330f729Sjoerg   this->TM = &static_cast<const MipsTargetMachine &>(TM);
567330f729Sjoerg }
577330f729Sjoerg 
587330f729Sjoerg // A address must be loaded from a small section if its size is less than the
597330f729Sjoerg // small section size threshold. Data in this section must be addressed using
607330f729Sjoerg // gp_rel operator.
IsInSmallSection(uint64_t Size)617330f729Sjoerg static bool IsInSmallSection(uint64_t Size) {
627330f729Sjoerg   // gcc has traditionally not treated zero-sized objects as small data, so this
637330f729Sjoerg   // is effectively part of the ABI.
647330f729Sjoerg   return Size > 0 && Size <= SSThreshold;
657330f729Sjoerg }
667330f729Sjoerg 
677330f729Sjoerg /// Return true if this global address should be placed into small data/bss
687330f729Sjoerg /// section.
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const697330f729Sjoerg bool MipsTargetObjectFile::IsGlobalInSmallSection(
707330f729Sjoerg     const GlobalObject *GO, const TargetMachine &TM) const {
717330f729Sjoerg   // We first check the case where global is a declaration, because finding
727330f729Sjoerg   // section kind using getKindForGlobal() is only allowed for global
737330f729Sjoerg   // definitions.
747330f729Sjoerg   if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
757330f729Sjoerg     return IsGlobalInSmallSectionImpl(GO, TM);
767330f729Sjoerg 
777330f729Sjoerg   return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
787330f729Sjoerg }
797330f729Sjoerg 
807330f729Sjoerg /// Return true if this global address should be placed into small data/bss
817330f729Sjoerg /// section.
827330f729Sjoerg bool MipsTargetObjectFile::
IsGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM,SectionKind Kind) const837330f729Sjoerg IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
847330f729Sjoerg                        SectionKind Kind) const {
857330f729Sjoerg   return IsGlobalInSmallSectionImpl(GO, TM) &&
867330f729Sjoerg          (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
877330f729Sjoerg           Kind.isReadOnly());
887330f729Sjoerg }
897330f729Sjoerg 
907330f729Sjoerg /// Return true if this global address should be placed into small data/bss
917330f729Sjoerg /// section. This method does all the work, except for checking the section
927330f729Sjoerg /// kind.
937330f729Sjoerg bool MipsTargetObjectFile::
IsGlobalInSmallSectionImpl(const GlobalObject * GO,const TargetMachine & TM) const947330f729Sjoerg IsGlobalInSmallSectionImpl(const GlobalObject *GO,
957330f729Sjoerg                            const TargetMachine &TM) const {
967330f729Sjoerg   const MipsSubtarget &Subtarget =
977330f729Sjoerg       *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
987330f729Sjoerg 
997330f729Sjoerg   // Return if small section is not available.
1007330f729Sjoerg   if (!Subtarget.useSmallSection())
1017330f729Sjoerg     return false;
1027330f729Sjoerg 
1037330f729Sjoerg   // Only global variables, not functions.
1047330f729Sjoerg   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
1057330f729Sjoerg   if (!GVA)
1067330f729Sjoerg     return false;
1077330f729Sjoerg 
1087330f729Sjoerg   // If the variable has an explicit section, it is placed in that section but
1097330f729Sjoerg   // it's addressing mode may change.
1107330f729Sjoerg   if (GVA->hasSection()) {
1117330f729Sjoerg     StringRef Section = GVA->getSection();
1127330f729Sjoerg 
1137330f729Sjoerg     // Explicitly placing any variable in the small data section overrides
1147330f729Sjoerg     // the global -G value.
1157330f729Sjoerg     if (Section == ".sdata" || Section == ".sbss")
1167330f729Sjoerg       return true;
1177330f729Sjoerg 
1187330f729Sjoerg     // Otherwise reject accessing it through the gp pointer. There are some
1197330f729Sjoerg     // historic cases which GCC doesn't appear to respect any more. These
1207330f729Sjoerg     // are .lit4, .lit8 and .srdata. For the moment reject these as well.
1217330f729Sjoerg     return false;
1227330f729Sjoerg   }
1237330f729Sjoerg 
1247330f729Sjoerg   // Enforce -mlocal-sdata.
1257330f729Sjoerg   if (!LocalSData && GVA->hasLocalLinkage())
1267330f729Sjoerg     return false;
1277330f729Sjoerg 
1287330f729Sjoerg   // Enforce -mextern-sdata.
1297330f729Sjoerg   if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
1307330f729Sjoerg                        GVA->hasCommonLinkage()))
1317330f729Sjoerg     return false;
1327330f729Sjoerg 
1337330f729Sjoerg   // Enforce -membedded-data.
1347330f729Sjoerg   if (EmbeddedData && GVA->isConstant())
1357330f729Sjoerg     return false;
1367330f729Sjoerg 
1377330f729Sjoerg   Type *Ty = GVA->getValueType();
1387330f729Sjoerg 
1397330f729Sjoerg   // It is possible that the type of the global is unsized, i.e. a declaration
1407330f729Sjoerg   // of a extern struct. In this case don't presume it is in the small data
1417330f729Sjoerg   // section. This happens e.g. when building the FreeBSD kernel.
1427330f729Sjoerg   if (!Ty->isSized())
1437330f729Sjoerg     return false;
1447330f729Sjoerg 
1457330f729Sjoerg   return IsInSmallSection(
1467330f729Sjoerg       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
1477330f729Sjoerg }
1487330f729Sjoerg 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const1497330f729Sjoerg MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
1507330f729Sjoerg     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1517330f729Sjoerg   // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
1527330f729Sjoerg   // sections?
1537330f729Sjoerg 
1547330f729Sjoerg   // Handle Small Section classification here.
1557330f729Sjoerg   if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
1567330f729Sjoerg     return SmallBSSSection;
1577330f729Sjoerg   if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
1587330f729Sjoerg     return SmallDataSection;
1597330f729Sjoerg   if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
1607330f729Sjoerg     return SmallDataSection;
1617330f729Sjoerg 
1627330f729Sjoerg   // Otherwise, we work the same as ELF.
1637330f729Sjoerg   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
1647330f729Sjoerg }
1657330f729Sjoerg 
1667330f729Sjoerg /// Return true if this constant should be placed into small data section.
IsConstantInSmallSection(const DataLayout & DL,const Constant * CN,const TargetMachine & TM) const1677330f729Sjoerg bool MipsTargetObjectFile::IsConstantInSmallSection(
1687330f729Sjoerg     const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
1697330f729Sjoerg   return (static_cast<const MipsTargetMachine &>(TM)
1707330f729Sjoerg               .getSubtargetImpl()
1717330f729Sjoerg               ->useSmallSection() &&
1727330f729Sjoerg           LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
1737330f729Sjoerg }
1747330f729Sjoerg 
1757330f729Sjoerg /// Return true if this constant should be placed into small data section.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const1767330f729Sjoerg MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
1777330f729Sjoerg                                                        SectionKind Kind,
1787330f729Sjoerg                                                        const Constant *C,
179*82d56013Sjoerg                                                        Align &Alignment) const {
1807330f729Sjoerg   if (IsConstantInSmallSection(DL, C, *TM))
1817330f729Sjoerg     return SmallDataSection;
1827330f729Sjoerg 
1837330f729Sjoerg   // Otherwise, we work the same as ELF.
184*82d56013Sjoerg   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
185*82d56013Sjoerg                                                             Alignment);
1867330f729Sjoerg }
1877330f729Sjoerg 
1887330f729Sjoerg const MCExpr *
getDebugThreadLocalSymbol(const MCSymbol * Sym) const1897330f729Sjoerg MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
1907330f729Sjoerg   const MCExpr *Expr =
1917330f729Sjoerg       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1927330f729Sjoerg   Expr = MCBinaryExpr::createAdd(
1937330f729Sjoerg       Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
1947330f729Sjoerg   return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext());
1957330f729Sjoerg }
196