198bc25a0SMandeep Singh Grang //===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
298bc25a0SMandeep Singh Grang //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
698bc25a0SMandeep Singh Grang //
798bc25a0SMandeep Singh Grang //===----------------------------------------------------------------------===//
898bc25a0SMandeep Singh Grang 
998bc25a0SMandeep Singh Grang #include "RISCVTargetObjectFile.h"
1098bc25a0SMandeep Singh Grang #include "RISCVTargetMachine.h"
11*7cc03bd0SShiva Chen #include "llvm/BinaryFormat/ELF.h"
12*7cc03bd0SShiva Chen #include "llvm/MC/MCContext.h"
13*7cc03bd0SShiva Chen #include "llvm/MC/MCSectionELF.h"
1498bc25a0SMandeep Singh Grang 
1598bc25a0SMandeep Singh Grang using namespace llvm;
1698bc25a0SMandeep Singh Grang 
1798bc25a0SMandeep Singh Grang void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
1898bc25a0SMandeep Singh Grang                                           const TargetMachine &TM) {
1998bc25a0SMandeep Singh Grang   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
2098bc25a0SMandeep Singh Grang   InitializeELF(TM.Options.UseInitArray);
21*7cc03bd0SShiva Chen 
22*7cc03bd0SShiva Chen   SmallDataSection = getContext().getELFSection(
23*7cc03bd0SShiva Chen       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
24*7cc03bd0SShiva Chen   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
25*7cc03bd0SShiva Chen                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
26*7cc03bd0SShiva Chen }
27*7cc03bd0SShiva Chen 
28*7cc03bd0SShiva Chen // A address must be loaded from a small section if its size is less than the
29*7cc03bd0SShiva Chen // small section size threshold. Data in this section could be addressed by
30*7cc03bd0SShiva Chen // using gp_rel operator.
31*7cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
32*7cc03bd0SShiva Chen   // gcc has traditionally not treated zero-sized objects as small data, so this
33*7cc03bd0SShiva Chen   // is effectively part of the ABI.
34*7cc03bd0SShiva Chen   return Size > 0 && Size <= SSThreshold;
35*7cc03bd0SShiva Chen }
36*7cc03bd0SShiva Chen 
37*7cc03bd0SShiva Chen // Return true if this global address should be placed into small data/bss
38*7cc03bd0SShiva Chen // section.
39*7cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
40*7cc03bd0SShiva Chen     const GlobalObject *GO, const TargetMachine &TM) const {
41*7cc03bd0SShiva Chen   // Only global variables, not functions.
42*7cc03bd0SShiva Chen   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
43*7cc03bd0SShiva Chen   if (!GVA)
44*7cc03bd0SShiva Chen     return false;
45*7cc03bd0SShiva Chen 
46*7cc03bd0SShiva Chen   // If the variable has an explicit section, it is placed in that section.
47*7cc03bd0SShiva Chen   if (GVA->hasSection()) {
48*7cc03bd0SShiva Chen     StringRef Section = GVA->getSection();
49*7cc03bd0SShiva Chen 
50*7cc03bd0SShiva Chen     // Explicitly placing any variable in the small data section overrides
51*7cc03bd0SShiva Chen     // the global -G value.
52*7cc03bd0SShiva Chen     if (Section == ".sdata" || Section == ".sbss")
53*7cc03bd0SShiva Chen       return true;
54*7cc03bd0SShiva Chen 
55*7cc03bd0SShiva Chen     // Otherwise reject putting the variable to small section if it has an
56*7cc03bd0SShiva Chen     // explicit section name.
57*7cc03bd0SShiva Chen     return false;
58*7cc03bd0SShiva Chen   }
59*7cc03bd0SShiva Chen 
60*7cc03bd0SShiva Chen   if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
61*7cc03bd0SShiva Chen        GVA->hasCommonLinkage()))
62*7cc03bd0SShiva Chen     return false;
63*7cc03bd0SShiva Chen 
64*7cc03bd0SShiva Chen   Type *Ty = GVA->getValueType();
65*7cc03bd0SShiva Chen   // It is possible that the type of the global is unsized, i.e. a declaration
66*7cc03bd0SShiva Chen   // of a extern struct. In this case don't presume it is in the small data
67*7cc03bd0SShiva Chen   // section. This happens e.g. when building the FreeBSD kernel.
68*7cc03bd0SShiva Chen   if (!Ty->isSized())
69*7cc03bd0SShiva Chen     return false;
70*7cc03bd0SShiva Chen 
71*7cc03bd0SShiva Chen   return isInSmallSection(
72*7cc03bd0SShiva Chen       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
73*7cc03bd0SShiva Chen }
74*7cc03bd0SShiva Chen 
75*7cc03bd0SShiva Chen MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
76*7cc03bd0SShiva Chen     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
77*7cc03bd0SShiva Chen   // Handle Small Section classification here.
78*7cc03bd0SShiva Chen   if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
79*7cc03bd0SShiva Chen     return SmallBSSSection;
80*7cc03bd0SShiva Chen   if (Kind.isData() && isGlobalInSmallSection(GO, TM))
81*7cc03bd0SShiva Chen     return SmallDataSection;
82*7cc03bd0SShiva Chen 
83*7cc03bd0SShiva Chen   // Otherwise, we work the same as ELF.
84*7cc03bd0SShiva Chen   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
85*7cc03bd0SShiva Chen }
86*7cc03bd0SShiva Chen 
87*7cc03bd0SShiva Chen void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
88*7cc03bd0SShiva Chen   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
89*7cc03bd0SShiva Chen   M.getModuleFlagsMetadata(ModuleFlags);
90*7cc03bd0SShiva Chen 
91*7cc03bd0SShiva Chen   for (const auto &MFE : ModuleFlags) {
92*7cc03bd0SShiva Chen     StringRef Key = MFE.Key->getString();
93*7cc03bd0SShiva Chen     if (Key == "SmallDataLimit") {
94*7cc03bd0SShiva Chen       SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
95*7cc03bd0SShiva Chen       break;
96*7cc03bd0SShiva Chen     }
97*7cc03bd0SShiva Chen   }
98*7cc03bd0SShiva Chen }
99*7cc03bd0SShiva Chen 
100*7cc03bd0SShiva Chen /// Return true if this constant should be placed into small data section.
101*7cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isConstantInSmallSection(
102*7cc03bd0SShiva Chen     const DataLayout &DL, const Constant *CN) const {
103*7cc03bd0SShiva Chen   return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
104*7cc03bd0SShiva Chen }
105*7cc03bd0SShiva Chen 
106*7cc03bd0SShiva Chen MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
107*7cc03bd0SShiva Chen     const DataLayout &DL, SectionKind Kind, const Constant *C,
108*7cc03bd0SShiva Chen     unsigned &Align) const {
109*7cc03bd0SShiva Chen   if (isConstantInSmallSection(DL, C))
110*7cc03bd0SShiva Chen     return SmallDataSection;
111*7cc03bd0SShiva Chen 
112*7cc03bd0SShiva Chen   // Otherwise, we work the same as ELF.
113*7cc03bd0SShiva Chen   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
11498bc25a0SMandeep Singh Grang }
115