Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/Fuchsia.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Fuchsia.cpp - Fuchsia 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 "Fuchsia.h"
10
#include "CommonArgs.h"
11
#include "clang/Config/config.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/Driver.h"
14
#include "clang/Driver/DriverDiagnostic.h"
15
#include "clang/Driver/Options.h"
16
#include "clang/Driver/SanitizerArgs.h"
17
#include "llvm/Option/ArgList.h"
18
#include "llvm/ProfileData/InstrProf.h"
19
#include "llvm/Support/FileSystem.h"
20
#include "llvm/Support/Path.h"
21
#include "llvm/Support/VirtualFileSystem.h"
22
23
using namespace clang::driver;
24
using namespace clang::driver::toolchains;
25
using namespace clang::driver::tools;
26
using namespace clang;
27
using namespace llvm::opt;
28
29
using tools::addMultilibFlag;
30
31
void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
32
                                   const InputInfo &Output,
33
                                   const InputInfoList &Inputs,
34
                                   const ArgList &Args,
35
53
                                   const char *LinkingOutput) const {
36
53
  const toolchains::Fuchsia &ToolChain =
37
53
      static_cast<const toolchains::Fuchsia &>(getToolChain());
38
53
  const Driver &D = ToolChain.getDriver();
39
40
53
  ArgStringList CmdArgs;
41
42
  // Silence warning for "clang -g foo.o -o foo"
43
53
  Args.ClaimAllArgs(options::OPT_g_Group);
44
  // and "clang -emit-llvm foo.o -o foo"
45
53
  Args.ClaimAllArgs(options::OPT_emit_llvm);
46
  // and for "clang -w foo.o -o foo". Other warning options are already
47
  // handled somewhere else.
48
53
  Args.ClaimAllArgs(options::OPT_w);
49
50
53
  CmdArgs.push_back("-z");
51
53
  CmdArgs.push_back("max-page-size=4096");
52
53
53
  CmdArgs.push_back("-z");
54
53
  CmdArgs.push_back("now");
55
56
53
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
57
53
  if (llvm::sys::path::filename(Exec).equals_insensitive("ld.lld") ||
58
53
      
llvm::sys::path::stem(Exec).equals_insensitive("ld.lld")0
) {
59
53
    CmdArgs.push_back("-z");
60
53
    CmdArgs.push_back("rodynamic");
61
53
    CmdArgs.push_back("-z");
62
53
    CmdArgs.push_back("separate-loadable-segments");
63
53
    CmdArgs.push_back("--pack-dyn-relocs=relr");
64
53
  }
65
66
53
  if (!D.SysRoot.empty())
67
6
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
68
69
53
  if (!Args.hasArg(options::OPT_shared) && 
!Args.hasArg(options::OPT_r)49
)
70
48
    CmdArgs.push_back("-pie");
71
72
53
  if (Args.hasArg(options::OPT_rdynamic))
73
0
    CmdArgs.push_back("-export-dynamic");
74
75
53
  if (Args.hasArg(options::OPT_s))
76
0
    CmdArgs.push_back("-s");
77
78
53
  if (Args.hasArg(options::OPT_r)) {
79
1
    CmdArgs.push_back("-r");
80
52
  } else {
81
52
    CmdArgs.push_back("--build-id");
82
52
    CmdArgs.push_back("--hash-style=gnu");
83
52
  }
84
85
53
  CmdArgs.push_back("--eh-frame-hdr");
86
87
53
  if (Args.hasArg(options::OPT_static))
88
1
    CmdArgs.push_back("-Bstatic");
89
52
  else if (Args.hasArg(options::OPT_shared))
90
4
    CmdArgs.push_back("-shared");
91
92
53
  const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs();
93
94
53
  if (!Args.hasArg(options::OPT_shared)) {
95
49
    std::string Dyld = D.DyldPrefix;
96
49
    if (SanArgs.needsAsanRt() && 
SanArgs.needsSharedRt()6
)
97
6
      Dyld += "asan/";
98
49
    if (SanArgs.needsHwasanRt() && 
SanArgs.needsSharedRt()4
)
99
4
      Dyld += "hwasan/";
100
49
    if (SanArgs.needsTsanRt() && 
SanArgs.needsSharedRt()0
)
101
0
      Dyld += "tsan/";
102
49
    Dyld += "ld.so.1";
103
49
    CmdArgs.push_back("-dynamic-linker");
104
49
    CmdArgs.push_back(Args.MakeArgString(Dyld));
105
49
  }
106
107
53
  CmdArgs.push_back("-o");
108
53
  CmdArgs.push_back(Output.getFilename());
109
110
53
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
111
52
    if (!Args.hasArg(options::OPT_shared)) {
112
48
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
113
48
    }
114
52
  }
