xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/WebAssembly.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "WebAssembly.h"
100b57cec5SDimitry Andric #include "CommonArgs.h"
1181ad6265SDimitry Andric #include "Gnu.h"
12480093f4SDimitry Andric #include "clang/Basic/Version.h"
130b57cec5SDimitry Andric #include "clang/Config/config.h"
140b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
150b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
160b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
170b57cec5SDimitry Andric #include "clang/Driver/Options.h"
1881ad6265SDimitry Andric #include "llvm/Option/ArgList.h"
190b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
200b57cec5SDimitry Andric #include "llvm/Support/Path.h"
2181ad6265SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace clang::driver;
240b57cec5SDimitry Andric using namespace clang::driver::tools;
250b57cec5SDimitry Andric using namespace clang::driver::toolchains;
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric using namespace llvm::opt;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
300b57cec5SDimitry Andric /// we remove the vendor field to form the multiarch triple.
31fe6060f1SDimitry Andric std::string WebAssembly::getMultiarchTriple(const Driver &D,
320b57cec5SDimitry Andric                                             const llvm::Triple &TargetTriple,
33fe6060f1SDimitry Andric                                             StringRef SysRoot) const {
340b57cec5SDimitry Andric     return (TargetTriple.getArchName() + "-" +
350b57cec5SDimitry Andric             TargetTriple.getOSAndEnvironmentName()).str();
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
390b57cec5SDimitry Andric   const ToolChain &ToolChain = getToolChain();
400b57cec5SDimitry Andric   if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
410b57cec5SDimitry Andric     StringRef UseLinker = A->getValue();
420b57cec5SDimitry Andric     if (!UseLinker.empty()) {
430b57cec5SDimitry Andric       if (llvm::sys::path::is_absolute(UseLinker) &&
440b57cec5SDimitry Andric           llvm::sys::fs::can_execute(UseLinker))
455ffd83dbSDimitry Andric         return std::string(UseLinker);
460b57cec5SDimitry Andric 
47439352acSDimitry Andric       // Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
48439352acSDimitry Andric       // linker. Note that for `wasm32-wasip2` this overrides the default linker
49439352acSDimitry Andric       // of `wasm-component-ld`.
50439352acSDimitry Andric       if (UseLinker == "lld") {
51439352acSDimitry Andric         return ToolChain.GetProgramPath("wasm-ld");
52439352acSDimitry Andric       }
53439352acSDimitry Andric 
54439352acSDimitry Andric       // Allow 'ld' as an alias for the default linker
55439352acSDimitry Andric       if (UseLinker != "ld")
560b57cec5SDimitry Andric         ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
570b57cec5SDimitry Andric             << A->getAsString(Args);
580b57cec5SDimitry Andric     }
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
650b57cec5SDimitry Andric                                 const InputInfo &Output,
660b57cec5SDimitry Andric                                 const InputInfoList &Inputs,
670b57cec5SDimitry Andric                                 const ArgList &Args,
680b57cec5SDimitry Andric                                 const char *LinkingOutput) const {
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   const ToolChain &ToolChain = getToolChain();
710b57cec5SDimitry Andric   const char *Linker = Args.MakeArgString(getLinkerPath(Args));
720b57cec5SDimitry Andric   ArgStringList CmdArgs;
730b57cec5SDimitry Andric 
745ffd83dbSDimitry Andric   CmdArgs.push_back("-m");
75349cc55cSDimitry Andric   if (ToolChain.getTriple().isArch64Bit())
765ffd83dbSDimitry Andric     CmdArgs.push_back("wasm64");
775ffd83dbSDimitry Andric   else
785ffd83dbSDimitry Andric     CmdArgs.push_back("wasm32");
795ffd83dbSDimitry Andric 
800b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_s))
810b57cec5SDimitry Andric     CmdArgs.push_back("--strip-all");
820b57cec5SDimitry Andric 
83439352acSDimitry Andric   // On `wasip2` the default linker is `wasm-component-ld` which wraps the
84439352acSDimitry Andric   // execution of `wasm-ld`. Find `wasm-ld` and pass it as an argument of where
85439352acSDimitry Andric   // to find it to avoid it needing to hunt and rediscover or search `PATH` for
86439352acSDimitry Andric   // where it is.
87439352acSDimitry Andric   if (llvm::sys::path::stem(Linker).ends_with_insensitive(
88439352acSDimitry Andric           "wasm-component-ld")) {
89439352acSDimitry Andric     CmdArgs.push_back("--wasm-ld-path");
90439352acSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetProgramPath("wasm-ld")));
91439352acSDimitry Andric   }
92439352acSDimitry Andric 
935f757f3fSDimitry Andric   Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
945f757f3fSDimitry Andric 
950b57cec5SDimitry Andric   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
960b57cec5SDimitry Andric 
975c16e71dSDimitry Andric   bool IsCommand = true;
985c16e71dSDimitry Andric   const char *Crt1;
9904eeddc0SDimitry Andric   const char *Entry = nullptr;
100fe6060f1SDimitry Andric 
1015c16e71dSDimitry Andric   // When -shared is specified, use the reactor exec model unless
1025c16e71dSDimitry Andric   // specified otherwise.
1035c16e71dSDimitry Andric   if (Args.hasArg(options::OPT_shared))
1045c16e71dSDimitry Andric     IsCommand = false;
1055c16e71dSDimitry Andric 
1065c16e71dSDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
1075c16e71dSDimitry Andric     StringRef CM = A->getValue();
1085c16e71dSDimitry Andric     if (CM == "command") {
1095c16e71dSDimitry Andric       IsCommand = true;
1105c16e71dSDimitry Andric     } else if (CM == "reactor") {
1115c16e71dSDimitry Andric       IsCommand = false;
1125c16e71dSDimitry Andric     } else {
1135c16e71dSDimitry Andric       ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
1145c16e71dSDimitry Andric           << CM << A->getOption().getName();
1155c16e71dSDimitry Andric     }
1165c16e71dSDimitry Andric   }
1175c16e71dSDimitry Andric 
1185c16e71dSDimitry Andric   if (IsCommand) {
119fe6060f1SDimitry Andric     // If crt1-command.o exists, it supports new-style commands, so use it.
120fe6060f1SDimitry Andric     // Otherwise, use the old crt1.o. This is a temporary transition measure.
121fe6060f1SDimitry Andric     // Once WASI libc no longer needs to support LLVM versions which lack
122fe6060f1SDimitry Andric     // support for new-style command, it can make crt1.o the same as
123fe6060f1SDimitry Andric     // crt1-command.o. And once LLVM no longer needs to support WASI libc
124fe6060f1SDimitry Andric     // versions before that, it can switch to using crt1-command.o.
1255c16e71dSDimitry Andric     Crt1 = "crt1.o";
126fe6060f1SDimitry Andric     if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
127fe6060f1SDimitry Andric       Crt1 = "crt1-command.o";
1285c16e71dSDimitry Andric   } else {
1295ffd83dbSDimitry Andric     Crt1 = "crt1-reactor.o";
1305ffd83dbSDimitry Andric     Entry = "_initialize";
1315ffd83dbSDimitry Andric   }
1325c16e71dSDimitry Andric 
1335c16e71dSDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
1345ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
1355ffd83dbSDimitry Andric   if (Entry) {
1365ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString("--entry"));
1375ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Entry));
1385ffd83dbSDimitry Andric   }
1390b57cec5SDimitry Andric 
14006c3fb27SDimitry Andric   if (Args.hasArg(options::OPT_shared))
14106c3fb27SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-shared"));
14206c3fb27SDimitry Andric 
1430b57cec5SDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
1460b57cec5SDimitry Andric     if (ToolChain.ShouldLinkCXXStdlib(Args))
1470b57cec5SDimitry Andric       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_pthread)) {
1500b57cec5SDimitry Andric       CmdArgs.push_back("-lpthread");
1510b57cec5SDimitry Andric       CmdArgs.push_back("--shared-memory");
1520b57cec5SDimitry Andric     }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     CmdArgs.push_back("-lc");
1550b57cec5SDimitry Andric     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   CmdArgs.push_back("-o");
1590b57cec5SDimitry Andric   CmdArgs.push_back(Output.getFilename());
1600b57cec5SDimitry Andric 
161*0fca6ea1SDimitry Andric   if (Args.hasFlag(options::OPT_wasm_opt, options::OPT_no_wasm_opt, true)) {
1625f757f3fSDimitry Andric     // When optimizing, if wasm-opt is available, run it.
1635f757f3fSDimitry Andric     std::string WasmOptPath;
1645f757f3fSDimitry Andric     if (Args.getLastArg(options::OPT_O_Group)) {
1655f757f3fSDimitry Andric       WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
1665f757f3fSDimitry Andric       if (WasmOptPath == "wasm-opt") {
1675f757f3fSDimitry Andric         WasmOptPath = {};
1685f757f3fSDimitry Andric       }
1695f757f3fSDimitry Andric     }
1705f757f3fSDimitry Andric 
1715f757f3fSDimitry Andric     if (!WasmOptPath.empty()) {
1725f757f3fSDimitry Andric       CmdArgs.push_back("--keep-section=target_features");
1735f757f3fSDimitry Andric     }
1745f757f3fSDimitry Andric 
175e8d8bef9SDimitry Andric     C.addCommand(std::make_unique<Command>(JA, *this,
176e8d8bef9SDimitry Andric                                            ResponseFileSupport::AtFileCurCP(),
177e8d8bef9SDimitry Andric                                            Linker, CmdArgs, Inputs, Output));
178480093f4SDimitry Andric 
179480093f4SDimitry Andric     if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
1805f757f3fSDimitry Andric       if (!WasmOptPath.empty()) {
181480093f4SDimitry Andric         StringRef OOpt = "s";
182480093f4SDimitry Andric         if (A->getOption().matches(options::OPT_O4) ||
183480093f4SDimitry Andric             A->getOption().matches(options::OPT_Ofast))
184480093f4SDimitry Andric           OOpt = "4";
185480093f4SDimitry Andric         else if (A->getOption().matches(options::OPT_O0))
186480093f4SDimitry Andric           OOpt = "0";
187480093f4SDimitry Andric         else if (A->getOption().matches(options::OPT_O))
188480093f4SDimitry Andric           OOpt = A->getValue();
189480093f4SDimitry Andric 
190480093f4SDimitry Andric         if (OOpt != "0") {
191480093f4SDimitry Andric           const char *WasmOpt = Args.MakeArgString(WasmOptPath);
1925f757f3fSDimitry Andric           ArgStringList OptArgs;
1935f757f3fSDimitry Andric           OptArgs.push_back(Output.getFilename());
1945f757f3fSDimitry Andric           OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
1955f757f3fSDimitry Andric           OptArgs.push_back("-o");
1965f757f3fSDimitry Andric           OptArgs.push_back(Output.getFilename());
1975ffd83dbSDimitry Andric           C.addCommand(std::make_unique<Command>(
1985f757f3fSDimitry Andric               JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
199e8d8bef9SDimitry Andric               Inputs, Output));
200480093f4SDimitry Andric         }
201480093f4SDimitry Andric       }
202480093f4SDimitry Andric     }
203480093f4SDimitry Andric   }
204*0fca6ea1SDimitry Andric }
205480093f4SDimitry Andric 
206480093f4SDimitry Andric /// Given a base library directory, append path components to form the
207480093f4SDimitry Andric /// LTO directory.
208480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) {
209480093f4SDimitry Andric     // The version allows the path to be keyed to the specific version of
210480093f4SDimitry Andric     // LLVM in used, as the bitcode format is not stable.
211480093f4SDimitry Andric     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
2150b57cec5SDimitry Andric                          const llvm::opt::ArgList &Args)
2160b57cec5SDimitry Andric     : ToolChain(D, Triple, Args) {
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
2190b57cec5SDimitry Andric 
220*0fca6ea1SDimitry Andric   getProgramPaths().push_back(getDriver().Dir);
2210b57cec5SDimitry Andric 
222480093f4SDimitry Andric   auto SysRoot = getDriver().SysRoot;
2230b57cec5SDimitry Andric   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
2240b57cec5SDimitry Andric     // Theoretically an "unknown" OS should mean no standard libraries, however
2250b57cec5SDimitry Andric     // it could also mean that a custom set of libraries is in use, so just add
2260b57cec5SDimitry Andric     // /lib to the search path. Disable multiarch in this case, to discourage
2270b57cec5SDimitry Andric     // paths containing "unknown" from acquiring meanings.
228480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib");
2290b57cec5SDimitry Andric   } else {
2300b57cec5SDimitry Andric     const std::string MultiarchTriple =
231480093f4SDimitry Andric         getMultiarchTriple(getDriver(), Triple, SysRoot);
232480093f4SDimitry Andric     if (D.isUsingLTO()) {
233480093f4SDimitry Andric       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
234480093f4SDimitry Andric       // Note that the directory is keyed to the LLVM revision, as LLVM's
235480093f4SDimitry Andric       // bitcode format is not stable.
236480093f4SDimitry Andric       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
237480093f4SDimitry Andric       getFilePaths().push_back(Dir);
238480093f4SDimitry Andric     }
239480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
243439352acSDimitry Andric const char *WebAssembly::getDefaultLinker() const {
244439352acSDimitry Andric   if (getOS() == "wasip2")
245439352acSDimitry Andric     return "wasm-component-ld";
246439352acSDimitry Andric   return "wasm-ld";
247439352acSDimitry Andric }
248439352acSDimitry Andric 
2490b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; }
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; }
2560b57cec5SDimitry Andric 
257349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
258349cc55cSDimitry Andric   return false;
259349cc55cSDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric // TODO: Support profiling.
2660b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
2710b57cec5SDimitry Andric                                         ArgStringList &CC1Args,
2720b57cec5SDimitry Andric                                         Action::OffloadKind) const {
273480093f4SDimitry Andric   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
2740b57cec5SDimitry Andric                           options::OPT_fno_use_init_array, true))
275480093f4SDimitry Andric     CC1Args.push_back("-fno-use-init-array");
2760b57cec5SDimitry Andric 
277a7dea167SDimitry Andric   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
2780b57cec5SDimitry Andric   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
2790b57cec5SDimitry Andric                          false)) {
2800b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
2810b57cec5SDimitry Andric                            false))
2820b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2830b57cec5SDimitry Andric           << "-pthread"
2840b57cec5SDimitry Andric           << "-mno-atomics";
2850b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
2860b57cec5SDimitry Andric                            options::OPT_mbulk_memory, false))
2870b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2880b57cec5SDimitry Andric           << "-pthread"
2890b57cec5SDimitry Andric           << "-mno-bulk-memory";
2900b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
2910b57cec5SDimitry Andric                            options::OPT_mmutable_globals, false))
2920b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2930b57cec5SDimitry Andric           << "-pthread"
2940b57cec5SDimitry Andric           << "-mno-mutable-globals";
295a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
296a7dea167SDimitry Andric                            false))
297a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
298a7dea167SDimitry Andric           << "-pthread"
299a7dea167SDimitry Andric           << "-mno-sign-ext";
3000b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
3010b57cec5SDimitry Andric     CC1Args.push_back("+atomics");
3020b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
3030b57cec5SDimitry Andric     CC1Args.push_back("+bulk-memory");
3040b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
3050b57cec5SDimitry Andric     CC1Args.push_back("+mutable-globals");
306a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
307a7dea167SDimitry Andric     CC1Args.push_back("+sign-ext");
308a7dea167SDimitry Andric   }
309a7dea167SDimitry Andric 
310e8d8bef9SDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
311e8d8bef9SDimitry Andric                           options::OPT_mno_mutable_globals, false)) {
312e8d8bef9SDimitry Andric     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
313e8d8bef9SDimitry Andric     // depends on importing and exporting mutable globals.
314e8d8bef9SDimitry Andric     llvm::Reloc::Model RelocationModel;
315e8d8bef9SDimitry Andric     unsigned PICLevel;
316e8d8bef9SDimitry Andric     bool IsPIE;
317e8d8bef9SDimitry Andric     std::tie(RelocationModel, PICLevel, IsPIE) =
318e8d8bef9SDimitry Andric         ParsePICArgs(*this, DriverArgs);
319e8d8bef9SDimitry Andric     if (RelocationModel == llvm::Reloc::PIC_) {
320e8d8bef9SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
321e8d8bef9SDimitry Andric                              options::OPT_mmutable_globals, false)) {
322e8d8bef9SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
323e8d8bef9SDimitry Andric             << "-fPIC"
324e8d8bef9SDimitry Andric             << "-mno-mutable-globals";
325e8d8bef9SDimitry Andric       }
326e8d8bef9SDimitry Andric       CC1Args.push_back("-target-feature");
327e8d8bef9SDimitry Andric       CC1Args.push_back("+mutable-globals");
328e8d8bef9SDimitry Andric     }
329e8d8bef9SDimitry Andric   }
330e8d8bef9SDimitry Andric 
331a7dea167SDimitry Andric   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
332a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
333a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
334a7dea167SDimitry Andric                            options::OPT_mexception_handing, false))
335a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
336a7dea167SDimitry Andric           << "-fwasm-exceptions"
337a7dea167SDimitry Andric           << "-mno-exception-handling";
338a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with
339a7dea167SDimitry Andric     // '-mllvm -enable-emscripten-cxx-exceptions'
340a7dea167SDimitry Andric     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
341a7dea167SDimitry Andric       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
342a7dea167SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
343a7dea167SDimitry Andric             << "-fwasm-exceptions"
344a7dea167SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
345a7dea167SDimitry Andric     }
346fe6060f1SDimitry Andric     // '-fwasm-exceptions' implies exception-handling feature
347a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
348a7dea167SDimitry Andric     CC1Args.push_back("+exception-handling");
349349cc55cSDimitry Andric     // Backend needs -wasm-enable-eh to enable Wasm EH
350349cc55cSDimitry Andric     CC1Args.push_back("-mllvm");
351349cc55cSDimitry Andric     CC1Args.push_back("-wasm-enable-eh");
352*0fca6ea1SDimitry Andric 
353*0fca6ea1SDimitry Andric     // New Wasm EH spec (adopted in Oct 2023) requires multivalue and
354*0fca6ea1SDimitry Andric     // reference-types.
355*0fca6ea1SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_multivalue,
356*0fca6ea1SDimitry Andric                            options::OPT_mmultivalue, false)) {
357*0fca6ea1SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
358*0fca6ea1SDimitry Andric           << "-fwasm-exceptions" << "-mno-multivalue";
359*0fca6ea1SDimitry Andric     }
360*0fca6ea1SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
361*0fca6ea1SDimitry Andric                            options::OPT_mreference_types, false)) {
362*0fca6ea1SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
363*0fca6ea1SDimitry Andric           << "-fwasm-exceptions" << "-mno-reference-types";
364*0fca6ea1SDimitry Andric     }
365*0fca6ea1SDimitry Andric     CC1Args.push_back("-target-feature");
366*0fca6ea1SDimitry Andric     CC1Args.push_back("+multivalue");
367*0fca6ea1SDimitry Andric     CC1Args.push_back("-target-feature");
368*0fca6ea1SDimitry Andric     CC1Args.push_back("+reference-types");
369fe6060f1SDimitry Andric   }
370fe6060f1SDimitry Andric 
371fe6060f1SDimitry Andric   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
372fe6060f1SDimitry Andric     StringRef Opt = A->getValue(0);
3735f757f3fSDimitry Andric     if (Opt.starts_with("-emscripten-cxx-exceptions-allowed")) {
374fe6060f1SDimitry Andric       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
375fe6060f1SDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
376349cc55cSDimitry Andric       bool EmEHArgExists = false;
377fe6060f1SDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
378fe6060f1SDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
379349cc55cSDimitry Andric           EmEHArgExists = true;
380fe6060f1SDimitry Andric           break;
381fe6060f1SDimitry Andric         }
382fe6060f1SDimitry Andric       }
383349cc55cSDimitry Andric       if (!EmEHArgExists)
384fe6060f1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
385fe6060f1SDimitry Andric             << "-mllvm -emscripten-cxx-exceptions-allowed"
386fe6060f1SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
387fe6060f1SDimitry Andric 
388fe6060f1SDimitry Andric       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
389fe6060f1SDimitry Andric       // from being inlined before reaching the wasm backend.
390fe6060f1SDimitry Andric       StringRef FuncNamesStr = Opt.split('=').second;
391fe6060f1SDimitry Andric       SmallVector<StringRef, 4> FuncNames;
392fe6060f1SDimitry Andric       FuncNamesStr.split(FuncNames, ',');
393fe6060f1SDimitry Andric       for (auto Name : FuncNames) {
394fe6060f1SDimitry Andric         CC1Args.push_back("-mllvm");
395fe6060f1SDimitry Andric         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
396fe6060f1SDimitry Andric                                                    ":noinline"));
397fe6060f1SDimitry Andric       }
398fe6060f1SDimitry Andric     }
399349cc55cSDimitry Andric 
4005f757f3fSDimitry Andric     if (Opt.starts_with("-wasm-enable-sjlj")) {
401349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
402349cc55cSDimitry Andric       // '-mno-exception-handling'
403349cc55cSDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
404349cc55cSDimitry Andric                              options::OPT_mexception_handing, false))
405349cc55cSDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
406349cc55cSDimitry Andric             << "-mllvm -wasm-enable-sjlj"
407349cc55cSDimitry Andric             << "-mno-exception-handling";
408349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
409349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
410349cc55cSDimitry Andric       // because we don't allow Emscripten EH + Wasm SjLj
411349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
412349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
413349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
414349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
415349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-cxx-exceptions";
416349cc55cSDimitry Andric       }
417349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
418349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-sjlj'
419349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
420349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
421349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
422349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
423349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-sjlj";
424349cc55cSDimitry Andric       }
425349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
426349cc55cSDimitry Andric       CC1Args.push_back("-target-feature");
427349cc55cSDimitry Andric       CC1Args.push_back("+exception-handling");
428349cc55cSDimitry Andric       // Backend needs '-exception-model=wasm' to use Wasm EH instructions
429349cc55cSDimitry Andric       CC1Args.push_back("-exception-model=wasm");
430*0fca6ea1SDimitry Andric 
431*0fca6ea1SDimitry Andric       // New Wasm EH spec (adopted in Oct 2023) requires multivalue and
432*0fca6ea1SDimitry Andric       // reference-types.
433*0fca6ea1SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_multivalue,
434*0fca6ea1SDimitry Andric                              options::OPT_mmultivalue, false)) {
435*0fca6ea1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
436*0fca6ea1SDimitry Andric             << "-mllvm -wasm-enable-sjlj" << "-mno-multivalue";
437*0fca6ea1SDimitry Andric       }
438*0fca6ea1SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
439*0fca6ea1SDimitry Andric                              options::OPT_mreference_types, false)) {
440*0fca6ea1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
441*0fca6ea1SDimitry Andric             << "-mllvm -wasm-enable-sjlj" << "-mno-reference-types";
442*0fca6ea1SDimitry Andric       }
443*0fca6ea1SDimitry Andric       CC1Args.push_back("-target-feature");
444*0fca6ea1SDimitry Andric       CC1Args.push_back("+multivalue");
445*0fca6ea1SDimitry Andric       CC1Args.push_back("-target-feature");
446*0fca6ea1SDimitry Andric       CC1Args.push_back("+reference-types");
447349cc55cSDimitry Andric     }
4480b57cec5SDimitry Andric   }
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
4520b57cec5SDimitry Andric   return ToolChain::RLT_CompilerRT;
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric ToolChain::CXXStdlibType
4560b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
4570b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
4580b57cec5SDimitry Andric     StringRef Value = A->getValue();
45981ad6265SDimitry Andric     if (Value == "libc++")
46081ad6265SDimitry Andric       return ToolChain::CST_Libcxx;
46181ad6265SDimitry Andric     else if (Value == "libstdc++")
46281ad6265SDimitry Andric       return ToolChain::CST_Libstdcxx;
46381ad6265SDimitry Andric     else
4640b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
4650b57cec5SDimitry Andric           << A->getAsString(Args);
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric   return ToolChain::CST_Libcxx;
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
4710b57cec5SDimitry Andric                                             ArgStringList &CC1Args) const {
4720b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
4730b57cec5SDimitry Andric     return;
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   const Driver &D = getDriver();
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
4780b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
4790b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
4800b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
4840b57cec5SDimitry Andric     return;
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   // Check for configure-time C include directories.
4870b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
4880b57cec5SDimitry Andric   if (CIncludeDirs != "") {
4890b57cec5SDimitry Andric     SmallVector<StringRef, 5> dirs;
4900b57cec5SDimitry Andric     CIncludeDirs.split(dirs, ":");
4910b57cec5SDimitry Andric     for (StringRef dir : dirs) {
4920b57cec5SDimitry Andric       StringRef Prefix =
4935ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
4940b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
4950b57cec5SDimitry Andric     }
4960b57cec5SDimitry Andric     return;
4970b57cec5SDimitry Andric   }
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
5000b57cec5SDimitry Andric     const std::string MultiarchTriple =
5010b57cec5SDimitry Andric         getMultiarchTriple(D, getTriple(), D.SysRoot);
5020b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
5030b57cec5SDimitry Andric   }
5040b57cec5SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
5080b57cec5SDimitry Andric                                                ArgStringList &CC1Args) const {
50981ad6265SDimitry Andric 
510bdd1243dSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
511bdd1243dSDimitry Andric                         options::OPT_nostdincxx))
51281ad6265SDimitry Andric     return;
51381ad6265SDimitry Andric 
51481ad6265SDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
51581ad6265SDimitry Andric   case ToolChain::CST_Libcxx:
51681ad6265SDimitry Andric     addLibCxxIncludePaths(DriverArgs, CC1Args);
51781ad6265SDimitry Andric     break;
51881ad6265SDimitry Andric   case ToolChain::CST_Libstdcxx:
51981ad6265SDimitry Andric     addLibStdCXXIncludePaths(DriverArgs, CC1Args);
52081ad6265SDimitry Andric     break;
5210b57cec5SDimitry Andric   }
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
5250b57cec5SDimitry Andric                                       llvm::opt::ArgStringList &CmdArgs) const {
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   switch (GetCXXStdlibType(Args)) {
5280b57cec5SDimitry Andric   case ToolChain::CST_Libcxx:
5290b57cec5SDimitry Andric     CmdArgs.push_back("-lc++");
530fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
531fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
5320b57cec5SDimitry Andric     CmdArgs.push_back("-lc++abi");
5330b57cec5SDimitry Andric     break;
5340b57cec5SDimitry Andric   case ToolChain::CST_Libstdcxx:
53581ad6265SDimitry Andric     CmdArgs.push_back("-lstdc++");
53681ad6265SDimitry Andric     break;
5370b57cec5SDimitry Andric   }
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const {
5410b57cec5SDimitry Andric   SanitizerMask Res = ToolChain::getSupportedSanitizers();
5420b57cec5SDimitry Andric   if (getTriple().isOSEmscripten()) {
5430b57cec5SDimitry Andric     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
5440b57cec5SDimitry Andric   }
54506c3fb27SDimitry Andric   // -fsanitize=function places two words before the function label, which are
54606c3fb27SDimitry Andric   // -unsupported.
54706c3fb27SDimitry Andric   Res &= ~SanitizerKind::Function;
5480b57cec5SDimitry Andric   return Res;
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const {
5520b57cec5SDimitry Andric   return new tools::wasm::Linker(*this);
5530b57cec5SDimitry Andric }
55481ad6265SDimitry Andric 
55581ad6265SDimitry Andric void WebAssembly::addLibCxxIncludePaths(
55681ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
55781ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
55881ad6265SDimitry Andric   const Driver &D = getDriver();
55981ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
56081ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
56181ad6265SDimitry Andric   const std::string MultiarchTriple =
56281ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
56381ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
56481ad6265SDimitry Andric 
56581ad6265SDimitry Andric   std::string Version = detectLibcxxVersion(LibPath);
56681ad6265SDimitry Andric   if (Version.empty())
56781ad6265SDimitry Andric     return;
56881ad6265SDimitry Andric 
56981ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
57081ad6265SDimitry Andric   if (IsKnownOs) {
57181ad6265SDimitry Andric     std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
57281ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
57381ad6265SDimitry Andric   }
57481ad6265SDimitry Andric 
57581ad6265SDimitry Andric   // Second add the generic one.
57681ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
57781ad6265SDimitry Andric }
57881ad6265SDimitry Andric 
57981ad6265SDimitry Andric void WebAssembly::addLibStdCXXIncludePaths(
58081ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
58181ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
58281ad6265SDimitry Andric   // We cannot use GCCInstallationDetector here as the sysroot usually does
58381ad6265SDimitry Andric   // not contain a full GCC installation.
58481ad6265SDimitry Andric   // Instead, we search the given sysroot for /usr/include/xx, similar
58581ad6265SDimitry Andric   // to how we do it for libc++.
58681ad6265SDimitry Andric   const Driver &D = getDriver();
58781ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
58881ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
58981ad6265SDimitry Andric   const std::string MultiarchTriple =
59081ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
59181ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
59281ad6265SDimitry Andric 
59381ad6265SDimitry Andric   // This is similar to detectLibcxxVersion()
59481ad6265SDimitry Andric   std::string Version;
59581ad6265SDimitry Andric   {
59681ad6265SDimitry Andric     std::error_code EC;
59781ad6265SDimitry Andric     Generic_GCC::GCCVersion MaxVersion =
59881ad6265SDimitry Andric         Generic_GCC::GCCVersion::Parse("0.0.0");
59981ad6265SDimitry Andric     SmallString<128> Path(LibPath);
60081ad6265SDimitry Andric     llvm::sys::path::append(Path, "c++");
60181ad6265SDimitry Andric     for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
60281ad6265SDimitry Andric          !EC && LI != LE; LI = LI.increment(EC)) {
60381ad6265SDimitry Andric       StringRef VersionText = llvm::sys::path::filename(LI->path());
60481ad6265SDimitry Andric       if (VersionText[0] != 'v') {
60581ad6265SDimitry Andric         auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
60681ad6265SDimitry Andric         if (Version > MaxVersion)
60781ad6265SDimitry Andric           MaxVersion = Version;
60881ad6265SDimitry Andric       }
60981ad6265SDimitry Andric     }
61081ad6265SDimitry Andric     if (MaxVersion.Major > 0)
61181ad6265SDimitry Andric       Version = MaxVersion.Text;
61281ad6265SDimitry Andric   }
61381ad6265SDimitry Andric 
61481ad6265SDimitry Andric   if (Version.empty())
61581ad6265SDimitry Andric     return;
61681ad6265SDimitry Andric 
61781ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
61881ad6265SDimitry Andric   if (IsKnownOs) {
61981ad6265SDimitry Andric     std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
62081ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
62181ad6265SDimitry Andric   }
62281ad6265SDimitry Andric 
62381ad6265SDimitry Andric   // Second add the generic one.
62481ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
62581ad6265SDimitry Andric   // Third the backward one.
62681ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
62781ad6265SDimitry Andric }
628