Coverage Report

Created: 2021-08-24 07:12

/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
10.9k
                                      StringRef SysRoot) const {
43
10.9k
  llvm::Triple::EnvironmentType TargetEnvironment =
44
10.9k
      TargetTriple.getEnvironment();
45
10.9k
  bool IsAndroid = TargetTriple.isAndroid();
46
10.9k
  bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
47
10.9k
  bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
48
49
  // For most architectures, just use whatever we have rather than trying to be
50
  // clever.
51
10.9k
  switch (TargetTriple.getArch()) {
52
298
  default:
53
298
    break;
54
55
  // We use the existence of '/lib/<triple>' as a directory to detect some
56
  // common linux triples that don't quite match the Clang triple for both
57
  // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
58
  // regardless of what the actual target triple is.
59
1.14k
  case llvm::Triple::arm:
60
1.16k
  case llvm::Triple::thumb:
61
1.16k
    if (IsAndroid)
62
198
      return "arm-linux-androideabi";
63
971
    if (TargetEnvironment == llvm::Triple::GNUEABIHF)
64
88
      return "arm-linux-gnueabihf";
65
883
    return "arm-linux-gnueabi";
66
86
  case llvm::Triple::armeb:
67
108
  case llvm::Triple::thumbeb:
68
108
    if (TargetEnvironment == llvm::Triple::GNUEABIHF)
69
4
      return "armeb-linux-gnueabihf";
70
104
    return "armeb-linux-gnueabi";
71
1.03k
  case llvm::Triple::x86:
72
1.03k
    if (IsAndroid)
73
46
      return "i686-linux-android";
74
989
    return "i386-linux-gnu";
75
5.80k
  case llvm::Triple::x86_64:
76
5.80k
    if (IsAndroid)
77
32
      return "x86_64-linux-android";
78
5.77k
    if (TargetEnvironment == llvm::Triple::GNUX32)
79
25
      return "x86_64-linux-gnux32";
80
5.75k
    return "x86_64-linux-gnu";
81
642
  case llvm::Triple::aarch64:
82
642
    if (IsAndroid)
83
100
      return "aarch64-linux-android";
84
542
    return "aarch64-linux-gnu";
85
16
  case llvm::Triple::aarch64_be:
86
16
    return "aarch64_be-linux-gnu";
87
88
60
  case llvm::Triple::m68k:
89
60
    return "m68k-linux-gnu";
90
91
543
  case llvm::Triple::mips:
92
543
    return IsMipsR6 ? 
"mipsisa32r6-linux-gnu"2
:
"mips-linux-gnu"541
;
93
199
  case llvm::Triple::mipsel:
94
199
    if (IsAndroid)
95
44
      return "mipsel-linux-android";
96
155
    return IsMipsR6 ? 
"mipsisa32r6el-linux-gnu"2
:
"mipsel-linux-gnu"153
;
97
198
  case llvm::Triple::mips64: {
98
198
    std::string MT = std::string(IsMipsR6 ? 
"mipsisa64r6"6
:
"mips64"192
) +
99
198
                     "-linux-" + (IsMipsN32Abi ? 
"gnuabin32"44
:
"gnuabi64"154
);
100
198
    if (D.getVFS().exists(SysRoot + "/lib/" + MT))
101
6
      return MT;
102
192
    if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu"))
103
0
      return "mips64-linux-gnu";
104
192
    break;
105
192
  }
106
192
  case llvm::Triple::mips64el: {
107
148
    if (IsAndroid)
108
20
      return "mips64el-linux-android";
109
128
    std::string MT = std::string(IsMipsR6 ? 
"mipsisa64r6el"6
:
"mips64el"122
) +
110
128
                     "-linux-" + (IsMipsN32Abi ? 
"gnuabin32"36
:
"gnuabi64"92
);
111
128
    if (D.getVFS().exists(SysRoot + "/lib/" + MT))
112
6
      return MT;
113
122
    if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu"))
114
0
      return "mips64el-linux-gnu";
115
122
    break;
116
122
  }
117
122
  case llvm::Triple::ppc:
118
62
    if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
119
0
      return "powerpc-linux-gnuspe";
120
62
    return "powerpc-linux-gnu";
121
6
  case llvm::Triple::ppcle:
122
6
    return "powerpcle-linux-gnu";
123
216
  case llvm::Triple::ppc64:
124
216
    return "powerpc64-linux-gnu";
125
348
  case llvm::Triple::ppc64le:
126
348
    return "powerpc64le-linux-gnu";
127
26
  case llvm::Triple::sparc:
128
26
    return "sparc-linux-gnu";
129
28
  case llvm::Triple::sparcv9:
130
28
    return "sparc64-linux-gnu";
131
87
  case llvm::Triple::systemz:
132
87
    return "s390x-linux-gnu";
133
10.9k
  }
134
612
  return TargetTriple.str();
135
10.9k
}
136
137
5.15k
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
138
5.15k
  if (Triple.isMIPS()) {
139
591
    if (Triple.isAndroid()) {
140
32
      StringRef CPUName;
141
32
      StringRef ABIName;
142
32
      tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
143
32
      if (CPUName == "mips32r6")
144
1
        return "libr6";
145
31
      if (CPUName == "mips32r2")
146
2
        return "libr2";
147
31
    }
148
    // lib32 directory has a special meaning on MIPS targets.
149
    // It contains N32 ABI binaries. Use this folder if produce
150
    // code for N32 ABI only.
151
588
    if (tools::mips::hasMipsAbiArg(Args, "n32"))
152
36
      return "lib32";
153
552
    return Triple.isArch32Bit() ? 
"lib"410
:
"lib64"142
;
154
588
  }
