xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "RISCVTargetObjectFile.h"
100b57cec5SDimitry Andric #include "RISCVTargetMachine.h"
110b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
120b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
130b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
180b57cec5SDimitry Andric                                           const TargetMachine &TM) {
190b57cec5SDimitry Andric   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric   SmallDataSection = getContext().getELFSection(
220b57cec5SDimitry Andric       ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
230b57cec5SDimitry Andric   SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
240b57cec5SDimitry Andric                                                ELF::SHF_WRITE | ELF::SHF_ALLOC);
250b57cec5SDimitry Andric }
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // A address must be loaded from a small section if its size is less than the
280b57cec5SDimitry Andric // small section size threshold. Data in this section could be addressed by
290b57cec5SDimitry Andric // using gp_rel operator.
300b57cec5SDimitry Andric bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
310b57cec5SDimitry Andric   // gcc has traditionally not treated zero-sized objects as small data, so this
320b57cec5SDimitry Andric   // is effectively part of the ABI.
330b57cec5SDimitry Andric   return Size > 0 && Size <= SSThreshold;
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric // Return true if this global address should be placed into small data/bss
370b57cec5SDimitry Andric // section.
380b57cec5SDimitry Andric bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
390b57cec5SDimitry Andric     const GlobalObject *GO, const TargetMachine &TM) const {
400b57cec5SDimitry Andric   // Only global variables, not functions.
410b57cec5SDimitry Andric   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
420b57cec5SDimitry Andric   if (!GVA)
430b57cec5SDimitry Andric     return false;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // If the variable has an explicit section, it is placed in that section.
460b57cec5SDimitry Andric   if (GVA->hasSection()) {
470b57cec5SDimitry Andric     StringRef Section = GVA->getSection();
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric     // Explicitly placing any variable in the small data section overrides
500b57cec5SDimitry Andric     // the global -G value.
510b57cec5SDimitry Andric     if (Section == ".sdata" || Section == ".sbss")
520b57cec5SDimitry Andric       return true;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric     // Otherwise reject putting the variable to small section if it has an
550b57cec5SDimitry Andric     // explicit section name.
560b57cec5SDimitry Andric     return false;
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
600b57cec5SDimitry Andric        GVA->hasCommonLinkage()))
610b57cec5SDimitry Andric     return false;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   Type *Ty = GVA->getValueType();
640b57cec5SDimitry Andric   // It is possible that the type of the global is unsized, i.e. a declaration
650b57cec5SDimitry Andric   // of a extern struct. In this case don't presume it is in the small data
660b57cec5SDimitry Andric   // section. This happens e.g. when building the FreeBSD kernel.
670b57cec5SDimitry Andric   if (!Ty->isSized())
680b57cec5SDimitry Andric     return false;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   return isInSmallSection(
710b57cec5SDimitry Andric       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
750b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
760b57cec5SDimitry Andric   // Handle Small Section classification here.
770b57cec5SDimitry Andric   if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
780b57cec5SDimitry Andric     return SmallBSSSection;
790b57cec5SDimitry Andric   if (Kind.isData() && isGlobalInSmallSection(GO, TM))
800b57cec5SDimitry Andric     return SmallDataSection;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // Otherwise, we work the same as ELF.
830b57cec5SDimitry Andric   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
87*fe6060f1SDimitry Andric   TargetLoweringObjectFileELF::getModuleMetadata(M);
880b57cec5SDimitry Andric   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
890b57cec5SDimitry Andric   M.getModuleFlagsMetadata(ModuleFlags);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   for (const auto &MFE : ModuleFlags) {
920b57cec5SDimitry Andric     StringRef Key = MFE.Key->getString();
930b57cec5SDimitry Andric     if (Key == "SmallDataLimit") {
940b57cec5SDimitry Andric       SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
950b57cec5SDimitry Andric       break;
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /// Return true if this constant should be placed into small data section.
1010b57cec5SDimitry Andric bool RISCVELFTargetObjectFile::isConstantInSmallSection(
1020b57cec5SDimitry Andric     const DataLayout &DL, const Constant *CN) const {
1030b57cec5SDimitry Andric   return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
1070b57cec5SDimitry Andric     const DataLayout &DL, SectionKind Kind, const Constant *C,
1085ffd83dbSDimitry Andric     Align &Alignment) const {
1090b57cec5SDimitry Andric   if (isConstantInSmallSection(DL, C))
1100b57cec5SDimitry Andric     return SmallDataSection;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // Otherwise, we work the same as ELF.
1135ffd83dbSDimitry Andric   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
1145ffd83dbSDimitry Andric                                                             Alignment);
1150b57cec5SDimitry Andric }
116