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_sdata4 : dwarf::DW_EH_PE_sdata8); 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 (getContext().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, getContext().getTargetTriple(), 1766 getMangler()); 1767 OS.flush(); 1768 if (!Flags.empty()) { 1769 Streamer.SwitchSection(getDrectveSection()); 1770 Streamer.emitBytes(Flags); 1771 } 1772 Flags.clear(); 1773 } 1774 1775 // Emit /INCLUDE: flags for each used global as necessary. 1776 if (const auto *LU = M.getNamedGlobal("llvm.used")) { 1777 assert(LU->hasInitializer() && "expected llvm.used to have an initializer"); 1778 assert(isa<ArrayType>(LU->getValueType()) && 1779 "expected llvm.used to be an array type"); 1780 if (const auto *A = cast<ConstantArray>(LU->getInitializer())) { 1781 for (const Value *Op : A->operands()) { 1782 const auto *GV = cast<GlobalValue>(Op->stripPointerCasts()); 1783 // Global symbols with internal or private linkage are not visible to 1784 // the linker, and thus would cause an error when the linker tried to 1785 // preserve the symbol due to the `/include:` directive. 1786 if (GV->hasLocalLinkage()) 1787 continue; 1788 1789 raw_string_ostream OS(Flags); 1790 emitLinkerFlagsForUsedCOFF(OS, GV, getContext().getTargetTriple(), 1791 getMangler()); 1792 OS.flush(); 1793 1794 if (!Flags.empty()) { 1795 Streamer.SwitchSection(getDrectveSection()); 1796 Streamer.emitBytes(Flags); 1797 } 1798 Flags.clear(); 1799 } 1800 } 1801 } 1802 } 1803 1804 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, 1805 const TargetMachine &TM) { 1806 TargetLoweringObjectFile::Initialize(Ctx, TM); 1807 this->TM = &TM; 1808 const Triple &T = TM.getTargetTriple(); 1809 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1810 StaticCtorSection = 1811 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1812 COFF::IMAGE_SCN_MEM_READ, 1813 SectionKind::getReadOnly()); 1814 StaticDtorSection = 1815 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1816 COFF::IMAGE_SCN_MEM_READ, 1817 SectionKind::getReadOnly()); 1818 } else { 1819 StaticCtorSection = Ctx.getCOFFSection( 1820 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1821 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1822 SectionKind::getData()); 1823 StaticDtorSection = Ctx.getCOFFSection( 1824 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1825 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 1826 SectionKind::getData()); 1827 } 1828 } 1829 1830 static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 1831 const Triple &T, bool IsCtor, 1832 unsigned Priority, 1833 const MCSymbol *KeySym, 1834 MCSectionCOFF *Default) { 1835 if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 1836 // If the priority is the default, use .CRT$XCU, possibly associative. 1837 if (Priority == 65535) 1838 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); 1839 1840 // Otherwise, we need to compute a new section name. Low priorities should 1841 // run earlier. The linker will sort sections ASCII-betically, and we need a 1842 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we 1843 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really 1844 // low priorities need to sort before 'L', since the CRT uses that 1845 // internally, so we use ".CRT$XCA00001" for them. 1846 SmallString<24> Name; 1847 raw_svector_ostream OS(Name); 1848 OS << ".CRT$X" << (IsCtor ? "C" : "T") << 1849 (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); 1850 MCSectionCOFF *Sec = Ctx.getCOFFSection( 1851 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 1852 SectionKind::getReadOnly()); 1853 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); 1854 } 1855 1856 std::string Name = IsCtor ? ".ctors" : ".dtors"; 1857 if (Priority != 65535) 1858 raw_string_ostream(Name) << format(".%05u", 65535 - Priority); 1859 1860 return Ctx.getAssociativeCOFFSection( 1861 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1862 COFF::IMAGE_SCN_MEM_READ | 1863 COFF::IMAGE_SCN_MEM_WRITE, 1864 SectionKind::getData()), 1865 KeySym, 0); 1866 } 1867 1868 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( 1869 unsigned Priority, const MCSymbol *KeySym) const { 1870 return getCOFFStaticStructorSection( 1871 getContext(), getContext().getTargetTriple(), true, Priority, KeySym, 1872 cast<MCSectionCOFF>(StaticCtorSection)); 1873 } 1874 1875 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( 1876 unsigned Priority, const MCSymbol *KeySym) const { 1877 return getCOFFStaticStructorSection( 1878 getContext(), getContext().getTargetTriple(), false, Priority, KeySym, 1879 cast<MCSectionCOFF>(StaticDtorSection)); 1880 } 1881 1882 const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( 1883 const GlobalValue *LHS, const GlobalValue *RHS, 1884 const TargetMachine &TM) const { 1885 const Triple &T = TM.getTargetTriple(); 1886 if (T.isOSCygMing()) 1887 return nullptr; 1888 1889 // Our symbols should exist in address space zero, cowardly no-op if 1890 // otherwise. 1891 if (LHS->getType()->getPointerAddressSpace() != 0 || 1892 RHS->getType()->getPointerAddressSpace() != 0) 1893 return nullptr; 1894 1895 // Both ptrtoint instructions must wrap global objects: 1896 // - Only global variables are eligible for image relative relocations. 1897 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. 1898 // We expect __ImageBase to be a global variable without a section, externally 1899 // defined. 1900 // 1901 // It should look something like this: @__ImageBase = external constant i8 1902 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || 1903 LHS->isThreadLocal() || RHS->isThreadLocal() || 1904 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || 1905 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) 1906 return nullptr; 1907 1908 return MCSymbolRefExpr::create(TM.getSymbol(LHS), 1909 MCSymbolRefExpr::VK_COFF_IMGREL32, 1910 getContext()); 1911 } 1912 1913 static std::string APIntToHexString(const APInt &AI) { 1914 unsigned Width = (AI.getBitWidth() / 8) * 2; 1915 std::string HexString = AI.toString(16, /*Signed=*/false); 1916 llvm::transform(HexString, HexString.begin(), tolower); 1917 unsigned Size = HexString.size(); 1918 assert(Width >= Size && "hex string is too large!"); 1919 HexString.insert(HexString.begin(), Width - Size, '0'); 1920 1921 return HexString; 1922 } 1923 1924 static std::string scalarConstantToHexString(const Constant *C) { 1925 Type *Ty = C->getType(); 1926 if (isa<UndefValue>(C)) { 1927 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); 1928 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { 1929 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); 1930 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { 1931 return APIntToHexString(CI->getValue()); 1932 } else { 1933 unsigned NumElements; 1934 if (auto *VTy = dyn_cast<VectorType>(Ty)) 1935 NumElements = cast<FixedVectorType>(VTy)->getNumElements(); 1936 else 1937 NumElements = Ty->getArrayNumElements(); 1938 std::string HexString; 1939 for (int I = NumElements - 1, E = -1; I != E; --I) 1940 HexString += scalarConstantToHexString(C->getAggregateElement(I)); 1941 return HexString; 1942 } 1943 } 1944 1945 MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( 1946 const DataLayout &DL, SectionKind Kind, const Constant *C, 1947 Align &Alignment) const { 1948 if (Kind.isMergeableConst() && C && 1949 getContext().getAsmInfo()->hasCOFFComdatConstants()) { 1950 // This creates comdat sections with the given symbol name, but unless 1951 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol 1952 // will be created with a null storage class, which makes GNU binutils 1953 // error out. 1954 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 1955 COFF::IMAGE_SCN_MEM_READ | 1956 COFF::IMAGE_SCN_LNK_COMDAT; 1957 std::string COMDATSymName; 1958 if (Kind.isMergeableConst4()) { 1959 if (Alignment <= 4) { 1960 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1961 Alignment = Align(4); 1962 } 1963 } else if (Kind.isMergeableConst8()) { 1964 if (Alignment <= 8) { 1965 COMDATSymName = "__real@" + scalarConstantToHexString(C); 1966 Alignment = Align(8); 1967 } 1968 } else if (Kind.isMergeableConst16()) { 1969 // FIXME: These may not be appropriate for non-x86 architectures. 1970 if (Alignment <= 16) { 1971 COMDATSymName = "__xmm@" + scalarConstantToHexString(C); 1972 Alignment = Align(16); 1973 } 1974 } else if (Kind.isMergeableConst32()) { 1975 if (Alignment <= 32) { 1976 COMDATSymName = "__ymm@" + scalarConstantToHexString(C); 1977 Alignment = Align(32); 1978 } 1979 } 1980 1981 if (!COMDATSymName.empty()) 1982 return getContext().getCOFFSection(".rdata", Characteristics, Kind, 1983 COMDATSymName, 1984 COFF::IMAGE_COMDAT_SELECT_ANY); 1985 } 1986 1987 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, 1988 Alignment); 1989 } 1990 1991 //===----------------------------------------------------------------------===// 1992 // Wasm 1993 //===----------------------------------------------------------------------===// 1994 1995 static const Comdat *getWasmComdat(const GlobalValue *GV) { 1996 const Comdat *C = GV->getComdat(); 1997 if (!C) 1998 return nullptr; 1999 2000 if (C->getSelectionKind() != Comdat::Any) 2001 report_fatal_error("WebAssembly COMDATs only support " 2002 "SelectionKind::Any, '" + C->getName() + "' cannot be " 2003 "lowered."); 2004 2005 return C; 2006 } 2007 2008 MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( 2009 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2010 // We don't support explict section names for functions in the wasm object 2011 // format. Each function has to be in its own unique section. 2012 if (isa<Function>(GO)) { 2013 return SelectSectionForGlobal(GO, Kind, TM); 2014 } 2015 2016 StringRef Name = GO->getSection(); 2017 2018 // Certain data sections we treat as named custom sections rather than 2019 // segments within the data section. 2020 // This could be avoided if all data segements (the wasm sense) were 2021 // represented as their own sections (in the llvm sense). 2022 // TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138 2023 if (Name == ".llvmcmd" || Name == ".llvmbc") 2024 Kind = SectionKind::getMetadata(); 2025 2026 StringRef Group = ""; 2027 if (const Comdat *C = getWasmComdat(GO)) { 2028 Group = C->getName(); 2029 } 2030 2031 MCSectionWasm* Section = 2032 getContext().getWasmSection(Name, Kind, Group, 2033 MCContext::GenericSectionID); 2034 2035 return Section; 2036 } 2037 2038 static MCSectionWasm *selectWasmSectionForGlobal( 2039 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, 2040 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { 2041 StringRef Group = ""; 2042 if (const Comdat *C = getWasmComdat(GO)) { 2043 Group = C->getName(); 2044 } 2045 2046 bool UniqueSectionNames = TM.getUniqueSectionNames(); 2047 SmallString<128> Name = getSectionPrefixForGlobal(Kind); 2048 2049 if (const auto *F = dyn_cast<Function>(GO)) { 2050 const auto &OptionalPrefix = F->getSectionPrefix(); 2051 if (OptionalPrefix) 2052 raw_svector_ostream(Name) << '.' << *OptionalPrefix; 2053 } 2054 2055 if (EmitUniqueSection && UniqueSectionNames) { 2056 Name.push_back('.'); 2057 TM.getNameWithPrefix(Name, GO, Mang, true); 2058 } 2059 unsigned UniqueID = MCContext::GenericSectionID; 2060 if (EmitUniqueSection && !UniqueSectionNames) { 2061 UniqueID = *NextUniqueID; 2062 (*NextUniqueID)++; 2063 } 2064 2065 return Ctx.getWasmSection(Name, Kind, Group, UniqueID); 2066 } 2067 2068 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( 2069 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2070 2071 if (Kind.isCommon()) 2072 report_fatal_error("mergable sections not supported yet on wasm"); 2073 2074 // If we have -ffunction-section or -fdata-section then we should emit the 2075 // global value to a uniqued section specifically for it. 2076 bool EmitUniqueSection = false; 2077 if (Kind.isText()) 2078 EmitUniqueSection = TM.getFunctionSections(); 2079 else 2080 EmitUniqueSection = TM.getDataSections(); 2081 EmitUniqueSection |= GO->hasComdat(); 2082 2083 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, 2084 EmitUniqueSection, &NextUniqueID); 2085 } 2086 2087 bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( 2088 bool UsesLabelDifference, const Function &F) const { 2089 // We can always create relative relocations, so use another section 2090 // that can be marked non-executable. 2091 return false; 2092 } 2093 2094 const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( 2095 const GlobalValue *LHS, const GlobalValue *RHS, 2096 const TargetMachine &TM) const { 2097 // We may only use a PLT-relative relocation to refer to unnamed_addr 2098 // functions. 2099 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) 2100 return nullptr; 2101 2102 // Basic sanity checks. 2103 if (LHS->getType()->getPointerAddressSpace() != 0 || 2104 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || 2105 RHS->isThreadLocal()) 2106 return nullptr; 2107 2108 return MCBinaryExpr::createSub( 2109 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, 2110 getContext()), 2111 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); 2112 } 2113 2114 void TargetLoweringObjectFileWasm::InitializeWasm() { 2115 StaticCtorSection = 2116 getContext().getWasmSection(".init_array", SectionKind::getData()); 2117 2118 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit 2119 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables. 2120 TTypeEncoding = dwarf::DW_EH_PE_absptr; 2121 } 2122 2123 MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( 2124 unsigned Priority, const MCSymbol *KeySym) const { 2125 return Priority == UINT16_MAX ? 2126 StaticCtorSection : 2127 getContext().getWasmSection(".init_array." + utostr(Priority), 2128 SectionKind::getData()); 2129 } 2130 2131 MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( 2132 unsigned Priority, const MCSymbol *KeySym) const { 2133 llvm_unreachable("@llvm.global_dtors should have been lowered already"); 2134 return nullptr; 2135 } 2136 2137 //===----------------------------------------------------------------------===// 2138 // XCOFF 2139 //===----------------------------------------------------------------------===// 2140 bool TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock( 2141 const MachineFunction *MF) { 2142 if (!MF->getLandingPads().empty()) 2143 return true; 2144 2145 const Function &F = MF->getFunction(); 2146 if (!F.hasPersonalityFn() || !F.needsUnwindTableEntry()) 2147 return false; 2148 2149 const GlobalValue *Per = 2150 dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts()); 2151 assert(Per && "Personality routine is not a GlobalValue type."); 2152 if (isNoOpWithoutInvoke(classifyEHPersonality(Per))) 2153 return false; 2154 2155 return true; 2156 } 2157 2158 MCSymbol * 2159 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { 2160 return MF->getMMI().getContext().getOrCreateSymbol( 2161 "__ehinfo." + Twine(MF->getFunctionNumber())); 2162 } 2163 2164 MCSymbol * 2165 TargetLoweringObjectFileXCOFF::getTargetSymbol(const GlobalValue *GV, 2166 const TargetMachine &TM) const { 2167 // We always use a qualname symbol for a GV that represents 2168 // a declaration, a function descriptor, or a common symbol. 2169 // If a GV represents a GlobalVariable and -fdata-sections is enabled, we 2170 // also return a qualname so that a label symbol could be avoided. 2171 // It is inherently ambiguous when the GO represents the address of a 2172 // function, as the GO could either represent a function descriptor or a 2173 // function entry point. We choose to always return a function descriptor 2174 // here. 2175 if (const GlobalObject *GO = dyn_cast<GlobalObject>(GV)) { 2176 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) 2177 if (GVar->hasAttribute("toc-data")) 2178 return cast<MCSectionXCOFF>( 2179 SectionForGlobal(GVar, SectionKind::getData(), TM)) 2180 ->getQualNameSymbol(); 2181 2182 if (GO->isDeclarationForLinker()) 2183 return cast<MCSectionXCOFF>(getSectionForExternalReference(GO, TM)) 2184 ->getQualNameSymbol(); 2185 2186 SectionKind GOKind = getKindForGlobal(GO, TM); 2187 if (GOKind.isText()) 2188 return cast<MCSectionXCOFF>( 2189 getSectionForFunctionDescriptor(cast<Function>(GO), TM)) 2190 ->getQualNameSymbol(); 2191 if ((TM.getDataSections() && !GO->hasSection()) || GO->hasCommonLinkage() || 2192 GOKind.isBSSLocal() || GOKind.isThreadBSSLocal()) 2193 return cast<MCSectionXCOFF>(SectionForGlobal(GO, GOKind, TM)) 2194 ->getQualNameSymbol(); 2195 } 2196 2197 // For all other cases, fall back to getSymbol to return the unqualified name. 2198 return nullptr; 2199 } 2200 2201 MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal( 2202 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2203 if (!GO->hasSection()) 2204 report_fatal_error("#pragma clang section is not yet supported"); 2205 2206 StringRef SectionName = GO->getSection(); 2207 2208 // Handle the XCOFF::TD case first, then deal with the rest. 2209 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) 2210 if (GVar->hasAttribute("toc-data")) 2211 return getContext().getXCOFFSection( 2212 SectionName, Kind, 2213 XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD), 2214 /* MultiSymbolsAllowed*/ true); 2215 2216 XCOFF::StorageMappingClass MappingClass; 2217 if (Kind.isText()) 2218 MappingClass = XCOFF::XMC_PR; 2219 else if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) 2220 MappingClass = XCOFF::XMC_RW; 2221 else if (Kind.isReadOnly()) 2222 MappingClass = XCOFF::XMC_RO; 2223 else 2224 report_fatal_error("XCOFF other section types not yet implemented."); 2225 2226 return getContext().getXCOFFSection( 2227 SectionName, Kind, XCOFF::CsectProperties(MappingClass, XCOFF::XTY_SD), 2228 /* MultiSymbolsAllowed*/ true); 2229 } 2230 2231 MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( 2232 const GlobalObject *GO, const TargetMachine &TM) const { 2233 assert(GO->isDeclarationForLinker() && 2234 "Tried to get ER section for a defined global."); 2235 2236 SmallString<128> Name; 2237 getNameWithPrefix(Name, GO, TM); 2238 2239 XCOFF::StorageMappingClass SMC = 2240 isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA; 2241 if (GO->isThreadLocal()) 2242 SMC = XCOFF::XMC_UL; 2243 2244 // Externals go into a csect of type ER. 2245 return getContext().getXCOFFSection( 2246 Name, SectionKind::getMetadata(), 2247 XCOFF::CsectProperties(SMC, XCOFF::XTY_ER)); 2248 } 2249 2250 MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( 2251 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 2252 // Handle the XCOFF::TD case first, then deal with the rest. 2253 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) 2254 if (GVar->hasAttribute("toc-data")) { 2255 SmallString<128> Name; 2256 getNameWithPrefix(Name, GO, TM); 2257 return getContext().getXCOFFSection( 2258 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, XCOFF::XTY_SD), 2259 /* MultiSymbolsAllowed*/ true); 2260 } 2261 2262 // Common symbols go into a csect with matching name which will get mapped 2263 // into the .bss section. 2264 // Zero-initialized local TLS symbols go into a csect with matching name which 2265 // will get mapped into the .tbss section. 2266 if (Kind.isBSSLocal() || GO->hasCommonLinkage() || Kind.isThreadBSSLocal()) { 2267 SmallString<128> Name; 2268 getNameWithPrefix(Name, GO, TM); 2269 XCOFF::StorageMappingClass SMC = Kind.isBSSLocal() ? XCOFF::XMC_BS 2270 : Kind.isCommon() ? XCOFF::XMC_RW 2271 : XCOFF::XMC_UL; 2272 return getContext().getXCOFFSection( 2273 Name, Kind, XCOFF::CsectProperties(SMC, XCOFF::XTY_CM)); 2274 } 2275 2276 if (Kind.isMergeableCString()) { 2277 Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( 2278 cast<GlobalVariable>(GO)); 2279 2280 unsigned EntrySize = getEntrySizeForKind(Kind); 2281 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; 2282 SmallString<128> Name; 2283 Name = SizeSpec + utostr(Alignment.value()); 2284 2285 if (TM.getDataSections()) 2286 getNameWithPrefix(Name, GO, TM); 2287 2288 return getContext().getXCOFFSection( 2289 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD), 2290 /* MultiSymbolsAllowed*/ !TM.getDataSections()); 2291 } 2292 2293 if (Kind.isText()) { 2294 if (TM.getFunctionSections()) { 2295 return cast<MCSymbolXCOFF>(getFunctionEntryPointSymbol(GO, TM)) 2296 ->getRepresentedCsect(); 2297 } 2298 return TextSection; 2299 } 2300 2301 // TODO: We may put Kind.isReadOnlyWithRel() under option control, because 2302 // user may want to have read-only data with relocations placed into a 2303 // read-only section by the compiler. 2304 // For BSS kind, zero initialized data must be emitted to the .data section 2305 // because external linkage control sections that get mapped to the .bss 2306 // section will be linked as tentative defintions, which is only appropriate 2307 // for SectionKind::Common. 2308 if (Kind.isData() || Kind.isReadOnlyWithRel() || Kind.isBSS()) { 2309 if (TM.getDataSections()) { 2310 SmallString<128> Name; 2311 getNameWithPrefix(Name, GO, TM); 2312 return getContext().getXCOFFSection( 2313 Name, SectionKind::getData(), 2314 XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)); 2315 } 2316 return DataSection; 2317 } 2318 2319 if (Kind.isReadOnly()) { 2320 if (TM.getDataSections()) { 2321 SmallString<128> Name; 2322 getNameWithPrefix(Name, GO, TM); 2323 return getContext().getXCOFFSection( 2324 Name, SectionKind::getReadOnly(), 2325 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2326 } 2327 return ReadOnlySection; 2328 } 2329 2330 // External/weak TLS data and initialized local TLS data are not eligible 2331 // to be put into common csect. If data sections are enabled, thread 2332 // data are emitted into separate sections. Otherwise, thread data 2333 // are emitted into the .tdata section. 2334 if (Kind.isThreadLocal()) { 2335 if (TM.getDataSections()) { 2336 SmallString<128> Name; 2337 getNameWithPrefix(Name, GO, TM); 2338 return getContext().getXCOFFSection( 2339 Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TL, XCOFF::XTY_SD)); 2340 } 2341 return TLSDataSection; 2342 } 2343 2344 report_fatal_error("XCOFF other section types not yet implemented."); 2345 } 2346 2347 MCSection *TargetLoweringObjectFileXCOFF::getSectionForJumpTable( 2348 const Function &F, const TargetMachine &TM) const { 2349 assert (!F.getComdat() && "Comdat not supported on XCOFF."); 2350 2351 if (!TM.getFunctionSections()) 2352 return ReadOnlySection; 2353 2354 // If the function can be removed, produce a unique section so that 2355 // the table doesn't prevent the removal. 2356 SmallString<128> NameStr(".rodata.jmp.."); 2357 getNameWithPrefix(NameStr, &F, TM); 2358 return getContext().getXCOFFSection( 2359 NameStr, SectionKind::getReadOnly(), 2360 XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD)); 2361 } 2362 2363 bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection( 2364 bool UsesLabelDifference, const Function &F) const { 2365 return false; 2366 } 2367 2368 /// Given a mergeable constant with the specified size and relocation 2369 /// information, return a section that it should be placed in. 2370 MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant( 2371 const DataLayout &DL, SectionKind Kind, const Constant *C, 2372 Align &Alignment) const { 2373 //TODO: Enable emiting constant pool to unique sections when we support it. 2374 return ReadOnlySection; 2375 } 2376 2377 void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx, 2378 const TargetMachine &TgtM) { 2379 TargetLoweringObjectFile::Initialize(Ctx, TgtM); 2380 TTypeEncoding = 2381 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel | 2382 (TgtM.getTargetTriple().isArch32Bit() ? dwarf::DW_EH_PE_sdata4 2383 : dwarf::DW_EH_PE_sdata8); 2384 PersonalityEncoding = 0; 2385 LSDAEncoding = 0; 2386 CallSiteEncoding = dwarf::DW_EH_PE_udata4; 2387 } 2388 2389 MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection( 2390 unsigned Priority, const MCSymbol *KeySym) const { 2391 report_fatal_error("no static constructor section on AIX"); 2392 } 2393 2394 MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection( 2395 unsigned Priority, const MCSymbol *KeySym) const { 2396 report_fatal_error("no static destructor section on AIX"); 2397 } 2398 2399 const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference( 2400 const GlobalValue *LHS, const GlobalValue *RHS, 2401 const TargetMachine &TM) const { 2402 report_fatal_error("XCOFF not yet implemented."); 2403 } 2404 2405 XCOFF::StorageClass 2406 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(const GlobalValue *GV) { 2407 assert(!isa<GlobalIFunc>(GV) && "GlobalIFunc is not supported on AIX."); 2408 2409 switch (GV->getLinkage()) { 2410 case GlobalValue::InternalLinkage: 2411 case GlobalValue::PrivateLinkage: 2412 return XCOFF::C_HIDEXT; 2413 case GlobalValue::ExternalLinkage: 2414 case GlobalValue::CommonLinkage: 2415 case GlobalValue::AvailableExternallyLinkage: 2416 return XCOFF::C_EXT; 2417 case GlobalValue::ExternalWeakLinkage: 2418 case GlobalValue::LinkOnceAnyLinkage: 2419 case GlobalValue::LinkOnceODRLinkage: 2420 case GlobalValue::WeakAnyLinkage: 2421 case GlobalValue::WeakODRLinkage: 2422 return XCOFF::C_WEAKEXT; 2423 case GlobalValue::AppendingLinkage: 2424 report_fatal_error( 2425 "There is no mapping that implements AppendingLinkage for XCOFF."); 2426 } 2427 llvm_unreachable("Unknown linkage type!"); 2428 } 2429 2430 MCSymbol *TargetLoweringObjectFileXCOFF::getFunctionEntryPointSymbol( 2431 const GlobalValue *Func, const TargetMachine &TM) const { 2432 assert( 2433 (isa<Function>(Func) || 2434 (isa<GlobalAlias>(Func) && 2435 isa_and_nonnull<Function>(cast<GlobalAlias>(Func)->getBaseObject()))) && 2436 "Func must be a function or an alias which has a function as base " 2437 "object."); 2438 2439 SmallString<128> NameStr; 2440 NameStr.push_back('.'); 2441 getNameWithPrefix(NameStr, Func, TM); 2442 2443 // When -function-sections is enabled and explicit section is not specified, 2444 // it's not necessary to emit function entry point label any more. We will use 2445 // function entry point csect instead. And for function delcarations, the 2446 // undefined symbols gets treated as csect with XTY_ER property. 2447 if (((TM.getFunctionSections() && !Func->hasSection()) || 2448 Func->isDeclaration()) && 2449 isa<Function>(Func)) { 2450 return getContext() 2451 .getXCOFFSection( 2452 NameStr, SectionKind::getText(), 2453 XCOFF::CsectProperties(XCOFF::XMC_PR, Func->isDeclaration() 2454 ? XCOFF::XTY_ER 2455 : XCOFF::XTY_SD)) 2456 ->getQualNameSymbol(); 2457 } 2458 2459 return getContext().getOrCreateSymbol(NameStr); 2460 } 2461 2462 MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( 2463 const Function *F, const TargetMachine &TM) const { 2464 SmallString<128> NameStr; 2465 getNameWithPrefix(NameStr, F, TM); 2466 return getContext().getXCOFFSection( 2467 NameStr, SectionKind::getData(), 2468 XCOFF::CsectProperties(XCOFF::XMC_DS, XCOFF::XTY_SD)); 2469 } 2470 2471 MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( 2472 const MCSymbol *Sym, const TargetMachine &TM) const { 2473 // Use TE storage-mapping class when large code model is enabled so that 2474 // the chance of needing -bbigtoc is decreased. 2475 return getContext().getXCOFFSection( 2476 cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(), 2477 XCOFF::CsectProperties( 2478 TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE : XCOFF::XMC_TC, 2479 XCOFF::XTY_SD)); 2480 } 2481