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" 11480093f4SDimitry Andric #include "clang/Basic/Version.h" 120b57cec5SDimitry Andric #include "clang/Config/config.h" 130b57cec5SDimitry Andric #include "clang/Driver/Compilation.h" 140b57cec5SDimitry Andric #include "clang/Driver/Driver.h" 150b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 160b57cec5SDimitry Andric #include "clang/Driver/Options.h" 170b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 180b57cec5SDimitry Andric #include "llvm/Support/Path.h" 190b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace clang::driver; 220b57cec5SDimitry Andric using namespace clang::driver::tools; 230b57cec5SDimitry Andric using namespace clang::driver::toolchains; 240b57cec5SDimitry Andric using namespace clang; 250b57cec5SDimitry Andric using namespace llvm::opt; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples, 280b57cec5SDimitry Andric /// we remove the vendor field to form the multiarch triple. 29fe6060f1SDimitry Andric std::string WebAssembly::getMultiarchTriple(const Driver &D, 300b57cec5SDimitry Andric const llvm::Triple &TargetTriple, 31fe6060f1SDimitry Andric StringRef SysRoot) const { 320b57cec5SDimitry Andric return (TargetTriple.getArchName() + "-" + 330b57cec5SDimitry Andric TargetTriple.getOSAndEnvironmentName()).str(); 340b57cec5SDimitry Andric } 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric std::string wasm::Linker::getLinkerPath(const ArgList &Args) const { 370b57cec5SDimitry Andric const ToolChain &ToolChain = getToolChain(); 380b57cec5SDimitry Andric if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { 390b57cec5SDimitry Andric StringRef UseLinker = A->getValue(); 400b57cec5SDimitry Andric if (!UseLinker.empty()) { 410b57cec5SDimitry Andric if (llvm::sys::path::is_absolute(UseLinker) && 420b57cec5SDimitry Andric llvm::sys::fs::can_execute(UseLinker)) 435ffd83dbSDimitry Andric return std::string(UseLinker); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // Accept 'lld', and 'ld' as aliases for the default linker 460b57cec5SDimitry Andric if (UseLinker != "lld" && UseLinker != "ld") 470b57cec5SDimitry Andric ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name) 480b57cec5SDimitry Andric << A->getAsString(Args); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric return ToolChain.GetProgramPath(ToolChain.getDefaultLinker()); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA, 560b57cec5SDimitry Andric const InputInfo &Output, 570b57cec5SDimitry Andric const InputInfoList &Inputs, 580b57cec5SDimitry Andric const ArgList &Args, 590b57cec5SDimitry Andric const char *LinkingOutput) const { 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric const ToolChain &ToolChain = getToolChain(); 620b57cec5SDimitry Andric const char *Linker = Args.MakeArgString(getLinkerPath(Args)); 630b57cec5SDimitry Andric ArgStringList CmdArgs; 640b57cec5SDimitry Andric 655ffd83dbSDimitry Andric CmdArgs.push_back("-m"); 66349cc55cSDimitry Andric if (ToolChain.getTriple().isArch64Bit()) 675ffd83dbSDimitry Andric CmdArgs.push_back("wasm64"); 685ffd83dbSDimitry Andric else 695ffd83dbSDimitry Andric CmdArgs.push_back("wasm32"); 705ffd83dbSDimitry Andric 710b57cec5SDimitry Andric if (Args.hasArg(options::OPT_s)) 720b57cec5SDimitry Andric CmdArgs.push_back("--strip-all"); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_L); 750b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_u); 760b57cec5SDimitry Andric ToolChain.AddFilePathLibArgs(Args, CmdArgs); 770b57cec5SDimitry Andric 785ffd83dbSDimitry Andric const char *Crt1 = "crt1.o"; 79*04eeddc0SDimitry Andric const char *Entry = nullptr; 80fe6060f1SDimitry Andric 81fe6060f1SDimitry Andric // If crt1-command.o exists, it supports new-style commands, so use it. 82fe6060f1SDimitry Andric // Otherwise, use the old crt1.o. This is a temporary transition measure. 83fe6060f1SDimitry Andric // Once WASI libc no longer needs to support LLVM versions which lack 84fe6060f1SDimitry Andric // support for new-style command, it can make crt1.o the same as 85fe6060f1SDimitry Andric // crt1-command.o. And once LLVM no longer needs to support WASI libc 86fe6060f1SDimitry Andric // versions before that, it can switch to using crt1-command.o. 87fe6060f1SDimitry Andric if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o") 88fe6060f1SDimitry Andric Crt1 = "crt1-command.o"; 89fe6060f1SDimitry Andric 905ffd83dbSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) { 915ffd83dbSDimitry Andric StringRef CM = A->getValue(); 925ffd83dbSDimitry Andric if (CM == "command") { 935ffd83dbSDimitry Andric // Use default values. 945ffd83dbSDimitry Andric } else if (CM == "reactor") { 955ffd83dbSDimitry Andric Crt1 = "crt1-reactor.o"; 965ffd83dbSDimitry Andric Entry = "_initialize"; 975ffd83dbSDimitry Andric } else { 985ffd83dbSDimitry Andric ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option) 995ffd83dbSDimitry Andric << CM << A->getOption().getName(); 1005ffd83dbSDimitry Andric } 1015ffd83dbSDimitry Andric } 1020b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) 1035ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1))); 1045ffd83dbSDimitry Andric if (Entry) { 1055ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--entry")); 1065ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Entry)); 1075ffd83dbSDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 1120b57cec5SDimitry Andric if (ToolChain.ShouldLinkCXXStdlib(Args)) 1130b57cec5SDimitry Andric ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric if (Args.hasArg(options::OPT_pthread)) { 1160b57cec5SDimitry Andric CmdArgs.push_back("-lpthread"); 1170b57cec5SDimitry Andric CmdArgs.push_back("--shared-memory"); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric CmdArgs.push_back("-lc"); 1210b57cec5SDimitry Andric AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric CmdArgs.push_back("-o"); 1250b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 1260b57cec5SDimitry Andric 127e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 128e8d8bef9SDimitry Andric ResponseFileSupport::AtFileCurCP(), 129e8d8bef9SDimitry Andric Linker, CmdArgs, Inputs, Output)); 130480093f4SDimitry Andric 131480093f4SDimitry Andric // When optimizing, if wasm-opt is available, run it. 132480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 133349cc55cSDimitry Andric auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt"); 134480093f4SDimitry Andric if (WasmOptPath != "wasm-opt") { 135480093f4SDimitry Andric StringRef OOpt = "s"; 136480093f4SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 137480093f4SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 138480093f4SDimitry Andric OOpt = "4"; 139480093f4SDimitry Andric else if (A->getOption().matches(options::OPT_O0)) 140480093f4SDimitry Andric OOpt = "0"; 141480093f4SDimitry Andric else if (A->getOption().matches(options::OPT_O)) 142480093f4SDimitry Andric OOpt = A->getValue(); 143480093f4SDimitry Andric 144480093f4SDimitry Andric if (OOpt != "0") { 145480093f4SDimitry Andric const char *WasmOpt = Args.MakeArgString(WasmOptPath); 146480093f4SDimitry Andric ArgStringList CmdArgs; 147480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 148480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt)); 149480093f4SDimitry Andric CmdArgs.push_back("-o"); 150480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 1515ffd83dbSDimitry Andric C.addCommand(std::make_unique<Command>( 1525ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs, 153e8d8bef9SDimitry Andric Inputs, Output)); 154480093f4SDimitry Andric } 155480093f4SDimitry Andric } 156480093f4SDimitry Andric } 157480093f4SDimitry Andric } 158480093f4SDimitry Andric 159480093f4SDimitry Andric /// Given a base library directory, append path components to form the 160480093f4SDimitry Andric /// LTO directory. 161480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) { 162480093f4SDimitry Andric // The version allows the path to be keyed to the specific version of 163480093f4SDimitry Andric // LLVM in used, as the bitcode format is not stable. 164480093f4SDimitry Andric return Dir + "/llvm-lto/" LLVM_VERSION_STRING; 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple, 1680b57cec5SDimitry Andric const llvm::opt::ArgList &Args) 1690b57cec5SDimitry Andric : ToolChain(D, Triple, Args) { 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric assert(Triple.isArch32Bit() != Triple.isArch64Bit()); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric getProgramPaths().push_back(getDriver().getInstalledDir()); 1740b57cec5SDimitry Andric 175480093f4SDimitry Andric auto SysRoot = getDriver().SysRoot; 1760b57cec5SDimitry Andric if (getTriple().getOS() == llvm::Triple::UnknownOS) { 1770b57cec5SDimitry Andric // Theoretically an "unknown" OS should mean no standard libraries, however 1780b57cec5SDimitry Andric // it could also mean that a custom set of libraries is in use, so just add 1790b57cec5SDimitry Andric // /lib to the search path. Disable multiarch in this case, to discourage 1800b57cec5SDimitry Andric // paths containing "unknown" from acquiring meanings. 181480093f4SDimitry Andric getFilePaths().push_back(SysRoot + "/lib"); 1820b57cec5SDimitry Andric } else { 1830b57cec5SDimitry Andric const std::string MultiarchTriple = 184480093f4SDimitry Andric getMultiarchTriple(getDriver(), Triple, SysRoot); 185480093f4SDimitry Andric if (D.isUsingLTO()) { 186480093f4SDimitry Andric // For LTO, enable use of lto-enabled sysroot libraries too, if available. 187480093f4SDimitry Andric // Note that the directory is keyed to the LLVM revision, as LLVM's 188480093f4SDimitry Andric // bitcode format is not stable. 189480093f4SDimitry Andric auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple); 190480093f4SDimitry Andric getFilePaths().push_back(Dir); 191480093f4SDimitry Andric } 192480093f4SDimitry Andric getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; } 2030b57cec5SDimitry Andric 204349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const { 205349cc55cSDimitry Andric return false; 206349cc55cSDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // TODO: Support profiling. 2150b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs, 2200b57cec5SDimitry Andric ArgStringList &CC1Args, 2210b57cec5SDimitry Andric Action::OffloadKind) const { 222480093f4SDimitry Andric if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array, 2230b57cec5SDimitry Andric options::OPT_fno_use_init_array, true)) 224480093f4SDimitry Andric CC1Args.push_back("-fno-use-init-array"); 2250b57cec5SDimitry Andric 226a7dea167SDimitry Andric // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext 2270b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, 2280b57cec5SDimitry Andric false)) { 2290b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics, 2300b57cec5SDimitry Andric false)) 2310b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2320b57cec5SDimitry Andric << "-pthread" 2330b57cec5SDimitry Andric << "-mno-atomics"; 2340b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory, 2350b57cec5SDimitry Andric options::OPT_mbulk_memory, false)) 2360b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2370b57cec5SDimitry Andric << "-pthread" 2380b57cec5SDimitry Andric << "-mno-bulk-memory"; 2390b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 2400b57cec5SDimitry Andric options::OPT_mmutable_globals, false)) 2410b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2420b57cec5SDimitry Andric << "-pthread" 2430b57cec5SDimitry Andric << "-mno-mutable-globals"; 244a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext, 245a7dea167SDimitry Andric false)) 246a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 247a7dea167SDimitry Andric << "-pthread" 248a7dea167SDimitry Andric << "-mno-sign-ext"; 2490b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2500b57cec5SDimitry Andric CC1Args.push_back("+atomics"); 2510b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2520b57cec5SDimitry Andric CC1Args.push_back("+bulk-memory"); 2530b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2540b57cec5SDimitry Andric CC1Args.push_back("+mutable-globals"); 255a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 256a7dea167SDimitry Andric CC1Args.push_back("+sign-ext"); 257a7dea167SDimitry Andric } 258a7dea167SDimitry Andric 259e8d8bef9SDimitry Andric if (!DriverArgs.hasFlag(options::OPT_mmutable_globals, 260e8d8bef9SDimitry Andric options::OPT_mno_mutable_globals, false)) { 261e8d8bef9SDimitry Andric // -fPIC implies +mutable-globals because the PIC ABI used by the linker 262e8d8bef9SDimitry Andric // depends on importing and exporting mutable globals. 263e8d8bef9SDimitry Andric llvm::Reloc::Model RelocationModel; 264e8d8bef9SDimitry Andric unsigned PICLevel; 265e8d8bef9SDimitry Andric bool IsPIE; 266e8d8bef9SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 267e8d8bef9SDimitry Andric ParsePICArgs(*this, DriverArgs); 268e8d8bef9SDimitry Andric if (RelocationModel == llvm::Reloc::PIC_) { 269e8d8bef9SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 270e8d8bef9SDimitry Andric options::OPT_mmutable_globals, false)) { 271e8d8bef9SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 272e8d8bef9SDimitry Andric << "-fPIC" 273e8d8bef9SDimitry Andric << "-mno-mutable-globals"; 274e8d8bef9SDimitry Andric } 275e8d8bef9SDimitry Andric CC1Args.push_back("-target-feature"); 276e8d8bef9SDimitry Andric CC1Args.push_back("+mutable-globals"); 277e8d8bef9SDimitry Andric } 278e8d8bef9SDimitry Andric } 279e8d8bef9SDimitry Andric 280a7dea167SDimitry Andric if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) { 281a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with '-mno-exception-handling' 282a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_exception_handing, 283a7dea167SDimitry Andric options::OPT_mexception_handing, false)) 284a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 285a7dea167SDimitry Andric << "-fwasm-exceptions" 286a7dea167SDimitry Andric << "-mno-exception-handling"; 287a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with 288a7dea167SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 289a7dea167SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 290a7dea167SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 291a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 292a7dea167SDimitry Andric << "-fwasm-exceptions" 293a7dea167SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 294a7dea167SDimitry Andric } 295fe6060f1SDimitry Andric // '-fwasm-exceptions' implies exception-handling feature 296a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 297a7dea167SDimitry Andric CC1Args.push_back("+exception-handling"); 298349cc55cSDimitry Andric // Backend needs -wasm-enable-eh to enable Wasm EH 299349cc55cSDimitry Andric CC1Args.push_back("-mllvm"); 300349cc55cSDimitry Andric CC1Args.push_back("-wasm-enable-eh"); 301fe6060f1SDimitry Andric } 302fe6060f1SDimitry Andric 303fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 304fe6060f1SDimitry Andric StringRef Opt = A->getValue(0); 305fe6060f1SDimitry Andric if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) { 306fe6060f1SDimitry Andric // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with 307fe6060f1SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 308349cc55cSDimitry Andric bool EmEHArgExists = false; 309fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 310fe6060f1SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") { 311349cc55cSDimitry Andric EmEHArgExists = true; 312fe6060f1SDimitry Andric break; 313fe6060f1SDimitry Andric } 314fe6060f1SDimitry Andric } 315349cc55cSDimitry Andric if (!EmEHArgExists) 316fe6060f1SDimitry Andric getDriver().Diag(diag::err_drv_argument_only_allowed_with) 317fe6060f1SDimitry Andric << "-mllvm -emscripten-cxx-exceptions-allowed" 318fe6060f1SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 319fe6060f1SDimitry Andric 320fe6060f1SDimitry Andric // Prevent functions specified in -emscripten-cxx-exceptions-allowed list 321fe6060f1SDimitry Andric // from being inlined before reaching the wasm backend. 322fe6060f1SDimitry Andric StringRef FuncNamesStr = Opt.split('=').second; 323fe6060f1SDimitry Andric SmallVector<StringRef, 4> FuncNames; 324fe6060f1SDimitry Andric FuncNamesStr.split(FuncNames, ','); 325fe6060f1SDimitry Andric for (auto Name : FuncNames) { 326fe6060f1SDimitry Andric CC1Args.push_back("-mllvm"); 327fe6060f1SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name + 328fe6060f1SDimitry Andric ":noinline")); 329fe6060f1SDimitry Andric } 330fe6060f1SDimitry Andric } 331349cc55cSDimitry Andric 332349cc55cSDimitry Andric if (Opt.startswith("-wasm-enable-sjlj")) { 333349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 334349cc55cSDimitry Andric // '-mno-exception-handling' 335349cc55cSDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_exception_handing, 336349cc55cSDimitry Andric options::OPT_mexception_handing, false)) 337349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 338349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 339349cc55cSDimitry Andric << "-mno-exception-handling"; 340349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 341349cc55cSDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 342349cc55cSDimitry Andric // because we don't allow Emscripten EH + Wasm SjLj 343349cc55cSDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 344349cc55cSDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 345349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 346349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 347349cc55cSDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 348349cc55cSDimitry Andric } 349349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' is not compatible with 350349cc55cSDimitry Andric // '-mllvm -enable-emscripten-sjlj' 351349cc55cSDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 352349cc55cSDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj") 353349cc55cSDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 354349cc55cSDimitry Andric << "-mllvm -wasm-enable-sjlj" 355349cc55cSDimitry Andric << "-mllvm -enable-emscripten-sjlj"; 356349cc55cSDimitry Andric } 357349cc55cSDimitry Andric // '-mllvm -wasm-enable-sjlj' implies exception-handling feature 358349cc55cSDimitry Andric CC1Args.push_back("-target-feature"); 359349cc55cSDimitry Andric CC1Args.push_back("+exception-handling"); 360349cc55cSDimitry Andric // Backend needs '-exception-model=wasm' to use Wasm EH instructions 361349cc55cSDimitry Andric CC1Args.push_back("-exception-model=wasm"); 362349cc55cSDimitry Andric } 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const { 3670b57cec5SDimitry Andric return ToolChain::RLT_CompilerRT; 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric ToolChain::CXXStdlibType 3710b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const { 3720b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 3730b57cec5SDimitry Andric StringRef Value = A->getValue(); 3740b57cec5SDimitry Andric if (Value != "libc++") 3750b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_invalid_stdlib_name) 3760b57cec5SDimitry Andric << A->getAsString(Args); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric return ToolChain::CST_Libcxx; 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 3820b57cec5SDimitry Andric ArgStringList &CC1Args) const { 3830b57cec5SDimitry Andric if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 3840b57cec5SDimitry Andric return; 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric const Driver &D = getDriver(); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 3890b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 3900b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 3910b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, P); 3920b57cec5SDimitry Andric } 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 3950b57cec5SDimitry Andric return; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric // Check for configure-time C include directories. 3980b57cec5SDimitry Andric StringRef CIncludeDirs(C_INCLUDE_DIRS); 3990b57cec5SDimitry Andric if (CIncludeDirs != "") { 4000b57cec5SDimitry Andric SmallVector<StringRef, 5> dirs; 4010b57cec5SDimitry Andric CIncludeDirs.split(dirs, ":"); 4020b57cec5SDimitry Andric for (StringRef dir : dirs) { 4030b57cec5SDimitry Andric StringRef Prefix = 4045ffd83dbSDimitry Andric llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 4050b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric return; 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric if (getTriple().getOS() != llvm::Triple::UnknownOS) { 4110b57cec5SDimitry Andric const std::string MultiarchTriple = 4120b57cec5SDimitry Andric getMultiarchTriple(D, getTriple(), D.SysRoot); 4130b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple); 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 4190b57cec5SDimitry Andric ArgStringList &CC1Args) const { 4200b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nostdlibinc) && 4210b57cec5SDimitry Andric !DriverArgs.hasArg(options::OPT_nostdincxx)) { 4220b57cec5SDimitry Andric if (getTriple().getOS() != llvm::Triple::UnknownOS) { 4230b57cec5SDimitry Andric const std::string MultiarchTriple = 4240b57cec5SDimitry Andric getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 4250b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, 4260b57cec5SDimitry Andric getDriver().SysRoot + "/include/" + MultiarchTriple + 4270b57cec5SDimitry Andric "/c++/v1"); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, 4300b57cec5SDimitry Andric getDriver().SysRoot + "/include/c++/v1"); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 4350b57cec5SDimitry Andric llvm::opt::ArgStringList &CmdArgs) const { 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric switch (GetCXXStdlibType(Args)) { 4380b57cec5SDimitry Andric case ToolChain::CST_Libcxx: 4390b57cec5SDimitry Andric CmdArgs.push_back("-lc++"); 4400b57cec5SDimitry Andric CmdArgs.push_back("-lc++abi"); 4410b57cec5SDimitry Andric break; 4420b57cec5SDimitry Andric case ToolChain::CST_Libstdcxx: 4430b57cec5SDimitry Andric llvm_unreachable("invalid stdlib name"); 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const { 4480b57cec5SDimitry Andric SanitizerMask Res = ToolChain::getSupportedSanitizers(); 4490b57cec5SDimitry Andric if (getTriple().isOSEmscripten()) { 4500b57cec5SDimitry Andric Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address; 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric return Res; 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const { 4560b57cec5SDimitry Andric return new tools::wasm::Linker(*this); 4570b57cec5SDimitry Andric } 458