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 if (Kind.isMergeableConst16()) { 274 EntrySize = 16; 275 } else { 276 assert(Kind.isMergeableConst32() && "unknown data width"); 277 EntrySize = 32; 278 } 279 } 280 281 StringRef Group = ""; 282 if (const Comdat *C = getELFComdat(GV)) { 283 Flags |= ELF::SHF_GROUP; 284 Group = C->getName(); 285 } 286 287 bool UniqueSectionNames = TM.getUniqueSectionNames(); 288 SmallString<128> Name; 289 if (Kind.isMergeableCString()) { 290 // We also need alignment here. 291 // FIXME: this is getting the alignment of the character, not the 292 // alignment of the global! 293 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment( 294 cast<GlobalVariable>(GV)); 295 296 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 297 Name = SizeSpec + utostr(Align); 298 } else if (Kind.isMergeableConst()) { 299 Name = ".rodata.cst"; 300 Name += utostr(EntrySize); 301 } else { 302 Name = getSectionPrefixForGlobal(Kind); 303 } 304 305 if (GroupFunctionsByHotness) { 306 if (const Function *F = dyn_cast<Function>(GV)) { 307 if (ProfileSummary::isFunctionHot(F)) { 308 Name += getHotSectionPrefix(); 309 } else if (ProfileSummary::isFunctionUnlikely(F)) { 310 Name += getUnlikelySectionPrefix(); 311 } 312 } 313 } 314 315 if (EmitUniqueSection && UniqueSectionNames) { 316 Name.push_back('.'); 317 TM.getNameWithPrefix(Name, GV, Mang, true); 318 } 319 unsigned UniqueID = ~0; 320 if (EmitUniqueSection && !UniqueSectionNames) { 321 UniqueID = *NextUniqueID; 322 (*NextUniqueID)++; 323 } 324 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 325 EntrySize, Group, UniqueID); 326 } 327 328 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 329 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 330 const TargetMachine &TM) const { 331 unsigned Flags = getELFSectionFlags(Kind); 332 333 // If we have -ffunction-section or -fdata-section then we should emit the 334 // global value to a uniqued section specifically for it. 335 bool EmitUniqueSection = false; 336 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 337 if (Kind.isText()) 338 EmitUniqueSection = TM.getFunctionSections(); 339 else 340 EmitUniqueSection = TM.getDataSections(); 341 } 342 EmitUniqueSection |= GV->hasComdat(); 343 344 return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM, 345 EmitUniqueSection, Flags, &NextUniqueID); 346 } 347 348 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 349 const Function &F, Mangler &Mang, const TargetMachine &TM) const { 350 // If the function can be removed, produce a unique section so that 351 // the table doesn't prevent the removal. 352 const Comdat *C = F.getComdat(); 353 bool EmitUniqueSection = TM.getFunctionSections() || C; 354 if (!EmitUniqueSection) 355 return ReadOnlySection; 356 357 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 358 Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC, 359 &NextUniqueID); 360 } 361 362 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 363 bool UsesLabelDifference, const Function &F) const { 364 // We can always create relative relocations, so use another section 365 // that can be marked non-executable. 366 return false; 367 } 368 369 /// Given a mergeable constant with the specified size and relocation 370 /// information, return a section that it should be placed in. 371 MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 372 const DataLayout &DL, SectionKind Kind, const Constant *C, 373 unsigned &Align) const { 374 if (Kind.isMergeableConst4() && MergeableConst4Section) 375 return MergeableConst4Section; 376 if (Kind.isMergeableConst8() && MergeableConst8Section) 377 return MergeableConst8Section; 378 if (Kind.isMergeableConst16() && MergeableConst16Section) 379 return MergeableConst16Section; 380 if (Kind.isMergeableConst32() && MergeableConst32Section) 381 return MergeableConst32Section; 382 if (Kind.isReadOnly()) 383 return ReadOnlySection; 384 385 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 386 return DataRelROSection; 387 } 388 389 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 390 bool IsCtor, unsigned Priority, 391 const MCSymbol *KeySym) { 392 std::string Name; 393 unsigned Type; 394 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 395 StringRef COMDAT = KeySym ? KeySym->getName() : ""; 396 397 if (KeySym) 398 Flags |= ELF::SHF_GROUP; 399 400 if (UseInitArray) { 401 if (IsCtor) { 402 Type = ELF::SHT_INIT_ARRAY; 403 Name = ".init_array"; 404 } else { 405 Type = ELF::SHT_FINI_ARRAY; 406 Name = ".fini_array"; 407 } 408 if (Priority != 65535) { 409 Name += '.'; 410 Name += utostr(Priority); 411 } 412 } else { 413 // The default scheme is .ctor / .dtor, so we have to invert the priority 414 // numbering. 415 if (IsCtor) 416 Name = ".ctors"; 417 else 418 Name = ".dtors"; 419 if (Priority != 65535) { 420 Name += '.'; 421 Name += utostr(65535 - Priority); 422 } 423 Type = ELF::SHT_PROGBITS; 424 } 425 426 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 427 } 428 429 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 430 unsigned Priority, const MCSymbol *KeySym) const { 431 return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 432 KeySym); 433 } 434 435 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 436 unsigned Priority, const MCSymbol *KeySym) const { 437 return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 438 KeySym); 439 } 440 441 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 442 const GlobalValue *LHS, const GlobalValue *RHS, Mangler &Mang, 443 const TargetMachine &TM) const { 444 // We may only use a PLT-relative relocation to refer to unnamed_addr 445 // functions. 446 if (!LHS->hasUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 447 return nullptr; 448 449 // Basic sanity checks. 450 if (LHS->getType()->getPointerAddressSpace() != 0 || 451 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 452 RHS->isThreadLocal()) 453 return nullptr; 454 455 return MCBinaryExpr::createSub( 456 MCSymbolRefExpr::create(TM.getSymbol(LHS, Mang), PLTRelativeVariantKind, 457 getContext()), 458 MCSymbolRefExpr::create(TM.getSymbol(RHS, Mang), getContext()), 459 getContext()); 460 } 461 462 void 463 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 464 UseInitArray = UseInitArray_; 465 if (!UseInitArray) 466 return; 467 468 StaticCtorSection = getContext().getELFSection( 469 ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 470 StaticDtorSection = getContext().getELFSection( 471 ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); 472 } 473 474 //===----------------------------------------------------------------------===// 475 // MachO 476 //===----------------------------------------------------------------------===// 477 478 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 479 : TargetLoweringObjectFile() { 480 SupportIndirectSymViaGOTPCRel = true; 481 } 482 483 /// emitModuleFlags - Perform code emission for module flags. 484 void TargetLoweringObjectFileMachO:: 485 emitModuleFlags(MCStreamer &Streamer, 486 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 487 Mangler &Mang, const TargetMachine &TM) const { 488 unsigned VersionVal = 0; 489 unsigned ImageInfoFlags = 0; 490 MDNode *LinkerOptions = nullptr; 491 StringRef SectionVal; 492 493 for (const auto &MFE : ModuleFlags) { 494 // Ignore flags with 'Require' behavior. 495 if (MFE.Behavior == Module::Require) 496 continue; 497 498 StringRef Key = MFE.Key->getString(); 499 Metadata *Val = MFE.Val; 500 501 if (Key == "Objective-C Image Info Version") { 502 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue(); 503 } else if (Key == "Objective-C Garbage Collection" || 504 Key == "Objective-C GC Only" || 505 Key == "Objective-C Is Simulated" || 506 Key == "Objective-C Class Properties" || 507 Key == "Objective-C Image Swift Version") { 508 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue(); 509 } else if (Key == "Objective-C Image Info Section") { 510 SectionVal = cast<MDString>(Val)->getString(); 511 } else if (Key == "Linker Options") { 512 LinkerOptions = cast<MDNode>(Val); 513 } 514 } 515 516 // Emit the linker options if present. 517 if (LinkerOptions) { 518 for (const auto &Option : LinkerOptions->operands()) { 519 SmallVector<std::string, 4> StrOptions; 520 for (const auto &Piece : cast<MDNode>(Option)->operands()) 521 StrOptions.push_back(cast<MDString>(Piece)->getString()); 522 Streamer.EmitLinkerOptions(StrOptions); 523 } 524 } 525 526 // The section is mandatory. If we don't have it, then we don't have GC info. 527 if (SectionVal.empty()) return; 528 529 StringRef Segment, Section; 530 unsigned TAA = 0, StubSize = 0; 531 bool TAAParsed; 532 std::string ErrorCode = 533 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 534 TAA, TAAParsed, StubSize); 535 if (!ErrorCode.empty()) 536 // If invalid, report the error with report_fatal_error. 537 report_fatal_error("Invalid section specifier '" + Section + "': " + 538 ErrorCode + "."); 539 540 // Get the section. 541 MCSectionMachO *S = getContext().getMachOSection( 542 Segment, Section, TAA, StubSize, SectionKind::getData()); 543 Streamer.SwitchSection(S); 544 Streamer.EmitLabel(getContext(). 545 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 546 Streamer.EmitIntValue(VersionVal, 4); 547 Streamer.EmitIntValue(ImageInfoFlags, 4); 548 Streamer.AddBlankLine(); 549 } 550 551 static void checkMachOComdat(const GlobalValue *GV) { 552 const Comdat *C = GV->getComdat(); 553 if (!C) 554 return; 555 556 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 557 "' cannot be lowered."); 558 } 559 560 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 561 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 562 const TargetMachine &TM) const { 563 // Parse the section specifier and create it if valid. 564 StringRef Segment, Section; 565 unsigned TAA = 0, StubSize = 0; 566 bool TAAParsed; 567 568 checkMachOComdat(GV); 569 570 std::string ErrorCode = 571 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 572 TAA, TAAParsed, StubSize); 573 if (!ErrorCode.empty()) { 574 // If invalid, report the error with report_fatal_error. 575 report_fatal_error("Global variable '" + GV->getName() + 576 "' has an invalid section specifier '" + 577 GV->getSection() + "': " + ErrorCode + "."); 578 } 579 580 // Get the section. 581 MCSectionMachO *S = 582 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 583 584 // If TAA wasn't set by ParseSectionSpecifier() above, 585 // use the value returned by getMachOSection() as a default. 586 if (!TAAParsed) 587 TAA = S->getTypeAndAttributes(); 588 589 // Okay, now that we got the section, verify that the TAA & StubSize agree. 590 // If the user declared multiple globals with different section flags, we need 591 // to reject it here. 592 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 593 // If invalid, report the error with report_fatal_error. 594 report_fatal_error("Global variable '" + GV->getName() + 595 "' section type or attributes does not match previous" 596 " section specifier"); 597 } 598 599 return S; 600 } 601 602 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 603 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 604 const TargetMachine &TM) const { 605 checkMachOComdat(GV); 606 607 // Handle thread local data. 608 if (Kind.isThreadBSS()) return TLSBSSSection; 609 if (Kind.isThreadData()) return TLSDataSection; 610 611 if (Kind.isText()) 612 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 613 614 // If this is weak/linkonce, put this in a coalescable section, either in text 615 // or data depending on if it is writable. 616 if (GV->isWeakForLinker()) { 617 if (Kind.isReadOnly()) 618 return ConstTextCoalSection; 619 return DataCoalSection; 620 } 621 622 // FIXME: Alignment check should be handled by section classifier. 623 if (Kind.isMergeable1ByteCString() && 624 GV->getParent()->getDataLayout().getPreferredAlignment( 625 cast<GlobalVariable>(GV)) < 32) 626 return CStringSection; 627 628 // Do not put 16-bit arrays in the UString section if they have an 629 // externally visible label, this runs into issues with certain linker 630 // versions. 631 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 632 GV->getParent()->getDataLayout().getPreferredAlignment( 633 cast<GlobalVariable>(GV)) < 32) 634 return UStringSection; 635 636 // With MachO only variables whose corresponding symbol starts with 'l' or 637 // 'L' can be merged, so we only try merging GVs with private linkage. 638 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) { 639 if (Kind.isMergeableConst4()) 640 return FourByteConstantSection; 641 if (Kind.isMergeableConst8()) 642 return EightByteConstantSection; 643 if (Kind.isMergeableConst16()) 644 return SixteenByteConstantSection; 645 } 646 647 // Otherwise, if it is readonly, but not something we can specially optimize, 648 // just drop it in .const. 649 if (Kind.isReadOnly()) 650 return ReadOnlySection; 651 652 // If this is marked const, put it into a const section. But if the dynamic 653 // linker needs to write to it, put it in the data segment. 654 if (Kind.isReadOnlyWithRel()) 655 return ConstDataSection; 656 657 // Put zero initialized globals with strong external linkage in the 658 // DATA, __common section with the .zerofill directive. 659 if (Kind.isBSSExtern()) 660 return DataCommonSection; 661 662 // Put zero initialized globals with local linkage in __DATA,__bss directive 663 // with the .zerofill directive (aka .lcomm). 664 if (Kind.isBSSLocal()) 665 return DataBSSSection; 666 667 // Otherwise, just drop the variable in the normal data section. 668 return DataSection; 669 } 670 671 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 672 const DataLayout &DL, SectionKind Kind, const Constant *C, 673 unsigned &Align) const { 674 // If this constant requires a relocation, we have to put it in the data 675 // segment, not in the text segment. 676 if (Kind.isData() || Kind.isReadOnlyWithRel()) 677 return ConstDataSection; 678 679 if (Kind.isMergeableConst4()) 680 return FourByteConstantSection; 681 if (Kind.isMergeableConst8()) 682 return EightByteConstantSection; 683 if (Kind.isMergeableConst16()) 684 return SixteenByteConstantSection; 685 return ReadOnlySection; // .const 686 } 687 688 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 689 const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 690 const TargetMachine &TM, MachineModuleInfo *MMI, 691 MCStreamer &Streamer) const { 692 // The mach-o version of this method defaults to returning a stub reference. 693 694 if (Encoding & DW_EH_PE_indirect) { 695 MachineModuleInfoMachO &MachOMMI = 696 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 697 698 MCSymbol *SSym = 699 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 700 701 // Add information about the stub reference to MachOMMI so that the stub 702 // gets emitted by the asmprinter. 703 MachineModuleInfoImpl::StubValueTy &StubSym = 704 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : 705 MachOMMI.getGVStubEntry(SSym); 706 if (!StubSym.getPointer()) { 707 MCSymbol *Sym = TM.getSymbol(GV, Mang); 708 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 709 } 710 711 return TargetLoweringObjectFile:: 712 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 713 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 714 } 715 716 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, 717 TM, MMI, Streamer); 718 } 719 720 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 721 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 722 MachineModuleInfo *MMI) const { 723 // The mach-o version of this method defaults to returning a stub reference. 724 MachineModuleInfoMachO &MachOMMI = 725 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 726 727 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 728 729 // Add information about the stub reference to MachOMMI so that the stub 730 // gets emitted by the asmprinter. 731 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 732 if (!StubSym.getPointer()) { 733 MCSymbol *Sym = TM.getSymbol(GV, Mang); 734 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 735 } 736 737 return SSym; 738 } 739 740 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 741 const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 742 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 743 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 744 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 745 // through a non_lazy_ptr stub instead. One advantage is that it allows the 746 // computation of deltas to final external symbols. Example: 747 // 748 // _extgotequiv: 749 // .long _extfoo 750 // 751 // _delta: 752 // .long _extgotequiv-_delta 753 // 754 // is transformed to: 755 // 756 // _delta: 757 // .long L_extfoo$non_lazy_ptr-(_delta+0) 758 // 759 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 760 // L_extfoo$non_lazy_ptr: 761 // .indirect_symbol _extfoo 762 // .long 0 763 // 764 MachineModuleInfoMachO &MachOMMI = 765 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 766 MCContext &Ctx = getContext(); 767 768 // The offset must consider the original displacement from the base symbol 769 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 770 Offset = -MV.getConstant(); 771 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 772 773 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 774 // non_lazy_ptr stubs. 775 SmallString<128> Name; 776 StringRef Suffix = "$non_lazy_ptr"; 777 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 778 Name += Sym->getName(); 779 Name += Suffix; 780 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 781 782 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 783 if (!StubSym.getPointer()) 784 StubSym = MachineModuleInfoImpl:: 785 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); 786 787 const MCExpr *BSymExpr = 788 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 789 const MCExpr *LHS = 790 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 791 792 if (!Offset) 793 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 794 795 const MCExpr *RHS = 796 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 797 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 798 } 799 800 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 801 const MCSection &Section) { 802 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 803 return true; 804 805 // If it is not dead stripped, it is safe to use private labels. 806 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 807 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 808 return true; 809 810 return false; 811 } 812 813 void TargetLoweringObjectFileMachO::getNameWithPrefix( 814 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 815 const TargetMachine &TM) const { 816 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); 817 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); 818 bool CannotUsePrivateLabel = 819 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 820 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 821 } 822 823 //===----------------------------------------------------------------------===// 824 // COFF 825 //===----------------------------------------------------------------------===// 826 827 static unsigned 828 getCOFFSectionFlags(SectionKind K) { 829 unsigned Flags = 0; 830 831 if (K.isMetadata()) 832 Flags |= 833 COFF::IMAGE_SCN_MEM_DISCARDABLE; 834 else if (K.isText()) 835 Flags |= 836 COFF::IMAGE_SCN_MEM_EXECUTE | 837 COFF::IMAGE_SCN_MEM_READ | 838 COFF::IMAGE_SCN_CNT_CODE; 839 else if (K.isBSS()) 840 Flags |= 841 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 842 COFF::IMAGE_SCN_MEM_READ | 843 COFF::IMAGE_SCN_MEM_WRITE; 844 else if (K.isThreadLocal()) 845 Flags |= 846 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 847 COFF::IMAGE_SCN_MEM_READ | 848 COFF::IMAGE_SCN_MEM_WRITE; 849 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 850 Flags |= 851 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 852 COFF::IMAGE_SCN_MEM_READ; 853 else if (K.isWriteable()) 854 Flags |= 855 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 856 COFF::IMAGE_SCN_MEM_READ | 857 COFF::IMAGE_SCN_MEM_WRITE; 858 859 return Flags; 860 } 861 862 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 863 const Comdat *C = GV->getComdat(); 864 assert(C && "expected GV to have a Comdat!"); 865 866 StringRef ComdatGVName = C->getName(); 867 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 868 if (!ComdatGV) 869 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 870 "' does not exist."); 871 872 if (ComdatGV->getComdat() != C) 873 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 874 "' is not a key for its COMDAT."); 875 876 return ComdatGV; 877 } 878 879 static int getSelectionForCOFF(const GlobalValue *GV) { 880 if (const Comdat *C = GV->getComdat()) { 881 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 882 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 883 ComdatKey = GA->getBaseObject(); 884 if (ComdatKey == GV) { 885 switch (C->getSelectionKind()) { 886 case Comdat::Any: 887 return COFF::IMAGE_COMDAT_SELECT_ANY; 888 case Comdat::ExactMatch: 889 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 890 case Comdat::Largest: 891 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 892 case Comdat::NoDuplicates: 893 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 894 case Comdat::SameSize: 895 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 896 } 897 } else { 898 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 899 } 900 } 901 return 0; 902 } 903 904 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 905 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 906 const TargetMachine &TM) const { 907 int Selection = 0; 908 unsigned Characteristics = getCOFFSectionFlags(Kind); 909 StringRef Name = GV->getSection(); 910 StringRef COMDATSymName = ""; 911 if (GV->hasComdat()) { 912 Selection = getSelectionForCOFF(GV); 913 const GlobalValue *ComdatGV; 914 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 915 ComdatGV = getComdatGVForCOFF(GV); 916 else 917 ComdatGV = GV; 918 919 if (!ComdatGV->hasPrivateLinkage()) { 920 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 921 COMDATSymName = Sym->getName(); 922 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 923 } else { 924 Selection = 0; 925 } 926 } 927 return getContext().getCOFFSection(Name, 928 Characteristics, 929 Kind, 930 COMDATSymName, 931 Selection); 932 } 933 934 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 935 if (Kind.isText()) 936 return ".text"; 937 if (Kind.isBSS()) 938 return ".bss"; 939 if (Kind.isThreadLocal()) 940 return ".tls$"; 941 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 942 return ".rdata"; 943 return ".data"; 944 } 945 946 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 947 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 948 const TargetMachine &TM) const { 949 // If we have -ffunction-sections then we should emit the global value to a 950 // uniqued section specifically for it. 951 bool EmitUniquedSection; 952 if (Kind.isText()) 953 EmitUniquedSection = TM.getFunctionSections(); 954 else 955 EmitUniquedSection = TM.getDataSections(); 956 957 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) { 958 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 959 unsigned Characteristics = getCOFFSectionFlags(Kind); 960 961 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 962 int Selection = getSelectionForCOFF(GV); 963 if (!Selection) 964 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 965 const GlobalValue *ComdatGV; 966 if (GV->hasComdat()) 967 ComdatGV = getComdatGVForCOFF(GV); 968 else 969 ComdatGV = GV; 970 971 if (!ComdatGV->hasPrivateLinkage()) { 972 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 973 StringRef COMDATSymName = Sym->getName(); 974 return getContext().getCOFFSection(Name, Characteristics, Kind, 975 COMDATSymName, Selection); 976 } else { 977 SmallString<256> TmpData; 978 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true); 979 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 980 Selection); 981 } 982 } 983 984 if (Kind.isText()) 985 return TextSection; 986 987 if (Kind.isThreadLocal()) 988 return TLSDataSection; 989 990 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 991 return ReadOnlySection; 992 993 // Note: we claim that common symbols are put in BSSSection, but they are 994 // really emitted with the magic .comm directive, which creates a symbol table 995 // entry but not a section. 996 if (Kind.isBSS() || Kind.isCommon()) 997 return BSSSection; 998 999 return DataSection; 1000 } 1001 1002 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1003 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 1004 const TargetMachine &TM) const { 1005 bool CannotUsePrivateLabel = false; 1006 if (GV->hasPrivateLinkage() && 1007 ((isa<Function>(GV) && TM.getFunctionSections()) || 1008 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1009 CannotUsePrivateLabel = true; 1010 1011 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1012 } 1013 1014 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1015 const Function &F, Mangler &Mang, const TargetMachine &TM) const { 1016 // If the function can be removed, produce a unique section so that 1017 // the table doesn't prevent the removal. 1018 const Comdat *C = F.getComdat(); 1019 bool EmitUniqueSection = TM.getFunctionSections() || C; 1020 if (!EmitUniqueSection) 1021 return ReadOnlySection; 1022 1023 // FIXME: we should produce a symbol for F instead. 1024 if (F.hasPrivateLinkage()) 1025 return ReadOnlySection; 1026 1027 MCSymbol *Sym = TM.getSymbol(&F, Mang); 1028 StringRef COMDATSymName = Sym->getName(); 1029 1030 SectionKind Kind = SectionKind::getReadOnly(); 1031 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 1032 unsigned Characteristics = getCOFFSectionFlags(Kind); 1033 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1034 1035 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1036 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); 1037 } 1038 1039 void TargetLoweringObjectFileCOFF:: 1040 emitModuleFlags(MCStreamer &Streamer, 1041 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 1042 Mangler &Mang, const TargetMachine &TM) const { 1043 MDNode *LinkerOptions = nullptr; 1044 1045 for (const auto &MFE : ModuleFlags) { 1046 StringRef Key = MFE.Key->getString(); 1047 if (Key == "Linker Options") 1048 LinkerOptions = cast<MDNode>(MFE.Val); 1049 } 1050 1051 if (LinkerOptions) { 1052 // Emit the linker options to the linker .drectve section. According to the 1053 // spec, this section is a space-separated string containing flags for 1054 // linker. 1055 MCSection *Sec = getDrectveSection(); 1056 Streamer.SwitchSection(Sec); 1057 for (const auto &Option : LinkerOptions->operands()) { 1058 for (const auto &Piece : cast<MDNode>(Option)->operands()) { 1059 // Lead with a space for consistency with our dllexport implementation. 1060 std::string Directive(" "); 1061 Directive.append(cast<MDString>(Piece)->getString()); 1062 Streamer.EmitBytes(Directive); 1063 } 1064 } 1065 } 1066 } 1067 1068 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1069 unsigned Priority, const MCSymbol *KeySym) const { 1070 return getContext().getAssociativeCOFFSection( 1071 cast<MCSectionCOFF>(StaticCtorSection), KeySym); 1072 } 1073 1074 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1075 unsigned Priority, const MCSymbol *KeySym) const { 1076 return getContext().getAssociativeCOFFSection( 1077 cast<MCSectionCOFF>(StaticDtorSection), KeySym); 1078 } 1079 1080 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 1081 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const { 1082 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration()) 1083 return; 1084 1085 const Triple &TT = getTargetTriple(); 1086 1087 if (TT.isKnownWindowsMSVCEnvironment()) 1088 OS << " /EXPORT:"; 1089 else 1090 OS << " -export:"; 1091 1092 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) { 1093 std::string Flag; 1094 raw_string_ostream FlagOS(Flag); 1095 Mang.getNameWithPrefix(FlagOS, GV, false); 1096 FlagOS.flush(); 1097 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) 1098 OS << Flag.substr(1); 1099 else 1100 OS << Flag; 1101 } else { 1102 Mang.getNameWithPrefix(OS, GV, false); 1103 } 1104 1105 if (!GV->getValueType()->isFunctionTy()) { 1106 if (TT.isKnownWindowsMSVCEnvironment()) 1107 OS << ",DATA"; 1108 else 1109 OS << ",data"; 1110 } 1111 } 1112