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