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