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 StaticCtorSection = 0; 38 StaticDtorSection = 0; 39 LSDASection = 0; 40 EHFrameSection = 0; 41 42 DwarfAbbrevSection = 0; 43 DwarfInfoSection = 0; 44 DwarfLineSection = 0; 45 DwarfFrameSection = 0; 46 DwarfPubNamesSection = 0; 47 DwarfPubTypesSection = 0; 48 DwarfDebugInlineSection = 0; 49 DwarfStrSection = 0; 50 DwarfLocSection = 0; 51 DwarfARangesSection = 0; 52 DwarfRangesSection = 0; 53 DwarfMacroInfoSection = 0; 54 } 55 56 TargetLoweringObjectFile::~TargetLoweringObjectFile() { 57 } 58 59 static bool isSuitableForBSS(const GlobalVariable *GV) { 60 Constant *C = GV->getInitializer(); 61 62 // Must have zero initializer. 63 if (!C->isNullValue()) 64 return false; 65 66 // Leave constant zeros in readonly constant sections, so they can be shared. 67 if (GV->isConstant()) 68 return false; 69 70 // If the global has an explicit section specified, don't put it in BSS. 71 if (!GV->getSection().empty()) 72 return false; 73 74 // If -nozero-initialized-in-bss is specified, don't ever use BSS. 75 if (NoZerosInBSS) 76 return false; 77 78 // Otherwise, put it in BSS! 79 return true; 80 } 81 82 /// IsNullTerminatedString - Return true if the specified constant (which is 83 /// known to have a type that is an array of 1/2/4 byte elements) ends with a 84 /// nul value and contains no other nuls in it. 85 static bool IsNullTerminatedString(const Constant *C) { 86 const ArrayType *ATy = cast<ArrayType>(C->getType()); 87 88 // First check: is we have constant array of i8 terminated with zero 89 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) { 90 if (ATy->getNumElements() == 0) return false; 91 92 ConstantInt *Null = 93 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1)); 94 if (Null == 0 || Null->getZExtValue() != 0) 95 return false; // Not null terminated. 96 97 // Verify that the null doesn't occur anywhere else in the string. 98 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i) 99 // Reject constantexpr elements etc. 100 if (!isa<ConstantInt>(CVA->getOperand(i)) || 101 CVA->getOperand(i) == Null) 102 return false; 103 return true; 104 } 105 106 // Another possibility: [1 x i8] zeroinitializer 107 if (isa<ConstantAggregateZero>(C)) 108 return ATy->getNumElements() == 1; 109 110 return false; 111 } 112 113 /// getKindForGlobal - This is a top-level target-independent classifier for 114 /// a global variable. Given an global variable and information from TM, it 115 /// classifies the global in a variety of ways that make various target 116 /// implementations simpler. The target implementation is free to ignore this 117 /// extra info of course. 118 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, 119 const TargetMachine &TM){ 120 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && 121 "Can only be used for global definitions"); 122 123 Reloc::Model ReloModel = TM.getRelocationModel(); 124 125 // Early exit - functions should be always in text sections. 126 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 127 if (GVar == 0) 128 return SectionKind::getText(); 129 130 131 // Handle thread-local data first. 132 if (GVar->isThreadLocal()) { 133 if (isSuitableForBSS(GVar)) 134 return SectionKind::getThreadBSS(); 135 return SectionKind::getThreadData(); 136 } 137 138 // Variable can be easily put to BSS section. 139 if (isSuitableForBSS(GVar)) 140 return SectionKind::getBSS(); 141 142 Constant *C = GVar->getInitializer(); 143 144 // If the global is marked constant, we can put it into a mergable section, 145 // a mergable string section, or general .data if it contains relocations. 146 if (GVar->isConstant()) { 147 // If the initializer for the global contains something that requires a 148 // relocation, then we may have to drop this into a wriable data section 149 // even though it is marked const. 150 switch (C->getRelocationInfo()) { 151 default: llvm_unreachable("unknown relocation info kind"); 152 case Constant::NoRelocation: 153 // If initializer is a null-terminated string, put it in a "cstring" 154 // section of the right width. 155 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 156 if (const IntegerType *ITy = 157 dyn_cast<IntegerType>(ATy->getElementType())) { 158 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 159 ITy->getBitWidth() == 32) && 160 IsNullTerminatedString(C)) { 161 if (ITy->getBitWidth() == 8) 162 return SectionKind::getMergeable1ByteCString(); 163 if (ITy->getBitWidth() == 16) 164 return SectionKind::getMergeable2ByteCString(); 165 166 assert(ITy->getBitWidth() == 32 && "Unknown width"); 167 return SectionKind::getMergeable4ByteCString(); 168 } 169 } 170 } 171 172 // Otherwise, just drop it into a mergable constant section. If we have 173 // a section for this size, use it, otherwise use the arbitrary sized 174 // mergable section. 175 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { 176 case 4: return SectionKind::getMergeableConst4(); 177 case 8: return SectionKind::getMergeableConst8(); 178 case 16: return SectionKind::getMergeableConst16(); 179 default: return SectionKind::getMergeableConst(); 180 } 181 182 case Constant::LocalRelocation: 183 // In static relocation model, the linker will resolve all addresses, so 184 // the relocation entries will actually be constants by the time the app 185 // starts up. However, we can't put this into a mergable section, because 186 // the linker doesn't take relocations into consideration when it tries to 187 // merge entries in the section. 188 if (ReloModel == Reloc::Static) 189 return SectionKind::getReadOnly(); 190 191 // Otherwise, the dynamic linker needs to fix it up, put it in the 192 // writable data.rel.local section. 193 return SectionKind::getReadOnlyWithRelLocal(); 194 195 case Constant::GlobalRelocations: 196 // In static relocation model, the linker will resolve all addresses, so 197 // the relocation entries will actually be constants by the time the app 198 // starts up. However, we can't put this into a mergable section, because 199 // the linker doesn't take relocations into consideration when it tries to 200 // merge entries in the section. 201 if (ReloModel == Reloc::Static) 202 return SectionKind::getReadOnly(); 203 204 // Otherwise, the dynamic linker needs to fix it up, put it in the 205 // writable data.rel section. 206 return SectionKind::getReadOnlyWithRel(); 207 } 208 } 209 210 // Okay, this isn't a constant. If the initializer for the global is going 211 // to require a runtime relocation by the dynamic linker, put it into a more 212 // specific section to improve startup time of the app. This coalesces these 213 // globals together onto fewer pages, improving the locality of the dynamic 214 // linker. 215 if (ReloModel == Reloc::Static) 216 return SectionKind::getDataNoRel(); 217 218 switch (C->getRelocationInfo()) { 219 default: llvm_unreachable("unknown relocation info kind"); 220 case Constant::NoRelocation: 221 return SectionKind::getDataNoRel(); 222 case Constant::LocalRelocation: 223 return SectionKind::getDataRelLocal(); 224 case Constant::GlobalRelocations: 225 return SectionKind::getDataRel(); 226 } 227 } 228 229 /// SectionForGlobal - This method computes the appropriate section to emit 230 /// the specified global variable or function definition. This should not 231 /// be passed external (or available externally) globals. 232 const MCSection *TargetLoweringObjectFile:: 233 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, 234 const TargetMachine &TM) const { 235 // Select section name. 236 if (GV->hasSection()) { 237 // If the target has special section hacks for specifically named globals, 238 // return them now. 239 if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind)) 240 return TS; 241 242 // If the target has magic semantics for certain section names, make sure to 243 // pick up the flags. This allows the user to write things with attribute 244 // section and still get the appropriate section flags printed. 245 Kind = getKindForNamedSection(GV->getSection().c_str(), Kind); 246 247 return getOrCreateSection(GV->getSection().c_str(), false, Kind); 248 } 249 250 251 // Use default section depending on the 'type' of global 252 return SelectSectionForGlobal(GV, Kind, Mang, TM); 253 } 254 255 256 // Lame default implementation. Calculate the section name for global. 257 const MCSection * 258 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, 259 SectionKind Kind, 260 Mangler *Mang, 261 const TargetMachine &TM) const{ 262 assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 263 264 if (Kind.isText()) 265 return getTextSection(); 266 267 if (Kind.isBSS() && BSSSection != 0) 268 return BSSSection; 269 270 if (Kind.isReadOnly() && ReadOnlySection != 0) 271 return ReadOnlySection; 272 273 return getDataSection(); 274 } 275 276 /// getSectionForConstant - Given a mergable constant with the 277 /// specified size and relocation information, return a section that it 278 /// should be placed in. 279 const MCSection * 280 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { 281 if (Kind.isReadOnly() && ReadOnlySection != 0) 282 return ReadOnlySection; 283 284 return DataSection; 285 } 286 287 288 const MCSection *TargetLoweringObjectFile:: 289 getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const { 290 if (MCSection *S = Ctx->GetSection(Name)) 291 return S; 292 return MCSection::Create(Name, isDirective, Kind, *Ctx); 293 } 294 295 296 297 //===----------------------------------------------------------------------===// 298 // ELF 299 //===----------------------------------------------------------------------===// 300 301 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 302 const TargetMachine &TM) { 303 TargetLoweringObjectFile::Initialize(Ctx, TM); 304 if (!HasCrazyBSS) 305 BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS()); 306 else 307 // PPC/Linux doesn't support the .bss directive, it needs .section .bss. 308 // FIXME: Does .section .bss work everywhere?? 309 // FIXME2: this should just be handle by the section printer. We should get 310 // away from syntactic view of the sections and MCSection should just be a 311 // semantic view. 312 BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS()); 313 314 315 TextSection = getOrCreateSection("\t.text", true, SectionKind::getText()); 316 DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel()); 317 ReadOnlySection = 318 getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly()); 319 TLSDataSection = 320 getOrCreateSection("\t.tdata", false, SectionKind::getThreadData()); 321 322 TLSBSSSection = getOrCreateSection("\t.tbss", false, 323 SectionKind::getThreadBSS()); 324 325 DataRelSection = getOrCreateSection("\t.data.rel", false, 326 SectionKind::getDataRel()); 327 DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false, 328 SectionKind::getDataRelLocal()); 329 DataRelROSection = getOrCreateSection("\t.data.rel.ro", false, 330 SectionKind::getReadOnlyWithRel()); 331 DataRelROLocalSection = 332 getOrCreateSection("\t.data.rel.ro.local", false, 333 SectionKind::getReadOnlyWithRelLocal()); 334 335 MergeableConst4Section = getOrCreateSection(".rodata.cst4", false, 336 SectionKind::getMergeableConst4()); 337 MergeableConst8Section = getOrCreateSection(".rodata.cst8", false, 338 SectionKind::getMergeableConst8()); 339 MergeableConst16Section = getOrCreateSection(".rodata.cst16", false, 340 SectionKind::getMergeableConst16()); 341 342 StaticCtorSection = 343 getOrCreateSection(".ctors", false, SectionKind::getDataRel()); 344 StaticDtorSection = 345 getOrCreateSection(".dtors", false, SectionKind::getDataRel()); 346 347 // Exception Handling Sections. 348 349 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 350 // it contains relocatable pointers. In PIC mode, this is probably a big 351 // runtime hit for C++ apps. Either the contents of the LSDA need to be 352 // adjusted or this should be a data section. 353 LSDASection = 354 getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly()); 355 EHFrameSection = 356 getOrCreateSection(".eh_frame", false, SectionKind::getDataRel()); 357 358 // Debug Info Sections. 359 DwarfAbbrevSection = 360 getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata()); 361 DwarfInfoSection = 362 getOrCreateSection(".debug_info", false, SectionKind::getMetadata()); 363 DwarfLineSection = 364 getOrCreateSection(".debug_line", false, SectionKind::getMetadata()); 365 DwarfFrameSection = 366 getOrCreateSection(".debug_frame", false, SectionKind::getMetadata()); 367 DwarfPubNamesSection = 368 getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata()); 369 DwarfPubTypesSection = 370 getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata()); 371 DwarfStrSection = 372 getOrCreateSection(".debug_str", false, SectionKind::getMetadata()); 373 DwarfLocSection = 374 getOrCreateSection(".debug_loc", false, SectionKind::getMetadata()); 375 DwarfARangesSection = 376 getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata()); 377 DwarfRangesSection = 378 getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata()); 379 DwarfMacroInfoSection = 380 getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata()); 381 } 382 383 384 SectionKind TargetLoweringObjectFileELF:: 385 getKindForNamedSection(const char *Name, SectionKind K) const { 386 if (Name[0] != '.') return K; 387 388 // Some lame default implementation based on some magic section names. 389 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || 390 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || 391 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || 392 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) 393 return SectionKind::getBSS(); 394 395 if (strcmp(Name, ".tdata") == 0 || 396 strncmp(Name, ".tdata.", 7) == 0 || 397 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || 398 strncmp(Name, ".llvm.linkonce.td.", 18) == 0) 399 return SectionKind::getThreadData(); 400 401 if (strcmp(Name, ".tbss") == 0 || 402 strncmp(Name, ".tbss.", 6) == 0 || 403 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || 404 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) 405 return SectionKind::getThreadBSS(); 406 407 return K; 408 } 409 410 void TargetLoweringObjectFileELF:: 411 getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { 412 Str.push_back(','); 413 Str.push_back('"'); 414 415 if (!Kind.isMetadata()) 416 Str.push_back('a'); 417 if (Kind.isText()) 418 Str.push_back('x'); 419 if (Kind.isWriteable()) 420 Str.push_back('w'); 421 if (Kind.isMergeable1ByteCString() || 422 Kind.isMergeable2ByteCString() || 423 Kind.isMergeable4ByteCString() || 424 Kind.isMergeableConst4() || 425 Kind.isMergeableConst8() || 426 Kind.isMergeableConst16()) 427 Str.push_back('M'); 428 if (Kind.isMergeable1ByteCString() || 429 Kind.isMergeable2ByteCString() || 430 Kind.isMergeable4ByteCString()) 431 Str.push_back('S'); 432 if (Kind.isThreadLocal()) 433 Str.push_back('T'); 434 435 Str.push_back('"'); 436 Str.push_back(','); 437 438 // If comment string is '@', e.g. as on ARM - use '%' instead 439 if (AtIsCommentChar) 440 Str.push_back('%'); 441 else 442 Str.push_back('@'); 443 444 const char *KindStr; 445 if (Kind.isBSS() || Kind.isThreadBSS()) 446 KindStr = "nobits"; 447 else 448 KindStr = "progbits"; 449 450 Str.append(KindStr, KindStr+strlen(KindStr)); 451 452 if (Kind.isMergeable1ByteCString()) { 453 Str.push_back(','); 454 Str.push_back('1'); 455 } else if (Kind.isMergeable2ByteCString()) { 456 Str.push_back(','); 457 Str.push_back('2'); 458 } else if (Kind.isMergeable4ByteCString()) { 459 Str.push_back(','); 460 Str.push_back('4'); 461 } else if (Kind.isMergeableConst4()) { 462 Str.push_back(','); 463 Str.push_back('4'); 464 } else if (Kind.isMergeableConst8()) { 465 Str.push_back(','); 466 Str.push_back('8'); 467 } else if (Kind.isMergeableConst16()) { 468 Str.push_back(','); 469 Str.push_back('1'); 470 Str.push_back('6'); 471 } 472 } 473 474 475 static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { 476 if (Kind.isText()) return ".gnu.linkonce.t."; 477 if (Kind.isReadOnly()) return ".gnu.linkonce.r."; 478 479 if (Kind.isThreadData()) return ".gnu.linkonce.td."; 480 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; 481 482 if (Kind.isBSS()) return ".gnu.linkonce.b."; 483 if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; 484 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; 485 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; 486 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; 487 488 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 489 return ".gnu.linkonce.d.rel.ro."; 490 } 491 492 const MCSection *TargetLoweringObjectFileELF:: 493 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 494 Mangler *Mang, const TargetMachine &TM) const { 495 496 // If this global is linkonce/weak and the target handles this by emitting it 497 // into a 'uniqued' section name, create and return the section now. 498 if (GV->isWeakForLinker()) { 499 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); 500 std::string Name = Mang->makeNameProper(GV->getNameStr()); 501 return getOrCreateSection((Prefix+Name).c_str(), false, Kind); 502 } 503 504 if (Kind.isText()) return TextSection; 505 506 if (Kind.isMergeable1ByteCString() || 507 Kind.isMergeable2ByteCString() || 508 Kind.isMergeable4ByteCString()) { 509 510 // We also need alignment here. 511 // FIXME: this is getting the alignment of the character, not the 512 // alignment of the global! 513 unsigned Align = 514 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); 515 516 const char *SizeSpec = ".rodata.str1."; 517 if (Kind.isMergeable2ByteCString()) 518 SizeSpec = ".rodata.str2."; 519 else if (Kind.isMergeable4ByteCString()) 520 SizeSpec = ".rodata.str4."; 521 else 522 assert(Kind.isMergeable1ByteCString() && "unknown string width"); 523 524 525 std::string Name = SizeSpec + utostr(Align); 526 return getOrCreateSection(Name.c_str(), false, Kind); 527 } 528 529 if (Kind.isMergeableConst()) { 530 if (Kind.isMergeableConst4()) 531 return MergeableConst4Section; 532 if (Kind.isMergeableConst8()) 533 return MergeableConst8Section; 534 if (Kind.isMergeableConst16()) 535 return MergeableConst16Section; 536 return ReadOnlySection; // .const 537 } 538 539 if (Kind.isReadOnly()) return ReadOnlySection; 540 541 if (Kind.isThreadData()) return TLSDataSection; 542 if (Kind.isThreadBSS()) return TLSBSSSection; 543 544 if (Kind.isBSS()) return BSSSection; 545 546 if (Kind.isDataNoRel()) return DataSection; 547 if (Kind.isDataRelLocal()) return DataRelLocalSection; 548 if (Kind.isDataRel()) return DataRelSection; 549 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 550 551 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 552 return DataRelROSection; 553 } 554 555 /// getSectionForConstant - Given a mergeable constant with the 556 /// specified size and relocation information, return a section that it 557 /// should be placed in. 558 const MCSection *TargetLoweringObjectFileELF:: 559 getSectionForConstant(SectionKind Kind) const { 560 if (Kind.isMergeableConst4()) 561 return MergeableConst4Section; 562 if (Kind.isMergeableConst8()) 563 return MergeableConst8Section; 564 if (Kind.isMergeableConst16()) 565 return MergeableConst16Section; 566 if (Kind.isReadOnly()) 567 return ReadOnlySection; 568 569 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 570 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 571 return DataRelROSection; 572 } 573 574 //===----------------------------------------------------------------------===// 575 // MachO 576 //===----------------------------------------------------------------------===// 577 578 const MCSection *TargetLoweringObjectFileMachO:: 579 getMachOSection(const char *Name, bool isDirective, SectionKind K) { 580 // FOR NOW, Just forward. 581 return getOrCreateSection(Name, isDirective, K); 582 } 583 584 585 586 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 587 const TargetMachine &TM) { 588 TargetLoweringObjectFile::Initialize(Ctx, TM); 589 TextSection = getOrCreateSection("\t.text", true, 590 SectionKind::getText()); 591 DataSection = getOrCreateSection("\t.data", true, 592 SectionKind::getDataRel()); 593 594 CStringSection = getOrCreateSection("\t.cstring", true, 595 SectionKind::getMergeable1ByteCString()); 596 UStringSection = getOrCreateSection("__TEXT,__ustring", false, 597 SectionKind::getMergeable2ByteCString()); 598 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true, 599 SectionKind::getMergeableConst4()); 600 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true, 601 SectionKind::getMergeableConst8()); 602 603 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back 604 // to using it in -static mode. 605 if (TM.getRelocationModel() != Reloc::Static && 606 TM.getTargetData()->getPointerSize() == 32) 607 SixteenByteConstantSection = 608 getOrCreateSection("\t.literal16\n", true, 609 SectionKind::getMergeableConst16()); 610 else 611 SixteenByteConstantSection = 0; 612 613 ReadOnlySection = getOrCreateSection("\t.const", true, 614 SectionKind::getReadOnly()); 615 616 TextCoalSection = 617 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", 618 false, SectionKind::getText()); 619 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced", 620 false, 621 SectionKind::getText()); 622 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced", 623 false, 624 SectionKind::getText()); 625 ConstDataSection = getOrCreateSection("\t.const_data", true, 626 SectionKind::getReadOnlyWithRel()); 627 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced", 628 false, 629 SectionKind::getDataRel()); 630 631 if (TM.getRelocationModel() == Reloc::Static) { 632 StaticCtorSection = 633 getOrCreateSection(".constructor", true, SectionKind::getDataRel()); 634 StaticDtorSection = 635 getOrCreateSection(".destructor", true, SectionKind::getDataRel()); 636 } else { 637 StaticCtorSection = 638 getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel()); 639 StaticDtorSection = 640 getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel()); 641 } 642 643 // Exception Handling. 644 LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false, 645 SectionKind::getDataRel()); 646 EHFrameSection = 647 getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms" 648 "+live_support", false, SectionKind::getReadOnly()); 649 650 // Debug Information. 651 // FIXME: Don't use 'directive' syntax: need flags for debug/regular?? 652 // FIXME: Need __DWARF segment. 653 DwarfAbbrevSection = 654 getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true, 655 SectionKind::getMetadata()); 656 DwarfInfoSection = 657 getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true, 658 SectionKind::getMetadata()); 659 DwarfLineSection = 660 getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true, 661 SectionKind::getMetadata()); 662 DwarfFrameSection = 663 getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true, 664 SectionKind::getMetadata()); 665 DwarfPubNamesSection = 666 getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true, 667 SectionKind::getMetadata()); 668 DwarfPubTypesSection = 669 getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true, 670 SectionKind::getMetadata()); 671 DwarfStrSection = 672 getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true, 673 SectionKind::getMetadata()); 674 DwarfLocSection = 675 getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true, 676 SectionKind::getMetadata()); 677 DwarfARangesSection = 678 getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true, 679 SectionKind::getMetadata()); 680 DwarfRangesSection = 681 getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true, 682 SectionKind::getMetadata()); 683 DwarfMacroInfoSection = 684 getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true, 685 SectionKind::getMetadata()); 686 DwarfDebugInlineSection = 687 getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true, 688 SectionKind::getMetadata()); 689 } 690 691 const MCSection *TargetLoweringObjectFileMachO:: 692 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 693 Mangler *Mang, const TargetMachine &TM) const { 694 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); 695 696 if (Kind.isText()) 697 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 698 699 // If this is weak/linkonce, put this in a coalescable section, either in text 700 // or data depending on if it is writable. 701 if (GV->isWeakForLinker()) { 702 if (Kind.isReadOnly()) 703 return ConstTextCoalSection; 704 return DataCoalSection; 705 } 706 707 // FIXME: Alignment check should be handled by section classifier. 708 if (Kind.isMergeable1ByteCString() || 709 Kind.isMergeable2ByteCString()) { 710 if (TM.getTargetData()->getPreferredAlignment( 711 cast<GlobalVariable>(GV)) < 32) { 712 if (Kind.isMergeable1ByteCString()) 713 return CStringSection; 714 assert(Kind.isMergeable2ByteCString()); 715 return UStringSection; 716 } 717 } 718 719 if (Kind.isMergeableConst()) { 720 if (Kind.isMergeableConst4()) 721 return FourByteConstantSection; 722 if (Kind.isMergeableConst8()) 723 return EightByteConstantSection; 724 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 725 return SixteenByteConstantSection; 726 } 727 728 // Otherwise, if it is readonly, but not something we can specially optimize, 729 // just drop it in .const. 730 if (Kind.isReadOnly()) 731 return ReadOnlySection; 732 733 // If this is marked const, put it into a const section. But if the dynamic 734 // linker needs to write to it, put it in the data segment. 735 if (Kind.isReadOnlyWithRel()) 736 return ConstDataSection; 737 738 // Otherwise, just drop the variable in the normal data section. 739 return DataSection; 740 } 741 742 const MCSection * 743 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { 744 // If this constant requires a relocation, we have to put it in the data 745 // segment, not in the text segment. 746 if (Kind.isDataRel()) 747 return ConstDataSection; 748 749 if (Kind.isMergeableConst4()) 750 return FourByteConstantSection; 751 if (Kind.isMergeableConst8()) 752 return EightByteConstantSection; 753 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 754 return SixteenByteConstantSection; 755 return ReadOnlySection; // .const 756 } 757 758 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide 759 /// not to emit the UsedDirective for some symbols in llvm.used. 760 // FIXME: REMOVE this (rdar://7071300) 761 bool TargetLoweringObjectFileMachO:: 762 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { 763 /// On Darwin, internally linked data beginning with "L" or "l" does not have 764 /// the directive emitted (this occurs in ObjC metadata). 765 if (!GV) return false; 766 767 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. 768 if (GV->hasLocalLinkage() && !isa<Function>(GV)) { 769 // FIXME: ObjC metadata is currently emitted as internal symbols that have 770 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and 771 // this horrible hack can go away. 772 const std::string &Name = Mang->getMangledName(GV); 773 if (Name[0] == 'L' || Name[0] == 'l') 774 return false; 775 } 776 777 return true; 778 } 779 780 781 //===----------------------------------------------------------------------===// 782 // COFF 783 //===----------------------------------------------------------------------===// 784 785 const MCSection *TargetLoweringObjectFileCOFF:: 786 getCOFFSection(const char *Name, bool isDirective, SectionKind K) { 787 return getOrCreateSection(Name, isDirective, K); 788 } 789 790 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 791 const TargetMachine &TM) { 792 TargetLoweringObjectFile::Initialize(Ctx, TM); 793 TextSection = getOrCreateSection("\t.text", true, 794 SectionKind::getText()); 795 DataSection = getOrCreateSection("\t.data", true, 796 SectionKind::getDataRel()); 797 StaticCtorSection = 798 getOrCreateSection(".ctors", false, SectionKind::getDataRel()); 799 StaticDtorSection = 800 getOrCreateSection(".dtors", false, SectionKind::getDataRel()); 801 802 803 // Debug info. 804 // FIXME: Don't use 'directive' mode here. 805 DwarfAbbrevSection = 806 getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"", 807 true, SectionKind::getMetadata()); 808 DwarfInfoSection = 809 getOrCreateSection("\t.section\t.debug_info,\"dr\"", 810 true, SectionKind::getMetadata()); 811 DwarfLineSection = 812 getOrCreateSection("\t.section\t.debug_line,\"dr\"", 813 true, SectionKind::getMetadata()); 814 DwarfFrameSection = 815 getOrCreateSection("\t.section\t.debug_frame,\"dr\"", 816 true, SectionKind::getMetadata()); 817 DwarfPubNamesSection = 818 getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"", 819 true, SectionKind::getMetadata()); 820 DwarfPubTypesSection = 821 getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"", 822 true, SectionKind::getMetadata()); 823 DwarfStrSection = 824 getOrCreateSection("\t.section\t.debug_str,\"dr\"", 825 true, SectionKind::getMetadata()); 826 DwarfLocSection = 827 getOrCreateSection("\t.section\t.debug_loc,\"dr\"", 828 true, SectionKind::getMetadata()); 829 DwarfARangesSection = 830 getOrCreateSection("\t.section\t.debug_aranges,\"dr\"", 831 true, SectionKind::getMetadata()); 832 DwarfRangesSection = 833 getOrCreateSection("\t.section\t.debug_ranges,\"dr\"", 834 true, SectionKind::getMetadata()); 835 DwarfMacroInfoSection = 836 getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"", 837 true, SectionKind::getMetadata()); 838 } 839 840 void TargetLoweringObjectFileCOFF:: 841 getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { 842 // FIXME: Inefficient. 843 std::string Res = ",\""; 844 if (Kind.isText()) 845 Res += 'x'; 846 if (Kind.isWriteable()) 847 Res += 'w'; 848 Res += "\""; 849 850 Str.append(Res.begin(), Res.end()); 851 } 852 853 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { 854 if (Kind.isText()) 855 return ".text$linkonce"; 856 if (Kind.isWriteable()) 857 return ".data$linkonce"; 858 return ".rdata$linkonce"; 859 } 860 861 862 const MCSection *TargetLoweringObjectFileCOFF:: 863 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 864 Mangler *Mang, const TargetMachine &TM) const { 865 assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 866 867 // If this global is linkonce/weak and the target handles this by emitting it 868 // into a 'uniqued' section name, create and return the section now. 869 if (GV->isWeakForLinker()) { 870 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); 871 std::string Name = Mang->makeNameProper(GV->getNameStr()); 872 return getOrCreateSection((Prefix+Name).c_str(), false, Kind); 873 } 874 875 if (Kind.isText()) 876 return getTextSection(); 877 878 return getDataSection(); 879 } 880 881