1 use super::crt_objects::CrtObjectsFallback;
2 use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
3 use std::collections::BTreeMap;
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));
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.
17 arg("stack-size=1048576");
19 // By default LLD's memory layout is:
21 // 1. First, a blank page
22 // 2. Next, all static data
23 // 3. Finally, the main stack (which grows down)
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.
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.
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");
47 // Rust code should never have warnings, and warnings are often
48 // indicative of bugs, let's prevent them.
49 arg("--fatal-warnings");
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
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);
63 // we allow dynamic linking, but only cdylibs. Basically we allow a
64 // final library artifact that exports some symbols (a wasm module) but
65 // we don't allow intermediate `dylib` crate types
66 dynamic_linking: true,
69 // This means we'll just embed a `#[start]` function in the wasm module
72 // relatively self-explanatory!
73 exe_suffix: ".wasm".to_string(),
74 dll_prefix: String::new(),
75 dll_suffix: ".wasm".to_string(),
77 eh_frame_header: false,
79 max_atomic_width: Some(64),
81 // Unwinding doesn't work right now, so the whole target unconditionally
82 // defaults to panic=abort. Note that this is guaranteed to change in
83 // the future once unwinding is implemented. Don't rely on this as we're
84 // basically guaranteed to change it once WebAssembly supports
86 panic_strategy: PanicStrategy::Abort,
88 // Wasm doesn't have atomics yet, so tell LLVM that we're in a single
89 // threaded model which will legalize atomics to normal operations.
92 // no dynamic linking, no need for default visibility!
93 default_hidden_visibility: true,
95 // Symbol visibility takes care of this for the WebAssembly.
96 // Additionally the only known linker, LLD, doesn't support the script
98 limit_rdylib_exports: false,
100 // we use the LLD shipped with the Rust toolchain by default
101 linker: Some("rust-lld".to_owned()),
102 lld_flavor: LldFlavor::Wasm,
104 // No need for indirection here, simd types can always be passed by
105 // value as the whole module either has simd or not, which is different
106 // from x86 (for example) where programs can have functions that don't
107 // enable simd features.
108 simd_types_indirect: false,
112 crt_objects_fallback: Some(CrtObjectsFallback::Wasm),
114 // This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
115 // PIC code is implemented this has quite a drastric effect if it stays
116 // at the default, `pic`. In an effort to keep wasm binaries as minimal
117 // as possible we're defaulting to `static` for now, but the hope is
118 // that eventually we can ship a `pic`-compatible standard library which
119 // works with `static` as well (or works with some method of generating
120 // non-relative calls and such later on).
121 relocation_model: RelocModel::Static,
123 // When the atomics feature is activated then these two keys matter,
124 // otherwise they're basically ignored by the standard library. In this
125 // mode, however, the `#[thread_local]` attribute works (i.e.
126 // `has_elf_tls`) and we need to get it to work by specifying
127 // `local-exec` as that's all that's implemented in LLVM today for wasm.
129 tls_model: TlsModel::LocalExec,
131 // gdb scripts don't work on wasm blobs
132 emit_debug_gdb_scripts: false,