-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add stubs for dlopen, dlsym, etc. (#443)
* add stubs for dlopen, dlsym, etc. This adds weak exports for the POSIX `dlopen`, `dlsym`, `dlclose`, and `dlerror` functions, allowing code which uses those features to compile. The implementations are stubs which always fail since there is currently no official standard for runtime dynamic linking. Since the symbols are weak, they can be overriden with useful, runtime-specific implementations, e.g. based on host functions or statically-generated tables (see https://github.com/dicej/component-linking-demo for an example of the latter). Signed-off-by: Joel Dice <[email protected]> * move `dlopen` stubs out of libc and into libdl Per review feedback, it's easier to simply replace libdl.so with a working implementation at runtime than it is to override a handful of symbols in libc. Note that I've both added libdl.so and replaced the empty libdl.a we were previously creating with one that contains the stubs. I'm thinking we might as well be consistent about what symbols the .so and the .a contain. Otherwise, e.g. the CPython build gets confused when the dlfcn.h says `dlopen` etc. exist but libdl.a is empty. Signed-off-by: Joel Dice <[email protected]> * customize dlfcn.h for WASI For WASI, we use flag values which match MacOS rather than musl. This gives `RTLD_LOCAL` a non-zero value, avoiding ambiguity and allowing us to defer the decision of whether `RTLD_LOCAL` or `RTLD_GLOBAL` should be the default when neither is specified. We also avoid declaring `dladdr`, `dlinfo`, and friends on WASI since they are neither supported nor stubbed at this time. Signed-off-by: Joel Dice <[email protected]> * use musl's RTLD_* flags except for RTLD_LOCAL This minimizes the divergence from upstream while still giving us the flexibility to choose a default value later. Signed-off-by: Joel Dice <[email protected]> * use `NULL` instead of `0` for null pointers Signed-off-by: Joel Dice <[email protected]> --------- Signed-off-by: Joel Dice <[email protected]>
- Loading branch information
Showing
7 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* This file is used to build libdl.so with stub versions of `dlopen`, `dlsym`, | ||
* etc. The intention is that this stubbed libdl.so can be used to build | ||
* libraries and applications which use `dlopen` without committing to a | ||
* specific runtime implementation. Later, it can be replaced with a real, | ||
* working libdl.so (e.g. at runtime or component composition time). | ||
* | ||
* For example, the `wasm-tools component link` subcommand can be used to create | ||
* a component that bundles any `dlopen`-able libraries in such a way that their | ||
* function exports can be resolved symbolically at runtime using an | ||
* implementation of libdl.so designed for that purpose. In other cases, a | ||
* runtime might provide Emscripten-style dynamic linking via URLs or else a | ||
* more traditional, filesystem-based implementation. Finally, even this | ||
* stubbed version of libdl.so can be used at runtime in cases where dynamic | ||
* library resolution cannot or should not be supported (and the application can | ||
* handle this situation gracefully). */ | ||
|
||
#include <stddef.h> | ||
#include <dlfcn.h> | ||
|
||
static const char *error = NULL; | ||
|
||
weak int dlclose(void *library) | ||
{ | ||
error = "dlclose not implemented"; | ||
return -1; | ||
} | ||
|
||
weak char *dlerror(void) | ||
{ | ||
const char *var = error; | ||
error = NULL; | ||
return (char*) var; | ||
} | ||
|
||
weak void *dlopen(const char *name, int flags) | ||
{ | ||
error = "dlopen not implemented"; | ||
return NULL; | ||
} | ||
|
||
weak void *dlsym(void *library, const char *name) | ||
{ | ||
error = "dlsym not implemented"; | ||
return NULL; | ||
} |