Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===// |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 9 | |
| 10 | #include "MipsTargetObjectFile.h" |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 11 | #include "MipsSubtarget.h" |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 12 | #include "MipsTargetMachine.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DataLayout.h" |
| 14 | #include "llvm/IR/DerivedTypes.h" |
| 15 | #include "llvm/IR/GlobalVariable.h" |
Chris Lattner | 80c3459 | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ELF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | static cl::opt<unsigned> |
| 24 | SSThreshold("mips-ssection-threshold", cl::Hidden, |
| 25 | cl::desc("Small data and bss section threshold size (default=8)"), |
| 26 | cl::init(8)); |
| 27 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 28 | static cl::opt<bool> |
| 29 | LocalSData("mlocal-sdata", cl::Hidden, |
| 30 | cl::desc("MIPS: Use gp_rel for object-local data."), |
| 31 | cl::init(true)); |
| 32 | |
| 33 | static cl::opt<bool> |
| 34 | ExternSData("mextern-sdata", cl::Hidden, |
| 35 | cl::desc("MIPS: Use gp_rel for data that is not defined by the " |
| 36 | "current object."), |
| 37 | cl::init(true)); |
| 38 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 39 | void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){ |
| 40 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
Logan Chien | eeaaf65 | 2012-09-05 06:17:17 +0000 | [diff] [blame] | 41 | InitializeELF(TM.Options.UseInitArray); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 42 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 43 | SmallDataSection = getContext().getELFSection( |
| 44 | ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 45 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 46 | SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
| 47 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Daniel Sanders | 8ef465f | 2015-05-27 08:44:01 +0000 | [diff] [blame] | 48 | this->TM = &static_cast<const MipsTargetMachine &>(TM); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 51 | // A address must be loaded from a small section if its size is less than the |
| 52 | // small section size threshold. Data in this section must be addressed using |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 53 | // gp_rel operator. |
| 54 | static bool IsInSmallSection(uint64_t Size) { |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 55 | // gcc has traditionally not treated zero-sized objects as small data, so this |
| 56 | // is effectively part of the ABI. |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 57 | return Size > 0 && Size <= SSThreshold; |
| 58 | } |
| 59 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 60 | /// Return true if this global address should be placed into small data/bss |
| 61 | /// section. |
| 62 | bool MipsTargetObjectFile:: |
| 63 | IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const { |
| 64 | // We first check the case where global is a declaration, because finding |
| 65 | // section kind using getKindForGlobal() is only allowed for global |
| 66 | // definitions. |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 67 | if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 68 | return IsGlobalInSmallSectionImpl(GV, TM); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 70 | return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM)); |
| 71 | } |
| 72 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 73 | /// Return true if this global address should be placed into small data/bss |
| 74 | /// section. |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 75 | bool MipsTargetObjectFile:: |
| 76 | IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, |
| 77 | SectionKind Kind) const { |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 78 | return (IsGlobalInSmallSectionImpl(GV, TM) && |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 79 | (Kind.isData() || Kind.isBSS() || Kind.isCommon())); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 80 | } |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 81 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 82 | /// Return true if this global address should be placed into small data/bss |
| 83 | /// section. This method does all the work, except for checking the section |
| 84 | /// kind. |
| 85 | bool MipsTargetObjectFile:: |
| 86 | IsGlobalInSmallSectionImpl(const GlobalValue *GV, |
| 87 | const TargetMachine &TM) const { |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 88 | const MipsSubtarget &Subtarget = |
| 89 | *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl(); |
Akira Hatanaka | ad49502 | 2012-08-22 03:18:13 +0000 | [diff] [blame] | 90 | |
| 91 | // Return if small section is not available. |
| 92 | if (!Subtarget.useSmallSection()) |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 93 | return false; |
| 94 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 95 | // Only global variables, not functions. |
| 96 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); |
| 97 | if (!GVA) |
| 98 | return false; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 99 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 100 | // Enforce -mlocal-sdata. |
| 101 | if (!LocalSData && GV->hasLocalLinkage()) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 102 | return false; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 103 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 104 | // Enforce -mextern-sdata. |
| 105 | if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) || |
| 106 | GV->hasCommonLinkage())) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 107 | return false; |
| 108 | |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame^] | 109 | Type *Ty = GV->getValueType(); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 110 | return IsInSmallSection( |
| 111 | GV->getParent()->getDataLayout().getTypeAllocSize(Ty)); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 114 | MCSection * |
| 115 | MipsTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
| 116 | SectionKind Kind, Mangler &Mang, |
| 117 | const TargetMachine &TM) const { |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 118 | // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" |
| 119 | // sections? |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 121 | // Handle Small Section classification here. |
| 122 | if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind)) |
| 123 | return SmallBSSSection; |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 124 | if (Kind.isData() && IsGlobalInSmallSection(GV, TM, Kind)) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 125 | return SmallDataSection; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 127 | // Otherwise, we work the same as ELF. |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 128 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 129 | } |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 130 | |
| 131 | /// Return true if this constant should be placed into small data section. |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 132 | bool MipsTargetObjectFile::IsConstantInSmallSection( |
| 133 | const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const { |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 134 | return (static_cast<const MipsTargetMachine &>(TM) |
| 135 | .getSubtargetImpl() |
| 136 | ->useSmallSection() && |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 137 | LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType()))); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 140 | /// Return true if this constant should be placed into small data section. |
| 141 | MCSection *MipsTargetObjectFile::getSectionForConstant( |
| 142 | const DataLayout &DL, SectionKind Kind, const Constant *C) const { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 143 | if (IsConstantInSmallSection(DL, C, *TM)) |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 144 | return SmallDataSection; |
| 145 | |
| 146 | // Otherwise, we work the same as ELF. |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 147 | return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 148 | } |