1 //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements classes used to handle lowerings specific to common 10 // object file formats. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/Triple.h" 20 #include "llvm/BinaryFormat/COFF.h" 21 #include "llvm/BinaryFormat/Dwarf.h" 22 #include "llvm/BinaryFormat/ELF.h" 23 #include "llvm/BinaryFormat/MachO.h" 24 #include "llvm/CodeGen/MachineModuleInfo.h" 25 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 26 #include "llvm/IR/Comdat.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/DerivedTypes.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/GlobalAlias.h" 32 #include "llvm/IR/GlobalObject.h" 33 #include "llvm/IR/GlobalValue.h" 34 #include "llvm/IR/GlobalVariable.h" 35 #include "llvm/IR/Mangler.h" 36 #include "llvm/IR/Metadata.h" 37 #include "llvm/IR/Module.h" 38 #include "llvm/IR/Type.h" 39 #include "llvm/MC/MCAsmInfo.h" 40 #include "llvm/MC/MCContext.h" 41 #include "llvm/MC/MCExpr.h" 42 #include "llvm/MC/MCSectionCOFF.h" 43 #include "llvm/MC/MCSectionELF.h" 44 #include "llvm/MC/MCSectionMachO.h" 45 #include "llvm/MC/MCSectionWasm.h" 46 #include "llvm/MC/MCSectionXCOFF.h" 47 #include "llvm/MC/MCStreamer.h" 48 #include "llvm/MC/MCSymbol.h" 49 #include "llvm/MC/MCSymbolELF.h" 50 #include "llvm/MC/MCValue.h" 51 #include "llvm/MC/SectionKind.h" 52 #include "llvm/ProfileData/InstrProf.h" 53 #include "llvm/Support/Casting.h" 54 #include "llvm/Support/CodeGen.h" 55 #include "llvm/Support/Format.h" 56 #include "llvm/Support/ErrorHandling.h" 57 #include "llvm/Support/raw_ostream.h" 58 #include "llvm/Target/TargetMachine.h" 59 #include <cassert> 60 #include <string> 61 62 using namespace llvm; 63 using namespace dwarf; 64 65 static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, 66 StringRef &Section) { 67 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 68 M.getModuleFlagsMetadata(ModuleFlags); 69 70 for (const auto &MFE: ModuleFlags) { 71 // Ignore flags with 'Require' behaviour. 72 if (MFE.Behavior == Module::Require) 73 continue; 74 75 StringRef Key = MFE.Key->getString(); 76 if (Key == "Objective-C Image Info Version") { 77 Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 78 } else if (Key == "Objective-C Garbage Collection" || 79 Key == "Objective-C GC Only" || 80 Key == "Objective-C Is Simulated" || 81 Key == "Objective-C Class Properties" || 82 Key == "Objective-C Image Swift Version") { 83 Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); 84 } else if (Key == "Objective-C Image Info Section") { 85 Section = cast<MDString>(MFE.Val)->getString(); 86 } 87 } 88 } 89 90 //===----------------------------------------------------------------------===// 91 // ELF 92 //===----------------------------------------------------------------------===// 93 94 void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, 95 const TargetMachine &TgtM) { 96 TargetLoweringObjectFile::Initialize(Ctx, TgtM); 97 TM = &TgtM; 98 99 CodeModel::Model CM = TgtM.getCodeModel(); 100 101 switch (TgtM.getTargetTriple().getArch()) { 102 case Triple::arm: 103 case Triple::armeb: 104 case Triple::thumb: 105 case Triple::thumbeb: 106 if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) 107 break; 108 // Fallthrough if not using EHABI 109 LLVM_FALLTHROUGH; 110 case Triple::ppc: 111 case Triple::x86: 112 PersonalityEncoding = isPositionIndependent() 113 ? dwarf::DW_EH_PE_indirect | 114 dwarf::DW_EH_PE_pcrel | 115 dwarf::DW_EH_PE_sdata4 116 : dwarf::DW_EH_PE_absptr; 117 LSDAEncoding = isPositionIndependent() 118 ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 119 : dwarf::DW_EH_PE_absptr; 120 TTypeEncoding = isPositionIndependent() 121 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 122 dwarf::DW_EH_PE_sdata4 123 : dwarf::DW_EH_PE_absptr; 124 break; 125 case Triple::x86_64: 126 if (isPositionIndependent()) { 127 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 128 ((CM == CodeModel::Small || CM == CodeModel::Medium) 129 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 130 LSDAEncoding = dwarf::DW_EH_PE_pcrel | 131 (CM == CodeModel::Small 132 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 133 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 134 ((CM == CodeModel::Small || CM == CodeModel::Medium) 135 ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); 136 } else { 137 PersonalityEncoding = 138 (CM == CodeModel::Small || CM == CodeModel::Medium) 139 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 140 LSDAEncoding = (CM == CodeModel::Small) 141 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 142 TTypeEncoding = (CM == CodeModel::Small) 143 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 144 } 145 break; 146 case Triple::hexagon: 147 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 148 LSDAEncoding = dwarf::DW_EH_PE_absptr; 149 TTypeEncoding = dwarf::DW_EH_PE_absptr; 150 if (isPositionIndependent()) { 151 PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 152 LSDAEncoding |= dwarf::DW_EH_PE_pcrel; 153 TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; 154 } 155 break; 156 case Triple::aarch64: 157 case Triple::aarch64_be: 158 // The small model guarantees static code/data size < 4GB, but not where it 159 // will be in memory. Most of these could end up >2GB away so even a signed 160 // pc-relative 32-bit address is insufficient, theoretically. 161 if (isPositionIndependent()) { 162 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 163 dwarf::DW_EH_PE_sdata8; 164 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8; 165 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 166 dwarf::DW_EH_PE_sdata8; 167 } else { 168 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 169 LSDAEncoding = dwarf::DW_EH_PE_absptr; 170 TTypeEncoding = dwarf::DW_EH_PE_absptr; 171 } 172 break; 173 case Triple::lanai: 174 LSDAEncoding = dwarf::DW_EH_PE_absptr; 175 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 176 TTypeEncoding = dwarf::DW_EH_PE_absptr; 177 break; 178 case Triple::mips: 179 case Triple::mipsel: 180 case Triple::mips64: 181 case Triple::mips64el: 182 // MIPS uses indirect pointer to refer personality functions and types, so 183 // that the eh_frame section can be read-only. DW.ref.personality will be 184 // generated for relocation. 185 PersonalityEncoding = dwarf::DW_EH_PE_indirect; 186 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't 187 // identify N64 from just a triple. 188 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 189 dwarf::DW_EH_PE_sdata4; 190 // We don't support PC-relative LSDA references in GAS so we use the default 191 // DW_EH_PE_absptr for those. 192 193 // FreeBSD must be explicit about the data size and using pcrel since it's 194 // assembler/linker won't do the automatic conversion that the Linux tools 195 // do. 196 if (TgtM.getTargetTriple().isOSFreeBSD()) { 197 PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 198 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 199 } 200 break; 201 case Triple::ppc64: 202 case Triple::ppc64le: 203 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 204 dwarf::DW_EH_PE_udata8; 205 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; 206 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 207 dwarf::DW_EH_PE_udata8; 208 break; 209 case Triple::sparcel: 210 case Triple::sparc: 211 if (isPositionIndependent()) { 212 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 213 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 214 dwarf::DW_EH_PE_sdata4; 215 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 216 dwarf::DW_EH_PE_sdata4; 217 } else { 218 LSDAEncoding = dwarf::DW_EH_PE_absptr; 219 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 220 TTypeEncoding = dwarf::DW_EH_PE_absptr; 221 } 222 CallSiteEncoding = dwarf::DW_EH_PE_udata4; 223 break; 224 case Triple::riscv32: 225 case Triple::riscv64: 226 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 227 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 228 dwarf::DW_EH_PE_sdata4; 229 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 230 dwarf::DW_EH_PE_sdata4; 231 CallSiteEncoding = dwarf::DW_EH_PE_udata4; 232 break; 233 case Triple::sparcv9: 234 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 235 if (isPositionIndependent()) { 236 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 237 dwarf::DW_EH_PE_sdata4; 238 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 239 dwarf::DW_EH_PE_sdata4; 240 } else { 241 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 242 TTypeEncoding = dwarf::DW_EH_PE_absptr; 243 } 244 break; 245 case Triple::systemz: 246 // All currently-defined code models guarantee that 4-byte PC-relative 247 // values will be in range. 248 if (isPositionIndependent()) { 249 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 250 dwarf::DW_EH_PE_sdata4; 251 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 252 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 253 dwarf::DW_EH_PE_sdata4; 254 } else { 255 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 256 LSDAEncoding = dwarf::DW_EH_PE_absptr; 257 TTypeEncoding = dwarf::DW_EH_PE_absptr; 258 } 259 break; 260 default: 261 break; 262 } 263 } 264 265 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, 266 Module &M) const { 267 auto &C = getContext(); 268 269 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 270 auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS, 271 ELF::SHF_EXCLUDE); 272 273 Streamer.SwitchSection(S); 274 275 for (const auto &Operand : LinkerOptions->operands()) { 276 if (cast<MDNode>(Operand)->getNumOperands() != 2) 277 report_fatal_error("invalid llvm.linker.options"); 278 for (const auto &Option : cast<MDNode>(Operand)->operands()) { 279 Streamer.EmitBytes(cast<MDString>(Option)->getString()); 280 Streamer.EmitIntValue(0, 1); 281 } 282 } 283 } 284 285 if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) { 286 auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES, 287 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 288 289 Streamer.SwitchSection(S); 290 291 for (const auto &Operand : DependentLibraries->operands()) { 292 Streamer.EmitBytes( 293 cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString()); 294 Streamer.EmitIntValue(0, 1); 295 } 296 } 297 298 unsigned Version = 0; 299 unsigned Flags = 0; 300 StringRef Section; 301 302 GetObjCImageInfo(M, Version, Flags, Section); 303 if (!Section.empty()) { 304 auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 305 Streamer.SwitchSection(S); 306 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 307 Streamer.EmitIntValue(Version, 4); 308 Streamer.EmitIntValue(Flags, 4); 309 Streamer.AddBlankLine(); 310 } 311 312 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 313 M.getModuleFlagsMetadata(ModuleFlags); 314 315 MDNode *CFGProfile = nullptr; 316 317 for (const auto &MFE : ModuleFlags) { 318 StringRef Key = MFE.Key->getString(); 319 if (Key == "CG Profile") { 320 CFGProfile = cast<MDNode>(MFE.Val); 321 break; 322 } 323 } 324 325 if (!CFGProfile) 326 return; 327 328 auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * { 329 if (!MDO) 330 return nullptr; 331 auto V = cast<ValueAsMetadata>(MDO); 332 const Function *F = cast<Function>(V->getValue()); 333 return TM->getSymbol(F); 334 }; 335 336 for (const auto &Edge : CFGProfile->operands()) { 337 MDNode *E = cast<MDNode>(Edge); 338 const MCSymbol *From = GetSym(E->getOperand(0)); 339 const MCSymbol *To = GetSym(E->getOperand(1)); 340 // Skip null functions. This can happen if functions are dead stripped after 341 // the CGProfile pass has been run. 342 if (!From || !To) 343 continue; 344 uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2)) 345 ->getValue() 346 ->getUniqueInteger() 347 .getZExtValue(); 348 Streamer.emitCGProfileEntry( 349 MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C), 350 MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count); 351 } 352 } 353 354 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( 355 const GlobalValue *GV, const TargetMachine &TM, 356 MachineModuleInfo *MMI) const { 357 unsigned Encoding = getPersonalityEncoding(); 358 if ((Encoding & 0x80) == DW_EH_PE_indirect) 359 return getContext().getOrCreateSymbol(StringRef("DW.ref.") + 360 TM.getSymbol(GV)->getName()); 361 if ((Encoding & 0x70) == DW_EH_PE_absptr) 362 return TM.getSymbol(GV); 363 report_fatal_error("We do not support this DWARF encoding yet!"); 364 } 365 366 void TargetLoweringObjectFileELF::emitPersonalityValue( 367 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { 368 SmallString<64> NameData("DW.ref."); 369 NameData += Sym->getName(); 370 MCSymbolELF *Label = 371 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); 372 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); 373 Streamer.EmitSymbolAttribute(Label, MCSA_Weak); 374 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; 375 MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(), 376 ELF::SHT_PROGBITS, Flags, 0); 377 unsigned Size = DL.getPointerSize(); 378 Streamer.SwitchSection(Sec); 379 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0)); 380 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); 381 const MCExpr *E = MCConstantExpr::create(Size, getContext()); 382 Streamer.emitELFSize(Label, E); 383 Streamer.EmitLabel(Label); 384 385 Streamer.EmitSymbolValue(Sym, Size); 386 } 387 388 const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( 389 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 390 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 391 if (Encoding & DW_EH_PE_indirect) { 392 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); 393 394 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM); 395 396 // Add information about the stub reference to ELFMMI so that the stub 397 // gets emitted by the asmprinter. 398 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); 399 if (!StubSym.getPointer()) { 400 MCSymbol *Sym = TM.getSymbol(GV); 401 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 402 } 403 404 return TargetLoweringObjectFile:: 405 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 406 Encoding & ~DW_EH_PE_indirect, Streamer); 407 } 408 409 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 410 MMI, Streamer); 411 } 412 413 static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { 414 // N.B.: The defaults used in here are not the same ones used in MC. 415 // We follow gcc, MC follows gas. For example, given ".section .eh_frame", 416 // both gas and MC will produce a section with no flags. Given 417 // section(".eh_frame") gcc will produce: 418 // 419 // .section .eh_frame,"a",@progbits 420 421 if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, 422 /*AddSegmentInfo=*/false)) 423 return SectionKind::getMetadata(); 424 425 if (Name.empty() || Name[0] != '.') return K; 426 427 // Default implementation based on some magic section names. 428 if (Name == ".bss" || 429 Name.startswith(".bss.") || 430 Name.startswith(".gnu.linkonce.b.") || 431 Name.startswith(".llvm.linkonce.b.") || 432 Name == ".sbss" || 433 Name.startswith(".sbss.") || 434 Name.startswith(".gnu.linkonce.sb.") || 435 Name.startswith(".llvm.linkonce.sb.")) 436 return SectionKind::getBSS(); 437 438 if (Name == ".tdata" || 439 Name.startswith(".tdata.") || 440 Name.startswith(".gnu.linkonce.td.") || 441 Name.startswith(".llvm.linkonce.td.")) 442 return SectionKind::getThreadData(); 443 444 if (Name == ".tbss" || 445 Name.startswith(".tbss.") || 446 Name.startswith(".gnu.linkonce.tb.") || 447 Name.startswith(".llvm.linkonce.tb.")) 448 return SectionKind::getThreadBSS(); 449 450 return K; 451 } 452 453 static unsigned getELFSectionType(StringRef Name, SectionKind K) { 454 // Use SHT_NOTE for section whose name starts with ".note" to allow 455 // emitting ELF notes from C variable declaration. 456 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 457 if (Name.startswith(".note")) 458 return ELF::SHT_NOTE; 459 460 if (Name == ".init_array") 461 return ELF::SHT_INIT_ARRAY; 462 463 if (Name == ".fini_array") 464 return ELF::SHT_FINI_ARRAY; 465 466 if (Name == ".preinit_array") 467 return ELF::SHT_PREINIT_ARRAY; 468 469 if (K.isBSS() || K.isThreadBSS()) 470 return ELF::SHT_NOBITS; 471 472 return ELF::SHT_PROGBITS; 473 } 474 475 static unsigned getELFSectionFlags(SectionKind K) { 476 unsigned Flags = 0; 477 478 if (!K.isMetadata()) 479 Flags |= ELF::SHF_ALLOC; 480 481 if (K.isText()) 482 Flags |= ELF::SHF_EXECINSTR; 483 484 if (K.isExecuteOnly()) 485 Flags |= ELF::SHF_ARM_PURECODE; 486 487 if (K.isWriteable()) 488 Flags |= ELF::SHF_WRITE; 489 490 if (K.isThreadLocal()) 491 Flags |= ELF::SHF_TLS; 492 493 if (K.isMergeableCString() || K.isMergeableConst()) 494 Flags |= ELF::SHF_MERGE; 495 496 if (K.isMergeableCString()) 497 Flags |= ELF::SHF_STRINGS; 498 499 return Flags; 500 } 501 502 static const Comdat *getELFComdat(const GlobalValue *GV) { 503 const Comdat *C = GV->getComdat(); 504 if (!C) 505 return nullptr; 506 507 if (C->getSelectionKind() != Comdat::Any) 508 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + 509 C->getName() + "' cannot be lowered."); 510 511 return C; 512 } 513 514 static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO, 515 const TargetMachine &TM) { 516 MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); 517 if (!MD) 518 return nullptr; 519 520 const MDOperand &Op = MD->getOperand(0); 521 if (!Op.get()) 522 return nullptr; 523 524 auto *VM = dyn_cast<ValueAsMetadata>(Op); 525 if (!VM) 526 report_fatal_error("MD_associated operand is not ValueAsMetadata"); 527 528 auto *OtherGV = dyn_cast<GlobalValue>(VM->getValue()); 529 return OtherGV ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGV)) : nullptr; 530 } 531 532 static unsigned getEntrySizeForKind(SectionKind Kind) { 533 if (Kind.isMergeable1ByteCString()) 534 return 1; 535 else if (Kind.isMergeable2ByteCString()) 536 return 2; 537 else if (Kind.isMergeable4ByteCString()) 538 return 4; 539 else if (Kind.isMergeableConst4()) 540 return 4; 541 else if (Kind.isMergeableConst8()) 542 return 8; 543 else if (Kind.isMergeableConst16()) 544 return 16; 545 else if (Kind.isMergeableConst32()) 546 return 32; 547 else { 548 // We shouldn't have mergeable C strings or mergeable constants that we 549 // didn't handle above. 550 assert(!Kind.isMergeableCString() && "unknown string width"); 551 assert(!Kind.isMergeableConst() && "unknown data width"); 552 return 0; 553 } 554 } 555 556 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( 557 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 558 StringRef SectionName = GO->getSection(); 559 560 // Check if '#pragma clang section' name is applicable. 561 // Note that pragma directive overrides -ffunction-section, -fdata-section 562 // and so section name is exactly as user specified and not uniqued. 563 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); 564 if (GV && GV->hasImplicitSection()) { 565 auto Attrs = GV->getAttributes(); 566 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { 567 SectionName = Attrs.getAttribute("bss-section").getValueAsString(); 568 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { 569 SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); 570 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { 571 SectionName = Attrs.getAttribute("data-section").getValueAsString(); 572 } 573 } 574 const Function *F = dyn_cast<Function>(GO); 575 if (F && F->hasFnAttribute("implicit-section-name")) { 576 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); 577 } 578 579 // Infer section flags from the section name if we can. 580 Kind = getELFKindForNamedSection(SectionName, Kind); 581 582 StringRef Group = ""; 583 unsigned Flags = getELFSectionFlags(Kind); 584 if (const Comdat *C = getELFComdat(GO)) { 585 Group = C->getName(); 586 Flags |= ELF::SHF_GROUP; 587 } 588 589 bool EmitUniqueSection = false; 590 591 // If we have -ffunction-sections or -fdata-sections then we should emit the 592 // global value to a uniqued section of the same name. 593 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 594 if (Kind.isText()) 595 EmitUniqueSection = TM.getFunctionSections(); 596 else 597 EmitUniqueSection = TM.getDataSections(); 598 } 599 EmitUniqueSection |= GO->hasComdat(); 600 601 // A section can have at most one associated section. Put each global with 602 // MD_associated in a unique section. 603 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); 604 if (AssociatedSymbol) { 605 EmitUniqueSection = true; 606 Flags |= ELF::SHF_LINK_ORDER; 607 } 608 609 unsigned UniqueID = MCContext::GenericSectionID; 610 if (EmitUniqueSection) 611 UniqueID = NextUniqueID++; 612 613 MCSectionELF *Section = getContext().getELFSection( 614 SectionName, getELFSectionType(SectionName, Kind), Flags, 615 getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol); 616 // Make sure that we did not get some other section with incompatible sh_link. 617 // This should not be possible due to UniqueID code above. 618 assert(Section->getAssociatedSymbol() == AssociatedSymbol && 619 "Associated symbol mismatch between sections"); 620 return Section; 621 } 622 623 /// Return the section prefix name used by options FunctionsSections and 624 /// DataSections. 625 static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 626 if (Kind.isText()) 627 return ".text"; 628 if (Kind.isReadOnly()) 629 return ".rodata"; 630 if (Kind.isBSS()) 631 return ".bss"; 632 if (Kind.isThreadData()) 633 return ".tdata"; 634 if (Kind.isThreadBSS()) 635 return ".tbss"; 636 if (Kind.isData()) 637 return ".data"; 638 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 639 return ".data.rel.ro"; 640 } 641 642 static MCSectionELF *selectELFSectionForGlobal( 643 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 644 const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, 645 unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { 646 647 StringRef Group = ""; 648 if (const Comdat *C = getELFComdat(GO)) { 649 Flags |= ELF::SHF_GROUP; 650 Group = C->getName(); 651 } 652 653 // Get the section entry size based on the kind. 654 unsigned EntrySize = getEntrySizeForKind(Kind); 655 656 SmallString<128> Name; 657 if (Kind.isMergeableCString()) { 658 // We also need alignment here. 659 // FIXME: this is getting the alignment of the character, not the 660 // alignment of the global! 661 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment( 662 cast<GlobalVariable>(GO)); 663 664 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 665 Name = SizeSpec + utostr(Align); 666 } else if (Kind.isMergeableConst()) { 667 Name = ".rodata.cst"; 668 Name += utostr(EntrySize); 669 } else { 670 Name = getSectionPrefixForGlobal(Kind); 671 } 672 673 if (const auto *F = dyn_cast<Function>(GO)) { 674 const auto &OptionalPrefix = F->getSectionPrefix(); 675 if (OptionalPrefix) 676 Name += *OptionalPrefix; 677 } 678 679 unsigned UniqueID = MCContext::GenericSectionID; 680 if (EmitUniqueSection) { 681 if (TM.getUniqueSectionNames()) { 682 Name.push_back('.'); 683 TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/); 684 } else { 685 UniqueID = *NextUniqueID; 686 (*NextUniqueID)++; 687 } 688 } 689 // Use 0 as the unique ID for execute-only text. 690 if (Kind.isExecuteOnly()) 691 UniqueID = 0; 692 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 693 EntrySize, Group, UniqueID, AssociatedSymbol); 694 } 695 696 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 697 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 698 unsigned Flags = getELFSectionFlags(Kind); 699 700 // If we have -ffunction-section or -fdata-section then we should emit the 701 // global value to a uniqued section specifically for it. 702 bool EmitUniqueSection = false; 703 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 704 if (Kind.isText()) 705 EmitUniqueSection = TM.getFunctionSections(); 706 else 707 EmitUniqueSection = TM.getDataSections(); 708 } 709 EmitUniqueSection |= GO->hasComdat(); 710 711 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); 712 if (AssociatedSymbol) { 713 EmitUniqueSection = true; 714 Flags |= ELF::SHF_LINK_ORDER; 715 } 716 717 MCSectionELF *Section = selectELFSectionForGlobal( 718 getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, 719 &NextUniqueID, AssociatedSymbol); 720 assert(Section->getAssociatedSymbol() == AssociatedSymbol); 721 return Section; 722 } 723 724 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 725 const Function &F, const TargetMachine &TM) const { 726 // If the function can be removed, produce a unique section so that 727 // the table doesn't prevent the removal. 728 const Comdat *C = F.getComdat(); 729 bool EmitUniqueSection = TM.getFunctionSections() || C; 730 if (!EmitUniqueSection) 731 return ReadOnlySection; 732 733 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 734 getMangler(), TM, EmitUniqueSection, 735 ELF::SHF_ALLOC, &NextUniqueID, 736 /* AssociatedSymbol */ nullptr); 737 } 738 739 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 740 bool UsesLabelDifference, const Function &F) const { 741 // We can always create relative relocations, so use another section 742 // that can be marked non-executable. 743 return false; 744 } 745 746 /// Given a mergeable constant with the specified size and relocation 747 /// information, return a section that it should be placed in. 748 MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 749 const DataLayout &DL, SectionKind Kind, const Constant *C, 750 unsigned &Align) const { 751 if (Kind.isMergeableConst4() && MergeableConst4Section) 752 return MergeableConst4Section; 753 if (Kind.isMergeableConst8() && MergeableConst8Section) 754 return MergeableConst8Section; 755 if (Kind.isMergeableConst16() && MergeableConst16Section) 756 return MergeableConst16Section; 757 if (Kind.isMergeableConst32() && MergeableConst32Section) 758 return MergeableConst32Section; 759 if (Kind.isReadOnly()) 760 return ReadOnlySection; 761 762 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 763 return DataRelROSection; 764 } 765 766 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 767 bool IsCtor, unsigned Priority, 768 const MCSymbol *KeySym) { 769 std::string Name; 770 unsigned Type; 771 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 772 StringRef COMDAT = KeySym ? KeySym->getName() : ""; 773 774 if (KeySym) 775 Flags |= ELF::SHF_GROUP; 776 777 if (UseInitArray) { 778 if (IsCtor) { 779 Type = ELF::SHT_INIT_ARRAY; 780 Name = ".init_array"; 781 } else { 782 Type = ELF::SHT_FINI_ARRAY; 783 Name = ".fini_array"; 784 } 785 if (Priority != 65535) { 786 Name += '.'; 787 Name += utostr(Priority); 788 } 789 } else { 790 // The default scheme is .ctor / .dtor, so we have to invert the priority 791 // numbering. 792 if (IsCtor) 793 Name = ".ctors"; 794 else 795 Name = ".dtors"; 796 if (Priority != 65535) 797 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 798 Type = ELF::SHT_PROGBITS; 799 } 800 801 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 802 } 803 804 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 805 unsigned Priority, const MCSymbol *KeySym) const { 806 return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 807 KeySym); 808 } 809 810 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 811 unsigned Priority, const MCSymbol *KeySym) const { 812 return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 813 KeySym); 814 } 815 816 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 817 const GlobalValue *LHS, const GlobalValue *RHS, 818 const TargetMachine &TM) const { 819 // We may only use a PLT-relative relocation to refer to unnamed_addr 820 // functions. 821 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 822 return nullptr; 823 824 // Basic sanity checks. 825 if (LHS->getType()->getPointerAddressSpace() != 0 || 826 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 827 RHS->isThreadLocal()) 828 return nullptr; 829 830 return MCBinaryExpr::createSub( 831 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, 832 getContext()), 833 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 834 } 835 836 MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { 837 // Use ".GCC.command.line" since this feature is to support clang's 838 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the 839 // same name. 840 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, 841 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 842 } 843 844 void 845 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 846 UseInitArray = UseInitArray_; 847 MCContext &Ctx = getContext(); 848 if (!UseInitArray) { 849 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, 850 ELF::SHF_ALLOC | ELF::SHF_WRITE); 851 852 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, 853 ELF::SHF_ALLOC | ELF::SHF_WRITE); 854 return; 855 } 856 857 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 858 ELF::SHF_WRITE | ELF::SHF_ALLOC); 859 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 860 ELF::SHF_WRITE | ELF::SHF_ALLOC); 861 } 862 863 //===----------------------------------------------------------------------===// 864 // MachO 865 //===----------------------------------------------------------------------===// 866 867 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 868 : TargetLoweringObjectFile() { 869 SupportIndirectSymViaGOTPCRel = true; 870 } 871 872 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 873 const TargetMachine &TM) { 874 TargetLoweringObjectFile::Initialize(Ctx, TM); 875 if (TM.getRelocationModel() == Reloc::Static) { 876 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, 877 SectionKind::getData()); 878 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, 879 SectionKind::getData()); 880 } else { 881 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", 882 MachO::S_MOD_INIT_FUNC_POINTERS, 883 SectionKind::getData()); 884 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", 885 MachO::S_MOD_TERM_FUNC_POINTERS, 886 SectionKind::getData()); 887 } 888 889 PersonalityEncoding = 890 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 891 LSDAEncoding = dwarf::DW_EH_PE_pcrel; 892 TTypeEncoding = 893 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 894 } 895 896 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, 897 Module &M) const { 898 // Emit the linker options if present. 899 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 900 for (const auto &Option : LinkerOptions->operands()) { 901 SmallVector<std::string, 4> StrOptions; 902 for (const auto &Piece : cast<MDNode>(Option)->operands()) 903 StrOptions.push_back(cast<MDString>(Piece)->getString()); 904 Streamer.EmitLinkerOptions(StrOptions); 905 } 906 } 907 908 unsigned VersionVal = 0; 909 unsigned ImageInfoFlags = 0; 910 StringRef SectionVal; 911 912 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); 913 914 // The section is mandatory. If we don't have it, then we don't have GC info. 915 if (SectionVal.empty()) 916 return; 917 918 StringRef Segment, Section; 919 unsigned TAA = 0, StubSize = 0; 920 bool TAAParsed; 921 std::string ErrorCode = 922 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 923 TAA, TAAParsed, StubSize); 924 if (!ErrorCode.empty()) 925 // If invalid, report the error with report_fatal_error. 926 report_fatal_error("Invalid section specifier '" + Section + "': " + 927 ErrorCode + "."); 928 929 // Get the section. 930 MCSectionMachO *S = getContext().getMachOSection( 931 Segment, Section, TAA, StubSize, SectionKind::getData()); 932 Streamer.SwitchSection(S); 933 Streamer.EmitLabel(getContext(). 934 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 935 Streamer.EmitIntValue(VersionVal, 4); 936 Streamer.EmitIntValue(ImageInfoFlags, 4); 937 Streamer.AddBlankLine(); 938 } 939 940 static void checkMachOComdat(const GlobalValue *GV) { 941 const Comdat *C = GV->getComdat(); 942 if (!C) 943 return; 944 945 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 946 "' cannot be lowered."); 947 } 948 949 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 950 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 951 // Parse the section specifier and create it if valid. 952 StringRef Segment, Section; 953 unsigned TAA = 0, StubSize = 0; 954 bool TAAParsed; 955 956 checkMachOComdat(GO); 957 958 std::string ErrorCode = 959 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section, 960 TAA, TAAParsed, StubSize); 961 if (!ErrorCode.empty()) { 962 // If invalid, report the error with report_fatal_error. 963 report_fatal_error("Global variable '" + GO->getName() + 964 "' has an invalid section specifier '" + 965 GO->getSection() + "': " + ErrorCode + "."); 966 } 967 968 // Get the section. 969 MCSectionMachO *S = 970 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 971 972 // If TAA wasn't set by ParseSectionSpecifier() above, 973 // use the value returned by getMachOSection() as a default. 974 if (!TAAParsed) 975 TAA = S->getTypeAndAttributes(); 976 977 // Okay, now that we got the section, verify that the TAA & StubSize agree. 978 // If the user declared multiple globals with different section flags, we need 979 // to reject it here. 980 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 981 // If invalid, report the error with report_fatal_error. 982 report_fatal_error("Global variable '" + GO->getName() + 983 "' section type or attributes does not match previous" 984 " section specifier"); 985 } 986 987 return S; 988 } 989 990 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 991 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 992 checkMachOComdat(GO); 993 994 // Handle thread local data. 995 if (Kind.isThreadBSS()) return TLSBSSSection; 996 if (Kind.isThreadData()) return TLSDataSection; 997 998 if (Kind.isText()) 999 return GO->isWeakForLinker() ? TextCoalSection : TextSection; 1000 1001 // If this is weak/linkonce, put this in a coalescable section, either in text 1002 // or data depending on if it is writable. 1003 if (GO->isWeakForLinker()) { 1004 if (Kind.isReadOnly()) 1005 return ConstTextCoalSection; 1006 if (Kind.isReadOnlyWithRel()) 1007 return ConstDataCoalSection; 1008 return DataCoalSection; 1009 } 1010 1011 // FIXME: Alignment check should be handled by section classifier. 1012 if (Kind.isMergeable1ByteCString() && 1013 GO->getParent()->getDataLayout().getPreferredAlignment( 1014 cast<GlobalVariable>(GO)) < 32) 1015 return CStringSection; 1016 1017 // Do not put 16-bit arrays in the UString section if they have an 1018 // externally visible label, this runs into issues with certain linker 1019 // versions. 1020 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && 1021 GO->getParent()->getDataLayout().getPreferredAlignment( 1022 cast<GlobalVariable>(GO)) < 32) 1023 return UStringSection; 1024 1025 // With MachO only variables whose corresponding symbol starts with 'l' or 1026 // 'L' can be merged, so we only try merging GVs with private linkage. 1027 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { 1028 if (Kind.isMergeableConst4()) 1029 return FourByteConstantSection; 1030 if (Kind.isMergeableConst8()) 1031 return EightByteConstantSection; 1032 if (Kind.isMergeableConst16()) 1033 return SixteenByteConstantSection; 1034 } 1035 1036 // Otherwise, if it is readonly, but not something we can specially optimize, 1037 // just drop it in .const. 1038 if (Kind.isReadOnly()) 1039 return ReadOnlySection; 1040 1041 // If this is marked const, put it into a const section. But if the dynamic 1042 // linker needs to write to it, put it in the data segment. 1043 if (Kind.isReadOnlyWithRel()) 1044 return ConstDataSection; 1045 1046 // Put zero initialized globals with strong external linkage in the 1047 // DATA, __common section with the .zerofill directive. 1048 if (Kind.isBSSExtern()) 1049 return DataCommonSection; 1050 1051 // Put zero initialized globals with local linkage in __DATA,__bss directive 1052 // with the .zerofill directive (aka .lcomm). 1053 if (Kind.isBSSLocal()) 1054 return DataBSSSection; 1055 1056 // Otherwise, just drop the variable in the normal data section. 1057 return DataSection; 1058 } 1059 1060 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 1061 const DataLayout &DL, SectionKind Kind, const Constant *C, 1062 unsigned &Align) const { 1063 // If this constant requires a relocation, we have to put it in the data 1064 // segment, not in the text segment. 1065 if (Kind.isData() || Kind.isReadOnlyWithRel()) 1066 return ConstDataSection; 1067 1068 if (Kind.isMergeableConst4()) 1069 return FourByteConstantSection; 1070 if (Kind.isMergeableConst8()) 1071 return EightByteConstantSection; 1072 if (Kind.isMergeableConst16()) 1073 return SixteenByteConstantSection; 1074 return ReadOnlySection; // .const 1075 } 1076 1077 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 1078 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 1079 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1080 // The mach-o version of this method defaults to returning a stub reference. 1081 1082 if (Encoding & DW_EH_PE_indirect) { 1083 MachineModuleInfoMachO &MachOMMI = 1084 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1085 1086 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1087 1088 // Add information about the stub reference to MachOMMI so that the stub 1089 // gets emitted by the asmprinter. 1090 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1091 if (!StubSym.getPointer()) { 1092 MCSymbol *Sym = TM.getSymbol(GV); 1093 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1094 } 1095 1096 return TargetLoweringObjectFile:: 1097 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 1098 Encoding & ~DW_EH_PE_indirect, Streamer); 1099 } 1100 1101 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 1102 MMI, Streamer); 1103 } 1104 1105 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 1106 const GlobalValue *GV, const TargetMachine &TM, 1107 MachineModuleInfo *MMI) const { 1108 // The mach-o version of this method defaults to returning a stub reference. 1109 MachineModuleInfoMachO &MachOMMI = 1110 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1111 1112 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1113 1114 // Add information about the stub reference to MachOMMI so that the stub 1115 // gets emitted by the asmprinter. 1116 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1117 if (!StubSym.getPointer()) { 1118 MCSymbol *Sym = TM.getSymbol(GV); 1119 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1120 } 1121 1122 return SSym; 1123 } 1124 1125 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 1126 const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 1127 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1128 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 1129 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 1130 // through a non_lazy_ptr stub instead. One advantage is that it allows the 1131 // computation of deltas to final external symbols. Example: 1132 // 1133 // _extgotequiv: 1134 // .long _extfoo 1135 // 1136 // _delta: 1137 // .long _extgotequiv-_delta 1138 // 1139 // is transformed to: 1140 // 1141 // _delta: 1142 // .long L_extfoo$non_lazy_ptr-(_delta+0) 1143 // 1144 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 1145 // L_extfoo$non_lazy_ptr: 1146 // .indirect_symbol _extfoo 1147 // .long 0 1148 // 1149 // The indirect symbol table (and sections of non_lazy_symbol_pointers type) 1150 // may point to both local (same translation unit) and global (other 1151 // translation units) symbols. Example: 1152 // 1153 // .section __DATA,__pointers,non_lazy_symbol_pointers 1154 // L1: 1155 // .indirect_symbol _myGlobal 1156 // .long 0 1157 // L2: 1158 // .indirect_symbol _myLocal 1159 // .long _myLocal 1160 // 1161 // If the symbol is local, instead of the symbol's index, the assembler 1162 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. 1163 // Then the linker will notice the constant in the table and will look at the 1164 // content of the symbol. 1165 MachineModuleInfoMachO &MachOMMI = 1166 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1167 MCContext &Ctx = getContext(); 1168 1169 // The offset must consider the original displacement from the base symbol 1170 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 1171 Offset = -MV.getConstant(); 1172 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 1173 1174 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 1175 // non_lazy_ptr stubs. 1176 SmallString<128> Name; 1177 StringRef Suffix = "$non_lazy_ptr"; 1178 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 1179 Name += Sym->getName(); 1180 Name += Suffix; 1181 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 1182 1183 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 1184 if (!StubSym.getPointer()) { 1185 bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal(); 1186 // With the assumption that IsIndirectLocal == GV->hasLocalLinkage(). 1187 StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), 1188 !IsIndirectLocal); 1189 } 1190 1191 const MCExpr *BSymExpr = 1192 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 1193 const MCExpr *LHS = 1194 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 1195 1196 if (!Offset) 1197 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 1198 1199 const MCExpr *RHS = 1200 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 1201 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 1202 } 1203 1204 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 1205 const MCSection &Section) { 1206 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 1207 return true; 1208 1209 // If it is not dead stripped, it is safe to use private labels. 1210 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 1211 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 1212 return true; 1213 1214 return false; 1215 } 1216 1217 void TargetLoweringObjectFileMachO::getNameWithPrefix( 1218 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1219 const TargetMachine &TM) const { 1220 bool CannotUsePrivateLabel = true; 1221 if (auto *GO = GV->getBaseObject()) { 1222 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); 1223 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); 1224 CannotUsePrivateLabel = 1225 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 1226 } 1227 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1228 } 1229 1230 //===----------------------------------------------------------------------===// 1231 // COFF 1232 //===----------------------------------------------------------------------===// 1233 1234 static unsigned 1235 getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { 1236 unsigned Flags = 0; 1237 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; 1238 1239 if (K.isMetadata()) 1240 Flags |= 1241 COFF::IMAGE_SCN_MEM_DISCARDABLE; 1242 else if (K.isText()) 1243 Flags |= 1244 COFF::IMAGE_SCN_MEM_EXECUTE | 1245 COFF::IMAGE_SCN_MEM_READ | 1246 COFF::IMAGE_SCN_CNT_CODE | 1247 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); 1248 else if (K.isBSS()) 1249 Flags |= 1250 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 1251 COFF::IMAGE_SCN_MEM_READ | 1252 COFF::IMAGE_SCN_MEM_WRITE; 1253 else if (K.isThreadLocal()) 1254 Flags |= 1255 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1256 COFF::IMAGE_SCN_MEM_READ | 1257 COFF::IMAGE_SCN_MEM_WRITE; 1258 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 1259 Flags |= 1260 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1261 COFF::IMAGE_SCN_MEM_READ; 1262 else if (K.isWriteable()) 1263 Flags |= 1264 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1265 COFF::IMAGE_SCN_MEM_READ | 1266 COFF::IMAGE_SCN_MEM_WRITE; 1267 1268 return Flags; 1269 } 1270 1271 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 1272 const Comdat *C = GV->getComdat(); 1273 assert(C && "expected GV to have a Comdat!"); 1274 1275 StringRef ComdatGVName = C->getName(); 1276 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 1277 if (!ComdatGV) 1278 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1279 "' does not exist."); 1280 1281 if (ComdatGV->getComdat() != C) 1282 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1283 "' is not a key for its COMDAT."); 1284 1285 return ComdatGV; 1286 } 1287 1288 static int getSelectionForCOFF(const GlobalValue *GV) { 1289 if (const Comdat *C = GV->getComdat()) { 1290 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 1291 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 1292 ComdatKey = GA->getBaseObject(); 1293 if (ComdatKey == GV) { 1294 switch (C->getSelectionKind()) { 1295 case Comdat::Any: 1296 return COFF::IMAGE_COMDAT_SELECT_ANY; 1297 case Comdat::ExactMatch: 1298 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 1299 case Comdat::Largest: 1300 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 1301 case Comdat::NoDuplicates: 1302 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1303 case Comdat::SameSize: 1304 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 1305 } 1306 } else { 1307 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 1308 } 1309 } 1310 return 0; 1311 } 1312 1313 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 1314 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1315 int Selection = 0; 1316 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1317 StringRef Name = GO->getSection(); 1318 StringRef COMDATSymName = ""; 1319 if (GO->hasComdat()) { 1320 Selection = getSelectionForCOFF(GO); 1321 const GlobalValue *ComdatGV; 1322 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 1323 ComdatGV = getComdatGVForCOFF(GO); 1324 else 1325 ComdatGV = GO; 1326 1327 if (!ComdatGV->hasPrivateLinkage()) { 1328 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1329 COMDATSymName = Sym->getName(); 1330 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1331 } else { 1332 Selection = 0; 1333 } 1334 } 1335 1336 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1337 Selection); 1338 } 1339 1340 static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 1341 if (Kind.isText()) 1342 return ".text"; 1343 if (Kind.isBSS()) 1344 return ".bss"; 1345 if (Kind.isThreadLocal()) 1346 return ".tls$"; 1347 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1348 return ".rdata"; 1349 return ".data"; 1350 } 1351 1352 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 1353 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1354 // If we have -ffunction-sections then we should emit the global value to a 1355 // uniqued section specifically for it. 1356 bool EmitUniquedSection; 1357 if (Kind.isText()) 1358 EmitUniquedSection = TM.getFunctionSections(); 1359 else 1360 EmitUniquedSection = TM.getDataSections(); 1361 1362 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { 1363 SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); 1364 1365 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1366 1367 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1368 int Selection = getSelectionForCOFF(GO); 1369 if (!Selection) 1370 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1371 const GlobalValue *ComdatGV; 1372 if (GO->hasComdat()) 1373 ComdatGV = getComdatGVForCOFF(GO); 1374 else 1375 ComdatGV = GO; 1376 1377 unsigned UniqueID = MCContext::GenericSectionID; 1378 if (EmitUniquedSection) 1379 UniqueID = NextUniqueID++; 1380 1381 if (!ComdatGV->hasPrivateLinkage()) { 1382 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1383 StringRef COMDATSymName = Sym->getName(); 1384 1385 // Append "$symbol" to the section name *before* IR-level mangling is 1386 // applied when targetting mingw. This is what GCC does, and the ld.bfd 1387 // COFF linker will not properly handle comdats otherwise. 1388 if (getTargetTriple().isWindowsGNUEnvironment()) 1389 raw_svector_ostream(Name) << '$' << ComdatGV->getName(); 1390 1391 return getContext().getCOFFSection(Name, Characteristics, Kind, 1392 COMDATSymName, Selection, UniqueID); 1393 } else { 1394 SmallString<256> TmpData; 1395 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); 1396 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 1397 Selection, UniqueID); 1398 } 1399 } 1400 1401 if (Kind.isText()) 1402 return TextSection; 1403 1404 if (Kind.isThreadLocal()) 1405 return TLSDataSection; 1406 1407 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1408 return ReadOnlySection; 1409 1410 // Note: we claim that common symbols are put in BSSSection, but they are 1411 // really emitted with the magic .comm directive, which creates a symbol table 1412 // entry but not a section. 1413 if (Kind.isBSS() || Kind.isCommon()) 1414 return BSSSection; 1415 1416 return DataSection; 1417 } 1418 1419 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1420 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1421 const TargetMachine &TM) const { 1422 bool CannotUsePrivateLabel = false; 1423 if (GV->hasPrivateLinkage() && 1424 ((isa<Function>(GV) && TM.getFunctionSections()) || 1425 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1426 CannotUsePrivateLabel = true; 1427 1428 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1429 } 1430 1431 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1432 const Function &F, const TargetMachine &TM) const { 1433 // If the function can be removed, produce a unique section so that 1434 // the table doesn't prevent the removal. 1435 const Comdat *C = F.getComdat(); 1436 bool EmitUniqueSection = TM.getFunctionSections() || C; 1437 if (!EmitUniqueSection) 1438 return ReadOnlySection; 1439 1440 // FIXME: we should produce a symbol for F instead. 1441 if (F.hasPrivateLinkage()) 1442 return ReadOnlySection; 1443 1444 MCSymbol *Sym = TM.getSymbol(&F); 1445 StringRef COMDATSymName = Sym->getName(); 1446 1447 SectionKind Kind = SectionKind::getReadOnly(); 1448 StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); 1449 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1450 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1451 unsigned UniqueID = NextUniqueID++; 1452 1453 return getContext().getCOFFSection( 1454 SecName, Characteristics, Kind, COMDATSymName, 1455 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); 1456 } 1457 1458 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, 1459 Module &M) const { 1460 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1461 // Emit the linker options to the linker .drectve section. According to the 1462 // spec, this section is a space-separated string containing flags for 1463 // linker. 1464 MCSection *Sec = getDrectveSection(); 1465 Streamer.SwitchSection(Sec); 1466 for (const auto &Option : LinkerOptions->operands()) { 1467 for (const auto &Piece : cast<MDNode>(Option)->operands()) { 1468 // Lead with a space for consistency with our dllexport implementation. 1469 std::string Directive(" "); 1470 Directive.append(cast<MDString>(Piece)->getString()); 1471 Streamer.EmitBytes(Directive); 1472 } 1473 } 1474 } 1475 1476 unsigned Version = 0; 1477 unsigned Flags = 0; 1478 StringRef Section; 1479 1480 GetObjCImageInfo(M, Version, Flags, Section); 1481 if (Section.empty()) 1482 return; 1483 1484 auto &C = getContext(); 1485 auto *S = C.getCOFFSection( 1486 Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1487 SectionKind::getReadOnly()); 1488 Streamer.SwitchSection(S); 1489 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 1490 Streamer.EmitIntValue(Version, 4); 1491 Streamer.EmitIntValue(Flags, 4); 1492 Streamer.AddBlankLine(); 1493 } 1494 1495 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 1496 const TargetMachine &TM) { 1497 TargetLoweringObjectFile::Initialize(Ctx, TM); 1498 const Triple &T = TM.getTargetTriple(); 1499 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1500 StaticCtorSection = 1501 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1502 COFF::IMAGE_SCN_MEM_READ, 1503 SectionKind::getReadOnly()); 1504 StaticDtorSection = 1505 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1506 COFF::IMAGE_SCN_MEM_READ, 1507 SectionKind::getReadOnly()); 1508 } else { 1509 StaticCtorSection = Ctx.getCOFFSection( 1510 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1511 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1512 SectionKind::getData()); 1513 StaticDtorSection = Ctx.getCOFFSection( 1514 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1515 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1516 SectionKind::getData()); 1517 } 1518 } 1519 1520 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 1521 const Triple &T, bool IsCtor, 1522 unsigned Priority, 1523 const MCSymbol *KeySym, 1524 MCSectionCOFF *Default) { 1525 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1526 // If the priority is the default, use .CRT$XCU, possibly associative. 1527 if (Priority == 65535) 1528 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 1529 1530 // Otherwise, we need to compute a new section name. Low priorities should 1531 // run earlier. The linker will sort sections ASCII-betically, and we need a 1532 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 1533 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 1534 // low priorities need to sort before 'L', since the CRT uses that 1535 // internally, so we use ".CRT$XCA00001" for them. 1536 SmallString<24> Name; 1537 raw_svector_ostream OS(Name); 1538 OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 1539 MCSectionCOFF *Sec = Ctx.getCOFFSection( 1540 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1541 SectionKind::getReadOnly()); 1542 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 1543 } 1544 1545 std::string Name = IsCtor ? ".ctors" : ".dtors"; 1546 if (Priority != 65535) 1547 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1548 1549 return Ctx.getAssociativeCOFFSection( 1550 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1551 COFF::IMAGE_SCN_MEM_READ | 1552 COFF::IMAGE_SCN_MEM_WRITE, 1553 SectionKind::getData()), 1554 KeySym, 0); 1555 } 1556 1557 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1558 unsigned Priority, const MCSymbol *KeySym) const { 1559 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, 1560 Priority, KeySym, 1561 cast<MCSectionCOFF>(StaticCtorSection)); 1562 } 1563 1564 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1565 unsigned Priority, const MCSymbol *KeySym) const { 1566 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, 1567 Priority, KeySym, 1568 cast<MCSectionCOFF>(StaticDtorSection)); 1569 } 1570 1571 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 1572 raw_ostream &OS, const GlobalValue *GV) const { 1573 emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler()); 1574 } 1575 1576 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed( 1577 raw_ostream &OS, const GlobalValue *GV) const { 1578 emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); 1579 } 1580 1581 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 1582 const GlobalValue *LHS, const GlobalValue *RHS, 1583 const TargetMachine &TM) const { 1584 const Triple &T = TM.getTargetTriple(); 1585 if (T.isOSCygMing()) 1586 return nullptr; 1587 1588 // Our symbols should exist in address space zero, cowardly no-op if 1589 // otherwise. 1590 if (LHS->getType()->getPointerAddressSpace() != 0 || 1591 RHS->getType()->getPointerAddressSpace() != 0) 1592 return nullptr; 1593 1594 // Both ptrtoint instructions must wrap global objects: 1595 // - Only global variables are eligible for image relative relocations. 1596 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 1597 // We expect __ImageBase to be a global variable without a section, externally 1598 // defined. 1599 // 1600 // It should look something like this: @__ImageBase = external constant i8 1601 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 1602 LHS->isThreadLocal() || RHS->isThreadLocal() || 1603 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 1604 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 1605 return nullptr; 1606 1607 return MCSymbolRefExpr::create(TM.getSymbol(LHS), 1608 MCSymbolRefExpr::VK_COFF_IMGREL32, 1609 getContext()); 1610 } 1611 1612 static std::string APIntToHexString(const APInt &AI) { 1613 unsigned Width = (AI.getBitWidth() / 8) * 2; 1614 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true); 1615 unsigned Size = HexString.size(); 1616 assert(Width >= Size && "hex string is too large!"); 1617 HexString.insert(HexString.begin(), Width - Size, '0'); 1618 1619 return HexString; 1620 } 1621 1622 static std::string scalarConstantToHexString(const Constant *C) { 1623 Type *Ty = C->getType(); 1624 if (isa<UndefValue>(C)) { 1625 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 1626 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 1627 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 1628 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 1629 return APIntToHexString(CI->getValue()); 1630 } else { 1631 unsigned NumElements; 1632 if (isa<VectorType>(Ty)) 1633 NumElements = Ty->getVectorNumElements(); 1634 else 1635 NumElements = Ty->getArrayNumElements(); 1636 std::string HexString; 1637 for (int I = NumElements - 1, E = -1; I != E; --I) 1638 HexString += scalarConstantToHexString(C->getAggregateElement(I)); 1639 return HexString; 1640 } 1641 } 1642 1643 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 1644 const DataLayout &DL, SectionKind Kind, const Constant *C, 1645 unsigned &Align) const { 1646 if (Kind.isMergeableConst() && C && 1647 getContext().getAsmInfo()->hasCOFFComdatConstants()) { 1648 // This creates comdat sections with the given symbol name, but unless 1649 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 1650 // will be created with a null storage class, which makes GNU binutils 1651 // error out. 1652 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1653 COFF::IMAGE_SCN_MEM_READ | 1654 COFF::IMAGE_SCN_LNK_COMDAT; 1655 std::string COMDATSymName; 1656 if (Kind.isMergeableConst4()) { 1657 if (Align <= 4) { 1658 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1659 Align = 4; 1660 } 1661 } else if (Kind.isMergeableConst8()) { 1662 if (Align <= 8) { 1663 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1664 Align = 8; 1665 } 1666 } else if (Kind.isMergeableConst16()) { 1667 // FIXME: These may not be appropriate for non-x86 architectures. 1668 if (Align <= 16) { 1669 COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 1670 Align = 16; 1671 } 1672 } else if (Kind.isMergeableConst32()) { 1673 if (Align <= 32) { 1674 COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 1675 Align = 32; 1676 } 1677 } 1678 1679 if (!COMDATSymName.empty()) 1680 return getContext().getCOFFSection(".rdata", Characteristics, Kind, 1681 COMDATSymName, 1682 COFF::IMAGE_COMDAT_SELECT_ANY); 1683 } 1684 1685 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align); 1686 } 1687 1688 1689 //===----------------------------------------------------------------------===// 1690 // Wasm 1691 //===----------------------------------------------------------------------===// 1692 1693 static const Comdat *getWasmComdat(const GlobalValue *GV) { 1694 const Comdat *C = GV->getComdat(); 1695 if (!C) 1696 return nullptr; 1697 1698 if (C->getSelectionKind() != Comdat::Any) 1699 report_fatal_error("WebAssembly COMDATs only support " 1700 "SelectionKind::Any, '" + C->getName() + "' cannot be " 1701 "lowered."); 1702 1703 return C; 1704 } 1705 1706 static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) { 1707 // If we're told we have function data, then use that. 1708 if (K.isText()) 1709 return SectionKind::getText(); 1710 1711 // Otherwise, ignore whatever section type the generic impl detected and use 1712 // a plain data section. 1713 return SectionKind::getData(); 1714 } 1715 1716 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 1717 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1718 // We don't support explict section names for functions in the wasm object 1719 // format. Each function has to be in its own unique section. 1720 if (isa<Function>(GO)) { 1721 return SelectSectionForGlobal(GO, Kind, TM); 1722 } 1723 1724 StringRef Name = GO->getSection(); 1725 1726 Kind = getWasmKindForNamedSection(Name, Kind); 1727 1728 StringRef Group = ""; 1729 if (const Comdat *C = getWasmComdat(GO)) { 1730 Group = C->getName(); 1731 } 1732 1733 MCSectionWasm* Section = 1734 getContext().getWasmSection(Name, Kind, Group, 1735 MCContext::GenericSectionID); 1736 1737 return Section; 1738 } 1739 1740 static MCSectionWasm *selectWasmSectionForGlobal( 1741 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 1742 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 1743 StringRef Group = ""; 1744 if (const Comdat *C = getWasmComdat(GO)) { 1745 Group = C->getName(); 1746 } 1747 1748 bool UniqueSectionNames = TM.getUniqueSectionNames(); 1749 SmallString<128> Name = getSectionPrefixForGlobal(Kind); 1750 1751 if (const auto *F = dyn_cast<Function>(GO)) { 1752 const auto &OptionalPrefix = F->getSectionPrefix(); 1753 if (OptionalPrefix) 1754 Name += *OptionalPrefix; 1755 } 1756 1757 if (EmitUniqueSection && UniqueSectionNames) { 1758 Name.push_back('.'); 1759 TM.getNameWithPrefix(Name, GO, Mang, true); 1760 } 1761 unsigned UniqueID = MCContext::GenericSectionID; 1762 if (EmitUniqueSection && !UniqueSectionNames) { 1763 UniqueID = *NextUniqueID; 1764 (*NextUniqueID)++; 1765 } 1766 1767 return Ctx.getWasmSection(Name, Kind, Group, UniqueID); 1768 } 1769 1770 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 1771 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1772 1773 if (Kind.isCommon()) 1774 report_fatal_error("mergable sections not supported yet on wasm"); 1775 1776 // If we have -ffunction-section or -fdata-section then we should emit the 1777 // global value to a uniqued section specifically for it. 1778 bool EmitUniqueSection = false; 1779 if (Kind.isText()) 1780 EmitUniqueSection = TM.getFunctionSections(); 1781 else 1782 EmitUniqueSection = TM.getDataSections(); 1783 EmitUniqueSection |= GO->hasComdat(); 1784 1785 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 1786 EmitUniqueSection, &NextUniqueID); 1787 } 1788 1789 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 1790 bool UsesLabelDifference, const Function &F) const { 1791 // We can always create relative relocations, so use another section 1792 // that can be marked non-executable. 1793 return false; 1794 } 1795 1796 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 1797 const GlobalValue *LHS, const GlobalValue *RHS, 1798 const TargetMachine &TM) const { 1799 // We may only use a PLT-relative relocation to refer to unnamed_addr 1800 // functions. 1801 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 1802 return nullptr; 1803 1804 // Basic sanity checks. 1805 if (LHS->getType()->getPointerAddressSpace() != 0 || 1806 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 1807 RHS->isThreadLocal()) 1808 return nullptr; 1809 1810 return MCBinaryExpr::createSub( 1811 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 1812 getContext()), 1813 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 1814 } 1815 1816 void TargetLoweringObjectFileWasm::InitializeWasm() { 1817 StaticCtorSection = 1818 getContext().getWasmSection(".init_array", SectionKind::getData()); 1819 1820 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 1821 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 1822 TTypeEncoding = dwarf::DW_EH_PE_absptr; 1823 } 1824 1825 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 1826 unsigned Priority, const MCSymbol *KeySym) const { 1827 return Priority == UINT16_MAX ? 1828 StaticCtorSection : 1829 getContext().getWasmSection(".init_array." + utostr(Priority), 1830 SectionKind::getData()); 1831 } 1832 1833 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 1834 unsigned Priority, const MCSymbol *KeySym) const { 1835 llvm_unreachable("@llvm.global_dtors should have been lowered already"); 1836 return nullptr; 1837 } 1838 1839 //===----------------------------------------------------------------------===// 1840 // XCOFF 1841 //===----------------------------------------------------------------------===// 1842 MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 1843 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1844 report_fatal_error("XCOFF explicit sections not yet implemented."); 1845 } 1846 1847 MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 1848 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1849 assert(!TM.getFunctionSections() && !TM.getDataSections() && 1850 "XCOFF unique sections not yet implemented."); 1851 1852 // Common symbols go into a csect with matching name which will get mapped 1853 // into the .bss section. 1854 if (Kind.isCommon()) { 1855 SmallString<128> Name; 1856 getNameWithPrefix(Name, GO, TM); 1857 return getContext().getXCOFFSection(Name, XCOFF::XMC_RW, XCOFF::XTY_CM, 1858 Kind, /* BeginSymbolName */ nullptr); 1859 } 1860 1861 if (Kind.isText()) 1862 return TextSection; 1863 1864 report_fatal_error("XCOFF other section types not yet implemented."); 1865 } 1866 1867 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 1868 bool UsesLabelDifference, const Function &F) const { 1869 report_fatal_error("TLOF XCOFF not yet implemented."); 1870 } 1871 1872 void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 1873 const TargetMachine &TgtM) { 1874 TargetLoweringObjectFile::Initialize(Ctx, TgtM); 1875 TTypeEncoding = 0; 1876 PersonalityEncoding = 0; 1877 LSDAEncoding = 0; 1878 } 1879 1880 MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 1881 unsigned Priority, const MCSymbol *KeySym) const { 1882 report_fatal_error("XCOFF ctor section not yet implemented."); 1883 } 1884 1885 MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 1886 unsigned Priority, const MCSymbol *KeySym) const { 1887 report_fatal_error("XCOFF dtor section not yet implemented."); 1888 } 1889 1890 const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 1891 const GlobalValue *LHS, const GlobalValue *RHS, 1892 const TargetMachine &TM) const { 1893 report_fatal_error("XCOFF not yet implemented."); 1894 } 1895