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