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