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