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 | |
| 15 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
Dan Gohman | ffef8ac | 2009-08-11 16:02:12 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 8da8d4b | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 24 | #include "llvm/Target/Mangler.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5277b22 | 2009-08-08 20:43:12 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Dwarf.h" |
Chris Lattner | 8f9b0f6 | 2009-11-07 09:20:54 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 8da8d4b | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Generic Code |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 38 | /// Initialize - this method must be called before any actual lowering is |
| 39 | /// done. This specifies the current context for codegen, and gives the |
| 40 | /// lowering implementations a chance to set up their default sections. |
| 41 | void TargetLoweringObjectFile::Initialize(MCContext &ctx, |
| 42 | const TargetMachine &TM) { |
| 43 | Ctx = &ctx; |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 44 | InitMCObjectFileInfo(TM.getTargetTriple(), |
| 45 | TM.getRelocationModel(), TM.getCodeModel(), *Ctx); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 46 | } |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 48 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
| 49 | } |
| 50 | |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 51 | static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { |
Jay Foad | 7d715df | 2011-06-19 18:37:11 +0000 | [diff] [blame] | 52 | const Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 53 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 54 | // Must have zero initializer. |
| 55 | if (!C->isNullValue()) |
| 56 | return false; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 57 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 58 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 59 | if (GV->isConstant()) |
| 60 | return false; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 61 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 62 | // If the global has an explicit section specified, don't put it in BSS. |
| 63 | if (!GV->getSection().empty()) |
| 64 | return false; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 66 | // If -nozero-initialized-in-bss is specified, don't ever use BSS. |
| 67 | if (NoZerosInBSS) |
| 68 | return false; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 69 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 70 | // Otherwise, put it in BSS! |
| 71 | return true; |
| 72 | } |
| 73 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 74 | /// IsNullTerminatedString - Return true if the specified constant (which is |
| 75 | /// 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^] | 76 | /// nul value and contains no other nuls in it. Note that this is more general |
| 77 | /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 78 | static bool IsNullTerminatedString(const Constant *C) { |
Chris Lattner | 29cc6cb | 2012-01-24 14:17:05 +0000 | [diff] [blame^] | 79 | // First check: is we have constant array terminated with zero |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 80 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) { |
Chris Lattner | 29cc6cb | 2012-01-24 14:17:05 +0000 | [diff] [blame^] | 81 | ArrayType *ATy = cast<ArrayType>(C->getType()); |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 82 | if (ATy->getNumElements() == 0) return false; |
| 83 | |
| 84 | ConstantInt *Null = |
| 85 | dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1)); |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 86 | if (Null == 0 || !Null->isZero()) |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 87 | return false; // Not null terminated. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 89 | // Verify that the null doesn't occur anywhere else in the string. |
| 90 | for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i) |
| 91 | // Reject constantexpr elements etc. |
| 92 | if (!isa<ConstantInt>(CVA->getOperand(i)) || |
| 93 | CVA->getOperand(i) == Null) |
| 94 | return false; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 95 | return true; |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 96 | } |
Chris Lattner | 29cc6cb | 2012-01-24 14:17:05 +0000 | [diff] [blame^] | 97 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { |
| 98 | unsigned NumElts = CDS->getNumElements(); |
| 99 | assert(NumElts != 0 && "Can't have an empty CDS"); |
| 100 | |
| 101 | if (CDS->getElementAsInteger(NumElts-1) != 0) |
| 102 | return false; // Not null terminated. |
| 103 | |
| 104 | // Verify that the null doesn't occur anywhere else in the string. |
| 105 | for (unsigned i = 0; i != NumElts-1; ++i) |
| 106 | if (CDS->getElementAsInteger(i) == 0) |
| 107 | return false; |
| 108 | return true; |
| 109 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 110 | |
| 111 | // Another possibility: [1 x i8] zeroinitializer |
| 112 | if (isa<ConstantAggregateZero>(C)) |
Chris Lattner | 29cc6cb | 2012-01-24 14:17:05 +0000 | [diff] [blame^] | 113 | return cast<ArrayType>(C->getType())->getNumElements() == 1; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 118 | MCSymbol *TargetLoweringObjectFile:: |
Rafael Espindola | 60246a9 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 119 | getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang, |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 120 | MachineModuleInfo *MMI) const { |
| 121 | return Mang->getSymbol(GV); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, |
| 125 | const TargetMachine &TM, |
| 126 | const MCSymbol *Sym) const { |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 130 | /// getKindForGlobal - This is a top-level target-independent classifier for |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 131 | /// a global variable. Given an global variable and information from TM, it |
| 132 | /// classifies the global in a variety of ways that make various target |
| 133 | /// implementations simpler. The target implementation is free to ignore this |
| 134 | /// extra info of course. |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 135 | SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, |
| 136 | const TargetMachine &TM){ |
| 137 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && |
| 138 | "Can only be used for global definitions"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 139 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 140 | Reloc::Model ReloModel = TM.getRelocationModel(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 141 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 142 | // Early exit - functions should be always in text sections. |
| 143 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 144 | if (GVar == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 145 | return SectionKind::getText(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 146 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 147 | // Handle thread-local data first. |
| 148 | if (GVar->isThreadLocal()) { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 149 | if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 150 | return SectionKind::getThreadBSS(); |
| 151 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | a3839bc | 2010-01-19 02:48:26 +0000 | [diff] [blame] | 154 | // Variables with common linkage always get classified as common. |
| 155 | if (GVar->hasCommonLinkage()) |
| 156 | return SectionKind::getCommon(); |
| 157 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 158 | // Variable can be easily put to BSS section. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 159 | if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) { |
Chris Lattner | ce8749e | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 160 | if (GVar->hasLocalLinkage()) |
| 161 | return SectionKind::getBSSLocal(); |
| 162 | else if (GVar->hasExternalLinkage()) |
| 163 | return SectionKind::getBSSExtern(); |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 164 | return SectionKind::getBSS(); |
Chris Lattner | ce8749e | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 166 | |
Jay Foad | 7d715df | 2011-06-19 18:37:11 +0000 | [diff] [blame] | 167 | const Constant *C = GVar->getInitializer(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 168 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 169 | // If the global is marked constant, we can put it into a mergable section, |
| 170 | // a mergable string section, or general .data if it contains relocations. |
Chris Lattner | 3289919 | 2011-01-18 01:23:44 +0000 | [diff] [blame] | 171 | if (GVar->isConstant()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 172 | // If the initializer for the global contains something that requires a |
| 173 | // relocation, then we may have to drop this into a wriable data section |
| 174 | // even though it is marked const. |
| 175 | switch (C->getRelocationInfo()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 176 | case Constant::NoRelocation: |
Chris Lattner | 3289919 | 2011-01-18 01:23:44 +0000 | [diff] [blame] | 177 | // If the global is required to have a unique address, it can't be put |
| 178 | // into a mergable section: just drop it into the general read-only |
| 179 | // section instead. |
| 180 | if (!GVar->hasUnnamedAddr()) |
| 181 | return SectionKind::getReadOnly(); |
| 182 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 183 | // If initializer is a null-terminated string, put it in a "cstring" |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 184 | // section of the right width. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 185 | if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
| 186 | if (IntegerType *ITy = |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 187 | dyn_cast<IntegerType>(ATy->getElementType())) { |
| 188 | if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || |
| 189 | ITy->getBitWidth() == 32) && |
| 190 | IsNullTerminatedString(C)) { |
| 191 | if (ITy->getBitWidth() == 8) |
| 192 | return SectionKind::getMergeable1ByteCString(); |
| 193 | if (ITy->getBitWidth() == 16) |
| 194 | return SectionKind::getMergeable2ByteCString(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 196 | assert(ITy->getBitWidth() == 32 && "Unknown width"); |
| 197 | return SectionKind::getMergeable4ByteCString(); |
| 198 | } |
| 199 | } |
| 200 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 201 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 202 | // Otherwise, just drop it into a mergable constant section. If we have |
| 203 | // a section for this size, use it, otherwise use the arbitrary sized |
| 204 | // mergable section. |
| 205 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 206 | case 4: return SectionKind::getMergeableConst4(); |
| 207 | case 8: return SectionKind::getMergeableConst8(); |
| 208 | case 16: return SectionKind::getMergeableConst16(); |
| 209 | default: return SectionKind::getMergeableConst(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 210 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 211 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 212 | case Constant::LocalRelocation: |
| 213 | // In static relocation model, the linker will resolve all addresses, so |
| 214 | // the relocation entries will actually be constants by the time the app |
| 215 | // starts up. However, we can't put this into a mergable section, because |
| 216 | // the linker doesn't take relocations into consideration when it tries to |
| 217 | // merge entries in the section. |
| 218 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 219 | return SectionKind::getReadOnly(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 220 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 221 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 222 | // writable data.rel.local section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 223 | return SectionKind::getReadOnlyWithRelLocal(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 224 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 225 | case Constant::GlobalRelocations: |
| 226 | // In static relocation model, the linker will resolve all addresses, so |
| 227 | // the relocation entries will actually be constants by the time the app |
| 228 | // starts up. However, we can't put this into a mergable section, because |
| 229 | // the linker doesn't take relocations into consideration when it tries to |
| 230 | // merge entries in the section. |
| 231 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 232 | return SectionKind::getReadOnly(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 233 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 234 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 235 | // writable data.rel section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 236 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | // Okay, this isn't a constant. If the initializer for the global is going |
| 241 | // to require a runtime relocation by the dynamic linker, put it into a more |
| 242 | // specific section to improve startup time of the app. This coalesces these |
| 243 | // globals together onto fewer pages, improving the locality of the dynamic |
| 244 | // linker. |
| 245 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 246 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 247 | |
| 248 | switch (C->getRelocationInfo()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 249 | case Constant::NoRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 250 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 251 | case Constant::LocalRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 252 | return SectionKind::getDataRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 253 | case Constant::GlobalRelocations: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 254 | return SectionKind::getDataRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 255 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 256 | llvm_unreachable("Invalid relocation"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /// SectionForGlobal - This method computes the appropriate section to emit |
| 260 | /// the specified global variable or function definition. This should not |
| 261 | /// be passed external (or available externally) globals. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 262 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 263 | SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 264 | const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 265 | // Select section name. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 266 | if (GV->hasSection()) |
| 267 | return getExplicitSectionGlobal(GV, Kind, Mang, TM); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 268 | |
| 269 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 270 | // Use default section depending on the 'type' of global |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 271 | return SelectSectionForGlobal(GV, Kind, Mang, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 274 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 275 | // Lame default implementation. Calculate the section name for global. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 276 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 277 | TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 278 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 279 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 280 | const TargetMachine &TM) const{ |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 281 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 282 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 283 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 284 | return getTextSection(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 286 | if (Kind.isBSS() && BSSSection != 0) |
| 287 | return BSSSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 288 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 289 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 290 | return ReadOnlySection; |
| 291 | |
| 292 | return getDataSection(); |
| 293 | } |
| 294 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 295 | /// getSectionForConstant - Given a mergable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 296 | /// specified size and relocation information, return a section that it |
| 297 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 298 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 299 | TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 300 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
| 301 | return ReadOnlySection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 302 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 303 | return DataSection; |
| 304 | } |
| 305 | |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 306 | /// getExprForDwarfGlobalReference - Return an MCExpr to use for a |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 307 | /// reference to the specified global variable from exception |
| 308 | /// handling information. |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 309 | const MCExpr *TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 310 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 311 | MachineModuleInfo *MMI, unsigned Encoding, |
| 312 | MCStreamer &Streamer) const { |
Chris Lattner | 73ff564 | 2010-03-12 18:55:20 +0000 | [diff] [blame] | 313 | const MCSymbol *Sym = Mang->getSymbol(GV); |
Rafael Espindola | 4788c3e | 2011-04-20 03:08:09 +0000 | [diff] [blame] | 314 | return getExprForDwarfReference(Sym, Encoding, Streamer); |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 315 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 316 | |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 317 | const MCExpr *TargetLoweringObjectFile:: |
Rafael Espindola | 4788c3e | 2011-04-20 03:08:09 +0000 | [diff] [blame] | 318 | getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding, |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 319 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 320 | const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext()); |
| 321 | |
Rafael Espindola | f0adba9 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 322 | switch (Encoding & 0x70) { |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 323 | default: |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 324 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 325 | case dwarf::DW_EH_PE_absptr: |
| 326 | // Do nothing special |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 327 | return Res; |
| 328 | case dwarf::DW_EH_PE_pcrel: { |
| 329 | // Emit a label to the streamer for the current position. This gives us |
| 330 | // .-foo addressing. |
Chris Lattner | 77e7694 | 2010-03-17 05:41:18 +0000 | [diff] [blame] | 331 | MCSymbol *PCSym = getContext().CreateTempSymbol(); |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 332 | Streamer.EmitLabel(PCSym); |
| 333 | const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext()); |
| 334 | return MCBinaryExpr::CreateSub(Res, PC, getContext()); |
| 335 | } |
| 336 | } |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 337 | } |