blob: 4e9d0f1f9f791897eb14c73421b8787b6e86670d [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Linux.h"
11#include "Arch/ARM.h"
12#include "Arch/Mips.h"
13#include "Arch/PPC.h"
Alex Bradbury71f45452018-01-11 13:36:56 +000014#include "Arch/RISCV.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000015#include "CommonArgs.h"
16#include "clang/Basic/VirtualFileSystem.h"
17#include "clang/Config/config.h"
18#include "clang/Driver/Distro.h"
19#include "clang/Driver/Driver.h"
20#include "clang/Driver/Options.h"
21#include "clang/Driver/SanitizerArgs.h"
22#include "llvm/Option/ArgList.h"
23#include "llvm/ProfileData/InstrProf.h"
24#include "llvm/Support/Path.h"
25#include <system_error>
26
27using namespace clang::driver;
28using namespace clang::driver::toolchains;
29using namespace clang;
30using namespace llvm::opt;
31
32using tools::addPathIfExists;
33
34/// \brief 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.
40static std::string getMultiarchTriple(const Driver &D,
41 const llvm::Triple &TargetTriple,
42 StringRef SysRoot) {
43 llvm::Triple::EnvironmentType TargetEnvironment =
44 TargetTriple.getEnvironment();
45
46 // For most architectures, just use whatever we have rather than trying to be
47 // clever.
48 switch (TargetTriple.getArch()) {
49 default:
50 break;
51
52 // We use the existence of '/lib/<triple>' as a directory to detect some
53 // common linux triples that don't quite match the Clang triple for both
54 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
55 // regardless of what the actual target triple is.
56 case llvm::Triple::arm:
57 case llvm::Triple::thumb:
58 if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
59 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf"))
60 return "arm-linux-gnueabihf";
61 } else {
62 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi"))
63 return "arm-linux-gnueabi";
64 }
65 break;
66 case llvm::Triple::armeb:
67 case llvm::Triple::thumbeb:
68 if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
69 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf"))
70 return "armeb-linux-gnueabihf";
71 } else {
72 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi"))
73 return "armeb-linux-gnueabi";
74 }
75 break;
76 case llvm::Triple::x86:
77 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu"))
78 return "i386-linux-gnu";
79 break;
80 case llvm::Triple::x86_64:
81 // We don't want this for x32, otherwise it will match x86_64 libs
82 if (TargetEnvironment != llvm::Triple::GNUX32 &&
83 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu"))
84 return "x86_64-linux-gnu";
85 break;
86 case llvm::Triple::aarch64:
87 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu"))
88 return "aarch64-linux-gnu";
89 break;
90 case llvm::Triple::aarch64_be:
91 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
92 return "aarch64_be-linux-gnu";
93 break;
94 case llvm::Triple::mips:
95 if (D.getVFS().exists(SysRoot + "/lib/mips-linux-gnu"))
96 return "mips-linux-gnu";
97 break;
98 case llvm::Triple::mipsel:
99 if (D.getVFS().exists(SysRoot + "/lib/mipsel-linux-gnu"))
100 return "mipsel-linux-gnu";
101 break;
102 case llvm::Triple::mips64:
103 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu"))
104 return "mips64-linux-gnu";
105 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnuabi64"))
106 return "mips64-linux-gnuabi64";
107 break;
108 case llvm::Triple::mips64el:
109 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu"))
110 return "mips64el-linux-gnu";
111 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnuabi64"))
112 return "mips64el-linux-gnuabi64";
113 break;
114 case llvm::Triple::ppc:
115 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
116 return "powerpc-linux-gnuspe";
117 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu"))
118 return "powerpc-linux-gnu";
119 break;
120 case llvm::Triple::ppc64:
121 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu"))
122 return "powerpc64-linux-gnu";
123 break;
124 case llvm::Triple::ppc64le:
125 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
126 return "powerpc64le-linux-gnu";
127 break;
128 case llvm::Triple::sparc:
129 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu"))
130 return "sparc-linux-gnu";
131 break;
132 case llvm::Triple::sparcv9:
133 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu"))
134 return "sparc64-linux-gnu";
135 break;
136 case llvm::Triple::systemz:
137 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu"))
138 return "s390x-linux-gnu";
139 break;
140 }
141 return TargetTriple.str();
142}
143
144static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
145 if (tools::isMipsArch(Triple.getArch())) {
146 if (Triple.isAndroid()) {
147 StringRef CPUName;
148 StringRef ABIName;
149 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
150 if (CPUName == "mips32r6")
151 return "libr6";
152 if (CPUName == "mips32r2")
153 return "libr2";
154 }
155 // lib32 directory has a special meaning on MIPS targets.
156 // It contains N32 ABI binaries. Use this folder if produce
157 // code for N32 ABI only.
158 if (tools::mips::hasMipsAbiArg(Args, "n32"))
159 return "lib32";
160 return Triple.isArch32Bit() ? "lib" : "lib64";
161 }
162
163 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
164 // using that variant while targeting other architectures causes problems
165 // because the libraries are laid out in shared system roots that can't cope
166 // with a 'lib32' library search path being considered. So we only enable
167 // them when we know we may need it.
168 //
169 // FIXME: This is a bit of a hack. We should really unify this code for
170 // reasoning about oslibdir spellings with the lib dir spellings in the
171 // GCCInstallationDetector, but that is a more significant refactoring.
172 if (Triple.getArch() == llvm::Triple::x86 ||
173 Triple.getArch() == llvm::Triple::ppc)
174 return "lib32";
175
176 if (Triple.getArch() == llvm::Triple::x86_64 &&
177 Triple.getEnvironment() == llvm::Triple::GNUX32)
178 return "libx32";
179
Alex Bradbury71f45452018-01-11 13:36:56 +0000180 if (Triple.getArch() == llvm::Triple::riscv32)
181 return "lib32";
182
David L. Jonesf561aba2017-03-08 01:02:16 +0000183 return Triple.isArch32Bit() ? "lib" : "lib64";
184}
185
186static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
187 const Multilib &Multilib,
188 StringRef InstallPath,
189 ToolChain::path_list &Paths) {
190 if (const auto &PathsCallback = Multilibs.filePathsCallback())
191 for (const auto &Path : PathsCallback(Multilib))
192 addPathIfExists(D, InstallPath + Path, Paths);
193}
194
195Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
196 : Generic_ELF(D, Triple, Args) {
197 GCCInstallation.init(Triple, Args);
198 Multilibs = GCCInstallation.getMultilibs();
199 llvm::Triple::ArchType Arch = Triple.getArch();
200 std::string SysRoot = computeSysRoot();
201
202 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
203 // least) put various tools in a triple-prefixed directory off of the parent
204 // of the GCC installation. We use the GCC triple here to ensure that we end
205 // up with tools that support the same amount of cross compiling as the
206 // detected GCC installation. For example, if we find a GCC installation
207 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
208 // used to target i386.
209 // FIXME: This seems unlikely to be Linux-specific.
210 ToolChain::path_list &PPaths = getProgramPaths();
211 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
212 GCCInstallation.getTriple().str() + "/bin")
213 .str());
214
215 Distro Distro(D.getVFS());
216
Martell Malone13c5d732017-11-19 00:08:12 +0000217 if (Distro.IsAlpineLinux()) {
218 ExtraOpts.push_back("-z");
219 ExtraOpts.push_back("now");
220 }
221
222 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux()) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000223 ExtraOpts.push_back("-z");
224 ExtraOpts.push_back("relro");
225 }
226
227 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
228 ExtraOpts.push_back("-X");
229
230 const bool IsAndroid = Triple.isAndroid();
231 const bool IsMips = tools::isMipsArch(Arch);
232 const bool IsHexagon = Arch == llvm::Triple::hexagon;
Alex Bradbury71f45452018-01-11 13:36:56 +0000233 const bool IsRISCV =
234 Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64;
David L. Jonesf561aba2017-03-08 01:02:16 +0000235
236 if (IsMips && !SysRoot.empty())
237 ExtraOpts.push_back("--sysroot=" + SysRoot);
238
239 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
240 // and the MIPS ABI require .dynsym to be sorted in different ways.
241 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
242 // ABI requires a mapping between the GOT and the symbol table.
243 // Android loader does not support .gnu.hash.
244 // Hexagon linker/loader does not support .gnu.hash
245 if (!IsMips && !IsAndroid && !IsHexagon) {
Martell Malone13c5d732017-11-19 00:08:12 +0000246 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
David L. Jonesf561aba2017-03-08 01:02:16 +0000247 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick))
248 ExtraOpts.push_back("--hash-style=gnu");
249
250 if (Distro.IsDebian() || Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid ||
251 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic)
252 ExtraOpts.push_back("--hash-style=both");
253 }
254
255 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6)
256 ExtraOpts.push_back("--no-add-needed");
257
258#ifdef ENABLE_LINKER_BUILD_ID
259 ExtraOpts.push_back("--build-id");
260#endif
261
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000262 if (IsAndroid || Distro.IsOpenSUSE())
David L. Jonesf561aba2017-03-08 01:02:16 +0000263 ExtraOpts.push_back("--enable-new-dtags");
264
265 // The selection of paths to try here is designed to match the patterns which
266 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
267 // This was determined by running GCC in a fake filesystem, creating all
268 // possible permutations of these directories, and seeing which ones it added
269 // to the link paths.
270 path_list &Paths = getFilePaths();
271
272 const std::string OSLibDir = getOSLibDir(Triple, Args);
273 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
274
275 // Add the multilib suffixed paths where they are available.
276 if (GCCInstallation.isValid()) {
277 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
278 const std::string &LibPath = GCCInstallation.getParentLibPath();
279 const Multilib &Multilib = GCCInstallation.getMultilib();
280 const MultilibSet &Multilibs = GCCInstallation.getMultilibs();
281
282 // Add toolchain / multilib specific file paths.
283 addMultilibsFilePaths(D, Multilibs, Multilib,
284 GCCInstallation.getInstallPath(), Paths);
285
286 // Sourcery CodeBench MIPS toolchain holds some libraries under
287 // a biarch-like suffix of the GCC installation.
288 addPathIfExists(D, GCCInstallation.getInstallPath() + Multilib.gccSuffix(),
289 Paths);
290
291 // GCC cross compiling toolchains will install target libraries which ship
292 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
293 // any part of the GCC installation in
294 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
295 // debatable, but is the reality today. We need to search this tree even
296 // when we have a sysroot somewhere else. It is the responsibility of
297 // whomever is doing the cross build targeting a sysroot using a GCC
298 // installation that is *not* within the system root to ensure two things:
299 //
300 // 1) Any DSOs that are linked in from this tree or from the install path
301 // above must be present on the system root and found via an
302 // appropriate rpath.
303 // 2) There must not be libraries installed into
304 // <prefix>/<triple>/<libdir> unless they should be preferred over
305 // those within the system root.
306 //
307 // Note that this matches the GCC behavior. See the below comment for where
308 // Clang diverges from GCC's behavior.
309 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" +
310 OSLibDir + Multilib.osSuffix(),
311 Paths);
312
313 // If the GCC installation we found is inside of the sysroot, we want to
314 // prefer libraries installed in the parent prefix of the GCC installation.
315 // It is important to *not* use these paths when the GCC installation is
316 // outside of the system root as that can pick up unintended libraries.
317 // This usually happens when there is an external cross compiler on the
318 // host system, and a more minimal sysroot available that is the target of
319 // the cross. Note that GCC does include some of these directories in some
320 // configurations but this seems somewhere between questionable and simply
321 // a bug.
322 if (StringRef(LibPath).startswith(SysRoot)) {
323 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths);
324 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
325 }
326 }
327
328 // Similar to the logic for GCC above, if we currently running Clang inside
329 // of the requested system root, add its parent library paths to
330 // those searched.
331 // FIXME: It's not clear whether we should use the driver's installed
332 // directory ('Dir' below) or the ResourceDir.
333 if (StringRef(D.Dir).startswith(SysRoot)) {
334 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
335 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
336 }
337
338 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
339 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
340 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
341 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
Alex Bradbury71f45452018-01-11 13:36:56 +0000342 if (IsRISCV) {
343 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
344 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
345 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
346 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000347
348 // Try walking via the GCC triple path in case of biarch or multiarch GCC
349 // installations with strange symlinks.
350 if (GCCInstallation.isValid()) {
351 addPathIfExists(D,
352 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
353 "/../../" + OSLibDir,
354 Paths);
355
356 // Add the 'other' biarch variant path
357 Multilib BiarchSibling;
358 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
359 addPathIfExists(D, GCCInstallation.getInstallPath() +
360 BiarchSibling.gccSuffix(),
361 Paths);
362 }
363
364 // See comments above on the multilib variant for details of why this is
365 // included even from outside the sysroot.
366 const std::string &LibPath = GCCInstallation.getParentLibPath();
367 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
368 const Multilib &Multilib = GCCInstallation.getMultilib();
369 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" +
370 Multilib.osSuffix(),
371 Paths);
372
373 // See comments above on the multilib variant for details of why this is
374 // only included from within the sysroot.
375 if (StringRef(LibPath).startswith(SysRoot))
376 addPathIfExists(D, LibPath, Paths);
377 }
378
379 // Similar to the logic for GCC above, if we are currently running Clang
380 // inside of the requested system root, add its parent library path to those
381 // searched.
382 // FIXME: It's not clear whether we should use the driver's installed
383 // directory ('Dir' below) or the ResourceDir.
384 if (StringRef(D.Dir).startswith(SysRoot))
385 addPathIfExists(D, D.Dir + "/../lib", Paths);
386
387 addPathIfExists(D, SysRoot + "/lib", Paths);
388 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
389}
390
David L. Jonesf561aba2017-03-08 01:02:16 +0000391bool Linux::HasNativeLLVMSupport() const { return true; }
392
393Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
394
395Tool *Linux::buildAssembler() const {
396 return new tools::gnutools::Assembler(*this);
397}
398
399std::string Linux::computeSysRoot() const {
400 if (!getDriver().SysRoot.empty())
401 return getDriver().SysRoot;
402
403 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch()))
404 return std::string();
405
406 // Standalone MIPS toolchains use different names for sysroot folder
407 // and put it into different places. Here we try to check some known
408 // variants.
409
410 const StringRef InstallDir = GCCInstallation.getInstallPath();
411 const StringRef TripleStr = GCCInstallation.getTriple().str();
412 const Multilib &Multilib = GCCInstallation.getMultilib();
413
414 std::string Path =
415 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
416 .str();
417
418 if (getVFS().exists(Path))
419 return Path;
420
421 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
422
423 if (getVFS().exists(Path))
424 return Path;
425
426 return std::string();
427}
428
429std::string Linux::getDynamicLinker(const ArgList &Args) const {
430 const llvm::Triple::ArchType Arch = getArch();
431 const llvm::Triple &Triple = getTriple();
432
433 const Distro Distro(getDriver().getVFS());
434
435 if (Triple.isAndroid())
436 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
437
438 if (Triple.isMusl()) {
439 std::string ArchName;
440 bool IsArm = false;
441
442 switch (Arch) {
443 case llvm::Triple::arm:
444 case llvm::Triple::thumb:
445 ArchName = "arm";
446 IsArm = true;
447 break;
448 case llvm::Triple::armeb:
449 case llvm::Triple::thumbeb:
450 ArchName = "armeb";
451 IsArm = true;
452 break;
453 default:
454 ArchName = Triple.getArchName().str();
455 }
456 if (IsArm &&
457 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
458 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
459 ArchName += "hf";
460
461 return "/lib/ld-musl-" + ArchName + ".so.1";
462 }
463
464 std::string LibDir;
465 std::string Loader;
466
467 switch (Arch) {
468 default:
469 llvm_unreachable("unsupported architecture");
470
471 case llvm::Triple::aarch64:
472 LibDir = "lib";
473 Loader = "ld-linux-aarch64.so.1";
474 break;
475 case llvm::Triple::aarch64_be:
476 LibDir = "lib";
477 Loader = "ld-linux-aarch64_be.so.1";
478 break;
479 case llvm::Triple::arm:
480 case llvm::Triple::thumb:
481 case llvm::Triple::armeb:
482 case llvm::Triple::thumbeb: {
483 const bool HF =
484 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
485 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
486
487 LibDir = "lib";
488 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
489 break;
490 }
491 case llvm::Triple::mips:
492 case llvm::Triple::mipsel:
493 case llvm::Triple::mips64:
494 case llvm::Triple::mips64el: {
495 bool LE = (Triple.getArch() == llvm::Triple::mipsel) ||
496 (Triple.getArch() == llvm::Triple::mips64el);
497 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
498
499 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
500
501 if (tools::mips::isUCLibc(Args))
502 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
503 else if (!Triple.hasEnvironment() &&
504 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
505 Loader = LE ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
506 else
507 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
508
509 break;
510 }
511 case llvm::Triple::ppc:
512 LibDir = "lib";
513 Loader = "ld.so.1";
514 break;
515 case llvm::Triple::ppc64:
516 LibDir = "lib64";
517 Loader =
518 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
519 break;
520 case llvm::Triple::ppc64le:
521 LibDir = "lib64";
522 Loader =
523 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
524 break;
Alex Bradbury71f45452018-01-11 13:36:56 +0000525 case llvm::Triple::riscv32: {
526 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
527 LibDir = "lib";
528 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
529 break;
530 }
531 case llvm::Triple::riscv64: {
532 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
533 LibDir = "lib";
534 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
535 break;
536 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000537 case llvm::Triple::sparc:
538 case llvm::Triple::sparcel:
539 LibDir = "lib";
540 Loader = "ld-linux.so.2";
541 break;
542 case llvm::Triple::sparcv9:
543 LibDir = "lib64";
544 Loader = "ld-linux.so.2";
545 break;
546 case llvm::Triple::systemz:
547 LibDir = "lib";
548 Loader = "ld64.so.1";
549 break;
550 case llvm::Triple::x86:
551 LibDir = "lib";
552 Loader = "ld-linux.so.2";
553 break;
554 case llvm::Triple::x86_64: {
555 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
556
557 LibDir = X32 ? "libx32" : "lib64";
558 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
559 break;
560 }
561 }
562
563 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor ||
564 Triple.getVendor() == llvm::Triple::PC))
565 return "/usr/" + Triple.str() + "/lib/" + Loader;
566 return "/" + LibDir + "/" + Loader;
567}
568
569void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
570 ArgStringList &CC1Args) const {
571 const Driver &D = getDriver();
572 std::string SysRoot = computeSysRoot();
573
574 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
575 return;
576
577 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
578 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
579
580 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
581 SmallString<128> P(D.ResourceDir);
582 llvm::sys::path::append(P, "include");
583 addSystemInclude(DriverArgs, CC1Args, P);
584 }
585
586 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
587 return;
588
589 // Check for configure-time C include directories.
590 StringRef CIncludeDirs(C_INCLUDE_DIRS);
591 if (CIncludeDirs != "") {
592 SmallVector<StringRef, 5> dirs;
593 CIncludeDirs.split(dirs, ":");
594 for (StringRef dir : dirs) {
595 StringRef Prefix =
596 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
597 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
598 }
599 return;
600 }
601
602 // Lacking those, try to detect the correct set of system includes for the
603 // target triple.
604
605 // Add include directories specific to the selected multilib set and multilib.
606 if (GCCInstallation.isValid()) {
607 const auto &Callback = Multilibs.includeDirsCallback();
608 if (Callback) {
609 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
610 addExternCSystemIncludeIfExists(
611 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
612 }
613 }
614
615 // Implement generic Debian multiarch support.
616 const StringRef X86_64MultiarchIncludeDirs[] = {
617 "/usr/include/x86_64-linux-gnu",
618
619 // FIXME: These are older forms of multiarch. It's not clear that they're
620 // in use in any released version of Debian, so we should consider
621 // removing them.
622 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
623 const StringRef X86MultiarchIncludeDirs[] = {
624 "/usr/include/i386-linux-gnu",
625
626 // FIXME: These are older forms of multiarch. It's not clear that they're
627 // in use in any released version of Debian, so we should consider
628 // removing them.
629 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
630 "/usr/include/i486-linux-gnu"};
631 const StringRef AArch64MultiarchIncludeDirs[] = {
632 "/usr/include/aarch64-linux-gnu"};
633 const StringRef ARMMultiarchIncludeDirs[] = {
634 "/usr/include/arm-linux-gnueabi"};
635 const StringRef ARMHFMultiarchIncludeDirs[] = {
636 "/usr/include/arm-linux-gnueabihf"};
637 const StringRef ARMEBMultiarchIncludeDirs[] = {
638 "/usr/include/armeb-linux-gnueabi"};
639 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
640 "/usr/include/armeb-linux-gnueabihf"};
641 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
642 const StringRef MIPSELMultiarchIncludeDirs[] = {
643 "/usr/include/mipsel-linux-gnu"};
644 const StringRef MIPS64MultiarchIncludeDirs[] = {
645 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"};
646 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
647 "/usr/include/mips64el-linux-gnu",
648 "/usr/include/mips64el-linux-gnuabi64"};
649 const StringRef PPCMultiarchIncludeDirs[] = {
650 "/usr/include/powerpc-linux-gnu"};
651 const StringRef PPC64MultiarchIncludeDirs[] = {
652 "/usr/include/powerpc64-linux-gnu"};
653 const StringRef PPC64LEMultiarchIncludeDirs[] = {
654 "/usr/include/powerpc64le-linux-gnu"};
655 const StringRef SparcMultiarchIncludeDirs[] = {
656 "/usr/include/sparc-linux-gnu"};
657 const StringRef Sparc64MultiarchIncludeDirs[] = {
658 "/usr/include/sparc64-linux-gnu"};
659 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
660 "/usr/include/s390x-linux-gnu"};
661 ArrayRef<StringRef> MultiarchIncludeDirs;
662 switch (getTriple().getArch()) {
663 case llvm::Triple::x86_64:
664 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
665 break;
666 case llvm::Triple::x86:
667 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
668 break;
669 case llvm::Triple::aarch64:
670 case llvm::Triple::aarch64_be:
671 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
672 break;
673 case llvm::Triple::arm:
674 case llvm::Triple::thumb:
675 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
676 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
677 else
678 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
679 break;
680 case llvm::Triple::armeb:
681 case llvm::Triple::thumbeb:
682 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
683 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
684 else
685 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
686 break;
687 case llvm::Triple::mips:
688 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
689 break;
690 case llvm::Triple::mipsel:
691 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
692 break;
693 case llvm::Triple::mips64:
694 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
695 break;
696 case llvm::Triple::mips64el:
697 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
698 break;
699 case llvm::Triple::ppc:
700 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
701 break;
702 case llvm::Triple::ppc64:
703 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
704 break;
705 case llvm::Triple::ppc64le:
706 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
707 break;
708 case llvm::Triple::sparc:
709 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
710 break;
711 case llvm::Triple::sparcv9:
712 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
713 break;
714 case llvm::Triple::systemz:
715 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
716 break;
717 default:
718 break;
719 }
720 for (StringRef Dir : MultiarchIncludeDirs) {
721 if (D.getVFS().exists(SysRoot + Dir)) {
722 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
723 break;
724 }
725 }
726
727 if (getTriple().getOS() == llvm::Triple::RTEMS)
728 return;
729
730 // Add an include of '/include' directly. This isn't provided by default by
731 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
732 // add even when Clang is acting as-if it were a system compiler.
733 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
734
735 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
736}
737
738static std::string DetectLibcxxIncludePath(StringRef base) {
739 std::error_code EC;
740 int MaxVersion = 0;
741 std::string MaxVersionString = "";
742 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE;
743 LI = LI.increment(EC)) {
744 StringRef VersionText = llvm::sys::path::filename(LI->path());
745 int Version;
746 if (VersionText[0] == 'v' &&
747 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
748 if (Version > MaxVersion) {
749 MaxVersion = Version;
750 MaxVersionString = VersionText;
751 }
752 }
753 }
754 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
755}
756
757std::string Linux::findLibCxxIncludePath() const {
758 const std::string LibCXXIncludePathCandidates[] = {
759 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"),
760 // If this is a development, non-installed, clang, libcxx will
761 // not be found at ../include/c++ but it likely to be found at
762 // one of the following two locations:
763 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/local/include/c++"),
764 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/include/c++") };
765 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
766 if (IncludePath.empty() || !getVFS().exists(IncludePath))
767 continue;
768 // Use the first candidate that exists.
769 return IncludePath;
770 }
771 return "";
772}
773
774void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
775 llvm::opt::ArgStringList &CC1Args) const {
776 // We need a detected GCC installation on Linux to provide libstdc++'s
777 // headers.
778 if (!GCCInstallation.isValid())
779 return;
780
781 // By default, look for the C++ headers in an include directory adjacent to
782 // the lib directory of the GCC installation. Note that this is expect to be
783 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
784 StringRef LibDir = GCCInstallation.getParentLibPath();
785 StringRef InstallDir = GCCInstallation.getInstallPath();
786 StringRef TripleStr = GCCInstallation.getTriple().str();
787 const Multilib &Multilib = GCCInstallation.getMultilib();
788 const std::string GCCMultiarchTriple = getMultiarchTriple(
789 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot);
790 const std::string TargetMultiarchTriple =
791 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
792 const GCCVersion &Version = GCCInstallation.getVersion();
793
794 // The primary search for libstdc++ supports multiarch variants.
795 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
796 "/c++/" + Version.Text, TripleStr,
797 GCCMultiarchTriple, TargetMultiarchTriple,
798 Multilib.includeSuffix(), DriverArgs, CC1Args))
799 return;
800
801 // Otherwise, fall back on a bunch of options which don't use multiarch
802 // layouts for simplicity.
803 const std::string LibStdCXXIncludePathCandidates[] = {
804 // Gentoo is weird and places its headers inside the GCC install,
805 // so if the first attempt to find the headers fails, try these patterns.
806 InstallDir.str() + "/include/g++-v" + Version.Text,
807 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
808 Version.MinorStr,
809 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
810 // Android standalone toolchain has C++ headers in yet another place.
811 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
812 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
813 // without a subdirectory corresponding to the gcc version.
814 LibDir.str() + "/../include/c++",
815 };
816
817 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
818 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
819 /*GCCMultiarchTriple*/ "",
820 /*TargetMultiarchTriple*/ "",
821 Multilib.includeSuffix(), DriverArgs, CC1Args))
822 break;
823 }
824}
825
826void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
827 ArgStringList &CC1Args) const {
828 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
829}
830
831void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
832 ArgStringList &CC1Args) const {
833 if (GCCInstallation.isValid()) {
834 CC1Args.push_back("-isystem");
835 CC1Args.push_back(DriverArgs.MakeArgString(
836 GCCInstallation.getParentLibPath() + "/../" +
837 GCCInstallation.getTriple().str() + "/include"));
838 }
839}
840
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000841bool Linux::isPIEDefault() const {
842 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
Martell Malone13c5d732017-11-19 00:08:12 +0000843 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000844}
David L. Jonesf561aba2017-03-08 01:02:16 +0000845
846SanitizerMask Linux::getSupportedSanitizers() const {
847 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
848 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
Simon Dardis2dd93772018-01-25 10:09:33 +0000849 const bool IsMIPS = getTriple().getArch() == llvm::Triple::mips ||
850 getTriple().getArch() == llvm::Triple::mipsel;
David L. Jonesf561aba2017-03-08 01:02:16 +0000851 const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 ||
852 getTriple().getArch() == llvm::Triple::mips64el;
853 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
854 getTriple().getArch() == llvm::Triple::ppc64le;
855 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
856 getTriple().getArch() == llvm::Triple::aarch64_be;
Maxim Ostapenko2084b6b2017-04-11 07:22:11 +0000857 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
Galina Kistanova8a338ea2017-06-03 16:47:06 +0000858 getTriple().getArch() == llvm::Triple::thumb ||
859 getTriple().getArch() == llvm::Triple::armeb ||
860 getTriple().getArch() == llvm::Triple::thumbeb;
David L. Jonesf561aba2017-03-08 01:02:16 +0000861 SanitizerMask Res = ToolChain::getSupportedSanitizers();
862 Res |= SanitizerKind::Address;
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000863 Res |= SanitizerKind::Fuzzer;
George Karpenkov33613f62017-08-11 17:22:58 +0000864 Res |= SanitizerKind::FuzzerNoLink;
David L. Jonesf561aba2017-03-08 01:02:16 +0000865 Res |= SanitizerKind::KernelAddress;
866 Res |= SanitizerKind::Vptr;
867 Res |= SanitizerKind::SafeStack;
868 if (IsX86_64 || IsMIPS64 || IsAArch64)
869 Res |= SanitizerKind::DataFlow;
Alex Shlyapnikov797bdbb2017-10-26 03:09:53 +0000870 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64)
David L. Jonesf561aba2017-03-08 01:02:16 +0000871 Res |= SanitizerKind::Leak;
872 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
873 Res |= SanitizerKind::Thread;
874 if (IsX86_64 || IsMIPS64 || IsPowerPC64 || IsAArch64)
875 Res |= SanitizerKind::Memory;
876 if (IsX86_64 || IsMIPS64)
877 Res |= SanitizerKind::Efficiency;
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +0000878 if (IsX86 || IsX86_64)
David L. Jonesf561aba2017-03-08 01:02:16 +0000879 Res |= SanitizerKind::Function;
Simon Dardis2dd93772018-01-25 10:09:33 +0000880 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch)
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +0000881 Res |= SanitizerKind::Scudo;
Evgeniy Stepanov12817e52017-12-09 01:32:07 +0000882 if (IsAArch64)
883 Res |= SanitizerKind::HWAddress;
David L. Jonesf561aba2017-03-08 01:02:16 +0000884 return Res;
885}
886
887void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
888 llvm::opt::ArgStringList &CmdArgs) const {
889 if (!needsProfileRT(Args)) return;
890
891 // Add linker option -u__llvm_runtime_variable to cause runtime
892 // initialization module to be linked in.
893 if (!Args.hasArg(options::OPT_coverage))
894 CmdArgs.push_back(Args.MakeArgString(
895 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
896 ToolChain::addProfileRTLibs(Args, CmdArgs);
897}