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