155
156
  // It happens that only x86, PPC and SPARC use the 'lib32' variant of
157
  // oslibdir, and using that variant while targeting other architectures causes
158
  // problems because the libraries are laid out in shared system roots that
159
  // can't cope with a 'lib32' library search path being considered. So we only
160
  // enable them when we know we may need it.
161
  //
162
  // FIXME: This is a bit of a hack. We should really unify this code for
163
  // reasoning about oslibdir spellings with the lib dir spellings in the
164
  // GCCInstallationDetector, but that is a more significant refactoring.
165
4.56k
  if (Triple.getArch() == llvm::Triple::x86 || 
Triple.isPPC32()4.05k
||
166
4.56k
      
Triple.getArch() == llvm::Triple::sparc4.02k
)
167
557
    return "lib32";
168
169
4.01k
  if (Triple.getArch() == llvm::Triple::x86_64 && 
Triple.isX32()2.44k
)
170
15
    return "libx32";
171
172
3.99k
  if (Triple.getArch() == llvm::Triple::riscv32)
173
47
    return "lib32";
174
175
3.94k
  return Triple.isArch32Bit() ? 
"lib"809
:
"lib64"3.13k
;
176
3.99k
}
177
178
Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
179
5.15k
    : Generic_ELF(D, Triple, Args) {
180
5.15k
  GCCInstallation.init(Triple, Args);
181
5.15k
  Multilibs = GCCInstallation.getMultilibs();
182
5.15k
  SelectedMultilib = GCCInstallation.getMultilib();
183
5.15k
  llvm::Triple::ArchType Arch = Triple.getArch();
184
5.15k
  std::string SysRoot = computeSysRoot();
185
5.15k
  ToolChain::path_list &PPaths = getProgramPaths();
186
187
5.15k
  Generic_GCC::PushPPaths(PPaths);
188
189
5.15k
  Distro Distro(D.getVFS(), Triple);
190
191
5.15k
  if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
192
224
    ExtraOpts.push_back("-z");
193
224
    ExtraOpts.push_back("now");
194
224
  }
195
196
5.15k
  if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
197
5.15k
      Triple.isAndroid()) {
198
224
    ExtraOpts.push_back("-z");
199
224
    ExtraOpts.push_back("relro");
200
224
  }
201
202
  // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld
203
  // from 11 onwards default max-page-size to 65536 for both ARM and AArch64.
