Coverage Report

Created: 2019-07-24 05:18

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