]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/wasm32_base.rs
Rollup merge of #80567 - lukaslueg:intersperse_with, r=m-ou-se
[rust.git] / compiler / rustc_target / src / spec / wasm32_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     // The symbol visibility story is a bit in flux right now with LLD.
59     // It's... not entirely clear to me what's going on, but this looks to
60     // make everything work when `export_symbols` isn't otherwise called for
61     // things like executables.
62     //
63     // This is really only here to get things working. If it can be removed and
64     // basic tests still work, then sounds like it should be removed!
65     arg("--export-dynamic");
66
67     let mut pre_link_args = BTreeMap::new();
68     pre_link_args.insert(LinkerFlavor::Lld(LldFlavor::Wasm), lld_args);
69     pre_link_args.insert(LinkerFlavor::Gcc, clang_args);
70
71     TargetOptions {
72         // we allow dynamic linking, but only cdylibs. Basically we allow a
73         // final library artifact that exports some symbols (a wasm module) but
74         // we don't allow intermediate `dylib` crate types
75         dynamic_linking: true,
76         only_cdylib: true,
77
78         // This means we'll just embed a `#[start]` function in the wasm module
79         executables: true,
80
81         // relatively self-explanatory!
82         exe_suffix: ".wasm".to_string(),
83         dll_prefix: String::new(),
84         dll_suffix: ".wasm".to_string(),
85         linker_is_gnu: false,
86         eh_frame_header: false,
87
88         max_atomic_width: Some(64),
89
90         // Unwinding doesn't work right now, so the whole target unconditionally
91         // defaults to panic=abort. Note that this is guaranteed to change in
92         // the future once unwinding is implemented. Don't rely on this as we're
93         // basically guaranteed to change it once WebAssembly supports
94         // exceptions.
95         panic_strategy: PanicStrategy::Abort,
96
97         // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
98         // threaded model which will legalize atomics to normal operations.
99         singlethread: true,
100
101         // no dynamic linking, no need for default visibility!
102         default_hidden_visibility: true,
103
104         // Symbol visibility takes care of this for the WebAssembly.
105         // Additionally the only known linker, LLD, doesn't support the script
106         // arguments just yet
107         limit_rdylib_exports: false,
108
109         // we use the LLD shipped with the Rust toolchain by default
110         linker: Some("rust-lld".to_owned()),
111         lld_flavor: LldFlavor::Wasm,
112
113         // No need for indirection here, simd types can always be passed by
114         // value as the whole module either has simd or not, which is different
115         // from x86 (for example) where programs can have functions that don't
116         // enable simd features.
117         simd_types_indirect: false,
118
119         pre_link_args,
120
121         crt_objects_fallback: Some(CrtObjectsFallback::Wasm),
122
123         // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
124         // PIC code is implemented this has quite a drastric effect if it stays
125         // at the default, `pic`. In an effort to keep wasm binaries as minimal
126         // as possible we're defaulting to `static` for now, but the hope is
127         // that eventually we can ship a `pic`-compatible standard library which
128         // works with `static` as well (or works with some method of generating
129         // non-relative calls and such later on).
130         relocation_model: RelocModel::Static,
131
132         // When the atomics feature is activated then these two keys matter,
133         // otherwise they're basically ignored by the standard library. In this
134         // mode, however, the `#[thread_local]` attribute works (i.e.
135         // `has_elf_tls`) and we need to get it to work by specifying
136         // `local-exec` as that's all that's implemented in LLVM today for wasm.
137         has_elf_tls: true,
138         tls_model: TlsModel::LocalExec,
139
140         // gdb scripts don't work on wasm blobs
141         emit_debug_gdb_scripts: false,
142
143         ..Default::default()
144     }
145 }