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. 29*fe6060f1SDimitry Andric std::string WebAssembly::getMultiarchTriple(const Driver &D, 300b57cec5SDimitry Andric const llvm::Triple &TargetTriple, 31*fe6060f1SDimitry 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"); 665ffd83dbSDimitry Andric if (getToolChain().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"; 795ffd83dbSDimitry Andric const char *Entry = NULL; 80*fe6060f1SDimitry Andric 81*fe6060f1SDimitry Andric // If crt1-command.o exists, it supports new-style commands, so use it. 82*fe6060f1SDimitry Andric // Otherwise, use the old crt1.o. This is a temporary transition measure. 83*fe6060f1SDimitry Andric // Once WASI libc no longer needs to support LLVM versions which lack 84*fe6060f1SDimitry Andric // support for new-style command, it can make crt1.o the same as 85*fe6060f1SDimitry Andric // crt1-command.o. And once LLVM no longer needs to support WASI libc 86*fe6060f1SDimitry Andric // versions before that, it can switch to using crt1-command.o. 87*fe6060f1SDimitry Andric if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o") 88*fe6060f1SDimitry Andric Crt1 = "crt1-command.o"; 89*fe6060f1SDimitry 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)) { 133480093f4SDimitry Andric auto WasmOptPath = getToolChain().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 2040b57cec5SDimitry Andric bool WebAssembly::isPIEDefault() const { return false; } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric // TODO: Support profiling. 2130b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs, 2180b57cec5SDimitry Andric ArgStringList &CC1Args, 2190b57cec5SDimitry Andric Action::OffloadKind) const { 220480093f4SDimitry Andric if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array, 2210b57cec5SDimitry Andric options::OPT_fno_use_init_array, true)) 222480093f4SDimitry Andric CC1Args.push_back("-fno-use-init-array"); 2230b57cec5SDimitry Andric 224a7dea167SDimitry Andric // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext 2250b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, 2260b57cec5SDimitry Andric false)) { 2270b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics, 2280b57cec5SDimitry Andric false)) 2290b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2300b57cec5SDimitry Andric << "-pthread" 2310b57cec5SDimitry Andric << "-mno-atomics"; 2320b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory, 2330b57cec5SDimitry Andric options::OPT_mbulk_memory, false)) 2340b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2350b57cec5SDimitry Andric << "-pthread" 2360b57cec5SDimitry Andric << "-mno-bulk-memory"; 2370b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 2380b57cec5SDimitry Andric options::OPT_mmutable_globals, false)) 2390b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 2400b57cec5SDimitry Andric << "-pthread" 2410b57cec5SDimitry Andric << "-mno-mutable-globals"; 242a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext, 243a7dea167SDimitry Andric false)) 244a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 245a7dea167SDimitry Andric << "-pthread" 246a7dea167SDimitry Andric << "-mno-sign-ext"; 2470b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2480b57cec5SDimitry Andric CC1Args.push_back("+atomics"); 2490b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2500b57cec5SDimitry Andric CC1Args.push_back("+bulk-memory"); 2510b57cec5SDimitry Andric CC1Args.push_back("-target-feature"); 2520b57cec5SDimitry Andric CC1Args.push_back("+mutable-globals"); 253a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 254a7dea167SDimitry Andric CC1Args.push_back("+sign-ext"); 255a7dea167SDimitry Andric } 256a7dea167SDimitry Andric 257e8d8bef9SDimitry Andric if (!DriverArgs.hasFlag(options::OPT_mmutable_globals, 258e8d8bef9SDimitry Andric options::OPT_mno_mutable_globals, false)) { 259e8d8bef9SDimitry Andric // -fPIC implies +mutable-globals because the PIC ABI used by the linker 260e8d8bef9SDimitry Andric // depends on importing and exporting mutable globals. 261e8d8bef9SDimitry Andric llvm::Reloc::Model RelocationModel; 262e8d8bef9SDimitry Andric unsigned PICLevel; 263e8d8bef9SDimitry Andric bool IsPIE; 264e8d8bef9SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 265e8d8bef9SDimitry Andric ParsePICArgs(*this, DriverArgs); 266e8d8bef9SDimitry Andric if (RelocationModel == llvm::Reloc::PIC_) { 267e8d8bef9SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals, 268e8d8bef9SDimitry Andric options::OPT_mmutable_globals, false)) { 269e8d8bef9SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 270e8d8bef9SDimitry Andric << "-fPIC" 271e8d8bef9SDimitry Andric << "-mno-mutable-globals"; 272e8d8bef9SDimitry Andric } 273e8d8bef9SDimitry Andric CC1Args.push_back("-target-feature"); 274e8d8bef9SDimitry Andric CC1Args.push_back("+mutable-globals"); 275e8d8bef9SDimitry Andric } 276e8d8bef9SDimitry Andric } 277e8d8bef9SDimitry Andric 278a7dea167SDimitry Andric if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) { 279a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with '-mno-exception-handling' 280a7dea167SDimitry Andric if (DriverArgs.hasFlag(options::OPT_mno_exception_handing, 281a7dea167SDimitry Andric options::OPT_mexception_handing, false)) 282a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 283a7dea167SDimitry Andric << "-fwasm-exceptions" 284a7dea167SDimitry Andric << "-mno-exception-handling"; 285a7dea167SDimitry Andric // '-fwasm-exceptions' is not compatible with 286a7dea167SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 287a7dea167SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 288a7dea167SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 289a7dea167SDimitry Andric getDriver().Diag(diag::err_drv_argument_not_allowed_with) 290a7dea167SDimitry Andric << "-fwasm-exceptions" 291a7dea167SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 292a7dea167SDimitry Andric } 293*fe6060f1SDimitry Andric // '-fwasm-exceptions' implies exception-handling feature 294a7dea167SDimitry Andric CC1Args.push_back("-target-feature"); 295a7dea167SDimitry Andric CC1Args.push_back("+exception-handling"); 296*fe6060f1SDimitry Andric } 297*fe6060f1SDimitry Andric 298*fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 299*fe6060f1SDimitry Andric StringRef Opt = A->getValue(0); 300*fe6060f1SDimitry Andric if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) { 301*fe6060f1SDimitry Andric // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with 302*fe6060f1SDimitry Andric // '-mllvm -enable-emscripten-cxx-exceptions' 303*fe6060f1SDimitry Andric bool EmExceptionArgExists = false; 304*fe6060f1SDimitry Andric for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 305*fe6060f1SDimitry Andric if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") { 306*fe6060f1SDimitry Andric EmExceptionArgExists = true; 307*fe6060f1SDimitry Andric break; 308*fe6060f1SDimitry Andric } 309*fe6060f1SDimitry Andric } 310*fe6060f1SDimitry Andric if (!EmExceptionArgExists) 311*fe6060f1SDimitry Andric getDriver().Diag(diag::err_drv_argument_only_allowed_with) 312*fe6060f1SDimitry Andric << "-mllvm -emscripten-cxx-exceptions-allowed" 313*fe6060f1SDimitry Andric << "-mllvm -enable-emscripten-cxx-exceptions"; 314*fe6060f1SDimitry Andric 315*fe6060f1SDimitry Andric // Prevent functions specified in -emscripten-cxx-exceptions-allowed list 316*fe6060f1SDimitry Andric // from being inlined before reaching the wasm backend. 317*fe6060f1SDimitry Andric StringRef FuncNamesStr = Opt.split('=').second; 318*fe6060f1SDimitry Andric SmallVector<StringRef, 4> FuncNames; 319*fe6060f1SDimitry Andric FuncNamesStr.split(FuncNames, ','); 320*fe6060f1SDimitry Andric for (auto Name : FuncNames) { 321*fe6060f1SDimitry Andric CC1Args.push_back("-mllvm"); 322*fe6060f1SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name + 323*fe6060f1SDimitry Andric ":noinline")); 324*fe6060f1SDimitry Andric } 325*fe6060f1SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const { 3300b57cec5SDimitry Andric return ToolChain::RLT_CompilerRT; 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric ToolChain::CXXStdlibType 3340b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const { 3350b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 3360b57cec5SDimitry Andric StringRef Value = A->getValue(); 3370b57cec5SDimitry Andric if (Value != "libc++") 3380b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_invalid_stdlib_name) 3390b57cec5SDimitry Andric << A->getAsString(Args); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric return ToolChain::CST_Libcxx; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 3450b57cec5SDimitry Andric ArgStringList &CC1Args) const { 3460b57cec5SDimitry Andric if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 3470b57cec5SDimitry Andric return; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric const Driver &D = getDriver(); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 3520b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 3530b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 3540b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, P); 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 3580b57cec5SDimitry Andric return; 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric // Check for configure-time C include directories. 3610b57cec5SDimitry Andric StringRef CIncludeDirs(C_INCLUDE_DIRS); 3620b57cec5SDimitry Andric if (CIncludeDirs != "") { 3630b57cec5SDimitry Andric SmallVector<StringRef, 5> dirs; 3640b57cec5SDimitry Andric CIncludeDirs.split(dirs, ":"); 3650b57cec5SDimitry Andric for (StringRef dir : dirs) { 3660b57cec5SDimitry Andric StringRef Prefix = 3675ffd83dbSDimitry Andric llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 3680b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric return; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric if (getTriple().getOS() != llvm::Triple::UnknownOS) { 3740b57cec5SDimitry Andric const std::string MultiarchTriple = 3750b57cec5SDimitry Andric getMultiarchTriple(D, getTriple(), D.SysRoot); 3760b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 3820b57cec5SDimitry Andric ArgStringList &CC1Args) const { 3830b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nostdlibinc) && 3840b57cec5SDimitry Andric !DriverArgs.hasArg(options::OPT_nostdincxx)) { 3850b57cec5SDimitry Andric if (getTriple().getOS() != llvm::Triple::UnknownOS) { 3860b57cec5SDimitry Andric const std::string MultiarchTriple = 3870b57cec5SDimitry Andric getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 3880b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, 3890b57cec5SDimitry Andric getDriver().SysRoot + "/include/" + MultiarchTriple + 3900b57cec5SDimitry Andric "/c++/v1"); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, 3930b57cec5SDimitry Andric getDriver().SysRoot + "/include/c++/v1"); 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 3980b57cec5SDimitry Andric llvm::opt::ArgStringList &CmdArgs) const { 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric switch (GetCXXStdlibType(Args)) { 4010b57cec5SDimitry Andric case ToolChain::CST_Libcxx: 4020b57cec5SDimitry Andric CmdArgs.push_back("-lc++"); 4030b57cec5SDimitry Andric CmdArgs.push_back("-lc++abi"); 4040b57cec5SDimitry Andric break; 4050b57cec5SDimitry Andric case ToolChain::CST_Libstdcxx: 4060b57cec5SDimitry Andric llvm_unreachable("invalid stdlib name"); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const { 4110b57cec5SDimitry Andric SanitizerMask Res = ToolChain::getSupportedSanitizers(); 4120b57cec5SDimitry Andric if (getTriple().isOSEmscripten()) { 4130b57cec5SDimitry Andric Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address; 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric return Res; 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const { 4190b57cec5SDimitry Andric return new tools::wasm::Linker(*this); 4200b57cec5SDimitry Andric } 421