TargetLoweringObjectFileImpl.cpp revision 204961
135124Sjb//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===// 235124Sjb// 335124Sjb// The LLVM Compiler Infrastructure 435124Sjb// 535124Sjb// This file is distributed under the University of Illinois Open Source 635124Sjb// License. See LICENSE.TXT for details. 735124Sjb// 835124Sjb//===----------------------------------------------------------------------===// 935124Sjb// 1035124Sjb// This file implements classes used to handle lowerings specific to common 1135124Sjb// object file formats. 1235124Sjb// 13165968Simp//===----------------------------------------------------------------------===// 1435124Sjb 1535124Sjb#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 1635124Sjb#include "llvm/Constants.h" 1735124Sjb#include "llvm/DerivedTypes.h" 1835124Sjb#include "llvm/Function.h" 1935124Sjb#include "llvm/GlobalVariable.h" 2035124Sjb#include "llvm/CodeGen/MachineModuleInfoImpls.h" 2135124Sjb#include "llvm/MC/MCContext.h" 2235124Sjb#include "llvm/MC/MCExpr.h" 2335124Sjb#include "llvm/MC/MCSectionMachO.h" 2435124Sjb#include "llvm/MC/MCSectionELF.h" 2535124Sjb#include "llvm/MC/MCSymbol.h" 2635124Sjb#include "llvm/Target/Mangler.h" 2735124Sjb#include "llvm/Target/TargetData.h" 2835124Sjb#include "llvm/Target/TargetMachine.h" 2950476Speter#include "llvm/Target/TargetOptions.h" 3035124Sjb#include "llvm/Support/Dwarf.h" 3135124Sjb#include "llvm/Support/ErrorHandling.h" 3235124Sjb#include "llvm/Support/raw_ostream.h" 3335124Sjb#include "llvm/ADT/SmallString.h" 3435124Sjb#include "llvm/ADT/StringExtras.h" 3535124Sjbusing namespace llvm; 3635124Sjbusing namespace dwarf; 37199606Sjhb 3835124Sjb//===----------------------------------------------------------------------===// 3935124Sjb// ELF 4035124Sjb//===----------------------------------------------------------------------===// 4135124Sjbtypedef StringMap<const MCSectionELF*> ELFUniqueMapTy; 4235124Sjb 4335124SjbTargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() { 4435124Sjb // If we have the section uniquing map, free it. 4535124Sjb delete (ELFUniqueMapTy*)UniquingMap; 4635124Sjb} 47232388Skib 48232388Skibconst MCSection *TargetLoweringObjectFileELF:: 49232388SkibgetELFSection(StringRef Section, unsigned Type, unsigned Flags, 50232388Skib SectionKind Kind, bool IsExplicit) const { 51232388Skib if (UniquingMap == 0) 52232388Skib UniquingMap = new ELFUniqueMapTy(); 53232388Skib ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap; 54232388Skib 55232388Skib // Do the lookup, if we have a hit, return it. 56228843Scperciva const MCSectionELF *&Entry = Map[Section]; 57228843Scperciva if (Entry) return Entry; 58228843Scperciva 59228843Scperciva return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit, 60228843Scperciva getContext()); 61228843Scperciva} 62228843Scperciva 63228843Scpercivavoid TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 64228843Scperciva const TargetMachine &TM) { 65228843Scperciva if (UniquingMap != 0) 66228843Scperciva ((ELFUniqueMapTy*)UniquingMap)->clear(); 6735124Sjb TargetLoweringObjectFile::Initialize(Ctx, TM); 6835124Sjb 6935124Sjb BSSSection = 7035124Sjb getELFSection(".bss", MCSectionELF::SHT_NOBITS, 7135124Sjb MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, 7235124Sjb SectionKind::getBSS()); 7335124Sjb 7471579Sdeischen TextSection = 7535124Sjb getELFSection(".text", MCSectionELF::SHT_PROGBITS, 7635124Sjb MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC, 7735124Sjb SectionKind::getText()); 7835124Sjb 7935124Sjb DataSection = 8035124Sjb getELFSection(".data", MCSectionELF::SHT_PROGBITS, 8135124Sjb MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, 8271579Sdeischen SectionKind::getDataRel()); 8335124Sjb 84234871Skib ReadOnlySection = 85234871Skib getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, 86234871Skib MCSectionELF::SHF_ALLOC, 87234871Skib SectionKind::getReadOnly()); 88234871Skib 89234871Skib TLSDataSection = 90234871Skib getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, 91234871Skib MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | 92234871Skib MCSectionELF::SHF_WRITE, SectionKind::getThreadData()); 93234871Skib 94234871Skib TLSBSSSection = 95234871Skib getELFSection(".tbss", MCSectionELF::SHT_NOBITS, 96234871Skib MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | 9793399Smarkm MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS()); 98106866Sdeischen 99106866Sdeischen DataRelSection = 100106866Sdeischen getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, 101106866Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 102106866Sdeischen SectionKind::getDataRel()); 103106866Sdeischen 104156319Sdeischen DataRelLocalSection = 105156319Sdeischen getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, 106156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 107156319Sdeischen SectionKind::getDataRelLocal()); 108156319Sdeischen 109156319Sdeischen DataRelROSection = 110156319Sdeischen getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, 111156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 112156319Sdeischen SectionKind::getReadOnlyWithRel()); 113156319Sdeischen 114156319Sdeischen DataRelROLocalSection = 115156319Sdeischen getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, 116156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 117156319Sdeischen SectionKind::getReadOnlyWithRelLocal()); 118156319Sdeischen 119156319Sdeischen MergeableConst4Section = 120156319Sdeischen getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, 121156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, 122156319Sdeischen SectionKind::getMergeableConst4()); 123156319Sdeischen 124156319Sdeischen MergeableConst8Section = 125156319Sdeischen getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, 126106866Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, 127106866Sdeischen SectionKind::getMergeableConst8()); 128106866Sdeischen 129106866Sdeischen MergeableConst16Section = 130156319Sdeischen getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, 131106866Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, 132156319Sdeischen SectionKind::getMergeableConst16()); 133156319Sdeischen 134156319Sdeischen StaticCtorSection = 135106866Sdeischen getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, 136156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 137106866Sdeischen SectionKind::getDataRel()); 138106866Sdeischen 139156319Sdeischen StaticDtorSection = 140106866Sdeischen getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, 141156319Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 142156319Sdeischen SectionKind::getDataRel()); 143156319Sdeischen 144106866Sdeischen // Exception Handling Sections. 145106866Sdeischen 146106866Sdeischen // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 147106866Sdeischen // it contains relocatable pointers. In PIC mode, this is probably a big 148106866Sdeischen // runtime hit for C++ apps. Either the contents of the LSDA need to be 149106866Sdeischen // adjusted or this should be a data section. 150106866Sdeischen LSDASection = 151106866Sdeischen getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, 152106866Sdeischen MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly()); 153106866Sdeischen EHFrameSection = 154106866Sdeischen getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, 155106866Sdeischen MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, 156106866Sdeischen SectionKind::getDataRel()); 157106866Sdeischen 158156319Sdeischen // Debug Info Sections. 159156319Sdeischen DwarfAbbrevSection = 160106866Sdeischen getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, 161106866Sdeischen SectionKind::getMetadata()); 162156319Sdeischen DwarfInfoSection = 163201546Sdavidxu getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, 164201546Sdavidxu SectionKind::getMetadata()); 165213153Sdavidxu DwarfLineSection = 166213153Sdavidxu getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, 167106866Sdeischen SectionKind::getMetadata()); 168106866Sdeischen DwarfFrameSection = 169106866Sdeischen getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, 170106870Sdeischen SectionKind::getMetadata()); 171106880Sdeischen DwarfPubNamesSection = 172106866Sdeischen getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, 173106880Sdeischen SectionKind::getMetadata()); 174106866Sdeischen DwarfPubTypesSection = 175106866Sdeischen getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, 176111618Snectar SectionKind::getMetadata()); 177111618Snectar DwarfStrSection = 178111618Snectar getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, 179111618Snectar SectionKind::getMetadata()); 180111618Snectar DwarfLocSection = 181111618Snectar getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, 182133754Sdfr SectionKind::getMetadata()); 183133754Sdfr DwarfARangesSection = 184133754Sdfr getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, 185133754Sdfr SectionKind::getMetadata()); 186111618Snectar DwarfRangesSection = 187111618Snectar getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, 188199606Sjhb SectionKind::getMetadata()); 189199606Sjhb DwarfMacroInfoSection = 190199606Sjhb getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, 191199606Sjhb SectionKind::getMetadata()); 192199606Sjhb} 193199606Sjhb 194133754Sdfr 195133754Sdfrstatic SectionKind 196133754SdfrgetELFKindForNamedSection(StringRef Name, SectionKind K) { 197133754Sdfr if (Name.empty() || Name[0] != '.') return K; 198133754Sdfr 19993399Smarkm // Some lame default implementation based on some magic section names. 20093399Smarkm if (Name == ".bss" || 20193399Smarkm Name.startswith(".bss.") || 20293399Smarkm Name.startswith(".gnu.linkonce.b.") || 20393399Smarkm Name.startswith(".llvm.linkonce.b.") || 204122069Sdeischen Name == ".sbss" || 205182225Sjasone Name.startswith(".sbss.") || 206182225Sjasone Name.startswith(".gnu.linkonce.sb.") || 207182225Sjasone Name.startswith(".llvm.linkonce.sb.")) 208182225Sjasone return SectionKind::getBSS(); 209182225Sjasone 210182225Sjasone if (Name == ".tdata" || 211154248Sjasone Name.startswith(".tdata.") || 212154248Sjasone Name.startswith(".gnu.linkonce.td.") || 213122069Sdeischen Name.startswith(".llvm.linkonce.td.")) 214154248Sjasone return SectionKind::getThreadData(); 215154248Sjasone 216122069Sdeischen if (Name == ".tbss" || 217150040Sstefanf Name.startswith(".tbss.") || 218150040Sstefanf Name.startswith(".gnu.linkonce.tb.") || 219150040Sstefanf Name.startswith(".llvm.linkonce.tb.")) 220150040Sstefanf return SectionKind::getThreadBSS(); 221150040Sstefanf 222171219Speter return K; 223171219Speter} 224171219Speter 225171219Speter 226171219Speterstatic unsigned getELFSectionType(StringRef Name, SectionKind K) { 227171219Speter 228171219Speter if (Name == ".init_array") 229171219Speter return MCSectionELF::SHT_INIT_ARRAY; 230171219Speter 231171219Speter if (Name == ".fini_array") 232171219Speter return MCSectionELF::SHT_FINI_ARRAY; 233171219Speter 234171219Speter if (Name == ".preinit_array") 235171219Speter return MCSectionELF::SHT_PREINIT_ARRAY; 236171219Speter 237171219Speter if (K.isBSS() || K.isThreadBSS()) 238171219Speter return MCSectionELF::SHT_NOBITS; 239171219Speter 240171219Speter return MCSectionELF::SHT_PROGBITS; 241171219Speter} 242171219Speter 243171219Speter 244171219Speterstatic unsigned 245177911SdfrgetELFSectionFlags(SectionKind K) { 246177911Sdfr unsigned Flags = 0; 247177911Sdfr 248179947Sed if (!K.isMetadata()) 249179947Sed Flags |= MCSectionELF::SHF_ALLOC; 250179947Sed 251211416Skib if (K.isText()) 252211706Skib Flags |= MCSectionELF::SHF_EXECINSTR; 253211706Skib 254232388Skib if (K.isWriteable()) 255211416Skib Flags |= MCSectionELF::SHF_WRITE; 256213153Sdavidxu 257213153Sdavidxu if (K.isThreadLocal()) 258213153Sdavidxu Flags |= MCSectionELF::SHF_TLS; 25935124Sjb 260 // K.isMergeableConst() is left out to honour PR4650 261 if (K.isMergeableCString() || K.isMergeableConst4() || 262 K.isMergeableConst8() || K.isMergeableConst16()) 263 Flags |= MCSectionELF::SHF_MERGE; 264 265 if (K.isMergeableCString()) 266 Flags |= MCSectionELF::SHF_STRINGS; 267 268 return Flags; 269} 270 271 272const MCSection *TargetLoweringObjectFileELF:: 273getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 274 Mangler *Mang, const TargetMachine &TM) const { 275 StringRef SectionName = GV->getSection(); 276 277 // Infer section flags from the section name if we can. 278 Kind = getELFKindForNamedSection(SectionName, Kind); 279 280 return getELFSection(SectionName, 281 getELFSectionType(SectionName, Kind), 282 getELFSectionFlags(Kind), Kind, true); 283} 284 285static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { 286 if (Kind.isText()) return ".gnu.linkonce.t."; 287 if (Kind.isReadOnly()) return ".gnu.linkonce.r."; 288 289 if (Kind.isThreadData()) return ".gnu.linkonce.td."; 290 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; 291 292 if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; 293 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; 294 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; 295 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; 296 297 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 298 return ".gnu.linkonce.d.rel.ro."; 299} 300 301const MCSection *TargetLoweringObjectFileELF:: 302SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 303 Mangler *Mang, const TargetMachine &TM) const { 304 305 // If this global is linkonce/weak and the target handles this by emitting it 306 // into a 'uniqued' section name, create and return the section now. 307 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) { 308 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); 309 SmallString<128> Name; 310 Name.append(Prefix, Prefix+strlen(Prefix)); 311 Mang->getNameWithPrefix(Name, GV, false); 312 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind), 313 getELFSectionFlags(Kind), Kind); 314 } 315 316 if (Kind.isText()) return TextSection; 317 318 if (Kind.isMergeable1ByteCString() || 319 Kind.isMergeable2ByteCString() || 320 Kind.isMergeable4ByteCString()) { 321 322 // We also need alignment here. 323 // FIXME: this is getting the alignment of the character, not the 324 // alignment of the global! 325 unsigned Align = 326 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); 327 328 const char *SizeSpec = ".rodata.str1."; 329 if (Kind.isMergeable2ByteCString()) 330 SizeSpec = ".rodata.str2."; 331 else if (Kind.isMergeable4ByteCString()) 332 SizeSpec = ".rodata.str4."; 333 else 334 assert(Kind.isMergeable1ByteCString() && "unknown string width"); 335 336 337 std::string Name = SizeSpec + utostr(Align); 338 return getELFSection(Name, MCSectionELF::SHT_PROGBITS, 339 MCSectionELF::SHF_ALLOC | 340 MCSectionELF::SHF_MERGE | 341 MCSectionELF::SHF_STRINGS, 342 Kind); 343 } 344 345 if (Kind.isMergeableConst()) { 346 if (Kind.isMergeableConst4() && MergeableConst4Section) 347 return MergeableConst4Section; 348 if (Kind.isMergeableConst8() && MergeableConst8Section) 349 return MergeableConst8Section; 350 if (Kind.isMergeableConst16() && MergeableConst16Section) 351 return MergeableConst16Section; 352 return ReadOnlySection; // .const 353 } 354 355 if (Kind.isReadOnly()) return ReadOnlySection; 356 357 if (Kind.isThreadData()) return TLSDataSection; 358 if (Kind.isThreadBSS()) return TLSBSSSection; 359 360 // Note: we claim that common symbols are put in BSSSection, but they are 361 // really emitted with the magic .comm directive, which creates a symbol table 362 // entry but not a section. 363 if (Kind.isBSS() || Kind.isCommon()) return BSSSection; 364 365 if (Kind.isDataNoRel()) return DataSection; 366 if (Kind.isDataRelLocal()) return DataRelLocalSection; 367 if (Kind.isDataRel()) return DataRelSection; 368 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 369 370 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 371 return DataRelROSection; 372} 373 374/// getSectionForConstant - Given a mergeable constant with the 375/// specified size and relocation information, return a section that it 376/// should be placed in. 377const MCSection *TargetLoweringObjectFileELF:: 378getSectionForConstant(SectionKind Kind) const { 379 if (Kind.isMergeableConst4() && MergeableConst4Section) 380 return MergeableConst4Section; 381 if (Kind.isMergeableConst8() && MergeableConst8Section) 382 return MergeableConst8Section; 383 if (Kind.isMergeableConst16() && MergeableConst16Section) 384 return MergeableConst16Section; 385 if (Kind.isReadOnly()) 386 return ReadOnlySection; 387 388 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 389 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 390 return DataRelROSection; 391} 392 393const MCExpr *TargetLoweringObjectFileELF:: 394getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 395 MachineModuleInfo *MMI, unsigned Encoding) const { 396 397 if (Encoding & dwarf::DW_EH_PE_indirect) { 398 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 399 400 SmallString<128> Name; 401 Mang->getNameWithPrefix(Name, GV, true); 402 Name += ".DW.stub"; 403 404 // Add information about the stub reference to ELFMMI so that the stub 405 // gets emitted by the asmprinter. 406 MCSymbol *Sym = getContext().GetOrCreateTemporarySymbol(Name.str()); 407 MCSymbol *&StubSym = ELFMMI.getGVStubEntry(Sym); 408 if (StubSym == 0) { 409 Name.clear(); 410 Mang->getNameWithPrefix(Name, GV, false); 411 if (GV->hasPrivateLinkage()) 412 StubSym = getContext().GetOrCreateTemporarySymbol(Name.str()); 413 else 414 StubSym = getContext().GetOrCreateSymbol(Name.str()); 415 } 416 417 return TargetLoweringObjectFile:: 418 getSymbolForDwarfReference(Sym, MMI, 419 Encoding & ~dwarf::DW_EH_PE_indirect); 420 } 421 422 return TargetLoweringObjectFile:: 423 getSymbolForDwarfGlobalReference(GV, Mang, MMI, Encoding); 424} 425 426//===----------------------------------------------------------------------===// 427// MachO 428//===----------------------------------------------------------------------===// 429 430typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; 431 432TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() { 433 // If we have the MachO uniquing map, free it. 434 delete (MachOUniqueMapTy*)UniquingMap; 435} 436 437 438const MCSectionMachO *TargetLoweringObjectFileMachO:: 439getMachOSection(StringRef Segment, StringRef Section, 440 unsigned TypeAndAttributes, 441 unsigned Reserved2, SectionKind Kind) const { 442 // We unique sections by their segment/section pair. The returned section 443 // may not have the same flags as the requested section, if so this should be 444 // diagnosed by the client as an error. 445 446 // Create the map if it doesn't already exist. 447 if (UniquingMap == 0) 448 UniquingMap = new MachOUniqueMapTy(); 449 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap; 450 451 // Form the name to look up. 452 SmallString<64> Name; 453 Name += Segment; 454 Name.push_back(','); 455 Name += Section; 456 457 // Do the lookup, if we have a hit, return it. 458 const MCSectionMachO *&Entry = Map[Name.str()]; 459 if (Entry) return Entry; 460 461 // Otherwise, return a new section. 462 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, 463 Reserved2, Kind, getContext()); 464} 465 466 467void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 468 const TargetMachine &TM) { 469 // _foo.eh symbols are currently always exported so that the linker knows 470 // about them. This is not necessary on 10.6 and later, but it 471 // doesn't hurt anything. 472 // FIXME: I need to get this from Triple. 473 IsFunctionEHSymbolGlobal = true; 474 IsFunctionEHFrameSymbolPrivate = false; 475 SupportsWeakOmittedEHFrame = false; 476 477 if (UniquingMap != 0) 478 ((MachOUniqueMapTy*)UniquingMap)->clear(); 479 TargetLoweringObjectFile::Initialize(Ctx, TM); 480 481 TextSection // .text 482 = getMachOSection("__TEXT", "__text", 483 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 484 SectionKind::getText()); 485 DataSection // .data 486 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel()); 487 488 CStringSection // .cstring 489 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS, 490 SectionKind::getMergeable1ByteCString()); 491 UStringSection 492 = getMachOSection("__TEXT","__ustring", 0, 493 SectionKind::getMergeable2ByteCString()); 494 FourByteConstantSection // .literal4 495 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS, 496 SectionKind::getMergeableConst4()); 497 EightByteConstantSection // .literal8 498 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS, 499 SectionKind::getMergeableConst8()); 500 501 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back 502 // to using it in -static mode. 503 SixteenByteConstantSection = 0; 504 if (TM.getRelocationModel() != Reloc::Static && 505 TM.getTargetData()->getPointerSize() == 32) 506 SixteenByteConstantSection = // .literal16 507 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS, 508 SectionKind::getMergeableConst16()); 509 510 ReadOnlySection // .const 511 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); 512 513 TextCoalSection 514 = getMachOSection("__TEXT", "__textcoal_nt", 515 MCSectionMachO::S_COALESCED | 516 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 517 SectionKind::getText()); 518 ConstTextCoalSection 519 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED, 520 SectionKind::getText()); 521 ConstDataCoalSection 522 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED, 523 SectionKind::getText()); 524 ConstDataSection // .const_data 525 = getMachOSection("__DATA", "__const", 0, 526 SectionKind::getReadOnlyWithRel()); 527 DataCoalSection 528 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED, 529 SectionKind::getDataRel()); 530 DataCommonSection 531 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL, 532 SectionKind::getBSS()); 533 DataBSSSection 534 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, 535 SectionKind::getBSS()); 536 537 538 LazySymbolPointerSection 539 = getMachOSection("__DATA", "__la_symbol_ptr", 540 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 541 SectionKind::getMetadata()); 542 NonLazySymbolPointerSection 543 = getMachOSection("__DATA", "__nl_symbol_ptr", 544 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 545 SectionKind::getMetadata()); 546 547 if (TM.getRelocationModel() == Reloc::Static) { 548 StaticCtorSection 549 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel()); 550 StaticDtorSection 551 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel()); 552 } else { 553 StaticCtorSection 554 = getMachOSection("__DATA", "__mod_init_func", 555 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 556 SectionKind::getDataRel()); 557 StaticDtorSection 558 = getMachOSection("__DATA", "__mod_term_func", 559 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 560 SectionKind::getDataRel()); 561 } 562 563 // Exception Handling. 564 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0, 565 SectionKind::getDataRel()); 566 EHFrameSection = 567 getMachOSection("__TEXT", "__eh_frame", 568 MCSectionMachO::S_COALESCED | 569 MCSectionMachO::S_ATTR_NO_TOC | 570 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | 571 MCSectionMachO::S_ATTR_LIVE_SUPPORT, 572 SectionKind::getReadOnly()); 573 574 // Debug Information. 575 DwarfAbbrevSection = 576 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG, 577 SectionKind::getMetadata()); 578 DwarfInfoSection = 579 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG, 580 SectionKind::getMetadata()); 581 DwarfLineSection = 582 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG, 583 SectionKind::getMetadata()); 584 DwarfFrameSection = 585 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG, 586 SectionKind::getMetadata()); 587 DwarfPubNamesSection = 588 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG, 589 SectionKind::getMetadata()); 590 DwarfPubTypesSection = 591 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG, 592 SectionKind::getMetadata()); 593 DwarfStrSection = 594 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG, 595 SectionKind::getMetadata()); 596 DwarfLocSection = 597 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG, 598 SectionKind::getMetadata()); 599 DwarfARangesSection = 600 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG, 601 SectionKind::getMetadata()); 602 DwarfRangesSection = 603 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG, 604 SectionKind::getMetadata()); 605 DwarfMacroInfoSection = 606 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG, 607 SectionKind::getMetadata()); 608 DwarfDebugInlineSection = 609 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, 610 SectionKind::getMetadata()); 611} 612 613const MCSection *TargetLoweringObjectFileMachO:: 614getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 615 Mangler *Mang, const TargetMachine &TM) const { 616 // Parse the section specifier and create it if valid. 617 StringRef Segment, Section; 618 unsigned TAA, StubSize; 619 std::string ErrorCode = 620 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 621 TAA, StubSize); 622 if (!ErrorCode.empty()) { 623 // If invalid, report the error with llvm_report_error. 624 llvm_report_error("Global variable '" + GV->getNameStr() + 625 "' has an invalid section specifier '" + GV->getSection()+ 626 "': " + ErrorCode + "."); 627 // Fall back to dropping it into the data section. 628 return DataSection; 629 } 630 631 // Get the section. 632 const MCSectionMachO *S = 633 getMachOSection(Segment, Section, TAA, StubSize, Kind); 634 635 // Okay, now that we got the section, verify that the TAA & StubSize agree. 636 // If the user declared multiple globals with different section flags, we need 637 // to reject it here. 638 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 639 // If invalid, report the error with llvm_report_error. 640 llvm_report_error("Global variable '" + GV->getNameStr() + 641 "' section type or attributes does not match previous" 642 " section specifier"); 643 } 644 645 return S; 646} 647 648const MCSection *TargetLoweringObjectFileMachO:: 649SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 650 Mangler *Mang, const TargetMachine &TM) const { 651 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); 652 653 if (Kind.isText()) 654 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 655 656 // If this is weak/linkonce, put this in a coalescable section, either in text 657 // or data depending on if it is writable. 658 if (GV->isWeakForLinker()) { 659 if (Kind.isReadOnly()) 660 return ConstTextCoalSection; 661 return DataCoalSection; 662 } 663 664 // FIXME: Alignment check should be handled by section classifier. 665 if (Kind.isMergeable1ByteCString() && 666 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 667 return CStringSection; 668 669 // Do not put 16-bit arrays in the UString section if they have an 670 // externally visible label, this runs into issues with certain linker 671 // versions. 672 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 673 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 674 return UStringSection; 675 676 if (Kind.isMergeableConst()) { 677 if (Kind.isMergeableConst4()) 678 return FourByteConstantSection; 679 if (Kind.isMergeableConst8()) 680 return EightByteConstantSection; 681 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 682 return SixteenByteConstantSection; 683 } 684 685 // Otherwise, if it is readonly, but not something we can specially optimize, 686 // just drop it in .const. 687 if (Kind.isReadOnly()) 688 return ReadOnlySection; 689 690 // If this is marked const, put it into a const section. But if the dynamic 691 // linker needs to write to it, put it in the data segment. 692 if (Kind.isReadOnlyWithRel()) 693 return ConstDataSection; 694 695 // Put zero initialized globals with strong external linkage in the 696 // DATA, __common section with the .zerofill directive. 697 if (Kind.isBSSExtern()) 698 return DataCommonSection; 699 700 // Put zero initialized globals with local linkage in __DATA,__bss directive 701 // with the .zerofill directive (aka .lcomm). 702 if (Kind.isBSSLocal()) 703 return DataBSSSection; 704 705 // Otherwise, just drop the variable in the normal data section. 706 return DataSection; 707} 708 709const MCSection * 710TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { 711 // If this constant requires a relocation, we have to put it in the data 712 // segment, not in the text segment. 713 if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) 714 return ConstDataSection; 715 716 if (Kind.isMergeableConst4()) 717 return FourByteConstantSection; 718 if (Kind.isMergeableConst8()) 719 return EightByteConstantSection; 720 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 721 return SixteenByteConstantSection; 722 return ReadOnlySection; // .const 723} 724 725/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide 726/// not to emit the UsedDirective for some symbols in llvm.used. 727// FIXME: REMOVE this (rdar://7071300) 728bool TargetLoweringObjectFileMachO:: 729shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { 730 /// On Darwin, internally linked data beginning with "L" or "l" does not have 731 /// the directive emitted (this occurs in ObjC metadata). 732 if (!GV) return false; 733 734 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. 735 if (GV->hasLocalLinkage() && !isa<Function>(GV)) { 736 // FIXME: ObjC metadata is currently emitted as internal symbols that have 737 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and 738 // this horrible hack can go away. 739 SmallString<64> Name; 740 Mang->getNameWithPrefix(Name, GV, false); 741 if (Name[0] == 'L' || Name[0] == 'l') 742 return false; 743 } 744 745 return true; 746} 747 748const MCExpr *TargetLoweringObjectFileMachO:: 749getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 750 MachineModuleInfo *MMI, unsigned Encoding) const { 751 // The mach-o version of this method defaults to returning a stub reference. 752 753 if (Encoding & DW_EH_PE_indirect) { 754 MachineModuleInfoMachO &MachOMMI = 755 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 756 757 SmallString<128> Name; 758 Mang->getNameWithPrefix(Name, GV, true); 759 Name += "$non_lazy_ptr"; 760 761 // Add information about the stub reference to MachOMMI so that the stub 762 // gets emitted by the asmprinter. 763 MCSymbol *Sym = getContext().GetOrCreateTemporarySymbol(Name.str()); 764 MCSymbol *&StubSym = MachOMMI.getGVStubEntry(Sym); 765 if (StubSym == 0) { 766 Name.clear(); 767 Mang->getNameWithPrefix(Name, GV, false); 768 if (GV->hasPrivateLinkage()) 769 StubSym = getContext().GetOrCreateTemporarySymbol(Name.str()); 770 else 771 StubSym = getContext().GetOrCreateSymbol(Name.str()); 772 } 773 774 return TargetLoweringObjectFile:: 775 getSymbolForDwarfReference(Sym, MMI, 776 Encoding & ~dwarf::DW_EH_PE_indirect); 777 } 778 779 return TargetLoweringObjectFile:: 780 getSymbolForDwarfGlobalReference(GV, Mang, MMI, Encoding); 781} 782 783unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const { 784 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; 785} 786 787unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const { 788 return DW_EH_PE_pcrel; 789} 790 791unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const { 792 return DW_EH_PE_pcrel; 793} 794 795unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const { 796 return DW_EH_PE_absptr; 797} 798 799//===----------------------------------------------------------------------===// 800// COFF 801//===----------------------------------------------------------------------===// 802 803typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; 804 805TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() { 806 delete (COFFUniqueMapTy*)UniquingMap; 807} 808 809 810const MCSection *TargetLoweringObjectFileCOFF:: 811getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const { 812 // Create the map if it doesn't already exist. 813 if (UniquingMap == 0) 814 UniquingMap = new MachOUniqueMapTy(); 815 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap; 816 817 // Do the lookup, if we have a hit, return it. 818 const MCSectionCOFF *&Entry = Map[Name]; 819 if (Entry) return Entry; 820 821 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext()); 822} 823 824void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 825 const TargetMachine &TM) { 826 if (UniquingMap != 0) 827 ((COFFUniqueMapTy*)UniquingMap)->clear(); 828 TargetLoweringObjectFile::Initialize(Ctx, TM); 829 TextSection = getCOFFSection("\t.text", true, SectionKind::getText()); 830 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel()); 831 StaticCtorSection = 832 getCOFFSection(".ctors", false, SectionKind::getDataRel()); 833 StaticDtorSection = 834 getCOFFSection(".dtors", false, SectionKind::getDataRel()); 835 836 // FIXME: We're emitting LSDA info into a readonly section on COFF, even 837 // though it contains relocatable pointers. In PIC mode, this is probably a 838 // big runtime hit for C++ apps. Either the contents of the LSDA need to be 839 // adjusted or this should be a data section. 840 LSDASection = 841 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly()); 842 EHFrameSection = 843 getCOFFSection(".eh_frame", false, SectionKind::getDataRel()); 844 845 // Debug info. 846 // FIXME: Don't use 'directive' mode here. 847 DwarfAbbrevSection = 848 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"", 849 true, SectionKind::getMetadata()); 850 DwarfInfoSection = 851 getCOFFSection("\t.section\t.debug_info,\"dr\"", 852 true, SectionKind::getMetadata()); 853 DwarfLineSection = 854 getCOFFSection("\t.section\t.debug_line,\"dr\"", 855 true, SectionKind::getMetadata()); 856 DwarfFrameSection = 857 getCOFFSection("\t.section\t.debug_frame,\"dr\"", 858 true, SectionKind::getMetadata()); 859 DwarfPubNamesSection = 860 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"", 861 true, SectionKind::getMetadata()); 862 DwarfPubTypesSection = 863 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"", 864 true, SectionKind::getMetadata()); 865 DwarfStrSection = 866 getCOFFSection("\t.section\t.debug_str,\"dr\"", 867 true, SectionKind::getMetadata()); 868 DwarfLocSection = 869 getCOFFSection("\t.section\t.debug_loc,\"dr\"", 870 true, SectionKind::getMetadata()); 871 DwarfARangesSection = 872 getCOFFSection("\t.section\t.debug_aranges,\"dr\"", 873 true, SectionKind::getMetadata()); 874 DwarfRangesSection = 875 getCOFFSection("\t.section\t.debug_ranges,\"dr\"", 876 true, SectionKind::getMetadata()); 877 DwarfMacroInfoSection = 878 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"", 879 true, SectionKind::getMetadata()); 880} 881 882const MCSection *TargetLoweringObjectFileCOFF:: 883getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 884 Mangler *Mang, const TargetMachine &TM) const { 885 return getCOFFSection(GV->getSection(), false, Kind); 886} 887 888static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { 889 if (Kind.isText()) 890 return ".text$linkonce"; 891 if (Kind.isWriteable()) 892 return ".data$linkonce"; 893 return ".rdata$linkonce"; 894} 895 896 897const MCSection *TargetLoweringObjectFileCOFF:: 898SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 899 Mangler *Mang, const TargetMachine &TM) const { 900 assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 901 902 // If this global is linkonce/weak and the target handles this by emitting it 903 // into a 'uniqued' section name, create and return the section now. 904 if (GV->isWeakForLinker()) { 905 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); 906 SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 907 Mang->getNameWithPrefix(Name, GV, false); 908 return getCOFFSection(Name.str(), false, Kind); 909 } 910 911 if (Kind.isText()) 912 return getTextSection(); 913 914 return getDataSection(); 915} 916 917