Skip to content

Replacing a symbol in built libraries #18733

Answered by kleisauke
iliakan asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the --wrap linker option for this.

main.c:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

extern pid_t __real_fork();

pid_t __wrap_fork() {
    printf("wrap fork\n");
    return 0;
}

int main() {
    printf("fork(): %d\n", fork());
    printf("real fork(): %d\n", __real_fork());
    return 0;
}
$ emcc -sEXIT_RUNTIME -sALLOW_UNIMPLEMENTED_SYSCALLS main.c -Wl,--wrap=fork -o main.js
$ node main.js
wrap fork
fork(): 0
real fork(): -1

This is supported in wasm-ld since commit llvm/llvm-project@a5ca34e.

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@kleisauke
Comment options

@kleisauke
Comment options

@iliakan
Comment options

@kleisauke
Comment options

@iliakan
Comment options

Answer selected by iliakan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants