1//===-- RISCVTargetObjectFile.cpp - RISC-V 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 "MCTargetDesc/RISCVMCObjectFileInfo.h"
11#include "RISCVTargetMachine.h"
12#include "llvm/BinaryFormat/ELF.h"
13#include "llvm/IR/Mangler.h"
14#include "llvm/IR/Module.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCSectionELF.h"
17#include "llvm/MC/MCValue.h"
18
19using namespace llvm;
20
21unsigned RISCVELFTargetObjectFile::getTextSectionAlignment() const {
22 return RISCVMCObjectFileInfo::getTextSectionAlignment(
23 STI: *getContext().getSubtargetInfo());
24}
25
26void RISCVELFTargetObjectFile::initialize(MCContext &Ctx,
27 const TargetMachine &TM,
28 const Module &M) {
29 TargetLoweringObjectFileELF::initialize(Ctx, TM, M);
30
31 PLTPCRelativeSpecifier = ELF::R_RISCV_PLT32;
32 SupportIndirectSymViaGOTPCRel = true;
33
34 SmallDataSection = getContext().getELFSection(
35 Section: ".sdata", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
36 SmallBSSSection = getContext().getELFSection(Section: ".sbss", Type: ELF::SHT_NOBITS,
37 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
38 SmallRODataSection =
39 getContext().getELFSection(Section: ".srodata", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC);
40 SmallROData4Section = getContext().getELFSection(
41 Section: ".srodata.cst4", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_MERGE, EntrySize: 4);
42 SmallROData8Section = getContext().getELFSection(
43 Section: ".srodata.cst8", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_MERGE, EntrySize: 8);
44 SmallROData16Section = getContext().getELFSection(
45 Section: ".srodata.cst16", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_MERGE, EntrySize: 16);
46 SmallROData32Section = getContext().getELFSection(
47 Section: ".srodata.cst32", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_MERGE, EntrySize: 32);
48
49 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
50 M.getModuleFlagsMetadata(Flags&: ModuleFlags);
51
52 for (const auto &MFE : ModuleFlags) {
53 StringRef Key = MFE.Key->getString();
54 if (Key == "SmallDataLimit") {
55 SSThreshold = mdconst::extract<ConstantInt>(MD: MFE.Val)->getZExtValue();
56 break;
57 }
58 }
59}
60
61const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
62 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
63 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
64 auto &Ctx = getContext();
65 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx);
66 Res = MCBinaryExpr::createAdd(
67 LHS: Res, RHS: MCConstantExpr::create(Value: Offset + MV.getConstant(), Ctx), Ctx);
68 return MCSpecifierExpr::create(Expr: Res, S: ELF::R_RISCV_GOT32_PCREL, Ctx);
69}
70
71bool RISCVELFTargetObjectFile::shouldPutJumpTableInFunctionSection(
72 bool UsesLabelDifference, const Function &F) const {
73 // With the large code model, keep the jump table in the function's section.
74 if (TM->getCodeModel() == CodeModel::Large)
75 return true;
76 return TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
77 UsesLabelDifference, F);
78}
79
80// A address must be loaded from a small section if its size is less than the
81// small section size threshold. Data in this section could be addressed by
82// using gp_rel operator.
83bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
84 // gcc has traditionally not treated zero-sized objects as small data, so this
85 // is effectively part of the ABI.
86 return Size > 0 && Size <= SSThreshold;
87}
88
89// Return true if this global address should be placed into small data/bss
90// section.
91bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
92 const GlobalObject *GO, const TargetMachine &TM) const {
93 // Only global variables, not functions.
94 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(Val: GO);
95 if (!GVA)
96 return false;
97
98 // If the variable has an explicit section, it is placed in that section.
99 if (GVA->hasSection()) {
100 StringRef Section = GVA->getSection();
101
102 // Explicitly placing any variable in the small data section overrides
103 // the global -G value.
104 if (Section == ".sdata" || Section == ".sbss")
105 return true;
106
107 // Otherwise reject putting the variable to small section if it has an
108 // explicit section name.
109 return false;
110 }
111
112 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
113 GVA->hasCommonLinkage()))
114 return false;
115
116 Type *Ty = GVA->getValueType();
117 // It is possible that the type of the global is unsized, i.e. a declaration
118 // of a extern struct. In this case don't presume it is in the small data
119 // section. This happens e.g. when building the FreeBSD kernel.
120 if (!Ty->isSized())
121 return false;
122
123 return isInSmallSection(
124 Size: GVA->getDataLayout().getTypeAllocSize(Ty));
125}
126
127MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
128 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
129 // Handle Small Section classification here.
130 if (isGlobalInSmallSection(GO, TM)) {
131 // Emit to an unique sdata/sbss section when -fdata-section is set.
132 // However, if a symbol has an explicit sdata/sbss section, place it in that
133 // section.
134 bool EmitUniquedSection = TM.getDataSections() && !GO->hasSection();
135
136 if (Kind.isBSS()) {
137 if (EmitUniquedSection) {
138 SmallString<128> Name(".sbss.");
139 Name.append(RHS: GO->getName());
140 return getContext().getELFSection(Section: Name.str(), Type: ELF::SHT_NOBITS,
141 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
142 }
143
144 return SmallBSSSection;
145 }
146
147 if (Kind.isData()) {
148 if (EmitUniquedSection) {
149 SmallString<128> Name(".sdata.");
150 Name.append(RHS: GO->getName());
151 return getContext().getELFSection(Section: Name.str(), Type: ELF::SHT_PROGBITS,
152 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
153 }
154
155 return SmallDataSection;
156 }
157 }
158
159 // Otherwise, we work the same as ELF.
160 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
161}
162
163/// Return true if this constant should be placed into small data section.
164bool RISCVELFTargetObjectFile::isConstantInSmallSection(
165 const DataLayout &DL, const Constant *CN) const {
166 return isInSmallSection(Size: DL.getTypeAllocSize(Ty: CN->getType()));
167}
168
169MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
170 const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
171 const Function *F) const {
172
173 // The large code model has to put constant pools close to the program, so we
174 // put them in the .text section. Large code model doesn't support PIC, so
175 // there should be no dynamic relocations that would require `.data.rel.ro`
176 // (which could be too far away anyway).
177 if (TM->getCodeModel() == CodeModel::Large) {
178 if (F)
179 return SectionForGlobal(GO: F, Kind: SectionKind::getText(), TM: *TM);
180 else
181 return TextSection;
182 }
183
184 if (C && isConstantInSmallSection(DL, CN: C)) {
185 if (Kind.isMergeableConst4())
186 return SmallROData4Section;
187 if (Kind.isMergeableConst8())
188 return SmallROData8Section;
189 if (Kind.isMergeableConst16())
190 return SmallROData16Section;
191 if (Kind.isMergeableConst32())
192 return SmallROData32Section;
193 // LLVM only generate up to .rodata.cst32, and use .rodata section if more
194 // than 32 bytes, so just use .srodata here.
195 return SmallRODataSection;
196 }
197
198 // Otherwise, we work the same as ELF.
199 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
200 Alignment, F);
201}
202
203void RISCVMachOTargetObjectFile::getNameWithPrefix(
204 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
205 const TargetMachine &TM) const {
206 // RISC-V does not use section-relative relocations so any global symbol must
207 // be accessed via at least a linker-private symbol.
208 getMangler().getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/true);
209}
210