Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1 | //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===// |
| 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 | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements classes used to handle lowerings specific to common |
| 10 | // object file formats. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/Dwarf.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/GlobalVariable.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Mangler.h" |
Chris Lattner | 4d2c0f9 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCContext.h" |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 03627cb | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | ccbeed2 | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | d82510e | 2009-11-07 09:20:54 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | ccbeed2 | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Generic Code |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 36 | /// Initialize - this method must be called before any actual lowering is |
| 37 | /// done. This specifies the current context for codegen, and gives the |
| 38 | /// lowering implementations a chance to set up their default sections. |
| 39 | void TargetLoweringObjectFile::Initialize(MCContext &ctx, |
| 40 | const TargetMachine &TM) { |
| 41 | Ctx = &ctx; |
Eric Liu | d07ad51 | 2016-09-16 11:50:57 +0000 | [diff] [blame] | 42 | // `Initialize` can be called more than once. |
Gabor Horvath | 43b72d5 | 2017-05-01 16:18:42 +0000 | [diff] [blame] | 43 | delete Mang; |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 44 | Mang = new Mangler(); |
Rafael Espindola | 9f92995 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 45 | InitMCObjectFileInfo(TM.getTargetTriple(), TM.isPositionIndependent(), *Ctx, |
| 46 | TM.getCodeModel() == CodeModel::Large); |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 47 | |
| 48 | // Reset various EH DWARF encodings. |
| 49 | PersonalityEncoding = LSDAEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr; |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 50 | } |
Saleem Abdulrasool | 0d3d6c4 | 2014-04-16 04:15:25 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 52 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 53 | delete Mang; |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Eli Friedman | cd07a3e | 2018-02-06 23:22:14 +0000 | [diff] [blame] | 56 | static bool isNullOrUndef(const Constant *C) { |
| 57 | // Check that the constant isn't all zeros or undefs. |
| 58 | if (C->isNullValue() || isa<UndefValue>(C)) |
| 59 | return true; |
| 60 | if (!isa<ConstantAggregate>(C)) |
| 61 | return false; |
| 62 | for (auto Operand : C->operand_values()) { |
| 63 | if (!isNullOrUndef(cast<Constant>(Operand))) |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
Eric Christopher | 958a1f8 | 2018-05-27 11:39:34 +0000 | [diff] [blame] | 69 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
Jay Foad | 6002068 | 2011-06-19 18:37:11 +0000 | [diff] [blame] | 70 | const Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 72 | // Must have zero initializer. |
Eli Friedman | cd07a3e | 2018-02-06 23:22:14 +0000 | [diff] [blame] | 73 | if (!isNullOrUndef(C)) |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 74 | return false; |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 76 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 77 | if (GV->isConstant()) |
| 78 | return false; |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 80 | // If the global has an explicit section specified, don't put it in BSS. |
David Majnemer | 483e4e0 | 2014-05-17 05:18:40 +0000 | [diff] [blame] | 81 | if (GV->hasSection()) |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 82 | return false; |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 84 | // Otherwise, put it in BSS! |
| 85 | return true; |
| 86 | } |
| 87 | |
Chris Lattner | 81bbf44 | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 88 | /// IsNullTerminatedString - Return true if the specified constant (which is |
| 89 | /// known to have a type that is an array of 1/2/4 byte elements) ends with a |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 90 | /// nul value and contains no other nuls in it. Note that this is more general |
| 91 | /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. |
Chris Lattner | 81bbf44 | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 92 | static bool IsNullTerminatedString(const Constant *C) { |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 93 | // First check: is we have constant array terminated with zero |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 94 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { |
| 95 | unsigned NumElts = CDS->getNumElements(); |
| 96 | assert(NumElts != 0 && "Can't have an empty CDS"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 98 | if (CDS->getElementAsInteger(NumElts-1) != 0) |
| 99 | return false; // Not null terminated. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 101 | // Verify that the null doesn't occur anywhere else in the string. |
| 102 | for (unsigned i = 0; i != NumElts-1; ++i) |
| 103 | if (CDS->getElementAsInteger(i) == 0) |
| 104 | return false; |
| 105 | return true; |
| 106 | } |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 107 | |
| 108 | // Another possibility: [1 x i8] zeroinitializer |
| 109 | if (isa<ConstantAggregateZero>(C)) |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 110 | return cast<ArrayType>(C->getType())->getNumElements() == 1; |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
Rafael Espindola | f4e6b29 | 2013-12-02 16:25:47 +0000 | [diff] [blame] | 115 | MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 116 | const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const { |
Rafael Espindola | 117b20c | 2013-12-05 05:53:12 +0000 | [diff] [blame] | 117 | assert(!Suffix.empty()); |
Rafael Espindola | 117b20c | 2013-12-05 05:53:12 +0000 | [diff] [blame] | 118 | |
Rafael Espindola | f4e6b29 | 2013-12-02 16:25:47 +0000 | [diff] [blame] | 119 | SmallString<60> NameStr; |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 120 | NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix(); |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 121 | TM.getNameWithPrefix(NameStr, GV, *Mang); |
Rafael Espindola | f4e6b29 | 2013-12-02 16:25:47 +0000 | [diff] [blame] | 122 | NameStr.append(Suffix.begin(), Suffix.end()); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 123 | return Ctx->getOrCreateSymbol(NameStr); |
Rafael Espindola | f4e6b29 | 2013-12-02 16:25:47 +0000 | [diff] [blame] | 124 | } |
Rafael Espindola | e133ed8 | 2013-10-29 17:28:26 +0000 | [diff] [blame] | 125 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 126 | MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 127 | const GlobalValue *GV, const TargetMachine &TM, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 128 | MachineModuleInfo *MMI) const { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 129 | return TM.getSymbol(GV); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 133 | const DataLayout &, |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 134 | const MCSymbol *Sym) const { |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Chris Lattner | cbc7b26 | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 138 | /// getKindForGlobal - This is a top-level target-independent classifier for |
Eric Christopher | ed169ec | 2018-05-27 11:23:20 +0000 | [diff] [blame] | 139 | /// a global object. Given a global variable and information from the TM, this |
| 140 | /// function classifies the global in a target independent manner. This function |
| 141 | /// may be overridden by the target implementation. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 142 | SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO, |
Chris Lattner | cbc7b26 | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 143 | const TargetMachine &TM){ |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 144 | assert(!GO->isDeclaration() && !GO->hasAvailableExternallyLinkage() && |
Chris Lattner | cbc7b26 | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 145 | "Can only be used for global definitions"); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 146 | |
Eric Christopher | ed169ec | 2018-05-27 11:23:20 +0000 | [diff] [blame] | 147 | // Functions are classified as text sections. |
| 148 | if (isa<Function>(GO)) |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 149 | return SectionKind::getText(); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 150 | |
Eric Christopher | ed169ec | 2018-05-27 11:23:20 +0000 | [diff] [blame] | 151 | // Global variables require more detailed analysis. |
| 152 | const auto *GVar = cast<GlobalVariable>(GO); |
| 153 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 154 | // Handle thread-local data first. |
| 155 | if (GVar->isThreadLocal()) { |
Eric Christopher | 958a1f8 | 2018-05-27 11:39:34 +0000 | [diff] [blame] | 156 | if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 157 | return SectionKind::getThreadBSS(); |
| 158 | return SectionKind::getThreadData(); |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 5b585f8 | 2010-01-19 02:48:26 +0000 | [diff] [blame] | 161 | // Variables with common linkage always get classified as common. |
| 162 | if (GVar->hasCommonLinkage()) |
| 163 | return SectionKind::getCommon(); |
| 164 | |
Eric Christopher | 958a1f8 | 2018-05-27 11:39:34 +0000 | [diff] [blame] | 165 | // Most non-mergeable zero data can be put in the BSS section unless otherwise |
| 166 | // specified. |
| 167 | if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) { |
Chris Lattner | b253421 | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 168 | if (GVar->hasLocalLinkage()) |
| 169 | return SectionKind::getBSSLocal(); |
| 170 | else if (GVar->hasExternalLinkage()) |
| 171 | return SectionKind::getBSSExtern(); |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 172 | return SectionKind::getBSS(); |
Chris Lattner | b253421 | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 173 | } |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 175 | // If the global is marked constant, we can put it into a mergable section, |
| 176 | // a mergable string section, or general .data if it contains relocations. |
Chris Lattner | ea4e983 | 2011-01-18 01:23:44 +0000 | [diff] [blame] | 177 | if (GVar->isConstant()) { |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 178 | // If the initializer for the global contains something that requires a |
Eric Christopher | de9e92e | 2012-05-05 01:16:06 +0000 | [diff] [blame] | 179 | // relocation, then we may have to drop this into a writable data section |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 180 | // even though it is marked const. |
Eric Christopher | ed169ec | 2018-05-27 11:23:20 +0000 | [diff] [blame] | 181 | const Constant *C = GVar->getInitializer(); |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 182 | if (!C->needsRelocation()) { |
Chris Lattner | ea4e983 | 2011-01-18 01:23:44 +0000 | [diff] [blame] | 183 | // If the global is required to have a unique address, it can't be put |
| 184 | // into a mergable section: just drop it into the general read-only |
| 185 | // section instead. |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 186 | if (!GVar->hasGlobalUnnamedAddr()) |
Chris Lattner | ea4e983 | 2011-01-18 01:23:44 +0000 | [diff] [blame] | 187 | return SectionKind::getReadOnly(); |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 189 | // If initializer is a null-terminated string, put it in a "cstring" |
Chris Lattner | 81bbf44 | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 190 | // section of the right width. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 191 | if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
| 192 | if (IntegerType *ITy = |
Chris Lattner | 81bbf44 | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 193 | dyn_cast<IntegerType>(ATy->getElementType())) { |
| 194 | if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || |
| 195 | ITy->getBitWidth() == 32) && |
| 196 | IsNullTerminatedString(C)) { |
| 197 | if (ITy->getBitWidth() == 8) |
| 198 | return SectionKind::getMergeable1ByteCString(); |
| 199 | if (ITy->getBitWidth() == 16) |
| 200 | return SectionKind::getMergeable2ByteCString(); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 81bbf44 | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 202 | assert(ITy->getBitWidth() == 32 && "Unknown width"); |
| 203 | return SectionKind::getMergeable4ByteCString(); |
| 204 | } |
| 205 | } |
| 206 | } |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 208 | // Otherwise, just drop it into a mergable constant section. If we have |
| 209 | // a section for this size, use it, otherwise use the arbitrary sized |
| 210 | // mergable section. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 211 | switch ( |
| 212 | GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) { |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 213 | case 4: return SectionKind::getMergeableConst4(); |
| 214 | case 8: return SectionKind::getMergeableConst8(); |
| 215 | case 16: return SectionKind::getMergeableConst16(); |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 216 | case 32: return SectionKind::getMergeableConst32(); |
Rafael Espindola | 33804ca | 2015-01-29 14:12:41 +0000 | [diff] [blame] | 217 | default: |
| 218 | return SectionKind::getReadOnly(); |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 219 | } |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 220 | |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 221 | } else { |
Oliver Stannard | 8331aae | 2016-08-08 15:28:31 +0000 | [diff] [blame] | 222 | // In static, ROPI and RWPI relocation models, the linker will resolve |
| 223 | // all addresses, so the relocation entries will actually be constants by |
| 224 | // the time the app starts up. However, we can't put this into a |
| 225 | // mergable section, because the linker doesn't take relocations into |
| 226 | // consideration when it tries to merge entries in the section. |
Eric Christopher | ed169ec | 2018-05-27 11:23:20 +0000 | [diff] [blame] | 227 | Reloc::Model ReloModel = TM.getRelocationModel(); |
Oliver Stannard | 8331aae | 2016-08-08 15:28:31 +0000 | [diff] [blame] | 228 | if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI || |
| 229 | ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI) |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 230 | return SectionKind::getReadOnly(); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 232 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 233 | // writable data.rel section. |
Chris Lattner | f8d9710 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 234 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Rafael Espindola | a686f12 | 2016-06-24 22:19:54 +0000 | [diff] [blame] | 238 | // Okay, this isn't a constant. |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 239 | return SectionKind::getData(); |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 242 | /// This method computes the appropriate section to emit the specified global |
| 243 | /// variable or function definition. This should not be passed external (or |
| 244 | /// available externally) globals. |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 245 | MCSection *TargetLoweringObjectFile::SectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 246 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 247 | // Select section name. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 248 | if (GO->hasSection()) |
| 249 | return getExplicitSectionGlobal(GO, Kind, TM); |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 250 | |
Javed Absar | b16d146 | 2017-06-05 10:09:13 +0000 | [diff] [blame] | 251 | if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { |
| 252 | auto Attrs = GVar->getAttributes(); |
| 253 | if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) || |
| 254 | (Attrs.hasAttribute("data-section") && Kind.isData()) || |
| 255 | (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) { |
| 256 | return getExplicitSectionGlobal(GO, Kind, TM); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (auto *F = dyn_cast<Function>(GO)) { |
| 261 | if (F->hasFnAttribute("implicit-section-name")) |
| 262 | return getExplicitSectionGlobal(GO, Kind, TM); |
| 263 | } |
| 264 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 265 | // Use default section depending on the 'type' of global |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 266 | return SelectSectionForGlobal(GO, Kind, TM); |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 269 | MCSection *TargetLoweringObjectFile::getSectionForJumpTable( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 270 | const Function &F, const TargetMachine &TM) const { |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 271 | unsigned Align = 0; |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 272 | return getSectionForConstant(F.getParent()->getDataLayout(), |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 273 | SectionKind::getReadOnly(), /*C=*/nullptr, |
| 274 | Align); |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rafael Espindola | df19519 | 2015-02-17 23:34:51 +0000 | [diff] [blame] | 277 | bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection( |
| 278 | bool UsesLabelDifference, const Function &F) const { |
| 279 | // In PIC mode, we need to emit the jump table to the same section as the |
| 280 | // function body itself, otherwise the label differences won't make sense. |
| 281 | // FIXME: Need a better predicate for this: what about custom entries? |
| 282 | if (UsesLabelDifference) |
| 283 | return true; |
| 284 | |
| 285 | // We should also do if the section name is NULL or function is declared |
| 286 | // in discardable section |
| 287 | // FIXME: this isn't the right predicate, should be based on the MCSection |
| 288 | // for the function. |
Davide Italiano | 76de68e | 2017-01-14 20:09:29 +0000 | [diff] [blame] | 289 | return F.isWeakForLinker(); |
Rafael Espindola | df19519 | 2015-02-17 23:34:51 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 292 | /// Given a mergable constant with the specified size and relocation |
| 293 | /// information, return a section that it should be placed in. |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 294 | MCSection *TargetLoweringObjectFile::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 295 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 296 | unsigned &Align) const { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 297 | if (Kind.isReadOnly() && ReadOnlySection != nullptr) |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 298 | return ReadOnlySection; |
Anton Korobeynikov | 1df5886 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 300 | return DataSection; |
| 301 | } |
| 302 | |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 303 | /// getTTypeGlobalReference - Return an MCExpr to use for a |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 304 | /// reference to the specified global variable from exception |
| 305 | /// handling information. |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 306 | const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 307 | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
| 308 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 309 | const MCSymbolRefExpr *Ref = |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 310 | MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 311 | |
| 312 | return getTTypeReference(Ref, Encoding, Streamer); |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 5e693ed | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 314 | |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 315 | const MCExpr *TargetLoweringObjectFile:: |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 316 | getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, |
| 317 | MCStreamer &Streamer) const { |
Rafael Espindola | a01cdb0 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 318 | switch (Encoding & 0x70) { |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 319 | default: |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 320 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 321 | case dwarf::DW_EH_PE_absptr: |
| 322 | // Do nothing special |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 323 | return Sym; |
Chris Lattner | 03627cb | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 324 | case dwarf::DW_EH_PE_pcrel: { |
| 325 | // Emit a label to the streamer for the current position. This gives us |
| 326 | // .-foo addressing. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 327 | MCSymbol *PCSym = getContext().createTempSymbol(); |
Chris Lattner | 03627cb | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 328 | Streamer.EmitLabel(PCSym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 329 | const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext()); |
| 330 | return MCBinaryExpr::createSub(Sym, PC, getContext()); |
Chris Lattner | 03627cb | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 333 | } |
David Blaikie | f269497 | 2013-06-28 20:05:11 +0000 | [diff] [blame] | 334 | |
Ulrich Weigand | 2b6fc8d | 2013-07-02 18:47:09 +0000 | [diff] [blame] | 335 | const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { |
David Blaikie | f269497 | 2013-06-28 20:05:11 +0000 | [diff] [blame] | 336 | // FIXME: It's not clear what, if any, default this should have - perhaps a |
| 337 | // null return could mean 'no location' & we should just do that here. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 338 | return MCSymbolRefExpr::create(Sym, *Ctx); |
David Blaikie | f269497 | 2013-06-28 20:05:11 +0000 | [diff] [blame] | 339 | } |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 340 | |
| 341 | void TargetLoweringObjectFile::getNameWithPrefix( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 342 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 343 | const TargetMachine &TM) const { |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 344 | Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 345 | } |