-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable constructor/destructor on WASM
Output `WASM_INIT_FUNCS` in linking custom section: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md#start-section On WASM, no `.init_array` nor `.fini_array`, call `__wasm_call_ctors` function on `_start` to call constructors. Generate a function to call destructors and register the function using `__cxa_atexit`.
- Loading branch information
Showing
14 changed files
with
393 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "stdlib.h" | ||
#include "_exit.h" | ||
|
||
#define MAX (8) | ||
|
||
typedef struct { | ||
void (*dtor)(void*); | ||
void *arg; | ||
void *dso_handle; | ||
} CxaAtexitBuf; | ||
|
||
static CxaAtexitBuf buf[MAX]; | ||
static int count; | ||
|
||
void *__dso_handle; | ||
|
||
int __cxa_atexit(void (*func)(void*), void *arg, void *d) { | ||
if (count >= MAX) | ||
return 1; | ||
CxaAtexitBuf *p = &buf[count++]; | ||
p->dtor = func; | ||
p->arg = arg; | ||
p->dso_handle = d; | ||
return 0; | ||
} | ||
|
||
static void call_cxa_atexit_funcs(void) { | ||
for (CxaAtexitBuf *p = &buf[count]; p > buf; ) { | ||
--p; | ||
(*p->dtor)(p->arg); | ||
} | ||
} | ||
|
||
__attribute__((constructor)) | ||
static void register_atexit(void) { | ||
static OnExitChain chain = {NULL, call_cxa_atexit_funcs}; | ||
chain.next = __on_exit_chain; | ||
__on_exit_chain = &chain; | ||
} |
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
Oops, something went wrong.