1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use rustc_data_structures::fx::FxHashMap;
16 use serialize::leb128;
18 // https://webassembly.github.io/spec/core/binary/modules.html#binary-importsec
19 const WASM_IMPORT_SECTION_ID: u8 = 2;
21 const WASM_EXTERNAL_KIND_FUNCTION: u8 = 0;
22 const WASM_EXTERNAL_KIND_TABLE: u8 = 1;
23 const WASM_EXTERNAL_KIND_MEMORY: u8 = 2;
24 const WASM_EXTERNAL_KIND_GLOBAL: u8 = 3;
26 /// Rewrite the module imports are listed from in a wasm module given the field
27 /// name to module name mapping in `import_map`.
29 /// LLVM 6 which we're using right now doesn't have the ability to configure the
30 /// module a wasm symbol is import from. Rather all imported symbols come from
31 /// the bland `"env"` module unconditionally. Furthermore we'd *also* need
32 /// support in LLD for preserving these import modules, which it unfortunately
33 /// currently does not.
35 /// This function is intended as a hack for now where we manually rewrite the
36 /// wasm output by LLVM to have the correct import modules listed. The
37 /// `#[link(wasm_import_module = "...")]` attribute in Rust translates to the
38 /// module that each symbol is imported from, so here we manually go through the
39 /// wasm file, decode it, rewrite imports, and then rewrite the wasm module.
41 /// Support for this was added to LLVM in
42 /// https://github.com/llvm-mirror/llvm/commit/0f32e1365, although support still
43 /// needs to be added, tracked at https://bugs.llvm.org/show_bug.cgi?id=37168
44 pub fn rewrite_imports(path: &Path, import_map: &FxHashMap<String, String>) {
45 if import_map.is_empty() {
49 let wasm = fs::read(path).expect("failed to read wasm output");
50 let mut ret = WasmEncoder::new();
51 ret.data.extend(&wasm[..8]);
53 // skip the 8 byte wasm/version header
54 for (id, raw) in WasmSections(WasmDecoder::new(&wasm[8..])) {
56 if id == WASM_IMPORT_SECTION_ID {
57 info!("rewriting import section");
58 let data = rewrite_import_section(
59 &mut WasmDecoder::new(raw),
64 info!("carry forward section {}, {} bytes long", id, raw.len());
69 fs::write(path, &ret.data).expect("failed to write wasm output");
71 fn rewrite_import_section(
72 wasm: &mut WasmDecoder,
73 import_map: &FxHashMap<String, String>,
77 let mut dst = WasmEncoder::new();
80 info!("rewriting {} imports", n);
82 rewrite_import_entry(wasm, &mut dst, import_map);
87 fn rewrite_import_entry(wasm: &mut WasmDecoder,
88 dst: &mut WasmEncoder,
89 import_map: &FxHashMap<String, String>) {
90 // More info about the binary format here is available at:
91 // https://webassembly.github.io/spec/core/binary/modules.html#import-section
93 // Note that you can also find the whole point of existence of this
94 // function here, where we map the `module` name to a different one if
95 // we've got one listed.
96 let module = wasm.str();
97 let field = wasm.str();
98 let new_module = if module == "env" {
99 import_map.get(field).map(|s| &**s).unwrap_or(module)
103 info!("import rewrite ({} => {}) / {}", module, new_module, field);
106 let kind = wasm.byte();
109 WASM_EXTERNAL_KIND_FUNCTION => dst.u32(wasm.u32()),
110 WASM_EXTERNAL_KIND_TABLE => {
111 dst.byte(wasm.byte()); // element_type
112 dst.limits(wasm.limits());
114 WASM_EXTERNAL_KIND_MEMORY => dst.limits(wasm.limits()),
115 WASM_EXTERNAL_KIND_GLOBAL => {
116 dst.byte(wasm.byte()); // content_type
117 dst.bool(wasm.bool()); // mutable
119 b => panic!("unknown kind: {}", b),
124 struct WasmSections<'a>(WasmDecoder<'a>);
126 impl<'a> Iterator for WasmSections<'a> {
127 type Item = (u8, &'a [u8]);
129 fn next(&mut self) -> Option<(u8, &'a [u8])> {
130 if self.0.data.is_empty() {
134 // see https://webassembly.github.io/spec/core/binary/modules.html#sections
135 let id = self.0.byte();
136 let section_len = self.0.u32();
137 info!("new section {} / {} bytes", id, section_len);
138 let section = self.0.skip(section_len as usize);
143 struct WasmDecoder<'a> {
147 impl<'a> WasmDecoder<'a> {
148 fn new(data: &'a [u8]) -> WasmDecoder<'a> {
152 fn byte(&mut self) -> u8 {
156 fn u32(&mut self) -> u32 {
157 let (n, l1) = leb128::read_u32_leb128(self.data);
158 self.data = &self.data[l1..];
162 fn skip(&mut self, amt: usize) -> &'a [u8] {
163 let (data, rest) = self.data.split_at(amt);
168 fn str(&mut self) -> &'a str {
169 let len = self.u32();
170 str::from_utf8(self.skip(len as usize)).unwrap()
173 fn bool(&mut self) -> bool {
177 fn limits(&mut self) -> (u32, Option<u32>) {
178 let has_max = self.bool();
179 (self.u32(), if has_max { Some(self.u32()) } else { None })
188 fn new() -> WasmEncoder {
189 WasmEncoder { data: Vec::new() }
192 fn u32(&mut self, val: u32) {
193 leb128::write_u32_leb128(&mut self.data, val);
196 fn byte(&mut self, val: u8) {
200 fn bytes(&mut self, val: &[u8]) {
201 self.u32(val.len() as u32);
202 self.data.extend_from_slice(val);
205 fn str(&mut self, val: &str) {
206 self.bytes(val.as_bytes())
209 fn bool(&mut self, b: bool) {
213 fn limits(&mut self, limits: (u32, Option<u32>)) {
214 self.bool(limits.1.is_some());
216 if let Some(c) = limits.1 {