clang 24.0.0git
WebAssembly.cpp
Go to the documentation of this file.
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 "Gnu.h"
11#include "clang/Config/config.h"
14#include "clang/Driver/Driver.h"
16#include "llvm/Config/llvm-config.h" // for LLVM_VERSION_STRING
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
22using namespace clang::driver;
23using namespace clang::driver::tools;
24using namespace clang::driver::toolchains;
25using namespace clang;
26using namespace llvm::opt;
27
28/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
29/// we remove the vendor field to form the multiarch triple.
30std::string WebAssembly::getMultiarchTriple(const Driver &D,
31 const llvm::Triple &TargetTriple,
32 StringRef SysRoot) const {
33 return (TargetTriple.getArchName() + "-" +
34 TargetTriple.getOSAndEnvironmentName()).str();
35}
36
37/// Returns a directory name in which separate objects compile with/without
38/// exceptions may lie. This is used both for `#include` paths as well as lib
39/// paths.
40static std::string GetCXXExceptionsDir(const ArgList &DriverArgs) {
41 if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions))
42 return "eh";
43 return "noeh";
44}
45
46std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
48 if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
49 StringRef UseLinker = A->getValue();
50 if (!UseLinker.empty()) {
51 if (llvm::sys::path::is_absolute(UseLinker) &&
52 llvm::sys::fs::can_execute(UseLinker))
53 return std::string(UseLinker);
54
55 // Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
56 // linker. Note that for `wasm32-wasip2` this overrides the default linker
57 // of `wasm-component-ld`.
58 if (UseLinker == "lld") {
59 return ToolChain.GetProgramPath("wasm-ld");
60 }
61
62 // Allow 'ld' as an alias for the default linker
63 if (UseLinker != "ld")
64 ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
65 << A->getAsString(Args);
66 }
67 }
68
70}
71
72static bool TargetBuildsComponents(const llvm::Triple &TargetTriple) {
73 // WASIp2 and above are all based on components, so test for WASI but exclude
74 // the original `wasi` target in addition to the `wasip1` name.
75 return TargetTriple.isOSWASI() && TargetTriple.getOSName() != "wasip1" &&
76 TargetTriple.getOSName() != "wasi";
77}
78
79static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args) {
80 bool WantsPthread = Args.hasArg(options::OPT_pthread);
81
82 // If the WASI environment is "threads" then enable pthreads support
83 // without requiring -pthread, in order to prevent user error
84 if (Triple.isOSWASI() && Triple.getEnvironmentName() == "threads")
85 WantsPthread = true;
86
87 // WASIp3 also implies pthreads support
88 if (Triple.getOS() == llvm::Triple::WASIp3)
89 WantsPthread = true;
90
91 return WantsPthread;
92}
93
94static bool WantsCooperativeMultithreading(const llvm::Triple &Triple,
95 const ArgList &Args) {
96 return Triple.getOS() == llvm::Triple::WASIp3;
97}
98
99static bool WantsSharedMemory(const llvm::Triple &Triple, const ArgList &Args) {
100 return WantsPthread(Triple, Args) &&
101 !WantsCooperativeMultithreading(Triple, Args);
102}
103
105 const InputInfo &Output,
106 const InputInfoList &Inputs,
107 const ArgList &Args,
108 const char *LinkingOutput) const {
109
111 const char *Linker = Args.MakeArgString(getLinkerPath(Args));
112 ArgStringList CmdArgs;
113
114 CmdArgs.push_back("-m");
115 if (ToolChain.getTriple().isArch64Bit())
116 CmdArgs.push_back("wasm64");
117 else
118 CmdArgs.push_back("wasm32");
119
120 if (Args.hasArg(options::OPT_s))
121 CmdArgs.push_back("--strip-all");
122
123 // On `wasip2` the default linker is `wasm-component-ld` which wraps the
124 // execution of `wasm-ld`. Find `wasm-ld` and pass it as an argument of where
125 // to find it to avoid it needing to hunt and rediscover or search `PATH` for
126 // where it is.
127 if (llvm::sys::path::stem(Linker).ends_with_insensitive(
128 "wasm-component-ld")) {
129 CmdArgs.push_back("--wasm-ld-path");
130 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetProgramPath("wasm-ld")));
131 }
132
133 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
134
135 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
136
137 bool IsCommand = true;
138 const char *Crt1;
139 const char *Entry = nullptr;
140
141 // When -shared is specified, use the reactor exec model unless
142 // specified otherwise.
143 if (Args.hasArg(options::OPT_shared))
144 IsCommand = false;
145
146 if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
147 StringRef CM = A->getValue();
148 if (CM == "command") {
149 IsCommand = true;
150 } else if (CM == "reactor") {
151 IsCommand = false;
152 } else {
153 ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
154 << CM << A->getOption().getName();
155 }
156 }
157
158 if (IsCommand) {
159 // If crt1-command.o exists, it supports new-style commands, so use it.
160 // Otherwise, use the old crt1.o. This is a temporary transition measure.
161 // Once WASI libc no longer needs to support LLVM versions which lack
162 // support for new-style command, it can make crt1.o the same as
163 // crt1-command.o. And once LLVM no longer needs to support WASI libc
164 // versions before that, it can switch to using crt1-command.o.
165 Crt1 = "crt1.o";
166 if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
167 Crt1 = "crt1-command.o";
168 } else {
169 Crt1 = "crt1-reactor.o";
170 Entry = "_initialize";
171 }
172
173 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
174 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
175 if (Entry) {
176 CmdArgs.push_back(Args.MakeArgString("--entry"));
177 CmdArgs.push_back(Args.MakeArgString(Entry));
178 }
179
180 if (Args.hasArg(options::OPT_shared))
181 CmdArgs.push_back(Args.MakeArgString("-shared"));
182
183 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
184
186 CmdArgs.push_back("--cooperative-threading");
187
189 CmdArgs.push_back("--shared-memory");
190
191 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
193 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
194
195 if (WantsPthread(ToolChain.getTriple(), Args))
196 CmdArgs.push_back("-lpthread");
197
198 CmdArgs.push_back("-lc");
199 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
200 }
201
202 ToolChain.addProfileRTLibs(Args, CmdArgs);
203
204 CmdArgs.push_back("-o");
205 CmdArgs.push_back(Output.getFilename());
206
207 // Don't use wasm-opt by default on `wasip2` as it doesn't have support for
208 // components at this time. Retain the historical default otherwise, though,
209 // of running `wasm-opt` by default.
210 bool WasmOptDefault = !TargetBuildsComponents(ToolChain.getTriple());
211 bool RunWasmOpt = Args.hasFlag(options::OPT_wasm_opt,
212 options::OPT_no_wasm_opt, WasmOptDefault);
213
214 // If wasm-opt is enabled and optimizations are happening look for the
215 // `wasm-opt` program. If it's not found auto-disable it.
216 std::string WasmOptPath;
217 if (RunWasmOpt && Args.getLastArg(options::OPT_O_Group)) {
218 WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
219 if (WasmOptPath == "wasm-opt") {
220 WasmOptPath = {};
221 }
222 }
223
224 if (!WasmOptPath.empty()) {
225 CmdArgs.push_back("--keep-section=target_features");
226 }
227
228 C.addCommand(std::make_unique<Command>(JA, *this,
230 Linker, CmdArgs, Inputs, Output));
231
232 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
233 if (!WasmOptPath.empty()) {
234 StringRef OOpt = "s";
235 if (A->getOption().matches(options::OPT_O4) ||
236 A->getOption().matches(options::OPT_Ofast))
237 OOpt = "4";
238 else if (A->getOption().matches(options::OPT_O0))
239 OOpt = "0";
240 else if (A->getOption().matches(options::OPT_O))
241 OOpt = A->getValue();
242
243 if (OOpt != "0") {
244 const char *WasmOpt = Args.MakeArgString(WasmOptPath);
245 ArgStringList OptArgs;
246 OptArgs.push_back(Output.getFilename());
247 OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
248 OptArgs.push_back("-o");
249 OptArgs.push_back(Output.getFilename());
250 C.addCommand(std::make_unique<Command>(
251 JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
252 Inputs, Output));
253 }
254 }
255 }
256}
257
258/// Append `Dir` to `Paths`, but also include the LTO directories before that if
259/// LTO is enabled.
261 const ToolChain &TC,
262 const llvm::opt::ArgList &Args,
263 const std::string &Dir) {
264 if (TC.isUsingLTO(Args)) {
265 // The version allows the path to be keyed to the specific version of
266 // LLVM in used, as the bitcode format is not stable.
267 Paths.push_back(Dir + "/llvm-lto/" LLVM_VERSION_STRING);
268 }
269 Paths.push_back(Dir);
270}
271
272WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
273 const llvm::opt::ArgList &Args)
274 : ToolChain(D, Triple, Args) {
275
276 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
277
278 getProgramPaths().push_back(getDriver().Dir);
279
280 auto SysRoot = getDriver().SysRoot;
281 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
282 // Theoretically an "unknown" OS should mean no standard libraries, however
283 // it could also mean that a custom set of libraries is in use, so just add
284 // /lib to the search path. Disable multiarch in this case, to discourage
285 // paths containing "unknown" from acquiring meanings.
286 getFilePaths().push_back(SysRoot + "/lib");
287 } else {
288 const std::string MultiarchTriple =
289 getMultiarchTriple(getDriver(), Triple, SysRoot);
290 std::string TripleLibDir = SysRoot + "/lib/" + MultiarchTriple;
291 // Allow sysroots to segregate objects based on whether exceptions are
292 // enabled or not. This is intended to assist with distribution of pre-built
293 // sysroots that contain libraries that are capable of producing binaries
294 // entirely without exception-handling instructions but also with if
295 // exceptions are enabled, for example.
296 AppendLibDirAndLTODir(getFilePaths(), *this, Args,
297 TripleLibDir + "/" + GetCXXExceptionsDir(Args));
298 AppendLibDirAndLTODir(getFilePaths(), *this, Args, TripleLibDir);
299 }
300
301 if (getTriple().getOS() == llvm::Triple::WASI) {
302 D.Diag(diag::warn_drv_deprecated_custom)
303 << "--target=wasm32-wasi"
304 << "use --target=wasm32-wasip1 instead";
305 }
306}
307
308const char *WebAssembly::getDefaultLinker() const {
310 return "wasm-component-ld";
311 return "wasm-ld";
312}
313
314bool WebAssembly::IsMathErrnoDefault() const { return false; }
315
316bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
317
318bool WebAssembly::UseObjCMixedDispatch() const { return true; }
319
320bool WebAssembly::isPICDefault() const { return false; }
321
322bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
323 return false;
324}
325
326bool WebAssembly::isPICDefaultForced() const { return false; }
327
328bool WebAssembly::hasBlocksRuntime() const { return false; }
329
330// TODO: Support profiling.
331bool WebAssembly::SupportsProfiling() const { return false; }
332
333bool WebAssembly::HasNativeLLVMSupport() const { return true; }
334
335void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
336 ArgStringList &CC1Args, BoundArch BA,
337 Action::OffloadKind) const {
338 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
339 options::OPT_fno_use_init_array, true))
340 CC1Args.push_back("-fno-use-init-array");
341
342 // '-pthread' implies bulk-memory, mutable-globals, and sign-ext.
343 // It also implies atomics, so long as we're not targeting a cooperative
344 // threading environment.
345 if (WantsPthread(getTriple(), DriverArgs)) {
346 if (!WantsCooperativeMultithreading(getTriple(), DriverArgs) &&
347 DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
348 false))
349 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
350 << "-pthread"
351 << "-mno-atomics";
352 if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
353 options::OPT_mbulk_memory, false))
354 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
355 << "-pthread"
356 << "-mno-bulk-memory";
357 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
358 options::OPT_mmutable_globals, false))
359 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
360 << "-pthread"
361 << "-mno-mutable-globals";
362 if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
363 false))
364 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
365 << "-pthread"
366 << "-mno-sign-ext";
367 if (!WantsCooperativeMultithreading(getTriple(), DriverArgs)) {
368 CC1Args.push_back("-target-feature");
369 CC1Args.push_back("+atomics");
370 }
371 CC1Args.push_back("-target-feature");
372 CC1Args.push_back("+bulk-memory");
373 CC1Args.push_back("-target-feature");
374 CC1Args.push_back("+mutable-globals");
375 CC1Args.push_back("-target-feature");
376 CC1Args.push_back("+sign-ext");
377 }
378
379 if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
380 options::OPT_mno_mutable_globals, false)) {
381 // -fPIC implies +mutable-globals because the PIC ABI used by the linker
382 // depends on importing and exporting mutable globals.
383 llvm::Reloc::Model RelocationModel;
384 unsigned PICLevel;
385 bool IsPIE;
386 std::tie(RelocationModel, PICLevel, IsPIE) =
387 ParsePICArgs(*this, DriverArgs);
388 if (RelocationModel == llvm::Reloc::PIC_) {
389 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
390 options::OPT_mmutable_globals, false)) {
391 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
392 << "-fPIC"
393 << "-mno-mutable-globals";
394 }
395 CC1Args.push_back("-target-feature");
396 CC1Args.push_back("+mutable-globals");
397 }
398 }
399
400 bool HasBannedIncompatibleOptionsForWasmEHSjLj = false;
401 bool HasEnabledFeaturesForWasmEHSjLj = false;
402
403 // Bans incompatible options for Wasm EH / SjLj. We don't allow using
404 // different modes for EH and SjLj.
405 auto BanIncompatibleOptionsForWasmEHSjLj = [&](StringRef CurOption) {
406 if (HasBannedIncompatibleOptionsForWasmEHSjLj)
407 return;
408 HasBannedIncompatibleOptionsForWasmEHSjLj = true;
409 if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
410 options::OPT_mexception_handing, false))
411 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
412 << CurOption << "-mno-exception-handling";
413 // The standardized Wasm EH spec requires multivalue and reference-types.
414 if (DriverArgs.hasFlag(options::OPT_mno_multivalue,
415 options::OPT_mmultivalue, false))
416 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
417 << CurOption << "-mno-multivalue";
418 if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
419 options::OPT_mreference_types, false))
420 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
421 << CurOption << "-mno-reference-types";
422
423 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
424 for (const auto *Option :
425 {"-enable-emscripten-cxx-exceptions", "-enable-emscripten-sjlj",
426 "-emscripten-cxx-exceptions-allowed"}) {
427 if (StringRef(A->getValue(0)) == Option)
428 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
429 << CurOption << Option;
430 }
431 }
432 };
433
434 // Enable necessary features for Wasm EH / SjLj in the backend.
435 auto EnableFeaturesForWasmEHSjLj = [&]() {
436 if (HasEnabledFeaturesForWasmEHSjLj)
437 return;
438 HasEnabledFeaturesForWasmEHSjLj = true;
439 CC1Args.push_back("-target-feature");
440 CC1Args.push_back("+exception-handling");
441 // The standardized Wasm EH spec requires multivalue and reference-types.
442 CC1Args.push_back("-target-feature");
443 CC1Args.push_back("+multivalue");
444 CC1Args.push_back("-target-feature");
445 CC1Args.push_back("+reference-types");
446 // Backend needs '-exception-model=wasm' to use Wasm EH instructions
447 CC1Args.push_back("-exception-model=wasm");
448 };
449
450 if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
451 BanIncompatibleOptionsForWasmEHSjLj("-fwasm-exceptions");
452 EnableFeaturesForWasmEHSjLj();
453 // Backend needs -wasm-enable-eh to enable Wasm EH
454 CC1Args.push_back("-mllvm");
455 CC1Args.push_back("-wasm-enable-eh");
456 }
457
458 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
459 StringRef Opt = A->getValue(0);
460 if (Opt.starts_with("-emscripten-cxx-exceptions-allowed")) {
461 // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
462 // '-mllvm -enable-emscripten-cxx-exceptions'
463 bool EmEHArgExists = false;
464 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
465 if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
466 EmEHArgExists = true;
467 break;
468 }
469 }
470 if (!EmEHArgExists)
471 getDriver().Diag(diag::err_drv_argument_only_allowed_with)
472 << "-mllvm -emscripten-cxx-exceptions-allowed"
473 << "-mllvm -enable-emscripten-cxx-exceptions";
474
475 // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
476 // from being inlined before reaching the wasm backend.
477 StringRef FuncNamesStr = Opt.split('=').second;
478 SmallVector<StringRef, 4> FuncNames;
479 FuncNamesStr.split(FuncNames, ',');
480 for (auto Name : FuncNames) {
481 CC1Args.push_back("-mllvm");
482 CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
483 ":noinline"));
484 }
485 }
486
487 for (const auto *Option :
488 {"-wasm-enable-eh", "-wasm-enable-sjlj", "-wasm-use-legacy-eh"}) {
489 if (Opt.starts_with(Option)) {
490 BanIncompatibleOptionsForWasmEHSjLj(Option);
491 EnableFeaturesForWasmEHSjLj();
492 }
493 }
494 }
495}
496
499}
500
502WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
503 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
504 StringRef Value = A->getValue();
505 if (Value == "libc++")
507 else if (Value == "libstdc++")
509 else
510 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
511 << A->getAsString(Args);
512 }
514}
515
516void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
517 ArgStringList &CC1Args) const {
518 if (DriverArgs.hasArg(options::OPT_nostdinc))
519 return;
520
521 const Driver &D = getDriver();
522
523 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
524 SmallString<128> P(D.ResourceDir);
525 llvm::sys::path::append(P, "include");
526 addSystemInclude(DriverArgs, CC1Args, P);
527 }
528
529 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
530 return;
531
532 // Check for configure-time C include directories.
533 StringRef CIncludeDirs(C_INCLUDE_DIRS);
534 if (CIncludeDirs != "") {
535 SmallVector<StringRef, 5> dirs;
536 CIncludeDirs.split(dirs, ":");
537 for (StringRef dir : dirs) {
538 StringRef Prefix =
539 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
540 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
541 }
542 return;
543 }
544
545 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
546 const std::string MultiarchTriple =
547 getMultiarchTriple(D, getTriple(), D.SysRoot);
548 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
549 }
550 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
551}
552
553void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
554 ArgStringList &CC1Args) const {
555
556 if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
557 options::OPT_nostdincxx))
558 return;
559
560 switch (GetCXXStdlibType(DriverArgs)) {
562 addLibCxxIncludePaths(DriverArgs, CC1Args);
563 break;
565 addLibStdCXXIncludePaths(DriverArgs, CC1Args);
566 break;
567 }
568}
569
570void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
571 llvm::opt::ArgStringList &CmdArgs) const {
572
573 switch (GetCXXStdlibType(Args)) {
575 CmdArgs.push_back("-lc++");
576 if (Args.hasArg(options::OPT_fexperimental_library))
577 CmdArgs.push_back("-lc++experimental");
578 CmdArgs.push_back("-lc++abi");
579 break;
581 CmdArgs.push_back("-lstdc++");
582 break;
583 }
584}
585
586SanitizerMask WebAssembly::getSupportedSanitizers(
587 BoundArch BA, Action::OffloadKind DeviceOffloadKind) const {
588 SanitizerMask Res = ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind);
589 if (getTriple().isOSEmscripten()) {
590 Res |= SanitizerKind::Vptr | SanitizerKind::Leak;
591 }
592
593 if (getTriple().isOSEmscripten() || getTriple().isOSWASI()) {
594 Res |= SanitizerKind::Address;
595 }
596
597 // -fsanitize=function places two words before the function label, which are
598 // -unsupported.
599 Res &= ~SanitizerKind::Function;
600 return Res;
601}
602
603Tool *WebAssembly::buildLinker() const {
604 return new tools::wasm::Linker(*this);
605}
606
607void WebAssembly::addLibCxxIncludePaths(
608 const llvm::opt::ArgList &DriverArgs,
609 llvm::opt::ArgStringList &CC1Args) const {
610 const Driver &D = getDriver();
611 std::string SysRoot = computeSysRoot();
612 std::string LibPath = SysRoot + "/include";
613 const std::string MultiarchTriple =
614 getMultiarchTriple(D, getTriple(), SysRoot);
615 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
616
617 std::string Version = detectLibcxxVersion(LibPath);
618 if (Version.empty())
619 return;
620
621 // First add the per-target-per-exception-handling include path if the
622 // OS is known, then second add the per-target include path.
623 if (IsKnownOs) {
624 std::string TargetDir = LibPath + "/" + MultiarchTriple;
625 std::string Suffix = "/c++/" + Version;
626 addSystemInclude(DriverArgs, CC1Args,
627 TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) +
628 Suffix);
629 addSystemInclude(DriverArgs, CC1Args, TargetDir + Suffix);
630 }
631
632 // Third add the generic one.
633 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
634}
635
636void WebAssembly::addLibStdCXXIncludePaths(
637 const llvm::opt::ArgList &DriverArgs,
638 llvm::opt::ArgStringList &CC1Args) const {
639 // We cannot use GCCInstallationDetector here as the sysroot usually does
640 // not contain a full GCC installation.
641 // Instead, we search the given sysroot for /usr/include/xx, similar
642 // to how we do it for libc++.
643 const Driver &D = getDriver();
644 std::string SysRoot = computeSysRoot();
645 std::string LibPath = SysRoot + "/include";
646 const std::string MultiarchTriple =
647 getMultiarchTriple(D, getTriple(), SysRoot);
648 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
649
650 // This is similar to detectLibcxxVersion()
651 std::string Version;
652 {
653 std::error_code EC;
654 Generic_GCC::GCCVersion MaxVersion =
656 SmallString<128> Path(LibPath);
657 llvm::sys::path::append(Path, "c++");
658 for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
659 !EC && LI != LE; LI = LI.increment(EC)) {
660 StringRef VersionText = llvm::sys::path::filename(LI->path());
661 if (VersionText[0] != 'v') {
662 auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
663 if (Version > MaxVersion)
664 MaxVersion = Version;
665 }
666 }
667 if (MaxVersion.Major > 0)
668 Version = MaxVersion.Text;
669 }
670
671 if (Version.empty())
672 return;
673
674 // First add the per-target include path if the OS is known.
675 if (IsKnownOs) {
676 std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
677 addSystemInclude(DriverArgs, CC1Args, TargetDir);
678 }
679
680 // Second add the generic one.
681 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
682 // Third the backward one.
683 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
684}
static std::string GetCXXExceptionsDir(const ArgList &DriverArgs)
Returns a directory name in which separate objects compile with/without exceptions may lie.
static void AppendLibDirAndLTODir(ToolChain::path_list &Paths, const ToolChain &TC, const llvm::opt::ArgList &Args, const std::string &Dir)
Append Dir to Paths, but also include the LTO directories before that if LTO is enabled.
static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args)
static bool WantsCooperativeMultithreading(const llvm::Triple &Triple, const ArgList &Args)
static bool TargetBuildsComponents(const llvm::Triple &TargetTriple)
static bool WantsSharedMemory(const llvm::Triple &Triple, const ArgList &Args)
static StringRef getTriple(const Command &Job)
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:46
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:95
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:159
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getFilename() const
Definition InputInfo.h:83
ToolChain - Access to tools for a single platform.
Definition ToolChain.h:96
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
bool isUsingLTO(const llvm::opt::ArgList &Args, Action::OffloadKind Kind=Action::OFK_None) const
Returns true if LTO is active for this toolchain given the args.
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
std::string GetFilePath(const char *Name) const
StringRef getOS() const
Definition ToolChain.h:305
virtual bool IsObjCNonFragileABIDefault() const
IsObjCNonFragileABIDefault - Does this tool chain set -fobjc-nonfragile-abi by default.
Definition ToolChain.h:521
virtual SanitizerMask getSupportedSanitizers(BoundArch BA, Action::OffloadKind DeviceOffloadKind) const
Return sanitizers which are available in this toolchain.
const Driver & getDriver() const
Definition ToolChain.h:286
virtual std::string detectLibcxxVersion(StringRef IncludePath) const
llvm::vfs::FileSystem & getVFS() const
virtual bool isPICDefaultForced() const =0
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
virtual bool IsMathErrnoDefault() const
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition ToolChain.h:513
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition ToolChain.h:547
virtual Tool * buildLinker() const
const llvm::Triple & getTriple() const
Definition ToolChain.h:288
virtual bool HasNativeLLVMSupport() const
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
std::string GetProgramPath(const char *Name) const
virtual bool hasBlocksRuntime() const
hasBlocksRuntime - Given that the user is compiling with -fblocks, does this tool chain guarantee the...
Definition ToolChain.h:742
virtual bool SupportsProfiling() const
SupportsProfiling - Does this tool chain support -pg.
Definition ToolChain.h:643
virtual bool UseObjCMixedDispatch() const
UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the mixed dispatch method be use...
Definition ToolChain.h:525
virtual void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition ToolChain.h:550
virtual bool isPICDefault() const =0
Test whether this toolchain defaults to PIC.
SmallVector< std::string, 16 > path_list
Definition ToolChain.h:98
const ToolChain & getToolChain() const
Definition Tool.h:52
WebAssembly(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
std::string getLinkerPath(const llvm::opt::ArgList &Args) const
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
std::tuple< llvm::Reloc::Model, unsigned, bool > ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:51
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1537
Top level wrappers for InstallAPI frontend operations.
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
Definition Wasm.cpp:36
static constexpr ResponseFileSupport AtFileCurCP()
Definition Job.h:93
int Major
The parsed major, minor, and patch numbers.
Definition Gnu.h:168
std::string Text
The unparsed text of the version.
Definition Gnu.h:165
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition Gnu.cpp:1992