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