204
5.15k
  if ((Triple.isARM() || 
Triple.isAArch64()4.50k
) &&
Triple.isAndroid()984
) {
205
149
    ExtraOpts.push_back("-z");
206
149
    ExtraOpts.push_back("max-page-size=4096");
207
149
  }
208
209
5.15k
  if (GCCInstallation.getParentLibPath().find("opt/rh/devtoolset") !=
210
5.15k
      StringRef::npos)
211
    // With devtoolset on RHEL, we want to add a bin directory that is relative
212
    // to the detected gcc install, because if we are using devtoolset gcc then
213
    // we want to use other tools from devtoolset (e.g. ld) instead of the
214
    // standard system tools.
215
0
    PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
216
0
                     "/../bin").str());
217
218
5.15k
  if (Arch == llvm::Triple::arm || 
Arch == llvm::Triple::thumb4.55k
)
219
620
    ExtraOpts.push_back("-X");
220
221
5.15k
  const bool IsAndroid = Triple.isAndroid();
222
5.15k
  const bool IsMips = Triple.isMIPS();
223
5.15k
  const bool IsHexagon = Arch == llvm::Triple::hexagon;
224
5.15k
  const bool IsRISCV = Triple.isRISCV();
225
226
5.15k
  if (IsMips && 
!SysRoot.empty()591
)
227
194
    ExtraOpts.push_back("--sysroot=" + SysRoot);
228
229
  // Do not use 'gnu' hash style for Mips targets because .gnu.hash
230
  // and the MIPS ABI require .dynsym to be sorted in different ways.
231
  // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
232
  // ABI requires a mapping between the GOT and the symbol table.
233
  // Android loader does not support .gnu.hash until API 23.
234
  // Hexagon linker/loader does not support .gnu.hash
235
5.15k
  if (!IsMips && 
!IsHexagon4.56k
) {
236
4.46k
    if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
237
4.46k
        (Distro.IsUbuntu() && 
Distro >= Distro::UbuntuMaverick0
) ||
238
4.46k
        (IsAndroid && 
!Triple.isAndroidVersionLT(23)192
))
239
15
      ExtraOpts.push_back("--hash-style=gnu");
240
241
4.46k
    if (Distro.IsDebian() || Distro.IsOpenSUSE() ||
242
4.46k
        Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty ||
243
4.46k
        Distro == Distro::UbuntuKarmic ||
244
4.46k
        (IsAndroid && 
Triple.isAndroidVersionLT(23)192
))
245
177
      ExtraOpts.push_back("--hash-style=both");
246
4.46k
  }
247
248
#ifdef ENABLE_LINKER_BUILD_ID
249
  ExtraOpts.push_back("--build-id");
250
#endif
251
252
5.15k
  if (IsAndroid || 
Distro.IsOpenSUSE()4.93k
)
253
224
    ExtraOpts.push_back("--enable-new-dtags");
254
255
  // The selection of paths to try here is designed to match the patterns which
256
  // the GCC driver itself uses, as this is part of the GCC-compatible driver.
257
  // This was determined by running GCC in a fake filesystem, creating all
258
  // possible permutations of these directories, and seeing which ones it added
259
  // to the link paths.
260
5.15k
  path_list &Paths = getFilePaths();
261
262
5.15k
  const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
263
5.15k
  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
264
265
5.15k
  Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
266
267
5.15k
  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
268
5.15k
  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
269
270
5.15k
  if (IsAndroid) {
271
    // Android sysroots contain a library directory for each supported OS
272
    // version as well as some unversioned libraries in the usual multiarch
273
    // directory.
274
224
    unsigned Major;
275
224
    unsigned Minor;
276
224
    unsigned Micro;
277
224
    Triple.getEnvironmentVersion(Major, Minor, Micro);
278
224
    addPathIfExists(D,
279
224
                    SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
280
224
                        llvm::to_string(Major),
281
224
                    Paths);
282
224
  }
283
284
5.15k
  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
285
  // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
286
  // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
287
  // this here.
288
5.15k
  if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
289
5.15k
      
Triple.isArch64Bit()4
)
290
2
    addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths);
291
5.15k
  else
292
5.15k
    addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
