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 // LLD only implements C++-like demangling, which doesn't match our own
32 // mangling scheme. Tell LLD to not demangle anything and leave it up to
33 // us to demangle these symbols later. Currently rustc does not perform
34 // further demangling, but tools like twiggy and wasm-bindgen are intended
35 // to do so.
36 concat!($prefix, "--no-demangle"),
37 ]
38 };
39 }
40
41 let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::WasmLld(Cc::No), args!(""));
42 add_link_args(&mut pre_link_args, LinkerFlavor::WasmLld(Cc::Yes), args!("-Wl,"));
43
44 TargetOptions {
45 is_like_wasm: true,
46 binary_format: BinaryFormat::Wasm,
47 families: cvs!["wasm"],
48
49 // we allow dynamic linking, but only cdylibs. Basically we allow a
50 // final library artifact that exports some symbols (a wasm module) but
51 // we don't allow intermediate `dylib` crate types
52 dynamic_linking: true,
53 only_cdylib: true,
54
55 // relatively self-explanatory!
56 exe_suffix: ".wasm".into(),
57 dll_prefix: "".into(),
58 dll_suffix: ".wasm".into(),
59 eh_frame_header: false,
60
61 max_atomic_width: Some(64),
62
63 // WebAssembly targets all started out originally as `-Cpanic=abort`
64 // since that was all that worked. Since then, however, the WebAssembly
65 // exception-handling proposal has been finalized/implemented and is now
66 // supported in both LLVM & rustc & libstd & co. The default still
67 // remains at `-Cpanic=abort` for now for historical compatibility,
68 // but `bootstrap.toml` can be used to configure a local build to
69 // use `-Cpanic=unwind` instead. This is used in CI, for example, to
70 // test wasm targets with `-Cpanic=unwind`.
71 //
72 // In the future this might change where preexisting targets migrate,
73 // but that's not going to be as easy as just changing this, so for now
74 // this is just a local build configuration option.
75 panic_strategy: if option_env!("CFG_WASM_PANIC_UNWIND_DEFAULT") == Some("1") {
76 PanicStrategy::Unwind
77 } else {
78 PanicStrategy::Abort
79 },
80
81 // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
82 // threaded model which will legalize atomics to normal operations.
83 singlethread: true,
84
85 // we use the LLD shipped with the Rust toolchain by default
86 linker: Some("rust-lld".into()),
87 linker_flavor: LinkerFlavor::WasmLld(Cc::No),
88
89 pre_link_args,
90
91 // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
92 //
93 // rust-lang/rust#104137: cannot blindly remove this without putting in
94 // some other way to compensate for lack of `-nostartfiles` in linker
95 // invocation.
96 link_self_contained: LinkSelfContainedDefault::True,
97
98 // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
99 // PIC code is implemented this has quite a drastic effect if it stays
100 // at the default, `pic`. In an effort to keep wasm binaries as minimal
101 // as possible we're defaulting to `static` for now, but the hope is
102 // that eventually we can ship a `pic`-compatible standard library which
103 // works with `static` as well (or works with some method of generating
104 // non-relative calls and such later on).
105 relocation_model: RelocModel::Static,
106
107 // When the atomics feature is activated then these two keys matter,
108 // otherwise they're basically ignored by the standard library. In this
109 // mode, however, the `#[thread_local]` attribute works (i.e.
110 // `has_thread_local`) and we need to get it to work by specifying
111 // `local-exec` as that's all that's implemented in LLVM today for wasm.
112 has_thread_local: true,
113 tls_model: TlsModel::LocalExec,
114
115 // gdb scripts don't work on wasm blobs
116 emit_debug_gdb_scripts: false,
117
118 // There's more discussion of this at
119 // https://bugs.llvm.org/show_bug.cgi?id=52442 but the general result is
120 // that this isn't useful for wasm and has tricky issues with
121 // representation, so this is disabled.
122 generate_arange_section: false,
123
124 // Differ from LLVM's default to use the legacy exception-handling
125 // proposal instructions and use the standard exception-handling
126 // instructions. Note that this is only applicable when unwinding is
127 // actually turned on, which it's not by default on this target. For
128 // `-Zbuild-std` builds, however, this affects when rebuilding libstd
129 // with unwinding.
130 llvm_args: cvs!["-wasm-use-legacy-eh=false"],
131
132 // WASI's `sys::args::init` function ignores its arguments; instead,
133 // `args::args()` makes the WASI API calls itself.
134 //
135 // Other Wasm targets make no use of `std::env` entirely.
136 // Emscripten enables it explicitly.
137 main_needs_argc_argv: false,
138
139 ..Default::default()
140 }
141}