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