| 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" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/ELF.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/DataLayout.h" |
| 15 | #include "llvm/IR/DerivedTypes.h" |
| 16 | #include "llvm/IR/GlobalVariable.h" |
| Chris Lattner | 80c3459 | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSectionELF.h" |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.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( |
| Simon Atanasyan | e774126 | 2016-02-03 11:50:22 +0000 | [diff] [blame] | 44 | ".sdata", ELF::SHT_PROGBITS, |
| 45 | ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL); |
| Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 46 | |
| Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 47 | SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
| Simon Atanasyan | e774126 | 2016-02-03 11:50:22 +0000 | [diff] [blame] | 48 | ELF::SHF_WRITE | ELF::SHF_ALLOC | |
| 49 | ELF::SHF_MIPS_GPREL); |
| Daniel Sanders | 8ef465f | 2015-05-27 08:44:01 +0000 | [diff] [blame] | 50 | this->TM = &static_cast<const MipsTargetMachine &>(TM); |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 53 | // A address must be loaded from a small section if its size is less than the |
| 54 | // small section size threshold. Data in this section must be addressed using |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 55 | // gp_rel operator. |
| 56 | static bool IsInSmallSection(uint64_t Size) { |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 57 | // gcc has traditionally not treated zero-sized objects as small data, so this |
| 58 | // is effectively part of the ABI. |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 59 | return Size > 0 && Size <= SSThreshold; |
| 60 | } |
| 61 | |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 62 | /// Return true if this global address should be placed into small data/bss |
| 63 | /// section. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 64 | bool MipsTargetObjectFile::IsGlobalInSmallSection( |
| 65 | const GlobalObject *GO, const TargetMachine &TM) const { |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 66 | // We first check the case where global is a declaration, because finding |
| 67 | // section kind using getKindForGlobal() is only allowed for global |
| 68 | // definitions. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 69 | if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage()) |
| 70 | return IsGlobalInSmallSectionImpl(GO, TM); |
| Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 71 | |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 72 | return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM)); |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 75 | /// Return true if this global address should be placed into small data/bss |
| 76 | /// section. |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 77 | bool MipsTargetObjectFile:: |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 78 | IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM, |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 79 | SectionKind Kind) const { |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 80 | return (IsGlobalInSmallSectionImpl(GO, TM) && |
| Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 81 | (Kind.isData() || Kind.isBSS() || Kind.isCommon())); |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 82 | } |
| Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 83 | |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 84 | /// Return true if this global address should be placed into small data/bss |
| 85 | /// section. This method does all the work, except for checking the section |
| 86 | /// kind. |
| 87 | bool MipsTargetObjectFile:: |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 88 | IsGlobalInSmallSectionImpl(const GlobalObject *GO, |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 89 | const TargetMachine &TM) const { |
| Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 90 | const MipsSubtarget &Subtarget = |
| 91 | *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl(); |
| Akira Hatanaka | ad49502 | 2012-08-22 03:18:13 +0000 | [diff] [blame] | 92 | |
| 93 | // Return if small section is not available. |
| 94 | if (!Subtarget.useSmallSection()) |
| Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 95 | return false; |
| 96 | |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 97 | // Only global variables, not functions. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 98 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO); |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 99 | if (!GVA) |
| 100 | return false; |
| Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 101 | |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 102 | // Enforce -mlocal-sdata. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 103 | if (!LocalSData && GVA->hasLocalLinkage()) |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 104 | return false; |
| Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 105 | |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 106 | // Enforce -mextern-sdata. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 107 | if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) || |
| 108 | GVA->hasCommonLinkage())) |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 109 | return false; |
| 110 | |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 111 | Type *Ty = GVA->getValueType(); |
| Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 112 | return IsInSmallSection( |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 113 | GVA->getParent()->getDataLayout().getTypeAllocSize(Ty)); |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 116 | MCSection *MipsTargetObjectFile::SelectSectionForGlobal( |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 117 | const GlobalObject *GO, SectionKind Kind, 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. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 122 | if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind)) |
| Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 123 | return SmallBSSSection; |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 124 | if (Kind.isData() && IsGlobalInSmallSection(GO, 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. |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 128 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, 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. |
| David Majnemer | 78f46be | 2016-02-21 01:40:04 +0000 | [diff] [blame] | 141 | MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL, |
| 142 | SectionKind Kind, |
| 143 | const Constant *C, |
| 144 | unsigned &Align) const { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 145 | if (IsConstantInSmallSection(DL, C, *TM)) |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 146 | return SmallDataSection; |
| 147 | |
| 148 | // Otherwise, we work the same as ELF. |
| David Majnemer | 78f46be | 2016-02-21 01:40:04 +0000 | [diff] [blame] | 149 | return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align); |
| Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 150 | } |
| Petar Jovanovic | dbb3935 | 2017-01-20 17:53:30 +0000 | [diff] [blame] | 151 | |
| 152 | const MCExpr * |
| 153 | MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { |
| 154 | const MCExpr *Expr = |
| 155 | MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
| 156 | return MCBinaryExpr::createAdd( |
| 157 | Expr, MCConstantExpr::create(0x8000, getContext()), getContext()); |
| 158 | } |