2023-11-08 14:15:26 +08:00
|
|
|
use crate::spec::{
|
|
|
|
|
add_link_args, cvs, Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel,
|
|
|
|
|
TargetOptions, TlsModel,
|
|
|
|
|
};
|
2019-03-19 13:06:37 -07:00
|
|
|
|
|
|
|
|
pub fn options() -> TargetOptions {
|
2022-06-17 17:38:42 +03:00
|
|
|
macro_rules! args {
|
|
|
|
|
($prefix:literal) => {
|
|
|
|
|
&[
|
|
|
|
|
// By default LLD only gives us one page of stack (64k) which is a
|
|
|
|
|
// little small. Default to a larger stack closer to other PC platforms
|
|
|
|
|
// (1MB) and users can always inject their own link-args to override this.
|
|
|
|
|
concat!($prefix, "-z"),
|
|
|
|
|
concat!($prefix, "stack-size=1048576"),
|
|
|
|
|
// By default LLD's memory layout is:
|
|
|
|
|
//
|
|
|
|
|
// 1. First, a blank page
|
|
|
|
|
// 2. Next, all static data
|
|
|
|
|
// 3. Finally, the main stack (which grows down)
|
|
|
|
|
//
|
|
|
|
|
// This has the unfortunate consequence that on stack overflows you
|
|
|
|
|
// corrupt static data and can cause some exceedingly weird bugs. To
|
|
|
|
|
// help detect this a little sooner we instead request that the stack is
|
|
|
|
|
// placed before static data.
|
|
|
|
|
//
|
|
|
|
|
// This means that we'll generate slightly larger binaries as references
|
|
|
|
|
// to static data will take more bytes in the ULEB128 encoding, but
|
|
|
|
|
// stack overflow will be guaranteed to trap as it underflows instead of
|
|
|
|
|
// corrupting static data.
|
|
|
|
|
concat!($prefix, "--stack-first"),
|
|
|
|
|
// FIXME we probably shouldn't pass this but instead pass an explicit list
|
|
|
|
|
// of symbols we'll allow to be undefined. We don't currently have a
|
|
|
|
|
// mechanism of knowing, however, which symbols are intended to be imported
|
|
|
|
|
// from the environment and which are intended to be imported from other
|
|
|
|
|
// objects linked elsewhere. This is a coarse approximation but is sure to
|
|
|
|
|
// hide some bugs and frustrate someone at some point, so we should ideally
|
|
|
|
|
// work towards a world where we can explicitly list symbols that are
|
|
|
|
|
// supposed to be imported and have all other symbols generate errors if
|
|
|
|
|
// they remain undefined.
|
|
|
|
|
concat!($prefix, "--allow-undefined"),
|
|
|
|
|
// LLD only implements C++-like demangling, which doesn't match our own
|
|
|
|
|
// mangling scheme. Tell LLD to not demangle anything and leave it up to
|
|
|
|
|
// us to demangle these symbols later. Currently rustc does not perform
|
|
|
|
|
// further demangling, but tools like twiggy and wasm-bindgen are intended
|
|
|
|
|
// to do so.
|
|
|
|
|
concat!($prefix, "--no-demangle"),
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 21:08:46 +03:00
|
|
|
let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::WasmLld(Cc::No), args!(""));
|
2023-11-08 14:15:26 +08:00
|
|
|
add_link_args(&mut pre_link_args, LinkerFlavor::WasmLld(Cc::Yes), args!("-Wl,"));
|
2019-03-19 13:06:37 -07:00
|
|
|
|
|
|
|
|
TargetOptions {
|
2020-12-30 12:52:21 -06:00
|
|
|
is_like_wasm: true,
|
2022-03-28 01:08:17 +02:00
|
|
|
families: cvs!["wasm"],
|
2020-12-30 12:52:21 -06:00
|
|
|
|
2019-03-19 13:06:37 -07:00
|
|
|
// we allow dynamic linking, but only cdylibs. Basically we allow a
|
|
|
|
|
// final library artifact that exports some symbols (a wasm module) but
|
|
|
|
|
// we don't allow intermediate `dylib` crate types
|
|
|
|
|
dynamic_linking: true,
|
|
|
|
|
only_cdylib: true,
|
|
|
|
|
|
|
|
|
|
// relatively self-explanatory!
|
2022-03-22 11:43:05 +01:00
|
|
|
exe_suffix: ".wasm".into(),
|
|
|
|
|
dll_prefix: "".into(),
|
|
|
|
|
dll_suffix: ".wasm".into(),
|
2020-09-03 17:49:09 -07:00
|
|
|
eh_frame_header: false,
|
2019-03-19 13:06:37 -07:00
|
|
|
|
|
|
|
|
max_atomic_width: Some(64),
|
|
|
|
|
|
|
|
|
|
// Unwinding doesn't work right now, so the whole target unconditionally
|
|
|
|
|
// defaults to panic=abort. Note that this is guaranteed to change in
|
|
|
|
|
// the future once unwinding is implemented. Don't rely on this as we're
|
|
|
|
|
// basically guaranteed to change it once WebAssembly supports
|
|
|
|
|
// exceptions.
|
|
|
|
|
panic_strategy: PanicStrategy::Abort,
|
|
|
|
|
|
|
|
|
|
// Wasm doesn't have atomics yet, so tell LLVM that we're in a single
|
|
|
|
|
// threaded model which will legalize atomics to normal operations.
|
|
|
|
|
singlethread: true,
|
|
|
|
|
|
|
|
|
|
// no dynamic linking, no need for default visibility!
|
|
|
|
|
default_hidden_visibility: true,
|
|
|
|
|
|
2019-04-14 19:05:21 +02:00
|
|
|
// Symbol visibility takes care of this for the WebAssembly.
|
|
|
|
|
// Additionally the only known linker, LLD, doesn't support the script
|
|
|
|
|
// arguments just yet
|
|
|
|
|
limit_rdylib_exports: false,
|
|
|
|
|
|
2019-03-19 13:06:37 -07:00
|
|
|
// we use the LLD shipped with the Rust toolchain by default
|
2022-03-22 11:43:05 +01:00
|
|
|
linker: Some("rust-lld".into()),
|
2022-08-06 21:08:46 +03:00
|
|
|
linker_flavor: LinkerFlavor::WasmLld(Cc::No),
|
2019-03-19 13:06:37 -07:00
|
|
|
|
|
|
|
|
pre_link_args,
|
|
|
|
|
|
2023-03-10 13:13:08 -05:00
|
|
|
// FIXME: Figure out cases in which WASM needs to link with a native toolchain.
|
|
|
|
|
//
|
|
|
|
|
// rust-lang/rust#104137: cannot blindly remove this without putting in
|
|
|
|
|
// some other way to compensate for lack of `-nostartfiles` in linker
|
|
|
|
|
// invocation.
|
2023-10-18 13:15:20 +00:00
|
|
|
link_self_contained: LinkSelfContainedDefault::True,
|
2023-03-10 13:13:08 -05:00
|
|
|
|
2019-04-04 13:23:42 -07:00
|
|
|
// This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
|
2022-03-30 15:14:15 -04:00
|
|
|
// PIC code is implemented this has quite a drastic effect if it stays
|
2019-04-04 13:23:42 -07:00
|
|
|
// at the default, `pic`. In an effort to keep wasm binaries as minimal
|
|
|
|
|
// as possible we're defaulting to `static` for now, but the hope is
|
|
|
|
|
// that eventually we can ship a `pic`-compatible standard library which
|
|
|
|
|
// works with `static` as well (or works with some method of generating
|
|
|
|
|
// non-relative calls and such later on).
|
2020-04-23 00:46:45 +03:00
|
|
|
relocation_model: RelocModel::Static,
|
2019-04-04 13:23:42 -07:00
|
|
|
|
2019-07-19 12:02:34 -07:00
|
|
|
// When the atomics feature is activated then these two keys matter,
|
|
|
|
|
// otherwise they're basically ignored by the standard library. In this
|
|
|
|
|
// mode, however, the `#[thread_local]` attribute works (i.e.
|
2021-12-17 20:56:38 +00:00
|
|
|
// `has_thread_local`) and we need to get it to work by specifying
|
2019-07-19 12:02:34 -07:00
|
|
|
// `local-exec` as that's all that's implemented in LLVM today for wasm.
|
2021-12-17 20:56:38 +00:00
|
|
|
has_thread_local: true,
|
2020-04-25 21:45:21 +03:00
|
|
|
tls_model: TlsModel::LocalExec,
|
2019-07-19 12:02:34 -07:00
|
|
|
|
2019-11-18 07:41:10 -08:00
|
|
|
// gdb scripts don't work on wasm blobs
|
|
|
|
|
emit_debug_gdb_scripts: false,
|
|
|
|
|
|
2021-11-10 10:47:00 -08:00
|
|
|
// There's more discussion of this at
|
|
|
|
|
// https://bugs.llvm.org/show_bug.cgi?id=52442 but the general result is
|
|
|
|
|
// that this isn't useful for wasm and has tricky issues with
|
|
|
|
|
// representation, so this is disabled.
|
|
|
|
|
generate_arange_section: false,
|
|
|
|
|
|
2019-03-19 13:06:37 -07:00
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|