17fa27ce4SDimitry Andric //===-- RISCVTargetObjectFile.cpp - RISC-V Object Info --------------------===//
2eb11fae6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb11fae6SDimitry Andric //
7eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
8eb11fae6SDimitry Andric
9eb11fae6SDimitry Andric #include "RISCVTargetObjectFile.h"
107fa27ce4SDimitry Andric #include "MCTargetDesc/RISCVMCObjectFileInfo.h"
11eb11fae6SDimitry Andric #include "RISCVTargetMachine.h"
12e6d15924SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
13ac9a064cSDimitry Andric #include "llvm/IR/Module.h"
14e6d15924SDimitry Andric #include "llvm/MC/MCContext.h"
15e6d15924SDimitry Andric #include "llvm/MC/MCSectionELF.h"
164df029ccSDimitry Andric #include "llvm/MC/MCValue.h"
17eb11fae6SDimitry Andric
18eb11fae6SDimitry Andric using namespace llvm;
19eb11fae6SDimitry Andric
getTextSectionAlignment() const207fa27ce4SDimitry Andric unsigned RISCVELFTargetObjectFile::getTextSectionAlignment() const {
217fa27ce4SDimitry Andric return RISCVMCObjectFileInfo::getTextSectionAlignment(
227fa27ce4SDimitry Andric *getContext().getSubtargetInfo());
237fa27ce4SDimitry Andric }
247fa27ce4SDimitry Andric
Initialize(MCContext & Ctx,const TargetMachine & TM)25eb11fae6SDimitry Andric void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
26eb11fae6SDimitry Andric const TargetMachine &TM) {
27eb11fae6SDimitry Andric TargetLoweringObjectFileELF::Initialize(Ctx, TM);
28e6d15924SDimitry Andric
297fa27ce4SDimitry Andric PLTRelativeVariantKind = MCSymbolRefExpr::VK_PLT;
304df029ccSDimitry Andric SupportIndirectSymViaGOTPCRel = true;
317fa27ce4SDimitry Andric
32e6d15924SDimitry Andric SmallDataSection = getContext().getELFSection(
33e6d15924SDimitry Andric ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
34e6d15924SDimitry Andric SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
35e6d15924SDimitry Andric ELF::SHF_WRITE | ELF::SHF_ALLOC);
36ac9a064cSDimitry Andric SmallRODataSection =
37ac9a064cSDimitry Andric getContext().getELFSection(".srodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
38ac9a064cSDimitry Andric SmallROData4Section = getContext().getELFSection(
39ac9a064cSDimitry Andric ".srodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
40ac9a064cSDimitry Andric SmallROData8Section = getContext().getELFSection(
41ac9a064cSDimitry Andric ".srodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
42ac9a064cSDimitry Andric SmallROData16Section = getContext().getELFSection(
43ac9a064cSDimitry Andric ".srodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
44ac9a064cSDimitry Andric SmallROData32Section = getContext().getELFSection(
45ac9a064cSDimitry Andric ".srodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
46e6d15924SDimitry Andric }
47e6d15924SDimitry Andric
getIndirectSymViaGOTPCRel(const GlobalValue * GV,const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer) const484df029ccSDimitry Andric const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
494df029ccSDimitry Andric const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
504df029ccSDimitry Andric int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
514df029ccSDimitry Andric int64_t FinalOffset = Offset + MV.getConstant();
524df029ccSDimitry Andric const MCExpr *Res =
534df029ccSDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
544df029ccSDimitry Andric const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
554df029ccSDimitry Andric return MCBinaryExpr::createAdd(Res, Off, getContext());
564df029ccSDimitry Andric }
574df029ccSDimitry Andric
58e6d15924SDimitry Andric // A address must be loaded from a small section if its size is less than the
59e6d15924SDimitry Andric // small section size threshold. Data in this section could be addressed by
60e6d15924SDimitry Andric // using gp_rel operator.
isInSmallSection(uint64_t Size) const61e6d15924SDimitry Andric bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
62e6d15924SDimitry Andric // gcc has traditionally not treated zero-sized objects as small data, so this
63e6d15924SDimitry Andric // is effectively part of the ABI.
64e6d15924SDimitry Andric return Size > 0 && Size <= SSThreshold;
65e6d15924SDimitry Andric }
66e6d15924SDimitry Andric
67e6d15924SDimitry Andric // Return true if this global address should be placed into small data/bss
68e6d15924SDimitry Andric // section.
isGlobalInSmallSection(const GlobalObject * GO,const TargetMachine & TM) const69e6d15924SDimitry Andric bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
70e6d15924SDimitry Andric const GlobalObject *GO, const TargetMachine &TM) const {
71e6d15924SDimitry Andric // Only global variables, not functions.
72e6d15924SDimitry Andric const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
73e6d15924SDimitry Andric if (!GVA)
74e6d15924SDimitry Andric return false;
75e6d15924SDimitry Andric
76e6d15924SDimitry Andric // If the variable has an explicit section, it is placed in that section.
77e6d15924SDimitry Andric if (GVA->hasSection()) {
78e6d15924SDimitry Andric StringRef Section = GVA->getSection();
79e6d15924SDimitry Andric
80e6d15924SDimitry Andric // Explicitly placing any variable in the small data section overrides
81e6d15924SDimitry Andric // the global -G value.
82e6d15924SDimitry Andric if (Section == ".sdata" || Section == ".sbss")
83e6d15924SDimitry Andric return true;
84e6d15924SDimitry Andric
85e6d15924SDimitry Andric // Otherwise reject putting the variable to small section if it has an
86e6d15924SDimitry Andric // explicit section name.
87e6d15924SDimitry Andric return false;
88e6d15924SDimitry Andric }
89e6d15924SDimitry Andric
90e6d15924SDimitry Andric if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
91e6d15924SDimitry Andric GVA->hasCommonLinkage()))
92e6d15924SDimitry Andric return false;
93e6d15924SDimitry Andric
94e6d15924SDimitry Andric Type *Ty = GVA->getValueType();
95e6d15924SDimitry Andric // It is possible that the type of the global is unsized, i.e. a declaration
96e6d15924SDimitry Andric // of a extern struct. In this case don't presume it is in the small data
97e6d15924SDimitry Andric // section. This happens e.g. when building the FreeBSD kernel.
98e6d15924SDimitry Andric if (!Ty->isSized())
99e6d15924SDimitry Andric return false;
100e6d15924SDimitry Andric
101e6d15924SDimitry Andric return isInSmallSection(
102ac9a064cSDimitry Andric GVA->getDataLayout().getTypeAllocSize(Ty));
103e6d15924SDimitry Andric }
104e6d15924SDimitry Andric
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const105e6d15924SDimitry Andric MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
106e6d15924SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
107e6d15924SDimitry Andric // Handle Small Section classification here.
108e6d15924SDimitry Andric if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
109e6d15924SDimitry Andric return SmallBSSSection;
110e6d15924SDimitry Andric if (Kind.isData() && isGlobalInSmallSection(GO, TM))
111e6d15924SDimitry Andric return SmallDataSection;
112e6d15924SDimitry Andric
113e6d15924SDimitry Andric // Otherwise, we work the same as ELF.
114e6d15924SDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
115e6d15924SDimitry Andric }
116e6d15924SDimitry Andric
getModuleMetadata(Module & M)117e6d15924SDimitry Andric void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
118344a3780SDimitry Andric TargetLoweringObjectFileELF::getModuleMetadata(M);
119e6d15924SDimitry Andric SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
120e6d15924SDimitry Andric M.getModuleFlagsMetadata(ModuleFlags);
121e6d15924SDimitry Andric
122e6d15924SDimitry Andric for (const auto &MFE : ModuleFlags) {
123e6d15924SDimitry Andric StringRef Key = MFE.Key->getString();
124e6d15924SDimitry Andric if (Key == "SmallDataLimit") {
125e6d15924SDimitry Andric SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
126e6d15924SDimitry Andric break;
127e6d15924SDimitry Andric }
128e6d15924SDimitry Andric }
129e6d15924SDimitry Andric }
130e6d15924SDimitry Andric
131e6d15924SDimitry Andric /// Return true if this constant should be placed into small data section.
isConstantInSmallSection(const DataLayout & DL,const Constant * CN) const132e6d15924SDimitry Andric bool RISCVELFTargetObjectFile::isConstantInSmallSection(
133e6d15924SDimitry Andric const DataLayout &DL, const Constant *CN) const {
134e6d15924SDimitry Andric return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
135e6d15924SDimitry Andric }
136e6d15924SDimitry Andric
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const137e6d15924SDimitry Andric MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
138e6d15924SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C,
139cfca06d7SDimitry Andric Align &Alignment) const {
140ac9a064cSDimitry Andric if (isConstantInSmallSection(DL, C)) {
141ac9a064cSDimitry Andric if (Kind.isMergeableConst4())
142ac9a064cSDimitry Andric return SmallROData4Section;
143ac9a064cSDimitry Andric if (Kind.isMergeableConst8())
144ac9a064cSDimitry Andric return SmallROData8Section;
145ac9a064cSDimitry Andric if (Kind.isMergeableConst16())
146ac9a064cSDimitry Andric return SmallROData16Section;
147ac9a064cSDimitry Andric if (Kind.isMergeableConst32())
148ac9a064cSDimitry Andric return SmallROData32Section;
149ac9a064cSDimitry Andric // LLVM only generate up to .rodata.cst32, and use .rodata section if more
150ac9a064cSDimitry Andric // than 32 bytes, so just use .srodata here.
151ac9a064cSDimitry Andric return SmallRODataSection;
152ac9a064cSDimitry Andric }
153e6d15924SDimitry Andric
154e6d15924SDimitry Andric // Otherwise, we work the same as ELF.
155cfca06d7SDimitry Andric return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
156cfca06d7SDimitry Andric Alignment);
157eb11fae6SDimitry Andric }
158