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" |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSectionELF.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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 283 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
| 284 | |
| 285 | TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() { |
| 286 | // If we have the section uniquing map, free it. |
| 287 | delete (ELFUniqueMapTy*)UniquingMap; |
| 288 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 289 | |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 290 | const MCSection *TargetLoweringObjectFileELF:: |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 291 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 292 | SectionKind Kind, bool IsExplicit) const { |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 293 | if (UniquingMap == 0) |
| 294 | UniquingMap = new ELFUniqueMapTy(); |
| 295 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap; |
| 296 | |
| 297 | // Do the lookup, if we have a hit, return it. |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 298 | const MCSectionELF *&Entry = Map[Section]; |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 299 | if (Entry) return Entry; |
| 300 | |
Bruno Cardoso Lopes | fdf229e | 2009-08-13 23:30:21 +0000 | [diff] [blame] | 301 | return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit, |
| 302 | getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 305 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 306 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 307 | if (UniquingMap != 0) |
| 308 | ((ELFUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 309 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 310 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 311 | BSSSection = |
| 312 | getELFSection(".bss", MCSectionELF::SHT_NOBITS, |
| 313 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 314 | SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 315 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 316 | TextSection = |
| 317 | getELFSection(".text", MCSectionELF::SHT_PROGBITS, |
| 318 | MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC, |
| 319 | SectionKind::getText()); |
| 320 | |
| 321 | DataSection = |
| 322 | getELFSection(".data", MCSectionELF::SHT_PROGBITS, |
| 323 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 324 | SectionKind::getDataRel()); |
| 325 | |
| 326 | ReadOnlySection = |
| 327 | getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, |
| 328 | MCSectionELF::SHF_ALLOC, |
| 329 | SectionKind::getReadOnly()); |
| 330 | |
| 331 | TLSDataSection = |
| 332 | getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, |
| 333 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 334 | MCSectionELF::SHF_WRITE, SectionKind::getThreadData()); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 335 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 336 | TLSBSSSection = |
| 337 | getELFSection(".tbss", MCSectionELF::SHT_NOBITS, |
| 338 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 339 | MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 340 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 341 | DataRelSection = |
| 342 | getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, |
| 343 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 344 | SectionKind::getDataRel()); |
| 345 | |
| 346 | DataRelLocalSection = |
| 347 | getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, |
| 348 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 349 | SectionKind::getDataRelLocal()); |
| 350 | |
| 351 | DataRelROSection = |
| 352 | getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, |
| 353 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 354 | SectionKind::getReadOnlyWithRel()); |
| 355 | |
| 356 | DataRelROLocalSection = |
| 357 | getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, |
| 358 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 359 | SectionKind::getReadOnlyWithRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 360 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 361 | MergeableConst4Section = |
| 362 | getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, |
| 363 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 364 | SectionKind::getMergeableConst4()); |
| 365 | |
| 366 | MergeableConst8Section = |
| 367 | getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, |
| 368 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 369 | SectionKind::getMergeableConst8()); |
| 370 | |
| 371 | MergeableConst16Section = |
| 372 | getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, |
| 373 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 374 | SectionKind::getMergeableConst16()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 375 | |
| 376 | StaticCtorSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 377 | getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, |
| 378 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 379 | SectionKind::getDataRel()); |
| 380 | |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 381 | StaticDtorSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 382 | getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, |
| 383 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 384 | SectionKind::getDataRel()); |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 386 | // Exception Handling Sections. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 387 | |
| 388 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 389 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 390 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 391 | // adjusted or this should be a data section. |
| 392 | LSDASection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 393 | getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, |
| 394 | MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 395 | EHFrameSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 396 | getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, |
Chris Lattner | b6ab299 | 2009-08-15 16:54:02 +0000 | [diff] [blame] | 397 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 398 | SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 399 | |
| 400 | // Debug Info Sections. |
| 401 | DwarfAbbrevSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 402 | getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, |
| 403 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 404 | DwarfInfoSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 405 | getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, |
| 406 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 407 | DwarfLineSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 408 | getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, |
| 409 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 410 | DwarfFrameSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 411 | getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, |
| 412 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 413 | DwarfPubNamesSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 414 | getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, |
| 415 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 416 | DwarfPubTypesSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 417 | getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, |
| 418 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 419 | DwarfStrSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 420 | getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, |
| 421 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 422 | DwarfLocSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 423 | getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, |
| 424 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 425 | DwarfARangesSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 426 | getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, |
| 427 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 428 | DwarfRangesSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 429 | getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, |
| 430 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 431 | DwarfMacroInfoSection = |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 432 | getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, |
| 433 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 437 | static SectionKind |
| 438 | getELFKindForNamedSection(const char *Name, SectionKind K) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 439 | if (Name[0] != '.') return K; |
| 440 | |
| 441 | // Some lame default implementation based on some magic section names. |
| 442 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 443 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 444 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 445 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 446 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 447 | |
| 448 | if (strcmp(Name, ".tdata") == 0 || |
| 449 | strncmp(Name, ".tdata.", 7) == 0 || |
| 450 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 451 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 452 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 453 | |
| 454 | if (strcmp(Name, ".tbss") == 0 || |
| 455 | strncmp(Name, ".tbss.", 6) == 0 || |
| 456 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 457 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 458 | return SectionKind::getThreadBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 459 | |
| 460 | return K; |
| 461 | } |
| 462 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 463 | |
| 464 | static unsigned |
| 465 | getELFSectionType(const char *Name, SectionKind K) { |
| 466 | |
Dan Gohman | fa9ca0f | 2009-08-14 00:10:19 +0000 | [diff] [blame] | 467 | if (strcmp(Name, ".init_array") == 0) |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 468 | return MCSectionELF::SHT_INIT_ARRAY; |
| 469 | |
Dan Gohman | fa9ca0f | 2009-08-14 00:10:19 +0000 | [diff] [blame] | 470 | if (strcmp(Name, ".fini_array") == 0) |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 471 | return MCSectionELF::SHT_FINI_ARRAY; |
| 472 | |
Dan Gohman | fa9ca0f | 2009-08-14 00:10:19 +0000 | [diff] [blame] | 473 | if (strcmp(Name, ".preinit_array") == 0) |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 474 | return MCSectionELF::SHT_PREINIT_ARRAY; |
| 475 | |
| 476 | if (K.isBSS() || K.isThreadBSS()) |
| 477 | return MCSectionELF::SHT_NOBITS; |
| 478 | |
| 479 | return MCSectionELF::SHT_PROGBITS; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | static unsigned |
| 484 | getELFSectionFlags(SectionKind K) { |
| 485 | unsigned Flags = 0; |
| 486 | |
| 487 | if (!K.isMetadata()) |
| 488 | Flags |= MCSectionELF::SHF_ALLOC; |
| 489 | |
| 490 | if (K.isWriteable()) |
| 491 | Flags |= MCSectionELF::SHF_WRITE; |
| 492 | |
| 493 | if (K.isThreadLocal()) |
| 494 | Flags |= MCSectionELF::SHF_TLS; |
| 495 | |
| 496 | // K.isMergeableConst() is left out to honour PR4650 |
| 497 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 498 | K.isMergeableConst8() || K.isMergeableConst16()) |
| 499 | Flags |= MCSectionELF::SHF_MERGE; |
| 500 | |
| 501 | if (K.isMergeableCString()) |
| 502 | Flags |= MCSectionELF::SHF_STRINGS; |
| 503 | |
| 504 | return Flags; |
| 505 | } |
| 506 | |
| 507 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 508 | const MCSection *TargetLoweringObjectFileELF:: |
| 509 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 510 | Mangler *Mang, const TargetMachine &TM) const { |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 511 | const char *SectionName = GV->getSection().c_str(); |
| 512 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 513 | // Infer section flags from the section name if we can. |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 514 | Kind = getELFKindForNamedSection(SectionName, Kind); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 515 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 516 | return getELFSection(SectionName, |
| 517 | getELFSectionType(SectionName, Kind), |
| 518 | getELFSectionFlags(Kind), Kind, true); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 519 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 520 | |
| 521 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 522 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 523 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 524 | |
| 525 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 526 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 527 | |
| 528 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 529 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 530 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 531 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 532 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 533 | |
| 534 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 535 | return ".gnu.linkonce.d.rel.ro."; |
| 536 | } |
| 537 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 538 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 539 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 540 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 541 | |
| 542 | // If this global is linkonce/weak and the target handles this by emitting it |
| 543 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 544 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 545 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | b8f396b | 2009-07-29 05:20:33 +0000 | [diff] [blame] | 546 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 547 | |
| 548 | return getELFSection((Prefix+Name).c_str(), |
| 549 | getELFSectionType((Prefix+Name).c_str(), Kind), |
| 550 | getELFSectionFlags(Kind), |
| 551 | Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 554 | if (Kind.isText()) return TextSection; |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 556 | if (Kind.isMergeable1ByteCString() || |
| 557 | Kind.isMergeable2ByteCString() || |
| 558 | Kind.isMergeable4ByteCString()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 560 | // We also need alignment here. |
| 561 | // FIXME: this is getting the alignment of the character, not the |
| 562 | // alignment of the global! |
| 563 | unsigned Align = |
| 564 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 566 | const char *SizeSpec = ".rodata.str1."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 567 | if (Kind.isMergeable2ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 568 | SizeSpec = ".rodata.str2."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 569 | else if (Kind.isMergeable4ByteCString()) |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 570 | SizeSpec = ".rodata.str4."; |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 571 | else |
| 572 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 573 | |
| 574 | |
Chris Lattner | 7e88a50 | 2009-08-04 16:19:50 +0000 | [diff] [blame] | 575 | std::string Name = SizeSpec + utostr(Align); |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 576 | return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS, |
| 577 | MCSectionELF::SHF_ALLOC | |
| 578 | MCSectionELF::SHF_MERGE | |
| 579 | MCSectionELF::SHF_STRINGS, |
| 580 | Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 583 | if (Kind.isMergeableConst()) { |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 584 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 585 | return MergeableConst4Section; |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 586 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 587 | return MergeableConst8Section; |
Chris Lattner | 203b3e9 | 2009-08-15 06:08:34 +0000 | [diff] [blame] | 588 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 589 | return MergeableConst16Section; |
| 590 | return ReadOnlySection; // .const |
| 591 | } |
| 592 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 593 | if (Kind.isReadOnly()) return ReadOnlySection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 594 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 595 | if (Kind.isThreadData()) return TLSDataSection; |
| 596 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 598 | if (Kind.isBSS()) return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 599 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 600 | if (Kind.isDataNoRel()) return DataSection; |
| 601 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 602 | if (Kind.isDataRel()) return DataRelSection; |
| 603 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 604 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 605 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 606 | return DataRelROSection; |
| 607 | } |
| 608 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 609 | /// getSectionForConstant - Given a mergeable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 610 | /// specified size and relocation information, return a section that it |
| 611 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 612 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 613 | getSectionForConstant(SectionKind Kind) const { |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 614 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 615 | return MergeableConst4Section; |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 616 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 617 | return MergeableConst8Section; |
Richard Osborne | 2a5e23b | 2009-08-17 16:37:11 +0000 | [diff] [blame] | 618 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 619 | return MergeableConst16Section; |
| 620 | if (Kind.isReadOnly()) |
| 621 | return ReadOnlySection; |
| 622 | |
| 623 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 624 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 625 | return DataRelROSection; |
| 626 | } |
| 627 | |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | // MachO |
| 630 | //===----------------------------------------------------------------------===// |
| 631 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 632 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 633 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 634 | TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() { |
| 635 | // If we have the MachO uniquing map, free it. |
| 636 | delete (MachOUniqueMapTy*)UniquingMap; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | const MCSectionMachO *TargetLoweringObjectFileMachO:: |
Chris Lattner | d3c4486f | 2009-08-12 23:34:27 +0000 | [diff] [blame] | 641 | getMachOSection(const StringRef &Segment, const StringRef &Section, |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 642 | unsigned TypeAndAttributes, |
| 643 | unsigned Reserved2, SectionKind Kind) const { |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 644 | // We unique sections by their segment/section pair. The returned section |
| 645 | // may not have the same flags as the requested section, if so this should be |
| 646 | // diagnosed by the client as an error. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 5dc47ff | 2009-08-12 23:55:02 +0000 | [diff] [blame] | 648 | // Create the map if it doesn't already exist. |
| 649 | if (UniquingMap == 0) |
| 650 | UniquingMap = new MachOUniqueMapTy(); |
| 651 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap; |
| 652 | |
| 653 | // Form the name to look up. |
| 654 | SmallString<64> Name; |
| 655 | Name.append(Segment.begin(), Segment.end()); |
| 656 | Name.push_back(','); |
| 657 | Name.append(Section.begin(), Section.end()); |
| 658 | |
| 659 | // Do the lookup, if we have a hit, return it. |
| 660 | const MCSectionMachO *&Entry = Map[StringRef(Name.data(), Name.size())]; |
| 661 | if (Entry) return Entry; |
| 662 | |
| 663 | // Otherwise, return a new section. |
| 664 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, |
| 665 | Reserved2, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 668 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 669 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 670 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 671 | if (UniquingMap != 0) |
| 672 | ((MachOUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 673 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 674 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 675 | TextSection // .text |
| 676 | = getMachOSection("__TEXT", "__text", |
| 677 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 678 | SectionKind::getText()); |
| 679 | DataSection // .data |
| 680 | = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel()); |
| 681 | |
| 682 | CStringSection // .cstring |
| 683 | = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS, |
| 684 | SectionKind::getMergeable1ByteCString()); |
| 685 | UStringSection |
| 686 | = getMachOSection("__TEXT","__ustring", 0, |
| 687 | SectionKind::getMergeable2ByteCString()); |
| 688 | FourByteConstantSection // .literal4 |
| 689 | = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS, |
| 690 | SectionKind::getMergeableConst4()); |
| 691 | EightByteConstantSection // .literal8 |
| 692 | = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS, |
| 693 | SectionKind::getMergeableConst8()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 694 | |
| 695 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 696 | // to using it in -static mode. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 697 | SixteenByteConstantSection = 0; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 698 | if (TM.getRelocationModel() != Reloc::Static && |
| 699 | TM.getTargetData()->getPointerSize() == 32) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 700 | SixteenByteConstantSection = // .literal16 |
| 701 | getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 702 | SectionKind::getMergeableConst16()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 703 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 704 | ReadOnlySection // .const |
| 705 | = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 706 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 707 | TextCoalSection |
| 708 | = getMachOSection("__TEXT", "__textcoal_nt", |
| 709 | MCSectionMachO::S_COALESCED | |
| 710 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 711 | SectionKind::getText()); |
| 712 | ConstTextCoalSection |
| 713 | = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED, |
| 714 | SectionKind::getText()); |
| 715 | ConstDataCoalSection |
| 716 | = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED, |
| 717 | SectionKind::getText()); |
| 718 | ConstDataSection // .const_data |
| 719 | = getMachOSection("__DATA", "__const", 0, |
| 720 | SectionKind::getReadOnlyWithRel()); |
| 721 | DataCoalSection |
| 722 | = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED, |
| 723 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 724 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 725 | |
| 726 | LazySymbolPointerSection |
| 727 | = getMachOSection("__DATA", "__la_symbol_ptr", |
| 728 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 729 | SectionKind::getMetadata()); |
| 730 | NonLazySymbolPointerSection |
| 731 | = getMachOSection("__DATA", "__nl_symbol_ptr", |
| 732 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 733 | SectionKind::getMetadata()); |
| 734 | |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 735 | if (TM.getRelocationModel() == Reloc::Static) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 736 | StaticCtorSection |
| 737 | = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel()); |
| 738 | StaticDtorSection |
| 739 | = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 740 | } else { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 741 | StaticCtorSection |
| 742 | = getMachOSection("__DATA", "__mod_init_func", |
| 743 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 744 | SectionKind::getDataRel()); |
| 745 | StaticDtorSection |
| 746 | = getMachOSection("__DATA", "__mod_term_func", |
| 747 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 748 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 751 | // Exception Handling. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 752 | LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 753 | SectionKind::getDataRel()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 754 | EHFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 755 | getMachOSection("__TEXT", "__eh_frame", |
| 756 | MCSectionMachO::S_COALESCED | |
| 757 | MCSectionMachO::S_ATTR_NO_TOC | |
| 758 | MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | |
| 759 | MCSectionMachO::S_ATTR_LIVE_SUPPORT, |
| 760 | SectionKind::getReadOnly()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 761 | |
| 762 | // Debug Information. |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 763 | DwarfAbbrevSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 764 | getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 765 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 766 | DwarfInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 767 | getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 768 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 769 | DwarfLineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 770 | getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 771 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 772 | DwarfFrameSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 773 | getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 774 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 775 | DwarfPubNamesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 776 | getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 777 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 778 | DwarfPubTypesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 779 | getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 780 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 781 | DwarfStrSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 782 | getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 783 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 784 | DwarfLocSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 785 | getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 786 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 787 | DwarfARangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 788 | getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 789 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 790 | DwarfRangesSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 791 | getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 792 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 793 | DwarfMacroInfoSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 794 | getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 795 | SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 796 | DwarfDebugInlineSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 797 | getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 798 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 801 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 802 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 803 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 804 | // Parse the section specifier and create it if valid. |
| 805 | StringRef Segment, Section; |
| 806 | unsigned TAA, StubSize; |
| 807 | std::string ErrorCode = |
| 808 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
| 809 | TAA, StubSize); |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 810 | if (!ErrorCode.empty()) { |
| 811 | // If invalid, report the error with llvm_report_error. |
| 812 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 813 | "' has an invalid section specifier '" + GV->getSection()+ |
| 814 | "': " + ErrorCode + "."); |
| 815 | // Fall back to dropping it into the data section. |
| 816 | return DataSection; |
| 817 | } |
| 818 | |
| 819 | // Get the section. |
| 820 | const MCSectionMachO *S = |
| 821 | getMachOSection(Segment, Section, TAA, StubSize, Kind); |
| 822 | |
| 823 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 824 | // If the user declared multiple globals with different section flags, we need |
| 825 | // to reject it here. |
| 826 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
| 827 | // If invalid, report the error with llvm_report_error. |
| 828 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 829 | "' section type or attributes does not match previous" |
| 830 | " section specifier"); |
| 831 | } |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 832 | |
Chris Lattner | e309cfa | 2009-08-13 00:05:07 +0000 | [diff] [blame] | 833 | return S; |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 837 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 838 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 839 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 840 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 841 | if (Kind.isText()) |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 842 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 843 | |
| 844 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 845 | // or data depending on if it is writable. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 846 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 847 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 848 | return ConstTextCoalSection; |
| 849 | return DataCoalSection; |
| 850 | } |
| 851 | |
| 852 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 853 | if (Kind.isMergeable1ByteCString() || |
| 854 | Kind.isMergeable2ByteCString()) { |
| 855 | if (TM.getTargetData()->getPreferredAlignment( |
| 856 | cast<GlobalVariable>(GV)) < 32) { |
| 857 | if (Kind.isMergeable1ByteCString()) |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 858 | return CStringSection; |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 859 | assert(Kind.isMergeable2ByteCString()); |
| 860 | return UStringSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 864 | if (Kind.isMergeableConst()) { |
| 865 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 866 | return FourByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 867 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 868 | return EightByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 869 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 870 | return SixteenByteConstantSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 871 | } |
Chris Lattner | ec40975 | 2009-08-04 16:27:13 +0000 | [diff] [blame] | 872 | |
| 873 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 874 | // just drop it in .const. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 875 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 876 | return ReadOnlySection; |
| 877 | |
| 878 | // If this is marked const, put it into a const section. But if the dynamic |
| 879 | // linker needs to write to it, put it in the data segment. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 880 | if (Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 881 | return ConstDataSection; |
| 882 | |
| 883 | // Otherwise, just drop the variable in the normal data section. |
| 884 | return DataSection; |
| 885 | } |
| 886 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 887 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 888 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 889 | // If this constant requires a relocation, we have to put it in the data |
| 890 | // segment, not in the text segment. |
| 891 | if (Kind.isDataRel()) |
| 892 | return ConstDataSection; |
| 893 | |
| 894 | if (Kind.isMergeableConst4()) |
| 895 | return FourByteConstantSection; |
| 896 | if (Kind.isMergeableConst8()) |
| 897 | return EightByteConstantSection; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 898 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 899 | return SixteenByteConstantSection; |
| 900 | return ReadOnlySection; // .const |
| 901 | } |
| 902 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 903 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 904 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 905 | // FIXME: REMOVE this (rdar://7071300) |
| 906 | bool TargetLoweringObjectFileMachO:: |
| 907 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 908 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 909 | /// the directive emitted (this occurs in ObjC metadata). |
| 910 | if (!GV) return false; |
| 911 | |
| 912 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 913 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 914 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 915 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 916 | // this horrible hack can go away. |
| 917 | const std::string &Name = Mang->getMangledName(GV); |
| 918 | if (Name[0] == 'L' || Name[0] == 'l') |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 926 | //===----------------------------------------------------------------------===// |
| 927 | // COFF |
| 928 | //===----------------------------------------------------------------------===// |
| 929 | |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 930 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
| 931 | |
| 932 | TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() { |
| 933 | delete (COFFUniqueMapTy*)UniquingMap; |
| 934 | } |
| 935 | |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 937 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 938 | getCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | 38cff38 | 2009-08-13 00:37:15 +0000 | [diff] [blame] | 939 | // Create the map if it doesn't already exist. |
| 940 | if (UniquingMap == 0) |
| 941 | UniquingMap = new MachOUniqueMapTy(); |
| 942 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap; |
| 943 | |
| 944 | // Do the lookup, if we have a hit, return it. |
| 945 | const MCSectionCOFF *&Entry = Map[Name]; |
| 946 | if (Entry) return Entry; |
| 947 | |
| 948 | return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 951 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 952 | const TargetMachine &TM) { |
Benjamin Kramer | 76d5ccf | 2009-08-17 17:05:44 +0000 | [diff] [blame] | 953 | if (UniquingMap != 0) |
| 954 | ((COFFUniqueMapTy*)UniquingMap)->clear(); |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 955 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 956 | TextSection = getCOFFSection("\t.text", true, SectionKind::getText()); |
| 957 | DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 958 | StaticCtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 959 | getCOFFSection(".ctors", false, SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 960 | StaticDtorSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 961 | getCOFFSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 962 | |
| 963 | |
| 964 | // Debug info. |
| 965 | // FIXME: Don't use 'directive' mode here. |
| 966 | DwarfAbbrevSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 967 | getCOFFSection("\t.section\t.debug_abbrev,\"dr\"", |
| 968 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 969 | DwarfInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 970 | getCOFFSection("\t.section\t.debug_info,\"dr\"", |
| 971 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 972 | DwarfLineSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 973 | getCOFFSection("\t.section\t.debug_line,\"dr\"", |
| 974 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 975 | DwarfFrameSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 976 | getCOFFSection("\t.section\t.debug_frame,\"dr\"", |
| 977 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 978 | DwarfPubNamesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 979 | getCOFFSection("\t.section\t.debug_pubnames,\"dr\"", |
| 980 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 981 | DwarfPubTypesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 982 | getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"", |
| 983 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 984 | DwarfStrSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 985 | getCOFFSection("\t.section\t.debug_str,\"dr\"", |
| 986 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 987 | DwarfLocSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 988 | getCOFFSection("\t.section\t.debug_loc,\"dr\"", |
| 989 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 990 | DwarfARangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 991 | getCOFFSection("\t.section\t.debug_aranges,\"dr\"", |
| 992 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 993 | DwarfRangesSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 994 | getCOFFSection("\t.section\t.debug_ranges,\"dr\"", |
| 995 | true, SectionKind::getMetadata()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 996 | DwarfMacroInfoSection = |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 997 | getCOFFSection("\t.section\t.debug_macinfo,\"dr\"", |
| 998 | true, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 1001 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 1002 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 1003 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1004 | return getCOFFSection(GV->getSection().c_str(), false, Kind); |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1007 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 1008 | if (Kind.isText()) |
| 1009 | return ".text$linkonce"; |
| 1010 | if (Kind.isWriteable()) |
| 1011 | return ".data$linkonce"; |
| 1012 | return ".rdata$linkonce"; |
| 1013 | } |
| 1014 | |
| 1015 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 1016 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1017 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 1018 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1019 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1020 | |
| 1021 | // If this global is linkonce/weak and the target handles this by emitting it |
| 1022 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 1023 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1024 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 1025 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | 0c0cb71 | 2009-08-08 20:22:20 +0000 | [diff] [blame] | 1026 | return getCOFFSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 1029 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1030 | return getTextSection(); |
| 1031 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1032 | return getDataSection(); |
| 1033 | } |
| 1034 | |