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