clang 18.0.0git
Linux.cpp
Go to the documentation of this file.
1//===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "Linux.h"
10#include "Arch/ARM.h"
11#include "Arch/LoongArch.h"
12#include "Arch/Mips.h"
13#include "Arch/PPC.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"
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>
27
28using namespace clang::driver;
29using namespace clang::driver::toolchains;
30using namespace clang;
31using namespace llvm::opt;
32
34
35/// Get our best guess at the multiarch triple for a target.
36///
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.
41std::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;
49
50 // For most architectures, just use whatever we have rather than trying to be
51 // clever.
52 switch (TargetTriple.getArch()) {
53 default:
54 break;
55
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:
62 if (IsAndroid)
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:
73 if (IsAndroid)
74 return "i686-linux-android";
75 return "i386-linux-gnu";
76 case llvm::Triple::x86_64:
77 if (IsAndroid)
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:
83 if (IsAndroid)
84 return "aarch64-linux-android";
85 return "aarch64-linux-gnu";
86 case llvm::Triple::aarch64_be:
87 return "aarch64_be-linux-gnu";
88
89 case llvm::Triple::loongarch64: {
90 const char *Libc;
91 const char *FPFlavor;
92
93 if (TargetTriple.isGNUEnvironment()) {
94 Libc = "gnu";
95 } else if (TargetTriple.isMusl()) {
96 Libc = "musl";
97 } else {
98 return TargetTriple.str();
99 }
100
101 switch (TargetEnvironment) {
102 default:
103 return TargetTriple.str();
104 case llvm::Triple::GNUSF:
105 FPFlavor = "sf";
106 break;
107 case llvm::Triple::GNUF32:
108 FPFlavor = "f32";
109 break;
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.
115 FPFlavor = "";
116 break;
117 }
118
119 return (Twine("loongarch64-linux-") + Libc + FPFlavor).str();
120 }
121
122 case llvm::Triple::m68k:
123 return "m68k-linux-gnu";
124
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)))
133 return MT;
134 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64-linux-gnu")))
135 return "mips64-linux-gnu";
136 break;
137 }
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)))
142 return MT;
143 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64el-linux-gnu")))
144 return "mips64el-linux-gnu";
145 break;
146 }
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:
158 if (IsAndroid)
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";
167 }
168 return TargetTriple.str();
169}
170
171static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
172 if (Triple.isMIPS()) {
173 if (Triple.isAndroid()) {
174 StringRef CPUName;
175 StringRef ABIName;
176 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
177 if (CPUName == "mips32r6")
178 return "libr6";
179 if (CPUName == "mips32r2")
180 return "libr2";
181 }
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"))
186 return "lib32";
187 return Triple.isArch32Bit() ? "lib" : "lib64";
188 }
189
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.
195 //
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)
201 return "lib32";
202
203 if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32())
204 return "libx32";
205
206 if (Triple.getArch() == llvm::Triple::riscv32)
207 return "lib32";
208
209 return Triple.isArch32Bit() ? "lib" : "lib64";
210}
211
212Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
213 : Generic_ELF(D, Triple, Args) {
214 GCCInstallation.init(Triple, Args);
217 llvm::Triple::ArchType Arch = Triple.getArch();
218 std::string SysRoot = computeSysRoot();
220
222
223 Distro Distro(D.getVFS(), Triple);
224
225 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
226 ExtraOpts.push_back("-z");
227 ExtraOpts.push_back("now");
228 }
229
231 Triple.isAndroid()) {
232 ExtraOpts.push_back("-z");
233 ExtraOpts.push_back("relro");
234 }
235
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");
241 }
242
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() +
249 "/../bin").str());
250
251 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
252 ExtraOpts.push_back("-X");
253
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();
259
260 if (IsCSKY && !SelectedMultilibs.empty())
261 SysRoot = SysRoot + SelectedMultilibs.back().osSuffix();
262
263 if ((IsMips || IsCSKY) && !SysRoot.empty())
264 ExtraOpts.push_back("--sysroot=" + SysRoot);
265
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) {
275 (IsAndroid && Triple.isAndroidVersionLT(23)))
276 ExtraOpts.push_back("--hash-style=both");
277 else
278 ExtraOpts.push_back("--hash-style=gnu");
279 }
280
281#ifdef ENABLE_LINKER_BUILD_ID
282 ExtraOpts.push_back("--build-id");
283#endif
284
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();
291
292 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
293 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
294
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);
301 }
302 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
303
304 addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths);
305 addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths);
306
307 if (IsAndroid) {
308 // Android sysroots contain a library directory for each supported OS
309 // version as well as some unversioned libraries in the usual multiarch
310 // directory.
311 addPathIfExists(
312 D,
313 concat(SysRoot, "/usr/lib", MultiarchTriple,
314 llvm::to_string(Triple.getEnvironmentVersion().getMajor())),
315 Paths);
316 }
317
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
321 // this here.
322 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
323 Triple.isArch64Bit())
324 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir), Paths);
325 else
326 addPathIfExists(D, concat(SysRoot, "/usr/lib/..", OSLibDir), Paths);
327 if (IsRISCV) {
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);
331 }
332
333 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
334
335 addPathIfExists(D, concat(SysRoot, "/lib"), Paths);
336 addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths);
337}
338
340 if (getTriple().isAndroid())
343}
344
346 if (getTriple().isAndroid())
347 return 4;
349}
350
352 if (getTriple().isAndroid())
355}
356
357bool Linux::HasNativeLLVMSupport() const { return true; }
358
359Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
360
362 return new tools::gnutools::StaticLibTool(*this);
363}
364
366 return new tools::gnutools::Assembler(*this);
367}
368
369std::string Linux::computeSysRoot() const {
370 if (!getDriver().SysRoot.empty())
371 return getDriver().SysRoot;
372
373 if (getTriple().isAndroid()) {
374 // Android toolchains typically include a sysroot at ../sysroot relative to
375 // the clang binary.
376 const StringRef ClangDir = getDriver().getInstalledDir();
377 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
378 if (getVFS().exists(AndroidSysRootPath))
379 return AndroidSysRootPath;
380 }
381
382 if (getTriple().isCSKY()) {
383 // CSKY toolchains use different names for sysroot folder.
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")
391 .str();
392 if (getVFS().exists(Path))
393 return Path;
394 return std::string();
395 }
396
397 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
398 return std::string();
399
400 // Standalone MIPS toolchains use different names for sysroot folder
401 // and put it into different places. Here we try to check some known
402 // variants.
403
404 const StringRef InstallDir = GCCInstallation.getInstallPath();
405 const StringRef TripleStr = GCCInstallation.getTriple().str();
407
408 std::string Path =
409 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
410 .str();
411
412 if (getVFS().exists(Path))
413 return Path;
414
415 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
416
417 if (getVFS().exists(Path))
418 return Path;
419
420 return std::string();
421}
422
423std::string Linux::getDynamicLinker(const ArgList &Args) const {
424 const llvm::Triple::ArchType Arch = getArch();
425 const llvm::Triple &Triple = getTriple();
426
427 const Distro Distro(getDriver().getVFS(), Triple);
428
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
435 // use that instead.
436 return "/system/bin/linker_hwasan64";
437 }
438 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
439 }
440 if (Triple.isMusl()) {
441 std::string ArchName;
442 bool IsArm = false;
443
444 switch (Arch) {
445 case llvm::Triple::arm:
446 case llvm::Triple::thumb:
447 ArchName = "arm";
448 IsArm = true;
449 break;
450 case llvm::Triple::armeb:
451 case llvm::Triple::thumbeb:
452 ArchName = "armeb";
453 IsArm = true;
454 break;
455 case llvm::Triple::x86:
456 ArchName = "i386";
457 break;
458 case llvm::Triple::x86_64:
459 ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str();
460 break;
461 default:
462 ArchName = Triple.getArchName().str();
463 }
464 if (IsArm &&
465 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
467 ArchName += "hf";
468 if (Arch == llvm::Triple::ppc &&
469 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
470 ArchName = "powerpc-sf";
471
472 return "/lib/ld-musl-" + ArchName + ".so.1";
473 }
474
475 std::string LibDir;
476 std::string Loader;
477
478 switch (Arch) {
479 default:
480 llvm_unreachable("unsupported architecture");
481
482 case llvm::Triple::aarch64:
483 LibDir = "lib";
484 Loader = "ld-linux-aarch64.so.1";
485 break;
486 case llvm::Triple::aarch64_be:
487 LibDir = "lib";
488 Loader = "ld-linux-aarch64_be.so.1";
489 break;
490 case llvm::Triple::arm:
491 case llvm::Triple::thumb:
492 case llvm::Triple::armeb:
493 case llvm::Triple::thumbeb: {
494 const bool HF =
495 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
497
498 LibDir = "lib";
499 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
500 break;
501 }
502 case llvm::Triple::loongarch32: {
503 LibDir = "lib32";
504 Loader =
505 ("ld-linux-loongarch-" +
506 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
507 .str();
508 break;
509 }
510 case llvm::Triple::loongarch64: {
511 LibDir = "lib64";
512 Loader =
513 ("ld-linux-loongarch-" +
514 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
515 .str();
516 break;
517 }
518 case llvm::Triple::m68k:
519 LibDir = "lib";
520 Loader = "ld.so.1";
521 break;
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);
527
528 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
529
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)
534 Loader =
535 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
536 else
537 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
538
539 break;
540 }
541 case llvm::Triple::ppc:
542 LibDir = "lib";
543 Loader = "ld.so.1";
544 break;
545 case llvm::Triple::ppcle:
546 LibDir = "lib";
547 Loader = "ld.so.1";
548 break;
549 case llvm::Triple::ppc64:
550 LibDir = "lib64";
551 Loader =
552 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
553 break;
554 case llvm::Triple::ppc64le:
555 LibDir = "lib64";
556 Loader =
557 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
558 break;
559 case llvm::Triple::riscv32: {
560 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
561 LibDir = "lib";
562 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
563 break;
564 }
565 case llvm::Triple::riscv64: {
566 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
567 LibDir = "lib";
568 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
569 break;
570 }
571 case llvm::Triple::sparc:
572 case llvm::Triple::sparcel:
573 LibDir = "lib";
574 Loader = "ld-linux.so.2";
575 break;
576 case llvm::Triple::sparcv9:
577 LibDir = "lib64";
578 Loader = "ld-linux.so.2";
579 break;
580 case llvm::Triple::systemz:
581 LibDir = "lib";
582 Loader = "ld64.so.1";
583 break;
584 case llvm::Triple::x86:
585 LibDir = "lib";
586 Loader = "ld-linux.so.2";
587 break;
588 case llvm::Triple::x86_64: {
589 bool X32 = Triple.isX32();
590
591 LibDir = X32 ? "libx32" : "lib64";
592 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
593 break;
594 }
595 case llvm::Triple::ve:
596 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
597 case llvm::Triple::csky: {
598 LibDir = "lib";
599 Loader = "ld.so.1";
600 break;
601 }
602 }
603
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;
609}
610
611void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
612 ArgStringList &CC1Args) const {
613 const Driver &D = getDriver();
614 std::string SysRoot = computeSysRoot();
615
616 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
617 return;
618
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);
628
629 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
630 return;
631
632 // LOCAL_INCLUDE_DIR
633 addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include"));
634 // TOOL_INCLUDE_DIR
635 AddMultilibIncludeArgs(DriverArgs, CC1Args);
636
637 // Check for configure-time C include directories.
638 StringRef CIncludeDirs(C_INCLUDE_DIRS);
639 if (CIncludeDirs != "") {
641 CIncludeDirs.split(dirs, ":");
642 for (StringRef dir : dirs) {
643 StringRef Prefix =
644 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
645 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
646 }
647 return;
648 }
649
650 // On systems using multiarch and Android, add /usr/include/$triple before
651 // /usr/include.
652 std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
653 if (!MultiarchIncludeDir.empty() &&
654 D.getVFS().exists(concat(SysRoot, "/usr/include", MultiarchIncludeDir)))
656 DriverArgs, CC1Args,
657 concat(SysRoot, "/usr/include", MultiarchIncludeDir));
658
659 if (getTriple().getOS() == llvm::Triple::RTEMS)
660 return;
661
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"));
666
667 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include"));
668
669 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
670 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
671}
672
673void 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.
678 return;
679
680 // Detect Debian g++-multiarch-incdir.diff.
681 StringRef TripleStr = GCCInstallation.getTriple().str();
682 StringRef DebianMultiarch =
683 GCCInstallation.getTriple().getArch() == llvm::Triple::x86
684 ? "i386-linux-gnu"
685 : TripleStr;
686
687 // Try generic GCC detection first.
688 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
689 DebianMultiarch))
690 return;
691
692 StringRef LibDir = GCCInstallation.getParentLibPath();
694 const GCCVersion &Version = GCCInstallation.getVersion();
695
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
703 // version suffix.
704 LibDir.str() + "/../include/g++",
705 };
706
707 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
708 if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
709 Multilib.includeSuffix(), DriverArgs, CC1Args))
710 break;
711 }
712}
713
714void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
715 ArgStringList &CC1Args) const {
716 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
717}
718
719void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
720 ArgStringList &CC1Args) const {
721 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
722}
723
724void Linux::AddHIPRuntimeLibArgs(const ArgList &Args,
725 ArgStringList &CmdArgs) const {
726 CmdArgs.push_back(
727 Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath()));
728
729 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
730 options::OPT_fno_rtlib_add_rpath, false))
731 CmdArgs.append(
732 {"-rpath", Args.MakeArgString(RocmInstallation->getLibPath())});
733
734 CmdArgs.push_back("-lamdhip64");
735}
736
737void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
738 ArgStringList &CC1Args) const {
739 if (GCCInstallation.isValid()) {
740 CC1Args.push_back("-isystem");
741 CC1Args.push_back(DriverArgs.MakeArgString(
743 GCCInstallation.getTriple().str() + "/include"));
744 }
745}
746
747bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
748 return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
749 getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
750}
751
752bool 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!");
757 if (RtLib == ToolChain::RLT_CompilerRT)
758 return true;
759 assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
761 return false;
762 return true;
763}
764
766 if (getTriple().isAndroid() || getTriple().isMusl())
767 return false;
769}
770
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;
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)
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;
813 }
814 if (IsX86_64 || IsAArch64) {
815 Res |= SanitizerKind::KernelHWAddress;
816 }
817 // Work around "Cannot represent a difference across sections".
818 if (getTriple().getArch() == llvm::Triple::ppc64)
819 Res &= ~SanitizerKind::Function;
820 return Res;
821}
822
823void 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);
831}
832
833llvm::DenormalMode
834Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
835 const JobAction &JA,
836 const llvm::fltSemantics *FPType) const {
837 switch (getTriple().getArch()) {
838 case llvm::Triple::x86:
839 case llvm::Triple::x86_64: {
840 std::string Unused;
841 // DAZ and FTZ are turned on in crtfastmath.o
842 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
844 return llvm::DenormalMode::getPreserveSign();
845 return llvm::DenormalMode::getIEEE();
846 }
847 default:
848 return llvm::DenormalMode::getIEEE();
849 }
850}
851
852void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
853 for (const auto &Opt : ExtraOpts)
854 CmdArgs.push_back(Opt.c_str());
855}
856
857const char *Linux::getDefaultLinker() const {
858 if (getTriple().isAndroid())
859 return "ld.lld";
861}
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Definition: Hurd.cpp:47
Distro - Helper class for detecting and classifying Linux distributions.
Definition: Distro.h:23
bool IsOpenSUSE() const
Definition: Distro.h:125
bool IsAlpineLinux() const
Definition: Distro.h:135
bool IsUbuntu() const
Definition: Distro.h:131
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
std::string SysRoot
sysroot, if present
Definition: Driver.h:183
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:167
const char * getInstalledDir() const
Get the path to where the clang executable was installed.
Definition: Driver.h:417
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:392
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:32
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:56
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:60
virtual bool isFastMathRuntimeAvailable(const llvm::opt::ArgList &Args, std::string &Path) const
If a runtime library exists that sets global flags for unsafe floating point math,...
Definition: ToolChain.cpp:1272
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1133
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1045
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
Definition: ToolChain.cpp:1148
path_list & getFilePaths()
Definition: ToolChain.h:287
static bool needsProfileRT(const llvm::opt::ArgList &Args)
needsProfileRT - returns true if instrumentation profile is on.
Definition: ToolChain.cpp:794
StringRef getOS() const
Definition: ToolChain.h:264
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:261
const Driver & getDriver() const
Definition: ToolChain.h:245
static std::string concat(StringRef Path, const Twine &A, const Twine &B="", const Twine &C="", const Twine &D="")
Definition: ToolChain.cpp:1172
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:132
path_list & getProgramPaths()
Definition: ToolChain.h:290
virtual bool IsMathErrnoDefault() const
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition: ToolChain.h:449
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition: ToolChain.h:483
const llvm::Triple & getTriple() const
Definition: ToolChain.h:247
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:1037
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: ToolChain.h:486
SanitizerArgs getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const
Definition: ToolChain.cpp:274
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:194
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1308
virtual unsigned GetDefaultDwarfVersion() const
Definition: ToolChain.cpp:430
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2077
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:246
void AddMultilibPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, const std::string &MultiarchTriple, path_list &Paths)
Definition: Gnu.cpp:2987
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:3193
LazyDetector< CudaInstallationDetector > CudaInstallation
Definition: Gnu.h:290
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:2972
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:289
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3068
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:3053
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3159
LazyDetector< RocmInstallationDetector > RocmInstallation
Definition: Gnu.h:291
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: Linux.cpp:747
std::vector< std::string > ExtraOpts
Definition: Linux.h:60
const char * getDefaultLinker() const override
GetDefaultLinker - Get the default linker to use.
Definition: Linux.cpp:857
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: Linux.cpp:339
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Linux.cpp:357
llvm::DenormalMode getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs, const JobAction &JA, const llvm::fltSemantics *FPType=nullptr) const override
Returns the output denormal handling type in the default floating point environment for the given FPT...
Definition: Linux.cpp:834
bool IsMathErrnoDefault() const override
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition: Linux.cpp:765
Tool * buildAssembler() const override
Definition: Linux.cpp:365
std::string computeSysRoot() const override
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: Linux.cpp:369
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Linux.cpp:611
Linux(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Linux.cpp:212
unsigned GetDefaultDwarfVersion() const override
Definition: Linux.cpp:345
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Linux.cpp:771
bool IsAArch64OutlineAtomicsDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain supports outline atomics by default.
Definition: Linux.cpp:752
void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: Linux.cpp:823
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: Linux.cpp:351
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific HIP includes.
Definition: Linux.cpp:719
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Linux.cpp:423
Tool * buildStaticLibTool() const override
Definition: Linux.cpp:361
void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Add the system specific linker arguments to use for the given HIP runtime library type.
Definition: Linux.cpp:724
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Linux.cpp:673
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Linux.cpp:852
std::string getMultiarchTriple(const Driver &D, const llvm::Triple &TargetTriple, StringRef SysRoot) const override
Get our best guess at the multiarch triple for a target.
Definition: Linux.cpp:41
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use MCU GCC toolchain includes.
Definition: Linux.cpp:737
Tool * buildLinker() const override
Definition: Linux.cpp:359
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: Linux.cpp:714
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
StringRef getLoongArchABI(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
bool isNaN2008(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
bool hasMipsAbiArg(const llvm::opt::ArgList &Args, const char *Value)
std::string getMipsABILibSuffix(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
bool isUCLibc(const llvm::opt::ArgList &Args)
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
bool hasPPCAbiArg(const llvm::opt::ArgList &Args, const char *Value)
StringRef getRISCVABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:150
Struct to store and manipulate GCC versions.
Definition: Gnu.h:162
bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch, StringRef RHSPatchSuffix=StringRef()) const
Generic_GCC - A tool chain using the 'gcc' command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.cpp:1962
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164