blob: 2ac335c97b22ba8d2b8f2f54814b95b52017e45d [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
Dan Albertb2c5cab2018-11-05 20:57:46 +0000446ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
447 if (getTriple().isAndroid())
448 return ToolChain::CST_Libcxx;
449 return ToolChain::CST_Libstdcxx;
450}
451
David L. Jonesf561aba2017-03-08 01:02:16 +0000452bool Linux::HasNativeLLVMSupport() const { return true; }
453
454Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
455
456Tool *Linux::buildAssembler() const {
457 return new tools::gnutools::Assembler(*this);
458}
459
460std::string Linux::computeSysRoot() const {
461 if (!getDriver().SysRoot.empty())
462 return getDriver().SysRoot;
463
Dan Albert4d643062018-05-02 19:38:37 +0000464 if (getTriple().isAndroid()) {
465 // Android toolchains typically include a sysroot at ../sysroot relative to
466 // the clang binary.
467 const StringRef ClangDir = getDriver().getInstalledDir();
468 std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
469 if (getVFS().exists(AndroidSysRootPath))
470 return AndroidSysRootPath;
471 }
472
Alexander Richardson742553d2018-06-25 16:49:52 +0000473 if (!GCCInstallation.isValid() || !getTriple().isMIPS())
David L. Jonesf561aba2017-03-08 01:02:16 +0000474 return std::string();
475
476 // Standalone MIPS toolchains use different names for sysroot folder
477 // and put it into different places. Here we try to check some known
478 // variants.
479
480 const StringRef InstallDir = GCCInstallation.getInstallPath();
481 const StringRef TripleStr = GCCInstallation.getTriple().str();
482 const Multilib &Multilib = GCCInstallation.getMultilib();
483
484 std::string Path =
485 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
486 .str();
487
488 if (getVFS().exists(Path))
489 return Path;
490
491 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
492
493 if (getVFS().exists(Path))
494 return Path;
495
496 return std::string();
497}
498
499std::string Linux::getDynamicLinker(const ArgList &Args) const {
500 const llvm::Triple::ArchType Arch = getArch();
501 const llvm::Triple &Triple = getTriple();
502
503 const Distro Distro(getDriver().getVFS());
504
505 if (Triple.isAndroid())
506 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
507
508 if (Triple.isMusl()) {
509 std::string ArchName;
510 bool IsArm = false;
511
512 switch (Arch) {
513 case llvm::Triple::arm:
514 case llvm::Triple::thumb:
515 ArchName = "arm";
516 IsArm = true;
517 break;
518 case llvm::Triple::armeb:
519 case llvm::Triple::thumbeb:
520 ArchName = "armeb";
521 IsArm = true;
522 break;
523 default:
524 ArchName = Triple.getArchName().str();
525 }
526 if (IsArm &&
527 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
528 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
529 ArchName += "hf";
530
531 return "/lib/ld-musl-" + ArchName + ".so.1";
532 }
533
534 std::string LibDir;
535 std::string Loader;
536
537 switch (Arch) {
538 default:
539 llvm_unreachable("unsupported architecture");
540
541 case llvm::Triple::aarch64:
542 LibDir = "lib";
543 Loader = "ld-linux-aarch64.so.1";
544 break;
545 case llvm::Triple::aarch64_be:
546 LibDir = "lib";
547 Loader = "ld-linux-aarch64_be.so.1";
548 break;
549 case llvm::Triple::arm:
550 case llvm::Triple::thumb:
551 case llvm::Triple::armeb:
552 case llvm::Triple::thumbeb: {
553 const bool HF =
554 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
555 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
556
557 LibDir = "lib";
558 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
559 break;
560 }
561 case llvm::Triple::mips:
562 case llvm::Triple::mipsel:
563 case llvm::Triple::mips64:
564 case llvm::Triple::mips64el: {
David L. Jonesf561aba2017-03-08 01:02:16 +0000565 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
566
567 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
568
569 if (tools::mips::isUCLibc(Args))
570 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
571 else if (!Triple.hasEnvironment() &&
572 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
Alexander Richardson742553d2018-06-25 16:49:52 +0000573 Loader =
574 Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
David L. Jonesf561aba2017-03-08 01:02:16 +0000575 else
576 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
577
578 break;
579 }
580 case llvm::Triple::ppc:
581 LibDir = "lib";
582 Loader = "ld.so.1";
583 break;
584 case llvm::Triple::ppc64:
585 LibDir = "lib64";
586 Loader =
587 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
588 break;
589 case llvm::Triple::ppc64le:
590 LibDir = "lib64";
591 Loader =
592 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
593 break;
Alex Bradbury71f45452018-01-11 13:36:56 +0000594 case llvm::Triple::riscv32: {
595 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
596 LibDir = "lib";
597 Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
598 break;
599 }
600 case llvm::Triple::riscv64: {
601 StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
602 LibDir = "lib";
603 Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
604 break;
605 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000606 case llvm::Triple::sparc:
607 case llvm::Triple::sparcel:
608 LibDir = "lib";
609 Loader = "ld-linux.so.2";
610 break;
611 case llvm::Triple::sparcv9:
612 LibDir = "lib64";
613 Loader = "ld-linux.so.2";
614 break;
615 case llvm::Triple::systemz:
616 LibDir = "lib";
617 Loader = "ld64.so.1";
618 break;
619 case llvm::Triple::x86:
620 LibDir = "lib";
621 Loader = "ld-linux.so.2";
622 break;
623 case llvm::Triple::x86_64: {
624 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32;
625
626 LibDir = X32 ? "libx32" : "lib64";
627 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
628 break;
629 }
630 }
631
632 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor ||
633 Triple.getVendor() == llvm::Triple::PC))
634 return "/usr/" + Triple.str() + "/lib/" + Loader;
635 return "/" + LibDir + "/" + Loader;
636}
637
638void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
639 ArgStringList &CC1Args) const {
640 const Driver &D = getDriver();
641 std::string SysRoot = computeSysRoot();
642
643 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
644 return;
645
646 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
647 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
648
649 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
650 SmallString<128> P(D.ResourceDir);
651 llvm::sys::path::append(P, "include");
652 addSystemInclude(DriverArgs, CC1Args, P);
653 }
654
655 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
656 return;
657
658 // Check for configure-time C include directories.
659 StringRef CIncludeDirs(C_INCLUDE_DIRS);
660 if (CIncludeDirs != "") {
661 SmallVector<StringRef, 5> dirs;
662 CIncludeDirs.split(dirs, ":");
663 for (StringRef dir : dirs) {
664 StringRef Prefix =
665 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
666 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
667 }
668 return;
669 }
670
671 // Lacking those, try to detect the correct set of system includes for the
672 // target triple.
673
674 // Add include directories specific to the selected multilib set and multilib.
675 if (GCCInstallation.isValid()) {
676 const auto &Callback = Multilibs.includeDirsCallback();
677 if (Callback) {
678 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
679 addExternCSystemIncludeIfExists(
680 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
681 }
682 }
683
684 // Implement generic Debian multiarch support.
685 const StringRef X86_64MultiarchIncludeDirs[] = {
686 "/usr/include/x86_64-linux-gnu",
687
688 // FIXME: These are older forms of multiarch. It's not clear that they're
689 // in use in any released version of Debian, so we should consider
690 // removing them.
691 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"};
692 const StringRef X86MultiarchIncludeDirs[] = {
693 "/usr/include/i386-linux-gnu",
694
695 // FIXME: These are older forms of multiarch. It's not clear that they're
696 // in use in any released version of Debian, so we should consider
697 // removing them.
698 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
699 "/usr/include/i486-linux-gnu"};
700 const StringRef AArch64MultiarchIncludeDirs[] = {
701 "/usr/include/aarch64-linux-gnu"};
702 const StringRef ARMMultiarchIncludeDirs[] = {
703 "/usr/include/arm-linux-gnueabi"};
704 const StringRef ARMHFMultiarchIncludeDirs[] = {
705 "/usr/include/arm-linux-gnueabihf"};
706 const StringRef ARMEBMultiarchIncludeDirs[] = {
707 "/usr/include/armeb-linux-gnueabi"};
708 const StringRef ARMEBHFMultiarchIncludeDirs[] = {
709 "/usr/include/armeb-linux-gnueabihf"};
710 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"};
711 const StringRef MIPSELMultiarchIncludeDirs[] = {
712 "/usr/include/mipsel-linux-gnu"};
713 const StringRef MIPS64MultiarchIncludeDirs[] = {
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000714 "/usr/include/mips64-linux-gnuabi64"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000715 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
David L. Jonesf561aba2017-03-08 01:02:16 +0000716 "/usr/include/mips64el-linux-gnuabi64"};
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000717 const StringRef MIPSN32MultiarchIncludeDirs[] = {
718 "/usr/include/mips64-linux-gnuabin32"};
719 const StringRef MIPSN32ELMultiarchIncludeDirs[] = {
720 "/usr/include/mips64el-linux-gnuabin32"};
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000721 const StringRef MIPSR6MultiarchIncludeDirs[] = {
722 "/usr/include/mipsisa32-linux-gnu"};
723 const StringRef MIPSR6ELMultiarchIncludeDirs[] = {
724 "/usr/include/mipsisa32r6el-linux-gnu"};
725 const StringRef MIPS64R6MultiarchIncludeDirs[] = {
726 "/usr/include/mipsisa64r6-linux-gnuabi64"};
727 const StringRef MIPS64R6ELMultiarchIncludeDirs[] = {
728 "/usr/include/mipsisa64r6el-linux-gnuabi64"};
729 const StringRef MIPSN32R6MultiarchIncludeDirs[] = {
730 "/usr/include/mipsisa64r6-linux-gnuabin32"};
731 const StringRef MIPSN32R6ELMultiarchIncludeDirs[] = {
732 "/usr/include/mipsisa64r6el-linux-gnuabin32"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000733 const StringRef PPCMultiarchIncludeDirs[] = {
Kristina Brooks200cd742018-09-14 12:42:13 +0000734 "/usr/include/powerpc-linux-gnu",
735 "/usr/include/powerpc-linux-gnuspe"};
David L. Jonesf561aba2017-03-08 01:02:16 +0000736 const StringRef PPC64MultiarchIncludeDirs[] = {
737 "/usr/include/powerpc64-linux-gnu"};
738 const StringRef PPC64LEMultiarchIncludeDirs[] = {
739 "/usr/include/powerpc64le-linux-gnu"};
740 const StringRef SparcMultiarchIncludeDirs[] = {
741 "/usr/include/sparc-linux-gnu"};
742 const StringRef Sparc64MultiarchIncludeDirs[] = {
743 "/usr/include/sparc64-linux-gnu"};
744 const StringRef SYSTEMZMultiarchIncludeDirs[] = {
745 "/usr/include/s390x-linux-gnu"};
746 ArrayRef<StringRef> MultiarchIncludeDirs;
747 switch (getTriple().getArch()) {
748 case llvm::Triple::x86_64:
749 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
750 break;
751 case llvm::Triple::x86:
752 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
753 break;
754 case llvm::Triple::aarch64:
755 case llvm::Triple::aarch64_be:
756 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
757 break;
758 case llvm::Triple::arm:
759 case llvm::Triple::thumb:
760 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
761 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
762 else
763 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
764 break;
765 case llvm::Triple::armeb:
766 case llvm::Triple::thumbeb:
767 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
768 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs;
769 else
770 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs;
771 break;
772 case llvm::Triple::mips:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000773 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
774 MultiarchIncludeDirs = MIPSR6MultiarchIncludeDirs;
775 else
776 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000777 break;
778 case llvm::Triple::mipsel:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000779 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
780 MultiarchIncludeDirs = MIPSR6ELMultiarchIncludeDirs;
781 else
782 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000783 break;
784 case llvm::Triple::mips64:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000785 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
786 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
787 MultiarchIncludeDirs = MIPSN32R6MultiarchIncludeDirs;
788 else
789 MultiarchIncludeDirs = MIPS64R6MultiarchIncludeDirs;
790 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000791 MultiarchIncludeDirs = MIPSN32MultiarchIncludeDirs;
792 else
793 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000794 break;
795 case llvm::Triple::mips64el:
Simon Atanasyaneaab2b72018-10-16 14:29:27 +0000796 if (getTriple().getSubArch() == llvm::Triple::MipsSubArch_r6)
797 if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
798 MultiarchIncludeDirs = MIPSN32R6ELMultiarchIncludeDirs;
799 else
800 MultiarchIncludeDirs = MIPS64R6ELMultiarchIncludeDirs;
801 else if (getTriple().getEnvironment() == llvm::Triple::GNUABIN32)
Simon Atanasyandb81c7b2018-10-15 22:43:23 +0000802 MultiarchIncludeDirs = MIPSN32ELMultiarchIncludeDirs;
803 else
804 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
David L. Jonesf561aba2017-03-08 01:02:16 +0000805 break;
806 case llvm::Triple::ppc:
807 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
808 break;
809 case llvm::Triple::ppc64:
810 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
811 break;
812 case llvm::Triple::ppc64le:
813 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
814 break;
815 case llvm::Triple::sparc:
816 MultiarchIncludeDirs = SparcMultiarchIncludeDirs;
817 break;
818 case llvm::Triple::sparcv9:
819 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs;
820 break;
821 case llvm::Triple::systemz:
822 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs;
823 break;
824 default:
825 break;
826 }
Dan Alberte00799e2018-04-04 21:28:34 +0000827
828 const std::string AndroidMultiarchIncludeDir =
829 std::string("/usr/include/") +
830 getMultiarchTriple(D, getTriple(), SysRoot);
831 const StringRef AndroidMultiarchIncludeDirs[] = {AndroidMultiarchIncludeDir};
832 if (getTriple().isAndroid())
833 MultiarchIncludeDirs = AndroidMultiarchIncludeDirs;
834
David L. Jonesf561aba2017-03-08 01:02:16 +0000835 for (StringRef Dir : MultiarchIncludeDirs) {
836 if (D.getVFS().exists(SysRoot + Dir)) {
837 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
838 break;
839 }
840 }
841
842 if (getTriple().getOS() == llvm::Triple::RTEMS)
843 return;
844
845 // Add an include of '/include' directly. This isn't provided by default by
846 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
847 // add even when Clang is acting as-if it were a system compiler.
848 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
849
850 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
851}
852
853static std::string DetectLibcxxIncludePath(StringRef base) {
854 std::error_code EC;
855 int MaxVersion = 0;
856 std::string MaxVersionString = "";
857 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE;
858 LI = LI.increment(EC)) {
859 StringRef VersionText = llvm::sys::path::filename(LI->path());
860 int Version;
861 if (VersionText[0] == 'v' &&
862 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
863 if (Version > MaxVersion) {
864 MaxVersion = Version;
865 MaxVersionString = VersionText;
866 }
867 }
868 }
869 return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
870}
871
Petr Hosek8d612142018-04-10 19:55:55 +0000872void Linux::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
873 llvm::opt::ArgStringList &CC1Args) const {
Dan Albertf6f11492018-05-02 19:31:01 +0000874 const std::string& SysRoot = computeSysRoot();
David L. Jonesf561aba2017-03-08 01:02:16 +0000875 const std::string LibCXXIncludePathCandidates[] = {
Petr Hosek887f26d2018-06-28 03:11:52 +0000876 DetectLibcxxIncludePath(getDriver().ResourceDir + "/include/c++"),
David L. Jonesf561aba2017-03-08 01:02:16 +0000877 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"),
878 // If this is a development, non-installed, clang, libcxx will
879 // not be found at ../include/c++ but it likely to be found at
880 // one of the following two locations:
Dan Albertf6f11492018-05-02 19:31:01 +0000881 DetectLibcxxIncludePath(SysRoot + "/usr/local/include/c++"),
882 DetectLibcxxIncludePath(SysRoot + "/usr/include/c++") };
David L. Jonesf561aba2017-03-08 01:02:16 +0000883 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
884 if (IncludePath.empty() || !getVFS().exists(IncludePath))
885 continue;
886 // Use the first candidate that exists.
Petr Hosek8d612142018-04-10 19:55:55 +0000887 addSystemInclude(DriverArgs, CC1Args, IncludePath);
888 return;
David L. Jonesf561aba2017-03-08 01:02:16 +0000889 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000890}
891
892void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
893 llvm::opt::ArgStringList &CC1Args) const {
894 // We need a detected GCC installation on Linux to provide libstdc++'s
895 // headers.
896 if (!GCCInstallation.isValid())
897 return;
898
899 // By default, look for the C++ headers in an include directory adjacent to
900 // the lib directory of the GCC installation. Note that this is expect to be
901 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
902 StringRef LibDir = GCCInstallation.getParentLibPath();
903 StringRef InstallDir = GCCInstallation.getInstallPath();
904 StringRef TripleStr = GCCInstallation.getTriple().str();
905 const Multilib &Multilib = GCCInstallation.getMultilib();
906 const std::string GCCMultiarchTriple = getMultiarchTriple(
907 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot);
908 const std::string TargetMultiarchTriple =
909 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
910 const GCCVersion &Version = GCCInstallation.getVersion();
911
912 // The primary search for libstdc++ supports multiarch variants.
913 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
914 "/c++/" + Version.Text, TripleStr,
915 GCCMultiarchTriple, TargetMultiarchTriple,
916 Multilib.includeSuffix(), DriverArgs, CC1Args))
917 return;
918
919 // Otherwise, fall back on a bunch of options which don't use multiarch
920 // layouts for simplicity.
921 const std::string LibStdCXXIncludePathCandidates[] = {
922 // Gentoo is weird and places its headers inside the GCC install,
923 // so if the first attempt to find the headers fails, try these patterns.
924 InstallDir.str() + "/include/g++-v" + Version.Text,
925 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
926 Version.MinorStr,
927 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
928 // Android standalone toolchain has C++ headers in yet another place.
929 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
930 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
931 // without a subdirectory corresponding to the gcc version.
932 LibDir.str() + "/../include/c++",
933 };
934
935 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
936 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
937 /*GCCMultiarchTriple*/ "",
938 /*TargetMultiarchTriple*/ "",
939 Multilib.includeSuffix(), DriverArgs, CC1Args))
940 break;
941 }
942}
943
944void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
945 ArgStringList &CC1Args) const {
946 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
947}
948
949void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
950 ArgStringList &CC1Args) const {
951 if (GCCInstallation.isValid()) {
952 CC1Args.push_back("-isystem");
953 CC1Args.push_back(DriverArgs.MakeArgString(
954 GCCInstallation.getParentLibPath() + "/../" +
955 GCCInstallation.getTriple().str() + "/include"));
956 }
957}
958
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000959bool Linux::isPIEDefault() const {
960 return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
Martell Malone13c5d732017-11-19 00:08:12 +0000961 getTriple().isMusl() || getSanitizerArgs().requiresPIE();
Evgeniy Stepanov117627c2017-10-25 20:39:22 +0000962}
David L. Jonesf561aba2017-03-08 01:02:16 +0000963
Pirama Arumuga Nainar569dd502018-08-22 17:43:05 +0000964bool Linux::IsMathErrnoDefault() const {
965 if (getTriple().isAndroid())
966 return false;
967 return Generic_ELF::IsMathErrnoDefault();
968}
969
David L. Jonesf561aba2017-03-08 01:02:16 +0000970SanitizerMask Linux::getSupportedSanitizers() const {
971 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
972 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
Alexander Richardson742553d2018-06-25 16:49:52 +0000973 const bool IsMIPS = getTriple().isMIPS32();
974 const bool IsMIPS64 = getTriple().isMIPS64();
David L. Jonesf561aba2017-03-08 01:02:16 +0000975 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
976 getTriple().getArch() == llvm::Triple::ppc64le;
977 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
978 getTriple().getArch() == llvm::Triple::aarch64_be;
Maxim Ostapenko2084b6b2017-04-11 07:22:11 +0000979 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
Galina Kistanova8a338ea2017-06-03 16:47:06 +0000980 getTriple().getArch() == llvm::Triple::thumb ||
981 getTriple().getArch() == llvm::Triple::armeb ||
982 getTriple().getArch() == llvm::Triple::thumbeb;
David L. Jonesf561aba2017-03-08 01:02:16 +0000983 SanitizerMask Res = ToolChain::getSupportedSanitizers();
984 Res |= SanitizerKind::Address;
George Karpenkovf2fc5b02017-04-24 18:23:24 +0000985 Res |= SanitizerKind::Fuzzer;
George Karpenkov33613f62017-08-11 17:22:58 +0000986 Res |= SanitizerKind::FuzzerNoLink;
David L. Jonesf561aba2017-03-08 01:02:16 +0000987 Res |= SanitizerKind::KernelAddress;
Evgeniy Stepanov072b1a22018-04-04 23:48:06 +0000988 Res |= SanitizerKind::Memory;
David L. Jonesf561aba2017-03-08 01:02:16 +0000989 Res |= SanitizerKind::Vptr;
990 Res |= SanitizerKind::SafeStack;
991 if (IsX86_64 || IsMIPS64 || IsAArch64)
992 Res |= SanitizerKind::DataFlow;
Alex Shlyapnikov797bdbb2017-10-26 03:09:53 +0000993 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64)
David L. Jonesf561aba2017-03-08 01:02:16 +0000994 Res |= SanitizerKind::Leak;
995 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
996 Res |= SanitizerKind::Thread;
Alexander Potapenkod49c32c2018-09-07 09:21:09 +0000997 if (IsX86_64)
998 Res |= SanitizerKind::KernelMemory;
David L. Jonesf561aba2017-03-08 01:02:16 +0000999 if (IsX86_64 || IsMIPS64)
1000 Res |= SanitizerKind::Efficiency;
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +00001001 if (IsX86 || IsX86_64)
David L. Jonesf561aba2017-03-08 01:02:16 +00001002 Res |= SanitizerKind::Function;
Kostya Kortchinskyeb5b79b42018-07-03 14:39:29 +00001003 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
1004 IsPowerPC64)
Kostya Kortchinsky8acdc982017-11-03 17:04:13 +00001005 Res |= SanitizerKind::Scudo;
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +00001006 if (IsX86_64 || IsAArch64) {
Evgeniy Stepanov12817e52017-12-09 01:32:07 +00001007 Res |= SanitizerKind::HWAddress;
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +00001008 Res |= SanitizerKind::KernelHWAddress;
1009 }
David L. Jonesf561aba2017-03-08 01:02:16 +00001010 return Res;
1011}
1012
1013void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
1014 llvm::opt::ArgStringList &CmdArgs) const {
1015 if (!needsProfileRT(Args)) return;
1016
1017 // Add linker option -u__llvm_runtime_variable to cause runtime
1018 // initialization module to be linked in.
1019 if (!Args.hasArg(options::OPT_coverage))
1020 CmdArgs.push_back(Args.MakeArgString(
1021 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
1022 ToolChain::addProfileRTLibs(Args, CmdArgs);
1023}