Home | History | Annotate | Line # | Download | only in ToolChains
WebAssembly.cpp revision 1.1.1.1
      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
    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";
    191     }
    192     // '-fwasm-exceptions' implies exception-handling
    193     CC1Args.push_back("-target-feature");
    194     CC1Args.push_back("+exception-handling");
    195   }
    196 }
    197 
    198 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
    199   return ToolChain::RLT_CompilerRT;
    200 }
    201 
    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);
    209   }
    210   return ToolChain::CST_Libcxx;
    211 }
    212 
    213 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
    214                                             ArgStringList &CC1Args) const {
    215   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
    216     return;
    217 
    218   const Driver &D = getDriver();
    219 
    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);
    224   }
    225 
    226   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
    227     return;
    228 
    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) {
    235       StringRef Prefix =
    236           llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
    237       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
    238     }
    239     return;
    240   }
    241 
    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);
    246   }
    247   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
    248 }
    249 
    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 +
    259                            "/c++/v1");
    260     }
    261     addSystemInclude(DriverArgs, CC1Args,
    262                      getDriver().SysRoot + "/include/c++/v1");
    263   }
    264 }
    265 
    266 void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
    267                                       llvm::opt::ArgStringList &CmdArgs) const {
    268 
    269   switch (GetCXXStdlibType(Args)) {
    270   case ToolChain::CST_Libcxx:
    271     CmdArgs.push_back("-lc++");
    272     CmdArgs.push_back("-lc++abi");
    273     break;
    274   case ToolChain::CST_Libstdcxx:
    275     llvm_unreachable("invalid stdlib name");
    276   }
    277 }
    278 
    279 SanitizerMask WebAssembly::getSupportedSanitizers() const {
    280   SanitizerMask Res = ToolChain::getSupportedSanitizers();
    281   if (getTriple().isOSEmscripten()) {
    282     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
    283   }
    284   return Res;
    285 }
    286 
    287 Tool *WebAssembly::buildLinker() const {
    288   return new tools::wasm::Linker(*this);
    289 }
    290