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