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/Mips.h"
12 #include "Arch/PPC.h"
13 #include "Arch/RISCV.h"
14 #include "CommonArgs.h"
15 #include "clang/Config/config.h"
16 #include "clang/Driver/Distro.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/Options.h"
19 #include "clang/Driver/SanitizerArgs.h"
20 #include "llvm/Option/ArgList.h"
21 #include "llvm/ProfileData/InstrProf.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/ScopedPrinter.h"
24 #include "llvm/Support/VirtualFileSystem.h"
25 #include <system_error>
26
27 using namespace clang::driver;
28 using namespace clang::driver::toolchains;
29 using namespace clang;
30 using namespace llvm::opt;
31
32 using tools::addPathIfExists;
33
34 /// Get our best guess at the multiarch triple for a target.
35 ///
36 /// Debian-based systems are starting to use a multiarch setup where they use
37 /// a target-triple directory in the library and header search paths.
38 /// Unfortunately, this triple does not align with the vanilla target triple,
39 /// so we provide a rough mapping here.
getMultiarchTriple(const Driver & D,const llvm::Triple & TargetTriple,StringRef SysRoot) const40 std::string Linux::getMultiarchTriple(const Driver &D,
41 const llvm::Triple &TargetTriple,
42 StringRef SysRoot) const {
43 llvm::Triple::EnvironmentType TargetEnvironment =
44 TargetTriple.getEnvironment();
45 bool IsAndroid = TargetTriple.isAndroid();
46 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
47 bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
48
49 // For most architectures, just use whatever we have rather than trying to be
50 // clever.
51 switch (TargetTriple.getArch()) {
52 default:
53 break;
54
55 // We use the existence of '/lib/<triple>' as a directory to detect some
56 // common linux triples that don't quite match the Clang triple for both
57 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
58 // regardless of what the actual target triple is.
59 case llvm::Triple::arm:
60 case llvm::Triple::thumb:
61 if (IsAndroid) {
62 return "arm-linux-androideabi";
63 } else if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
64 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf"))
65 return "arm-linux-gnueabihf";
66 } else {
67 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi"))
68 return "arm-linux-gnueabi";
69 }
70 break;
71 case llvm::Triple::armeb:
72 case llvm::Triple::thumbeb:
73 if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
74 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf"))
75 return "armeb-linux-gnueabihf";
76 } else {
77 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi"))
78 return "armeb-linux-gnueabi";
79 }
80 break;
81 case llvm::Triple::x86:
82 if (IsAndroid)
83 return "i686-linux-android";
84 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu"))
85 return "i386-linux-gnu";
86 break;
87 case llvm::Triple::x86_64:
88 if (IsAndroid)
89 return "x86_64-linux-android";
90 // We don't want this for x32, otherwise it will match x86_64 libs
91 if (TargetEnvironment != llvm::Triple::GNUX32 &&
92 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu"))
93 return "x86_64-linux-gnu";
94 break;
95 case llvm::Triple::aarch64:
96 if (IsAndroid)
97 return "aarch64-linux-android";
98 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu"))
99 return "aarch64-linux-gnu";
100 break;
101 case llvm::Triple::aarch64_be:
102 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
103 return "aarch64_be-linux-gnu";
104 break;
105 case llvm::Triple::mips: {
106 std::string MT = IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu";
107 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
108 return MT;
109 break;
110 }
111 case llvm::Triple::mipsel: {
112 if (IsAndroid)
113 return "mipsel-linux-android";
114 std::string MT = IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu";
115 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
116 return MT;
117 break;
118 }
119 case llvm::Triple::mips64: {
120 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") +
121 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
122 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
123 return MT;
124 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu"))
125 return "mips64-linux-gnu";
126 break;
127 }
128 case llvm::Triple::mips64el: {
129 if (IsAndroid)
130 return "mips64el-linux-android";
131 std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") +
132 "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
133 if (D.getVFS().exists(SysRoot + "/lib/" + MT))
134 return MT;
135 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu"))
136 return "mips64el-linux-gnu";
137 break;
138 }
139 case llvm::Triple::ppc:
140 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
141 return "powerpc-linux-gnuspe";
142 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu"))
143 return "powerpc-linux-gnu";
144 break;
145 case llvm::Triple::ppcle:
146 if (D.getVFS().exists(SysRoot + "/lib/powerpcle-linux-gnu"))
147 return "powerpcle-linux-gnu";
148 break;
149 case llvm::Triple::ppc64:
150 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu"))
151 return "powerpc64-linux-gnu";
152 break;
153 case llvm::Triple::ppc64le:
154 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
155 return "powerpc64le-linux-gnu";
156 break;
157 case llvm::Triple::sparc:
158 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu"))
159 return "sparc-linux-gnu";
160 break;
161 case llvm::Triple::sparcv9:
162 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu"))
163 return "sparc64-linux-gnu";
164 break;
165 case llvm::Triple::systemz:
166 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu"))
167 return "s390x-linux-gnu";
168 break;
169 }
170 return TargetTriple.str();
171 }
172
getOSLibDir(const llvm::Triple & Triple,const ArgList & Args)173 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
174 if (Triple.isMIPS()) {
175 if (Triple.isAndroid()) {
176 StringRef CPUName;
177 StringRef ABIName;
178 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
179 if (CPUName == "mips32r6")
180 return "libr6";
181 if (CPUName == "mips32r2")
182 return "libr2";
183 }
184 // lib32 directory has a special meaning on MIPS targets.
185 // It contains N32 ABI binaries. Use this folder if produce
186 // code for N32 ABI only.
187 if (tools::mips::hasMipsAbiArg(Args, "n32"))
188 return "lib32";
189 return Triple.isArch32Bit() ? "lib" : "lib64";
190 }
191
192 // It happens that only x86, PPC and SPARC use the 'lib32' variant of
193 // oslibdir, and using that variant while targeting other architectures causes
194 // problems because the libraries are laid out in shared system roots that
195 // can't cope with a 'lib32' library search path being considered. So we only
196 // enable them when we know we may need it.
197 //
198 // FIXME: This is a bit of a hack. We should really unify this code for
199 // reasoning about oslibdir spellings with the lib dir spellings in the
200 // GCCInstallationDetector, but that is a more significant refactoring.
201 if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() ||
202 Triple.getArch() == llvm::Triple::sparc)
203 return "lib32";
204
205 if (Triple.getArch() == llvm::Triple::x86_64 &&
206 Triple.getEnvironment() == llvm::Triple::GNUX32)
207 return "libx32";
208
209 if (Triple.getArch() == llvm::Triple::riscv32)
210 return "lib32";
211
212 return Triple.isArch32Bit() ? "lib" : "lib64";
213 }
214
Linux(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)215 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
216 : Generic_ELF(D, Triple, Args) {
217 GCCInstallation.init(Triple, Args);
218 Multilibs = GCCInstallation.getMultilibs();
219 SelectedMultilib = GCCInstallation.getMultilib();
220 llvm::Triple::ArchType Arch = Triple.getArch();
221 std::string SysRoot = computeSysRoot();
222 ToolChain::path_list &PPaths = getProgramPaths();
223
224 Generic_GCC::PushPPaths(PPaths);
225
226 Distro Distro(D.getVFS(), Triple);
227
228 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
229 ExtraOpts.push_back("-z");
230 ExtraOpts.push_back("now");
231 }
232
233 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
234 Triple.isAndroid()) {
235 ExtraOpts.push_back("-z");
236 ExtraOpts.push_back("relro");
237 }
238
239 // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld
240 // from 11 onwards default max-page-size to 65536 for both ARM and AArch64.
241 if ((Triple.isARM() || Triple.isAArch64()) && Triple.isAndroid()) {
242 ExtraOpts.push_back("-z");
243 ExtraOpts.push_back("max-page-size=4096");
244 }
245
246 if (GCCInstallation.getParentLibPath().find("opt/rh/devtoolset") !=
247 StringRef::npos)
248 // With devtoolset on RHEL, we want to add a bin directory that is relative
249 // to the detected gcc install, because if we are using devtoolset gcc then
250 // we want to use other tools from devtoolset (e.g. ld) instead of the
251 // standard system tools.
252 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
253 "/../bin").str());
254
255 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
256 ExtraOpts.push_back("-X");
257
258 const bool IsAndroid = Triple.isAndroid();
259 const bool IsMips = Triple.isMIPS();
260 const bool IsHexagon = Arch == llvm::Triple::hexagon;
261 const bool IsRISCV = Triple.isRISCV();
262
263 if (IsMips && !SysRoot.empty())
264 ExtraOpts.push_back("--sysroot=" + SysRoot);
265
266 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
267 // and the MIPS ABI require .dynsym to be sorted in different ways.
268 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
269 // ABI requires a mapping between the GOT and the symbol table.
270 // Android loader does not support .gnu.hash until API 23.
271 // Hexagon linker/loader does not support .gnu.hash
272 if (!IsMips && !IsHexagon) {
273 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
274 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) ||
275 (IsAndroid && !Triple.isAndroidVersionLT(23)))
276 ExtraOpts.push_back("--hash-style=gnu");
277
278 if (Distro.IsDebian() || Distro.IsOpenSUSE() ||
279 Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty ||
280 Distro == Distro::UbuntuKarmic ||
281 (IsAndroid && Triple.isAndroidVersionLT(23)))
282 ExtraOpts.push_back("--hash-style=both");
283 }
284
285 #ifdef ENABLE_LINKER_BUILD_ID
286 ExtraOpts.push_back("--build-id");
287 #endif
288
289 if (IsAndroid || Distro.IsOpenSUSE())
290 ExtraOpts.push_back("--enable-new-dtags");
291
292 // The selection of paths to try here is designed to match the patterns which
293 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
294 // This was determined by running GCC in a fake filesystem, creating all
295 // possible permutations of these directories, and seeing which ones it added
296 // to the link paths.
297 path_list &Paths = getFilePaths();
298
299 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
300 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
301
302 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
303
304 // Similar to the logic for GCC above, if we currently running Clang inside
305 // of the requested system root, add its parent library paths to
306 // those searched.
307 // FIXME: It's not clear whether we should use the driver's installed
308 // directory ('Dir' below) or the ResourceDir.
309 if (StringRef(D.Dir).startswith(SysRoot)) {
310 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
311 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
312 }
313
314 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
315 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
316
317 if (IsAndroid) {
318 // Android sysroots contain a library directory for each supported OS
319 // version as well as some unversioned libraries in the usual multiarch
320 // directory.
321 unsigned Major;
322 unsigned Minor;
323 unsigned Micro;
324 Triple.getEnvironmentVersion(Major, Minor, Micro);
325 addPathIfExists(D,
326 SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
327 llvm::to_string(Major),
328 Paths);
329 }
330
331 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
332 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
333 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
334 // this here.
335 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
336 Triple.isArch64Bit())
337 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths);
338 else
339 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
340 if (IsRISCV) {
341 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
342 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
343 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
344 }
345
346 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
347
348 // Similar to the logic for GCC above, if we are currently running Clang
349 // inside of the requested system root, add its parent library path to those
350 // searched.
351 // FIXME: It's not clear whether we should use the driver's installed
352 // directory ('Dir' below) or the ResourceDir.
353 if (StringRef(D.Dir).startswith(SysRoot))
354 addPathIfExists(D, D.Dir + "/../lib", Paths);
355
356 addPathIfExists(D, SysRoot + "/lib", Paths);
357 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
358 }
359
GetDefaultCXXStdlibType() const360 ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
361 if (getTriple().isAndroid())
362 return ToolChain::CST_Libcxx;
363 return ToolChain::CST_Libstdcxx;
364 }
365
HasNativeLLVMSupport() const366 bool Linux::HasNativeLLVMSupport() const { return true; }
367
buildLinker() const368 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
369
buildStaticLibTool() const370 Tool *Linux::buildStaticLibTool() const {
371 return new tools::gnutools::StaticLibTool(*this);
372 }
373
buildAssembler() const374 Tool *Linux::buildAssembler() const {
375 return new tools::gnutools::Assembler(*this);
376 }
377
computeSysRoot() const378 std::string Linux::computeSysRoot() const {
379 if (!getDriver().SysRoot.empty())
380 return getDriver().SysRoot;
381
382 if (getTriple().isAndroid()) {
383 // Android toolchains typically include a sysroot at ../sysroot relative to
384 // the clang binary.
385 const StringRef ClangDir = getDriver().getInstalledDir();
386 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
387 if (getVFS().exists(AndroidSysRootPath))
388 return AndroidSysRootPath;
389 }
390
391 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
392 return std::string();
393
394 // Standalone MIPS toolchains use different names for sysroot folder
395 // and put it into different places. Here we try to check some known
396 // variants.
397
398 const StringRef InstallDir = GCCInstallation.getInstallPath();
399 const StringRef TripleStr = GCCInstallation.getTriple().str();
400 const Multilib &Multilib = GCCInstallation.getMultilib();
401
402 std::string Path =
403 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
404 .str();
405
406 if (getVFS().exists(Path))
407 return Path;
408
409 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
410
411 if (getVFS().exists(Path))
412 return Path;
413
414 return std::string();
415 }
416
getDynamicLinker(const ArgList & Args) const417 std::string Linux::getDynamicLinker(const ArgList &Args) const {
418 const llvm::Triple::ArchType Arch = getArch();
419 const llvm::Triple &Triple = getTriple();
420
421 const Distro Distro(getDriver().getVFS(), Triple);
422
423 if (Triple.isAndroid())
424 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
425
426 if (Triple.isMusl()) {
427 std::string ArchName;
428 bool IsArm = false;
429
430 switch (Arch) {
431 case llvm::Triple::arm:
432 case llvm::Triple::thumb:
433 ArchName = "arm";
434 IsArm = true;
435 break;
436 case llvm::Triple::armeb:
437 case llvm::Triple::thumbeb:
438 ArchName = "armeb";
439 IsArm = true;
440 break;
441 default:
442 ArchName = Triple.getArchName().str();
443 }
444 if (IsArm &&
445 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
446 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
447 ArchName += "hf";
448
449 return "/lib/ld-musl-" + ArchName + ".so.1";
450 }
451
452 std::string LibDir;
453 std::string Loader;
454
455 switch (Arch) {
456 default:
457 llvm_unreachable("unsupported architecture");
458
459 case llvm::Triple::aarch64:
460 LibDir = "lib";
461 Loader = "ld-linux-aarch64.so.1";
462 break;
463 case llvm::Triple::aarch64_be:
464 LibDir = "lib";
465 Loader = "ld-linux-aarch64_be.so.1";
466 break;
467 case llvm::Triple::arm:
468 case llvm::Triple::thumb:
469 case llvm::Triple::armeb:
470 case llvm::Triple::thumbeb: {
471 const bool HF =
472 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
473 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
474
475 LibDir = "lib";
476 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
477 break;
478 }
479 case llvm::Triple::mips:
480 case llvm::Triple::mipsel:
481 case llvm::Triple::mips64:
482 case llvm::Triple::mips64el: {
483 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
484
485 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
486
487 if (tools::mips::isUCLibc(Args))
488 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
489 else if (!Triple.hasEnvironment() &&
490 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
491 Loader =
492 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
493 else
494 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
495
496 break;
497 }
498 case llvm::Triple::ppc:
499 LibDir = "lib";
500 Loader = "ld.so.1";
501 break;
502 case llvm::Triple::ppcle:
503 LibDir = "lib";
504 Loader = "ld.so.1";
505 break;
506 case llvm::Triple::ppc64:
507 LibDir = "lib64";
508 Loader =
509 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
510 break;
511 case llvm::Triple::ppc64le:
512 LibDir = "lib64";
513 Loader =
514 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
515 break;
516 case llvm::Triple::riscv32: {
517 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
518 LibDir = "lib";
519 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
520 break;
521 }
522 case llvm::Triple::riscv64: {
523 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
524 LibDir = "lib";
525 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
526 break;
527 }
528 case llvm::Triple::sparc:
529 case llvm::Triple::sparcel:
530 LibDir = "lib";
531 Loader = "ld-linux.so.2";
532 break;
533 case llvm::Triple::sparcv9:
534 LibDir = "lib64";
535 Loader = "ld-linux.so.2";
536 break;
537 case llvm::Triple::systemz:
538 LibDir = "lib";
539 Loader = "ld64.so.1";
540 break;
541 case llvm::Triple::x86:
542 LibDir = "lib";
543 Loader = "ld-linux.so.2";
544 break;
545 case llvm::Triple::x86_64: {
546 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
547
548 LibDir = X32 ? "libx32" : "lib64";
549 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
550 break;
551 }
552 case llvm::Triple::ve:
553 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
554 }
555
556 if (Distro == Distro::Exherbo &&
557 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
558 Triple.getVendor() == llvm::Triple::PC))
559 return "/usr/" + Triple.str() + "/lib/" + Loader;
560 return "/" + LibDir + "/" + Loader;
561 }
562
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const563 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
564 ArgStringList &CC1Args) const {
565 const Driver &D = getDriver();
566 std::string SysRoot = computeSysRoot();
567
568 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
569 return;
570
571 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
572 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
573
574 SmallString<128> ResourceDirInclude(D.ResourceDir);
575 llvm::sys::path::append(ResourceDirInclude, "include");
576 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
577 (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
578 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
579
580 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
581 return;
582
583 // Check for configure-time C include directories.
584 StringRef CIncludeDirs(C_INCLUDE_DIRS);
585 if (CIncludeDirs != "") {
586 SmallVector<StringRef, 5> dirs;
587 CIncludeDirs.split(dirs, ":");
588 for (StringRef dir : dirs) {
589 StringRef Prefix =
590 llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
591 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
592 }
593 return;
594 }
595
596 // Lacking those, try to detect the correct set of system includes for the
597 // target triple.
598
599 AddMultilibIncludeArgs(DriverArgs, CC1Args);
600
601 // Implement generic Debian multiarch support.
602 const StringRef X86_64MultiarchIncludeDirs[] = {
603 "/usr/include/x86_64-linux-gnu",
604
605 // FIXME: These are older forms of multiarch. It's not clear that they're
606 // in use in any released version of Debian, so we should consider
607 // removing them.
608 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
609 const StringRef X86MultiarchIncludeDirs[] = {
610 "/usr/include/i386-linux-gnu",
611
612 // FIXME: These are older forms of multiarch. It's not clear that they're
613 // in use in any released version of Debian, so we should consider
614 // removing them.
615 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
616 "/usr/include/i486-linux-gnu"};
617 const StringRef AArch64MultiarchIncludeDirs[] = {
618 "/usr/include/aarch64-linux-gnu"};
619 const StringRef ARMMultiarchIncludeDirs[] = {
620 "/usr/include/arm-linux-gnueabi"};
621 const StringRef ARMHFMultiarchIncludeDirs[] = {
622 "/usr/include/arm-linux-gnueabihf"};
623 const StringRef ARMEBMultiarchIncludeDirs[] = {
624 "/usr/include/armeb-linux-gnueabi"};
625 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
626 "/usr/include/armeb-linux-gnueabihf"};
627 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
628 const StringRef MIPSELMultiarchIncludeDirs[] = {
629 "/usr/include/mipsel-linux-gnu"};
630 const StringRef MIPS64MultiarchIncludeDirs[] = {
631 "/usr/include/mips64-linux-gnuabi64"};
632 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
633 "/usr/include/mips64el-linux-gnuabi64"};
634 const StringRef MIPSN32MultiarchIncludeDirs[] = {
635 "/usr/include/mips64-linux-gnuabin32"};
636 const StringRef MIPSN32ELMultiarchIncludeDirs[] = {
637 "/usr/include/mips64el-linux-gnuabin32"};
638 const StringRef MIPSR6MultiarchIncludeDirs[] = {
639 "/usr/include/mipsisa32-linux-gnu"};
640 const StringRef MIPSR6ELMultiarchIncludeDirs[] = {
641 "/usr/include/mipsisa32r6el-linux-gnu"};
642 const StringRef MIPS64R6MultiarchIncludeDirs[] = {
643 "/usr/include/mipsisa64r6-linux-gnuabi64"};
644 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = {
645 "/usr/include/mipsisa64r6el-linux-gnuabi64"};
646 const StringRef MIPSN32R6MultiarchIncludeDirs[] = {
647 "/usr/include/mipsisa64r6-linux-gnuabin32"};
648 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = {
649 "/usr/include/mipsisa64r6el-linux-gnuabin32"};
650 const StringRef PPCMultiarchIncludeDirs[] = {
651 "/usr/include/powerpc-linux-gnu",
652 "/usr/include/powerpc-linux-gnuspe"};
653 const StringRef PPCLEMultiarchIncludeDirs[] = {
654 "/usr/include/powerpcle-linux-gnu"};
655 const StringRef PPC64MultiarchIncludeDirs[] = {
656 "/usr/include/powerpc64-linux-gnu"};
657 const StringRef PPC64LEMultiarchIncludeDirs[] = {
658 "/usr/include/powerpc64le-linux-gnu"};
659 const StringRef SparcMultiarchIncludeDirs[] = {
660 "/usr/include/sparc-linux-gnu"};
661 const StringRef Sparc64MultiarchIncludeDirs[] = {
662 "/usr/include/sparc64-linux-gnu"};
663 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
664 "/usr/include/s390x-linux-gnu"};
665 ArrayRef<StringRef> MultiarchIncludeDirs;
666 switch (getTriple().getArch()) {
667 case llvm::Triple::x86_64:
668 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
669 break;
670 case llvm::Triple::x86:
671 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
672 break;
673 case llvm::Triple::aarch64:
674 case llvm::Triple::aarch64_be:
675 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
676 break;
677 case llvm::Triple::arm:
678 case llvm::Triple::thumb:
679 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
680 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
681 else
682 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
683 break;
684 case llvm::Triple::armeb:
685 case llvm::Triple::thumbeb:
686 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
687 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
688 else
689 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
690 break;
691 case llvm::Triple::mips:
692 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
693 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs;
694 else
695 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
696 break;
697 case llvm::Triple::mipsel:
698 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
699 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs;
700 else
701 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
702 break;
703 case llvm::Triple::mips64:
704 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
705 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
706 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs;
707 else
708 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs;
709 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
710 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs;
711 else
712 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
713 break;
714 case llvm::Triple::mips64el:
715 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
716 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
717 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs;
718 else
719 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs;
720 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
721 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs;
722 else
723 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
724 break;
725 case llvm::Triple::ppc:
726 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
727 break;
728 case llvm::Triple::ppcle:
729 MultiarchIncludeDirs = PPCLEMultiarchIncludeDirs;
730 break;
731 case llvm::Triple::ppc64:
732 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
733 break;
734 case llvm::Triple::ppc64le:
735 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
736 break;
737 case llvm::Triple::sparc:
738 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
739 break;
740 case llvm::Triple::sparcv9:
741 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
742 break;
743 case llvm::Triple::systemz:
744 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
745 break;
746 default:
747 break;
748 }
749
750 const std::string AndroidMultiarchIncludeDir =
751 std::string("/usr/include/") +
752 getMultiarchTriple(D, getTriple(), SysRoot);
753 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir};
754 if (getTriple().isAndroid())
755 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs;
756
757 for (StringRef Dir : MultiarchIncludeDirs) {
758 if (D.getVFS().exists(SysRoot + Dir)) {
759 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
760 break;
761 }
762 }
763
764 if (getTriple().getOS() == llvm::Triple::RTEMS)
765 return;
766
767 // Add an include of '/include' directly. This isn't provided by default by
768 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
769 // add even when Clang is acting as-if it were a system compiler.
770 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
771
772 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
773
774 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
775 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
776 }
777
addLibStdCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const778 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
779 llvm::opt::ArgStringList &CC1Args) const {
780 // Try generic GCC detection first.
781 if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args))
782 return;
783
784 // We need a detected GCC installation on Linux to provide libstdc++'s
785 // headers in odd Linuxish places.
786 if (!GCCInstallation.isValid())
787 return;
788
789 StringRef LibDir = GCCInstallation.getParentLibPath();
790 StringRef TripleStr = GCCInstallation.getTriple().str();
791 const Multilib &Multilib = GCCInstallation.getMultilib();
792 const GCCVersion &Version = GCCInstallation.getVersion();
793
794 const std::string LibStdCXXIncludePathCandidates[] = {
795 // Android standalone toolchain has C++ headers in yet another place.
796 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
797 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
798 // without a subdirectory corresponding to the gcc version.
799 LibDir.str() + "/../include/c++",
800 // Cray's gcc installation puts headers under "g++" without a
801 // version suffix.
802 LibDir.str() + "/../include/g++",
803 };
804
805 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
806 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
807 /*GCCMultiarchTriple*/ "",
808 /*TargetMultiarchTriple*/ "",
809 Multilib.includeSuffix(), DriverArgs, CC1Args))
810 break;
811 }
812 }
813
AddCudaIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const814 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
815 ArgStringList &CC1Args) const {
816 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
817 }
818
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const819 void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
820 ArgStringList &CC1Args) const {
821 RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
822 }
823
AddIAMCUIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const824 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
825 ArgStringList &CC1Args) const {
826 if (GCCInstallation.isValid()) {
827 CC1Args.push_back("-isystem");
828 CC1Args.push_back(DriverArgs.MakeArgString(
829 GCCInstallation.getParentLibPath() + "/../" +
830 GCCInstallation.getTriple().str() + "/include"));
831 }
832 }
833
isPIEDefault() const834 bool Linux::isPIEDefault() const {
835 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
836 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
837 }
838
isNoExecStackDefault() const839 bool Linux::isNoExecStackDefault() const {
840 return getTriple().isAndroid();
841 }
842
IsMathErrnoDefault() const843 bool Linux::IsMathErrnoDefault() const {
844 if (getTriple().isAndroid())
845 return false;
846 return Generic_ELF::IsMathErrnoDefault();
847 }
848
getSupportedSanitizers() const849 SanitizerMask Linux::getSupportedSanitizers() const {
850 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
851 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
852 const bool IsMIPS = getTriple().isMIPS32();
853 const bool IsMIPS64 = getTriple().isMIPS64();
854 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
855 getTriple().getArch() == llvm::Triple::ppc64le;
856 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
857 getTriple().getArch() == llvm::Triple::aarch64_be;
858 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
859 getTriple().getArch() == llvm::Triple::thumb ||
860 getTriple().getArch() == llvm::Triple::armeb ||
861 getTriple().getArch() == llvm::Triple::thumbeb;
862 const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
863 SanitizerMask Res = ToolChain::getSupportedSanitizers();
864 Res |= SanitizerKind::Address;
865 Res |= SanitizerKind::PointerCompare;
866 Res |= SanitizerKind::PointerSubtract;
867 Res |= SanitizerKind::Fuzzer;
868 Res |= SanitizerKind::FuzzerNoLink;
869 Res |= SanitizerKind::KernelAddress;
870 Res |= SanitizerKind::Memory;
871 Res |= SanitizerKind::Vptr;
872 Res |= SanitizerKind::SafeStack;
873 if (IsX86_64 || IsMIPS64 || IsAArch64)
874 Res |= SanitizerKind::DataFlow;
875 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
876 IsSystemZ)
877 Res |= SanitizerKind::Leak;
878 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
879 Res |= SanitizerKind::Thread;
880 if (IsX86_64)
881 Res |= SanitizerKind::KernelMemory;
882 if (IsX86 || IsX86_64)
883 Res |= SanitizerKind::Function;
884 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
885 IsPowerPC64)
886 Res |= SanitizerKind::Scudo;
887 if (IsX86_64 || IsAArch64) {
888 Res |= SanitizerKind::HWAddress;
889 Res |= SanitizerKind::KernelHWAddress;
890 }
891 return Res;
892 }
893
addProfileRTLibs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const894 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
895 llvm::opt::ArgStringList &CmdArgs) const {
896 // Add linker option -u__llvm_profile_runtime to cause runtime
897 // initialization module to be linked in.
898 if (needsProfileRT(Args))
899 CmdArgs.push_back(Args.MakeArgString(
900 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
901 ToolChain::addProfileRTLibs(Args, CmdArgs);
902 }
903
904 llvm::DenormalMode
getDefaultDenormalModeForType(const llvm::opt::ArgList & DriverArgs,const JobAction & JA,const llvm::fltSemantics * FPType) const905 Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
906 const JobAction &JA,
907 const llvm::fltSemantics *FPType) const {
908 switch (getTriple().getArch()) {
909 case llvm::Triple::x86:
910 case llvm::Triple::x86_64: {
911 std::string Unused;
912 // DAZ and FTZ are turned on in crtfastmath.o
913 if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
914 isFastMathRuntimeAvailable(DriverArgs, Unused))
915 return llvm::DenormalMode::getPreserveSign();
916 return llvm::DenormalMode::getIEEE();
917 }
918 default:
919 return llvm::DenormalMode::getIEEE();
920 }
921 }
922
addExtraOpts(llvm::opt::ArgStringList & CmdArgs) const923 void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
924 for (const auto &Opt : ExtraOpts)
925 CmdArgs.push_back(Opt.c_str());
926 }
927