115
116
53
  Args.AddAllArgs(CmdArgs, options::OPT_L);
117
53
  Args.AddAllArgs(CmdArgs, options::OPT_u);
118
119
53
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
120
121
53
  if (D.isUsingLTO()) {
122
2
    assert(!Inputs.empty() && "Must have at least one input.");
123
0
    addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
124
2
                  D.getLTOMode() == LTOK_Thin);
125
2
  }
126
127
0
  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
128
53
  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
129
53
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
130
53
  ToolChain.addProfileRTLibs(Args, CmdArgs);
131
132
53
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
133
51
    if (Args.hasArg(options::OPT_static))
134
1
      CmdArgs.push_back("-Bdynamic");
135
136
51
    if (D.CCCIsCXX()) {
137
24
      if (ToolChain.ShouldLinkCXXStdlib(Args)) {
138
23
        bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
139
23
                                   
!Args.hasArg(options::OPT_static)1
;
140
23
        CmdArgs.push_back("--push-state");
141
23
        CmdArgs.push_back("--as-needed");
142
23
        if (OnlyLibstdcxxStatic)
143
1
          CmdArgs.push_back("-Bstatic");
144
23
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
145
23
        if (OnlyLibstdcxxStatic)
146
1
          CmdArgs.push_back("-Bdynamic");
147
23
        CmdArgs.push_back("-lm");
148
23
        CmdArgs.push_back("--pop-state");
149
23
      }
150
24
    }
151
152
51
    if (NeedsSanitizerDeps)
153
2
      linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
154
155
51
    if (NeedsXRayDeps)
156
2
      linkXRayRuntimeDeps(ToolChain, CmdArgs);
157
158
51
    AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
159
160
51
    if (Args.hasArg(options::OPT_pthread) ||
161
51
        Args.hasArg(options::OPT_pthreads))
162
0
      CmdArgs.push_back("-lpthread");
163
164
51
    if (Args.hasArg(options::OPT_fsplit_stack))
165
0
      CmdArgs.push_back("--wrap=pthread_create");
166
167
51
    if (!Args.hasArg(options::OPT_nolibc))
168
50
      CmdArgs.push_back("-lc");
169
51
  }
170
171
53
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
172
53
                                         Exec, CmdArgs, Inputs, Output));
173
53
}
174
175
/// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
176
177
Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
178
                 const ArgList &Args)
179
58
    : ToolChain(D, Triple, Args) {
180
58
  getProgramPaths().push_back(getDriver().getInstalledDir());
181
58
  if (getDriver().getInstalledDir() != D.Dir)
182
0
    getProgramPaths().push_back(D.Dir);
183
184
58
  if (!D.SysRoot.empty()) {
185
6
    SmallString<128> P(D.SysRoot);
186
6
    llvm::sys::path::append(P, "lib");
187
6
    getFilePaths().push_back(std::string(P.str()));
188
6
  }
189
190
768
  auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> {
191
768
    std::vector<std::string> FP;
192
768
    SmallString<128> P(getStdlibPath());
193
768
    llvm::sys::path::append(P, M.gccSuffix());
194
768
    FP.push_back(std::string(P.str()));
195
768
    return FP;
196
768
  };
197
198
58
  Multilibs.push_back(Multilib());
199
  // Use the noexcept variant with -fno-exceptions to avoid the extra overhead.
200
58
  Multilibs.push_back(Multilib("noexcept", {}, {}, 1)
201
58
                          .flag("-fexceptions")
202
58
                          .flag("+fno-exceptions"));
203
  // ASan has higher priority because we always want the instrumentated version.
204
58
  Multilibs.push_back(Multilib("asan", {}, {}, 2)
205
58
                          .flag("+fsanitize=address"));
206
  // Use the asan+noexcept variant with ASan and -fno-exceptions.
207
58
  Multilibs.push_back(Multilib("asan+noexcept", {}, {}, 3)
208
58
                          .flag("+fsanitize=address")
209
58
                          .flag("-fexceptions")
210
58
                          .flag("+fno-exceptions"));
211
  // HWASan has higher priority because we always want the instrumentated
212
  // version.
213
58
  Multilibs.push_back(
214
58
      Multilib("hwasan", {}, {}, 4).flag("+fsanitize=hwaddress"));
215
  // Use the hwasan+noexcept variant with HWASan and -fno-exceptions.
216
58
  Multilibs.push_back(Multilib("hwasan+noexcept", {}, {}, 5)
217
58
                          .flag("+fsanitize=hwaddress")
218
58
                          .flag("-fexceptions")
219
58
                          .flag("+fno-exceptions"));
220
  // Use the relative vtables ABI.
221
  // TODO: Remove these multilibs once relative vtables are enabled by default
222
  // for Fuchsia.
223
58
  Multilibs.push_back(Multilib("relative-vtables", {}, {}, 6)
224
58
                          .flag("+fexperimental-relative-c++-abi-vtables"));
225
58
  Multilibs.push_back(Multilib("relative-vtables+noexcept", {}, {}, 7)
226
58
                          .flag("+fexperimental-relative-c++-abi-vtables")
227
58
                          .flag("-fexceptions")
228
58
                          .flag("+fno-exceptions"));
229
58
  Multilibs.push_back(Multilib("relative-vtables+asan", {}, {}, 8)
230
58
                          .flag("+fexperimental-relative-c++-abi-vtables")
231
58
                          .flag("+fsanitize=address"));
232
58
  Multilibs.push_back(Multilib("relative-vtables+asan+noexcept", {}, {}, 9)
233
58
                          .flag("+fexperimental-relative-c++-abi-vtables")
234
58
                          .flag("+fsanitize=address")
235
58
                          .flag("-fexceptions")
236
58
                          .flag("+fno-exceptions"));
237
58
  Multilibs.push_back(Multilib("relative-vtables+hwasan", {}, {}, 10)
238
58
                          .flag("+fexperimental-relative-c++-abi-vtables")
239
58
                          .flag("+fsanitize=hwaddress"));
240
58
  Multilibs.push_back(Multilib("relative-vtables+hwasan+noexcept", {}, {}, 11)
241
58
                          .flag("+fexperimental-relative-c++-abi-vtables")
242
58
                          .flag("+fsanitize=hwaddress")
243
58
                          .flag("-fexceptions")
244
58
                          .flag("+fno-exceptions"));
245
  // Use Itanium C++ ABI for the compat multilib.
246
58
  Multilibs.push_back(Multilib("compat", {}, {}, 12).flag("+fc++-abi=itanium"));
247
248
754
  Multilibs.FilterOut([&](const Multilib &M) {
249
754
    std::vector<std::string> RD = FilePaths(M);
250
754
    return std::all_of(RD.begin(), RD.end(), [&](std::string P) {
251
754
      return !getVFS().exists(P);
252
754
    });
253
754
  });
254
255
58
  Multilib::flags_list Flags;
256
58
  addMultilibFlag(
257
58
      Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true),
258
58
      "fexceptions", Flags);
