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 | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSectionMachO.h" |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | 8da8d4b | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 25 | #include "llvm/Target/Mangler.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5277b22 | 2009-08-08 20:43:12 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetOptions.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 | #include "llvm/ADT/StringExtras.h" |
| 33 | using namespace llvm; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Generic Code |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 39 | TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 40 | TextSection = 0; |
| 41 | DataSection = 0; |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 42 | BSSSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 43 | ReadOnlySection = 0; |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 44 | StaticCtorSection = 0; |
| 45 | StaticDtorSection = 0; |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 46 | LSDASection = 0; |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 47 | EHFrameSection = 0; |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 48 | |
| 49 | DwarfAbbrevSection = 0; |
| 50 | DwarfInfoSection = 0; |
| 51 | DwarfLineSection = 0; |
| 52 | DwarfFrameSection = 0; |
| 53 | DwarfPubNamesSection = 0; |
| 54 | DwarfPubTypesSection = 0; |
| 55 | DwarfDebugInlineSection = 0; |
| 56 | DwarfStrSection = 0; |
| 57 | DwarfLocSection = 0; |
| 58 | DwarfARangesSection = 0; |
| 59 | DwarfRangesSection = 0; |
| 60 | DwarfMacroInfoSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
| 64 | } |
| 65 | |
| 66 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 67 | Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 68 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 69 | // Must have zero initializer. |
| 70 | if (!C->isNullValue()) |
| 71 | return false; |
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 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 74 | if (GV->isConstant()) |
| 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 | // If the global has an explicit section specified, don't put it in BSS. |
| 78 | if (!GV->getSection().empty()) |
| 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 -nozero-initialized-in-bss is specified, don't ever use BSS. |
| 82 | if (NoZerosInBSS) |
| 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 |
| 91 | /// nul value and contains no other nuls in it. |
| 92 | static bool IsNullTerminatedString(const Constant *C) { |
| 93 | const ArrayType *ATy = cast<ArrayType>(C->getType()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 94 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 95 | // First check: is we have constant array of i8 terminated with zero |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 96 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) { |
| 97 | if (ATy->getNumElements() == 0) return false; |
| 98 | |
| 99 | ConstantInt *Null = |
| 100 | dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1)); |
| 101 | if (Null == 0 || Null->getZExtValue() != 0) |
| 102 | return false; // Not null terminated. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 104 | // Verify that the null doesn't occur anywhere else in the string. |
| 105 | for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i) |
| 106 | // Reject constantexpr elements etc. |
| 107 | if (!isa<ConstantInt>(CVA->getOperand(i)) || |
| 108 | CVA->getOperand(i) == Null) |
| 109 | return false; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 110 | return true; |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 112 | |
| 113 | // Another possibility: [1 x i8] zeroinitializer |
| 114 | if (isa<ConstantAggregateZero>(C)) |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 115 | return ATy->getNumElements() == 1; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 120 | /// getKindForGlobal - This is a top-level target-independent classifier for |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 121 | /// a global variable. Given an global variable and information from TM, it |
| 122 | /// classifies the global in a variety of ways that make various target |
| 123 | /// implementations simpler. The target implementation is free to ignore this |
| 124 | /// extra info of course. |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 125 | SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, |
| 126 | const TargetMachine &TM){ |
| 127 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && |
| 128 | "Can only be used for global definitions"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 129 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 130 | Reloc::Model ReloModel = TM.getRelocationModel(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 131 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 132 | // Early exit - functions should be always in text sections. |
| 133 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 134 | if (GVar == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 135 | return SectionKind::getText(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 136 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 137 | // Handle thread-local data first. |
| 138 | if (GVar->isThreadLocal()) { |
| 139 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 140 | return SectionKind::getThreadBSS(); |
| 141 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | a3839bc | 2010-01-19 02:48:26 +0000 | [diff] [blame] | 144 | // Variables with common linkage always get classified as common. |
| 145 | if (GVar->hasCommonLinkage()) |
| 146 | return SectionKind::getCommon(); |
| 147 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 148 | // Variable can be easily put to BSS section. |
Chris Lattner | ce8749e | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 149 | if (isSuitableForBSS(GVar)) { |
| 150 | if (GVar->hasLocalLinkage()) |
| 151 | return SectionKind::getBSSLocal(); |
| 152 | else if (GVar->hasExternalLinkage()) |
| 153 | return SectionKind::getBSSExtern(); |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 154 | return SectionKind::getBSS(); |
Chris Lattner | ce8749e | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 155 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 156 | |
| 157 | Constant *C = GVar->getInitializer(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 158 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 159 | // If the global is marked constant, we can put it into a mergable section, |
| 160 | // a mergable string section, or general .data if it contains relocations. |
| 161 | if (GVar->isConstant()) { |
| 162 | // If the initializer for the global contains something that requires a |
| 163 | // relocation, then we may have to drop this into a wriable data section |
| 164 | // even though it is marked const. |
| 165 | switch (C->getRelocationInfo()) { |
Chris Lattner | 8f9b0f6 | 2009-11-07 09:20:54 +0000 | [diff] [blame] | 166 | default: assert(0 && "unknown relocation info kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 167 | case Constant::NoRelocation: |
| 168 | // If initializer is a null-terminated string, put it in a "cstring" |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 169 | // section of the right width. |
| 170 | if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 171 | if (const IntegerType *ITy = |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 172 | dyn_cast<IntegerType>(ATy->getElementType())) { |
| 173 | if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || |
| 174 | ITy->getBitWidth() == 32) && |
| 175 | IsNullTerminatedString(C)) { |
| 176 | if (ITy->getBitWidth() == 8) |
| 177 | return SectionKind::getMergeable1ByteCString(); |
| 178 | if (ITy->getBitWidth() == 16) |
| 179 | return SectionKind::getMergeable2ByteCString(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 181 | assert(ITy->getBitWidth() == 32 && "Unknown width"); |
| 182 | return SectionKind::getMergeable4ByteCString(); |
| 183 | } |
| 184 | } |
| 185 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 186 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 187 | // Otherwise, just drop it into a mergable constant section. If we have |
| 188 | // a section for this size, use it, otherwise use the arbitrary sized |
| 189 | // mergable section. |
| 190 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 191 | case 4: return SectionKind::getMergeableConst4(); |
| 192 | case 8: return SectionKind::getMergeableConst8(); |
| 193 | case 16: return SectionKind::getMergeableConst16(); |
| 194 | default: return SectionKind::getMergeableConst(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 195 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 196 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 197 | case Constant::LocalRelocation: |
| 198 | // In static relocation model, the linker will resolve all addresses, so |
| 199 | // the relocation entries will actually be constants by the time the app |
| 200 | // starts up. However, we can't put this into a mergable section, because |
| 201 | // the linker doesn't take relocations into consideration when it tries to |
| 202 | // merge entries in the section. |
| 203 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 204 | return SectionKind::getReadOnly(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 205 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 206 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 207 | // writable data.rel.local section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 208 | return SectionKind::getReadOnlyWithRelLocal(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 209 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 210 | case Constant::GlobalRelocations: |
| 211 | // In static relocation model, the linker will resolve all addresses, so |
| 212 | // the relocation entries will actually be constants by the time the app |
| 213 | // starts up. However, we can't put this into a mergable section, because |
| 214 | // the linker doesn't take relocations into consideration when it tries to |
| 215 | // merge entries in the section. |
| 216 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 217 | return SectionKind::getReadOnly(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 218 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 219 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 220 | // writable data.rel section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 221 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | // Okay, this isn't a constant. If the initializer for the global is going |
| 226 | // to require a runtime relocation by the dynamic linker, put it into a more |
| 227 | // specific section to improve startup time of the app. This coalesces these |
| 228 | // globals together onto fewer pages, improving the locality of the dynamic |
| 229 | // linker. |
| 230 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 231 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 232 | |
| 233 | switch (C->getRelocationInfo()) { |
Chris Lattner | 8f9b0f6 | 2009-11-07 09:20:54 +0000 | [diff] [blame] | 234 | default: assert(0 && "unknown relocation info kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 235 | case Constant::NoRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 236 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 237 | case Constant::LocalRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 238 | return SectionKind::getDataRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 239 | case Constant::GlobalRelocations: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 240 | return SectionKind::getDataRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | /// SectionForGlobal - This method computes the appropriate section to emit |
| 245 | /// the specified global variable or function definition. This should not |
| 246 | /// be passed external (or available externally) globals. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 247 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 248 | SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 249 | const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 250 | // Select section name. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 251 | if (GV->hasSection()) |
| 252 | return getExplicitSectionGlobal(GV, Kind, Mang, TM); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 253 | |
| 254 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 255 | // Use default section depending on the 'type' of global |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 256 | return SelectSectionForGlobal(GV, Kind, Mang, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 259 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 260 | // Lame default implementation. Calculate the section name for global. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 261 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 262 | TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 263 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 264 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 265 | const TargetMachine &TM) const{ |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 266 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 267 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 268 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 269 | return getTextSection(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 271 | if (Kind.isBSS() && BSSSection != 0) |
| 272 | return BSSSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 273 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 274 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 275 | return ReadOnlySection; |
| 276 | |
| 277 | return getDataSection(); |
| 278 | } |
| 279 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 280 | /// getSectionForConstant - Given a mergable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 281 | /// specified size and relocation information, return a section that it |
| 282 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 283 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 284 | TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 285 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
| 286 | return ReadOnlySection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 287 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 288 | return DataSection; |
| 289 | } |
| 290 | |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 291 | /// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a |
| 292 | /// pc-relative reference to the specified global variable from exception |
| 293 | /// handling information. In addition to the symbol, this returns |
| 294 | /// by-reference: |
| 295 | /// |
| 296 | /// IsIndirect - True if the returned symbol is actually a stub that contains |
| 297 | /// the address of the symbol, false if the symbol is the global itself. |
| 298 | /// |
| 299 | /// IsPCRel - True if the symbol reference is already pc-relative, false if |
| 300 | /// the caller needs to subtract off the address of the reference from the |
| 301 | /// symbol. |
| 302 | /// |
| 303 | const MCExpr *TargetLoweringObjectFile:: |
| 304 | getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
Chris Lattner | 8609c7c | 2009-09-17 18:49:52 +0000 | [diff] [blame] | 305 | MachineModuleInfo *MMI, |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 306 | bool &IsIndirect, bool &IsPCRel) const { |
| 307 | // The generic implementation of this just returns a direct reference to the |
| 308 | // symbol. |
| 309 | IsIndirect = false; |
| 310 | IsPCRel = false; |
| 311 | |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 312 | // FIXME: Use GetGlobalValueSymbol. |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 313 | SmallString<128> Name; |
| 314 | Mang->getNameWithPrefix(Name, GV, false); |
| 315 | return MCSymbolRefExpr::Create(Name.str(), getContext()); |
| 316 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 317 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 318 | |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | // ELF |
| 321 | //===----------------------------------------------------------------------===// |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 322 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
| 323 | |
| 324 | TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() { |
| 325 | // If we have the section uniquing map, free it. |
| 326 | delete (ELFUniqueMapTy*)UniquingMap; |
| 327 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 328 | |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 329 | const MCSection *TargetLoweringObjectFileELF:: |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 330 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 331 | SectionKind Kind, bool IsExplicit) const { |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 332 | if (UniquingMap == 0) |
| 333 | UniquingMap = new ELFUniqueMapTy(); |
| 334 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 336 | // Do the lookup, if we have a hit, return it. |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 337 | const MCSectionELF *&Entry = Map[Section]; |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 338 | if (Entry) return Entry; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 339 | |
Bruno Cardoso Lopes | fdf229e | 2009-08-13 23:30:21 +0000 | [diff] [blame] | 340 | return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit, |
| 341 | getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 344 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 345 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 346 | if (UniquingMap != 0) |
| 347 | ((ELFUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 348 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 349 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 350 | BSSSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 351 | getELFSection(".bss", MCSectionELF::SHT_NOBITS, |
| 352 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 353 | SectionKind::getBSS()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 354 | |
| 355 | TextSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 356 | getELFSection(".text", MCSectionELF::SHT_PROGBITS, |
| 357 | MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC, |
| 358 | SectionKind::getText()); |
| 359 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 360 | DataSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 361 | getELFSection(".data", MCSectionELF::SHT_PROGBITS, |
| 362 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 363 | SectionKind::getDataRel()); |
| 364 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 365 | ReadOnlySection = |
| 366 | getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, |
| 367 | MCSectionELF::SHF_ALLOC, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 368 | SectionKind::getReadOnly()); |
| 369 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 370 | TLSDataSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 371 | getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 372 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 373 | MCSectionELF::SHF_WRITE, SectionKind::getThreadData()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 374 | |
| 375 | TLSBSSSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 376 | getELFSection(".tbss", MCSectionELF::SHT_NOBITS, |
| 377 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 378 | MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 379 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 380 | DataRelSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 381 | getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, |
| 382 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 383 | SectionKind::getDataRel()); |
| 384 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 385 | DataRelLocalSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 386 | getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, |
| 387 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 388 | SectionKind::getDataRelLocal()); |
| 389 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 390 | DataRelROSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 391 | getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, |
| 392 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 393 | SectionKind::getReadOnlyWithRel()); |
| 394 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 395 | DataRelROLocalSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 396 | getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, |
| 397 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 398 | SectionKind::getReadOnlyWithRelLocal()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 399 | |
| 400 | MergeableConst4Section = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 401 | getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, |
| 402 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 403 | SectionKind::getMergeableConst4()); |
| 404 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 405 | MergeableConst8Section = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 406 | getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, |
| 407 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 408 | SectionKind::getMergeableConst8()); |
| 409 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 410 | MergeableConst16Section = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 411 | getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, |
| 412 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 413 | SectionKind::getMergeableConst16()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 414 | |
| 415 | StaticCtorSection = |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 416 | getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 417 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 418 | SectionKind::getDataRel()); |
| 419 | |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 420 | StaticDtorSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 421 | getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, |
| 422 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 423 | SectionKind::getDataRel()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 425 | // Exception Handling Sections. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 426 | |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 427 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 428 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 429 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 430 | // adjusted or this should be a data section. |
| 431 | LSDASection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 432 | getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, |
| 433 | MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 434 | EHFrameSection = |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 435 | getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, |
Chris Lattner | b6ab299 | 2009-08-15 16:54:02 +0000 | [diff] [blame] | 436 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 437 | SectionKind::getDataRel()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 439 | // Debug Info Sections. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 440 | DwarfAbbrevSection = |
| 441 | getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 442 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 443 | DwarfInfoSection = |
| 444 | getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 445 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 446 | DwarfLineSection = |
| 447 | getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 448 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 449 | DwarfFrameSection = |
| 450 | getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 451 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 452 | DwarfPubNamesSection = |
| 453 | getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 454 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 455 | DwarfPubTypesSection = |
| 456 | getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 457 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 458 | DwarfStrSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 459 | getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, |
| 460 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 461 | DwarfLocSection = |
| 462 | getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 463 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 464 | DwarfARangesSection = |
| 465 | getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 466 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 467 | DwarfRangesSection = |
| 468 | getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 469 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 470 | DwarfMacroInfoSection = |
| 471 | getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 472 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 476 | static SectionKind |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 477 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
| 478 | if (Name.empty() || Name[0] != '.') return K; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 479 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 480 | // Some lame default implementation based on some magic section names. |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 481 | if (Name == ".bss" || |
| 482 | Name.startswith(".bss.") || |
| 483 | Name.startswith(".gnu.linkonce.b.") || |
| 484 | Name.startswith(".llvm.linkonce.b.") || |
| 485 | Name == ".sbss" || |
| 486 | Name.startswith(".sbss.") || |
| 487 | Name.startswith(".gnu.linkonce.sb.") || |
| 488 | Name.startswith(".llvm.linkonce.sb.")) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 489 | return SectionKind::getBSS(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 490 | |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 491 | if (Name == ".tdata" || |
| 492 | Name.startswith(".tdata.") || |
| 493 | Name.startswith(".gnu.linkonce.td.") || |
| 494 | Name.startswith(".llvm.linkonce.td.")) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 495 | return SectionKind::getThreadData(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 496 | |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 497 | if (Name == ".tbss" || |
| 498 | Name.startswith(".tbss.") || |
| 499 | Name.startswith(".gnu.linkonce.tb.") || |
| 500 | Name.startswith(".llvm.linkonce.tb.")) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 501 | return SectionKind::getThreadBSS(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 502 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 503 | return K; |
| 504 | } |
| 505 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 507 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 509 | if (Name == ".init_array") |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 510 | return MCSectionELF::SHT_INIT_ARRAY; |
| 511 | |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 512 | if (Name == ".fini_array") |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 513 | return MCSectionELF::SHT_FINI_ARRAY; |
| 514 | |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 515 | if (Name == ".preinit_array") |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 516 | return MCSectionELF::SHT_PREINIT_ARRAY; |
| 517 | |
| 518 | if (K.isBSS() || K.isThreadBSS()) |
| 519 | return MCSectionELF::SHT_NOBITS; |
| 520 | |
| 521 | return MCSectionELF::SHT_PROGBITS; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | static unsigned |
| 526 | getELFSectionFlags(SectionKind K) { |
| 527 | unsigned Flags = 0; |
| 528 | |
| 529 | if (!K.isMetadata()) |
| 530 | Flags |= MCSectionELF::SHF_ALLOC; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 531 | |
Anton Korobeynikov | 848c293 | 2009-08-18 14:06:12 +0000 | [diff] [blame] | 532 | if (K.isText()) |
| 533 | Flags |= MCSectionELF::SHF_EXECINSTR; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 534 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 535 | if (K.isWriteable()) |
| 536 | Flags |= MCSectionELF::SHF_WRITE; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 537 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 538 | if (K.isThreadLocal()) |
| 539 | Flags |= MCSectionELF::SHF_TLS; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 540 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 541 | // K.isMergeableConst() is left out to honour PR4650 |
| 542 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 543 | K.isMergeableConst8() || K.isMergeableConst16()) |
| 544 | Flags |= MCSectionELF::SHF_MERGE; |
| 545 | |
| 546 | if (K.isMergeableCString()) |
| 547 | Flags |= MCSectionELF::SHF_STRINGS; |
| 548 | |
| 549 | return Flags; |
| 550 | } |
| 551 | |
| 552 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 553 | const MCSection *TargetLoweringObjectFileELF:: |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 554 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 555 | Mangler *Mang, const TargetMachine &TM) const { |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 556 | StringRef SectionName = GV->getSection(); |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 558 | // Infer section flags from the section name if we can. |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 559 | Kind = getELFKindForNamedSection(SectionName, Kind); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 560 | |
| 561 | return getELFSection(SectionName, |
| 562 | getELFSectionType(SectionName, Kind), |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 563 | getELFSectionFlags(Kind), Kind, true); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 564 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 565 | |
| 566 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 567 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 568 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 569 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 570 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 571 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 572 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 573 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 574 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 575 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 576 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 577 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 578 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 579 | return ".gnu.linkonce.d.rel.ro."; |
| 580 | } |
| 581 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 582 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 583 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 584 | Mangler *Mang, const TargetMachine &TM) const { |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 585 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 586 | // If this global is linkonce/weak and the target handles this by emitting it |
| 587 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | c7fbe90 | 2010-01-19 03:06:01 +0000 | [diff] [blame] | 588 | if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 589 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 590 | SmallString<128> Name; |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 591 | Name.append(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | 8da8d4b | 2010-01-13 21:29:21 +0000 | [diff] [blame] | 592 | Mang->getNameWithPrefix(Name, GV, false); |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 593 | return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind), |
| 594 | getELFSectionFlags(Kind), Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 595 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 596 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 597 | if (Kind.isText()) return TextSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 599 | if (Kind.isMergeable1ByteCString() || |
| 600 | Kind.isMergeable2ByteCString() || |
| 601 | Kind.isMergeable4ByteCString()) { |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 603 | // We also need alignment here. |
| 604 | // FIXME: this is getting the alignment of the character, not the |
| 605 | // alignment of the global! |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 606 | unsigned Align = |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 607 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 609 | const char *SizeSpec = ".rodata.str1."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 610 | if (Kind.isMergeable2ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 611 | SizeSpec = ".rodata.str2."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 612 | else if (Kind.isMergeable4ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 613 | SizeSpec = ".rodata.str4."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 614 | else |
| 615 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 616 | |
| 617 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 618 | std::string Name = SizeSpec + utostr(Align); |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 619 | return getELFSection(Name, MCSectionELF::SHT_PROGBITS, |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 620 | MCSectionELF::SHF_ALLOC | |
| 621 | MCSectionELF::SHF_MERGE | |
| 622 | MCSectionELF::SHF_STRINGS, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 623 | Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 624 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 625 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 626 | if (Kind.isMergeableConst()) { |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 627 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 628 | return MergeableConst4Section; |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 629 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 630 | return MergeableConst8Section; |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 631 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 632 | return MergeableConst16Section; |
| 633 | return ReadOnlySection; // .const |
| 634 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 635 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 636 | if (Kind.isReadOnly()) return ReadOnlySection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 637 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 638 | if (Kind.isThreadData()) return TLSDataSection; |
| 639 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 640 | |
Chris Lattner | c7fbe90 | 2010-01-19 03:06:01 +0000 | [diff] [blame] | 641 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 642 | // really emitted with the magic .comm directive, which creates a symbol table |
| 643 | // entry but not a section. |
Chris Lattner | a3839bc | 2010-01-19 02:48:26 +0000 | [diff] [blame] | 644 | if (Kind.isBSS() || Kind.isCommon()) return BSSSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 645 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 646 | if (Kind.isDataNoRel()) return DataSection; |
| 647 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 648 | if (Kind.isDataRel()) return DataRelSection; |
| 649 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 650 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 651 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 652 | return DataRelROSection; |
| 653 | } |
| 654 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 655 | /// getSectionForConstant - Given a mergeable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 656 | /// specified size and relocation information, return a section that it |
| 657 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 658 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 659 | getSectionForConstant(SectionKind Kind) const { |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 660 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 661 | return MergeableConst4Section; |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 662 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 663 | return MergeableConst8Section; |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 664 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 665 | return MergeableConst16Section; |
| 666 | if (Kind.isReadOnly()) |
| 667 | return ReadOnlySection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 668 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 669 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 670 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 671 | return DataRelROSection; |
| 672 | } |
| 673 | |
| 674 | //===----------------------------------------------------------------------===// |
| 675 | // MachO |
| 676 | //===----------------------------------------------------------------------===// |
| 677 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 678 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 680 | TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() { |
| 681 | // If we have the MachO uniquing map, free it. |
| 682 | delete (MachOUniqueMapTy*)UniquingMap; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | const MCSectionMachO *TargetLoweringObjectFileMachO:: |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 687 | getMachOSection(StringRef Segment, StringRef Section, |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 688 | unsigned TypeAndAttributes, |
| 689 | unsigned Reserved2, SectionKind Kind) const { |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 690 | // We unique sections by their segment/section pair. The returned section |
| 691 | // may not have the same flags as the requested section, if so this should be |
| 692 | // diagnosed by the client as an error. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 694 | // Create the map if it doesn't already exist. |
| 695 | if (UniquingMap == 0) |
| 696 | UniquingMap = new MachOUniqueMapTy(); |
| 697 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 699 | // Form the name to look up. |
| 700 | SmallString<64> Name; |
Daniel Dunbar | 16df208 | 2009-08-26 23:12:33 +0000 | [diff] [blame] | 701 | Name += Segment; |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 702 | Name.push_back(','); |
Daniel Dunbar | 16df208 | 2009-08-26 23:12:33 +0000 | [diff] [blame] | 703 | Name += Section; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 705 | // Do the lookup, if we have a hit, return it. |
Daniel Dunbar | 16df208 | 2009-08-26 23:12:33 +0000 | [diff] [blame] | 706 | const MCSectionMachO *&Entry = Map[Name.str()]; |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 707 | if (Entry) return Entry; |
| 708 | |
| 709 | // Otherwise, return a new section. |
| 710 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, |
| 711 | Reserved2, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 714 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 715 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 716 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 717 | if (UniquingMap != 0) |
| 718 | ((MachOUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 719 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 720 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 721 | TextSection // .text |
| 722 | = getMachOSection("__TEXT", "__text", |
| 723 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 724 | SectionKind::getText()); |
| 725 | DataSection // .data |
| 726 | = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 727 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 728 | CStringSection // .cstring |
| 729 | = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS, |
| 730 | SectionKind::getMergeable1ByteCString()); |
| 731 | UStringSection |
| 732 | = getMachOSection("__TEXT","__ustring", 0, |
| 733 | SectionKind::getMergeable2ByteCString()); |
| 734 | FourByteConstantSection // .literal4 |
| 735 | = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS, |
| 736 | SectionKind::getMergeableConst4()); |
| 737 | EightByteConstantSection // .literal8 |
| 738 | = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS, |
| 739 | SectionKind::getMergeableConst8()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 741 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 742 | // to using it in -static mode. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 743 | SixteenByteConstantSection = 0; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 744 | if (TM.getRelocationModel() != Reloc::Static && |
| 745 | TM.getTargetData()->getPointerSize() == 32) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 746 | SixteenByteConstantSection = // .literal16 |
| 747 | getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 748 | SectionKind::getMergeableConst16()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 749 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 750 | ReadOnlySection // .const |
| 751 | = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 752 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 753 | TextCoalSection |
| 754 | = getMachOSection("__TEXT", "__textcoal_nt", |
| 755 | MCSectionMachO::S_COALESCED | |
| 756 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 757 | SectionKind::getText()); |
| 758 | ConstTextCoalSection |
| 759 | = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED, |
| 760 | SectionKind::getText()); |
| 761 | ConstDataCoalSection |
| 762 | = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED, |
| 763 | SectionKind::getText()); |
| 764 | ConstDataSection // .const_data |
| 765 | = getMachOSection("__DATA", "__const", 0, |
| 766 | SectionKind::getReadOnlyWithRel()); |
| 767 | DataCoalSection |
| 768 | = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED, |
| 769 | SectionKind::getDataRel()); |
Chris Lattner | 814819f | 2010-01-19 06:25:51 +0000 | [diff] [blame] | 770 | DataCommonSection |
| 771 | = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL, |
| 772 | SectionKind::getBSS()); |
| 773 | DataBSSSection |
| 774 | = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, |
| 775 | SectionKind::getBSS()); |
| 776 | |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 777 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 778 | LazySymbolPointerSection |
| 779 | = getMachOSection("__DATA", "__la_symbol_ptr", |
| 780 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 781 | SectionKind::getMetadata()); |
| 782 | NonLazySymbolPointerSection |
| 783 | = getMachOSection("__DATA", "__nl_symbol_ptr", |
| 784 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 785 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 787 | if (TM.getRelocationModel() == Reloc::Static) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 788 | StaticCtorSection |
| 789 | = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel()); |
| 790 | StaticDtorSection |
| 791 | = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 792 | } else { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 793 | StaticCtorSection |
| 794 | = getMachOSection("__DATA", "__mod_init_func", |
| 795 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 796 | SectionKind::getDataRel()); |
| 797 | StaticDtorSection |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 798 | = getMachOSection("__DATA", "__mod_term_func", |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 799 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 800 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 801 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 803 | // Exception Handling. |
Bill Wendling | fb7634f | 2009-11-19 19:21:09 +0000 | [diff] [blame] | 804 | LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0, |
| 805 | SectionKind::getDataRel()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 806 | EHFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 807 | getMachOSection("__TEXT", "__eh_frame", |
| 808 | MCSectionMachO::S_COALESCED | |
| 809 | MCSectionMachO::S_ATTR_NO_TOC | |
| 810 | MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | |
| 811 | MCSectionMachO::S_ATTR_LIVE_SUPPORT, |
| 812 | SectionKind::getReadOnly()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 813 | |
| 814 | // Debug Information. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 815 | DwarfAbbrevSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 816 | getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 817 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 818 | DwarfInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 819 | getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 820 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 821 | DwarfLineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 822 | getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 823 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 824 | DwarfFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 825 | getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 826 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 827 | DwarfPubNamesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 828 | getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 829 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 830 | DwarfPubTypesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 831 | getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 832 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 833 | DwarfStrSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 834 | getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 835 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 836 | DwarfLocSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 837 | getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 838 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 839 | DwarfARangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 840 | getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 841 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 842 | DwarfRangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 843 | getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 844 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 845 | DwarfMacroInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 846 | getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 847 | SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 848 | DwarfDebugInlineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 849 | getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 850 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 853 | const MCSection *TargetLoweringObjectFileMachO:: |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 854 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 855 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 856 | // Parse the section specifier and create it if valid. |
| 857 | StringRef Segment, Section; |
| 858 | unsigned TAA, StubSize; |
| 859 | std::string ErrorCode = |
| 860 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
| 861 | TAA, StubSize); |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 862 | if (!ErrorCode.empty()) { |
| 863 | // If invalid, report the error with llvm_report_error. |
| 864 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 865 | "' has an invalid section specifier '" + GV->getSection()+ |
| 866 | "': " + ErrorCode + "."); |
| 867 | // Fall back to dropping it into the data section. |
| 868 | return DataSection; |
| 869 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 870 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 871 | // Get the section. |
| 872 | const MCSectionMachO *S = |
| 873 | getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 874 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 875 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 876 | // If the user declared multiple globals with different section flags, we need |
| 877 | // to reject it here. |
| 878 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
| 879 | // If invalid, report the error with llvm_report_error. |
| 880 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 881 | "' section type or attributes does not match previous" |
| 882 | " section specifier"); |
| 883 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 884 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 885 | return S; |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 889 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 890 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 891 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 892 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 893 | if (Kind.isText()) |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 894 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 895 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 896 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 897 | // or data depending on if it is writable. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 898 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 899 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 900 | return ConstTextCoalSection; |
| 901 | return DataCoalSection; |
| 902 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 903 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 904 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 905 | if (Kind.isMergeable1ByteCString() || |
| 906 | Kind.isMergeable2ByteCString()) { |
| 907 | if (TM.getTargetData()->getPreferredAlignment( |
| 908 | cast<GlobalVariable>(GV)) < 32) { |
| 909 | if (Kind.isMergeable1ByteCString()) |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 910 | return CStringSection; |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 911 | assert(Kind.isMergeable2ByteCString()); |
| 912 | return UStringSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 913 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 914 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 915 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 916 | if (Kind.isMergeableConst()) { |
| 917 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 918 | return FourByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 919 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 920 | return EightByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 921 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 922 | return SixteenByteConstantSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 923 | } |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 924 | |
| 925 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 926 | // just drop it in .const. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 927 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 928 | return ReadOnlySection; |
| 929 | |
| 930 | // If this is marked const, put it into a const section. But if the dynamic |
| 931 | // linker needs to write to it, put it in the data segment. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 932 | if (Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 933 | return ConstDataSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 934 | |
Chris Lattner | aac138e | 2010-01-19 02:09:44 +0000 | [diff] [blame] | 935 | // Put zero initialized globals with strong external linkage in the |
| 936 | // DATA, __common section with the .zerofill directive. |
Chris Lattner | ce8749e | 2010-01-19 04:15:51 +0000 | [diff] [blame] | 937 | if (Kind.isBSSExtern()) |
Chris Lattner | aac138e | 2010-01-19 02:09:44 +0000 | [diff] [blame] | 938 | return DataCommonSection; |
Chris Lattner | 814819f | 2010-01-19 06:25:51 +0000 | [diff] [blame] | 939 | |
| 940 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 941 | // with the .zerofill directive (aka .lcomm). |
| 942 | if (Kind.isBSSLocal()) |
| 943 | return DataBSSSection; |
Chris Lattner | aac138e | 2010-01-19 02:09:44 +0000 | [diff] [blame] | 944 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 945 | // Otherwise, just drop the variable in the normal data section. |
| 946 | return DataSection; |
| 947 | } |
| 948 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 949 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 950 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 951 | // If this constant requires a relocation, we have to put it in the data |
| 952 | // segment, not in the text segment. |
Eric Christopher | a07b750 | 2010-01-07 19:44:05 +0000 | [diff] [blame] | 953 | if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 954 | return ConstDataSection; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 955 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 956 | if (Kind.isMergeableConst4()) |
| 957 | return FourByteConstantSection; |
| 958 | if (Kind.isMergeableConst8()) |
| 959 | return EightByteConstantSection; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 960 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 961 | return SixteenByteConstantSection; |
| 962 | return ReadOnlySection; // .const |
| 963 | } |
| 964 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 965 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 966 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 967 | // FIXME: REMOVE this (rdar://7071300) |
| 968 | bool TargetLoweringObjectFileMachO:: |
| 969 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 970 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 971 | /// the directive emitted (this occurs in ObjC metadata). |
| 972 | if (!GV) return false; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 974 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 975 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 976 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 977 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 978 | // this horrible hack can go away. |
Chris Lattner | 0b3735e | 2010-01-16 02:16:09 +0000 | [diff] [blame] | 979 | SmallString<64> Name; |
Chris Lattner | 036d8f9 | 2010-01-16 03:38:27 +0000 | [diff] [blame] | 980 | Mang->getNameWithPrefix(Name, GV, false); |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 981 | if (Name[0] == 'L' || Name[0] == 'l') |
| 982 | return false; |
| 983 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 985 | return true; |
| 986 | } |
| 987 | |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 988 | const MCExpr *TargetLoweringObjectFileMachO:: |
| 989 | getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
Chris Lattner | 8609c7c | 2009-09-17 18:49:52 +0000 | [diff] [blame] | 990 | MachineModuleInfo *MMI, |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 991 | bool &IsIndirect, bool &IsPCRel) const { |
| 992 | // The mach-o version of this method defaults to returning a stub reference. |
| 993 | IsIndirect = true; |
| 994 | IsPCRel = false; |
| 995 | |
| 996 | SmallString<128> Name; |
| 997 | Mang->getNameWithPrefix(Name, GV, true); |
| 998 | Name += "$non_lazy_ptr"; |
| 999 | return MCSymbolRefExpr::Create(Name.str(), getContext()); |
| 1000 | } |
| 1001 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1003 | //===----------------------------------------------------------------------===// |
| 1004 | // COFF |
| 1005 | //===----------------------------------------------------------------------===// |
| 1006 | |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 1007 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
| 1008 | |
| 1009 | TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() { |
| 1010 | delete (COFFUniqueMapTy*)UniquingMap; |
| 1011 | } |
| 1012 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 1014 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 1015 | getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 1016 | // Create the map if it doesn't already exist. |
| 1017 | if (UniquingMap == 0) |
| 1018 | UniquingMap = new MachOUniqueMapTy(); |
| 1019 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 1021 | // Do the lookup, if we have a hit, return it. |
| 1022 | const MCSectionCOFF *&Entry = Map[Name]; |
| 1023 | if (Entry) return Entry; |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 1025 | return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 1028 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 1029 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 1030 | if (UniquingMap != 0) |
| 1031 | ((COFFUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 1032 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1033 | TextSection = getCOFFSection("\t.text", true, SectionKind::getText()); |
| 1034 | DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 1035 | StaticCtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1036 | getCOFFSection(".ctors", false, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 1037 | StaticDtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1038 | getCOFFSection(".dtors", false, SectionKind::getDataRel()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 35c3531 | 2009-08-18 16:56:17 +0000 | [diff] [blame] | 1040 | // FIXME: We're emitting LSDA info into a readonly section on COFF, even |
| 1041 | // though it contains relocatable pointers. In PIC mode, this is probably a |
| 1042 | // big runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 1043 | // adjusted or this should be a data section. |
| 1044 | LSDASection = |
| 1045 | getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly()); |
| 1046 | EHFrameSection = |
| 1047 | getCOFFSection(".eh_frame", false, SectionKind::getDataRel()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 1049 | // Debug info. |
| 1050 | // FIXME: Don't use 'directive' mode here. |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1051 | DwarfAbbrevSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1052 | getCOFFSection("\t.section\t.debug_abbrev,\"dr\"", |
| 1053 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1054 | DwarfInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1055 | getCOFFSection("\t.section\t.debug_info,\"dr\"", |
| 1056 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1057 | DwarfLineSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1058 | getCOFFSection("\t.section\t.debug_line,\"dr\"", |
| 1059 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1060 | DwarfFrameSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1061 | getCOFFSection("\t.section\t.debug_frame,\"dr\"", |
| 1062 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 1063 | DwarfPubNamesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1064 | getCOFFSection("\t.section\t.debug_pubnames,\"dr\"", |
| 1065 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 1066 | DwarfPubTypesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1067 | getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"", |
| 1068 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1069 | DwarfStrSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1070 | getCOFFSection("\t.section\t.debug_str,\"dr\"", |
| 1071 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1072 | DwarfLocSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1073 | getCOFFSection("\t.section\t.debug_loc,\"dr\"", |
| 1074 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1075 | DwarfARangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1076 | getCOFFSection("\t.section\t.debug_aranges,\"dr\"", |
| 1077 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1078 | DwarfRangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1079 | getCOFFSection("\t.section\t.debug_ranges,\"dr\"", |
| 1080 | true, SectionKind::getMetadata()); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1081 | DwarfMacroInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1082 | getCOFFSection("\t.section\t.debug_macinfo,\"dr\"", |
| 1083 | true, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 1086 | const MCSection *TargetLoweringObjectFileCOFF:: |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1087 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 1088 | Mangler *Mang, const TargetMachine &TM) const { |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame^] | 1089 | return getCOFFSection(GV->getSection(), false, Kind); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1092 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 1093 | if (Kind.isText()) |
| 1094 | return ".text$linkonce"; |
| 1095 | if (Kind.isWriteable()) |
| 1096 | return ".data$linkonce"; |
| 1097 | return ".rdata$linkonce"; |
| 1098 | } |
| 1099 | |
| 1100 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 1101 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1102 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 1103 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1104 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1106 | // If this global is linkonce/weak and the target handles this by emitting it |
| 1107 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 1108 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1109 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 1110 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | cab16cc | 2010-01-13 19:19:17 +0000 | [diff] [blame] | 1111 | Mang->getNameWithPrefix(Name, GV, false); |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 1112 | return getCOFFSection(Name.str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1113 | } |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1115 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1116 | return getTextSection(); |
Anton Korobeynikov | 8ddb569 | 2009-09-09 08:41:20 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1118 | return getDataSection(); |
| 1119 | } |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 1120 | |