Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/WebAssembly.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- 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 "WebAssembly.h"
10
#include "CommonArgs.h"
11
#include "clang/Basic/Version.h"
12
#include "clang/Config/config.h"
13
#include "clang/Driver/Compilation.h"
14
#include "clang/Driver/Driver.h"
15
#include "clang/Driver/DriverDiagnostic.h"
16
#include "clang/Driver/Options.h"
17
#include "llvm/Support/FileSystem.h"
18
#include "llvm/Support/Path.h"
19
#include "llvm/Option/ArgList.h"
20
21
using namespace clang::driver;
22
using namespace clang::driver::tools;
23
using namespace clang::driver::toolchains;
24
using namespace clang;
25
using namespace llvm::opt;
26
27
/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
28
/// we remove the vendor field to form the multiarch triple.
29
static std::string getMultiarchTriple(const Driver &D,
30
                                      const llvm::Triple &TargetTriple,
31
19
                                      StringRef SysRoot) {
32
19
    return (TargetTriple.getArchName() + "-" +
33
19
            TargetTriple.getOSAndEnvironmentName()).str();
34
19
}
35
36
29
std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
37
29
  const ToolChain &ToolChain = getToolChain();
38
29
  if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
39
1
    StringRef UseLinker = A->getValue();
40
1
    if (!UseLinker.empty()) {
41
1
      if (llvm::sys::path::is_absolute(UseLinker) &&
42
0
          llvm::sys::fs::can_execute(UseLinker))
43
0
        return std::string(UseLinker);
44
1
45
      // Accept 'lld', and 'ld' as aliases for the default linker
46
1
      if (UseLinker != "lld" && UseLinker != "ld")
47
1
        ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
48
1
            << A->getAsString(Args);
49
1
    }
50
1
  }
51
29
52
29
  return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
53
29
}
54
55
void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
56
                                const InputInfo &Output,
57
                                const InputInfoList &Inputs,
58
                                const ArgList &Args,
59
29
                                const char *LinkingOutput) const {
60
29
61
29
  const ToolChain &ToolChain = getToolChain();
62
29
  const char *Linker = Args.MakeArgString(getLinkerPath(Args));
63
29
  ArgStringList CmdArgs;
64
29
65
29
  CmdArgs.push_back("-m");
66
29
  if (getToolChain().getTriple().isArch64Bit())
67
0
    CmdArgs.push_back("wasm64");
68
29
  else
69
29
    CmdArgs.push_back("wasm32");
70
29
71
29
  if (Args.hasArg(options::OPT_s))
72
0
    CmdArgs.push_back("--strip-all");
73
29
74
29
  Args.AddAllArgs(CmdArgs, options::OPT_L);
75
29
  Args.AddAllArgs(CmdArgs, options::OPT_u);
76
29
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
77
29
78
29
  const char *Crt1 = "crt1.o";
79
29
  const char *Entry = NULL;
80
29
  if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
81
2
    StringRef CM = A->getValue();
82
2
    if (CM == "command") {
83
      // Use default values.
84
1
    } else if (CM == "reactor") {
85
1
      Crt1 = "crt1-reactor.o";
86
1
      Entry = "_initialize";
87
0
    } else {
88
0
      ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
89
0
          << CM << A->getOption().getName();
90
0
    }
91
2
  }
92
29
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
93
29
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
94
29
  if (Entry) {
95
1
    CmdArgs.push_back(Args.MakeArgString("--entry"));
96
1
    CmdArgs.push_back(Args.MakeArgString(Entry));
97
1
  }
98
29
99
29
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
100
29
101
29
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
102
29
    if (ToolChain.ShouldLinkCXXStdlib(Args))
103
7
      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
104
29
105
29
    if (Args.hasArg(options::OPT_pthread)) {
106
5
      CmdArgs.push_back("-lpthread");
107
5
      CmdArgs.push_back("--shared-memory");
108
5
    }
109
29
110
29
    CmdArgs.push_back("-lc");
111
29
    AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
112
29
  }
113
29
114
29
  CmdArgs.push_back("-o");
115
29
  CmdArgs.push_back(Output.getFilename());
116
29
117
29
  C.addCommand(std::make_unique<Command>(
118
29
      JA, *this, ResponseFileSupport::AtFileCurCP(), Linker, CmdArgs, Inputs));
119
29
120
  // When optimizing, if wasm-opt is available, run it.
121
29
  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
122
5
    auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
