Просмотр исходного кода

Update wasm app build scripts for wasi-sdk-12 and refine interpreter (#481)

Update wasm app build scripts for wasi-sdk-12.0: add --export=__main_argc_argv, remove --no-threads
Lookup function with name "__main_argc_argv" as main function besides "main"
Change module_malloc to runtime_malloc in wasi native lib
Refine classic interpreter op_block and op_br_table
Refine faster interpreter op_br_table

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Wenyong Huang 5 лет назад
Родитель
Сommit
724858c731

+ 3 - 6
core/iwasm/common/wasm_runtime_common.c

@@ -2039,12 +2039,9 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
     }
 #endif /* end of WASM_ENABLE_LIBC_WASI */
 
-    func = resolve_function(module_inst, "_main");
-    if (!func) {
-        func = resolve_function(module_inst, "main");
-    }
-
-    if (!func) {
+    if (!(func = resolve_function(module_inst, "main"))
+        && !(func = resolve_function(module_inst, "__main_argc_argv"))
+        && !(func = resolve_function(module_inst, "_main"))) {
         wasm_runtime_set_exception(module_inst,
                                    "lookup main function failed");
         return false;

+ 1 - 1
core/iwasm/compilation/aot_emit_control.c

@@ -383,7 +383,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
     /* Get block info */
     if (!(wasm_loader_find_block_addr((BlockAddr*)block_addr_cache,
                                       *p_frame_ip, frame_ip_end, (uint8)label_type,
-                                      &else_addr, &end_addr, NULL, 0))) {
+                                      &else_addr, &end_addr))) {
         aot_set_last_error("find block end addr failed.");
         return false;
     }

+ 2 - 2
core/iwasm/interpreter/wasm.h

@@ -405,10 +405,10 @@ typedef struct BlockType {
 } BlockType;
 
 typedef struct WASMBranchBlock {
-    uint8 label_type;
-    uint32 cell_num;
+    uint8 *begin_addr;
     uint8 *target_addr;
     uint32 *frame_sp;
+    uint32 cell_num;
 } WASMBranchBlock;
 
 /* Execution environment, e.g. stack info */

+ 35 - 49
core/iwasm/interpreter/wasm_interp_classic.c

@@ -381,28 +381,28 @@ popcount64(uint64 u)
 static uint64
 read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
 {
-    uint64 result = 0;
+    uint64 result = 0, byte;
+    uint32 offset = *p_offset;
     uint32 shift = 0;
-    uint32 bcnt = 0;
-    uint64 byte;
 
     while (true) {
-        byte = buf[*p_offset];
-        *p_offset += 1;
+        byte = buf[offset++];
         result |= ((byte & 0x7f) << shift);
         shift += 7;
         if ((byte & 0x80) == 0) {
             break;
         }
-        bcnt += 1;
     }
     if (sign && (shift < maxbits) && (byte & 0x40)) {
         /* Sign extend */
         result |= - ((uint64)1 << shift);
     }
+    *p_offset = offset;
     return result;
 }
 
+#define skip_leb(p) while (*p++ & 0x80)
+
 #define PUSH_I32(value) do {                    \
     *(int32*)frame_sp++ = (int32)(value);       \
   } while (0)
@@ -423,8 +423,9 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
 
 #define PUSH_CSP(_label_type, cell_num, _target_addr) do {   \
     bh_assert(frame_csp < frame->csp_boundary);              \
-    frame_csp->label_type = _label_type;                     \
+    /* frame_csp->label_type = _label_type; */               \
     frame_csp->cell_num = cell_num;                          \
+    frame_csp->begin_addr = frame_ip;                        \
     frame_csp->target_addr = _target_addr;                   \
     frame_csp->frame_sp = frame_sp;                          \
     frame_csp++;                                             \
@@ -1078,11 +1079,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
   BlockAddr *cache_items;
   uint8 *frame_ip_end = frame_ip + 1;
   uint8 opcode;
-  uint32 *depths = NULL;
-  uint32 depth_buf[BR_TABLE_TMP_BUF_LEN];
-  uint32 i, depth, cond, count, fidx, tidx, frame_size = 0;
+  uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0;
   uint64 all_cell_num = 0;
