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