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"
117cc03bd0SShiva Chen #include "llvm/BinaryFormat/ELF.h"
127cc03bd0SShiva Chen #include "llvm/MC/MCContext.h"
137cc03bd0SShiva Chen #include "llvm/MC/MCSectionELF.h"
1498bc25a0SMandeep Singh Grang
1598bc25a0SMandeep Singh Grang using namespace llvm;
1698bc25a0SMandeep Singh Grang
Initialize(MCContext & Ctx,const TargetMachine & TM)1798bc25a0SMandeep Singh Grang void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
1898bc25a0SMandeep Singh Grang const TargetMachine &TM) {
1998bc25a0SMandeep Singh Grang TargetLoweringObjectFileELF::Initialize(Ctx, TM);
207cc03bd0SShiva Chen
217cc03bd0SShiva Chen SmallDataSection = getContext().getELFSection(
227cc03bd0SShiva Chen ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
237cc03bd0SShiva Chen SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
247cc03bd0SShiva Chen ELF::SHF_WRITE | ELF::SHF_ALLOC);
257cc03bd0SShiva Chen }
267cc03bd0SShiva Chen
277cc03bd0SShiva Chen // A address must be loaded from a small section if its size is less than the
287cc03bd0SShiva Chen // small section size threshold. Data in this section could be addressed by
297cc03bd0SShiva Chen // using gp_rel operator.
isInSmallSection(uint64_t Size) const307cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
317cc03bd0SShiva Chen // gcc has traditionally not treated zero-sized objects as small data, so this
327cc03bd0SShiva Chen // is effectively part of the ABI.
337cc03bd0SShiva Chen return Size > 0 && Size <= SSThreshold;
347cc03bd0SShiva Chen }
357cc03bd0SShiva Chen
367cc03bd0SShiva Chen // Return true if this global address should be placed into small data/bss
377cc03bd0SShiva Chen // section.
isGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const387cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
397cc03bd0SShiva Chen const GlobalObject *GO, const TargetMachine &TM) const {
407cc03bd0SShiva Chen // Only global variables, not functions.
417cc03bd0SShiva Chen const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
427cc03bd0SShiva Chen if (!GVA)
437cc03bd0SShiva Chen return false;
447cc03bd0SShiva Chen
457cc03bd0SShiva Chen // If the variable has an explicit section, it is placed in that section.
467cc03bd0SShiva Chen if (GVA->hasSection()) {
477cc03bd0SShiva Chen StringRef Section = GVA->getSection();
487cc03bd0SShiva Chen
497cc03bd0SShiva Chen // Explicitly placing any variable in the small data section overrides
507cc03bd0SShiva Chen // the global -G value.
517cc03bd0SShiva Chen if (Section == ".sdata" || Section == ".sbss")
527cc03bd0SShiva Chen return true;
537cc03bd0SShiva Chen
547cc03bd0SShiva Chen // Otherwise reject putting the variable to small section if it has an
557cc03bd0SShiva Chen // explicit section name.
567cc03bd0SShiva Chen return false;
577cc03bd0SShiva Chen }
587cc03bd0SShiva Chen
597cc03bd0SShiva Chen if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
607cc03bd0SShiva Chen GVA->hasCommonLinkage()))
617cc03bd0SShiva Chen return false;
627cc03bd0SShiva Chen
637cc03bd0SShiva Chen Type *Ty = GVA->getValueType();
647cc03bd0SShiva Chen // It is possible that the type of the global is unsized, i.e. a declaration
657cc03bd0SShiva Chen // of a extern struct. In this case don't presume it is in the small data
667cc03bd0SShiva Chen // section. This happens e.g. when building the FreeBSD kernel.
677cc03bd0SShiva Chen if (!Ty->isSized())
687cc03bd0SShiva Chen return false;
697cc03bd0SShiva Chen
707cc03bd0SShiva Chen return isInSmallSection(
717cc03bd0SShiva Chen GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
727cc03bd0SShiva Chen }
737cc03bd0SShiva Chen
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const747cc03bd0SShiva Chen MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
757cc03bd0SShiva Chen const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
767cc03bd0SShiva Chen // Handle Small Section classification here.
777cc03bd0SShiva Chen if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
787cc03bd0SShiva Chen return SmallBSSSection;
797cc03bd0SShiva Chen if (Kind.isData() && isGlobalInSmallSection(GO, TM))
807cc03bd0SShiva Chen return SmallDataSection;
817cc03bd0SShiva Chen
827cc03bd0SShiva Chen // Otherwise, we work the same as ELF.
837cc03bd0SShiva Chen return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
847cc03bd0SShiva Chen }
857cc03bd0SShiva Chen
getModuleMetadata(Module & M)867cc03bd0SShiva Chen void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
87*47c5576dSFangrui Song TargetLoweringObjectFileELF::getModuleMetadata(M);
887cc03bd0SShiva Chen SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
897cc03bd0SShiva Chen M.getModuleFlagsMetadata(ModuleFlags);
907cc03bd0SShiva Chen
917cc03bd0SShiva Chen for (const auto &MFE : ModuleFlags) {
927cc03bd0SShiva Chen StringRef Key = MFE.Key->getString();
937cc03bd0SShiva Chen if (Key == "SmallDataLimit") {
947cc03bd0SShiva Chen SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
957cc03bd0SShiva Chen break;
967cc03bd0SShiva Chen }
977cc03bd0SShiva Chen }
987cc03bd0SShiva Chen }
997cc03bd0SShiva Chen
1007cc03bd0SShiva Chen /// Return true if this constant should be placed into small data section.
isConstantInSmallSection(const DataLayout & DL,const Constant * CN) const1017cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isConstantInSmallSection(
1027cc03bd0SShiva Chen const DataLayout &DL, const Constant *CN) const {
1037cc03bd0SShiva Chen return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
1047cc03bd0SShiva Chen }
1057cc03bd0SShiva Chen
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const1067cc03bd0SShiva Chen MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
1077cc03bd0SShiva Chen const DataLayout &DL, SectionKind Kind, const Constant *C,
108f96a7706SCraig Topper Align &Alignment) const {
1097cc03bd0SShiva Chen if (isConstantInSmallSection(DL, C))
1107cc03bd0SShiva Chen return SmallDataSection;
1117cc03bd0SShiva Chen
1127cc03bd0SShiva Chen // Otherwise, we work the same as ELF.
113f96a7706SCraig Topper return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
114f96a7706SCraig Topper Alignment);
11598bc25a0SMandeep Singh Grang }
116