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