293
5.15k
  if (IsRISCV) {
294
91
    StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
295
91
    addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
296
91
    addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
297
91
  }
298
299
5.15k
  Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
300
301
  // Similar to the logic for GCC above, if we are currently running Clang
302
  // inside of the requested system root, add its parent library path to those
303
  // searched.
304
  // FIXME: It's not clear whether we should use the driver's installed
305
  // directory ('Dir' below) or the ResourceDir.
306
5.15k
  if (StringRef(D.Dir).startswith(SysRoot))
307
4.61k
    addPathIfExists(D, D.Dir + "/../lib", Paths);
308
309
5.15k
  addPathIfExists(D, SysRoot + "/lib", Paths);
310
5.15k
  addPathIfExists(D, SysRoot + "/usr/lib", Paths);
311
5.15k
}
312
313
1.82k
ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const {
314
1.82k
  if (getTriple().isAndroid())
315
167
    return ToolChain::RLT_CompilerRT;
316
1.65k
  return Generic_ELF::GetDefaultRuntimeLibType();
317
1.82k
}
318
319
1.53k
ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
320
1.53k
  if (getTriple().isAndroid())
321
22
    return ToolChain::CST_Libcxx;
322
1.51k
  return ToolChain::CST_Libstdcxx;
323
1.53k
}
324
325
1.79k
bool Linux::HasNativeLLVMSupport() const { return true; }
326
327
1.63k
Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
328
329
7
Tool *Linux::buildStaticLibTool() const {
330
7
  return new tools::gnutools::StaticLibTool(*this);
331
7
}
332
333
199
Tool *Linux::buildAssembler() const {
334
199
  return new tools::gnutools::Assembler(*this);
335
199
}
336
337
11.0k
std::string Linux::computeSysRoot() const {
338
11.0k
  if (!getDriver().SysRoot.empty())
339
773
    return getDriver().SysRoot;
340
341
10.2k
  if (getTriple().isAndroid()) {
342
    // Android toolchains typically include a sysroot at ../sysroot relative to
343
    // the clang binary.
344
239
    const StringRef ClangDir = getDriver().getInstalledDir();
345
239
    std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
346
239
    if (getVFS().exists(AndroidSysRootPath))
347
0
      return AndroidSysRootPath;
348
239
  }
349
350
10.2k
  if (!GCCInstallation.isValid() || 
!getTriple().isMIPS()369
)
351
9.93k
    return std::string();
352
353
  // Standalone MIPS toolchains use different names for sysroot folder
354
  // and put it into different places. Here we try to check some known
355
  // variants.
356
357
324
  const StringRef InstallDir = GCCInstallation.getInstallPath();
358
324
  const StringRef TripleStr = GCCInstallation.getTriple().str();
359
324
  const Multilib &Multilib = GCCInstallation.getMultilib();
360
361
324
  std::string Path =
362
324
      (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
363
324
          .str();
364
365
324
  if (getVFS().exists(Path))
366
48
    return Path;
367
368
276
  Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
369
370
276
  if (getVFS().exists(Path))
371
276
    return Path;
372
373
0
  return std::string();
374
276
}
375
376
1.53k
std::string Linux::getDynamicLinker(const ArgList &Args) const {
377
1.53k
  const llvm::Triple::ArchType Arch = getArch();
378
1.53k
  const llvm::Triple &Triple = getTriple();
379
380
1.53k
  const Distro Distro(getDriver().getVFS(), Triple);
381
382
1.53k
  if (Triple.isAndroid())
383
133
    return Triple.isArch64Bit() ? 
"/system/bin/linker64"47
:
"/system/bin/linker"86
;
384
385
1.40k
  if (Triple.isMusl()) {
386
30
    std::string ArchName;
387
30
    bool IsArm = false;
388
389
30
    switch (Arch) {
390
4
    case llvm::Triple::arm:
391
7
    case llvm::Triple::thumb:
392
7
      ArchName = "arm";
393
7
      IsArm = true;
394
7
      break;
395
3
    case llvm::Triple::armeb:
396
6
    case llvm::Triple::thumbeb:
397
6
      ArchName = "armeb";
398
6
      IsArm = true;
399
6
      break;
400
2
    case llvm::Triple::x86:
401
2
      ArchName = "i386";
402
2
      break;
403
6
    case llvm::Triple::x86_64:
404
6
      ArchName = Triple.isX32() ? 
"x32"2
:
Triple.getArchName().str()4
;
405
6
      break;
406
9
    default:
407
9
      ArchName = Triple.getArchName().str();
408
30
    }
409
30
    if (IsArm &&
410
30
        
(13
Triple.getEnvironment() == llvm::Triple::MuslEABIHF13
||
411
13
         
tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard8
))
412
9
      ArchName += "hf";
413
414
30
    return "/lib/ld-musl-" + ArchName + ".so.1";
415
30
  }
416
417
1.37k
  std::string LibDir;
418
1.37k
  std::string Loader;
419
420
1.37k
  switch (Arch) {
421
0
  default:
422
0
    llvm_unreachable("unsupported architecture");
423
424
38
  case llvm::Triple::aarch64:
425
38
    LibDir = "lib";
426
38
    Loader = "ld-linux-aarch64.so.1";
427
38
    break;
428
3
  case llvm::Triple::aarch64_be:
429
3
    LibDir = "lib";
430
3
    Loader = "ld-linux-aarch64_be.so.1";
431
3
    break;
432
130
  case llvm::Triple::arm:
433
132
  case llvm::Triple::thumb:
434
138
  case llvm::Triple::armeb:
435
140
  case llvm::Triple::thumbeb: {
436
140
    const bool HF =
437
140
        Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
438
140
        
tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard122
;
439
440
140
    LibDir = "lib";
441
140
    Loader = HF ? 
"ld-linux-armhf.so.3"20
:
"ld-linux.so.3"120
;
442
140
    break;
443
138
  }
444
24
  case llvm::Triple::m68k:
445
24
    LibDir = "lib";
446
24
    Loader = "ld.so.1";
447
24
    break;
448
59
  case llvm::Triple::mips:
449
118
  case llvm::Triple::mipsel:
450
160
  case llvm::Triple::mips64:
451
201
  case llvm::Triple::mips64el: {
452
201
    bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple);
453
454
201
    LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
455
456
201
    if (tools::mips::isUCLibc(Args))
457
16
      Loader = IsNaN2008 ? 
"ld-uClibc-mipsn8.so.0"6
:
"ld-uClibc.so.0"10
;
458
185
    else if (!Triple.hasEnvironment() &&
459
185
             
Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies5
)
460
2
      Loader =
461
2
          Triple.isLittleEndian() ? 
"ld-musl-mipsel.so.1"1
:
"ld-musl-mips.so.1"1
;
462
183
    else
463
183
      Loader = IsNaN2008 ? 
"ld-linux-mipsn8.so.1"68
:
"ld.so.1"115
;
464
465
201
    break;
466
160
  }
467
18
  case llvm::Triple::ppc:
468
18
    LibDir = "lib";
469
18
    Loader = "ld.so.1";
470
18
    break;
471
3
  case llvm::Triple::ppcle:
472
3
    LibDir = "lib";
473
3
    Loader = "ld.so.1";
474
3
    break;
475
49
  case llvm::Triple::ppc64:
476
49
    LibDir = "lib64";
477
49
    Loader =
478
49
        (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? 
"ld64.so.2"3
:
"ld64.so.1"46
;
479
49
    break;
480
43
  case llvm::Triple::ppc64le:
481
43
    LibDir = "lib64";
482
43
    Loader =
483
43
        (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? 
"ld64.so.1"4
:
"ld64.so.2"39
;
484
43
    break;
485
4
  case llvm::Triple::riscv32: {
486
4
    StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
487
4
    LibDir = "lib";
488
4
    Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
489
4
    break;
490
160
  }
491
10
  case llvm::Triple::riscv64: {
492
10
    StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
493
10
    LibDir = "lib";
494
10
    Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
495
10
    break;
496
160
  }
497
1
  case llvm::Triple::sparc:
498
2
  case llvm::Triple::sparcel:
499
2
    LibDir = "lib";
500
2
    Loader = "ld-linux.so.2";
501
2
    break;
502
1
  case llvm::Triple::sparcv9:
503
1
    LibDir = "lib64";
504
1
    Loader = "ld-linux.so.2";
505
1
    break;
506
10
  case llvm::Triple::systemz:
507
10
    LibDir = "lib";
508
10
    Loader = "ld64.so.1";
509
10
    break;
510
222
  case llvm::Triple::x86:
511
222
    LibDir = "lib";
512
222
    Loader = "ld-linux.so.2";
513
222
    break;
514
604
  case llvm::Triple::x86_64: {
515
604
    bool X32 = Triple.isX32();
516
517
604
    LibDir = X32 ? 
"libx32"11
:
"lib64"593
;
518
604
    Loader = X32 ? 
"ld-linux-x32.so.2"11
:
"ld-linux-x86-64.so.2"593
;
519
604
    break;
520
1
  }
521
0
  case llvm::Triple::ve:
522
0
    return "/opt/nec/ve/lib/ld-linux-ve.so.1";
523
1.37k
  }
524
525
1.37k
  if (Distro == Distro::Exherbo &&
526
1.37k
      
(0
Triple.getVendor() == llvm::Triple::UnknownVendor0
||
527
0
       Triple.getVendor() == llvm::Triple::PC))
528
0
    return "/usr/" + Triple.str() + "/lib/" + Loader;
529
1.37k
  return "/" + LibDir + "/" + Loader;
530
1.37k
}
531
532
void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
533
5.84k
                                      ArgStringList &CC1Args) const {
534
5.84k
  const Driver &D = getDriver();
535
5.84k
  std::string SysRoot = computeSysRoot();
536
537
5.84k
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
538
1
    return;
539
540
  // Add 'include' in the resource directory, which is similar to
541
  // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
542
  // contains some files conflicting with system /usr/include. musl systems
543
  // prefer the /usr/include copies which are more relevant.
544
5.84k
  SmallString<128> ResourceDirInclude(D.ResourceDir);
545
5.84k
  llvm::sys::path::append(ResourceDirInclude, "include");
546
5.84k
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
547
5.84k
      
(5.83k
!getTriple().isMusl()5.83k
||
DriverArgs.hasArg(options::OPT_nostdlibinc)43
))
548
5.79k
    addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
549
550
5.84k
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
551
2
    return;
552
553
  // LOCAL_INCLUDE_DIR
554
5.83k
  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
555
  // TOOL_INCLUDE_DIR
556
5.83k
  AddMultilibIncludeArgs(DriverArgs, CC1Args);
557
558
  // Check for configure-time C include directories.
559
5.83k
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
560
5.83k
  if (CIncludeDirs != "") {
561
0
    SmallVector<StringRef, 5> dirs;
562
0
    CIncludeDirs.split(dirs, ":");
563
0
    for (StringRef dir : dirs) {
564
0
      StringRef Prefix =
565
0
          llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
566
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
567
0
    }
568
0
    return;
569
0
  }
570
571
  // On systems using multiarch and Android, add /usr/include/$triple before
572
  // /usr/include.
573
5.83k
  std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
574
5.83k
  if (!MultiarchIncludeDir.empty() &&
575
5.83k
      D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
576
43
    addExternCSystemInclude(DriverArgs, CC1Args,
577
43
                            SysRoot + "/usr/include/" + MultiarchIncludeDir);
578
579
5.83k
  if (getTriple().getOS() == llvm::Triple::RTEMS)
580
0
    return;
581
582
  // Add an include of '/include' directly. This isn't provided by default by
583
  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
584
  // add even when Clang is acting as-if it were a system compiler.
585
5.83k
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
586
587
5.83k
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
588
589
5.83k
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && 
getTriple().isMusl()5.83k
)
590
42
    addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
591
5.83k
}
592
593
void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
594
2.83k
                                     llvm::opt::ArgStringList &CC1Args) const {
595
  // We need a detected GCC installation on Linux to provide libstdc++'s
596
  // headers in odd Linuxish places.
597
2.83k
  if (!GCCInstallation.isValid())
598
2.60k
    return;
599
600
  // Detect Debian g++-multiarch-incdir.diff.
601
226
  StringRef TripleStr = GCCInstallation.getTriple().str();
602
226
  StringRef DebianMultiarch =
603
226
      GCCInstallation.getTriple().getArch() == llvm::Triple::x86
604
226
          ? 
"i386-linux-gnu"6
605
226
          : 
TripleStr220
;
606
607
  // Try generic GCC detection first.
608
226
  if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
609
226
                                               DebianMultiarch))
