emscripten-core / emscripten-core/emscripten

Wrapping syscalls doesn't work with `-sMAIN_MODULE -sEXPORT_ALL`

Open
#26,355 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Version of emscripten/emsdk:**
Tip of tree
```
$ ./emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 5.0.1-git (d4c1c6e1d37bf38e84cca063753a462143b7af9a)
```

### a.c:
```
#include "stdio.h"
#include

int syscall_openat_orig(int dirfd, intptr_t path, int flags, void* varags)
__attribute__((__import_module__("env"),
__import_name__("__syscall_openat"), __warn_unused_result__));

int __syscall_openat(int dirfd, intptr_t path, int flags, void* varargs) {
printf("__syscall_openat!\n");
return syscall_openat_orig(dirfd, path, flags, varargs);
}

int main() {
int fd = open("a.c", O_RDONLY);
printf("fd: %d\n", fd);
}
```

### compile, link, run

```
$ emcc a.c -sNODERAWFS -sEXPORT_ALL -sMAIN_MODULE -O2 && node a.out.js
RuntimeError: Aborted(LinkError: WebAssembly.instantiate(): Import #0 "env" "__syscall_openat": function import requires a callable).
```

### Comments

Removing either `-sEXPORT_ALL` or `-sMAIN_MODULE` makes the error go away.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.