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" |
| 20 | #include "llvm/MC/MCSection.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mangler.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Generic Code |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 32 | TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 33 | TextSection = 0; |
| 34 | DataSection = 0; |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 35 | BSSSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 36 | ReadOnlySection = 0; |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 37 | StaticCtorSection = 0; |
| 38 | StaticDtorSection = 0; |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame^] | 39 | LSDASection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
| 43 | } |
| 44 | |
| 45 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 46 | Constant *C = GV->getInitializer(); |
| 47 | |
| 48 | // Must have zero initializer. |
| 49 | if (!C->isNullValue()) |
| 50 | return false; |
| 51 | |
| 52 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 53 | if (GV->isConstant()) |
| 54 | return false; |
| 55 | |
| 56 | // If the global has an explicit section specified, don't put it in BSS. |
| 57 | if (!GV->getSection().empty()) |
| 58 | return false; |
| 59 | |
| 60 | // If -nozero-initialized-in-bss is specified, don't ever use BSS. |
| 61 | if (NoZerosInBSS) |
| 62 | return false; |
| 63 | |
| 64 | // Otherwise, put it in BSS! |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | static bool isConstantString(const Constant *C) { |
| 69 | // First check: is we have constant array of i8 terminated with zero |
| 70 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 71 | // Check, if initializer is a null-terminated string |
| 72 | if (CVA && CVA->isCString()) |
| 73 | return true; |
| 74 | |
| 75 | // Another possibility: [1 x i8] zeroinitializer |
| 76 | if (isa<ConstantAggregateZero>(C)) |
| 77 | if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) |
| 78 | return (Ty->getElementType() == Type::Int8Ty && |
| 79 | Ty->getNumElements() == 1); |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 84 | /// SectionKindForGlobal - This is a top-level target-independent classifier for |
| 85 | /// a global variable. Given an global variable and information from TM, it |
| 86 | /// classifies the global in a variety of ways that make various target |
| 87 | /// implementations simpler. The target implementation is free to ignore this |
| 88 | /// extra info of course. |
| 89 | static SectionKind SectionKindForGlobal(const GlobalValue *GV, |
| 90 | const TargetMachine &TM) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 91 | Reloc::Model ReloModel = TM.getRelocationModel(); |
| 92 | |
| 93 | // Early exit - functions should be always in text sections. |
| 94 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 95 | if (GVar == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 96 | return SectionKind::getText(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | // Handle thread-local data first. |
| 100 | if (GVar->isThreadLocal()) { |
| 101 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 102 | return SectionKind::getThreadBSS(); |
| 103 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Variable can be easily put to BSS section. |
| 107 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 108 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 109 | |
| 110 | Constant *C = GVar->getInitializer(); |
| 111 | |
| 112 | // If the global is marked constant, we can put it into a mergable section, |
| 113 | // a mergable string section, or general .data if it contains relocations. |
| 114 | if (GVar->isConstant()) { |
| 115 | // If the initializer for the global contains something that requires a |
| 116 | // relocation, then we may have to drop this into a wriable data section |
| 117 | // even though it is marked const. |
| 118 | switch (C->getRelocationInfo()) { |
| 119 | default: llvm_unreachable("unknown relocation info kind"); |
| 120 | case Constant::NoRelocation: |
| 121 | // If initializer is a null-terminated string, put it in a "cstring" |
| 122 | // section if the target has it. |
| 123 | if (isConstantString(C)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 124 | return SectionKind::getMergeableCString(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 125 | |
| 126 | // Otherwise, just drop it into a mergable constant section. If we have |
| 127 | // a section for this size, use it, otherwise use the arbitrary sized |
| 128 | // mergable section. |
| 129 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 130 | case 4: return SectionKind::getMergeableConst4(); |
| 131 | case 8: return SectionKind::getMergeableConst8(); |
| 132 | case 16: return SectionKind::getMergeableConst16(); |
| 133 | default: return SectionKind::getMergeableConst(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | case Constant::LocalRelocation: |
| 137 | // In static relocation model, the linker will resolve all addresses, so |
| 138 | // the relocation entries will actually be constants by the time the app |
| 139 | // starts up. However, we can't put this into a mergable section, because |
| 140 | // the linker doesn't take relocations into consideration when it tries to |
| 141 | // merge entries in the section. |
| 142 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 143 | return SectionKind::getReadOnly(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 144 | |
| 145 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 146 | // writable data.rel.local section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 147 | return SectionKind::getReadOnlyWithRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 148 | |
| 149 | case Constant::GlobalRelocations: |
| 150 | // In static relocation model, the linker will resolve all addresses, so |
| 151 | // the relocation entries will actually be constants by the time the app |
| 152 | // starts up. However, we can't put this into a mergable section, because |
| 153 | // the linker doesn't take relocations into consideration when it tries to |
| 154 | // merge entries in the section. |
| 155 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 156 | return SectionKind::getReadOnly(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 157 | |
| 158 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 159 | // writable data.rel section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 160 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | // Okay, this isn't a constant. If the initializer for the global is going |
| 165 | // to require a runtime relocation by the dynamic linker, put it into a more |
| 166 | // specific section to improve startup time of the app. This coalesces these |
| 167 | // globals together onto fewer pages, improving the locality of the dynamic |
| 168 | // linker. |
| 169 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 170 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 171 | |
| 172 | switch (C->getRelocationInfo()) { |
| 173 | default: llvm_unreachable("unknown relocation info kind"); |
| 174 | case Constant::NoRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 175 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 176 | case Constant::LocalRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 177 | return SectionKind::getDataRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 178 | case Constant::GlobalRelocations: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 179 | return SectionKind::getDataRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /// SectionForGlobal - This method computes the appropriate section to emit |
| 184 | /// the specified global variable or function definition. This should not |
| 185 | /// be passed external (or available externally) globals. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 186 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 187 | SectionForGlobal(const GlobalValue *GV, Mangler *Mang, |
| 188 | const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 189 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && |
| 190 | "Can only be used for global definitions"); |
| 191 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 192 | SectionKind Kind = SectionKindForGlobal(GV, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 193 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 194 | // Select section name. |
| 195 | if (GV->hasSection()) { |
| 196 | // If the target has special section hacks for specifically named globals, |
| 197 | // return them now. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 198 | if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind)) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 199 | return TS; |
| 200 | |
| 201 | // If the target has magic semantics for certain section names, make sure to |
| 202 | // pick up the flags. This allows the user to write things with attribute |
| 203 | // section and still get the appropriate section flags printed. |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 204 | Kind = getKindForNamedSection(GV->getSection().c_str(), Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 206 | return getOrCreateSection(GV->getSection().c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
| 210 | // Use default section depending on the 'type' of global |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 211 | return SelectSectionForGlobal(GV, Kind, Mang, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Lame default implementation. Calculate the section name for global. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 215 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 216 | TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 217 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 218 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 219 | const TargetMachine &TM) const{ |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 220 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 221 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 222 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 223 | return getTextSection(); |
| 224 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 225 | if (Kind.isBSS() && BSSSection != 0) |
| 226 | return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 227 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 228 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 229 | return ReadOnlySection; |
| 230 | |
| 231 | return getDataSection(); |
| 232 | } |
| 233 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 234 | /// getSectionForConstant - Given a mergable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 235 | /// specified size and relocation information, return a section that it |
| 236 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 237 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 238 | TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 239 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
| 240 | return ReadOnlySection; |
| 241 | |
| 242 | return DataSection; |
| 243 | } |
| 244 | |
| 245 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 246 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 247 | getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 248 | if (MCSection *S = Ctx->GetSection(Name)) |
| 249 | return S; |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 250 | return MCSection::Create(Name, isDirective, Kind, *Ctx); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | // ELF |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 259 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 260 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 261 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 262 | if (!HasCrazyBSS) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 263 | BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 264 | else |
| 265 | // PPC/Linux doesn't support the .bss directive, it needs .section .bss. |
| 266 | // FIXME: Does .section .bss work everywhere?? |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 267 | // FIXME2: this should just be handle by the section printer. We should get |
| 268 | // away from syntactic view of the sections and MCSection should just be a |
| 269 | // semantic view. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 270 | BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 271 | |
| 272 | |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 273 | TextSection = getOrCreateSection("\t.text", true, SectionKind::getText()); |
| 274 | DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 275 | ReadOnlySection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 276 | getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 277 | TLSDataSection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 278 | getOrCreateSection("\t.tdata", false, SectionKind::getThreadData()); |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 279 | CStringSection = getOrCreateSection("\t.rodata.str", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 280 | SectionKind::getMergeableCString()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 282 | TLSBSSSection = getOrCreateSection("\t.tbss", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 283 | SectionKind::getThreadBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 284 | |
| 285 | DataRelSection = getOrCreateSection("\t.data.rel", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 286 | SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 287 | DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 288 | SectionKind::getDataRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 289 | DataRelROSection = getOrCreateSection("\t.data.rel.ro", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 290 | SectionKind::getReadOnlyWithRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 291 | DataRelROLocalSection = |
| 292 | getOrCreateSection("\t.data.rel.ro.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 293 | SectionKind::getReadOnlyWithRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 294 | |
| 295 | MergeableConst4Section = getOrCreateSection(".rodata.cst4", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 296 | SectionKind::getMergeableConst4()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 297 | MergeableConst8Section = getOrCreateSection(".rodata.cst8", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 298 | SectionKind::getMergeableConst8()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 299 | MergeableConst16Section = getOrCreateSection(".rodata.cst16", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 300 | SectionKind::getMergeableConst16()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 301 | |
| 302 | StaticCtorSection = |
| 303 | getOrCreateSection(".ctors", false, SectionKind::getDataRel()); |
| 304 | StaticDtorSection = |
| 305 | getOrCreateSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame^] | 306 | |
| 307 | |
| 308 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 309 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 310 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 311 | // adjusted or this should be a data section. |
| 312 | LSDASection = |
| 313 | getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 317 | SectionKind TargetLoweringObjectFileELF:: |
| 318 | getKindForNamedSection(const char *Name, SectionKind K) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 319 | if (Name[0] != '.') return K; |
| 320 | |
| 321 | // Some lame default implementation based on some magic section names. |
| 322 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 323 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 324 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 325 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 326 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 327 | |
| 328 | if (strcmp(Name, ".tdata") == 0 || |
| 329 | strncmp(Name, ".tdata.", 7) == 0 || |
| 330 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 331 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 332 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 333 | |
| 334 | if (strcmp(Name, ".tbss") == 0 || |
| 335 | strncmp(Name, ".tbss.", 6) == 0 || |
| 336 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 337 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 338 | return SectionKind::getThreadBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 339 | |
| 340 | return K; |
| 341 | } |
| 342 | |
| 343 | void TargetLoweringObjectFileELF:: |
| 344 | getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { |
| 345 | Str.push_back(','); |
| 346 | Str.push_back('"'); |
| 347 | |
| 348 | if (!Kind.isMetadata()) |
| 349 | Str.push_back('a'); |
| 350 | if (Kind.isText()) |
| 351 | Str.push_back('x'); |
| 352 | if (Kind.isWriteable()) |
| 353 | Str.push_back('w'); |
Chris Lattner | 82987bf | 2009-07-31 16:17:13 +0000 | [diff] [blame] | 354 | if (Kind.isMergeableCString() || |
| 355 | Kind.isMergeableConst4() || |
| 356 | Kind.isMergeableConst8() || |
| 357 | Kind.isMergeableConst16()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 358 | Str.push_back('M'); |
| 359 | if (Kind.isMergeableCString()) |
| 360 | Str.push_back('S'); |
| 361 | if (Kind.isThreadLocal()) |
| 362 | Str.push_back('T'); |
| 363 | |
| 364 | Str.push_back('"'); |
| 365 | Str.push_back(','); |
| 366 | |
| 367 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 368 | if (AtIsCommentChar) |
| 369 | Str.push_back('%'); |
| 370 | else |
| 371 | Str.push_back('@'); |
| 372 | |
| 373 | const char *KindStr; |
Chris Lattner | bf15e43 | 2009-07-28 17:57:51 +0000 | [diff] [blame] | 374 | if (Kind.isBSS() || Kind.isThreadBSS()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 375 | KindStr = "nobits"; |
| 376 | else |
| 377 | KindStr = "progbits"; |
| 378 | |
| 379 | Str.append(KindStr, KindStr+strlen(KindStr)); |
| 380 | |
| 381 | if (Kind.isMergeableCString()) { |
| 382 | // TODO: Eventually handle multiple byte character strings. For now, all |
| 383 | // mergable C strings are single byte. |
| 384 | Str.push_back(','); |
| 385 | Str.push_back('1'); |
| 386 | } else if (Kind.isMergeableConst4()) { |
| 387 | Str.push_back(','); |
| 388 | Str.push_back('4'); |
| 389 | } else if (Kind.isMergeableConst8()) { |
| 390 | Str.push_back(','); |
| 391 | Str.push_back('8'); |
| 392 | } else if (Kind.isMergeableConst16()) { |
| 393 | Str.push_back(','); |
| 394 | Str.push_back('1'); |
| 395 | Str.push_back('6'); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 401 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 402 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 403 | |
| 404 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 405 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 406 | |
| 407 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 408 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 409 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 410 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 411 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 412 | |
| 413 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 414 | return ".gnu.linkonce.d.rel.ro."; |
| 415 | } |
| 416 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 417 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 418 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 419 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 420 | |
| 421 | // If this global is linkonce/weak and the target handles this by emitting it |
| 422 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 423 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 424 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | b8f396b | 2009-07-29 05:20:33 +0000 | [diff] [blame] | 425 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 426 | return getOrCreateSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 429 | if (Kind.isText()) return TextSection; |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 430 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 431 | if (Kind.isMergeableCString()) { |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 432 | assert(CStringSection && "Should have string section prefix"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 434 | // We also need alignment here. |
| 435 | // FIXME: this is getting the alignment of the character, not the |
| 436 | // alignment of the global! |
| 437 | unsigned Align = |
| 438 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 440 | std::string Name = CStringSection->getName() + "1." + utostr(Align); |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 441 | return getOrCreateSection(Name.c_str(), false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 442 | SectionKind::getMergeableCString()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 445 | if (Kind.isMergeableConst()) { |
| 446 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 447 | return MergeableConst4Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 448 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 449 | return MergeableConst8Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 450 | if (Kind.isMergeableConst16()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 451 | return MergeableConst16Section; |
| 452 | return ReadOnlySection; // .const |
| 453 | } |
| 454 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 455 | if (Kind.isReadOnly()) return ReadOnlySection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 456 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 457 | if (Kind.isThreadData()) return TLSDataSection; |
| 458 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 460 | if (Kind.isBSS()) return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 461 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 462 | if (Kind.isDataNoRel()) return DataSection; |
| 463 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 464 | if (Kind.isDataRel()) return DataRelSection; |
| 465 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 466 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 467 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 468 | return DataRelROSection; |
| 469 | } |
| 470 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 471 | /// getSectionForConstant - Given a mergeable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 472 | /// specified size and relocation information, return a section that it |
| 473 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 474 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 475 | getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 476 | if (Kind.isMergeableConst4()) |
| 477 | return MergeableConst4Section; |
| 478 | if (Kind.isMergeableConst8()) |
| 479 | return MergeableConst8Section; |
| 480 | if (Kind.isMergeableConst16()) |
| 481 | return MergeableConst16Section; |
| 482 | if (Kind.isReadOnly()) |
| 483 | return ReadOnlySection; |
| 484 | |
| 485 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 486 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 487 | return DataRelROSection; |
| 488 | } |
| 489 | |
| 490 | //===----------------------------------------------------------------------===// |
| 491 | // MachO |
| 492 | //===----------------------------------------------------------------------===// |
| 493 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 494 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 495 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 496 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 497 | TextSection = getOrCreateSection("\t.text", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 498 | SectionKind::getText()); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 499 | DataSection = getOrCreateSection("\t.data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 500 | SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 502 | CStringSection = getOrCreateSection("\t.cstring", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 503 | SectionKind::getMergeableCString()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 504 | FourByteConstantSection = getOrCreateSection("\t.literal4\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 505 | SectionKind::getMergeableConst4()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 506 | EightByteConstantSection = getOrCreateSection("\t.literal8\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 507 | SectionKind::getMergeableConst8()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 508 | |
| 509 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 510 | // to using it in -static mode. |
| 511 | if (TM.getRelocationModel() != Reloc::Static && |
| 512 | TM.getTargetData()->getPointerSize() == 32) |
| 513 | SixteenByteConstantSection = |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 514 | getOrCreateSection("\t.literal16\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 515 | SectionKind::getMergeableConst16()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 516 | else |
| 517 | SixteenByteConstantSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 519 | ReadOnlySection = getOrCreateSection("\t.const", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 520 | SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 521 | |
| 522 | TextCoalSection = |
| 523 | getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 524 | false, SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 525 | ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 526 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 527 | SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 528 | ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 529 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 530 | SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 531 | ConstDataSection = getOrCreateSection("\t.const_data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 532 | SectionKind::getReadOnlyWithRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 533 | DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 534 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 535 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 536 | |
| 537 | if (TM.getRelocationModel() == Reloc::Static) { |
| 538 | StaticCtorSection = |
| 539 | getOrCreateSection(".constructor", true, SectionKind::getDataRel()); |
| 540 | StaticDtorSection = |
| 541 | getOrCreateSection(".destructor", true, SectionKind::getDataRel()); |
| 542 | } else { |
| 543 | StaticCtorSection = |
| 544 | getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel()); |
| 545 | StaticDtorSection = |
| 546 | getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel()); |
| 547 | } |
| 548 | |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame^] | 549 | LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false, |
| 550 | SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 553 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 554 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 555 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 556 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 557 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 558 | if (Kind.isText()) |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 559 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 560 | |
| 561 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 562 | // or data depending on if it is writable. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 563 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 564 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 565 | return ConstTextCoalSection; |
| 566 | return DataCoalSection; |
| 567 | } |
| 568 | |
| 569 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 570 | if (Kind.isMergeableCString()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 571 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 572 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
| 573 | const TargetData &TD = *TM.getTargetData(); |
| 574 | unsigned Size = TD.getTypeAllocSize(Ty); |
| 575 | if (Size) { |
| 576 | unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV)); |
| 577 | if (Align <= 32) |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 578 | return CStringSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | return ReadOnlySection; |
| 582 | } |
| 583 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 584 | if (Kind.isMergeableConst()) { |
| 585 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 586 | return FourByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 587 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 588 | return EightByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 589 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 590 | return SixteenByteConstantSection; |
| 591 | return ReadOnlySection; // .const |
| 592 | } |
| 593 | |
| 594 | // FIXME: ROData -> const in -static mode that is relocatable but they happen |
| 595 | // by the static linker. Why not mergeable? |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 596 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 597 | return ReadOnlySection; |
| 598 | |
| 599 | // If this is marked const, put it into a const section. But if the dynamic |
| 600 | // linker needs to write to it, put it in the data segment. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 601 | if (Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 602 | return ConstDataSection; |
| 603 | |
| 604 | // Otherwise, just drop the variable in the normal data section. |
| 605 | return DataSection; |
| 606 | } |
| 607 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 608 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 609 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 610 | // If this constant requires a relocation, we have to put it in the data |
| 611 | // segment, not in the text segment. |
| 612 | if (Kind.isDataRel()) |
| 613 | return ConstDataSection; |
| 614 | |
| 615 | if (Kind.isMergeableConst4()) |
| 616 | return FourByteConstantSection; |
| 617 | if (Kind.isMergeableConst8()) |
| 618 | return EightByteConstantSection; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 619 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 620 | return SixteenByteConstantSection; |
| 621 | return ReadOnlySection; // .const |
| 622 | } |
| 623 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 624 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 625 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 626 | // FIXME: REMOVE this (rdar://7071300) |
| 627 | bool TargetLoweringObjectFileMachO:: |
| 628 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 629 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 630 | /// the directive emitted (this occurs in ObjC metadata). |
| 631 | if (!GV) return false; |
| 632 | |
| 633 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 634 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 635 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 636 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 637 | // this horrible hack can go away. |
| 638 | const std::string &Name = Mang->getMangledName(GV); |
| 639 | if (Name[0] == 'L' || Name[0] == 'l') |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 647 | //===----------------------------------------------------------------------===// |
| 648 | // COFF |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 651 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 652 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 653 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 654 | TextSection = getOrCreateSection("\t.text", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 655 | SectionKind::getText()); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 656 | DataSection = getOrCreateSection("\t.data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 657 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 658 | StaticCtorSection = |
| 659 | getOrCreateSection(".ctors", false, SectionKind::getDataRel()); |
| 660 | StaticDtorSection = |
| 661 | getOrCreateSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void TargetLoweringObjectFileCOFF:: |
| 665 | getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { |
| 666 | // FIXME: Inefficient. |
| 667 | std::string Res = ",\""; |
| 668 | if (Kind.isText()) |
| 669 | Res += 'x'; |
| 670 | if (Kind.isWriteable()) |
| 671 | Res += 'w'; |
| 672 | Res += "\""; |
| 673 | |
| 674 | Str.append(Res.begin(), Res.end()); |
| 675 | } |
| 676 | |
| 677 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 678 | if (Kind.isText()) |
| 679 | return ".text$linkonce"; |
| 680 | if (Kind.isWriteable()) |
| 681 | return ".data$linkonce"; |
| 682 | return ".rdata$linkonce"; |
| 683 | } |
| 684 | |
| 685 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 686 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 687 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 688 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 689 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 690 | |
| 691 | // If this global is linkonce/weak and the target handles this by emitting it |
| 692 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 693 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 694 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 695 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 696 | return getOrCreateSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 699 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 700 | return getTextSection(); |
| 701 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 702 | return getDataSection(); |
| 703 | } |
| 704 | |