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