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