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