Skip to main content

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), &["-z", "stack-size=1048576", "--stack-first", "--no-demangle"]args!(""));
42    add_link_args(&mut pre_link_args, LinkerFlavor::WasmLld(Cc::Yes), &["-Wl,-z", "-Wl,stack-size=1048576", "-Wl,--stack-first",
            "-Wl,--no-demangle"]args!("-Wl,"));
43
44    TargetOptions {
45        is_like_wasm: true,
46        binary_format: BinaryFormat::Wasm,
47        families: ::std::borrow::Cow::Borrowed(&[::std::borrow::Cow::Borrowed("wasm")])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        // Unwinding doesn't work right now, so the whole target unconditionally
64        // defaults to panic=abort. Note that this is guaranteed to change in
65        // the future once unwinding is implemented. Don't rely on this as we're
66        // basically guaranteed to change it once WebAssembly supports
67        // exceptions.
68        panic_strategy: PanicStrategy::Abort,
69
70        // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
71        // threaded model which will legalize atomics to normal operations.
72        singlethread: true,
73
74        // we use the LLD shipped with the Rust toolchain by default
75        linker: Some("rust-lld".into()),
76        linker_flavor: LinkerFlavor::WasmLld(Cc::No),
77
78        pre_link_args,
79
80        // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
81        //
82        // rust-lang/rust#104137: cannot blindly remove this without putting in
83        // some other way to compensate for lack of `-nostartfiles` in linker
84        // invocation.
85        link_self_contained: LinkSelfContainedDefault::True,
86
87        // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
88        // PIC code is implemented this has quite a drastic effect if it stays
89        // at the default, `pic`. In an effort to keep wasm binaries as minimal
90        // as possible we're defaulting to `static` for now, but the hope is
91        // that eventually we can ship a `pic`-compatible standard library which
92        // works with `static` as well (or works with some method of generating
93        // non-relative calls and such later on).
94        relocation_model: RelocModel::Static,
95
96        // When the atomics feature is activated then these two keys matter,
97        // otherwise they're basically ignored by the standard library. In this
98        // mode, however, the `#[thread_local]` attribute works (i.e.
99        // `has_thread_local`) and we need to get it to work by specifying
100        // `local-exec` as that's all that's implemented in LLVM today for wasm.
101        has_thread_local: true,
102        tls_model: TlsModel::LocalExec,
103
104        // gdb scripts don't work on wasm blobs
105        emit_debug_gdb_scripts: false,
106
107        // There's more discussion of this at
108        // https://bugs.llvm.org/show_bug.cgi?id=52442 but the general result is
109        // that this isn't useful for wasm and has tricky issues with
110        // representation, so this is disabled.
111        generate_arange_section: false,
112
113        ..Default::default()
114    }
115}