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 | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionMachO.h" |
Chris Lattner | 5277b22 | 2009-08-08 20:43:12 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetAsmInfo.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5277b22 | 2009-08-08 20:43:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame^] | 27 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
| 29 | using namespace llvm; |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Generic Code |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 35 | TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 36 | TextSection = 0; |
| 37 | DataSection = 0; |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 38 | BSSSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 39 | ReadOnlySection = 0; |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 40 | StaticCtorSection = 0; |
| 41 | StaticDtorSection = 0; |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 42 | LSDASection = 0; |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 43 | EHFrameSection = 0; |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 44 | |
| 45 | DwarfAbbrevSection = 0; |
| 46 | DwarfInfoSection = 0; |
| 47 | DwarfLineSection = 0; |
| 48 | DwarfFrameSection = 0; |
| 49 | DwarfPubNamesSection = 0; |
| 50 | DwarfPubTypesSection = 0; |
| 51 | DwarfDebugInlineSection = 0; |
| 52 | DwarfStrSection = 0; |
| 53 | DwarfLocSection = 0; |
| 54 | DwarfARangesSection = 0; |
| 55 | DwarfRangesSection = 0; |
| 56 | DwarfMacroInfoSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
| 60 | } |
| 61 | |
| 62 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 63 | Constant *C = GV->getInitializer(); |
| 64 | |
| 65 | // Must have zero initializer. |
| 66 | if (!C->isNullValue()) |
| 67 | return false; |
| 68 | |
| 69 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 70 | if (GV->isConstant()) |
| 71 | return false; |
| 72 | |
| 73 | // If the global has an explicit section specified, don't put it in BSS. |
| 74 | if (!GV->getSection().empty()) |
| 75 | return false; |
| 76 | |
| 77 | // If -nozero-initialized-in-bss is specified, don't ever use BSS. |
| 78 | if (NoZerosInBSS) |
| 79 | return false; |
| 80 | |
| 81 | // Otherwise, put it in BSS! |
| 82 | return true; |
| 83 | } |
| 84 | |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 85 | /// IsNullTerminatedString - Return true if the specified constant (which is |
| 86 | /// known to have a type that is an array of 1/2/4 byte elements) ends with a |
| 87 | /// nul value and contains no other nuls in it. |
| 88 | static bool IsNullTerminatedString(const Constant *C) { |
| 89 | const ArrayType *ATy = cast<ArrayType>(C->getType()); |
| 90 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 91 | // First check: is we have constant array of i8 terminated with zero |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 92 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) { |
| 93 | if (ATy->getNumElements() == 0) return false; |
| 94 | |
| 95 | ConstantInt *Null = |
| 96 | dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1)); |
| 97 | if (Null == 0 || Null->getZExtValue() != 0) |
| 98 | return false; // Not null terminated. |
| 99 | |
| 100 | // Verify that the null doesn't occur anywhere else in the string. |
| 101 | for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i) |
| 102 | // Reject constantexpr elements etc. |
| 103 | if (!isa<ConstantInt>(CVA->getOperand(i)) || |
| 104 | CVA->getOperand(i) == Null) |
| 105 | return false; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 106 | return true; |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 108 | |
| 109 | // Another possibility: [1 x i8] zeroinitializer |
| 110 | if (isa<ConstantAggregateZero>(C)) |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 111 | return ATy->getNumElements() == 1; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 116 | /// getKindForGlobal - This is a top-level target-independent classifier for |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 117 | /// a global variable. Given an global variable and information from TM, it |
| 118 | /// classifies the global in a variety of ways that make various target |
| 119 | /// implementations simpler. The target implementation is free to ignore this |
| 120 | /// extra info of course. |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 121 | SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, |
| 122 | const TargetMachine &TM){ |
| 123 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && |
| 124 | "Can only be used for global definitions"); |
| 125 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 126 | Reloc::Model ReloModel = TM.getRelocationModel(); |
| 127 | |
| 128 | // Early exit - functions should be always in text sections. |
| 129 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 130 | if (GVar == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 131 | return SectionKind::getText(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 132 | |
| 133 | // Handle thread-local data first. |
| 134 | if (GVar->isThreadLocal()) { |
| 135 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 136 | return SectionKind::getThreadBSS(); |
| 137 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Variable can be easily put to BSS section. |
| 141 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 142 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 143 | |
| 144 | Constant *C = GVar->getInitializer(); |
| 145 | |
| 146 | // If the global is marked constant, we can put it into a mergable section, |
| 147 | // a mergable string section, or general .data if it contains relocations. |
| 148 | if (GVar->isConstant()) { |
| 149 | // If the initializer for the global contains something that requires a |
| 150 | // relocation, then we may have to drop this into a wriable data section |
| 151 | // even though it is marked const. |
| 152 | switch (C->getRelocationInfo()) { |
| 153 | default: llvm_unreachable("unknown relocation info kind"); |
| 154 | case Constant::NoRelocation: |
| 155 | // If initializer is a null-terminated string, put it in a "cstring" |
Chris Lattner | 1850e5a | 2009-08-04 16:13:09 +0000 | [diff] [blame] | 156 | // section of the right width. |
| 157 | if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
| 158 | if (const IntegerType *ITy = |
| 159 | dyn_cast<IntegerType>(ATy->getElementType())) { |
| 160 | if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || |
| 161 | ITy->getBitWidth() == 32) && |
| 162 | IsNullTerminatedString(C)) { |
| 163 | if (ITy->getBitWidth() == 8) |
| 164 | return SectionKind::getMergeable1ByteCString(); |
| 165 | if (ITy->getBitWidth() == 16) |
| 166 | return SectionKind::getMergeable2ByteCString(); |
| 167 | |
| 168 | assert(ITy->getBitWidth() == 32 && "Unknown width"); |
| 169 | return SectionKind::getMergeable4ByteCString(); |
| 170 | } |
| 171 | } |
| 172 | } |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 173 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 174 | // Otherwise, just drop it into a mergable constant section. If we have |
| 175 | // a section for this size, use it, otherwise use the arbitrary sized |
| 176 | // mergable section. |
| 177 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 178 | case 4: return SectionKind::getMergeableConst4(); |
| 179 | case 8: return SectionKind::getMergeableConst8(); |
| 180 | case 16: return SectionKind::getMergeableConst16(); |
| 181 | default: return SectionKind::getMergeableConst(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | case Constant::LocalRelocation: |
| 185 | // In static relocation model, the linker will resolve all addresses, so |
| 186 | // the relocation entries will actually be constants by the time the app |
| 187 | // starts up. However, we can't put this into a mergable section, because |
| 188 | // the linker doesn't take relocations into consideration when it tries to |
| 189 | // merge entries in the section. |
| 190 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 191 | return SectionKind::getReadOnly(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 192 | |
| 193 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 194 | // writable data.rel.local section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 195 | return SectionKind::getReadOnlyWithRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 196 | |
| 197 | case Constant::GlobalRelocations: |
| 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(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 205 | |
| 206 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 207 | // writable data.rel section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 208 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | // Okay, this isn't a constant. If the initializer for the global is going |
| 213 | // to require a runtime relocation by the dynamic linker, put it into a more |
| 214 | // specific section to improve startup time of the app. This coalesces these |
| 215 | // globals together onto fewer pages, improving the locality of the dynamic |
| 216 | // linker. |
| 217 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 218 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 219 | |
| 220 | switch (C->getRelocationInfo()) { |
| 221 | default: llvm_unreachable("unknown relocation info kind"); |
| 222 | case Constant::NoRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 223 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 224 | case Constant::LocalRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 225 | return SectionKind::getDataRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 226 | case Constant::GlobalRelocations: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 227 | return SectionKind::getDataRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | /// SectionForGlobal - This method computes the appropriate section to emit |
| 232 | /// the specified global variable or function definition. This should not |
| 233 | /// be passed external (or available externally) globals. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 234 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 235 | SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 236 | const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 237 | // Select section name. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 238 | if (GV->hasSection()) |
| 239 | return getExplicitSectionGlobal(GV, Kind, Mang, TM); |
| 240 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 241 | |
| 242 | // Use default section depending on the 'type' of global |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 243 | return SelectSectionForGlobal(GV, Kind, Mang, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 58bed8f | 2009-08-05 04:25:40 +0000 | [diff] [blame] | 246 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 247 | // Lame default implementation. Calculate the section name for global. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 248 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 249 | TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 250 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 251 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 252 | const TargetMachine &TM) const{ |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 253 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 254 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 255 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 256 | return getTextSection(); |
| 257 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 258 | if (Kind.isBSS() && BSSSection != 0) |
| 259 | return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 260 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 261 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 262 | return ReadOnlySection; |
| 263 | |
| 264 | return getDataSection(); |
| 265 | } |
| 266 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 267 | /// getSectionForConstant - Given a mergable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 268 | /// specified size and relocation information, return a section that it |
| 269 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 270 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 271 | TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 272 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
| 273 | return ReadOnlySection; |
| 274 | |
| 275 | return DataSection; |
| 276 | } |
| 277 | |
| 278 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 279 | |
| 280 | //===----------------------------------------------------------------------===// |
| 281 | // ELF |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 284 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 285 | getELFSection(const char *Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 286 | if (MCSection *S = getContext().GetSection(Name)) |
| 287 | return S; |
Chris Lattner | 7c599d0 | 2009-08-08 20:52:13 +0000 | [diff] [blame] | 288 | return MCSectionELF::Create(Name, isDirective, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 291 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 292 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 293 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 294 | if (!HasCrazyBSS) |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 295 | BSSSection = getELFSection("\t.bss", true, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 296 | else |
| 297 | // PPC/Linux doesn't support the .bss directive, it needs .section .bss. |
| 298 | // FIXME: Does .section .bss work everywhere?? |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 299 | // FIXME2: this should just be handle by the section printer. We should get |
| 300 | // away from syntactic view of the sections and MCSection should just be a |
| 301 | // semantic view. |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 302 | BSSSection = getELFSection("\t.bss", false, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 303 | |
| 304 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 305 | TextSection = getELFSection("\t.text", true, SectionKind::getText()); |
| 306 | DataSection = getELFSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 307 | ReadOnlySection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 308 | getELFSection("\t.rodata", false, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 309 | TLSDataSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 310 | getELFSection("\t.tdata", false, SectionKind::getThreadData()); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 312 | TLSBSSSection = getELFSection("\t.tbss", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 313 | SectionKind::getThreadBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 315 | DataRelSection = getELFSection("\t.data.rel", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 316 | SectionKind::getDataRel()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 317 | DataRelLocalSection = getELFSection("\t.data.rel.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 318 | SectionKind::getDataRelLocal()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 319 | DataRelROSection = getELFSection("\t.data.rel.ro", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 320 | SectionKind::getReadOnlyWithRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 321 | DataRelROLocalSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 322 | getELFSection("\t.data.rel.ro.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 323 | SectionKind::getReadOnlyWithRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 325 | MergeableConst4Section = getELFSection(".rodata.cst4", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 326 | SectionKind::getMergeableConst4()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 327 | MergeableConst8Section = getELFSection(".rodata.cst8", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 328 | SectionKind::getMergeableConst8()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 329 | MergeableConst16Section = getELFSection(".rodata.cst16", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 330 | SectionKind::getMergeableConst16()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 331 | |
| 332 | StaticCtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 333 | getELFSection(".ctors", false, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 334 | StaticDtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 335 | getELFSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 337 | // Exception Handling Sections. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 338 | |
| 339 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 340 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 341 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 342 | // adjusted or this should be a data section. |
| 343 | LSDASection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 344 | getELFSection(".gcc_except_table", false, SectionKind::getReadOnly()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 345 | EHFrameSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 346 | getELFSection(".eh_frame", false, SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 347 | |
| 348 | // Debug Info Sections. |
| 349 | DwarfAbbrevSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 350 | getELFSection(".debug_abbrev", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 351 | DwarfInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 352 | getELFSection(".debug_info", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 353 | DwarfLineSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 354 | getELFSection(".debug_line", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 355 | DwarfFrameSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 356 | getELFSection(".debug_frame", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 357 | DwarfPubNamesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 358 | getELFSection(".debug_pubnames", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 359 | DwarfPubTypesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 360 | getELFSection(".debug_pubtypes", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 361 | DwarfStrSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 362 | getELFSection(".debug_str", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 363 | DwarfLocSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 364 | getELFSection(".debug_loc", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 365 | DwarfARangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 366 | getELFSection(".debug_aranges", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 367 | DwarfRangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 368 | getELFSection(".debug_ranges", false, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 369 | DwarfMacroInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 370 | getELFSection(".debug_macinfo", false, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 374 | static SectionKind |
| 375 | getELFKindForNamedSection(const char *Name, SectionKind K) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 376 | if (Name[0] != '.') return K; |
| 377 | |
| 378 | // Some lame default implementation based on some magic section names. |
| 379 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 380 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 381 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 382 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 383 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 384 | |
| 385 | if (strcmp(Name, ".tdata") == 0 || |
| 386 | strncmp(Name, ".tdata.", 7) == 0 || |
| 387 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 388 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 389 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 390 | |
| 391 | if (strcmp(Name, ".tbss") == 0 || |
| 392 | strncmp(Name, ".tbss.", 6) == 0 || |
| 393 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 394 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 395 | return SectionKind::getThreadBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 396 | |
| 397 | return K; |
| 398 | } |
| 399 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 400 | const MCSection *TargetLoweringObjectFileELF:: |
| 401 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 402 | Mangler *Mang, const TargetMachine &TM) const { |
| 403 | // Infer section flags from the section name if we can. |
| 404 | Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind); |
| 405 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 406 | return getELFSection(GV->getSection().c_str(), false, Kind); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 407 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 408 | |
| 409 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 410 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 411 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 412 | |
| 413 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 414 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 415 | |
| 416 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 417 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 418 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 419 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 420 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 421 | |
| 422 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 423 | return ".gnu.linkonce.d.rel.ro."; |
| 424 | } |
| 425 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 426 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 427 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 428 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 429 | |
| 430 | // If this global is linkonce/weak and the target handles this by emitting it |
| 431 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 432 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 433 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | b8f396b | 2009-07-29 05:20:33 +0000 | [diff] [blame] | 434 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 435 | return getELFSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 438 | if (Kind.isText()) return TextSection; |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 440 | if (Kind.isMergeable1ByteCString() || |
| 441 | Kind.isMergeable2ByteCString() || |
| 442 | Kind.isMergeable4ByteCString()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 444 | // We also need alignment here. |
| 445 | // FIXME: this is getting the alignment of the character, not the |
| 446 | // alignment of the global! |
| 447 | unsigned Align = |
| 448 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 450 | const char *SizeSpec = ".rodata.str1."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 451 | if (Kind.isMergeable2ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 452 | SizeSpec = ".rodata.str2."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 453 | else if (Kind.isMergeable4ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 454 | SizeSpec = ".rodata.str4."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 455 | else |
| 456 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 457 | |
| 458 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 459 | std::string Name = SizeSpec + utostr(Align); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 460 | return getELFSection(Name.c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 463 | if (Kind.isMergeableConst()) { |
| 464 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 465 | return MergeableConst4Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 466 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 467 | return MergeableConst8Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 468 | if (Kind.isMergeableConst16()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 469 | return MergeableConst16Section; |
| 470 | return ReadOnlySection; // .const |
| 471 | } |
| 472 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 473 | if (Kind.isReadOnly()) return ReadOnlySection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 474 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 475 | if (Kind.isThreadData()) return TLSDataSection; |
| 476 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 478 | if (Kind.isBSS()) return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 479 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 480 | if (Kind.isDataNoRel()) return DataSection; |
| 481 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 482 | if (Kind.isDataRel()) return DataRelSection; |
| 483 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 484 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 485 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 486 | return DataRelROSection; |
| 487 | } |
| 488 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 489 | /// getSectionForConstant - Given a mergeable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 490 | /// specified size and relocation information, return a section that it |
| 491 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 492 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 493 | getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 494 | if (Kind.isMergeableConst4()) |
| 495 | return MergeableConst4Section; |
| 496 | if (Kind.isMergeableConst8()) |
| 497 | return MergeableConst8Section; |
| 498 | if (Kind.isMergeableConst16()) |
| 499 | return MergeableConst16Section; |
| 500 | if (Kind.isReadOnly()) |
| 501 | return ReadOnlySection; |
| 502 | |
| 503 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 504 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 505 | return DataRelROSection; |
| 506 | } |
| 507 | |
| 508 | //===----------------------------------------------------------------------===// |
| 509 | // MachO |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame^] | 512 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame^] | 514 | TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() { |
| 515 | // If we have the MachO uniquing map, free it. |
| 516 | delete (MachOUniqueMapTy*)UniquingMap; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | const MCSectionMachO *TargetLoweringObjectFileMachO:: |
Chris Lattner | d3c4486f | 2009-08-12 23:34:27 +0000 | [diff] [blame] | 521 | getMachOSection(const StringRef &Segment, const StringRef &Section, |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 522 | unsigned TypeAndAttributes, |
| 523 | unsigned Reserved2, SectionKind Kind) const { |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame^] | 524 | // We unique sections by their segment/section pair. The returned section |
| 525 | // may not have the same flags as the requested section, if so this should be |
| 526 | // diagnosed by the client as an error. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame^] | 528 | // Create the map if it doesn't already exist. |
| 529 | if (UniquingMap == 0) |
| 530 | UniquingMap = new MachOUniqueMapTy(); |
| 531 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap; |
| 532 | |
| 533 | // Form the name to look up. |
| 534 | SmallString<64> Name; |
| 535 | Name.append(Segment.begin(), Segment.end()); |
| 536 | Name.push_back(','); |
| 537 | Name.append(Section.begin(), Section.end()); |
| 538 | |
| 539 | // Do the lookup, if we have a hit, return it. |
| 540 | const MCSectionMachO *&Entry = Map[StringRef(Name.data(), Name.size())]; |
| 541 | if (Entry) return Entry; |
| 542 | |
| 543 | // Otherwise, return a new section. |
| 544 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, |
| 545 | Reserved2, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 548 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 549 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 550 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 551 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 552 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 553 | TextSection // .text |
| 554 | = getMachOSection("__TEXT", "__text", |
| 555 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 556 | SectionKind::getText()); |
| 557 | DataSection // .data |
| 558 | = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel()); |
| 559 | |
| 560 | CStringSection // .cstring |
| 561 | = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS, |
| 562 | SectionKind::getMergeable1ByteCString()); |
| 563 | UStringSection |
| 564 | = getMachOSection("__TEXT","__ustring", 0, |
| 565 | SectionKind::getMergeable2ByteCString()); |
| 566 | FourByteConstantSection // .literal4 |
| 567 | = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS, |
| 568 | SectionKind::getMergeableConst4()); |
| 569 | EightByteConstantSection // .literal8 |
| 570 | = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS, |
| 571 | SectionKind::getMergeableConst8()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 572 | |
| 573 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 574 | // to using it in -static mode. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 575 | SixteenByteConstantSection = 0; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 576 | if (TM.getRelocationModel() != Reloc::Static && |
| 577 | TM.getTargetData()->getPointerSize() == 32) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 578 | SixteenByteConstantSection = // .literal16 |
| 579 | getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 580 | SectionKind::getMergeableConst16()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 581 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 582 | ReadOnlySection // .const |
| 583 | = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 584 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 585 | TextCoalSection |
| 586 | = getMachOSection("__TEXT", "__textcoal_nt", |
| 587 | MCSectionMachO::S_COALESCED | |
| 588 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 589 | SectionKind::getText()); |
| 590 | ConstTextCoalSection |
| 591 | = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED, |
| 592 | SectionKind::getText()); |
| 593 | ConstDataCoalSection |
| 594 | = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED, |
| 595 | SectionKind::getText()); |
| 596 | ConstDataSection // .const_data |
| 597 | = getMachOSection("__DATA", "__const", 0, |
| 598 | SectionKind::getReadOnlyWithRel()); |
| 599 | DataCoalSection |
| 600 | = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED, |
| 601 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 602 | |
| 603 | if (TM.getRelocationModel() == Reloc::Static) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 604 | StaticCtorSection |
| 605 | = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel()); |
| 606 | StaticDtorSection |
| 607 | = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 608 | } else { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 609 | StaticCtorSection |
| 610 | = getMachOSection("__DATA", "__mod_init_func", |
| 611 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 612 | SectionKind::getDataRel()); |
| 613 | StaticDtorSection |
| 614 | = getMachOSection("__DATA", "__mod_term_func", |
| 615 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 616 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 619 | // Exception Handling. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 620 | LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 621 | SectionKind::getDataRel()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 622 | EHFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 623 | getMachOSection("__TEXT", "__eh_frame", |
| 624 | MCSectionMachO::S_COALESCED | |
| 625 | MCSectionMachO::S_ATTR_NO_TOC | |
| 626 | MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | |
| 627 | MCSectionMachO::S_ATTR_LIVE_SUPPORT, |
| 628 | SectionKind::getReadOnly()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 629 | |
| 630 | // Debug Information. |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 631 | DwarfAbbrevSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 632 | getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 633 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 634 | DwarfInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 635 | getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 636 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 637 | DwarfLineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 638 | getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 639 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 640 | DwarfFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 641 | getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 642 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 643 | DwarfPubNamesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 644 | getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 645 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 646 | DwarfPubTypesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 647 | getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 648 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 649 | DwarfStrSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 650 | getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 651 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 652 | DwarfLocSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 653 | getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 654 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 655 | DwarfARangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 656 | getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 657 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 658 | DwarfRangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 659 | getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 660 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 661 | DwarfMacroInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 662 | getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 663 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 664 | DwarfDebugInlineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 665 | getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 666 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 669 | /// getLazySymbolPointerSection - Return the section corresponding to |
| 670 | /// the .lazy_symbol_pointer directive. |
| 671 | const MCSection *TargetLoweringObjectFileMachO:: |
| 672 | getLazySymbolPointerSection() const { |
| 673 | return getMachOSection("__DATA", "__la_symbol_ptr", |
| 674 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 675 | SectionKind::getMetadata()); |
| 676 | } |
| 677 | |
| 678 | /// getNonLazySymbolPointerSection - Return the section corresponding to |
| 679 | /// the .non_lazy_symbol_pointer directive. |
| 680 | const MCSection *TargetLoweringObjectFileMachO:: |
| 681 | getNonLazySymbolPointerSection() const { |
| 682 | return getMachOSection("__DATA", "__nl_symbol_ptr", |
| 683 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 684 | SectionKind::getMetadata()); |
| 685 | } |
| 686 | |
| 687 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 688 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 689 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 690 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 691 | // Parse the section specifier and create it if valid. |
| 692 | StringRef Segment, Section; |
| 693 | unsigned TAA, StubSize; |
| 694 | std::string ErrorCode = |
| 695 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
| 696 | TAA, StubSize); |
| 697 | if (ErrorCode.empty()) |
| 698 | return getMachOSection(Segment, Section, TAA, StubSize, Kind); |
| 699 | |
| 700 | |
| 701 | // If invalid, report the error with llvm_report_error. |
| 702 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 703 | "' has an invalid section specifier '" + GV->getSection() + |
| 704 | "': " + ErrorCode + "."); |
| 705 | // Fall back to dropping it into the data section. |
| 706 | return DataSection; |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 710 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 711 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 712 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 713 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 714 | if (Kind.isText()) |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 715 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 716 | |
| 717 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 718 | // or data depending on if it is writable. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 719 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 720 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 721 | return ConstTextCoalSection; |
| 722 | return DataCoalSection; |
| 723 | } |
| 724 | |
| 725 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 726 | if (Kind.isMergeable1ByteCString() || |
| 727 | Kind.isMergeable2ByteCString()) { |
| 728 | if (TM.getTargetData()->getPreferredAlignment( |
| 729 | cast<GlobalVariable>(GV)) < 32) { |
| 730 | if (Kind.isMergeable1ByteCString()) |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 731 | return CStringSection; |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 732 | assert(Kind.isMergeable2ByteCString()); |
| 733 | return UStringSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 734 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 737 | if (Kind.isMergeableConst()) { |
| 738 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 739 | return FourByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 740 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 741 | return EightByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 742 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 743 | return SixteenByteConstantSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 744 | } |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 745 | |
| 746 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 747 | // just drop it in .const. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 748 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 749 | return ReadOnlySection; |
| 750 | |
| 751 | // If this is marked const, put it into a const section. But if the dynamic |
| 752 | // linker needs to write to it, put it in the data segment. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 753 | if (Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 754 | return ConstDataSection; |
| 755 | |
| 756 | // Otherwise, just drop the variable in the normal data section. |
| 757 | return DataSection; |
| 758 | } |
| 759 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 760 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 761 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 762 | // If this constant requires a relocation, we have to put it in the data |
| 763 | // segment, not in the text segment. |
| 764 | if (Kind.isDataRel()) |
| 765 | return ConstDataSection; |
| 766 | |
| 767 | if (Kind.isMergeableConst4()) |
| 768 | return FourByteConstantSection; |
| 769 | if (Kind.isMergeableConst8()) |
| 770 | return EightByteConstantSection; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 771 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 772 | return SixteenByteConstantSection; |
| 773 | return ReadOnlySection; // .const |
| 774 | } |
| 775 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 776 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 777 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 778 | // FIXME: REMOVE this (rdar://7071300) |
| 779 | bool TargetLoweringObjectFileMachO:: |
| 780 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 781 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 782 | /// the directive emitted (this occurs in ObjC metadata). |
| 783 | if (!GV) return false; |
| 784 | |
| 785 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 786 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 787 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 788 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 789 | // this horrible hack can go away. |
| 790 | const std::string &Name = Mang->getMangledName(GV); |
| 791 | if (Name[0] == 'L' || Name[0] == 'l') |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | return true; |
| 796 | } |
| 797 | |
| 798 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 799 | //===----------------------------------------------------------------------===// |
| 800 | // COFF |
| 801 | //===----------------------------------------------------------------------===// |
| 802 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 804 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 805 | getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 806 | if (MCSection *S = getContext().GetSection(Name)) |
| 807 | return S; |
Chris Lattner | 7c599d0 | 2009-08-08 20:52:13 +0000 | [diff] [blame] | 808 | return MCSectionCOFF::Create(Name, isDirective, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 811 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 812 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 813 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 814 | TextSection = getCOFFSection("\t.text", true, SectionKind::getText()); |
| 815 | DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 816 | StaticCtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 817 | getCOFFSection(".ctors", false, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 818 | StaticDtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 819 | getCOFFSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 820 | |
| 821 | |
| 822 | // Debug info. |
| 823 | // FIXME: Don't use 'directive' mode here. |
| 824 | DwarfAbbrevSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 825 | getCOFFSection("\t.section\t.debug_abbrev,\"dr\"", |
| 826 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 827 | DwarfInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 828 | getCOFFSection("\t.section\t.debug_info,\"dr\"", |
| 829 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 830 | DwarfLineSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 831 | getCOFFSection("\t.section\t.debug_line,\"dr\"", |
| 832 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 833 | DwarfFrameSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 834 | getCOFFSection("\t.section\t.debug_frame,\"dr\"", |
| 835 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 836 | DwarfPubNamesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 837 | getCOFFSection("\t.section\t.debug_pubnames,\"dr\"", |
| 838 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 839 | DwarfPubTypesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 840 | getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"", |
| 841 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 842 | DwarfStrSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 843 | getCOFFSection("\t.section\t.debug_str,\"dr\"", |
| 844 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 845 | DwarfLocSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 846 | getCOFFSection("\t.section\t.debug_loc,\"dr\"", |
| 847 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 848 | DwarfARangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 849 | getCOFFSection("\t.section\t.debug_aranges,\"dr\"", |
| 850 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 851 | DwarfRangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 852 | getCOFFSection("\t.section\t.debug_ranges,\"dr\"", |
| 853 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 854 | DwarfMacroInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 855 | getCOFFSection("\t.section\t.debug_macinfo,\"dr\"", |
| 856 | true, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 859 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 860 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 861 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 862 | return getCOFFSection(GV->getSection().c_str(), false, Kind); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 865 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 866 | if (Kind.isText()) |
| 867 | return ".text$linkonce"; |
| 868 | if (Kind.isWriteable()) |
| 869 | return ".data$linkonce"; |
| 870 | return ".rdata$linkonce"; |
| 871 | } |
| 872 | |
| 873 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 874 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 875 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 876 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 877 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 878 | |
| 879 | // If this global is linkonce/weak and the target handles this by emitting it |
| 880 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 881 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 882 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 883 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 884 | return getCOFFSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 887 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 888 | return getTextSection(); |
| 889 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 890 | return getDataSection(); |
| 891 | } |
| 892 | |