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 "CommonArgs.h" 16 #include "clang/Config/config.h" 17 #include "clang/Driver/Distro.h" 18 #include "clang/Driver/Driver.h" 19 #include "clang/Driver/Options.h" 20 #include "clang/Driver/SanitizerArgs.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 #include <system_error> 27 28 using namespace clang::driver; 29 using namespace clang::driver::toolchains; 30 using namespace clang; 31 using namespace llvm::opt; 32 33 using tools::addPathIfExists; 34 35 /// Get our best guess at the multiarch triple for a target. 36 /// 37 /// Debian-based systems are starting to use a multiarch setup where they use 38 /// a target-triple directory in the library and header search paths. 39 /// Unfortunately, this triple does not align with the vanilla target triple, 40 /// so we provide a rough mapping here. 41 std::string Linux::getMultiarchTriple(const Driver &D, 42 const llvm::Triple &TargetTriple, 43 StringRef SysRoot) const { 44 llvm::Triple::EnvironmentType TargetEnvironment = 45 TargetTriple.getEnvironment(); 46 bool IsAndroid = TargetTriple.isAndroid(); 47 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6; 48 bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32; 49 50 // For most architectures, just use whatever we have rather than trying to be 51 // clever. 52 switch (TargetTriple.getArch()) { 53 default: 54 break; 55 56 // We use the existence of '/lib/<triple>' as a directory to detect some 57 // common linux triples that don't quite match the Clang triple for both 58 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these 59 // regardless of what the actual target triple is. 60 case llvm::Triple::arm: 61 case llvm::Triple::thumb: 62 if (IsAndroid) 63 return "arm-linux-androideabi"; 64 if (TargetEnvironment == llvm::Triple::GNUEABIHF) 65 return "arm-linux-gnueabihf"; 66 return "arm-linux-gnueabi"; 67 case llvm::Triple::armeb: 68 case llvm::Triple::thumbeb: 69 if (TargetEnvironment == llvm::Triple::GNUEABIHF) 70 return "armeb-linux-gnueabihf"; 71 return "armeb-linux-gnueabi"; 72 case llvm::Triple::x86: 73 if (IsAndroid) 74 return "i686-linux-android"; 75 return "i386-linux-gnu"; 76 case llvm::Triple::x86_64: 77 if (IsAndroid) 78 return "x86_64-linux-android"; 79 if (TargetEnvironment == llvm::Triple::GNUX32) 80 return "x86_64-linux-gnux32"; 81 return "x86_64-linux-gnu"; 82 case llvm::Triple::aarch64: 83 if (IsAndroid) 84 return "aarch64-linux-android"; 85 return "aarch64-linux-gnu"; 86 case llvm::Triple::aarch64_be: 87 return "aarch64_be-linux-gnu"; 88 89 case llvm::Triple::loongarch64: { 90 const char *Libc; 91 const char *FPFlavor; 92 93 if (TargetTriple.isGNUEnvironment()) { 94 Libc = "gnu"; 95 } else if (TargetTriple.isMusl()) { 96 Libc = "musl"; 97 } else { 98 return TargetTriple.str(); 99 } 100 101 switch (TargetEnvironment) { 102 default: 103 return TargetTriple.str(); 104 case llvm::Triple::GNUSF: 105 FPFlavor = "sf"; 106 break; 107 case llvm::Triple::GNUF32: 108 FPFlavor = "f32"; 109 break; 110 case llvm::Triple::GNU: 111 case llvm::Triple::GNUF64: 112 // This was going to be "f64" in an earlier Toolchain Conventions 113 // revision, but starting from Feb 2023 the F64 ABI variants are 114 // unmarked in their canonical forms. 115 FPFlavor = ""; 116 break; 117 } 118 119 return (Twine("loongarch64-linux-") + Libc + FPFlavor).str(); 120 } 121 122 case llvm::Triple::m68k: 123 return "m68k-linux-gnu"; 124 125 case llvm::Triple::mips: 126 return IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu"; 127 case llvm::Triple::mipsel: 128 return IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu"; 129 case llvm::Triple::mips64: { 130 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") + 131 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64"); 132 if (D.getVFS().exists(concat(SysRoot, "/lib", MT))) 133 return MT; 134 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64-linux-gnu"))) 135 return "mips64-linux-gnu"; 136 break; 137 } 138 case llvm::Triple::mips64el: { 139 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") + 140 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64"); 141 if (D.getVFS().exists(concat(SysRoot, "/lib", MT))) 142 return MT; 143 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64el-linux-gnu"))) 144 return "mips64el-linux-gnu"; 145 break; 146 } 147 case llvm::Triple::ppc: 148 if (D.getVFS().exists(concat(SysRoot, "/lib/powerpc-linux-gnuspe"))) 149 return "powerpc-linux-gnuspe"; 150 return "powerpc-linux-gnu"; 151 case llvm::Triple::ppcle: 152 return "powerpcle-linux-gnu"; 153 case llvm::Triple::ppc64: 154 return "powerpc64-linux-gnu"; 155 case llvm::Triple::ppc64le: 156 return "powerpc64le-linux-gnu"; 157 case llvm::Triple::riscv64: 158 if (IsAndroid) 159 return "riscv64-linux-android"; 160 return "riscv64-linux-gnu"; 161 case llvm::Triple::sparc: 162 return "sparc-linux-gnu"; 163 case llvm::Triple::sparcv9: 164 return "sparc64-linux-gnu"; 165 case llvm::Triple::systemz: 166 return "s390x-linux-gnu"; 167 } 168 return TargetTriple.str(); 169 } 170 171 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 172 if (Triple.isMIPS()) { 173 if (Triple.isAndroid()) { 174 StringRef CPUName; 175 StringRef ABIName; 176 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 177 if (CPUName == "mips32r6") 178 return "libr6"; 179 if (CPUName == "mips32r2") 180 return "libr2"; 181 } 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, "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 return Triple.isArch32Bit() ? "lib" : "lib64"; 210 } 211 212 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 213 : Generic_ELF(D, Triple, Args) { 214 GCCInstallation.init(Triple, Args); 215 Multilibs = GCCInstallation.getMultilibs(); 216 SelectedMultilibs.assign({GCCInstallation.getMultilib()}); 217 llvm::Triple::ArchType Arch = Triple.getArch(); 218 std::string SysRoot = computeSysRoot(); 219 ToolChain::path_list &PPaths = getProgramPaths(); 220 221 Generic_GCC::PushPPaths(PPaths); 222 223 Distro Distro(D.getVFS(), Triple); 224 225 if (Distro.IsAlpineLinux() || Triple.isAndroid()) { 226 ExtraOpts.push_back("-z"); 227 ExtraOpts.push_back("now"); 228 } 229 230 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() || 231 Triple.isAndroid()) { 232 ExtraOpts.push_back("-z"); 233 ExtraOpts.push_back("relro"); 234 } 235 236 // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld 237 // from 11 onwards default max-page-size to 65536 for both ARM and AArch64. 238 if ((Triple.isARM() || Triple.isAArch64()) && Triple.isAndroid()) { 239 ExtraOpts.push_back("-z"); 240 ExtraOpts.push_back("max-page-size=4096"); 241 } 242 243 if (GCCInstallation.getParentLibPath().contains("opt/rh/")) 244 // With devtoolset on RHEL, we want to add a bin directory that is relative 245 // to the detected gcc install, because if we are using devtoolset gcc then 246 // we want to use other tools from devtoolset (e.g. ld) instead of the 247 // standard system tools. 248 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + 249 "/../bin").str()); 250 251 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 252 ExtraOpts.push_back("-X"); 253 254 const bool IsAndroid = Triple.isAndroid(); 255 const bool IsMips = Triple.isMIPS(); 256 const bool IsHexagon = Arch == llvm::Triple::hexagon; 257 const bool IsRISCV = Triple.isRISCV(); 258 const bool IsCSKY = Triple.isCSKY(); 259 260 if (IsCSKY && !SelectedMultilibs.empty()) 261 SysRoot = SysRoot + SelectedMultilibs.back().osSuffix(); 262 263 if ((IsMips || IsCSKY) && !SysRoot.empty()) 264 ExtraOpts.push_back("--sysroot=" + SysRoot); 265 266 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 267 // and the MIPS ABI require .dynsym to be sorted in different ways. 268 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 269 // ABI requires a mapping between the GOT and the symbol table. 270 // Android loader does not support .gnu.hash until API 23. 271 // Hexagon linker/loader does not support .gnu.hash 272 if (!IsMips && !IsHexagon) { 273 if (Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid || 274 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic || 275 (IsAndroid && Triple.isAndroidVersionLT(23))) 276 ExtraOpts.push_back("--hash-style=both"); 277 else 278 ExtraOpts.push_back("--hash-style=gnu"); 279 } 280 281 #ifdef ENABLE_LINKER_BUILD_ID 282 ExtraOpts.push_back("--build-id"); 283 #endif 284 285 // The selection of paths to try here is designed to match the patterns which 286 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 287 // This was determined by running GCC in a fake filesystem, creating all 288 // possible permutations of these directories, and seeing which ones it added 289 // to the link paths. 290 path_list &Paths = getFilePaths(); 291 292 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); 293 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 294 295 // mips32: Debian multilib, we use /libo32, while in other case, /lib is 296 // used. We need add both libo32 and /lib. 297 if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) { 298 Generic_GCC::AddMultilibPaths(D, SysRoot, "libo32", MultiarchTriple, Paths); 299 addPathIfExists(D, concat(SysRoot, "/libo32"), Paths); 300 addPathIfExists(D, concat(SysRoot, "/usr/libo32"), Paths); 301 } 302 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); 303 304 addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths); 305 addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths); 306 307 if (IsAndroid) { 308 // Android sysroots contain a library directory for each supported OS 309 // version as well as some unversioned libraries in the usual multiarch 310 // directory. 311 addPathIfExists( 312 D, 313 concat(SysRoot, "/usr/lib", MultiarchTriple, 314 llvm::to_string(Triple.getEnvironmentVersion().getMajor())), 315 Paths); 316 } 317 318 addPathIfExists(D, concat(SysRoot, "/usr/lib", MultiarchTriple), Paths); 319 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot 320 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle 321 // this here. 322 if (Triple.getVendor() == llvm::Triple::OpenEmbedded && 323 Triple.isArch64Bit()) 324 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir), Paths); 325 else 326 addPathIfExists(D, concat(SysRoot, "/usr/lib/..", OSLibDir), Paths); 327 if (IsRISCV) { 328 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 329 addPathIfExists(D, concat(SysRoot, "/", OSLibDir, ABIName), Paths); 330 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir, ABIName), Paths); 331 } 332 333 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); 334 335 addPathIfExists(D, concat(SysRoot, "/lib"), Paths); 336 addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths); 337 } 338 339 ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const { 340 if (getTriple().isAndroid()) 341 return ToolChain::RLT_CompilerRT; 342 return Generic_ELF::GetDefaultRuntimeLibType(); 343 } 344 345 unsigned Linux::GetDefaultDwarfVersion() const { 346 if (getTriple().isAndroid()) 347 return 4; 348 return ToolChain::GetDefaultDwarfVersion(); 349 } 350 351 ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const { 352 if (getTriple().isAndroid()) 353 return ToolChain::CST_Libcxx; 354 return ToolChain::CST_Libstdcxx; 355 } 356 357 bool Linux::HasNativeLLVMSupport() const { return true; } 358 359 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } 360 361 Tool *Linux::buildStaticLibTool() const { 362 return new tools::gnutools::StaticLibTool(*this); 363 } 364 365 Tool *Linux::buildAssembler() const { 366 return new tools::gnutools::Assembler(*this); 367 } 368 369 std::string Linux::computeSysRoot() const { 370 if (!getDriver().SysRoot.empty()) 371 return getDriver().SysRoot; 372 373 if (getTriple().isAndroid()) { 374 // Android toolchains typically include a sysroot at ../sysroot relative to 375 // the clang binary. 376 const StringRef ClangDir = getDriver().getInstalledDir(); 377 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str(); 378 if (getVFS().exists(AndroidSysRootPath)) 379 return AndroidSysRootPath; 380 } 381 382 if (getTriple().isCSKY()) { 383 // CSKY toolchains use different names for sysroot folder. 384 if (!GCCInstallation.isValid()) 385 return std::string(); 386 // GCCInstallation.getInstallPath() = 387 // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0 388 // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc 389 std::string Path = (GCCInstallation.getInstallPath() + "/../../../../" + 390 GCCInstallation.getTriple().str() + "/libc") 391 .str(); 392 if (getVFS().exists(Path)) 393 return Path; 394 return std::string(); 395 } 396 397 if (!GCCInstallation.isValid() || !getTriple().isMIPS()) 398 return std::string(); 399 400 // Standalone MIPS toolchains use different names for sysroot folder 401 // and put it into different places. Here we try to check some known 402 // variants. 403 404 const StringRef InstallDir = GCCInstallation.getInstallPath(); 405 const StringRef TripleStr = GCCInstallation.getTriple().str(); 406 const Multilib &Multilib = GCCInstallation.getMultilib(); 407 408 std::string Path = 409 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 410 .str(); 411 412 if (getVFS().exists(Path)) 413 return Path; 414 415 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 416 417 if (getVFS().exists(Path)) 418 return Path; 419 420 return std::string(); 421 } 422 423 std::string Linux::getDynamicLinker(const ArgList &Args) const { 424 const llvm::Triple::ArchType Arch = getArch(); 425 const llvm::Triple &Triple = getTriple(); 426 427 const Distro Distro(getDriver().getVFS(), Triple); 428 429 if (Triple.isAndroid()) { 430 if (getSanitizerArgs(Args).needsHwasanRt() && 431 !Triple.isAndroidVersionLT(34) && Triple.isArch64Bit()) { 432 // On Android 14 and newer, there is a special linker_hwasan64 that 433 // allows to run HWASan binaries on non-HWASan system images. This 434 // is also available on HWASan system images, so we can just always 435 // use that instead. 436 return "/system/bin/linker_hwasan64"; 437 } 438 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 439 } 440 if (Triple.isMusl()) { 441 std::string ArchName; 442 bool IsArm = false; 443 444 switch (Arch) { 445 case llvm::Triple::arm: 446 case llvm::Triple::thumb: 447 ArchName = "arm"; 448 IsArm = true; 449 break; 450 case llvm::Triple::armeb: 451 case llvm::Triple::thumbeb: 452 ArchName = "armeb"; 453 IsArm = true; 454 break; 455 case llvm::Triple::x86: 456 ArchName = "i386"; 457 break; 458 case llvm::Triple::x86_64: 459 ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str(); 460 break; 461 default: 462 ArchName = Triple.getArchName().str(); 463 } 464 if (IsArm && 465 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 466 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 467 ArchName += "hf"; 468 if (Arch == llvm::Triple::ppc && 469 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe) 470 ArchName = "powerpc-sf"; 471 472 return "/lib/ld-musl-" + ArchName + ".so.1"; 473 } 474 475 std::string LibDir; 476 std::string Loader; 477 478 switch (Arch) { 479 default: 480 llvm_unreachable("unsupported architecture"); 481 482 case llvm::Triple::aarch64: 483 LibDir = "lib"; 484 Loader = "ld-linux-aarch64.so.1"; 485 break; 486 case llvm::Triple::aarch64_be: 487 LibDir = "lib"; 488 Loader = "ld-linux-aarch64_be.so.1"; 489 break; 490 case llvm::Triple::arm: 491 case llvm::Triple::thumb: 492 case llvm::Triple::armeb: 493 case llvm::Triple::thumbeb: { 494 const bool HF = 495 Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 496 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard; 497 498 LibDir = "lib"; 499 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3"; 500 break; 501 } 502 case llvm::Triple::loongarch32: { 503 LibDir = "lib32"; 504 Loader = 505 ("ld-linux-loongarch-" + 506 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1") 507 .str(); 508 break; 509 } 510 case llvm::Triple::loongarch64: { 511 LibDir = "lib64"; 512 Loader = 513 ("ld-linux-loongarch-" + 514 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1") 515 .str(); 516 break; 517 } 518 case llvm::Triple::m68k: 519 LibDir = "lib"; 520 Loader = "ld.so.1"; 521 break; 522 case llvm::Triple::mips: 523 case llvm::Triple::mipsel: 524 case llvm::Triple::mips64: 525 case llvm::Triple::mips64el: { 526 bool IsNaN2008 = tools::mips::isNaN2008(getDriver(), Args, Triple); 527 528 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 529 530 if (tools::mips::isUCLibc(Args)) 531 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 532 else if (!Triple.hasEnvironment() && 533 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 534 Loader = 535 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 536 else 537 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 538 539 break; 540 } 541 case llvm::Triple::ppc: 542 LibDir = "lib"; 543 Loader = "ld.so.1"; 544 break; 545 case llvm::Triple::ppcle: 546 LibDir = "lib"; 547 Loader = "ld.so.1"; 548 break; 549 case llvm::Triple::ppc64: 550 LibDir = "lib64"; 551 Loader = 552 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 553 break; 554 case llvm::Triple::ppc64le: 555 LibDir = "lib64"; 556 Loader = 557 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 558 break; 559 case llvm::Triple::riscv32: { 560 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 561 LibDir = "lib"; 562 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 563 break; 564 } 565 case llvm::Triple::riscv64: { 566 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 567 LibDir = "lib"; 568 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 569 break; 570 } 571 case llvm::Triple::sparc: 572 case llvm::Triple::sparcel: 573 LibDir = "lib"; 574 Loader = "ld-linux.so.2"; 575 break; 576 case llvm::Triple::sparcv9: 577 LibDir = "lib64"; 578 Loader = "ld-linux.so.2"; 579 break; 580 case llvm::Triple::systemz: 581 LibDir = "lib"; 582 Loader = "ld64.so.1"; 583 break; 584 case llvm::Triple::x86: 585 LibDir = "lib"; 586 Loader = "ld-linux.so.2"; 587 break; 588 case llvm::Triple::x86_64: { 589 bool X32 = Triple.isX32(); 590 591 LibDir = X32 ? "libx32" : "lib64"; 592 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 593 break; 594 } 595 case llvm::Triple::ve: 596 return "/opt/nec/ve/lib/ld-linux-ve.so.1"; 597 case llvm::Triple::csky: { 598 LibDir = "lib"; 599 Loader = "ld.so.1"; 600 break; 601 } 602 } 603 604 if (Distro == Distro::Exherbo && 605 (Triple.getVendor() == llvm::Triple::UnknownVendor || 606 Triple.getVendor() == llvm::Triple::PC)) 607 return "/usr/" + Triple.str() + "/lib/" + Loader; 608 return "/" + LibDir + "/" + Loader; 609 } 610 611 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 612 ArgStringList &CC1Args) const { 613 const Driver &D = getDriver(); 614 std::string SysRoot = computeSysRoot(); 615 616 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 617 return; 618 619 // Add 'include' in the resource directory, which is similar to 620 // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory 621 // contains some files conflicting with system /usr/include. musl systems 622 // prefer the /usr/include copies which are more relevant. 623 SmallString<128> ResourceDirInclude(D.ResourceDir); 624 llvm::sys::path::append(ResourceDirInclude, "include"); 625 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && 626 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc))) 627 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 628 629 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 630 return; 631 632 // LOCAL_INCLUDE_DIR 633 addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include")); 634 // TOOL_INCLUDE_DIR 635 AddMultilibIncludeArgs(DriverArgs, CC1Args); 636 637 // Check for configure-time C include directories. 638 StringRef CIncludeDirs(C_INCLUDE_DIRS); 639 if (CIncludeDirs != "") { 640 SmallVector<StringRef, 5> dirs; 641 CIncludeDirs.split(dirs, ":"); 642 for (StringRef dir : dirs) { 643 StringRef Prefix = 644 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot); 645 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 646 } 647 return; 648 } 649 650 // On systems using multiarch and Android, add /usr/include/$triple before 651 // /usr/include. 652 std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot); 653 if (!MultiarchIncludeDir.empty() && 654 D.getVFS().exists(concat(SysRoot, "/usr/include", MultiarchIncludeDir))) 655 addExternCSystemInclude( 656 DriverArgs, CC1Args, 657 concat(SysRoot, "/usr/include", MultiarchIncludeDir)); 658 659 if (getTriple().getOS() == llvm::Triple::RTEMS) 660 return; 661 662 // Add an include of '/include' directly. This isn't provided by default by 663 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 664 // add even when Clang is acting as-if it were a system compiler. 665 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include")); 666 667 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include")); 668 669 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl()) 670 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 671 } 672 673 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 674 llvm::opt::ArgStringList &CC1Args) const { 675 // We need a detected GCC installation on Linux to provide libstdc++'s 676 // headers in odd Linuxish places. 677 if (!GCCInstallation.isValid()) 678 return; 679 680 // Detect Debian g++-multiarch-incdir.diff. 681 StringRef TripleStr = GCCInstallation.getTriple().str(); 682 StringRef DebianMultiarch = 683 GCCInstallation.getTriple().getArch() == llvm::Triple::x86 684 ? "i386-linux-gnu" 685 : TripleStr; 686 687 // Try generic GCC detection first. 688 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, 689 DebianMultiarch)) 690 return; 691 692 StringRef LibDir = GCCInstallation.getParentLibPath(); 693 const Multilib &Multilib = GCCInstallation.getMultilib(); 694 const GCCVersion &Version = GCCInstallation.getVersion(); 695 696 const std::string LibStdCXXIncludePathCandidates[] = { 697 // Android standalone toolchain has C++ headers in yet another place. 698 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 699 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 700 // without a subdirectory corresponding to the gcc version. 701 LibDir.str() + "/../include/c++", 702 // Cray's gcc installation puts headers under "g++" without a 703 // version suffix. 704 LibDir.str() + "/../include/g++", 705 }; 706 707 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 708 if (addLibStdCXXIncludePaths(IncludePath, TripleStr, 709 Multilib.includeSuffix(), DriverArgs, CC1Args)) 710 break; 711 } 712 } 713 714 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 715 ArgStringList &CC1Args) const { 716 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args); 717 } 718 719 void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, 720 ArgStringList &CC1Args) const { 721 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); 722 } 723 724 void Linux::AddHIPRuntimeLibArgs(const ArgList &Args, 725 ArgStringList &CmdArgs) const { 726 CmdArgs.push_back( 727 Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath())); 728 729 if (Args.hasFlag(options::OPT_frtlib_add_rpath, 730 options::OPT_fno_rtlib_add_rpath, false)) 731 CmdArgs.append( 732 {"-rpath", Args.MakeArgString(RocmInstallation->getLibPath())}); 733 734 CmdArgs.push_back("-lamdhip64"); 735 } 736 737 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 738 ArgStringList &CC1Args) const { 739 if (GCCInstallation.isValid()) { 740 CC1Args.push_back("-isystem"); 741 CC1Args.push_back(DriverArgs.MakeArgString( 742 GCCInstallation.getParentLibPath() + "/../" + 743 GCCInstallation.getTriple().str() + "/include")); 744 } 745 } 746 747 bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const { 748 return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() || 749 getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE(); 750 } 751 752 bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const { 753 // Outline atomics for AArch64 are supported by compiler-rt 754 // and libgcc since 9.3.1 755 assert(getTriple().isAArch64() && "expected AArch64 target!"); 756 ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args); 757 if (RtLib == ToolChain::RLT_CompilerRT) 758 return true; 759 assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!"); 760 if (GCCInstallation.getVersion().isOlderThan(9, 3, 1)) 761 return false; 762 return true; 763 } 764 765 bool Linux::IsMathErrnoDefault() const { 766 if (getTriple().isAndroid() || getTriple().isMusl()) 767 return false; 768 return Generic_ELF::IsMathErrnoDefault(); 769 } 770 771 SanitizerMask Linux::getSupportedSanitizers() const { 772 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 773 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 774 const bool IsMIPS = getTriple().isMIPS32(); 775 const bool IsMIPS64 = getTriple().isMIPS64(); 776 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 777 getTriple().getArch() == llvm::Triple::ppc64le; 778 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 779 getTriple().getArch() == llvm::Triple::aarch64_be; 780 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 781 getTriple().getArch() == llvm::Triple::thumb || 782 getTriple().getArch() == llvm::Triple::armeb || 783 getTriple().getArch() == llvm::Triple::thumbeb; 784 const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64; 785 const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64; 786 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz; 787 const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon; 788 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 789 Res |= SanitizerKind::Address; 790 Res |= SanitizerKind::PointerCompare; 791 Res |= SanitizerKind::PointerSubtract; 792 Res |= SanitizerKind::Fuzzer; 793 Res |= SanitizerKind::FuzzerNoLink; 794 Res |= SanitizerKind::KernelAddress; 795 Res |= SanitizerKind::Memory; 796 Res |= SanitizerKind::Vptr; 797 Res |= SanitizerKind::SafeStack; 798 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64) 799 Res |= SanitizerKind::DataFlow; 800 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 || 801 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64) 802 Res |= SanitizerKind::Leak; 803 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ || 804 IsLoongArch64) 805 Res |= SanitizerKind::Thread; 806 if (IsX86_64 || IsSystemZ) 807 Res |= SanitizerKind::KernelMemory; 808 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch || 809 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64) 810 Res |= SanitizerKind::Scudo; 811 if (IsX86_64 || IsAArch64 || IsRISCV64) { 812 Res |= SanitizerKind::HWAddress; 813 } 814 if (IsX86_64 || IsAArch64) { 815 Res |= SanitizerKind::KernelHWAddress; 816 } 817 // Work around "Cannot represent a difference across sections". 818 if (getTriple().getArch() == llvm::Triple::ppc64) 819 Res &= ~SanitizerKind::Function; 820 return Res; 821 } 822 823 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 824 llvm::opt::ArgStringList &CmdArgs) const { 825 // Add linker option -u__llvm_profile_runtime to cause runtime 826 // initialization module to be linked in. 827 if (needsProfileRT(Args)) 828 CmdArgs.push_back(Args.MakeArgString( 829 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 830 ToolChain::addProfileRTLibs(Args, CmdArgs); 831 } 832 833 llvm::DenormalMode 834 Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs, 835 const JobAction &JA, 836 const llvm::fltSemantics *FPType) const { 837 switch (getTriple().getArch()) { 838 case llvm::Triple::x86: 839 case llvm::Triple::x86_64: { 840 std::string Unused; 841 // DAZ and FTZ are turned on in crtfastmath.o 842 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) && 843 isFastMathRuntimeAvailable(DriverArgs, Unused)) 844 return llvm::DenormalMode::getPreserveSign(); 845 return llvm::DenormalMode::getIEEE(); 846 } 847 default: 848 return llvm::DenormalMode::getIEEE(); 849 } 850 } 851 852 void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { 853 for (const auto &Opt : ExtraOpts) 854 CmdArgs.push_back(Opt.c_str()); 855 } 856 857 const char *Linux::getDefaultLinker() const { 858 if (getTriple().isAndroid()) 859 return "ld.lld"; 860 return Generic_ELF::getDefaultLinker(); 861 } 862