610
197
    return;
611
612
29
  StringRef LibDir = GCCInstallation.getParentLibPath();
613
29
  const Multilib &Multilib = GCCInstallation.getMultilib();
614
29
  const GCCVersion &Version = GCCInstallation.getVersion();
615
616
29
  const std::string LibStdCXXIncludePathCandidates[] = {
617
      // Android standalone toolchain has C++ headers in yet another place.
618
29
      LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
619
      // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
620
      // without a subdirectory corresponding to the gcc version.
621
29
      LibDir.str() + "/../include/c++",
622
      // Cray's gcc installation puts headers under "g++" without a
623
      // version suffix.
624
29
      LibDir.str() + "/../include/g++",
625
29
  };
626
627
87
  for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
628
87
    if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
629
87
                                 Multilib.includeSuffix(), DriverArgs, CC1Args))
630
1
      break;
631
87
  }
632
29
}
633
634
void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
635
143
                               ArgStringList &CC1Args) const {
636
143
  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
637
143
}
638
639
void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
640
165
                              ArgStringList &CC1Args) const {
641
165
  RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
642
165
}
643
644
void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
645
9
                                ArgStringList &CC1Args) const {
646
9
  if (GCCInstallation.isValid()) {
647
0
    CC1Args.push_back("-isystem");
648
0
    CC1Args.push_back(DriverArgs.MakeArgString(
649
0
        GCCInstallation.getParentLibPath() + "/../" +
650
0
        GCCInstallation.getTriple().str() + "/include"));
651
0
  }
652
9
}
653
654
7.32k
bool Linux::isPIEDefault() const {
655
7.32k
  return (getTriple().isAndroid() && 
!getTriple().isAndroidVersionLT(16)368
) ||
656
7.32k
          
getTriple().isMusl()7.13k
||
getSanitizerArgs().requiresPIE()7.04k
;
657
7.32k
}
658
659
323
bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
660
  // Outline atomics for AArch64 are supported by compiler-rt
