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() || Triple.getArch() == llvm::Triple::x86_64) {
248 // Android AArch64 uses max-page-size=16384 to support 4k/16k page sizes.
249 // Android emulates a 16k page size for app testing on x86_64 machines.
250 ExtraOpts.push_back("-z");
251 ExtraOpts.push_back("max-page-size=16384");
252 }
253 }
254
255 if (GCCInstallation.getParentLibPath().contains("opt/rh/"))
256 // With devtoolset on RHEL, we want to add a bin directory that is relative
257 // to the detected gcc install, because if we are using devtoolset gcc then
258 // we want to use other tools from devtoolset (e.g. ld) instead of the
259 // standard system tools.
260 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
261 "/../bin").str());
262
263 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
264 ExtraOpts.push_back("-X");
265
266 const bool IsAndroid = Triple.isAndroid();
267 const bool IsMips = Triple.isMIPS();
268 const bool IsHexagon = Arch == llvm::Triple::hexagon;
269 const bool IsRISCV = Triple.isRISCV();
270 const bool IsCSKY = Triple.isCSKY();
271
272 if (IsCSKY && !SelectedMultilibs.empty())
273 SysRoot = SysRoot + SelectedMultilibs.back().osSuffix();
274
275 if ((IsMips || IsCSKY) && !SysRoot.empty())
276 ExtraOpts.push_back("--sysroot=" + SysRoot);
277
278 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
279 // and the MIPS ABI require .dynsym to be sorted in different ways.
280 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
281 // ABI requires a mapping between the GOT and the symbol table.
282 // Android loader does not support .gnu.hash until API 23.
283 // Hexagon linker/loader does not support .gnu.hash
284 if (!IsMips && !IsHexagon) {
287 (IsAndroid && Triple.isAndroidVersionLT(23)))
288 ExtraOpts.push_back("--hash-style=both");
289 else
290 ExtraOpts.push_back("--hash-style=gnu");
291 }
292
293#ifdef ENABLE_LINKER_BUILD_ID
294 ExtraOpts.push_back("--build-id");
295#endif
296
297 // The selection of paths to try here is designed to match the patterns which
298 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
299 // This was determined by running GCC in a fake filesystem, creating all
300 // possible permutations of these directories, and seeing which ones it added
301 // to the link paths.
302 path_list &Paths = getFilePaths();
303
304 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
305 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
306
307 // mips32: Debian multilib, we use /libo32, while in other case, /lib is
308 // used. We need add both libo32 and /lib.
309 if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) {
310 Generic_GCC::AddMultilibPaths(D, SysRoot, "libo32", MultiarchTriple, Paths);
311 addPathIfExists(D, concat(SysRoot, "/libo32"), Paths);
312 addPathIfExists(D, concat(SysRoot, "/usr/libo32"), Paths);
313 }
314 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
315
316 addPathIfExists(D, concat(SysRoot, "/lib", MultiarchTriple), Paths);
317 addPathIfExists(D, concat(SysRoot, "/lib/..", OSLibDir), Paths);
318
319 if (IsAndroid) {
320 // Android sysroots contain a library directory for each supported OS
321 // version as well as some unversioned libraries in the usual multiarch
322 // directory.
323 addPathIfExists(
324 D,
325 concat(SysRoot, "/usr/lib", MultiarchTriple,
326 llvm::to_string(Triple.getEnvironmentVersion().getMajor())),
327 Paths);
328 }
329
330 addPathIfExists(D, concat(SysRoot, "/usr/lib", MultiarchTriple), Paths);
331 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
332 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
333 // this here.
334 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
335 Triple.isArch64Bit())
336 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir), Paths);
337 else
338 addPathIfExists(D, concat(SysRoot, "/usr/lib/..", OSLibDir), Paths);
339 if (IsRISCV) {
340 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
341 addPathIfExists(D, concat(SysRoot, "/", OSLibDir, ABIName), Paths);
342 addPathIfExists(D, concat(SysRoot, "/usr", OSLibDir, ABIName), Paths);
343 }
344
345 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
346
347 addPathIfExists(D, concat(SysRoot, "/lib"), Paths);
348 addPathIfExists(D, concat(SysRoot, "/usr/lib"), Paths);
349}
350
352 if (getTriple().isAndroid())
355}
356
358 if (getTriple().isAndroid())
359 return 4;
361}
362
364 if (getTriple().isAndroid())
367}
368
369bool Linux::HasNativeLLVMSupport() const { return true; }
370
371Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
372
374 return new tools::gnutools::StaticLibTool(*this);
375}
376
378 return new tools::gnutools::Assembler(*this);
379}
380
381std::string Linux::computeSysRoot() const {
382 if (!getDriver().SysRoot.empty())
383 return getDriver().SysRoot;
384
385 if (getTriple().isAndroid()) {
386 // Android toolchains typically include a sysroot at ../sysroot relative to
387 // the clang binary.
388 const StringRef ClangDir = getDriver().Dir;
389 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
390 if (getVFS().exists(AndroidSysRootPath))
391 return AndroidSysRootPath;
392 }
393
394 if (getTriple().isCSKY()) {
395 // CSKY toolchains use different names for sysroot folder.
397 return std::string();
398 // GCCInstallation.getInstallPath() =
399 // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0
400 // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc
401 std::string Path = (GCCInstallation.getInstallPath() + "/../../../../" +
402 GCCInstallation.getTriple().str() + "/libc")
403 .str();
404 if (getVFS().exists(Path))
405 return Path;
406 return std::string();
407 }
408
409 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
410 return std::string();
411
412 // Standalone MIPS toolchains use different names for sysroot folder
413 // and put it into different places. Here we try to check some known
414 // variants.
415
416 const StringRef InstallDir = GCCInstallation.getInstallPath();
417 const StringRef TripleStr = GCCInstallation.getTriple().str();
419
420 std::string Path =
421 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
422 .str();
423
424 if (getVFS().exists(Path))
425 return Path;
426
427 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
428
429 if (getVFS().exists(Path))
430 return Path;
431
432 return std::string();
433}
434
435std::string Linux::getDynamicLinker(const ArgList &Args) const {
436 const llvm::Triple::ArchType Arch = getArch();
437 const llvm::Triple &Triple = getTriple();
438
439 const Distro Distro(getDriver().getVFS(), Triple);
440
441 if (Triple.isAndroid()) {
442 if (getSanitizerArgs(Args).needsHwasanRt() &&
443 !Triple.isAndroidVersionLT(34) && Triple.isArch64Bit()) {
444 // On Android 14 and newer, there is a special linker_hwasan64 that
445 // allows to run HWASan binaries on non-HWASan system images. This
446 // is also available on HWASan system images, so we can just always
447 // use that instead.
448 return "/system/bin/linker_hwasan64";
449 }
450 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
451 }
452 if (Triple.isMusl()) {
453 std::string ArchName;
454 bool IsArm = false;
455
456 switch (Arch) {
457 case llvm::Triple::arm:
458 case llvm::Triple::thumb:
459 ArchName = "arm";
460 IsArm = true;
461 break;
462 case llvm::Triple::armeb:
463 case llvm::Triple::thumbeb:
464 ArchName = "armeb";
465 IsArm = true;
466 break;
467 case llvm::Triple::x86:
468 ArchName = "i386";
469 break;
470 case llvm::Triple::x86_64:
471 ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str();
472 break;
473 default:
474 ArchName = Triple.getArchName().str();
475 }
476 if (IsArm &&
477 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
479 ArchName += "hf";
480 if (Arch == llvm::Triple::ppc &&
481 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
482 ArchName = "powerpc-sf";
483
484 return "/lib/ld-musl-" + ArchName + ".so.1";
485 }
486
487 std::string LibDir;
488 std::string Loader;
489
490 switch (Arch) {
491 default:
492 llvm_unreachable("unsupported architecture");
493
494 case llvm::Triple::aarch64:
495 LibDir = "lib";
496 Loader = "ld-linux-aarch64.so.1";
497 break;
498 case llvm::Triple::aarch64_be:
499 LibDir = "lib";
500 Loader = "ld-linux-aarch64_be.so.1";
501 break;
502 case llvm::Triple::arm:
503 case llvm::Triple::thumb:
504 case llvm::Triple::armeb:
505 case llvm::Triple::thumbeb: {
506 const bool HF =
507 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
509
510 LibDir = "lib";
511 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
512 break;
513 }
514 case llvm::Triple::loongarch32: {
515 LibDir = "lib32";
516 Loader =
517 ("ld-linux-loongarch-" +
518 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
519 .str();
520 break;
521 }
522 case llvm::Triple::loongarch64: {
523 LibDir = "lib64";
524 Loader =
525 ("ld-linux-loongarch-" +
526 tools::loongarch::getLoongArchABI(getDriver(), Args, Triple) + ".so.1")
527 .str();
528 break;
529 }
530 case llvm::Triple::m68k:
531 LibDir = "lib";
532 Loader = "ld.so.1";
533 break;
534 case llvm::Triple::mips:
535 case llvm::Triple::mipsel:
536 case llvm::Triple::mips64:
537 case llvm::Triple::mips64el: {
538 bool IsNaN2008 = tools::mips::isNaN2008(getDriver(), Args, Triple);
539
540 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
541
542 if (tools::mips::isUCLibc(Args))
543 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
544 else if (!Triple.hasEnvironment() &&
545 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
546 Loader =
547 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
548 else
549 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
550
551 break;
552 }
553 case llvm::Triple::ppc:
554 LibDir = "lib";
555 Loader = "ld.so.1";
556 break;
557 case llvm::Triple::ppcle:
558 LibDir = "lib";
559 Loader = "ld.so.1";
560 break;
561 case llvm::Triple::ppc64:
562 LibDir = "lib64";
563 Loader =
564 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
565 break;
566 case llvm::Triple::ppc64le:
567 LibDir = "lib64";
568 Loader =
569 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
570 break;
571 case llvm::Triple::riscv32: {
572 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
573 LibDir = "lib";
574 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
575 break;
576 }
577 case llvm::Triple::riscv64: {
578 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
579 LibDir = "lib";
580 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
581 break;
582 }
583 case llvm::Triple::sparc:
584 case llvm::Triple::sparcel:
585 LibDir = "lib";
586 Loader = "ld-linux.so.2";
587 break;
588 case llvm::Triple::sparcv9:
589 LibDir = "lib64";
590 Loader = "ld-linux.so.2";
591 break;
592 case llvm::Triple::systemz:
593 LibDir = "lib";
594 Loader = "ld64.so.1";
595 break;
596 case llvm::Triple::x86:
597 LibDir = "lib";
598 Loader = "ld-linux.so.2";
599 break;
600 case llvm::Triple::x86_64: {
601 bool X32 = Triple.isX32();
602
603 LibDir = X32 ? "libx32" : "lib64";
604 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
605 break;
606 }
607 case llvm::Triple::ve:
608 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
609 case llvm::Triple::csky: {
610 LibDir = "lib";
611 Loader = "ld.so.1";
612 break;
613 }
614 }
615
616 if (Distro == Distro::Exherbo &&
617 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
618 Triple.getVendor() == llvm::Triple::PC))
619 return "/usr/" + Triple.str() + "/lib/" + Loader;
620 return "/" + LibDir + "/" + Loader;
621}
622
623void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
624 ArgStringList &CC1Args) const {
625 const Driver &D = getDriver();
626 std::string SysRoot = computeSysRoot();
627
628 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
629 return;
630
631 // Add 'include' in the resource directory, which is similar to
632 // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
633 // contains some files conflicting with system /usr/include. musl systems
634 // prefer the /usr/include copies which are more relevant.
635 SmallString<128> ResourceDirInclude(D.ResourceDir);
636 llvm::sys::path::append(ResourceDirInclude, "include");
637 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
638 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
639 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
640
641 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
642 return;
643
644 // LOCAL_INCLUDE_DIR
645 addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include"));
646 // TOOL_INCLUDE_DIR
647 AddMultilibIncludeArgs(DriverArgs, CC1Args);
648
649 // Check for configure-time C include directories.
650 StringRef CIncludeDirs(C_INCLUDE_DIRS);
651 if (CIncludeDirs != "") {
653 CIncludeDirs.split(dirs, ":");
654 for (StringRef dir : dirs) {
655 StringRef Prefix =
656 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
657 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
658 }
659 return;
660 }
661
662 // On systems using multiarch and Android, add /usr/include/$triple before
663 // /usr/include.
664 std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
665 if (!MultiarchIncludeDir.empty() &&
666 D.getVFS().exists(concat(SysRoot, "/usr/include", MultiarchIncludeDir)))
668 DriverArgs, CC1Args,
669 concat(SysRoot, "/usr/include", MultiarchIncludeDir));
670
671 if (getTriple().getOS() == llvm::Triple::RTEMS)
672 return;
673
674 // Add an include of '/include' directly. This isn't provided by default by
675 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
676 // add even when Clang is acting as-if it were a system compiler.
677 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
678
679 addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include"));
680
681 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
682 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
683}
684
685void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
686 llvm::opt::ArgStringList &CC1Args) const {
687 // We need a detected GCC installation on Linux to provide libstdc++'s
688 // headers in odd Linuxish places.
690 return;
691
692 // Detect Debian g++-multiarch-incdir.diff.
693 StringRef TripleStr = GCCInstallation.getTriple().str();
694 StringRef DebianMultiarch =
695 GCCInstallation.getTriple().getArch() == llvm::Triple::x86
696 ? "i386-linux-gnu"
697 : TripleStr;
698
699 // Try generic GCC detection first.
700 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
701 DebianMultiarch))
702 return;
703
704 StringRef LibDir = GCCInstallation.getParentLibPath();
706 const GCCVersion &Version = GCCInstallation.getVersion();
707
708 const std::string LibStdCXXIncludePathCandidates[] = {
709 // Android standalone toolchain has C++ headers in yet another place.
710 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
711 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
712 // without a subdirectory corresponding to the gcc version.
713 LibDir.str() + "/../include/c++",
714 // Cray's gcc installation puts headers under "g++" without a
715 // version suffix.
716 LibDir.str() + "/../include/g++",
717 };
718
719 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
720 if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
721 Multilib.includeSuffix(), DriverArgs, CC1Args))
722 break;
723 }
724}
725
726void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
727 ArgStringList &CC1Args) const {
728 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
729}
730
731void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
732 ArgStringList &CC1Args) const {
733 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
734}
735
736void Linux::AddHIPRuntimeLibArgs(const ArgList &Args,
737 ArgStringList &CmdArgs) const {
738 CmdArgs.push_back(
739 Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath()));
740
741 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
742 options::OPT_fno_rtlib_add_rpath, false))
743 CmdArgs.append(
744 {"-rpath", Args.MakeArgString(RocmInstallation->getLibPath())});
745
746 CmdArgs.push_back("-lamdhip64");
747}
748
749void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
750 ArgStringList &CC1Args) const {
751 if (GCCInstallation.isValid()) {
752 CC1Args.push_back("-isystem");
753 CC1Args.push_back(DriverArgs.MakeArgString(
755 GCCInstallation.getTriple().str() + "/include"));
756 }
757}
758
759bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
760 return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
761 getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
762}
763
764bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
765 // Outline atomics for AArch64 are supported by compiler-rt
766 // and libgcc since 9.3.1
767 assert(getTriple().isAArch64() && "expected AArch64 target!");
769 if (RtLib == ToolChain::RLT_CompilerRT)
770 return true;
771 assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
773 return false;
774 return true;
775}
776
778 if (getTriple().isAndroid() || getTriple().isMusl())
779 return false;
781}
782
784 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
785 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
786 const bool IsMIPS = getTriple().isMIPS32();
787 const bool IsMIPS64 = getTriple().isMIPS64();
788 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
789 getTriple().getArch() == llvm::Triple::ppc64le;
790 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
791 getTriple().getArch() == llvm::Triple::aarch64_be;
792 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
793 getTriple().getArch() == llvm::Triple::thumb ||
794 getTriple().getArch() == llvm::Triple::armeb ||
795 getTriple().getArch() == llvm::Triple::thumbeb;
796 const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64;
797 const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
798 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
799 const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon;
801 Res |= SanitizerKind::Address;
802 Res |= SanitizerKind::PointerCompare;
803 Res |= SanitizerKind::PointerSubtract;
804 Res |= SanitizerKind::Fuzzer;
805 Res |= SanitizerKind::FuzzerNoLink;
806 Res |= SanitizerKind::KernelAddress;
807 Res |= SanitizerKind::Memory;
808 Res |= SanitizerKind::Vptr;
809 Res |= SanitizerKind::SafeStack;
810 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64)
811 Res |= SanitizerKind::DataFlow;
812 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
813 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64)
814 Res |= SanitizerKind::Leak;
815 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
816 IsLoongArch64 || IsRISCV64)
817 Res |= SanitizerKind::Thread;
818 if (IsX86_64 || IsSystemZ)
819 Res |= SanitizerKind::KernelMemory;
820 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
821 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64)
822 Res |= SanitizerKind::Scudo;
823 if (IsX86_64 || IsAArch64 || IsRISCV64) {
824 Res |= SanitizerKind::HWAddress;
825 }
826 if (IsX86_64 || IsAArch64) {
827 Res |= SanitizerKind::KernelHWAddress;
828 }
829 // Work around "Cannot represent a difference across sections".
830 if (getTriple().getArch() == llvm::Triple::ppc64)
832 return Res;
833}
834
835void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
836 llvm::opt::ArgStringList &CmdArgs) const {
837 // Add linker option -u__llvm_profile_runtime to cause runtime
838 // initialization module to be linked in.
839 if (needsProfileRT(Args))
840 CmdArgs.push_back(Args.MakeArgString(
841 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
842 ToolChain::addProfileRTLibs(Args, CmdArgs);
843}
844
845void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
846 for (const auto &Opt : ExtraOpts)
847 CmdArgs.push_back(Opt.c_str());
848}
849
850const char *Linux::getDefaultLinker() const {
851 if (getTriple().isAndroid())
852 return "ld.lld";
854}
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:180
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:164
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:403
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:155
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
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 unsigned GetDefaultDwarfVersion() const
Definition: ToolChain.h:583
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:1360
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:2218
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:3153
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:3359
LazyDetector< CudaInstallationDetector > CudaInstallation
Definition: Gnu.h:289
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:3138
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3234
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:3219
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3325
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:759
std::vector< std::string > ExtraOpts
Definition: Linux.h:60
const char * getDefaultLinker() const override
GetDefaultLinker - Get the default linker to use.
Definition: Linux.cpp:850
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: Linux.cpp:351
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Linux.cpp:369
bool IsMathErrnoDefault() const override
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition: Linux.cpp:777
Tool * buildAssembler() const override
Definition: Linux.cpp:377
std::string computeSysRoot() const override
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: Linux.cpp:381
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:623
Linux(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Linux.cpp:216
unsigned GetDefaultDwarfVersion() const override
Definition: Linux.cpp:357
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Linux.cpp:783
bool IsAArch64OutlineAtomicsDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain supports outline atomics by default.
Definition: Linux.cpp:764
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:835
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: Linux.cpp:363
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific HIP includes.
Definition: Linux.cpp:731
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Linux.cpp:435
Tool * buildStaticLibTool() const override
Definition: Linux.cpp:373
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:736
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Linux.cpp:685
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Linux.cpp:845
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:749
Tool * buildLinker() const override
Definition: Linux.cpp:371
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: Linux.cpp:726
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:290
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:2076
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164