-  int32 didx, val;
+  int32 val;
   uint8 *else_addr, *end_addr, *maddr = NULL;
   uint32 local_idx, local_offset, global_idx;
   uint8 local_type, *global_addr;
@@ -1127,15 +1126,9 @@ handle_op_block:
         else if (cache_items[1].start_addr == frame_ip) {
           end_addr = cache_items[1].end_addr;
         }
-        else if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache,
-                                              frame_ip, (uint8*)-1,
-                                              LABEL_TYPE_BLOCK,
-                                              &else_addr, &end_addr,
-                                              NULL, 0)) {
-          wasm_set_exception(module, "find block address failed");
-          goto got_exception;
+        else {
+          end_addr = NULL;
         }
-
         PUSH_CSP(LABEL_TYPE_BLOCK, cell_num, end_addr);
         HANDLE_OP_END ();
 
@@ -1173,26 +1166,26 @@ handle_op_if:
         else if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache,
                                               frame_ip, (uint8*)-1,
                                               LABEL_TYPE_IF,
-                                              &else_addr, &end_addr,
-                                              NULL, 0)) {
+                                              &else_addr, &end_addr)) {
           wasm_set_exception(module, "find block address failed");
           goto got_exception;
         }
 
         cond = (uint32)POP_I32();
 
-        PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr);
-
-        /* condition of the if branch is false, else condition is met */
-        if (cond == 0) {
+        if (cond) { /* if branch is met */
+          PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr);
+        }
+        else { /* if branch is not met */
           /* if there is no else branch, go to the end addr */
           if (else_addr == NULL) {
-            POP_CSP();
             frame_ip = end_addr + 1;
           }
           /* if there is an else branch, go to the else addr */
-          else
+          else {
+            PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr);
             frame_ip = else_addr + 1;
+          }
         }
         HANDLE_OP_END ();
 
@@ -1221,6 +1214,16 @@ handle_op_if:
         read_leb_uint32(frame_ip, frame_ip_end, depth);
 label_pop_csp_n:
         POP_CSP_N(depth);
+        if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */
+          if (!wasm_loader_find_block_addr((BlockAddr*)exec_env->block_addr_cache,
+                                           (frame_csp - 1)->begin_addr, (uint8*)-1,
+                                           LABEL_TYPE_BLOCK,
+                                           &else_addr, &end_addr)) {
+            wasm_set_exception(module, "find block address failed");
+            goto got_exception;
+          }
+          frame_ip = end_addr;
+        }
         HANDLE_OP_END ();
 
       HANDLE_OP (WASM_OP_BR_IF):
@@ -1238,30 +1241,13 @@ label_pop_csp_n:
         CHECK_SUSPEND_FLAGS();
 #endif
         read_leb_uint32(frame_ip, frame_ip_end, count);
-        if (count <= BR_TABLE_TMP_BUF_LEN)
-          depths = depth_buf;
-        else {
-          uint64 total_size = sizeof(uint32) * (uint64)count;
-          if (total_size >= UINT32_MAX
-              || !(depths = wasm_runtime_malloc((uint32)total_size))) {
-            wasm_set_exception(module, "allocate memory failed");
-            goto got_exception;
-          }
-        }
-        for (i = 0; i < count; i++) {
-          read_leb_uint32(frame_ip, frame_ip_end, depths[i]);
-        }
+        lidx = POP_I32();
+        if (lidx > count)
+          lidx = count;
+        for (i = 0; i < lidx; i++)
+          skip_leb(frame_ip);
         read_leb_uint32(frame_ip, frame_ip_end, depth);
-        didx = POP_I32();
-        if (didx >= 0 && (uint32)didx < count) {
-          depth = depths[didx];
-        }
-        if (depths != depth_buf) {
-          wasm_runtime_free(depths);
-          depths = NULL;
-        }
         goto label_pop_csp_n;
-        HANDLE_OP_END ();
 
       HANDLE_OP (WASM_OP_RETURN):
         frame_sp -= cur_func->ret_cell_num;

+ 24 - 8
core/iwasm/interpreter/wasm_interp_fast.c

@@ -1249,20 +1249,36 @@ recover_br_info:
         HANDLE_OP_END ();
 
       HANDLE_OP (WASM_OP_BR_TABLE):
