]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_target/src/spec/wasm_base.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_target / src / spec / wasm_base.rs
CommitLineData
f2b60f7d 1use super::crt_objects::LinkSelfContainedDefault;
5e7ed085 2use super::{cvs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
532ac7d7
XL
3
4pub fn options() -> TargetOptions {
064997fb
FG
5 macro_rules! args {
6 ($prefix:literal) => {
7 &[
8 // By default LLD only gives us one page of stack (64k) which is a
9 // little small. Default to a larger stack closer to other PC platforms
10 // (1MB) and users can always inject their own link-args to override this.
11 concat!($prefix, "-z"),
12 concat!($prefix, "stack-size=1048576"),
13 // By default LLD's memory layout is:
14 //
15 // 1. First, a blank page
16 // 2. Next, all static data
17 // 3. Finally, the main stack (which grows down)
18 //
19 // This has the unfortunate consequence that on stack overflows you
20 // corrupt static data and can cause some exceedingly weird bugs. To
21 // help detect this a little sooner we instead request that the stack is
22 // placed before static data.
23 //
24 // This means that we'll generate slightly larger binaries as references
25 // to static data will take more bytes in the ULEB128 encoding, but
26 // stack overflow will be guaranteed to trap as it underflows instead of
27 // corrupting static data.
28 concat!($prefix, "--stack-first"),
29 // FIXME we probably shouldn't pass this but instead pass an explicit list
30 // of symbols we'll allow to be undefined. We don't currently have a
31 // mechanism of knowing, however, which symbols are intended to be imported
32 // from the environment and which are intended to be imported from other
33 // objects linked elsewhere. This is a coarse approximation but is sure to
34 // hide some bugs and frustrate someone at some point, so we should ideally
35 // work towards a world where we can explicitly list symbols that are
36 // supposed to be imported and have all other symbols generate errors if
37 // they remain undefined.
38 concat!($prefix, "--allow-undefined"),
39 // Rust code should never have warnings, and warnings are often
40 // indicative of bugs, let's prevent them.
41 concat!($prefix, "--fatal-warnings"),
42 // LLD only implements C++-like demangling, which doesn't match our own
43 // mangling scheme. Tell LLD to not demangle anything and leave it up to
44 // us to demangle these symbols later. Currently rustc does not perform
45 // further demangling, but tools like twiggy and wasm-bindgen are intended
46 // to do so.
47 concat!($prefix, "--no-demangle"),
48 ]
49 };
50 }
532ac7d7 51
064997fb
FG
52 let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::Lld(LldFlavor::Wasm), args!(""));
53 super::add_link_args(&mut pre_link_args, LinkerFlavor::Gcc, args!("-Wl,"));
532ac7d7
XL
54
55 TargetOptions {
cdc7bbd5 56 is_like_wasm: true,
5e7ed085 57 families: cvs!["wasm"],
cdc7bbd5 58
532ac7d7
XL
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
532ac7d7 65 // relatively self-explanatory!
5e7ed085
FG
66 exe_suffix: ".wasm".into(),
67 dll_prefix: "".into(),
68 dll_suffix: ".wasm".into(),
1b1a35ee 69 eh_frame_header: false,
532ac7d7
XL
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 // no dynamic linking, no need for default visibility!
85 default_hidden_visibility: true,
86
dc9dc135
XL
87 // Symbol visibility takes care of this for the WebAssembly.
88 // Additionally the only known linker, LLD, doesn't support the script
89 // arguments just yet
90 limit_rdylib_exports: false,
91
532ac7d7 92 // we use the LLD shipped with the Rust toolchain by default
5e7ed085 93 linker: Some("rust-lld".into()),
532ac7d7 94 lld_flavor: LldFlavor::Wasm,
17df50a5 95 linker_is_gnu: false,
532ac7d7
XL
96
97 pre_link_args,
98
f2b60f7d
FG
99 // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
100 link_self_contained: LinkSelfContainedDefault::True,
f9f354fc 101
532ac7d7 102 // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
5e7ed085 103 // PIC code is implemented this has quite a drastic effect if it stays
532ac7d7
XL
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).
f9f354fc 109 relocation_model: RelocModel::Static,
532ac7d7 110
416331ca
XL
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.
a2a8927a 114 // `has_thread_local`) and we need to get it to work by specifying
416331ca 115 // `local-exec` as that's all that's implemented in LLVM today for wasm.
a2a8927a 116 has_thread_local: true,
f9f354fc 117 tls_model: TlsModel::LocalExec,
416331ca 118
60c5eb7d
XL
119 // gdb scripts don't work on wasm blobs
120 emit_debug_gdb_scripts: false,
121
3c0e092e
XL
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
dfeec247 128 ..Default::default()
532ac7d7
XL
129 }
130}