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