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 bool IsComdat = false; 894 if (const Comdat *C = getELFComdat(&F)) { 895 Flags |= ELF::SHF_GROUP; 896 Group = C->getName(); 897 IsComdat = C->getSelectionKind() == Comdat::Any; 898 } 899 // Use SHF_LINK_ORDER to facilitate --gc-sections if we can use GNU ld>=2.36 900 // or LLD, which support mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER. 901 if (TM.getFunctionSections() && 902 (getContext().getAsmInfo()->useIntegratedAssembler() && 903 getContext().getAsmInfo()->binutilsIsAtLeast(2, 36))) { 904 Flags |= ELF::SHF_LINK_ORDER; 905 LinkedToSym = cast<MCSymbolELF>(&FnSym); 906 } 907 908 // Append the function name as the suffix like GCC, assuming 909 // -funique-section-names applies to .gcc_except_table sections. 910 return getContext().getELFSection( 911 (TM.getUniqueSectionNames() ? LSDA->getName() + "." + F.getName() 912 : LSDA->getName()), 913 LSDA->getType(), Flags, 0, Group, IsComdat, MCSection::NonUniqueID, 914 LinkedToSym); 915 } 916 917 bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( 918 bool UsesLabelDifference, const Function &F) const { 919 // We can always create relative relocations, so use another section 920 // that can be marked non-executable. 921 return false; 922 } 923 924 /// Given a mergeable constant with the specified size and relocation 925 /// information, return a section that it should be placed in. 926 MCSection *TargetLoweringObjectFileELF::getSectionForConstant( 927 const DataLayout &DL, SectionKind Kind, const Constant *C, 928 Align &Alignment) const { 929 if (Kind.isMergeableConst4() && MergeableConst4Section) 930 return MergeableConst4Section; 931 if (Kind.isMergeableConst8() && MergeableConst8Section) 932 return MergeableConst8Section; 933 if (Kind.isMergeableConst16() && MergeableConst16Section) 934 return MergeableConst16Section; 935 if (Kind.isMergeableConst32() && MergeableConst32Section) 936 return MergeableConst32Section; 937 if (Kind.isReadOnly()) 938 return ReadOnlySection; 939 940 assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); 941 return DataRelROSection; 942 } 943 944 /// Returns a unique section for the given machine basic block. 945 MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock( 946 const Function &F, const MachineBasicBlock &MBB, 947 const TargetMachine &TM) const { 948 assert(MBB.isBeginSection() && "Basic block does not start a section!"); 949 unsigned UniqueID = MCContext::GenericSectionID; 950 951 // For cold sections use the .text.split. prefix along with the parent 952 // function name. All cold blocks for the same function go to the same 953 // section. Similarly all exception blocks are grouped by symbol name 954 // under the .text.eh prefix. For regular sections, we either use a unique 955 // name, or a unique ID for the section. 956 SmallString<128> Name; 957 if (MBB.getSectionID() == MBBSectionID::ColdSectionID) { 958 Name += BBSectionsColdTextPrefix; 959 Name += MBB.getParent()->getName(); 960 } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) { 961 Name += ".text.eh."; 962 Name += MBB.getParent()->getName(); 963 } else { 964 Name += MBB.getParent()->getSection()->getName(); 965 if (TM.getUniqueBasicBlockSectionNames()) { 966 if (!Name.endswith(".")) 967 Name += "."; 968 Name += MBB.getSymbol()->getName(); 969 } else { 970 UniqueID = NextUniqueID++; 971 } 972 } 973 974 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; 975 std::string GroupName; 976 if (F.hasComdat()) { 977 Flags |= ELF::SHF_GROUP; 978 GroupName = F.getComdat()->getName().str(); 979 } 980 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 981 0 /* Entry Size */, GroupName, 982 F.hasComdat(), UniqueID, nullptr); 983 } 984 985 static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, 986 bool IsCtor, unsigned Priority, 987 const MCSymbol *KeySym) { 988 std::string Name; 989 unsigned Type; 990 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 991 StringRef Comdat = KeySym ? KeySym->getName() : ""; 992 993 if (KeySym) 994 Flags |= ELF::SHF_GROUP; 995 996 if (UseInitArray) { 997 if (IsCtor) { 998 Type = ELF::SHT_INIT_ARRAY; 999 Name = ".init_array"; 1000 } else { 1001 Type = ELF::SHT_FINI_ARRAY; 1002 Name = ".fini_array"; 1003 } 1004 if (Priority != 65535) { 1005 Name += '.'; 1006 Name += utostr(Priority); 1007 } 1008 } else { 1009 // The default scheme is .ctor / .dtor, so we have to invert the priority 1010 // numbering. 1011 if (IsCtor) 1012 Name = ".ctors"; 1013 else 1014 Name = ".dtors"; 1015 if (Priority != 65535) 1016 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1017 Type = ELF::SHT_PROGBITS; 1018 } 1019 1020 return Ctx.getELFSection(Name, Type, Flags, 0, Comdat, /*IsComdat=*/true); 1021 } 1022 1023 MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( 1024 unsigned Priority, const MCSymbol *KeySym) const { 1025 return getStaticStructorSection(getContext(), UseInitArray, true, Priority, 1026 KeySym); 1027 } 1028 1029 MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( 1030 unsigned Priority, const MCSymbol *KeySym) const { 1031 return getStaticStructorSection(getContext(), UseInitArray, false, Priority, 1032 KeySym); 1033 } 1034 1035 const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( 1036 const GlobalValue *LHS, const GlobalValue *RHS, 1037 const TargetMachine &TM) const { 1038 // We may only use a PLT-relative relocation to refer to unnamed_addr 1039 // functions. 1040 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 1041 return nullptr; 1042 1043 // Basic sanity checks. 1044 if (LHS->getType()->getPointerAddressSpace() != 0 || 1045 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 1046 RHS->isThreadLocal()) 1047 return nullptr; 1048 1049 return MCBinaryExpr::createSub( 1050 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, 1051 getContext()), 1052 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 1053 } 1054 1055 const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent( 1056 const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const { 1057 assert(supportDSOLocalEquivalentLowering()); 1058 1059 const auto *GV = Equiv->getGlobalValue(); 1060 1061 // A PLT entry is not needed for dso_local globals. 1062 if (GV->isDSOLocal() || GV->isImplicitDSOLocal()) 1063 return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); 1064 1065 return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeVariantKind, 1066 getContext()); 1067 } 1068 1069 MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const { 1070 // Use ".GCC.command.line" since this feature is to support clang's 1071 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the 1072 // same name. 1073 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS, 1074 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); 1075 } 1076 1077 void 1078 TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { 1079 UseInitArray = UseInitArray_; 1080 MCContext &Ctx = getContext(); 1081 if (!UseInitArray) { 1082 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, 1083 ELF::SHF_ALLOC | ELF::SHF_WRITE); 1084 1085 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, 1086 ELF::SHF_ALLOC | ELF::SHF_WRITE); 1087 return; 1088 } 1089 1090 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, 1091 ELF::SHF_WRITE | ELF::SHF_ALLOC); 1092 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, 1093 ELF::SHF_WRITE | ELF::SHF_ALLOC); 1094 } 1095 1096 //===----------------------------------------------------------------------===// 1097 // MachO 1098 //===----------------------------------------------------------------------===// 1099 1100 TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() 1101 : TargetLoweringObjectFile() { 1102 SupportIndirectSymViaGOTPCRel = true; 1103 } 1104 1105 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, 1106 const TargetMachine &TM) { 1107 TargetLoweringObjectFile::Initialize(Ctx, TM); 1108 if (TM.getRelocationModel() == Reloc::Static) { 1109 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, 1110 SectionKind::getData()); 1111 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, 1112 SectionKind::getData()); 1113 } else { 1114 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", 1115 MachO::S_MOD_INIT_FUNC_POINTERS, 1116 SectionKind::getData()); 1117 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", 1118 MachO::S_MOD_TERM_FUNC_POINTERS, 1119 SectionKind::getData()); 1120 } 1121 1122 PersonalityEncoding = 1123 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 1124 LSDAEncoding = dwarf::DW_EH_PE_pcrel; 1125 TTypeEncoding = 1126 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 1127 } 1128 1129 void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, 1130 Module &M) const { 1131 // Emit the linker options if present. 1132 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1133 for (const auto *Option : LinkerOptions->operands()) { 1134 SmallVector<std::string, 4> StrOptions; 1135 for (const auto &Piece : cast<MDNode>(Option)->operands()) 1136 StrOptions.push_back(std::string(cast<MDString>(Piece)->getString())); 1137 Streamer.emitLinkerOptions(StrOptions); 1138 } 1139 } 1140 1141 unsigned VersionVal = 0; 1142 unsigned ImageInfoFlags = 0; 1143 StringRef SectionVal; 1144 1145 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); 1146 1147 // The section is mandatory. If we don't have it, then we don't have GC info. 1148 if (SectionVal.empty()) 1149 return; 1150 1151 StringRef Segment, Section; 1152 unsigned TAA = 0, StubSize = 0; 1153 bool TAAParsed; 1154 if (Error E = MCSectionMachO::ParseSectionSpecifier( 1155 SectionVal, Segment, Section, TAA, TAAParsed, StubSize)) { 1156 // If invalid, report the error with report_fatal_error. 1157 report_fatal_error("Invalid section specifier '" + Section + 1158 "': " + toString(std::move(E)) + "."); 1159 } 1160 1161 // Get the section. 1162 MCSectionMachO *S = getContext().getMachOSection( 1163 Segment, Section, TAA, StubSize, SectionKind::getData()); 1164 Streamer.SwitchSection(S); 1165 Streamer.emitLabel(getContext(). 1166 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); 1167 Streamer.emitInt32(VersionVal); 1168 Streamer.emitInt32(ImageInfoFlags); 1169 Streamer.AddBlankLine(); 1170 } 1171 1172 static void checkMachOComdat(const GlobalValue *GV) { 1173 const Comdat *C = GV->getComdat(); 1174 if (!C) 1175 return; 1176 1177 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + 1178 "' cannot be lowered."); 1179 } 1180 1181 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( 1182 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1183 1184 StringRef SectionName = GO->getSection(); 1185 1186 const Function *F = dyn_cast<Function>(GO); 1187 if (F && F->hasFnAttribute("implicit-section-name")) { 1188 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); 1189 } 1190 1191 // Parse the section specifier and create it if valid. 1192 StringRef Segment, Section; 1193 unsigned TAA = 0, StubSize = 0; 1194 bool TAAParsed; 1195 1196 checkMachOComdat(GO); 1197 1198 if (Error E = MCSectionMachO::ParseSectionSpecifier( 1199 SectionName, Segment, Section, TAA, TAAParsed, StubSize)) { 1200 // If invalid, report the error with report_fatal_error. 1201 report_fatal_error("Global variable '" + GO->getName() + 1202 "' has an invalid section specifier '" + 1203 GO->getSection() + "': " + toString(std::move(E)) + "."); 1204 } 1205 1206 // Get the section. 1207 MCSectionMachO *S = 1208 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); 1209 1210 // If TAA wasn't set by ParseSectionSpecifier() above, 1211 // use the value returned by getMachOSection() as a default. 1212 if (!TAAParsed) 1213 TAA = S->getTypeAndAttributes(); 1214 1215 // Okay, now that we got the section, verify that the TAA & StubSize agree. 1216 // If the user declared multiple globals with different section flags, we need 1217 // to reject it here. 1218 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { 1219 // If invalid, report the error with report_fatal_error. 1220 report_fatal_error("Global variable '" + GO->getName() + 1221 "' section type or attributes does not match previous" 1222 " section specifier"); 1223 } 1224 1225 return S; 1226 } 1227 1228 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( 1229 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1230 checkMachOComdat(GO); 1231 1232 // Handle thread local data. 1233 if (Kind.isThreadBSS()) return TLSBSSSection; 1234 if (Kind.isThreadData()) return TLSDataSection; 1235 1236 if (Kind.isText()) 1237 return GO->isWeakForLinker() ? TextCoalSection : TextSection; 1238 1239 // If this is weak/linkonce, put this in a coalescable section, either in text 1240 // or data depending on if it is writable. 1241 if (GO->isWeakForLinker()) { 1242 if (Kind.isReadOnly()) 1243 return ConstTextCoalSection; 1244 if (Kind.isReadOnlyWithRel()) 1245 return ConstDataCoalSection; 1246 return DataCoalSection; 1247 } 1248 1249 // FIXME: Alignment check should be handled by section classifier. 1250 if (Kind.isMergeable1ByteCString() && 1251 GO->getParent()->getDataLayout().getPreferredAlign( 1252 cast<GlobalVariable>(GO)) < Align(32)) 1253 return CStringSection; 1254 1255 // Do not put 16-bit arrays in the UString section if they have an 1256 // externally visible label, this runs into issues with certain linker 1257 // versions. 1258 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && 1259 GO->getParent()->getDataLayout().getPreferredAlign( 1260 cast<GlobalVariable>(GO)) < Align(32)) 1261 return UStringSection; 1262 1263 // With MachO only variables whose corresponding symbol starts with 'l' or 1264 // 'L' can be merged, so we only try merging GVs with private linkage. 1265 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { 1266 if (Kind.isMergeableConst4()) 1267 return FourByteConstantSection; 1268 if (Kind.isMergeableConst8()) 1269 return EightByteConstantSection; 1270 if (Kind.isMergeableConst16()) 1271 return SixteenByteConstantSection; 1272 } 1273 1274 // Otherwise, if it is readonly, but not something we can specially optimize, 1275 // just drop it in .const. 1276 if (Kind.isReadOnly()) 1277 return ReadOnlySection; 1278 1279 // If this is marked const, put it into a const section. But if the dynamic 1280 // linker needs to write to it, put it in the data segment. 1281 if (Kind.isReadOnlyWithRel()) 1282 return ConstDataSection; 1283 1284 // Put zero initialized globals with strong external linkage in the 1285 // DATA, __common section with the .zerofill directive. 1286 if (Kind.isBSSExtern()) 1287 return DataCommonSection; 1288 1289 // Put zero initialized globals with local linkage in __DATA,__bss directive 1290 // with the .zerofill directive (aka .lcomm). 1291 if (Kind.isBSSLocal()) 1292 return DataBSSSection; 1293 1294 // Otherwise, just drop the variable in the normal data section. 1295 return DataSection; 1296 } 1297 1298 MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( 1299 const DataLayout &DL, SectionKind Kind, const Constant *C, 1300 Align &Alignment) const { 1301 // If this constant requires a relocation, we have to put it in the data 1302 // segment, not in the text segment. 1303 if (Kind.isData() || Kind.isReadOnlyWithRel()) 1304 return ConstDataSection; 1305 1306 if (Kind.isMergeableConst4()) 1307 return FourByteConstantSection; 1308 if (Kind.isMergeableConst8()) 1309 return EightByteConstantSection; 1310 if (Kind.isMergeableConst16()) 1311 return SixteenByteConstantSection; 1312 return ReadOnlySection; // .const 1313 } 1314 1315 const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( 1316 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 1317 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1318 // The mach-o version of this method defaults to returning a stub reference. 1319 1320 if (Encoding & DW_EH_PE_indirect) { 1321 MachineModuleInfoMachO &MachOMMI = 1322 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1323 1324 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1325 1326 // Add information about the stub reference to MachOMMI so that the stub 1327 // gets emitted by the asmprinter. 1328 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1329 if (!StubSym.getPointer()) { 1330 MCSymbol *Sym = TM.getSymbol(GV); 1331 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1332 } 1333 1334 return TargetLoweringObjectFile:: 1335 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), 1336 Encoding & ~DW_EH_PE_indirect, Streamer); 1337 } 1338 1339 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, 1340 MMI, Streamer); 1341 } 1342 1343 MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( 1344 const GlobalValue *GV, const TargetMachine &TM, 1345 MachineModuleInfo *MMI) const { 1346 // The mach-o version of this method defaults to returning a stub reference. 1347 MachineModuleInfoMachO &MachOMMI = 1348 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1349 1350 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); 1351 1352 // Add information about the stub reference to MachOMMI so that the stub 1353 // gets emitted by the asmprinter. 1354 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); 1355 if (!StubSym.getPointer()) { 1356 MCSymbol *Sym = TM.getSymbol(GV); 1357 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); 1358 } 1359 1360 return SSym; 1361 } 1362 1363 const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( 1364 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, 1365 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { 1366 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation 1367 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol 1368 // through a non_lazy_ptr stub instead. One advantage is that it allows the 1369 // computation of deltas to final external symbols. Example: 1370 // 1371 // _extgotequiv: 1372 // .long _extfoo 1373 // 1374 // _delta: 1375 // .long _extgotequiv-_delta 1376 // 1377 // is transformed to: 1378 // 1379 // _delta: 1380 // .long L_extfoo$non_lazy_ptr-(_delta+0) 1381 // 1382 // .section __IMPORT,__pointers,non_lazy_symbol_pointers 1383 // L_extfoo$non_lazy_ptr: 1384 // .indirect_symbol _extfoo 1385 // .long 0 1386 // 1387 // The indirect symbol table (and sections of non_lazy_symbol_pointers type) 1388 // may point to both local (same translation unit) and global (other 1389 // translation units) symbols. Example: 1390 // 1391 // .section __DATA,__pointers,non_lazy_symbol_pointers 1392 // L1: 1393 // .indirect_symbol _myGlobal 1394 // .long 0 1395 // L2: 1396 // .indirect_symbol _myLocal 1397 // .long _myLocal 1398 // 1399 // If the symbol is local, instead of the symbol's index, the assembler 1400 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table. 1401 // Then the linker will notice the constant in the table and will look at the 1402 // content of the symbol. 1403 MachineModuleInfoMachO &MachOMMI = 1404 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1405 MCContext &Ctx = getContext(); 1406 1407 // The offset must consider the original displacement from the base symbol 1408 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. 1409 Offset = -MV.getConstant(); 1410 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); 1411 1412 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated 1413 // non_lazy_ptr stubs. 1414 SmallString<128> Name; 1415 StringRef Suffix = "$non_lazy_ptr"; 1416 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); 1417 Name += Sym->getName(); 1418 Name += Suffix; 1419 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); 1420 1421 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); 1422 1423 if (!StubSym.getPointer()) 1424 StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym), 1425 !GV->hasLocalLinkage()); 1426 1427 const MCExpr *BSymExpr = 1428 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); 1429 const MCExpr *LHS = 1430 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); 1431 1432 if (!Offset) 1433 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); 1434 1435 const MCExpr *RHS = 1436 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); 1437 return MCBinaryExpr::createSub(LHS, RHS, Ctx); 1438 } 1439 1440 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 1441 const MCSection &Section) { 1442 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 1443 return true; 1444 1445 // If it is not dead stripped, it is safe to use private labels. 1446 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 1447 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 1448 return true; 1449 1450 return false; 1451 } 1452 1453 void TargetLoweringObjectFileMachO::getNameWithPrefix( 1454 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1455 const TargetMachine &TM) const { 1456 bool CannotUsePrivateLabel = true; 1457 if (auto *GO = GV->getBaseObject()) { 1458 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); 1459 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); 1460 CannotUsePrivateLabel = 1461 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); 1462 } 1463 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1464 } 1465 1466 //===----------------------------------------------------------------------===// 1467 // COFF 1468 //===----------------------------------------------------------------------===// 1469 1470 static unsigned 1471 getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { 1472 unsigned Flags = 0; 1473 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; 1474 1475 if (K.isMetadata()) 1476 Flags |= 1477 COFF::IMAGE_SCN_MEM_DISCARDABLE; 1478 else if (K.isText()) 1479 Flags |= 1480 COFF::IMAGE_SCN_MEM_EXECUTE | 1481 COFF::IMAGE_SCN_MEM_READ | 1482 COFF::IMAGE_SCN_CNT_CODE | 1483 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); 1484 else if (K.isBSS()) 1485 Flags |= 1486 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 1487 COFF::IMAGE_SCN_MEM_READ | 1488 COFF::IMAGE_SCN_MEM_WRITE; 1489 else if (K.isThreadLocal()) 1490 Flags |= 1491 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1492 COFF::IMAGE_SCN_MEM_READ | 1493 COFF::IMAGE_SCN_MEM_WRITE; 1494 else if (K.isReadOnly() || K.isReadOnlyWithRel()) 1495 Flags |= 1496 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1497 COFF::IMAGE_SCN_MEM_READ; 1498 else if (K.isWriteable()) 1499 Flags |= 1500 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1501 COFF::IMAGE_SCN_MEM_READ | 1502 COFF::IMAGE_SCN_MEM_WRITE; 1503 1504 return Flags; 1505 } 1506 1507 static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { 1508 const Comdat *C = GV->getComdat(); 1509 assert(C && "expected GV to have a Comdat!"); 1510 1511 StringRef ComdatGVName = C->getName(); 1512 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); 1513 if (!ComdatGV) 1514 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1515 "' does not exist."); 1516 1517 if (ComdatGV->getComdat() != C) 1518 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + 1519 "' is not a key for its COMDAT."); 1520 1521 return ComdatGV; 1522 } 1523 1524 static int getSelectionForCOFF(const GlobalValue *GV) { 1525 if (const Comdat *C = GV->getComdat()) { 1526 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); 1527 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) 1528 ComdatKey = GA->getBaseObject(); 1529 if (ComdatKey == GV) { 1530 switch (C->getSelectionKind()) { 1531 case Comdat::Any: 1532 return COFF::IMAGE_COMDAT_SELECT_ANY; 1533 case Comdat::ExactMatch: 1534 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; 1535 case Comdat::Largest: 1536 return COFF::IMAGE_COMDAT_SELECT_LARGEST; 1537 case Comdat::NoDuplicates: 1538 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1539 case Comdat::SameSize: 1540 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; 1541 } 1542 } else { 1543 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 1544 } 1545 } 1546 return 0; 1547 } 1548 1549 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( 1550 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1551 int Selection = 0; 1552 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1553 StringRef Name = GO->getSection(); 1554 StringRef COMDATSymName = ""; 1555 if (GO->hasComdat()) { 1556 Selection = getSelectionForCOFF(GO); 1557 const GlobalValue *ComdatGV; 1558 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 1559 ComdatGV = getComdatGVForCOFF(GO); 1560 else 1561 ComdatGV = GO; 1562 1563 if (!ComdatGV->hasPrivateLinkage()) { 1564 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1565 COMDATSymName = Sym->getName(); 1566 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1567 } else { 1568 Selection = 0; 1569 } 1570 } 1571 1572 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, 1573 Selection); 1574 } 1575 1576 static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { 1577 if (Kind.isText()) 1578 return ".text"; 1579 if (Kind.isBSS()) 1580 return ".bss"; 1581 if (Kind.isThreadLocal()) 1582 return ".tls$"; 1583 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1584 return ".rdata"; 1585 return ".data"; 1586 } 1587 1588 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( 1589 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1590 // If we have -ffunction-sections then we should emit the global value to a 1591 // uniqued section specifically for it. 1592 bool EmitUniquedSection; 1593 if (Kind.isText()) 1594 EmitUniquedSection = TM.getFunctionSections(); 1595 else 1596 EmitUniquedSection = TM.getDataSections(); 1597 1598 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { 1599 SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); 1600 1601 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1602 1603 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1604 int Selection = getSelectionForCOFF(GO); 1605 if (!Selection) 1606 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; 1607 const GlobalValue *ComdatGV; 1608 if (GO->hasComdat()) 1609 ComdatGV = getComdatGVForCOFF(GO); 1610 else 1611 ComdatGV = GO; 1612 1613 unsigned UniqueID = MCContext::GenericSectionID; 1614 if (EmitUniquedSection) 1615 UniqueID = NextUniqueID++; 1616 1617 if (!ComdatGV->hasPrivateLinkage()) { 1618 MCSymbol *Sym = TM.getSymbol(ComdatGV); 1619 StringRef COMDATSymName = Sym->getName(); 1620 1621 if (const auto *F = dyn_cast<Function>(GO)) 1622 if (Optional<StringRef> Prefix = F->getSectionPrefix()) 1623 raw_svector_ostream(Name) << '$' << *Prefix; 1624 1625 // Append "$symbol" to the section name *before* IR-level mangling is 1626 // applied when targetting mingw. This is what GCC does, and the ld.bfd 1627 // COFF linker will not properly handle comdats otherwise. 1628 if (getTargetTriple().isWindowsGNUEnvironment()) 1629 raw_svector_ostream(Name) << '$' << ComdatGV->getName(); 1630 1631 return getContext().getCOFFSection(Name, Characteristics, Kind, 1632 COMDATSymName, Selection, UniqueID); 1633 } else { 1634 SmallString<256> TmpData; 1635 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); 1636 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, 1637 Selection, UniqueID); 1638 } 1639 } 1640 1641 if (Kind.isText()) 1642 return TextSection; 1643 1644 if (Kind.isThreadLocal()) 1645 return TLSDataSection; 1646 1647 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) 1648 return ReadOnlySection; 1649 1650 // Note: we claim that common symbols are put in BSSSection, but they are 1651 // really emitted with the magic .comm directive, which creates a symbol table 1652 // entry but not a section. 1653 if (Kind.isBSS() || Kind.isCommon()) 1654 return BSSSection; 1655 1656 return DataSection; 1657 } 1658 1659 void TargetLoweringObjectFileCOFF::getNameWithPrefix( 1660 SmallVectorImpl<char> &OutName, const GlobalValue *GV, 1661 const TargetMachine &TM) const { 1662 bool CannotUsePrivateLabel = false; 1663 if (GV->hasPrivateLinkage() && 1664 ((isa<Function>(GV) && TM.getFunctionSections()) || 1665 (isa<GlobalVariable>(GV) && TM.getDataSections()))) 1666 CannotUsePrivateLabel = true; 1667 1668 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); 1669 } 1670 1671 MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( 1672 const Function &F, const TargetMachine &TM) const { 1673 // If the function can be removed, produce a unique section so that 1674 // the table doesn't prevent the removal. 1675 const Comdat *C = F.getComdat(); 1676 bool EmitUniqueSection = TM.getFunctionSections() || C; 1677 if (!EmitUniqueSection) 1678 return ReadOnlySection; 1679 1680 // FIXME: we should produce a symbol for F instead. 1681 if (F.hasPrivateLinkage()) 1682 return ReadOnlySection; 1683 1684 MCSymbol *Sym = TM.getSymbol(&F); 1685 StringRef COMDATSymName = Sym->getName(); 1686 1687 SectionKind Kind = SectionKind::getReadOnly(); 1688 StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); 1689 unsigned Characteristics = getCOFFSectionFlags(Kind, TM); 1690 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; 1691 unsigned UniqueID = NextUniqueID++; 1692 1693 return getContext().getCOFFSection( 1694 SecName, Characteristics, Kind, COMDATSymName, 1695 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); 1696 } 1697 1698 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, 1699 Module &M) const { 1700 emitLinkerDirectives(Streamer, M); 1701 1702 unsigned Version = 0; 1703 unsigned Flags = 0; 1704 StringRef Section; 1705 1706 GetObjCImageInfo(M, Version, Flags, Section); 1707 if (!Section.empty()) { 1708 auto &C = getContext(); 1709 auto *S = C.getCOFFSection(Section, 1710 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1711 COFF::IMAGE_SCN_MEM_READ, 1712 SectionKind::getReadOnly()); 1713 Streamer.SwitchSection(S); 1714 Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); 1715 Streamer.emitInt32(Version); 1716 Streamer.emitInt32(Flags); 1717 Streamer.AddBlankLine(); 1718 } 1719 1720 emitCGProfileMetadata(Streamer, M); 1721 } 1722 1723 void TargetLoweringObjectFileCOFF::emitLinkerDirectives( 1724 MCStreamer &Streamer, Module &M) const { 1725 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { 1726 // Emit the linker options to the linker .drectve section. According to the 1727 // spec, this section is a space-separated string containing flags for 1728 // linker. 1729 MCSection *Sec = getDrectveSection(); 1730 Streamer.SwitchSection(Sec); 1731 for (const auto *Option : LinkerOptions->operands()) { 1732 for (const auto &Piece : cast<MDNode>(Option)->operands()) { 1733 // Lead with a space for consistency with our dllexport implementation. 1734 std::string Directive(" "); 1735 Directive.append(std::string(cast<MDString>(Piece)->getString())); 1736 Streamer.emitBytes(Directive); 1737 } 1738 } 1739 } 1740 1741 // Emit /EXPORT: flags for each exported global as necessary. 1742 std::string Flags; 1743 for (const GlobalValue &GV : M.global_values()) { 1744 raw_string_ostream OS(Flags); 1745 emitLinkerFlagsForGlobalCOFF(OS, &GV, getTargetTriple(), getMangler()); 1746 OS.flush(); 1747 if (!Flags.empty()) { 1748 Streamer.SwitchSection(getDrectveSection()); 1749 Streamer.emitBytes(Flags); 1750 } 1751 Flags.clear(); 1752 } 1753 1754 // Emit /INCLUDE: flags for each used global as necessary. 1755 if (const auto *LU = M.getNamedGlobal("llvm.used")) { 1756 assert(LU->hasInitializer() && "expected llvm.used to have an initializer"); 1757 assert(isa<ArrayType>(LU->getValueType()) && 1758 "expected llvm.used to be an array type"); 1759 if (const auto *A = cast<ConstantArray>(LU->getInitializer())) { 1760 for (const Value *Op : A->operands()) { 1761 const auto *GV = cast<GlobalValue>(Op->stripPointerCasts()); 1762 // Global symbols with internal or private linkage are not visible to 1763 // the linker, and thus would cause an error when the linker tried to 1764 // preserve the symbol due to the `/include:` directive. 1765 if (GV->hasLocalLinkage()) 1766 continue; 1767 1768 raw_string_ostream OS(Flags); 1769 emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); 1770 OS.flush(); 1771 1772 if (!Flags.empty()) { 1773 Streamer.SwitchSection(getDrectveSection()); 1774 Streamer.emitBytes(Flags); 1775 } 1776 Flags.clear(); 1777 } 1778 } 1779 } 1780 } 1781 1782 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 1783 const TargetMachine &TM) { 1784 TargetLoweringObjectFile::Initialize(Ctx, TM); 1785 this->TM = &TM; 1786 const Triple &T = TM.getTargetTriple(); 1787 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1788 StaticCtorSection = 1789 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1790 COFF::IMAGE_SCN_MEM_READ, 1791 SectionKind::getReadOnly()); 1792 StaticDtorSection = 1793 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1794 COFF::IMAGE_SCN_MEM_READ, 1795 SectionKind::getReadOnly()); 1796 } else { 1797 StaticCtorSection = Ctx.getCOFFSection( 1798 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1799 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1800 SectionKind::getData()); 1801 StaticDtorSection = Ctx.getCOFFSection( 1802 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1803 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1804 SectionKind::getData()); 1805 } 1806 } 1807 1808 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 1809 const Triple &T, bool IsCtor, 1810 unsigned Priority, 1811 const MCSymbol *KeySym, 1812 MCSectionCOFF *Default) { 1813 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1814 // If the priority is the default, use .CRT$XCU, possibly associative. 1815 if (Priority == 65535) 1816 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 1817 1818 // Otherwise, we need to compute a new section name. Low priorities should 1819 // run earlier. The linker will sort sections ASCII-betically, and we need a 1820 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 1821 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 1822 // low priorities need to sort before 'L', since the CRT uses that 1823 // internally, so we use ".CRT$XCA00001" for them. 1824 SmallString<24> Name; 1825 raw_svector_ostream OS(Name); 1826 OS << ".CRT$X" << (IsCtor ? "C" : "T") << 1827 (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 1828 MCSectionCOFF *Sec = Ctx.getCOFFSection( 1829 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1830 SectionKind::getReadOnly()); 1831 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 1832 } 1833 1834 std::string Name = IsCtor ? ".ctors" : ".dtors"; 1835 if (Priority != 65535) 1836 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1837 1838 return Ctx.getAssociativeCOFFSection( 1839 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1840 COFF::IMAGE_SCN_MEM_READ | 1841 COFF::IMAGE_SCN_MEM_WRITE, 1842 SectionKind::getData()), 1843 KeySym, 0); 1844 } 1845 1846 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1847 unsigned Priority, const MCSymbol *KeySym) const { 1848 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, 1849 Priority, KeySym, 1850 cast<MCSectionCOFF>(StaticCtorSection)); 1851 } 1852 1853 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1854 unsigned Priority, const MCSymbol *KeySym) const { 1855 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, 1856 Priority, KeySym, 1857 cast<MCSectionCOFF>(StaticDtorSection)); 1858 } 1859 1860 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 1861 const GlobalValue *LHS, const GlobalValue *RHS, 1862 const TargetMachine &TM) const { 1863 const Triple &T = TM.getTargetTriple(); 1864 if (T.isOSCygMing()) 1865 return nullptr; 1866 1867 // Our symbols should exist in address space zero, cowardly no-op if 1868 // otherwise. 1869 if (LHS->getType()->getPointerAddressSpace() != 0 || 1870 RHS->getType()->getPointerAddressSpace() != 0) 1871 return nullptr; 1872 1873 // Both ptrtoint instructions must wrap global objects: 1874 // - Only global variables are eligible for image relative relocations. 1875 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 1876 // We expect __ImageBase to be a global variable without a section, externally 1877 // defined. 1878 // 1879 // It should look something like this: @__ImageBase = external constant i8 1880 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 1881 LHS->isThreadLocal() || RHS->isThreadLocal() || 1882 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 1883 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 1884 return nullptr; 1885 1886 return MCSymbolRefExpr::create(TM.getSymbol(LHS), 1887 MCSymbolRefExpr::VK_COFF_IMGREL32, 1888 getContext()); 1889 } 1890 1891 static std::string APIntToHexString(const APInt &AI) { 1892 unsigned Width = (AI.getBitWidth() / 8) * 2; 1893 std::string HexString = AI.toString(16, /*Signed=*/false); 1894 llvm::transform(HexString, HexString.begin(), tolower); 1895 unsigned Size = HexString.size(); 1896 assert(Width >= Size && "hex string is too large!"); 1897 HexString.insert(HexString.begin(), Width - Size, '0'); 1898 1899 return HexString; 1900 } 1901 1902 static std::string scalarConstantToHexString(const Constant *C) { 1903 Type *Ty = C->getType(); 1904 if (isa<UndefValue>(C)) { 1905 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 1906 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 1907 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 1908 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 1909 return APIntToHexString(CI->getValue()); 1910 } else { 1911 unsigned NumElements; 1912 if (auto *VTy = dyn_cast<VectorType>(Ty)) 1913 NumElements = cast<FixedVectorType>(VTy)->getNumElements(); 1914 else 1915 NumElements = Ty->getArrayNumElements(); 1916 std::string HexString; 1917 for (int I = NumElements - 1, E = -1; I != E; --I) 1918 HexString += scalarConstantToHexString(C->getAggregateElement(I)); 1919 return HexString; 1920 } 1921 } 1922 1923 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 1924 const DataLayout &DL, SectionKind Kind, const Constant *C, 1925 Align &Alignment) const { 1926 if (Kind.isMergeableConst() && C && 1927 getContext().getAsmInfo()->hasCOFFComdatConstants()) { 1928 // This creates comdat sections with the given symbol name, but unless 1929 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 1930 // will be created with a null storage class, which makes GNU binutils 1931 // error out. 1932 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1933 COFF::IMAGE_SCN_MEM_READ | 1934 COFF::IMAGE_SCN_LNK_COMDAT; 1935 std::string COMDATSymName; 1936 if (Kind.isMergeableConst4()) { 1937 if (Alignment <= 4) { 1938 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1939 Alignment = Align(4); 1940 } 1941 } else if (Kind.isMergeableConst8()) { 1942 if (Alignment <= 8) { 1943 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1944 Alignment = Align(8); 1945 } 1946 } else if (Kind.isMergeableConst16()) { 1947 // FIXME: These may not be appropriate for non-x86 architectures. 1948 if (Alignment <= 16) { 1949 COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 1950 Alignment = Align(16); 1951 } 1952 } else if (Kind.isMergeableConst32()) { 1953 if (Alignment <= 32) { 1954 COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 1955 Alignment = Align(32); 1956 } 1957 } 1958 1959 if (!COMDATSymName.empty()) 1960 return getContext().getCOFFSection(".rdata", Characteristics, Kind, 1961 COMDATSymName, 1962 COFF::IMAGE_COMDAT_SELECT_ANY); 1963 } 1964 1965 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, 1966 Alignment); 1967 } 1968 1969 //===----------------------------------------------------------------------===// 1970 // Wasm 1971 //===----------------------------------------------------------------------===// 1972 1973 static const Comdat *getWasmComdat(const GlobalValue *GV) { 1974 const Comdat *C = GV->getComdat(); 1975 if (!C) 1976 return nullptr; 1977 1978 if (C->getSelectionKind() != Comdat::Any) 1979 report_fatal_error("WebAssembly COMDATs only support " 1980 "SelectionKind::Any, '" + C->getName() + "' cannot be " 1981 "lowered."); 1982 1983 return C; 1984 } 1985 1986 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 1987 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 1988 // We don't support explict section names for functions in the wasm object 1989 // format. Each function has to be in its own unique section. 1990 if (isa<Function>(GO)) { 1991 return SelectSectionForGlobal(GO, Kind, TM); 1992 } 1993 1994 StringRef Name = GO->getSection(); 1995 1996 // Certain data sections we treat as named custom sections rather than 1997 // segments within the data section. 1998 // This could be avoided if all data segements (the wasm sense) were 1999 // represented as their own sections (in the llvm sense). 2000 // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 2001 if (Name == ".llvmcmd" || Name == ".llvmbc") 2002 Kind = SectionKind::getMetadata(); 2003 2004 StringRef Group = ""; 2005 if (const Comdat *C = getWasmComdat(GO)) { 2006 Group = C->getName(); 2007 } 2008 2009 MCSectionWasm* Section = 2010 getContext().getWasmSection(Name, Kind, Group, 2011 MCContext::GenericSectionID); 2012 2013 return Section; 2014 } 2015 2016 static MCSectionWasm *selectWasmSectionForGlobal( 2017 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 2018 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 2019 StringRef Group = ""; 2020 if (const Comdat *C = getWasmComdat(GO)) { 2021 Group = C->getName(); 2022 } 2023 2024 bool UniqueSectionNames = TM.getUniqueSectionNames(); 2025 SmallString<128> Name = getSectionPrefixForGlobal(Kind); 2026 2027 if (const auto *F = dyn_cast<Function>(GO)) { 2028 const auto &OptionalPrefix = F->getSectionPrefix(); 2029 if (OptionalPrefix) 2030 raw_svector_ostream(Name) << '.' << *OptionalPrefix; 2031 } 2032 2033 if (EmitUniqueSection && UniqueSectionNames) { 2034 Name.push_back('.'); 2035 TM.getNameWithPrefix(Name, GO, Mang, true); 2036 } 2037 unsigned UniqueID = MCContext::GenericSectionID; 2038 if (EmitUniqueSection && !UniqueSectionNames) { 2039 UniqueID = *NextUniqueID; 2040 (*NextUniqueID)++; 2041 } 2042 2043 return Ctx.getWasmSection(Name, Kind, Group, UniqueID); 2044 } 2045 2046 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 2047 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2048 2049 if (Kind.isCommon()) 2050 report_fatal_error("mergable sections not supported yet on wasm"); 2051 2052 // If we have -ffunction-section or -fdata-section then we should emit the 2053 // global value to a uniqued section specifically for it. 2054 bool EmitUniqueSection = false; 2055 if (Kind.isText()) 2056 EmitUniqueSection = TM.getFunctionSections(); 2057 else 2058 EmitUniqueSection = TM.getDataSections(); 2059 EmitUniqueSection |= GO->hasComdat(); 2060 2061 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 2062 EmitUniqueSection, &NextUniqueID); 2063 } 2064 2065 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 2066 bool UsesLabelDifference, const Function &F) const { 2067 // We can always create relative relocations, so use another section 2068 // that can be marked non-executable. 2069 return false; 2070 } 2071 2072 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 2073 const GlobalValue *LHS, const GlobalValue *RHS, 2074 const TargetMachine &TM) const { 2075 // We may only use a PLT-relative relocation to refer to unnamed_addr 2076 // functions. 2077 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 2078 return nullptr; 2079 2080 // Basic sanity checks. 2081 if (LHS->getType()->getPointerAddressSpace() != 0 || 2082 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 2083 RHS->isThreadLocal()) 2084 return nullptr; 2085 2086 return MCBinaryExpr::createSub( 2087 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 2088 getContext()), 2089 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 2090 } 2091 2092 void TargetLoweringObjectFileWasm::InitializeWasm() { 2093 StaticCtorSection = 2094 getContext().getWasmSection(".init_array", SectionKind::getData()); 2095 2096 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 2097 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 2098 TTypeEncoding = dwarf::DW_EH_PE_absptr; 2099 } 2100 2101 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 2102 unsigned Priority, const MCSymbol *KeySym) const { 2103 return Priority == UINT16_MAX ? 2104 StaticCtorSection : 2105 getContext().getWasmSection(".init_array." + utostr(Priority), 2106 SectionKind::getData()); 2107 } 2108 2109 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 2110 unsigned Priority, const MCSymbol *KeySym) const { 2111 llvm_unreachable("@llvm.global_dtors should have been lowered already"); 2112 return nullptr; 2113 } 2114 2115 //===----------------------------------------------------------------------===// 2116 // XCOFF 2117 //===----------------------------------------------------------------------===// 2118 bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock( 2119 const MachineFunction *MF) { 2120 if (!MF->getLandingPads().empty()) 2121 return true; 2122 2123 const Function &F = MF->getFunction(); 2124 if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry()) 2125 return false; 2126 2127 const Function *Per = 2128 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); 2129 if (isNoOpWithoutInvoke(classifyEHPersonality(Per))) 2130 return false; 2131 2132 return true; 2133 } 2134 2135 MCSymbol * 2136 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { 2137 return MF->getMMI().getContext().getOrCreateSymbol( 2138 "__ehinfo." + Twine(MF->getFunctionNumber())); 2139 } 2140 2141 MCSymbol * 2142 TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV, 2143 const TargetMachine &TM) const { 2144 // We always use a qualname symbol for a GV that represents 2145 // a declaration, a function descriptor, or a common symbol. 2146 // If a GV represents a GlobalVariable and -fdata-sections is enabled, we 2147 // also return a qualname so that a label symbol could be avoided. 2148 // It is inherently ambiguous when the GO represents the address of a 2149 // function, as the GO could either represent a function descriptor or a 2150 // function entry point. We choose to always return a function descriptor 2151 // here. 2152 if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) { 2153 if (GO->isDeclarationForLinker()) 2154 return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM)) 2155 ->getQualNameSymbol(); 2156 2157 SectionKind GOKind = getKindForGlobal(GO, TM); 2158 if (GOKind.isText()) 2159 return cast<MCSectionXCOFF>( 2160 getSectionForFunctionDescriptor(cast<Function>(GO), TM)) 2161 ->getQualNameSymbol(); 2162 if ((TM.getDataSections() && !GO->hasSection()) || GO->hasCommonLinkage() || 2163 GOKind.isBSSLocal() || GOKind.isThreadBSSLocal()) 2164 return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM)) 2165 ->getQualNameSymbol(); 2166 } 2167 2168 // For all other cases, fall back to getSymbol to return the unqualified name. 2169 return nullptr; 2170 } 2171 2172 MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 2173 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2174 if (!GO->hasSection()) 2175 report_fatal_error("#pragma clang section is not yet supported"); 2176 2177 StringRef SectionName = GO->getSection(); 2178 XCOFF::StorageMappingClass MappingClass; 2179 if (Kind.isText()) 2180 MappingClass = XCOFF::XMC_PR; 2181 else if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) 2182 MappingClass = XCOFF::XMC_RW; 2183 else if (Kind.isReadOnly()) 2184 MappingClass = XCOFF::XMC_RO; 2185 else 2186 report_fatal_error("XCOFF other section types not yet implemented."); 2187 2188 return getContext().getXCOFFSection( 2189 SectionName, Kind, XCOFF::CsectProperties(MappingClass, XCOFF::XTY_SD), 2190 /* MultiSymbolsAllowed*/ true); 2191 } 2192 2193 MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( 2194 const GlobalObject *GO, const TargetMachine &TM) const { 2195 assert(GO->isDeclarationForLinker() && 2196 "Tried to get ER section for a defined global."); 2197 2198 SmallString<128> Name; 2199 getNameWithPrefix(Name, GO, TM); 2200 2201 XCOFF::StorageMappingClass SMC = 2202 isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA; 2203 if (GO->isThreadLocal()) 2204 SMC = XCOFF::XMC_UL; 2205 2206 // Externals go into a csect of type ER. 2207 return getContext().getXCOFFSection( 2208 Name, SectionKind::getMetadata(), 2209 XCOFF::CsectProperties(SMC, XCOFF::XTY_ER)); 2210 } 2211 2212 MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 2213 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2214 // Common symbols go into a csect with matching name which will get mapped 2215 // into the .bss section. 2216 // Zero-initialized local TLS symbols go into a csect with matching name which 2217 // will get mapped into the .tbss section. 2218 if (Kind.isBSSLocal() || GO->hasCommonLinkage() || Kind.isThreadBSSLocal()) { 2219 SmallString<128> Name; 2220 getNameWithPrefix(Name, GO, TM); 2221 XCOFF::StorageMappingClass SMC = Kind.isBSSLocal() ? XCOFF::XMC_BS 2222 : Kind.isCommon() ? XCOFF::XMC_RW 2223 : XCOFF::XMC_UL; 2224 return getContext().getXCOFFSection( 2225 Name, Kind, XCOFF::CsectProperties(SMC, XCOFF::XTY_CM)); 2226 } 2227 2228 if (Kind.isMergeableCString()) { 2229 Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 2230 cast<GlobalVariable>(GO)); 2231 2232 unsigned EntrySize = getEntrySizeForKind(Kind); 2233 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 2234 SmallString<128> Name; 2235 Name = SizeSpec + utostr(Alignment.value()); 2236 2237 if (TM.getDataSections()) 2238 getNameWithPrefix(Name, GO, TM); 2239 2240 return getContext().getXCOFFSection( 2241 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD), 2242 /* MultiSymbolsAllowed*/ !TM.getDataSections()); 2243 } 2244 2245 if (Kind.isText()) { 2246 if (TM.getFunctionSections()) { 2247 return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM)) 2248 ->getRepresentedCsect(); 2249 } 2250 return TextSection; 2251 } 2252 2253 // TODO: We may put Kind.isReadOnlyWithRel() under option control, because 2254 // user may want to have read-only data with relocations placed into a 2255 // read-only section by the compiler. 2256 // For BSS kind, zero initialized data must be emitted to the .data section 2257 // because external linkage control sections that get mapped to the .bss 2258 // section will be linked as tentative defintions, which is only appropriate 2259 // for SectionKind::Common. 2260 if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) { 2261 if (TM.getDataSections()) { 2262 SmallString<128> Name; 2263 getNameWithPrefix(Name, GO, TM); 2264 return getContext().getXCOFFSection( 2265 Name, SectionKind::getData(), 2266 XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)); 2267 } 2268 return DataSection; 2269 } 2270 2271 if (Kind.isReadOnly()) { 2272 if (TM.getDataSections()) { 2273 SmallString<128> Name; 2274 getNameWithPrefix(Name, GO, TM); 2275 return getContext().getXCOFFSection( 2276 Name, SectionKind::getReadOnly(), 2277 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2278 } 2279 return ReadOnlySection; 2280 } 2281 2282 // External/weak TLS data and initialized local TLS data are not eligible 2283 // to be put into common csect. If data sections are enabled, thread 2284 // data are emitted into separate sections. Otherwise, thread data 2285 // are emitted into the .tdata section. 2286 if (Kind.isThreadLocal()) { 2287 if (TM.getDataSections()) { 2288 SmallString<128> Name; 2289 getNameWithPrefix(Name, GO, TM); 2290 return getContext().getXCOFFSection( 2291 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TL, XCOFF::XTY_SD)); 2292 } 2293 return TLSDataSection; 2294 } 2295 2296 report_fatal_error("XCOFF other section types not yet implemented."); 2297 } 2298 2299 MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable( 2300 const Function &F, const TargetMachine &TM) const { 2301 assert (!F.getComdat() && "Comdat not supported on XCOFF."); 2302 2303 if (!TM.getFunctionSections()) 2304 return ReadOnlySection; 2305 2306 // If the function can be removed, produce a unique section so that 2307 // the table doesn't prevent the removal. 2308 SmallString<128> NameStr(".rodata.jmp.."); 2309 getNameWithPrefix(NameStr, &F, TM); 2310 return getContext().getXCOFFSection( 2311 NameStr, SectionKind::getReadOnly(), 2312 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2313 } 2314 2315 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 2316 bool UsesLabelDifference, const Function &F) const { 2317 return false; 2318 } 2319 2320 /// Given a mergeable constant with the specified size and relocation 2321 /// information, return a section that it should be placed in. 2322 MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant( 2323 const DataLayout &DL, SectionKind Kind, const Constant *C, 2324 Align &Alignment) const { 2325 //TODO: Enable emiting constant pool to unique sections when we support it. 2326 return ReadOnlySection; 2327 } 2328 2329 void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 2330 const TargetMachine &TgtM) { 2331 TargetLoweringObjectFile::Initialize(Ctx, TgtM); 2332 TTypeEncoding = 2333 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel | 2334 (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4 2335 : dwarf::DW_EH_PE_sdata8); 2336 PersonalityEncoding = 0; 2337 LSDAEncoding = 0; 2338 CallSiteEncoding = dwarf::DW_EH_PE_udata4; 2339 } 2340 2341 MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 2342 unsigned Priority, const MCSymbol *KeySym) const { 2343 report_fatal_error("no static constructor section on AIX"); 2344 } 2345 2346 MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 2347 unsigned Priority, const MCSymbol *KeySym) const { 2348 report_fatal_error("no static destructor section on AIX"); 2349 } 2350 2351 const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 2352 const GlobalValue *LHS, const GlobalValue *RHS, 2353 const TargetMachine &TM) const { 2354 report_fatal_error("XCOFF not yet implemented."); 2355 } 2356 2357 XCOFF::StorageClass 2358 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) { 2359 assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX."); 2360 2361 switch (GV->getLinkage()) { 2362 case GlobalValue::InternalLinkage: 2363 case GlobalValue::PrivateLinkage: 2364 return XCOFF::C_HIDEXT; 2365 case GlobalValue::ExternalLinkage: 2366 case GlobalValue::CommonLinkage: 2367 case GlobalValue::AvailableExternallyLinkage: 2368 return XCOFF::C_EXT; 2369 case GlobalValue::ExternalWeakLinkage: 2370 case GlobalValue::LinkOnceAnyLinkage: 2371 case GlobalValue::LinkOnceODRLinkage: 2372 case GlobalValue::WeakAnyLinkage: 2373 case GlobalValue::WeakODRLinkage: 2374 return XCOFF::C_WEAKEXT; 2375 case GlobalValue::AppendingLinkage: 2376 report_fatal_error( 2377 "There is no mapping that implements AppendingLinkage for XCOFF."); 2378 } 2379 llvm_unreachable("Unknown linkage type!"); 2380 } 2381 2382 MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol( 2383 const GlobalValue *Func, const TargetMachine &TM) const { 2384 assert( 2385 (isa<Function>(Func) || 2386 (isa<GlobalAlias>(Func) && 2387 isa_and_nonnull<Function>(cast<GlobalAlias>(Func)->getBaseObject()))) && 2388 "Func must be a function or an alias which has a function as base " 2389 "object."); 2390 2391 SmallString<128> NameStr; 2392 NameStr.push_back('.'); 2393 getNameWithPrefix(NameStr, Func, TM); 2394 2395 // When -function-sections is enabled and explicit section is not specified, 2396 // it's not necessary to emit function entry point label any more. We will use 2397 // function entry point csect instead. And for function delcarations, the 2398 // undefined symbols gets treated as csect with XTY_ER property. 2399 if (((TM.getFunctionSections() && !Func->hasSection()) || 2400 Func->isDeclaration()) && 2401 isa<Function>(Func)) { 2402 return getContext() 2403 .getXCOFFSection( 2404 NameStr, SectionKind::getText(), 2405 XCOFF::CsectProperties(XCOFF::XMC_PR, Func->isDeclaration() 2406 ? XCOFF::XTY_ER 2407 : XCOFF::XTY_SD)) 2408 ->getQualNameSymbol(); 2409 } 2410 2411 return getContext().getOrCreateSymbol(NameStr); 2412 } 2413 2414 MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( 2415 const Function *F, const TargetMachine &TM) const { 2416 SmallString<128> NameStr; 2417 getNameWithPrefix(NameStr, F, TM); 2418 return getContext().getXCOFFSection( 2419 NameStr, SectionKind::getData(), 2420 XCOFF::CsectProperties(XCOFF::XMC_DS, XCOFF::XTY_SD)); 2421 } 2422 2423 MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( 2424 const MCSymbol *Sym, const TargetMachine &TM) const { 2425 // Use TE storage-mapping class when large code model is enabled so that 2426 // the chance of needing -bbigtoc is decreased. 2427 return getContext().getXCOFFSection( 2428 cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(), 2429 XCOFF::CsectProperties( 2430 TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE : XCOFF::XMC_TC, 2431 XCOFF::XTY_SD)); 2432 } 2433