123
5
    if (WasmOptPath != "wasm-opt") {
124
0
      StringRef OOpt = "s";
125
0
      if (A->getOption().matches(options::OPT_O4) ||
126
0
          A->getOption().matches(options::OPT_Ofast))
127
0
        OOpt = "4";
128
0
      else if (A->getOption().matches(options::OPT_O0))
129
0
        OOpt = "0";
130
0
      else if (A->getOption().matches(options::OPT_O))
131
0
        OOpt = A->getValue();
132
0
133
0
      if (OOpt != "0") {
134
0
        const char *WasmOpt = Args.MakeArgString(WasmOptPath);
135
0
        ArgStringList CmdArgs;
136
0
        CmdArgs.push_back(Output.getFilename());
137
0
        CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
138
0
        CmdArgs.push_back("-o");
139
0
        CmdArgs.push_back(Output.getFilename());
140
0
        C.addCommand(std::make_unique<Command>(
141
0
            JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
142
0
            Inputs));
143
0
      }
144
0
    }
145
5
  }
146
29
}
147
148
/// Given a base library directory, append path components to form the
149
/// LTO directory.
150
1
static std::string AppendLTOLibDir(const std::string &Dir) {
151
    // The version allows the path to be keyed to the specific version of
152
    // LLVM in used, as the bitcode format is not stable.
153
1
    return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
154
1
}
155
156
WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
157
                         const llvm::opt::ArgList &Args)
158
69
    : ToolChain(D, Triple, Args) {
159
69
160
69
  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
161
69
162
69
  getProgramPaths().push_back(getDriver().getInstalledDir());
163
69
164
69
  auto SysRoot = getDriver().SysRoot;
165
69
  if (getTriple().getOS() == llvm::Triple::UnknownOS) {
166
    // Theoretically an "unknown" OS should mean no standard libraries, however
167
    // it could also mean that a custom set of libraries is in use, so just add
168
    // /lib to the search path. Disable multiarch in this case, to discourage
169
    // paths containing "unknown" from acquiring meanings.
170
61
    getFilePaths().push_back(SysRoot + "/lib");
171
8
  } else {
172
8
    const std::string MultiarchTriple =
173
8
        getMultiarchTriple(getDriver(), Triple, SysRoot);
174
8
    if (D.isUsingLTO()) {
175
      // For LTO, enable use of lto-enabled sysroot libraries too, if available.
176
      // Note that the directory is keyed to the LLVM revision, as LLVM's
177
      // bitcode format is not stable.
178
1
      auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
179
1
      getFilePaths().push_back(Dir);
180
1
    }
181
8
    getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
182
8
  }
183
69
}
184
185
72
bool WebAssembly::IsMathErrnoDefault() const { return false; }
186
187
70
bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
188
189
0
bool WebAssembly::UseObjCMixedDispatch() const { return true; }
190
191
140
bool WebAssembly::isPICDefault() const { return false; }
192
193
140
bool WebAssembly::isPIEDefault() const { return false; }
194
195
140
bool WebAssembly::isPICDefaultForced() const { return false; }
196
197
352
bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
198
199
0
bool WebAssembly::hasBlocksRuntime() const { return false; }
200
201
// TODO: Support profiling.
202
70
bool WebAssembly::SupportsProfiling() const { return false; }
203
204
30
bool WebAssembly::HasNativeLLVMSupport() const { return true; }
205
206
void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
207
                                        ArgStringList &CC1Args,
208
72
                                        Action::OffloadKind) const {
209
72
  if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
210
72
                          options::OPT_fno_use_init_array, true))
211
0
    CC1Args.push_back("-fno-use-init-array");
212
72
213
  // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
214
72
  if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
215
11
                         false)) {
216
11
    if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
217
11
                           false))
218
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
219
1
          << "-pthread"
220
1
          << "-mno-atomics";
221
11
    if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
222
11
                           options::OPT_mbulk_memory, false))
223
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
224
1
          << "-pthread"
225
1
          << "-mno-bulk-memory";
226
11
    if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
227
11
                           options::OPT_mmutable_globals, false))
228
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
229
1
          << "-pthread"
230
1
          << "-mno-mutable-globals";
231
11
    if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
232
11
                           false))
233
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
234
1
          << "-pthread"
235
1
          << "-mno-sign-ext";
236
11
    CC1Args.push_back("-target-feature");
237
11
    CC1Args.push_back("+atomics");
238
11
    CC1Args.push_back("-target-feature");
239
11
    CC1Args.push_back("+bulk-memory");
240
11
    CC1Args.push_back("-target-feature");
241
11
    CC1Args.push_back("+mutable-globals");
242
11
    CC1Args.push_back("-target-feature");
243
11
    CC1Args.push_back("+sign-ext");
244
11
  }
245
72
246
72
  if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
247
70
                          options::OPT_mno_mutable_globals, false)) {
248
    // -fPIC implies +mutable-globals because the PIC ABI used by the linker
249
    // depends on importing and exporting mutable globals.
250
70
    llvm::Reloc::Model RelocationModel;
251
70
    unsigned PICLevel;
252
70
    bool IsPIE;
253
70
    std::tie(RelocationModel, PICLevel, IsPIE) =
254
70
        ParsePICArgs(*this, DriverArgs);
255
70
    if (RelocationModel == llvm::Reloc::PIC_) {
256
3
      if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
257
2
                             options::OPT_mmutable_globals, false)) {
258
2
        getDriver().Diag(diag::err_drv_argument_not_allowed_with)
259
2
            << "-fPIC"
260
2
            << "-mno-mutable-globals";
261
2
      }
