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