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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 6 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 8 | |
| 9 | #include "MipsTargetObjectFile.h" |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 10 | #include "MipsSubtarget.h" |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 11 | #include "MipsTargetMachine.h" |
Simon Atanasyan | f76884b | 2018-12-03 21:54:43 +0000 | [diff] [blame] | 12 | #include "MCTargetDesc/MipsMCExpr.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 | |
Simon Dardis | 0310eb7 | 2017-07-21 17:19:00 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | EmbeddedData("membedded-data", cl::Hidden, |
| 41 | cl::desc("MIPS: Try to allocate variables in the following" |
| 42 | " sections if possible: .rodata, .sdata, .data ."), |
| 43 | cl::init(false)); |
| 44 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 45 | void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){ |
| 46 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
Logan Chien | eeaaf65 | 2012-09-05 06:17:17 +0000 | [diff] [blame] | 47 | InitializeELF(TM.Options.UseInitArray); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 48 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 49 | SmallDataSection = getContext().getELFSection( |
Simon Atanasyan | e774126 | 2016-02-03 11:50:22 +0000 | [diff] [blame] | 50 | ".sdata", ELF::SHT_PROGBITS, |
| 51 | ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 53 | SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
Simon Atanasyan | e774126 | 2016-02-03 11:50:22 +0000 | [diff] [blame] | 54 | ELF::SHF_WRITE | ELF::SHF_ALLOC | |
| 55 | ELF::SHF_MIPS_GPREL); |
Daniel Sanders | 8ef465f | 2015-05-27 08:44:01 +0000 | [diff] [blame] | 56 | this->TM = &static_cast<const MipsTargetMachine &>(TM); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 59 | // A address must be loaded from a small section if its size is less than the |
| 60 | // small section size threshold. Data in this section must be addressed using |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 61 | // gp_rel operator. |
| 62 | static bool IsInSmallSection(uint64_t Size) { |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 63 | // gcc has traditionally not treated zero-sized objects as small data, so this |
| 64 | // is effectively part of the ABI. |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 65 | return Size > 0 && Size <= SSThreshold; |
| 66 | } |
| 67 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 68 | /// Return true if this global address should be placed into small data/bss |
| 69 | /// section. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 70 | bool MipsTargetObjectFile::IsGlobalInSmallSection( |
| 71 | const GlobalObject *GO, const TargetMachine &TM) const { |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 72 | // We first check the case where global is a declaration, because finding |
| 73 | // section kind using getKindForGlobal() is only allowed for global |
| 74 | // definitions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 75 | if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage()) |
| 76 | return IsGlobalInSmallSectionImpl(GO, TM); |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 77 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 78 | return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM)); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 81 | /// Return true if this global address should be placed into small data/bss |
| 82 | /// section. |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 83 | bool MipsTargetObjectFile:: |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 84 | IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM, |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 85 | SectionKind Kind) const { |
Simon Dardis | 0310eb7 | 2017-07-21 17:19:00 +0000 | [diff] [blame] | 86 | return IsGlobalInSmallSectionImpl(GO, TM) && |
| 87 | (Kind.isData() || Kind.isBSS() || Kind.isCommon() || |
| 88 | Kind.isReadOnly()); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 89 | } |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 90 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 91 | /// Return true if this global address should be placed into small data/bss |
| 92 | /// section. This method does all the work, except for checking the section |
| 93 | /// kind. |
| 94 | bool MipsTargetObjectFile:: |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 95 | IsGlobalInSmallSectionImpl(const GlobalObject *GO, |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 96 | const TargetMachine &TM) const { |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 97 | const MipsSubtarget &Subtarget = |
| 98 | *static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl(); |
Akira Hatanaka | ad49502 | 2012-08-22 03:18:13 +0000 | [diff] [blame] | 99 | |
| 100 | // Return if small section is not available. |
| 101 | if (!Subtarget.useSmallSection()) |
Bruno Cardoso Lopes | 8bd8723 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 102 | return false; |
| 103 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 104 | // Only global variables, not functions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 105 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 106 | if (!GVA) |
| 107 | return false; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 108 | |
Simon Dardis | 9c5d64b | 2017-08-16 12:18:04 +0000 | [diff] [blame] | 109 | // If the variable has an explicit section, it is placed in that section but |
| 110 | // it's addressing mode may change. |
| 111 | if (GVA->hasSection()) { |
| 112 | StringRef Section = GVA->getSection(); |
| 113 | |
| 114 | // Explicitly placing any variable in the small data section overrides |
| 115 | // the global -G value. |
| 116 | if (Section == ".sdata" || Section == ".sbss") |
| 117 | return true; |
| 118 | |
| 119 | // Otherwise reject accessing it through the gp pointer. There are some |
| 120 | // historic cases which GCC doesn't appear to respect any more. These |
| 121 | // are .lit4, .lit8 and .srdata. For the moment reject these as well. |
| 122 | return false; |
| 123 | } |
| 124 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 125 | // Enforce -mlocal-sdata. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 126 | if (!LocalSData && GVA->hasLocalLinkage()) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 127 | return false; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 128 | |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 129 | // Enforce -mextern-sdata. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 130 | if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) || |
| 131 | GVA->hasCommonLinkage())) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 132 | return false; |
| 133 | |
Simon Dardis | 0310eb7 | 2017-07-21 17:19:00 +0000 | [diff] [blame] | 134 | // Enforce -membedded-data. |
| 135 | if (EmbeddedData && GVA->isConstant()) |
| 136 | return false; |
| 137 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 138 | Type *Ty = GVA->getValueType(); |
Alexander Richardson | 1f9636f | 2018-01-26 15:56:14 +0000 | [diff] [blame] | 139 | |
| 140 | // It is possible that the type of the global is unsized, i.e. a declaration |
| 141 | // of a extern struct. In this case don't presume it is in the small data |
| 142 | // section. This happens e.g. when building the FreeBSD kernel. |
| 143 | if (!Ty->isSized()) |
| 144 | return false; |
| 145 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 146 | return IsInSmallSection( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 147 | GVA->getParent()->getDataLayout().getTypeAllocSize(Ty)); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 150 | MCSection *MipsTargetObjectFile::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 151 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 152 | // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" |
| 153 | // sections? |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 155 | // Handle Small Section classification here. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 156 | if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind)) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 157 | return SmallBSSSection; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 158 | if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind)) |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 159 | return SmallDataSection; |
Simon Dardis | 0310eb7 | 2017-07-21 17:19:00 +0000 | [diff] [blame] | 160 | if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind)) |
| 161 | return SmallDataSection; |
Che-Liang Chiou | bb03389 | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 163 | // Otherwise, we work the same as ELF. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 164 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM); |
Chris Lattner | 68535f7 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 165 | } |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 166 | |
| 167 | /// Return true if this constant should be placed into small data section. |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 168 | bool MipsTargetObjectFile::IsConstantInSmallSection( |
| 169 | const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const { |
Eric Christopher | 948bdf9 | 2015-03-21 03:13:05 +0000 | [diff] [blame] | 170 | return (static_cast<const MipsTargetMachine &>(TM) |
| 171 | .getSubtargetImpl() |
| 172 | ->useSmallSection() && |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 173 | LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType()))); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 176 | /// Return true if this constant should be placed into small data section. |
David Majnemer | 78f46be | 2016-02-21 01:40:04 +0000 | [diff] [blame] | 177 | MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL, |
| 178 | SectionKind Kind, |
| 179 | const Constant *C, |
| 180 | unsigned &Align) const { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 181 | if (IsConstantInSmallSection(DL, C, *TM)) |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 182 | return SmallDataSection; |
| 183 | |
| 184 | // Otherwise, we work the same as ELF. |
David Majnemer | 78f46be | 2016-02-21 01:40:04 +0000 | [diff] [blame] | 185 | return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align); |
Sasa Stankovic | b38db1e | 2014-11-06 13:20:12 +0000 | [diff] [blame] | 186 | } |
Petar Jovanovic | dbb3935 | 2017-01-20 17:53:30 +0000 | [diff] [blame] | 187 | |
| 188 | const MCExpr * |
| 189 | MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { |
| 190 | const MCExpr *Expr = |
| 191 | MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Simon Atanasyan | f76884b | 2018-12-03 21:54:43 +0000 | [diff] [blame] | 192 | Expr = MCBinaryExpr::createAdd( |
Petar Jovanovic | dbb3935 | 2017-01-20 17:53:30 +0000 | [diff] [blame] | 193 | Expr, MCConstantExpr::create(0x8000, getContext()), getContext()); |
Simon Atanasyan | f76884b | 2018-12-03 21:54:43 +0000 | [diff] [blame] | 194 | return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL, Expr, getContext()); |
Petar Jovanovic | dbb3935 | 2017-01-20 17:53:30 +0000 | [diff] [blame] | 195 | } |