259
58
  addMultilibFlag(getSanitizerArgs().needsAsanRt(), "fsanitize=address", Flags);
260
58
  addMultilibFlag(getSanitizerArgs().needsHwasanRt(), "fsanitize=hwaddress",
261
58
                  Flags);
262
263
58
  addMultilibFlag(
264
58
      Args.hasFlag(options::OPT_fexperimental_relative_cxx_abi_vtables,
265
58
                   options::OPT_fno_experimental_relative_cxx_abi_vtables,
266
58
                   /*default=*/false),
267
58
      "fexperimental-relative-c++-abi-vtables", Flags);
268
58
  addMultilibFlag(Args.getLastArgValue(options::OPT_fcxx_abi_EQ) == "itanium",
269
58
                  "fc++-abi=itanium", Flags);
270
271
58
  Multilibs.setFilePathsCallback(FilePaths);
272
273
58
  if (Multilibs.select(Flags, SelectedMultilib))
274
20
    if (!SelectedMultilib.isDefault())
275
14
      if (const auto &PathsCallback = Multilibs.filePathsCallback())
276
14
        for (const auto &Path : PathsCallback(SelectedMultilib))
277
          // Prepend the multilib path to ensure it takes the precedence.
278
14
          getFilePaths().insert(getFilePaths().begin(), Path);
279
58
}
280
281
std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
282
112
                                                 types::ID InputType) const {
283
112
  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
284
112
  return Triple.str();
285
112
}
286
287
53
Tool *Fuchsia::buildLinker() const {
288
53
  return new tools::fuchsia::Linker(*this);
289
53
}
290
291
ToolChain::RuntimeLibType Fuchsia::GetRuntimeLibType(
292
102
    const ArgList &Args) const {
293
102
  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
294
2
    StringRef Value = A->getValue();
295
2
    if (Value != "compiler-rt")
296
2
      getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
297
2
          << A->getAsString(Args);
298
2
  }
299
300
102
  return ToolChain::RLT_CompilerRT;
301
102
}
302
303
ToolChain::CXXStdlibType
304
53
Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
305
53
  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
306
2
    StringRef Value = A->getValue();
307
2
    if (Value != "libc++")
308
2
      getDriver().Diag(diag::err_drv_invalid_stdlib_name)
309
2
        << A->getAsString(Args);
310
2
  }
311
312
53
  return ToolChain::CST_Libcxx;
313
53
}
314
315
void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
316
                                    ArgStringList &CC1Args,
