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