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