661
  // and libgcc since 9.3.1
662
323
  assert(getTriple().isAArch64() && "expected AArch64 target!");
663
0
  ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args);
664
323
  if (RtLib == ToolChain::RLT_CompilerRT)
665
52
    return true;
666
271
  assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
667
271
  if (GCCInstallation.getVersion().isOlderThan(9, 3, 1))
668
268
    return false;
669
3
  return true;
670
271
}
671
672
4.46k
bool Linux::isNoExecStackDefault() const {
673
4.46k
    return getTriple().isAndroid();
674
4.46k
}
675
676
4.82k
bool Linux::IsMathErrnoDefault() const {
677
4.82k
  if (getTriple().isAndroid())
678
216
    return false;
679
4.61k
  return Generic_ELF::IsMathErrnoDefault();
680
4.82k
}
681
682
5.30k
SanitizerMask Linux::getSupportedSanitizers() const {
683
5.30k
  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
684
5.30k
  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
685
5.30k
  const bool IsMIPS = getTriple().isMIPS32();
686
5.30k
  const bool IsMIPS64 = getTriple().isMIPS64();
687
5.30k
  const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
688
5.30k
                           
getTriple().getArch() == llvm::Triple::ppc64le5.19k
;
689
5.30k
  const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
690
5.30k
                         
getTriple().getArch() == llvm::Triple::aarch64_be4.97k
;
691
5.30k
  const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
692
5.30k
                         
getTriple().getArch() == llvm::Triple::thumb4.70k
||
693
5.30k
                         
getTriple().getArch() == llvm::Triple::armeb4.69k
||
694
5.30k
                         
getTriple().getArch() == llvm::Triple::thumbeb4.64k
;
695
5.30k
  const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
696
5.30k
  const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
697
5.30k
  const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon;
698
5.30k
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
699
5.30k
  Res |= SanitizerKind::Address;
700
5.30k
  Res |= SanitizerKind::PointerCompare;
701
5.30k
  Res |= SanitizerKind::PointerSubtract;
702
5.30k
  Res |= SanitizerKind::Fuzzer;
703
5.30k
  Res |= SanitizerKind::FuzzerNoLink;
704
5.30k
  Res |= SanitizerKind::KernelAddress;
705
5.30k
  Res |= SanitizerKind::Memory;
706
5.30k
  Res |= SanitizerKind::Vptr;
707
5.30k
  Res |= SanitizerKind::SafeStack;
708
5.30k
  if (IsX86_64 || 
IsMIPS642.67k
||
IsAArch642.49k
)
709
3.13k
    Res |= SanitizerKind::DataFlow;
710
5.30k
  if (IsX86_64 || 
IsMIPS642.67k
||
IsAArch642.49k
||
IsX862.16k
||
IsArmArch1.65k
||
IsPowerPC64991
||
711
5.30k
      
IsRISCV64748
||
IsSystemZ704
||
IsHexagon659
)
712
4.74k
    Res |= SanitizerKind::Leak;
713
5.30k
  if (IsX86_64 || 
IsMIPS642.67k
||
IsAArch642.49k
||
IsPowerPC642.16k
||
IsSystemZ1.92k
)
714
3.42k
    Res |= SanitizerKind::Thread;
715
5.30k
  if (IsX86_64)
716
2.62k
    Res |= SanitizerKind::KernelMemory;
717
5.30k
  if (IsX86 || 
IsX86_644.79k
)
718
3.13k
    Res |= SanitizerKind::Function;
719
5.30k
  if (IsX86_64 || 
IsMIPS642.67k
||
IsAArch642.49k
||
IsX862.16k
||
IsMIPS1.65k
||
IsArmArch1.24k
||
720
5.30k
      
IsPowerPC64578
||
IsHexagon335
)
721
5.06k
    Res |= SanitizerKind::Scudo;
722
5.30k
  if (IsX86_64 || 
IsAArch642.67k
) {
723
2.96k
    Res |= SanitizerKind::HWAddress;
724
2.96k
    Res |= SanitizerKind::KernelHWAddress;
725
2.96k
  }
726
5.30k
  return Res;
727
5.30k
}
728
729
void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
730
1.62k
                             llvm::opt::ArgStringList &CmdArgs) const {
731
  // Add linker option -u__llvm_profile_runtime to cause runtime
732
  // initialization module to be linked in.
733
1.62k
  if (needsProfileRT(Args))
734
5
    CmdArgs.push_back(Args.MakeArgString(
735
5
        Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
736
1.62k
  ToolChain::addProfileRTLibs(Args, CmdArgs);
737
1.62k
}
738
739
llvm::DenormalMode
740
Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
741
                                     const JobAction &JA,
742
9.63k
                                     const llvm::fltSemantics *FPType) const {
743
9.63k
  switch (getTriple().getArch()) {
744
952
  case llvm::Triple::x86:
745
5.60k
  case llvm::Triple::x86_64: {
746
5.60k
    std::string Unused;
747
    // DAZ and FTZ are turned on in crtfastmath.o
748
5.60k
    if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
749
5.60k
        
isFastMathRuntimeAvailable(DriverArgs, Unused)5.58k
)
750
20
      return llvm::DenormalMode::getPreserveSign();
751
5.58k
    return llvm::DenormalMode::getIEEE();
752
5.60k
  }
753
4.03k
  default:
754
4.03k
    return llvm::DenormalMode::getIEEE();
755
9.63k
  }
756
9.63k
}
757
758
1.63k
void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
759
1.63k
  for (const auto &Opt : ExtraOpts)
760
1.58k
    CmdArgs.push_back(Opt.c_str());
761
1.63k
}