@@ -1212,17 +1212,26 @@ impl Linker {
12121212 )
12131213 . map_err ( LinkError :: MainModuleHandleInitFailed ) ?;
12141214
1215- // The main module isn't added to the link state's list of new modules, so we need to
1216- // call its initialization functions separately
1217- trace ! ( "Calling data relocator function for main module" ) ;
1218- call_initialization_function :: < ( ) > ( & main_instance, store, "__wasm_apply_data_relocs" ) ?;
1219- call_initialization_function :: < ( ) > ( & main_instance, store, "__wasm_apply_tls_relocs" ) ?;
1220-
12211215 {
1222- let group_guard = linker. instance_group_state . lock ( ) . unwrap ( ) ;
1216+ trace ! ( ?link_state, "Finalizing linking of main module" ) ;
1217+
1218+ let mut group_guard = linker. instance_group_state . lock ( ) . unwrap ( ) ;
12231219 let mut linker_state = linker. linker_state . write ( ) . unwrap ( ) ;
1224- trace ! ( "Finalizing linking of main module" ) ;
1225- linker. finalize_link_operation ( group_guard, & mut linker_state, store, link_state) ?;
1220+
1221+ let group_state = group_guard. as_mut ( ) . unwrap ( ) ;
1222+ group_state. finalize_pending_globals (
1223+ & mut linker_state,
1224+ store,
1225+ & link_state. unresolved_globals ,
1226+ ) ?;
1227+
1228+ // The main module isn't added to the link state's list of new modules, so we need to
1229+ // call its initialization functions separately
1230+ trace ! ( "Calling data relocator function for main module" ) ;
1231+ call_initialization_function :: < ( ) > ( & main_instance, store, "__wasm_apply_data_relocs" ) ?;
1232+ call_initialization_function :: < ( ) > ( & main_instance, store, "__wasm_apply_tls_relocs" ) ?;
1233+
1234+ linker. initialize_new_modules ( group_guard, store, link_state) ?;
12261235 }
12271236
12281237 trace ! ( "Calling main module's _initialize function" ) ;
@@ -1672,6 +1681,18 @@ impl Linker {
16721681 & link_state. unresolved_globals ,
16731682 ) ?;
16741683
1684+ self . initialize_new_modules ( group_state_guard, store, link_state)
1685+ }
1686+
1687+ fn initialize_new_modules (
1688+ & self ,
1689+ // Take ownership of the guard and drop it ourselves to ensure no deadlock can happen
1690+ mut group_state_guard : MutexGuard < ' _ , Option < InstanceGroupState > > ,
1691+ store : & mut impl AsStoreMut ,
1692+ link_state : InProgressLinkState ,
1693+ ) -> Result < ( ) , LinkError > {
1694+ let group_state = group_state_guard. as_mut ( ) . unwrap ( ) ;
1695+
16751696 let new_instances = link_state
16761697 . new_modules
16771698 . iter ( )
0 commit comments