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 /// 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 (getTriple().isAndroid()) { 433 // Android toolchains typically include a sysroot at ../sysroot relative to 434 // the clang binary. 435 const StringRef ClangDir = getDriver().getInstalledDir(); 436 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str(); 437 if (getVFS().exists(AndroidSysRootPath)) 438 return AndroidSysRootPath; 439 } 440 441 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch())) 442 return std::string(); 443 444 // Standalone MIPS toolchains use different names for sysroot folder 445 // and put it into different places. Here we try to check some known 446 // variants. 447 448 const StringRef InstallDir = GCCInstallation.getInstallPath(); 449 const StringRef TripleStr = GCCInstallation.getTriple().str(); 450 const Multilib &Multilib = GCCInstallation.getMultilib(); 451 452 std::string Path = 453 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 454 .str(); 455 456 if (getVFS().exists(Path)) 457 return Path; 458 459 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 460 461 if (getVFS().exists(Path)) 462 return Path; 463 464 return std::string(); 465 } 466 467 std::string Linux::getDynamicLinker(const ArgList &Args) const { 468 const llvm::Triple::ArchType Arch = getArch(); 469 const llvm::Triple &Triple = getTriple(); 470 471 const Distro Distro(getDriver().getVFS()); 472 473 if (Triple.isAndroid()) 474 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 475 476 if (Triple.isMusl()) { 477 std::string ArchName; 478 bool IsArm = false; 479 480 switch (Arch) { 481 case llvm::Triple::arm: 482 case llvm::Triple::thumb: 483 ArchName = "arm"; 484 IsArm = true; 485 break; 486 case llvm::Triple::armeb: 487 case llvm::Triple::thumbeb: 488 ArchName = "armeb"; 489 IsArm = true; 490 break; 491 default: 492 ArchName = Triple.getArchName().str(); 493 } 494 if (IsArm && 495 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 496 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 497 ArchName += "hf"; 498 499 return "/lib/ld-musl-" + ArchName + ".so.1"; 500 } 501 502 std::string LibDir; 503 std::string Loader; 504 505 switch (Arch) { 506 default: 507 llvm_unreachable("unsupported architecture"); 508 509 case llvm::Triple::aarch64: 510 LibDir = "lib"; 511 Loader = "ld-linux-aarch64.so.1"; 512 break; 513 case llvm::Triple::aarch64_be: 514 LibDir = "lib"; 515 Loader = "ld-linux-aarch64_be.so.1"; 516 break; 517 case llvm::Triple::arm: 518 case llvm::Triple::thumb: 519 case llvm::Triple::armeb: 520 case llvm::Triple::thumbeb: { 521 const bool HF = 522 Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 523 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard; 524 525 LibDir = "lib"; 526 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3"; 527 break; 528 } 529 case llvm::Triple::mips: 530 case llvm::Triple::mipsel: 531 case llvm::Triple::mips64: 532 case llvm::Triple::mips64el: { 533 bool LE = (Triple.getArch() == llvm::Triple::mipsel) || 534 (Triple.getArch() == llvm::Triple::mips64el); 535 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 536 537 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 538 539 if (tools::mips::isUCLibc(Args)) 540 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 541 else if (!Triple.hasEnvironment() && 542 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 543 Loader = LE ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 544 else 545 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 546 547 break; 548 } 549 case llvm::Triple::ppc: 550 LibDir = "lib"; 551 Loader = "ld.so.1"; 552 break; 553 case llvm::Triple::ppc64: 554 LibDir = "lib64"; 555 Loader = 556 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 557 break; 558 case llvm::Triple::ppc64le: 559 LibDir = "lib64"; 560 Loader = 561 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 562 break; 563 case llvm::Triple::riscv32: { 564 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 565 LibDir = "lib"; 566 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 567 break; 568 } 569 case llvm::Triple::riscv64: { 570 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 571 LibDir = "lib"; 572 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 573 break; 574 } 575 case llvm::Triple::sparc: 576 case llvm::Triple::sparcel: 577 LibDir = "lib"; 578 Loader = "ld-linux.so.2"; 579 break; 580 case llvm::Triple::sparcv9: 581 LibDir = "lib64"; 582 Loader = "ld-linux.so.2"; 583 break; 584 case llvm::Triple::systemz: 585 LibDir = "lib"; 586 Loader = "ld64.so.1"; 587 break; 588 case llvm::Triple::x86: 589 LibDir = "lib"; 590 Loader = "ld-linux.so.2"; 591 break; 592 case llvm::Triple::x86_64: { 593 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 594 595 LibDir = X32 ? "libx32" : "lib64"; 596 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 597 break; 598 } 599 } 600 601 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor || 602 Triple.getVendor() == llvm::Triple::PC)) 603 return "/usr/" + Triple.str() + "/lib/" + Loader; 604 return "/" + LibDir + "/" + Loader; 605 } 606 607 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 608 ArgStringList &CC1Args) const { 609 const Driver &D = getDriver(); 610 std::string SysRoot = computeSysRoot(); 611 612 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 613 return; 614 615 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 616 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 617 618 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 619 SmallString<128> P(D.ResourceDir); 620 llvm::sys::path::append(P, "include"); 621 addSystemInclude(DriverArgs, CC1Args, P); 622 } 623 624 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 625 return; 626 627 // Check for configure-time C include directories. 628 StringRef CIncludeDirs(C_INCLUDE_DIRS); 629 if (CIncludeDirs != "") { 630 SmallVector<StringRef, 5> dirs; 631 CIncludeDirs.split(dirs, ":"); 632 for (StringRef dir : dirs) { 633 StringRef Prefix = 634 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; 635 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 636 } 637 return; 638 } 639 640 // Lacking those, try to detect the correct set of system includes for the 641 // target triple. 642 643 // Add include directories specific to the selected multilib set and multilib. 644 if (GCCInstallation.isValid()) { 645 const auto &Callback = Multilibs.includeDirsCallback(); 646 if (Callback) { 647 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 648 addExternCSystemIncludeIfExists( 649 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 650 } 651 } 652 653 // Implement generic Debian multiarch support. 654 const StringRef X86_64MultiarchIncludeDirs[] = { 655 "/usr/include/x86_64-linux-gnu", 656 657 // FIXME: These are older forms of multiarch. It's not clear that they're 658 // in use in any released version of Debian, so we should consider 659 // removing them. 660 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 661 const StringRef X86MultiarchIncludeDirs[] = { 662 "/usr/include/i386-linux-gnu", 663 664 // FIXME: These are older forms of multiarch. It's not clear that they're 665 // in use in any released version of Debian, so we should consider 666 // removing them. 667 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 668 "/usr/include/i486-linux-gnu"}; 669 const StringRef AArch64MultiarchIncludeDirs[] = { 670 "/usr/include/aarch64-linux-gnu"}; 671 const StringRef ARMMultiarchIncludeDirs[] = { 672 "/usr/include/arm-linux-gnueabi"}; 673 const StringRef ARMHFMultiarchIncludeDirs[] = { 674 "/usr/include/arm-linux-gnueabihf"}; 675 const StringRef ARMEBMultiarchIncludeDirs[] = { 676 "/usr/include/armeb-linux-gnueabi"}; 677 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 678 "/usr/include/armeb-linux-gnueabihf"}; 679 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 680 const StringRef MIPSELMultiarchIncludeDirs[] = { 681 "/usr/include/mipsel-linux-gnu"}; 682 const StringRef MIPS64MultiarchIncludeDirs[] = { 683 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"}; 684 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 685 "/usr/include/mips64el-linux-gnu", 686 "/usr/include/mips64el-linux-gnuabi64"}; 687 const StringRef PPCMultiarchIncludeDirs[] = { 688 "/usr/include/powerpc-linux-gnu"}; 689 const StringRef PPC64MultiarchIncludeDirs[] = { 690 "/usr/include/powerpc64-linux-gnu"}; 691 const StringRef PPC64LEMultiarchIncludeDirs[] = { 692 "/usr/include/powerpc64le-linux-gnu"}; 693 const StringRef SparcMultiarchIncludeDirs[] = { 694 "/usr/include/sparc-linux-gnu"}; 695 const StringRef Sparc64MultiarchIncludeDirs[] = { 696 "/usr/include/sparc64-linux-gnu"}; 697 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 698 "/usr/include/s390x-linux-gnu"}; 699 ArrayRef<StringRef> MultiarchIncludeDirs; 700 switch (getTriple().getArch()) { 701 case llvm::Triple::x86_64: 702 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 703 break; 704 case llvm::Triple::x86: 705 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 706 break; 707 case llvm::Triple::aarch64: 708 case llvm::Triple::aarch64_be: 709 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 710 break; 711 case llvm::Triple::arm: 712 case llvm::Triple::thumb: 713 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 714 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 715 else 716 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 717 break; 718 case llvm::Triple::armeb: 719 case llvm::Triple::thumbeb: 720 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 721 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 722 else 723 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 724 break; 725 case llvm::Triple::mips: 726 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 727 break; 728 case llvm::Triple::mipsel: 729 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 730 break; 731 case llvm::Triple::mips64: 732 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 733 break; 734 case llvm::Triple::mips64el: 735 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 736 break; 737 case llvm::Triple::ppc: 738 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 739 break; 740 case llvm::Triple::ppc64: 741 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 742 break; 743 case llvm::Triple::ppc64le: 744 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 745 break; 746 case llvm::Triple::sparc: 747 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 748 break; 749 case llvm::Triple::sparcv9: 750 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 751 break; 752 case llvm::Triple::systemz: 753 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 754 break; 755 default: 756 break; 757 } 758 759 const std::string AndroidMultiarchIncludeDir = 760 std::string("/usr/include/") + 761 getMultiarchTriple(D, getTriple(), SysRoot); 762 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir}; 763 if (getTriple().isAndroid()) 764 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs; 765 766 for (StringRef Dir : MultiarchIncludeDirs) { 767 if (D.getVFS().exists(SysRoot + Dir)) { 768 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 769 break; 770 } 771 } 772 773 if (getTriple().getOS() == llvm::Triple::RTEMS) 774 return; 775 776 // Add an include of '/include' directly. This isn't provided by default by 777 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 778 // add even when Clang is acting as-if it were a system compiler. 779 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 780 781 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 782 } 783 784 static std::string DetectLibcxxIncludePath(StringRef base) { 785 std::error_code EC; 786 int MaxVersion = 0; 787 std::string MaxVersionString = ""; 788 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE; 789 LI = LI.increment(EC)) { 790 StringRef VersionText = llvm::sys::path::filename(LI->path()); 791 int Version; 792 if (VersionText[0] == 'v' && 793 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 794 if (Version > MaxVersion) { 795 MaxVersion = Version; 796 MaxVersionString = VersionText; 797 } 798 } 799 } 800 return MaxVersion ? (base + "/" + MaxVersionString).str() : ""; 801 } 802 803 void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 804 llvm::opt::ArgStringList &CC1Args) const { 805 const std::string& SysRoot = computeSysRoot(); 806 const std::string LibCXXIncludePathCandidates[] = { 807 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"), 808 // If this is a development, non-installed, clang, libcxx will 809 // not be found at ../include/c++ but it likely to be found at 810 // one of the following two locations: 811 DetectLibcxxIncludePath(SysRoot + "/usr/local/include/c++"), 812 DetectLibcxxIncludePath(SysRoot + "/usr/include/c++") }; 813 for (const auto &IncludePath : LibCXXIncludePathCandidates) { 814 if (IncludePath.empty() || !getVFS().exists(IncludePath)) 815 continue; 816 // Use the first candidate that exists. 817 addSystemInclude(DriverArgs, CC1Args, IncludePath); 818 return; 819 } 820 } 821 822 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 823 llvm::opt::ArgStringList &CC1Args) const { 824 // We need a detected GCC installation on Linux to provide libstdc++'s 825 // headers. 826 if (!GCCInstallation.isValid()) 827 return; 828 829 // By default, look for the C++ headers in an include directory adjacent to 830 // the lib directory of the GCC installation. Note that this is expect to be 831 // equivalent to '/usr/include/c++/X.Y' in almost all cases. 832 StringRef LibDir = GCCInstallation.getParentLibPath(); 833 StringRef InstallDir = GCCInstallation.getInstallPath(); 834 StringRef TripleStr = GCCInstallation.getTriple().str(); 835 const Multilib &Multilib = GCCInstallation.getMultilib(); 836 const std::string GCCMultiarchTriple = getMultiarchTriple( 837 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot); 838 const std::string TargetMultiarchTriple = 839 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 840 const GCCVersion &Version = GCCInstallation.getVersion(); 841 842 // The primary search for libstdc++ supports multiarch variants. 843 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", 844 "/c++/" + Version.Text, TripleStr, 845 GCCMultiarchTriple, TargetMultiarchTriple, 846 Multilib.includeSuffix(), DriverArgs, CC1Args)) 847 return; 848 849 // Otherwise, fall back on a bunch of options which don't use multiarch 850 // layouts for simplicity. 851 const std::string LibStdCXXIncludePathCandidates[] = { 852 // Gentoo is weird and places its headers inside the GCC install, 853 // so if the first attempt to find the headers fails, try these patterns. 854 InstallDir.str() + "/include/g++-v" + Version.Text, 855 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." + 856 Version.MinorStr, 857 InstallDir.str() + "/include/g++-v" + Version.MajorStr, 858 // Android standalone toolchain has C++ headers in yet another place. 859 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 860 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 861 // without a subdirectory corresponding to the gcc version. 862 LibDir.str() + "/../include/c++", 863 }; 864 865 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 866 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 867 /*GCCMultiarchTriple*/ "", 868 /*TargetMultiarchTriple*/ "", 869 Multilib.includeSuffix(), DriverArgs, CC1Args)) 870 break; 871 } 872 } 873 874 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 875 ArgStringList &CC1Args) const { 876 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 877 } 878 879 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 880 ArgStringList &CC1Args) const { 881 if (GCCInstallation.isValid()) { 882 CC1Args.push_back("-isystem"); 883 CC1Args.push_back(DriverArgs.MakeArgString( 884 GCCInstallation.getParentLibPath() + "/../" + 885 GCCInstallation.getTriple().str() + "/include")); 886 } 887 } 888 889 bool Linux::isPIEDefault() const { 890 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || 891 getTriple().isMusl() || getSanitizerArgs().requiresPIE(); 892 } 893 894 SanitizerMask Linux::getSupportedSanitizers() const { 895 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 896 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 897 const bool IsMIPS = getTriple().getArch() == llvm::Triple::mips || 898 getTriple().getArch() == llvm::Triple::mipsel; 899 const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 || 900 getTriple().getArch() == llvm::Triple::mips64el; 901 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 902 getTriple().getArch() == llvm::Triple::ppc64le; 903 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 904 getTriple().getArch() == llvm::Triple::aarch64_be; 905 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 906 getTriple().getArch() == llvm::Triple::thumb || 907 getTriple().getArch() == llvm::Triple::armeb || 908 getTriple().getArch() == llvm::Triple::thumbeb; 909 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 910 Res |= SanitizerKind::Address; 911 Res |= SanitizerKind::Fuzzer; 912 Res |= SanitizerKind::FuzzerNoLink; 913 Res |= SanitizerKind::KernelAddress; 914 Res |= SanitizerKind::Memory; 915 Res |= SanitizerKind::Vptr; 916 Res |= SanitizerKind::SafeStack; 917 if (IsX86_64 || IsMIPS64 || IsAArch64) 918 Res |= SanitizerKind::DataFlow; 919 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64) 920 Res |= SanitizerKind::Leak; 921 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 922 Res |= SanitizerKind::Thread; 923 if (IsX86_64 || IsMIPS64) 924 Res |= SanitizerKind::Efficiency; 925 if (IsX86 || IsX86_64) 926 Res |= SanitizerKind::Function; 927 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch) 928 Res |= SanitizerKind::Scudo; 929 if (IsX86_64 || IsAArch64) { 930 Res |= SanitizerKind::HWAddress; 931 Res |= SanitizerKind::KernelHWAddress; 932 } 933 return Res; 934 } 935 936 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 937 llvm::opt::ArgStringList &CmdArgs) const { 938 if (!needsProfileRT(Args)) return; 939 940 // Add linker option -u__llvm_runtime_variable to cause runtime 941 // initialization module to be linked in. 942 if (!Args.hasArg(options::OPT_coverage)) 943 CmdArgs.push_back(Args.MakeArgString( 944 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 945 ToolChain::addProfileRTLibs(Args, CmdArgs); 946 } 947