Coverage Report

Created: 2020-09-22 08:39

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