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