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