Cheng Shao pushed to branch wip/T26166 at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • rts/RtsToHsIface.c
    ... ... @@ -18,16 +18,17 @@
    18 18
      *         // etc.
    
    19 19
      *     };
    
    20 20
      *
    
    21
    - * `ghc_hs_iface` is initialized during program loading via the
    
    22
    - * `init_ghc_hs_iface` constructor in `ghc-interface`
    
    21
    + * `ghc_hs_iface` is initialized via the `init_ghc_hs_iface` function
    
    22
    + * in `ghc-internal` cbits, when it is invoked by `hs_init_ghc` during
    
    23
    + * GHC RTS initialization:
    
    23 24
      *
    
    24
    - *     const struct HsIface the_hs_iface = {
    
    25
    + *     struct HsIface the_hs_iface = {
    
    25 26
      *         .runIO = &ghczminternal_GHCziInternalziTopHandler_runIO_closure,
    
    26 27
      *         .Z0T = &ghczminternal_GHCziInternalziTuple_Z0T_closure,
    
    27 28
      *         // etc.
    
    28 29
      *     };
    
    29 30
      *
    
    30
    - *     void __attribute__((constructor)) init_ghc_hs_iface() {
    
    31
    + *     void init_ghc_hs_iface(void) {
    
    31 32
      *         ghc_hs_iface = &the_hs_iface;
    
    32 33
      *     }
    
    33 34
      *
    
    ... ... @@ -36,7 +37,7 @@
    36 37
      * side-effect of making the interface between the RTS and `ghc-internal`
    
    37 38
      * explicit.
    
    38 39
      *
    
    39
    - * Note that the ((constructor)) symbol is explicitly included in the final
    
    40
    + * Note that the init_ghc_hs_iface symbol is explicitly included in the final
    
    40 41
      * binary via the linker flag `-uinit_ghc_hs_iface`, when an executable is
    
    41 42
      * linked against ghc-internal by GHC.
    
    42 43
      *
    
    ... ... @@ -45,11 +46,15 @@
    45 46
      * (since, otherwise, only objects members which provide undefined symbols
    
    46 47
      * needed by the executable are included in the final object).
    
    47 48
      *
    
    48
    - * When `ghc-internal` is a dynamic dependency of an executable, the
    
    49
    - * constructor will run when the shared object for `ghc-internal` is loaded.
    
    50
    - * When the executable is statically linked against `ghc-internal`, the
    
    51
    - * constructor will be run when the program is loaded (since the constructor
    
    52
    - * symbol was forcibly included as described in the paragraph above).
    
    49
    + * CAUTION: there may be constructors invoked before hs_init_ghc, and
    
    50
    + * those constructors will see an uninitialized ghc_hs_iface! For now
    
    51
    + * we're fine: when GHC generates a constructor that references
    
    52
    + * certain closures or info tables, the generated references don't go
    
    53
    + * through ghc_hs_iface redirection, and they are not forward
    
    54
    + * references to other objects not compiled yet. Still, always keep
    
    55
    + * the no-ghc_hs_iface restriction in mind when doing work related to
    
    56
    + * constructors, be it the compiler-generated ones or explicitly
    
    57
    + * written ones in RTS.
    
    53 58
      */
    
    54 59
     
    
    55 60
     #include "Rts.h"
    

  • rts/wasm/JSFFI.c
    ... ... @@ -68,6 +68,15 @@ static void __init_ghc_hs_iface_jsffi(void) {
    68 68
     // Therefore, on wasm32, we designate priority 101 to ctors generated
    
    69 69
     // by the GHC codegen, and priority 102 to the initialization logic
    
    70 70
     // here to ensure hs_init_ghc() sees everything it needs to see.
    
    71
    +//
    
    72
    +// It's simpler when it comes to shared libraries: we'll need to load
    
    73
    +// them via dyld written in JS anyway, and hence we always compile the
    
    74
    +// rts shared library with JSVal functionality. The constructors here
    
    75
    +// are only for non-shared objects, so that the user would only need
    
    76
    +// to invoke _initialize for all the RTS initialization logic to be
    
    77
    +// done before invoking user-specified exports; for the shared library
    
    78
    +// case, we explicitly export __ghc_wasm_jsffi_init to be invoked by
    
    79
    +// dyld, after at least rts & ghc-internal has been loaded.
    
    71 80
     
    
    72 81
     #if defined(__PIC__)
    
    73 82
     __attribute__((export_name("__ghc_wasm_jsffi_init"))) void __ghc_wasm_jsffi_init(void);