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