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