about summary refs log tree commit diff
path: root/src/librustc_binaryen
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-26 18:30:12 -0700
committerAlex Crichton <alex@alexcrichton.com>2018-03-03 20:21:35 -0800
commitd69b24805b5071e5ec9e62d2777a761723644144 (patch)
treef4b8f9d6205f7eb73ddc124457d2bc03f364f292 /src/librustc_binaryen
parent0be38e1ce3dcfe6402b3bd9617f74857fe7fa8c3 (diff)
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.

Moving to LLD brings with it a number of benefits for wasm code:

* LLD is itself an actual linker, so there's no need to compile all wasm code
  with LTO any more. As a result builds should be *much* speedier as LTO is no
  longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
  This, I believe at least, is intended to be the main supported linker for
  native code and wasm moving forward. Picking up support early on should help
  ensure that we can help LLD identify bugs and otherwise prove that it works
  great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
  and LLD (from what I can tell at least), so it's in general much better to be
  on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
  will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
  means a postprocessor is no longer needed to show off Rust's "small wasm
  binary size".

LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!

LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.

Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.

Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.

[gc]: https://reviews.llvm.org/D42511
Diffstat (limited to 'src/librustc_binaryen')
-rw-r--r--src/librustc_binaryen/BinaryenWrapper.cpp160
-rw-r--r--src/librustc_binaryen/Cargo.toml16
-rw-r--r--src/librustc_binaryen/build.rs60
-rw-r--r--src/librustc_binaryen/lib.rs172
4 files changed, 0 insertions, 408 deletions
diff --git a/src/librustc_binaryen/BinaryenWrapper.cpp b/src/librustc_binaryen/BinaryenWrapper.cpp
deleted file mode 100644
index 55f11665f6d..00000000000
--- a/src/librustc_binaryen/BinaryenWrapper.cpp
+++ /dev/null
@@ -1,160 +0,0 @@
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// This is a small C API inserted on top of the Binaryen C++ API which we use
-// from Rust. Once we have a real linker for we'll be able to remove all this,
-// and otherwise this is just all on a "as we need it" basis for now.
-
-#include <stdint.h>
-#include <string>
-#include <sstream>
-#include <stdlib.h>
-
-#include "s2wasm.h"
-#include "wasm-binary.h"
-#include "wasm-linker.h"
-
-using namespace wasm;
-
-struct BinaryenRustModule {
-  BufferWithRandomAccess buffer;
-  std::string sourceMapJSON;
-};
-
-struct BinaryenRustModuleOptions {
-  uint64_t globalBase;
-  bool debug;
-  uint64_t stackAllocation;
-  uint64_t initialMem;
-  uint64_t maxMem;
-  bool importMemory;
-  bool ignoreUnknownSymbols;
-  bool debugInfo;
-  std::string startFunction;
-  std::string sourceMapUrl;
-
-  BinaryenRustModuleOptions() :
-    globalBase(0),
-    debug(false),
-    stackAllocation(0),
-    initialMem(0),
-    maxMem(0),
-    importMemory(false),
-    ignoreUnknownSymbols(false),
-    debugInfo(false),
-    startFunction(""),
-    sourceMapUrl("")
-  {}
-
-};
-
-extern "C" BinaryenRustModuleOptions*
-BinaryenRustModuleOptionsCreate() {
-  return new BinaryenRustModuleOptions;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsFree(BinaryenRustModuleOptions *options) {
-  delete options;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsSetDebugInfo(BinaryenRustModuleOptions *options,
-                                      bool debugInfo) {
-  options->debugInfo = debugInfo;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
-                                  char *start) {
-  options->startFunction = start;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsSetSourceMapUrl(BinaryenRustModuleOptions *options,
-                                         char *sourceMapUrl) {
-  options->sourceMapUrl = sourceMapUrl;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
-                                            uint64_t stack) {
-  options->stackAllocation = stack;
-}
-
-extern "C" void
-BinaryenRustModuleOptionsSetImportMemory(BinaryenRustModuleOptions *options,
-                                         bool import) {
-  options->importMemory = import;
-}
-
-extern "C" BinaryenRustModule*
-BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
-                         const char *assembly) {
-  Linker linker(
-      options->globalBase,
-      options->stackAllocation,
-      options->initialMem,
-      options->maxMem,
-      options->importMemory,
-      options->ignoreUnknownSymbols,
-      options->startFunction,
-      options->debug);
-
-  S2WasmBuilder mainbuilder(assembly, options->debug);
-  linker.linkObject(mainbuilder);
-  linker.layout();
-
-  auto ret = make_unique<BinaryenRustModule>();
-  {
-    WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
-    writer.setNamesSection(options->debugInfo);
-
-    std::unique_ptr<std::ostringstream> sourceMapStream = nullptr;
-    {
-      sourceMapStream = make_unique<std::ostringstream>();
-      writer.setSourceMap(sourceMapStream.get(), options->sourceMapUrl);
-    }
-
-    // FIXME: support symbol maps?
-    // writer.setSymbolMap(symbolMap);
-    writer.write();
-
-    if (sourceMapStream) {
-      ret->sourceMapJSON = sourceMapStream->str();
-    }
-  }
-  return ret.release();
-}
-
-extern "C" const uint8_t*
-BinaryenRustModulePtr(const BinaryenRustModule *M) {
-  return M->buffer.data();
-}
-
-extern "C" size_t
-BinaryenRustModuleLen(const BinaryenRustModule *M) {
-  return M->buffer.size();
-}
-
-extern "C" const char*
-BinaryenRustModuleSourceMapPtr(const BinaryenRustModule *M) {
-  return M->sourceMapJSON.data();
-}
-
-extern "C" size_t
-BinaryenRustModuleSourceMapLen(const BinaryenRustModule *M) {
-  return M->sourceMapJSON.length();
-}
-
-extern "C" void
-BinaryenRustModuleFree(BinaryenRustModule *M) {
-  delete M;
-}
diff --git a/src/librustc_binaryen/Cargo.toml b/src/librustc_binaryen/Cargo.toml
deleted file mode 100644
index 9573c894714..00000000000
--- a/src/librustc_binaryen/Cargo.toml
+++ /dev/null
@@ -1,16 +0,0 @@
-# Wondering what this crate is? Take a look at the `lib.rs`!
-
-[package]
-name = "rustc_binaryen"
-version = "0.0.0"
-authors = ["The Rust Project Developers"]
-
-[lib]
-path = "lib.rs"
-
-[dependencies]
-libc = "0.2"
-
-[build-dependencies]
-cmake = "0.1"
-cc = "1.0"
diff --git a/src/librustc_binaryen/build.rs b/src/librustc_binaryen/build.rs
deleted file mode 100644
index f23ff3cee55..00000000000
--- a/src/librustc_binaryen/build.rs
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-extern crate cc;
-extern crate cmake;
-
-use std::env;
-
-use cmake::Config;
-
-fn main() {
-    let target = env::var("TARGET").unwrap();
-
-    // Bring in `__emutls_get_address` which is apparently needed for now
-    if target.contains("pc-windows-gnu") {
-        println!("cargo:rustc-link-lib=gcc_eh");
-        println!("cargo:rustc-link-lib=pthread");
-    }
-
-    Config::new("../binaryen")
-        .define("BUILD_STATIC_LIB", "ON")
-        .build_target("binaryen")
-        .build();
-
-    // I couldn't figure out how to link just one of these, so link everything.
-    println!("cargo:rustc-link-lib=static=asmjs");
-    println!("cargo:rustc-link-lib=static=binaryen");
-    println!("cargo:rustc-link-lib=static=cfg");
-    println!("cargo:rustc-link-lib=static=emscripten-optimizer");
-    println!("cargo:rustc-link-lib=static=ir");
-    println!("cargo:rustc-link-lib=static=passes");
-    println!("cargo:rustc-link-lib=static=support");
-    println!("cargo:rustc-link-lib=static=wasm");
-
-    let out_dir = env::var("OUT_DIR").unwrap();
-    println!("cargo:rustc-link-search=native={}/build/lib", out_dir);
-
-    // Add in our own little shim along with some extra files that weren't
-    // included in the main build.
-    let mut cfg = cc::Build::new();
-    cfg.file("BinaryenWrapper.cpp")
-        .file("../binaryen/src/wasm-linker.cpp")
-        .file("../binaryen/src/wasm-emscripten.cpp")
-        .include("../binaryen/src")
-        .cpp_link_stdlib(None)
-        .warnings(false)
-        .cpp(true);
-
-    if !target.contains("msvc") {
-        cfg.flag("-std=c++11");
-    }
-    cfg.compile("binaryen_wrapper");
-}
diff --git a/src/librustc_binaryen/lib.rs b/src/librustc_binaryen/lib.rs
deleted file mode 100644
index 36174e11ba0..00000000000
--- a/src/librustc_binaryen/lib.rs
+++ /dev/null
@@ -1,172 +0,0 @@
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Rustc bindings to the binaryen project.
-//!
-//! This crate is a small shim around the binaryen project which provides us the
-//! ability to take LLVM's output and generate a wasm module. Specifically this
-//! only supports one operation, creating a module from LLVM's assembly format
-//! and then serializing that module to a wasm module.
-
-extern crate libc;
-
-use std::slice;
-use std::ffi::{CString, CStr};
-
-/// In-memory representation of a serialized wasm module.
-pub struct Module {
-    ptr: *mut BinaryenRustModule,
-}
-
-impl Module {
-    /// Creates a new wasm module from the LLVM-assembly provided (in a C string
-    /// format).
-    ///
-    /// The actual module creation can be tweaked through the various options in
-    /// `ModuleOptions` as well. Any errors are just returned as a bland string.
-    pub fn new(assembly: &CStr, opts: &ModuleOptions) -> Result<Module, String> {
-        unsafe {
-            let ptr = BinaryenRustModuleCreate(opts.ptr, assembly.as_ptr());
-            if ptr.is_null() {
-                Err(format!("failed to create binaryen module"))
-            } else {
-                Ok(Module { ptr })
-            }
-        }
-    }
-
-    /// Returns the data of the serialized wasm module. This is a `foo.wasm`
-    /// file contents.
-    pub fn data(&self) -> &[u8] {
-        unsafe {
-            let ptr = BinaryenRustModulePtr(self.ptr);
-            let len = BinaryenRustModuleLen(self.ptr);
-            slice::from_raw_parts(ptr, len)
-        }
-    }
-
-    /// Returns the data of the source map JSON.
-    pub fn source_map(&self) -> &[u8] {
-        unsafe {
-            let ptr = BinaryenRustModuleSourceMapPtr(self.ptr);
-            let len = BinaryenRustModuleSourceMapLen(self.ptr);
-            slice::from_raw_parts(ptr, len)
-        }
-    }
-}
-
-impl Drop for Module {
-    fn drop(&mut self) {
-        unsafe {
-            BinaryenRustModuleFree(self.ptr);
-        }
-    }
-}
-
-pub struct ModuleOptions {
-    ptr: *mut BinaryenRustModuleOptions,
-}
-
-impl ModuleOptions {
-    pub fn new() -> ModuleOptions {
-        unsafe {
-            let ptr = BinaryenRustModuleOptionsCreate();
-            ModuleOptions { ptr }
-        }
-    }
-
-    /// Turns on or off debug info.
-    ///
-    /// From what I can tell this just creates a "names" section of the wasm
-    /// module which contains a table of the original function names.
-    pub fn debuginfo(&mut self, debug: bool) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetDebugInfo(self.ptr, debug);
-        }
-        self
-    }
-
-    /// Configures a `start` function for the module, to be executed when it's
-    /// loaded.
-    pub fn start(&mut self, func: &str) -> &mut Self {
-        let func = CString::new(func).unwrap();
-        unsafe {
-            BinaryenRustModuleOptionsSetStart(self.ptr, func.as_ptr());
-        }
-        self
-    }
-
-    /// Configures a `sourceMappingURL` custom section value for the module.
-    pub fn source_map_url(&mut self, url: &str) -> &mut Self {
-        let url = CString::new(url).unwrap();
-        unsafe {
-            BinaryenRustModuleOptionsSetSourceMapUrl(self.ptr, url.as_ptr());
-        }
-        self
-    }
-
-    /// Configures how much stack is initially allocated for the module. 1MB is
-    /// probably good enough for now.
-    pub fn stack(&mut self, amt: u64) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetStackAllocation(self.ptr, amt);
-        }
-        self
-    }
-
-    /// Flags whether the initial memory should be imported or exported. So far
-    /// we export it by default.
-    pub fn import_memory(&mut self, import: bool) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetImportMemory(self.ptr, import);
-        }
-        self
-    }
-}
-
-impl Drop for ModuleOptions {
-    fn drop(&mut self) {
-        unsafe {
-            BinaryenRustModuleOptionsFree(self.ptr);
-        }
-    }
-}
-
-enum BinaryenRustModule {}
-enum BinaryenRustModuleOptions {}
-
-extern {
-    fn BinaryenRustModuleCreate(opts: *const BinaryenRustModuleOptions,
-                                assembly: *const libc::c_char)
-        -> *mut BinaryenRustModule;
-    fn BinaryenRustModulePtr(module: *const BinaryenRustModule) -> *const u8;
-    fn BinaryenRustModuleLen(module: *const BinaryenRustModule) -> usize;
-    fn BinaryenRustModuleSourceMapPtr(module: *const BinaryenRustModule) -> *const u8;
-    fn BinaryenRustModuleSourceMapLen(module: *const BinaryenRustModule) -> usize;
-    fn BinaryenRustModuleFree(module: *mut BinaryenRustModule);
-
-    fn BinaryenRustModuleOptionsCreate()
-        -> *mut BinaryenRustModuleOptions;
-    fn BinaryenRustModuleOptionsSetDebugInfo(module: *mut BinaryenRustModuleOptions,
-                                             debuginfo: bool);
-    fn BinaryenRustModuleOptionsSetStart(module: *mut BinaryenRustModuleOptions,
-                                         start: *const libc::c_char);
-    fn BinaryenRustModuleOptionsSetSourceMapUrl(module: *mut BinaryenRustModuleOptions,
-                                                sourceMapUrl: *const libc::c_char);
-    fn BinaryenRustModuleOptionsSetStackAllocation(
-        module: *mut BinaryenRustModuleOptions,
-        stack: u64,
-    );
-    fn BinaryenRustModuleOptionsSetImportMemory(
-        module: *mut BinaryenRustModuleOptions,
-        import: bool,
-    );
-    fn BinaryenRustModuleOptionsFree(module: *mut BinaryenRustModuleOptions);
-}