1 //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Linux.h" 11 #include "Arch/ARM.h" 12 #include "Arch/Mips.h" 13 #include "Arch/PPC.h" 14 #include "Arch/RISCV.h" 15 #include "CommonArgs.h" 16 #include "clang/Basic/VirtualFileSystem.h" 17 #include "clang/Config/config.h" 18 #include "clang/Driver/Distro.h" 19 #include "clang/Driver/Driver.h" 20 #include "clang/Driver/Options.h" 21 #include "clang/Driver/SanitizerArgs.h" 22 #include "llvm/Option/ArgList.h" 23 #include "llvm/ProfileData/InstrProf.h" 24 #include "llvm/Support/Path.h" 25 #include "llvm/Support/ScopedPrinter.h" 26 #include <system_error> 27 28 using namespace clang::driver; 29 using namespace clang::driver::toolchains; 30 using namespace clang; 31 using namespace llvm::opt; 32 33 using tools::addPathIfExists; 34 35 /// \brief Get our best guess at the multiarch triple for a target. 36 /// 37 /// Debian-based systems are starting to use a multiarch setup where they use 38 /// a target-triple directory in the library and header search paths. 39 /// Unfortunately, this triple does not align with the vanilla target triple, 40 /// so we provide a rough mapping here. 41 static std::string getMultiarchTriple(const Driver &D, 42 const llvm::Triple &TargetTriple, 43 StringRef SysRoot) { 44 llvm::Triple::EnvironmentType TargetEnvironment = 45 TargetTriple.getEnvironment(); 46 bool IsAndroid = TargetTriple.isAndroid(); 47 48 // For most architectures, just use whatever we have rather than trying to be 49 // clever. 50 switch (TargetTriple.getArch()) { 51 default: 52 break; 53 54 // We use the existence of '/lib/<triple>' as a directory to detect some 55 // common linux triples that don't quite match the Clang triple for both 56 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these 57 // regardless of what the actual target triple is. 58 case llvm::Triple::arm: 59 case llvm::Triple::thumb: 60 if (IsAndroid) { 61 return "arm-linux-androideabi"; 62 } else if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 63 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf")) 64 return "arm-linux-gnueabihf"; 65 } else { 66 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi")) 67 return "arm-linux-gnueabi"; 68 } 69 break; 70 case llvm::Triple::armeb: 71 case llvm::Triple::thumbeb: 72 if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 73 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf")) 74 return "armeb-linux-gnueabihf"; 75 } else { 76 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi")) 77 return "armeb-linux-gnueabi"; 78 } 79 break; 80 case llvm::Triple::x86: 81 if (IsAndroid) 82 return "i686-linux-android"; 83 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu")) 84 return "i386-linux-gnu"; 85 break; 86 case llvm::Triple::x86_64: 87 if (IsAndroid) 88 return "x86_64-linux-android"; 89 // We don't want this for x32, otherwise it will match x86_64 libs 90 if (TargetEnvironment != llvm::Triple::GNUX32 && 91 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu")) 92 return "x86_64-linux-gnu"; 93 break; 94 case llvm::Triple::aarch64: 95 if (IsAndroid) 96 return "aarch64-linux-android"; 97 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu")) 98 return "aarch64-linux-gnu"; 99 break; 100 case llvm::Triple::aarch64_be: 101 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu")) 102 return "aarch64_be-linux-gnu"; 103 break; 104 case llvm::Triple::mips: 105 if (D.getVFS().exists(SysRoot + "/lib/mips-linux-gnu")) 106 return "mips-linux-gnu"; 107 break; 108 case llvm::Triple::mipsel: 109 if (IsAndroid) 110 return "mipsel-linux-android"; 111 if (D.getVFS().exists(SysRoot + "/lib/mipsel-linux-gnu")) 112 return "mipsel-linux-gnu"; 113 break; 114 case llvm::Triple::mips64: 115 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu")) 116 return "mips64-linux-gnu"; 117 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnuabi64")) 118 return "mips64-linux-gnuabi64"; 119 break; 120 case llvm::Triple::mips64el: 121 if (IsAndroid) 122 return "mips64el-linux-android"; 123 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu")) 124 return "mips64el-linux-gnu"; 125 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnuabi64")) 126 return "mips64el-linux-gnuabi64"; 127 break; 128 case llvm::Triple::ppc: 129 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe")) 130 return "powerpc-linux-gnuspe"; 131 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu")) 132 return "powerpc-linux-gnu"; 133 break; 134 case llvm::Triple::ppc64: 135 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu")) 136 return "powerpc64-linux-gnu"; 137 break; 138 case llvm::Triple::ppc64le: 139 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu")) 140 return "powerpc64le-linux-gnu"; 141 break; 142 case llvm::Triple::sparc: 143 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu")) 144 return "sparc-linux-gnu"; 145 break; 146 case llvm::Triple::sparcv9: 147 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu")) 148 return "sparc64-linux-gnu"; 149 break; 150 case llvm::Triple::systemz: 151 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu")) 152 return "s390x-linux-gnu"; 153 break; 154 } 155 return TargetTriple.str(); 156 } 157 158 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 159 if (tools::isMipsArch(Triple.getArch())) { 160 if (Triple.isAndroid()) { 161 StringRef CPUName; 162 StringRef ABIName; 163 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 164 if (CPUName == "mips32r6") 165 return "libr6"; 166 if (CPUName == "mips32r2") 167 return "libr2"; 168 } 169 // lib32 directory has a special meaning on MIPS targets. 170 // It contains N32 ABI binaries. Use this folder if produce 171 // code for N32 ABI only. 172 if (tools::mips::hasMipsAbiArg(Args, "n32")) 173 return "lib32"; 174 return Triple.isArch32Bit() ? "lib" : "lib64"; 175 } 176 177 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and 178 // using that variant while targeting other architectures causes problems 179 // because the libraries are laid out in shared system roots that can't cope 180 // with a 'lib32' library search path being considered. So we only enable 181 // them when we know we may need it. 182 // 183 // FIXME: This is a bit of a hack. We should really unify this code for 184 // reasoning about oslibdir spellings with the lib dir spellings in the 185 // GCCInstallationDetector, but that is a more significant refactoring. 186 if (Triple.getArch() == llvm::Triple::x86 || 187 Triple.getArch() == llvm::Triple::ppc) 188 return "lib32"; 189 190 if (Triple.getArch() == llvm::Triple::x86_64 && 191 Triple.getEnvironment() == llvm::Triple::GNUX32) 192 return "libx32"; 193 194 if (Triple.getArch() == llvm::Triple::riscv32) 195 return "lib32"; 196 197 return Triple.isArch32Bit() ? "lib" : "lib64"; 198 } 199 200 static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, 201 const Multilib &Multilib, 202 StringRef InstallPath, 203 ToolChain::path_list &Paths) { 204 if (const auto &PathsCallback = Multilibs.filePathsCallback()) 205 for (const auto &Path : PathsCallback(Multilib)) 206 addPathIfExists(D, InstallPath + Path, Paths); 207 } 208 209 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 210 : Generic_ELF(D, Triple, Args) { 211 GCCInstallation.init(Triple, Args); 212 Multilibs = GCCInstallation.getMultilibs(); 213 llvm::Triple::ArchType Arch = Triple.getArch(); 214 std::string SysRoot = computeSysRoot(); 215 216 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at 217 // least) put various tools in a triple-prefixed directory off of the parent 218 // of the GCC installation. We use the GCC triple here to ensure that we end 219 // up with tools that support the same amount of cross compiling as the 220 // detected GCC installation. For example, if we find a GCC installation 221 // targeting x86_64, but it is a bi-arch GCC installation, it can also be 222 // used to target i386. 223 // FIXME: This seems unlikely to be Linux-specific. 224 ToolChain::path_list &PPaths = getProgramPaths(); 225 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" + 226 GCCInstallation.getTriple().str() + "/bin") 227 .str()); 228 229 Distro Distro(D.getVFS()); 230 231 if (Distro.IsAlpineLinux()) { 232 ExtraOpts.push_back("-z"); 233 ExtraOpts.push_back("now"); 234 } 235 236 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux()) { 237 ExtraOpts.push_back("-z"); 238 ExtraOpts.push_back("relro"); 239 } 240 241 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 242 ExtraOpts.push_back("-X"); 243 244 const bool IsAndroid = Triple.isAndroid(); 245 const bool IsMips = tools::isMipsArch(Arch); 246 const bool IsHexagon = Arch == llvm::Triple::hexagon; 247 const bool IsRISCV = 248 Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64; 249 250 if (IsMips && !SysRoot.empty()) 251 ExtraOpts.push_back("--sysroot=" + SysRoot); 252 253 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 254 // and the MIPS ABI require .dynsym to be sorted in different ways. 255 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 256 // ABI requires a mapping between the GOT and the symbol table. 257 // Android loader does not support .gnu.hash. 258 // Hexagon linker/loader does not support .gnu.hash 259 if (!IsMips && !IsAndroid && !IsHexagon) { 260 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() || 261 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick)) 262 ExtraOpts.push_back("--hash-style=gnu"); 263 264 if (Distro.IsDebian() || Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid || 265 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic) 266 ExtraOpts.push_back("--hash-style=both"); 267 } 268 269 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6) 270 ExtraOpts.push_back("--no-add-needed"); 271 272 #ifdef ENABLE_LINKER_BUILD_ID 273 ExtraOpts.push_back("--build-id"); 274 #endif 275 276 if (IsAndroid || Distro.IsOpenSUSE()) 277 ExtraOpts.push_back("--enable-new-dtags"); 278 279 // The selection of paths to try here is designed to match the patterns which 280 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 281 // This was determined by running GCC in a fake filesystem, creating all 282 // possible permutations of these directories, and seeing which ones it added 283 // to the link paths. 284 path_list &Paths = getFilePaths(); 285 286 const std::string OSLibDir = getOSLibDir(Triple, Args); 287 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 288 289 // Add the multilib suffixed paths where they are available. 290 if (GCCInstallation.isValid()) { 291 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 292 const std::string &LibPath = GCCInstallation.getParentLibPath(); 293 const Multilib &Multilib = GCCInstallation.getMultilib(); 294 const MultilibSet &Multilibs = GCCInstallation.getMultilibs(); 295 296 // Add toolchain / multilib specific file paths. 297 addMultilibsFilePaths(D, Multilibs, Multilib, 298 GCCInstallation.getInstallPath(), Paths); 299 300 // Sourcery CodeBench MIPS toolchain holds some libraries under 301 // a biarch-like suffix of the GCC installation. 302 addPathIfExists(D, GCCInstallation.getInstallPath() + Multilib.gccSuffix(), 303 Paths); 304 305 // GCC cross compiling toolchains will install target libraries which ship 306 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as 307 // any part of the GCC installation in 308 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat 309 // debatable, but is the reality today. We need to search this tree even 310 // when we have a sysroot somewhere else. It is the responsibility of 311 // whomever is doing the cross build targeting a sysroot using a GCC 312 // installation that is *not* within the system root to ensure two things: 313 // 314 // 1) Any DSOs that are linked in from this tree or from the install path 315 // above must be present on the system root and found via an 316 // appropriate rpath. 317 // 2) There must not be libraries installed into 318 // <prefix>/<triple>/<libdir> unless they should be preferred over 319 // those within the system root. 320 // 321 // Note that this matches the GCC behavior. See the below comment for where 322 // Clang diverges from GCC's behavior. 323 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" + 324 OSLibDir + Multilib.osSuffix(), 325 Paths); 326 327 // If the GCC installation we found is inside of the sysroot, we want to 328 // prefer libraries installed in the parent prefix of the GCC installation. 329 // It is important to *not* use these paths when the GCC installation is 330 // outside of the system root as that can pick up unintended libraries. 331 // This usually happens when there is an external cross compiler on the 332 // host system, and a more minimal sysroot available that is the target of 333 // the cross. Note that GCC does include some of these directories in some 334 // configurations but this seems somewhere between questionable and simply 335 // a bug. 336 if (StringRef(LibPath).startswith(SysRoot)) { 337 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths); 338 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths); 339 } 340 } 341 342 // Similar to the logic for GCC above, if we currently running Clang inside 343 // of the requested system root, add its parent library paths to 344 // those searched. 345 // FIXME: It's not clear whether we should use the driver's installed 346 // directory ('Dir' below) or the ResourceDir. 347 if (StringRef(D.Dir).startswith(SysRoot)) { 348 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 349 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 350 } 351 352 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 353 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 354 355 if (IsAndroid) { 356 // Android sysroots contain a library directory for each supported OS 357 // version as well as some unversioned libraries in the usual multiarch 358 // directory. 359 unsigned Major; 360 unsigned Minor; 361 unsigned Micro; 362 Triple.getEnvironmentVersion(Major, Minor, Micro); 363 addPathIfExists(D, 364 SysRoot + "/usr/lib/" + MultiarchTriple + "/" + 365 llvm::to_string(Major), 366 Paths); 367 } 368 369 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 370 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 371 if (IsRISCV) { 372 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 373 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths); 374 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths); 375 } 376 377 // Try walking via the GCC triple path in case of biarch or multiarch GCC 378 // installations with strange symlinks. 379 if (GCCInstallation.isValid()) { 380 addPathIfExists(D, 381 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() + 382 "/../../" + OSLibDir, 383 Paths); 384 385 // Add the 'other' biarch variant path 386 Multilib BiarchSibling; 387 if (GCCInstallation.getBiarchSibling(BiarchSibling)) { 388 addPathIfExists(D, GCCInstallation.getInstallPath() + 389 BiarchSibling.gccSuffix(), 390 Paths); 391 } 392 393 // See comments above on the multilib variant for details of why this is 394 // included even from outside the sysroot. 395 const std::string &LibPath = GCCInstallation.getParentLibPath(); 396 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 397 const Multilib &Multilib = GCCInstallation.getMultilib(); 398 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" + 399 Multilib.osSuffix(), 400 Paths); 401 402 // See comments above on the multilib variant for details of why this is 403 // only included from within the sysroot. 404 if (StringRef(LibPath).startswith(SysRoot)) 405 addPathIfExists(D, LibPath, Paths); 406 } 407 408 // Similar to the logic for GCC above, if we are currently running Clang 409 // inside of the requested system root, add its parent library path to those 410 // searched. 411 // FIXME: It's not clear whether we should use the driver's installed 412 // directory ('Dir' below) or the ResourceDir. 413 if (StringRef(D.Dir).startswith(SysRoot)) 414 addPathIfExists(D, D.Dir + "/../lib", Paths); 415 416 addPathIfExists(D, SysRoot + "/lib", Paths); 417 addPathIfExists(D, SysRoot + "/usr/lib", Paths); 418 } 419 420 bool Linux::HasNativeLLVMSupport() const { return true; } 421 422 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } 423 424 Tool *Linux::buildAssembler() const { 425 return new tools::gnutools::Assembler(*this); 426 } 427 428 std::string Linux::computeSysRoot() const { 429 if (!getDriver().SysRoot.empty()) 430 return getDriver().SysRoot; 431 432 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch())) 433 return std::string(); 434 435 // Standalone MIPS toolchains use different names for sysroot folder 436 // and put it into different places. Here we try to check some known 437 // variants. 438 439 const StringRef InstallDir = GCCInstallation.getInstallPath(); 440 const StringRef TripleStr = GCCInstallation.getTriple().str(); 441 const Multilib &Multilib = GCCInstallation.getMultilib(); 442 443 std::string Path = 444 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 445 .str(); 446 447 if (getVFS().exists(Path)) 448 return Path; 449 450 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 451 452 if (getVFS().exists(Path)) 453 return Path; 454 455 return std::string(); 456 } 457 458 std::string Linux::getDynamicLinker(const ArgList &Args) const { 459 const llvm::Triple::ArchType Arch = getArch(); 460 const llvm::Triple &Triple = getTriple(); 461 462 const Distro Distro(getDriver().getVFS()); 463 464 if (Triple.isAndroid()) 465 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 466 467 if (Triple.isMusl()) { 468 std::string ArchName; 469 bool IsArm = false; 470 471 switch (Arch) { 472 case llvm::Triple::arm: 473 case llvm::Triple::thumb: 474 ArchName = "arm"; 475 IsArm = true; 476 break; 477 case llvm::Triple::armeb: 478 case llvm::Triple::thumbeb: 479 ArchName = "armeb"; 480 IsArm = true; 481 break; 482 default: 483 ArchName = Triple.getArchName().str(); 484 } 485 if (IsArm && 486 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 487 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 488 ArchName += "hf"; 489 490 return "/lib/ld-musl-" + ArchName + ".so.1"; 491 } 492 493 std::string LibDir; 494 std::string Loader; 495 496 switch (Arch) { 497 default: 498 llvm_unreachable("unsupported architecture"); 499 500 case llvm::Triple::aarch64: 501 LibDir = "lib"; 502 Loader = "ld-linux-aarch64.so.1"; 503 break; 504 case llvm::Triple::aarch64_be: 505 LibDir = "lib"; 506 Loader = "ld-linux-aarch64_be.so.1"; 507 break; 508 case llvm::Triple::arm: 509 case llvm::Triple::thumb: 510 case llvm::Triple::armeb: 511 case llvm::Triple::thumbeb: { 512 const bool HF = 513 Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 514 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard; 515 516 LibDir = "lib"; 517 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3"; 518 break; 519 } 520 case llvm::Triple::mips: 521 case llvm::Triple::mipsel: 522 case llvm::Triple::mips64: 523 case llvm::Triple::mips64el: { 524 bool LE = (Triple.getArch() == llvm::Triple::mipsel) || 525 (Triple.getArch() == llvm::Triple::mips64el); 526 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 527 528 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 529 530 if (tools::mips::isUCLibc(Args)) 531 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 532 else if (!Triple.hasEnvironment() && 533 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 534 Loader = LE ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 535 else 536 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 537 538 break; 539 } 540 case llvm::Triple::ppc: 541 LibDir = "lib"; 542 Loader = "ld.so.1"; 543 break; 544 case llvm::Triple::ppc64: 545 LibDir = "lib64"; 546 Loader = 547 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 548 break; 549 case llvm::Triple::ppc64le: 550 LibDir = "lib64"; 551 Loader = 552 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 553 break; 554 case llvm::Triple::riscv32: { 555 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 556 LibDir = "lib"; 557 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 558 break; 559 } 560 case llvm::Triple::riscv64: { 561 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 562 LibDir = "lib"; 563 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 564 break; 565 } 566 case llvm::Triple::sparc: 567 case llvm::Triple::sparcel: 568 LibDir = "lib"; 569 Loader = "ld-linux.so.2"; 570 break; 571 case llvm::Triple::sparcv9: 572 LibDir = "lib64"; 573 Loader = "ld-linux.so.2"; 574 break; 575 case llvm::Triple::systemz: 576 LibDir = "lib"; 577 Loader = "ld64.so.1"; 578 break; 579 case llvm::Triple::x86: 580 LibDir = "lib"; 581 Loader = "ld-linux.so.2"; 582 break; 583 case llvm::Triple::x86_64: { 584 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 585 586 LibDir = X32 ? "libx32" : "lib64"; 587 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 588 break; 589 } 590 } 591 592 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor || 593 Triple.getVendor() == llvm::Triple::PC)) 594 return "/usr/" + Triple.str() + "/lib/" + Loader; 595 return "/" + LibDir + "/" + Loader; 596 } 597 598 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 599 ArgStringList &CC1Args) const { 600 const Driver &D = getDriver(); 601 std::string SysRoot = computeSysRoot(); 602 603 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 604 return; 605 606 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 607 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 608 609 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 610 SmallString<128> P(D.ResourceDir); 611 llvm::sys::path::append(P, "include"); 612 addSystemInclude(DriverArgs, CC1Args, P); 613 } 614 615 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 616 return; 617 618 // Check for configure-time C include directories. 619 StringRef CIncludeDirs(C_INCLUDE_DIRS); 620 if (CIncludeDirs != "") { 621 SmallVector<StringRef, 5> dirs; 622 CIncludeDirs.split(dirs, ":"); 623 for (StringRef dir : dirs) { 624 StringRef Prefix = 625 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; 626 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 627 } 628 return; 629 } 630 631 // Lacking those, try to detect the correct set of system includes for the 632 // target triple. 633 634 // Add include directories specific to the selected multilib set and multilib. 635 if (GCCInstallation.isValid()) { 636 const auto &Callback = Multilibs.includeDirsCallback(); 637 if (Callback) { 638 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 639 addExternCSystemIncludeIfExists( 640 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 641 } 642 } 643 644 // Implement generic Debian multiarch support. 645 const StringRef X86_64MultiarchIncludeDirs[] = { 646 "/usr/include/x86_64-linux-gnu", 647 648 // FIXME: These are older forms of multiarch. It's not clear that they're 649 // in use in any released version of Debian, so we should consider 650 // removing them. 651 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 652 const StringRef X86MultiarchIncludeDirs[] = { 653 "/usr/include/i386-linux-gnu", 654 655 // FIXME: These are older forms of multiarch. It's not clear that they're 656 // in use in any released version of Debian, so we should consider 657 // removing them. 658 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 659 "/usr/include/i486-linux-gnu"}; 660 const StringRef AArch64MultiarchIncludeDirs[] = { 661 "/usr/include/aarch64-linux-gnu"}; 662 const StringRef ARMMultiarchIncludeDirs[] = { 663 "/usr/include/arm-linux-gnueabi"}; 664 const StringRef ARMHFMultiarchIncludeDirs[] = { 665 "/usr/include/arm-linux-gnueabihf"}; 666 const StringRef ARMEBMultiarchIncludeDirs[] = { 667 "/usr/include/armeb-linux-gnueabi"}; 668 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 669 "/usr/include/armeb-linux-gnueabihf"}; 670 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 671 const StringRef MIPSELMultiarchIncludeDirs[] = { 672 "/usr/include/mipsel-linux-gnu"}; 673 const StringRef MIPS64MultiarchIncludeDirs[] = { 674 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"}; 675 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 676 "/usr/include/mips64el-linux-gnu", 677 "/usr/include/mips64el-linux-gnuabi64"}; 678 const StringRef PPCMultiarchIncludeDirs[] = { 679 "/usr/include/powerpc-linux-gnu"}; 680 const StringRef PPC64MultiarchIncludeDirs[] = { 681 "/usr/include/powerpc64-linux-gnu"}; 682 const StringRef PPC64LEMultiarchIncludeDirs[] = { 683 "/usr/include/powerpc64le-linux-gnu"}; 684 const StringRef SparcMultiarchIncludeDirs[] = { 685 "/usr/include/sparc-linux-gnu"}; 686 const StringRef Sparc64MultiarchIncludeDirs[] = { 687 "/usr/include/sparc64-linux-gnu"}; 688 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 689 "/usr/include/s390x-linux-gnu"}; 690 ArrayRef<StringRef> MultiarchIncludeDirs; 691 switch (getTriple().getArch()) { 692 case llvm::Triple::x86_64: 693 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 694 break; 695 case llvm::Triple::x86: 696 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 697 break; 698 case llvm::Triple::aarch64: 699 case llvm::Triple::aarch64_be: 700 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 701 break; 702 case llvm::Triple::arm: 703 case llvm::Triple::thumb: 704 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 705 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 706 else 707 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 708 break; 709 case llvm::Triple::armeb: 710 case llvm::Triple::thumbeb: 711 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 712 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 713 else 714 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 715 break; 716 case llvm::Triple::mips: 717 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 718 break; 719 case llvm::Triple::mipsel: 720 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 721 break; 722 case llvm::Triple::mips64: 723 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 724 break; 725 case llvm::Triple::mips64el: 726 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 727 break; 728 case llvm::Triple::ppc: 729 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 730 break; 731 case llvm::Triple::ppc64: 732 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 733 break; 734 case llvm::Triple::ppc64le: 735 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 736 break; 737 case llvm::Triple::sparc: 738 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 739 break; 740 case llvm::Triple::sparcv9: 741 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 742 break; 743 case llvm::Triple::systemz: 744 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 745 break; 746 default: 747 break; 748 } 749 750 const std::string AndroidMultiarchIncludeDir = 751 std::string("/usr/include/") + 752 getMultiarchTriple(D, getTriple(), SysRoot); 753 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir}; 754 if (getTriple().isAndroid()) 755 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs; 756 757 for (StringRef Dir : MultiarchIncludeDirs) { 758 if (D.getVFS().exists(SysRoot + Dir)) { 759 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 760 break; 761 } 762 } 763 764 if (getTriple().getOS() == llvm::Triple::RTEMS) 765 return; 766 767 // Add an include of '/include' directly. This isn't provided by default by 768 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 769 // add even when Clang is acting as-if it were a system compiler. 770 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 771 772 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 773 } 774 775 static std::string DetectLibcxxIncludePath(StringRef base) { 776 std::error_code EC; 777 int MaxVersion = 0; 778 std::string MaxVersionString = ""; 779 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE; 780 LI = LI.increment(EC)) { 781 StringRef VersionText = llvm::sys::path::filename(LI->path()); 782 int Version; 783 if (VersionText[0] == 'v' && 784 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 785 if (Version > MaxVersion) { 786 MaxVersion = Version; 787 MaxVersionString = VersionText; 788 } 789 } 790 } 791 return MaxVersion ? (base + "/" + MaxVersionString).str() : ""; 792 } 793 794 void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 795 llvm::opt::ArgStringList &CC1Args) const { 796 const std::string LibCXXIncludePathCandidates[] = { 797 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"), 798 // If this is a development, non-installed, clang, libcxx will 799 // not be found at ../include/c++ but it likely to be found at 800 // one of the following two locations: 801 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/local/include/c++"), 802 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/include/c++") }; 803 for (const auto &IncludePath : LibCXXIncludePathCandidates) { 804 if (IncludePath.empty() || !getVFS().exists(IncludePath)) 805 continue; 806 // Use the first candidate that exists. 807 addSystemInclude(DriverArgs, CC1Args, IncludePath); 808 return; 809 } 810 } 811 812 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 813 llvm::opt::ArgStringList &CC1Args) const { 814 // We need a detected GCC installation on Linux to provide libstdc++'s 815 // headers. 816 if (!GCCInstallation.isValid()) 817 return; 818 819 // By default, look for the C++ headers in an include directory adjacent to 820 // the lib directory of the GCC installation. Note that this is expect to be 821 // equivalent to '/usr/include/c++/X.Y' in almost all cases. 822 StringRef LibDir = GCCInstallation.getParentLibPath(); 823 StringRef InstallDir = GCCInstallation.getInstallPath(); 824 StringRef TripleStr = GCCInstallation.getTriple().str(); 825 const Multilib &Multilib = GCCInstallation.getMultilib(); 826 const std::string GCCMultiarchTriple = getMultiarchTriple( 827 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot); 828 const std::string TargetMultiarchTriple = 829 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 830 const GCCVersion &Version = GCCInstallation.getVersion(); 831 832 // The primary search for libstdc++ supports multiarch variants. 833 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", 834 "/c++/" + Version.Text, TripleStr, 835 GCCMultiarchTriple, TargetMultiarchTriple, 836 Multilib.includeSuffix(), DriverArgs, CC1Args)) 837 return; 838 839 // Otherwise, fall back on a bunch of options which don't use multiarch 840 // layouts for simplicity. 841 const std::string LibStdCXXIncludePathCandidates[] = { 842 // Gentoo is weird and places its headers inside the GCC install, 843 // so if the first attempt to find the headers fails, try these patterns. 844 InstallDir.str() + "/include/g++-v" + Version.Text, 845 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." + 846 Version.MinorStr, 847 InstallDir.str() + "/include/g++-v" + Version.MajorStr, 848 // Android standalone toolchain has C++ headers in yet another place. 849 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 850 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 851 // without a subdirectory corresponding to the gcc version. 852 LibDir.str() + "/../include/c++", 853 }; 854 855 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 856 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 857 /*GCCMultiarchTriple*/ "", 858 /*TargetMultiarchTriple*/ "", 859 Multilib.includeSuffix(), DriverArgs, CC1Args)) 860 break; 861 } 862 } 863 864 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 865 ArgStringList &CC1Args) const { 866 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 867 } 868 869 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 870 ArgStringList &CC1Args) const { 871 if (GCCInstallation.isValid()) { 872 CC1Args.push_back("-isystem"); 873 CC1Args.push_back(DriverArgs.MakeArgString( 874 GCCInstallation.getParentLibPath() + "/../" + 875 GCCInstallation.getTriple().str() + "/include")); 876 } 877 } 878 879 bool Linux::isPIEDefault() const { 880 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || 881 getTriple().isMusl() || getSanitizerArgs().requiresPIE(); 882 } 883 884 SanitizerMask Linux::getSupportedSanitizers() const { 885 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 886 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 887 const bool IsMIPS = getTriple().getArch() == llvm::Triple::mips || 888 getTriple().getArch() == llvm::Triple::mipsel; 889 const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 || 890 getTriple().getArch() == llvm::Triple::mips64el; 891 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 892 getTriple().getArch() == llvm::Triple::ppc64le; 893 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 894 getTriple().getArch() == llvm::Triple::aarch64_be; 895 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 896 getTriple().getArch() == llvm::Triple::thumb || 897 getTriple().getArch() == llvm::Triple::armeb || 898 getTriple().getArch() == llvm::Triple::thumbeb; 899 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 900 Res |= SanitizerKind::Address; 901 Res |= SanitizerKind::Fuzzer; 902 Res |= SanitizerKind::FuzzerNoLink; 903 Res |= SanitizerKind::KernelAddress; 904 Res |= SanitizerKind::Memory; 905 Res |= SanitizerKind::Vptr; 906 Res |= SanitizerKind::SafeStack; 907 if (IsX86_64 || IsMIPS64 || IsAArch64) 908 Res |= SanitizerKind::DataFlow; 909 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64) 910 Res |= SanitizerKind::Leak; 911 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 912 Res |= SanitizerKind::Thread; 913 if (IsX86_64 || IsMIPS64) 914 Res |= SanitizerKind::Efficiency; 915 if (IsX86 || IsX86_64) 916 Res |= SanitizerKind::Function; 917 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch) 918 Res |= SanitizerKind::Scudo; 919 if (IsX86_64 || IsAArch64) { 920 Res |= SanitizerKind::HWAddress; 921 Res |= SanitizerKind::KernelHWAddress; 922 } 923 return Res; 924 } 925 926 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 927 llvm::opt::ArgStringList &CmdArgs) const { 928 if (!needsProfileRT(Args)) return; 929 930 // Add linker option -u__llvm_runtime_variable to cause runtime 931 // initialization module to be linked in. 932 if (!Args.hasArg(options::OPT_coverage)) 933 CmdArgs.push_back(Args.MakeArgString( 934 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 935 ToolChain::addProfileRTLibs(Args, CmdArgs); 936 } 937