| 1 | //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===// |
| 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 | #include "Linux.h" |
| 10 | #include "Arch/ARM.h" |
| 11 | #include "Arch/LoongArch.h" |
| 12 | #include "Arch/Mips.h" |
| 13 | #include "Arch/PPC.h" |
| 14 | #include "Arch/RISCV.h" |
| 15 | #include "clang/Config/config.h" |
| 16 | #include "clang/Driver/CommonArgs.h" |
| 17 | #include "clang/Driver/Distro.h" |
| 18 | #include "clang/Driver/Driver.h" |
| 19 | #include "clang/Driver/SanitizerArgs.h" |
| 20 | #include "clang/Options/Options.h" |
| 21 | #include "llvm/Option/ArgList.h" |
| 22 | #include "llvm/ProfileData/InstrProf.h" |
| 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/ScopedPrinter.h" |
| 25 | #include "llvm/Support/VirtualFileSystem.h" |
| 26 | |
| 27 | using namespace clang::driver; |
| 28 | using namespace clang::driver::toolchains; |
| 29 | using namespace clang; |
| 30 | using namespace llvm::opt; |
| 31 | |
| 32 | using tools::addPathIfExists; |
| 33 | |
| 34 | /// Get our best guess at the multiarch triple for a target. |
| 35 | /// |
| 36 | /// Debian-based systems are starting to use a multiarch setup where they use |
| 37 | /// a target-triple directory in the library and header search paths. |
| 38 | /// Unfortunately, this triple does not align with the vanilla target triple, |
| 39 | /// so we provide a rough mapping here. |
| 40 | std::string Linux::getMultiarchTriple(const Driver &D, |
| 41 | const llvm::Triple &TargetTriple, |
| 42 | StringRef SysRoot) const { |
| 43 | llvm::Triple::EnvironmentType TargetEnvironment = |
| 44 | TargetTriple.getEnvironment(); |
| 45 | bool IsAndroid = TargetTriple.isAndroid(); |
| 46 | bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6; |
| 47 | bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32; |
| 48 | |
| 49 | // For most architectures, just use whatever we have rather than trying to be |
| 50 | // clever. |
| 51 | switch (TargetTriple.getArch()) { |
| 52 | default: |
| 53 | break; |
| 54 | |
| 55 | // We use the existence of '/lib/<triple>' as a directory to detect some |
| 56 | // common linux triples that don't quite match the Clang triple for both |
| 57 | // 32-bit and 64-bit targets. Multiarch fixes its install triples to these |
| 58 | // regardless of what the actual target triple is. |
| 59 | case llvm::Triple::arm: |
| 60 | case llvm::Triple::thumb: |
| 61 | if (IsAndroid) |
| 62 | return "arm-linux-androideabi" ; |
| 63 | if (TargetEnvironment == llvm::Triple::GNUEABIHF || |
| 64 | TargetEnvironment == llvm::Triple::MuslEABIHF || |
| 65 | TargetEnvironment == llvm::Triple::EABIHF) |
| 66 | return "arm-linux-gnueabihf" ; |
| 67 | return "arm-linux-gnueabi" ; |
| 68 | case llvm::Triple::armeb: |
| 69 | case llvm::Triple::thumbeb: |
| 70 | if (TargetEnvironment == llvm::Triple::GNUEABIHF || |
| 71 | TargetEnvironment == llvm::Triple::MuslEABIHF || |
| 72 | TargetEnvironment == llvm::Triple::EABIHF) |
| 73 | return "armeb-linux-gnueabihf" ; |
| 74 | return "armeb-linux-gnueabi" ; |
| 75 | case llvm::Triple::x86: |
| 76 | if (IsAndroid) |
| 77 | return "i686-linux-android" ; |
| 78 | return "i386-linux-gnu" ; |
| 79 | case llvm::Triple::x86_64: |
| 80 | if (IsAndroid) |
| 81 | return "x86_64-linux-android" ; |
| 82 | if (TargetEnvironment == llvm::Triple::GNUX32) |
| 83 | return "x86_64-linux-gnux32" ; |
| 84 | return "x86_64-linux-gnu" ; |
| 85 | case llvm::Triple::aarch64: |
| 86 | if (IsAndroid) |
| 87 | return "aarch64-linux-android" ; |
| 88 | if (hasEffectiveTriple() && |
| 89 | getEffectiveTriple().getEnvironment() == llvm::Triple::PAuthTest) |
| 90 | return "aarch64-linux-pauthtest" ; |
| 91 | return "aarch64-linux-gnu" ; |
| 92 | case llvm::Triple::aarch64_be: |
| 93 | return "aarch64_be-linux-gnu" ; |
| 94 | |
| 95 | case llvm::Triple::loongarch64: { |
| 96 | const char *Libc; |
| 97 | const char *FPFlavor; |
| 98 | |
| 99 | if (TargetTriple.isGNUEnvironment()) { |
| 100 | Libc = "gnu" ; |
| 101 | } else if (TargetTriple.isMusl()) { |
| 102 | Libc = "musl" ; |
| 103 | } else { |
| 104 | return TargetTriple.str(); |
| 105 | } |
| 106 | |
| 107 | switch (TargetEnvironment) { |
| 108 | default: |
| 109 | return TargetTriple.str(); |
| 110 | case llvm::Triple::GNUSF: |
| 111 | case llvm::Triple::MuslSF: |
| 112 | FPFlavor = "sf" ; |
| 113 | break; |
| 114 | case llvm::Triple::GNUF32: |
| 115 | case llvm::Triple::MuslF32: |
| 116 | FPFlavor = "f32" ; |
| 117 | break; |
| 118 | case llvm::Triple::GNU: |
| 119 | case llvm::Triple::GNUF64: |
| 120 | case llvm::Triple::Musl: |
| 121 | // This was going to be "f64" in an earlier Toolchain Conventions |
| 122 | // revision, but starting from Feb 2023 the F64 ABI variants are |
| 123 | // unmarked in their canonical forms. |
| 124 | FPFlavor = "" ; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | return (Twine("loongarch64-linux-" ) + Libc + FPFlavor).str(); |
| 129 | } |
| 130 | |
| 131 | case llvm::Triple::m68k: |
| 132 | return "m68k-linux-gnu" ; |
| 133 | |
| 134 | case llvm::Triple::mips: |
| 135 | return IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu" ; |
| 136 | case llvm::Triple::mipsel: |
| 137 | return IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu" ; |
| 138 | case llvm::Triple::mips64: { |
| 139 | std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64" ) + |
| 140 | "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64" ); |
| 141 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib" , B: MT))) |
| 142 | return MT; |
| 143 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/mips64-linux-gnu" ))) |
| 144 | return "mips64-linux-gnu" ; |
| 145 | break; |
| 146 | } |
| 147 | case llvm::Triple::mips64el: { |
| 148 | std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el" ) + |
| 149 | "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64" ); |
| 150 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib" , B: MT))) |
| 151 | return MT; |
| 152 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/mips64el-linux-gnu" ))) |
| 153 | return "mips64el-linux-gnu" ; |
| 154 | break; |
| 155 | } |
| 156 | case llvm::Triple::ppc: |
| 157 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/powerpc-linux-gnuspe" ))) |
| 158 | return "powerpc-linux-gnuspe" ; |
| 159 | return "powerpc-linux-gnu" ; |
| 160 | case llvm::Triple::ppcle: |
| 161 | return "powerpcle-linux-gnu" ; |
| 162 | case llvm::Triple::ppc64: |
| 163 | return "powerpc64-linux-gnu" ; |
| 164 | case llvm::Triple::ppc64le: |
| 165 | return "powerpc64le-linux-gnu" ; |
| 166 | case llvm::Triple::riscv64: |
| 167 | if (IsAndroid) |
| 168 | return "riscv64-linux-android" ; |
| 169 | return "riscv64-linux-gnu" ; |
| 170 | case llvm::Triple::sparc: |
| 171 | return "sparc-linux-gnu" ; |
| 172 | case llvm::Triple::sparcv9: |
| 173 | return "sparc64-linux-gnu" ; |
| 174 | case llvm::Triple::systemz: |
| 175 | return "s390x-linux-gnu" ; |
| 176 | } |
| 177 | return TargetTriple.str(); |
| 178 | } |
| 179 | |
| 180 | static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { |
| 181 | if (Triple.isMIPS()) { |
| 182 | // lib32 directory has a special meaning on MIPS targets. |
| 183 | // It contains N32 ABI binaries. Use this folder if produce |
| 184 | // code for N32 ABI only. |
| 185 | if (tools::mips::hasMipsAbiArg(Args, Value: "n32" )) |
| 186 | return "lib32" ; |
| 187 | return Triple.isArch32Bit() ? "lib" : "lib64" ; |
| 188 | } |
| 189 | |
| 190 | // It happens that only x86, PPC and SPARC use the 'lib32' variant of |
| 191 | // oslibdir, and using that variant while targeting other architectures causes |
| 192 | // problems because the libraries are laid out in shared system roots that |
| 193 | // can't cope with a 'lib32' library search path being considered. So we only |
| 194 | // enable them when we know we may need it. |
| 195 | // |
| 196 | // FIXME: This is a bit of a hack. We should really unify this code for |
| 197 | // reasoning about oslibdir spellings with the lib dir spellings in the |
| 198 | // GCCInstallationDetector, but that is a more significant refactoring. |
| 199 | if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() || |
| 200 | Triple.getArch() == llvm::Triple::sparc) |
| 201 | return "lib32" ; |
| 202 | |
| 203 | if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32()) |
| 204 | return "libx32" ; |
| 205 | |
| 206 | if (Triple.getArch() == llvm::Triple::riscv32) |
| 207 | return "lib32" ; |
| 208 | |
| 209 | if (Triple.getArch() == llvm::Triple::loongarch32) { |
| 210 | switch (Triple.getEnvironment()) { |
| 211 | default: |
| 212 | return "lib32" ; |
| 213 | case llvm::Triple::GNUSF: |
| 214 | case llvm::Triple::MuslSF: |
| 215 | return "lib32/sf" ; |
| 216 | case llvm::Triple::GNUF32: |
| 217 | case llvm::Triple::MuslF32: |
| 218 | return "lib32/f32" ; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return Triple.isArch32Bit() ? "lib" : "lib64" ; |
| 223 | } |
| 224 | |
| 225 | Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 226 | : Generic_ELF(D, Triple, Args) { |
| 227 | GCCInstallation.TripleToDebianMultiarch = [](const llvm::Triple &T) { |
| 228 | StringRef TripleStr = T.str(); |
| 229 | StringRef DebianMultiarch = |
| 230 | T.getArch() == llvm::Triple::x86 ? "i386-linux-gnu" : TripleStr; |
| 231 | return DebianMultiarch; |
| 232 | }; |
| 233 | |
| 234 | GCCInstallation.init(TargetTriple: Triple, Args); |
| 235 | Multilibs = GCCInstallation.getMultilibs(); |
| 236 | SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()}); |
| 237 | llvm::Triple::ArchType Arch = Triple.getArch(); |
| 238 | std::string SysRoot = computeSysRoot(); |
| 239 | ToolChain::path_list &PPaths = getProgramPaths(); |
| 240 | |
| 241 | Generic_GCC::PushPPaths(PPaths); |
| 242 | |
| 243 | Distro Distro(D.getVFS(), Triple); |
| 244 | |
| 245 | if (Distro.IsAlpineLinux() || Triple.isAndroid()) { |
| 246 | ExtraOpts.push_back(x: "-z" ); |
| 247 | ExtraOpts.push_back(x: "now" ); |
| 248 | } |
| 249 | |
| 250 | if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() || |
| 251 | Triple.isAndroid()) { |
| 252 | ExtraOpts.push_back(x: "-z" ); |
| 253 | ExtraOpts.push_back(x: "relro" ); |
| 254 | } |
| 255 | |
| 256 | // Note, lld from 11 onwards default max-page-size to 65536 for both ARM and |
| 257 | // AArch64. |
| 258 | if (Triple.isAndroid()) { |
| 259 | if (Triple.isARM()) { |
| 260 | // Android ARM uses max-page-size=4096 to reduce VMA usage. |
| 261 | ExtraOpts.push_back(x: "-z" ); |
| 262 | ExtraOpts.push_back(x: "max-page-size=4096" ); |
| 263 | } else if (Triple.isAArch64() || Triple.getArch() == llvm::Triple::x86_64) { |
| 264 | // Android AArch64 uses max-page-size=16384 to support 4k/16k page sizes. |
| 265 | // Android emulates a 16k page size for app testing on x86_64 machines. |
| 266 | ExtraOpts.push_back(x: "-z" ); |
| 267 | ExtraOpts.push_back(x: "max-page-size=16384" ); |
| 268 | } |
| 269 | if (Triple.isAndroidVersionLT(Major: 29)) { |
| 270 | // https://github.com/android/ndk/issues/1196 |
| 271 | // The unwinder used by the crash handler on versions of Android prior to |
| 272 | // API 29 did not correctly handle binaries built with rosegment, which is |
| 273 | // enabled by default for LLD. Android only supports LLD, so it's not an |
| 274 | // issue that this flag is not accepted by other linkers. |
| 275 | ExtraOpts.push_back(x: "--no-rosegment" ); |
| 276 | } |
| 277 | if (!Triple.isAndroidVersionLT(Major: 28)) { |
| 278 | // Android supports relr packing starting with API 28 and had its own |
| 279 | // flavor (--pack-dyn-relocs=android) starting in API 23. |
| 280 | // TODO: It's possible to use both with --pack-dyn-relocs=android+relr, |
| 281 | // but we need to gather some data on the impact of that form before we |
| 282 | // can know if it's a good default. |
| 283 | // On the other hand, relr should always be an improvement. |
| 284 | ExtraOpts.push_back(x: "--use-android-relr-tags" ); |
| 285 | ExtraOpts.push_back(x: "--pack-dyn-relocs=relr" ); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (GCCInstallation.getParentLibPath().contains(Other: "opt/rh/" )) |
| 290 | // With devtoolset on RHEL, we want to add a bin directory that is relative |
| 291 | // to the detected gcc install, because if we are using devtoolset gcc then |
| 292 | // we want to use other tools from devtoolset (e.g. ld) instead of the |
| 293 | // standard system tools. |
| 294 | PPaths.push_back(Elt: Twine(GCCInstallation.getParentLibPath() + |
| 295 | "/../bin" ).str()); |
| 296 | |
| 297 | if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) |
| 298 | ExtraOpts.push_back(x: "-X" ); |
| 299 | |
| 300 | const bool IsAndroid = Triple.isAndroid(); |
| 301 | const bool IsMips = Triple.isMIPS(); |
| 302 | const bool IsHexagon = Arch == llvm::Triple::hexagon; |
| 303 | const bool IsRISCV = Triple.isRISCV(); |
| 304 | const bool IsCSKY = Triple.isCSKY(); |
| 305 | |
| 306 | if (IsCSKY && !SelectedMultilibs.empty()) |
| 307 | SysRoot = SysRoot + SelectedMultilibs.back().osSuffix(); |
| 308 | |
| 309 | if ((IsMips || IsCSKY) && !SysRoot.empty()) |
| 310 | ExtraOpts.push_back(x: "--sysroot=" + SysRoot); |
| 311 | |
| 312 | // Do not use 'gnu' hash style for Mips targets because .gnu.hash |
| 313 | // and the MIPS ABI require .dynsym to be sorted in different ways. |
| 314 | // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS |
| 315 | // ABI requires a mapping between the GOT and the symbol table. |
| 316 | // Android loader does not support .gnu.hash until API 23. |
| 317 | // Hexagon linker/loader does not support .gnu.hash. |
| 318 | // SUSE SLES 11 will stop being supported Mar 2028. |
| 319 | if (!IsMips && !IsHexagon) { |
| 320 | if (Distro.IsOpenSUSE() || (IsAndroid && Triple.isAndroidVersionLT(Major: 23))) |
| 321 | ExtraOpts.push_back(x: "--hash-style=both" ); |
| 322 | else |
| 323 | ExtraOpts.push_back(x: "--hash-style=gnu" ); |
| 324 | } |
| 325 | |
| 326 | #ifdef ENABLE_LINKER_BUILD_ID |
| 327 | ExtraOpts.push_back("--build-id" ); |
| 328 | #endif |
| 329 | |
| 330 | // The selection of paths to try here is designed to match the patterns which |
| 331 | // the GCC driver itself uses, as this is part of the GCC-compatible driver. |
| 332 | // This was determined by running GCC in a fake filesystem, creating all |
| 333 | // possible permutations of these directories, and seeing which ones it added |
| 334 | // to the link paths. |
| 335 | path_list &Paths = getFilePaths(); |
| 336 | |
| 337 | const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); |
| 338 | const std::string MultiarchTriple = getMultiarchTriple(D, TargetTriple: Triple, SysRoot); |
| 339 | |
| 340 | // mips32: Debian multilib, we use /libo32, while in other case, /lib is |
| 341 | // used. We need add both libo32 and /lib. |
| 342 | if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) { |
| 343 | Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir: "libo32" , MultiarchTriple, Paths); |
| 344 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/libo32" ), Paths); |
| 345 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/libo32" ), Paths); |
| 346 | } |
| 347 | Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); |
| 348 | |
| 349 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib" , B: MultiarchTriple), Paths); |
| 350 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib/.." , B: OSLibDir), Paths); |
| 351 | |
| 352 | if (IsAndroid) { |
| 353 | // Android sysroots contain a library directory for each supported OS |
| 354 | // version as well as some unversioned libraries in the usual multiarch |
| 355 | // directory. |
| 356 | addPathIfExists( |
| 357 | D, |
| 358 | Path: concat(Path: SysRoot, A: "/usr/lib" , B: MultiarchTriple, |
| 359 | C: llvm::to_string(Value: Triple.getEnvironmentVersion().getMajor())), |
| 360 | Paths); |
| 361 | } |
| 362 | |
| 363 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib" , B: MultiarchTriple), Paths); |
| 364 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr" , B: OSLibDir), Paths); |
| 365 | if (IsRISCV) { |
| 366 | StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); |
| 367 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/" , B: OSLibDir, C: ABIName), Paths); |
| 368 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr" , B: OSLibDir, C: ABIName), Paths); |
| 369 | } |
| 370 | |
| 371 | Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); |
| 372 | |
| 373 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib" ), Paths); |
| 374 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib" ), Paths); |
| 375 | } |
| 376 | |
| 377 | ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const { |
| 378 | if (getTriple().isAndroid()) |
| 379 | return ToolChain::RLT_CompilerRT; |
| 380 | return Generic_ELF::GetDefaultRuntimeLibType(); |
| 381 | } |
| 382 | |
| 383 | unsigned Linux::GetDefaultDwarfVersion() const { |
| 384 | if (getTriple().isAndroid()) |
| 385 | return 4; |
| 386 | return ToolChain::GetDefaultDwarfVersion(); |
| 387 | } |
| 388 | |
| 389 | ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const { |
| 390 | if (getTriple().isAndroid()) |
| 391 | return ToolChain::CST_Libcxx; |
| 392 | return ToolChain::CST_Libstdcxx; |
| 393 | } |
| 394 | |
| 395 | bool Linux::HasNativeLLVMSupport() const { return true; } |
| 396 | |
| 397 | Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } |
| 398 | |
| 399 | Tool *Linux::buildStaticLibTool() const { |
| 400 | return new tools::gnutools::StaticLibTool(*this); |
| 401 | } |
| 402 | |
| 403 | Tool *Linux::buildAssembler() const { |
| 404 | return new tools::gnutools::Assembler(*this); |
| 405 | } |
| 406 | |
| 407 | std::string Linux::computeSysRoot() const { |
| 408 | if (!getDriver().SysRoot.empty()) |
| 409 | return getDriver().SysRoot; |
| 410 | |
| 411 | if (getTriple().isAndroid()) { |
| 412 | // Android toolchains typically include a sysroot at ../sysroot relative to |
| 413 | // the clang binary. |
| 414 | const StringRef ClangDir = getDriver().Dir; |
| 415 | std::string AndroidSysRootPath = (ClangDir + "/../sysroot" ).str(); |
| 416 | if (getVFS().exists(Path: AndroidSysRootPath)) |
| 417 | return AndroidSysRootPath; |
| 418 | } |
| 419 | |
| 420 | if (getTriple().isCSKY()) { |
| 421 | // CSKY toolchains use different names for sysroot folder. |
| 422 | if (!GCCInstallation.isValid()) |
| 423 | return std::string(); |
| 424 | // GCCInstallation.getInstallPath() = |
| 425 | // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0 |
| 426 | // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc |
| 427 | std::string Path = (GCCInstallation.getInstallPath() + "/../../../../" + |
| 428 | GCCInstallation.getTriple().str() + "/libc" ) |
| 429 | .str(); |
| 430 | if (getVFS().exists(Path)) |
| 431 | return Path; |
| 432 | return std::string(); |
| 433 | } |
| 434 | |
| 435 | if (!GCCInstallation.isValid() || !getTriple().isMIPS()) |
| 436 | return std::string(); |
| 437 | |
| 438 | // Standalone MIPS toolchains use different names for sysroot folder |
| 439 | // and put it into different places. Here we try to check some known |
| 440 | // variants. |
| 441 | |
| 442 | const StringRef InstallDir = GCCInstallation.getInstallPath(); |
| 443 | const StringRef TripleStr = GCCInstallation.getTriple().str(); |
| 444 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
| 445 | |
| 446 | std::string Path = |
| 447 | (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) |
| 448 | .str(); |
| 449 | |
| 450 | if (getVFS().exists(Path)) |
| 451 | return Path; |
| 452 | |
| 453 | Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); |
| 454 | |
| 455 | if (getVFS().exists(Path)) |
| 456 | return Path; |
| 457 | |
| 458 | return std::string(); |
| 459 | } |
| 460 | |
| 461 | static void setPAuthABIInTriple(const Driver &D, const ArgList &Args, |
| 462 | llvm::Triple &Triple) { |
| 463 | Arg *ABIArg = Args.getLastArg(Ids: options::OPT_mabi_EQ); |
| 464 | bool HasPAuthABI = |
| 465 | ABIArg ? (StringRef(ABIArg->getValue()) == "pauthtest" ) : false; |
| 466 | |
| 467 | switch (Triple.getEnvironment()) { |
| 468 | case llvm::Triple::UnknownEnvironment: |
| 469 | if (HasPAuthABI) |
| 470 | Triple.setEnvironment(llvm::Triple::PAuthTest); |
| 471 | break; |
| 472 | case llvm::Triple::PAuthTest: |
| 473 | break; |
| 474 | default: |
| 475 | if (HasPAuthABI) |
| 476 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 477 | << ABIArg->getAsString(Args) << Triple.getTriple(); |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | std::string Linux::ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, |
| 483 | types::ID InputType) const { |
| 484 | std::string TripleString = |
| 485 | Generic_ELF::ComputeEffectiveClangTriple(Args, InputType); |
| 486 | if (getTriple().isAArch64()) { |
| 487 | llvm::Triple Triple(TripleString); |
| 488 | setPAuthABIInTriple(D: getDriver(), Args, Triple); |
| 489 | return Triple.getTriple(); |
| 490 | } |
| 491 | return TripleString; |
| 492 | } |
| 493 | |
| 494 | // Each combination of options here forms a signing schema, and in most cases |
| 495 | // each signing schema is its own incompatible ABI. The default values of the |
| 496 | // options represent the default signing schema. |
| 497 | static void handlePAuthABI(const Driver &D, const ArgList &DriverArgs, |
| 498 | ArgStringList &CC1Args) { |
| 499 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_intrinsics, |
| 500 | Ids: options::OPT_fno_ptrauth_intrinsics)) |
| 501 | CC1Args.push_back(Elt: "-fptrauth-intrinsics" ); |
| 502 | |
| 503 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_calls, |
| 504 | Ids: options::OPT_fno_ptrauth_calls)) |
| 505 | CC1Args.push_back(Elt: "-fptrauth-calls" ); |
| 506 | |
| 507 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_returns, |
| 508 | Ids: options::OPT_fno_ptrauth_returns)) |
| 509 | CC1Args.push_back(Elt: "-fptrauth-returns" ); |
| 510 | |
| 511 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_auth_traps, |
| 512 | Ids: options::OPT_fno_ptrauth_auth_traps)) |
| 513 | CC1Args.push_back(Elt: "-fptrauth-auth-traps" ); |
| 514 | |
| 515 | if (!DriverArgs.hasArg( |
| 516 | Ids: options::OPT_fptrauth_vtable_pointer_address_discrimination, |
| 517 | Ids: options::OPT_fno_ptrauth_vtable_pointer_address_discrimination)) |
| 518 | CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-address-discrimination" ); |
| 519 | |
| 520 | if (!DriverArgs.hasArg( |
| 521 | Ids: options::OPT_fptrauth_vtable_pointer_type_discrimination, |
| 522 | Ids: options::OPT_fno_ptrauth_vtable_pointer_type_discrimination)) |
| 523 | CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-type-discrimination" ); |
| 524 | |
| 525 | if (!DriverArgs.hasArg( |
| 526 | Ids: options::OPT_fptrauth_type_info_vtable_pointer_discrimination, |
| 527 | Ids: options::OPT_fno_ptrauth_type_info_vtable_pointer_discrimination)) |
| 528 | CC1Args.push_back(Elt: "-fptrauth-type-info-vtable-pointer-discrimination" ); |
| 529 | |
| 530 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_indirect_gotos, |
| 531 | Ids: options::OPT_fno_ptrauth_indirect_gotos)) |
| 532 | CC1Args.push_back(Elt: "-fptrauth-indirect-gotos" ); |
| 533 | |
| 534 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_init_fini, |
| 535 | Ids: options::OPT_fno_ptrauth_init_fini)) |
| 536 | CC1Args.push_back(Elt: "-fptrauth-init-fini" ); |
| 537 | |
| 538 | if (!DriverArgs.hasArg( |
| 539 | Ids: options::OPT_fptrauth_init_fini_address_discrimination, |
| 540 | Ids: options::OPT_fno_ptrauth_init_fini_address_discrimination)) |
| 541 | CC1Args.push_back(Elt: "-fptrauth-init-fini-address-discrimination" ); |
| 542 | |
| 543 | if (!DriverArgs.hasArg(Ids: options::OPT_faarch64_jump_table_hardening, |
| 544 | Ids: options::OPT_fno_aarch64_jump_table_hardening)) |
| 545 | CC1Args.push_back(Elt: "-faarch64-jump-table-hardening" ); |
| 546 | } |
| 547 | |
| 548 | void Linux::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 549 | llvm::opt::ArgStringList &CC1Args, |
| 550 | Action::OffloadKind DeviceOffloadKind) const { |
| 551 | llvm::Triple Triple(ComputeEffectiveClangTriple(Args: DriverArgs)); |
| 552 | if (Triple.isAArch64() && Triple.getEnvironment() == llvm::Triple::PAuthTest) |
| 553 | handlePAuthABI(D: getDriver(), DriverArgs, CC1Args); |
| 554 | Generic_ELF::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind); |
| 555 | } |
| 556 | |
| 557 | std::string Linux::getDynamicLinker(const ArgList &Args) const { |
| 558 | const llvm::Triple::ArchType Arch = getArch(); |
| 559 | const llvm::Triple &Triple = getTriple(); |
| 560 | |
| 561 | const Distro Distro(getDriver().getVFS(), Triple); |
| 562 | |
| 563 | if (Triple.isAndroid()) { |
| 564 | if (getSanitizerArgs(JobArgs: Args).needsHwasanRt() && |
| 565 | !Triple.isAndroidVersionLT(Major: 34) && Triple.isArch64Bit()) { |
| 566 | // On Android 14 and newer, there is a special linker_hwasan64 that |
| 567 | // allows to run HWASan binaries on non-HWASan system images. This |
| 568 | // is also available on HWASan system images, so we can just always |
| 569 | // use that instead. |
| 570 | return "/system/bin/linker_hwasan64" ; |
| 571 | } |
| 572 | return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker" ; |
| 573 | } |
| 574 | if (Triple.isMusl()) { |
| 575 | std::string ArchName; |
| 576 | bool IsArm = false; |
| 577 | |
| 578 | switch (Arch) { |
| 579 | case llvm::Triple::arm: |
| 580 | case llvm::Triple::thumb: |
| 581 | ArchName = "arm" ; |
| 582 | IsArm = true; |
| 583 | break; |
| 584 | case llvm::Triple::armeb: |
| 585 | case llvm::Triple::thumbeb: |
| 586 | ArchName = "armeb" ; |
| 587 | IsArm = true; |
| 588 | break; |
| 589 | case llvm::Triple::x86: |
| 590 | ArchName = "i386" ; |
| 591 | break; |
| 592 | case llvm::Triple::x86_64: |
| 593 | ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str(); |
| 594 | break; |
| 595 | default: |
| 596 | ArchName = Triple.getArchName().str(); |
| 597 | } |
| 598 | if (IsArm && |
| 599 | (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
| 600 | tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard)) |
| 601 | ArchName += "hf" ; |
| 602 | if (Arch == llvm::Triple::ppc && |
| 603 | Triple.getSubArch() == llvm::Triple::PPCSubArch_spe) |
| 604 | ArchName = "powerpc-sf" ; |
| 605 | |
| 606 | return "/lib/ld-musl-" + ArchName + ".so.1" ; |
| 607 | } |
| 608 | |
| 609 | std::string LibDir; |
| 610 | std::string Loader; |
| 611 | |
| 612 | switch (Arch) { |
| 613 | default: |
| 614 | llvm_unreachable("unsupported architecture" ); |
| 615 | |
| 616 | case llvm::Triple::aarch64: |
| 617 | LibDir = "lib" ; |
| 618 | Loader = "ld-linux-aarch64.so.1" ; |
| 619 | break; |
| 620 | case llvm::Triple::aarch64_be: |
| 621 | LibDir = "lib" ; |
| 622 | Loader = "ld-linux-aarch64_be.so.1" ; |
| 623 | break; |
| 624 | case llvm::Triple::arm: |
| 625 | case llvm::Triple::thumb: |
| 626 | case llvm::Triple::armeb: |
| 627 | case llvm::Triple::thumbeb: { |
| 628 | const bool HF = |
| 629 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
| 630 | Triple.getEnvironment() == llvm::Triple::GNUEABIHFT64 || |
| 631 | tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard; |
| 632 | |
| 633 | LibDir = "lib" ; |
| 634 | Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3" ; |
| 635 | break; |
| 636 | } |
| 637 | case llvm::Triple::loongarch32: { |
| 638 | LibDir = "lib32" ; |
| 639 | Loader = |
| 640 | ("ld-linux-loongarch-" + |
| 641 | tools::loongarch::getLoongArchABI(D: getDriver(), Args, Triple) + ".so.1" ) |
| 642 | .str(); |
| 643 | break; |
| 644 | } |
| 645 | case llvm::Triple::loongarch64: { |
| 646 | LibDir = "lib64" ; |
| 647 | Loader = |
| 648 | ("ld-linux-loongarch-" + |
| 649 | tools::loongarch::getLoongArchABI(D: getDriver(), Args, Triple) + ".so.1" ) |
| 650 | .str(); |
| 651 | break; |
| 652 | } |
| 653 | case llvm::Triple::m68k: |
| 654 | LibDir = "lib" ; |
| 655 | Loader = "ld.so.1" ; |
| 656 | break; |
| 657 | case llvm::Triple::mips: |
| 658 | case llvm::Triple::mipsel: |
| 659 | case llvm::Triple::mips64: |
| 660 | case llvm::Triple::mips64el: { |
| 661 | bool IsNaN2008 = tools::mips::isNaN2008(D: getDriver(), Args, Triple); |
| 662 | |
| 663 | LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); |
| 664 | |
| 665 | if (tools::mips::isUCLibc(Args)) |
| 666 | Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0" ; |
| 667 | else if (!Triple.hasEnvironment() && |
| 668 | Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) |
| 669 | Loader = |
| 670 | Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1" ; |
| 671 | else |
| 672 | Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1" ; |
| 673 | |
| 674 | break; |
| 675 | } |
| 676 | case llvm::Triple::ppc: |
| 677 | LibDir = "lib" ; |
| 678 | Loader = "ld.so.1" ; |
| 679 | break; |
| 680 | case llvm::Triple::ppcle: |
| 681 | LibDir = "lib" ; |
| 682 | Loader = "ld.so.1" ; |
| 683 | break; |
| 684 | case llvm::Triple::ppc64: |
| 685 | LibDir = "lib64" ; |
| 686 | Loader = |
| 687 | (tools::ppc::hasPPCAbiArg(Args, Value: "elfv2" )) ? "ld64.so.2" : "ld64.so.1" ; |
| 688 | break; |
| 689 | case llvm::Triple::ppc64le: |
| 690 | LibDir = "lib64" ; |
| 691 | Loader = |
| 692 | (tools::ppc::hasPPCAbiArg(Args, Value: "elfv1" )) ? "ld64.so.1" : "ld64.so.2" ; |
| 693 | break; |
| 694 | case llvm::Triple::riscv32: |
| 695 | case llvm::Triple::riscv64: { |
| 696 | StringRef ArchName = llvm::Triple::getArchTypeName(Kind: Arch); |
| 697 | StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); |
| 698 | LibDir = "lib" ; |
| 699 | Loader = ("ld-linux-" + ArchName + "-" + ABIName + ".so.1" ).str(); |
| 700 | break; |
| 701 | } |
| 702 | case llvm::Triple::sparc: |
| 703 | case llvm::Triple::sparcel: |
| 704 | LibDir = "lib" ; |
| 705 | Loader = "ld-linux.so.2" ; |
| 706 | break; |
| 707 | case llvm::Triple::sparcv9: |
| 708 | LibDir = "lib64" ; |
| 709 | Loader = "ld-linux.so.2" ; |
| 710 | break; |
| 711 | case llvm::Triple::systemz: |
| 712 | LibDir = "lib" ; |
| 713 | Loader = "ld64.so.1" ; |
| 714 | break; |
| 715 | case llvm::Triple::x86: |
| 716 | LibDir = "lib" ; |
| 717 | Loader = "ld-linux.so.2" ; |
| 718 | break; |
| 719 | case llvm::Triple::x86_64: { |
| 720 | bool X32 = Triple.isX32(); |
| 721 | |
| 722 | LibDir = X32 ? "libx32" : "lib64" ; |
| 723 | Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2" ; |
| 724 | break; |
| 725 | } |
| 726 | case llvm::Triple::ve: |
| 727 | return "/opt/nec/ve/lib/ld-linux-ve.so.1" ; |
| 728 | case llvm::Triple::csky: { |
| 729 | LibDir = "lib" ; |
| 730 | Loader = "ld.so.1" ; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (Distro == Distro::Exherbo && |
| 736 | (Triple.getVendor() == llvm::Triple::UnknownVendor || |
| 737 | Triple.getVendor() == llvm::Triple::PC)) |
| 738 | return "/usr/" + Triple.str() + "/lib/" + Loader; |
| 739 | return "/" + LibDir + "/" + Loader; |
| 740 | } |
| 741 | |
| 742 | void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 743 | ArgStringList &CC1Args) const { |
| 744 | const Driver &D = getDriver(); |
| 745 | std::string SysRoot = computeSysRoot(); |
| 746 | |
| 747 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc)) |
| 748 | return; |
| 749 | |
| 750 | // Add 'include' in the resource directory, which is similar to |
| 751 | // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory |
| 752 | // contains some files conflicting with system /usr/include. musl systems |
| 753 | // prefer the /usr/include copies which are more relevant. |
| 754 | SmallString<128> ResourceDirInclude(D.ResourceDir); |
| 755 | llvm::sys::path::append(path&: ResourceDirInclude, a: "include" ); |
| 756 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc) && |
| 757 | (!getTriple().isMusl() || DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))) |
| 758 | addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude); |
| 759 | |
| 760 | if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 761 | return; |
| 762 | |
| 763 | // After the resource directory, we prioritize the standard clang include |
| 764 | // directory. |
| 765 | if (std::optional<std::string> Path = getStdlibIncludePath()) |
| 766 | addSystemInclude(DriverArgs, CC1Args, Path: *Path); |
| 767 | |
| 768 | // LOCAL_INCLUDE_DIR |
| 769 | addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/local/include" )); |
| 770 | // TOOL_INCLUDE_DIR |
| 771 | AddMultilibIncludeArgs(DriverArgs, CC1Args); |
| 772 | |
| 773 | // Check for configure-time C include directories. |
| 774 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 775 | if (CIncludeDirs != "" ) { |
| 776 | SmallVector<StringRef, 5> dirs; |
| 777 | CIncludeDirs.split(A&: dirs, Separator: ":" ); |
| 778 | for (StringRef dir : dirs) { |
| 779 | StringRef Prefix = |
| 780 | llvm::sys::path::is_absolute(path: dir) ? "" : StringRef(SysRoot); |
| 781 | addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir); |
| 782 | } |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | // On systems using multiarch and Android, add /usr/include/$triple before |
| 787 | // /usr/include. |
| 788 | std::string MultiarchIncludeDir = getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot); |
| 789 | if (!MultiarchIncludeDir.empty() && |
| 790 | D.getVFS().exists(Path: concat(Path: SysRoot, A: "/usr/include" , B: MultiarchIncludeDir))) |
| 791 | addExternCSystemInclude( |
| 792 | DriverArgs, CC1Args, |
| 793 | Path: concat(Path: SysRoot, A: "/usr/include" , B: MultiarchIncludeDir)); |
| 794 | |
| 795 | if (getTriple().getOS() == llvm::Triple::RTEMS) |
| 796 | return; |
| 797 | |
| 798 | // Add an include of '/include' directly. This isn't provided by default by |
| 799 | // system GCCs, but is often used with cross-compiling GCCs, and harmless to |
| 800 | // add even when Clang is acting as-if it were a system compiler. |
| 801 | addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/include" )); |
| 802 | |
| 803 | addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/include" )); |
| 804 | |
| 805 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc) && getTriple().isMusl()) |
| 806 | addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude); |
| 807 | } |
| 808 | |
| 809 | void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 810 | llvm::opt::ArgStringList &CC1Args) const { |
| 811 | // We need a detected GCC installation on Linux to provide libstdc++'s |
| 812 | // headers in odd Linuxish places. |
| 813 | if (!GCCInstallation.isValid()) |
| 814 | return; |
| 815 | |
| 816 | // Try generic GCC detection first. |
| 817 | if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC&: CC1Args)) |
| 818 | return; |
| 819 | |
| 820 | StringRef LibDir = GCCInstallation.getParentLibPath(); |
| 821 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
| 822 | const GCCVersion &Version = GCCInstallation.getVersion(); |
| 823 | |
| 824 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
| 825 | const std::string LibStdCXXIncludePathCandidates[] = { |
| 826 | // Android standalone toolchain has C++ headers in yet another place. |
| 827 | LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, |
| 828 | // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, |
| 829 | // without a subdirectory corresponding to the gcc version. |
| 830 | LibDir.str() + "/../include/c++" , |
| 831 | // Cray's gcc installation puts headers under "g++" without a |
| 832 | // version suffix. |
| 833 | LibDir.str() + "/../include/g++" , |
| 834 | }; |
| 835 | |
| 836 | for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { |
| 837 | if (addLibStdCXXIncludePaths(IncludeDir: IncludePath, Triple: TripleStr, |
| 838 | IncludeSuffix: Multilib.includeSuffix(), DriverArgs, CC1Args)) |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, |
| 844 | ArgStringList &CC1Args) const { |
| 845 | CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args); |
| 846 | } |
| 847 | |
| 848 | void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, |
| 849 | ArgStringList &CC1Args) const { |
| 850 | RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); |
| 851 | } |
| 852 | |
| 853 | void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args, |
| 854 | ArgStringList &CmdArgs) const { |
| 855 | if (!Args.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, |
| 856 | Default: true) || |
| 857 | Args.hasArg(Ids: options::OPT_nostdlib) || |
| 858 | Args.hasArg(Ids: options::OPT_no_hip_rt) || Args.hasArg(Ids: options::OPT_r)) |
| 859 | return; |
| 860 | |
| 861 | llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries; |
| 862 | if (ActiveKinds & Action::OFK_HIP) |
| 863 | Libraries.emplace_back(Args: RocmInstallation->getLibPath(), Args: "libamdhip64.so" ); |
| 864 | |
| 865 | for (auto [Path, Library] : Libraries) { |
| 866 | if (Args.hasFlag(Pos: options::OPT_frtlib_add_rpath, |
| 867 | Neg: options::OPT_fno_rtlib_add_rpath, Default: false)) { |
| 868 | SmallString<0> p = Path; |
| 869 | llvm::sys::path::remove_dots(path&: p, remove_dot_dot: true); |
| 870 | CmdArgs.append(IL: {"-rpath" , Args.MakeArgString(Str: p)}); |
| 871 | } |
| 872 | |
| 873 | SmallString<0> p = Path; |
| 874 | llvm::sys::path::append(path&: p, a: Library); |
| 875 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: p)); |
| 876 | } |
| 877 | |
| 878 | // FIXME: The ROCm builds implicitly depends on this being present. |
| 879 | if (ActiveKinds & Action::OFK_HIP) |
| 880 | CmdArgs.push_back( |
| 881 | Elt: Args.MakeArgString(Str: StringRef("-L" ) + RocmInstallation->getLibPath())); |
| 882 | } |
| 883 | |
| 884 | void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, |
| 885 | ArgStringList &CC1Args) const { |
| 886 | if (GCCInstallation.isValid()) { |
| 887 | CC1Args.push_back(Elt: "-isystem" ); |
| 888 | CC1Args.push_back(Elt: DriverArgs.MakeArgString( |
| 889 | Str: GCCInstallation.getParentLibPath() + "/../" + |
| 890 | GCCInstallation.getTriple().str() + "/include" )); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void Linux::addSYCLIncludeArgs(const ArgList &DriverArgs, |
| 895 | ArgStringList &CC1Args) const { |
| 896 | SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args); |
| 897 | } |
| 898 | |
| 899 | bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const { |
| 900 | return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() || |
| 901 | getTriple().isMusl() || getSanitizerArgs(JobArgs: Args).requiresPIE(); |
| 902 | } |
| 903 | |
| 904 | bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const { |
| 905 | // Outline atomics for AArch64 are supported by compiler-rt |
| 906 | // and libgcc since 9.3.1 |
| 907 | assert(getTriple().isAArch64() && "expected AArch64 target!" ); |
| 908 | ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args); |
| 909 | if (RtLib == ToolChain::RLT_CompilerRT) |
| 910 | return true; |
| 911 | assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!" ); |
| 912 | if (GCCInstallation.getVersion().isOlderThan(RHSMajor: 9, RHSMinor: 3, RHSPatch: 1)) |
| 913 | return false; |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | bool Linux::IsMathErrnoDefault() const { |
| 918 | if (getTriple().isAndroid() || getTriple().isMusl()) |
| 919 | return false; |
| 920 | return Generic_ELF::IsMathErrnoDefault(); |
| 921 | } |
| 922 | |
| 923 | SanitizerMask Linux::getSupportedSanitizers() const { |
| 924 | const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; |
| 925 | const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
| 926 | const bool IsMIPS = getTriple().isMIPS32(); |
| 927 | const bool IsMIPS64 = getTriple().isMIPS64(); |
| 928 | const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || |
| 929 | getTriple().getArch() == llvm::Triple::ppc64le; |
| 930 | const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || |
| 931 | getTriple().getArch() == llvm::Triple::aarch64_be; |
| 932 | const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || |
| 933 | getTriple().getArch() == llvm::Triple::thumb || |
| 934 | getTriple().getArch() == llvm::Triple::armeb || |
| 935 | getTriple().getArch() == llvm::Triple::thumbeb; |
| 936 | const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64; |
| 937 | const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64; |
| 938 | const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz; |
| 939 | const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon; |
| 940 | const bool IsAndroid = getTriple().isAndroid(); |
| 941 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 942 | Res |= SanitizerKind::Address; |
| 943 | Res |= SanitizerKind::PointerCompare; |
| 944 | Res |= SanitizerKind::PointerSubtract; |
| 945 | Res |= SanitizerKind::Realtime; |
| 946 | Res |= SanitizerKind::Fuzzer; |
| 947 | Res |= SanitizerKind::FuzzerNoLink; |
| 948 | Res |= SanitizerKind::KernelAddress; |
| 949 | Res |= SanitizerKind::Vptr; |
| 950 | Res |= SanitizerKind::SafeStack; |
| 951 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64 || IsSystemZ) |
| 952 | Res |= SanitizerKind::DataFlow; |
| 953 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 || |
| 954 | IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64) |
| 955 | Res |= SanitizerKind::Leak; |
| 956 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ || |
| 957 | IsLoongArch64 || IsRISCV64) |
| 958 | Res |= SanitizerKind::Thread; |
| 959 | if (IsX86_64 || IsAArch64 || IsSystemZ) |
| 960 | Res |= SanitizerKind::Type; |
| 961 | if (IsX86_64 || IsSystemZ || IsPowerPC64) |
| 962 | Res |= SanitizerKind::KernelMemory; |
| 963 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch || |
| 964 | IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64 || IsSystemZ) |
| 965 | Res |= SanitizerKind::Scudo; |
| 966 | if (IsX86_64 || IsAArch64 || IsRISCV64) { |
| 967 | Res |= SanitizerKind::HWAddress; |
| 968 | } |
| 969 | if (IsX86_64 || IsAArch64) { |
| 970 | Res |= SanitizerKind::KernelHWAddress; |
| 971 | } |
| 972 | if (IsX86_64) |
| 973 | Res |= SanitizerKind::NumericalStability; |
| 974 | if (!IsAndroid) |
| 975 | Res |= SanitizerKind::Memory; |
| 976 | |
| 977 | // Work around "Cannot represent a difference across sections". |
| 978 | if (getTriple().getArch() == llvm::Triple::ppc64) |
| 979 | Res &= ~SanitizerKind::Function; |
| 980 | return Res; |
| 981 | } |
| 982 | |
| 983 | void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, |
| 984 | llvm::opt::ArgStringList &CmdArgs) const { |
| 985 | // Add linker option -u__llvm_profile_runtime to cause runtime |
| 986 | // initialization module to be linked in. |
| 987 | if (needsProfileRT(Args)) |
| 988 | CmdArgs.push_back(Elt: Args.MakeArgString( |
| 989 | Str: Twine("-u" , llvm::getInstrProfRuntimeHookVarName()))); |
| 990 | ToolChain::addProfileRTLibs(Args, CmdArgs); |
| 991 | } |
| 992 | |
| 993 | void Linux::(llvm::opt::ArgStringList &CmdArgs) const { |
| 994 | for (const auto &Opt : ExtraOpts) |
| 995 | CmdArgs.push_back(Elt: Opt.c_str()); |
| 996 | } |
| 997 | |
| 998 | const char *Linux::getDefaultLinker() const { |
| 999 | if (getTriple().isAndroid()) |
| 1000 | return "ld.lld" ; |
| 1001 | return Generic_ELF::getDefaultLinker(); |
| 1002 | } |
| 1003 | |