Coverage Report

Created: 2019-07-24 05:18

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