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 (Triple.isMIPS()) { 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 = Triple.isMIPS(); 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() || !getTriple().isMIPS()) 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 IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 534 535 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 536 537 if (tools::mips::isUCLibc(Args)) 538 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 539 else if (!Triple.hasEnvironment() && 540 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 541 Loader = 542 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 543 else 544 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 545 546 break; 547 } 548 case llvm::Triple::ppc: 549 LibDir = "lib"; 550 Loader = "ld.so.1"; 551 break; 552 case llvm::Triple::ppc64: 553 LibDir = "lib64"; 554 Loader = 555 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 556 break; 557 case llvm::Triple::ppc64le: 558 LibDir = "lib64"; 559 Loader = 560 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 561 break; 562 case llvm::Triple::riscv32: { 563 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 564 LibDir = "lib"; 565 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str(); 566 break; 567 } 568 case llvm::Triple::riscv64: { 569 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); 570 LibDir = "lib"; 571 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str(); 572 break; 573 } 574 case llvm::Triple::sparc: 575 case llvm::Triple::sparcel: 576 LibDir = "lib"; 577 Loader = "ld-linux.so.2"; 578 break; 579 case llvm::Triple::sparcv9: 580 LibDir = "lib64"; 581 Loader = "ld-linux.so.2"; 582 break; 583 case llvm::Triple::systemz: 584 LibDir = "lib"; 585 Loader = "ld64.so.1"; 586 break; 587 case llvm::Triple::x86: 588 LibDir = "lib"; 589 Loader = "ld-linux.so.2"; 590 break; 591 case llvm::Triple::x86_64: { 592 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 593 594 LibDir = X32 ? "libx32" : "lib64"; 595 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 596 break; 597 } 598 } 599 600 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor || 601 Triple.getVendor() == llvm::Triple::PC)) 602 return "/usr/" + Triple.str() + "/lib/" + Loader; 603 return "/" + LibDir + "/" + Loader; 604 } 605 606 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 607 ArgStringList &CC1Args) const { 608 const Driver &D = getDriver(); 609 std::string SysRoot = computeSysRoot(); 610 611 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 612 return; 613 614 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 615 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 616 617 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 618 SmallString<128> P(D.ResourceDir); 619 llvm::sys::path::append(P, "include"); 620 addSystemInclude(DriverArgs, CC1Args, P); 621 } 622 623 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 624 return; 625 626 // Check for configure-time C include directories. 627 StringRef CIncludeDirs(C_INCLUDE_DIRS); 628 if (CIncludeDirs != "") { 629 SmallVector<StringRef, 5> dirs; 630 CIncludeDirs.split(dirs, ":"); 631 for (StringRef dir : dirs) { 632 StringRef Prefix = 633 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; 634 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 635 } 636 return; 637 } 638 639 // Lacking those, try to detect the correct set of system includes for the 640 // target triple. 641 642 // Add include directories specific to the selected multilib set and multilib. 643 if (GCCInstallation.isValid()) { 644 const auto &Callback = Multilibs.includeDirsCallback(); 645 if (Callback) { 646 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 647 addExternCSystemIncludeIfExists( 648 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 649 } 650 } 651 652 // Implement generic Debian multiarch support. 653 const StringRef X86_64MultiarchIncludeDirs[] = { 654 "/usr/include/x86_64-linux-gnu", 655 656 // FIXME: These are older forms of multiarch. It's not clear that they're 657 // in use in any released version of Debian, so we should consider 658 // removing them. 659 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 660 const StringRef X86MultiarchIncludeDirs[] = { 661 "/usr/include/i386-linux-gnu", 662 663 // FIXME: These are older forms of multiarch. It's not clear that they're 664 // in use in any released version of Debian, so we should consider 665 // removing them. 666 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 667 "/usr/include/i486-linux-gnu"}; 668 const StringRef AArch64MultiarchIncludeDirs[] = { 669 "/usr/include/aarch64-linux-gnu"}; 670 const StringRef ARMMultiarchIncludeDirs[] = { 671 "/usr/include/arm-linux-gnueabi"}; 672 const StringRef ARMHFMultiarchIncludeDirs[] = { 673 "/usr/include/arm-linux-gnueabihf"}; 674 const StringRef ARMEBMultiarchIncludeDirs[] = { 675 "/usr/include/armeb-linux-gnueabi"}; 676 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 677 "/usr/include/armeb-linux-gnueabihf"}; 678 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 679 const StringRef MIPSELMultiarchIncludeDirs[] = { 680 "/usr/include/mipsel-linux-gnu"}; 681 const StringRef MIPS64MultiarchIncludeDirs[] = { 682 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"}; 683 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 684 "/usr/include/mips64el-linux-gnu", 685 "/usr/include/mips64el-linux-gnuabi64"}; 686 const StringRef PPCMultiarchIncludeDirs[] = { 687 "/usr/include/powerpc-linux-gnu"}; 688 const StringRef PPC64MultiarchIncludeDirs[] = { 689 "/usr/include/powerpc64-linux-gnu"}; 690 const StringRef PPC64LEMultiarchIncludeDirs[] = { 691 "/usr/include/powerpc64le-linux-gnu"}; 692 const StringRef SparcMultiarchIncludeDirs[] = { 693 "/usr/include/sparc-linux-gnu"}; 694 const StringRef Sparc64MultiarchIncludeDirs[] = { 695 "/usr/include/sparc64-linux-gnu"}; 696 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 697 "/usr/include/s390x-linux-gnu"}; 698 ArrayRef<StringRef> MultiarchIncludeDirs; 699 switch (getTriple().getArch()) { 700 case llvm::Triple::x86_64: 701 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 702 break; 703 case llvm::Triple::x86: 704 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 705 break; 706 case llvm::Triple::aarch64: 707 case llvm::Triple::aarch64_be: 708 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 709 break; 710 case llvm::Triple::arm: 711 case llvm::Triple::thumb: 712 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 713 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 714 else 715 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 716 break; 717 case llvm::Triple::armeb: 718 case llvm::Triple::thumbeb: 719 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 720 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 721 else 722 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 723 break; 724 case llvm::Triple::mips: 725 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 726 break; 727 case llvm::Triple::mipsel: 728 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 729 break; 730 case llvm::Triple::mips64: 731 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 732 break; 733 case llvm::Triple::mips64el: 734 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 735 break; 736 case llvm::Triple::ppc: 737 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 738 break; 739 case llvm::Triple::ppc64: 740 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 741 break; 742 case llvm::Triple::ppc64le: 743 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 744 break; 745 case llvm::Triple::sparc: 746 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 747 break; 748 case llvm::Triple::sparcv9: 749 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 750 break; 751 case llvm::Triple::systemz: 752 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 753 break; 754 default: 755 break; 756 } 757 758 const std::string AndroidMultiarchIncludeDir = 759 std::string("/usr/include/") + 760 getMultiarchTriple(D, getTriple(), SysRoot); 761 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir}; 762 if (getTriple().isAndroid()) 763 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs; 764 765 for (StringRef Dir : MultiarchIncludeDirs) { 766 if (D.getVFS().exists(SysRoot + Dir)) { 767 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 768 break; 769 } 770 } 771 772 if (getTriple().getOS() == llvm::Triple::RTEMS) 773 return; 774 775 // Add an include of '/include' directly. This isn't provided by default by 776 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 777 // add even when Clang is acting as-if it were a system compiler. 778 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 779 780 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 781 } 782 783 static std::string DetectLibcxxIncludePath(StringRef base) { 784 std::error_code EC; 785 int MaxVersion = 0; 786 std::string MaxVersionString = ""; 787 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE; 788 LI = LI.increment(EC)) { 789 StringRef VersionText = llvm::sys::path::filename(LI->path()); 790 int Version; 791 if (VersionText[0] == 'v' && 792 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 793 if (Version > MaxVersion) { 794 MaxVersion = Version; 795 MaxVersionString = VersionText; 796 } 797 } 798 } 799 return MaxVersion ? (base + "/" + MaxVersionString).str() : ""; 800 } 801 802 void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 803 llvm::opt::ArgStringList &CC1Args) const { 804 const std::string& SysRoot = computeSysRoot(); 805 const std::string LibCXXIncludePathCandidates[] = { 806 DetectLibcxxIncludePath(getDriver().ResourceDir + "/include/c++"), 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().isMIPS32(); 898 const bool IsMIPS64 = getTriple().isMIPS64(); 899 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 900 getTriple().getArch() == llvm::Triple::ppc64le; 901 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 902 getTriple().getArch() == llvm::Triple::aarch64_be; 903 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 904 getTriple().getArch() == llvm::Triple::thumb || 905 getTriple().getArch() == llvm::Triple::armeb || 906 getTriple().getArch() == llvm::Triple::thumbeb; 907 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 908 Res |= SanitizerKind::Address; 909 Res |= SanitizerKind::Fuzzer; 910 Res |= SanitizerKind::FuzzerNoLink; 911 Res |= SanitizerKind::KernelAddress; 912 Res |= SanitizerKind::Memory; 913 Res |= SanitizerKind::Vptr; 914 Res |= SanitizerKind::SafeStack; 915 if (IsX86_64 || IsMIPS64 || IsAArch64) 916 Res |= SanitizerKind::DataFlow; 917 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64) 918 Res |= SanitizerKind::Leak; 919 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 920 Res |= SanitizerKind::Thread; 921 if (IsX86_64 || IsMIPS64) 922 Res |= SanitizerKind::Efficiency; 923 if (IsX86 || IsX86_64) 924 Res |= SanitizerKind::Function; 925 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch) 926 Res |= SanitizerKind::Scudo; 927 if (IsX86_64 || IsAArch64) { 928 Res |= SanitizerKind::HWAddress; 929 Res |= SanitizerKind::KernelHWAddress; 930 } 931 return Res; 932 } 933 934 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 935 llvm::opt::ArgStringList &CmdArgs) const { 936 if (!needsProfileRT(Args)) return; 937 938 // Add linker option -u__llvm_runtime_variable to cause runtime 939 // initialization module to be linked in. 940 if (!Args.hasArg(options::OPT_coverage)) 941 CmdArgs.push_back(Args.MakeArgString( 942 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 943 ToolChain::addProfileRTLibs(Args, CmdArgs); 944 } 945