Coverage Report

Created: 2021-01-23 06:44

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