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