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 (ArrayRef<Module::ModuleFlagEntry>::iterator 494 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 495 const Module::ModuleFlagEntry &MFE = *i; 496 497 // Ignore flags with 'Require' behavior. 498 if (MFE.Behavior == Module::Require) 499 continue; 500 501 StringRef Key = MFE.Key->getString(); 502 Metadata *Val = MFE.Val; 503 504 if (Key == "Objective-C Image Info Version") { 505 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue(); 506 } else if (Key == "Objective-C Garbage Collection" || 507 Key == "Objective-C GC Only" || 508 Key == "Objective-C Is Simulated" || 509 Key == "Objective-C Class Properties" || 510 Key == "Objective-C Image Swift Version") { 511 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue(); 512 } else if (Key == "Objective-C Image Info Section") { 513 SectionVal = cast<MDString>(Val)->getString(); 514 } else if (Key == "Linker Options") { 515 LinkerOptions = cast<MDNode>(Val); 516 } 517 } 518 519 // Emit the linker options if present. 520 if (LinkerOptions) { 521 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 522 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 523 SmallVector<std::string, 4> StrOptions; 524 525 // Convert to strings. 526 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 527 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 528 StrOptions.push_back(MDOption->getString()); 529 } 530 531 Streamer.EmitLinkerOptions(StrOptions); 532 } 533 } 534 535 // The section is mandatory. If we don't have it, then we don't have GC info. 536 if (SectionVal.empty()) return; 537 538 StringRef Segment, Section; 539 unsigned TAA = 0, StubSize = 0; 540 bool TAAParsed; 541 std::string ErrorCode = 542 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 543 TAA, TAAParsed, StubSize); 544 if (!ErrorCode.empty()) 545 // If invalid, report the error with report_fatal_error. 546 report_fatal_error("Invalid section specifier '" + Section + "': " + 547 ErrorCode + "."); 548 549 // Get the section. 550 MCSectionMachO *S = getContext().getMachOSection( 551 Segment, Section, TAA, StubSize, SectionKind::getData()); 552 Streamer.SwitchSection(S); 553 Streamer.EmitLabel(getContext(). 554 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 555 Streamer.EmitIntValue(VersionVal, 4); 556 Streamer.EmitIntValue(ImageInfoFlags, 4); 557 Streamer.AddBlankLine(); 558 } 559 560 static void checkMachOComdat(const GlobalValue *GV) { 561 const Comdat *C = GV->getComdat(); 562 if (!C) 563 return; 564 565 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 566 "' cannot be lowered."); 567 } 568 569 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 570 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 571 const TargetMachine &TM) const { 572 // Parse the section specifier and create it if valid. 573 StringRef Segment, Section; 574 unsigned TAA = 0, StubSize = 0; 575 bool TAAParsed; 576 577 checkMachOComdat(GV); 578 579 std::string ErrorCode = 580 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, 581 TAA, TAAParsed, StubSize); 582 if (!ErrorCode.empty()) { 583 // If invalid, report the error with report_fatal_error. 584 report_fatal_error("Global variable '" + GV->getName() + 585 "' has an invalid section specifier '" + 586 GV->getSection() + "': " + ErrorCode + "."); 587 } 588 589 // Get the section. 590 MCSectionMachO *S = 591 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 592 593 // If TAA wasn't set by ParseSectionSpecifier() above, 594 // use the value returned by getMachOSection() as a default. 595 if (!TAAParsed) 596 TAA = S->getTypeAndAttributes(); 597 598 // Okay, now that we got the section, verify that the TAA & StubSize agree. 599 // If the user declared multiple globals with different section flags, we need 600 // to reject it here. 601 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 602 // If invalid, report the error with report_fatal_error. 603 report_fatal_error("Global variable '" + GV->getName() + 604 "' section type or attributes does not match previous" 605 " section specifier"); 606 } 607 608 return S; 609 } 610 611 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 612 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 613 const TargetMachine &TM) const { 614 checkMachOComdat(GV); 615 616 // Handle thread local data. 617 if (Kind.isThreadBSS()) return TLSBSSSection; 618 if (Kind.isThreadData()) return TLSDataSection; 619 620 if (Kind.isText()) 621 return GV->isWeakForLinker() ? TextCoalSection : TextSection; 622 623 // If this is weak/linkonce, put this in a coalescable section, either in text 624 // or data depending on if it is writable. 625 if (GV->isWeakForLinker()) { 626 if (Kind.isReadOnly()) 627 return ConstTextCoalSection; 628 return DataCoalSection; 629 } 630 631 // FIXME: Alignment check should be handled by section classifier. 632 if (Kind.isMergeable1ByteCString() && 633 GV->getParent()->getDataLayout().getPreferredAlignment( 634 cast<GlobalVariable>(GV)) < 32) 635 return CStringSection; 636 637 // Do not put 16-bit arrays in the UString section if they have an 638 // externally visible label, this runs into issues with certain linker 639 // versions. 640 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && 641 GV->getParent()->getDataLayout().getPreferredAlignment( 642 cast<GlobalVariable>(GV)) < 32) 643 return UStringSection; 644 645 // With MachO only variables whose corresponding symbol starts with 'l' or 646 // 'L' can be merged, so we only try merging GVs with private linkage. 647 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) { 648 if (Kind.isMergeableConst4()) 649 return FourByteConstantSection; 650 if (Kind.isMergeableConst8()) 651 return EightByteConstantSection; 652 if (Kind.isMergeableConst16()) 653 return SixteenByteConstantSection; 654 } 655 656 // Otherwise, if it is readonly, but not something we can specially optimize, 657 // just drop it in .const. 658 if (Kind.isReadOnly()) 659 return ReadOnlySection; 660 661 // If this is marked const, put it into a const section. But if the dynamic 662 // linker needs to write to it, put it in the data segment. 663 if (Kind.isReadOnlyWithRel()) 664 return ConstDataSection; 665 666 // Put zero initialized globals with strong external linkage in the 667 // DATA, __common section with the .zerofill directive. 668 if (Kind.isBSSExtern()) 669 return DataCommonSection; 670 671 // Put zero initialized globals with local linkage in __DATA,__bss directive 672 // with the .zerofill directive (aka .lcomm). 673 if (Kind.isBSSLocal()) 674 return DataBSSSection; 675 676 // Otherwise, just drop the variable in the normal data section. 677 return DataSection; 678 } 679 680 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 681 const DataLayout &DL, SectionKind Kind, const Constant *C, 682 unsigned &Align) const { 683 // If this constant requires a relocation, we have to put it in the data 684 // segment, not in the text segment. 685 if (Kind.isData() || Kind.isReadOnlyWithRel()) 686 return ConstDataSection; 687 688 if (Kind.isMergeableConst4()) 689 return FourByteConstantSection; 690 if (Kind.isMergeableConst8()) 691 return EightByteConstantSection; 692 if (Kind.isMergeableConst16()) 693 return SixteenByteConstantSection; 694 return ReadOnlySection; // .const 695 } 696 697 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 698 const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 699 const TargetMachine &TM, MachineModuleInfo *MMI, 700 MCStreamer &Streamer) const { 701 // The mach-o version of this method defaults to returning a stub reference. 702 703 if (Encoding & DW_EH_PE_indirect) { 704 MachineModuleInfoMachO &MachOMMI = 705 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 706 707 MCSymbol *SSym = 708 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 709 710 // Add information about the stub reference to MachOMMI so that the stub 711 // gets emitted by the asmprinter. 712 MachineModuleInfoImpl::StubValueTy &StubSym = 713 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : 714 MachOMMI.getGVStubEntry(SSym); 715 if (!StubSym.getPointer()) { 716 MCSymbol *Sym = TM.getSymbol(GV, Mang); 717 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 718 } 719 720 return TargetLoweringObjectFile:: 721 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 722 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); 723 } 724 725 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, 726 TM, MMI, Streamer); 727 } 728 729 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 730 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 731 MachineModuleInfo *MMI) const { 732 // The mach-o version of this method defaults to returning a stub reference. 733 MachineModuleInfoMachO &MachOMMI = 734 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 735 736 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); 737 738 // Add information about the stub reference to MachOMMI so that the stub 739 // gets emitted by the asmprinter. 740 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 741 if (!StubSym.getPointer()) { 742 MCSymbol *Sym = TM.getSymbol(GV, Mang); 743 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 744 } 745 746 return SSym; 747 } 748 749 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 750 const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 751 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 752 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 753 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 754 // through a non_lazy_ptr stub instead. One advantage is that it allows the 755 // computation of deltas to final external symbols. Example: 756 // 757 // _extgotequiv: 758 // .long _extfoo 759 // 760 // _delta: 761 // .long _extgotequiv-_delta 762 // 763 // is transformed to: 764 // 765 // _delta: 766 // .long L_extfoo$non_lazy_ptr-(_delta+0) 767 // 768 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 769 // L_extfoo$non_lazy_ptr: 770 // .indirect_symbol _extfoo 771 // .long 0 772 // 773 MachineModuleInfoMachO &MachOMMI = 774 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 775 MCContext &Ctx = getContext(); 776 777 // The offset must consider the original displacement from the base symbol 778 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 779 Offset = -MV.getConstant(); 780 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 781 782 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 783 // non_lazy_ptr stubs. 784 SmallString<128> Name; 785 StringRef Suffix = "$non_lazy_ptr"; 786 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 787 Name += Sym->getName(); 788 Name += Suffix; 789 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 790 791 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 792 if (!StubSym.getPointer()) 793 StubSym = MachineModuleInfoImpl:: 794 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); 795 796 const MCExpr *BSymExpr = 797 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 798 const MCExpr *LHS = 799 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 800 801 if (!Offset) 802 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 803 804 const MCExpr *RHS = 805 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 806 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 807 } 808 809 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 810 const MCSection &Section) { 811 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 812 return true; 813 814 // If it is not dead stripped, it is safe to use private labels. 815 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 816 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 817 return true; 818 819 return false; 820 } 821 822 void TargetLoweringObjectFileMachO::getNameWithPrefix( 823 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 824 const TargetMachine &TM) const { 825 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); 826 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); 827 bool CannotUsePrivateLabel = 828 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 829 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 830 } 831 832 //===----------------------------------------------------------------------===// 833 // COFF 834 //===----------------------------------------------------------------------===// 835 836 static unsigned 837 getCOFFSectionFlags(SectionKind K) { 838 unsigned Flags = 0; 839 840 if (K.isMetadata()) 841 Flags |= 842 COFF::IMAGE_SCN_MEM_DISCARDABLE; 843 else if (K.isText()) 844 Flags |= 845 COFF::IMAGE_SCN_MEM_EXECUTE | 846 COFF::IMAGE_SCN_MEM_READ | 847 COFF::IMAGE_SCN_CNT_CODE; 848 else if (K.isBSS()) 849 Flags |= 850 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 851 COFF::IMAGE_SCN_MEM_READ | 852 COFF::IMAGE_SCN_MEM_WRITE; 853 else if (K.isThreadLocal()) 854 Flags |= 855 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 856 COFF::IMAGE_SCN_MEM_READ | 857 COFF::IMAGE_SCN_MEM_WRITE; 858 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 859 Flags |= 860 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 861 COFF::IMAGE_SCN_MEM_READ; 862 else if (K.isWriteable()) 863 Flags |= 864 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 865 COFF::IMAGE_SCN_MEM_READ | 866 COFF::IMAGE_SCN_MEM_WRITE; 867 868 return Flags; 869 } 870 871 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 872 const Comdat *C = GV->getComdat(); 873 assert(C && "expected GV to have a Comdat!"); 874 875 StringRef ComdatGVName = C->getName(); 876 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 877 if (!ComdatGV) 878 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 879 "' does not exist."); 880 881 if (ComdatGV->getComdat() != C) 882 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 883 "' is not a key for its COMDAT."); 884 885 return ComdatGV; 886 } 887 888 static int getSelectionForCOFF(const GlobalValue *GV) { 889 if (const Comdat *C = GV->getComdat()) { 890 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 891 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 892 ComdatKey = GA->getBaseObject(); 893 if (ComdatKey == GV) { 894 switch (C->getSelectionKind()) { 895 case Comdat::Any: 896 return COFF::IMAGE_COMDAT_SELECT_ANY; 897 case Comdat::ExactMatch: 898 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 899 case Comdat::Largest: 900 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 901 case Comdat::NoDuplicates: 902 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 903 case Comdat::SameSize: 904 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 905 } 906 } else { 907 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 908 } 909 } 910 return 0; 911 } 912 913 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 914 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 915 const TargetMachine &TM) const { 916 int Selection = 0; 917 unsigned Characteristics = getCOFFSectionFlags(Kind); 918 StringRef Name = GV->getSection(); 919 StringRef COMDATSymName = ""; 920 if (GV->hasComdat()) { 921 Selection = getSelectionForCOFF(GV); 922 const GlobalValue *ComdatGV; 923 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 924 ComdatGV = getComdatGVForCOFF(GV); 925 else 926 ComdatGV = GV; 927 928 if (!ComdatGV->hasPrivateLinkage()) { 929 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 930 COMDATSymName = Sym->getName(); 931 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 932 } else { 933 Selection = 0; 934 } 935 } 936 return getContext().getCOFFSection(Name, 937 Characteristics, 938 Kind, 939 COMDATSymName, 940 Selection); 941 } 942 943 static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 944 if (Kind.isText()) 945 return ".text"; 946 if (Kind.isBSS()) 947 return ".bss"; 948 if (Kind.isThreadLocal()) 949 return ".tls$"; 950 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 951 return ".rdata"; 952 return ".data"; 953 } 954 955 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 956 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 957 const TargetMachine &TM) const { 958 // If we have -ffunction-sections then we should emit the global value to a 959 // uniqued section specifically for it. 960 bool EmitUniquedSection; 961 if (Kind.isText()) 962 EmitUniquedSection = TM.getFunctionSections(); 963 else 964 EmitUniquedSection = TM.getDataSections(); 965 966 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) { 967 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 968 unsigned Characteristics = getCOFFSectionFlags(Kind); 969 970 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 971 int Selection = getSelectionForCOFF(GV); 972 if (!Selection) 973 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 974 const GlobalValue *ComdatGV; 975 if (GV->hasComdat()) 976 ComdatGV = getComdatGVForCOFF(GV); 977 else 978 ComdatGV = GV; 979 980 if (!ComdatGV->hasPrivateLinkage()) { 981 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); 982 StringRef COMDATSymName = Sym->getName(); 983 return getContext().getCOFFSection(Name, Characteristics, Kind, 984 COMDATSymName, Selection); 985 } else { 986 SmallString<256> TmpData; 987 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true); 988 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 989 Selection); 990 } 991 } 992 993 if (Kind.isText()) 994 return TextSection; 995 996 if (Kind.isThreadLocal()) 997 return TLSDataSection; 998 999 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1000 return ReadOnlySection; 1001 1002 // Note: we claim that common symbols are put in BSSSection, but they are 1003 // really emitted with the magic .comm directive, which creates a symbol table 1004 // entry but not a section. 1005 if (Kind.isBSS() || Kind.isCommon()) 1006 return BSSSection; 1007 1008 return DataSection; 1009 } 1010 1011 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1012 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, 1013 const TargetMachine &TM) const { 1014 bool CannotUsePrivateLabel = false; 1015 if (GV->hasPrivateLinkage() && 1016 ((isa<Function>(GV) && TM.getFunctionSections()) || 1017 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1018 CannotUsePrivateLabel = true; 1019 1020 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1021 } 1022 1023 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1024 const Function &F, Mangler &Mang, const TargetMachine &TM) const { 1025 // If the function can be removed, produce a unique section so that 1026 // the table doesn't prevent the removal. 1027 const Comdat *C = F.getComdat(); 1028 bool EmitUniqueSection = TM.getFunctionSections() || C; 1029 if (!EmitUniqueSection) 1030 return ReadOnlySection; 1031 1032 // FIXME: we should produce a symbol for F instead. 1033 if (F.hasPrivateLinkage()) 1034 return ReadOnlySection; 1035 1036 MCSymbol *Sym = TM.getSymbol(&F, Mang); 1037 StringRef COMDATSymName = Sym->getName(); 1038 1039 SectionKind Kind = SectionKind::getReadOnly(); 1040 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); 1041 unsigned Characteristics = getCOFFSectionFlags(Kind); 1042 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1043 1044 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1045 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); 1046 } 1047 1048 void TargetLoweringObjectFileCOFF:: 1049 emitModuleFlags(MCStreamer &Streamer, 1050 ArrayRef<Module::ModuleFlagEntry> ModuleFlags, 1051 Mangler &Mang, const TargetMachine &TM) const { 1052 MDNode *LinkerOptions = nullptr; 1053 1054 // Look for the "Linker Options" flag, since it's the only one we support. 1055 for (ArrayRef<Module::ModuleFlagEntry>::iterator 1056 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { 1057 const Module::ModuleFlagEntry &MFE = *i; 1058 StringRef Key = MFE.Key->getString(); 1059 Metadata *Val = MFE.Val; 1060 if (Key == "Linker Options") { 1061 LinkerOptions = cast<MDNode>(Val); 1062 break; 1063 } 1064 } 1065 if (!LinkerOptions) 1066 return; 1067 1068 // Emit the linker options to the linker .drectve section. According to the 1069 // spec, this section is a space-separated string containing flags for linker. 1070 MCSection *Sec = getDrectveSection(); 1071 Streamer.SwitchSection(Sec); 1072 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { 1073 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); 1074 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { 1075 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); 1076 // Lead with a space for consistency with our dllexport implementation. 1077 std::string Directive(" "); 1078 Directive.append(MDOption->getString()); 1079 Streamer.EmitBytes(Directive); 1080 } 1081 } 1082 } 1083 1084 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1085 unsigned Priority, const MCSymbol *KeySym) const { 1086 return getContext().getAssociativeCOFFSection( 1087 cast<MCSectionCOFF>(StaticCtorSection), KeySym); 1088 } 1089 1090 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1091 unsigned Priority, const MCSymbol *KeySym) const { 1092 return getContext().getAssociativeCOFFSection( 1093 cast<MCSectionCOFF>(StaticDtorSection), KeySym); 1094 } 1095 1096 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 1097 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const { 1098 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration()) 1099 return; 1100 1101 const Triple &TT = getTargetTriple(); 1102 1103 if (TT.isKnownWindowsMSVCEnvironment()) 1104 OS << " /EXPORT:"; 1105 else 1106 OS << " -export:"; 1107 1108 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) { 1109 std::string Flag; 1110 raw_string_ostream FlagOS(Flag); 1111 Mang.getNameWithPrefix(FlagOS, GV, false); 1112 FlagOS.flush(); 1113 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) 1114 OS << Flag.substr(1); 1115 else 1116 OS << Flag; 1117 } else { 1118 Mang.getNameWithPrefix(OS, GV, false); 1119 } 1120 1121 if (!GV->getValueType()->isFunctionTy()) { 1122 if (TT.isKnownWindowsMSVCEnvironment()) 1123 OS << ",DATA"; 1124 else 1125 OS << ",data"; 1126 } 1127 } 1128