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