317
59
                                    Action::OffloadKind) const {
318
59
  if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
319
59
                          options::OPT_fno_use_init_array, true))
320
0
    CC1Args.push_back("-fno-use-init-array");
321
59
}
322
323
void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
324
59
                                        ArgStringList &CC1Args) const {
325
59
  const Driver &D = getDriver();
326
327
59
  if (DriverArgs.hasArg(options::OPT_nostdinc))
328
0
    return;
329
330
59
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
331
59
    SmallString<128> P(D.ResourceDir);
332
59
    llvm::sys::path::append(P, "include");
333
59
    addSystemInclude(DriverArgs, CC1Args, P);
334
59
  }
335
336
59
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
337
0
    return;
338
339
  // Check for configure-time C include directories.
340
59
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
341
59
  if (CIncludeDirs != "") {
342
0
    SmallVector<StringRef, 5> dirs;
343
0
    CIncludeDirs.split(dirs, ":");
344
0
    for (StringRef dir : dirs) {
345
0
      StringRef Prefix =
346
0
          llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
347
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
348
0
    }
349
0
    return;
350
0
  }
351
352
59
  if (!D.SysRoot.empty()) {
353
6
    SmallString<128> P(D.SysRoot);
354
6
    llvm::sys::path::append(P, "include");
355
6
    addExternCSystemInclude(DriverArgs, CC1Args, P.str());
356
6
  }
357
59
}
358
359
void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
360
28
                                           ArgStringList &CC1Args) const {
361
28
  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
362
28
      DriverArgs.hasArg(options::OPT_nostdincxx))
363
0
    return;
364
365
28
  const Driver &D = getDriver();
366
28
  std::string Target = getTripleString();
367
368
28
  auto AddCXXIncludePath = [&](StringRef Path) {
369
28
    std::string Version = detectLibcxxVersion(Path);
370
28
    if (Version.empty())
371
0
      return;
372
373
    // First add the per-target include path.
374
28
    SmallString<128> TargetDir(Path);
375
28
    llvm::sys::path::append(TargetDir, Target, "c++", Version);
376
28
    if (getVFS().exists(TargetDir))
377
21
      addSystemInclude(DriverArgs, CC1Args, TargetDir);
378
379
    // Second add the generic one.
380
28
    SmallString<128> Dir(Path);
381
28
    llvm::sys::path::append(Dir, "c++", Version);
382
28
    addSystemInclude(DriverArgs, CC1Args, Dir);
383
28
  };
384
385
28
  switch (GetCXXStdlibType(DriverArgs)) {
386
28
  case ToolChain::CST_Libcxx: {
387
28
    SmallString<128> P(D.Dir);
388
28
    llvm::sys::path::append(P, "..", "include");
389
28
    AddCXXIncludePath(P);
390
28
    break;
391
0
  }
392
393
0
  default:
394
0
    llvm_unreachable("invalid stdlib name");
395
28
  }
396
28
}
397
398
void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
399
25
                                  ArgStringList &CmdArgs) const {
400
25
  switch (GetCXXStdlibType(Args)) {
401
25
  case ToolChain::CST_Libcxx:
402
25
    CmdArgs.push_back("-lc++");
403
25
    break;
404
405
0
  case ToolChain::CST_Libstdcxx:
406
0
    llvm_unreachable("invalid stdlib name");
407
25
  }
408
25
}
409
410
58
SanitizerMask Fuchsia::getSupportedSanitizers() const {
411
58
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
412
58
  Res |= SanitizerKind::Address;
413
58
  Res |= SanitizerKind::HWAddress;
414
58
  Res |= SanitizerKind::PointerCompare;
415
58
  Res |= SanitizerKind::PointerSubtract;
416
58
  Res |= SanitizerKind::Fuzzer;
417
58
  Res |= SanitizerKind::FuzzerNoLink;
418
58
  Res |= SanitizerKind::Leak;
419
58
  Res |= SanitizerKind::SafeStack;
420
58
  Res |= SanitizerKind::Scudo;
421
58
  Res |= SanitizerKind::Thread;
422
58
  return Res;
423
58
}
424
425
58
SanitizerMask Fuchsia::getDefaultSanitizers() const {
426
58
  SanitizerMask Res;
427
58
  switch (getTriple().getArch()) {
428
13
  case llvm::Triple::aarch64:
429
13
    Res |= SanitizerKind::ShadowCallStack;
430
13
    break;
431
43
  case llvm::Triple::x86_64:
432
43
    Res |= SanitizerKind::SafeStack;
433
43
    break;
434
2
  default:
435
    // TODO: Enable SafeStack on RISC-V once tested.
436
2
    break;
437
58
  }
438
58
  return Res;
439
58
}