+        {
+          uint32 arity, br_item_size;
+
 #if WASM_ENABLE_THREAD_MGR != 0
-        CHECK_SUSPEND_FLAGS();
+          CHECK_SUSPEND_FLAGS();
 #endif
-        count = read_uint32(frame_ip);
-        didx = GET_OPERAND(uint32, 0);
-        frame_ip += 2;
+          count = read_uint32(frame_ip);
+          didx = GET_OPERAND(uint32, 0);
+          frame_ip += 2;
 
-        if (!(didx >= 0 && (uint32)didx < count))
+          if (!(didx >= 0 && (uint32)didx < count))
             didx = count;
 
-        while (didx--)
-          SKIP_BR_INFO();
+          /* all br items must have the same arity and item size,
+             so we only calculate the first item size */
+          arity = *(uint32*)frame_ip;
+          br_item_size = sizeof(uint32); /* arity */
+          if (arity) {
+            /* total cell num */
+            br_item_size += sizeof(uint32);
+            /* cells, src offsets and dst offsets */
+            br_item_size += (sizeof(uint8) + sizeof(int16) + sizeof(uint16))
+                            * arity;
+          }
+          /* target address */
+          br_item_size += sizeof(uint8*);
 
-        goto recover_br_info;
+          frame_ip += br_item_size * didx;
+          goto recover_br_info;
+        }
 
       HANDLE_OP (WASM_OP_RETURN):
         {

+ 17 - 69
core/iwasm/interpreter/wasm_loader.c

@@ -79,53 +79,10 @@ check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
   }                                             \
 } while (0)
 
-static bool
-skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits,
-         char* error_buf, uint32 error_buf_size)
-{
-    const uint8 *buf = *p_buf;
-    uint32 offset = 0, bcnt = 0;
-    uint64 byte;
-
-    while (true) {
-        if (bcnt + 1 > (maxbits + 6) / 7) {
-            set_error_buf(error_buf, error_buf_size,
-                          "integer representation too long");
-            return false;
-        }
-
-        CHECK_BUF(buf, buf_end, offset + 1);
-        byte = buf[offset];
-        offset += 1;
-        bcnt += 1;
-        if ((byte & 0x80) == 0) {
-            break;
-        }
-    }
-
-    *p_buf += offset;
-    return true;
-fail:
-    return false;
-}
-
-#define skip_leb_int64(p, p_end) do {               \
-  if (!skip_leb(&p, p_end, 64,                      \
-                error_buf, error_buf_size))         \
-    return false;                                   \
-} while (0)
-
-#define skip_leb_uint32(p, p_end) do {              \
-  if (!skip_leb(&p, p_end, 32,                      \
-                error_buf, error_buf_size))         \
-    return false;                                   \
-} while (0)
-
-#define skip_leb_int32(p, p_end) do {               \
-  if (!skip_leb(&p, p_end, 32,                      \
-                error_buf, error_buf_size))         \
-    return false;                                   \
-} while (0)
+#define skip_leb(p) while (*p++ & 0x80)
+#define skip_leb_int64(p, p_end) skip_leb(p)
+#define skip_leb_uint32(p, p_end) skip_leb(p)
+#define skip_leb_int32(p, p_end) skip_leb(p)
 
 static bool
 read_leb(uint8 **p_buf, const uint8 *buf_end,
@@ -1134,13 +1091,15 @@ load_global_import(const WASMModule *parent_module,
     }
 
     if (wasm_runtime_is_host_module(sub_module_name)) {
-        /* do nothing, let host injects the symbol */
+        /* do nothing, let host inject the symbol */
     }
+#if WASM_ENABLE_LIBC_BUILTIN != 0
     else if (wasm_runtime_is_built_in_module(sub_module_name)) {
         /* check built-in modules */
         global->is_linked = wasm_native_lookup_libc_builtin_global(
               sub_module_name, global_name, global);
     }
+#endif
 #if WASM_ENABLE_MULTI_MODULE != 0
     else {
         /* check sub modules */
@@ -3267,17 +3226,17 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
                             const uint8 *code_end_addr,
                             uint8 label_type,
                             uint8 **p_else_addr,
-                            uint8 **p_end_addr,
-                            char *error_buf,
-                            uint32 error_buf_size)
+                            uint8 **p_end_addr)
 {
     const uint8 *p = start_addr, *p_end = code_end_addr;
     uint8 *else_addr = NULL;
+    char error_buf[128];
     uint32 block_nested_depth = 1, count, i, j, t;
+    uint32 error_buf_size = sizeof(error_buf);
     uint8 opcode, u8;
     BlockAddr block_stack[16] = { 0 }, *block;
 
-    i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
+    i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
     block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
 
     for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
@@ -3303,7 +3262,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
             case WASM_OP_BLOCK:
             case WASM_OP_LOOP:
             case WASM_OP_IF:
-                CHECK_BUF(p, p_end, 1);
                 /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
                 u8 = read_uint8(p);
                 if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
@@ -3342,7 +3300,8 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
                     for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
                         start_addr = block_stack[t].start_addr;
                         if (start_addr) {
-                            i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
+                            i = ((uintptr_t)start_addr)
+                                & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
                             block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
                             for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
                                 if (!block[j].start_addr)
@@ -3633,9 +3592,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
                         break;
 #endif
                     default:
-                        set_error_buf_v(error_buf, error_buf_size,
-                                        "%s %02x %02x",
-                                        "unsupported opcode", 0xfc, opcode);
                         return false;
                 }
                 break;
@@ -3704,10 +3660,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
                     default:
                         LOG_WARNING("WASM loader find block addr failed: "
                                     "invalid opcode fd 0x%02x.", opcode);
-                        if (error_buf)
-                            snprintf(error_buf, error_buf_size,
-                                     "WASM loader find block addr failed: "
-                                     "invalid opcode fd %02x.", opcode);
                         return false;
                 }
                 break;
