Coverage Report

Created: 2020-02-15 09:57

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