1 //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "Arch/Mips.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>
26 using namespace clang::driver;
27 using namespace clang::driver::toolchains;
28 using namespace clang;
29 using namespace llvm::opt;
31 using tools::addPathIfExists;
33 /// \brief Get our best guess at the multiarch triple for a target.
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,
42 llvm::Triple::EnvironmentType TargetEnvironment =
43 TargetTriple.getEnvironment();
45 // For most architectures, just use whatever we have rather than trying to be
47 switch (TargetTriple.getArch()) {
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";
61 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi"))
62 return "arm-linux-gnueabi";
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";
71 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi"))
72 return "armeb-linux-gnueabi";
75 case llvm::Triple::x86:
76 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu"))
77 return "i386-linux-gnu";
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";
85 case llvm::Triple::aarch64:
86 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu"))
87 return "aarch64-linux-gnu";
89 case llvm::Triple::aarch64_be:
90 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
91 return "aarch64_be-linux-gnu";
93 case llvm::Triple::mips:
94 if (D.getVFS().exists(SysRoot + "/lib/mips-linux-gnu"))
95 return "mips-linux-gnu";
97 case llvm::Triple::mipsel:
98 if (D.getVFS().exists(SysRoot + "/lib/mipsel-linux-gnu"))
99 return "mipsel-linux-gnu";
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";
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";
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";
119 case llvm::Triple::ppc64:
120 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu"))
121 return "powerpc64-linux-gnu";
123 case llvm::Triple::ppc64le:
124 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
125 return "powerpc64le-linux-gnu";
127 case llvm::Triple::sparc:
128 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu"))
129 return "sparc-linux-gnu";
131 case llvm::Triple::sparcv9:
132 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu"))
133 return "sparc64-linux-gnu";
135 case llvm::Triple::systemz:
136 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu"))
137 return "s390x-linux-gnu";
140 return TargetTriple.str();
143 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
144 if (tools::isMipsArch(Triple.getArch())) {
145 if (Triple.isAndroid()) {
148 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
149 if (CPUName == "mips32r6")
151 if (CPUName == "mips32r2")
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"))
159 return Triple.isArch32Bit() ? "lib" : "lib64";
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.
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)
175 if (Triple.getArch() == llvm::Triple::x86_64 &&
176 Triple.getEnvironment() == llvm::Triple::GNUX32)
179 return Triple.isArch32Bit() ? "lib" : "lib64";
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);
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();
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")
211 Distro Distro(D.getVFS());
213 if (Distro.IsAlpineLinux()) {
214 ExtraOpts.push_back("-z");
215 ExtraOpts.push_back("now");
218 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux()) {
219 ExtraOpts.push_back("-z");
220 ExtraOpts.push_back("relro");
223 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
224 ExtraOpts.push_back("-X");
226 const bool IsAndroid = Triple.isAndroid();
227 const bool IsMips = tools::isMipsArch(Arch);
228 const bool IsHexagon = Arch == llvm::Triple::hexagon;
230 if (IsMips && !SysRoot.empty())
231 ExtraOpts.push_back("--sysroot=" + SysRoot);
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");
244 if (Distro.IsDebian() || Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid ||
245 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic)
246 ExtraOpts.push_back("--hash-style=both");
249 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6)
250 ExtraOpts.push_back("--no-add-needed");
252 #ifdef ENABLE_LINKER_BUILD_ID
253 ExtraOpts.push_back("--build-id");
256 if (IsAndroid || Distro.IsOpenSUSE())
257 ExtraOpts.push_back("--enable-new-dtags");
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();
266 const std::string OSLibDir = getOSLibDir(Triple, Args);
267 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
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();
276 // Add toolchain / multilib specific file paths.
277 addMultilibsFilePaths(D, Multilibs, Multilib,
278 GCCInstallation.getInstallPath(), Paths);
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(),
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:
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.
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(),
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
316 if (StringRef(LibPath).startswith(SysRoot)) {
317 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths);
318 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
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
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);
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);
337 // Try walking via the GCC triple path in case of biarch or multiarch GCC
338 // installations with strange symlinks.
339 if (GCCInstallation.isValid()) {
341 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
342 "/../../" + OSLibDir,
345 // Add the 'other' biarch variant path
346 Multilib BiarchSibling;
347 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
348 addPathIfExists(D, GCCInstallation.getInstallPath() +
349 BiarchSibling.gccSuffix(),
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" +
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);
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
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);
376 addPathIfExists(D, SysRoot + "/lib", Paths);
377 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
380 bool Linux::HasNativeLLVMSupport() const { return true; }
382 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
384 Tool *Linux::buildAssembler() const {
385 return new tools::gnutools::Assembler(*this);
388 std::string Linux::computeSysRoot() const {
389 if (!getDriver().SysRoot.empty())
390 return getDriver().SysRoot;
392 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch()))
393 return std::string();
395 // Standalone MIPS toolchains use different names for sysroot folder
396 // and put it into different places. Here we try to check some known
399 const StringRef InstallDir = GCCInstallation.getInstallPath();
400 const StringRef TripleStr = GCCInstallation.getTriple().str();
401 const Multilib &Multilib = GCCInstallation.getMultilib();
404 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
407 if (getVFS().exists(Path))
410 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
412 if (getVFS().exists(Path))
415 return std::string();
418 std::string Linux::getDynamicLinker(const ArgList &Args) const {
419 const llvm::Triple::ArchType Arch = getArch();
420 const llvm::Triple &Triple = getTriple();
422 const Distro Distro(getDriver().getVFS());
424 if (Triple.isAndroid())
425 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
427 if (Triple.isMusl()) {
428 std::string ArchName;
432 case llvm::Triple::arm:
433 case llvm::Triple::thumb:
437 case llvm::Triple::armeb:
438 case llvm::Triple::thumbeb:
443 ArchName = Triple.getArchName().str();
446 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
447 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
450 return "/lib/ld-musl-" + ArchName + ".so.1";
458 llvm_unreachable("unsupported architecture");
460 case llvm::Triple::aarch64:
462 Loader = "ld-linux-aarch64.so.1";
464 case llvm::Triple::aarch64_be:
466 Loader = "ld-linux-aarch64_be.so.1";
468 case llvm::Triple::arm:
469 case llvm::Triple::thumb:
470 case llvm::Triple::armeb:
471 case llvm::Triple::thumbeb: {
473 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
474 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
477 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
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);
488 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
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";
496 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
500 case llvm::Triple::ppc:
504 case llvm::Triple::ppc64:
507 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
509 case llvm::Triple::ppc64le:
512 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
514 case llvm::Triple::sparc:
515 case llvm::Triple::sparcel:
517 Loader = "ld-linux.so.2";
519 case llvm::Triple::sparcv9:
521 Loader = "ld-linux.so.2";
523 case llvm::Triple::systemz:
525 Loader = "ld64.so.1";
527 case llvm::Triple::x86:
529 Loader = "ld-linux.so.2";
531 case llvm::Triple::x86_64: {
532 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
534 LibDir = X32 ? "libx32" : "lib64";
535 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
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;
546 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
547 ArgStringList &CC1Args) const {
548 const Driver &D = getDriver();
549 std::string SysRoot = computeSysRoot();
551 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
554 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
555 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
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);
563 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
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) {
573 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
574 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
579 // Lacking those, try to detect the correct set of system includes for the
582 // Add include directories specific to the selected multilib set and multilib.
583 if (GCCInstallation.isValid()) {
584 const auto &Callback = Multilibs.includeDirsCallback();
586 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
587 addExternCSystemIncludeIfExists(
588 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
592 // Implement generic Debian multiarch support.
593 const StringRef X86_64MultiarchIncludeDirs[] = {
594 "/usr/include/x86_64-linux-gnu",
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
599 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
600 const StringRef X86MultiarchIncludeDirs[] = {
601 "/usr/include/i386-linux-gnu",
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
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;
643 case llvm::Triple::x86:
644 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
646 case llvm::Triple::aarch64:
647 case llvm::Triple::aarch64_be:
648 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
650 case llvm::Triple::arm:
651 case llvm::Triple::thumb:
652 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
653 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
655 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
657 case llvm::Triple::armeb:
658 case llvm::Triple::thumbeb:
659 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
660 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
662 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
664 case llvm::Triple::mips:
665 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
667 case llvm::Triple::mipsel:
668 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
670 case llvm::Triple::mips64:
671 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
673 case llvm::Triple::mips64el:
674 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
676 case llvm::Triple::ppc:
677 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
679 case llvm::Triple::ppc64:
680 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
682 case llvm::Triple::ppc64le:
683 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
685 case llvm::Triple::sparc:
686 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
688 case llvm::Triple::sparcv9:
689 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
691 case llvm::Triple::systemz:
692 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
697 for (StringRef Dir : MultiarchIncludeDirs) {
698 if (D.getVFS().exists(SysRoot + Dir)) {
699 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
704 if (getTriple().getOS() == llvm::Triple::RTEMS)
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");
712 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
715 static std::string DetectLibcxxIncludePath(StringRef base) {
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());
723 if (VersionText[0] == 'v' &&
724 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
725 if (Version > MaxVersion) {
726 MaxVersion = Version;
727 MaxVersionString = VersionText;
731 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
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))
745 // Use the first candidate that exists.
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
755 if (!GCCInstallation.isValid())
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();
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))
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 + "." +
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++",
794 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
795 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
796 /*GCCMultiarchTriple*/ "",
797 /*TargetMultiarchTriple*/ "",
798 Multilib.includeSuffix(), DriverArgs, CC1Args))
803 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
804 ArgStringList &CC1Args) const {
805 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
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"));
818 bool Linux::isPIEDefault() const {
819 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
820 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
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;
858 Res |= SanitizerKind::HWAddress;
862 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
863 llvm::opt::ArgStringList &CmdArgs) const {
864 if (!needsProfileRT(Args)) return;
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);