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