0

我正在尝试使用wasi-sdk在 C 程序中直接调用 WASI 函数(fd_write) 。这是库(lib.c):

#include <stdint.h>

struct Ciovec
{
    uint8_t *buf;
    uint32_t buf_len;
};

uint16_t fd_write(uint32_t fd, struct Ciovec *iovs_ptr, uint32_t iovs_len, uint32_t *nwritten);

static char *str = "just testing\n";

void c_main()
{
    struct Ciovec vec = { .buf = (uint8_t*)str, .buf_len = 13 };
    uint32_t nwritten = 0;
    fd_write(1, &vec, 1, &nwritten);
}

现在,如果我使用 wasi-sdk 将其构建为静态可链接库

~/wasi-sdk-11.0/bin/clang --sysroot ~/wasi-sdk-11.0/share/wasi-sysroot/ lib.c -c -o lib.o -fpic

我得到这个笏:(wasm2wat --enable-all输出)

(module
  (type (;0;) (func))
  (type (;1;) (func (param i32 i32 i32 i32) (result i32)))
  (import "env" "__linear_memory" (memory (;0;) 1))
  (import "env" "__indirect_function_table" (table (;0;) 0 funcref))
  (import "env" "__stack_pointer" (global (;0;) (mut i32)))
  (import "env" "fd_write" (func (;0;) (type 1)))
  ...)

现在这个导入行fd_write不正确。据我了解,需要从wasi_snapshot_preview1or导入 WASI 函数wasi_unstable(我没有第一个的源代码,我只是在开源代码中看到它,第二个请参见此处的示例,但我不是确定在 C 中该怎么做。有什么想法吗?

4

2 回答 2

0

在wasi-libc 源代码中找到了答案:

uint16_t fd_write(uint32_t fd, struct Ciovec *iovs_ptr, uint32_t iovs_len, uint32_t *nwritten)
    __attribute__((
                __import_module__("wasi_snapshot_preview1"),
                __import_name__("fd_write"),
                ));

生成的 Wasm:

  (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (type 1)))

这仍然不起作用,我得到undefined symbol: fd_write错误,但至少我现在知道如何指定导入路径。

于 2020-07-25T07:33:56.130 回答
0

所以我让这个工作:

#include <stdint.h>
#include <stdio.h>

extern uint16_t my_func()
__attribute__((
    __import_module__("my_module"),
    __import_name__("my_func"),
));

int main(){
    my_func();
    return 0;
}

编译-Wl,--allow-undefined

生成

(module
  (type (;0;) (func (param i32 i32 i32) (result i32)))
  (type (;1;) (func (param i32 i64 i32) (result i64)))
  (type (;2;) (func (param i32 i32) (result i32)))
  (type (;3;) (func (result i32)))
  (type (;4;) (func (param i32)))
  (type (;5;) (func (param i32) (result i32)))
  (type (;6;) (func (param i32 i32 i32 i32) (result i32)))
  (type (;7;) (func (param i32 i64 i32 i32) (result i32)))
  (type (;8;) (func))
  (import "wasi_unstable" "fd_prestat_get" (func $__wasi_fd_prestat_get (type 2)))
  (import "wasi_unstable" "fd_prestat_dir_name" (func $__wasi_fd_prestat_dir_name (type 0)))
  (import "my_module" "my_func" (func $my_func__ (type 3)))
  (import "wasi_unstable" "proc_exit" (func $__wasi_proc_exit (type 4)))
  (import "wasi_unstable" "fd_fdstat_get" (func $__wasi_fd_fdstat_get (type 2)))
  (import "wasi_unstable" "fd_close" (func $__wasi_fd_close (type 5)))
  (import "wasi_unstable" "fd_write" (func $__wasi_fd_write (type 6)))
  (import "wasi_unstable" "fd_seek" (func $__wasi_fd_seek (type 7)))
  (func $__wasm_call_ctors (type 8))
  (func $undefined:__wasilibc_populate_environ (type 3) (result i32)
于 2022-02-26T22:56:53.227 回答