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 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. 307cc03bd0SShiva 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. 387cc03bd0SShiva 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 747cc03bd0SShiva 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 867cc03bd0SShiva Chen void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) { 877cc03bd0SShiva Chen SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 887cc03bd0SShiva Chen M.getModuleFlagsMetadata(ModuleFlags); 897cc03bd0SShiva Chen 907cc03bd0SShiva Chen for (const auto &MFE : ModuleFlags) { 917cc03bd0SShiva Chen StringRef Key = MFE.Key->getString(); 927cc03bd0SShiva Chen if (Key == "SmallDataLimit") { 937cc03bd0SShiva Chen SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 947cc03bd0SShiva Chen break; 957cc03bd0SShiva Chen } 967cc03bd0SShiva Chen } 977cc03bd0SShiva Chen } 987cc03bd0SShiva Chen 997cc03bd0SShiva Chen /// Return true if this constant should be placed into small data section. 1007cc03bd0SShiva Chen bool RISCVELFTargetObjectFile::isConstantInSmallSection( 1017cc03bd0SShiva Chen const DataLayout &DL, const Constant *CN) const { 1027cc03bd0SShiva Chen return isInSmallSection(DL.getTypeAllocSize(CN->getType())); 1037cc03bd0SShiva Chen } 1047cc03bd0SShiva Chen 1057cc03bd0SShiva Chen MCSection *RISCVELFTargetObjectFile::getSectionForConstant( 1067cc03bd0SShiva Chen const DataLayout &DL, SectionKind Kind, const Constant *C, 107*f96a7706SCraig Topper Align &Alignment) const { 1087cc03bd0SShiva Chen if (isConstantInSmallSection(DL, C)) 1097cc03bd0SShiva Chen return SmallDataSection; 1107cc03bd0SShiva Chen 1117cc03bd0SShiva Chen // Otherwise, we work the same as ELF. 112*f96a7706SCraig Topper return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, 113*f96a7706SCraig Topper Alignment); 11498bc25a0SMandeep Singh Grang } 115