Coverage Report

Created: 2020-02-25 14:32

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