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