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