1 //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.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/CodeGen/TargetLoweringObjectFileImpl.h" 16 #include "llvm/Constants.h" 17 #include "llvm/DerivedTypes.h" 18 #include "llvm/Function.h" 19 #include "llvm/GlobalVariable.h" 20 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCSectionMachO.h" 24 #include "llvm/MC/MCSectionELF.h" 25 #include "llvm/MC/MCSectionCOFF.h" 26 #include "llvm/MC/MCSymbol.h" 27 #include "llvm/Target/Mangler.h" 28 #include "llvm/Target/TargetData.h" 29 #include "llvm/Target/TargetMachine.h" 30 #include "llvm/Target/TargetOptions.h" 31 #include "llvm/Support/Dwarf.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/ADT/SmallString.h" 35 #include "llvm/ADT/StringExtras.h" 36 using namespace llvm; 37 using namespace dwarf; 38 39 //===----------------------------------------------------------------------===// 40 // ELF 41 //===----------------------------------------------------------------------===// 42 43 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 44 const TargetMachine &TM) { 45 TargetLoweringObjectFile::Initialize(Ctx, TM); 46 47 BSSSection = 48 getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS, 49 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, 50 SectionKind::getBSS()); 51 52 TextSection = 53 getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS, 54 MCSectionELF::SHF_EXECINSTR | 55 MCSectionELF::SHF_ALLOC, 56 SectionKind::getText()); 57 58 DataSection = 59 getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS, 60 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, 61 SectionKind::getDataRel()); 62 63 ReadOnlySection = 64 getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, 65 MCSectionELF::SHF_ALLOC, 66 SectionKind::getReadOnly()); 67 68 TLSDataSection = 69 getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, 70 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | 71 MCSectionELF::SHF_WRITE, 72 SectionKind::getThreadData()); 73 74 TLSBSSSection = 75 getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS, 76 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | 77 MCSectionELF::SHF_WRITE, 78 SectionKind::getThreadBSS()); 79 80 DataRelSection = 81 getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, 82 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 83 SectionKind::getDataRel()); 84 85 DataRelLocalSection = 86 getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, 87 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 88 SectionKind::getDataRelLocal()); 89 90 DataRelROSection = 91 getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, 92 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 93 SectionKind::getReadOnlyWithRel()); 94 95 DataRelROLocalSection = 96 getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, 97 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 98 SectionKind::getReadOnlyWithRelLocal()); 99 100 MergeableConst4Section = 101 getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, 102 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, 103 SectionKind::getMergeableConst4()); 104 105 MergeableConst8Section = 106 getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, 107 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, 108 SectionKind::getMergeableConst8()); 109 110 MergeableConst16Section = 111 getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, 112 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, 113 SectionKind::getMergeableConst16()); 114 115 StaticCtorSection = 116 getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, 117 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 118 SectionKind::getDataRel()); 119 120 StaticDtorSection = 121 getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, 122 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 123 SectionKind::getDataRel()); 124 125 // Exception Handling Sections. 126 127 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 128 // it contains relocatable pointers. In PIC mode, this is probably a big 129 // runtime hit for C++ apps. Either the contents of the LSDA need to be 130 // adjusted or this should be a data section. 131 LSDASection = 132 getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, 133 MCSectionELF::SHF_ALLOC, 134 SectionKind::getReadOnly()); 135 EHFrameSection = 136 getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, 137 MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, 138 SectionKind::getDataRel()); 139 140 // Debug Info Sections. 141 DwarfAbbrevSection = 142 getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, 143 SectionKind::getMetadata()); 144 DwarfInfoSection = 145 getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, 146 SectionKind::getMetadata()); 147 DwarfLineSection = 148 getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, 149 SectionKind::getMetadata()); 150 DwarfFrameSection = 151 getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, 152 SectionKind::getMetadata()); 153 DwarfPubNamesSection = 154 getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, 155 SectionKind::getMetadata()); 156 DwarfPubTypesSection = 157 getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, 158 SectionKind::getMetadata()); 159 DwarfStrSection = 160 getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, 161 SectionKind::getMetadata()); 162 DwarfLocSection = 163 getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, 164 SectionKind::getMetadata()); 165 DwarfARangesSection = 166 getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, 167 SectionKind::getMetadata()); 168 DwarfRangesSection = 169 getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, 170 SectionKind::getMetadata()); 171 DwarfMacroInfoSection = 172 getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, 173 SectionKind::getMetadata()); 174 } 175 176 177 static SectionKind 178 getELFKindForNamedSection(StringRef Name, SectionKind K) { 179 if (Name.empty() || Name[0] != '.') return K; 180 181 // Some lame default implementation based on some magic section names. 182 if (Name == ".bss" || 183 Name.startswith(".bss.") || 184 Name.startswith(".gnu.linkonce.b.") || 185 Name.startswith(".llvm.linkonce.b.") || 186 Name == ".sbss" || 187 Name.startswith(".sbss.") || 188 Name.startswith(".gnu.linkonce.sb.") || 189 Name.startswith(".llvm.linkonce.sb.")) 190 return SectionKind::getBSS(); 191 192 if (Name == ".tdata" || 193 Name.startswith(".tdata.") || 194 Name.startswith(".gnu.linkonce.td.") || 195 Name.startswith(".llvm.linkonce.td.")) 196 return SectionKind::getThreadData(); 197 198 if (Name == ".tbss" || 199 Name.startswith(".tbss.") || 200 Name.startswith(".gnu.linkonce.tb.") || 201 Name.startswith(".llvm.linkonce.tb.")) 202 return SectionKind::getThreadBSS(); 203 204 return K; 205 } 206 207 208 static unsigned getELFSectionType(StringRef Name, SectionKind K) { 209 210 if (Name == ".init_array") 211 return MCSectionELF::SHT_INIT_ARRAY; 212 213 if (Name == ".fini_array") 214 return MCSectionELF::SHT_FINI_ARRAY; 215 216 if (Name == ".preinit_array") 217 return MCSectionELF::SHT_PREINIT_ARRAY; 218 219 if (K.isBSS() || K.isThreadBSS()) 220 return MCSectionELF::SHT_NOBITS; 221 222 return MCSectionELF::SHT_PROGBITS; 223 } 224 225 226 static unsigned 227 getELFSectionFlags(SectionKind K) { 228 unsigned Flags = 0; 229 230 if (!K.isMetadata()) 231 Flags |= MCSectionELF::SHF_ALLOC; 232 233 if (K.isText()) 234 Flags |= MCSectionELF::SHF_EXECINSTR; 235 236 if (K.isWriteable()) 237 Flags |= MCSectionELF::SHF_WRITE; 238 239 if (K.isThreadLocal()) 240 Flags |= MCSectionELF::SHF_TLS; 241 242 // K.isMergeableConst() is left out to honour PR4650 243 if (K.isMergeableCString() || K.isMergeableConst4() || 244 K.isMergeableConst8() || K.isMergeableConst16()) 245 Flags |= MCSectionELF::SHF_MERGE; 246 247 if (K.isMergeableCString()) 248 Flags |= MCSectionELF::SHF_STRINGS; 249 250 return Flags; 251 } 252 253 254 const MCSection *TargetLoweringObjectFileELF:: 255 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 256 Mangler *Mang, const TargetMachine &TM) const { 257 StringRef SectionName = GV->getSection(); 258 259 // Infer section flags from the section name if we can. 260 Kind = getELFKindForNamedSection(SectionName, Kind); 261 262 return getContext().getELFSection(SectionName, 263 getELFSectionType(SectionName, Kind), 264 getELFSectionFlags(Kind), Kind, true); 265 } 266 267 static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { 268 if (Kind.isText()) return ".gnu.linkonce.t."; 269 if (Kind.isReadOnly()) return ".gnu.linkonce.r."; 270 271 if (Kind.isThreadData()) return ".gnu.linkonce.td."; 272 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; 273 274 if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; 275 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; 276 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; 277 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; 278 279 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 280 return ".gnu.linkonce.d.rel.ro."; 281 } 282 283 /// getSectionPrefixForGlobal - Return the section prefix name used by options 284 /// FunctionsSections and DataSections. 285 static const char *getSectionPrefixForGlobal(SectionKind Kind) { 286 if (Kind.isText()) return ".text."; 287 if (Kind.isReadOnly()) return ".rodata."; 288 289 if (Kind.isThreadData()) return ".tdata."; 290 if (Kind.isThreadBSS()) return ".tbss."; 291 292 if (Kind.isDataNoRel()) return ".data."; 293 if (Kind.isDataRelLocal()) return ".data.rel.local."; 294 if (Kind.isDataRel()) return ".data.rel."; 295 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; 296 297 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 298 return ".data.rel.ro."; 299 } 300 301 302 const MCSection *TargetLoweringObjectFileELF:: 303 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 304 Mangler *Mang, const TargetMachine &TM) const { 305 // If we have -ffunction-section or -fdata-section then we should emit the 306 // global value to a uniqued section specifically for it. 307 bool EmitUniquedSection; 308 if (Kind.isText()) 309 EmitUniquedSection = TM.getFunctionSections(); 310 else 311 EmitUniquedSection = TM.getDataSections(); 312 313 // If this global is linkonce/weak and the target handles this by emitting it 314 // into a 'uniqued' section name, create and return the section now. 315 if ((GV->isWeakForLinker() || EmitUniquedSection) && 316 !Kind.isCommon() && !Kind.isBSS()) { 317 const char *Prefix; 318 if (GV->isWeakForLinker()) 319 Prefix = getSectionPrefixForUniqueGlobal(Kind); 320 else { 321 assert(EmitUniquedSection); 322 Prefix = getSectionPrefixForGlobal(Kind); 323 } 324 325 SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 326 MCSymbol *Sym = Mang->getSymbol(GV); 327 Name.append(Sym->getName().begin(), Sym->getName().end()); 328 return getContext().getELFSection(Name.str(), 329 getELFSectionType(Name.str(), Kind), 330 getELFSectionFlags(Kind), Kind); 331 } 332 333 if (Kind.isText()) return TextSection; 334 335 if (Kind.isMergeable1ByteCString() || 336 Kind.isMergeable2ByteCString() || 337 Kind.isMergeable4ByteCString()) { 338 339 // We also need alignment here. 340 // FIXME: this is getting the alignment of the character, not the 341 // alignment of the global! 342 unsigned Align = 343 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); 344 345 const char *SizeSpec = ".rodata.str1."; 346 if (Kind.isMergeable2ByteCString()) 347 SizeSpec = ".rodata.str2."; 348 else if (Kind.isMergeable4ByteCString()) 349 SizeSpec = ".rodata.str4."; 350 else 351 assert(Kind.isMergeable1ByteCString() && "unknown string width"); 352 353 354 std::string Name = SizeSpec + utostr(Align); 355 return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS, 356 MCSectionELF::SHF_ALLOC | 357 MCSectionELF::SHF_MERGE | 358 MCSectionELF::SHF_STRINGS, 359 Kind); 360 } 361 362 if (Kind.isMergeableConst()) { 363 if (Kind.isMergeableConst4() && MergeableConst4Section) 364 return MergeableConst4Section; 365 if (Kind.isMergeableConst8() && MergeableConst8Section) 366 return MergeableConst8Section; 367 if (Kind.isMergeableConst16() && MergeableConst16Section) 368 return MergeableConst16Section; 369 return ReadOnlySection; // .const 370 } 371 372 if (Kind.isReadOnly()) return ReadOnlySection; 373 374 if (Kind.isThreadData()) return TLSDataSection; 375 if (Kind.isThreadBSS()) return TLSBSSSection; 376 377 // Note: we claim that common symbols are put in BSSSection, but they are 378 // really emitted with the magic .comm directive, which creates a symbol table 379 // entry but not a section. 380 if (Kind.isBSS() || Kind.isCommon()) return BSSSection; 381 382 if (Kind.isDataNoRel()) return DataSection; 383 if (Kind.isDataRelLocal()) return DataRelLocalSection; 384 if (Kind.isDataRel()) return DataRelSection; 385 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 386 387 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 388 return DataRelROSection; 389 } 390 391 /// getSectionForConstant - Given a mergeable constant with the 392 /// specified size and relocation information, return a section that it 393 /// should be placed in. 394 const MCSection *TargetLoweringObjectFileELF:: 395 getSectionForConstant(SectionKind Kind) const { 396 if (Kind.isMergeableConst4() && MergeableConst4Section) 397 return MergeableConst4Section; 398 if (Kind.isMergeableConst8() && MergeableConst8Section) 399 return MergeableConst8Section; 400 if (Kind.isMergeableConst16() && MergeableConst16Section) 401 return MergeableConst16Section; 402 if (Kind.isReadOnly()) 403 return ReadOnlySection; 404 405 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 406 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 407 return DataRelROSection; 408 } 409 410 const MCExpr *TargetLoweringObjectFileELF:: 411 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 412 MachineModuleInfo *MMI, 413 unsigned Encoding, MCStreamer &Streamer) const { 414 415 if (Encoding & dwarf::DW_EH_PE_indirect) { 416 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 417 418 SmallString<128> Name; 419 Mang->getNameWithPrefix(Name, GV, true); 420 Name += ".DW.stub"; 421 422 // Add information about the stub reference to ELFMMI so that the stub 423 // gets emitted by the asmprinter. 424 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); 425 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 426 if (StubSym.getPointer() == 0) { 427 MCSymbol *Sym = Mang->getSymbol(GV); 428 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 429 } 430 431 return TargetLoweringObjectFile:: 432 getExprForDwarfReference(SSym, Mang, MMI, 433 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 434 } 435 436 return TargetLoweringObjectFile:: 437 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); 438 } 439 440 //===----------------------------------------------------------------------===// 441 // MachO 442 //===----------------------------------------------------------------------===// 443 444 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 445 const TargetMachine &TM) { 446 // _foo.eh symbols are currently always exported so that the linker knows 447 // about them. This is not necessary on 10.6 and later, but it 448 // doesn't hurt anything. 449 // FIXME: I need to get this from Triple. 450 IsFunctionEHSymbolGlobal = true; 451 IsFunctionEHFrameSymbolPrivate = false; 452 SupportsWeakOmittedEHFrame = false; 453 454 TargetLoweringObjectFile::Initialize(Ctx, TM); 455 456 TextSection // .text 457 = getContext().getMachOSection("__TEXT", "__text", 458 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 459 SectionKind::getText()); 460 DataSection // .data 461 = getContext().getMachOSection("__DATA", "__data", 0, 462 SectionKind::getDataRel()); 463 464 TLSDataSection // .tdata 465 = getContext().getMachOSection("__DATA", "__thread_data", 466 MCSectionMachO::S_THREAD_LOCAL_REGULAR, 467 SectionKind::getDataRel()); 468 TLSBSSSection // .tbss 469 = getContext().getMachOSection("__DATA", "__thread_bss", 470 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, 471 SectionKind::getThreadBSS()); 472 473 // TODO: Verify datarel below. 474 TLSTLVSection // .tlv 475 = getContext().getMachOSection("__DATA", "__thread_vars", 476 MCSectionMachO::S_THREAD_LOCAL_VARIABLES, 477 SectionKind::getDataRel()); 478 CStringSection // .cstring 479 = getContext().getMachOSection("__TEXT", "__cstring", 480 MCSectionMachO::S_CSTRING_LITERALS, 481 SectionKind::getMergeable1ByteCString()); 482 UStringSection 483 = getContext().getMachOSection("__TEXT","__ustring", 0, 484 SectionKind::getMergeable2ByteCString()); 485 FourByteConstantSection // .literal4 486 = getContext().getMachOSection("__TEXT", "__literal4", 487 MCSectionMachO::S_4BYTE_LITERALS, 488 SectionKind::getMergeableConst4()); 489 EightByteConstantSection // .literal8 490 = getContext().getMachOSection("__TEXT", "__literal8", 491 MCSectionMachO::S_8BYTE_LITERALS, 492 SectionKind::getMergeableConst8()); 493 494 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back 495 // to using it in -static mode. 496 SixteenByteConstantSection = 0; 497 if (TM.getRelocationModel() != Reloc::Static && 498 TM.getTargetData()->getPointerSize() == 32) 499 SixteenByteConstantSection = // .literal16 500 getContext().getMachOSection("__TEXT", "__literal16", 501 MCSectionMachO::S_16BYTE_LITERALS, 502 SectionKind::getMergeableConst16()); 503 504 ReadOnlySection // .const 505 = getContext().getMachOSection("__TEXT", "__const", 0, 506 SectionKind::getReadOnly()); 507 508 TextCoalSection 509 = getContext().getMachOSection("__TEXT", "__textcoal_nt", 510 MCSectionMachO::S_COALESCED | 511 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 512 SectionKind::getText()); 513 ConstTextCoalSection 514 = getContext().getMachOSection("__TEXT", "__const_coal", 515 MCSectionMachO::S_COALESCED, 516 SectionKind::getText()); 517 ConstDataCoalSection 518 = getContext().getMachOSection("__DATA","__const_coal", 519 MCSectionMachO::S_COALESCED, 520 SectionKind::getText()); 521 ConstDataSection // .const_data 522 = getContext().getMachOSection("__DATA", "__const", 0, 523 SectionKind::getReadOnlyWithRel()); 524 DataCoalSection 525 = getContext().getMachOSection("__DATA","__datacoal_nt", 526 MCSectionMachO::S_COALESCED, 527 SectionKind::getDataRel()); 528 DataCommonSection 529 = getContext().getMachOSection("__DATA","__common", 530 MCSectionMachO::S_ZEROFILL, 531 SectionKind::getBSS()); 532 DataBSSSection 533 = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, 534 SectionKind::getBSS()); 535 536 537 LazySymbolPointerSection 538 = getContext().getMachOSection("__DATA", "__la_symbol_ptr", 539 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 540 SectionKind::getMetadata()); 541 NonLazySymbolPointerSection 542 = getContext().getMachOSection("__DATA", "__nl_symbol_ptr", 543 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 544 SectionKind::getMetadata()); 545 546 if (TM.getRelocationModel() == Reloc::Static) { 547 StaticCtorSection 548 = getContext().getMachOSection("__TEXT", "__constructor", 0, 549 SectionKind::getDataRel()); 550 StaticDtorSection 551 = getContext().getMachOSection("__TEXT", "__destructor", 0, 552 SectionKind::getDataRel()); 553 } else { 554 StaticCtorSection 555 = getContext().getMachOSection("__DATA", "__mod_init_func", 556 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 557 SectionKind::getDataRel()); 558 StaticDtorSection 559 = getContext().getMachOSection("__DATA", "__mod_term_func", 560 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 561 SectionKind::getDataRel()); 562 } 563 564 // Exception Handling. 565 LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0, 566 SectionKind::getReadOnlyWithRel()); 567 EHFrameSection = 568 getContext().getMachOSection("__TEXT", "__eh_frame", 569 MCSectionMachO::S_COALESCED | 570 MCSectionMachO::S_ATTR_NO_TOC | 571 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | 572 MCSectionMachO::S_ATTR_LIVE_SUPPORT, 573 SectionKind::getReadOnly()); 574 575 // Debug Information. 576 DwarfAbbrevSection = 577 getContext().getMachOSection("__DWARF", "__debug_abbrev", 578 MCSectionMachO::S_ATTR_DEBUG, 579 SectionKind::getMetadata()); 580 DwarfInfoSection = 581 getContext().getMachOSection("__DWARF", "__debug_info", 582 MCSectionMachO::S_ATTR_DEBUG, 583 SectionKind::getMetadata()); 584 DwarfLineSection = 585 getContext().getMachOSection("__DWARF", "__debug_line", 586 MCSectionMachO::S_ATTR_DEBUG, 587 SectionKind::getMetadata()); 588 DwarfFrameSection = 589 getContext().getMachOSection("__DWARF", "__debug_frame", 590 MCSectionMachO::S_ATTR_DEBUG, 591 SectionKind::getMetadata()); 592 DwarfPubNamesSection = 593 getContext().getMachOSection("__DWARF", "__debug_pubnames", 594 MCSectionMachO::S_ATTR_DEBUG, 595 SectionKind::getMetadata()); 596 DwarfPubTypesSection = 597 getContext().getMachOSection("__DWARF", "__debug_pubtypes", 598 MCSectionMachO::S_ATTR_DEBUG, 599 SectionKind::getMetadata()); 600 DwarfStrSection = 601 getContext().getMachOSection("__DWARF", "__debug_str", 602 MCSectionMachO::S_ATTR_DEBUG, 603 SectionKind::getMetadata()); 604 DwarfLocSection = 605 getContext().getMachOSection("__DWARF", "__debug_loc", 606 MCSectionMachO::S_ATTR_DEBUG, 607 SectionKind::getMetadata()); 608 DwarfARangesSection = 609 getContext().getMachOSection("__DWARF", "__debug_aranges", 610 MCSectionMachO::S_ATTR_DEBUG, 611 SectionKind::getMetadata()); 612 DwarfRangesSection = 613 getContext().getMachOSection("__DWARF", "__debug_ranges", 614 MCSectionMachO::S_ATTR_DEBUG, 615 SectionKind::getMetadata()); 616 DwarfMacroInfoSection = 617 getContext().getMachOSection("__DWARF", "__debug_macinfo", 618 MCSectionMachO::S_ATTR_DEBUG, 619 SectionKind::getMetadata()); 620 DwarfDebugInlineSection = 621 getContext().getMachOSection("__DWARF", "__debug_inlined", 622 MCSectionMachO::S_ATTR_DEBUG, 623 SectionKind::getMetadata()); 624 } 625 626 const MCSection *TargetLoweringObjectFileMachO:: 627 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 628 Mangler *Mang, const TargetMachine &TM) const { 629 // Parse the section specifier and create it if valid. 630 StringRef Segment, Section; 631 unsigned TAA, StubSize; 632 std::string ErrorCode = 633 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 634 TAA, StubSize); 635 if (!ErrorCode.empty()) { 636 // If invalid, report the error with report_fatal_error. 637 report_fatal_error("Global variable '" + GV->getNameStr() + 638 "' has an invalid section specifier '" + GV->getSection()+ 639 "': " + ErrorCode + "."); 640 // Fall back to dropping it into the data section. 641 return DataSection; 642 } 643 644 // Get the section. 645 const MCSectionMachO *S = 646 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 647 648 // Okay, now that we got the section, verify that the TAA & StubSize agree. 649 // If the user declared multiple globals with different section flags, we need 650 // to reject it here. 651 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 652 // If invalid, report the error with report_fatal_error. 653 report_fatal_error("Global variable '" + GV->getNameStr() + 654 "' section type or attributes does not match previous" 655 " section specifier"); 656 } 657 658 return S; 659 } 660 661 const MCSection *TargetLoweringObjectFileMachO:: 662 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 663 Mangler *Mang, const TargetMachine &TM) const { 664 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); 665 666 if (Kind.isText()) 667 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 668 669 // If this is weak/linkonce, put this in a coalescable section, either in text 670 // or data depending on if it is writable. 671 if (GV->isWeakForLinker()) { 672 if (Kind.isReadOnly()) 673 return ConstTextCoalSection; 674 return DataCoalSection; 675 } 676 677 // FIXME: Alignment check should be handled by section classifier. 678 if (Kind.isMergeable1ByteCString() && 679 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 680 return CStringSection; 681 682 // Do not put 16-bit arrays in the UString section if they have an 683 // externally visible label, this runs into issues with certain linker 684 // versions. 685 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 686 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 687 return UStringSection; 688 689 if (Kind.isMergeableConst()) { 690 if (Kind.isMergeableConst4()) 691 return FourByteConstantSection; 692 if (Kind.isMergeableConst8()) 693 return EightByteConstantSection; 694 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 695 return SixteenByteConstantSection; 696 } 697 698 // Otherwise, if it is readonly, but not something we can specially optimize, 699 // just drop it in .const. 700 if (Kind.isReadOnly()) 701 return ReadOnlySection; 702 703 // If this is marked const, put it into a const section. But if the dynamic 704 // linker needs to write to it, put it in the data segment. 705 if (Kind.isReadOnlyWithRel()) 706 return ConstDataSection; 707 708 // Put zero initialized globals with strong external linkage in the 709 // DATA, __common section with the .zerofill directive. 710 if (Kind.isBSSExtern()) 711 return DataCommonSection; 712 713 // Put zero initialized globals with local linkage in __DATA,__bss directive 714 // with the .zerofill directive (aka .lcomm). 715 if (Kind.isBSSLocal()) 716 return DataBSSSection; 717 718 // Otherwise, just drop the variable in the normal data section. 719 return DataSection; 720 } 721 722 const MCSection * 723 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { 724 // If this constant requires a relocation, we have to put it in the data 725 // segment, not in the text segment. 726 if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) 727 return ConstDataSection; 728 729 if (Kind.isMergeableConst4()) 730 return FourByteConstantSection; 731 if (Kind.isMergeableConst8()) 732 return EightByteConstantSection; 733 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 734 return SixteenByteConstantSection; 735 return ReadOnlySection; // .const 736 } 737 738 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide 739 /// not to emit the UsedDirective for some symbols in llvm.used. 740 // FIXME: REMOVE this (rdar://7071300) 741 bool TargetLoweringObjectFileMachO:: 742 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { 743 /// On Darwin, internally linked data beginning with "L" or "l" does not have 744 /// the directive emitted (this occurs in ObjC metadata). 745 if (!GV) return false; 746 747 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. 748 if (GV->hasLocalLinkage() && !isa<Function>(GV)) { 749 // FIXME: ObjC metadata is currently emitted as internal symbols that have 750 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and 751 // this horrible hack can go away. 752 MCSymbol *Sym = Mang->getSymbol(GV); 753 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l') 754 return false; 755 } 756 757 return true; 758 } 759 760 const MCExpr *TargetLoweringObjectFileMachO:: 761 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 762 MachineModuleInfo *MMI, unsigned Encoding, 763 MCStreamer &Streamer) const { 764 // The mach-o version of this method defaults to returning a stub reference. 765 766 if (Encoding & DW_EH_PE_indirect) { 767 MachineModuleInfoMachO &MachOMMI = 768 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 769 770 SmallString<128> Name; 771 Mang->getNameWithPrefix(Name, GV, true); 772 Name += "$non_lazy_ptr"; 773 774 // Add information about the stub reference to MachOMMI so that the stub 775 // gets emitted by the asmprinter. 776 MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); 777 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 778 if (StubSym.getPointer() == 0) { 779 MCSymbol *Sym = Mang->getSymbol(GV); 780 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 781 } 782 783 return TargetLoweringObjectFile:: 784 getExprForDwarfReference(SSym, Mang, MMI, 785 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 786 } 787 788 return TargetLoweringObjectFile:: 789 getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); 790 } 791 792 unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const { 793 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; 794 } 795 796 unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const { 797 return DW_EH_PE_pcrel; 798 } 799 800 unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const { 801 return DW_EH_PE_pcrel; 802 } 803 804 unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const { 805 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; 806 } 807 808 //===----------------------------------------------------------------------===// 809 // COFF 810 //===----------------------------------------------------------------------===// 811 812 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 813 const TargetMachine &TM) { 814 TargetLoweringObjectFile::Initialize(Ctx, TM); 815 TextSection = 816 getContext().getCOFFSection(".text", 817 MCSectionCOFF::IMAGE_SCN_CNT_CODE | 818 MCSectionCOFF::IMAGE_SCN_MEM_EXECUTE | 819 MCSectionCOFF::IMAGE_SCN_MEM_READ, 820 SectionKind::getText()); 821 DataSection = 822 getContext().getCOFFSection(".data", 823 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 824 MCSectionCOFF::IMAGE_SCN_MEM_READ | 825 MCSectionCOFF::IMAGE_SCN_MEM_WRITE, 826 SectionKind::getDataRel()); 827 ReadOnlySection = 828 getContext().getCOFFSection(".rdata", 829 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 830 MCSectionCOFF::IMAGE_SCN_MEM_READ, 831 SectionKind::getReadOnly()); 832 StaticCtorSection = 833 getContext().getCOFFSection(".ctors", 834 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 835 MCSectionCOFF::IMAGE_SCN_MEM_READ | 836 MCSectionCOFF::IMAGE_SCN_MEM_WRITE, 837 SectionKind::getDataRel()); 838 StaticDtorSection = 839 getContext().getCOFFSection(".dtors", 840 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 841 MCSectionCOFF::IMAGE_SCN_MEM_READ | 842 MCSectionCOFF::IMAGE_SCN_MEM_WRITE, 843 SectionKind::getDataRel()); 844 845 // FIXME: We're emitting LSDA info into a readonly section on COFF, even 846 // though it contains relocatable pointers. In PIC mode, this is probably a 847 // big runtime hit for C++ apps. Either the contents of the LSDA need to be 848 // adjusted or this should be a data section. 849 LSDASection = 850 getContext().getCOFFSection(".gcc_except_table", 851 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 852 MCSectionCOFF::IMAGE_SCN_MEM_READ, 853 SectionKind::getReadOnly()); 854 EHFrameSection = 855 getContext().getCOFFSection(".eh_frame", 856 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 857 MCSectionCOFF::IMAGE_SCN_MEM_READ | 858 MCSectionCOFF::IMAGE_SCN_MEM_WRITE, 859 SectionKind::getDataRel()); 860 861 // Debug info. 862 DwarfAbbrevSection = 863 getContext().getCOFFSection(".debug_abbrev", 864 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 865 MCSectionCOFF::IMAGE_SCN_MEM_READ, 866 SectionKind::getMetadata()); 867 DwarfInfoSection = 868 getContext().getCOFFSection(".debug_info", 869 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 870 MCSectionCOFF::IMAGE_SCN_MEM_READ, 871 SectionKind::getMetadata()); 872 DwarfLineSection = 873 getContext().getCOFFSection(".debug_line", 874 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 875 MCSectionCOFF::IMAGE_SCN_MEM_READ, 876 SectionKind::getMetadata()); 877 DwarfFrameSection = 878 getContext().getCOFFSection(".debug_frame", 879 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 880 MCSectionCOFF::IMAGE_SCN_MEM_READ, 881 SectionKind::getMetadata()); 882 DwarfPubNamesSection = 883 getContext().getCOFFSection(".debug_pubnames", 884 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 885 MCSectionCOFF::IMAGE_SCN_MEM_READ, 886 SectionKind::getMetadata()); 887 DwarfPubTypesSection = 888 getContext().getCOFFSection(".debug_pubtypes", 889 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 890 MCSectionCOFF::IMAGE_SCN_MEM_READ, 891 SectionKind::getMetadata()); 892 DwarfStrSection = 893 getContext().getCOFFSection(".debug_str", 894 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 895 MCSectionCOFF::IMAGE_SCN_MEM_READ, 896 SectionKind::getMetadata()); 897 DwarfLocSection = 898 getContext().getCOFFSection(".debug_loc", 899 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 900 MCSectionCOFF::IMAGE_SCN_MEM_READ, 901 SectionKind::getMetadata()); 902 DwarfARangesSection = 903 getContext().getCOFFSection(".debug_aranges", 904 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 905 MCSectionCOFF::IMAGE_SCN_MEM_READ, 906 SectionKind::getMetadata()); 907 DwarfRangesSection = 908 getContext().getCOFFSection(".debug_ranges", 909 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 910 MCSectionCOFF::IMAGE_SCN_MEM_READ, 911 SectionKind::getMetadata()); 912 DwarfMacroInfoSection = 913 getContext().getCOFFSection(".debug_macinfo", 914 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE | 915 MCSectionCOFF::IMAGE_SCN_MEM_READ, 916 SectionKind::getMetadata()); 917 918 DrectveSection = 919 getContext().getCOFFSection(".drectve", 920 MCSectionCOFF::IMAGE_SCN_LNK_INFO, 921 SectionKind::getMetadata()); 922 } 923 924 static unsigned 925 getCOFFSectionFlags(SectionKind K) { 926 unsigned Flags = 0; 927 928 if (!K.isMetadata()) 929 Flags |= 930 MCSectionCOFF::IMAGE_SCN_MEM_DISCARDABLE; 931 else if (K.isText()) 932 Flags |= 933 MCSectionCOFF::IMAGE_SCN_MEM_EXECUTE | 934 MCSectionCOFF::IMAGE_SCN_CNT_CODE; 935 else if (K.isBSS ()) 936 Flags |= 937 MCSectionCOFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 938 MCSectionCOFF::IMAGE_SCN_MEM_READ | 939 MCSectionCOFF::IMAGE_SCN_MEM_WRITE; 940 else if (K.isReadOnly()) 941 Flags |= 942 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 943 MCSectionCOFF::IMAGE_SCN_MEM_READ; 944 else if (K.isWriteable()) 945 Flags |= 946 MCSectionCOFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 947 MCSectionCOFF::IMAGE_SCN_MEM_READ | 948 MCSectionCOFF::IMAGE_SCN_MEM_WRITE; 949 950 return Flags; 951 } 952 953 const MCSection *TargetLoweringObjectFileCOFF:: 954 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 955 Mangler *Mang, const TargetMachine &TM) const { 956 return getContext().getCOFFSection(GV->getSection(), 957 getCOFFSectionFlags(Kind), 958 Kind); 959 } 960 961 static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { 962 if (Kind.isText()) 963 return ".text$linkonce"; 964 if (Kind.isBSS ()) 965 return ".bss$linkonce"; 966 if (Kind.isWriteable()) 967 return ".data$linkonce"; 968 return ".rdata$linkonce"; 969 } 970 971 972 const MCSection *TargetLoweringObjectFileCOFF:: 973 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 974 Mangler *Mang, const TargetMachine &TM) const { 975 assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 976 977 // If this global is linkonce/weak and the target handles this by emitting it 978 // into a 'uniqued' section name, create and return the section now. 979 if (GV->isWeakForLinker()) { 980 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); 981 SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 982 MCSymbol *Sym = Mang->getSymbol(GV); 983 Name.append(Sym->getName().begin(), Sym->getName().end()); 984 985 unsigned Characteristics = getCOFFSectionFlags(Kind); 986 987 Characteristics |= MCSectionCOFF::IMAGE_SCN_LNK_COMDAT; 988 989 return getContext().getCOFFSection(Name.str(), Characteristics, 990 MCSectionCOFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind); 991 } 992 993 if (Kind.isText()) 994 return getTextSection(); 995 996 return getDataSection(); 997 } 998 999