1 // 2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 3 // See https://llvm.org/LICENSE.txt for license information. 4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5 // 6 //===----------------------------------------------------------------------===// 7 8 #include "LanaiTargetObjectFile.h" 9 10 #include "LanaiSubtarget.h" 11 #include "LanaiTargetMachine.h" 12 #include "llvm/BinaryFormat/ELF.h" 13 #include "llvm/IR/DataLayout.h" 14 #include "llvm/IR/DerivedTypes.h" 15 #include "llvm/IR/GlobalVariable.h" 16 #include "llvm/MC/MCContext.h" 17 #include "llvm/MC/MCSectionELF.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Target/TargetMachine.h" 20 21 using namespace llvm; 22 23 static cl::opt<unsigned> SSThreshold( 24 "lanai-ssection-threshold", cl::Hidden, 25 cl::desc("Small data and bss section threshold size (default=0)"), 26 cl::init(0)); 27 28 void LanaiTargetObjectFile::Initialize(MCContext &Ctx, 29 const TargetMachine &TM) { 30 TargetLoweringObjectFileELF::Initialize(Ctx, TM); 31 InitializeELF(TM.Options.UseInitArray); 32 33 SmallDataSection = getContext().getELFSection( 34 ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 35 SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, 36 ELF::SHF_WRITE | ELF::SHF_ALLOC); 37 } 38 39 // A address must be loaded from a small section if its size is less than the 40 // small section size threshold. Data in this section must be addressed using 41 // gp_rel operator. 42 static bool isInSmallSection(uint64_t Size) { 43 // gcc has traditionally not treated zero-sized objects as small data, so this 44 // is effectively part of the ABI. 45 return Size > 0 && Size <= SSThreshold; 46 } 47 48 // Return true if this global address should be placed into small data/bss 49 // section. 50 bool LanaiTargetObjectFile::isGlobalInSmallSection( 51 const GlobalObject *GO, const TargetMachine &TM) const { 52 if (GO == nullptr) return TM.getCodeModel() == CodeModel::Small; 53 54 // We first check the case where global is a declaration, because finding 55 // section kind using getKindForGlobal() is only allowed for global 56 // definitions. 57 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage()) 58 return isGlobalInSmallSectionImpl(GO, TM); 59 60 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM)); 61 } 62 63 // Return true if this global address should be placed into small data/bss 64 // section. 65 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO, 66 const TargetMachine &TM, 67 SectionKind Kind) const { 68 return isGlobalInSmallSectionImpl(GO, TM); 69 } 70 71 // Return true if this global address should be placed into small data/bss 72 // section. This method does all the work, except for checking the section 73 // kind. 74 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl( 75 const GlobalObject *GO, const TargetMachine &TM) const { 76 const auto *GVA = dyn_cast<GlobalVariable>(GO); 77 78 // If not a GlobalVariable, only consider the code model. 79 if (!GVA) return TM.getCodeModel() == CodeModel::Small; 80 81 // Global values placed in sections starting with .ldata do not fit in 82 // 21-bits, so always use large memory access for them. FIXME: This is a 83 // workaround for a tool limitation. 84 if (GVA->getSection().startswith(".ldata")) 85 return false; 86 87 if (TM.getCodeModel() == CodeModel::Small) 88 return true; 89 90 if (GVA->hasLocalLinkage()) 91 return false; 92 93 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) || 94 GVA->hasCommonLinkage())) 95 return false; 96 97 Type *Ty = GVA->getValueType(); 98 return isInSmallSection( 99 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty)); 100 } 101 102 MCSection *LanaiTargetObjectFile::SelectSectionForGlobal( 103 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 104 // Handle Small Section classification here. 105 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind)) 106 return SmallBSSSection; 107 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind)) 108 return SmallDataSection; 109 110 // Otherwise, we work the same as ELF. 111 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); 112 } 113 114 /// Return true if this constant should be placed into small data section. 115 bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL, 116 const Constant *CN) const { 117 return isInSmallSection(DL.getTypeAllocSize(CN->getType())); 118 } 119 120 MCSection *LanaiTargetObjectFile::getSectionForConstant(const DataLayout &DL, 121 SectionKind Kind, 122 const Constant *C, 123 unsigned &Align) const { 124 if (isConstantInSmallSection(DL, C)) 125 return SmallDataSection; 126 127 // Otherwise, we work the same as ELF. 128 return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align); 129 } 130