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