@@ -1278,7 +1278,7 @@ impl<'a> WasmLd<'a> {
12781278 // symbols are how the TLS segments are initialized and configured.
12791279 let mut wasm_ld = WasmLd { cmd, sess } ;
12801280 if sess. target_features . contains ( & sym:: atomics) {
1281- wasm_ld. link_args ( & [ "--shared-memory" , "--max-memory=1073741824 " , "--import-memory" ] ) ;
1281+ wasm_ld. link_args ( & [ "--shared-memory" , "--max-memory=4294967296 " , "--import-memory" ] ) ;
12821282 if sess. target . os == "unknown" || sess. target . os == "none" {
12831283 wasm_ld. link_args ( & [
12841284 "--export=__wasm_init_tls" ,
@@ -1304,11 +1304,37 @@ impl<'a> Linker for WasmLd<'a> {
13041304 _out_filename : & Path ,
13051305 ) {
13061306 match output_kind {
1307- LinkOutputKind :: DynamicNoPicExe
1308- | LinkOutputKind :: DynamicPicExe
1309- | LinkOutputKind :: StaticNoPicExe
1310- | LinkOutputKind :: StaticPicExe => { }
1311- LinkOutputKind :: DynamicDylib | LinkOutputKind :: StaticDylib => {
1307+ LinkOutputKind :: DynamicNoPicExe | LinkOutputKind :: StaticNoPicExe => { }
1308+ LinkOutputKind :: StaticPicExe | LinkOutputKind :: DynamicPicExe => {
1309+ // FIXME: The wasm32-wasmer-wasi-dl toolchain produces broken executables
1310+ // right now, because the start function isn't linked correctly. Instead of
1311+ // _start calling the rust main function, it calls __main_void from libc,
1312+ // which is broken because rustc does not generate a __main_argc_argv.
1313+
1314+ self . link_args ( [ "-pie" , "--export-all" , "--no-gc-sections" ] ) ;
1315+
1316+ // We link and export all of libc, as well as all of rust's stdlib into dynamic
1317+ // executables, so that side modules can just link against the existing code at
1318+ // runtime. This has two benefits:
1319+ // * Reduced code size for side modules
1320+ // * More importantly, static and thread-local variables will exist only once.
1321+ // This is especially important for libc, which stores things such as the
1322+ // state of its memory allocator (malloc et al.) in static variables.
1323+ let whole_archive = true ;
1324+ let verbatim = false ; // No effect for WasmLd
1325+ self . link_staticlib_by_name ( "c" , verbatim, whole_archive) ;
1326+ self . link_staticlib_by_name ( "resolv" , verbatim, whole_archive) ;
1327+ self . link_staticlib_by_name ( "rt" , verbatim, whole_archive) ;
1328+ self . link_staticlib_by_name ( "m" , verbatim, whole_archive) ;
1329+ self . link_staticlib_by_name ( "pthread" , verbatim, whole_archive) ;
1330+ self . link_staticlib_by_name ( "util" , verbatim, whole_archive) ;
1331+ self . link_staticlib_by_name ( "wasi-emulated-mman" , verbatim, whole_archive) ;
1332+ self . link_staticlib_by_name ( "common-tag-stubs" , verbatim, whole_archive) ;
1333+ }
1334+ LinkOutputKind :: DynamicDylib => {
1335+ self . link_args ( [ "--no-entry" , "-shared" , "--unresolved-symbols=import-dynamic" ] ) ;
1336+ }
1337+ LinkOutputKind :: StaticDylib => {
13121338 self . link_arg ( "--no-entry" ) ;
13131339 }
13141340 LinkOutputKind :: WasiReactorExe => {
@@ -1394,7 +1420,7 @@ impl<'a> Linker for WasmLd<'a> {
13941420
13951421 fn no_default_libraries ( & mut self ) { }
13961422
1397- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
1423+ fn export_symbols ( & mut self , _tmpdir : & Path , crate_type : CrateType , symbols : & [ String ] ) {
13981424 for sym in symbols {
13991425 self . link_args ( & [ "--export" , sym] ) ;
14001426 }
@@ -1403,7 +1429,23 @@ impl<'a> Linker for WasmLd<'a> {
14031429 // symbols explicitly passed via the `--export` flags above and hides all
14041430 // others. Various bits and pieces of wasm32-unknown-unknown tooling use
14051431 // this, so be sure these symbols make their way out of the linker as well.
1406- self . link_args ( & [ "--export=__heap_base" , "--export=__data_end" , "--export=__stack_pointer" ] ) ;
1432+ self . link_args ( & [
1433+ "--export=__wasm_init_tls" ,
1434+ "--export=__tls_size" ,
1435+ "--export=__tls_align" ,
1436+ "--export=__tls_base" ,
1437+ "--export=__wasm_call_ctors" ,
1438+ "--export=__wasm_signal" ,
1439+ "--export-if-defined=__wasm_apply_data_relocs" ,
1440+ ] ) ;
1441+
1442+ if matches ! ( crate_type, CrateType :: Executable ) {
1443+ self . link_args ( & [
1444+ "--export-if-defined=__stack_pointer" ,
1445+ "--export-if-defined=__heap_base" ,
1446+ "--export-if-defined=__data_end" ,
1447+ ] ) ;
1448+ }
14071449 }
14081450
14091451 fn subsystem ( & mut self , _subsystem : & str ) { }
0 commit comments