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