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 470b57cec5SDimitry Andric // Accept 'lld', and 'ld' as aliases for the default linker 480b57cec5SDimitry Andric if (UseLinker != "lld" && UseLinker != "ld") 490b57cec5SDimitry Andric ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name) 500b57cec5SDimitry Andric << A->getAsString(Args); 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric return ToolChain.GetProgramPath(ToolChain.getDefaultLinker()); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA, 580b57cec5SDimitry Andric const InputInfo &Output, 590b57cec5SDimitry Andric const InputInfoList &Inputs, 600b57cec5SDimitry Andric const ArgList &Args, 610b57cec5SDimitry Andric const char *LinkingOutput) const { 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric const ToolChain &ToolChain = getToolChain(); 640b57cec5SDimitry Andric const char *Linker = Args.MakeArgString(getLinkerPath(Args)); 650b57cec5SDimitry Andric ArgStringList CmdArgs; 660b57cec5SDimitry Andric 675ffd83dbSDimitry Andric CmdArgs.push_back("-m"); 68349cc55cSDimitry Andric if (ToolChain.getTriple().isArch64Bit()) 695ffd83dbSDimitry Andric CmdArgs.push_back("wasm64"); 705ffd83dbSDimitry Andric else 715ffd83dbSDimitry Andric CmdArgs.push_back("wasm32"); 725ffd83dbSDimitry Andric 730b57cec5SDimitry Andric if (Args.hasArg(options::OPT_s)) 740b57cec5SDimitry Andric CmdArgs.push_back("--strip-all"); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_L); 770b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_u); 780b57cec5SDimitry Andric ToolChain.AddFilePathLibArgs(Args, CmdArgs); 790b57cec5SDimitry Andric 80*5c16e71dSDimitry Andric bool IsCommand = true; 81*5c16e71dSDimitry Andric const char *Crt1; 8204eeddc0SDimitry Andric const char *Entry = nullptr; 83fe6060f1SDimitry Andric 84*5c16e71dSDimitry Andric // When -shared is specified, use the reactor exec model unless 85*5c16e71dSDimitry Andric // specified otherwise. 86*5c16e71dSDimitry Andric if (Args.hasArg(options::OPT_shared)) 87*5c16e71dSDimitry Andric IsCommand = false; 88*5c16e71dSDimitry Andric 89*5c16e71dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) { 90*5c16e71dSDimitry Andric StringRef CM = A->getValue(); 91*5c16e71dSDimitry Andric if (CM == "command") { 92*5c16e71dSDimitry Andric IsCommand = true; 93*5c16e71dSDimitry Andric } else if (CM == "reactor") { 94*5c16e71dSDimitry Andric IsCommand = false; 95*5c16e71dSDimitry Andric } else { 96*5c16e71dSDimitry Andric ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option) 97*5c16e71dSDimitry Andric << CM << A->getOption().getName(); 98*5c16e71dSDimitry Andric } 99*5c16e71dSDimitry Andric } 100*5c16e71dSDimitry Andric 101*5c16e71dSDimitry Andric if (IsCommand) { 102fe6060f1SDimitry Andric // If crt1-command.o exists, it supports new-style commands, so use it. 103fe6060f1SDimitry Andric // Otherwise, use the old crt1.o. This is a temporary transition measure. 104fe6060f1SDimitry Andric // Once WASI libc no longer needs to support LLVM versions which lack 105fe6060f1SDimitry Andric // support for new-style command, it can make crt1.o the same as 106fe6060f1SDimitry Andric // crt1-command.o. And once LLVM no longer needs to support WASI libc 107fe6060f1SDimitry Andric // versions before that, it can switch to using crt1-command.o. 108*5c16e71dSDimitry Andric Crt1 = "crt1.o"; 109fe6060f1SDimitry Andric if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o") 110fe6060f1SDimitry Andric Crt1 = "crt1-command.o"; 111*5c16e71dSDimitry Andric } else { 1125ffd83dbSDimitry Andric Crt1 = "crt1-reactor.o"; 1135ffd83dbSDimitry Andric Entry = "_initialize"; 1145ffd83dbSDimitry Andric } 115*5c16e71dSDimitry Andric 116*5c16e71dSDimitry Andric if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) 1175ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1))); 1185ffd83dbSDimitry Andric if (Entry) { 1195ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--entry")); 1205ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Entry)); 1215ffd83dbSDimitry Andric } 1220b57cec5SDimitry Andric 12306c3fb27SDimitry Andric if (Args.hasArg(options::OPT_shared)) 12406c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-shared")); 12506c3fb27SDimitry Andric 1260b57cec5SDimitry Andric AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 1290b57cec5SDimitry Andric if (ToolChain.ShouldLinkCXXStdlib(Args)) 1300b57cec5SDimitry Andric ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric if (Args.hasArg(options::OPT_pthread)) { 1330b57cec5SDimitry Andric CmdArgs.push_back("-lpthread"); 1340b57cec5SDimitry Andric CmdArgs.push_back("--shared-memory"); 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric CmdArgs.push_back("-lc"); 1380b57cec5SDimitry Andric AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args); 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric CmdArgs.push_back("-o"); 1420b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 1430b57cec5SDimitry Andric 144e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 145e8d8bef9SDimitry Andric ResponseFileSupport::AtFileCurCP(), 146e8d8bef9SDimitry Andric Linker, CmdArgs, Inputs, Output)); 147480093f4SDimitry Andric 148480093f4SDimitry Andric // When optimizing, if wasm-opt is available, run it. 149480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 150349cc55cSDimitry Andric auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt"); 151480093f4SDimitry Andric if (WasmOptPath != "wasm-opt") { 152480093f4SDimitry Andric StringRef OOpt = "s"; 153480093f4SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 154480093f4SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 155480093f4SDimitry Andric OOpt = "4"; 156480093f4SDimitry Andric else if (A->getOption().matches(options::OPT_O0)) 157480093f4SDimitry Andric OOpt = "0"; 158480093f4SDimitry Andric else if (A->getOption().matches(options::OPT_O)) 159480093f4SDimitry Andric OOpt = A->getValue(); 160480093f4SDimitry Andric 161480093f4SDimitry Andric if (OOpt != "0") { 162480093f4SDimitry Andric const char *WasmOpt = Args.MakeArgString(WasmOptPath); 163480093f4SDimitry Andric ArgStringList CmdArgs; 164480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 165480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt)); 166480093f4SDimitry Andric CmdArgs.push_back("-o"); 167480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 1685ffd83dbSDimitry Andric C.addCommand(std::make_unique<Command>( 1695ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs, 170e8d8bef9SDimitry Andric Inputs, Output)); 171480093f4SDimitry Andric } 172480093f4SDimitry Andric } 173480093f4SDimitry Andric } 174480093f4SDimitry Andric } 175480093f4SDimitry Andric 176480093f4SDimitry Andric /// Given a base library directory, append path components to form the 177480093f4SDimitry Andric /// LTO directory. 178480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) { 179480093f4SDimitry Andric // The version allows the path to be keyed to the specific version of 180480093f4SDimitry Andric // LLVM in used, as the bitcode format is not stable. 181480093f4SDimitry Andric return Dir + "/llvm-lto/" LLVM_VERSION_STRING; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple, 1850b57cec5SDimitry Andric const llvm::opt::ArgList &Args) 1860b57cec5SDimitry Andric : ToolChain(D, Triple, Args) { 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric assert(Triple.isArch32Bit() != Triple.isArch64Bit()); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric getProgramPaths().push_back(getDriver().getInstalledDir()); 1910b57cec5SDimitry Andric 192480093f4SDimitry Andric auto SysRoot = getDriver().SysRoot; 1930b57cec5SDimitry Andric if (getTriple().getOS() == llvm::Triple::UnknownOS) { 1940b57cec5SDimitry Andric // Theoretically an "unknown" OS should mean no standard libraries, however 1950b57cec5SDimitry Andric // it could also mean that a custom set of libraries is in use, so just add 1960b57cec5SDimitry Andric // /lib to the search path. Disable multiarch in this case, to discourage 1970b57cec5SDimitry Andric // paths containing "unknown" from acquiring meanings. 198480093f4SDimitry Andric getFilePaths().push_back(SysRoot + "/lib"); 1990b57cec5SDimitry Andric } else { 2000b57cec5SDimitry Andric const std::string MultiarchTriple = 201480093f4SDimitry Andric getMultiarchTriple(getDriver(), Triple, SysRoot); 202480093f4SDimitry Andric if (D.isUsingLTO()) { 203480093f4SDimitry Andric // For LTO, enable use of lto-enabled sysroot libraries too, if available. 204480093f4SDimitry Andric // Note that the directory is keyed to the LLVM revision, as LLVM's 205480093f4SDimitry Andric // bitcode format is not stable. 206480093f4SDimitry Andric auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple); 207480093f4SDimitry Andric getFilePaths().push_back(Dir); 208480093f4SDimitry Andric } 209480093f4SDimitry Andric getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; } 2200b57cec5SDimitry Andric 221349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const { 222349cc55cSDimitry Andric return false; 223349cc55cSDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric // TODO: Support profiling. 2300b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs, 2350b57cec5SDimitry Andric ArgStringList &CC1Args, 2360b57cec5SDimitry Andric Action::OffloadKind) const { 237480093f4SDimitry Andric if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array, 2380b57cec5SDimitry Andric options::OPT_fno_use_init_array, true)) 239480093f4SDimitry Andric CC1Args.push_back("-fno-use-init-array"); 2400b57cec5SDimitry Andric 241a7dea167SDimitry Andric // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext 2420b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, 2430b57cec5SDimitry Andric false)) { 2440b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics, 2450b57cec5SDimitry Andric false)) 2460b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2470b57cec5SDimitry Andric << "-pthread" 2480b57cec5SDimitry Andric << "-mno-atomics"; 2490b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory, 2500b57cec5SDimitry Andric options::OPT_mbulk_memory, false)) 2510b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2520b57cec5SDimitry Andric << "-pthread" 2530b57cec5SDimitry Andric << "-mno-bulk-memory"; 2540b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 2550b57cec5SDimitry Andric options::OPT_mmutable_globals, false)) 2560b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2570b57cec5SDimitry Andric << "-pthread" 2580b57cec5SDimitry Andric << "-mno-mutable-globals"; 259a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext, 260a7dea167SDimitry Andric false)) 261a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 262a7dea167SDimitry Andric << "-pthread" 263a7dea167SDimitry Andric << "-mno-sign-ext"; 2640b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2650b57cec5SDimitry Andric CC1Args.push_back("+atomics"); 2660b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2670b57cec5SDimitry Andric CC1Args.push_back("+bulk-memory"); 2680b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2690b57cec5SDimitry Andric CC1Args.push_back("+mutable-globals"); 270a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 271a7dea167SDimitry Andric CC1Args.push_back("+sign-ext"); 272a7dea167SDimitry Andric } 273a7dea167SDimitry Andric 274e8d8bef9SDimitry Andric if (!DriverArgs.hasFlag(options::OPT_mmutable_globals, 275e8d8bef9SDimitry Andric options::OPT_mno_mutable_globals, false)) { 276e8d8bef9SDimitry Andric // -fPIC implies +mutable-globals because the PIC ABI used by the linker 277e8d8bef9SDimitry Andric // depends on importing and exporting mutable globals. 278e8d8bef9SDimitry Andric llvm::Reloc::Model RelocationModel; 279e8d8bef9SDimitry Andric unsigned PICLevel; 280e8d8bef9SDimitry Andric bool IsPIE; 281e8d8bef9SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 282e8d8bef9SDimitry Andric ParsePICArgs(*this, DriverArgs); 283e8d8bef9SDimitry Andric if (RelocationModel == llvm::Reloc::PIC_) { 284e8d8bef9SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 285e8d8bef9SDimitry Andric options::OPT_mmutable_globals, false)) { 286e8d8bef9SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 287e8d8bef9SDimitry Andric << "-fPIC" 288e8d8bef9SDimitry Andric << "-mno-mutable-globals"; 289e8d8bef9SDimitry Andric } 290e8d8bef9SDimitry Andric CC1Args.push_back("-target-feature"); 291e8d8bef9SDimitry Andric CC1Args.push_back("+mutable-globals"); 292e8d8bef9SDimitry Andric } 293e8d8bef9SDimitry Andric } 294e8d8bef9SDimitry Andric 295a7dea167SDimitry Andric if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) { 296a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with '-mno-exception-handling' 297a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_exception_handing, 298a7dea167SDimitry Andric options::OPT_mexception_handing, false)) 299a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 300a7dea167SDimitry Andric << "-fwasm-exceptions" 301a7dea167SDimitry Andric << "-mno-exception-handling"; 302a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with 303a7dea167SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 304a7dea167SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 305a7dea167SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 306a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 307a7dea167SDimitry Andric << "-fwasm-exceptions" 308a7dea167SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 309a7dea167SDimitry Andric } 310fe6060f1SDimitry Andric // '-fwasm-exceptions' implies exception-handling feature 311a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 312a7dea167SDimitry Andric CC1Args.push_back("+exception-handling"); 313349cc55cSDimitry Andric // Backend needs -wasm-enable-eh to enable Wasm EH 314349cc55cSDimitry Andric CC1Args.push_back("-mllvm"); 315349cc55cSDimitry Andric CC1Args.push_back("-wasm-enable-eh"); 316fe6060f1SDimitry Andric } 317fe6060f1SDimitry Andric 318fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 319fe6060f1SDimitry Andric StringRef Opt = A->getValue(0); 320fe6060f1SDimitry Andric if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) { 321fe6060f1SDimitry Andric // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with 322fe6060f1SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 323349cc55cSDimitry Andric bool EmEHArgExists = false; 324fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 325fe6060f1SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") { 326349cc55cSDimitry Andric EmEHArgExists = true; 327fe6060f1SDimitry Andric break; 328fe6060f1SDimitry Andric } 329fe6060f1SDimitry Andric } 330349cc55cSDimitry Andric if (!EmEHArgExists) 331fe6060f1SDimitry Andric getDriver().Diag(diag::err_drv_argument_only_allowed_with) 332fe6060f1SDimitry Andric << "-mllvm -emscripten-cxx-exceptions-allowed" 333fe6060f1SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 334fe6060f1SDimitry Andric 335fe6060f1SDimitry Andric // Prevent functions specified in -emscripten-cxx-exceptions-allowed list 336fe6060f1SDimitry Andric // from being inlined before reaching the wasm backend. 337fe6060f1SDimitry Andric StringRef FuncNamesStr = Opt.split('=').second; 338fe6060f1SDimitry Andric SmallVector<StringRef, 4> FuncNames; 339fe6060f1SDimitry Andric FuncNamesStr.split(FuncNames, ','); 340fe6060f1SDimitry Andric for (auto Name : FuncNames) { 341fe6060f1SDimitry Andric CC1Args.push_back("-mllvm"); 342fe6060f1SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name + 343fe6060f1SDimitry Andric ":noinline")); 344fe6060f1SDimitry Andric } 345fe6060f1SDimitry Andric } 346349cc55cSDimitry Andric 347349cc55cSDimitry Andric if (Opt.startswith("-wasm-enable-sjlj")) { 348349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 349349cc55cSDimitry Andric // '-mno-exception-handling' 350349cc55cSDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_exception_handing, 351349cc55cSDimitry Andric options::OPT_mexception_handing, false)) 352349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 353349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 354349cc55cSDimitry Andric << "-mno-exception-handling"; 355349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 356349cc55cSDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 357349cc55cSDimitry Andric // because we don't allow Emscripten EH + Wasm SjLj 358349cc55cSDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 359349cc55cSDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 360349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 361349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 362349cc55cSDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 363349cc55cSDimitry Andric } 364349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 365349cc55cSDimitry Andric // '-mllvm -enable-emscripten-sjlj' 366349cc55cSDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 367349cc55cSDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj") 368349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 369349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 370349cc55cSDimitry Andric << "-mllvm -enable-emscripten-sjlj"; 371349cc55cSDimitry Andric } 372349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' implies exception-handling feature 373349cc55cSDimitry Andric CC1Args.push_back("-target-feature"); 374349cc55cSDimitry Andric CC1Args.push_back("+exception-handling"); 375349cc55cSDimitry Andric // Backend needs '-exception-model=wasm' to use Wasm EH instructions 376349cc55cSDimitry Andric CC1Args.push_back("-exception-model=wasm"); 377349cc55cSDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const { 3820b57cec5SDimitry Andric return ToolChain::RLT_CompilerRT; 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric ToolChain::CXXStdlibType 3860b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const { 3870b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 3880b57cec5SDimitry Andric StringRef Value = A->getValue(); 38981ad6265SDimitry Andric if (Value == "libc++") 39081ad6265SDimitry Andric return ToolChain::CST_Libcxx; 39181ad6265SDimitry Andric else if (Value == "libstdc++") 39281ad6265SDimitry Andric return ToolChain::CST_Libstdcxx; 39381ad6265SDimitry Andric else 3940b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_invalid_stdlib_name) 3950b57cec5SDimitry Andric << A->getAsString(Args); 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric return ToolChain::CST_Libcxx; 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 4010b57cec5SDimitry Andric ArgStringList &CC1Args) const { 4020b57cec5SDimitry Andric if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 4030b57cec5SDimitry Andric return; 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric const Driver &D = getDriver(); 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 4080b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 4090b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 4100b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, P); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 4140b57cec5SDimitry Andric return; 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric // Check for configure-time C include directories. 4170b57cec5SDimitry Andric StringRef CIncludeDirs(C_INCLUDE_DIRS); 4180b57cec5SDimitry Andric if (CIncludeDirs != "") { 4190b57cec5SDimitry Andric SmallVector<StringRef, 5> dirs; 4200b57cec5SDimitry Andric CIncludeDirs.split(dirs, ":"); 4210b57cec5SDimitry Andric for (StringRef dir : dirs) { 4220b57cec5SDimitry Andric StringRef Prefix = 4235ffd83dbSDimitry Andric llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 4240b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric return; 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric if (getTriple().getOS() != llvm::Triple::UnknownOS) { 4300b57cec5SDimitry Andric const std::string MultiarchTriple = 4310b57cec5SDimitry Andric getMultiarchTriple(D, getTriple(), D.SysRoot); 4320b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 4380b57cec5SDimitry Andric ArgStringList &CC1Args) const { 43981ad6265SDimitry Andric 440bdd1243dSDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc, 441bdd1243dSDimitry Andric options::OPT_nostdincxx)) 44281ad6265SDimitry Andric return; 44381ad6265SDimitry Andric 44481ad6265SDimitry Andric switch (GetCXXStdlibType(DriverArgs)) { 44581ad6265SDimitry Andric case ToolChain::CST_Libcxx: 44681ad6265SDimitry Andric addLibCxxIncludePaths(DriverArgs, CC1Args); 44781ad6265SDimitry Andric break; 44881ad6265SDimitry Andric case ToolChain::CST_Libstdcxx: 44981ad6265SDimitry Andric addLibStdCXXIncludePaths(DriverArgs, CC1Args); 45081ad6265SDimitry Andric break; 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 4550b57cec5SDimitry Andric llvm::opt::ArgStringList &CmdArgs) const { 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric switch (GetCXXStdlibType(Args)) { 4580b57cec5SDimitry Andric case ToolChain::CST_Libcxx: 4590b57cec5SDimitry Andric CmdArgs.push_back("-lc++"); 460fcaf7f86SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_library)) 461fcaf7f86SDimitry Andric CmdArgs.push_back("-lc++experimental"); 4620b57cec5SDimitry Andric CmdArgs.push_back("-lc++abi"); 4630b57cec5SDimitry Andric break; 4640b57cec5SDimitry Andric case ToolChain::CST_Libstdcxx: 46581ad6265SDimitry Andric CmdArgs.push_back("-lstdc++"); 46681ad6265SDimitry Andric break; 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const { 4710b57cec5SDimitry Andric SanitizerMask Res = ToolChain::getSupportedSanitizers(); 4720b57cec5SDimitry Andric if (getTriple().isOSEmscripten()) { 4730b57cec5SDimitry Andric Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address; 4740b57cec5SDimitry Andric } 47506c3fb27SDimitry Andric // -fsanitize=function places two words before the function label, which are 47606c3fb27SDimitry Andric // -unsupported. 47706c3fb27SDimitry Andric Res &= ~SanitizerKind::Function; 4780b57cec5SDimitry Andric return Res; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const { 4820b57cec5SDimitry Andric return new tools::wasm::Linker(*this); 4830b57cec5SDimitry Andric } 48481ad6265SDimitry Andric 48581ad6265SDimitry Andric void WebAssembly::addLibCxxIncludePaths( 48681ad6265SDimitry Andric const llvm::opt::ArgList &DriverArgs, 48781ad6265SDimitry Andric llvm::opt::ArgStringList &CC1Args) const { 48881ad6265SDimitry Andric const Driver &D = getDriver(); 48981ad6265SDimitry Andric std::string SysRoot = computeSysRoot(); 49081ad6265SDimitry Andric std::string LibPath = SysRoot + "/include"; 49181ad6265SDimitry Andric const std::string MultiarchTriple = 49281ad6265SDimitry Andric getMultiarchTriple(D, getTriple(), SysRoot); 49381ad6265SDimitry Andric bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS); 49481ad6265SDimitry Andric 49581ad6265SDimitry Andric std::string Version = detectLibcxxVersion(LibPath); 49681ad6265SDimitry Andric if (Version.empty()) 49781ad6265SDimitry Andric return; 49881ad6265SDimitry Andric 49981ad6265SDimitry Andric // First add the per-target include path if the OS is known. 50081ad6265SDimitry Andric if (IsKnownOs) { 50181ad6265SDimitry Andric std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version; 50281ad6265SDimitry Andric addSystemInclude(DriverArgs, CC1Args, TargetDir); 50381ad6265SDimitry Andric } 50481ad6265SDimitry Andric 50581ad6265SDimitry Andric // Second add the generic one. 50681ad6265SDimitry Andric addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version); 50781ad6265SDimitry Andric } 50881ad6265SDimitry Andric 50981ad6265SDimitry Andric void WebAssembly::addLibStdCXXIncludePaths( 51081ad6265SDimitry Andric const llvm::opt::ArgList &DriverArgs, 51181ad6265SDimitry Andric llvm::opt::ArgStringList &CC1Args) const { 51281ad6265SDimitry Andric // We cannot use GCCInstallationDetector here as the sysroot usually does 51381ad6265SDimitry Andric // not contain a full GCC installation. 51481ad6265SDimitry Andric // Instead, we search the given sysroot for /usr/include/xx, similar 51581ad6265SDimitry Andric // to how we do it for libc++. 51681ad6265SDimitry Andric const Driver &D = getDriver(); 51781ad6265SDimitry Andric std::string SysRoot = computeSysRoot(); 51881ad6265SDimitry Andric std::string LibPath = SysRoot + "/include"; 51981ad6265SDimitry Andric const std::string MultiarchTriple = 52081ad6265SDimitry Andric getMultiarchTriple(D, getTriple(), SysRoot); 52181ad6265SDimitry Andric bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS); 52281ad6265SDimitry Andric 52381ad6265SDimitry Andric // This is similar to detectLibcxxVersion() 52481ad6265SDimitry Andric std::string Version; 52581ad6265SDimitry Andric { 52681ad6265SDimitry Andric std::error_code EC; 52781ad6265SDimitry Andric Generic_GCC::GCCVersion MaxVersion = 52881ad6265SDimitry Andric Generic_GCC::GCCVersion::Parse("0.0.0"); 52981ad6265SDimitry Andric SmallString<128> Path(LibPath); 53081ad6265SDimitry Andric llvm::sys::path::append(Path, "c++"); 53181ad6265SDimitry Andric for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE; 53281ad6265SDimitry Andric !EC && LI != LE; LI = LI.increment(EC)) { 53381ad6265SDimitry Andric StringRef VersionText = llvm::sys::path::filename(LI->path()); 53481ad6265SDimitry Andric if (VersionText[0] != 'v') { 53581ad6265SDimitry Andric auto Version = Generic_GCC::GCCVersion::Parse(VersionText); 53681ad6265SDimitry Andric if (Version > MaxVersion) 53781ad6265SDimitry Andric MaxVersion = Version; 53881ad6265SDimitry Andric } 53981ad6265SDimitry Andric } 54081ad6265SDimitry Andric if (MaxVersion.Major > 0) 54181ad6265SDimitry Andric Version = MaxVersion.Text; 54281ad6265SDimitry Andric } 54381ad6265SDimitry Andric 54481ad6265SDimitry Andric if (Version.empty()) 54581ad6265SDimitry Andric return; 54681ad6265SDimitry Andric 54781ad6265SDimitry Andric // First add the per-target include path if the OS is known. 54881ad6265SDimitry Andric if (IsKnownOs) { 54981ad6265SDimitry Andric std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple; 55081ad6265SDimitry Andric addSystemInclude(DriverArgs, CC1Args, TargetDir); 55181ad6265SDimitry Andric } 55281ad6265SDimitry Andric 55381ad6265SDimitry Andric // Second add the generic one. 55481ad6265SDimitry Andric addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version); 55581ad6265SDimitry Andric // Third the backward one. 55681ad6265SDimitry Andric addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward"); 55781ad6265SDimitry Andric } 558