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