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