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