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 std::string WebAssembly::getMultiarchTriple(const Driver &D,
30                                             const llvm::Triple &TargetTriple,
31                                             StringRef SysRoot) const {
32     return (TargetTriple.getArchName() + "-" +
33             TargetTriple.getOSAndEnvironmentName()).str();
34 }
35 
36 std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
37   const ToolChain &ToolChain = getToolChain();
38   if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
39     StringRef UseLinker = A->getValue();
40     if (!UseLinker.empty()) {
41       if (llvm::sys::path::is_absolute(UseLinker) &&
42           llvm::sys::fs::can_execute(UseLinker))
43         return std::string(UseLinker);
44 
45       // Accept 'lld', and 'ld' as aliases for the default linker
46       if (UseLinker != "lld" && UseLinker != "ld")
47         ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
48             << A->getAsString(Args);
49     }
50   }
51 
52   return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
53 }
54 
55 void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
56                                 const InputInfo &Output,
57                                 const InputInfoList &Inputs,
58                                 const ArgList &Args,
59                                 const char *LinkingOutput) const {
60 
61   const ToolChain &ToolChain = getToolChain();
62   const char *Linker = Args.MakeArgString(getLinkerPath(Args));
63   ArgStringList CmdArgs;
64 
65   CmdArgs.push_back("-m");
66   if (ToolChain.getTriple().isArch64Bit())
67     CmdArgs.push_back("wasm64");
68   else
69     CmdArgs.push_back("wasm32");
70 
71   if (Args.hasArg(options::OPT_s))
72     CmdArgs.push_back("--strip-all");
73 
74   Args.AddAllArgs(CmdArgs, options::OPT_L);
75   Args.AddAllArgs(CmdArgs, options::OPT_u);
76   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
77 
78   const char *Crt1 = "crt1.o";
79   const char *Entry = NULL;
80 
81   // If crt1-command.o exists, it supports new-style commands, so use it.
82   // Otherwise, use the old crt1.o. This is a temporary transition measure.
83   // Once WASI libc no longer needs to support LLVM versions which lack
84   // support for new-style command, it can make crt1.o the same as
85   // crt1-command.o. And once LLVM no longer needs to support WASI libc
86   // versions before that, it can switch to using crt1-command.o.
87   if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
88     Crt1 = "crt1-command.o";
89 
90   if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
91     StringRef CM = A->getValue();
92     if (CM == "command") {
93       // Use default values.
94     } else if (CM == "reactor") {
95       Crt1 = "crt1-reactor.o";
96       Entry = "_initialize";
97     } else {
98       ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
99           << CM << A->getOption().getName();
100     }
101   }
102   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
103     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
104   if (Entry) {
105     CmdArgs.push_back(Args.MakeArgString("--entry"));
106     CmdArgs.push_back(Args.MakeArgString(Entry));
107   }
108 
109   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
110 
111   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
112     if (ToolChain.ShouldLinkCXXStdlib(Args))
113       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
114 
115     if (Args.hasArg(options::OPT_pthread)) {
116       CmdArgs.push_back("-lpthread");
117       CmdArgs.push_back("--shared-memory");
118     }
119 
120     CmdArgs.push_back("-lc");
121     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
122   }
123 
124   CmdArgs.push_back("-o");
125   CmdArgs.push_back(Output.getFilename());
126 
127   C.addCommand(std::make_unique<Command>(JA, *this,
128                                          ResponseFileSupport::AtFileCurCP(),
129                                          Linker, CmdArgs, Inputs, Output));
130 
131   // When optimizing, if wasm-opt is available, run it.
132   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
133     auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
134     if (WasmOptPath != "wasm-opt") {
135       StringRef OOpt = "s";
136       if (A->getOption().matches(options::OPT_O4) ||
137           A->getOption().matches(options::OPT_Ofast))
138         OOpt = "4";
139       else if (A->getOption().matches(options::OPT_O0))
140         OOpt = "0";
141       else if (A->getOption().matches(options::OPT_O))
142         OOpt = A->getValue();
143 
144       if (OOpt != "0") {
145         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
146         ArgStringList CmdArgs;
147         CmdArgs.push_back(Output.getFilename());
148         CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
149         CmdArgs.push_back("-o");
150         CmdArgs.push_back(Output.getFilename());
151         C.addCommand(std::make_unique<Command>(
152             JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
153             Inputs, Output));
154       }
155     }
156   }
157 }
158 
159 /// Given a base library directory, append path components to form the
160 /// LTO directory.
161 static std::string AppendLTOLibDir(const std::string &Dir) {
162     // The version allows the path to be keyed to the specific version of
163     // LLVM in used, as the bitcode format is not stable.
164     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
165 }
166 
167 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
168                          const llvm::opt::ArgList &Args)
169     : ToolChain(D, Triple, Args) {
170 
171   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
172 
173   getProgramPaths().push_back(getDriver().getInstalledDir());
174 
175   auto SysRoot = getDriver().SysRoot;
176   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
177     // Theoretically an "unknown" OS should mean no standard libraries, however
178     // it could also mean that a custom set of libraries is in use, so just add
179     // /lib to the search path. Disable multiarch in this case, to discourage
180     // paths containing "unknown" from acquiring meanings.
181     getFilePaths().push_back(SysRoot + "/lib");
182   } else {
183     const std::string MultiarchTriple =
184         getMultiarchTriple(getDriver(), Triple, SysRoot);
185     if (D.isUsingLTO()) {
186       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
187       // Note that the directory is keyed to the LLVM revision, as LLVM's
188       // bitcode format is not stable.
189       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
190       getFilePaths().push_back(Dir);
191     }
192     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
193   }
194 }
195 
196 bool WebAssembly::IsMathErrnoDefault() const { return false; }
197 
198 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
199 
200 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
201 
202 bool WebAssembly::isPICDefault() const { return false; }
203 
204 bool WebAssembly::isPIEDefault() const { return false; }
205 
206 bool WebAssembly::isPICDefaultForced() const { return false; }
207 
208 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
209 
210 bool WebAssembly::hasBlocksRuntime() const { return false; }
211 
212 // TODO: Support profiling.
213 bool WebAssembly::SupportsProfiling() const { return false; }
214 
215 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
216 
217 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
218                                         ArgStringList &CC1Args,
219                                         Action::OffloadKind) const {
220   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
221                           options::OPT_fno_use_init_array, true))
222     CC1Args.push_back("-fno-use-init-array");
223 
224   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
225   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
226                          false)) {
227     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
228                            false))
229       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
230           << "-pthread"
231           << "-mno-atomics";
232     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
233                            options::OPT_mbulk_memory, false))
234       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
235           << "-pthread"
236           << "-mno-bulk-memory";
237     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
238                            options::OPT_mmutable_globals, false))
239       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
240           << "-pthread"
241           << "-mno-mutable-globals";
242     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
243                            false))
244       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
245           << "-pthread"
246           << "-mno-sign-ext";
247     CC1Args.push_back("-target-feature");
248     CC1Args.push_back("+atomics");
249     CC1Args.push_back("-target-feature");
250     CC1Args.push_back("+bulk-memory");
251     CC1Args.push_back("-target-feature");
252     CC1Args.push_back("+mutable-globals");
253     CC1Args.push_back("-target-feature");
254     CC1Args.push_back("+sign-ext");
255   }
256 
257   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
258                           options::OPT_mno_mutable_globals, false)) {
259     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
260     // depends on importing and exporting mutable globals.
261     llvm::Reloc::Model RelocationModel;
262     unsigned PICLevel;
263     bool IsPIE;
264     std::tie(RelocationModel, PICLevel, IsPIE) =
265         ParsePICArgs(*this, DriverArgs);
266     if (RelocationModel == llvm::Reloc::PIC_) {
267       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
268                              options::OPT_mmutable_globals, false)) {
269         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
270             << "-fPIC"
271             << "-mno-mutable-globals";
272       }
273       CC1Args.push_back("-target-feature");
274       CC1Args.push_back("+mutable-globals");
275     }
276   }
277 
278   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
279     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
280     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
281                            options::OPT_mexception_handing, false))
282       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
283           << "-fwasm-exceptions"
284           << "-mno-exception-handling";
285     // '-fwasm-exceptions' is not compatible with
286     // '-mllvm -enable-emscripten-cxx-exceptions'
287     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
288       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
289         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
290             << "-fwasm-exceptions"
291             << "-mllvm -enable-emscripten-cxx-exceptions";
292     }
293     // '-fwasm-exceptions' implies exception-handling feature
294     CC1Args.push_back("-target-feature");
295     CC1Args.push_back("+exception-handling");
296     // Backend needs -wasm-enable-eh to enable Wasm EH
297     CC1Args.push_back("-mllvm");
298     CC1Args.push_back("-wasm-enable-eh");
299   }
300 
301   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
302     StringRef Opt = A->getValue(0);
303     if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) {
304       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
305       // '-mllvm -enable-emscripten-cxx-exceptions'
306       bool EmEHArgExists = false;
307       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
308         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
309           EmEHArgExists = true;
310           break;
311         }
312       }
313       if (!EmEHArgExists)
314         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
315             << "-mllvm -emscripten-cxx-exceptions-allowed"
316             << "-mllvm -enable-emscripten-cxx-exceptions";
317 
318       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
319       // from being inlined before reaching the wasm backend.
320       StringRef FuncNamesStr = Opt.split('=').second;
321       SmallVector<StringRef, 4> FuncNames;
322       FuncNamesStr.split(FuncNames, ',');
323       for (auto Name : FuncNames) {
324         CC1Args.push_back("-mllvm");
325         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
326                                                    ":noinline"));
327       }
328     }
329 
330     if (Opt.startswith("-wasm-enable-sjlj")) {
331       // '-mllvm -wasm-enable-sjlj' is not compatible with
332       // '-mno-exception-handling'
333       if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
334                              options::OPT_mexception_handing, false))
335         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
336             << "-mllvm -wasm-enable-sjlj"
337             << "-mno-exception-handling";
338       // '-mllvm -wasm-enable-sjlj' is not compatible with
339       // '-mllvm -enable-emscripten-cxx-exceptions'
340       // because we don't allow Emscripten EH + Wasm SjLj
341       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
342         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
343           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
344               << "-mllvm -wasm-enable-sjlj"
345               << "-mllvm -enable-emscripten-cxx-exceptions";
346       }
347       // '-mllvm -wasm-enable-sjlj' is not compatible with
348       // '-mllvm -enable-emscripten-sjlj'
349       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
350         if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
351           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
352               << "-mllvm -wasm-enable-sjlj"
353               << "-mllvm -enable-emscripten-sjlj";
354       }
355       // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
356       CC1Args.push_back("-target-feature");
357       CC1Args.push_back("+exception-handling");
358       // Backend needs '-exception-model=wasm' to use Wasm EH instructions
359       CC1Args.push_back("-exception-model=wasm");
360     }
361   }
362 }
363 
364 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
365   return ToolChain::RLT_CompilerRT;
366 }
367 
368 ToolChain::CXXStdlibType
369 WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
370   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
371     StringRef Value = A->getValue();
372     if (Value != "libc++")
373       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
374           << A->getAsString(Args);
375   }
376   return ToolChain::CST_Libcxx;
377 }
378 
379 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
380                                             ArgStringList &CC1Args) const {
381   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
382     return;
383 
384   const Driver &D = getDriver();
385 
386   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
387     SmallString<128> P(D.ResourceDir);
388     llvm::sys::path::append(P, "include");
389     addSystemInclude(DriverArgs, CC1Args, P);
390   }
391 
392   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
393     return;
394 
395   // Check for configure-time C include directories.
396   StringRef CIncludeDirs(C_INCLUDE_DIRS);
397   if (CIncludeDirs != "") {
398     SmallVector<StringRef, 5> dirs;
399     CIncludeDirs.split(dirs, ":");
400     for (StringRef dir : dirs) {
401       StringRef Prefix =
402           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
403       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
404     }
405     return;
406   }
407 
408   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
409     const std::string MultiarchTriple =
410         getMultiarchTriple(D, getTriple(), D.SysRoot);
411     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
412   }
413   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
414 }
415 
416 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
417                                                ArgStringList &CC1Args) const {
418   if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
419       !DriverArgs.hasArg(options::OPT_nostdincxx)) {
420     if (getTriple().getOS() != llvm::Triple::UnknownOS) {
421       const std::string MultiarchTriple =
422           getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
423       addSystemInclude(DriverArgs, CC1Args,
424                        getDriver().SysRoot + "/include/" + MultiarchTriple +
425                            "/c++/v1");
426     }
427     addSystemInclude(DriverArgs, CC1Args,
428                      getDriver().SysRoot + "/include/c++/v1");
429   }
430 }
431 
432 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
433                                       llvm::opt::ArgStringList &CmdArgs) const {
434 
435   switch (GetCXXStdlibType(Args)) {
436   case ToolChain::CST_Libcxx:
437     CmdArgs.push_back("-lc++");
438     CmdArgs.push_back("-lc++abi");
439     break;
440   case ToolChain::CST_Libstdcxx:
441     llvm_unreachable("invalid stdlib name");
442   }
443 }
444 
445 SanitizerMask WebAssembly::getSupportedSanitizers() const {
446   SanitizerMask Res = ToolChain::getSupportedSanitizers();
447   if (getTriple().isOSEmscripten()) {
448     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
449   }
450   return Res;
451 }
452 
453 Tool *WebAssembly::buildLinker() const {
454   return new tools::wasm::Linker(*this);
455 }
456