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.IsOpenSUSE() || Distro.IsUbuntu()) {
214 ExtraOpts.push_back("-z");
215 ExtraOpts.push_back("relro");
218 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
219 ExtraOpts.push_back("-X");
221 const bool IsAndroid = Triple.isAndroid();
222 const bool IsMips = tools::isMipsArch(Arch);
223 const bool IsHexagon = Arch == llvm::Triple::hexagon;
225 if (IsMips && !SysRoot.empty())
226 ExtraOpts.push_back("--sysroot=" + SysRoot);
228 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
229 // and the MIPS ABI require .dynsym to be sorted in different ways.
230 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
231 // ABI requires a mapping between the GOT and the symbol table.
232 // Android loader does not support .gnu.hash.
233 // Hexagon linker/loader does not support .gnu.hash
234 if (!IsMips && !IsAndroid && !IsHexagon) {
235 if (Distro.IsRedhat() || Distro.IsOpenSUSE() ||
236 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick))
237 ExtraOpts.push_back("--hash-style=gnu");
239 if (Distro.IsDebian() || Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid ||
240 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic)
241 ExtraOpts.push_back("--hash-style=both");
244 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6)
245 ExtraOpts.push_back("--no-add-needed");
247 #ifdef ENABLE_LINKER_BUILD_ID
248 ExtraOpts.push_back("--build-id");
251 if (Distro.IsOpenSUSE())
252 ExtraOpts.push_back("--enable-new-dtags");
254 // The selection of paths to try here is designed to match the patterns which
255 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
256 // This was determined by running GCC in a fake filesystem, creating all
257 // possible permutations of these directories, and seeing which ones it added
258 // to the link paths.
259 path_list &Paths = getFilePaths();
261 const std::string OSLibDir = getOSLibDir(Triple, Args);
262 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
264 // Add the multilib suffixed paths where they are available.
265 if (GCCInstallation.isValid()) {
266 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
267 const std::string &LibPath = GCCInstallation.getParentLibPath();
268 const Multilib &Multilib = GCCInstallation.getMultilib();
269 const MultilibSet &Multilibs = GCCInstallation.getMultilibs();
271 // Add toolchain / multilib specific file paths.
272 addMultilibsFilePaths(D, Multilibs, Multilib,
273 GCCInstallation.getInstallPath(), Paths);
275 // Sourcery CodeBench MIPS toolchain holds some libraries under
276 // a biarch-like suffix of the GCC installation.
277 addPathIfExists(D, GCCInstallation.getInstallPath() + Multilib.gccSuffix(),
280 // GCC cross compiling toolchains will install target libraries which ship
281 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
282 // any part of the GCC installation in
283 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
284 // debatable, but is the reality today. We need to search this tree even
285 // when we have a sysroot somewhere else. It is the responsibility of
286 // whomever is doing the cross build targeting a sysroot using a GCC
287 // installation that is *not* within the system root to ensure two things:
289 // 1) Any DSOs that are linked in from this tree or from the install path
290 // above must be present on the system root and found via an
291 // appropriate rpath.
292 // 2) There must not be libraries installed into
293 // <prefix>/<triple>/<libdir> unless they should be preferred over
294 // those within the system root.
296 // Note that this matches the GCC behavior. See the below comment for where
297 // Clang diverges from GCC's behavior.
298 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" +
299 OSLibDir + Multilib.osSuffix(),
302 // If the GCC installation we found is inside of the sysroot, we want to
303 // prefer libraries installed in the parent prefix of the GCC installation.
304 // It is important to *not* use these paths when the GCC installation is
305 // outside of the system root as that can pick up unintended libraries.
306 // This usually happens when there is an external cross compiler on the
307 // host system, and a more minimal sysroot available that is the target of
308 // the cross. Note that GCC does include some of these directories in some
309 // configurations but this seems somewhere between questionable and simply
311 if (StringRef(LibPath).startswith(SysRoot)) {
312 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths);
313 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
317 // Similar to the logic for GCC above, if we currently running Clang inside
318 // of the requested system root, add its parent library paths to
320 // FIXME: It's not clear whether we should use the driver's installed
321 // directory ('Dir' below) or the ResourceDir.
322 if (StringRef(D.Dir).startswith(SysRoot)) {
323 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
324 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
327 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
328 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
329 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
330 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
332 // Try walking via the GCC triple path in case of biarch or multiarch GCC
333 // installations with strange symlinks.
334 if (GCCInstallation.isValid()) {
336 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
337 "/../../" + OSLibDir,
340 // Add the 'other' biarch variant path
341 Multilib BiarchSibling;
342 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
343 addPathIfExists(D, GCCInstallation.getInstallPath() +
344 BiarchSibling.gccSuffix(),
348 // See comments above on the multilib variant for details of why this is
349 // included even from outside the sysroot.
350 const std::string &LibPath = GCCInstallation.getParentLibPath();
351 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
352 const Multilib &Multilib = GCCInstallation.getMultilib();
353 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" +
357 // See comments above on the multilib variant for details of why this is
358 // only included from within the sysroot.
359 if (StringRef(LibPath).startswith(SysRoot))
360 addPathIfExists(D, LibPath, Paths);
363 // Similar to the logic for GCC above, if we are currently running Clang
364 // inside of the requested system root, add its parent library path to those
366 // FIXME: It's not clear whether we should use the driver's installed
367 // directory ('Dir' below) or the ResourceDir.
368 if (StringRef(D.Dir).startswith(SysRoot))
369 addPathIfExists(D, D.Dir + "/../lib", Paths);
371 addPathIfExists(D, SysRoot + "/lib", Paths);
372 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
375 /// \brief Parse a GCCVersion object out of a string of text.
377 /// This is the primary means of forming GCCVersion objects.
379 Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
380 const GCCVersion BadVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
381 std::pair<StringRef, StringRef> First = VersionText.split('.');
382 std::pair<StringRef, StringRef> Second = First.second.split('.');
384 GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
385 if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0)
387 GoodVersion.MajorStr = First.first.str();
388 if (First.second.empty())
390 if (Second.first.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0)
392 GoodVersion.MinorStr = Second.first.str();
394 // First look for a number prefix and parse that if present. Otherwise just
395 // stash the entire patch string in the suffix, and leave the number
396 // unspecified. This covers versions strings such as:
403 // And retains any patch number it finds.
404 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
405 if (!PatchText.empty()) {
406 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
407 // Try to parse the number and any suffix.
408 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
409 GoodVersion.Patch < 0)
411 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
418 bool Linux::HasNativeLLVMSupport() const { return true; }
420 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
422 Tool *Linux::buildAssembler() const {
423 return new tools::gnutools::Assembler(*this);
426 std::string Linux::computeSysRoot() const {
427 if (!getDriver().SysRoot.empty())
428 return getDriver().SysRoot;
430 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch()))
431 return std::string();
433 // Standalone MIPS toolchains use different names for sysroot folder
434 // and put it into different places. Here we try to check some known
437 const StringRef InstallDir = GCCInstallation.getInstallPath();
438 const StringRef TripleStr = GCCInstallation.getTriple().str();
439 const Multilib &Multilib = GCCInstallation.getMultilib();
442 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
445 if (getVFS().exists(Path))
448 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
450 if (getVFS().exists(Path))
453 return std::string();
456 std::string Linux::getDynamicLinker(const ArgList &Args) const {
457 const llvm::Triple::ArchType Arch = getArch();
458 const llvm::Triple &Triple = getTriple();
460 const Distro Distro(getDriver().getVFS());
462 if (Triple.isAndroid())
463 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
465 if (Triple.isMusl()) {
466 std::string ArchName;
470 case llvm::Triple::arm:
471 case llvm::Triple::thumb:
475 case llvm::Triple::armeb:
476 case llvm::Triple::thumbeb:
481 ArchName = Triple.getArchName().str();
484 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
485 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
488 return "/lib/ld-musl-" + ArchName + ".so.1";
496 llvm_unreachable("unsupported architecture");
498 case llvm::Triple::aarch64:
500 Loader = "ld-linux-aarch64.so.1";
502 case llvm::Triple::aarch64_be:
504 Loader = "ld-linux-aarch64_be.so.1";
506 case llvm::Triple::arm:
507 case llvm::Triple::thumb:
508 case llvm::Triple::armeb:
509 case llvm::Triple::thumbeb: {
511 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
512 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
515 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
518 case llvm::Triple::mips:
519 case llvm::Triple::mipsel:
520 case llvm::Triple::mips64:
521 case llvm::Triple::mips64el: {
522 bool LE = (Triple.getArch() == llvm::Triple::mipsel) ||
523 (Triple.getArch() == llvm::Triple::mips64el);
524 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
526 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
528 if (tools::mips::isUCLibc(Args))
529 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
530 else if (!Triple.hasEnvironment() &&
531 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
532 Loader = LE ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
534 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
538 case llvm::Triple::ppc:
542 case llvm::Triple::ppc64:
545 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
547 case llvm::Triple::ppc64le:
550 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
552 case llvm::Triple::sparc:
553 case llvm::Triple::sparcel:
555 Loader = "ld-linux.so.2";
557 case llvm::Triple::sparcv9:
559 Loader = "ld-linux.so.2";
561 case llvm::Triple::systemz:
563 Loader = "ld64.so.1";
565 case llvm::Triple::x86:
567 Loader = "ld-linux.so.2";
569 case llvm::Triple::x86_64: {
570 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
572 LibDir = X32 ? "libx32" : "lib64";
573 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
578 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor ||
579 Triple.getVendor() == llvm::Triple::PC))
580 return "/usr/" + Triple.str() + "/lib/" + Loader;
581 return "/" + LibDir + "/" + Loader;
584 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
585 ArgStringList &CC1Args) const {
586 const Driver &D = getDriver();
587 std::string SysRoot = computeSysRoot();
589 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
592 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
593 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
595 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
596 SmallString<128> P(D.ResourceDir);
597 llvm::sys::path::append(P, "include");
598 addSystemInclude(DriverArgs, CC1Args, P);
601 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
604 // Check for configure-time C include directories.
605 StringRef CIncludeDirs(C_INCLUDE_DIRS);
606 if (CIncludeDirs != "") {
607 SmallVector<StringRef, 5> dirs;
608 CIncludeDirs.split(dirs, ":");
609 for (StringRef dir : dirs) {
611 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
612 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
617 // Lacking those, try to detect the correct set of system includes for the
620 // Add include directories specific to the selected multilib set and multilib.
621 if (GCCInstallation.isValid()) {
622 const auto &Callback = Multilibs.includeDirsCallback();
624 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
625 addExternCSystemIncludeIfExists(
626 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
630 // Implement generic Debian multiarch support.
631 const StringRef X86_64MultiarchIncludeDirs[] = {
632 "/usr/include/x86_64-linux-gnu",
634 // FIXME: These are older forms of multiarch. It's not clear that they're
635 // in use in any released version of Debian, so we should consider
637 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
638 const StringRef X86MultiarchIncludeDirs[] = {
639 "/usr/include/i386-linux-gnu",
641 // FIXME: These are older forms of multiarch. It's not clear that they're
642 // in use in any released version of Debian, so we should consider
644 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
645 "/usr/include/i486-linux-gnu"};
646 const StringRef AArch64MultiarchIncludeDirs[] = {
647 "/usr/include/aarch64-linux-gnu"};
648 const StringRef ARMMultiarchIncludeDirs[] = {
649 "/usr/include/arm-linux-gnueabi"};
650 const StringRef ARMHFMultiarchIncludeDirs[] = {
651 "/usr/include/arm-linux-gnueabihf"};
652 const StringRef ARMEBMultiarchIncludeDirs[] = {
653 "/usr/include/armeb-linux-gnueabi"};
654 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
655 "/usr/include/armeb-linux-gnueabihf"};
656 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
657 const StringRef MIPSELMultiarchIncludeDirs[] = {
658 "/usr/include/mipsel-linux-gnu"};
659 const StringRef MIPS64MultiarchIncludeDirs[] = {
660 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"};
661 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
662 "/usr/include/mips64el-linux-gnu",
663 "/usr/include/mips64el-linux-gnuabi64"};
664 const StringRef PPCMultiarchIncludeDirs[] = {
665 "/usr/include/powerpc-linux-gnu"};
666 const StringRef PPC64MultiarchIncludeDirs[] = {
667 "/usr/include/powerpc64-linux-gnu"};
668 const StringRef PPC64LEMultiarchIncludeDirs[] = {
669 "/usr/include/powerpc64le-linux-gnu"};
670 const StringRef SparcMultiarchIncludeDirs[] = {
671 "/usr/include/sparc-linux-gnu"};
672 const StringRef Sparc64MultiarchIncludeDirs[] = {
673 "/usr/include/sparc64-linux-gnu"};
674 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
675 "/usr/include/s390x-linux-gnu"};
676 ArrayRef<StringRef> MultiarchIncludeDirs;
677 switch (getTriple().getArch()) {
678 case llvm::Triple::x86_64:
679 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
681 case llvm::Triple::x86:
682 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
684 case llvm::Triple::aarch64:
685 case llvm::Triple::aarch64_be:
686 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
688 case llvm::Triple::arm:
689 case llvm::Triple::thumb:
690 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
691 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
693 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
695 case llvm::Triple::armeb:
696 case llvm::Triple::thumbeb:
697 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
698 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
700 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
702 case llvm::Triple::mips:
703 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
705 case llvm::Triple::mipsel:
706 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
708 case llvm::Triple::mips64:
709 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
711 case llvm::Triple::mips64el:
712 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
714 case llvm::Triple::ppc:
715 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
717 case llvm::Triple::ppc64:
718 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
720 case llvm::Triple::ppc64le:
721 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
723 case llvm::Triple::sparc:
724 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
726 case llvm::Triple::sparcv9:
727 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
729 case llvm::Triple::systemz:
730 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
735 for (StringRef Dir : MultiarchIncludeDirs) {
736 if (D.getVFS().exists(SysRoot + Dir)) {
737 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
742 if (getTriple().getOS() == llvm::Triple::RTEMS)
745 // Add an include of '/include' directly. This isn't provided by default by
746 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
747 // add even when Clang is acting as-if it were a system compiler.
748 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
750 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
753 static std::string DetectLibcxxIncludePath(StringRef base) {
756 std::string MaxVersionString = "";
757 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE;
758 LI = LI.increment(EC)) {
759 StringRef VersionText = llvm::sys::path::filename(LI->path());
761 if (VersionText[0] == 'v' &&
762 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
763 if (Version > MaxVersion) {
764 MaxVersion = Version;
765 MaxVersionString = VersionText;
769 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
772 std::string Linux::findLibCxxIncludePath() const {
773 const std::string LibCXXIncludePathCandidates[] = {
774 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"),
775 // If this is a development, non-installed, clang, libcxx will
776 // not be found at ../include/c++ but it likely to be found at
777 // one of the following two locations:
778 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/local/include/c++"),
779 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/include/c++") };
780 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
781 if (IncludePath.empty() || !getVFS().exists(IncludePath))
783 // Use the first candidate that exists.
789 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
790 llvm::opt::ArgStringList &CC1Args) const {
791 // We need a detected GCC installation on Linux to provide libstdc++'s
793 if (!GCCInstallation.isValid())
796 // By default, look for the C++ headers in an include directory adjacent to
797 // the lib directory of the GCC installation. Note that this is expect to be
798 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
799 StringRef LibDir = GCCInstallation.getParentLibPath();
800 StringRef InstallDir = GCCInstallation.getInstallPath();
801 StringRef TripleStr = GCCInstallation.getTriple().str();
802 const Multilib &Multilib = GCCInstallation.getMultilib();
803 const std::string GCCMultiarchTriple = getMultiarchTriple(
804 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot);
805 const std::string TargetMultiarchTriple =
806 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
807 const GCCVersion &Version = GCCInstallation.getVersion();
809 // The primary search for libstdc++ supports multiarch variants.
810 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
811 "/c++/" + Version.Text, TripleStr,
812 GCCMultiarchTriple, TargetMultiarchTriple,
813 Multilib.includeSuffix(), DriverArgs, CC1Args))
816 // Otherwise, fall back on a bunch of options which don't use multiarch
817 // layouts for simplicity.
818 const std::string LibStdCXXIncludePathCandidates[] = {
819 // Gentoo is weird and places its headers inside the GCC install,
820 // so if the first attempt to find the headers fails, try these patterns.
821 InstallDir.str() + "/include/g++-v" + Version.Text,
822 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
824 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
825 // Android standalone toolchain has C++ headers in yet another place.
826 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
827 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
828 // without a subdirectory corresponding to the gcc version.
829 LibDir.str() + "/../include/c++",
832 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
833 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
834 /*GCCMultiarchTriple*/ "",
835 /*TargetMultiarchTriple*/ "",
836 Multilib.includeSuffix(), DriverArgs, CC1Args))
841 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
842 ArgStringList &CC1Args) const {
843 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
846 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
847 ArgStringList &CC1Args) const {
848 if (GCCInstallation.isValid()) {
849 CC1Args.push_back("-isystem");
850 CC1Args.push_back(DriverArgs.MakeArgString(
851 GCCInstallation.getParentLibPath() + "/../" +
852 GCCInstallation.getTriple().str() + "/include"));
856 bool Linux::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
858 SanitizerMask Linux::getSupportedSanitizers() const {
859 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
860 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
861 const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 ||
862 getTriple().getArch() == llvm::Triple::mips64el;
863 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
864 getTriple().getArch() == llvm::Triple::ppc64le;
865 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
866 getTriple().getArch() == llvm::Triple::aarch64_be;
867 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
868 llvm::Triple::thumb || llvm::Triple::armeb ||
869 llvm::Triple::thumbeb;
870 SanitizerMask Res = ToolChain::getSupportedSanitizers();
871 Res |= SanitizerKind::Address;
872 Res |= SanitizerKind::Fuzzer;
873 Res |= SanitizerKind::KernelAddress;
874 Res |= SanitizerKind::Vptr;
875 Res |= SanitizerKind::SafeStack;
876 if (IsX86_64 || IsMIPS64 || IsAArch64)
877 Res |= SanitizerKind::DataFlow;
878 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch)
879 Res |= SanitizerKind::Leak;
880 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
881 Res |= SanitizerKind::Thread;
882 if (IsX86_64 || IsMIPS64 || IsPowerPC64 || IsAArch64)
883 Res |= SanitizerKind::Memory;
884 if (IsX86_64 || IsMIPS64)
885 Res |= SanitizerKind::Efficiency;
886 if (IsX86 || IsX86_64) {
887 Res |= SanitizerKind::Function;
892 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
893 llvm::opt::ArgStringList &CmdArgs) const {
894 if (!needsProfileRT(Args)) return;
896 // Add linker option -u__llvm_runtime_variable to cause runtime
897 // initialization module to be linked in.
898 if (!Args.hasArg(options::OPT_coverage))
899 CmdArgs.push_back(Args.MakeArgString(
900 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
901 ToolChain::addProfileRTLibs(Args, CmdArgs);