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