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 } else if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 64 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf")) 65 return "arm-linux-gnueabihf"; 66 } else { 67 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi")) 68 return "arm-linux-gnueabi"; 69 } 70 break; 71 case llvm::Triple::armeb: 72 case llvm::Triple::thumbeb: 73 if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 74 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf")) 75 return "armeb-linux-gnueabihf"; 76 } else { 77 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi")) 78 return "armeb-linux-gnueabi"; 79 } 80 break; 81 case llvm::Triple::x86: 82 if (IsAndroid) 83 return "i686-linux-android"; 84 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu")) 85 return "i386-linux-gnu"; 86 break; 87 case llvm::Triple::x86_64: 88 if (IsAndroid) 89 return "x86_64-linux-android"; 90 // We don't want this for x32, otherwise it will match x86_64 libs 91 if (TargetEnvironment != llvm::Triple::GNUX32 && 92 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu")) 93 return "x86_64-linux-gnu"; 94 break; 95 case llvm::Triple::aarch64: 96 if (IsAndroid) 97 return "aarch64-linux-android"; 98 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu")) 99 return "aarch64-linux-gnu"; 100 break; 101 case llvm::Triple::aarch64_be: 102 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu")) 103 return "aarch64_be-linux-gnu"; 104 break; 105 case llvm::Triple::mips: { 106 std::string MT = IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu"; 107 if (D.getVFS().exists(SysRoot + "/lib/" + MT)) 108 return MT; 109 break; 110 } 111 case llvm::Triple::mipsel: { 112 if (IsAndroid) 113 return "mipsel-linux-android"; 114 std::string MT = IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu"; 115 if (D.getVFS().exists(SysRoot + "/lib/" + MT)) 116 return MT; 117 break; 118 } 119 case llvm::Triple::mips64: { 120 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") + 121 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64"); 122 if (D.getVFS().exists(SysRoot + "/lib/" + MT)) 123 return MT; 124 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu")) 125 return "mips64-linux-gnu"; 126 break; 127 } 128 case llvm::Triple::mips64el: { 129 if (IsAndroid) 130 return "mips64el-linux-android"; 131 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") + 132 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64"); 133 if (D.getVFS().exists(SysRoot + "/lib/" + MT)) 134 return MT; 135 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu")) 136 return "mips64el-linux-gnu"; 137 break; 138 } 139 case llvm::Triple::ppc: 140 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe")) 141 return "powerpc-linux-gnuspe"; 142 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu")) 143 return "powerpc-linux-gnu"; 144 break; 145 case llvm::Triple::ppc64: 146 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu")) 147 return "powerpc64-linux-gnu"; 148 break; 149 case llvm::Triple::ppc64le: 150 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu")) 151 return "powerpc64le-linux-gnu"; 152 break; 153 case llvm::Triple::sparc: 154 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu")) 155 return "sparc-linux-gnu"; 156 break; 157 case llvm::Triple::sparcv9: 158 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu")) 159 return "sparc64-linux-gnu"; 160 break; 161 case llvm::Triple::systemz: 162 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu")) 163 return "s390x-linux-gnu"; 164 break; 165 } 166 return TargetTriple.str(); 167 } 168 169 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 170 if (Triple.isMIPS()) { 171 if (Triple.isAndroid()) { 172 StringRef CPUName; 173 StringRef ABIName; 174 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 175 if (CPUName == "mips32r6") 176 return "libr6"; 177 if (CPUName == "mips32r2") 178 return "libr2"; 179 } 180 // lib32 directory has a special meaning on MIPS targets. 181 // It contains N32 ABI binaries. Use this folder if produce 182 // code for N32 ABI only. 183 if (tools::mips::hasMipsAbiArg(Args, "n32")) 184 return "lib32"; 185 return Triple.isArch32Bit() ? "lib" : "lib64"; 186 } 187 188 // It happens that only x86, PPC and SPARC use the 'lib32' variant of 189 // oslibdir, and using that variant while targeting other architectures causes 190 // problems because the libraries are laid out in shared system roots that 191 // can't cope with a 'lib32' library search path being considered. So we only 192 // enable them when we know we may need it. 193 // 194 // FIXME: This is a bit of a hack. We should really unify this code for 195 // reasoning about oslibdir spellings with the lib dir spellings in the 196 // GCCInstallationDetector, but that is a more significant refactoring. 197 if (Triple.getArch() == llvm::Triple::x86 || 198 Triple.getArch() == llvm::Triple::ppc || 199 Triple.getArch() == llvm::Triple::sparc) 200 return "lib32"; 201 202 if (Triple.getArch() == llvm::Triple::x86_64 && 203 Triple.getEnvironment() == llvm::Triple::GNUX32) 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 SelectedMultilib = 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().find("opt/rh/devtoolset") != 244 StringRef::npos) 245 // With devtoolset on RHEL, we want to add a bin directory that is relative 246 // to the detected gcc install, because if we are using devtoolset gcc then 247 // we want to use other tools from devtoolset (e.g. ld) instead of the 248 // standard system tools. 249 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + 250 "/../bin").str()); 251 252 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 253 ExtraOpts.push_back("-X"); 254 255 const bool IsAndroid = Triple.isAndroid(); 256 const bool IsMips = Triple.isMIPS(); 257 const bool IsHexagon = Arch == llvm::Triple::hexagon; 258 const bool IsRISCV = Triple.isRISCV(); 259 260 if (IsMips && !SysRoot.empty()) 261 ExtraOpts.push_back("--sysroot=" + SysRoot); 262 263 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 264 // and the MIPS ABI require .dynsym to be sorted in different ways. 265 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 266 // ABI requires a mapping between the GOT and the symbol table. 267 // Android loader does not support .gnu.hash until API 23. 268 // Hexagon linker/loader does not support .gnu.hash 269 if (!IsMips && !IsHexagon) { 270 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() || 271 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) || 272 (IsAndroid && !Triple.isAndroidVersionLT(23))) 273 ExtraOpts.push_back("--hash-style=gnu"); 274 275 if (Distro.IsDebian() || Distro.IsOpenSUSE() || 276 Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty || 277 Distro == Distro::UbuntuKarmic || 278 (IsAndroid && Triple.isAndroidVersionLT(23))) 279 ExtraOpts.push_back("--hash-style=both"); 280 } 281 282 #ifdef ENABLE_LINKER_BUILD_ID 283 ExtraOpts.push_back("--build-id"); 284 #endif 285 286 if (IsAndroid || Distro.IsOpenSUSE()) 287 ExtraOpts.push_back("--enable-new-dtags"); 288 289 // The selection of paths to try here is designed to match the patterns which 290 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 291 // This was determined by running GCC in a fake filesystem, creating all 292 // possible permutations of these directories, and seeing which ones it added 293 // to the link paths. 294 path_list &Paths = getFilePaths(); 295 296 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); 297 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 298 299 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); 300 301 // Similar to the logic for GCC above, if we currently running Clang inside 302 // of the requested system root, add its parent library paths to 303 // those searched. 304 // FIXME: It's not clear whether we should use the driver's installed 305 // directory ('Dir' below) or the ResourceDir. 306 if (StringRef(D.Dir).startswith(SysRoot)) { 307 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 308 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 309 } 310 311 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 312 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 313 314 if (IsAndroid) { 315 // Android sysroots contain a library directory for each supported OS 316 // version as well as some unversioned libraries in the usual multiarch 317 // directory. 318 unsigned Major; 319 unsigned Minor; 320 unsigned Micro; 321 Triple.getEnvironmentVersion(Major, Minor, Micro); 322 addPathIfExists(D, 323 SysRoot + "/usr/lib/" + MultiarchTriple + "/" + 324 llvm::to_string(Major), 325 Paths); 326 } 327 328 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 329 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot 330 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle 331 // this here. 332 if (Triple.getVendor() == llvm::Triple::OpenEmbedded && 333 Triple.isArch64Bit()) 334 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths); 335 else 336 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 337 if (IsRISCV) { 338 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 339 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths); 340 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths); 341 } 342 343 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); 344 345 // Similar to the logic for GCC above, if we are currently running Clang 346 // inside of the requested system root, add its parent library path to those 347 // searched. 348 // FIXME: It's not clear whether we should use the driver's installed 349 // directory ('Dir' below) or the ResourceDir. 350 if (StringRef(D.Dir).startswith(SysRoot)) 351 addPathIfExists(D, D.Dir + "/../lib", Paths); 352 353 addPathIfExists(D, SysRoot + "/lib", Paths); 354 addPathIfExists(D, SysRoot + "/usr/lib", Paths); 355 } 356 357 ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const { 358 if (getTriple().isAndroid()) 359 return ToolChain::CST_Libcxx; 360 return ToolChain::CST_Libstdcxx; 361 } 362 363 bool Linux::HasNativeLLVMSupport() const { return true; } 364 365 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } 366 367 Tool *Linux::buildStaticLibTool() const { 368 return new tools::gnutools::StaticLibTool(*this); 369 } 370 371 Tool *Linux::buildAssembler() const { 372 return new tools::gnutools::Assembler(*this); 373 } 374 375 std::string Linux::computeSysRoot() const { 376 if (!getDriver().SysRoot.empty()) 377 return getDriver().SysRoot; 378 379 if (getTriple().isAndroid()) { 380 // Android toolchains typically include a sysroot at ../sysroot relative to 381 // the clang binary. 382 const StringRef ClangDir = getDriver().getInstalledDir(); 383 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str(); 384 if (getVFS().exists(AndroidSysRootPath)) 385 return AndroidSysRootPath; 386 } 387 388 if (!GCCInstallation.isValid() || !getTriple().isMIPS()) 389 return std::string(); 390 391 // Standalone MIPS toolchains use different names for sysroot folder 392 // and put it into different places. Here we try to check some known 393 // variants. 394 395 const StringRef InstallDir = GCCInstallation.getInstallPath(); 396 const StringRef TripleStr = GCCInstallation.getTriple().str(); 397 const Multilib &Multilib = GCCInstallation.getMultilib(); 398 399 std::string Path = 400 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 401 .str(); 402 403 if (getVFS().exists(Path)) 404 return Path; 405 406 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 407 408 if (getVFS().exists(Path)) 409 return Path; 410 411 return std::string(); 412 } 413 414 std::string Linux::getDynamicLinker(const ArgList &Args) const { 415 const llvm::Triple::ArchType Arch = getArch(); 416 const llvm::Triple &Triple = getTriple(); 417 418 const Distro Distro(getDriver().getVFS(), Triple); 419 420 if (Triple.isAndroid()) 421 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 422 423 if (Triple.isMusl()) { 424 std::string ArchName; 425 bool IsArm = false; 426 427 switch (Arch) { 428 case llvm::Triple::arm: 429 case llvm::Triple::thumb: 430 ArchName = "arm"; 431 IsArm = true; 432 break; 433 case llvm::Triple::armeb: 434 case llvm::Triple::thumbeb: 435 ArchName = "armeb"; 436 IsArm = true; 437 break; 438 default: 439 ArchName = Triple.getArchName().str(); 440 } 441 if (IsArm && 442 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 443 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 444 ArchName += "hf"; 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::mips: 477 case llvm::Triple::mipsel: 478 case llvm::Triple::mips64: 479 case llvm::Triple::mips64el: { 480 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 481 482 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 483 484 if (tools::mips::isUCLibc(Args)) 485 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 486 else if (!Triple.hasEnvironment() && 487 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 488 Loader = 489 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 490 else 491 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 492 493 break; 494 } 495 case llvm::Triple::ppc: 496 LibDir = "lib"; 497 Loader = "ld.so.1"; 498 break; 499 case llvm::Triple::ppc64: 500 LibDir = "lib64"; 501 Loader = 502 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 503 break; 504 case llvm::Triple::ppc64le: 505 LibDir = "lib64"; 506 Loader = 507 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 508 break; 509 case llvm::Triple::riscv32: { 510 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 511 LibDir = "lib"; 512 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 513 break; 514 } 515 case llvm::Triple::riscv64: { 516 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 517 LibDir = "lib"; 518 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 519 break; 520 } 521 case llvm::Triple::sparc: 522 case llvm::Triple::sparcel: 523 LibDir = "lib"; 524 Loader = "ld-linux.so.2"; 525 break; 526 case llvm::Triple::sparcv9: 527 LibDir = "lib64"; 528 Loader = "ld-linux.so.2"; 529 break; 530 case llvm::Triple::systemz: 531 LibDir = "lib"; 532 Loader = "ld64.so.1"; 533 break; 534 case llvm::Triple::x86: 535 LibDir = "lib"; 536 Loader = "ld-linux.so.2"; 537 break; 538 case llvm::Triple::x86_64: { 539 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 540 541 LibDir = X32 ? "libx32" : "lib64"; 542 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 543 break; 544 } 545 case llvm::Triple::ve: 546 return "/opt/nec/ve/lib/ld-linux-ve.so.1"; 547 } 548 549 if (Distro == Distro::Exherbo && 550 (Triple.getVendor() == llvm::Triple::UnknownVendor || 551 Triple.getVendor() == llvm::Triple::PC)) 552 return "/usr/" + Triple.str() + "/lib/" + Loader; 553 return "/" + LibDir + "/" + Loader; 554 } 555 556 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 557 ArgStringList &CC1Args) const { 558 const Driver &D = getDriver(); 559 std::string SysRoot = computeSysRoot(); 560 561 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 562 return; 563 564 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 565 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 566 567 SmallString<128> ResourceDirInclude(D.ResourceDir); 568 llvm::sys::path::append(ResourceDirInclude, "include"); 569 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && 570 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc))) 571 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 572 573 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 574 return; 575 576 // Check for configure-time C include directories. 577 StringRef CIncludeDirs(C_INCLUDE_DIRS); 578 if (CIncludeDirs != "") { 579 SmallVector<StringRef, 5> dirs; 580 CIncludeDirs.split(dirs, ":"); 581 for (StringRef dir : dirs) { 582 StringRef Prefix = 583 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot); 584 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 585 } 586 return; 587 } 588 589 // Lacking those, try to detect the correct set of system includes for the 590 // target triple. 591 592 AddMultilibIncludeArgs(DriverArgs, CC1Args); 593 594 // Implement generic Debian multiarch support. 595 const StringRef X86_64MultiarchIncludeDirs[] = { 596 "/usr/include/x86_64-linux-gnu", 597 598 // FIXME: These are older forms of multiarch. It's not clear that they're 599 // in use in any released version of Debian, so we should consider 600 // removing them. 601 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 602 const StringRef X86MultiarchIncludeDirs[] = { 603 "/usr/include/i386-linux-gnu", 604 605 // FIXME: These are older forms of multiarch. It's not clear that they're 606 // in use in any released version of Debian, so we should consider 607 // removing them. 608 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 609 "/usr/include/i486-linux-gnu"}; 610 const StringRef AArch64MultiarchIncludeDirs[] = { 611 "/usr/include/aarch64-linux-gnu"}; 612 const StringRef ARMMultiarchIncludeDirs[] = { 613 "/usr/include/arm-linux-gnueabi"}; 614 const StringRef ARMHFMultiarchIncludeDirs[] = { 615 "/usr/include/arm-linux-gnueabihf"}; 616 const StringRef ARMEBMultiarchIncludeDirs[] = { 617 "/usr/include/armeb-linux-gnueabi"}; 618 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 619 "/usr/include/armeb-linux-gnueabihf"}; 620 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 621 const StringRef MIPSELMultiarchIncludeDirs[] = { 622 "/usr/include/mipsel-linux-gnu"}; 623 const StringRef MIPS64MultiarchIncludeDirs[] = { 624 "/usr/include/mips64-linux-gnuabi64"}; 625 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 626 "/usr/include/mips64el-linux-gnuabi64"}; 627 const StringRef MIPSN32MultiarchIncludeDirs[] = { 628 "/usr/include/mips64-linux-gnuabin32"}; 629 const StringRef MIPSN32ELMultiarchIncludeDirs[] = { 630 "/usr/include/mips64el-linux-gnuabin32"}; 631 const StringRef MIPSR6MultiarchIncludeDirs[] = { 632 "/usr/include/mipsisa32-linux-gnu"}; 633 const StringRef MIPSR6ELMultiarchIncludeDirs[] = { 634 "/usr/include/mipsisa32r6el-linux-gnu"}; 635 const StringRef MIPS64R6MultiarchIncludeDirs[] = { 636 "/usr/include/mipsisa64r6-linux-gnuabi64"}; 637 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = { 638 "/usr/include/mipsisa64r6el-linux-gnuabi64"}; 639 const StringRef MIPSN32R6MultiarchIncludeDirs[] = { 640 "/usr/include/mipsisa64r6-linux-gnuabin32"}; 641 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = { 642 "/usr/include/mipsisa64r6el-linux-gnuabin32"}; 643 const StringRef PPCMultiarchIncludeDirs[] = { 644 "/usr/include/powerpc-linux-gnu", 645 "/usr/include/powerpc-linux-gnuspe"}; 646 const StringRef PPC64MultiarchIncludeDirs[] = { 647 "/usr/include/powerpc64-linux-gnu"}; 648 const StringRef PPC64LEMultiarchIncludeDirs[] = { 649 "/usr/include/powerpc64le-linux-gnu"}; 650 const StringRef SparcMultiarchIncludeDirs[] = { 651 "/usr/include/sparc-linux-gnu"}; 652 const StringRef Sparc64MultiarchIncludeDirs[] = { 653 "/usr/include/sparc64-linux-gnu"}; 654 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 655 "/usr/include/s390x-linux-gnu"}; 656 ArrayRef<StringRef> MultiarchIncludeDirs; 657 switch (getTriple().getArch()) { 658 case llvm::Triple::x86_64: 659 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 660 break; 661 case llvm::Triple::x86: 662 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 663 break; 664 case llvm::Triple::aarch64: 665 case llvm::Triple::aarch64_be: 666 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 667 break; 668 case llvm::Triple::arm: 669 case llvm::Triple::thumb: 670 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 671 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 672 else 673 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 674 break; 675 case llvm::Triple::armeb: 676 case llvm::Triple::thumbeb: 677 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 678 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 679 else 680 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 681 break; 682 case llvm::Triple::mips: 683 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 684 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs; 685 else 686 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 687 break; 688 case llvm::Triple::mipsel: 689 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 690 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs; 691 else 692 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 693 break; 694 case llvm::Triple::mips64: 695 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 696 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 697 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs; 698 else 699 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs; 700 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 701 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs; 702 else 703 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 704 break; 705 case llvm::Triple::mips64el: 706 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 707 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 708 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs; 709 else 710 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs; 711 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 712 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs; 713 else 714 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 715 break; 716 case llvm::Triple::ppc: 717 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 718 break; 719 case llvm::Triple::ppc64: 720 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 721 break; 722 case llvm::Triple::ppc64le: 723 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 724 break; 725 case llvm::Triple::sparc: 726 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 727 break; 728 case llvm::Triple::sparcv9: 729 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 730 break; 731 case llvm::Triple::systemz: 732 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 733 break; 734 default: 735 break; 736 } 737 738 const std::string AndroidMultiarchIncludeDir = 739 std::string("/usr/include/") + 740 getMultiarchTriple(D, getTriple(), SysRoot); 741 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir}; 742 if (getTriple().isAndroid()) 743 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs; 744 745 for (StringRef Dir : MultiarchIncludeDirs) { 746 if (D.getVFS().exists(SysRoot + Dir)) { 747 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 748 break; 749 } 750 } 751 752 if (getTriple().getOS() == llvm::Triple::RTEMS) 753 return; 754 755 // Add an include of '/include' directly. This isn't provided by default by 756 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 757 // add even when Clang is acting as-if it were a system compiler. 758 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 759 760 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 761 762 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl()) 763 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 764 } 765 766 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 767 llvm::opt::ArgStringList &CC1Args) const { 768 // Try generic GCC detection first. 769 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args)) 770 return; 771 772 // We need a detected GCC installation on Linux to provide libstdc++'s 773 // headers in odd Linuxish places. 774 if (!GCCInstallation.isValid()) 775 return; 776 777 StringRef LibDir = GCCInstallation.getParentLibPath(); 778 StringRef TripleStr = GCCInstallation.getTriple().str(); 779 const Multilib &Multilib = GCCInstallation.getMultilib(); 780 const GCCVersion &Version = GCCInstallation.getVersion(); 781 782 const std::string LibStdCXXIncludePathCandidates[] = { 783 // Android standalone toolchain has C++ headers in yet another place. 784 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 785 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 786 // without a subdirectory corresponding to the gcc version. 787 LibDir.str() + "/../include/c++", 788 // Cray's gcc installation puts headers under "g++" without a 789 // version suffix. 790 LibDir.str() + "/../include/g++", 791 }; 792 793 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 794 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 795 /*GCCMultiarchTriple*/ "", 796 /*TargetMultiarchTriple*/ "", 797 Multilib.includeSuffix(), DriverArgs, CC1Args)) 798 break; 799 } 800 } 801 802 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 803 ArgStringList &CC1Args) const { 804 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 805 } 806 807 void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, 808 ArgStringList &CC1Args) const { 809 RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args); 810 } 811 812 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 813 ArgStringList &CC1Args) const { 814 if (GCCInstallation.isValid()) { 815 CC1Args.push_back("-isystem"); 816 CC1Args.push_back(DriverArgs.MakeArgString( 817 GCCInstallation.getParentLibPath() + "/../" + 818 GCCInstallation.getTriple().str() + "/include")); 819 } 820 } 821 822 bool Linux::isPIEDefault() const { 823 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || 824 getTriple().isMusl() || getSanitizerArgs().requiresPIE(); 825 } 826 827 bool Linux::isNoExecStackDefault() const { 828 return getTriple().isAndroid(); 829 } 830 831 bool Linux::IsMathErrnoDefault() const { 832 if (getTriple().isAndroid()) 833 return false; 834 return Generic_ELF::IsMathErrnoDefault(); 835 } 836 837 SanitizerMask Linux::getSupportedSanitizers() const { 838 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 839 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 840 const bool IsMIPS = getTriple().isMIPS32(); 841 const bool IsMIPS64 = getTriple().isMIPS64(); 842 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 843 getTriple().getArch() == llvm::Triple::ppc64le; 844 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 845 getTriple().getArch() == llvm::Triple::aarch64_be; 846 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 847 getTriple().getArch() == llvm::Triple::thumb || 848 getTriple().getArch() == llvm::Triple::armeb || 849 getTriple().getArch() == llvm::Triple::thumbeb; 850 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz; 851 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 852 Res |= SanitizerKind::Address; 853 Res |= SanitizerKind::PointerCompare; 854 Res |= SanitizerKind::PointerSubtract; 855 Res |= SanitizerKind::Fuzzer; 856 Res |= SanitizerKind::FuzzerNoLink; 857 Res |= SanitizerKind::KernelAddress; 858 Res |= SanitizerKind::Memory; 859 Res |= SanitizerKind::Vptr; 860 Res |= SanitizerKind::SafeStack; 861 if (IsX86_64 || IsMIPS64 || IsAArch64) 862 Res |= SanitizerKind::DataFlow; 863 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 || 864 IsSystemZ) 865 Res |= SanitizerKind::Leak; 866 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 867 Res |= SanitizerKind::Thread; 868 if (IsX86_64) 869 Res |= SanitizerKind::KernelMemory; 870 if (IsX86 || IsX86_64) 871 Res |= SanitizerKind::Function; 872 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch || 873 IsPowerPC64) 874 Res |= SanitizerKind::Scudo; 875 if (IsX86_64 || IsAArch64) { 876 Res |= SanitizerKind::HWAddress; 877 Res |= SanitizerKind::KernelHWAddress; 878 } 879 return Res; 880 } 881 882 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 883 llvm::opt::ArgStringList &CmdArgs) const { 884 // Add linker option -u__llvm_profile_runtime to cause runtime 885 // initialization module to be linked in. 886 if (needsProfileRT(Args)) 887 CmdArgs.push_back(Args.MakeArgString( 888 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 889 ToolChain::addProfileRTLibs(Args, CmdArgs); 890 } 891 892 llvm::DenormalMode 893 Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs, 894 const JobAction &JA, 895 const llvm::fltSemantics *FPType) const { 896 switch (getTriple().getArch()) { 897 case llvm::Triple::x86: 898 case llvm::Triple::x86_64: { 899 std::string Unused; 900 // DAZ and FTZ are turned on in crtfastmath.o 901 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) && 902 isFastMathRuntimeAvailable(DriverArgs, Unused)) 903 return llvm::DenormalMode::getPreserveSign(); 904 return llvm::DenormalMode::getIEEE(); 905 } 906 default: 907 return llvm::DenormalMode::getIEEE(); 908 } 909 } 910 911 void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { 912 for (const auto &Opt : ExtraOpts) 913 CmdArgs.push_back(Opt.c_str()); 914 } 915