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" 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" 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 54 void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA, 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, mutable-globals, and sign-ext 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 if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext, 163 false)) 164 getDriver().Diag(diag::err_drv_argument_not_allowed_with) 165 << "-pthread" 166 << "-mno-sign-ext"; 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"); 175 } 176 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 '-mno-reference-types' 185 if (DriverArgs.hasFlag(options::OPT_mno_reference_types, 186 options::OPT_mexception_handing, false)) 187 getDriver().Diag(diag::err_drv_argument_not_allowed_with) 188 << "-fwasm-exceptions" 189 << "-mno-reference-types"; 190 // '-fwasm-exceptions' is not compatible with 191 // '-mllvm -enable-emscripten-cxx-exceptions' 192 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) { 193 if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") 194 getDriver().Diag(diag::err_drv_argument_not_allowed_with) 195 << "-fwasm-exceptions" 196 << "-mllvm -enable-emscripten-cxx-exceptions"; 197 } 198 // '-fwasm-exceptions' implies exception-handling and reference-types 199 CC1Args.push_back("-target-feature"); 200 CC1Args.push_back("+exception-handling"); 201 CC1Args.push_back("-target-feature"); 202 CC1Args.push_back("+reference-types"); 203 } 204 } 205 206 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const { 207 return ToolChain::RLT_CompilerRT; 208 } 209 210 ToolChain::CXXStdlibType 211 WebAssembly::GetCXXStdlibType(const ArgList &Args) const { 212 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 213 StringRef Value = A->getValue(); 214 if (Value != "libc++") 215 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 216 << A->getAsString(Args); 217 } 218 return ToolChain::CST_Libcxx; 219 } 220 221 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 222 ArgStringList &CC1Args) const { 223 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 224 return; 225 226 const Driver &D = getDriver(); 227 228 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 229 SmallString<128> P(D.ResourceDir); 230 llvm::sys::path::append(P, "include"); 231 addSystemInclude(DriverArgs, CC1Args, P); 232 } 233 234 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 235 return; 236 237 // Check for configure-time C include directories. 238 StringRef CIncludeDirs(C_INCLUDE_DIRS); 239 if (CIncludeDirs != "") { 240 SmallVector<StringRef, 5> dirs; 241 CIncludeDirs.split(dirs, ":"); 242 for (StringRef dir : dirs) { 243 StringRef Prefix = 244 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; 245 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 246 } 247 return; 248 } 249 250 if (getTriple().getOS() != llvm::Triple::UnknownOS) { 251 const std::string MultiarchTriple = 252 getMultiarchTriple(D, getTriple(), D.SysRoot); 253 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple); 254 } 255 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); 256 } 257 258 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 259 ArgStringList &CC1Args) const { 260 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) && 261 !DriverArgs.hasArg(options::OPT_nostdincxx)) { 262 if (getTriple().getOS() != llvm::Triple::UnknownOS) { 263 const std::string MultiarchTriple = 264 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 265 addSystemInclude(DriverArgs, CC1Args, 266 getDriver().SysRoot + "/include/" + MultiarchTriple + 267 "/c++/v1"); 268 } 269 addSystemInclude(DriverArgs, CC1Args, 270 getDriver().SysRoot + "/include/c++/v1"); 271 } 272 } 273 274 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 275 llvm::opt::ArgStringList &CmdArgs) const { 276 277 switch (GetCXXStdlibType(Args)) { 278 case ToolChain::CST_Libcxx: 279 CmdArgs.push_back("-lc++"); 280 CmdArgs.push_back("-lc++abi"); 281 break; 282 case ToolChain::CST_Libstdcxx: 283 llvm_unreachable("invalid stdlib name"); 284 } 285 } 286 287 SanitizerMask WebAssembly::getSupportedSanitizers() const { 288 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 289 if (getTriple().isOSEmscripten()) { 290 Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address; 291 } 292 return Res; 293 } 294 295 Tool *WebAssembly::buildLinker() const { 296 return new tools::wasm::Linker(*this); 297 } 298