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