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