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