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