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 // A section can have at most one associated section. Put each global with 590 // MD_associated in a unique section. 591 unsigned UniqueID = MCContext::GenericSectionID; 592 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); 593 if (AssociatedSymbol) { 594 UniqueID = NextUniqueID++; 595 Flags |= ELF::SHF_LINK_ORDER; 596 } 597 598 MCSectionELF *Section = getContext().getELFSection( 599 SectionName, getELFSectionType(SectionName, Kind), Flags, 600 getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol); 601 // Make sure that we did not get some other section with incompatible sh_link. 602 // This should not be possible due to UniqueID code above. 603 assert(Section->getAssociatedSymbol() == AssociatedSymbol && 604 "Associated symbol mismatch between sections"); 605 return Section; 606 } 607 608 /// Return the section prefix name used by options FunctionsSections and 609 /// DataSections. 610 static StringRef getSectionPrefixForGlobal(SectionKind Kind) { 611 if (Kind.isText()) 612 return ".text"; 613 if (Kind.isReadOnly()) 614 return ".rodata"; 615 if (Kind.isBSS()) 616 return ".bss"; 617 if (Kind.isThreadData()) 618 return ".tdata"; 619 if (Kind.isThreadBSS()) 620 return ".tbss"; 621 if (Kind.isData()) 622 return ".data"; 623 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 624 return ".data.rel.ro"; 625 } 626 627 static MCSectionELF *selectELFSectionForGlobal( 628 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 629 const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, 630 unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { 631 632 StringRef Group = ""; 633 if (const Comdat *C = getELFComdat(GO)) { 634 Flags |= ELF::SHF_GROUP; 635 Group = C->getName(); 636 } 637 638 // Get the section entry size based on the kind. 639 unsigned EntrySize = getEntrySizeForKind(Kind); 640 641 SmallString<128> Name; 642 if (Kind.isMergeableCString()) { 643 // We also need alignment here. 644 // FIXME: this is getting the alignment of the character, not the 645 // alignment of the global! 646 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment( 647 cast<GlobalVariable>(GO)); 648 649 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 650 Name = SizeSpec + utostr(Align); 651 } else if (Kind.isMergeableConst()) { 652 Name = ".rodata.cst"; 653 Name += utostr(EntrySize); 654 } else { 655 Name = getSectionPrefixForGlobal(Kind); 656 } 657 658 if (const auto *F = dyn_cast<Function>(GO)) { 659 const auto &OptionalPrefix = F->getSectionPrefix(); 660 if (OptionalPrefix) 661 Name += *OptionalPrefix; 662 } 663 664 unsigned UniqueID = MCContext::GenericSectionID; 665 if (EmitUniqueSection) { 666 if (TM.getUniqueSectionNames()) { 667 Name.push_back('.'); 668 TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/); 669 } else { 670 UniqueID = *NextUniqueID; 671 (*NextUniqueID)++; 672 } 673 } 674 // Use 0 as the unique ID for execute-only text. 675 if (Kind.isExecuteOnly()) 676 UniqueID = 0; 677 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, 678 EntrySize, Group, UniqueID, AssociatedSymbol); 679 } 680 681 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( 682 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 683 unsigned Flags = getELFSectionFlags(Kind); 684 685 // If we have -ffunction-section or -fdata-section then we should emit the 686 // global value to a uniqued section specifically for it. 687 bool EmitUniqueSection = false; 688 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { 689 if (Kind.isText()) 690 EmitUniqueSection = TM.getFunctionSections(); 691 else 692 EmitUniqueSection = TM.getDataSections(); 693 } 694 EmitUniqueSection |= GO->hasComdat(); 695 696 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); 697 if (AssociatedSymbol) { 698 EmitUniqueSection = true; 699 Flags |= ELF::SHF_LINK_ORDER; 700 } 701 702 MCSectionELF *Section = selectELFSectionForGlobal( 703 getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, 704 &NextUniqueID, AssociatedSymbol); 705 assert(Section->getAssociatedSymbol() == AssociatedSymbol); 706 return Section; 707 } 708 709 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( 710 const Function &F, const TargetMachine &TM) const { 711 // If the function can be removed, produce a unique section so that 712 // the table doesn't prevent the removal. 713 const Comdat *C = F.getComdat(); 714 bool EmitUniqueSection = TM.getFunctionSections() || C; 715 if (!EmitUniqueSection) 716 return ReadOnlySection; 717 718 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), 719 getMangler(), TM, EmitUniqueSection, 720 ELF::SHF_ALLOC, &NextUniqueID, 721 /* AssociatedSymbol */ nullptr); 722 } 723 724 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 725 bool UsesLabelDifference, const Function &F) const { 726 // We can always create relative relocations, so use another section 727 // that can be marked non-executable. 728 return false; 729 } 730 731 /// Given a mergeable constant with the specified size and relocation 732 /// information, return a section that it should be placed in. 733 MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 734 const DataLayout &DL, SectionKind Kind, const Constant *C, 735 unsigned &Align) const { 736 if (Kind.isMergeableConst4() && MergeableConst4Section) 737 return MergeableConst4Section; 738 if (Kind.isMergeableConst8() && MergeableConst8Section) 739 return MergeableConst8Section; 740 if (Kind.isMergeableConst16() && MergeableConst16Section) 741 return MergeableConst16Section; 742 if (Kind.isMergeableConst32() && MergeableConst32Section) 743 return MergeableConst32Section; 744 if (Kind.isReadOnly()) 745 return ReadOnlySection; 746 747 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 748 return DataRelROSection; 749 } 750 751 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 752 bool IsCtor, unsigned Priority, 753 const MCSymbol *KeySym) { 754 std::string Name; 755 unsigned Type; 756 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 757 StringRef COMDAT = KeySym ? KeySym->getName() : ""; 758 759 if (KeySym) 760 Flags |= ELF::SHF_GROUP; 761 762 if (UseInitArray) { 763 if (IsCtor) { 764 Type = ELF::SHT_INIT_ARRAY; 765 Name = ".init_array"; 766 } else { 767 Type = ELF::SHT_FINI_ARRAY; 768 Name = ".fini_array"; 769 } 770 if (Priority != 65535) { 771 Name += '.'; 772 Name += utostr(Priority); 773 } 774 } else { 775 // The default scheme is .ctor / .dtor, so we have to invert the priority 776 // numbering. 777 if (IsCtor) 778 Name = ".ctors"; 779 else 780 Name = ".dtors"; 781 if (Priority != 65535) 782 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 783 Type = ELF::SHT_PROGBITS; 784 } 785 786 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); 787 } 788 789 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 790 unsigned Priority, const MCSymbol *KeySym) const { 791 return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 792 KeySym); 793 } 794 795 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 796 unsigned Priority, const MCSymbol *KeySym) const { 797 return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 798 KeySym); 799 } 800 801 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 802 const GlobalValue *LHS, const GlobalValue *RHS, 803 const TargetMachine &TM) const { 804 // We may only use a PLT-relative relocation to refer to unnamed_addr 805 // functions. 806 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 807 return nullptr; 808 809 // Basic sanity checks. 810 if (LHS->getType()->getPointerAddressSpace() != 0 || 811 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 812 RHS->isThreadLocal()) 813 return nullptr; 814 815 return MCBinaryExpr::createSub( 816 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, 817 getContext()), 818 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 819 } 820 821 MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { 822 // Use ".GCC.command.line" since this feature is to support clang's 823 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the 824 // same name. 825 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, 826 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 827 } 828 829 void 830 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 831 UseInitArray = UseInitArray_; 832 MCContext &Ctx = getContext(); 833 if (!UseInitArray) { 834 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, 835 ELF::SHF_ALLOC | ELF::SHF_WRITE); 836 837 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, 838 ELF::SHF_ALLOC | ELF::SHF_WRITE); 839 return; 840 } 841 842 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 843 ELF::SHF_WRITE | ELF::SHF_ALLOC); 844 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 845 ELF::SHF_WRITE | ELF::SHF_ALLOC); 846 } 847 848 //===----------------------------------------------------------------------===// 849 // MachO 850 //===----------------------------------------------------------------------===// 851 852 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 853 : TargetLoweringObjectFile() { 854 SupportIndirectSymViaGOTPCRel = true; 855 } 856 857 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 858 const TargetMachine &TM) { 859 TargetLoweringObjectFile::Initialize(Ctx, TM); 860 if (TM.getRelocationModel() == Reloc::Static) { 861 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, 862 SectionKind::getData()); 863 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, 864 SectionKind::getData()); 865 } else { 866 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", 867 MachO::S_MOD_INIT_FUNC_POINTERS, 868 SectionKind::getData()); 869 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", 870 MachO::S_MOD_TERM_FUNC_POINTERS, 871 SectionKind::getData()); 872 } 873 874 PersonalityEncoding = 875 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 876 LSDAEncoding = dwarf::DW_EH_PE_pcrel; 877 TTypeEncoding = 878 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 879 } 880 881 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, 882 Module &M) const { 883 // Emit the linker options if present. 884 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 885 for (const auto &Option : LinkerOptions->operands()) { 886 SmallVector<std::string, 4> StrOptions; 887 for (const auto &Piece : cast<MDNode>(Option)->operands()) 888 StrOptions.push_back(cast<MDString>(Piece)->getString()); 889 Streamer.EmitLinkerOptions(StrOptions); 890 } 891 } 892 893 unsigned VersionVal = 0; 894 unsigned ImageInfoFlags = 0; 895 StringRef SectionVal; 896 897 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); 898 899 // The section is mandatory. If we don't have it, then we don't have GC info. 900 if (SectionVal.empty()) 901 return; 902 903 StringRef Segment, Section; 904 unsigned TAA = 0, StubSize = 0; 905 bool TAAParsed; 906 std::string ErrorCode = 907 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, 908 TAA, TAAParsed, StubSize); 909 if (!ErrorCode.empty()) 910 // If invalid, report the error with report_fatal_error. 911 report_fatal_error("Invalid section specifier '" + Section + "': " + 912 ErrorCode + "."); 913 914 // Get the section. 915 MCSectionMachO *S = getContext().getMachOSection( 916 Segment, Section, TAA, StubSize, SectionKind::getData()); 917 Streamer.SwitchSection(S); 918 Streamer.EmitLabel(getContext(). 919 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 920 Streamer.EmitIntValue(VersionVal, 4); 921 Streamer.EmitIntValue(ImageInfoFlags, 4); 922 Streamer.AddBlankLine(); 923 } 924 925 static void checkMachOComdat(const GlobalValue *GV) { 926 const Comdat *C = GV->getComdat(); 927 if (!C) 928 return; 929 930 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 931 "' cannot be lowered."); 932 } 933 934 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 935 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 936 // Parse the section specifier and create it if valid. 937 StringRef Segment, Section; 938 unsigned TAA = 0, StubSize = 0; 939 bool TAAParsed; 940 941 checkMachOComdat(GO); 942 943 std::string ErrorCode = 944 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section, 945 TAA, TAAParsed, StubSize); 946 if (!ErrorCode.empty()) { 947 // If invalid, report the error with report_fatal_error. 948 report_fatal_error("Global variable '" + GO->getName() + 949 "' has an invalid section specifier '" + 950 GO->getSection() + "': " + ErrorCode + "."); 951 } 952 953 // Get the section. 954 MCSectionMachO *S = 955 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 956 957 // If TAA wasn't set by ParseSectionSpecifier() above, 958 // use the value returned by getMachOSection() as a default. 959 if (!TAAParsed) 960 TAA = S->getTypeAndAttributes(); 961 962 // Okay, now that we got the section, verify that the TAA & StubSize agree. 963 // If the user declared multiple globals with different section flags, we need 964 // to reject it here. 965 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 966 // If invalid, report the error with report_fatal_error. 967 report_fatal_error("Global variable '" + GO->getName() + 968 "' section type or attributes does not match previous" 969 " section specifier"); 970 } 971 972 return S; 973 } 974 975 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 976 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 977 checkMachOComdat(GO); 978 979 // Handle thread local data. 980 if (Kind.isThreadBSS()) return TLSBSSSection; 981 if (Kind.isThreadData()) return TLSDataSection; 982 983 if (Kind.isText()) 984 return GO->isWeakForLinker() ? TextCoalSection : TextSection; 985 986 // If this is weak/linkonce, put this in a coalescable section, either in text 987 // or data depending on if it is writable. 988 if (GO->isWeakForLinker()) { 989 if (Kind.isReadOnly()) 990 return ConstTextCoalSection; 991 if (Kind.isReadOnlyWithRel()) 992 return ConstDataCoalSection; 993 return DataCoalSection; 994 } 995 996 // FIXME: Alignment check should be handled by section classifier. 997 if (Kind.isMergeable1ByteCString() && 998 GO->getParent()->getDataLayout().getPreferredAlignment( 999 cast<GlobalVariable>(GO)) < 32) 1000 return CStringSection; 1001 1002 // Do not put 16-bit arrays in the UString section if they have an 1003 // externally visible label, this runs into issues with certain linker 1004 // versions. 1005 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && 1006 GO->getParent()->getDataLayout().getPreferredAlignment( 1007 cast<GlobalVariable>(GO)) < 32) 1008 return UStringSection; 1009 1010 // With MachO only variables whose corresponding symbol starts with 'l' or 1011 // 'L' can be merged, so we only try merging GVs with private linkage. 1012 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { 1013 if (Kind.isMergeableConst4()) 1014 return FourByteConstantSection; 1015 if (Kind.isMergeableConst8()) 1016 return EightByteConstantSection; 1017 if (Kind.isMergeableConst16()) 1018 return SixteenByteConstantSection; 1019 } 1020 1021 // Otherwise, if it is readonly, but not something we can specially optimize, 1022 // just drop it in .const. 1023 if (Kind.isReadOnly()) 1024 return ReadOnlySection; 1025 1026 // If this is marked const, put it into a const section. But if the dynamic 1027 // linker needs to write to it, put it in the data segment. 1028 if (Kind.isReadOnlyWithRel()) 1029 return ConstDataSection; 1030 1031 // Put zero initialized globals with strong external linkage in the 1032 // DATA, __common section with the .zerofill directive. 1033 if (Kind.isBSSExtern()) 1034 return DataCommonSection; 1035 1036 // Put zero initialized globals with local linkage in __DATA,__bss directive 1037 // with the .zerofill directive (aka .lcomm). 1038 if (Kind.isBSSLocal()) 1039 return DataBSSSection; 1040 1041 // Otherwise, just drop the variable in the normal data section. 1042 return DataSection; 1043 } 1044 1045 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 1046 const DataLayout &DL, SectionKind Kind, const Constant *C, 1047 unsigned &Align) const { 1048 // If this constant requires a relocation, we have to put it in the data 1049 // segment, not in the text segment. 1050 if (Kind.isData() || Kind.isReadOnlyWithRel()) 1051 return ConstDataSection; 1052 1053 if (Kind.isMergeableConst4()) 1054 return FourByteConstantSection; 1055 if (Kind.isMergeableConst8()) 1056 return EightByteConstantSection; 1057 if (Kind.isMergeableConst16()) 1058 return SixteenByteConstantSection; 1059 return ReadOnlySection; // .const 1060 } 1061 1062 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 1063 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 1064 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1065 // The mach-o version of this method defaults to returning a stub reference. 1066 1067 if (Encoding & DW_EH_PE_indirect) { 1068 MachineModuleInfoMachO &MachOMMI = 1069 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1070 1071 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1072 1073 // Add information about the stub reference to MachOMMI so that the stub 1074 // gets emitted by the asmprinter. 1075 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1076 if (!StubSym.getPointer()) { 1077 MCSymbol *Sym = TM.getSymbol(GV); 1078 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1079 } 1080 1081 return TargetLoweringObjectFile:: 1082 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 1083 Encoding & ~DW_EH_PE_indirect, Streamer); 1084 } 1085 1086 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 1087 MMI, Streamer); 1088 } 1089 1090 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 1091 const GlobalValue *GV, const TargetMachine &TM, 1092 MachineModuleInfo *MMI) const { 1093 // The mach-o version of this method defaults to returning a stub reference. 1094 MachineModuleInfoMachO &MachOMMI = 1095 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1096 1097 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1098 1099 // Add information about the stub reference to MachOMMI so that the stub 1100 // gets emitted by the asmprinter. 1101 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1102 if (!StubSym.getPointer()) { 1103 MCSymbol *Sym = TM.getSymbol(GV); 1104 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1105 } 1106 1107 return SSym; 1108 } 1109 1110 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 1111 const MCSymbol *Sym, const MCValue &MV, int64_t Offset, 1112 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1113 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 1114 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 1115 // through a non_lazy_ptr stub instead. One advantage is that it allows the 1116 // computation of deltas to final external symbols. Example: 1117 // 1118 // _extgotequiv: 1119 // .long _extfoo 1120 // 1121 // _delta: 1122 // .long _extgotequiv-_delta 1123 // 1124 // is transformed to: 1125 // 1126 // _delta: 1127 // .long L_extfoo$non_lazy_ptr-(_delta+0) 1128 // 1129 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 1130 // L_extfoo$non_lazy_ptr: 1131 // .indirect_symbol _extfoo 1132 // .long 0 1133 // 1134 // The indirect symbol table (and sections of non_lazy_symbol_pointers type) 1135 // may point to both local (same translation unit) and global (other 1136 // translation units) symbols. Example: 1137 // 1138 // .section __DATA,__pointers,non_lazy_symbol_pointers 1139 // L1: 1140 // .indirect_symbol _myGlobal 1141 // .long 0 1142 // L2: 1143 // .indirect_symbol _myLocal 1144 // .long _myLocal 1145 // 1146 // If the symbol is local, instead of the symbol's index, the assembler 1147 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. 1148 // Then the linker will notice the constant in the table and will look at the 1149 // content of the symbol. 1150 MachineModuleInfoMachO &MachOMMI = 1151 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1152 MCContext &Ctx = getContext(); 1153 1154 // The offset must consider the original displacement from the base symbol 1155 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 1156 Offset = -MV.getConstant(); 1157 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 1158 1159 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 1160 // non_lazy_ptr stubs. 1161 SmallString<128> Name; 1162 StringRef Suffix = "$non_lazy_ptr"; 1163 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 1164 Name += Sym->getName(); 1165 Name += Suffix; 1166 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 1167 1168 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 1169 if (!StubSym.getPointer()) { 1170 bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal(); 1171 // With the assumption that IsIndirectLocal == GV->hasLocalLinkage(). 1172 StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), 1173 !IsIndirectLocal); 1174 } 1175 1176 const MCExpr *BSymExpr = 1177 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 1178 const MCExpr *LHS = 1179 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 1180 1181 if (!Offset) 1182 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 1183 1184 const MCExpr *RHS = 1185 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 1186 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 1187 } 1188 1189 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 1190 const MCSection &Section) { 1191 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 1192 return true; 1193 1194 // If it is not dead stripped, it is safe to use private labels. 1195 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 1196 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 1197 return true; 1198 1199 return false; 1200 } 1201 1202 void TargetLoweringObjectFileMachO::getNameWithPrefix( 1203 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1204 const TargetMachine &TM) const { 1205 bool CannotUsePrivateLabel = true; 1206 if (auto *GO = GV->getBaseObject()) { 1207 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); 1208 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); 1209 CannotUsePrivateLabel = 1210 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 1211 } 1212 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1213 } 1214 1215 //===----------------------------------------------------------------------===// 1216 // COFF 1217 //===----------------------------------------------------------------------===// 1218 1219 static unsigned 1220 getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { 1221 unsigned Flags = 0; 1222 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; 1223 1224 if (K.isMetadata()) 1225 Flags |= 1226 COFF::IMAGE_SCN_MEM_DISCARDABLE; 1227 else if (K.isText()) 1228 Flags |= 1229 COFF::IMAGE_SCN_MEM_EXECUTE | 1230 COFF::IMAGE_SCN_MEM_READ | 1231 COFF::IMAGE_SCN_CNT_CODE | 1232 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); 1233 else if (K.isBSS()) 1234 Flags |= 1235 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 1236 COFF::IMAGE_SCN_MEM_READ | 1237 COFF::IMAGE_SCN_MEM_WRITE; 1238 else if (K.isThreadLocal()) 1239 Flags |= 1240 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1241 COFF::IMAGE_SCN_MEM_READ | 1242 COFF::IMAGE_SCN_MEM_WRITE; 1243 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 1244 Flags |= 1245 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1246 COFF::IMAGE_SCN_MEM_READ; 1247 else if (K.isWriteable()) 1248 Flags |= 1249 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1250 COFF::IMAGE_SCN_MEM_READ | 1251 COFF::IMAGE_SCN_MEM_WRITE; 1252 1253 return Flags; 1254 } 1255 1256 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 1257 const Comdat *C = GV->getComdat(); 1258 assert(C && "expected GV to have a Comdat!"); 1259 1260 StringRef ComdatGVName = C->getName(); 1261 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 1262 if (!ComdatGV) 1263 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1264 "' does not exist."); 1265 1266 if (ComdatGV->getComdat() != C) 1267 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1268 "' is not a key for its COMDAT."); 1269 1270 return ComdatGV; 1271 } 1272 1273 static int getSelectionForCOFF(const GlobalValue *GV) { 1274 if (const Comdat *C = GV->getComdat()) { 1275 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 1276 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 1277 ComdatKey = GA->getBaseObject(); 1278 if (ComdatKey == GV) { 1279 switch (C->getSelectionKind()) { 1280 case Comdat::Any: 1281 return COFF::IMAGE_COMDAT_SELECT_ANY; 1282 case Comdat::ExactMatch: 1283 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 1284 case Comdat::Largest: 1285 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 1286 case Comdat::NoDuplicates: 1287 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1288 case Comdat::SameSize: 1289 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 1290 } 1291 } else { 1292 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 1293 } 1294 } 1295 return 0; 1296 } 1297 1298 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 1299 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1300 int Selection = 0; 1301 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1302 StringRef Name = GO->getSection(); 1303 StringRef COMDATSymName = ""; 1304 if (GO->hasComdat()) { 1305 Selection = getSelectionForCOFF(GO); 1306 const GlobalValue *ComdatGV; 1307 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 1308 ComdatGV = getComdatGVForCOFF(GO); 1309 else 1310 ComdatGV = GO; 1311 1312 if (!ComdatGV->hasPrivateLinkage()) { 1313 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1314 COMDATSymName = Sym->getName(); 1315 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1316 } else { 1317 Selection = 0; 1318 } 1319 } 1320 1321 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1322 Selection); 1323 } 1324 1325 static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 1326 if (Kind.isText()) 1327 return ".text"; 1328 if (Kind.isBSS()) 1329 return ".bss"; 1330 if (Kind.isThreadLocal()) 1331 return ".tls$"; 1332 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1333 return ".rdata"; 1334 return ".data"; 1335 } 1336 1337 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 1338 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1339 // If we have -ffunction-sections then we should emit the global value to a 1340 // uniqued section specifically for it. 1341 bool EmitUniquedSection; 1342 if (Kind.isText()) 1343 EmitUniquedSection = TM.getFunctionSections(); 1344 else 1345 EmitUniquedSection = TM.getDataSections(); 1346 1347 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { 1348 SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); 1349 1350 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1351 1352 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1353 int Selection = getSelectionForCOFF(GO); 1354 if (!Selection) 1355 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1356 const GlobalValue *ComdatGV; 1357 if (GO->hasComdat()) 1358 ComdatGV = getComdatGVForCOFF(GO); 1359 else 1360 ComdatGV = GO; 1361 1362 unsigned UniqueID = MCContext::GenericSectionID; 1363 if (EmitUniquedSection) 1364 UniqueID = NextUniqueID++; 1365 1366 if (!ComdatGV->hasPrivateLinkage()) { 1367 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1368 StringRef COMDATSymName = Sym->getName(); 1369 1370 // Append "$symbol" to the section name *before* IR-level mangling is 1371 // applied when targetting mingw. This is what GCC does, and the ld.bfd 1372 // COFF linker will not properly handle comdats otherwise. 1373 if (getTargetTriple().isWindowsGNUEnvironment()) 1374 raw_svector_ostream(Name) << '$' << ComdatGV->getName(); 1375 1376 return getContext().getCOFFSection(Name, Characteristics, Kind, 1377 COMDATSymName, Selection, UniqueID); 1378 } else { 1379 SmallString<256> TmpData; 1380 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); 1381 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 1382 Selection, UniqueID); 1383 } 1384 } 1385 1386 if (Kind.isText()) 1387 return TextSection; 1388 1389 if (Kind.isThreadLocal()) 1390 return TLSDataSection; 1391 1392 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1393 return ReadOnlySection; 1394 1395 // Note: we claim that common symbols are put in BSSSection, but they are 1396 // really emitted with the magic .comm directive, which creates a symbol table 1397 // entry but not a section. 1398 if (Kind.isBSS() || Kind.isCommon()) 1399 return BSSSection; 1400 1401 return DataSection; 1402 } 1403 1404 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1405 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1406 const TargetMachine &TM) const { 1407 bool CannotUsePrivateLabel = false; 1408 if (GV->hasPrivateLinkage() && 1409 ((isa<Function>(GV) && TM.getFunctionSections()) || 1410 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1411 CannotUsePrivateLabel = true; 1412 1413 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1414 } 1415 1416 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1417 const Function &F, const TargetMachine &TM) const { 1418 // If the function can be removed, produce a unique section so that 1419 // the table doesn't prevent the removal. 1420 const Comdat *C = F.getComdat(); 1421 bool EmitUniqueSection = TM.getFunctionSections() || C; 1422 if (!EmitUniqueSection) 1423 return ReadOnlySection; 1424 1425 // FIXME: we should produce a symbol for F instead. 1426 if (F.hasPrivateLinkage()) 1427 return ReadOnlySection; 1428 1429 MCSymbol *Sym = TM.getSymbol(&F); 1430 StringRef COMDATSymName = Sym->getName(); 1431 1432 SectionKind Kind = SectionKind::getReadOnly(); 1433 StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); 1434 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1435 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1436 unsigned UniqueID = NextUniqueID++; 1437 1438 return getContext().getCOFFSection( 1439 SecName, Characteristics, Kind, COMDATSymName, 1440 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); 1441 } 1442 1443 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, 1444 Module &M) const { 1445 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1446 // Emit the linker options to the linker .drectve section. According to the 1447 // spec, this section is a space-separated string containing flags for 1448 // linker. 1449 MCSection *Sec = getDrectveSection(); 1450 Streamer.SwitchSection(Sec); 1451 for (const auto &Option : LinkerOptions->operands()) { 1452 for (const auto &Piece : cast<MDNode>(Option)->operands()) { 1453 // Lead with a space for consistency with our dllexport implementation. 1454 std::string Directive(" "); 1455 Directive.append(cast<MDString>(Piece)->getString()); 1456 Streamer.EmitBytes(Directive); 1457 } 1458 } 1459 } 1460 1461 unsigned Version = 0; 1462 unsigned Flags = 0; 1463 StringRef Section; 1464 1465 GetObjCImageInfo(M, Version, Flags, Section); 1466 if (Section.empty()) 1467 return; 1468 1469 auto &C = getContext(); 1470 auto *S = C.getCOFFSection( 1471 Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1472 SectionKind::getReadOnly()); 1473 Streamer.SwitchSection(S); 1474 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 1475 Streamer.EmitIntValue(Version, 4); 1476 Streamer.EmitIntValue(Flags, 4); 1477 Streamer.AddBlankLine(); 1478 } 1479 1480 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 1481 const TargetMachine &TM) { 1482 TargetLoweringObjectFile::Initialize(Ctx, TM); 1483 const Triple &T = TM.getTargetTriple(); 1484 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1485 StaticCtorSection = 1486 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1487 COFF::IMAGE_SCN_MEM_READ, 1488 SectionKind::getReadOnly()); 1489 StaticDtorSection = 1490 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1491 COFF::IMAGE_SCN_MEM_READ, 1492 SectionKind::getReadOnly()); 1493 } else { 1494 StaticCtorSection = Ctx.getCOFFSection( 1495 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1496 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1497 SectionKind::getData()); 1498 StaticDtorSection = Ctx.getCOFFSection( 1499 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1500 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1501 SectionKind::getData()); 1502 } 1503 } 1504 1505 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 1506 const Triple &T, bool IsCtor, 1507 unsigned Priority, 1508 const MCSymbol *KeySym, 1509 MCSectionCOFF *Default) { 1510 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1511 // If the priority is the default, use .CRT$XCU, possibly associative. 1512 if (Priority == 65535) 1513 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 1514 1515 // Otherwise, we need to compute a new section name. Low priorities should 1516 // run earlier. The linker will sort sections ASCII-betically, and we need a 1517 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 1518 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 1519 // low priorities need to sort before 'L', since the CRT uses that 1520 // internally, so we use ".CRT$XCA00001" for them. 1521 SmallString<24> Name; 1522 raw_svector_ostream OS(Name); 1523 OS << ".CRT$X" << (IsCtor ? "C" : "T") << 1524 (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 1525 MCSectionCOFF *Sec = Ctx.getCOFFSection( 1526 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1527 SectionKind::getReadOnly()); 1528 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 1529 } 1530 1531 std::string Name = IsCtor ? ".ctors" : ".dtors"; 1532 if (Priority != 65535) 1533 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1534 1535 return Ctx.getAssociativeCOFFSection( 1536 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1537 COFF::IMAGE_SCN_MEM_READ | 1538 COFF::IMAGE_SCN_MEM_WRITE, 1539 SectionKind::getData()), 1540 KeySym, 0); 1541 } 1542 1543 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1544 unsigned Priority, const MCSymbol *KeySym) const { 1545 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, 1546 Priority, KeySym, 1547 cast<MCSectionCOFF>(StaticCtorSection)); 1548 } 1549 1550 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1551 unsigned Priority, const MCSymbol *KeySym) const { 1552 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, 1553 Priority, KeySym, 1554 cast<MCSectionCOFF>(StaticDtorSection)); 1555 } 1556 1557 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( 1558 raw_ostream &OS, const GlobalValue *GV) const { 1559 emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler()); 1560 } 1561 1562 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed( 1563 raw_ostream &OS, const GlobalValue *GV) const { 1564 emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); 1565 } 1566 1567 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 1568 const GlobalValue *LHS, const GlobalValue *RHS, 1569 const TargetMachine &TM) const { 1570 const Triple &T = TM.getTargetTriple(); 1571 if (T.isOSCygMing()) 1572 return nullptr; 1573 1574 // Our symbols should exist in address space zero, cowardly no-op if 1575 // otherwise. 1576 if (LHS->getType()->getPointerAddressSpace() != 0 || 1577 RHS->getType()->getPointerAddressSpace() != 0) 1578 return nullptr; 1579 1580 // Both ptrtoint instructions must wrap global objects: 1581 // - Only global variables are eligible for image relative relocations. 1582 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 1583 // We expect __ImageBase to be a global variable without a section, externally 1584 // defined. 1585 // 1586 // It should look something like this: @__ImageBase = external constant i8 1587 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 1588 LHS->isThreadLocal() || RHS->isThreadLocal() || 1589 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 1590 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 1591 return nullptr; 1592 1593 return MCSymbolRefExpr::create(TM.getSymbol(LHS), 1594 MCSymbolRefExpr::VK_COFF_IMGREL32, 1595 getContext()); 1596 } 1597 1598 static std::string APIntToHexString(const APInt &AI) { 1599 unsigned Width = (AI.getBitWidth() / 8) * 2; 1600 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true); 1601 unsigned Size = HexString.size(); 1602 assert(Width >= Size && "hex string is too large!"); 1603 HexString.insert(HexString.begin(), Width - Size, '0'); 1604 1605 return HexString; 1606 } 1607 1608 static std::string scalarConstantToHexString(const Constant *C) { 1609 Type *Ty = C->getType(); 1610 if (isa<UndefValue>(C)) { 1611 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 1612 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 1613 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 1614 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 1615 return APIntToHexString(CI->getValue()); 1616 } else { 1617 unsigned NumElements; 1618 if (isa<VectorType>(Ty)) 1619 NumElements = Ty->getVectorNumElements(); 1620 else 1621 NumElements = Ty->getArrayNumElements(); 1622 std::string HexString; 1623 for (int I = NumElements - 1, E = -1; I != E; --I) 1624 HexString += scalarConstantToHexString(C->getAggregateElement(I)); 1625 return HexString; 1626 } 1627 } 1628 1629 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 1630 const DataLayout &DL, SectionKind Kind, const Constant *C, 1631 unsigned &Align) const { 1632 if (Kind.isMergeableConst() && C && 1633 getContext().getAsmInfo()->hasCOFFComdatConstants()) { 1634 // This creates comdat sections with the given symbol name, but unless 1635 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 1636 // will be created with a null storage class, which makes GNU binutils 1637 // error out. 1638 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1639 COFF::IMAGE_SCN_MEM_READ | 1640 COFF::IMAGE_SCN_LNK_COMDAT; 1641 std::string COMDATSymName; 1642 if (Kind.isMergeableConst4()) { 1643 if (Align <= 4) { 1644 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1645 Align = 4; 1646 } 1647 } else if (Kind.isMergeableConst8()) { 1648 if (Align <= 8) { 1649 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1650 Align = 8; 1651 } 1652 } else if (Kind.isMergeableConst16()) { 1653 // FIXME: These may not be appropriate for non-x86 architectures. 1654 if (Align <= 16) { 1655 COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 1656 Align = 16; 1657 } 1658 } else if (Kind.isMergeableConst32()) { 1659 if (Align <= 32) { 1660 COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 1661 Align = 32; 1662 } 1663 } 1664 1665 if (!COMDATSymName.empty()) 1666 return getContext().getCOFFSection(".rdata", Characteristics, Kind, 1667 COMDATSymName, 1668 COFF::IMAGE_COMDAT_SELECT_ANY); 1669 } 1670 1671 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align); 1672 } 1673 1674 1675 //===----------------------------------------------------------------------===// 1676 // Wasm 1677 //===----------------------------------------------------------------------===// 1678 1679 static const Comdat *getWasmComdat(const GlobalValue *GV) { 1680 const Comdat *C = GV->getComdat(); 1681 if (!C) 1682 return nullptr; 1683 1684 if (C->getSelectionKind() != Comdat::Any) 1685 report_fatal_error("WebAssembly COMDATs only support " 1686 "SelectionKind::Any, '" + C->getName() + "' cannot be " 1687 "lowered."); 1688 1689 return C; 1690 } 1691 1692 static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) { 1693 // If we're told we have function data, then use that. 1694 if (K.isText()) 1695 return SectionKind::getText(); 1696 1697 // Otherwise, ignore whatever section type the generic impl detected and use 1698 // a plain data section. 1699 return SectionKind::getData(); 1700 } 1701 1702 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 1703 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1704 // We don't support explict section names for functions in the wasm object 1705 // format. Each function has to be in its own unique section. 1706 if (isa<Function>(GO)) { 1707 return SelectSectionForGlobal(GO, Kind, TM); 1708 } 1709 1710 StringRef Name = GO->getSection(); 1711 1712 Kind = getWasmKindForNamedSection(Name, Kind); 1713 1714 StringRef Group = ""; 1715 if (const Comdat *C = getWasmComdat(GO)) { 1716 Group = C->getName(); 1717 } 1718 1719 MCSectionWasm* Section = 1720 getContext().getWasmSection(Name, Kind, Group, 1721 MCContext::GenericSectionID); 1722 1723 return Section; 1724 } 1725 1726 static MCSectionWasm *selectWasmSectionForGlobal( 1727 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 1728 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 1729 StringRef Group = ""; 1730 if (const Comdat *C = getWasmComdat(GO)) { 1731 Group = C->getName(); 1732 } 1733 1734 bool UniqueSectionNames = TM.getUniqueSectionNames(); 1735 SmallString<128> Name = getSectionPrefixForGlobal(Kind); 1736 1737 if (const auto *F = dyn_cast<Function>(GO)) { 1738 const auto &OptionalPrefix = F->getSectionPrefix(); 1739 if (OptionalPrefix) 1740 Name += *OptionalPrefix; 1741 } 1742 1743 if (EmitUniqueSection && UniqueSectionNames) { 1744 Name.push_back('.'); 1745 TM.getNameWithPrefix(Name, GO, Mang, true); 1746 } 1747 unsigned UniqueID = MCContext::GenericSectionID; 1748 if (EmitUniqueSection && !UniqueSectionNames) { 1749 UniqueID = *NextUniqueID; 1750 (*NextUniqueID)++; 1751 } 1752 1753 return Ctx.getWasmSection(Name, Kind, Group, UniqueID); 1754 } 1755 1756 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 1757 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1758 1759 if (Kind.isCommon()) 1760 report_fatal_error("mergable sections not supported yet on wasm"); 1761 1762 // If we have -ffunction-section or -fdata-section then we should emit the 1763 // global value to a uniqued section specifically for it. 1764 bool EmitUniqueSection = false; 1765 if (Kind.isText()) 1766 EmitUniqueSection = TM.getFunctionSections(); 1767 else 1768 EmitUniqueSection = TM.getDataSections(); 1769 EmitUniqueSection |= GO->hasComdat(); 1770 1771 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 1772 EmitUniqueSection, &NextUniqueID); 1773 } 1774 1775 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 1776 bool UsesLabelDifference, const Function &F) const { 1777 // We can always create relative relocations, so use another section 1778 // that can be marked non-executable. 1779 return false; 1780 } 1781 1782 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 1783 const GlobalValue *LHS, const GlobalValue *RHS, 1784 const TargetMachine &TM) const { 1785 // We may only use a PLT-relative relocation to refer to unnamed_addr 1786 // functions. 1787 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 1788 return nullptr; 1789 1790 // Basic sanity checks. 1791 if (LHS->getType()->getPointerAddressSpace() != 0 || 1792 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 1793 RHS->isThreadLocal()) 1794 return nullptr; 1795 1796 return MCBinaryExpr::createSub( 1797 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 1798 getContext()), 1799 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 1800 } 1801 1802 void TargetLoweringObjectFileWasm::InitializeWasm() { 1803 StaticCtorSection = 1804 getContext().getWasmSection(".init_array", SectionKind::getData()); 1805 1806 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 1807 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 1808 TTypeEncoding = dwarf::DW_EH_PE_absptr; 1809 } 1810 1811 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 1812 unsigned Priority, const MCSymbol *KeySym) const { 1813 return Priority == UINT16_MAX ? 1814 StaticCtorSection : 1815 getContext().getWasmSection(".init_array." + utostr(Priority), 1816 SectionKind::getData()); 1817 } 1818 1819 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 1820 unsigned Priority, const MCSymbol *KeySym) const { 1821 llvm_unreachable("@llvm.global_dtors should have been lowered already"); 1822 return nullptr; 1823 } 1824 1825 //===----------------------------------------------------------------------===// 1826 // XCOFF 1827 //===----------------------------------------------------------------------===// 1828 MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 1829 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1830 report_fatal_error("XCOFF explicit sections not yet implemented."); 1831 } 1832 1833 MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 1834 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1835 assert(!TM.getFunctionSections() && !TM.getDataSections() && 1836 "XCOFF unique sections not yet implemented."); 1837 1838 // Common symbols go into a csect with matching name which will get mapped 1839 // into the .bss section. 1840 if (Kind.isBSSLocal() || Kind.isCommon()) { 1841 SmallString<128> Name; 1842 getNameWithPrefix(Name, GO, TM); 1843 return getContext().getXCOFFSection( 1844 Name, Kind.isBSSLocal() ? XCOFF::XMC_BS : XCOFF::XMC_RW, XCOFF::XTY_CM, 1845 Kind, /* BeginSymbolName */ nullptr); 1846 } 1847 1848 if (Kind.isText()) 1849 return TextSection; 1850 1851 report_fatal_error("XCOFF other section types not yet implemented."); 1852 } 1853 1854 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 1855 bool UsesLabelDifference, const Function &F) const { 1856 report_fatal_error("TLOF XCOFF not yet implemented."); 1857 } 1858 1859 void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 1860 const TargetMachine &TgtM) { 1861 TargetLoweringObjectFile::Initialize(Ctx, TgtM); 1862 TTypeEncoding = 0; 1863 PersonalityEncoding = 0; 1864 LSDAEncoding = 0; 1865 } 1866 1867 MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 1868 unsigned Priority, const MCSymbol *KeySym) const { 1869 report_fatal_error("XCOFF ctor section not yet implemented."); 1870 } 1871 1872 MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 1873 unsigned Priority, const MCSymbol *KeySym) const { 1874 report_fatal_error("XCOFF dtor section not yet implemented."); 1875 } 1876 1877 const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 1878 const GlobalValue *LHS, const GlobalValue *RHS, 1879 const TargetMachine &TM) const { 1880 report_fatal_error("XCOFF not yet implemented."); 1881 } 1882