1 //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "Arch/Mips.h"
13 #include "Arch/RISCV.h"
14 #include "CommonArgs.h"
15 #include "clang/Config/config.h"
16 #include "clang/Driver/Distro.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/Options.h"
19 #include "clang/Driver/SanitizerArgs.h"
20 #include "llvm/Option/ArgList.h"
21 #include "llvm/ProfileData/InstrProf.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/ScopedPrinter.h"
24 #include "llvm/Support/VirtualFileSystem.h"
25 #include <system_error>
27 using namespace clang::driver;
28 using namespace clang::driver::toolchains;
29 using namespace clang;
30 using namespace llvm::opt;
32 using tools::addPathIfExists;
34 /// Get our best guess at the multiarch triple for a target.
36 /// Debian-based systems are starting to use a multiarch setup where they use
37 /// a target-triple directory in the library and header search paths.
38 /// Unfortunately, this triple does not align with the vanilla target triple,
39 /// so we provide a rough mapping here.
40 static std::string getMultiarchTriple(const Driver &D,
41 const llvm::Triple &TargetTriple,
43 llvm::Triple::EnvironmentType TargetEnvironment =
44 TargetTriple.getEnvironment();
45 bool IsAndroid = TargetTriple.isAndroid();
46 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
47 bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
49 // For most architectures, just use whatever we have rather than trying to be
51 switch (TargetTriple.getArch()) {
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:
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";
67 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi"))
68 return "arm-linux-gnueabi";
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";
77 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi"))
78 return "armeb-linux-gnueabi";
81 case llvm::Triple::x86:
83 return "i686-linux-android";
84 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu"))
85 return "i386-linux-gnu";
87 case llvm::Triple::x86_64:
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";
95 case llvm::Triple::aarch64:
97 return "aarch64-linux-android";
98 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu"))
99 return "aarch64-linux-gnu";
101 case llvm::Triple::aarch64_be:
102 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
103 return "aarch64_be-linux-gnu";
105 case llvm::Triple::mips: {
106 std::string MT = IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu";
107 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
111 case llvm::Triple::mipsel: {
113 return "mipsel-linux-android";
114 std::string MT = IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu";
115 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
119 case llvm::Triple::mips64: {
120 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") +
121 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
122 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
124 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu"))
125 return "mips64-linux-gnu";
128 case llvm::Triple::mips64el: {
130 return "mips64el-linux-android";
131 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") +
132 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
133 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
135 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu"))
136 return "mips64el-linux-gnu";
139 case llvm::Triple::ppc:
140 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
141 return "powerpc-linux-gnuspe";
142 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu"))
143 return "powerpc-linux-gnu";
145 case llvm::Triple::ppc64:
146 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu"))
147 return "powerpc64-linux-gnu";
149 case llvm::Triple::ppc64le:
150 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
151 return "powerpc64le-linux-gnu";
153 case llvm::Triple::sparc:
154 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu"))
155 return "sparc-linux-gnu";
157 case llvm::Triple::sparcv9:
158 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu"))
159 return "sparc64-linux-gnu";
161 case llvm::Triple::systemz:
162 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu"))
163 return "s390x-linux-gnu";
166 return TargetTriple.str();
169 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
170 if (Triple.isMIPS()) {
171 if (Triple.isAndroid()) {
174 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
175 if (CPUName == "mips32r6")
177 if (CPUName == "mips32r2")
180 // lib32 directory has a special meaning on MIPS targets.
181 // It contains N32 ABI binaries. Use this folder if produce
182 // code for N32 ABI only.
183 if (tools::mips::hasMipsAbiArg(Args, "n32"))
185 return Triple.isArch32Bit() ? "lib" : "lib64";
188 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
189 // using that variant while targeting other architectures causes problems
190 // because the libraries are laid out in shared system roots that can't cope
191 // with a 'lib32' library search path being considered. So we only enable
192 // them when we know we may need it.
194 // FIXME: This is a bit of a hack. We should really unify this code for
195 // reasoning about oslibdir spellings with the lib dir spellings in the
196 // GCCInstallationDetector, but that is a more significant refactoring.
197 if (Triple.getArch() == llvm::Triple::x86 ||
198 Triple.getArch() == llvm::Triple::ppc)
201 if (Triple.getArch() == llvm::Triple::x86_64 &&
202 Triple.getEnvironment() == llvm::Triple::GNUX32)
205 if (Triple.getArch() == llvm::Triple::riscv32)
208 return Triple.isArch32Bit() ? "lib" : "lib64";
211 static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
212 const Multilib &Multilib,
213 StringRef InstallPath,
214 ToolChain::path_list &Paths) {
215 if (const auto &PathsCallback = Multilibs.filePathsCallback())
216 for (const auto &Path : PathsCallback(Multilib))
217 addPathIfExists(D, InstallPath + Path, Paths);
220 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
221 : Generic_ELF(D, Triple, Args) {
222 GCCInstallation.init(Triple, Args);
223 Multilibs = GCCInstallation.getMultilibs();
224 SelectedMultilib = GCCInstallation.getMultilib();
225 llvm::Triple::ArchType Arch = Triple.getArch();
226 std::string SysRoot = computeSysRoot();
228 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
229 // least) put various tools in a triple-prefixed directory off of the parent
230 // of the GCC installation. We use the GCC triple here to ensure that we end
231 // up with tools that support the same amount of cross compiling as the
232 // detected GCC installation. For example, if we find a GCC installation
233 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
234 // used to target i386.
235 // FIXME: This seems unlikely to be Linux-specific.
236 ToolChain::path_list &PPaths = getProgramPaths();
237 if (GCCInstallation.isValid()) {
238 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
239 GCCInstallation.getTriple().str() + "/bin")
243 Distro Distro(D.getVFS());
245 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
246 ExtraOpts.push_back("-z");
247 ExtraOpts.push_back("now");
250 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
251 Triple.isAndroid()) {
252 ExtraOpts.push_back("-z");
253 ExtraOpts.push_back("relro");
256 // The lld default page size is too large for Aarch64, which produces much
257 // larger .so files and images for arm64 device targets. Use 4KB page size
258 // for Android arm64 targets instead.
259 if (Triple.isAArch64() && Triple.isAndroid()) {
260 ExtraOpts.push_back("-z");
261 ExtraOpts.push_back("max-page-size=4096");
264 if (GCCInstallation.getParentLibPath().find("opt/rh/devtoolset") !=
266 // With devtoolset on RHEL, we want to add a bin directory that is relative
267 // to the detected gcc install, because if we are using devtoolset gcc then
268 // we want to use other tools from devtoolset (e.g. ld) instead of the
269 // standard system tools.
270 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
273 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
274 ExtraOpts.push_back("-X");
276 const bool IsAndroid = Triple.isAndroid();
277 const bool IsMips = Triple.isMIPS();
278 const bool IsHexagon = Arch == llvm::Triple::hexagon;
279 const bool IsRISCV = Triple.isRISCV();
281 if (IsMips && !SysRoot.empty())
282 ExtraOpts.push_back("--sysroot=" + SysRoot);
284 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
285 // and the MIPS ABI require .dynsym to be sorted in different ways.
286 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
287 // ABI requires a mapping between the GOT and the symbol table.
288 // Android loader does not support .gnu.hash until API 23.
289 // Hexagon linker/loader does not support .gnu.hash
290 if (!IsMips && !IsHexagon) {
291 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
292 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) ||
293 (IsAndroid && !Triple.isAndroidVersionLT(23)))
294 ExtraOpts.push_back("--hash-style=gnu");
296 if (Distro.IsDebian() || Distro.IsOpenSUSE() ||
297 Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty ||
298 Distro == Distro::UbuntuKarmic ||
299 (IsAndroid && Triple.isAndroidVersionLT(23)))
300 ExtraOpts.push_back("--hash-style=both");
303 #ifdef ENABLE_LINKER_BUILD_ID
304 ExtraOpts.push_back("--build-id");
307 if (IsAndroid || Distro.IsOpenSUSE())
308 ExtraOpts.push_back("--enable-new-dtags");
310 // The selection of paths to try here is designed to match the patterns which
311 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
312 // This was determined by running GCC in a fake filesystem, creating all
313 // possible permutations of these directories, and seeing which ones it added
314 // to the link paths.
315 path_list &Paths = getFilePaths();
317 const std::string OSLibDir = getOSLibDir(Triple, Args);
318 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
320 // Add the multilib suffixed paths where they are available.
321 if (GCCInstallation.isValid()) {
322 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
323 const std::string &LibPath = GCCInstallation.getParentLibPath();
325 // Add toolchain / multilib specific file paths.
326 addMultilibsFilePaths(D, Multilibs, SelectedMultilib,
327 GCCInstallation.getInstallPath(), Paths);
329 // Sourcery CodeBench MIPS toolchain holds some libraries under
330 // a biarch-like suffix of the GCC installation.
332 D, GCCInstallation.getInstallPath() + SelectedMultilib.gccSuffix(),
335 // GCC cross compiling toolchains will install target libraries which ship
336 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
337 // any part of the GCC installation in
338 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
339 // debatable, but is the reality today. We need to search this tree even
340 // when we have a sysroot somewhere else. It is the responsibility of
341 // whomever is doing the cross build targeting a sysroot using a GCC
342 // installation that is *not* within the system root to ensure two things:
344 // 1) Any DSOs that are linked in from this tree or from the install path
345 // above must be present on the system root and found via an
346 // appropriate rpath.
347 // 2) There must not be libraries installed into
348 // <prefix>/<triple>/<libdir> unless they should be preferred over
349 // those within the system root.
351 // Note that this matches the GCC behavior. See the below comment for where
352 // Clang diverges from GCC's behavior.
353 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" +
354 OSLibDir + SelectedMultilib.osSuffix(),
357 // If the GCC installation we found is inside of the sysroot, we want to
358 // prefer libraries installed in the parent prefix of the GCC installation.
359 // It is important to *not* use these paths when the GCC installation is
360 // outside of the system root as that can pick up unintended libraries.
361 // This usually happens when there is an external cross compiler on the
362 // host system, and a more minimal sysroot available that is the target of
363 // the cross. Note that GCC does include some of these directories in some
364 // configurations but this seems somewhere between questionable and simply
366 if (StringRef(LibPath).startswith(SysRoot)) {
367 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths);
368 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
372 // Similar to the logic for GCC above, if we currently running Clang inside
373 // of the requested system root, add its parent library paths to
375 // FIXME: It's not clear whether we should use the driver's installed
376 // directory ('Dir' below) or the ResourceDir.
377 if (StringRef(D.Dir).startswith(SysRoot)) {
378 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
379 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
382 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
383 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
386 // Android sysroots contain a library directory for each supported OS
387 // version as well as some unversioned libraries in the usual multiarch
392 Triple.getEnvironmentVersion(Major, Minor, Micro);
394 SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
395 llvm::to_string(Major),
399 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
400 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
401 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
403 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
404 Triple.isArch64Bit())
405 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths);
407 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
409 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
410 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
411 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
414 // Try walking via the GCC triple path in case of biarch or multiarch GCC
415 // installations with strange symlinks.
416 if (GCCInstallation.isValid()) {
418 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
419 "/../../" + OSLibDir,
422 // Add the 'other' biarch variant path
423 Multilib BiarchSibling;
424 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
425 addPathIfExists(D, GCCInstallation.getInstallPath() +
426 BiarchSibling.gccSuffix(),
430 // See comments above on the multilib variant for details of why this is
431 // included even from outside the sysroot.
432 const std::string &LibPath = GCCInstallation.getParentLibPath();
433 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
434 const Multilib &Multilib = GCCInstallation.getMultilib();
435 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" +
439 // See comments above on the multilib variant for details of why this is
440 // only included from within the sysroot.
441 if (StringRef(LibPath).startswith(SysRoot))
442 addPathIfExists(D, LibPath, Paths);
445 // Similar to the logic for GCC above, if we are currently running Clang
446 // inside of the requested system root, add its parent library path to those
448 // FIXME: It's not clear whether we should use the driver's installed
449 // directory ('Dir' below) or the ResourceDir.
450 if (StringRef(D.Dir).startswith(SysRoot))
451 addPathIfExists(D, D.Dir + "/../lib", Paths);
453 addPathIfExists(D, SysRoot + "/lib", Paths);
454 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
457 ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
458 if (getTriple().isAndroid())
459 return ToolChain::CST_Libcxx;
460 return ToolChain::CST_Libstdcxx;
463 bool Linux::HasNativeLLVMSupport() const { return true; }
465 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
467 Tool *Linux::buildAssembler() const {
468 return new tools::gnutools::Assembler(*this);
471 std::string Linux::computeSysRoot() const {
472 if (!getDriver().SysRoot.empty())
473 return getDriver().SysRoot;
475 if (getTriple().isAndroid()) {
476 // Android toolchains typically include a sysroot at ../sysroot relative to
478 const StringRef ClangDir = getDriver().getInstalledDir();
479 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
480 if (getVFS().exists(AndroidSysRootPath))
481 return AndroidSysRootPath;
484 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
485 return std::string();
487 // Standalone MIPS toolchains use different names for sysroot folder
488 // and put it into different places. Here we try to check some known
491 const StringRef InstallDir = GCCInstallation.getInstallPath();
492 const StringRef TripleStr = GCCInstallation.getTriple().str();
493 const Multilib &Multilib = GCCInstallation.getMultilib();
496 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
499 if (getVFS().exists(Path))
502 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
504 if (getVFS().exists(Path))
507 return std::string();
510 std::string Linux::getDynamicLinker(const ArgList &Args) const {
511 const llvm::Triple::ArchType Arch = getArch();
512 const llvm::Triple &Triple = getTriple();
514 const Distro Distro(getDriver().getVFS());
516 if (Triple.isAndroid())
517 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
519 if (Triple.isMusl()) {
520 std::string ArchName;
524 case llvm::Triple::arm:
525 case llvm::Triple::thumb:
529 case llvm::Triple::armeb:
530 case llvm::Triple::thumbeb:
535 ArchName = Triple.getArchName().str();
538 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
539 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
542 return "/lib/ld-musl-" + ArchName + ".so.1";
550 llvm_unreachable("unsupported architecture");
552 case llvm::Triple::aarch64:
554 Loader = "ld-linux-aarch64.so.1";
556 case llvm::Triple::aarch64_be:
558 Loader = "ld-linux-aarch64_be.so.1";
560 case llvm::Triple::arm:
561 case llvm::Triple::thumb:
562 case llvm::Triple::armeb:
563 case llvm::Triple::thumbeb: {
565 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
566 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
569 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
572 case llvm::Triple::mips:
573 case llvm::Triple::mipsel:
574 case llvm::Triple::mips64:
575 case llvm::Triple::mips64el: {
576 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
578 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
580 if (tools::mips::isUCLibc(Args))
581 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
582 else if (!Triple.hasEnvironment() &&
583 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
585 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
587 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
591 case llvm::Triple::ppc:
595 case llvm::Triple::ppc64:
598 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
600 case llvm::Triple::ppc64le:
603 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
605 case llvm::Triple::riscv32: {
606 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
608 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
611 case llvm::Triple::riscv64: {
612 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
614 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
617 case llvm::Triple::sparc:
618 case llvm::Triple::sparcel:
620 Loader = "ld-linux.so.2";
622 case llvm::Triple::sparcv9:
624 Loader = "ld-linux.so.2";
626 case llvm::Triple::systemz:
628 Loader = "ld64.so.1";
630 case llvm::Triple::x86:
632 Loader = "ld-linux.so.2";
634 case llvm::Triple::x86_64: {
635 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
637 LibDir = X32 ? "libx32" : "lib64";
638 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
643 if (Distro == Distro::Exherbo &&
644 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
645 Triple.getVendor() == llvm::Triple::PC))
646 return "/usr/" + Triple.str() + "/lib/" + Loader;
647 return "/" + LibDir + "/" + Loader;
650 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
651 ArgStringList &CC1Args) const {
652 const Driver &D = getDriver();
653 std::string SysRoot = computeSysRoot();
655 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
658 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
659 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
661 SmallString<128> ResourceDirInclude(D.ResourceDir);
662 llvm::sys::path::append(ResourceDirInclude, "include");
663 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
664 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
665 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
667 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
670 // Check for configure-time C include directories.
671 StringRef CIncludeDirs(C_INCLUDE_DIRS);
672 if (CIncludeDirs != "") {
673 SmallVector<StringRef, 5> dirs;
674 CIncludeDirs.split(dirs, ":");
675 for (StringRef dir : dirs) {
677 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
678 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
683 // Lacking those, try to detect the correct set of system includes for the
686 // Add include directories specific to the selected multilib set and multilib.
687 if (GCCInstallation.isValid()) {
688 const auto &Callback = Multilibs.includeDirsCallback();
690 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
691 addExternCSystemIncludeIfExists(
692 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
696 // Implement generic Debian multiarch support.
697 const StringRef X86_64MultiarchIncludeDirs[] = {
698 "/usr/include/x86_64-linux-gnu",
700 // FIXME: These are older forms of multiarch. It's not clear that they're
701 // in use in any released version of Debian, so we should consider
703 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
704 const StringRef X86MultiarchIncludeDirs[] = {
705 "/usr/include/i386-linux-gnu",
707 // FIXME: These are older forms of multiarch. It's not clear that they're
708 // in use in any released version of Debian, so we should consider
710 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
711 "/usr/include/i486-linux-gnu"};
712 const StringRef AArch64MultiarchIncludeDirs[] = {
713 "/usr/include/aarch64-linux-gnu"};
714 const StringRef ARMMultiarchIncludeDirs[] = {
715 "/usr/include/arm-linux-gnueabi"};
716 const StringRef ARMHFMultiarchIncludeDirs[] = {
717 "/usr/include/arm-linux-gnueabihf"};
718 const StringRef ARMEBMultiarchIncludeDirs[] = {
719 "/usr/include/armeb-linux-gnueabi"};
720 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
721 "/usr/include/armeb-linux-gnueabihf"};
722 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
723 const StringRef MIPSELMultiarchIncludeDirs[] = {
724 "/usr/include/mipsel-linux-gnu"};
725 const StringRef MIPS64MultiarchIncludeDirs[] = {
726 "/usr/include/mips64-linux-gnuabi64"};
727 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
728 "/usr/include/mips64el-linux-gnuabi64"};
729 const StringRef MIPSN32MultiarchIncludeDirs[] = {
730 "/usr/include/mips64-linux-gnuabin32"};
731 const StringRef MIPSN32ELMultiarchIncludeDirs[] = {
732 "/usr/include/mips64el-linux-gnuabin32"};
733 const StringRef MIPSR6MultiarchIncludeDirs[] = {
734 "/usr/include/mipsisa32-linux-gnu"};
735 const StringRef MIPSR6ELMultiarchIncludeDirs[] = {
736 "/usr/include/mipsisa32r6el-linux-gnu"};
737 const StringRef MIPS64R6MultiarchIncludeDirs[] = {
738 "/usr/include/mipsisa64r6-linux-gnuabi64"};
739 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = {
740 "/usr/include/mipsisa64r6el-linux-gnuabi64"};
741 const StringRef MIPSN32R6MultiarchIncludeDirs[] = {
742 "/usr/include/mipsisa64r6-linux-gnuabin32"};
743 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = {
744 "/usr/include/mipsisa64r6el-linux-gnuabin32"};
745 const StringRef PPCMultiarchIncludeDirs[] = {
746 "/usr/include/powerpc-linux-gnu",
747 "/usr/include/powerpc-linux-gnuspe"};
748 const StringRef PPC64MultiarchIncludeDirs[] = {
749 "/usr/include/powerpc64-linux-gnu"};
750 const StringRef PPC64LEMultiarchIncludeDirs[] = {
751 "/usr/include/powerpc64le-linux-gnu"};
752 const StringRef SparcMultiarchIncludeDirs[] = {
753 "/usr/include/sparc-linux-gnu"};
754 const StringRef Sparc64MultiarchIncludeDirs[] = {
755 "/usr/include/sparc64-linux-gnu"};
756 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
757 "/usr/include/s390x-linux-gnu"};
758 ArrayRef<StringRef> MultiarchIncludeDirs;
759 switch (getTriple().getArch()) {
760 case llvm::Triple::x86_64:
761 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
763 case llvm::Triple::x86:
764 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
766 case llvm::Triple::aarch64:
767 case llvm::Triple::aarch64_be:
768 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
770 case llvm::Triple::arm:
771 case llvm::Triple::thumb:
772 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
773 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
775 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
777 case llvm::Triple::armeb:
778 case llvm::Triple::thumbeb:
779 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
780 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
782 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
784 case llvm::Triple::mips:
785 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
786 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs;
788 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
790 case llvm::Triple::mipsel:
791 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
792 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs;
794 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
796 case llvm::Triple::mips64:
797 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
798 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
799 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs;
801 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs;
802 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
803 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs;
805 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
807 case llvm::Triple::mips64el:
808 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
809 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
810 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs;
812 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs;
813 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
814 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs;
816 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
818 case llvm::Triple::ppc:
819 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
821 case llvm::Triple::ppc64:
822 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
824 case llvm::Triple::ppc64le:
825 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
827 case llvm::Triple::sparc:
828 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
830 case llvm::Triple::sparcv9:
831 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
833 case llvm::Triple::systemz:
834 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
840 const std::string AndroidMultiarchIncludeDir =
841 std::string("/usr/include/") +
842 getMultiarchTriple(D, getTriple(), SysRoot);
843 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir};
844 if (getTriple().isAndroid())
845 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs;
847 for (StringRef Dir : MultiarchIncludeDirs) {
848 if (D.getVFS().exists(SysRoot + Dir)) {
849 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
854 if (getTriple().getOS() == llvm::Triple::RTEMS)
857 // Add an include of '/include' directly. This isn't provided by default by
858 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
859 // add even when Clang is acting as-if it were a system compiler.
860 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
862 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
864 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
865 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
868 static std::string DetectLibcxxIncludePath(llvm::vfs::FileSystem &vfs,
872 std::string MaxVersionString = "";
873 for (llvm::vfs::directory_iterator LI = vfs.dir_begin(base, EC), LE;
874 !EC && LI != LE; LI = LI.increment(EC)) {
875 StringRef VersionText = llvm::sys::path::filename(LI->path());
877 if (VersionText[0] == 'v' &&
878 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
879 if (Version > MaxVersion) {
880 MaxVersion = Version;
881 MaxVersionString = VersionText;
885 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
888 void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
889 llvm::opt::ArgStringList &CC1Args) const {
890 const std::string& SysRoot = computeSysRoot();
891 const std::string LibCXXIncludePathCandidates[] = {
892 DetectLibcxxIncludePath(getVFS(), getDriver().Dir + "/../include/c++"),
893 // If this is a development, non-installed, clang, libcxx will
894 // not be found at ../include/c++ but it likely to be found at
895 // one of the following two locations:
896 DetectLibcxxIncludePath(getVFS(), SysRoot + "/usr/local/include/c++"),
897 DetectLibcxxIncludePath(getVFS(), SysRoot + "/usr/include/c++") };
898 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
899 if (IncludePath.empty() || !getVFS().exists(IncludePath))
901 // Use the first candidate that exists.
902 addSystemInclude(DriverArgs, CC1Args, IncludePath);
907 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
908 llvm::opt::ArgStringList &CC1Args) const {
909 // We need a detected GCC installation on Linux to provide libstdc++'s
911 if (!GCCInstallation.isValid())
914 // By default, look for the C++ headers in an include directory adjacent to
915 // the lib directory of the GCC installation. Note that this is expect to be
916 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
917 StringRef LibDir = GCCInstallation.getParentLibPath();
918 StringRef InstallDir = GCCInstallation.getInstallPath();
919 StringRef TripleStr = GCCInstallation.getTriple().str();
920 const Multilib &Multilib = GCCInstallation.getMultilib();
921 const std::string GCCMultiarchTriple = getMultiarchTriple(
922 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot);
923 const std::string TargetMultiarchTriple =
924 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
925 const GCCVersion &Version = GCCInstallation.getVersion();
927 // The primary search for libstdc++ supports multiarch variants.
928 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
929 "/c++/" + Version.Text, TripleStr,
930 GCCMultiarchTriple, TargetMultiarchTriple,
931 Multilib.includeSuffix(), DriverArgs, CC1Args))
934 // Otherwise, fall back on a bunch of options which don't use multiarch
935 // layouts for simplicity.
936 const std::string LibStdCXXIncludePathCandidates[] = {
937 // Gentoo is weird and places its headers inside the GCC install,
938 // so if the first attempt to find the headers fails, try these patterns.
939 InstallDir.str() + "/include/g++-v" + Version.Text,
940 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
942 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
943 // Android standalone toolchain has C++ headers in yet another place.
944 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
945 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
946 // without a subdirectory corresponding to the gcc version.
947 LibDir.str() + "/../include/c++",
948 // Cray's gcc installation puts headers under "g++" without a
950 LibDir.str() + "/../include/g++",
953 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
954 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
955 /*GCCMultiarchTriple*/ "",
956 /*TargetMultiarchTriple*/ "",
957 Multilib.includeSuffix(), DriverArgs, CC1Args))
962 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
963 ArgStringList &CC1Args) const {
964 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
967 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
968 ArgStringList &CC1Args) const {
969 if (GCCInstallation.isValid()) {
970 CC1Args.push_back("-isystem");
971 CC1Args.push_back(DriverArgs.MakeArgString(
972 GCCInstallation.getParentLibPath() + "/../" +
973 GCCInstallation.getTriple().str() + "/include"));
977 bool Linux::isPIEDefault() const {
978 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
979 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
982 bool Linux::isNoExecStackDefault() const {
983 return getTriple().isAndroid();
986 bool Linux::IsMathErrnoDefault() const {
987 if (getTriple().isAndroid())
989 return Generic_ELF::IsMathErrnoDefault();
992 SanitizerMask Linux::getSupportedSanitizers() const {
993 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
994 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
995 const bool IsMIPS = getTriple().isMIPS32();
996 const bool IsMIPS64 = getTriple().isMIPS64();
997 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
998 getTriple().getArch() == llvm::Triple::ppc64le;
999 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
1000 getTriple().getArch() == llvm::Triple::aarch64_be;
1001 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
1002 getTriple().getArch() == llvm::Triple::thumb ||
1003 getTriple().getArch() == llvm::Triple::armeb ||
1004 getTriple().getArch() == llvm::Triple::thumbeb;
1005 SanitizerMask Res = ToolChain::getSupportedSanitizers();
1006 Res |= SanitizerKind::Address;
1007 Res |= SanitizerKind::PointerCompare;
1008 Res |= SanitizerKind::PointerSubtract;
1009 Res |= SanitizerKind::Fuzzer;
1010 Res |= SanitizerKind::FuzzerNoLink;
1011 Res |= SanitizerKind::KernelAddress;
1012 Res |= SanitizerKind::Memory;
1013 Res |= SanitizerKind::Vptr;
1014 Res |= SanitizerKind::SafeStack;
1015 if (IsX86_64 || IsMIPS64 || IsAArch64)
1016 Res |= SanitizerKind::DataFlow;
1017 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64)
1018 Res |= SanitizerKind::Leak;
1019 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
1020 Res |= SanitizerKind::Thread;
1022 Res |= SanitizerKind::KernelMemory;
1023 if (IsX86 || IsX86_64)
1024 Res |= SanitizerKind::Function;
1025 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
1027 Res |= SanitizerKind::Scudo;
1028 if (IsX86_64 || IsAArch64) {
1029 Res |= SanitizerKind::HWAddress;
1030 Res |= SanitizerKind::KernelHWAddress;
1035 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
1036 llvm::opt::ArgStringList &CmdArgs) const {
1037 if (!needsProfileRT(Args)) return;
1039 // Add linker option -u__llvm_runtime_variable to cause runtime
1040 // initialization module to be linked in.
1041 if ((!Args.hasArg(options::OPT_coverage)) &&
1042 (!Args.hasArg(options::OPT_ftest_coverage)))
1043 CmdArgs.push_back(Args.MakeArgString(
1044 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
1045 ToolChain::addProfileRTLibs(Args, CmdArgs);