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/ADT/SmallString.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 20 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/DerivedTypes.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/IR/GlobalVariable.h" 25 #include "llvm/IR/Mangler.h" 26 #include "llvm/IR/Module.h" 27 #include "llvm/MC/MCContext.h" 28 #include "llvm/MC/MCExpr.h" 29 #include "llvm/MC/MCSectionCOFF.h" 30 #include "llvm/MC/MCSectionELF.h" 31 #include "llvm/MC/MCSectionMachO.h" 32 #include "llvm/MC/MCStreamer.h" 33 #include "llvm/MC/MCSymbol.h" 34 #include "llvm/Support/Dwarf.h" 35 #include "llvm/Support/ELF.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include "llvm/Target/TargetMachine.h" 39 using namespace llvm; 40 using namespace dwarf; 41 42 //===----------------------------------------------------------------------===// 43 // ELF 44 //===----------------------------------------------------------------------===// 45 46 MCSymbol * 47 TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV, 48 Mangler *Mang, 49 MachineModuleInfo *MMI) const { 50 unsigned Encoding = getPersonalityEncoding(); 51 switch (Encoding & 0x70) { 52 default: 53 report_fatal_error("We do not support this DWARF encoding yet!"); 54 case dwarf::DW_EH_PE_absptr: 55 return getSymbol(*Mang, GV); 56 case dwarf::DW_EH_PE_pcrel: { 57 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") + 58 getSymbol(*Mang, GV)->getName()); 59 } 60 } 61 } 62 63 void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer, 64 const TargetMachine &TM, 65 const MCSymbol *Sym) const { 66 SmallString<64> NameData("DW.ref."); 67 NameData += Sym->getName(); 68 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData); 69 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); 70 Streamer.EmitSymbolAttribute(Label, MCSA_Weak); 71 StringRef Prefix = ".data."; 72 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); 73 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; 74 const MCSection *Sec = getContext().getELFSection(NameData, 75 ELF::SHT_PROGBITS, 76 Flags, 77 SectionKind::getDataRel(), 78 0, Label->getName()); 79 unsigned Size = TM.getDataLayout()->getPointerSize(); 80 Streamer.SwitchSection(Sec); 81 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment()); 82 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); 83 const MCExpr *E = MCConstantExpr::Create(Size, getContext()); 84 Streamer.EmitELFSize(Label, E); 85 Streamer.EmitLabel(Label); 86 87 Streamer.EmitSymbolValue(Sym, Size); 88 } 89 90 const MCExpr *TargetLoweringObjectFileELF:: 91 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang, 92 MachineModuleInfo *MMI, unsigned Encoding, 93 MCStreamer &Streamer) const { 94 95 if (Encoding & dwarf::DW_EH_PE_indirect) { 96 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 97 98 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, ".DW.stub"); 99 100 // Add information about the stub reference to ELFMMI so that the stub 101 // gets emitted by the asmprinter. 102 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 103 if (StubSym.getPointer() == 0) { 104 MCSymbol *Sym = getSymbol(*Mang, GV); 105 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 106 } 107 108 return TargetLoweringObjectFile:: 109 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()), 110 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 111 } 112 113 return TargetLoweringObjectFile:: 114 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer); 115 } 116 117 static SectionKind 118 getELFKindForNamedSection(StringRef Name, SectionKind K) { 119 // N.B.: The defaults used in here are no the same ones used in MC. 120 // We follow gcc, MC follows gas. For example, given ".section .eh_frame", 121 // both gas and MC will produce a section with no flags. Given 122 // section(".eh_frame") gcc will produce: 123 // 124 // .section .eh_frame,"a",@progbits 125 if (Name.empty() || Name[0] != '.') return K; 126 127 // Some lame default implementation based on some magic section names. 128 if (Name == ".bss" || 129 Name.startswith(".bss.") || 130 Name.startswith(".gnu.linkonce.b.") || 131 Name.startswith(".llvm.linkonce.b.") || 132 Name == ".sbss" || 133 Name.startswith(".sbss.") || 134 Name.startswith(".gnu.linkonce.sb.") || 135 Name.startswith(".llvm.linkonce.sb.")) 136 return SectionKind::getBSS(); 137 138 if (Name == ".tdata" || 139 Name.startswith(".tdata.") || 140 Name.startswith(".gnu.linkonce.td.") || 141 Name.startswith(".llvm.linkonce.td.")) 142 return SectionKind::getThreadData(); 143 144 if (Name == ".tbss" || 145 Name.startswith(".tbss.") || 146 Name.startswith(".gnu.linkonce.tb.") || 147 Name.startswith(".llvm.linkonce.tb.")) 148 return SectionKind::getThreadBSS(); 149 150 return K; 151 } 152 153 154 static unsigned getELFSectionType(StringRef Name, SectionKind K) { 155 156 if (Name == ".init_array") 157 return ELF::SHT_INIT_ARRAY; 158 159 if (Name == ".fini_array") 160 return ELF::SHT_FINI_ARRAY; 161 162 if (Name == ".preinit_array") 163 return ELF::SHT_PREINIT_ARRAY; 164 165 if (K.isBSS() || K.isThreadBSS()) 166 return ELF::SHT_NOBITS; 167 168 return ELF::SHT_PROGBITS; 169 } 170 171 172 static unsigned 173 getELFSectionFlags(SectionKind K) { 174 unsigned Flags = 0; 175 176 if (!K.isMetadata()) 177 Flags |= ELF::SHF_ALLOC; 178 179 if (K.isText()) 180 Flags |= ELF::SHF_EXECINSTR; 181 182 if (K.isWriteable()) 183 Flags |= ELF::SHF_WRITE; 184 185 if (K.isThreadLocal()) 186 Flags |= ELF::SHF_TLS; 187 188 // K.isMergeableConst() is left out to honour PR4650 189 if (K.isMergeableCString() || K.isMergeableConst4() || 190 K.isMergeableConst8() || K.isMergeableConst16()) 191 Flags |= ELF::SHF_MERGE; 192 193 if (K.isMergeableCString()) 194 Flags |= ELF::SHF_STRINGS; 195 196 return Flags; 197 } 198 199 200 const MCSection *TargetLoweringObjectFileELF:: 201 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 202 Mangler *Mang, const TargetMachine &TM) const { 203 StringRef SectionName = GV->getSection(); 204 205 // Infer section flags from the section name if we can. 206 Kind = getELFKindForNamedSection(SectionName, Kind); 207 208 return getContext().getELFSection(SectionName, 209 getELFSectionType(SectionName, Kind), 210 getELFSectionFlags(Kind), Kind); 211 } 212 213 /// getSectionPrefixForGlobal - Return the section prefix name used by options 214 /// FunctionsSections and DataSections. 215 static const char *getSectionPrefixForGlobal(SectionKind Kind) { 216 if (Kind.isText()) return ".text."; 217 if (Kind.isReadOnly()) return ".rodata."; 218 if (Kind.isBSS()) return ".bss."; 219 220 if (Kind.isThreadData()) return ".tdata."; 221 if (Kind.isThreadBSS()) return ".tbss."; 222 223 if (Kind.isDataNoRel()) return ".data."; 224 if (Kind.isDataRelLocal()) return ".data.rel.local."; 225 if (Kind.isDataRel()) return ".data.rel."; 226 if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; 227 228 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 229 return ".data.rel.ro."; 230 } 231 232 233 const MCSection *TargetLoweringObjectFileELF:: 234 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 235 Mangler *Mang, const TargetMachine &TM) const { 236 // If we have -ffunction-section or -fdata-section then we should emit the 237 // global value to a uniqued section specifically for it. 238 bool EmitUniquedSection; 239 if (Kind.isText()) 240 EmitUniquedSection = TM.getFunctionSections(); 241 else 242 EmitUniquedSection = TM.getDataSections(); 243 244 // If this global is linkonce/weak and the target handles this by emitting it 245 // into a 'uniqued' section name, create and return the section now. 246 if ((GV->isWeakForLinker() || EmitUniquedSection) && 247 !Kind.isCommon()) { 248 const char *Prefix; 249 Prefix = getSectionPrefixForGlobal(Kind); 250 251 SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); 252 MCSymbol *Sym = getSymbol(*Mang, GV); 253 Name.append(Sym->getName().begin(), Sym->getName().end()); 254 StringRef Group = ""; 255 unsigned Flags = getELFSectionFlags(Kind); 256 if (GV->isWeakForLinker()) { 257 Group = Sym->getName(); 258 Flags |= ELF::SHF_GROUP; 259 } 260 261 return getContext().getELFSection(Name.str(), 262 getELFSectionType(Name.str(), Kind), 263 Flags, Kind, 0, Group); 264 } 265 266 if (Kind.isText()) return TextSection; 267 268 if (Kind.isMergeable1ByteCString() || 269 Kind.isMergeable2ByteCString() || 270 Kind.isMergeable4ByteCString()) { 271 272 // We also need alignment here. 273 // FIXME: this is getting the alignment of the character, not the 274 // alignment of the global! 275 unsigned Align = 276 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)); 277 278 const char *SizeSpec = ".rodata.str1."; 279 if (Kind.isMergeable2ByteCString()) 280 SizeSpec = ".rodata.str2."; 281 else if (Kind.isMergeable4ByteCString()) 282 SizeSpec = ".rodata.str4."; 283 else 284 assert(Kind.isMergeable1ByteCString() && "unknown string width"); 285 286 287 std::string Name = SizeSpec + utostr(Align); 288 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, 289 ELF::SHF_ALLOC | 290 ELF::SHF_MERGE | 291 ELF::SHF_STRINGS, 292 Kind); 293 } 294 295 if (Kind.isMergeableConst()) { 296 if (Kind.isMergeableConst4() && MergeableConst4Section) 297 return MergeableConst4Section; 298 if (Kind.isMergeableConst8() && MergeableConst8Section) 299 return MergeableConst8Section; 300 if (Kind.isMergeableConst16() && MergeableConst16Section) 301 return MergeableConst16Section; 302 return ReadOnlySection; // .const 303 } 304 305 if (Kind.isReadOnly()) return ReadOnlySection; 306 307 if (Kind.isThreadData()) return TLSDataSection; 308 if (Kind.isThreadBSS()) return TLSBSSSection; 309 310 // Note: we claim that common symbols are put in BSSSection, but they are 311 // really emitted with the magic .comm directive, which creates a symbol table 312 // entry but not a section. 313 if (Kind.isBSS() || Kind.isCommon()) return BSSSection; 314 315 if (Kind.isDataNoRel()) return DataSection; 316 if (Kind.isDataRelLocal()) return DataRelLocalSection; 317 if (Kind.isDataRel()) return DataRelSection; 318 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 319 320 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 321 return DataRelROSection; 322 } 323 324 /// getSectionForConstant - Given a mergeable constant with the 325 /// specified size and relocation information, return a section that it 326 /// should be placed in. 327 const MCSection *TargetLoweringObjectFileELF:: 328 getSectionForConstant(SectionKind Kind) const { 329 if (Kind.isMergeableConst4() && MergeableConst4Section) 330 return MergeableConst4Section; 331 if (Kind.isMergeableConst8() && MergeableConst8Section) 332 return MergeableConst8Section; 333 if (Kind.isMergeableConst16() && MergeableConst16Section) 334 return MergeableConst16Section; 335 if (Kind.isReadOnly()) 336 return ReadOnlySection; 337 338 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; 339 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 340 return DataRelROSection; 341 } 342 343 const MCSection * 344 TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const { 345 // The default scheme is .ctor / .dtor, so we have to invert the priority 346 // numbering. 347 if (Priority == 65535) 348 return StaticCtorSection; 349 350 if (UseInitArray) { 351 std::string Name = std::string(".init_array.") + utostr(Priority); 352 return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY, 353 ELF::SHF_ALLOC | ELF::SHF_WRITE, 354 SectionKind::getDataRel()); 355 } else { 356 std::string Name = std::string(".ctors.") + utostr(65535 - Priority); 357 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, 358 ELF::SHF_ALLOC |ELF::SHF_WRITE, 359 SectionKind::getDataRel()); 360 } 361 } 362 363 const MCSection * 364 TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const { 365 // The default scheme is .ctor / .dtor, so we have to invert the priority 366 // numbering. 367 if (Priority == 65535) 368 return StaticDtorSection; 369 370 if (UseInitArray) { 371 std::string Name = std::string(".fini_array.") + utostr(Priority); 372 return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY, 373 ELF::SHF_ALLOC | ELF::SHF_WRITE, 374 SectionKind::getDataRel()); 375 } else { 376 std::string Name = std::string(".dtors.") + utostr(65535 - Priority); 377 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, 378 ELF::SHF_ALLOC |ELF::SHF_WRITE, 379 SectionKind::getDataRel()); 380 } 381 } 382 383 void 384 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 385 UseInitArray = UseInitArray_; 386 if (!UseInitArray) 387 return; 388 389 StaticCtorSection = 390 getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 391 ELF::SHF_WRITE | 392 ELF::SHF_ALLOC, 393 SectionKind::getDataRel()); 394 StaticDtorSection = 395 getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 396 ELF::SHF_WRITE | 397 ELF::SHF_ALLOC, 398 SectionKind::getDataRel()); 399 } 400 401 //===----------------------------------------------------------------------===// 402 // MachO 403 //===----------------------------------------------------------------------===// 404 405 /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker 406 /// option string. Returns StringRef() if the option does not specify a library. 407 StringRef TargetLoweringObjectFileMachO:: 408 getDepLibFromLinkerOpt(StringRef LinkerOption) const { 409 const char *LibCmd = "-l"; 410 if (LinkerOption.startswith(LibCmd)) 411 return LinkerOption.substr(strlen(LibCmd)); 412 return StringRef(); 413 } 414 415 /// emitModuleFlags - Perform code emission for module flags. 416 void TargetLoweringObjectFileMachO:: 417 emitModuleFlags(MCStreamer &Streamer, 418 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 419 Mangler *Mang, const TargetMachine &TM) const { 420 unsigned VersionVal = 0; 421 unsigned ImageInfoFlags = 0; 422 MDNode *LinkerOptions = 0; 423 StringRef SectionVal; 424 425 for (ArrayRef<Module::ModuleFlagEntry>::iterator 426 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 427 const Module::ModuleFlagEntry &MFE = *i; 428 429 // Ignore flags with 'Require' behavior. 430 if (MFE.Behavior == Module::Require) 431 continue; 432 433 StringRef Key = MFE.Key->getString(); 434 Value *Val = MFE.Val; 435 436 if (Key == "Objective-C Image Info Version") { 437 VersionVal = cast<ConstantInt>(Val)->getZExtValue(); 438 } else if (Key == "Objective-C Garbage Collection" || 439 Key == "Objective-C GC Only" || 440 Key == "Objective-C Is Simulated") { 441 ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue(); 442 } else if (Key == "Objective-C Image Info Section") { 443 SectionVal = cast<MDString>(Val)->getString(); 444 } else if (Key == "Linker Options") { 445 LinkerOptions = cast<MDNode>(Val); 446 } 447 } 448 449 // Emit the linker options if present. 450 if (LinkerOptions) { 451 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 452 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 453 SmallVector<std::string, 4> StrOptions; 454 455 // Convert to strings. 456 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 457 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 458 StrOptions.push_back(MDOption->getString()); 459 } 460 461 Streamer.EmitLinkerOptions(StrOptions); 462 } 463 } 464 465 // The section is mandatory. If we don't have it, then we don't have GC info. 466 if (SectionVal.empty()) return; 467 468 StringRef Segment, Section; 469 unsigned TAA = 0, StubSize = 0; 470 bool TAAParsed; 471 std::string ErrorCode = 472 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 473 TAA, TAAParsed, StubSize); 474 if (!ErrorCode.empty()) 475 // If invalid, report the error with report_fatal_error. 476 report_fatal_error("Invalid section specifier '" + Section + "': " + 477 ErrorCode + "."); 478 479 // Get the section. 480 const MCSectionMachO *S = 481 getContext().getMachOSection(Segment, Section, TAA, StubSize, 482 SectionKind::getDataNoRel()); 483 Streamer.SwitchSection(S); 484 Streamer.EmitLabel(getContext(). 485 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 486 Streamer.EmitIntValue(VersionVal, 4); 487 Streamer.EmitIntValue(ImageInfoFlags, 4); 488 Streamer.AddBlankLine(); 489 } 490 491 const MCSection *TargetLoweringObjectFileMachO:: 492 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 493 Mangler *Mang, const TargetMachine &TM) const { 494 // Parse the section specifier and create it if valid. 495 StringRef Segment, Section; 496 unsigned TAA = 0, StubSize = 0; 497 bool TAAParsed; 498 std::string ErrorCode = 499 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 500 TAA, TAAParsed, StubSize); 501 if (!ErrorCode.empty()) { 502 // If invalid, report the error with report_fatal_error. 503 report_fatal_error("Global variable '" + GV->getName() + 504 "' has an invalid section specifier '" + 505 GV->getSection() + "': " + ErrorCode + "."); 506 } 507 508 // Get the section. 509 const MCSectionMachO *S = 510 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 511 512 // If TAA wasn't set by ParseSectionSpecifier() above, 513 // use the value returned by getMachOSection() as a default. 514 if (!TAAParsed) 515 TAA = S->getTypeAndAttributes(); 516 517 // Okay, now that we got the section, verify that the TAA & StubSize agree. 518 // If the user declared multiple globals with different section flags, we need 519 // to reject it here. 520 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 521 // If invalid, report the error with report_fatal_error. 522 report_fatal_error("Global variable '" + GV->getName() + 523 "' section type or attributes does not match previous" 524 " section specifier"); 525 } 526 527 return S; 528 } 529 530 const MCSection *TargetLoweringObjectFileMachO:: 531 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 532 Mangler *Mang, const TargetMachine &TM) const { 533 534 // Handle thread local data. 535 if (Kind.isThreadBSS()) return TLSBSSSection; 536 if (Kind.isThreadData()) return TLSDataSection; 537 538 if (Kind.isText()) 539 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 540 541 // If this is weak/linkonce, put this in a coalescable section, either in text 542 // or data depending on if it is writable. 543 if (GV->isWeakForLinker()) { 544 if (Kind.isReadOnly()) 545 return ConstTextCoalSection; 546 return DataCoalSection; 547 } 548 549 // FIXME: Alignment check should be handled by section classifier. 550 if (Kind.isMergeable1ByteCString() && 551 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 552 return CStringSection; 553 554 // Do not put 16-bit arrays in the UString section if they have an 555 // externally visible label, this runs into issues with certain linker 556 // versions. 557 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 558 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) 559 return UStringSection; 560 561 if (Kind.isMergeableConst()) { 562 if (Kind.isMergeableConst4()) 563 return FourByteConstantSection; 564 if (Kind.isMergeableConst8()) 565 return EightByteConstantSection; 566 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 567 return SixteenByteConstantSection; 568 } 569 570 // Otherwise, if it is readonly, but not something we can specially optimize, 571 // just drop it in .const. 572 if (Kind.isReadOnly()) 573 return ReadOnlySection; 574 575 // If this is marked const, put it into a const section. But if the dynamic 576 // linker needs to write to it, put it in the data segment. 577 if (Kind.isReadOnlyWithRel()) 578 return ConstDataSection; 579 580 // Put zero initialized globals with strong external linkage in the 581 // DATA, __common section with the .zerofill directive. 582 if (Kind.isBSSExtern()) 583 return DataCommonSection; 584 585 // Put zero initialized globals with local linkage in __DATA,__bss directive 586 // with the .zerofill directive (aka .lcomm). 587 if (Kind.isBSSLocal()) 588 return DataBSSSection; 589 590 // Otherwise, just drop the variable in the normal data section. 591 return DataSection; 592 } 593 594 const MCSection * 595 TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { 596 // If this constant requires a relocation, we have to put it in the data 597 // segment, not in the text segment. 598 if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) 599 return ConstDataSection; 600 601 if (Kind.isMergeableConst4()) 602 return FourByteConstantSection; 603 if (Kind.isMergeableConst8()) 604 return EightByteConstantSection; 605 if (Kind.isMergeableConst16() && SixteenByteConstantSection) 606 return SixteenByteConstantSection; 607 return ReadOnlySection; // .const 608 } 609 610 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide 611 /// not to emit the UsedDirective for some symbols in llvm.used. 612 // FIXME: REMOVE this (rdar://7071300) 613 bool TargetLoweringObjectFileMachO:: 614 shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { 615 /// On Darwin, internally linked data beginning with "L" or "l" does not have 616 /// the directive emitted (this occurs in ObjC metadata). 617 if (!GV) return false; 618 619 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. 620 if (GV->hasLocalLinkage() && !isa<Function>(GV)) { 621 // FIXME: ObjC metadata is currently emitted as internal symbols that have 622 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and 623 // this horrible hack can go away. 624 MCSymbol *Sym = getSymbol(*Mang, GV); 625 if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l') 626 return false; 627 } 628 629 return true; 630 } 631 632 const MCExpr *TargetLoweringObjectFileMachO:: 633 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang, 634 MachineModuleInfo *MMI, unsigned Encoding, 635 MCStreamer &Streamer) const { 636 // The mach-o version of this method defaults to returning a stub reference. 637 638 if (Encoding & DW_EH_PE_indirect) { 639 MachineModuleInfoMachO &MachOMMI = 640 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 641 642 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr"); 643 644 // Add information about the stub reference to MachOMMI so that the stub 645 // gets emitted by the asmprinter. 646 MachineModuleInfoImpl::StubValueTy &StubSym = 647 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : 648 MachOMMI.getGVStubEntry(SSym); 649 if (StubSym.getPointer() == 0) { 650 MCSymbol *Sym = getSymbol(*Mang, GV); 651 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 652 } 653 654 return TargetLoweringObjectFile:: 655 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()), 656 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 657 } 658 659 return TargetLoweringObjectFile:: 660 getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer); 661 } 662 663 MCSymbol *TargetLoweringObjectFileMachO:: 664 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang, 665 MachineModuleInfo *MMI) const { 666 // The mach-o version of this method defaults to returning a stub reference. 667 MachineModuleInfoMachO &MachOMMI = 668 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 669 670 MCSymbol *SSym = getSymbolWithGlobalValueBase(*Mang, GV, "$non_lazy_ptr"); 671 672 // Add information about the stub reference to MachOMMI so that the stub 673 // gets emitted by the asmprinter. 674 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 675 if (StubSym.getPointer() == 0) { 676 MCSymbol *Sym = getSymbol(*Mang, GV); 677 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 678 } 679 680 return SSym; 681 } 682 683 //===----------------------------------------------------------------------===// 684 // COFF 685 //===----------------------------------------------------------------------===// 686 687 static unsigned 688 getCOFFSectionFlags(SectionKind K) { 689 unsigned Flags = 0; 690 691 if (K.isMetadata()) 692 Flags |= 693 COFF::IMAGE_SCN_MEM_DISCARDABLE; 694 else if (K.isText()) 695 Flags |= 696 COFF::IMAGE_SCN_MEM_EXECUTE | 697 COFF::IMAGE_SCN_MEM_READ | 698 COFF::IMAGE_SCN_CNT_CODE; 699 else if (K.isBSS ()) 700 Flags |= 701 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 702 COFF::IMAGE_SCN_MEM_READ | 703 COFF::IMAGE_SCN_MEM_WRITE; 704 else if (K.isThreadLocal()) 705 Flags |= 706 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 707 COFF::IMAGE_SCN_MEM_READ | 708 COFF::IMAGE_SCN_MEM_WRITE; 709 else if (K.isReadOnly()) 710 Flags |= 711 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 712 COFF::IMAGE_SCN_MEM_READ; 713 else if (K.isWriteable()) 714 Flags |= 715 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 716 COFF::IMAGE_SCN_MEM_READ | 717 COFF::IMAGE_SCN_MEM_WRITE; 718 719 return Flags; 720 } 721 722 const MCSection *TargetLoweringObjectFileCOFF:: 723 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 724 Mangler *Mang, const TargetMachine &TM) const { 725 int Selection = 0; 726 unsigned Characteristics = getCOFFSectionFlags(Kind); 727 StringRef Name = GV->getSection(); 728 StringRef COMDATSymName = ""; 729 if (GV->isWeakForLinker()) { 730 Selection = COFF::IMAGE_COMDAT_SELECT_ANY; 731 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 732 MCSymbol *Sym = getSymbol(*Mang, GV); 733 COMDATSymName = Sym->getName(); 734 } 735 return getContext().getCOFFSection(Name, 736 Characteristics, 737 Kind, 738 COMDATSymName, 739 Selection); 740 } 741 742 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 743 if (Kind.isText()) 744 return ".text"; 745 if (Kind.isBSS ()) 746 return ".bss"; 747 if (Kind.isThreadLocal()) 748 return ".tls$"; 749 if (Kind.isWriteable()) 750 return ".data"; 751 return ".rdata"; 752 } 753 754 755 const MCSection *TargetLoweringObjectFileCOFF:: 756 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 757 Mangler *Mang, const TargetMachine &TM) const { 758 759 // If this global is linkonce/weak and the target handles this by emitting it 760 // into a 'uniqued' section name, create and return the section now. 761 if (GV->isWeakForLinker()) { 762 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 763 unsigned Characteristics = getCOFFSectionFlags(Kind); 764 765 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 766 MCSymbol *Sym = getSymbol(*Mang, GV); 767 return getContext().getCOFFSection(Name, Characteristics, 768 Kind, Sym->getName(), 769 COFF::IMAGE_COMDAT_SELECT_ANY); 770 } 771 772 if (Kind.isText()) 773 return TextSection; 774 775 if (Kind.isThreadLocal()) 776 return TLSDataSection; 777 778 if (Kind.isReadOnly()) 779 return ReadOnlySection; 780 781 if (Kind.isBSS()) 782 return BSSSection; 783 784 return DataSection; 785 } 786 787 StringRef TargetLoweringObjectFileCOFF:: 788 getDepLibFromLinkerOpt(StringRef LinkerOption) const { 789 const char *LibCmd = "/DEFAULTLIB:"; 790 if (LinkerOption.startswith(LibCmd)) 791 return LinkerOption.substr(strlen(LibCmd)); 792 return StringRef(); 793 } 794 795 void TargetLoweringObjectFileCOFF:: 796 emitModuleFlags(MCStreamer &Streamer, 797 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 798 Mangler *Mang, const TargetMachine &TM) const { 799 MDNode *LinkerOptions = 0; 800 801 // Look for the "Linker Options" flag, since it's the only one we support. 802 for (ArrayRef<Module::ModuleFlagEntry>::iterator 803 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 804 const Module::ModuleFlagEntry &MFE = *i; 805 StringRef Key = MFE.Key->getString(); 806 Value *Val = MFE.Val; 807 if (Key == "Linker Options") { 808 LinkerOptions = cast<MDNode>(Val); 809 break; 810 } 811 } 812 if (!LinkerOptions) 813 return; 814 815 // Emit the linker options to the linker .drectve section. According to the 816 // spec, this section is a space-separated string containing flags for linker. 817 const MCSection *Sec = getDrectveSection(); 818 Streamer.SwitchSection(Sec); 819 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 820 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 821 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 822 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 823 StringRef Op = MDOption->getString(); 824 // Lead with a space for consistency with our dllexport implementation. 825 std::string Escaped(" "); 826 if (Op.find(" ") != StringRef::npos) { 827 // The PE-COFF spec says args with spaces must be quoted. It doesn't say 828 // how to escape quotes, but it probably uses this algorithm: 829 // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx 830 // FIXME: Reuse escaping code from Support/Windows/Program.inc 831 Escaped.push_back('\"'); 832 Escaped.append(Op); 833 Escaped.push_back('\"'); 834 } else { 835 Escaped.append(Op); 836 } 837 Streamer.EmitBytes(Escaped); 838 } 839 } 840 } 841