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"
19#include "clang/Driver/Options.h"
20#include "clang/Driver/SanitizerArgs.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
33using tools::addPathIfExists;
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 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(Path: concat(Path: SysRoot, A: "/lib", B: MT)))
137 return MT;
138 if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/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(Path: concat(Path: SysRoot, A: "/lib", B: MT)))
146 return MT;
147 if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/mips64el-linux-gnu")))
148 return "mips64el-linux-gnu";
149 break;
150 }
151 case llvm::Triple::ppc:
152 if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/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, Value: "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(TargetTriple: Triple, Args);
219 Multilibs = GCCInstallation.getMultilibs();
220 SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()});
221 llvm::Triple::ArchType Arch = Triple.getArch();
222 std::string SysRoot = computeSysRoot();
223 ToolChain::path_list &PPaths = getProgramPaths();
224
225 Generic_GCC::PushPPaths(PPaths);
226
227 Distro Distro(D.getVFS(), Triple);
228
229 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
230 ExtraOpts.push_back(x: "-z");
231 ExtraOpts.push_back(x: "now");
232 }
233
234 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
235 Triple.isAndroid()) {
236 ExtraOpts.push_back(x: "-z");
237 ExtraOpts.push_back(x: "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(x: "-z");
246 ExtraOpts.push_back(x: "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(x: "-z");
251 ExtraOpts.push_back(x: "max-page-size=16384");
252 }
253 }
254
255 if (GCCInstallation.getParentLibPath().contains(Other: "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(Elt: Twine(GCCInstallation.getParentLibPath() +
261 "/../bin").str());
262
263 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
264 ExtraOpts.push_back(x: "-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(x: "--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) {
285 if (Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid ||
286 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic ||
287 (IsAndroid && Triple.isAndroidVersionLT(Major: 23)))
288 ExtraOpts.push_back(x: "--hash-style=both");
289 else
290 ExtraOpts.push_back(x: "--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, TargetTriple: 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, OSLibDir: "libo32", MultiarchTriple, Paths);
311 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/libo32"), Paths);
312 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/libo32"), Paths);
313 }
314 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
315
316 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib", B: MultiarchTriple), Paths);
317 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib/..", B: 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 Path: concat(Path: SysRoot, A: "/usr/lib", B: MultiarchTriple,
326 C: llvm::to_string(Value: Triple.getEnvironmentVersion().getMajor())),
327 Paths);
328 }
329
330 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib", B: 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, Path: concat(Path: SysRoot, A: "/usr", B: OSLibDir), Paths);
337 else
338 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib/..", B: OSLibDir), Paths);
339 if (IsRISCV) {
340 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
341 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/", B: OSLibDir, C: ABIName), Paths);
342 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr", B: OSLibDir, C: ABIName), Paths);
343 }
344
345 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
346
347 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib"), Paths);
348 addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib"), Paths);
349}
350
351ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const {
352 if (getTriple().isAndroid())
353 return ToolChain::RLT_CompilerRT;
354 return Generic_ELF::GetDefaultRuntimeLibType();
355}
356
357unsigned Linux::GetDefaultDwarfVersion() const {
358 if (getTriple().isAndroid())
359 return 4;
360 return ToolChain::GetDefaultDwarfVersion();
361}
362
363ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
364 if (getTriple().isAndroid())
365 return ToolChain::CST_Libcxx;
366 return ToolChain::CST_Libstdcxx;
367}
368
369bool Linux::HasNativeLLVMSupport() const { return true; }
370
371Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
372
373Tool *Linux::buildStaticLibTool() const {
374 return new tools::gnutools::StaticLibTool(*this);
375}
376
377Tool *Linux::buildAssembler() const {
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(Path: AndroidSysRootPath))
391 return AndroidSysRootPath;
392 }
393
394 if (getTriple().isCSKY()) {
395 // CSKY toolchains use different names for sysroot folder.
396 if (!GCCInstallation.isValid())
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();
418 const Multilib &Multilib = GCCInstallation.getMultilib();
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(JobArgs: Args).needsHwasanRt() &&
443 !Triple.isAndroidVersionLT(Major: 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 ||
478 tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard))
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 ||
508 tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard;
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(D: 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(D: 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(D: 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, Value: "elfv2")) ? "ld64.so.2" : "ld64.so.1";
565 break;
566 case llvm::Triple::ppc64le:
567 LibDir = "lib64";
568 Loader =
569 (tools::ppc::hasPPCAbiArg(Args, Value: "elfv1")) ? "ld64.so.1" : "ld64.so.2";
570 break;
571 case llvm::Triple::riscv32: {
572 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
573 LibDir = "lib";
574 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
575 break;
576 }
577 case llvm::Triple::riscv64: {
578 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
579 LibDir = "lib";
580 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
581 break;
582 }
583 case llvm::Triple::sparc:
584 case llvm::Triple::sparcel:
585 LibDir = "lib";
586 Loader = "ld-linux.so.2";
587 break;
588 case llvm::Triple::sparcv9:
589 LibDir = "lib64";
590 Loader = "ld-linux.so.2";
591 break;
592 case llvm::Triple::systemz:
593 LibDir = "lib";
594 Loader = "ld64.so.1";
595 break;
596 case llvm::Triple::x86:
597 LibDir = "lib";
598 Loader = "ld-linux.so.2";
599 break;
600 case llvm::Triple::x86_64: {
601 bool X32 = Triple.isX32();
602
603 LibDir = X32 ? "libx32" : "lib64";
604 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
605 break;
606 }
607 case llvm::Triple::ve:
608 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
609 case llvm::Triple::csky: {
610 LibDir = "lib";
611 Loader = "ld.so.1";
612 break;
613 }
614 }
615
616 if (Distro == Distro::Exherbo &&
617 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
618 Triple.getVendor() == llvm::Triple::PC))
619 return "/usr/" + Triple.str() + "/lib/" + Loader;
620 return "/" + LibDir + "/" + Loader;
621}
622
623void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
624 ArgStringList &CC1Args) const {
625 const Driver &D = getDriver();
626 std::string SysRoot = computeSysRoot();
627
628 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
629 return;
630
631 // Add 'include' in the resource directory, which is similar to
632 // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
633 // contains some files conflicting with system /usr/include. musl systems
634 // prefer the /usr/include copies which are more relevant.
635 SmallString<128> ResourceDirInclude(D.ResourceDir);
636 llvm::sys::path::append(path&: ResourceDirInclude, a: "include");
637 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
638 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
639 addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude);
640
641 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
642 return;
643
644 // LOCAL_INCLUDE_DIR
645 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/local/include"));
646 // TOOL_INCLUDE_DIR
647 AddMultilibIncludeArgs(DriverArgs, CC1Args);
648
649 // Check for configure-time C include directories.
650 StringRef CIncludeDirs(C_INCLUDE_DIRS);
651 if (CIncludeDirs != "") {
652 SmallVector<StringRef, 5> dirs;
653 CIncludeDirs.split(A&: dirs, Separator: ":");
654 for (StringRef dir : dirs) {
655 StringRef Prefix =
656 llvm::sys::path::is_absolute(path: dir) ? "" : StringRef(SysRoot);
657 addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir);
658 }
659 return;
660 }
661
662 // On systems using multiarch and Android, add /usr/include/$triple before
663 // /usr/include.
664 std::string MultiarchIncludeDir = getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot);
665 if (!MultiarchIncludeDir.empty() &&
666 D.getVFS().exists(Path: concat(Path: SysRoot, A: "/usr/include", B: MultiarchIncludeDir)))
667 addExternCSystemInclude(
668 DriverArgs, CC1Args,
669 Path: concat(Path: SysRoot, A: "/usr/include", B: MultiarchIncludeDir));
670
671 if (getTriple().getOS() == llvm::Triple::RTEMS)
672 return;
673
674 // Add an include of '/include' directly. This isn't provided by default by
675 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
676 // add even when Clang is acting as-if it were a system compiler.
677 addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/include"));
678
679 addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/include"));
680
681 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
682 addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude);
683}
684
685void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
686 llvm::opt::ArgStringList &CC1Args) const {
687 // We need a detected GCC installation on Linux to provide libstdc++'s
688 // headers in odd Linuxish places.
689 if (!GCCInstallation.isValid())
690 return;
691
692 // Detect Debian g++-multiarch-incdir.diff.
693 StringRef TripleStr = GCCInstallation.getTriple().str();
694 StringRef DebianMultiarch =
695 GCCInstallation.getTriple().getArch() == llvm::Triple::x86
696 ? "i386-linux-gnu"
697 : TripleStr;
698
699 // Try generic GCC detection first.
700 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
701 DebianMultiarch))
702 return;
703
704 StringRef LibDir = GCCInstallation.getParentLibPath();
705 const Multilib &Multilib = GCCInstallation.getMultilib();
706 const GCCVersion &Version = GCCInstallation.getVersion();
707
708 const std::string LibStdCXXIncludePathCandidates[] = {
709 // Android standalone toolchain has C++ headers in yet another place.
710 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
711 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
712 // without a subdirectory corresponding to the gcc version.
713 LibDir.str() + "/../include/c++",
714 // Cray's gcc installation puts headers under "g++" without a
715 // version suffix.
716 LibDir.str() + "/../include/g++",
717 };
718
719 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
720 if (addLibStdCXXIncludePaths(IncludeDir: IncludePath, Triple: TripleStr,
721 IncludeSuffix: Multilib.includeSuffix(), DriverArgs, CC1Args))
722 break;
723 }
724}
725
726void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
727 ArgStringList &CC1Args) const {
728 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
729}
730
731void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
732 ArgStringList &CC1Args) const {
733 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
734}
735
736void Linux::AddHIPRuntimeLibArgs(const ArgList &Args,
737 ArgStringList &CmdArgs) const {
738 CmdArgs.push_back(
739 Elt: Args.MakeArgString(Str: StringRef("-L") + RocmInstallation->getLibPath()));
740
741 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
742 options::OPT_fno_rtlib_add_rpath, false))
743 CmdArgs.append(
744 IL: {"-rpath", Args.MakeArgString(Str: RocmInstallation->getLibPath())});
745
746 CmdArgs.push_back(Elt: "-lamdhip64");
747}
748
749void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
750 ArgStringList &CC1Args) const {
751 if (GCCInstallation.isValid()) {
752 CC1Args.push_back(Elt: "-isystem");
753 CC1Args.push_back(Elt: DriverArgs.MakeArgString(
754 Str: GCCInstallation.getParentLibPath() + "/../" +
755 GCCInstallation.getTriple().str() + "/include"));
756 }
757}
758
759bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
760 return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
761 getTriple().isMusl() || getSanitizerArgs(JobArgs: Args).requiresPIE();
762}
763
764bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
765 // Outline atomics for AArch64 are supported by compiler-rt
766 // and libgcc since 9.3.1
767 assert(getTriple().isAArch64() && "expected AArch64 target!");
768 ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args);
769 if (RtLib == ToolChain::RLT_CompilerRT)
770 return true;
771 assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
772 if (GCCInstallation.getVersion().isOlderThan(RHSMajor: 9, RHSMinor: 3, RHSPatch: 1))
773 return false;
774 return true;
775}
776
777bool Linux::IsMathErrnoDefault() const {
778 if (getTriple().isAndroid() || getTriple().isMusl())
779 return false;
780 return Generic_ELF::IsMathErrnoDefault();
781}
782
783SanitizerMask Linux::getSupportedSanitizers() const {
784 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
785 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
786 const bool IsMIPS = getTriple().isMIPS32();
787 const bool IsMIPS64 = getTriple().isMIPS64();
788 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
789 getTriple().getArch() == llvm::Triple::ppc64le;
790 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
791 getTriple().getArch() == llvm::Triple::aarch64_be;
792 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
793 getTriple().getArch() == llvm::Triple::thumb ||
794 getTriple().getArch() == llvm::Triple::armeb ||
795 getTriple().getArch() == llvm::Triple::thumbeb;
796 const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64;
797 const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
798 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
799 const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon;
800 SanitizerMask Res = ToolChain::getSupportedSanitizers();
801 Res |= SanitizerKind::Address;
802 Res |= SanitizerKind::PointerCompare;
803 Res |= SanitizerKind::PointerSubtract;
804 Res |= SanitizerKind::Fuzzer;
805 Res |= SanitizerKind::FuzzerNoLink;
806 Res |= SanitizerKind::KernelAddress;
807 Res |= SanitizerKind::Memory;
808 Res |= SanitizerKind::Vptr;
809 Res |= SanitizerKind::SafeStack;
810 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64)
811 Res |= SanitizerKind::DataFlow;
812 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
813 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64)
814 Res |= SanitizerKind::Leak;
815 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
816 IsLoongArch64 || IsRISCV64)
817 Res |= SanitizerKind::Thread;
818 if (IsX86_64 || IsSystemZ)
819 Res |= SanitizerKind::KernelMemory;
820 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
821 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64)
822 Res |= SanitizerKind::Scudo;
823 if (IsX86_64 || IsAArch64 || IsRISCV64) {
824 Res |= SanitizerKind::HWAddress;
825 }
826 if (IsX86_64 || IsAArch64) {
827 Res |= SanitizerKind::KernelHWAddress;
828 }
829 // Work around "Cannot represent a difference across sections".
830 if (getTriple().getArch() == llvm::Triple::ppc64)
831 Res &= ~SanitizerKind::Function;
832 return Res;
833}
834
835void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
836 llvm::opt::ArgStringList &CmdArgs) const {
837 // Add linker option -u__llvm_profile_runtime to cause runtime
838 // initialization module to be linked in.
839 if (needsProfileRT(Args))
840 CmdArgs.push_back(Elt: Args.MakeArgString(
841 Str: Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
842 ToolChain::addProfileRTLibs(Args, CmdArgs);
843}
844
845llvm::DenormalMode
846Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
847 const JobAction &JA,
848 const llvm::fltSemantics *FPType) const {
849 switch (getTriple().getArch()) {
850 case llvm::Triple::x86:
851 case llvm::Triple::x86_64: {
852 std::string Unused;
853 // DAZ and FTZ are turned on in crtfastmath.o
854 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
855 isFastMathRuntimeAvailable(Args: DriverArgs, Path&: Unused))
856 return llvm::DenormalMode::getPreserveSign();
857 return llvm::DenormalMode::getIEEE();
858 }
859 default:
860 return llvm::DenormalMode::getIEEE();
861 }
862}
863
864void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
865 for (const auto &Opt : ExtraOpts)
866 CmdArgs.push_back(Elt: Opt.c_str());
867}
868
869const char *Linux::getDefaultLinker() const {
870 if (getTriple().isAndroid())
871 return "ld.lld";
872 return Generic_ELF::getDefaultLinker();
873}
874

source code of clang/lib/Driver/ToolChains/Linux.cpp