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