clang  10.0.0svn
WebAssembly.cpp
Go to the documentation of this file.
1 //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "WebAssembly.h"
10 #include "CommonArgs.h"
11 #include "clang/Config/config.h"
13 #include "clang/Driver/Driver.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"
19 
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;
25 
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,
30  StringRef SysRoot) {
31  return (TargetTriple.getArchName() + "-" +
32  TargetTriple.getOSAndEnvironmentName()).str();
33 }
34 
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))
42  return UseLinker;
43 
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);
48  }
49  }
50 
51  return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
52 }
53 
55  const InputInfo &Output,
56  const InputInfoList &Inputs,
57  const ArgList &Args,
58  const char *LinkingOutput) const {
59 
60  const ToolChain &ToolChain = getToolChain();
61  const char *Linker = Args.MakeArgString(getLinkerPath(Args));
62  ArgStringList CmdArgs;
63 
64  if (Args.hasArg(options::OPT_s))
65  CmdArgs.push_back("--strip-all");
66 
67  Args.AddAllArgs(CmdArgs, options::OPT_L);
68  Args.AddAllArgs(CmdArgs, options::OPT_u);
69  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
70 
71  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
72  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
73 
74  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
75 
76  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
77  if (ToolChain.ShouldLinkCXXStdlib(Args))
78  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
79 
80  if (Args.hasArg(options::OPT_pthread)) {
81  CmdArgs.push_back("-lpthread");
82  CmdArgs.push_back("--shared-memory");
83  }
84 
85  CmdArgs.push_back("-lc");
86  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
87  }
88 
89  CmdArgs.push_back("-o");
90  CmdArgs.push_back(Output.getFilename());
91 
92  C.addCommand(std::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
93 }
94 
95 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
96  const llvm::opt::ArgList &Args)
97  : ToolChain(D, Triple, Args) {
98 
99  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
100 
101  getProgramPaths().push_back(getDriver().getInstalledDir());
102 
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");
109  } else {
110  const std::string MultiarchTriple =
111  getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
112  getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
113  }
114 }
115 
116 bool WebAssembly::IsMathErrnoDefault() const { return false; }
117 
118 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
119 
120 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
121 
122 bool WebAssembly::isPICDefault() const { return false; }
123 
124 bool WebAssembly::isPIEDefault() const { return false; }
125 
126 bool WebAssembly::isPICDefaultForced() const { return false; }
127 
128 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
129 
130 bool WebAssembly::hasBlocksRuntime() const { return false; }
131 
132 // TODO: Support profiling.
133 bool WebAssembly::SupportsProfiling() const { return false; }
134 
135 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
136 
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");
143 
144  // '-pthread' implies atomics, bulk-memory, and mutable-globals
145  if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
146  false)) {
147  if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
148  false))
149  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
150  << "-pthread"
151  << "-mno-atomics";
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)
155  << "-pthread"
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)
160  << "-pthread"
161  << "-mno-mutable-globals";
162  CC1Args.push_back("-target-feature");
163  CC1Args.push_back("+atomics");
164  CC1Args.push_back("-target-feature");
165  CC1Args.push_back("+bulk-memory");
166  CC1Args.push_back("-target-feature");
167  CC1Args.push_back("+mutable-globals");
168  }
169 
170  if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
171  // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
172  if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
173  options::OPT_mexception_handing, false))
174  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
175  << "-fwasm-exceptions"
176  << "-mno-exception-handling";
177  // '-fwasm-exceptions' is not compatible with
178  // '-mllvm -enable-emscripten-cxx-exceptions'
179  for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
180  if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
181  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
182  << "-fwasm-exceptions"
183  << "-mllvm -enable-emscripten-cxx-exceptions";
184  }
185  // '-fwasm-exceptions' implies exception-handling
186  CC1Args.push_back("-target-feature");
187  CC1Args.push_back("+exception-handling");
188  }
189 }
190 
191 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
193 }
194 
196 WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
197  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
198  StringRef Value = A->getValue();
199  if (Value != "libc++")
200  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
201  << A->getAsString(Args);
202  }
203  return ToolChain::CST_Libcxx;
204 }
205 
206 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
207  ArgStringList &CC1Args) const {
208  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
209  return;
210 
211  const Driver &D = getDriver();
212 
213  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
215  llvm::sys::path::append(P, "include");
216  addSystemInclude(DriverArgs, CC1Args, P);
217  }
218 
219  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
220  return;
221 
222  // Check for configure-time C include directories.
223  StringRef CIncludeDirs(C_INCLUDE_DIRS);
224  if (CIncludeDirs != "") {
226  CIncludeDirs.split(dirs, ":");
227  for (StringRef dir : dirs) {
228  StringRef Prefix =
229  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
230  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
231  }
232  return;
233  }
234 
235  if (getTriple().getOS() != llvm::Triple::UnknownOS) {
236  const std::string MultiarchTriple =
237  getMultiarchTriple(D, getTriple(), D.SysRoot);
238  addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
239  }
240  addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
241 }
242 
243 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
244  ArgStringList &CC1Args) const {
245  if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
246  !DriverArgs.hasArg(options::OPT_nostdincxx)) {
247  if (getTriple().getOS() != llvm::Triple::UnknownOS) {
248  const std::string MultiarchTriple =
249  getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
250  addSystemInclude(DriverArgs, CC1Args,
251  getDriver().SysRoot + "/include/" + MultiarchTriple +
252  "/c++/v1");
253  }
254  addSystemInclude(DriverArgs, CC1Args,
255  getDriver().SysRoot + "/include/c++/v1");
256  }
257 }
258 
259 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
260  llvm::opt::ArgStringList &CmdArgs) const {
261 
262  switch (GetCXXStdlibType(Args)) {
264  CmdArgs.push_back("-lc++");
265  CmdArgs.push_back("-lc++abi");
266  break;
268  llvm_unreachable("invalid stdlib name");
269  }
270 }
271 
272 SanitizerMask WebAssembly::getSupportedSanitizers() const {
274  if (getTriple().isOSEmscripten()) {
275  Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
276  }
277  return Res;
278 }
279 
280 Tool *WebAssembly::buildLinker() const {
281  return new tools::wasm::Linker(*this);
282 }
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition: ToolChain.h:367
std::string getLinkerPath(const llvm::opt::ArgList &Args) const
Definition: WebAssembly.cpp:35
StringRef P
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:108
const char * getFilename() const
Definition: InputInfo.h:83
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:484
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:868
const Driver & getDriver() const
Definition: ToolChain.h:193
static std::string getMultiarchTriple(const Driver &D, const llvm::Triple &TargetTriple, StringRef SysRoot)
Following the conventions in https://wiki.debian.org/Multiarch/Tuples, we remove the vendor field to ...
Definition: WebAssembly.cpp:28
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:845
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: WebAssembly.cpp:54
std::string SysRoot
sysroot, if present
Definition: Driver.h:147
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:904
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:488
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:851
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:131