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 and PPC use the 'lib32' variant of oslibdir, and 189 // using that variant while targeting other architectures causes problems 190 // because the libraries are laid out in shared system roots that can't cope 191 // with a 'lib32' library search path being considered. So we only enable 192 // 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 return "lib32"; 200 201 if (Triple.getArch() == llvm::Triple::x86_64 && 202 Triple.getEnvironment() == llvm::Triple::GNUX32) 203 return "libx32"; 204 205 if (Triple.getArch() == llvm::Triple::riscv32) 206 return "lib32"; 207 208 return Triple.isArch32Bit() ? "lib" : "lib64"; 209 } 210 211 static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, 212 const Multilib &Multilib, 213 StringRef InstallPath, 214 ToolChain::path_list &Paths) { 215 if (const auto &PathsCallback = Multilibs.filePathsCallback()) 216 for (const auto &Path : PathsCallback(Multilib)) 217 addPathIfExists(D, InstallPath + Path, Paths); 218 } 219 220 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 221 : Generic_ELF(D, Triple, Args) { 222 GCCInstallation.init(Triple, Args); 223 Multilibs = GCCInstallation.getMultilibs(); 224 SelectedMultilib = GCCInstallation.getMultilib(); 225 llvm::Triple::ArchType Arch = Triple.getArch(); 226 std::string SysRoot = computeSysRoot(); 227 228 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at 229 // least) put various tools in a triple-prefixed directory off of the parent 230 // of the GCC installation. We use the GCC triple here to ensure that we end 231 // up with tools that support the same amount of cross compiling as the 232 // detected GCC installation. For example, if we find a GCC installation 233 // targeting x86_64, but it is a bi-arch GCC installation, it can also be 234 // used to target i386. 235 // FIXME: This seems unlikely to be Linux-specific. 236 ToolChain::path_list &PPaths = getProgramPaths(); 237 if (GCCInstallation.isValid()) { 238 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" + 239 GCCInstallation.getTriple().str() + "/bin") 240 .str()); 241 } 242 243 Distro Distro(D.getVFS(), Triple); 244 245 if (Distro.IsAlpineLinux() || Triple.isAndroid()) { 246 ExtraOpts.push_back("-z"); 247 ExtraOpts.push_back("now"); 248 } 249 250 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() || 251 Triple.isAndroid()) { 252 ExtraOpts.push_back("-z"); 253 ExtraOpts.push_back("relro"); 254 } 255 256 // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld 257 // from 11 onwards default max-page-size to 65536 for both ARM and AArch64. 258 if ((Triple.isARM() || Triple.isAArch64()) && Triple.isAndroid()) { 259 ExtraOpts.push_back("-z"); 260 ExtraOpts.push_back("max-page-size=4096"); 261 } 262 263 if (GCCInstallation.getParentLibPath().find("opt/rh/devtoolset") != 264 StringRef::npos) 265 // With devtoolset on RHEL, we want to add a bin directory that is relative 266 // to the detected gcc install, because if we are using devtoolset gcc then 267 // we want to use other tools from devtoolset (e.g. ld) instead of the 268 // standard system tools. 269 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + 270 "/../bin").str()); 271 272 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 273 ExtraOpts.push_back("-X"); 274 275 const bool IsAndroid = Triple.isAndroid(); 276 const bool IsMips = Triple.isMIPS(); 277 const bool IsHexagon = Arch == llvm::Triple::hexagon; 278 const bool IsRISCV = Triple.isRISCV(); 279 280 if (IsMips && !SysRoot.empty()) 281 ExtraOpts.push_back("--sysroot=" + SysRoot); 282 283 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 284 // and the MIPS ABI require .dynsym to be sorted in different ways. 285 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 286 // ABI requires a mapping between the GOT and the symbol table. 287 // Android loader does not support .gnu.hash until API 23. 288 // Hexagon linker/loader does not support .gnu.hash 289 if (!IsMips && !IsHexagon) { 290 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() || 291 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) || 292 (IsAndroid && !Triple.isAndroidVersionLT(23))) 293 ExtraOpts.push_back("--hash-style=gnu"); 294 295 if (Distro.IsDebian() || Distro.IsOpenSUSE() || 296 Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty || 297 Distro == Distro::UbuntuKarmic || 298 (IsAndroid && Triple.isAndroidVersionLT(23))) 299 ExtraOpts.push_back("--hash-style=both"); 300 } 301 302 #ifdef ENABLE_LINKER_BUILD_ID 303 ExtraOpts.push_back("--build-id"); 304 #endif 305 306 if (IsAndroid || Distro.IsOpenSUSE()) 307 ExtraOpts.push_back("--enable-new-dtags"); 308 309 // The selection of paths to try here is designed to match the patterns which 310 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 311 // This was determined by running GCC in a fake filesystem, creating all 312 // possible permutations of these directories, and seeing which ones it added 313 // to the link paths. 314 path_list &Paths = getFilePaths(); 315 316 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); 317 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 318 319 // Add the multilib suffixed paths where they are available. 320 if (GCCInstallation.isValid()) { 321 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 322 const std::string &LibPath = 323 std::string(GCCInstallation.getParentLibPath()); 324 325 // Add toolchain / multilib specific file paths. 326 addMultilibsFilePaths(D, Multilibs, SelectedMultilib, 327 GCCInstallation.getInstallPath(), Paths); 328 329 // Sourcery CodeBench MIPS toolchain holds some libraries under 330 // a biarch-like suffix of the GCC installation. 331 addPathIfExists( 332 D, GCCInstallation.getInstallPath() + SelectedMultilib.gccSuffix(), 333 Paths); 334 335 // GCC cross compiling toolchains will install target libraries which ship 336 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as 337 // any part of the GCC installation in 338 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat 339 // debatable, but is the reality today. We need to search this tree even 340 // when we have a sysroot somewhere else. It is the responsibility of 341 // whomever is doing the cross build targeting a sysroot using a GCC 342 // installation that is *not* within the system root to ensure two things: 343 // 344 // 1) Any DSOs that are linked in from this tree or from the install path 345 // above must be present on the system root and found via an 346 // appropriate rpath. 347 // 2) There must not be libraries installed into 348 // <prefix>/<triple>/<libdir> unless they should be preferred over 349 // those within the system root. 350 // 351 // Note that this matches the GCC behavior. See the below comment for where 352 // Clang diverges from GCC's behavior. 353 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" + 354 OSLibDir + SelectedMultilib.osSuffix(), 355 Paths); 356 357 // If the GCC installation we found is inside of the sysroot, we want to 358 // prefer libraries installed in the parent prefix of the GCC installation. 359 // It is important to *not* use these paths when the GCC installation is 360 // outside of the system root as that can pick up unintended libraries. 361 // This usually happens when there is an external cross compiler on the 362 // host system, and a more minimal sysroot available that is the target of 363 // the cross. Note that GCC does include some of these directories in some 364 // configurations but this seems somewhere between questionable and simply 365 // a bug. 366 if (StringRef(LibPath).startswith(SysRoot)) { 367 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths); 368 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths); 369 } 370 } 371 372 // Similar to the logic for GCC above, if we currently running Clang inside 373 // of the requested system root, add its parent library paths to 374 // those searched. 375 // FIXME: It's not clear whether we should use the driver's installed 376 // directory ('Dir' below) or the ResourceDir. 377 if (StringRef(D.Dir).startswith(SysRoot)) { 378 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 379 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 380 } 381 382 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 383 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 384 385 if (IsAndroid) { 386 // Android sysroots contain a library directory for each supported OS 387 // version as well as some unversioned libraries in the usual multiarch 388 // directory. 389 unsigned Major; 390 unsigned Minor; 391 unsigned Micro; 392 Triple.getEnvironmentVersion(Major, Minor, Micro); 393 addPathIfExists(D, 394 SysRoot + "/usr/lib/" + MultiarchTriple + "/" + 395 llvm::to_string(Major), 396 Paths); 397 } 398 399 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 400 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot 401 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle 402 // this here. 403 if (Triple.getVendor() == llvm::Triple::OpenEmbedded && 404 Triple.isArch64Bit()) 405 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths); 406 else 407 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 408 if (IsRISCV) { 409 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 410 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths); 411 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths); 412 } 413 414 // Try walking via the GCC triple path in case of biarch or multiarch GCC 415 // installations with strange symlinks. 416 if (GCCInstallation.isValid()) { 417 addPathIfExists(D, 418 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() + 419 "/../../" + OSLibDir, 420 Paths); 421 422 // Add the 'other' biarch variant path 423 Multilib BiarchSibling; 424 if (GCCInstallation.getBiarchSibling(BiarchSibling)) { 425 addPathIfExists(D, GCCInstallation.getInstallPath() + 426 BiarchSibling.gccSuffix(), 427 Paths); 428 } 429 430 // See comments above on the multilib variant for details of why this is 431 // included even from outside the sysroot. 432 const std::string &LibPath = 433 std::string(GCCInstallation.getParentLibPath()); 434 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 435 const Multilib &Multilib = GCCInstallation.getMultilib(); 436 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" + 437 Multilib.osSuffix(), 438 Paths); 439 440 // See comments above on the multilib variant for details of why this is 441 // only included from within the sysroot. 442 if (StringRef(LibPath).startswith(SysRoot)) 443 addPathIfExists(D, LibPath, Paths); 444 } 445 446 // Similar to the logic for GCC above, if we are currently running Clang 447 // inside of the requested system root, add its parent library path to those 448 // searched. 449 // FIXME: It's not clear whether we should use the driver's installed 450 // directory ('Dir' below) or the ResourceDir. 451 if (StringRef(D.Dir).startswith(SysRoot)) 452 addPathIfExists(D, D.Dir + "/../lib", Paths); 453 454 addPathIfExists(D, SysRoot + "/lib", Paths); 455 addPathIfExists(D, SysRoot + "/usr/lib", Paths); 456 } 457 458 ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const { 459 if (getTriple().isAndroid()) 460 return ToolChain::CST_Libcxx; 461 return ToolChain::CST_Libstdcxx; 462 } 463 464 bool Linux::HasNativeLLVMSupport() const { return true; } 465 466 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } 467 468 Tool *Linux::buildAssembler() const { 469 return new tools::gnutools::Assembler(*this); 470 } 471 472 std::string Linux::computeSysRoot() const { 473 if (!getDriver().SysRoot.empty()) 474 return getDriver().SysRoot; 475 476 if (getTriple().isAndroid()) { 477 // Android toolchains typically include a sysroot at ../sysroot relative to 478 // the clang binary. 479 const StringRef ClangDir = getDriver().getInstalledDir(); 480 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str(); 481 if (getVFS().exists(AndroidSysRootPath)) 482 return AndroidSysRootPath; 483 } 484 485 if (!GCCInstallation.isValid() || !getTriple().isMIPS()) 486 return std::string(); 487 488 // Standalone MIPS toolchains use different names for sysroot folder 489 // and put it into different places. Here we try to check some known 490 // variants. 491 492 const StringRef InstallDir = GCCInstallation.getInstallPath(); 493 const StringRef TripleStr = GCCInstallation.getTriple().str(); 494 const Multilib &Multilib = GCCInstallation.getMultilib(); 495 496 std::string Path = 497 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 498 .str(); 499 500 if (getVFS().exists(Path)) 501 return Path; 502 503 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 504 505 if (getVFS().exists(Path)) 506 return Path; 507 508 return std::string(); 509 } 510 511 std::string Linux::getDynamicLinker(const ArgList &Args) const { 512 const llvm::Triple::ArchType Arch = getArch(); 513 const llvm::Triple &Triple = getTriple(); 514 515 const Distro Distro(getDriver().getVFS(), Triple); 516 517 if (Triple.isAndroid()) 518 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 519 520 if (Triple.isMusl()) { 521 std::string ArchName; 522 bool IsArm = false; 523 524 switch (Arch) { 525 case llvm::Triple::arm: 526 case llvm::Triple::thumb: 527 ArchName = "arm"; 528 IsArm = true; 529 break; 530 case llvm::Triple::armeb: 531 case llvm::Triple::thumbeb: 532 ArchName = "armeb"; 533 IsArm = true; 534 break; 535 default: 536 ArchName = Triple.getArchName().str(); 537 } 538 if (IsArm && 539 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 540 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 541 ArchName += "hf"; 542 543 return "/lib/ld-musl-" + ArchName + ".so.1"; 544 } 545 546 std::string LibDir; 547 std::string Loader; 548 549 switch (Arch) { 550 default: 551 llvm_unreachable("unsupported architecture"); 552 553 case llvm::Triple::aarch64: 554 LibDir = "lib"; 555 Loader = "ld-linux-aarch64.so.1"; 556 break; 557 case llvm::Triple::aarch64_be: 558 LibDir = "lib"; 559 Loader = "ld-linux-aarch64_be.so.1"; 560 break; 561 case llvm::Triple::arm: 562 case llvm::Triple::thumb: 563 case llvm::Triple::armeb: 564 case llvm::Triple::thumbeb: { 565 const bool HF = 566 Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 567 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard; 568 569 LibDir = "lib"; 570 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3"; 571 break; 572 } 573 case llvm::Triple::mips: 574 case llvm::Triple::mipsel: 575 case llvm::Triple::mips64: 576 case llvm::Triple::mips64el: { 577 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 578 579 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 580 581 if (tools::mips::isUCLibc(Args)) 582 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 583 else if (!Triple.hasEnvironment() && 584 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 585 Loader = 586 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 587 else 588 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 589 590 break; 591 } 592 case llvm::Triple::ppc: 593 LibDir = "lib"; 594 Loader = "ld.so.1"; 595 break; 596 case llvm::Triple::ppc64: 597 LibDir = "lib64"; 598 Loader = 599 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 600 break; 601 case llvm::Triple::ppc64le: 602 LibDir = "lib64"; 603 Loader = 604 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 605 break; 606 case llvm::Triple::riscv32: { 607 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 608 LibDir = "lib"; 609 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 610 break; 611 } 612 case llvm::Triple::riscv64: { 613 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 614 LibDir = "lib"; 615 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 616 break; 617 } 618 case llvm::Triple::sparc: 619 case llvm::Triple::sparcel: 620 LibDir = "lib"; 621 Loader = "ld-linux.so.2"; 622 break; 623 case llvm::Triple::sparcv9: 624 LibDir = "lib64"; 625 Loader = "ld-linux.so.2"; 626 break; 627 case llvm::Triple::systemz: 628 LibDir = "lib"; 629 Loader = "ld64.so.1"; 630 break; 631 case llvm::Triple::x86: 632 LibDir = "lib"; 633 Loader = "ld-linux.so.2"; 634 break; 635 case llvm::Triple::x86_64: { 636 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 637 638 LibDir = X32 ? "libx32" : "lib64"; 639 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 640 break; 641 } 642 } 643 644 if (Distro == Distro::Exherbo && 645 (Triple.getVendor() == llvm::Triple::UnknownVendor || 646 Triple.getVendor() == llvm::Triple::PC)) 647 return "/usr/" + Triple.str() + "/lib/" + Loader; 648 return "/" + LibDir + "/" + Loader; 649 } 650 651 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 652 ArgStringList &CC1Args) const { 653 const Driver &D = getDriver(); 654 std::string SysRoot = computeSysRoot(); 655 656 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 657 return; 658 659 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 660 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 661 662 SmallString<128> ResourceDirInclude(D.ResourceDir); 663 llvm::sys::path::append(ResourceDirInclude, "include"); 664 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && 665 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc))) 666 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 667 668 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 669 return; 670 671 // Check for configure-time C include directories. 672 StringRef CIncludeDirs(C_INCLUDE_DIRS); 673 if (CIncludeDirs != "") { 674 SmallVector<StringRef, 5> dirs; 675 CIncludeDirs.split(dirs, ":"); 676 for (StringRef dir : dirs) { 677 StringRef Prefix = 678 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot); 679 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 680 } 681 return; 682 } 683 684 // Lacking those, try to detect the correct set of system includes for the 685 // target triple. 686 687 // Add include directories specific to the selected multilib set and multilib. 688 if (GCCInstallation.isValid()) { 689 const auto &Callback = Multilibs.includeDirsCallback(); 690 if (Callback) { 691 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 692 addExternCSystemIncludeIfExists( 693 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 694 } 695 } 696 697 // Implement generic Debian multiarch support. 698 const StringRef X86_64MultiarchIncludeDirs[] = { 699 "/usr/include/x86_64-linux-gnu", 700 701 // FIXME: These are older forms of multiarch. It's not clear that they're 702 // in use in any released version of Debian, so we should consider 703 // removing them. 704 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 705 const StringRef X86MultiarchIncludeDirs[] = { 706 "/usr/include/i386-linux-gnu", 707 708 // FIXME: These are older forms of multiarch. It's not clear that they're 709 // in use in any released version of Debian, so we should consider 710 // removing them. 711 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 712 "/usr/include/i486-linux-gnu"}; 713 const StringRef AArch64MultiarchIncludeDirs[] = { 714 "/usr/include/aarch64-linux-gnu"}; 715 const StringRef ARMMultiarchIncludeDirs[] = { 716 "/usr/include/arm-linux-gnueabi"}; 717 const StringRef ARMHFMultiarchIncludeDirs[] = { 718 "/usr/include/arm-linux-gnueabihf"}; 719 const StringRef ARMEBMultiarchIncludeDirs[] = { 720 "/usr/include/armeb-linux-gnueabi"}; 721 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 722 "/usr/include/armeb-linux-gnueabihf"}; 723 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 724 const StringRef MIPSELMultiarchIncludeDirs[] = { 725 "/usr/include/mipsel-linux-gnu"}; 726 const StringRef MIPS64MultiarchIncludeDirs[] = { 727 "/usr/include/mips64-linux-gnuabi64"}; 728 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 729 "/usr/include/mips64el-linux-gnuabi64"}; 730 const StringRef MIPSN32MultiarchIncludeDirs[] = { 731 "/usr/include/mips64-linux-gnuabin32"}; 732 const StringRef MIPSN32ELMultiarchIncludeDirs[] = { 733 "/usr/include/mips64el-linux-gnuabin32"}; 734 const StringRef MIPSR6MultiarchIncludeDirs[] = { 735 "/usr/include/mipsisa32-linux-gnu"}; 736 const StringRef MIPSR6ELMultiarchIncludeDirs[] = { 737 "/usr/include/mipsisa32r6el-linux-gnu"}; 738 const StringRef MIPS64R6MultiarchIncludeDirs[] = { 739 "/usr/include/mipsisa64r6-linux-gnuabi64"}; 740 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = { 741 "/usr/include/mipsisa64r6el-linux-gnuabi64"}; 742 const StringRef MIPSN32R6MultiarchIncludeDirs[] = { 743 "/usr/include/mipsisa64r6-linux-gnuabin32"}; 744 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = { 745 "/usr/include/mipsisa64r6el-linux-gnuabin32"}; 746 const StringRef PPCMultiarchIncludeDirs[] = { 747 "/usr/include/powerpc-linux-gnu", 748 "/usr/include/powerpc-linux-gnuspe"}; 749 const StringRef PPC64MultiarchIncludeDirs[] = { 750 "/usr/include/powerpc64-linux-gnu"}; 751 const StringRef PPC64LEMultiarchIncludeDirs[] = { 752 "/usr/include/powerpc64le-linux-gnu"}; 753 const StringRef SparcMultiarchIncludeDirs[] = { 754 "/usr/include/sparc-linux-gnu"}; 755 const StringRef Sparc64MultiarchIncludeDirs[] = { 756 "/usr/include/sparc64-linux-gnu"}; 757 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 758 "/usr/include/s390x-linux-gnu"}; 759 ArrayRef<StringRef> MultiarchIncludeDirs; 760 switch (getTriple().getArch()) { 761 case llvm::Triple::x86_64: 762 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 763 break; 764 case llvm::Triple::x86: 765 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 766 break; 767 case llvm::Triple::aarch64: 768 case llvm::Triple::aarch64_be: 769 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 770 break; 771 case llvm::Triple::arm: 772 case llvm::Triple::thumb: 773 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 774 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 775 else 776 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 777 break; 778 case llvm::Triple::armeb: 779 case llvm::Triple::thumbeb: 780 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 781 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 782 else 783 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 784 break; 785 case llvm::Triple::mips: 786 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 787 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs; 788 else 789 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 790 break; 791 case llvm::Triple::mipsel: 792 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 793 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs; 794 else 795 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 796 break; 797 case llvm::Triple::mips64: 798 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 799 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 800 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs; 801 else 802 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs; 803 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 804 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs; 805 else 806 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 807 break; 808 case llvm::Triple::mips64el: 809 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6) 810 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 811 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs; 812 else 813 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs; 814 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32) 815 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs; 816 else 817 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 818 break; 819 case llvm::Triple::ppc: 820 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 821 break; 822 case llvm::Triple::ppc64: 823 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 824 break; 825 case llvm::Triple::ppc64le: 826 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 827 break; 828 case llvm::Triple::sparc: 829 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 830 break; 831 case llvm::Triple::sparcv9: 832 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 833 break; 834 case llvm::Triple::systemz: 835 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 836 break; 837 default: 838 break; 839 } 840 841 const std::string AndroidMultiarchIncludeDir = 842 std::string("/usr/include/") + 843 getMultiarchTriple(D, getTriple(), SysRoot); 844 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir}; 845 if (getTriple().isAndroid()) 846 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs; 847 848 for (StringRef Dir : MultiarchIncludeDirs) { 849 if (D.getVFS().exists(SysRoot + Dir)) { 850 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 851 break; 852 } 853 } 854 855 if (getTriple().getOS() == llvm::Triple::RTEMS) 856 return; 857 858 // Add an include of '/include' directly. This isn't provided by default by 859 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 860 // add even when Clang is acting as-if it were a system compiler. 861 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 862 863 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 864 865 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl()) 866 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude); 867 } 868 869 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 870 llvm::opt::ArgStringList &CC1Args) const { 871 // Try generic GCC detection first. 872 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args)) 873 return; 874 875 // We need a detected GCC installation on Linux to provide libstdc++'s 876 // headers in odd Linuxish places. 877 if (!GCCInstallation.isValid()) 878 return; 879 880 StringRef LibDir = GCCInstallation.getParentLibPath(); 881 StringRef TripleStr = GCCInstallation.getTriple().str(); 882 const Multilib &Multilib = GCCInstallation.getMultilib(); 883 const GCCVersion &Version = GCCInstallation.getVersion(); 884 885 const std::string LibStdCXXIncludePathCandidates[] = { 886 // Android standalone toolchain has C++ headers in yet another place. 887 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 888 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 889 // without a subdirectory corresponding to the gcc version. 890 LibDir.str() + "/../include/c++", 891 // Cray's gcc installation puts headers under "g++" without a 892 // version suffix. 893 LibDir.str() + "/../include/g++", 894 }; 895 896 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 897 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 898 /*GCCMultiarchTriple*/ "", 899 /*TargetMultiarchTriple*/ "", 900 Multilib.includeSuffix(), DriverArgs, CC1Args)) 901 break; 902 } 903 } 904 905 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 906 ArgStringList &CC1Args) const { 907 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 908 } 909 910 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 911 ArgStringList &CC1Args) const { 912 if (GCCInstallation.isValid()) { 913 CC1Args.push_back("-isystem"); 914 CC1Args.push_back(DriverArgs.MakeArgString( 915 GCCInstallation.getParentLibPath() + "/../" + 916 GCCInstallation.getTriple().str() + "/include")); 917 } 918 } 919 920 bool Linux::isPIEDefault() const { 921 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || 922 getTriple().isMusl() || getSanitizerArgs().requiresPIE(); 923 } 924 925 bool Linux::isNoExecStackDefault() const { 926 return getTriple().isAndroid(); 927 } 928 929 bool Linux::IsMathErrnoDefault() const { 930 if (getTriple().isAndroid()) 931 return false; 932 return Generic_ELF::IsMathErrnoDefault(); 933 } 934 935 SanitizerMask Linux::getSupportedSanitizers() const { 936 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 937 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 938 const bool IsMIPS = getTriple().isMIPS32(); 939 const bool IsMIPS64 = getTriple().isMIPS64(); 940 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 941 getTriple().getArch() == llvm::Triple::ppc64le; 942 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 943 getTriple().getArch() == llvm::Triple::aarch64_be; 944 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 945 getTriple().getArch() == llvm::Triple::thumb || 946 getTriple().getArch() == llvm::Triple::armeb || 947 getTriple().getArch() == llvm::Triple::thumbeb; 948 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 949 Res |= SanitizerKind::Address; 950 Res |= SanitizerKind::PointerCompare; 951 Res |= SanitizerKind::PointerSubtract; 952 Res |= SanitizerKind::Fuzzer; 953 Res |= SanitizerKind::FuzzerNoLink; 954 Res |= SanitizerKind::KernelAddress; 955 Res |= SanitizerKind::Memory; 956 Res |= SanitizerKind::Vptr; 957 Res |= SanitizerKind::SafeStack; 958 if (IsX86_64 || IsMIPS64 || IsAArch64) 959 Res |= SanitizerKind::DataFlow; 960 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64) 961 Res |= SanitizerKind::Leak; 962 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 963 Res |= SanitizerKind::Thread; 964 if (IsX86_64) 965 Res |= SanitizerKind::KernelMemory; 966 if (IsX86 || IsX86_64) 967 Res |= SanitizerKind::Function; 968 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch || 969 IsPowerPC64) 970 Res |= SanitizerKind::Scudo; 971 if (IsX86_64 || IsAArch64) { 972 Res |= SanitizerKind::HWAddress; 973 Res |= SanitizerKind::KernelHWAddress; 974 } 975 return Res; 976 } 977 978 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 979 llvm::opt::ArgStringList &CmdArgs) const { 980 if (!needsProfileRT(Args)) return; 981 982 // Add linker option -u__llvm_runtime_variable to cause runtime 983 // initialization module to be linked in. 984 if ((!Args.hasArg(options::OPT_coverage)) && 985 (!Args.hasArg(options::OPT_ftest_coverage))) 986 CmdArgs.push_back(Args.MakeArgString( 987 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 988 ToolChain::addProfileRTLibs(Args, CmdArgs); 989 } 990 991 llvm::DenormalMode 992 Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs, 993 const JobAction &JA, 994 const llvm::fltSemantics *FPType) const { 995 switch (getTriple().getArch()) { 996 case llvm::Triple::x86: 997 case llvm::Triple::x86_64: { 998 std::string Unused; 999 // DAZ and FTZ are turned on in crtfastmath.o 1000 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) && 1001 isFastMathRuntimeAvailable(DriverArgs, Unused)) 1002 return llvm::DenormalMode::getPreserveSign(); 1003 return llvm::DenormalMode::getIEEE(); 1004 } 1005 default: 1006 return llvm::DenormalMode::getIEEE(); 1007 } 1008 } 1009 1010 void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { 1011 for (const auto &Opt : ExtraOpts) 1012 CmdArgs.push_back(Opt.c_str()); 1013 } 1014