clang 19.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 TargetEnvironment == llvm::Triple::MuslEABIHF ||
66 TargetEnvironment == llvm::Triple::EABIHF)
67 return "arm-linux-gnueabihf";
68 return "arm-linux-gnueabi";
69 case llvm::Triple::armeb:
70 case llvm::Triple::thumbeb:
71 if (TargetEnvironment == llvm::Triple::GNUEABIHF ||
72 TargetEnvironment == llvm::Triple::MuslEABIHF ||
73 TargetEnvironment == llvm::Triple::EABIHF)
74 return "armeb-linux-gnueabihf";
75 return "armeb-linux-gnueabi";
76 case llvm::Triple::x86:
77 if (IsAndroid)
78 return "i686-linux-android";
79 return "i386-linux-gnu";
80 case llvm::Triple::x86_64:
81 if (IsAndroid)
82 return "x86_64-linux-android";
83 if (TargetEnvironment == llvm::Triple::GNUX32)
84 return "x86_64-linux-gnux32";
85 return "x86_64-linux-gnu";
86 case llvm::Triple::aarch64:
87 if (IsAndroid)
88 return "aarch64-linux-android";
89 return "aarch64-linux-gnu";
90 case llvm::Triple::aarch64_be:
91 return "aarch64_be-linux-gnu";
92
93 case llvm::Triple::loongarch64: {
94 const char *Libc;
95 const char *FPFlavor;
96
97 if (TargetTriple.isGNUEnvironment()) {
98 Libc = "gnu";
99 } else if (TargetTriple.isMusl()) {
100 Libc = "musl";
101 } else {
102 return TargetTriple.str();
103 }
104
105 switch (TargetEnvironment) {
106 default:
107 return TargetTriple.str();
108 case llvm::Triple::GNUSF:
109 FPFlavor = "sf";
110 break;
111 case llvm::Triple::GNUF32:
112 FPFlavor = "f32";
113 break;
114 case llvm::Triple::GNU:
115 case llvm::Triple::GNUF64:
116 // This was going to be "f64" in an earlier Toolchain Conventions
117 // revision, but starting from Feb 2023 the F64 ABI variants are
118 // unmarked in their canonical forms.
119 FPFlavor = "";
120 break;
121 }
122
123 return (Twine("loongarch64-linux-") + Libc + FPFlavor).str();
124 }
125
126 case llvm::Triple::m68k:
127 return "m68k-linux-gnu";
128
129 case llvm::Triple::mips:
130 return IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu";
131 case llvm::Triple::mipsel:
132 return IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu";
133 case llvm::Triple::mips64: {
134 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") +
135 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
136 if (D.getVFS().exists(concat(SysRoot, "/lib", MT)))
137 return MT;
138 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64-linux-gnu")))
139 return "mips64-linux-gnu";
140 break;
141 }
142 case llvm::Triple::mips64el: {
143 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") +
144 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
145 if (D.getVFS().exists(concat(SysRoot, "/lib", MT)))
146 return MT;
147 if (D.getVFS().exists(concat(SysRoot, "/lib/mips64el-linux-gnu")))
148 return "mips64el-linux-gnu";
149 break;
150 }
151 case llvm::Triple::ppc:
152 if (D.getVFS().exists(concat(SysRoot, "/lib/powerpc-linux-gnuspe")))
153 return "powerpc-linux-gnuspe";
154 return "powerpc-linux-gnu";
155 case llvm::Triple::ppcle:
156 return "powerpcle-linux-gnu";
157 case llvm::Triple::ppc64:
158 return "powerpc64-linux-gnu";
159 case llvm::Triple::ppc64le:
160 return "powerpc64le-linux-gnu";
161 case llvm::Triple::riscv64:
162 if (IsAndroid)
163 return "riscv64-linux-android";
164 return "riscv64-linux-gnu";
165 case llvm::Triple::sparc:
166 return "sparc-linux-gnu";
167 case llvm::Triple::sparcv9:
168 return "sparc64-linux-gnu";
169 case llvm::Triple::systemz:
170 return "s390x-linux-gnu";
171 }
172 return TargetTriple.str();
173}
174
175static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
176 if (Triple.isMIPS()) {
177 if (Triple.isAndroid()) {
178 StringRef CPUName;
179 StringRef ABIName;
180 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
181 if (CPUName == "mips32r6")
182 return "libr6";
183 if (CPUName == "mips32r2")
184 return "libr2";
185 }
186 // lib32 directory has a special meaning on MIPS targets.
187 // It contains N32 ABI binaries. Use this folder if produce
188 // code for N32 ABI only.
189 if (tools::mips::hasMipsAbiArg(Args, "n32"))
190 return "lib32";
191 return Triple.isArch32Bit() ? "lib" : "lib64";
192 }
193
194 // It happens that only x86, PPC and SPARC use the 'lib32' variant of
195 // oslibdir, and using that variant while targeting other architectures causes
196 // problems because the libraries are laid out in shared system roots that
197 // can't cope with a 'lib32' library search path being considered. So we only
198 // enable them when we know we may need it.
199 //
200 // FIXME: This is a bit of a hack. We should really unify this code for
201 // reasoning about oslibdir spellings with the lib dir spellings in the
202 // GCCInstallationDetector, but that is a more significant refactoring.
203 if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() ||
204 Triple.getArch() == llvm::Triple::sparc)
205 return "lib32";
206
207 if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32())
208 return "libx32";
209
210 if (Triple.getArch() == llvm::Triple::riscv32)
211 return "lib32";
212
213 return Triple.isArch32Bit() ? "lib" : "lib64";
214}
215
216Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
217 : Generic_ELF(D, Triple, Args) {
218 GCCInstallation.init(Triple, Args);
221 llvm::Triple::ArchType Arch = Triple.getArch();
222 std::string SysRoot = computeSysRoot();
224
226
227 Distro Distro(D.getVFS(), Triple);
228
229 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
230 ExtraOpts.push_back("-z");
231 ExtraOpts.push_back("now");
232 }
233
235 Triple.isAndroid()) {
236 ExtraOpts.push_back("-z");
237 ExtraOpts.push_back("relro");
238 }
239
240 // Note, lld from 11 onwards default max-page-size to 65536 for both ARM and
241 // AArch64.
242 if (Triple.isAndroid()) {
243 if (Triple.isARM()) {
244 // Android ARM uses max-page-size=4096 to reduce VMA usage.
245 ExtraOpts.push_back("-z");
246 ExtraOpts.push_back("max-page-size=4096");
247 } else if (Triple.isAArch64()) {
248 // Android AArch64 uses max-page-size=16384 to support 4k/16k page sizes.
249 ExtraOpts.push_back("-z");
250 ExtraOpts.push_back("max-page-size=16384");
251 }
252 }
253
254 if (GCCInstallation.getParentLibPath().contains("opt/rh/"))
255 // With devtoolset on RHEL, we want to add a bin directory that is relative
256 // to the detected gcc install, because if we are using devtoolset gcc then
257 // we want to use other tools from devtoolset (e.g. ld) instead of the
258 // standard system tools.
259 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
260 "/../bin").str());
261
262 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
263 ExtraOpts.push_back("-X");
264
265 const bool IsAndroid = Triple.isAndroid();
266 const bool IsMips = Triple.isMIPS();
267 const bool IsHexagon = Arch == llvm::Triple::hexagon;
268 const bool IsRISCV = Triple.isRISCV();
269 const bool IsCSKY = Triple.isCSKY();
270
271 if (IsCSKY && !SelectedMultilibs.empty())
272 SysRoot = SysRoot + SelectedMultilibs.back().osSuffix();
273
274 if ((IsMips || IsCSKY) && !SysRoot.empty())
275 ExtraOpts.push_back("--sysroot=" + SysRoot);
276
277 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
278 // and the MIPS ABI require .dynsym to be sorted in different ways.
279 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
280 // ABI requires a mapping between the GOT and the symbol table.
281 // Android loader does not support .gnu.hash until API 23.
282 // Hexagon linker/loader does not support .gnu.hash
283 if (!IsMips && !IsHexagon) {
286 (IsAndroid && Triple.isAndroidVersionLT(23)))
287 ExtraOpts.push_back("--hash-style=both");
288 else
289 ExtraOpts.push_back("--hash-style=gnu");
290 }
291
292#ifdef ENABLE_LINKER_BUILD_ID
293 ExtraOpts.push_back("--build-id");
294#endif
295
296 // The selection of paths to try here is designed to match the patterns which
297 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
298 // This was determined by running GCC in a fake filesystem, creating all
299 // possible permutations of these directories, and seeing which ones it added
300 // to the link paths.
301 path_list &Paths = getFilePaths();
302
303 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
304 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
305
306 // mips32: Debian multilib, we use /libo32, while in other case, /lib is
307 // used. We need add both libo32 and /lib.
308 if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) {
309 Generic_GCC::AddMultilibPaths(D, SysRoot, "libo32", MultiarchTriple, Paths);
310 addPathIfExists(D, concat(SysRoot, "/libo32"), Paths);
311 addPathIfExists(D, concat(SysRoot, "/usr/libo32"), Paths);
312 }
313 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
314
315 addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths);
316 addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths);
317
318 if (IsAndroid) {
319 // Android sysroots contain a library directory for each supported OS
320 // version as well as some unversioned libraries in the usual multiarch
321 // directory.
322 addPathIfExists(
323 D,
324 concat(SysRoot, "/usr/lib", MultiarchTriple,
325 llvm::to_string(Triple.getEnvironmentVersion().getMajor())),
326 Paths);
327 }
328
329 addPathIfExists(D, concat(SysRoot, "/usr/lib", MultiarchTriple), Paths);
330 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
331 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
332 // this here.
333 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
334 Triple.isArch64Bit())
335 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir), Paths);
336 else
337 addPathIfExists(D, concat(SysRoot, "/usr/lib/..", OSLibDir), Paths);
338 if (IsRISCV) {
339 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
340 addPathIfExists(D, concat(SysRoot, "/", OSLibDir, ABIName), Paths);
341 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir, ABIName), Paths);
342 }
343
344 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
345
346 addPathIfExists(D, concat(SysRoot, "/lib"), Paths);
347 addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths);
348}
349
351 if (getTriple().isAndroid())
354}
355
357 if (getTriple().isAndroid())
358 return 4;
360}
361
363 if (getTriple().isAndroid())
366}
367
368bool Linux::HasNativeLLVMSupport() const { return true; }
369
370Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
371
373 return new tools::gnutools::StaticLibTool(*this);
374}
375
377 return new tools::gnutools::Assembler(*this);
378}
379
380std::string Linux::computeSysRoot() const {
381 if (!getDriver().SysRoot.empty())
382 return getDriver().SysRoot;
383
384 if (getTriple().isAndroid()) {
385 // Android toolchains typically include a sysroot at ../sysroot relative to
386 // the clang binary.
387 const StringRef ClangDir = getDriver().getInstalledDir();
388 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
389 if (getVFS().exists(AndroidSysRootPath))
390 return AndroidSysRootPath;
391 }
392
393 if (getTriple().isCSKY()) {
394 // CSKY toolchains use different names for sysroot folder.
396 return std::string();
397 // GCCInstallation.getInstallPath() =
398 // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0
399 // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc
400 std::string Path = (GCCInstallation.getInstallPath() + "/../../../../" +
401 GCCInstallation.getTriple().str() + "/libc")
402 .str();
403 if (getVFS().exists(Path))
404 return Path;
405 return std::string();
406 }
407
408 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
409 return std::string();
410
411 // Standalone MIPS toolchains use different names for sysroot folder
412 // and put it into different places. Here we try to check some known
413 // variants.
414
415 const StringRef InstallDir = GCCInstallation.getInstallPath();
416 const StringRef TripleStr = GCCInstallation.getTriple().str();
418
419 std::string Path =
420 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
421 .str();
422
423 if (getVFS().exists(Path))
424 return Path;
425
426 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
427
428 if (getVFS().exists(Path))
429 return Path;
430
431 return std::string();
432}
433
434std::string Linux::getDynamicLinker(const ArgList &Args) const {
435 const llvm::Triple::ArchType Arch = getArch();
436 const llvm::Triple &Triple = getTriple();
437
438 const Distro Distro(getDriver().getVFS(), Triple);
439
440 if (Triple.isAndroid()) {
441 if (getSanitizerArgs(Args).needsHwasanRt() &&
442 !Triple.isAndroidVersionLT(34) && Triple.isArch64Bit()) {
443 // On Android 14 and newer, there is a special linker_hwasan64 that
444 // allows to run HWASan binaries on non-HWASan system images. This
445 // is also available on HWASan system images, so we can just always
446 // use that instead.
447 return "/system/bin/linker_hwasan64";
448 }
449 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
450 }
451 if (Triple.isMusl()) {
452 std::string ArchName;
453 bool IsArm = false;
454
455 switch (Arch) {
456 case llvm::Triple::arm:
457 case llvm::Triple::thumb:
458 ArchName = "arm";
459 IsArm = true;
460 break;
461 case llvm::Triple::armeb:
462 case llvm::Triple::thumbeb:
463 ArchName = "armeb";
464 IsArm = true;
465 break;
466 case llvm::Triple::x86:
467 ArchName = "i386";
468 break;
469 case llvm::Triple::x86_64:
470 ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str();
471 break;
472 default:
473 ArchName = Triple.getArchName().str();
474 }
475 if (IsArm &&
476 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
478 ArchName += "hf";
479 if (Arch == llvm::Triple::ppc &&
480 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
481 ArchName = "powerpc-sf";
482
483 return "/lib/ld-musl-" + ArchName + ".so.1";
484 }
485
486 std::string LibDir;
487 std::string Loader;
488
489 switch (Arch) {
490 default:
491 llvm_unreachable("unsupported architecture");
492
493 case llvm::Triple::aarch64:
494 LibDir = "lib";
495 Loader = "ld-linux-aarch64.so.1";
496 break;
497 case llvm::Triple::aarch64_be:
498 LibDir = "lib";
499 Loader = "ld-linux-aarch64_be.so.1";
500 break;
501 case llvm::Triple::arm:
502 case llvm::Triple::thumb:
503 case llvm::Triple::armeb:
504 case llvm::Triple::thumbeb: {
505 const bool HF =
506 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
508
509 LibDir = "lib";
510 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
511 break;
512 }
513 case llvm::Triple::loongarch32: {
514 LibDir = "lib32";
515 Loader =
516 ("ld-linux-loongarch-" +
517 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
518 .str();
519 break;
520 }
521 case llvm::Triple::loongarch64: {
522 LibDir = "lib64";
523 Loader =
524 ("ld-linux-loongarch-" +
525 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
526 .str();
527 break;
528 }
529 case llvm::Triple::m68k:
530 LibDir = "lib";
531 Loader = "ld.so.1";
532 break;
533 case llvm::Triple::mips:
534 case llvm::Triple::mipsel:
535 case llvm::Triple::mips64:
536 case llvm::Triple::mips64el: {
537 bool IsNaN2008 = tools::mips::isNaN2008(getDriver(), Args, Triple);
538
539 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
540
541 if (tools::mips::isUCLibc(Args))
542 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
543 else if (!Triple.hasEnvironment() &&
544 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
545 Loader =
546 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
547 else
548 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
549
550 break;
551 }
552 case llvm::Triple::ppc:
553 LibDir = "lib";
554 Loader = "ld.so.1";
555 break;
556 case llvm::Triple::ppcle:
557 LibDir = "lib";
558 Loader = "ld.so.1";
559 break;
560 case llvm::Triple::ppc64:
561 LibDir = "lib64";
562 Loader =
563 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
564 break;
565 case llvm::Triple::ppc64le:
566 LibDir = "lib64";
567 Loader =
568 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
569 break;
570 case llvm::Triple::riscv32: {
571 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
572 LibDir = "lib";
573 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
574 break;
575 }
576 case llvm::Triple::riscv64: {
577 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
578 LibDir = "lib";
579 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
580 break;
581 }
582 case llvm::Triple::sparc:
583 case llvm::Triple::sparcel:
584 LibDir = "lib";
585 Loader = "ld-linux.so.2";
586 break;
587 case llvm::Triple::sparcv9:
588 LibDir = "lib64";
589 Loader = "ld-linux.so.2";
590 break;
591 case llvm::Triple::systemz:
592 LibDir = "lib";
593 Loader = "ld64.so.1";
594 break;
595 case llvm::Triple::x86:
596 LibDir = "lib";
597 Loader = "ld-linux.so.2";
598 break;
599 case llvm::Triple::x86_64: {
600 bool X32 = Triple.isX32();
601
602 LibDir = X32 ? "libx32" : "lib64";
603 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
604 break;
605 }
606 case llvm::Triple::ve:
607 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
608 case llvm::Triple::csky: {
609 LibDir = "lib";
610 Loader = "ld.so.1";
611 break;
612 }
613 }
614
615 if (Distro == Distro::Exherbo &&
616 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
617 Triple.getVendor() == llvm::Triple::PC))
618 return "/usr/" + Triple.str() + "/lib/" + Loader;
619 return "/" + LibDir + "/" + Loader;
620}
621
622void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
623 ArgStringList &CC1Args) const {
624 const Driver &D = getDriver();
625 std::string SysRoot = computeSysRoot();
626
627 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
628 return;
629
630 // Add 'include' in the resource directory, which is similar to
631 // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
632 // contains some files conflicting with system /usr/include. musl systems
633 // prefer the /usr/include copies which are more relevant.
634 SmallString<128> ResourceDirInclude(D.ResourceDir);
635 llvm::sys::path::append(ResourceDirInclude, "include");
636 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
637 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
638 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
639
640 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
641 return;
642
643 // LOCAL_INCLUDE_DIR
644 addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include"));
645 // TOOL_INCLUDE_DIR
646 AddMultilibIncludeArgs(DriverArgs, CC1Args);
647
648 // Check for configure-time C include directories.
649 StringRef CIncludeDirs(C_INCLUDE_DIRS);
650 if (CIncludeDirs != "") {
652 CIncludeDirs.split(dirs, ":");
653 for (StringRef dir : dirs) {
654 StringRef Prefix =
655 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
656 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
657 }
658 return;
659 }
660
661 // On systems using multiarch and Android, add /usr/include/$triple before
662 // /usr/include.
663 std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
664 if (!MultiarchIncludeDir.empty() &&
665 D.getVFS().exists(concat(SysRoot, "/usr/include", MultiarchIncludeDir)))
667 DriverArgs, CC1Args,
668 concat(SysRoot, "/usr/include", MultiarchIncludeDir));
669
670 if (getTriple().getOS() == llvm::Triple::RTEMS)
671 return;
672
673 // Add an include of '/include' directly. This isn't provided by default by
674 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
675 // add even when Clang is acting as-if it were a system compiler.
676 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
677
678 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include"));
679
680 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
681 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
682}
683
684void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
685 llvm::opt::ArgStringList &CC1Args) const {
686 // We need a detected GCC installation on Linux to provide libstdc++'s
687 // headers in odd Linuxish places.
689 return;
690
691 // Detect Debian g++-multiarch-incdir.diff.
692 StringRef TripleStr = GCCInstallation.getTriple().str();
693 StringRef DebianMultiarch =
694 GCCInstallation.getTriple().getArch() == llvm::Triple::x86
695 ? "i386-linux-gnu"
696 : TripleStr;
697
698 // Try generic GCC detection first.
699 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
700 DebianMultiarch))
701 return;
702
703 StringRef LibDir = GCCInstallation.getParentLibPath();
705 const GCCVersion &Version = GCCInstallation.getVersion();
706
707 const std::string LibStdCXXIncludePathCandidates[] = {
708 // Android standalone toolchain has C++ headers in yet another place.
709 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
710 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
711 // without a subdirectory corresponding to the gcc version.
712 LibDir.str() + "/../include/c++",
713 // Cray's gcc installation puts headers under "g++" without a
714 // version suffix.
715 LibDir.str() + "/../include/g++",
716 };
717
718 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
719 if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
720 Multilib.includeSuffix(), DriverArgs, CC1Args))
721 break;
722 }
723}
724
725void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
726 ArgStringList &CC1Args) const {
727 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
728}
729
730void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
731 ArgStringList &CC1Args) const {
732 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
733}
734
735void Linux::AddHIPRuntimeLibArgs(const ArgList &Args,
736 ArgStringList &CmdArgs) const {
737 CmdArgs.push_back(
738 Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath()));
739
740 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
741 options::OPT_fno_rtlib_add_rpath, false))
742 CmdArgs.append(
743 {"-rpath", Args.MakeArgString(RocmInstallation->getLibPath())});
744
745 CmdArgs.push_back("-lamdhip64");
746}
747
748void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
749 ArgStringList &CC1Args) const {
750 if (GCCInstallation.isValid()) {
751 CC1Args.push_back("-isystem");
752 CC1Args.push_back(DriverArgs.MakeArgString(
754 GCCInstallation.getTriple().str() + "/include"));
755 }
756}
757
758bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
759 return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
760 getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
761}
762
763bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
764 // Outline atomics for AArch64 are supported by compiler-rt
765 // and libgcc since 9.3.1
766 assert(getTriple().isAArch64() && "expected AArch64 target!");
768 if (RtLib == ToolChain::RLT_CompilerRT)
769 return true;
770 assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
772 return false;
773 return true;
774}
775
777 if (getTriple().isAndroid() || getTriple().isMusl())
778 return false;
780}
781
783 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
784 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
785 const bool IsMIPS = getTriple().isMIPS32();
786 const bool IsMIPS64 = getTriple().isMIPS64();
787 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
788 getTriple().getArch() == llvm::Triple::ppc64le;
789 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
790 getTriple().getArch() == llvm::Triple::aarch64_be;
791 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
792 getTriple().getArch() == llvm::Triple::thumb ||
793 getTriple().getArch() == llvm::Triple::armeb ||
794 getTriple().getArch() == llvm::Triple::thumbeb;
795 const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64;
796 const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
797 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
798 const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon;
800 Res |= SanitizerKind::Address;
801 Res |= SanitizerKind::PointerCompare;
802 Res |= SanitizerKind::PointerSubtract;
803 Res |= SanitizerKind::Fuzzer;
804 Res |= SanitizerKind::FuzzerNoLink;
805 Res |= SanitizerKind::KernelAddress;
806 Res |= SanitizerKind::Memory;
807 Res |= SanitizerKind::Vptr;
808 Res |= SanitizerKind::SafeStack;
809 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64)
810 Res |= SanitizerKind::DataFlow;
811 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
812 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64)
813 Res |= SanitizerKind::Leak;
814 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
815 IsLoongArch64 || IsRISCV64)
816 Res |= SanitizerKind::Thread;
817 if (IsX86_64 || IsSystemZ)
818 Res |= SanitizerKind::KernelMemory;
819 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
820 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64)
821 Res |= SanitizerKind::Scudo;
822 if (IsX86_64 || IsAArch64 || IsRISCV64) {
823 Res |= SanitizerKind::HWAddress;
824 }
825 if (IsX86_64 || IsAArch64) {
826 Res |= SanitizerKind::KernelHWAddress;
827 }
828 // Work around "Cannot represent a difference across sections".
829 if (getTriple().getArch() == llvm::Triple::ppc64)
831 return Res;
832}
833
834void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
835 llvm::opt::ArgStringList &CmdArgs) const {
836 // Add linker option -u__llvm_profile_runtime to cause runtime
837 // initialization module to be linked in.
838 if (needsProfileRT(Args))
839 CmdArgs.push_back(Args.MakeArgString(
840 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
841 ToolChain::addProfileRTLibs(Args, CmdArgs);
842}
843
844llvm::DenormalMode
845Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
846 const JobAction &JA,
847 const llvm::fltSemantics *FPType) const {
848 switch (getTriple().getArch()) {
849 case llvm::Triple::x86:
850 case llvm::Triple::x86_64: {
851 std::string Unused;
852 // DAZ and FTZ are turned on in crtfastmath.o
853 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
855 return llvm::DenormalMode::getPreserveSign();
856 return llvm::DenormalMode::getIEEE();
857 }
858 default:
859 return llvm::DenormalMode::getIEEE();
860 }
861}
862
863void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
864 for (const auto &Opt : ExtraOpts)
865 CmdArgs.push_back(Opt.c_str());
866}
867
868const char *Linux::getDefaultLinker() const {
869 if (getTriple().isAndroid())
870 return "ld.lld";
872}
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Definition: Hurd.cpp:55
Distro - Helper class for detecting and classifying Linux distributions.
Definition: Distro.h:23
bool IsOpenSUSE() const
Definition: Distro.h:126
bool IsAlpineLinux() const
Definition: Distro.h:136
bool IsUbuntu() const
Definition: Distro.h:132
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:431
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:406
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:65
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:69
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:1308
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:1169
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1081
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:1184
path_list & getFilePaths()
Definition: ToolChain.h:294
static bool needsProfileRT(const llvm::opt::ArgList &Args)
needsProfileRT - returns true if instrumentation profile is on.
Definition: ToolChain.cpp:830
StringRef getOS() const
Definition: ToolChain.h:271
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:268
const Driver & getDriver() const
Definition: ToolChain.h:252
static std::string concat(StringRef Path, const Twine &A, const Twine &B="", const Twine &C="", const Twine &D="")
Definition: ToolChain.cpp:1208
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:141
path_list & getProgramPaths()
Definition: ToolChain.h:297
virtual bool IsMathErrnoDefault() const
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition: ToolChain.h:459
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition: ToolChain.h:493
const llvm::Triple & getTriple() const
Definition: ToolChain.h:254
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:1073
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: ToolChain.h:496
SanitizerArgs getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const
Definition: ToolChain.cpp:300
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:201
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1344
virtual unsigned GetDefaultDwarfVersion() const
Definition: ToolChain.cpp:456
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:2220
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:3157
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:3363
LazyDetector< CudaInstallationDetector > CudaInstallation
Definition: Gnu.h:289
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:3142
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3238
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:3223
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3329
LazyDetector< RocmInstallationDetector > RocmInstallation
Definition: Gnu.h:290
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: Linux.cpp:758
std::vector< std::string > ExtraOpts
Definition: Linux.h:60
const char * getDefaultLinker() const override
GetDefaultLinker - Get the default linker to use.
Definition: Linux.cpp:868
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: Linux.cpp:350
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Linux.cpp:368
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:845
bool IsMathErrnoDefault() const override
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition: Linux.cpp:776
Tool * buildAssembler() const override
Definition: Linux.cpp:376
std::string computeSysRoot() const override
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: Linux.cpp:380
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:622
Linux(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Linux.cpp:216
unsigned GetDefaultDwarfVersion() const override
Definition: Linux.cpp:356
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Linux.cpp:782
bool IsAArch64OutlineAtomicsDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain supports outline atomics by default.
Definition: Linux.cpp:763
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:834
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: Linux.cpp:362
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific HIP includes.
Definition: Linux.cpp:730
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Linux.cpp:434
Tool * buildStaticLibTool() const override
Definition: Linux.cpp:372
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:735
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Linux.cpp:684
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Linux.cpp:863
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:748
Tool * buildLinker() const override
Definition: Linux.cpp:370
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: Linux.cpp:725
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:289
The JSON file list parser is used to communicate input to InstallAPI.
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:2078
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164