]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_target/src/spec/wasm_base.rs
New upstream version 1.54.0+dfsg1
[rustc.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 families: vec!["wasm".to_string()],
65
66 // we allow dynamic linking, but only cdylibs. Basically we allow a
67 // final library artifact that exports some symbols (a wasm module) but
68 // we don't allow intermediate `dylib` crate types
69 dynamic_linking: true,
70 only_cdylib: true,
71
72 // This means we'll just embed a `#[start]` function in the wasm module
73 executables: true,
74
75 // relatively self-explanatory!
76 exe_suffix: ".wasm".to_string(),
77 dll_prefix: String::new(),
78 dll_suffix: ".wasm".to_string(),
79 eh_frame_header: false,
80
81 max_atomic_width: Some(64),
82
83 // Unwinding doesn't work right now, so the whole target unconditionally
84 // defaults to panic=abort. Note that this is guaranteed to change in
85 // the future once unwinding is implemented. Don't rely on this as we're
86 // basically guaranteed to change it once WebAssembly supports
87 // exceptions.
88 panic_strategy: PanicStrategy::Abort,
89
90 // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
91 // threaded model which will legalize atomics to normal operations.
92 singlethread: true,
93
94 // no dynamic linking, no need for default visibility!
95 default_hidden_visibility: true,
96
97 // Symbol visibility takes care of this for the WebAssembly.
98 // Additionally the only known linker, LLD, doesn't support the script
99 // arguments just yet
100 limit_rdylib_exports: false,
101
102 // we use the LLD shipped with the Rust toolchain by default
103 linker: Some("rust-lld".to_owned()),
104 lld_flavor: LldFlavor::Wasm,
105 linker_is_gnu: false,
106
107 pre_link_args,
108
109 crt_objects_fallback: Some(CrtObjectsFallback::Wasm),
110
111 // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
112 // PIC code is implemented this has quite a drastric effect if it stays
113 // at the default, `pic`. In an effort to keep wasm binaries as minimal
114 // as possible we're defaulting to `static` for now, but the hope is
115 // that eventually we can ship a `pic`-compatible standard library which
116 // works with `static` as well (or works with some method of generating
117 // non-relative calls and such later on).
118 relocation_model: RelocModel::Static,
119
120 // When the atomics feature is activated then these two keys matter,
121 // otherwise they're basically ignored by the standard library. In this
122 // mode, however, the `#[thread_local]` attribute works (i.e.
123 // `has_elf_tls`) and we need to get it to work by specifying
124 // `local-exec` as that's all that's implemented in LLVM today for wasm.
125 has_elf_tls: true,
126 tls_model: TlsModel::LocalExec,
127
128 // gdb scripts don't work on wasm blobs
129 emit_debug_gdb_scripts: false,
130
131 ..Default::default()
132 }
133 }