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