Coverage Report

Created: 2022-01-22 13:19

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