blob: cd129297623f9d48a68f0e383c82b659e1dcdea1 [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"
David L. Jonesf561aba2017-03-08 01:02:16 +000016#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"
Dan Albertf4832792018-04-17 20:42:07 +000024#include "llvm/Support/ScopedPrinter.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000025#include "llvm/Support/VirtualFileSystem.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000026#include <system_error>
27
28using namespace clang::driver;
29using namespace clang::driver::toolchains;
30using namespace clang;
31using namespace llvm::opt;
32
33using tools::addPathIfExists;
34
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000035/// Get our best guess at the multiarch triple for a target.
David L. Jonesf561aba2017-03-08 01:02:16 +000036///
37/// Debian-based systems are starting to use a multiarch setup where they use
38/// a target-triple directory in the library and header search paths.
39/// Unfortunately, this triple does not align with the vanilla target triple,
40/// so we provide a rough mapping here.
41static std::string getMultiarchTriple(const Driver &D,
42 const llvm::Triple &TargetTriple,
43 StringRef SysRoot) {
44 llvm::Triple::EnvironmentType TargetEnvironment =
45 TargetTriple.getEnvironment();
Dan Alberte00799e2018-04-04 21:28:34 +000046 bool IsAndroid = TargetTriple.isAndroid();
Simon Atanasyaneaab2b72018-10-16 14:29:27 +000047 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
David L. Jonesf561aba2017-03-08 01:02:16 +000048
49 // For most architectures, just use whatever we have rather than trying to be
50 // clever.
51 switch (TargetTriple.getArch()) {
52 default:
53 break;
54
55 // We use the existence of '/lib/<triple>' as a directory to detect some
56 // common linux triples that don't quite match the Clang triple for both
57 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
58 // regardless of what the actual target triple is.
59 case llvm::Triple::arm:
60 case llvm::Triple::thumb:
Dan Alberte00799e2018-04-04 21:28:34 +000061 if (IsAndroid) {
62 return "arm-linux-androideabi";
63 } else if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
David L. Jonesf561aba2017-03-08 01:02:16 +000064 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf"))
65 return "arm-linux-gnueabihf";
66 } else {
67 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi"))
68 return "arm-linux-gnueabi";
69 }
70 break;
71 case llvm::Triple::armeb:
72 case llvm::Triple::thumbeb:
73 if (TargetEnvironment == llvm::Triple::GNUEABIHF) {
74 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf"))
75 return "armeb-linux-gnueabihf";
76 } else {
77 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi"))
78 return "armeb-linux-gnueabi";
79 }
80 break;
81 case llvm::Triple::x86:
Dan Alberte00799e2018-04-04 21:28:34 +000082 if (IsAndroid)
83 return "i686-linux-android";
David L. Jonesf561aba2017-03-08 01:02:16 +000084 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu"))
85 return "i386-linux-gnu";
86 break;
87 case llvm::Triple::x86_64:
Dan Alberte00799e2018-04-04 21:28:34 +000088 if (IsAndroid)
89 return "x86_64-linux-android";
David L. Jonesf561aba2017-03-08 01:02:16 +000090 // We don't want this for x32, otherwise it will match x86_64 libs
91 if (TargetEnvironment != llvm::Triple::GNUX32 &&
92 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu"))
93 return "x86_64-linux-gnu";
94 break;
95 case llvm::Triple::aarch64:
Dan Alberte00799e2018-04-04 21:28:34 +000096 if (IsAndroid)
97 return "aarch64-linux-android";
David L. Jonesf561aba2017-03-08 01:02:16 +000098 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu"))
99 return "aarch64-linux-gnu";
100 break;
101 case llvm::Triple::aarch64_be:
102 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
103 return "aarch64_be-linux-gnu";
104 break;
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000105 case llvm::Triple::mips: {
106 std::string Arch = IsMipsR6 ? "mipsisa32r6" : "mips";
107 if (D.getVFS().exists(SysRoot + "/lib/" + Arch + "-linux-gnu"))
108 return Arch + "-linux-gnu";
David L. Jonesf561aba2017-03-08 01:02:16 +0000109 break;
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000110 }
111 case llvm::Triple::mipsel: {
Dan Alberte00799e2018-04-04 21:28:34 +0000112 if (IsAndroid)
113 return "mipsel-linux-android";
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000114 std::string Arch = IsMipsR6 ? "mipsisa32r6el" : "mipsel";
115 if (D.getVFS().exists(SysRoot + "/lib/" + Arch + "-linux-gnu"))
116 return Arch + "-linux-gnu";
David L. Jonesf561aba2017-03-08 01:02:16 +0000117 break;
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000118 }
119 case llvm::Triple::mips64: {
120 std::string Arch = IsMipsR6 ? "mipsisa64r6" : "mips64";
121 std::string ABI = llvm::Triple::getEnvironmentTypeName(TargetEnvironment);
122 if (D.getVFS().exists(SysRoot + "/lib/" + Arch + "-linux-" + ABI))
123 return Arch + "-linux-" + ABI;
David L. Jonesf561aba2017-03-08 01:02:16 +0000124 break;
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000125 }
126 case llvm::Triple::mips64el: {
Dan Alberte00799e2018-04-04 21:28:34 +0000127 if (IsAndroid)
128 return "mips64el-linux-android";
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000129 std::string Arch = IsMipsR6 ? "mipsisa64r6el" : "mips64el";
130 std::string ABI = llvm::Triple::getEnvironmentTypeName(TargetEnvironment);
131 if (D.getVFS().exists(SysRoot + "/lib/" + Arch + "-linux-" + ABI))
132 return Arch + "-linux-" + ABI;
David L. Jonesf561aba2017-03-08 01:02:16 +0000133 break;
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000134 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000135 case llvm::Triple::ppc:
136 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
137 return "powerpc-linux-gnuspe";
138 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu"))
139 return "powerpc-linux-gnu";
140 break;
141 case llvm::Triple::ppc64:
142 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu"))
143 return "powerpc64-linux-gnu";
144 break;
145 case llvm::Triple::ppc64le:
146 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
147 return "powerpc64le-linux-gnu";
148 break;
149 case llvm::Triple::sparc:
150 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu"))
151 return "sparc-linux-gnu";
152 break;
153 case llvm::Triple::sparcv9:
154 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu"))
155 return "sparc64-linux-gnu";
156 break;
157 case llvm::Triple::systemz:
158 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu"))
159 return "s390x-linux-gnu";
160 break;
161 }
162 return TargetTriple.str();
163}
164
165static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
Alexander Richardson742553d2018-06-25 16:49:52 +0000166 if (Triple.isMIPS()) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000167 if (Triple.isAndroid()) {
168 StringRef CPUName;
169 StringRef ABIName;
170 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
171 if (CPUName == "mips32r6")
172 return "libr6";
173 if (CPUName == "mips32r2")
174 return "libr2";
175 }
176 // lib32 directory has a special meaning on MIPS targets.
177 // It contains N32 ABI binaries. Use this folder if produce
178 // code for N32 ABI only.
179 if (tools::mips::hasMipsAbiArg(Args, "n32"))
180 return "lib32";
181 return Triple.isArch32Bit() ? "lib" : "lib64";
182 }
183
184 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
185 // using that variant while targeting other architectures causes problems
186 // because the libraries are laid out in shared system roots that can't cope
187 // with a 'lib32' library search path being considered. So we only enable
188 // them when we know we may need it.
189 //
190 // FIXME: This is a bit of a hack. We should really unify this code for
191 // reasoning about oslibdir spellings with the lib dir spellings in the
192 // GCCInstallationDetector, but that is a more significant refactoring.
193 if (Triple.getArch() == llvm::Triple::x86 ||
194 Triple.getArch() == llvm::Triple::ppc)
195 return "lib32";
196
197 if (Triple.getArch() == llvm::Triple::x86_64 &&
198 Triple.getEnvironment() == llvm::Triple::GNUX32)
199 return "libx32";
200
Alex Bradbury71f45452018-01-11 13:36:56 +0000201 if (Triple.getArch() == llvm::Triple::riscv32)
202 return "lib32";
203
David L. Jonesf561aba2017-03-08 01:02:16 +0000204 return Triple.isArch32Bit() ? "lib" : "lib64";
205}
206
207static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
208 const Multilib &Multilib,
209 StringRef InstallPath,
210 ToolChain::path_list &Paths) {
211 if (const auto &PathsCallback = Multilibs.filePathsCallback())
212 for (const auto &Path : PathsCallback(Multilib))
213 addPathIfExists(D, InstallPath + Path, Paths);
214}
215
216Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
217 : Generic_ELF(D, Triple, Args) {
218 GCCInstallation.init(Triple, Args);
219 Multilibs = GCCInstallation.getMultilibs();
Christian Bruel6ccc4a72018-09-06 14:03:44 +0000220 SelectedMultilib = GCCInstallation.getMultilib();
David L. Jonesf561aba2017-03-08 01:02:16 +0000221 llvm::Triple::ArchType Arch = Triple.getArch();
222 std::string SysRoot = computeSysRoot();
223
224 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
225 // least) put various tools in a triple-prefixed directory off of the parent
226 // of the GCC installation. We use the GCC triple here to ensure that we end
227 // up with tools that support the same amount of cross compiling as the
228 // detected GCC installation. For example, if we find a GCC installation
229 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
230 // used to target i386.
231 // FIXME: This seems unlikely to be Linux-specific.
232 ToolChain::path_list &PPaths = getProgramPaths();
233 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
234 GCCInstallation.getTriple().str() + "/bin")
235 .str());
236
237 Distro Distro(D.getVFS());
238
Dan Alberte4dd75a2018-10-11 20:57:54 +0000239 if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
Martell Malone13c5d732017-11-19 00:08:12 +0000240 ExtraOpts.push_back("-z");
241 ExtraOpts.push_back("now");
242 }
243
Dan Alberte4dd75a2018-10-11 20:57:54 +0000244 if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
245 Triple.isAndroid()) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000246 ExtraOpts.push_back("-z");
247 ExtraOpts.push_back("relro");
248 }
249
Tom Stellardc5fe10f2018-06-30 02:55:54 +0000250 if (GCCInstallation.getParentLibPath().find("opt/rh/devtoolset") !=
251 StringRef::npos)
252 // With devtoolset on RHEL, we want to add a bin directory that is relative
253 // to the detected gcc install, because if we are using devtoolset gcc then
254 // we want to use other tools from devtoolset (e.g. ld) instead of the
255 // standard system tools.
256 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
257 "/../bin").str());
258
David L. Jonesf561aba2017-03-08 01:02:16 +0000259 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
260 ExtraOpts.push_back("-X");
261
262 const bool IsAndroid = Triple.isAndroid();
Alexander Richardson742553d2018-06-25 16:49:52 +0000263 const bool IsMips = Triple.isMIPS();
David L. Jonesf561aba2017-03-08 01:02:16 +0000264 const bool IsHexagon = Arch == llvm::Triple::hexagon;
Alex Bradbury71f45452018-01-11 13:36:56 +0000265 const bool IsRISCV =
266 Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64;
David L. Jonesf561aba2017-03-08 01:02:16 +0000267
268 if (IsMips && !SysRoot.empty())
269 ExtraOpts.push_back("--sysroot=" + SysRoot);
270
271 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
272 // and the MIPS ABI require .dynsym to be sorted in different ways.
273 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
274 // ABI requires a mapping between the GOT and the symbol table.
Dan Albert99ac6c8e2018-10-11 20:39:32 +0000275 // Android loader does not support .gnu.hash until API 23.
David L. Jonesf561aba2017-03-08 01:02:16 +0000276 // Hexagon linker/loader does not support .gnu.hash
Dan Albert99ac6c8e2018-10-11 20:39:32 +0000277 if (!IsMips && !IsHexagon) {
Martell Malone13c5d732017-11-19 00:08:12 +0000278 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
Dan Albert99ac6c8e2018-10-11 20:39:32 +0000279 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) ||
280 (IsAndroid && !Triple.isAndroidVersionLT(23)))
David L. Jonesf561aba2017-03-08 01:02:16 +0000281 ExtraOpts.push_back("--hash-style=gnu");
282
Dan Albert99ac6c8e2018-10-11 20:39:32 +0000283 if (Distro.IsDebian() || Distro.IsOpenSUSE() ||
284 Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty ||
285 Distro == Distro::UbuntuKarmic ||
286 (IsAndroid && Triple.isAndroidVersionLT(23)))
David L. Jonesf561aba2017-03-08 01:02:16 +0000287 ExtraOpts.push_back("--hash-style=both");
288 }
289
290 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6)
291 ExtraOpts.push_back("--no-add-needed");
292
293#ifdef ENABLE_LINKER_BUILD_ID
294 ExtraOpts.push_back("--build-id");
295#endif
296
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000297 if (IsAndroid || Distro.IsOpenSUSE())
David L. Jonesf561aba2017-03-08 01:02:16 +0000298 ExtraOpts.push_back("--enable-new-dtags");
299
300 // The selection of paths to try here is designed to match the patterns which
301 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
302 // This was determined by running GCC in a fake filesystem, creating all
303 // possible permutations of these directories, and seeing which ones it added
304 // to the link paths.
305 path_list &Paths = getFilePaths();
306
307 const std::string OSLibDir = getOSLibDir(Triple, Args);
308 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
309
310 // Add the multilib suffixed paths where they are available.
311 if (GCCInstallation.isValid()) {
312 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
313 const std::string &LibPath = GCCInstallation.getParentLibPath();
David L. Jonesf561aba2017-03-08 01:02:16 +0000314
315 // Add toolchain / multilib specific file paths.
Christian Bruel6ccc4a72018-09-06 14:03:44 +0000316 addMultilibsFilePaths(D, Multilibs, SelectedMultilib,
David L. Jonesf561aba2017-03-08 01:02:16 +0000317 GCCInstallation.getInstallPath(), Paths);
318
319 // Sourcery CodeBench MIPS toolchain holds some libraries under
320 // a biarch-like suffix of the GCC installation.
Christian Bruel6ccc4a72018-09-06 14:03:44 +0000321 addPathIfExists(D, GCCInstallation.getInstallPath() + SelectedMultilib.gccSuffix(),
David L. Jonesf561aba2017-03-08 01:02:16 +0000322 Paths);
323
324 // GCC cross compiling toolchains will install target libraries which ship
325 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
326 // any part of the GCC installation in
327 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
328 // debatable, but is the reality today. We need to search this tree even
329 // when we have a sysroot somewhere else. It is the responsibility of
330 // whomever is doing the cross build targeting a sysroot using a GCC
331 // installation that is *not* within the system root to ensure two things:
332 //
333 // 1) Any DSOs that are linked in from this tree or from the install path
334 // above must be present on the system root and found via an
335 // appropriate rpath.
336 // 2) There must not be libraries installed into
337 // <prefix>/<triple>/<libdir> unless they should be preferred over
338 // those within the system root.
339 //
340 // Note that this matches the GCC behavior. See the below comment for where
341 // Clang diverges from GCC's behavior.
342 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" +
Christian Bruel6ccc4a72018-09-06 14:03:44 +0000343 OSLibDir + SelectedMultilib.osSuffix(),
David L. Jonesf561aba2017-03-08 01:02:16 +0000344 Paths);
345
346 // If the GCC installation we found is inside of the sysroot, we want to
347 // prefer libraries installed in the parent prefix of the GCC installation.
348 // It is important to *not* use these paths when the GCC installation is
349 // outside of the system root as that can pick up unintended libraries.
350 // This usually happens when there is an external cross compiler on the
351 // host system, and a more minimal sysroot available that is the target of
352 // the cross. Note that GCC does include some of these directories in some
353 // configurations but this seems somewhere between questionable and simply
354 // a bug.
355 if (StringRef(LibPath).startswith(SysRoot)) {
356 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths);
357 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
358 }
359 }
360
361 // Similar to the logic for GCC above, if we currently running Clang inside
362 // of the requested system root, add its parent library paths to
363 // those searched.
364 // FIXME: It's not clear whether we should use the driver's installed
365 // directory ('Dir' below) or the ResourceDir.
366 if (StringRef(D.Dir).startswith(SysRoot)) {
367 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
368 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
369 }
370
371 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
372 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
Dan Albertf4832792018-04-17 20:42:07 +0000373
374 if (IsAndroid) {
375 // Android sysroots contain a library directory for each supported OS
376 // version as well as some unversioned libraries in the usual multiarch
377 // directory.
378 unsigned Major;
379 unsigned Minor;
380 unsigned Micro;
381 Triple.getEnvironmentVersion(Major, Minor, Micro);
382 addPathIfExists(D,
383 SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
384 llvm::to_string(Major),
385 Paths);
386 }
387
David L. Jonesf561aba2017-03-08 01:02:16 +0000388 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
Mandeep Singh Grange9ddc442018-07-30 19:44:13 +0000389 // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
390 // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
391 // this here.
392 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
393 Triple.isArch64Bit())
394 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths);
395 else
396 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
Alex Bradbury71f45452018-01-11 13:36:56 +0000397 if (IsRISCV) {
398 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
399 addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
400 addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
401 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000402
403 // Try walking via the GCC triple path in case of biarch or multiarch GCC
404 // installations with strange symlinks.
405 if (GCCInstallation.isValid()) {
406 addPathIfExists(D,
407 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
408 "/../../" + OSLibDir,
409 Paths);
410
411 // Add the 'other' biarch variant path
412 Multilib BiarchSibling;
413 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
414 addPathIfExists(D, GCCInstallation.getInstallPath() +
415 BiarchSibling.gccSuffix(),
416 Paths);
417 }
418
419 // See comments above on the multilib variant for details of why this is
420 // included even from outside the sysroot.
421 const std::string &LibPath = GCCInstallation.getParentLibPath();
422 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
423 const Multilib &Multilib = GCCInstallation.getMultilib();
424 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" +
425 Multilib.osSuffix(),
426 Paths);
427
428 // See comments above on the multilib variant for details of why this is
429 // only included from within the sysroot.
430 if (StringRef(LibPath).startswith(SysRoot))
431 addPathIfExists(D, LibPath, Paths);
432 }
433
434 // Similar to the logic for GCC above, if we are currently running Clang
435 // inside of the requested system root, add its parent library path to those
436 // searched.
437 // FIXME: It's not clear whether we should use the driver's installed
438 // directory ('Dir' below) or the ResourceDir.
439 if (StringRef(D.Dir).startswith(SysRoot))
440 addPathIfExists(D, D.Dir + "/../lib", Paths);
441
442 addPathIfExists(D, SysRoot + "/lib", Paths);
443 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
444}
445
David L. Jonesf561aba2017-03-08 01:02:16 +0000446bool Linux::HasNativeLLVMSupport() const { return true; }
447
448Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
449
450Tool *Linux::buildAssembler() const {
451 return new tools::gnutools::Assembler(*this);
452}
453
454std::string Linux::computeSysRoot() const {
455 if (!getDriver().SysRoot.empty())
456 return getDriver().SysRoot;
457
Dan Albert4d643062018-05-02 19:38:37 +0000458 if (getTriple().isAndroid()) {
459 // Android toolchains typically include a sysroot at ../sysroot relative to
460 // the clang binary.
461 const StringRef ClangDir = getDriver().getInstalledDir();
462 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
463 if (getVFS().exists(AndroidSysRootPath))
464 return AndroidSysRootPath;
465 }
466
Alexander Richardson742553d2018-06-25 16:49:52 +0000467 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
David L. Jonesf561aba2017-03-08 01:02:16 +0000468 return std::string();
469
470 // Standalone MIPS toolchains use different names for sysroot folder
471 // and put it into different places. Here we try to check some known
472 // variants.
473
474 const StringRef InstallDir = GCCInstallation.getInstallPath();
475 const StringRef TripleStr = GCCInstallation.getTriple().str();
476 const Multilib &Multilib = GCCInstallation.getMultilib();
477
478 std::string Path =
479 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
480 .str();
481
482 if (getVFS().exists(Path))
483 return Path;
484
485 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
486
487 if (getVFS().exists(Path))
488 return Path;
489
490 return std::string();
491}
492
493std::string Linux::getDynamicLinker(const ArgList &Args) const {
494 const llvm::Triple::ArchType Arch = getArch();
495 const llvm::Triple &Triple = getTriple();
496
497 const Distro Distro(getDriver().getVFS());
498
499 if (Triple.isAndroid())
500 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
501
502 if (Triple.isMusl()) {
503 std::string ArchName;
504 bool IsArm = false;
505
506 switch (Arch) {
507 case llvm::Triple::arm:
508 case llvm::Triple::thumb:
509 ArchName = "arm";
510 IsArm = true;
511 break;
512 case llvm::Triple::armeb:
513 case llvm::Triple::thumbeb:
514 ArchName = "armeb";
515 IsArm = true;
516 break;
517 default:
518 ArchName = Triple.getArchName().str();
519 }
520 if (IsArm &&
521 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
522 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
523 ArchName += "hf";
524
525 return "/lib/ld-musl-" + ArchName + ".so.1";
526 }
527
528 std::string LibDir;
529 std::string Loader;
530
531 switch (Arch) {
532 default:
533 llvm_unreachable("unsupported architecture");
534
535 case llvm::Triple::aarch64:
536 LibDir = "lib";
537 Loader = "ld-linux-aarch64.so.1";
538 break;
539 case llvm::Triple::aarch64_be:
540 LibDir = "lib";
541 Loader = "ld-linux-aarch64_be.so.1";
542 break;
543 case llvm::Triple::arm:
544 case llvm::Triple::thumb:
545 case llvm::Triple::armeb:
546 case llvm::Triple::thumbeb: {
547 const bool HF =
548 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
549 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
550
551 LibDir = "lib";
552 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
553 break;
554 }
555 case llvm::Triple::mips:
556 case llvm::Triple::mipsel:
557 case llvm::Triple::mips64:
558 case llvm::Triple::mips64el: {
David L. Jonesf561aba2017-03-08 01:02:16 +0000559 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
560
561 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
562
563 if (tools::mips::isUCLibc(Args))
564 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
565 else if (!Triple.hasEnvironment() &&
566 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
Alexander Richardson742553d2018-06-25 16:49:52 +0000567 Loader =
568 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
David L. Jonesf561aba2017-03-08 01:02:16 +0000569 else
570 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
571
572 break;
573 }
574 case llvm::Triple::ppc:
575 LibDir = "lib";
576 Loader = "ld.so.1";
577 break;
578 case llvm::Triple::ppc64:
579 LibDir = "lib64";
580 Loader =
581 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
582 break;
583 case llvm::Triple::ppc64le:
584 LibDir = "lib64";
585 Loader =
586 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
587 break;
Alex Bradbury71f45452018-01-11 13:36:56 +0000588 case llvm::Triple::riscv32: {
589 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
590 LibDir = "lib";
591 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
592 break;
593 }
594 case llvm::Triple::riscv64: {
595 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
596 LibDir = "lib";
597 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
598 break;
599 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000600 case llvm::Triple::sparc:
601 case llvm::Triple::sparcel:
602 LibDir = "lib";
603 Loader = "ld-linux.so.2";
604 break;
605 case llvm::Triple::sparcv9:
606 LibDir = "lib64";
607 Loader = "ld-linux.so.2";
608 break;
609 case llvm::Triple::systemz:
610 LibDir = "lib";
611 Loader = "ld64.so.1";
612 break;
613 case llvm::Triple::x86:
614 LibDir = "lib";
615 Loader = "ld-linux.so.2";
616 break;
617 case llvm::Triple::x86_64: {
618 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
619
620 LibDir = X32 ? "libx32" : "lib64";
621 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
622 break;
623 }
624 }
625
626 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor ||
627 Triple.getVendor() == llvm::Triple::PC))
628 return "/usr/" + Triple.str() + "/lib/" + Loader;
629 return "/" + LibDir + "/" + Loader;
630}
631
632void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
633 ArgStringList &CC1Args) const {
634 const Driver &D = getDriver();
635 std::string SysRoot = computeSysRoot();
636
637 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
638 return;
639
640 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
641 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
642
643 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
644 SmallString<128> P(D.ResourceDir);
645 llvm::sys::path::append(P, "include");
646 addSystemInclude(DriverArgs, CC1Args, P);
647 }
648
649 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
650 return;
651
652 // Check for configure-time C include directories.
653 StringRef CIncludeDirs(C_INCLUDE_DIRS);
654 if (CIncludeDirs != "") {
655 SmallVector<StringRef, 5> dirs;
656 CIncludeDirs.split(dirs, ":");
657 for (StringRef dir : dirs) {
658 StringRef Prefix =
659 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
660 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
661 }
662 return;
663 }
664
665 // Lacking those, try to detect the correct set of system includes for the
666 // target triple.
667
668 // Add include directories specific to the selected multilib set and multilib.
669 if (GCCInstallation.isValid()) {
670 const auto &Callback = Multilibs.includeDirsCallback();
671 if (Callback) {
672 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
673 addExternCSystemIncludeIfExists(
674 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
675 }
676 }
677
678 // Implement generic Debian multiarch support.
679 const StringRef X86_64MultiarchIncludeDirs[] = {
680 "/usr/include/x86_64-linux-gnu",
681
682 // FIXME: These are older forms of multiarch. It's not clear that they're
683 // in use in any released version of Debian, so we should consider
684 // removing them.
685 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
686 const StringRef X86MultiarchIncludeDirs[] = {
687 "/usr/include/i386-linux-gnu",
688
689 // FIXME: These are older forms of multiarch. It's not clear that they're
690 // in use in any released version of Debian, so we should consider
691 // removing them.
692 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
693 "/usr/include/i486-linux-gnu"};
694 const StringRef AArch64MultiarchIncludeDirs[] = {
695 "/usr/include/aarch64-linux-gnu"};
696 const StringRef ARMMultiarchIncludeDirs[] = {
697 "/usr/include/arm-linux-gnueabi"};
698 const StringRef ARMHFMultiarchIncludeDirs[] = {
699 "/usr/include/arm-linux-gnueabihf"};
700 const StringRef ARMEBMultiarchIncludeDirs[] = {
701 "/usr/include/armeb-linux-gnueabi"};
702 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
703 "/usr/include/armeb-linux-gnueabihf"};
704 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
705 const StringRef MIPSELMultiarchIncludeDirs[] = {
706 "/usr/include/mipsel-linux-gnu"};
707 const StringRef MIPS64MultiarchIncludeDirs[] = {
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000708 "/usr/include/mips64-linux-gnuabi64"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000709 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
David L. Jonesf561aba2017-03-08 01:02:16 +0000710 "/usr/include/mips64el-linux-gnuabi64"};
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000711 const StringRef MIPSN32MultiarchIncludeDirs[] = {
712 "/usr/include/mips64-linux-gnuabin32"};
713 const StringRef MIPSN32ELMultiarchIncludeDirs[] = {
714 "/usr/include/mips64el-linux-gnuabin32"};
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000715 const StringRef MIPSR6MultiarchIncludeDirs[] = {
716 "/usr/include/mipsisa32-linux-gnu"};
717 const StringRef MIPSR6ELMultiarchIncludeDirs[] = {
718 "/usr/include/mipsisa32r6el-linux-gnu"};
719 const StringRef MIPS64R6MultiarchIncludeDirs[] = {
720 "/usr/include/mipsisa64r6-linux-gnuabi64"};
721 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = {
722 "/usr/include/mipsisa64r6el-linux-gnuabi64"};
723 const StringRef MIPSN32R6MultiarchIncludeDirs[] = {
724 "/usr/include/mipsisa64r6-linux-gnuabin32"};
725 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = {
726 "/usr/include/mipsisa64r6el-linux-gnuabin32"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000727 const StringRef PPCMultiarchIncludeDirs[] = {
Kristina Brooks200cd742018-09-14 12:42:13 +0000728 "/usr/include/powerpc-linux-gnu",
729 "/usr/include/powerpc-linux-gnuspe"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000730 const StringRef PPC64MultiarchIncludeDirs[] = {
731 "/usr/include/powerpc64-linux-gnu"};
732 const StringRef PPC64LEMultiarchIncludeDirs[] = {
733 "/usr/include/powerpc64le-linux-gnu"};
734 const StringRef SparcMultiarchIncludeDirs[] = {
735 "/usr/include/sparc-linux-gnu"};
736 const StringRef Sparc64MultiarchIncludeDirs[] = {
737 "/usr/include/sparc64-linux-gnu"};
738 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
739 "/usr/include/s390x-linux-gnu"};
740 ArrayRef<StringRef> MultiarchIncludeDirs;
741 switch (getTriple().getArch()) {
742 case llvm::Triple::x86_64:
743 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
744 break;
745 case llvm::Triple::x86:
746 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
747 break;
748 case llvm::Triple::aarch64:
749 case llvm::Triple::aarch64_be:
750 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
751 break;
752 case llvm::Triple::arm:
753 case llvm::Triple::thumb:
754 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
755 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
756 else
757 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
758 break;
759 case llvm::Triple::armeb:
760 case llvm::Triple::thumbeb:
761 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
762 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
763 else
764 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
765 break;
766 case llvm::Triple::mips:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000767 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
768 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs;
769 else
770 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000771 break;
772 case llvm::Triple::mipsel:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000773 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
774 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs;
775 else
776 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000777 break;
778 case llvm::Triple::mips64:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000779 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
780 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
781 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs;
782 else
783 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs;
784 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000785 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs;
786 else
787 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000788 break;
789 case llvm::Triple::mips64el:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000790 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
791 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
792 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs;
793 else
794 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs;
795 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000796 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs;
797 else
798 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000799 break;
800 case llvm::Triple::ppc:
801 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
802 break;
803 case llvm::Triple::ppc64:
804 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
805 break;
806 case llvm::Triple::ppc64le:
807 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
808 break;
809 case llvm::Triple::sparc:
810 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
811 break;
812 case llvm::Triple::sparcv9:
813 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
814 break;
815 case llvm::Triple::systemz:
816 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
817 break;
818 default:
819 break;
820 }
Dan Alberte00799e2018-04-04 21:28:34 +0000821
822 const std::string AndroidMultiarchIncludeDir =
823 std::string("/usr/include/") +
824 getMultiarchTriple(D, getTriple(), SysRoot);
825 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir};
826 if (getTriple().isAndroid())
827 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs;
828
David L. Jonesf561aba2017-03-08 01:02:16 +0000829 for (StringRef Dir : MultiarchIncludeDirs) {
830 if (D.getVFS().exists(SysRoot + Dir)) {
831 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
832 break;
833 }
834 }
835
836 if (getTriple().getOS() == llvm::Triple::RTEMS)
837 return;
838
839 // Add an include of '/include' directly. This isn't provided by default by
840 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
841 // add even when Clang is acting as-if it were a system compiler.
842 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
843
844 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
845}
846
847static std::string DetectLibcxxIncludePath(StringRef base) {
848 std::error_code EC;
849 int MaxVersion = 0;
850 std::string MaxVersionString = "";
851 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE;
852 LI = LI.increment(EC)) {
853 StringRef VersionText = llvm::sys::path::filename(LI->path());
854 int Version;
855 if (VersionText[0] == 'v' &&
856 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
857 if (Version > MaxVersion) {
858 MaxVersion = Version;
859 MaxVersionString = VersionText;
860 }
861 }
862 }
863 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
864}
865
Petr Hosek8d612142018-04-10 19:55:55 +0000866void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
867 llvm::opt::ArgStringList &CC1Args) const {
Dan Albertf6f11492018-05-02 19:31:01 +0000868 const std::string& SysRoot = computeSysRoot();
David L. Jonesf561aba2017-03-08 01:02:16 +0000869 const std::string LibCXXIncludePathCandidates[] = {
Petr Hosek887f26d2018-06-28 03:11:52 +0000870 DetectLibcxxIncludePath(getDriver().ResourceDir + "/include/c++"),
David L. Jonesf561aba2017-03-08 01:02:16 +0000871 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"),
872 // If this is a development, non-installed, clang, libcxx will
873 // not be found at ../include/c++ but it likely to be found at
874 // one of the following two locations:
Dan Albertf6f11492018-05-02 19:31:01 +0000875 DetectLibcxxIncludePath(SysRoot + "/usr/local/include/c++"),
876 DetectLibcxxIncludePath(SysRoot + "/usr/include/c++") };
David L. Jonesf561aba2017-03-08 01:02:16 +0000877 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
878 if (IncludePath.empty() || !getVFS().exists(IncludePath))
879 continue;
880 // Use the first candidate that exists.
Petr Hosek8d612142018-04-10 19:55:55 +0000881 addSystemInclude(DriverArgs, CC1Args, IncludePath);
882 return;
David L. Jonesf561aba2017-03-08 01:02:16 +0000883 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000884}
885
886void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
887 llvm::opt::ArgStringList &CC1Args) const {
888 // We need a detected GCC installation on Linux to provide libstdc++'s
889 // headers.
890 if (!GCCInstallation.isValid())
891 return;
892
893 // By default, look for the C++ headers in an include directory adjacent to
894 // the lib directory of the GCC installation. Note that this is expect to be
895 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
896 StringRef LibDir = GCCInstallation.getParentLibPath();
897 StringRef InstallDir = GCCInstallation.getInstallPath();
898 StringRef TripleStr = GCCInstallation.getTriple().str();
899 const Multilib &Multilib = GCCInstallation.getMultilib();
900 const std::string GCCMultiarchTriple = getMultiarchTriple(
901 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot);
902 const std::string TargetMultiarchTriple =
903 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
904 const GCCVersion &Version = GCCInstallation.getVersion();
905
906 // The primary search for libstdc++ supports multiarch variants.
907 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
908 "/c++/" + Version.Text, TripleStr,
909 GCCMultiarchTriple, TargetMultiarchTriple,
910 Multilib.includeSuffix(), DriverArgs, CC1Args))
911 return;
912
913 // Otherwise, fall back on a bunch of options which don't use multiarch
914 // layouts for simplicity.
915 const std::string LibStdCXXIncludePathCandidates[] = {
916 // Gentoo is weird and places its headers inside the GCC install,
917 // so if the first attempt to find the headers fails, try these patterns.
918 InstallDir.str() + "/include/g++-v" + Version.Text,
919 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
920 Version.MinorStr,
921 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
922 // Android standalone toolchain has C++ headers in yet another place.
923 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
924 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
925 // without a subdirectory corresponding to the gcc version.
926 LibDir.str() + "/../include/c++",
927 };
928
929 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
930 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
931 /*GCCMultiarchTriple*/ "",
932 /*TargetMultiarchTriple*/ "",
933 Multilib.includeSuffix(), DriverArgs, CC1Args))
934 break;
935 }
936}
937
938void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
939 ArgStringList &CC1Args) const {
940 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
941}
942
943void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
944 ArgStringList &CC1Args) const {
945 if (GCCInstallation.isValid()) {
946 CC1Args.push_back("-isystem");
947 CC1Args.push_back(DriverArgs.MakeArgString(
948 GCCInstallation.getParentLibPath() + "/../" +
949 GCCInstallation.getTriple().str() + "/include"));
950 }
951}
952
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000953bool Linux::isPIEDefault() const {
954 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
Martell Malone13c5d732017-11-19 00:08:12 +0000955 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000956}
David L. Jonesf561aba2017-03-08 01:02:16 +0000957
Pirama Arumuga Nainar569dd502018-08-22 17:43:05 +0000958bool Linux::IsMathErrnoDefault() const {
959 if (getTriple().isAndroid())
960 return false;
961 return Generic_ELF::IsMathErrnoDefault();
962}
963
David L. Jonesf561aba2017-03-08 01:02:16 +0000964SanitizerMask Linux::getSupportedSanitizers() const {
965 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
966 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
Alexander Richardson742553d2018-06-25 16:49:52 +0000967 const bool IsMIPS = getTriple().isMIPS32();
968 const bool IsMIPS64 = getTriple().isMIPS64();
David L. Jonesf561aba2017-03-08 01:02:16 +0000969 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
970 getTriple().getArch() == llvm::Triple::ppc64le;
971 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
972 getTriple().getArch() == llvm::Triple::aarch64_be;
Maxim Ostapenko2084b6b2017-04-11 07:22:11 +0000973 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
Galina Kistanova8a338ea2017-06-03 16:47:06 +0000974 getTriple().getArch() == llvm::Triple::thumb ||
975 getTriple().getArch() == llvm::Triple::armeb ||
976 getTriple().getArch() == llvm::Triple::thumbeb;
David L. Jonesf561aba2017-03-08 01:02:16 +0000977 SanitizerMask Res = ToolChain::getSupportedSanitizers();
978 Res |= SanitizerKind::Address;
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000979 Res |= SanitizerKind::Fuzzer;
George Karpenkov33613f62017-08-11 17:22:58 +0000980 Res |= SanitizerKind::FuzzerNoLink;
David L. Jonesf561aba2017-03-08 01:02:16 +0000981 Res |= SanitizerKind::KernelAddress;
Evgeniy Stepanov072b1a22018-04-04 23:48:06 +0000982 Res |= SanitizerKind::Memory;
David L. Jonesf561aba2017-03-08 01:02:16 +0000983 Res |= SanitizerKind::Vptr;
984 Res |= SanitizerKind::SafeStack;
985 if (IsX86_64 || IsMIPS64 || IsAArch64)
986 Res |= SanitizerKind::DataFlow;
Alex Shlyapnikov797bdbb2017-10-26 03:09:53 +0000987 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64)
David L. Jonesf561aba2017-03-08 01:02:16 +0000988 Res |= SanitizerKind::Leak;
989 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
990 Res |= SanitizerKind::Thread;
Alexander Potapenkod49c32c2018-09-07 09:21:09 +0000991 if (IsX86_64)
992 Res |= SanitizerKind::KernelMemory;
David L. Jonesf561aba2017-03-08 01:02:16 +0000993 if (IsX86_64 || IsMIPS64)
994 Res |= SanitizerKind::Efficiency;
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +0000995 if (IsX86 || IsX86_64)
David L. Jonesf561aba2017-03-08 01:02:16 +0000996 Res |= SanitizerKind::Function;
Kostya Kortchinskyeb5b79b42018-07-03 14:39:29 +0000997 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
998 IsPowerPC64)
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +0000999 Res |= SanitizerKind::Scudo;
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +00001000 if (IsX86_64 || IsAArch64) {
Evgeniy Stepanov12817e52017-12-09 01:32:07 +00001001 Res |= SanitizerKind::HWAddress;
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +00001002 Res |= SanitizerKind::KernelHWAddress;
1003 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001004 return Res;
1005}
1006
1007void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
1008 llvm::opt::ArgStringList &CmdArgs) const {
1009 if (!needsProfileRT(Args)) return;
1010
1011 // Add linker option -u__llvm_runtime_variable to cause runtime
1012 // initialization module to be linked in.
1013 if (!Args.hasArg(options::OPT_coverage))
1014 CmdArgs.push_back(Args.MakeArgString(
1015 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
1016 ToolChain::addProfileRTLibs(Args, CmdArgs);
1017}