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/MCAsmInfo.h" 28 #include "llvm/MC/MCContext.h" 29 #include "llvm/MC/MCExpr.h" 30 #include "llvm/MC/MCSectionCOFF.h" 31 #include "llvm/MC/MCSectionELF.h" 32 #include "llvm/MC/MCSectionMachO.h" 33 #include "llvm/MC/MCStreamer.h" 34 #include "llvm/MC/MCSymbolELF.h" 35 #include "llvm/MC/MCValue.h" 36 #include "llvm/ProfileData/InstrProf.h" 37 #include "llvm/ProfileData/ProfileCommon.h" 38 #include "llvm/Support/COFF.h" 39 #include "llvm/Support/Dwarf.h" 40 #include "llvm/Support/ELF.h" 41 #include "llvm/Support/ErrorHandling.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include "llvm/Target/TargetLowering.h" 44 #include "llvm/Target/TargetMachine.h" 45 #include "llvm/Target/TargetSubtargetInfo.h" 46 using namespace llvm; 47 using namespace dwarf; 48 49 //===----------------------------------------------------------------------===// 50 // ELF 51 //===----------------------------------------------------------------------===// 52 53 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( 54 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 55 MachineModuleInfo *MMI) const { 56 unsigned Encoding = getPersonalityEncoding(); 57 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect) 58 return getContext().getOrCreateSymbol(StringRef("DW.ref.") + 59 TM.getSymbol(GV, Mang)->getName()); 60 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr) 61 return TM.getSymbol(GV, Mang); 62 report_fatal_error("We do not support this DWARF encoding yet!"); 63 } 64 65 void TargetLoweringObjectFileELF::emitPersonalityValue( 66 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { 67 SmallString<64> NameData("DW.ref."); 68 NameData += Sym->getName(); 69 MCSymbolELF *Label = 70 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); 71 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); 72 Streamer.EmitSymbolAttribute(Label, MCSA_Weak); 73 StringRef Prefix = ".data."; 74 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); 75 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; 76 MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS, 77 Flags, 0, Label->getName()); 78 unsigned Size = DL.getPointerSize(); 79 Streamer.SwitchSection(Sec); 80 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment()); 81 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); 82 const MCExpr *E = MCConstantExpr::create(Size, getContext()); 83 Streamer.emitELFSize(Label, E); 84 Streamer.EmitLabel(Label); 85 86 Streamer.EmitSymbolValue(Sym, Size); 87 } 88 89 const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( 90 const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 91 const TargetMachine &TM, MachineModuleInfo *MMI, 92 MCStreamer &Streamer) const { 93 94 if (Encoding & dwarf::DW_EH_PE_indirect) { 95 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 96 97 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM); 98 99 // Add information about the stub reference to ELFMMI so that the stub 100 // gets emitted by the asmprinter. 101 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 102 if (!StubSym.getPointer()) { 103 MCSymbol *Sym = TM.getSymbol(GV, Mang); 104 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 105 } 106 107 return TargetLoweringObjectFile:: 108 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 109 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 110 } 111 112 return TargetLoweringObjectFile:: 113 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer); 114 } 115 116 static SectionKind 117 getELFKindForNamedSection(StringRef Name, SectionKind K) { 118 // N.B.: The defaults used in here are no the same ones used in MC. 119 // We follow gcc, MC follows gas. For example, given ".section .eh_frame", 120 // both gas and MC will produce a section with no flags. Given 121 // section(".eh_frame") gcc will produce: 122 // 123 // .section .eh_frame,"a",@progbits 124 125 if (Name == getInstrProfCoverageSectionName(false)) 126 return SectionKind::getMetadata(); 127 128 if (Name.empty() || Name[0] != '.') return K; 129 130 // Some lame default implementation based on some magic section names. 131 if (Name == ".bss" || 132 Name.startswith(".bss.") || 133 Name.startswith(".gnu.linkonce.b.") || 134 Name.startswith(".llvm.linkonce.b.") || 135 Name == ".sbss" || 136 Name.startswith(".sbss.") || 137 Name.startswith(".gnu.linkonce.sb.") || 138 Name.startswith(".llvm.linkonce.sb.")) 139 return SectionKind::getBSS(); 140 141 if (Name == ".tdata" || 142 Name.startswith(".tdata.") || 143 Name.startswith(".gnu.linkonce.td.") || 144 Name.startswith(".llvm.linkonce.td.")) 145 return SectionKind::getThreadData(); 146 147 if (Name == ".tbss" || 148 Name.startswith(".tbss.") || 149 Name.startswith(".gnu.linkonce.tb.") || 150 Name.startswith(".llvm.linkonce.tb.")) 151 return SectionKind::getThreadBSS(); 152 153 return K; 154 } 155 156 157 static unsigned getELFSectionType(StringRef Name, SectionKind K) { 158 159 if (Name == ".init_array") 160 return ELF::SHT_INIT_ARRAY; 161 162 if (Name == ".fini_array") 163 return ELF::SHT_FINI_ARRAY; 164 165 if (Name == ".preinit_array") 166 return ELF::SHT_PREINIT_ARRAY; 167 168 if (K.isBSS() || K.isThreadBSS()) 169 return ELF::SHT_NOBITS; 170 171 return ELF::SHT_PROGBITS; 172 } 173 174 static unsigned getELFSectionFlags(SectionKind K) { 175 unsigned Flags = 0; 176 177 if (!K.isMetadata()) 178 Flags |= ELF::SHF_ALLOC; 179 180 if (K.isText()) 181 Flags |= ELF::SHF_EXECINSTR; 182 183 if (K.isWriteable()) 184 Flags |= ELF::SHF_WRITE; 185 186 if (K.isThreadLocal()) 187 Flags |= ELF::SHF_TLS; 188 189 if (K.isMergeableCString() || K.isMergeableConst()) 190 Flags |= ELF::SHF_MERGE; 191 192 if (K.isMergeableCString()) 193 Flags |= ELF::SHF_STRINGS; 194 195 return Flags; 196 } 197 198 static const Comdat *getELFComdat(const GlobalValue *GV) { 199 const Comdat *C = GV->getComdat(); 200 if (!C) 201 return nullptr; 202 203 if (C->getSelectionKind() != Comdat::Any) 204 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + 205 C->getName() + "' cannot be lowered."); 206 207 return C; 208 } 209 210 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( 211 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 212 const TargetMachine &TM) const { 213 StringRef SectionName = GV->getSection(); 214 215 // Infer section flags from the section name if we can. 216 Kind = getELFKindForNamedSection(SectionName, Kind); 217 218 StringRef Group = ""; 219 unsigned Flags = getELFSectionFlags(Kind); 220 if (const Comdat *C = getELFComdat(GV)) { 221 Group = C->getName(); 222 Flags |= ELF::SHF_GROUP; 223 } 224 return getContext().getELFSection(SectionName, 225 getELFSectionType(SectionName, Kind), Flags, 226 /*EntrySize=*/0, Group); 227 } 228 229 /// Return the section prefix name used by options FunctionsSections and 230 /// DataSections. 231 static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 232 if (Kind.isText()) 233 return ".text"; 234 if (Kind.isReadOnly()) 235 return ".rodata"; 236 if (Kind.isBSS()) 237 return ".bss"; 238 if (Kind.isThreadData()) 239 return ".tdata"; 240 if (Kind.isThreadBSS()) 241 return ".tbss"; 242 if (Kind.isData()) 243 return ".data"; 244 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 245 return ".data.rel.ro"; 246 } 247 248 static cl::opt<bool> GroupFunctionsByHotness( 249 "group-functions-by-hotness", 250 llvm::cl::desc("Partition hot/cold functions by sections prefix"), 251 cl::init(false)); 252 253 static MCSectionELF * 254 selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV, 255 SectionKind Kind, Mangler &Mang, 256 const TargetMachine &TM, bool EmitUniqueSection, 257 unsigned Flags, unsigned *NextUniqueID) { 258 unsigned EntrySize = 0; 259 if (Kind.isMergeableCString()) { 260 if (Kind.isMergeable2ByteCString()) { 261 EntrySize = 2; 262 } else if (Kind.isMergeable4ByteCString()) { 263 EntrySize = 4; 264 } else { 265 EntrySize = 1; 266 assert(Kind.isMergeable1ByteCString() && "unknown string width"); 267 } 268 } else if (Kind.isMergeableConst()) { 269 if (Kind.isMergeableConst4()) { 270 EntrySize = 4; 271 } else if (Kind.isMergeableConst8()) { 272 EntrySize = 8; 273 } else { 274 assert(Kind.isMergeableConst16() && "unknown data width"); 275 EntrySize = 16; 276 } 277 } 278 279 StringRef Group = ""; 280 if (const Comdat *C = getELFComdat(GV)) { 281 Flags |= ELF::SHF_GROUP; 282 Group = C->getName(); 283 } 284 285 bool UniqueSectionNames = TM.getUniqueSectionNames(); 286 SmallString<128> Name; 287 if (Kind.isMergeableCString()) { 288 // We also need alignment here. 289 // FIXME: this is getting the alignment of the character, not the 290 // alignment of the global! 291 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment( 292 cast<GlobalVariable>(GV)); 293 294 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 295 Name = SizeSpec + utostr(Align); 296 } else if (Kind.isMergeableConst()) { 297 Name = ".rodata.cst"; 298 Name += utostr(EntrySize); 299 } else { 300 Name = getSectionPrefixForGlobal(Kind); 301 } 302 303 if (GroupFunctionsByHotness) { 304 if (const Function *F = dyn_cast<Function>(GV)) { 305 if (ProfileSummary::isFunctionHot(F)) { 306 Name += getHotSectionPrefix(); 307 } else if (ProfileSummary::isFunctionUnlikely(F)) { 308 Name += getUnlikelySectionPrefix(); 309 } 310 } 311 } 312 313 if (EmitUniqueSection && UniqueSectionNames) { 314 Name.push_back('.'); 315 TM.getNameWithPrefix(Name, GV, Mang, true); 316 } 317 unsigned UniqueID = ~0; 318 if (EmitUniqueSection && !UniqueSectionNames) { 319 UniqueID = *NextUniqueID; 320 (*NextUniqueID)++; 321 } 322 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 323 EntrySize, Group, UniqueID); 324 } 325 326 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 327 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 328 const TargetMachine &TM) const { 329 unsigned Flags = getELFSectionFlags(Kind); 330 331 // If we have -ffunction-section or -fdata-section then we should emit the 332 // global value to a uniqued section specifically for it. 333 bool EmitUniqueSection = false; 334 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 335 if (Kind.isText()) 336 EmitUniqueSection = TM.getFunctionSections(); 337 else 338 EmitUniqueSection = TM.getDataSections(); 339 } 340 EmitUniqueSection |= GV->hasComdat(); 341 342 return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM, 343 EmitUniqueSection, Flags, &NextUniqueID); 344 } 345 346 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 347 const Function &F, Mangler &Mang, const TargetMachine &TM) const { 348 // If the function can be removed, produce a unique section so that 349 // the table doesn't prevent the removal. 350 const Comdat *C = F.getComdat(); 351 bool EmitUniqueSection = TM.getFunctionSections() || C; 352 if (!EmitUniqueSection) 353 return ReadOnlySection; 354 355 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 356 Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC, 357 &NextUniqueID); 358 } 359 360 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 361 bool UsesLabelDifference, const Function &F) const { 362 // We can always create relative relocations, so use another section 363 // that can be marked non-executable. 364 return false; 365 } 366 367 /// Given a mergeable constant with the specified size and relocation 368 /// information, return a section that it should be placed in. 369 MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 370 const DataLayout &DL, SectionKind Kind, const Constant *C, 371 unsigned &Align) const { 372 if (Kind.isMergeableConst4() && MergeableConst4Section) 373 return MergeableConst4Section; 374 if (Kind.isMergeableConst8() && MergeableConst8Section) 375 return MergeableConst8Section; 376 if (Kind.isMergeableConst16() && MergeableConst16Section) 377 return MergeableConst16Section; 378 if (Kind.isReadOnly()) 379 return ReadOnlySection; 380 381 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 382 return DataRelROSection; 383 } 384 385 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 386 bool IsCtor, unsigned Priority, 387 const MCSymbol *KeySym) { 388 std::string Name; 389 unsigned Type; 390 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 391 StringRef COMDAT = KeySym ? KeySym->getName() : ""; 392 393 if (KeySym) 394 Flags |= ELF::SHF_GROUP; 395 396 if (UseInitArray) { 397 if (IsCtor) { 398 Type = ELF::SHT_INIT_ARRAY; 399 Name = ".init_array"; 400 } else { 401 Type = ELF::SHT_FINI_ARRAY; 402 Name = ".fini_array"; 403 } 404 if (Priority != 65535) { 405 Name += '.'; 406 Name += utostr(Priority); 407 } 408 } else { 409 // The default scheme is .ctor / .dtor, so we have to invert the priority 410 // numbering. 411 if (IsCtor) 412 Name = ".ctors"; 413 else 414 Name = ".dtors"; 415 if (Priority != 65535) { 416 Name += '.'; 417 Name += utostr(65535 - Priority); 418 } 419 Type = ELF::SHT_PROGBITS; 420 } 421 422 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 423 } 424 425 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 426 unsigned Priority, const MCSymbol *KeySym) const { 427 return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 428 KeySym); 429 } 430 431 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 432 unsigned Priority, const MCSymbol *KeySym) const { 433 return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 434 KeySym); 435 } 436 437 void 438 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 439 UseInitArray = UseInitArray_; 440 if (!UseInitArray) 441 return; 442 443 StaticCtorSection = getContext().getELFSection( 444 ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 445 StaticDtorSection = getContext().getELFSection( 446 ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 447 } 448 449 //===----------------------------------------------------------------------===// 450 // MachO 451 //===----------------------------------------------------------------------===// 452 453 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 454 : TargetLoweringObjectFile() { 455 SupportIndirectSymViaGOTPCRel = true; 456 } 457 458 /// emitModuleFlags - Perform code emission for module flags. 459 void TargetLoweringObjectFileMachO:: 460 emitModuleFlags(MCStreamer &Streamer, 461 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 462 Mangler &Mang, const TargetMachine &TM) const { 463 unsigned VersionVal = 0; 464 unsigned ImageInfoFlags = 0; 465 MDNode *LinkerOptions = nullptr; 466 StringRef SectionVal; 467 468 for (ArrayRef<Module::ModuleFlagEntry>::iterator 469 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 470 const Module::ModuleFlagEntry &MFE = *i; 471 472 // Ignore flags with 'Require' behavior. 473 if (MFE.Behavior == Module::Require) 474 continue; 475 476 StringRef Key = MFE.Key->getString(); 477 Metadata *Val = MFE.Val; 478 479 if (Key == "Objective-C Image Info Version") { 480 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue(); 481 } else if (Key == "Objective-C Garbage Collection" || 482 Key == "Objective-C GC Only" || 483 Key == "Objective-C Is Simulated" || 484 Key == "Objective-C Class Properties" || 485 Key == "Objective-C Image Swift Version") { 486 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue(); 487 } else if (Key == "Objective-C Image Info Section") { 488 SectionVal = cast<MDString>(Val)->getString(); 489 } else if (Key == "Linker Options") { 490 LinkerOptions = cast<MDNode>(Val); 491 } 492 } 493 494 // Emit the linker options if present. 495 if (LinkerOptions) { 496 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 497 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 498 SmallVector<std::string, 4> StrOptions; 499 500 // Convert to strings. 501 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 502 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 503 StrOptions.push_back(MDOption->getString()); 504 } 505 506 Streamer.EmitLinkerOptions(StrOptions); 507 } 508 } 509 510 // The section is mandatory. If we don't have it, then we don't have GC info. 511 if (SectionVal.empty()) return; 512 513 StringRef Segment, Section; 514 unsigned TAA = 0, StubSize = 0; 515 bool TAAParsed; 516 std::string ErrorCode = 517 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 518 TAA, TAAParsed, StubSize); 519 if (!ErrorCode.empty()) 520 // If invalid, report the error with report_fatal_error. 521 report_fatal_error("Invalid section specifier '" + Section + "': " + 522 ErrorCode + "."); 523 524 // Get the section. 525 MCSectionMachO *S = getContext().getMachOSection( 526 Segment, Section, TAA, StubSize, SectionKind::getData()); 527 Streamer.SwitchSection(S); 528 Streamer.EmitLabel(getContext(). 529 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 530 Streamer.EmitIntValue(VersionVal, 4); 531 Streamer.EmitIntValue(ImageInfoFlags, 4); 532 Streamer.AddBlankLine(); 533 } 534 535 static void checkMachOComdat(const GlobalValue *GV) { 536 const Comdat *C = GV->getComdat(); 537 if (!C) 538 return; 539 540 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 541 "' cannot be lowered."); 542 } 543 544 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 545 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 546 const TargetMachine &TM) const { 547 // Parse the section specifier and create it if valid. 548 StringRef Segment, Section; 549 unsigned TAA = 0, StubSize = 0; 550 bool TAAParsed; 551 552 checkMachOComdat(GV); 553 554 std::string ErrorCode = 555 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 556 TAA, TAAParsed, StubSize); 557 if (!ErrorCode.empty()) { 558 // If invalid, report the error with report_fatal_error. 559 report_fatal_error("Global variable '" + GV->getName() + 560 "' has an invalid section specifier '" + 561 GV->getSection() + "': " + ErrorCode + "."); 562 } 563 564 // Get the section. 565 MCSectionMachO *S = 566 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 567 568 // If TAA wasn't set by ParseSectionSpecifier() above, 569 // use the value returned by getMachOSection() as a default. 570 if (!TAAParsed) 571 TAA = S->getTypeAndAttributes(); 572 573 // Okay, now that we got the section, verify that the TAA & StubSize agree. 574 // If the user declared multiple globals with different section flags, we need 575 // to reject it here. 576 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 577 // If invalid, report the error with report_fatal_error. 578 report_fatal_error("Global variable '" + GV->getName() + 579 "' section type or attributes does not match previous" 580 " section specifier"); 581 } 582 583 return S; 584 } 585 586 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 587 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 588 const TargetMachine &TM) const { 589 checkMachOComdat(GV); 590 591 // Handle thread local data. 592 if (Kind.isThreadBSS()) return TLSBSSSection; 593 if (Kind.isThreadData()) return TLSDataSection; 594 595 if (Kind.isText()) 596 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 597 598 // If this is weak/linkonce, put this in a coalescable section, either in text 599 // or data depending on if it is writable. 600 if (GV->isWeakForLinker()) { 601 if (Kind.isReadOnly()) 602 return ConstTextCoalSection; 603 return DataCoalSection; 604 } 605 606 // FIXME: Alignment check should be handled by section classifier. 607 if (Kind.isMergeable1ByteCString() && 608 GV->getParent()->getDataLayout().getPreferredAlignment( 609 cast<GlobalVariable>(GV)) < 32) 610 return CStringSection; 611 612 // Do not put 16-bit arrays in the UString section if they have an 613 // externally visible label, this runs into issues with certain linker 614 // versions. 615 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 616 GV->getParent()->getDataLayout().getPreferredAlignment( 617 cast<GlobalVariable>(GV)) < 32) 618 return UStringSection; 619 620 // With MachO only variables whose corresponding symbol starts with 'l' or 621 // 'L' can be merged, so we only try merging GVs with private linkage. 622 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) { 623 if (Kind.isMergeableConst4()) 624 return FourByteConstantSection; 625 if (Kind.isMergeableConst8()) 626 return EightByteConstantSection; 627 if (Kind.isMergeableConst16()) 628 return SixteenByteConstantSection; 629 } 630 631 // Otherwise, if it is readonly, but not something we can specially optimize, 632 // just drop it in .const. 633 if (Kind.isReadOnly()) 634 return ReadOnlySection; 635 636 // If this is marked const, put it into a const section. But if the dynamic 637 // linker needs to write to it, put it in the data segment. 638 if (Kind.isReadOnlyWithRel()) 639 return ConstDataSection; 640 641 // Put zero initialized globals with strong external linkage in the 642 // DATA, __common section with the .zerofill directive. 643 if (Kind.isBSSExtern()) 644 return DataCommonSection; 645 646 // Put zero initialized globals with local linkage in __DATA,__bss directive 647 // with the .zerofill directive (aka .lcomm). 648 if (Kind.isBSSLocal()) 649 return DataBSSSection; 650 651 // Otherwise, just drop the variable in the normal data section. 652 return DataSection; 653 } 654 655 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 656 const DataLayout &DL, SectionKind Kind, const Constant *C, 657 unsigned &Align) const { 658 // If this constant requires a relocation, we have to put it in the data 659 // segment, not in the text segment. 660 if (Kind.isData() || Kind.isReadOnlyWithRel()) 661 return ConstDataSection; 662 663 if (Kind.isMergeableConst4()) 664 return FourByteConstantSection; 665 if (Kind.isMergeableConst8()) 666 return EightByteConstantSection; 667 if (Kind.isMergeableConst16()) 668 return SixteenByteConstantSection; 669 return ReadOnlySection; // .const 670 } 671 672 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 673 const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 674 const TargetMachine &TM, MachineModuleInfo *MMI, 675 MCStreamer &Streamer) const { 676 // The mach-o version of this method defaults to returning a stub reference. 677 678 if (Encoding & DW_EH_PE_indirect) { 679 MachineModuleInfoMachO &MachOMMI = 680 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 681 682 MCSymbol *SSym = 683 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 684 685 // Add information about the stub reference to MachOMMI so that the stub 686 // gets emitted by the asmprinter. 687 MachineModuleInfoImpl::StubValueTy &StubSym = 688 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : 689 MachOMMI.getGVStubEntry(SSym); 690 if (!StubSym.getPointer()) { 691 MCSymbol *Sym = TM.getSymbol(GV, Mang); 692 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 693 } 694 695 return TargetLoweringObjectFile:: 696 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 697 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 698 } 699 700 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, 701 TM, MMI, Streamer); 702 } 703 704 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 705 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 706 MachineModuleInfo *MMI) const { 707 // The mach-o version of this method defaults to returning a stub reference. 708 MachineModuleInfoMachO &MachOMMI = 709 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 710 711 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 712 713 // Add information about the stub reference to MachOMMI so that the stub 714 // gets emitted by the asmprinter. 715 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 716 if (!StubSym.getPointer()) { 717 MCSymbol *Sym = TM.getSymbol(GV, Mang); 718 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 719 } 720 721 return SSym; 722 } 723 724 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 725 const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 726 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 727 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 728 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 729 // through a non_lazy_ptr stub instead. One advantage is that it allows the 730 // computation of deltas to final external symbols. Example: 731 // 732 // _extgotequiv: 733 // .long _extfoo 734 // 735 // _delta: 736 // .long _extgotequiv-_delta 737 // 738 // is transformed to: 739 // 740 // _delta: 741 // .long L_extfoo$non_lazy_ptr-(_delta+0) 742 // 743 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 744 // L_extfoo$non_lazy_ptr: 745 // .indirect_symbol _extfoo 746 // .long 0 747 // 748 MachineModuleInfoMachO &MachOMMI = 749 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 750 MCContext &Ctx = getContext(); 751 752 // The offset must consider the original displacement from the base symbol 753 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 754 Offset = -MV.getConstant(); 755 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 756 757 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 758 // non_lazy_ptr stubs. 759 SmallString<128> Name; 760 StringRef Suffix = "$non_lazy_ptr"; 761 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 762 Name += Sym->getName(); 763 Name += Suffix; 764 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 765 766 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 767 if (!StubSym.getPointer()) 768 StubSym = MachineModuleInfoImpl:: 769 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); 770 771 const MCExpr *BSymExpr = 772 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 773 const MCExpr *LHS = 774 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 775 776 if (!Offset) 777 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 778 779 const MCExpr *RHS = 780 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 781 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 782 } 783 784 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 785 const MCSection &Section) { 786 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 787 return true; 788 789 // If it is not dead stripped, it is safe to use private labels. 790 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 791 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 792 return true; 793 794 return false; 795 } 796 797 void TargetLoweringObjectFileMachO::getNameWithPrefix( 798 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 799 const TargetMachine &TM) const { 800 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); 801 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); 802 bool CannotUsePrivateLabel = 803 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 804 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 805 } 806 807 //===----------------------------------------------------------------------===// 808 // COFF 809 //===----------------------------------------------------------------------===// 810 811 static unsigned 812 getCOFFSectionFlags(SectionKind K) { 813 unsigned Flags = 0; 814 815 if (K.isMetadata()) 816 Flags |= 817 COFF::IMAGE_SCN_MEM_DISCARDABLE; 818 else if (K.isText()) 819 Flags |= 820 COFF::IMAGE_SCN_MEM_EXECUTE | 821 COFF::IMAGE_SCN_MEM_READ | 822 COFF::IMAGE_SCN_CNT_CODE; 823 else if (K.isBSS()) 824 Flags |= 825 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 826 COFF::IMAGE_SCN_MEM_READ | 827 COFF::IMAGE_SCN_MEM_WRITE; 828 else if (K.isThreadLocal()) 829 Flags |= 830 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 831 COFF::IMAGE_SCN_MEM_READ | 832 COFF::IMAGE_SCN_MEM_WRITE; 833 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 834 Flags |= 835 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 836 COFF::IMAGE_SCN_MEM_READ; 837 else if (K.isWriteable()) 838 Flags |= 839 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 840 COFF::IMAGE_SCN_MEM_READ | 841 COFF::IMAGE_SCN_MEM_WRITE; 842 843 return Flags; 844 } 845 846 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 847 const Comdat *C = GV->getComdat(); 848 assert(C && "expected GV to have a Comdat!"); 849 850 StringRef ComdatGVName = C->getName(); 851 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 852 if (!ComdatGV) 853 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 854 "' does not exist."); 855 856 if (ComdatGV->getComdat() != C) 857 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 858 "' is not a key for its COMDAT."); 859 860 return ComdatGV; 861 } 862 863 static int getSelectionForCOFF(const GlobalValue *GV) { 864 if (const Comdat *C = GV->getComdat()) { 865 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 866 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 867 ComdatKey = GA->getBaseObject(); 868 if (ComdatKey == GV) { 869 switch (C->getSelectionKind()) { 870 case Comdat::Any: 871 return COFF::IMAGE_COMDAT_SELECT_ANY; 872 case Comdat::ExactMatch: 873 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 874 case Comdat::Largest: 875 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 876 case Comdat::NoDuplicates: 877 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 878 case Comdat::SameSize: 879 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 880 } 881 } else { 882 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 883 } 884 } 885 return 0; 886 } 887 888 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 889 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 890 const TargetMachine &TM) const { 891 int Selection = 0; 892 unsigned Characteristics = getCOFFSectionFlags(Kind); 893 StringRef Name = GV->getSection(); 894 StringRef COMDATSymName = ""; 895 if (GV->hasComdat()) { 896 Selection = getSelectionForCOFF(GV); 897 const GlobalValue *ComdatGV; 898 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 899 ComdatGV = getComdatGVForCOFF(GV); 900 else 901 ComdatGV = GV; 902 903 if (!ComdatGV->hasPrivateLinkage()) { 904 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 905 COMDATSymName = Sym->getName(); 906 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 907 } else { 908 Selection = 0; 909 } 910 } 911 return getContext().getCOFFSection(Name, 912 Characteristics, 913 Kind, 914 COMDATSymName, 915 Selection); 916 } 917 918 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 919 if (Kind.isText()) 920 return ".text"; 921 if (Kind.isBSS()) 922 return ".bss"; 923 if (Kind.isThreadLocal()) 924 return ".tls$"; 925 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 926 return ".rdata"; 927 return ".data"; 928 } 929 930 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 931 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 932 const TargetMachine &TM) const { 933 // If we have -ffunction-sections then we should emit the global value to a 934 // uniqued section specifically for it. 935 bool EmitUniquedSection; 936 if (Kind.isText()) 937 EmitUniquedSection = TM.getFunctionSections(); 938 else 939 EmitUniquedSection = TM.getDataSections(); 940 941 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) { 942 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 943 unsigned Characteristics = getCOFFSectionFlags(Kind); 944 945 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 946 int Selection = getSelectionForCOFF(GV); 947 if (!Selection) 948 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 949 const GlobalValue *ComdatGV; 950 if (GV->hasComdat()) 951 ComdatGV = getComdatGVForCOFF(GV); 952 else 953 ComdatGV = GV; 954 955 if (!ComdatGV->hasPrivateLinkage()) { 956 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 957 StringRef COMDATSymName = Sym->getName(); 958 return getContext().getCOFFSection(Name, Characteristics, Kind, 959 COMDATSymName, Selection); 960 } else { 961 SmallString<256> TmpData; 962 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true); 963 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 964 Selection); 965 } 966 } 967 968 if (Kind.isText()) 969 return TextSection; 970 971 if (Kind.isThreadLocal()) 972 return TLSDataSection; 973 974 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 975 return ReadOnlySection; 976 977 // Note: we claim that common symbols are put in BSSSection, but they are 978 // really emitted with the magic .comm directive, which creates a symbol table 979 // entry but not a section. 980 if (Kind.isBSS() || Kind.isCommon()) 981 return BSSSection; 982 983 return DataSection; 984 } 985 986 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 987 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 988 const TargetMachine &TM) const { 989 bool CannotUsePrivateLabel = false; 990 if (GV->hasPrivateLinkage() && 991 ((isa<Function>(GV) && TM.getFunctionSections()) || 992 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 993 CannotUsePrivateLabel = true; 994 995 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 996 } 997 998 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 999 const Function &F, Mangler &Mang, const TargetMachine &TM) const { 1000 // If the function can be removed, produce a unique section so that 1001 // the table doesn't prevent the removal. 1002 const Comdat *C = F.getComdat(); 1003 bool EmitUniqueSection = TM.getFunctionSections() || C; 1004 if (!EmitUniqueSection) 1005 return ReadOnlySection; 1006 1007 // FIXME: we should produce a symbol for F instead. 1008 if (F.hasPrivateLinkage()) 1009 return ReadOnlySection; 1010 1011 MCSymbol *Sym = TM.getSymbol(&F, Mang); 1012 StringRef COMDATSymName = Sym->getName(); 1013 1014 SectionKind Kind = SectionKind::getReadOnly(); 1015 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 1016 unsigned Characteristics = getCOFFSectionFlags(Kind); 1017 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1018 1019 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1020 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); 1021 } 1022 1023 void TargetLoweringObjectFileCOFF:: 1024 emitModuleFlags(MCStreamer &Streamer, 1025 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 1026 Mangler &Mang, const TargetMachine &TM) const { 1027 MDNode *LinkerOptions = nullptr; 1028 1029 // Look for the "Linker Options" flag, since it's the only one we support. 1030 for (ArrayRef<Module::ModuleFlagEntry>::iterator 1031 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 1032 const Module::ModuleFlagEntry &MFE = *i; 1033 StringRef Key = MFE.Key->getString(); 1034 Metadata *Val = MFE.Val; 1035 if (Key == "Linker Options") { 1036 LinkerOptions = cast<MDNode>(Val); 1037 break; 1038 } 1039 } 1040 if (!LinkerOptions) 1041 return; 1042 1043 // Emit the linker options to the linker .drectve section. According to the 1044 // spec, this section is a space-separated string containing flags for linker. 1045 MCSection *Sec = getDrectveSection(); 1046 Streamer.SwitchSection(Sec); 1047 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 1048 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 1049 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 1050 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 1051 // Lead with a space for consistency with our dllexport implementation. 1052 std::string Directive(" "); 1053 Directive.append(MDOption->getString()); 1054 Streamer.EmitBytes(Directive); 1055 } 1056 } 1057 } 1058 1059 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1060 unsigned Priority, const MCSymbol *KeySym) const { 1061 return getContext().getAssociativeCOFFSection( 1062 cast<MCSectionCOFF>(StaticCtorSection), KeySym); 1063 } 1064 1065 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1066 unsigned Priority, const MCSymbol *KeySym) const { 1067 return getContext().getAssociativeCOFFSection( 1068 cast<MCSectionCOFF>(StaticDtorSection), KeySym); 1069 } 1070 1071 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 1072 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const { 1073 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration()) 1074 return; 1075 1076 const Triple &TT = getTargetTriple(); 1077 1078 if (TT.isKnownWindowsMSVCEnvironment()) 1079 OS << " /EXPORT:"; 1080 else 1081 OS << " -export:"; 1082 1083 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) { 1084 std::string Flag; 1085 raw_string_ostream FlagOS(Flag); 1086 Mang.getNameWithPrefix(FlagOS, GV, false); 1087 FlagOS.flush(); 1088 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) 1089 OS << Flag.substr(1); 1090 else 1091 OS << Flag; 1092 } else { 1093 Mang.getNameWithPrefix(OS, GV, false); 1094 } 1095 1096 if (!GV->getValueType()->isFunctionTy()) { 1097 if (TT.isKnownWindowsMSVCEnvironment()) 1098 OS << ",DATA"; 1099 else 1100 OS << ",data"; 1101 } 1102 } 1103