@@ -3733,9 +3685,6 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
 #endif
 
             default:
-                set_error_buf_v(error_buf, error_buf_size,
-                                "%s %02x",
-                                "unsupported opcode", opcode);
                 return false;
         }
     }
@@ -6126,7 +6075,6 @@ handle_op_block_and_loop:
 #endif
                 POP_I32();
 
-                /* TODO: check the const */
                 for (i = 0; i <= count; i++) {
                     if (!(frame_csp_tmp =
                             check_branch_block(loader_ctx, &p, p_end,
@@ -6136,8 +6084,8 @@ handle_op_block_and_loop:
                     if (i == 0) {
                         if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
                             ret_count =
-                                    block_type_get_result_types(&frame_csp_tmp->block_type,
-                                                                &ret_types);
+                                block_type_get_result_types(&frame_csp_tmp->block_type,
+                                                             &ret_types);
                     }
                     else {
                         uint8 *tmp_ret_types = NULL;
@@ -6146,8 +6094,8 @@ handle_op_block_and_loop:
                         /* Check whether all table items have the same return type */
                         if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
                             tmp_ret_count =
-                                    block_type_get_result_types(&frame_csp_tmp->block_type,
-                                                                &tmp_ret_types);
+                                block_type_get_result_types(&frame_csp_tmp->block_type,
+                                                            &tmp_ret_types);
 
                         if (ret_count != tmp_ret_count
                             || (ret_count

+ 1 - 3
core/iwasm/interpreter/wasm_loader.h

@@ -68,9 +68,7 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
                             const uint8 *code_end_addr,
                             uint8 block_type,
                             uint8 **p_else_addr,
-                            uint8 **p_end_addr,
-                            char *error_buf,
-                            uint32 error_buf_size);
+                            uint8 **p_end_addr);
 
 #ifdef __cplusplus
 }

+ 12 - 18
core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

@@ -340,7 +340,6 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
     wasi_iovec_t *iovec, *iovec_begin;
     uint64 total_size;
     size_t nread;
-    uint32 mem;
     uint32 i;
     wasi_errno_t err;
 
@@ -355,7 +354,7 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
 
     total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin)))
+        || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     iovec = iovec_begin;
@@ -380,7 +379,7 @@ wasi_fd_pread(wasm_exec_env_t exec_env,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(iovec_begin);
     return err;
 }
 
@@ -395,7 +394,6 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
     wasi_ciovec_t *ciovec, *ciovec_begin;
     uint64 total_size;
     size_t nwritten;
-    uint32 mem;
     uint32 i;
     wasi_errno_t err;
 
@@ -410,7 +408,7 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
 
     total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin)))
