blob: adf8eb077201c7f940999c610041fce01702b6e2 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David L. Jonesf561aba2017-03-08 01:02:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "WebAssembly.h"
10#include "CommonArgs.h"
Dan Gohman81282892019-11-20 15:25:43 -080011#include "clang/Basic/Version.h"
Sam Clegg818dd862019-06-13 09:42:43 +000012#include "clang/Config/config.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000013#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Driver.h"
Sam Cleggffbfc0f2018-01-12 17:54:49 +000015#include "clang/Driver/DriverDiagnostic.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000016#include "clang/Driver/Options.h"
Sam Cleggfc5ddee2019-03-28 17:45:18 +000017#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000019#include "llvm/Option/ArgList.h"
20
21using namespace clang::driver;
22using namespace clang::driver::tools;
23using namespace clang::driver::toolchains;
24using namespace clang;
25using namespace llvm::opt;
26
Dan Gohman055a6f02019-01-15 06:58:16 +000027/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
28/// we remove the vendor field to form the multiarch triple.
29static std::string getMultiarchTriple(const Driver &D,
30 const llvm::Triple &TargetTriple,
31 StringRef SysRoot) {
32 return (TargetTriple.getArchName() + "-" +
33 TargetTriple.getOSAndEnvironmentName()).str();
34}
35
Sam Cleggfc5ddee2019-03-28 17:45:18 +000036std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
37 const ToolChain &ToolChain = getToolChain();
38 if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
39 StringRef UseLinker = A->getValue();
40 if (!UseLinker.empty()) {
41 if (llvm::sys::path::is_absolute(UseLinker) &&
42 llvm::sys::fs::can_execute(UseLinker))
Benjamin Krameradcd0262020-01-28 20:23:46 +010043 return std::string(UseLinker);
Sam Cleggfc5ddee2019-03-28 17:45:18 +000044
45 // Accept 'lld', and 'ld' as aliases for the default linker
46 if (UseLinker != "lld" && UseLinker != "ld")
47 ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
48 << A->getAsString(Args);
49 }
50 }
51
52 return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
53}
54
David L. Jonesf561aba2017-03-08 01:02:16 +000055void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
56 const InputInfo &Output,
57 const InputInfoList &Inputs,
58 const ArgList &Args,
59 const char *LinkingOutput) const {
60
61 const ToolChain &ToolChain = getToolChain();
Sam Cleggfc5ddee2019-03-28 17:45:18 +000062 const char *Linker = Args.MakeArgString(getLinkerPath(Args));
David L. Jonesf561aba2017-03-08 01:02:16 +000063 ArgStringList CmdArgs;
David L. Jonesf561aba2017-03-08 01:02:16 +000064
David L. Jonesf561aba2017-03-08 01:02:16 +000065 if (Args.hasArg(options::OPT_s))
66 CmdArgs.push_back("--strip-all");
David L. Jonesf561aba2017-03-08 01:02:16 +000067
68 Args.AddAllArgs(CmdArgs, options::OPT_L);
Sam Cleggd09a3562017-12-02 23:11:13 +000069 Args.AddAllArgs(CmdArgs, options::OPT_u);
David L. Jonesf561aba2017-03-08 01:02:16 +000070 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
71
Sam Clegg471d7af2017-10-27 18:10:19 +000072 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
73 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
74
David L. Jonesf561aba2017-03-08 01:02:16 +000075 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
76
77 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Nico Weber0ee47d92017-07-25 18:02:57 +000078 if (ToolChain.ShouldLinkCXXStdlib(Args))
David L. Jonesf561aba2017-03-08 01:02:16 +000079 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
80
Thomas Lively80ff58e2019-03-22 22:25:37 +000081 if (Args.hasArg(options::OPT_pthread)) {
David L. Jonesf561aba2017-03-08 01:02:16 +000082 CmdArgs.push_back("-lpthread");
Thomas Lively80ff58e2019-03-22 22:25:37 +000083 CmdArgs.push_back("--shared-memory");
84 }
David L. Jonesf561aba2017-03-08 01:02:16 +000085
86 CmdArgs.push_back("-lc");
Sam Clegga08631e2017-10-27 00:26:07 +000087 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +000088 }
89
David L. Jonesf561aba2017-03-08 01:02:16 +000090 CmdArgs.push_back("-o");
91 CmdArgs.push_back(Output.getFilename());
92
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +000093 C.addCommand(std::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
Dan Gohman81282892019-11-20 15:25:43 -080094
Dan Gohman8f1e2152019-12-02 11:47:31 -080095 // When optimizing, if wasm-opt is available, run it.
Dan Gohman81282892019-11-20 15:25:43 -080096 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Dan Gohman8f1e2152019-12-02 11:47:31 -080097 auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
98 if (WasmOptPath != "wasm-opt") {
Dan Gohman81282892019-11-20 15:25:43 -080099 StringRef OOpt = "s";
100 if (A->getOption().matches(options::OPT_O4) ||
101 A->getOption().matches(options::OPT_Ofast))
102 OOpt = "4";
103 else if (A->getOption().matches(options::OPT_O0))
104 OOpt = "0";
105 else if (A->getOption().matches(options::OPT_O))
106 OOpt = A->getValue();
107
108 if (OOpt != "0") {
Dan Gohman8f1e2152019-12-02 11:47:31 -0800109 const char *WasmOpt = Args.MakeArgString(WasmOptPath);
Dan Gohman81282892019-11-20 15:25:43 -0800110 ArgStringList CmdArgs;
111 CmdArgs.push_back(Output.getFilename());
112 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
113 CmdArgs.push_back("-o");
114 CmdArgs.push_back(Output.getFilename());
115 C.addCommand(std::make_unique<Command>(JA, *this, WasmOpt, CmdArgs, Inputs));
116 }
117 }
118 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000119}
120
Dan Gohman872a53e2019-11-25 09:28:31 -0800121/// Given a base library directory, append path components to form the
122/// LTO directory.
123static std::string AppendLTOLibDir(const std::string &Dir) {
124 // The version allows the path to be keyed to the specific version of
125 // LLVM in used, as the bitcode format is not stable.
126 return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
127}
128
David L. Jonesf561aba2017-03-08 01:02:16 +0000129WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
130 const llvm::opt::ArgList &Args)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000131 : ToolChain(D, Triple, Args) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000132
133 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
Sam Clegg27ea1562017-05-09 17:47:50 +0000134
135 getProgramPaths().push_back(getDriver().getInstalledDir());
136
Dan Gohman872a53e2019-11-25 09:28:31 -0800137 auto SysRoot = getDriver().SysRoot;
Dan Gohman055a6f02019-01-15 06:58:16 +0000138 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
139 // Theoretically an "unknown" OS should mean no standard libraries, however
140 // it could also mean that a custom set of libraries is in use, so just add
141 // /lib to the search path. Disable multiarch in this case, to discourage
142 // paths containing "unknown" from acquiring meanings.
Dan Gohman872a53e2019-11-25 09:28:31 -0800143 getFilePaths().push_back(SysRoot + "/lib");
Dan Gohman055a6f02019-01-15 06:58:16 +0000144 } else {
145 const std::string MultiarchTriple =
Dan Gohman872a53e2019-11-25 09:28:31 -0800146 getMultiarchTriple(getDriver(), Triple, SysRoot);
Dan Gohman81282892019-11-20 15:25:43 -0800147 if (D.isUsingLTO()) {
Dan Gohman872a53e2019-11-25 09:28:31 -0800148 // For LTO, enable use of lto-enabled sysroot libraries too, if available.
149 // Note that the directory is keyed to the LLVM revision, as LLVM's
150 // bitcode format is not stable.
151 auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
152 getFilePaths().push_back(Dir);
Dan Gohman81282892019-11-20 15:25:43 -0800153 }
Dan Gohman872a53e2019-11-25 09:28:31 -0800154 getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
Dan Gohman055a6f02019-01-15 06:58:16 +0000155 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000156}
157
158bool WebAssembly::IsMathErrnoDefault() const { return false; }
159
160bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
161
162bool WebAssembly::UseObjCMixedDispatch() const { return true; }
163
164bool WebAssembly::isPICDefault() const { return false; }
165
166bool WebAssembly::isPIEDefault() const { return false; }
167
168bool WebAssembly::isPICDefaultForced() const { return false; }
169
170bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
171
David L. Jonesf561aba2017-03-08 01:02:16 +0000172bool WebAssembly::hasBlocksRuntime() const { return false; }
173
174// TODO: Support profiling.
175bool WebAssembly::SupportsProfiling() const { return false; }
176
177bool WebAssembly::HasNativeLLVMSupport() const { return true; }
178
179void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000180 ArgStringList &CC1Args,
181 Action::OffloadKind) const {
Fangrui Songb2b5cac2019-12-11 21:48:59 -0800182 if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
183 options::OPT_fno_use_init_array, true))
184 CC1Args.push_back("-fno-use-init-array");
Heejin Ahn9d5a0892019-02-11 22:47:50 +0000185
Thomas Lively807ceca2019-10-18 04:34:26 +0000186 // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
Thomas Livelyf3b4f992019-02-28 18:39:08 +0000187 if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
188 false)) {
189 if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
190 false))
191 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
192 << "-pthread"
193 << "-mno-atomics";
Thomas Livelydb8e3642019-07-12 18:23:13 +0000194 if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
195 options::OPT_mbulk_memory, false))
196 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
197 << "-pthread"
198 << "-mno-bulk-memory";
199 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
200 options::OPT_mmutable_globals, false))
201 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
202 << "-pthread"
203 << "-mno-mutable-globals";
Thomas Lively807ceca2019-10-18 04:34:26 +0000204 if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
205 false))
206 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
207 << "-pthread"
208 << "-mno-sign-ext";
Heejin Ahn9d5a0892019-02-11 22:47:50 +0000209 CC1Args.push_back("-target-feature");
210 CC1Args.push_back("+atomics");
Thomas Livelydb8e3642019-07-12 18:23:13 +0000211 CC1Args.push_back("-target-feature");
212 CC1Args.push_back("+bulk-memory");
213 CC1Args.push_back("-target-feature");
214 CC1Args.push_back("+mutable-globals");
Thomas Lively807ceca2019-10-18 04:34:26 +0000215 CC1Args.push_back("-target-feature");
216 CC1Args.push_back("+sign-ext");
Heejin Ahn9d5a0892019-02-11 22:47:50 +0000217 }
Heejin Ahne8b2b882019-09-12 04:01:37 +0000218
219 if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
220 // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
221 if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
222 options::OPT_mexception_handing, false))
223 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
224 << "-fwasm-exceptions"
225 << "-mno-exception-handling";
Heejin Ahn70ee4302019-11-04 16:04:15 -0800226 // '-fwasm-exceptions' is not compatible with '-mno-reference-types'
227 if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
228 options::OPT_mexception_handing, false))
229 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
230 << "-fwasm-exceptions"
231 << "-mno-reference-types";
Heejin Ahne8b2b882019-09-12 04:01:37 +0000232 // '-fwasm-exceptions' is not compatible with
233 // '-mllvm -enable-emscripten-cxx-exceptions'
234 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
235 if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
236 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
237 << "-fwasm-exceptions"
238 << "-mllvm -enable-emscripten-cxx-exceptions";
239 }
Heejin Ahn70ee4302019-11-04 16:04:15 -0800240 // '-fwasm-exceptions' implies exception-handling and reference-types
Heejin Ahne8b2b882019-09-12 04:01:37 +0000241 CC1Args.push_back("-target-feature");
242 CC1Args.push_back("+exception-handling");
Heejin Ahn70ee4302019-11-04 16:04:15 -0800243 CC1Args.push_back("-target-feature");
244 CC1Args.push_back("+reference-types");
Heejin Ahne8b2b882019-09-12 04:01:37 +0000245 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000246}
247
248ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
249 return ToolChain::RLT_CompilerRT;
250}
251
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000252ToolChain::CXXStdlibType
253WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000254 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
255 StringRef Value = A->getValue();
256 if (Value != "libc++")
257 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
258 << A->getAsString(Args);
259 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000260 return ToolChain::CST_Libcxx;
261}
262
263void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
264 ArgStringList &CC1Args) const {
Sam Clegg818dd862019-06-13 09:42:43 +0000265 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
266 return;
267
268 const Driver &D = getDriver();
269
270 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
271 SmallString<128> P(D.ResourceDir);
272 llvm::sys::path::append(P, "include");
273 addSystemInclude(DriverArgs, CC1Args, P);
Dan Gohman055a6f02019-01-15 06:58:16 +0000274 }
Sam Clegg818dd862019-06-13 09:42:43 +0000275
276 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
277 return;
278
279 // Check for configure-time C include directories.
280 StringRef CIncludeDirs(C_INCLUDE_DIRS);
281 if (CIncludeDirs != "") {
282 SmallVector<StringRef, 5> dirs;
283 CIncludeDirs.split(dirs, ":");
284 for (StringRef dir : dirs) {
285 StringRef Prefix =
286 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
287 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
288 }
289 return;
290 }
291
292 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
293 const std::string MultiarchTriple =
294 getMultiarchTriple(D, getTriple(), D.SysRoot);
295 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
296 }
297 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
David L. Jonesf561aba2017-03-08 01:02:16 +0000298}
299
300void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
301 ArgStringList &CC1Args) const {
302 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
Dan Gohman055a6f02019-01-15 06:58:16 +0000303 !DriverArgs.hasArg(options::OPT_nostdincxx)) {
304 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
305 const std::string MultiarchTriple =
306 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
307 addSystemInclude(DriverArgs, CC1Args,
Sam Clegg818dd862019-06-13 09:42:43 +0000308 getDriver().SysRoot + "/include/" + MultiarchTriple +
309 "/c++/v1");
Dan Gohman055a6f02019-01-15 06:58:16 +0000310 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000311 addSystemInclude(DriverArgs, CC1Args,
312 getDriver().SysRoot + "/include/c++/v1");
Dan Gohman055a6f02019-01-15 06:58:16 +0000313 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000314}
315
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000316void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
317 llvm::opt::ArgStringList &CmdArgs) const {
318
319 switch (GetCXXStdlibType(Args)) {
320 case ToolChain::CST_Libcxx:
321 CmdArgs.push_back("-lc++");
322 CmdArgs.push_back("-lc++abi");
323 break;
324 case ToolChain::CST_Libstdcxx:
325 llvm_unreachable("invalid stdlib name");
326 }
327}
328
Thomas Lively5458cd42019-05-29 18:31:50 +0000329SanitizerMask WebAssembly::getSupportedSanitizers() const {
330 SanitizerMask Res = ToolChain::getSupportedSanitizers();
331 if (getTriple().isOSEmscripten()) {
Guanzhong Chen9aad9972019-06-26 20:16:14 +0000332 Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
Thomas Lively5458cd42019-05-29 18:31:50 +0000333 }
334 return Res;
335}
336
David L. Jonesf561aba2017-03-08 01:02:16 +0000337Tool *WebAssembly::buildLinker() const {
338 return new tools::wasm::Linker(*this);
339}