# Shared-everything linking design sketch ## TL;DR Follow [`emscripten`'s dynamic linking convention](https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md), but use the component model (instead of a purpose-built, host-implemented linker) to hook things up and instantiate, leaving memory and table allocation and initialization to happen post-instantiation via an `init` function. The latter will use `ftalloc` and `malloc` exports from the main module to avoid confusion about who owns which parts of the table and memory. ## Stage 0: wasm-ld produces main module which can use shared libraries - exports a memory, a table, and `ftalloc` and `malloc` functions - plus globals for each export (function or global) address? - optionally imports WASI `dlopen` and `dlsym` functions ## Stage 1: wasm-ld produces a shared library - like emscripten's dynamic linking convention, except: - different custom section name, to avoid confusion - no dedicated runtime or instantiation-time linker needed -- just a component model implementation - use `init` export instead of `start` function to initialize memory and table - this will subsume `__post_instantiate` - make global imports mutable, since we'll need to update them post-instantiation ## Stage 2: wit-component takes a main module plus zero or more shared libraries and produces a component - for each shared library, synthesize a linker module which hooks it up to the main module, including: - `__stack_pointer`, `__memory_base`, `__table_base`, globals exported to the shared library - `init` function exported to the containing component which uses main module's `ftalloc` and `malloc` functions to allocate table and memory space, then calls the shared library `init` function - (if required by the main module) synthesizes `dlopen` and `dlsym` functions backed by tables derived from the shared library's exports - produce a component which: - instantiates all modules, hooking up all the imports and exports - exports an `init` or `main` function which calls the linker `init` functions (then runs main module `main` if it's a command) ## Stage 3: runtime runs the component - should just work in any valid component model implementation?