+        || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     ciovec = ciovec_begin;
@@ -435,7 +433,7 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(ciovec_begin);
     return err;
 }
 
@@ -451,7 +449,6 @@ wasi_fd_read(wasm_exec_env_t exec_env,
     uint64 total_size;
     size_t nread;
     uint32 i;
-    uint32 mem;
     wasi_errno_t err;
 
     if (!wasi_ctx)
@@ -465,7 +462,7 @@ wasi_fd_read(wasm_exec_env_t exec_env,
 
     total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin)))
+        || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     iovec = iovec_begin;
@@ -490,7 +487,7 @@ wasi_fd_read(wasm_exec_env_t exec_env,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(iovec_begin);
     return err;
 }
 
@@ -622,7 +619,6 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
     wasi_ciovec_t *ciovec, *ciovec_begin;
     uint64 total_size;
     size_t nwritten;
-    uint32 mem;
     uint32 i;
     wasi_errno_t err;
 
@@ -637,7 +633,7 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
 
     total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin)))
+        || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     ciovec = ciovec_begin;
@@ -662,7 +658,7 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(ciovec_begin);
     return err;
 }
 
@@ -1052,7 +1048,6 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
     wasi_iovec_t *iovec, *iovec_begin;
     uint64 total_size;
     size_t ro_datalen;
-    uint32 mem;
     uint32 i;
     wasi_errno_t err;
 
@@ -1068,7 +1063,7 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
 
     total_size = sizeof(wasi_iovec_t) * (uint64)ri_data_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&iovec_begin)))
+        || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     iovec = iovec_begin;
@@ -1095,7 +1090,7 @@ wasi_sock_recv(wasm_exec_env_t exec_env,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(iovec_begin);
     return err;
 }
 
