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/LoongArch.h"
12 #include "Arch/Mips.h"
14 #include "Arch/RISCV.h"
15 #include "CommonArgs.h"
16 #include "clang/Config/config.h"
17 #include "clang/Driver/Distro.h"
18 #include "clang/Driver/Driver.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Driver/SanitizerArgs.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/ProfileData/InstrProf.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/ScopedPrinter.h"
25 #include "llvm/Support/VirtualFileSystem.h"
26 #include <system_error>
28 using namespace clang::driver
;
29 using namespace clang::driver::toolchains
;
30 using namespace clang
;
31 using namespace llvm::opt
;
33 using tools::addPathIfExists
;
35 /// Get our best guess at the multiarch triple for a target.
37 /// Debian-based systems are starting to use a multiarch setup where they use
38 /// a target-triple directory in the library and header search paths.
39 /// Unfortunately, this triple does not align with the vanilla target triple,
40 /// so we provide a rough mapping here.
41 std::string
Linux::getMultiarchTriple(const Driver
&D
,
42 const llvm::Triple
&TargetTriple
,
43 StringRef SysRoot
) const {
44 llvm::Triple::EnvironmentType TargetEnvironment
=
45 TargetTriple
.getEnvironment();
46 bool IsAndroid
= TargetTriple
.isAndroid();
47 bool IsMipsR6
= TargetTriple
.getSubArch() == llvm::Triple::MipsSubArch_r6
;
48 bool IsMipsN32Abi
= TargetTriple
.getEnvironment() == llvm::Triple::GNUABIN32
;
50 // For most architectures, just use whatever we have rather than trying to be
52 switch (TargetTriple
.getArch()) {
56 // We use the existence of '/lib/<triple>' as a directory to detect some
57 // common linux triples that don't quite match the Clang triple for both
58 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
59 // regardless of what the actual target triple is.
60 case llvm::Triple::arm
:
61 case llvm::Triple::thumb
:
63 return "arm-linux-androideabi";
64 if (TargetEnvironment
== llvm::Triple::GNUEABIHF
)
65 return "arm-linux-gnueabihf";
66 return "arm-linux-gnueabi";
67 case llvm::Triple::armeb
:
68 case llvm::Triple::thumbeb
:
69 if (TargetEnvironment
== llvm::Triple::GNUEABIHF
)
70 return "armeb-linux-gnueabihf";
71 return "armeb-linux-gnueabi";
72 case llvm::Triple::x86
:
74 return "i686-linux-android";
75 return "i386-linux-gnu";
76 case llvm::Triple::x86_64
:
78 return "x86_64-linux-android";
79 if (TargetEnvironment
== llvm::Triple::GNUX32
)
80 return "x86_64-linux-gnux32";
81 return "x86_64-linux-gnu";
82 case llvm::Triple::aarch64
:
84 return "aarch64-linux-android";
85 return "aarch64-linux-gnu";
86 case llvm::Triple::aarch64_be
:
87 return "aarch64_be-linux-gnu";
89 case llvm::Triple::loongarch64
: {
93 if (TargetTriple
.isGNUEnvironment()) {
95 } else if (TargetTriple
.isMusl()) {
98 return TargetTriple
.str();
101 switch (TargetEnvironment
) {
103 return TargetTriple
.str();
104 case llvm::Triple::GNUSF
:
107 case llvm::Triple::GNUF32
:
110 case llvm::Triple::GNU
:
111 case llvm::Triple::GNUF64
:
112 // This was going to be "f64" in an earlier Toolchain Conventions
113 // revision, but starting from Feb 2023 the F64 ABI variants are
114 // unmarked in their canonical forms.
119 return (Twine("loongarch64-linux-") + Libc
+ FPFlavor
).str();
122 case llvm::Triple::m68k
:
123 return "m68k-linux-gnu";
125 case llvm::Triple::mips
:
126 return IsMipsR6
? "mipsisa32r6-linux-gnu" : "mips-linux-gnu";
127 case llvm::Triple::mipsel
:
128 return IsMipsR6
? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu";
129 case llvm::Triple::mips64
: {
130 std::string MT
= std::string(IsMipsR6
? "mipsisa64r6" : "mips64") +
131 "-linux-" + (IsMipsN32Abi
? "gnuabin32" : "gnuabi64");
132 if (D
.getVFS().exists(concat(SysRoot
, "/lib", MT
)))
134 if (D
.getVFS().exists(concat(SysRoot
, "/lib/mips64-linux-gnu")))
135 return "mips64-linux-gnu";
138 case llvm::Triple::mips64el
: {
139 std::string MT
= std::string(IsMipsR6
? "mipsisa64r6el" : "mips64el") +
140 "-linux-" + (IsMipsN32Abi
? "gnuabin32" : "gnuabi64");
141 if (D
.getVFS().exists(concat(SysRoot
, "/lib", MT
)))
143 if (D
.getVFS().exists(concat(SysRoot
, "/lib/mips64el-linux-gnu")))
144 return "mips64el-linux-gnu";
147 case llvm::Triple::ppc
:
148 if (D
.getVFS().exists(concat(SysRoot
, "/lib/powerpc-linux-gnuspe")))
149 return "powerpc-linux-gnuspe";
150 return "powerpc-linux-gnu";
151 case llvm::Triple::ppcle
:
152 return "powerpcle-linux-gnu";
153 case llvm::Triple::ppc64
:
154 return "powerpc64-linux-gnu";
155 case llvm::Triple::ppc64le
:
156 return "powerpc64le-linux-gnu";
157 case llvm::Triple::riscv64
:
159 return "riscv64-linux-android";
160 return "riscv64-linux-gnu";
161 case llvm::Triple::sparc
:
162 return "sparc-linux-gnu";
163 case llvm::Triple::sparcv9
:
164 return "sparc64-linux-gnu";
165 case llvm::Triple::systemz
:
166 return "s390x-linux-gnu";
168 return TargetTriple
.str();
171 static StringRef
getOSLibDir(const llvm::Triple
&Triple
, const ArgList
&Args
) {
172 if (Triple
.isMIPS()) {
173 if (Triple
.isAndroid()) {
176 tools::mips::getMipsCPUAndABI(Args
, Triple
, CPUName
, ABIName
);
177 if (CPUName
== "mips32r6")
179 if (CPUName
== "mips32r2")
182 // lib32 directory has a special meaning on MIPS targets.
183 // It contains N32 ABI binaries. Use this folder if produce
184 // code for N32 ABI only.
185 if (tools::mips::hasMipsAbiArg(Args
, "n32"))
187 return Triple
.isArch32Bit() ? "lib" : "lib64";
190 // It happens that only x86, PPC and SPARC use the 'lib32' variant of
191 // oslibdir, and using that variant while targeting other architectures causes
192 // problems because the libraries are laid out in shared system roots that
193 // can't cope with a 'lib32' library search path being considered. So we only
194 // enable them when we know we may need it.
196 // FIXME: This is a bit of a hack. We should really unify this code for
197 // reasoning about oslibdir spellings with the lib dir spellings in the
198 // GCCInstallationDetector, but that is a more significant refactoring.
199 if (Triple
.getArch() == llvm::Triple::x86
|| Triple
.isPPC32() ||
200 Triple
.getArch() == llvm::Triple::sparc
)
203 if (Triple
.getArch() == llvm::Triple::x86_64
&& Triple
.isX32())
206 if (Triple
.getArch() == llvm::Triple::riscv32
)
209 return Triple
.isArch32Bit() ? "lib" : "lib64";
212 Linux::Linux(const Driver
&D
, const llvm::Triple
&Triple
, const ArgList
&Args
)
213 : Generic_ELF(D
, Triple
, Args
) {
214 GCCInstallation
.init(Triple
, Args
);
215 Multilibs
= GCCInstallation
.getMultilibs();
216 SelectedMultilibs
.assign({GCCInstallation
.getMultilib()});
217 llvm::Triple::ArchType Arch
= Triple
.getArch();
218 std::string SysRoot
= computeSysRoot();
219 ToolChain::path_list
&PPaths
= getProgramPaths();
221 Generic_GCC::PushPPaths(PPaths
);
223 Distro
Distro(D
.getVFS(), Triple
);
225 if (Distro
.IsAlpineLinux() || Triple
.isAndroid()) {
226 ExtraOpts
.push_back("-z");
227 ExtraOpts
.push_back("now");
230 if (Distro
.IsOpenSUSE() || Distro
.IsUbuntu() || Distro
.IsAlpineLinux() ||
231 Triple
.isAndroid()) {
232 ExtraOpts
.push_back("-z");
233 ExtraOpts
.push_back("relro");
236 // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld
237 // from 11 onwards default max-page-size to 65536 for both ARM and AArch64.
238 if ((Triple
.isARM() || Triple
.isAArch64()) && Triple
.isAndroid()) {
239 ExtraOpts
.push_back("-z");
240 ExtraOpts
.push_back("max-page-size=4096");
243 if (GCCInstallation
.getParentLibPath().contains("opt/rh/"))
244 // With devtoolset on RHEL, we want to add a bin directory that is relative
245 // to the detected gcc install, because if we are using devtoolset gcc then
246 // we want to use other tools from devtoolset (e.g. ld) instead of the
247 // standard system tools.
248 PPaths
.push_back(Twine(GCCInstallation
.getParentLibPath() +
251 if (Arch
== llvm::Triple::arm
|| Arch
== llvm::Triple::thumb
)
252 ExtraOpts
.push_back("-X");
254 const bool IsAndroid
= Triple
.isAndroid();
255 const bool IsMips
= Triple
.isMIPS();
256 const bool IsHexagon
= Arch
== llvm::Triple::hexagon
;
257 const bool IsRISCV
= Triple
.isRISCV();
258 const bool IsCSKY
= Triple
.isCSKY();
260 if (IsCSKY
&& !SelectedMultilibs
.empty())
261 SysRoot
= SysRoot
+ SelectedMultilibs
.back().osSuffix();
263 if ((IsMips
|| IsCSKY
) && !SysRoot
.empty())
264 ExtraOpts
.push_back("--sysroot=" + SysRoot
);
266 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
267 // and the MIPS ABI require .dynsym to be sorted in different ways.
268 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
269 // ABI requires a mapping between the GOT and the symbol table.
270 // Android loader does not support .gnu.hash until API 23.
271 // Hexagon linker/loader does not support .gnu.hash
272 if (!IsMips
&& !IsHexagon
) {
273 if (Distro
.IsOpenSUSE() || Distro
== Distro::UbuntuLucid
||
274 Distro
== Distro::UbuntuJaunty
|| Distro
== Distro::UbuntuKarmic
||
275 (IsAndroid
&& Triple
.isAndroidVersionLT(23)))
276 ExtraOpts
.push_back("--hash-style=both");
278 ExtraOpts
.push_back("--hash-style=gnu");
281 #ifdef ENABLE_LINKER_BUILD_ID
282 ExtraOpts
.push_back("--build-id");
285 // The selection of paths to try here is designed to match the patterns which
286 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
287 // This was determined by running GCC in a fake filesystem, creating all
288 // possible permutations of these directories, and seeing which ones it added
289 // to the link paths.
290 path_list
&Paths
= getFilePaths();
292 const std::string OSLibDir
= std::string(getOSLibDir(Triple
, Args
));
293 const std::string MultiarchTriple
= getMultiarchTriple(D
, Triple
, SysRoot
);
295 // mips32: Debian multilib, we use /libo32, while in other case, /lib is
296 // used. We need add both libo32 and /lib.
297 if (Arch
== llvm::Triple::mips
|| Arch
== llvm::Triple::mipsel
) {
298 Generic_GCC::AddMultilibPaths(D
, SysRoot
, "libo32", MultiarchTriple
, Paths
);
299 addPathIfExists(D
, concat(SysRoot
, "/libo32"), Paths
);
300 addPathIfExists(D
, concat(SysRoot
, "/usr/libo32"), Paths
);
302 Generic_GCC::AddMultilibPaths(D
, SysRoot
, OSLibDir
, MultiarchTriple
, Paths
);
304 addPathIfExists(D
, concat(SysRoot
, "/lib", MultiarchTriple
), Paths
);
305 addPathIfExists(D
, concat(SysRoot
, "/lib/..", OSLibDir
), Paths
);
308 // Android sysroots contain a library directory for each supported OS
309 // version as well as some unversioned libraries in the usual multiarch
313 concat(SysRoot
, "/usr/lib", MultiarchTriple
,
314 llvm::to_string(Triple
.getEnvironmentVersion().getMajor())),
318 addPathIfExists(D
, concat(SysRoot
, "/usr/lib", MultiarchTriple
), Paths
);
319 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
320 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
322 if (Triple
.getVendor() == llvm::Triple::OpenEmbedded
&&
323 Triple
.isArch64Bit())
324 addPathIfExists(D
, concat(SysRoot
, "/usr", OSLibDir
), Paths
);
326 addPathIfExists(D
, concat(SysRoot
, "/usr/lib/..", OSLibDir
), Paths
);
328 StringRef ABIName
= tools::riscv::getRISCVABI(Args
, Triple
);
329 addPathIfExists(D
, concat(SysRoot
, "/", OSLibDir
, ABIName
), Paths
);
330 addPathIfExists(D
, concat(SysRoot
, "/usr", OSLibDir
, ABIName
), Paths
);
333 Generic_GCC::AddMultiarchPaths(D
, SysRoot
, OSLibDir
, Paths
);
335 addPathIfExists(D
, concat(SysRoot
, "/lib"), Paths
);
336 addPathIfExists(D
, concat(SysRoot
, "/usr/lib"), Paths
);
339 ToolChain::RuntimeLibType
Linux::GetDefaultRuntimeLibType() const {
340 if (getTriple().isAndroid())
341 return ToolChain::RLT_CompilerRT
;
342 return Generic_ELF::GetDefaultRuntimeLibType();
345 unsigned Linux::GetDefaultDwarfVersion() const {
346 if (getTriple().isAndroid())
348 return ToolChain::GetDefaultDwarfVersion();
351 ToolChain::CXXStdlibType
Linux::GetDefaultCXXStdlibType() const {
352 if (getTriple().isAndroid())
353 return ToolChain::CST_Libcxx
;
354 return ToolChain::CST_Libstdcxx
;
357 bool Linux::HasNativeLLVMSupport() const { return true; }
359 Tool
*Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
361 Tool
*Linux::buildStaticLibTool() const {
362 return new tools::gnutools::StaticLibTool(*this);
365 Tool
*Linux::buildAssembler() const {
366 return new tools::gnutools::Assembler(*this);
369 std::string
Linux::computeSysRoot() const {
370 if (!getDriver().SysRoot
.empty())
371 return getDriver().SysRoot
;
373 if (getTriple().isAndroid()) {
374 // Android toolchains typically include a sysroot at ../sysroot relative to
376 const StringRef ClangDir
= getDriver().getInstalledDir();
377 std::string AndroidSysRootPath
= (ClangDir
+ "/../sysroot").str();
378 if (getVFS().exists(AndroidSysRootPath
))
379 return AndroidSysRootPath
;
382 if (getTriple().isCSKY()) {
383 // CSKY toolchains use different names for sysroot folder.
384 if (!GCCInstallation
.isValid())
385 return std::string();
386 // GCCInstallation.getInstallPath() =
387 // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0
388 // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc
389 std::string Path
= (GCCInstallation
.getInstallPath() + "/../../../../" +
390 GCCInstallation
.getTriple().str() + "/libc")
392 if (getVFS().exists(Path
))
394 return std::string();
397 if (!GCCInstallation
.isValid() || !getTriple().isMIPS())
398 return std::string();
400 // Standalone MIPS toolchains use different names for sysroot folder
401 // and put it into different places. Here we try to check some known
404 const StringRef InstallDir
= GCCInstallation
.getInstallPath();
405 const StringRef TripleStr
= GCCInstallation
.getTriple().str();
406 const Multilib
&Multilib
= GCCInstallation
.getMultilib();
409 (InstallDir
+ "/../../../../" + TripleStr
+ "/libc" + Multilib
.osSuffix())
412 if (getVFS().exists(Path
))
415 Path
= (InstallDir
+ "/../../../../sysroot" + Multilib
.osSuffix()).str();
417 if (getVFS().exists(Path
))
420 return std::string();
423 std::string
Linux::getDynamicLinker(const ArgList
&Args
) const {
424 const llvm::Triple::ArchType Arch
= getArch();
425 const llvm::Triple
&Triple
= getTriple();
427 const Distro
Distro(getDriver().getVFS(), Triple
);
429 if (Triple
.isAndroid()) {
430 if (getSanitizerArgs(Args
).needsHwasanRt() &&
431 !Triple
.isAndroidVersionLT(34) && Triple
.isArch64Bit()) {
432 // On Android 14 and newer, there is a special linker_hwasan64 that
433 // allows to run HWASan binaries on non-HWASan system images. This
434 // is also available on HWASan system images, so we can just always
436 return "/system/bin/linker_hwasan64";
438 return Triple
.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
440 if (Triple
.isMusl()) {
441 std::string ArchName
;
445 case llvm::Triple::arm
:
446 case llvm::Triple::thumb
:
450 case llvm::Triple::armeb
:
451 case llvm::Triple::thumbeb
:
455 case llvm::Triple::x86
:
458 case llvm::Triple::x86_64
:
459 ArchName
= Triple
.isX32() ? "x32" : Triple
.getArchName().str();
462 ArchName
= Triple
.getArchName().str();
465 (Triple
.getEnvironment() == llvm::Triple::MuslEABIHF
||
466 tools::arm::getARMFloatABI(*this, Args
) == tools::arm::FloatABI::Hard
))
468 if (Arch
== llvm::Triple::ppc
&&
469 Triple
.getSubArch() == llvm::Triple::PPCSubArch_spe
)
470 ArchName
= "powerpc-sf";
472 return "/lib/ld-musl-" + ArchName
+ ".so.1";
480 llvm_unreachable("unsupported architecture");
482 case llvm::Triple::aarch64
:
484 Loader
= "ld-linux-aarch64.so.1";
486 case llvm::Triple::aarch64_be
:
488 Loader
= "ld-linux-aarch64_be.so.1";
490 case llvm::Triple::arm
:
491 case llvm::Triple::thumb
:
492 case llvm::Triple::armeb
:
493 case llvm::Triple::thumbeb
: {
495 Triple
.getEnvironment() == llvm::Triple::GNUEABIHF
||
496 tools::arm::getARMFloatABI(*this, Args
) == tools::arm::FloatABI::Hard
;
499 Loader
= HF
? "ld-linux-armhf.so.3" : "ld-linux.so.3";
502 case llvm::Triple::loongarch32
: {
505 ("ld-linux-loongarch-" +
506 tools::loongarch::getLoongArchABI(getDriver(), Args
, Triple
) + ".so.1")
510 case llvm::Triple::loongarch64
: {
513 ("ld-linux-loongarch-" +
514 tools::loongarch::getLoongArchABI(getDriver(), Args
, Triple
) + ".so.1")
518 case llvm::Triple::m68k
:
522 case llvm::Triple::mips
:
523 case llvm::Triple::mipsel
:
524 case llvm::Triple::mips64
:
525 case llvm::Triple::mips64el
: {
526 bool IsNaN2008
= tools::mips::isNaN2008(getDriver(), Args
, Triple
);
528 LibDir
= "lib" + tools::mips::getMipsABILibSuffix(Args
, Triple
);
530 if (tools::mips::isUCLibc(Args
))
531 Loader
= IsNaN2008
? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
532 else if (!Triple
.hasEnvironment() &&
533 Triple
.getVendor() == llvm::Triple::VendorType::MipsTechnologies
)
535 Triple
.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
537 Loader
= IsNaN2008
? "ld-linux-mipsn8.so.1" : "ld.so.1";
541 case llvm::Triple::ppc
:
545 case llvm::Triple::ppcle
:
549 case llvm::Triple::ppc64
:
552 (tools::ppc::hasPPCAbiArg(Args
, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
554 case llvm::Triple::ppc64le
:
557 (tools::ppc::hasPPCAbiArg(Args
, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
559 case llvm::Triple::riscv32
: {
560 StringRef ABIName
= tools::riscv::getRISCVABI(Args
, Triple
);
562 Loader
= ("ld-linux-riscv32-" + ABIName
+ ".so.1").str();
565 case llvm::Triple::riscv64
: {
566 StringRef ABIName
= tools::riscv::getRISCVABI(Args
, Triple
);
568 Loader
= ("ld-linux-riscv64-" + ABIName
+ ".so.1").str();
571 case llvm::Triple::sparc
:
572 case llvm::Triple::sparcel
:
574 Loader
= "ld-linux.so.2";
576 case llvm::Triple::sparcv9
:
578 Loader
= "ld-linux.so.2";
580 case llvm::Triple::systemz
:
582 Loader
= "ld64.so.1";
584 case llvm::Triple::x86
:
586 Loader
= "ld-linux.so.2";
588 case llvm::Triple::x86_64
: {
589 bool X32
= Triple
.isX32();
591 LibDir
= X32
? "libx32" : "lib64";
592 Loader
= X32
? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
595 case llvm::Triple::ve
:
596 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
597 case llvm::Triple::csky
: {
604 if (Distro
== Distro::Exherbo
&&
605 (Triple
.getVendor() == llvm::Triple::UnknownVendor
||
606 Triple
.getVendor() == llvm::Triple::PC
))
607 return "/usr/" + Triple
.str() + "/lib/" + Loader
;
608 return "/" + LibDir
+ "/" + Loader
;
611 void Linux::AddClangSystemIncludeArgs(const ArgList
&DriverArgs
,
612 ArgStringList
&CC1Args
) const {
613 const Driver
&D
= getDriver();
614 std::string SysRoot
= computeSysRoot();
616 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
619 // Add 'include' in the resource directory, which is similar to
620 // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
621 // contains some files conflicting with system /usr/include. musl systems
622 // prefer the /usr/include copies which are more relevant.
623 SmallString
<128> ResourceDirInclude(D
.ResourceDir
);
624 llvm::sys::path::append(ResourceDirInclude
, "include");
625 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
) &&
626 (!getTriple().isMusl() || DriverArgs
.hasArg(options::OPT_nostdlibinc
)))
627 addSystemInclude(DriverArgs
, CC1Args
, ResourceDirInclude
);
629 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
633 addSystemInclude(DriverArgs
, CC1Args
, concat(SysRoot
, "/usr/local/include"));
635 AddMultilibIncludeArgs(DriverArgs
, CC1Args
);
637 // Check for configure-time C include directories.
638 StringRef
CIncludeDirs(C_INCLUDE_DIRS
);
639 if (CIncludeDirs
!= "") {
640 SmallVector
<StringRef
, 5> dirs
;
641 CIncludeDirs
.split(dirs
, ":");
642 for (StringRef dir
: dirs
) {
644 llvm::sys::path::is_absolute(dir
) ? "" : StringRef(SysRoot
);
645 addExternCSystemInclude(DriverArgs
, CC1Args
, Prefix
+ dir
);
650 // On systems using multiarch and Android, add /usr/include/$triple before
652 std::string MultiarchIncludeDir
= getMultiarchTriple(D
, getTriple(), SysRoot
);
653 if (!MultiarchIncludeDir
.empty() &&
654 D
.getVFS().exists(concat(SysRoot
, "/usr/include", MultiarchIncludeDir
)))
655 addExternCSystemInclude(
657 concat(SysRoot
, "/usr/include", MultiarchIncludeDir
));
659 if (getTriple().getOS() == llvm::Triple::RTEMS
)
662 // Add an include of '/include' directly. This isn't provided by default by
663 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
664 // add even when Clang is acting as-if it were a system compiler.
665 addExternCSystemInclude(DriverArgs
, CC1Args
, concat(SysRoot
, "/include"));
667 addExternCSystemInclude(DriverArgs
, CC1Args
, concat(SysRoot
, "/usr/include"));
669 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
) && getTriple().isMusl())
670 addSystemInclude(DriverArgs
, CC1Args
, ResourceDirInclude
);
673 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList
&DriverArgs
,
674 llvm::opt::ArgStringList
&CC1Args
) const {
675 // We need a detected GCC installation on Linux to provide libstdc++'s
676 // headers in odd Linuxish places.
677 if (!GCCInstallation
.isValid())
680 // Detect Debian g++-multiarch-incdir.diff.
681 StringRef TripleStr
= GCCInstallation
.getTriple().str();
682 StringRef DebianMultiarch
=
683 GCCInstallation
.getTriple().getArch() == llvm::Triple::x86
687 // Try generic GCC detection first.
688 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs
, CC1Args
,
692 StringRef LibDir
= GCCInstallation
.getParentLibPath();
693 const Multilib
&Multilib
= GCCInstallation
.getMultilib();
694 const GCCVersion
&Version
= GCCInstallation
.getVersion();
696 const std::string LibStdCXXIncludePathCandidates
[] = {
697 // Android standalone toolchain has C++ headers in yet another place.
698 LibDir
.str() + "/../" + TripleStr
.str() + "/include/c++/" + Version
.Text
,
699 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
700 // without a subdirectory corresponding to the gcc version.
701 LibDir
.str() + "/../include/c++",
702 // Cray's gcc installation puts headers under "g++" without a
704 LibDir
.str() + "/../include/g++",
707 for (const auto &IncludePath
: LibStdCXXIncludePathCandidates
) {
708 if (addLibStdCXXIncludePaths(IncludePath
, TripleStr
,
709 Multilib
.includeSuffix(), DriverArgs
, CC1Args
))
714 void Linux::AddCudaIncludeArgs(const ArgList
&DriverArgs
,
715 ArgStringList
&CC1Args
) const {
716 CudaInstallation
->AddCudaIncludeArgs(DriverArgs
, CC1Args
);
719 void Linux::AddHIPIncludeArgs(const ArgList
&DriverArgs
,
720 ArgStringList
&CC1Args
) const {
721 RocmInstallation
->AddHIPIncludeArgs(DriverArgs
, CC1Args
);
724 void Linux::AddHIPRuntimeLibArgs(const ArgList
&Args
,
725 ArgStringList
&CmdArgs
) const {
727 Args
.MakeArgString(StringRef("-L") + RocmInstallation
->getLibPath()));
729 if (Args
.hasFlag(options::OPT_frtlib_add_rpath
,
730 options::OPT_fno_rtlib_add_rpath
, false))
732 {"-rpath", Args
.MakeArgString(RocmInstallation
->getLibPath())});
734 CmdArgs
.push_back("-lamdhip64");
737 void Linux::AddIAMCUIncludeArgs(const ArgList
&DriverArgs
,
738 ArgStringList
&CC1Args
) const {
739 if (GCCInstallation
.isValid()) {
740 CC1Args
.push_back("-isystem");
741 CC1Args
.push_back(DriverArgs
.MakeArgString(
742 GCCInstallation
.getParentLibPath() + "/../" +
743 GCCInstallation
.getTriple().str() + "/include"));
747 bool Linux::isPIEDefault(const llvm::opt::ArgList
&Args
) const {
748 return CLANG_DEFAULT_PIE_ON_LINUX
|| getTriple().isAndroid() ||
749 getTriple().isMusl() || getSanitizerArgs(Args
).requiresPIE();
752 bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList
&Args
) const {
753 // Outline atomics for AArch64 are supported by compiler-rt
754 // and libgcc since 9.3.1
755 assert(getTriple().isAArch64() && "expected AArch64 target!");
756 ToolChain::RuntimeLibType RtLib
= GetRuntimeLibType(Args
);
757 if (RtLib
== ToolChain::RLT_CompilerRT
)
759 assert(RtLib
== ToolChain::RLT_Libgcc
&& "unexpected runtime library type!");
760 if (GCCInstallation
.getVersion().isOlderThan(9, 3, 1))
765 bool Linux::IsMathErrnoDefault() const {
766 if (getTriple().isAndroid() || getTriple().isMusl())
768 return Generic_ELF::IsMathErrnoDefault();
771 SanitizerMask
Linux::getSupportedSanitizers() const {
772 const bool IsX86
= getTriple().getArch() == llvm::Triple::x86
;
773 const bool IsX86_64
= getTriple().getArch() == llvm::Triple::x86_64
;
774 const bool IsMIPS
= getTriple().isMIPS32();
775 const bool IsMIPS64
= getTriple().isMIPS64();
776 const bool IsPowerPC64
= getTriple().getArch() == llvm::Triple::ppc64
||
777 getTriple().getArch() == llvm::Triple::ppc64le
;
778 const bool IsAArch64
= getTriple().getArch() == llvm::Triple::aarch64
||
779 getTriple().getArch() == llvm::Triple::aarch64_be
;
780 const bool IsArmArch
= getTriple().getArch() == llvm::Triple::arm
||
781 getTriple().getArch() == llvm::Triple::thumb
||
782 getTriple().getArch() == llvm::Triple::armeb
||
783 getTriple().getArch() == llvm::Triple::thumbeb
;
784 const bool IsLoongArch64
= getTriple().getArch() == llvm::Triple::loongarch64
;
785 const bool IsRISCV64
= getTriple().getArch() == llvm::Triple::riscv64
;
786 const bool IsSystemZ
= getTriple().getArch() == llvm::Triple::systemz
;
787 const bool IsHexagon
= getTriple().getArch() == llvm::Triple::hexagon
;
788 SanitizerMask Res
= ToolChain::getSupportedSanitizers();
789 Res
|= SanitizerKind::Address
;
790 Res
|= SanitizerKind::PointerCompare
;
791 Res
|= SanitizerKind::PointerSubtract
;
792 Res
|= SanitizerKind::Fuzzer
;
793 Res
|= SanitizerKind::FuzzerNoLink
;
794 Res
|= SanitizerKind::KernelAddress
;
795 Res
|= SanitizerKind::Memory
;
796 Res
|= SanitizerKind::Vptr
;
797 Res
|= SanitizerKind::SafeStack
;
798 if (IsX86_64
|| IsMIPS64
|| IsAArch64
|| IsLoongArch64
)
799 Res
|= SanitizerKind::DataFlow
;
800 if (IsX86_64
|| IsMIPS64
|| IsAArch64
|| IsX86
|| IsArmArch
|| IsPowerPC64
||
801 IsRISCV64
|| IsSystemZ
|| IsHexagon
|| IsLoongArch64
)
802 Res
|= SanitizerKind::Leak
;
803 if (IsX86_64
|| IsMIPS64
|| IsAArch64
|| IsPowerPC64
|| IsSystemZ
||
804 IsLoongArch64
|| IsRISCV64
)
805 Res
|= SanitizerKind::Thread
;
806 if (IsX86_64
|| IsSystemZ
)
807 Res
|= SanitizerKind::KernelMemory
;
808 if (IsX86_64
|| IsMIPS64
|| IsAArch64
|| IsX86
|| IsMIPS
|| IsArmArch
||
809 IsPowerPC64
|| IsHexagon
|| IsLoongArch64
|| IsRISCV64
)
810 Res
|= SanitizerKind::Scudo
;
811 if (IsX86_64
|| IsAArch64
|| IsRISCV64
) {
812 Res
|= SanitizerKind::HWAddress
;
814 if (IsX86_64
|| IsAArch64
) {
815 Res
|= SanitizerKind::KernelHWAddress
;
817 // Work around "Cannot represent a difference across sections".
818 if (getTriple().getArch() == llvm::Triple::ppc64
)
819 Res
&= ~SanitizerKind::Function
;
823 void Linux::addProfileRTLibs(const llvm::opt::ArgList
&Args
,
824 llvm::opt::ArgStringList
&CmdArgs
) const {
825 // Add linker option -u__llvm_profile_runtime to cause runtime
826 // initialization module to be linked in.
827 if (needsProfileRT(Args
))
828 CmdArgs
.push_back(Args
.MakeArgString(
829 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
830 ToolChain::addProfileRTLibs(Args
, CmdArgs
);
834 Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList
&DriverArgs
,
836 const llvm::fltSemantics
*FPType
) const {
837 switch (getTriple().getArch()) {
838 case llvm::Triple::x86
:
839 case llvm::Triple::x86_64
: {
841 // DAZ and FTZ are turned on in crtfastmath.o
842 if (!DriverArgs
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
) &&
843 isFastMathRuntimeAvailable(DriverArgs
, Unused
))
844 return llvm::DenormalMode::getPreserveSign();
845 return llvm::DenormalMode::getIEEE();
848 return llvm::DenormalMode::getIEEE();
852 void Linux::addExtraOpts(llvm::opt::ArgStringList
&CmdArgs
) const {
853 for (const auto &Opt
: ExtraOpts
)
854 CmdArgs
.push_back(Opt
.c_str());
857 const char *Linux::getDefaultLinker() const {
858 if (getTriple().isAndroid())
860 return Generic_ELF::getDefaultLinker();