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.
getMultiarchTriple(const Driver & D,const llvm::Triple & TargetTriple,StringRef SysRoot) const31fe6060f1SDimitry 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 
getLinkerPath(const ArgList & Args) const380b57cec5SDimitry 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 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const640b57cec5SDimitry 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 
1615f757f3fSDimitry Andric   // When optimizing, if wasm-opt is available, run it.
1625f757f3fSDimitry Andric   std::string WasmOptPath;
1635f757f3fSDimitry Andric   if (Args.getLastArg(options::OPT_O_Group)) {
1645f757f3fSDimitry Andric     WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
1655f757f3fSDimitry Andric     if (WasmOptPath == "wasm-opt") {
1665f757f3fSDimitry Andric       WasmOptPath = {};
1675f757f3fSDimitry Andric     }
1685f757f3fSDimitry Andric   }
1695f757f3fSDimitry Andric 
1705f757f3fSDimitry Andric   if (!WasmOptPath.empty()) {
1715f757f3fSDimitry Andric     CmdArgs.push_back("--keep-section=target_features");
1725f757f3fSDimitry Andric   }
1735f757f3fSDimitry Andric 
174e8d8bef9SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this,
175e8d8bef9SDimitry Andric                                          ResponseFileSupport::AtFileCurCP(),
176e8d8bef9SDimitry Andric                                          Linker, CmdArgs, Inputs, Output));
177480093f4SDimitry Andric 
178480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
1795f757f3fSDimitry Andric     if (!WasmOptPath.empty()) {
180480093f4SDimitry Andric       StringRef OOpt = "s";
181480093f4SDimitry Andric       if (A->getOption().matches(options::OPT_O4) ||
182480093f4SDimitry Andric           A->getOption().matches(options::OPT_Ofast))
183480093f4SDimitry Andric         OOpt = "4";
184480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O0))
185480093f4SDimitry Andric         OOpt = "0";
186480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O))
187480093f4SDimitry Andric         OOpt = A->getValue();
188480093f4SDimitry Andric 
189480093f4SDimitry Andric       if (OOpt != "0") {
190480093f4SDimitry Andric         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
1915f757f3fSDimitry Andric         ArgStringList OptArgs;
1925f757f3fSDimitry Andric         OptArgs.push_back(Output.getFilename());
1935f757f3fSDimitry Andric         OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
1945f757f3fSDimitry Andric         OptArgs.push_back("-o");
1955f757f3fSDimitry Andric         OptArgs.push_back(Output.getFilename());
1965ffd83dbSDimitry Andric         C.addCommand(std::make_unique<Command>(
1975f757f3fSDimitry Andric             JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
198e8d8bef9SDimitry Andric             Inputs, Output));
199480093f4SDimitry Andric       }
200480093f4SDimitry Andric     }
201480093f4SDimitry Andric   }
202480093f4SDimitry Andric }
203480093f4SDimitry Andric 
204480093f4SDimitry Andric /// Given a base library directory, append path components to form the
205480093f4SDimitry Andric /// LTO directory.
AppendLTOLibDir(const std::string & Dir)206480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) {
207480093f4SDimitry Andric     // The version allows the path to be keyed to the specific version of
208480093f4SDimitry Andric     // LLVM in used, as the bitcode format is not stable.
209480093f4SDimitry Andric     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
WebAssembly(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args)2120b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
2130b57cec5SDimitry Andric                          const llvm::opt::ArgList &Args)
2140b57cec5SDimitry Andric     : ToolChain(D, Triple, Args) {
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   getProgramPaths().push_back(getDriver().getInstalledDir());
2190b57cec5SDimitry Andric 
220480093f4SDimitry Andric   auto SysRoot = getDriver().SysRoot;
2210b57cec5SDimitry Andric   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
2220b57cec5SDimitry Andric     // Theoretically an "unknown" OS should mean no standard libraries, however
2230b57cec5SDimitry Andric     // it could also mean that a custom set of libraries is in use, so just add
2240b57cec5SDimitry Andric     // /lib to the search path. Disable multiarch in this case, to discourage
2250b57cec5SDimitry Andric     // paths containing "unknown" from acquiring meanings.
226480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib");
2270b57cec5SDimitry Andric   } else {
2280b57cec5SDimitry Andric     const std::string MultiarchTriple =
229480093f4SDimitry Andric         getMultiarchTriple(getDriver(), Triple, SysRoot);
230480093f4SDimitry Andric     if (D.isUsingLTO()) {
231480093f4SDimitry Andric       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
232480093f4SDimitry Andric       // Note that the directory is keyed to the LLVM revision, as LLVM's
233480093f4SDimitry Andric       // bitcode format is not stable.
234480093f4SDimitry Andric       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
235480093f4SDimitry Andric       getFilePaths().push_back(Dir);
236480093f4SDimitry Andric     }
237480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
getDefaultLinker() const241439352acSDimitry Andric const char *WebAssembly::getDefaultLinker() const {
242439352acSDimitry Andric   if (getOS() == "wasip2")
243439352acSDimitry Andric     return "wasm-component-ld";
244439352acSDimitry Andric   return "wasm-ld";
245439352acSDimitry Andric }
246439352acSDimitry Andric 
IsMathErrnoDefault() const2470b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; }
2480b57cec5SDimitry Andric 
IsObjCNonFragileABIDefault() const2490b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
2500b57cec5SDimitry Andric 
UseObjCMixedDispatch() const2510b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; }
2520b57cec5SDimitry Andric 
isPICDefault() const2530b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; }
2540b57cec5SDimitry Andric 
isPIEDefault(const llvm::opt::ArgList & Args) const255349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
256349cc55cSDimitry Andric   return false;
257349cc55cSDimitry Andric }
2580b57cec5SDimitry Andric 
isPICDefaultForced() const2590b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; }
2600b57cec5SDimitry Andric 
hasBlocksRuntime() const2610b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric // TODO: Support profiling.
SupportsProfiling() const2640b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; }
2650b57cec5SDimitry Andric 
HasNativeLLVMSupport() const2660b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; }
2670b57cec5SDimitry Andric 
addClangTargetOptions(const ArgList & DriverArgs,ArgStringList & CC1Args,Action::OffloadKind) const2680b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
2690b57cec5SDimitry Andric                                         ArgStringList &CC1Args,
2700b57cec5SDimitry Andric                                         Action::OffloadKind) const {
271480093f4SDimitry Andric   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
2720b57cec5SDimitry Andric                           options::OPT_fno_use_init_array, true))
273480093f4SDimitry Andric     CC1Args.push_back("-fno-use-init-array");
2740b57cec5SDimitry Andric 
275a7dea167SDimitry Andric   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
2760b57cec5SDimitry Andric   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
2770b57cec5SDimitry Andric                          false)) {
2780b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
2790b57cec5SDimitry Andric                            false))
2800b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2810b57cec5SDimitry Andric           << "-pthread"
2820b57cec5SDimitry Andric           << "-mno-atomics";
2830b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
2840b57cec5SDimitry Andric                            options::OPT_mbulk_memory, false))
2850b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2860b57cec5SDimitry Andric           << "-pthread"
2870b57cec5SDimitry Andric           << "-mno-bulk-memory";
2880b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
2890b57cec5SDimitry Andric                            options::OPT_mmutable_globals, false))
2900b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2910b57cec5SDimitry Andric           << "-pthread"
2920b57cec5SDimitry Andric           << "-mno-mutable-globals";
293a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
294a7dea167SDimitry Andric                            false))
295a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
296a7dea167SDimitry Andric           << "-pthread"
297a7dea167SDimitry Andric           << "-mno-sign-ext";
2980b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2990b57cec5SDimitry Andric     CC1Args.push_back("+atomics");
3000b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
3010b57cec5SDimitry Andric     CC1Args.push_back("+bulk-memory");
3020b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
3030b57cec5SDimitry Andric     CC1Args.push_back("+mutable-globals");
304a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
305a7dea167SDimitry Andric     CC1Args.push_back("+sign-ext");
306a7dea167SDimitry Andric   }
307a7dea167SDimitry Andric 
308e8d8bef9SDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
309e8d8bef9SDimitry Andric                           options::OPT_mno_mutable_globals, false)) {
310e8d8bef9SDimitry Andric     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
311e8d8bef9SDimitry Andric     // depends on importing and exporting mutable globals.
312e8d8bef9SDimitry Andric     llvm::Reloc::Model RelocationModel;
313e8d8bef9SDimitry Andric     unsigned PICLevel;
314e8d8bef9SDimitry Andric     bool IsPIE;
315e8d8bef9SDimitry Andric     std::tie(RelocationModel, PICLevel, IsPIE) =
316e8d8bef9SDimitry Andric         ParsePICArgs(*this, DriverArgs);
317e8d8bef9SDimitry Andric     if (RelocationModel == llvm::Reloc::PIC_) {
318e8d8bef9SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
319e8d8bef9SDimitry Andric                              options::OPT_mmutable_globals, false)) {
320e8d8bef9SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
321e8d8bef9SDimitry Andric             << "-fPIC"
322e8d8bef9SDimitry Andric             << "-mno-mutable-globals";
323e8d8bef9SDimitry Andric       }
324e8d8bef9SDimitry Andric       CC1Args.push_back("-target-feature");
325e8d8bef9SDimitry Andric       CC1Args.push_back("+mutable-globals");
326e8d8bef9SDimitry Andric     }
327e8d8bef9SDimitry Andric   }
328e8d8bef9SDimitry Andric 
329a7dea167SDimitry Andric   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
330a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
331a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
332a7dea167SDimitry Andric                            options::OPT_mexception_handing, false))
333a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
334a7dea167SDimitry Andric           << "-fwasm-exceptions"
335a7dea167SDimitry Andric           << "-mno-exception-handling";
336a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with
337a7dea167SDimitry Andric     // '-mllvm -enable-emscripten-cxx-exceptions'
338a7dea167SDimitry Andric     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
339a7dea167SDimitry Andric       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
340a7dea167SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
341a7dea167SDimitry Andric             << "-fwasm-exceptions"
342a7dea167SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
343a7dea167SDimitry Andric     }
344fe6060f1SDimitry Andric     // '-fwasm-exceptions' implies exception-handling feature
345a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
346a7dea167SDimitry Andric     CC1Args.push_back("+exception-handling");
347349cc55cSDimitry Andric     // Backend needs -wasm-enable-eh to enable Wasm EH
348349cc55cSDimitry Andric     CC1Args.push_back("-mllvm");
349349cc55cSDimitry Andric     CC1Args.push_back("-wasm-enable-eh");
350fe6060f1SDimitry Andric   }
351fe6060f1SDimitry Andric 
352fe6060f1SDimitry Andric   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
353fe6060f1SDimitry Andric     StringRef Opt = A->getValue(0);
3545f757f3fSDimitry Andric     if (Opt.starts_with("-emscripten-cxx-exceptions-allowed")) {
355fe6060f1SDimitry Andric       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
356fe6060f1SDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
357349cc55cSDimitry Andric       bool EmEHArgExists = false;
358fe6060f1SDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
359fe6060f1SDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
360349cc55cSDimitry Andric           EmEHArgExists = true;
361fe6060f1SDimitry Andric           break;
362fe6060f1SDimitry Andric         }
363fe6060f1SDimitry Andric       }
364349cc55cSDimitry Andric       if (!EmEHArgExists)
365fe6060f1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
366fe6060f1SDimitry Andric             << "-mllvm -emscripten-cxx-exceptions-allowed"
367fe6060f1SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
368fe6060f1SDimitry Andric 
369fe6060f1SDimitry Andric       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
370fe6060f1SDimitry Andric       // from being inlined before reaching the wasm backend.
371fe6060f1SDimitry Andric       StringRef FuncNamesStr = Opt.split('=').second;
372fe6060f1SDimitry Andric       SmallVector<StringRef, 4> FuncNames;
373fe6060f1SDimitry Andric       FuncNamesStr.split(FuncNames, ',');
374fe6060f1SDimitry Andric       for (auto Name : FuncNames) {
375fe6060f1SDimitry Andric         CC1Args.push_back("-mllvm");
376fe6060f1SDimitry Andric         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
377fe6060f1SDimitry Andric                                                    ":noinline"));
378fe6060f1SDimitry Andric       }
379fe6060f1SDimitry Andric     }
380349cc55cSDimitry Andric 
3815f757f3fSDimitry Andric     if (Opt.starts_with("-wasm-enable-sjlj")) {
382349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
383349cc55cSDimitry Andric       // '-mno-exception-handling'
384349cc55cSDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
385349cc55cSDimitry Andric                              options::OPT_mexception_handing, false))
386349cc55cSDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
387349cc55cSDimitry Andric             << "-mllvm -wasm-enable-sjlj"
388349cc55cSDimitry Andric             << "-mno-exception-handling";
389349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
390349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
391349cc55cSDimitry Andric       // because we don't allow Emscripten EH + Wasm SjLj
392349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
393349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
394349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
395349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
396349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-cxx-exceptions";
397349cc55cSDimitry Andric       }
398349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
399349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-sjlj'
400349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
401349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
402349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
403349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
404349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-sjlj";
405349cc55cSDimitry Andric       }
406349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
407349cc55cSDimitry Andric       CC1Args.push_back("-target-feature");
408349cc55cSDimitry Andric       CC1Args.push_back("+exception-handling");
409349cc55cSDimitry Andric       // Backend needs '-exception-model=wasm' to use Wasm EH instructions
410349cc55cSDimitry Andric       CC1Args.push_back("-exception-model=wasm");
411349cc55cSDimitry Andric     }
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric 
GetDefaultRuntimeLibType() const4150b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
4160b57cec5SDimitry Andric   return ToolChain::RLT_CompilerRT;
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric ToolChain::CXXStdlibType
GetCXXStdlibType(const ArgList & Args) const4200b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
4210b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
4220b57cec5SDimitry Andric     StringRef Value = A->getValue();
42381ad6265SDimitry Andric     if (Value == "libc++")
42481ad6265SDimitry Andric       return ToolChain::CST_Libcxx;
42581ad6265SDimitry Andric     else if (Value == "libstdc++")
42681ad6265SDimitry Andric       return ToolChain::CST_Libstdcxx;
42781ad6265SDimitry Andric     else
4280b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
4290b57cec5SDimitry Andric           << A->getAsString(Args);
4300b57cec5SDimitry Andric   }
4310b57cec5SDimitry Andric   return ToolChain::CST_Libcxx;
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const4340b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
4350b57cec5SDimitry Andric                                             ArgStringList &CC1Args) const {
4360b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
4370b57cec5SDimitry Andric     return;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   const Driver &D = getDriver();
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
4420b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
4430b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
4440b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
4480b57cec5SDimitry Andric     return;
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   // Check for configure-time C include directories.
4510b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
4520b57cec5SDimitry Andric   if (CIncludeDirs != "") {
4530b57cec5SDimitry Andric     SmallVector<StringRef, 5> dirs;
4540b57cec5SDimitry Andric     CIncludeDirs.split(dirs, ":");
4550b57cec5SDimitry Andric     for (StringRef dir : dirs) {
4560b57cec5SDimitry Andric       StringRef Prefix =
4575ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
4580b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
4590b57cec5SDimitry Andric     }
4600b57cec5SDimitry Andric     return;
4610b57cec5SDimitry Andric   }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
4640b57cec5SDimitry Andric     const std::string MultiarchTriple =
4650b57cec5SDimitry Andric         getMultiarchTriple(D, getTriple(), D.SysRoot);
4660b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
4670b57cec5SDimitry Andric   }
4680b57cec5SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric 
AddClangCXXStdlibIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const4710b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
4720b57cec5SDimitry Andric                                                ArgStringList &CC1Args) const {
47381ad6265SDimitry Andric 
474bdd1243dSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
475bdd1243dSDimitry Andric                         options::OPT_nostdincxx))
47681ad6265SDimitry Andric     return;
47781ad6265SDimitry Andric 
47881ad6265SDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
47981ad6265SDimitry Andric   case ToolChain::CST_Libcxx:
48081ad6265SDimitry Andric     addLibCxxIncludePaths(DriverArgs, CC1Args);
48181ad6265SDimitry Andric     break;
48281ad6265SDimitry Andric   case ToolChain::CST_Libstdcxx:
48381ad6265SDimitry Andric     addLibStdCXXIncludePaths(DriverArgs, CC1Args);
48481ad6265SDimitry Andric     break;
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric 
AddCXXStdlibLibArgs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const4880b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
4890b57cec5SDimitry Andric                                       llvm::opt::ArgStringList &CmdArgs) const {
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   switch (GetCXXStdlibType(Args)) {
4920b57cec5SDimitry Andric   case ToolChain::CST_Libcxx:
4930b57cec5SDimitry Andric     CmdArgs.push_back("-lc++");
494fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
495fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
4960b57cec5SDimitry Andric     CmdArgs.push_back("-lc++abi");
4970b57cec5SDimitry Andric     break;
4980b57cec5SDimitry Andric   case ToolChain::CST_Libstdcxx:
49981ad6265SDimitry Andric     CmdArgs.push_back("-lstdc++");
50081ad6265SDimitry Andric     break;
5010b57cec5SDimitry Andric   }
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric 
getSupportedSanitizers() const5040b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const {
5050b57cec5SDimitry Andric   SanitizerMask Res = ToolChain::getSupportedSanitizers();
5060b57cec5SDimitry Andric   if (getTriple().isOSEmscripten()) {
5070b57cec5SDimitry Andric     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
5080b57cec5SDimitry Andric   }
50906c3fb27SDimitry Andric   // -fsanitize=function places two words before the function label, which are
51006c3fb27SDimitry Andric   // -unsupported.
51106c3fb27SDimitry Andric   Res &= ~SanitizerKind::Function;
5120b57cec5SDimitry Andric   return Res;
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric 
buildLinker() const5150b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const {
5160b57cec5SDimitry Andric   return new tools::wasm::Linker(*this);
5170b57cec5SDimitry Andric }
51881ad6265SDimitry Andric 
addLibCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const51981ad6265SDimitry Andric void WebAssembly::addLibCxxIncludePaths(
52081ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
52181ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
52281ad6265SDimitry Andric   const Driver &D = getDriver();
52381ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
52481ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
52581ad6265SDimitry Andric   const std::string MultiarchTriple =
52681ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
52781ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
52881ad6265SDimitry Andric 
52981ad6265SDimitry Andric   std::string Version = detectLibcxxVersion(LibPath);
53081ad6265SDimitry Andric   if (Version.empty())
53181ad6265SDimitry Andric     return;
53281ad6265SDimitry Andric 
53381ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
53481ad6265SDimitry Andric   if (IsKnownOs) {
53581ad6265SDimitry Andric     std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
53681ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
53781ad6265SDimitry Andric   }
53881ad6265SDimitry Andric 
53981ad6265SDimitry Andric   // Second add the generic one.
54081ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
54181ad6265SDimitry Andric }
54281ad6265SDimitry Andric 
addLibStdCXXIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const54381ad6265SDimitry Andric void WebAssembly::addLibStdCXXIncludePaths(
54481ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
54581ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
54681ad6265SDimitry Andric   // We cannot use GCCInstallationDetector here as the sysroot usually does
54781ad6265SDimitry Andric   // not contain a full GCC installation.
54881ad6265SDimitry Andric   // Instead, we search the given sysroot for /usr/include/xx, similar
54981ad6265SDimitry Andric   // to how we do it for libc++.
55081ad6265SDimitry Andric   const Driver &D = getDriver();
55181ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
55281ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
55381ad6265SDimitry Andric   const std::string MultiarchTriple =
55481ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
55581ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
55681ad6265SDimitry Andric 
55781ad6265SDimitry Andric   // This is similar to detectLibcxxVersion()
55881ad6265SDimitry Andric   std::string Version;
55981ad6265SDimitry Andric   {
56081ad6265SDimitry Andric     std::error_code EC;
56181ad6265SDimitry Andric     Generic_GCC::GCCVersion MaxVersion =
56281ad6265SDimitry Andric         Generic_GCC::GCCVersion::Parse("0.0.0");
56381ad6265SDimitry Andric     SmallString<128> Path(LibPath);
56481ad6265SDimitry Andric     llvm::sys::path::append(Path, "c++");
56581ad6265SDimitry Andric     for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
56681ad6265SDimitry Andric          !EC && LI != LE; LI = LI.increment(EC)) {
56781ad6265SDimitry Andric       StringRef VersionText = llvm::sys::path::filename(LI->path());
56881ad6265SDimitry Andric       if (VersionText[0] != 'v') {
56981ad6265SDimitry Andric         auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
57081ad6265SDimitry Andric         if (Version > MaxVersion)
57181ad6265SDimitry Andric           MaxVersion = Version;
57281ad6265SDimitry Andric       }
57381ad6265SDimitry Andric     }
57481ad6265SDimitry Andric     if (MaxVersion.Major > 0)
57581ad6265SDimitry Andric       Version = MaxVersion.Text;
57681ad6265SDimitry Andric   }
57781ad6265SDimitry Andric 
57881ad6265SDimitry Andric   if (Version.empty())
57981ad6265SDimitry Andric     return;
58081ad6265SDimitry Andric 
58181ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
58281ad6265SDimitry Andric   if (IsKnownOs) {
58381ad6265SDimitry Andric     std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
58481ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
58581ad6265SDimitry Andric   }
58681ad6265SDimitry Andric 
58781ad6265SDimitry Andric   // Second add the generic one.
58881ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
58981ad6265SDimitry Andric   // Third the backward one.
59081ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
59181ad6265SDimitry Andric }
592