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