@@ -1112,7 +1107,6 @@ wasi_sock_send(wasm_exec_env_t exec_env,
     wasi_ciovec_t *ciovec, *ciovec_begin;
     uint64 total_size;
     size_t so_datalen;
-    uint32 mem;
     uint32 i;
     wasi_errno_t err;
 
@@ -1127,7 +1121,7 @@ wasi_sock_send(wasm_exec_env_t exec_env,
 
     total_size = sizeof(wasi_ciovec_t) * (uint64)si_data_len;
     if (total_size >= UINT32_MAX
-        || !(mem = module_malloc((uint32)total_size, (void**)&ciovec_begin)))
+        || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
         return (wasi_errno_t)-1;
 
     ciovec = ciovec_begin;
@@ -1153,7 +1147,7 @@ wasi_sock_send(wasm_exec_env_t exec_env,
     err = 0;
 
 fail:
-    module_free(mem);
+    wasm_runtime_free(ciovec_begin);
     return err;
 }
 

+ 5 - 4
doc/build_wasm_app.md

@@ -64,18 +64,19 @@ There are some useful options which can be specified to build the source code:
 
 - **-Wl,--shared-memory** Use shared linear memory
 
-- **-Wl,--threads** or **-Wl,--no-threads** Run or do not run the linker multi-threaded
-
 - **-Wl,--allow-undefined** Allow undefined symbols in linked binary
 
 - **-Wl,--allow-undefined-file=<value>** Allow symbols listed in <file> to be undefined in linked binary
 
+- **-pthread** Support POSIX threads in generated code
+
 For example, we can build the wasm app with command:
 ``` Bash
 /opt/wasi-sdk/bin/clang -O3 -nostdlib \
     -z stack-size=8192 -Wl,--initial-memory=65536 \
-    -Wl,--export=main -o test.wasm test.c \
-    -Wl,--export=__heap_base,--export=__data_end \
+    -o test.wasm test.c \
+    -Wl,--export=main -Wl,--export=__main_argc_argv \
+    -Wl,--export=__heap_base -Wl,--export=__data_end \
     -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined
 ```
 to generate a wasm binary with small footprint.

+ 19 - 9
product-mini/app-samples/hello-world/build.sh

@@ -3,13 +3,23 @@
 
 WAMR_DIR=${PWD}/../../..
 
-/opt/wasi-sdk/bin/clang     \
-        --target=wasm32 -O3 \
+echo "Build wasm app .."
+/opt/wasi-sdk/bin/clang -O3 \
         -z stack-size=4096 -Wl,--initial-memory=65536 \
-        --sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot    \
-        -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \
-        -Wl,--export=main, \
-        -Wl,--export=__data_end, -Wl,--export=__heap_base \
-        -Wl,--no-threads,--strip-all,--no-entry \
-        -nostdlib -o test.wasm *.c
-#./jeffdump -o test_wasm.h -n wasm_test_file test.wasm
+        -o test.wasm main.c \
+        -Wl,--export=main -Wl,--export=__main_argc_argv \
+        -Wl,--export=__data_end -Wl,--export=__heap_base \
+        -Wl,--strip-all,--no-entry \
+        -Wl,--allow-undefined \
+        -nostdlib \
+
+echo "Build binarydump tool .."
+rm -fr build && mkdir build && cd build
+cmake ../../../../test-tools/binarydump-tool
+make
+cd ..
+
+echo "Generate test_wasm.h .."
+./build/binarydump -o test_wasm.h -n wasm_test_file test.wasm
+
+echo "Done"

+ 1 - 1
samples/basic/build.sh

@@ -46,7 +46,7 @@ OUT_FILE=${i%.*}.wasm
         --target=wasm32 -O0 -z stack-size=4096 -Wl,--initial-memory=65536 \
         --sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot  \
         -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \
-        -Wl,--no-threads,--strip-all,--no-entry -nostdlib \
+        -Wl,--strip-all,--no-entry -nostdlib \
         -Wl,--export=generate_float \
         -Wl,--export=float_to_string \
         -Wl,--export=calculate\

+ 2 - 2
samples/gui/wasm-apps/decrease/Makefile

@@ -21,8 +21,8 @@ all:
     --target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
     --sysroot=$(SDK_DIR)/libc-builtin-sysroot       \
     -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework      \
-    -Wl,--allow-undefined-file=$(SDK_DIR)/libc-builtin-sysroot/share/defined-symbols.txt	\
-    -Wl,--no-threads,--strip-all,--no-entry -nostdlib \
+    -Wl,--allow-undefined-file=$(SDK_DIR)/libc-builtin-sysroot/share/defined-symbols.txt \
+    -Wl,--strip-all,--no-entry -nostdlib \
     -Wl,--export=on_init -Wl,--export=on_timer_callback \
     -Wl,--export=on_widget_event \
     -Wl,--export=__heap_base,--export=__data_end \

+ 1 - 1
samples/gui/wasm-apps/increase/Makefile

@@ -27,7 +27,7 @@ all:
 	@$(CC) $(CFLAGS) $(SRCS) \
     --target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
     -Wl,--allow-undefined \
-    -Wl,--no-threads,--strip-all,--no-entry -nostdlib \
+    -Wl,--strip-all,--no-entry -nostdlib \
     -Wl,--export=on_init -Wl,--export=on_timer_callback \
     -Wl,--export=on_widget_event \
     -Wl,--export=__heap_base,--export=__data_end \

+ 2 - 2
samples/littlevgl/wasm-apps/Makefile_wasm_app

@@ -49,9 +49,9 @@ all:
 	@$(CC) $(CFLAGS) $(SRCS) \
     -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
     -DLV_CONF_INCLUDE_SIMPLE                        \
-    -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework		\
+    -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework      \
     -Wl,--allow-undefined                           \
-    -Wl,--no-threads,--strip-all,--no-entry          \
+    -Wl,--strip-all,--no-entry                      \
     -Wl,--export=on_init -Wl,--export=on_timer_callback \
     -Wl,--export=__heap_base,--export=__data_end \
     -o ui_app_wasi.wasm

+ 2 - 2
samples/littlevgl/wasm-apps/Makefile_wasm_app_no_wasi

@@ -52,8 +52,8 @@ all:
     --sysroot=$(WAMR_DIR)/wamr-sdk/app/libc-builtin-sysroot \
     -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
     -DLV_CONF_INCLUDE_SIMPLE                        \
-    -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework		\
+    -L$(APP_FRAMEWORK_DIR)/lib -lapp_framework      \
     -Wl,--allow-undefined                           \
-    -Wl,--no-threads,--strip-all,--no-entry -nostdlib    \
+    -Wl,--strip-all,--no-entry -nostdlib            \
     -Wl,--export=on_init -Wl,--export=on_timer_callback \
     -o ui_app_builtin_libc.wasm

+ 7 - 5
samples/multi-thread/wasm-apps/CMakeLists.txt

@@ -27,11 +27,13 @@ set (DEFINED_SYMBOLS
 "${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
 
 set (CMAKE_EXE_LINKER_FLAGS
-    "-Wl,--shared-memory,--max-memory=131072,       \
-    -Wl,--no-entry,--strip-all,--export=main,       \
-    -Wl,--export=__heap_base,--export=__data_end    \
-    -Wl,--export=__wasm_call_ctors  \
-    -Wl,--allow-undefined-file=${DEFINED_SYMBOLS}"
+     "-Wl,--shared-memory,--max-memory=131072,          \
+      -Wl,--no-entry,--strip-all,                       \
+      -Wl,--export=__heap_base,--export=__data_end      \
+      -Wl,--export=__wasm_call_ctors                    \
+      -Wl,--export=main -Wl,--export=__main_argc_argv   \
+      -Wl,--allow-undefined"
+      #-Wl,--allow-undefined-file=${DEFINED_SYMBOLS}"
 )
 
 add_executable(test.wasm  main.c)

+ 1 - 1
samples/simple/build.sh

@@ -153,7 +153,7 @@ OUT_FILE=${i%.*}.wasm
         --target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
         --sysroot=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot  \
         -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/out/$PROFILE/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \
-        -Wl,--no-threads,--strip-all,--no-entry -nostdlib \
+        -Wl,--strip-all,--no-entry -nostdlib \
         -Wl,--export=on_init -Wl,--export=on_destroy \
         -Wl,--export=on_request -Wl,--export=on_response \
         -Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \

+ 6 - 6
test-tools/component_test/suites/01-life-cycle/test-app/build.sh

@@ -20,11 +20,11 @@ OUT_FILE=${i%.*}.wasm
                         -Wno-int-conversion \
                         -I${APP_FRAMEWORK_DIR}/include \
                         -I${DEPS_DIR} \
-                        --target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
-                        --sysroot=${SDK_DIR}/app-sdk/libc-builtin-sysroot       \
-                        -L${APP_FRAMEWORK_DIR}/lib -lapp_framework      \
-                        -Wl,--allow-undefined-file=${SDK_DIR}/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt        \
-                        -Wl,--no-threads,--strip-all,--no-entry -nostdlib \
+                        -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
+                        --sysroot=${SDK_DIR}/app-sdk/libc-builtin-sysroot \
+                        -L${APP_FRAMEWORK_DIR}/lib -lapp_framework \
+                        -Wl,--allow-undefined-file=${SDK_DIR}/app-sdk/libc-builtin-sysroot/share/defined-symbols.txt \
+                        -Wl,--strip-all,--no-entry -nostdlib \
                         -Wl,--export=on_init -Wl,--export=on_destroy \
                         -Wl,--export=on_request -Wl,--export=on_response \
                         -Wl,--export=on_sensor_event -Wl,--export=on_timer_callback \
@@ -36,4 +36,4 @@ if [ -f ${OUT_FILE} ]; then
 else
         echo "build ${OUT_FILE} fail"
 fi
-done
+done

+ 1 - 1
wamr-sdk/app/wamr_toolchain.cmake

@@ -18,7 +18,7 @@ SET (CMAKE_CXX_COMPILER_TARGET      "wasm32")
 SET (CMAKE_CXX_COMPILER             "${WASI_SDK_DIR}/bin/clang++")
 
 SET (CMAKE_EXE_LINKER_FLAGS
-    "-Wl,--initial-memory=65536,--no-entry,--no-threads,--strip-all" CACHE INTERNAL "")
+    "-Wl,--initial-memory=65536,--no-entry,--strip-all" CACHE INTERNAL "")
 
 SET (CMAKE_LINKER  "${WASI_SDK_DIR}/bin/wasm-ld"                     CACHE INTERNAL "")
 SET (CMAKE_AR      "${WASI_SDK_DIR}/bin/llvm-ar"                     CACHE INTERNAL "")