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