262
3
      CC1Args.push_back("-target-feature");
263
3
      CC1Args.push_back("+mutable-globals");
264
3
    }
265
70
  }
266
72
267
72
  if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
268
    // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
269
4
    if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
270
4
                           options::OPT_mexception_handing, false))
271
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
272
1
          << "-fwasm-exceptions"
273
1
          << "-mno-exception-handling";
274
    // '-fwasm-exceptions' is not compatible with '-mno-reference-types'
275
4
    if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
276
4
                           options::OPT_mexception_handing, false))
277
1
      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
278
1
          << "-fwasm-exceptions"
279
1
          << "-mno-reference-types";
280
    // '-fwasm-exceptions' is not compatible with
281
    // '-mllvm -enable-emscripten-cxx-exceptions'
282
1
    for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
283
1
      if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
284
1
        getDriver().Diag(diag::err_drv_argument_not_allowed_with)
285
1
            << "-fwasm-exceptions"
286
1
            << "-mllvm -enable-emscripten-cxx-exceptions";
287
1
    }
288
    // '-fwasm-exceptions' implies exception-handling and reference-types
289
4
    CC1Args.push_back("-target-feature");
290
4
    CC1Args.push_back("+exception-handling");
291
4
    CC1Args.push_back("-target-feature");
292
4
    CC1Args.push_back("+reference-types");
293
4
  }
294
72
}
295
296
58
ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
297
58
  return ToolChain::RLT_CompilerRT;
298
58
}
299
300
ToolChain::CXXStdlibType
301
7
WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
302
7
  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
303
5
    StringRef Value = A->getValue();
304
5
    if (Value != "libc++")
305
5
      getDriver().Diag(diag::err_drv_invalid_stdlib_name)
306
5
          << A->getAsString(Args);
307
5
  }
308
7
  return ToolChain::CST_Libcxx;
309
7
}
310
311
void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
312
70
                                            ArgStringList &CC1Args) const {
313
70
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
314
0
    return;
315
70
316
70
  const Driver &D = getDriver();
317
70
318
70
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
319
70
    SmallString<128> P(D.ResourceDir);
320
70
    llvm::sys::path::append(P, "include");
321
70
    addSystemInclude(DriverArgs, CC1Args, P);
322
70
  }
323
70
324
70
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
325
0
    return;
326
70
327
  // Check for configure-time C include directories.
328
70
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
329
70
  if (CIncludeDirs != "") {
330
0
    SmallVector<StringRef, 5> dirs;
331
0
    CIncludeDirs.split(dirs, ":");
332
0
    for (StringRef dir : dirs) {
333
0
      StringRef Prefix =
334
0
          llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
335
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
336
0
    }
337
0
    return;
338
0
  }
339
70
340
70
  if (getTriple().getOS() != llvm::Triple::UnknownOS) {
341
8
    const std::string MultiarchTriple =
342
8
        getMultiarchTriple(D, getTriple(), D.SysRoot);
343
8
    addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
344
8
  }
345
70
  addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
346
70
}
347
348
void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
349
13
                                               ArgStringList &CC1Args) const {
350
13
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
351
13
      !DriverArgs.hasArg(options::OPT_nostdincxx)) {
352
13
    if (getTriple().getOS() != llvm::Triple::UnknownOS) {
353
3
      const std::string MultiarchTriple =
354
3
          getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
355
3
      addSystemInclude(DriverArgs, CC1Args,
356
3
                       getDriver().SysRoot + "/include/" + MultiarchTriple +
357
3
                           "/c++/v1");
358
3
    }
359
13
    addSystemInclude(DriverArgs, CC1Args,
360
13
                     getDriver().SysRoot + "/include/c++/v1");
361
13
  }
362
13
}
363
364
void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
365
7
                                      llvm::opt::ArgStringList &CmdArgs) const {
366
7
367
7
  switch (GetCXXStdlibType(Args)) {
368
7
  case ToolChain::CST_Libcxx:
369
7
    CmdArgs.push_back("-lc++");
370
7
    CmdArgs.push_back("-lc++abi");
371
7
    break;
372
0
  case ToolChain::CST_Libstdcxx:
373
0
    llvm_unreachable("invalid stdlib name");
374
7
  }
375
7
}
376
377
69
SanitizerMask WebAssembly::getSupportedSanitizers() const {
378
69
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
379
69
  if (getTriple().isOSEmscripten()) {
380
1
    Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
381
1
  }
382
69
  return Res;
383
69
}
384
385
29
Tool *WebAssembly::buildLinker() const {
386
29
  return new tools::wasm::Linker(*this);
387
29
}