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