@@ -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,38 @@ 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+ // ... probably because we're linking in libc's _start and overriding rustc's?
1314+
1315+ self . link_args ( [ "-pie" , "--export-all" , "--no-gc-sections" ] ) ;
1316+
1317+ // We link and export all of libc, as well as all of rust's stdlib into dynamic
1318+ // executables, so that side modules can just link against the existing code at
1319+ // runtime. This has two benefits:
1320+ // * Reduced code size for side modules
1321+ // * More importantly, static and thread-local variables will exist only once.
1322+ // This is especially important for libc, which stores things such as the
1323+ // state of its memory allocator (malloc et al.) in static variables.
1324+ let whole_archive = true ;
1325+ let verbatim = false ; // No effect for WasmLd
1326+ self . link_staticlib_by_name ( "c" , verbatim, whole_archive) ;
1327+ self . link_staticlib_by_name ( "resolv" , verbatim, whole_archive) ;
1328+ self . link_staticlib_by_name ( "rt" , verbatim, whole_archive) ;
1329+ self . link_staticlib_by_name ( "m" , verbatim, whole_archive) ;
1330+ self . link_staticlib_by_name ( "pthread" , verbatim, whole_archive) ;
1331+ self . link_staticlib_by_name ( "util" , verbatim, whole_archive) ;
1332+ self . link_staticlib_by_name ( "wasi-emulated-mman" , verbatim, whole_archive) ;
1333+ self . link_staticlib_by_name ( "common-tag-stubs" , verbatim, whole_archive) ;
1334+ }
1335+ LinkOutputKind :: DynamicDylib => {
1336+ self . link_args ( [ "--no-entry" , "-shared" , "--unresolved-symbols=import-dynamic" ] ) ;
1337+ }
1338+ LinkOutputKind :: StaticDylib => {
13121339 self . link_arg ( "--no-entry" ) ;
13131340 }
13141341 LinkOutputKind :: WasiReactorExe => {
@@ -1394,7 +1421,7 @@ impl<'a> Linker for WasmLd<'a> {
13941421
13951422 fn no_default_libraries ( & mut self ) { }
13961423
1397- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
1424+ fn export_symbols ( & mut self , _tmpdir : & Path , crate_type : CrateType , symbols : & [ String ] ) {
13981425 for sym in symbols {
13991426 self . link_args ( & [ "--export" , sym] ) ;
14001427 }
@@ -1403,7 +1430,23 @@ impl<'a> Linker for WasmLd<'a> {
14031430 // symbols explicitly passed via the `--export` flags above and hides all
14041431 // others. Various bits and pieces of wasm32-unknown-unknown tooling use
14051432 // 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" ] ) ;
1433+ self . link_args ( & [
1434+ "--export=__wasm_init_tls" ,
1435+ "--export=__tls_size" ,
1436+ "--export=__tls_align" ,
1437+ "--export=__tls_base" ,
1438+ "--export=__wasm_call_ctors" ,
1439+ "--export=__wasm_signal" ,
1440+ "--export-if-defined=__wasm_apply_data_relocs" ,
1441+ ] ) ;
1442+
1443+ if matches ! ( crate_type, CrateType :: Executable ) {
1444+ self . link_args ( & [
1445+ "--export-if-defined=__stack_pointer" ,
1446+ "--export-if-defined=__heap_base" ,
1447+ "--export-if-defined=__data_end" ,
1448+ ] ) ;
1449+ }
14071450 }
14081451
14091452 fn subsystem ( & mut self , _subsystem : & str ) { }
0 commit comments