1 //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "WebAssembly.h"
10 #include "CommonArgs.h"
11 #include "clang/Config/config.h"
12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/DriverDiagnostic.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Option/ArgList.h"
20 using namespace clang::driver;
21 using namespace clang::driver::tools;
22 using namespace clang::driver::toolchains;
23 using namespace clang;
24 using namespace llvm::opt;
26 /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
27 /// we remove the vendor field to form the multiarch triple.
28 static std::string getMultiarchTriple(const Driver &D,
29 const llvm::Triple &TargetTriple,
31 return (TargetTriple.getArchName() + "-" +
32 TargetTriple.getOSAndEnvironmentName()).str();
35 std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
36 const ToolChain &ToolChain = getToolChain();
37 if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
38 StringRef UseLinker = A->getValue();
39 if (!UseLinker.empty()) {
40 if (llvm::sys::path::is_absolute(UseLinker) &&
41 llvm::sys::fs::can_execute(UseLinker))
44 // Accept 'lld', and 'ld' as aliases for the default linker
45 if (UseLinker != "lld" && UseLinker != "ld")
46 ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
47 << A->getAsString(Args);
51 return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
54 void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
55 const InputInfo &Output,
56 const InputInfoList &Inputs,
58 const char *LinkingOutput) const {
60 const ToolChain &ToolChain = getToolChain();
61 const char *Linker = Args.MakeArgString(getLinkerPath(Args));
62 ArgStringList CmdArgs;
64 if (Args.hasArg(options::OPT_s))
65 CmdArgs.push_back("--strip-all");
67 Args.AddAllArgs(CmdArgs, options::OPT_L);
68 Args.AddAllArgs(CmdArgs, options::OPT_u);
69 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
71 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
72 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
74 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
76 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
77 if (ToolChain.ShouldLinkCXXStdlib(Args))
78 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
80 if (Args.hasArg(options::OPT_pthread)) {
81 CmdArgs.push_back("-lpthread");
82 CmdArgs.push_back("--shared-memory");
85 CmdArgs.push_back("-lc");
86 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
89 CmdArgs.push_back("-o");
90 CmdArgs.push_back(Output.getFilename());
92 C.addCommand(std::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
95 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
96 const llvm::opt::ArgList &Args)
97 : ToolChain(D, Triple, Args) {
99 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
101 getProgramPaths().push_back(getDriver().getInstalledDir());
103 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
104 // Theoretically an "unknown" OS should mean no standard libraries, however
105 // it could also mean that a custom set of libraries is in use, so just add
106 // /lib to the search path. Disable multiarch in this case, to discourage
107 // paths containing "unknown" from acquiring meanings.
108 getFilePaths().push_back(getDriver().SysRoot + "/lib");
110 const std::string MultiarchTriple =
111 getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
112 getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
116 bool WebAssembly::IsMathErrnoDefault() const { return false; }
118 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
120 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
122 bool WebAssembly::isPICDefault() const { return false; }
124 bool WebAssembly::isPIEDefault() const { return false; }
126 bool WebAssembly::isPICDefaultForced() const { return false; }
128 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
130 bool WebAssembly::hasBlocksRuntime() const { return false; }
132 // TODO: Support profiling.
133 bool WebAssembly::SupportsProfiling() const { return false; }
135 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
137 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
138 ArgStringList &CC1Args,
139 Action::OffloadKind) const {
140 if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
141 options::OPT_fno_use_init_array, true))
142 CC1Args.push_back("-fuse-init-array");
144 // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
145 if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
147 if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
149 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
152 if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
153 options::OPT_mbulk_memory, false))
154 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
156 << "-mno-bulk-memory";
157 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
158 options::OPT_mmutable_globals, false))
159 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
161 << "-mno-mutable-globals";
162 if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
164 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
167 CC1Args.push_back("-target-feature");
168 CC1Args.push_back("+atomics");
169 CC1Args.push_back("-target-feature");
170 CC1Args.push_back("+bulk-memory");
171 CC1Args.push_back("-target-feature");
172 CC1Args.push_back("+mutable-globals");
173 CC1Args.push_back("-target-feature");
174 CC1Args.push_back("+sign-ext");
177 if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
178 // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
179 if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
180 options::OPT_mexception_handing, false))
181 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
182 << "-fwasm-exceptions"
183 << "-mno-exception-handling";
184 // '-fwasm-exceptions' is not compatible with
185 // '-mllvm -enable-emscripten-cxx-exceptions'
186 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
187 if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
188 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
189 << "-fwasm-exceptions"
190 << "-mllvm -enable-emscripten-cxx-exceptions";
192 // '-fwasm-exceptions' implies exception-handling
193 CC1Args.push_back("-target-feature");
194 CC1Args.push_back("+exception-handling");
198 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
199 return ToolChain::RLT_CompilerRT;
202 ToolChain::CXXStdlibType
203 WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
204 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
205 StringRef Value = A->getValue();
206 if (Value != "libc++")
207 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
208 << A->getAsString(Args);
210 return ToolChain::CST_Libcxx;
213 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
214 ArgStringList &CC1Args) const {
215 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
218 const Driver &D = getDriver();
220 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
221 SmallString<128> P(D.ResourceDir);
222 llvm::sys::path::append(P, "include");
223 addSystemInclude(DriverArgs, CC1Args, P);
226 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
229 // Check for configure-time C include directories.
230 StringRef CIncludeDirs(C_INCLUDE_DIRS);
231 if (CIncludeDirs != "") {
232 SmallVector<StringRef, 5> dirs;
233 CIncludeDirs.split(dirs, ":");
234 for (StringRef dir : dirs) {
236 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
237 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
242 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
243 const std::string MultiarchTriple =
244 getMultiarchTriple(D, getTriple(), D.SysRoot);
245 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
247 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
250 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
251 ArgStringList &CC1Args) const {
252 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
253 !DriverArgs.hasArg(options::OPT_nostdincxx)) {
254 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
255 const std::string MultiarchTriple =
256 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
257 addSystemInclude(DriverArgs, CC1Args,
258 getDriver().SysRoot + "/include/" + MultiarchTriple +
261 addSystemInclude(DriverArgs, CC1Args,
262 getDriver().SysRoot + "/include/c++/v1");
266 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
267 llvm::opt::ArgStringList &CmdArgs) const {
269 switch (GetCXXStdlibType(Args)) {
270 case ToolChain::CST_Libcxx:
271 CmdArgs.push_back("-lc++");
272 CmdArgs.push_back("-lc++abi");
274 case ToolChain::CST_Libstdcxx:
275 llvm_unreachable("invalid stdlib name");
279 SanitizerMask WebAssembly::getSupportedSanitizers() const {
280 SanitizerMask Res = ToolChain::getSupportedSanitizers();
281 if (getTriple().isOSEmscripten()) {
282 Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
287 Tool *WebAssembly::buildLinker() const {
288 return new tools::wasm::Linker(*this);