blob: bbd45c970d3dcae2061daf11d3617c422f603376 [file] [log] [blame]
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +00001//===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "RISCVTargetObjectFile.h"
10#include "RISCVTargetMachine.h"
Shiva Chen7cc03bd2019-04-11 04:59:13 +000011#include "llvm/BinaryFormat/ELF.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSectionELF.h"
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +000014
15using namespace llvm;
16
17void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
18 const TargetMachine &TM) {
19 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
20 InitializeELF(TM.Options.UseInitArray);
Shiva Chen7cc03bd2019-04-11 04:59:13 +000021
22 SmallDataSection = getContext().getELFSection(
23 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
24 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
25 ELF::SHF_WRITE | ELF::SHF_ALLOC);
26}
27
28// A address must be loaded from a small section if its size is less than the
29// small section size threshold. Data in this section could be addressed by
30// using gp_rel operator.
31bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
32 // gcc has traditionally not treated zero-sized objects as small data, so this
33 // is effectively part of the ABI.
34 return Size > 0 && Size <= SSThreshold;
35}
36
37// Return true if this global address should be placed into small data/bss
38// section.
39bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
40 const GlobalObject *GO, const TargetMachine &TM) const {
41 // Only global variables, not functions.
42 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
43 if (!GVA)
44 return false;
45
46 // If the variable has an explicit section, it is placed in that section.
47 if (GVA->hasSection()) {
48 StringRef Section = GVA->getSection();
49
50 // Explicitly placing any variable in the small data section overrides
51 // the global -G value.
52 if (Section == ".sdata" || Section == ".sbss")
53 return true;
54
55 // Otherwise reject putting the variable to small section if it has an
56 // explicit section name.
57 return false;
58 }
59
60 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
61 GVA->hasCommonLinkage()))
62 return false;
63
64 Type *Ty = GVA->getValueType();
65 // It is possible that the type of the global is unsized, i.e. a declaration
66 // of a extern struct. In this case don't presume it is in the small data
67 // section. This happens e.g. when building the FreeBSD kernel.
68 if (!Ty->isSized())
69 return false;
70
71 return isInSmallSection(
72 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
73}
74
75MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
76 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
77 // Handle Small Section classification here.
78 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
79 return SmallBSSSection;
80 if (Kind.isData() && isGlobalInSmallSection(GO, TM))
81 return SmallDataSection;
82
83 // Otherwise, we work the same as ELF.
84 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
85}
86
87void RISCVELFTargetObjectFile::getModuleMetadata(Module &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.
101bool RISCVELFTargetObjectFile::isConstantInSmallSection(
102 const DataLayout &DL, const Constant *CN) const {
103 return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
104}
105
106MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
107 const DataLayout &DL, SectionKind Kind, const Constant *C,
108 unsigned &Align) 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, Align);
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +0000114}