]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/wasm_base.rs
Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup
[rust.git] / compiler / rustc_target / src / spec / wasm_base.rs
1 use super::crt_objects::CrtObjectsFallback;
2 use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
3 use std::collections::BTreeMap;
4
5 pub fn options() -> TargetOptions {
6     let mut lld_args = Vec::new();
7     let mut clang_args = Vec::new();
8     let mut arg = |arg: &str| {
9         lld_args.push(arg.to_string());
10         clang_args.push(format!("-Wl,{}", arg));
11     };
12
13     // By default LLD only gives us one page of stack (64k) which is a
14     // little small. Default to a larger stack closer to other PC platforms
15     // (1MB) and users can always inject their own link-args to override this.
16     arg("-z");
17     arg("stack-size=1048576");
18
19     // By default LLD's memory layout is:
20     //
21     // 1. First, a blank page
22     // 2. Next, all static data
23     // 3. Finally, the main stack (which grows down)
24     //
25     // This has the unfortunate consequence that on stack overflows you
26     // corrupt static data and can cause some exceedingly weird bugs. To
27     // help detect this a little sooner we instead request that the stack is
28     // placed before static data.
29     //
30     // This means that we'll generate slightly larger binaries as references
31     // to static data will take more bytes in the ULEB128 encoding, but
32     // stack overflow will be guaranteed to trap as it underflows instead of
33     // corrupting static data.
34     arg("--stack-first");
35
36     // FIXME we probably shouldn't pass this but instead pass an explicit list
37     // of symbols we'll allow to be undefined. We don't currently have a
38     // mechanism of knowing, however, which symbols are intended to be imported
39     // from the environment and which are intended to be imported from other
40     // objects linked elsewhere. This is a coarse approximation but is sure to
41     // hide some bugs and frustrate someone at some point, so we should ideally
42     // work towards a world where we can explicitly list symbols that are
43     // supposed to be imported and have all other symbols generate errors if
44     // they remain undefined.
45     arg("--allow-undefined");
46
47     // Rust code should never have warnings, and warnings are often
48     // indicative of bugs, let's prevent them.
49     arg("--fatal-warnings");
50
51     // LLD only implements C++-like demangling, which doesn't match our own
52     // mangling scheme. Tell LLD to not demangle anything and leave it up to
53     // us to demangle these symbols later. Currently rustc does not perform
54     // further demangling, but tools like twiggy and wasm-bindgen are intended
55     // to do so.
56     arg("--no-demangle");
57
58     let mut pre_link_args = BTreeMap::new();
59     pre_link_args.insert(LinkerFlavor::Lld(LldFlavor::Wasm), lld_args);
60     pre_link_args.insert(LinkerFlavor::Gcc, clang_args);
61
62     TargetOptions {
63         is_like_wasm: true,
64
65         // we allow dynamic linking, but only cdylibs. Basically we allow a
66         // final library artifact that exports some symbols (a wasm module) but
67         // we don't allow intermediate `dylib` crate types
68         dynamic_linking: true,
69         only_cdylib: true,
70
71         // This means we'll just embed a `#[start]` function in the wasm module
72         executables: true,
73
74         // relatively self-explanatory!
75         exe_suffix: ".wasm".to_string(),
76         dll_prefix: String::new(),
77         dll_suffix: ".wasm".to_string(),
78         eh_frame_header: false,
79
80         max_atomic_width: Some(64),
81
82         // Unwinding doesn't work right now, so the whole target unconditionally
83         // defaults to panic=abort. Note that this is guaranteed to change in
84         // the future once unwinding is implemented. Don't rely on this as we're
85         // basically guaranteed to change it once WebAssembly supports
86         // exceptions.
87         panic_strategy: PanicStrategy::Abort,
88
89         // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
90         // threaded model which will legalize atomics to normal operations.
91         singlethread: true,
92
93         // no dynamic linking, no need for default visibility!
94         default_hidden_visibility: true,
95
96         // Symbol visibility takes care of this for the WebAssembly.
97         // Additionally the only known linker, LLD, doesn't support the script
98         // arguments just yet
99         limit_rdylib_exports: false,
100
101         // we use the LLD shipped with the Rust toolchain by default
102         linker: Some("rust-lld".to_owned()),
103         lld_flavor: LldFlavor::Wasm,
104
105         // No need for indirection here, simd types can always be passed by
106         // value as the whole module either has simd or not, which is different
107         // from x86 (for example) where programs can have functions that don't
108         // enable simd features.
109         simd_types_indirect: false,
110
111         pre_link_args,
112
113         crt_objects_fallback: Some(CrtObjectsFallback::Wasm),
114
115         // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
116         // PIC code is implemented this has quite a drastric effect if it stays
117         // at the default, `pic`. In an effort to keep wasm binaries as minimal
118         // as possible we're defaulting to `static` for now, but the hope is
119         // that eventually we can ship a `pic`-compatible standard library which
120         // works with `static` as well (or works with some method of generating
121         // non-relative calls and such later on).
122         relocation_model: RelocModel::Static,
123
124         // When the atomics feature is activated then these two keys matter,
125         // otherwise they're basically ignored by the standard library. In this
126         // mode, however, the `#[thread_local]` attribute works (i.e.
127         // `has_elf_tls`) and we need to get it to work by specifying
128         // `local-exec` as that's all that's implemented in LLVM today for wasm.
129         has_elf_tls: true,
130         tls_model: TlsModel::LocalExec,
131
132         // gdb scripts don't work on wasm blobs
133         emit_debug_gdb_scripts: false,
134
135         ..Default::default()
136     }
137 }