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