diff options
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot')
26 files changed, 0 insertions, 11993 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/SConscript b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/SConscript deleted file mode 100644 index 790f28404..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/SConscript +++ /dev/null @@ -1,33 +0,0 @@ -# -# Copyright (c) 2021, RT-Thread Development Team -# -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -# - -from building import * -import re -Import('rtconfig') - -cwd = GetCurrentDir() - -src = Split(''' -aot_loader.c -aot_runtime.c -aot_intrinsic.c -''') - -if rtconfig.ARCH == 'arm': - if re.match('^cortex-m.*', rtconfig.CPU): - src += ['arch/aot_reloc_thumb.c'] - elif re.match('^cortex-a.*', rtconfig.CPU): - src += ['arch/aot_reloc_arm.c'] -elif rtconfig.ARCH == 'ia32': - src += ['arch/aot_reloc_x86_32.c'] - -CPPPATH = [cwd, cwd + '/../include'] - -CPPDEFINES = ['WASM_ENABLE_AOT=1'] - -group = DefineGroup('iwasm_aot', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) - -Return('group') diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.c deleted file mode 100644 index 319eeda5c..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.c +++ /dev/null @@ -1,765 +0,0 @@ -/* - * Copyright (C) 2021 XiaoMi Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_intrinsic.h" - -typedef struct { - const char *llvm_intrinsic; - const char *native_intrinsic; - uint64 flag; -} aot_intrinsic; - -/* clang-format off */ -static const aot_intrinsic g_intrinsic_mapping[] = { - { "llvm.experimental.constrained.fadd.f32", "aot_intrinsic_fadd_f32", AOT_INTRINSIC_FLAG_F32_FADD }, - { "llvm.experimental.constrained.fadd.f64", "aot_intrinsic_fadd_f64", AOT_INTRINSIC_FLAG_F64_FADD }, - { "llvm.experimental.constrained.fsub.f32", "aot_intrinsic_fsub_f32", AOT_INTRINSIC_FLAG_F32_FSUB }, - { "llvm.experimental.constrained.fsub.f64", "aot_intrinsic_fsub_f64", AOT_INTRINSIC_FLAG_F64_FSUB }, - { "llvm.experimental.constrained.fmul.f32", "aot_intrinsic_fmul_f32", AOT_INTRINSIC_FLAG_F32_FMUL }, - { "llvm.experimental.constrained.fmul.f64", "aot_intrinsic_fmul_f64", AOT_INTRINSIC_FLAG_F64_FMUL }, - { "llvm.experimental.constrained.fdiv.f32", "aot_intrinsic_fdiv_f32", AOT_INTRINSIC_FLAG_F32_FDIV }, - { "llvm.experimental.constrained.fdiv.f64", "aot_intrinsic_fdiv_f64", AOT_INTRINSIC_FLAG_F64_FDIV }, - { "llvm.fabs.f32", "aot_intrinsic_fabs_f32", AOT_INTRINSIC_FLAG_F32_FABS }, - { "llvm.fabs.f64", "aot_intrinsic_fabs_f64", AOT_INTRINSIC_FLAG_F64_FABS }, - { "llvm.ceil.f32", "aot_intrinsic_ceil_f32", AOT_INTRINSIC_FLAG_F32_CEIL }, - { "llvm.ceil.f64", "aot_intrinsic_ceil_f64", AOT_INTRINSIC_FLAG_F64_CEIL }, - { "llvm.floor.f32", "aot_intrinsic_floor_f32", AOT_INTRINSIC_FLAG_F32_FLOOR }, - { "llvm.floor.f64", "aot_intrinsic_floor_f64", AOT_INTRINSIC_FLAG_F64_FLOOR }, - { "llvm.trunc.f32", "aot_intrinsic_trunc_f32", AOT_INTRINSIC_FLAG_F32_TRUNC }, - { "llvm.trunc.f64", "aot_intrinsic_trunc_f64", AOT_INTRINSIC_FLAG_F64_TRUNC }, - { "llvm.rint.f32", "aot_intrinsic_rint_f32", AOT_INTRINSIC_FLAG_F32_RINT }, - { "llvm.rint.f64", "aot_intrinsic_rint_f64", AOT_INTRINSIC_FLAG_F64_RINT }, - { "llvm.sqrt.f32", "aot_intrinsic_sqrt_f32", AOT_INTRINSIC_FLAG_F32_SQRT }, - { "llvm.sqrt.f64", "aot_intrinsic_sqrt_f64", AOT_INTRINSIC_FLAG_F64_SQRT }, - { "llvm.copysign.f32", "aot_intrinsic_copysign_f32", AOT_INTRINSIC_FLAG_F32_COPYSIGN }, - { "llvm.copysign.f64", "aot_intrinsic_copysign_f64", AOT_INTRINSIC_FLAG_F64_COPYSIGN }, - { "llvm.minnum.f32", "aot_intrinsic_fmin_f32", AOT_INTRINSIC_FLAG_F32_MIN }, - { "llvm.minnum.f64", "aot_intrinsic_fmin_f64", AOT_INTRINSIC_FLAG_F64_MIN }, - { "llvm.maxnum.f32", "aot_intrinsic_fmax_f32", AOT_INTRINSIC_FLAG_F32_MAX }, - { "llvm.maxnum.f64", "aot_intrinsic_fmax_f64", AOT_INTRINSIC_FLAG_F64_MAX }, - { "llvm.ctlz.i32", "aot_intrinsic_clz_i32", AOT_INTRINSIC_FLAG_I32_CLZ }, - { "llvm.ctlz.i64", "aot_intrinsic_clz_i64", AOT_INTRINSIC_FLAG_I64_CLZ }, - { "llvm.cttz.i32", "aot_intrinsic_ctz_i32", AOT_INTRINSIC_FLAG_I32_CTZ }, - { "llvm.cttz.i64", "aot_intrinsic_ctz_i64", AOT_INTRINSIC_FLAG_I64_CTZ }, - { "llvm.ctpop.i32", "aot_intrinsic_popcnt_i32", AOT_INTRINSIC_FLAG_I32_POPCNT }, - { "llvm.ctpop.i64", "aot_intrinsic_popcnt_i64", AOT_INTRINSIC_FLAG_I64_POPCNT }, - { "f64_convert_i32_s", "aot_intrinsic_i32_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64 }, - { "f64_convert_i32_u", "aot_intrinsic_u32_to_f64", AOT_INTRINSIC_FLAG_U32_TO_F64 }, - { "f32_convert_i32_s", "aot_intrinsic_i32_to_f32", AOT_INTRINSIC_FLAG_I32_TO_F32 }, - { "f32_convert_i32_u", "aot_intrinsic_u32_to_f32", AOT_INTRINSIC_FLAG_U32_TO_F32 }, - { "f64_convert_i64_s", "aot_intrinsic_i64_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64 }, - { "f64_convert_i64_u", "aot_intrinsic_u64_to_f64", AOT_INTRINSIC_FLAG_U64_TO_F64 }, - { "f32_convert_i64_s", "aot_intrinsic_i64_to_f32", AOT_INTRINSIC_FLAG_I64_TO_F32 }, - { "f32_convert_i64_u", "aot_intrinsic_u64_to_f32", AOT_INTRINSIC_FLAG_U64_TO_F32 }, - { "i32_trunc_f32_u", "aot_intrinsic_f32_to_u32", AOT_INTRINSIC_FLAG_F32_TO_U32 }, - { "i32_trunc_f32_s", "aot_intrinsic_f32_to_i32", AOT_INTRINSIC_FLAG_F32_TO_I32 }, - { "i32_trunc_f64_u", "aot_intrinsic_f64_to_u32", AOT_INTRINSIC_FLAG_F64_TO_U32 }, - { "i32_trunc_f64_s", "aot_intrinsic_f64_to_i32", AOT_INTRINSIC_FLAG_F64_TO_I32 }, - { "i64_trunc_f64_u", "aot_intrinsic_f64_to_u64", AOT_INTRINSIC_FLAG_F64_TO_U64 }, - { "i64_trunc_f32_s", "aot_intrinsic_f32_to_i64", AOT_INTRINSIC_FLAG_F32_TO_I64 }, - { "i64_trunc_f32_u", "aot_intrinsic_f32_to_u64", AOT_INTRINSIC_FLAG_F32_TO_U64 }, - { "i64_trunc_f64_s", "aot_intrinsic_f64_to_i64", AOT_INTRINSIC_FLAG_F64_TO_I64 }, - { "f32_demote_f64", "aot_intrinsic_f64_to_f32", AOT_INTRINSIC_FLAG_F64_TO_F32 }, - { "f64_promote_f32", "aot_intrinsic_f32_to_f64", AOT_INTRINSIC_FLAG_F32_TO_F64 }, - { "f32_cmp", "aot_intrinsic_f32_cmp", AOT_INTRINSIC_FLAG_F32_CMP }, - { "f64_cmp", "aot_intrinsic_f64_cmp", AOT_INTRINSIC_FLAG_F64_CMP }, - { "i32.const", NULL, AOT_INTRINSIC_FLAG_I32_CONST }, - { "i64.const", NULL, AOT_INTRINSIC_FLAG_I64_CONST }, - { "f32.const", NULL, AOT_INTRINSIC_FLAG_F32_CONST }, - { "f64.const", NULL, AOT_INTRINSIC_FLAG_F64_CONST }, - { "i64.div_s", "aot_intrinsic_i64_div_s", AOT_INTRINSIC_FLAG_I64_DIV_S}, - { "i32.div_s", "aot_intrinsic_i32_div_s", AOT_INTRINSIC_FLAG_I32_DIV_S}, - { "i32.div_u", "aot_intrinsic_i32_div_u", AOT_INTRINSIC_FLAG_I32_DIV_U}, - { "i32.rem_s", "aot_intrinsic_i32_rem_s", AOT_INTRINSIC_FLAG_I32_REM_S}, - { "i32.rem_u", "aot_intrinsic_i32_rem_u", AOT_INTRINSIC_FLAG_I32_REM_U}, - { "i64.div_u", "aot_intrinsic_i64_div_u", AOT_INTRINSIC_FLAG_I64_DIV_U}, - { "i64.rem_s", "aot_intrinsic_i64_rem_s", AOT_INTRINSIC_FLAG_I64_REM_S}, - { "i64.rem_u", "aot_intrinsic_i64_rem_u", AOT_INTRINSIC_FLAG_I64_REM_U}, - { "i64.or", "aot_intrinsic_i64_bit_or", AOT_INTRINSIC_FLAG_I64_BIT_OR}, - { "i64.and", "aot_intrinsic_i64_bit_and", AOT_INTRINSIC_FLAG_I64_BIT_AND}, -}; -/* clang-format on */ - -static const uint32 g_intrinsic_count = - sizeof(g_intrinsic_mapping) / sizeof(aot_intrinsic); - -float32 -aot_intrinsic_fadd_f32(float32 a, float32 b) -{ - return a + b; -} - -float64 -aot_intrinsic_fadd_f64(float64 a, float64 b) -{ - return a + b; -} - -float32 -aot_intrinsic_fsub_f32(float32 a, float32 b) -{ - return a - b; -} - -float64 -aot_intrinsic_fsub_f64(float64 a, float64 b) -{ - return a - b; -} - -float32 -aot_intrinsic_fmul_f32(float32 a, float32 b) -{ - return a * b; -} - -float64 -aot_intrinsic_fmul_f64(float64 a, float64 b) -{ - return a * b; -} - -float32 -aot_intrinsic_fdiv_f32(float32 a, float32 b) -{ - return a / b; -} - -float64 -aot_intrinsic_fdiv_f64(float64 a, float64 b) -{ - return a / b; -} - -float32 -aot_intrinsic_fabs_f32(float32 a) -{ - return fabsf(a); -} - -float64 -aot_intrinsic_fabs_f64(float64 a) -{ - return fabs(a); -} - -float32 -aot_intrinsic_ceil_f32(float32 a) -{ - return ceilf(a); -} - -float64 -aot_intrinsic_ceil_f64(float64 a) -{ - return ceil(a); -} - -float32 -aot_intrinsic_floor_f32(float32 a) -{ - return floorf(a); -} - -float64 -aot_intrinsic_floor_f64(float64 a) -{ - return floor(a); -} - -float32 -aot_intrinsic_trunc_f32(float32 a) -{ - return truncf(a); -} - -float64 -aot_intrinsic_trunc_f64(float64 a) -{ - return trunc(a); -} - -float32 -aot_intrinsic_rint_f32(float32 a) -{ - return rintf(a); -} - -float64 -aot_intrinsic_rint_f64(float64 a) -{ - return rint(a); -} - -float32 -aot_intrinsic_sqrt_f32(float32 a) -{ - return sqrtf(a); -} - -float64 -aot_intrinsic_sqrt_f64(float64 a) -{ - return sqrt(a); -} - -float32 -aot_intrinsic_copysign_f32(float32 a, float32 b) -{ - return signbit(b) ? -fabsf(a) : fabsf(a); -} - -float64 -aot_intrinsic_copysign_f64(float64 a, float64 b) -{ - return signbit(b) ? -fabs(a) : fabs(a); -} - -float32 -aot_intrinsic_fmin_f32(float32 a, float32 b) -{ - if (isnan(a) || isnan(b)) - return NAN; - else if (a == 0 && a == b) - return signbit(a) ? a : b; - else - return a > b ? b : a; -} - -float64 -aot_intrinsic_fmin_f64(float64 a, float64 b) -{ - if (isnan(a) || isnan(b)) - return NAN; - else if (a == 0 && a == b) - return signbit(a) ? a : b; - else - return a > b ? b : a; -} - -float32 -aot_intrinsic_fmax_f32(float32 a, float32 b) -{ - if (isnan(a) || isnan(b)) - return NAN; - else if (a == 0 && a == b) - return signbit(a) ? b : a; - else - return a > b ? a : b; -} - -float64 -aot_intrinsic_fmax_f64(float64 a, float64 b) -{ - if (isnan(a) || isnan(b)) - return NAN; - else if (a == 0 && a == b) - return signbit(a) ? b : a; - else - return a > b ? a : b; -} - -uint32 -aot_intrinsic_clz_i32(uint32 type) -{ - uint32 num = 0; - if (type == 0) - return 32; - while (!(type & 0x80000000)) { - num++; - type <<= 1; - } - return num; -} - -uint32 -aot_intrinsic_clz_i64(uint64 type) -{ - uint32 num = 0; - if (type == 0) - return 64; - while (!(type & 0x8000000000000000LL)) { - num++; - type <<= 1; - } - return num; -} - -uint32 -aot_intrinsic_ctz_i32(uint32 type) -{ - uint32 num = 0; - if (type == 0) - return 32; - while (!(type & 1)) { - num++; - type >>= 1; - } - return num; -} - -uint32 -aot_intrinsic_ctz_i64(uint64 type) -{ - uint32 num = 0; - if (type == 0) - return 64; - while (!(type & 1)) { - num++; - type >>= 1; - } - return num; -} - -uint32 -aot_intrinsic_popcnt_i32(uint32 u) -{ - uint32 ret = 0; - while (u) { - u = (u & (u - 1)); - ret++; - } - return ret; -} - -uint32 -aot_intrinsic_popcnt_i64(uint64 u) -{ - uint32 ret = 0; - while (u) { - u = (u & (u - 1)); - ret++; - } - return ret; -} - -float32 -aot_intrinsic_i32_to_f32(int32 i) -{ - return (float32)i; -} - -float32 -aot_intrinsic_u32_to_f32(uint32 u) -{ - return (float32)u; -} - -float64 -aot_intrinsic_i32_to_f64(int32 i) -{ - return (float64)i; -} - -float64 -aot_intrinsic_u32_to_f64(uint32 u) -{ - return (float64)u; -} - -float32 -aot_intrinsic_i64_to_f32(int64 i) -{ - return (float32)i; -} - -float32 -aot_intrinsic_u64_to_f32(uint64 u) -{ - return (float32)u; -} - -float64 -aot_intrinsic_i64_to_f64(int64 i) -{ - return (float64)i; -} - -float64 -aot_intrinsic_u64_to_f64(uint64 u) -{ - return (float64)u; -} - -int32 -aot_intrinsic_f32_to_i32(float32 f) -{ - return (int32)f; -} - -uint32 -aot_intrinsic_f32_to_u32(float32 f) -{ - return (uint32)f; -} - -int64 -aot_intrinsic_f32_to_i64(float32 f) -{ - return (int64)f; -} - -uint64 -aot_intrinsic_f32_to_u64(float32 f) -{ - return (uint64)f; -} - -int32 -aot_intrinsic_f64_to_i32(float64 f) -{ - return (int32)f; -} - -uint32 -aot_intrinsic_f64_to_u32(float64 f) -{ - return (uint32)f; -} - -int64 -aot_intrinsic_f64_to_i64(float64 f) -{ - return (int64)f; -} - -uint64 -aot_intrinsic_f64_to_u64(float64 f) -{ - return (uint64)f; -} - -float64 -aot_intrinsic_f32_to_f64(float32 f) -{ - return (float64)f; -} - -float32 -aot_intrinsic_f64_to_f32(float64 f) -{ - return (float32)f; -} - -int32 -aot_intrinsic_f32_cmp(AOTFloatCond cond, float32 lhs, float32 rhs) -{ - switch (cond) { - case FLOAT_EQ: - return lhs == rhs ? 1 : 0; - - case FLOAT_LT: - return lhs < rhs ? 1 : 0; - - case FLOAT_GT: - return lhs > rhs ? 1 : 0; - - case FLOAT_LE: - return lhs <= rhs ? 1 : 0; - - case FLOAT_GE: - return lhs >= rhs ? 1 : 0; - - case FLOAT_NE: - return (isnan(lhs) || isnan(rhs) || lhs != rhs) ? 1 : 0; - - case FLOAT_UNO: - return (isnan(lhs) || isnan(rhs)) ? 1 : 0; - - default: - break; - } - return 0; -} - -int32 -aot_intrinsic_f64_cmp(AOTFloatCond cond, float64 lhs, float64 rhs) -{ - switch (cond) { - case FLOAT_EQ: - return lhs == rhs ? 1 : 0; - - case FLOAT_LT: - return lhs < rhs ? 1 : 0; - - case FLOAT_GT: - return lhs > rhs ? 1 : 0; - - case FLOAT_LE: - return lhs <= rhs ? 1 : 0; - - case FLOAT_GE: - return lhs >= rhs ? 1 : 0; - - case FLOAT_NE: - return (isnan(lhs) || isnan(rhs) || lhs != rhs) ? 1 : 0; - - case FLOAT_UNO: - return (isnan(lhs) || isnan(rhs)) ? 1 : 0; - - default: - break; - } - return 0; -} - -int64 -aot_intrinsic_i64_div_s(int64 l, int64 r) -{ - return l / r; -} - -int32 -aot_intrinsic_i32_div_s(int32 l, int32 r) -{ - return l / r; -} - -uint32 -aot_intrinsic_i32_div_u(uint32 l, uint32 r) -{ - return l / r; -} - -int32 -aot_intrinsic_i32_rem_s(int32 l, int32 r) -{ - return l % r; -} - -uint32 -aot_intrinsic_i32_rem_u(uint32 l, uint32 r) -{ - return l % r; -} - -uint64 -aot_intrinsic_i64_div_u(uint64 l, uint64 r) -{ - return l / r; -} - -int64 -aot_intrinsic_i64_rem_s(int64 l, int64 r) -{ - return l % r; -} - -uint64 -aot_intrinsic_i64_rem_u(uint64 l, uint64 r) -{ - return l % r; -} - -uint64 -aot_intrinsic_i64_bit_or(uint64 l, uint64 r) -{ - return l | r; -} - -uint64 -aot_intrinsic_i64_bit_and(uint64 l, uint64 r) -{ - return l & r; -} - -const char * -aot_intrinsic_get_symbol(const char *llvm_intrinsic) -{ - uint32 cnt; - for (cnt = 0; cnt < g_intrinsic_count; cnt++) { - if (!strcmp(llvm_intrinsic, g_intrinsic_mapping[cnt].llvm_intrinsic)) { - return g_intrinsic_mapping[cnt].native_intrinsic; - } - } - return NULL; -} - -#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 - -static void -add_intrinsic_capability(AOTCompContext *comp_ctx, uint64 flag) -{ - uint64 group = AOT_INTRINSIC_GET_GROUP_FROM_FLAG(flag); - if (group < sizeof(comp_ctx->flags) / sizeof(uint64)) { - comp_ctx->flags[group] |= flag; - } - else { - bh_log(BH_LOG_LEVEL_WARNING, __FILE__, __LINE__, - "intrinsic exceeds max limit."); - } -} - -static void -add_i64_common_intrinsics(AOTCompContext *comp_ctx) -{ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_DIV_S); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_DIV_U); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_REM_S); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_REM_U); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_BIT_OR); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_BIT_AND); -} - -static void -add_i32_common_intrinsics(AOTCompContext *comp_ctx) -{ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_DIV_S); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_DIV_U); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_REM_S); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_REM_U); -} - -static void -add_f32_common_intrinsics(AOTCompContext *comp_ctx) -{ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FABS); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FADD); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FSUB); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FMUL); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FDIV); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_SQRT); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_CMP); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_MIN); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_MAX); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_CEIL); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_FLOOR); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TRUNC); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_RINT); -} - -static void -add_f64_common_intrinsics(AOTCompContext *comp_ctx) -{ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FABS); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FADD); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FSUB); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FMUL); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_MIN); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_MAX); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CEIL); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FLOOR); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TRUNC); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_RINT); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FDIV); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_SQRT); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CMP); -} - -static void -add_common_float_integer_convertion(AOTCompContext *comp_ctx) -{ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_TO_F32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_U32_TO_F32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_TO_F64); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_U32_TO_F64); - - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_TO_F32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_U64_TO_F32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_TO_F64); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_U64_TO_F64); - - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TO_I32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TO_U32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TO_I64); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TO_U64); - - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TO_I32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TO_U32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TO_I64); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TO_U64); - - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TO_F32); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_TO_F64); -} - -bool -aot_intrinsic_check_capability(const AOTCompContext *comp_ctx, - const char *llvm_intrinsic) -{ - uint32 cnt; - uint64 flag; - uint64 group; - - for (cnt = 0; cnt < g_intrinsic_count; cnt++) { - if (!strcmp(llvm_intrinsic, g_intrinsic_mapping[cnt].llvm_intrinsic)) { - flag = g_intrinsic_mapping[cnt].flag; - group = AOT_INTRINSIC_GET_GROUP_FROM_FLAG(flag); - flag &= AOT_INTRINSIC_FLAG_MASK; - if (group < sizeof(comp_ctx->flags) / sizeof(uint64)) { - if (comp_ctx->flags[group] & flag) { - return true; - } - } - else { - bh_log(BH_LOG_LEVEL_WARNING, __FILE__, __LINE__, - "intrinsic exceeds max limit."); - } - } - } - return false; -} - -void -aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx) -{ - memset(comp_ctx->flags, 0, sizeof(comp_ctx->flags)); - - if (!comp_ctx->target_cpu) - return; - - if (!strncmp(comp_ctx->target_arch, "thumb", 5)) { - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_CONST); - add_i32_common_intrinsics(comp_ctx); - if (!strcmp(comp_ctx->target_cpu, "cortex-m7")) { - } - else if (!strcmp(comp_ctx->target_cpu, "cortex-m4")) { - add_f64_common_intrinsics(comp_ctx); - } - else { - add_f32_common_intrinsics(comp_ctx); - add_f64_common_intrinsics(comp_ctx); - add_i64_common_intrinsics(comp_ctx); - add_common_float_integer_convertion(comp_ctx); - } - } - else if (!strncmp(comp_ctx->target_arch, "riscv", 5)) { - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_CONST); - /* - * Note: Use builtin intrinsics since hardware float operation - * will cause rodata relocation - */ - add_f32_common_intrinsics(comp_ctx); - add_f64_common_intrinsics(comp_ctx); - add_common_float_integer_convertion(comp_ctx); - if (!strncmp(comp_ctx->target_arch, "riscv32", 7)) { - add_i64_common_intrinsics(comp_ctx); - } - } - else if (!strncmp(comp_ctx->target_arch, "xtensa", 6)) { - /* - * Note: Use builtin intrinsics since hardware float operation - * will cause rodata relocation - */ - add_f32_common_intrinsics(comp_ctx); - add_i32_common_intrinsics(comp_ctx); - add_f64_common_intrinsics(comp_ctx); - add_i64_common_intrinsics(comp_ctx); - add_common_float_integer_convertion(comp_ctx); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_CONST); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CONST); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_CONST); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_CONST); - } - else { - /* - * Use constant value table by default - */ - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F32_CONST); - add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CONST); - } -} - -#endif /* WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.h deleted file mode 100644 index 2123058b9..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_intrinsic.h +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright (C) 2021 XiaoMi Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _AOT_INTRINSIC_H -#define _AOT_INTRINSIC_H - -#include "aot_runtime.h" -#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 -#include "aot_llvm.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#define AOT_INTRINSIC_GROUPS 2 - -/* Use uint64 as flag container: - * - The upper 16 bits are the intrinsic group number - * - The lower 48 bits are the intrinsic capability mask - */ - -#define AOT_INTRINSIC_FLAG(group, number) \ - ((((uint64)(group & 0xffffLL)) << 48) | ((uint64)1 << number)) - -#define AOT_INTRINSIC_FLAG_MASK (0x0000ffffffffffffLL) - -#define AOT_INTRINSIC_GET_GROUP_FROM_FLAG(flag) \ - ((((uint64)flag) >> 48) & 0xffffLL) - -/* clang-format off */ -#define AOT_INTRINSIC_FLAG_F32_FADD AOT_INTRINSIC_FLAG(0, 0) -#define AOT_INTRINSIC_FLAG_F32_FSUB AOT_INTRINSIC_FLAG(0, 1) -#define AOT_INTRINSIC_FLAG_F32_FMUL AOT_INTRINSIC_FLAG(0, 2) -#define AOT_INTRINSIC_FLAG_F32_FDIV AOT_INTRINSIC_FLAG(0, 3) -#define AOT_INTRINSIC_FLAG_F32_FABS AOT_INTRINSIC_FLAG(0, 4) -#define AOT_INTRINSIC_FLAG_F32_CEIL AOT_INTRINSIC_FLAG(0, 5) -#define AOT_INTRINSIC_FLAG_F32_FLOOR AOT_INTRINSIC_FLAG(0, 6) -#define AOT_INTRINSIC_FLAG_F32_TRUNC AOT_INTRINSIC_FLAG(0, 7) -#define AOT_INTRINSIC_FLAG_F32_RINT AOT_INTRINSIC_FLAG(0, 8) -#define AOT_INTRINSIC_FLAG_F32_SQRT AOT_INTRINSIC_FLAG(0, 9) -#define AOT_INTRINSIC_FLAG_F32_COPYSIGN AOT_INTRINSIC_FLAG(0, 10) -#define AOT_INTRINSIC_FLAG_F32_MIN AOT_INTRINSIC_FLAG(0, 11) -#define AOT_INTRINSIC_FLAG_F32_MAX AOT_INTRINSIC_FLAG(0, 12) -#define AOT_INTRINSIC_FLAG_I32_CLZ AOT_INTRINSIC_FLAG(0, 13) -#define AOT_INTRINSIC_FLAG_I32_CTZ AOT_INTRINSIC_FLAG(0, 14) -#define AOT_INTRINSIC_FLAG_I32_POPCNT AOT_INTRINSIC_FLAG(0, 15) -#define AOT_INTRINSIC_FLAG_I32_TO_F32 AOT_INTRINSIC_FLAG(0, 16) -#define AOT_INTRINSIC_FLAG_U32_TO_F32 AOT_INTRINSIC_FLAG(0, 17) -#define AOT_INTRINSIC_FLAG_I32_TO_F64 AOT_INTRINSIC_FLAG(0, 18) -#define AOT_INTRINSIC_FLAG_U32_TO_F64 AOT_INTRINSIC_FLAG(0, 19) -#define AOT_INTRINSIC_FLAG_F32_TO_I32 AOT_INTRINSIC_FLAG(0, 20) -#define AOT_INTRINSIC_FLAG_F32_TO_U32 AOT_INTRINSIC_FLAG(0, 21) -#define AOT_INTRINSIC_FLAG_F32_TO_I64 AOT_INTRINSIC_FLAG(0, 22) -#define AOT_INTRINSIC_FLAG_F32_TO_U64 AOT_INTRINSIC_FLAG(0, 23) -#define AOT_INTRINSIC_FLAG_F32_TO_F64 AOT_INTRINSIC_FLAG(0, 24) -#define AOT_INTRINSIC_FLAG_F32_CMP AOT_INTRINSIC_FLAG(0, 25) -#define AOT_INTRINSIC_FLAG_F32_CONST AOT_INTRINSIC_FLAG(0, 26) -#define AOT_INTRINSIC_FLAG_I32_CONST AOT_INTRINSIC_FLAG(0, 27) -#define AOT_INTRINSIC_FLAG_I32_DIV_U AOT_INTRINSIC_FLAG(0, 28) -#define AOT_INTRINSIC_FLAG_I32_REM_S AOT_INTRINSIC_FLAG(0, 29) -#define AOT_INTRINSIC_FLAG_I32_REM_U AOT_INTRINSIC_FLAG(0, 30) -#define AOT_INTRINSIC_FLAG_I32_DIV_S AOT_INTRINSIC_FLAG(0, 31) - -#define AOT_INTRINSIC_FLAG_F64_FADD AOT_INTRINSIC_FLAG(1, 0) -#define AOT_INTRINSIC_FLAG_F64_FSUB AOT_INTRINSIC_FLAG(1, 1) -#define AOT_INTRINSIC_FLAG_F64_FMUL AOT_INTRINSIC_FLAG(1, 2) -#define AOT_INTRINSIC_FLAG_F64_FDIV AOT_INTRINSIC_FLAG(1, 3) -#define AOT_INTRINSIC_FLAG_F64_FABS AOT_INTRINSIC_FLAG(1, 4) -#define AOT_INTRINSIC_FLAG_F64_CEIL AOT_INTRINSIC_FLAG(1, 5) -#define AOT_INTRINSIC_FLAG_F64_FLOOR AOT_INTRINSIC_FLAG(1, 6) -#define AOT_INTRINSIC_FLAG_F64_TRUNC AOT_INTRINSIC_FLAG(1, 7) -#define AOT_INTRINSIC_FLAG_F64_RINT AOT_INTRINSIC_FLAG(1, 8) -#define AOT_INTRINSIC_FLAG_F64_SQRT AOT_INTRINSIC_FLAG(1, 9) -#define AOT_INTRINSIC_FLAG_F64_COPYSIGN AOT_INTRINSIC_FLAG(1, 10) -#define AOT_INTRINSIC_FLAG_F64_MIN AOT_INTRINSIC_FLAG(1, 11) -#define AOT_INTRINSIC_FLAG_F64_MAX AOT_INTRINSIC_FLAG(1, 12) -#define AOT_INTRINSIC_FLAG_I64_CLZ AOT_INTRINSIC_FLAG(1, 13) -#define AOT_INTRINSIC_FLAG_I64_CTZ AOT_INTRINSIC_FLAG(1, 14) -#define AOT_INTRINSIC_FLAG_I64_POPCNT AOT_INTRINSIC_FLAG(1, 15) -#define AOT_INTRINSIC_FLAG_I64_TO_F32 AOT_INTRINSIC_FLAG(1, 16) -#define AOT_INTRINSIC_FLAG_U64_TO_F32 AOT_INTRINSIC_FLAG(1, 17) -#define AOT_INTRINSIC_FLAG_I64_TO_F64 AOT_INTRINSIC_FLAG(1, 18) -#define AOT_INTRINSIC_FLAG_U64_TO_F64 AOT_INTRINSIC_FLAG(1, 19) -#define AOT_INTRINSIC_FLAG_F64_TO_I32 AOT_INTRINSIC_FLAG(1, 20) -#define AOT_INTRINSIC_FLAG_F64_TO_U32 AOT_INTRINSIC_FLAG(1, 21) -#define AOT_INTRINSIC_FLAG_F64_TO_I64 AOT_INTRINSIC_FLAG(1, 22) -#define AOT_INTRINSIC_FLAG_F64_TO_U64 AOT_INTRINSIC_FLAG(1, 23) -#define AOT_INTRINSIC_FLAG_F64_TO_F32 AOT_INTRINSIC_FLAG(1, 24) -#define AOT_INTRINSIC_FLAG_F64_CMP AOT_INTRINSIC_FLAG(1, 25) -#define AOT_INTRINSIC_FLAG_F64_CONST AOT_INTRINSIC_FLAG(1, 26) -#define AOT_INTRINSIC_FLAG_I64_CONST AOT_INTRINSIC_FLAG(1, 27) -#define AOT_INTRINSIC_FLAG_I64_DIV_S AOT_INTRINSIC_FLAG(1, 28) -#define AOT_INTRINSIC_FLAG_I64_DIV_U AOT_INTRINSIC_FLAG(1, 29) -#define AOT_INTRINSIC_FLAG_I64_REM_S AOT_INTRINSIC_FLAG(1, 30) -#define AOT_INTRINSIC_FLAG_I64_REM_U AOT_INTRINSIC_FLAG(1, 31) -#define AOT_INTRINSIC_FLAG_I64_BIT_OR AOT_INTRINSIC_FLAG(1, 32) -#define AOT_INTRINSIC_FLAG_I64_BIT_AND AOT_INTRINSIC_FLAG(1, 33) - -/* clang-format on */ - -float32 -aot_intrinsic_fadd_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fadd_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fsub_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fsub_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fmul_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fmul_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fdiv_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fdiv_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fabs_f32(float32 a); - -float64 -aot_intrinsic_fabs_f64(float64 a); - -float32 -aot_intrinsic_ceil_f32(float32 a); - -float64 -aot_intrinsic_ceil_f64(float64 a); - -float32 -aot_intrinsic_floor_f32(float32 a); - -float64 -aot_intrinsic_floor_f64(float64 a); - -float32 -aot_intrinsic_trunc_f32(float32 a); - -float64 -aot_intrinsic_trunc_f64(float64 a); - -float32 -aot_intrinsic_rint_f32(float32 a); - -float64 -aot_intrinsic_rint_f64(float64 a); - -float32 -aot_intrinsic_sqrt_f32(float32 a); - -float64 -aot_intrinsic_sqrt_f64(float64 a); - -float32 -aot_intrinsic_copysign_f32(float32 a, float32 b); - -float64 -aot_intrinsic_copysign_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fmin_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fmin_f64(float64 a, float64 b); - -float32 -aot_intrinsic_fmax_f32(float32 a, float32 b); - -float64 -aot_intrinsic_fmax_f64(float64 a, float64 b); - -uint32 -aot_intrinsic_clz_i32(uint32 type); - -uint32 -aot_intrinsic_clz_i64(uint64 type); - -uint32 -aot_intrinsic_ctz_i32(uint32 type); - -uint32 -aot_intrinsic_ctz_i64(uint64 type); - -uint32 -aot_intrinsic_popcnt_i32(uint32 u); - -uint32 -aot_intrinsic_popcnt_i64(uint64 u); - -float32 -aot_intrinsic_i32_to_f32(int32 i); - -float32 -aot_intrinsic_u32_to_f32(uint32 u); - -float64 -aot_intrinsic_i32_to_f64(int32 i); - -float64 -aot_intrinsic_u32_to_f64(uint32 u); - -float32 -aot_intrinsic_i64_to_f32(int64 i); - -float32 -aot_intrinsic_u64_to_f32(uint64 u); - -float64 -aot_intrinsic_i64_to_f64(int64 i); - -float64 -aot_intrinsic_u64_to_f64(uint64 u); - -int32 -aot_intrinsic_f32_to_i32(float32 f); - -uint32 -aot_intrinsic_f32_to_u32(float32 f); - -int64 -aot_intrinsic_f32_to_i64(float32 f); - -uint64 -aot_intrinsic_f32_to_u64(float32 f); - -int32 -aot_intrinsic_f64_to_i32(float64 f); - -uint32 -aot_intrinsic_f64_to_u32(float64 f); - -int64 -aot_intrinsic_f64_to_i64(float64 f); - -uint64 -aot_intrinsic_f64_to_u64(float64 f); - -float64 -aot_intrinsic_f32_to_f64(float32 f); - -float32 -aot_intrinsic_f64_to_f32(float64 f); - -int32 -aot_intrinsic_f32_cmp(AOTFloatCond cond, float32 lhs, float32 rhs); - -int32 -aot_intrinsic_f64_cmp(AOTFloatCond cond, float64 lhs, float64 rhs); - -int64 -aot_intrinsic_i64_div_s(int64 l, int64 r); - -int32 -aot_intrinsic_i32_div_s(int32 l, int32 r); - -uint32 -aot_intrinsic_i32_div_u(uint32 l, uint32 r); - -int32 -aot_intrinsic_i32_rem_s(int32 l, int32 r); - -uint32 -aot_intrinsic_i32_rem_u(uint32 l, uint32 r); - -uint64 -aot_intrinsic_i64_div_u(uint64 l, uint64 r); - -int64 -aot_intrinsic_i64_rem_s(int64 l, int64 r); - -uint64 -aot_intrinsic_i64_rem_u(uint64 l, uint64 r); - -uint64 -aot_intrinsic_i64_bit_or(uint64 l, uint64 r); - -uint64 -aot_intrinsic_i64_bit_and(uint64 l, uint64 r); - -const char * -aot_intrinsic_get_symbol(const char *llvm_intrinsic); - -#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 -bool -aot_intrinsic_check_capability(const AOTCompContext *comp_ctx, - const char *llvm_intrinsic); - -void -aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* end of _AOT_INTRINSIC_H */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_loader.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_loader.c deleted file mode 100644 index 5345fb2d7..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_loader.c +++ /dev/null @@ -1,3035 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_runtime.h" -#include "bh_common.h" -#include "bh_log.h" -#include "aot_reloc.h" -#include "../common/wasm_runtime_common.h" -#include "../common/wasm_native.h" -#include "../compilation/aot.h" - -#if WASM_ENABLE_DEBUG_AOT != 0 -#include "debug/elf_parser.h" -#include "debug/jit_debug.h" -#endif - -#define YMM_PLT_PREFIX "__ymm@" -#define XMM_PLT_PREFIX "__xmm@" -#define REAL_PLT_PREFIX "__real@" - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) { - snprintf(error_buf, error_buf_size, "AOT module load failed: %s", - string); - } -} - -static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) -{ - va_list args; - char buf[128]; - - if (error_buf != NULL) { - va_start(args, format); - vsnprintf(buf, sizeof(buf), format, args); - va_end(args); - snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf); - } -} - -#define exchange_uint8(p_data) (void)0 - -static void -exchange_uint16(uint8 *p_data) -{ - uint8 value = *p_data; - *p_data = *(p_data + 1); - *(p_data + 1) = value; -} - -static void -exchange_uint32(uint8 *p_data) -{ - uint8 value = *p_data; - *p_data = *(p_data + 3); - *(p_data + 3) = value; - - value = *(p_data + 1); - *(p_data + 1) = *(p_data + 2); - *(p_data + 2) = value; -} - -static void -exchange_uint64(uint8 *pData) -{ - uint32 value; - - value = *(uint32 *)pData; - *(uint32 *)pData = *(uint32 *)(pData + 4); - *(uint32 *)(pData + 4) = value; - exchange_uint32(pData); - exchange_uint32(pData + 4); -} - -static union { - int a; - char b; -} __ue = { .a = 1 }; - -#define is_little_endian() (__ue.b == 1) - -static bool -check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, - char *error_buf, uint32 error_buf_size) -{ - if ((uintptr_t)buf + length < (uintptr_t)buf - || (uintptr_t)buf + length > (uintptr_t)buf_end) { - set_error_buf(error_buf, error_buf_size, "unexpect end"); - return false; - } - return true; -} - -#define CHECK_BUF(buf, buf_end, length) \ - do { \ - if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \ - goto fail; \ - } \ - } while (0) - -static uint8 * -align_ptr(const uint8 *p, uint32 b) -{ - uintptr_t v = (uintptr_t)p; - uintptr_t m = b - 1; - return (uint8 *)((v + m) & ~m); -} - -static inline uint64 -GET_U64_FROM_ADDR(uint32 *addr) -{ - union { - uint64 val; - uint32 parts[2]; - } u; - u.parts[0] = addr[0]; - u.parts[1] = addr[1]; - return u.val; -} - -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - -static inline uint8 -GET_U8_FROM_ADDR(const uint8 *p) -{ - uint8 res = 0; - bh_assert(p); - - const uint8 *p_aligned = align_ptr(p, 4); - p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned; - - uint32 buf32 = *(const uint32 *)p_aligned; - const uint8 *pbuf = (const uint8 *)&buf32; - - res = *(uint8 *)(pbuf + (p - p_aligned)); - - return res; -} - -static inline uint16 -GET_U16_FROM_ADDR(const uint8 *p) -{ - uint16 res = 0; - bh_assert(p); - - const uint8 *p_aligned = align_ptr(p, 4); - p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned; - - uint32 buf32 = *(const uint32 *)p_aligned; - const uint8 *pbuf = (const uint8 *)&buf32; - - res = *(uint16 *)(pbuf + (p - p_aligned)); - - return res; -} - -#define TEMPLATE_READ(p, p_end, res, type) \ - do { \ - if (sizeof(type) != sizeof(uint64)) \ - p = (uint8 *)align_ptr(p, sizeof(type)); \ - else \ - /* align 4 bytes if type is uint64 */ \ - p = (uint8 *)align_ptr(p, sizeof(uint32)); \ - CHECK_BUF(p, p_end, sizeof(type)); \ - if (sizeof(type) == sizeof(uint8)) \ - res = GET_U8_FROM_ADDR(p); \ - else if (sizeof(type) == sizeof(uint16)) \ - res = GET_U16_FROM_ADDR(p); \ - else if (sizeof(type) == sizeof(uint32)) \ - res = *(type *)p; \ - else \ - res = (type)GET_U64_FROM_ADDR((uint32 *)p); \ - if (!is_little_endian()) \ - exchange_##type((uint8 *)&res); \ - p += sizeof(type); \ - } while (0) - -#define read_byte_array(p, p_end, addr, len) \ - do { \ - CHECK_BUF(p, p_end, len); \ - bh_memcpy_wa(addr, len, p, len); \ - p += len; \ - } while (0) - -#define read_string(p, p_end, str) \ - do { \ - if (!(str = load_string((uint8 **)&p, p_end, module, \ - is_load_from_file_buf, true, error_buf, \ - error_buf_size))) \ - goto fail; \ - } while (0) - -#else /* else of (WASM_ENABLE_WORD_ALIGN_READ != 0) */ - -#define TEMPLATE_READ(p, p_end, res, type) \ - do { \ - if (sizeof(type) != sizeof(uint64)) \ - p = (uint8 *)align_ptr(p, sizeof(type)); \ - else \ - /* align 4 bytes if type is uint64 */ \ - p = (uint8 *)align_ptr(p, sizeof(uint32)); \ - CHECK_BUF(p, p_end, sizeof(type)); \ - if (sizeof(type) != sizeof(uint64)) \ - res = *(type *)p; \ - else \ - res = (type)GET_U64_FROM_ADDR((uint32 *)p); \ - if (!is_little_endian()) \ - exchange_##type((uint8 *)&res); \ - p += sizeof(type); \ - } while (0) - -#define read_byte_array(p, p_end, addr, len) \ - do { \ - CHECK_BUF(p, p_end, len); \ - bh_memcpy_s(addr, len, p, len); \ - p += len; \ - } while (0) - -#define read_string(p, p_end, str) \ - do { \ - if (!(str = load_string((uint8 **)&p, p_end, module, \ - is_load_from_file_buf, error_buf, \ - error_buf_size))) \ - goto fail; \ - } while (0) - -#endif /* end of (WASM_ENABLE_WORD_ALIGN_READ != 0) */ - -#define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8) -#define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16) -#define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32) -#define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64) - -/* Legal values for bin_type */ -#define BIN_TYPE_ELF32L 0 /* 32-bit little endian */ -#define BIN_TYPE_ELF32B 1 /* 32-bit big endian */ -#define BIN_TYPE_ELF64L 2 /* 64-bit little endian */ -#define BIN_TYPE_ELF64B 3 /* 64-bit big endian */ -#define BIN_TYPE_COFF32 4 /* 32-bit little endian */ -#define BIN_TYPE_COFF64 6 /* 64-bit little endian */ - -/* Legal values for e_type (object file type). */ -#define E_TYPE_NONE 0 /* No file type */ -#define E_TYPE_REL 1 /* Relocatable file */ -#define E_TYPE_EXEC 2 /* Executable file */ -#define E_TYPE_DYN 3 /* Shared object file */ -#define E_TYPE_XIP 4 /* eXecute In Place file */ - -/* Legal values for e_machine (architecture). */ -#define E_MACHINE_386 3 /* Intel 80386 */ -#define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */ -#define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ -#define E_MACHINE_ARM 40 /* ARM/Thumb */ -#define E_MACHINE_AARCH64 183 /* AArch64 */ -#define E_MACHINE_ARC 45 /* Argonaut RISC Core */ -#define E_MACHINE_IA_64 50 /* Intel Merced */ -#define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */ -#define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */ -#define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */ -#define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */ -#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */ -#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */ -#define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */ -#define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */ - -/* Legal values for e_version */ -#define E_VERSION_CURRENT 1 /* Current version */ - -static void * -loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) -{ - void *mem; - - if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, "allocate memory failed"); - return NULL; - } - - memset(mem, 0, (uint32)size); - return mem; -} - -static char * -const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - bool is_vram_word_align, -#endif - char *error_buf, uint32 error_buf_size) -{ - HashMap *set = module->const_str_set; - char *c_str, *value; - - /* Create const string set if it isn't created */ - if (!set - && !(set = module->const_str_set = bh_hash_map_create( - 32, false, (HashFunc)wasm_string_hash, - (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) { - set_error_buf(error_buf, error_buf_size, - "create const string set failed"); - return NULL; - } - - /* Lookup const string set, use the string if found */ - if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) { - return NULL; - } -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - if (is_vram_word_align) { - bh_memcpy_wa(c_str, (uint32)(len + 1), str, (uint32)len); - } - else -#endif - { - bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len); - } - c_str[len] = '\0'; - - if ((value = bh_hash_map_find(set, c_str))) { - wasm_runtime_free(c_str); - return value; - } - - if (!bh_hash_map_insert(set, c_str, c_str)) { - set_error_buf(error_buf, error_buf_size, - "insert string to hash map failed"); - wasm_runtime_free(c_str); - return NULL; - } - - return c_str; -} - -static char * -load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - bool is_vram_word_align, -#endif - char *error_buf, uint32 error_buf_size) -{ - uint8 *p = *p_buf; - const uint8 *p_end = buf_end; - char *str; - uint16 str_len; - - read_uint16(p, p_end, str_len); - CHECK_BUF(p, p_end, str_len); - - if (str_len == 0) { - str = ""; - } -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - else if (is_vram_word_align) { - if (!(str = const_str_set_insert((uint8 *)p, str_len, module, - is_vram_word_align, error_buf, - error_buf_size))) { - goto fail; - } - } -#endif - else if (p[str_len - 1] == '\0') { - /* The string is terminated with '\0', use it directly */ - str = (char *)p; - } - else if (is_load_from_file_buf) { - /* As the file buffer can be referred to after loading, - we use the 2 bytes of size to adjust the string: - move string 2 byte backward and then append '\0' */ - str = (char *)(p - 2); - bh_memmove_s(str, (uint32)(str_len + 1), p, (uint32)str_len); - str[str_len] = '\0'; - } - else { - /* Load from sections, the file buffer cannot be reffered to - after loading, we must create another string and insert it - into const string set */ - if (!(str = const_str_set_insert((uint8 *)p, str_len, module, -#if (WASM_ENABLE_WORD_ALIGN_READ != 0) - is_vram_word_align, -#endif - error_buf, error_buf_size))) { - goto fail; - } - } - p += str_len; - - *p_buf = p; - return str; -fail: - return NULL; -} - -static bool -get_aot_file_target(AOTTargetInfo *target_info, char *target_buf, - uint32 target_buf_size, char *error_buf, - uint32 error_buf_size) -{ - char *machine_type = NULL; - switch (target_info->e_machine) { - case E_MACHINE_X86_64: - case E_MACHINE_WIN_X86_64: - machine_type = "x86_64"; - break; - case E_MACHINE_386: - case E_MACHINE_WIN_I386: - machine_type = "i386"; - break; - case E_MACHINE_ARM: - case E_MACHINE_AARCH64: - machine_type = target_info->arch; - break; - case E_MACHINE_MIPS: - machine_type = "mips"; - break; - case E_MACHINE_XTENSA: - machine_type = "xtensa"; - break; - case E_MACHINE_RISCV: - machine_type = "riscv"; - break; - case E_MACHINE_ARC_COMPACT: - case E_MACHINE_ARC_COMPACT2: - machine_type = "arc"; - break; - default: - set_error_buf_v(error_buf, error_buf_size, - "unknown machine type %d", target_info->e_machine); - return false; - } - if (strncmp(target_info->arch, machine_type, strlen(machine_type))) { - set_error_buf_v( - error_buf, error_buf_size, - "machine type (%s) isn't consistent with target type (%s)", - machine_type, target_info->arch); - return false; - } - snprintf(target_buf, target_buf_size, "%s", target_info->arch); - return true; -} - -static bool -check_machine_info(AOTTargetInfo *target_info, char *error_buf, - uint32 error_buf_size) -{ - char target_expected[32], target_got[32]; - - get_current_target(target_expected, sizeof(target_expected)); - - if (!get_aot_file_target(target_info, target_got, sizeof(target_got), - error_buf, error_buf_size)) - return false; - - if (strncmp(target_expected, target_got, strlen(target_expected))) { - set_error_buf_v(error_buf, error_buf_size, - "invalid target type, expected %s but got %s", - target_expected, target_got); - return false; - } - - return true; -} - -static bool -load_target_info_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) -{ - AOTTargetInfo target_info; - const uint8 *p = buf, *p_end = buf_end; - bool is_target_little_endian, is_target_64_bit; - - read_uint16(p, p_end, target_info.bin_type); - read_uint16(p, p_end, target_info.abi_type); - read_uint16(p, p_end, target_info.e_type); - read_uint16(p, p_end, target_info.e_machine); - read_uint32(p, p_end, target_info.e_version); - read_uint32(p, p_end, target_info.e_flags); - read_uint32(p, p_end, target_info.reserved); - read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch)); - - if (p != buf_end) { - set_error_buf(error_buf, error_buf_size, "invalid section size"); - return false; - } - - /* Check target endian type */ - is_target_little_endian = target_info.bin_type & 1 ? false : true; - if (is_little_endian() != is_target_little_endian) { - set_error_buf_v(error_buf, error_buf_size, - "invalid target endian type, expected %s but got %s", - is_little_endian() ? "little endian" : "big endian", - is_target_little_endian ? "little endian" - : "big endian"); - return false; - } - - /* Check target bit width */ - is_target_64_bit = target_info.bin_type & 2 ? true : false; - if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) { - set_error_buf_v(error_buf, error_buf_size, - "invalid target bit width, expected %s but got %s", - sizeof(void *) == 8 ? "64-bit" : "32-bit", - is_target_64_bit ? "64-bit" : "32-bit"); - return false; - } - - /* Check target elf file type */ - if (target_info.e_type != E_TYPE_REL && target_info.e_type != E_TYPE_XIP) { - set_error_buf(error_buf, error_buf_size, - "invalid object file type, " - "expected relocatable or XIP file type but got others"); - return false; - } - - /* Check machine info */ - if (!check_machine_info(&target_info, error_buf, error_buf_size)) { - return false; - } - - if (target_info.e_version != E_VERSION_CURRENT) { - set_error_buf(error_buf, error_buf_size, "invalid elf file version"); - return false; - } - - return true; -fail: - return false; -} - -static void * -get_native_symbol_by_name(const char *name) -{ - void *func = NULL; - uint32 symnum = 0; - SymbolMap *sym = NULL; - - sym = get_target_symbol_map(&symnum); - - while (symnum--) { - if (strcmp(sym->symbol_name, name) == 0) { - func = sym->symbol_addr; - break; - } - sym++; - } - - return func; -} - -static bool -str2uint32(const char *buf, uint32 *p_res); - -static bool -str2uint64(const char *buf, uint64 *p_res); - -static bool -load_native_symbol_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf_end; - uint32 cnt; - int32 i; - const char *symbol; - - read_uint32(p, p_end, cnt); - - if (cnt > 0) { - module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *)); - if (module->native_symbol_list == NULL) { - set_error_buf(error_buf, error_buf_size, - "malloc native symbol list failed"); - goto fail; - } - - for (i = cnt - 1; i >= 0; i--) { - read_string(p, p_end, symbol); - if (!strncmp(symbol, "f32#", 4) || !strncmp(symbol, "i32#", 4)) { - uint32 u32; - /* Resolve the raw int bits of f32 const */ - if (!str2uint32(symbol + 4, &u32)) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto fail; - } - *(uint32 *)(&module->native_symbol_list[i]) = u32; - } - else if (!strncmp(symbol, "f64#", 4) - || !strncmp(symbol, "i64#", 4)) { - uint64 u64; - /* Resolve the raw int bits of f64 const */ - if (!str2uint64(symbol + 4, &u64)) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto fail; - } - *(uint64 *)(&module->native_symbol_list[i]) = u64; - } - else if (!strncmp(symbol, "__ignore", 8)) { - /* Padding bytes to make f64 on 8-byte aligned address, - or it is the second 32-bit slot in 32-bit system */ - continue; - } - else { - module->native_symbol_list[i] = - get_native_symbol_by_name(symbol); - if (module->native_symbol_list[i] == NULL) { - set_error_buf_v(error_buf, error_buf_size, - "missing native symbol: %s", symbol); - goto fail; - } - } - } - } - - return true; -fail: - return false; -} - -static bool -load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, char *error_buf, - uint32 error_buf_size) -{ -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - const uint8 *p = buf, *p_end = buf_end; - uint32 *aux_func_indexes; - const char **aux_func_names; - uint32 name_type, subsection_size; - uint32 previous_name_type = 0; - uint32 num_func_name; - uint32 func_index; - uint32 previous_func_index = ~0U; - uint32 name_index; - int i = 0; - uint32 name_len; - uint64 size; - - if (p >= p_end) { - set_error_buf(error_buf, error_buf_size, "unexpected end"); - return false; - } - - read_uint32(p, p_end, name_len); - - if (name_len != 4 || p + name_len > p_end) { - set_error_buf(error_buf, error_buf_size, "unexpected end"); - return false; - } - - if (memcmp(p, "name", 4) != 0) { - set_error_buf(error_buf, error_buf_size, "invalid custom name section"); - return false; - } - p += name_len; - - while (p < p_end) { - read_uint32(p, p_end, name_type); - if (i != 0) { - if (name_type == previous_name_type) { - set_error_buf(error_buf, error_buf_size, - "duplicate sub-section"); - return false; - } - if (name_type < previous_name_type) { - set_error_buf(error_buf, error_buf_size, - "out-of-order sub-section"); - return false; - } - } - previous_name_type = name_type; - read_uint32(p, p_end, subsection_size); - CHECK_BUF(p, p_end, subsection_size); - switch (name_type) { - case SUB_SECTION_TYPE_FUNC: - if (subsection_size) { - read_uint32(p, p_end, num_func_name); - if (num_func_name - > module->import_func_count + module->func_count) { - set_error_buf(error_buf, error_buf_size, - "function name count out of bounds"); - return false; - } - module->aux_func_name_count = num_func_name; - - /* Allocate memory */ - size = sizeof(uint32) * (uint64)module->aux_func_name_count; - if (!(aux_func_indexes = module->aux_func_indexes = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - size = - sizeof(char **) * (uint64)module->aux_func_name_count; - if (!(aux_func_names = module->aux_func_names = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - for (name_index = 0; name_index < num_func_name; - name_index++) { - read_uint32(p, p_end, func_index); - if (name_index != 0 - && func_index == previous_func_index) { - set_error_buf(error_buf, error_buf_size, - "duplicate function name"); - return false; - } - if (name_index != 0 - && func_index < previous_func_index) { - set_error_buf(error_buf, error_buf_size, - "out-of-order function index "); - return false; - } - if (func_index - >= module->import_func_count + module->func_count) { - set_error_buf(error_buf, error_buf_size, - "function index out of bounds"); - return false; - } - previous_func_index = func_index; - *(aux_func_indexes + name_index) = func_index; - read_string(p, p_end, *(aux_func_names + name_index)); -#if 0 - LOG_DEBUG("func_index %u -> aux_func_name = %s\n", - func_index, *(aux_func_names + name_index)); -#endif - } - } - break; - case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection - */ - case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */ - default: - p = p + subsection_size; - break; - } - i++; - } - - return true; -fail: - return false; -#else - return true; -#endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */ -} - -static bool -load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf_end; - uint32 sub_section_type; - - read_uint32(p, p_end, sub_section_type); - buf = p; - - switch (sub_section_type) { - case AOT_CUSTOM_SECTION_NATIVE_SYMBOL: - if (!load_native_symbol_section(buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - goto fail; - break; - case AOT_CUSTOM_SECTION_NAME: - if (!load_name_section(buf, buf_end, module, is_load_from_file_buf, - error_buf, error_buf_size)) - goto fail; - break; -#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 - case AOT_CUSTOM_SECTION_RAW: - { - const char *section_name; - WASMCustomSection *section; - - if (p >= p_end) { - set_error_buf(error_buf, error_buf_size, "unexpected end"); - goto fail; - } - - read_string(p, p_end, section_name); - - section = loader_malloc(sizeof(WASMCustomSection), error_buf, - error_buf_size); - if (!section) { - goto fail; - } - - section->name_addr = (char *)section_name; - section->name_len = (uint32)strlen(section_name); - section->content_addr = (uint8 *)p; - section->content_len = (uint32)(p_end - p); - - section->next = module->custom_section_list; - module->custom_section_list = section; - LOG_VERBOSE("Load custom section [%s] success.", section_name); - break; - } -#endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */ - default: - break; - } - - return true; -fail: - return false; -} - -static void -destroy_import_memories(AOTImportMemory *import_memories) -{ - wasm_runtime_free(import_memories); -} - -static void -destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count) -{ - uint32 i; - for (i = 0; i < count; i++) - if (data_list[i]) - wasm_runtime_free(data_list[i]); - wasm_runtime_free(data_list); -} - -static bool -load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTMemInitData **data_list; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count; - if (!(module->mem_init_data_list = data_list = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each memory data segment */ - for (i = 0; i < module->mem_init_data_count; i++) { - uint32 init_expr_type, byte_count; - uint64 init_expr_value; - uint32 is_passive; - uint32 memory_index; - - read_uint32(buf, buf_end, is_passive); - read_uint32(buf, buf_end, memory_index); - read_uint32(buf, buf_end, init_expr_type); - read_uint64(buf, buf_end, init_expr_value); - read_uint32(buf, buf_end, byte_count); - size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count; - if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - -#if WASM_ENABLE_BULK_MEMORY != 0 - /* is_passive and memory_index is only used in bulk memory mode */ - data_list[i]->is_passive = (bool)is_passive; - data_list[i]->memory_index = memory_index; -#endif - data_list[i]->offset.init_expr_type = (uint8)init_expr_type; - data_list[i]->offset.u.i64 = (int64)init_expr_value; - data_list[i]->byte_count = byte_count; - read_byte_array(buf, buf_end, data_list[i]->bytes, - data_list[i]->byte_count); - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - uint32 i; - uint64 total_size; - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->import_memory_count); - /* We don't support import_memory_count > 0 currently */ - bh_assert(module->import_memory_count == 0); - - read_uint32(buf, buf_end, module->memory_count); - total_size = sizeof(AOTMemory) * (uint64)module->memory_count; - if (!(module->memories = - loader_malloc(total_size, error_buf, error_buf_size))) { - return false; - } - - for (i = 0; i < module->memory_count; i++) { - read_uint32(buf, buf_end, module->memories[i].memory_flags); - read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page); - read_uint32(buf, buf_end, module->memories[i].mem_init_page_count); - read_uint32(buf, buf_end, module->memories[i].mem_max_page_count); - } - - read_uint32(buf, buf_end, module->mem_init_data_count); - - /* load memory init data list */ - if (module->mem_init_data_count > 0 - && !load_mem_init_data_list(&buf, buf_end, module, error_buf, - error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_import_tables(AOTImportTable *import_tables) -{ - wasm_runtime_free(import_tables); -} - -static void -destroy_tables(AOTTable *tables) -{ - wasm_runtime_free(tables); -} - -static void -destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count) -{ - uint32 i; - for (i = 0; i < count; i++) - if (data_list[i]) - wasm_runtime_free(data_list[i]); - wasm_runtime_free(data_list); -} - -static bool -load_import_table_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTImportTable *import_table; - uint64 size; - uint32 i, possible_grow; - - /* Allocate memory */ - size = sizeof(AOTImportTable) * (uint64)module->import_table_count; - if (!(module->import_tables = import_table = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* keep sync with aot_emit_table_info() aot_emit_aot_file */ - for (i = 0; i < module->import_table_count; i++, import_table++) { - read_uint32(buf, buf_end, import_table->elem_type); - read_uint32(buf, buf_end, import_table->table_init_size); - read_uint32(buf, buf_end, import_table->table_max_size); - read_uint32(buf, buf_end, possible_grow); - import_table->possible_grow = (possible_grow & 0x1); - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTTable *table; - uint64 size; - uint32 i, possible_grow; - - /* Allocate memory */ - size = sizeof(AOTTable) * (uint64)module->table_count; - if (!(module->tables = table = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each table data segment */ - for (i = 0; i < module->table_count; i++, table++) { - read_uint32(buf, buf_end, table->elem_type); - read_uint32(buf, buf_end, table->table_flags); - read_uint32(buf, buf_end, table->table_init_size); - read_uint32(buf, buf_end, table->table_max_size); - read_uint32(buf, buf_end, possible_grow); - table->possible_grow = (possible_grow & 0x1); - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTTableInitData **data_list; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count; - if (!(module->table_init_data_list = data_list = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each table data segment */ - for (i = 0; i < module->table_init_data_count; i++) { - uint32 mode, elem_type; - uint32 table_index, init_expr_type, func_index_count; - uint64 init_expr_value, size1; - - read_uint32(buf, buf_end, mode); - read_uint32(buf, buf_end, elem_type); - read_uint32(buf, buf_end, table_index); - read_uint32(buf, buf_end, init_expr_type); - read_uint64(buf, buf_end, init_expr_value); - read_uint32(buf, buf_end, func_index_count); - - size1 = sizeof(uint32) * (uint64)func_index_count; - size = offsetof(AOTTableInitData, func_indexes) + size1; - if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - data_list[i]->mode = mode; - data_list[i]->elem_type = elem_type; - data_list[i]->is_dropped = false; - data_list[i]->table_index = table_index; - data_list[i]->offset.init_expr_type = (uint8)init_expr_type; - data_list[i]->offset.u.i64 = (int64)init_expr_value; - data_list[i]->func_index_count = func_index_count; - read_byte_array(buf, buf_end, data_list[i]->func_indexes, - (uint32)size1); - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->import_table_count); - if (module->import_table_count > 0 - && !load_import_table_list(&buf, buf_end, module, error_buf, - error_buf_size)) - return false; - - read_uint32(buf, buf_end, module->table_count); - if (module->table_count > 0 - && !load_table_list(&buf, buf_end, module, error_buf, error_buf_size)) - return false; - - read_uint32(buf, buf_end, module->table_init_data_count); - - /* load table init data list */ - if (module->table_init_data_count > 0 - && !load_table_init_data_list(&buf, buf_end, module, error_buf, - error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_func_types(AOTFuncType **func_types, uint32 count) -{ - uint32 i; - for (i = 0; i < count; i++) - if (func_types[i]) - wasm_runtime_free(func_types[i]); - wasm_runtime_free(func_types); -} - -static bool -load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTFuncType **func_types; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTFuncType *) * (uint64)module->func_type_count; - if (!(module->func_types = func_types = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each function type */ - for (i = 0; i < module->func_type_count; i++) { - uint32 param_count, result_count; - uint32 param_cell_num, ret_cell_num; - uint64 size1; - - read_uint32(buf, buf_end, param_count); - read_uint32(buf, buf_end, result_count); - - if (param_count > UINT16_MAX || result_count > UINT16_MAX) { - set_error_buf(error_buf, error_buf_size, - "param count or result count too large"); - return false; - } - - size1 = (uint64)param_count + (uint64)result_count; - size = offsetof(AOTFuncType, types) + size1; - if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - func_types[i]->param_count = (uint16)param_count; - func_types[i]->result_count = (uint16)result_count; - read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1); - - param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count); - ret_cell_num = - wasm_get_cell_num(func_types[i]->types + param_count, result_count); - if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) { - set_error_buf(error_buf, error_buf_size, - "param count or result count too large"); - return false; - } - - func_types[i]->param_cell_num = (uint16)param_cell_num; - func_types[i]->ret_cell_num = (uint16)ret_cell_num; - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_func_type_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->func_type_count); - - /* load function type */ - if (module->func_type_count > 0 - && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_import_globals(AOTImportGlobal *import_globals) -{ - wasm_runtime_free(import_globals); -} - -static bool -load_import_globals(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTImportGlobal *import_globals; - uint64 size; - uint32 i, data_offset = 0; -#if WASM_ENABLE_LIBC_BUILTIN != 0 - WASMGlobalImport tmp_global; -#endif - - /* Allocate memory */ - size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count; - if (!(module->import_globals = import_globals = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each import global */ - for (i = 0; i < module->import_global_count; i++) { - buf = (uint8 *)align_ptr(buf, 2); - read_uint8(buf, buf_end, import_globals[i].type); - read_uint8(buf, buf_end, import_globals[i].is_mutable); - read_string(buf, buf_end, import_globals[i].module_name); - read_string(buf, buf_end, import_globals[i].global_name); - -#if WASM_ENABLE_LIBC_BUILTIN != 0 - if (wasm_native_lookup_libc_builtin_global( - import_globals[i].module_name, import_globals[i].global_name, - &tmp_global)) { - if (tmp_global.type != import_globals[i].type - || tmp_global.is_mutable != import_globals[i].is_mutable) { - set_error_buf(error_buf, error_buf_size, - "incompatible import type"); - return false; - } - import_globals[i].global_data_linked = - tmp_global.global_data_linked; - import_globals[i].is_linked = true; - } -#else - import_globals[i].is_linked = false; -#endif - - import_globals[i].size = wasm_value_type_size(import_globals[i].type); - import_globals[i].data_offset = data_offset; - data_offset += import_globals[i].size; - module->global_data_size += import_globals[i].size; - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_import_global_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->import_global_count); - - /* load import globals */ - if (module->import_global_count > 0 - && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf, - error_buf, error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_globals(AOTGlobal *globals) -{ - wasm_runtime_free(globals); -} - -static bool -load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTGlobal *globals; - uint64 size; - uint32 i, data_offset = 0; - AOTImportGlobal *last_import_global; - - /* Allocate memory */ - size = sizeof(AOTGlobal) * (uint64)module->global_count; - if (!(module->globals = globals = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - if (module->import_global_count > 0) { - last_import_global = - &module->import_globals[module->import_global_count - 1]; - data_offset = - last_import_global->data_offset + last_import_global->size; - } - - /* Create each global */ - for (i = 0; i < module->global_count; i++) { - uint16 init_expr_type; - - read_uint8(buf, buf_end, globals[i].type); - read_uint8(buf, buf_end, globals[i].is_mutable); - read_uint16(buf, buf_end, init_expr_type); - - if (init_expr_type != INIT_EXPR_TYPE_V128_CONST) { - read_uint64(buf, buf_end, globals[i].init_expr.u.i64); - } - else { - uint64 *i64x2 = (uint64 *)globals[i].init_expr.u.v128.i64x2; - CHECK_BUF(buf, buf_end, sizeof(uint64) * 2); - wasm_runtime_read_v128(buf, &i64x2[0], &i64x2[1]); - buf += sizeof(uint64) * 2; - } - - globals[i].init_expr.init_expr_type = (uint8)init_expr_type; - - globals[i].size = wasm_value_type_size(globals[i].type); - globals[i].data_offset = data_offset; - data_offset += globals[i].size; - module->global_data_size += globals[i].size; - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->global_count); - - /* load globals */ - if (module->global_count > 0 - && !load_globals(&buf, buf_end, module, error_buf, error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_import_funcs(AOTImportFunc *import_funcs) -{ - wasm_runtime_free(import_funcs); -} - -static bool -load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, char *error_buf, - uint32 error_buf_size) -{ - const char *module_name, *field_name; - const uint8 *buf = *p_buf; - AOTImportFunc *import_funcs; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTImportFunc) * (uint64)module->import_func_count; - if (!(module->import_funcs = import_funcs = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each import func */ - for (i = 0; i < module->import_func_count; i++) { - read_uint16(buf, buf_end, import_funcs[i].func_type_index); - if (import_funcs[i].func_type_index >= module->func_type_count) { - set_error_buf(error_buf, error_buf_size, "unknown type"); - return false; - } - import_funcs[i].func_type = - module->func_types[import_funcs[i].func_type_index]; - read_string(buf, buf_end, import_funcs[i].module_name); - read_string(buf, buf_end, import_funcs[i].func_name); - - module_name = import_funcs[i].module_name; - field_name = import_funcs[i].func_name; - import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol( - module_name, field_name, import_funcs[i].func_type, - &import_funcs[i].signature, &import_funcs[i].attachment, - &import_funcs[i].call_conv_raw); - -#if WASM_ENABLE_LIBC_WASI != 0 - if (!strcmp(import_funcs[i].module_name, "wasi_unstable") - || !strcmp(import_funcs[i].module_name, "wasi_snapshot_preview1")) - module->import_wasi_api = true; -#endif - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_import_func_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->import_func_count); - - /* load import funcs */ - if (module->import_func_count > 0 - && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf, - error_buf, error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static void -destroy_object_data_sections(AOTObjectDataSection *data_sections, - uint32 data_section_count) -{ - uint32 i; - AOTObjectDataSection *data_section = data_sections; - for (i = 0; i < data_section_count; i++, data_section++) - if (data_section->data) - os_munmap(data_section->data, data_section->size); - wasm_runtime_free(data_sections); -} - -static bool -load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTObjectDataSection *data_sections; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count; - if (!(module->data_sections = data_sections = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each data section */ - for (i = 0; i < module->data_section_count; i++) { - int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE; -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ - || defined(BUILD_TARGET_RISCV64_LP64) - /* aot code and data in x86_64 must be in range 0 to 2G due to - relocation for R_X86_64_32/32S/PC32 */ - int map_flags = MMAP_MAP_32BIT; -#else - int map_flags = MMAP_MAP_NONE; -#endif - - read_string(buf, buf_end, data_sections[i].name); - read_uint32(buf, buf_end, data_sections[i].size); - - /* Allocate memory for data */ - if (data_sections[i].size > 0 - && !(data_sections[i].data = os_mmap(NULL, data_sections[i].size, - map_prot, map_flags))) { - set_error_buf(error_buf, error_buf_size, "allocate memory failed"); - return false; - } -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) -#if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \ - && !defined(BH_PLATFORM_DARWIN) - /* address must be in the first 2 Gigabytes of - the process address space */ - bh_assert((uintptr_t)data_sections[i].data < INT32_MAX); -#endif -#endif - - read_byte_array(buf, buf_end, data_sections[i].data, - data_sections[i].size); - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->data_section_count); - - /* load object data sections */ - if (module->data_section_count > 0 - && !load_object_data_sections(&buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_init_data_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf_end; - - if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size) - || !load_table_info(&p, p_end, module, error_buf, error_buf_size) - || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size) - || !load_import_global_info(&p, p_end, module, is_load_from_file_buf, - error_buf, error_buf_size) - || !load_global_info(&p, p_end, module, error_buf, error_buf_size) - || !load_import_func_info(&p, p_end, module, is_load_from_file_buf, - error_buf, error_buf_size)) - return false; - - /* load function count and start function index */ - read_uint32(p, p_end, module->func_count); - read_uint32(p, p_end, module->start_func_index); - - /* check start function index */ - if (module->start_func_index != (uint32)-1 - && (module->start_func_index - >= module->import_func_count + module->func_count)) { - set_error_buf(error_buf, error_buf_size, - "invalid start function index"); - return false; - } - - read_uint32(p, p_end, module->aux_data_end_global_index); - read_uint32(p, p_end, module->aux_data_end); - read_uint32(p, p_end, module->aux_heap_base_global_index); - read_uint32(p, p_end, module->aux_heap_base); - read_uint32(p, p_end, module->aux_stack_top_global_index); - read_uint32(p, p_end, module->aux_stack_bottom); - read_uint32(p, p_end, module->aux_stack_size); - - if (!load_object_data_sections_info(&p, p_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - - if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "invalid init data section size"); - return false; - } - - return true; -fail: - return false; -} - -static bool -load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - uint8 *plt_base; - - if (module->func_count > 0 && buf_end == buf) { - set_error_buf(error_buf, error_buf_size, "invalid code size"); - return false; - } - - /* The layout is: literal size + literal + code (with plt table) */ - read_uint32(buf, buf_end, module->literal_size); - - /* literal data is at beginning of the text section */ - module->literal = (uint8 *)buf; - module->code = (void *)(buf + module->literal_size); - module->code_size = (uint32)(buf_end - (uint8 *)module->code); - -#if WASM_ENABLE_DEBUG_AOT != 0 - module->elf_size = module->code_size; - - if (is_ELF(module->code)) { - /* Now code points to an ELF object, we pull it down to .text section */ - uint64 offset; - uint64 size; - char *code_buf = module->code; - module->elf_hdr = code_buf; - if (!get_text_section(code_buf, &offset, &size)) { - set_error_buf(error_buf, error_buf_size, - "get text section of ELF failed"); - return false; - } - module->code = code_buf + offset; - module->code_size -= (uint32)offset; - } -#endif - - if ((module->code_size > 0) && !module->is_indirect_mode) { - plt_base = (uint8 *)buf_end - get_plt_table_size(); - init_plt_table(plt_base); - } - return true; -fail: - return false; -} - -static bool -load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf_end; - uint32 i; - uint64 size, text_offset; -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - RUNTIME_FUNCTION *rtl_func_table; - AOTUnwindInfo *unwind_info; - uint32 unwind_info_offset = module->code_size - sizeof(AOTUnwindInfo); - uint32 unwind_code_offset = unwind_info_offset - PLT_ITEM_SIZE; -#endif - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - unwind_info = (AOTUnwindInfo *)((uint8 *)module->code + module->code_size - - sizeof(AOTUnwindInfo)); - unwind_info->Version = 1; - unwind_info->Flags = UNW_FLAG_NHANDLER; - *(uint32 *)&unwind_info->UnwindCode[0] = unwind_code_offset; - - size = sizeof(RUNTIME_FUNCTION) * (uint64)module->func_count; - if (size > 0 - && !(rtl_func_table = module->rtl_func_table = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } -#endif - - size = sizeof(void *) * (uint64)module->func_count; - if (size > 0 - && !(module->func_ptrs = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - for (i = 0; i < module->func_count; i++) { - if (sizeof(void *) == 8) { - read_uint64(p, p_end, text_offset); - } - else { - uint32 text_offset32; - read_uint32(p, p_end, text_offset32); - text_offset = text_offset32; - } - if (text_offset >= module->code_size) { - set_error_buf(error_buf, error_buf_size, - "invalid function code offset"); - return false; - } - module->func_ptrs[i] = (uint8 *)module->code + text_offset; -#if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP) - /* bits[0] of thumb function address must be 1 */ - module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1); -#endif -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - rtl_func_table[i].BeginAddress = (DWORD)text_offset; - if (i > 0) { - rtl_func_table[i - 1].EndAddress = rtl_func_table[i].BeginAddress; - } - rtl_func_table[i].UnwindInfoAddress = (DWORD)unwind_info_offset; -#endif - } - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - if (module->func_count > 0) { - uint32 plt_table_size = - module->is_indirect_mode ? 0 : get_plt_table_size(); - rtl_func_table[module->func_count - 1].EndAddress = - (DWORD)(module->code_size - plt_table_size); - - if (!RtlAddFunctionTable(rtl_func_table, module->func_count, - (DWORD64)(uintptr_t)module->code)) { - set_error_buf(error_buf, error_buf_size, - "add dynamic function table failed"); - return false; - } - module->rtl_func_table_registered = true; - } -#endif - - /* Set start function when function pointers are resolved */ - if (module->start_func_index != (uint32)-1) { - if (module->start_func_index >= module->import_func_count) - module->start_function = - module->func_ptrs[module->start_func_index - - module->import_func_count]; - else - /* TODO: fix start function can be import function issue */ - module->start_function = NULL; - } - else { - module->start_function = NULL; - } - - size = sizeof(uint32) * (uint64)module->func_count; - if (size > 0 - && !(module->func_type_indexes = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - for (i = 0; i < module->func_count; i++) { - read_uint32(p, p_end, module->func_type_indexes[i]); - if (module->func_type_indexes[i] >= module->func_type_count) { - set_error_buf(error_buf, error_buf_size, "unknown type"); - return false; - } - } - - if (p != buf_end) { - set_error_buf(error_buf, error_buf_size, - "invalid function section size"); - return false; - } - - return true; -fail: - return false; -} - -static void -destroy_exports(AOTExport *exports) -{ - wasm_runtime_free(exports); -} - -static bool -load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) -{ - const uint8 *buf = *p_buf; - AOTExport *exports; - uint64 size; - uint32 i; - - /* Allocate memory */ - size = sizeof(AOTExport) * (uint64)module->export_count; - if (!(module->exports = exports = - loader_malloc(size, error_buf, error_buf_size))) { - return false; - } - - /* Create each export */ - for (i = 0; i < module->export_count; i++) { - read_uint32(buf, buf_end, exports[i].index); - read_uint8(buf, buf_end, exports[i].kind); - read_string(buf, buf_end, exports[i].name); -#if 0 /* TODO: check kind and index */ - if (export_funcs[i].index >= - module->func_count + module->import_func_count) { - set_error_buf(error_buf, error_buf_size, - "function index is out of range"); - return false; - } -#endif - } - - *p_buf = buf; - return true; -fail: - return false; -} - -static bool -load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - bool is_load_from_file_buf, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf_end; - - /* load export functions */ - read_uint32(p, p_end, module->export_count); - if (module->export_count > 0 - && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - - if (p != p_end) { - set_error_buf(error_buf, error_buf_size, "invalid export section size"); - return false; - } - - return true; -fail: - return false; -} - -static void * -get_data_section_addr(AOTModule *module, const char *section_name, - uint32 *p_data_size) -{ - uint32 i; - AOTObjectDataSection *data_section = module->data_sections; - - for (i = 0; i < module->data_section_count; i++, data_section++) { - if (!strcmp(data_section->name, section_name)) { - if (p_data_size) - *p_data_size = data_section->size; - return data_section->data; - } - } - - return NULL; -} - -static void * -resolve_target_sym(const char *symbol, int32 *p_index) -{ - uint32 i, num = 0; - SymbolMap *target_sym_map; - - if (!(target_sym_map = get_target_symbol_map(&num))) - return NULL; - - for (i = 0; i < num; i++) { - if (!strcmp(target_sym_map[i].symbol_name, symbol) -#if defined(_WIN32) || defined(_WIN32_) - /* In Win32, the symbol name of function added by - LLVMAddFunction() is prefixed by '_', ignore it */ - || (strlen(symbol) > 1 && symbol[0] == '_' - && !strcmp(target_sym_map[i].symbol_name, symbol + 1)) -#endif - ) { - *p_index = (int32)i; - return target_sym_map[i].symbol_addr; - } - } - return NULL; -} - -static bool -is_literal_relocation(const char *reloc_sec_name) -{ - return !strcmp(reloc_sec_name, ".rela.literal"); -} - -static bool -str2uint32(const char *buf, uint32 *p_res) -{ - uint32 res = 0, val; - const char *buf_end = buf + 8; - char ch; - - while (buf < buf_end) { - ch = *buf++; - if (ch >= '0' && ch <= '9') - val = ch - '0'; - else if (ch >= 'a' && ch <= 'f') - val = ch - 'a' + 0xA; - else if (ch >= 'A' && ch <= 'F') - val = ch - 'A' + 0xA; - else - return false; - res = (res << 4) | val; - } - *p_res = res; - return true; -} - -static bool -str2uint64(const char *buf, uint64 *p_res) -{ - uint64 res = 0, val; - const char *buf_end = buf + 16; - char ch; - - while (buf < buf_end) { - ch = *buf++; - if (ch >= '0' && ch <= '9') - val = ch - '0'; - else if (ch >= 'a' && ch <= 'f') - val = ch - 'a' + 0xA; - else if (ch >= 'A' && ch <= 'F') - val = ch - 'A' + 0xA; - else - return false; - res = (res << 4) | val; - } - *p_res = res; - return true; -} - -static bool -do_text_relocation(AOTModule *module, AOTRelocationGroup *group, - char *error_buf, uint32 error_buf_size) -{ - bool is_literal = is_literal_relocation(group->section_name); - uint8 *aot_text = is_literal ? module->literal : module->code; - uint32 aot_text_size = - is_literal ? module->literal_size : module->code_size; - uint32 i, func_index, symbol_len; -#if defined(BH_PLATFORM_WINDOWS) - uint32 ymm_plt_index = 0, xmm_plt_index = 0; - uint32 real_plt_index = 0, float_plt_index = 0, j; -#endif - char symbol_buf[128] = { 0 }, *symbol, *p; - void *symbol_addr; - AOTRelocation *relocation = group->relocations; - - if (group->relocation_count > 0 && !aot_text) { - set_error_buf(error_buf, error_buf_size, - "invalid text relocation count"); - return false; - } - - for (i = 0; i < group->relocation_count; i++, relocation++) { - int32 symbol_index = -1; - symbol_len = (uint32)strlen(relocation->symbol_name); - if (symbol_len + 1 <= sizeof(symbol_buf)) - symbol = symbol_buf; - else { - if (!(symbol = loader_malloc(symbol_len + 1, error_buf, - error_buf_size))) { - return false; - } - } - bh_memcpy_s(symbol, symbol_len, relocation->symbol_name, symbol_len); - symbol[symbol_len] = '\0'; - - if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) { - p = symbol + strlen(AOT_FUNC_PREFIX); - if (*p == '\0' - || (func_index = (uint32)atoi(p)) > module->func_count) { - set_error_buf_v(error_buf, error_buf_size, - "invalid import symbol %s", symbol); - goto check_symbol_fail; - } - symbol_addr = module->func_ptrs[func_index]; - } - else if (!strcmp(symbol, ".text")) { - symbol_addr = module->code; - } - else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata") - || !strcmp(symbol, ".rdata") - || !strcmp(symbol, ".rodata") - /* ".rodata.cst4/8/16/.." */ - || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst")) - /* ".rodata.strn.m" */ - || !strncmp(symbol, ".rodata.str", strlen(".rodata.str"))) { - symbol_addr = get_data_section_addr(module, symbol, NULL); - if (!symbol_addr) { - set_error_buf_v(error_buf, error_buf_size, - "invalid data section (%s)", symbol); - goto check_symbol_fail; - } - } - else if (!strcmp(symbol, ".literal")) { - symbol_addr = module->literal; - } -#if defined(BH_PLATFORM_WINDOWS) - /* Relocation for symbols which start with "__ymm@", "__xmm@" or - "__real@" and end with the ymm value, xmm value or real value. - In Windows PE file, the data is stored in some individual ".rdata" - sections. We simply create extra plt data, parse the values from - the symbols and stored them into the extra plt data. */ - else if (!strcmp(group->section_name, ".text") - && !strncmp(symbol, YMM_PLT_PREFIX, strlen(YMM_PLT_PREFIX)) - && strlen(symbol) == strlen(YMM_PLT_PREFIX) + 64) { - char ymm_buf[17] = { 0 }; - - symbol_addr = module->extra_plt_data + ymm_plt_index * 32; - for (j = 0; j < 4; j++) { - bh_memcpy_s(ymm_buf, sizeof(ymm_buf), - symbol + strlen(YMM_PLT_PREFIX) + 48 - 16 * j, 16); - if (!str2uint64(ymm_buf, - (uint64 *)((uint8 *)symbol_addr + 8 * j))) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto check_symbol_fail; - } - } - ymm_plt_index++; - } - else if (!strcmp(group->section_name, ".text") - && !strncmp(symbol, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX)) - && strlen(symbol) == strlen(XMM_PLT_PREFIX) + 32) { - char xmm_buf[17] = { 0 }; - - symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32 - + xmm_plt_index * 16; - for (j = 0; j < 2; j++) { - bh_memcpy_s(xmm_buf, sizeof(xmm_buf), - symbol + strlen(XMM_PLT_PREFIX) + 16 - 16 * j, 16); - if (!str2uint64(xmm_buf, - (uint64 *)((uint8 *)symbol_addr + 8 * j))) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto check_symbol_fail; - } - } - xmm_plt_index++; - } - else if (!strcmp(group->section_name, ".text") - && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX)) - && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 16) { - char real_buf[17] = { 0 }; - - symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32 - + module->xmm_plt_count * 16 + real_plt_index * 8; - bh_memcpy_s(real_buf, sizeof(real_buf), - symbol + strlen(REAL_PLT_PREFIX), 16); - if (!str2uint64(real_buf, (uint64 *)symbol_addr)) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto check_symbol_fail; - } - real_plt_index++; - } - else if (!strcmp(group->section_name, ".text") - && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX)) - && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 8) { - char float_buf[9] = { 0 }; - - symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32 - + module->xmm_plt_count * 16 - + module->real_plt_count * 8 + float_plt_index * 4; - bh_memcpy_s(float_buf, sizeof(float_buf), - symbol + strlen(REAL_PLT_PREFIX), 8); - if (!str2uint32(float_buf, (uint32 *)symbol_addr)) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto check_symbol_fail; - } - float_plt_index++; - } -#endif /* end of defined(BH_PLATFORM_WINDOWS) */ - else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) { - set_error_buf_v(error_buf, error_buf_size, - "resolve symbol %s failed", symbol); - goto check_symbol_fail; - } - - if (symbol != symbol_buf) - wasm_runtime_free(symbol); - - if (!apply_relocation( - module, aot_text, aot_text_size, relocation->relocation_offset, - relocation->relocation_addend, relocation->relocation_type, - symbol_addr, symbol_index, error_buf, error_buf_size)) - return false; - } - - return true; - -check_symbol_fail: - if (symbol != symbol_buf) - wasm_runtime_free(symbol); - return false; -} - -static bool -do_data_relocation(AOTModule *module, AOTRelocationGroup *group, - char *error_buf, uint32 error_buf_size) - -{ - uint8 *data_addr; - uint32 data_size = 0, i; - AOTRelocation *relocation = group->relocations; - void *symbol_addr; - char *symbol, *data_section_name; - - if (!strncmp(group->section_name, ".rela.", 6)) { - data_section_name = group->section_name + strlen(".rela"); - } - else if (!strncmp(group->section_name, ".rel.", 5)) { - data_section_name = group->section_name + strlen(".rel"); - } - else if (!strcmp(group->section_name, ".rdata")) { - data_section_name = group->section_name; - } - else { - set_error_buf(error_buf, error_buf_size, - "invalid data relocation section name"); - return false; - } - - data_addr = get_data_section_addr(module, data_section_name, &data_size); - - if (group->relocation_count > 0 && !data_addr) { - set_error_buf(error_buf, error_buf_size, - "invalid data relocation count"); - return false; - } - - for (i = 0; i < group->relocation_count; i++, relocation++) { - symbol = relocation->symbol_name; - if (!strcmp(symbol, ".text")) { - symbol_addr = module->code; - } - else { - set_error_buf_v(error_buf, error_buf_size, - "invalid relocation symbol %s", symbol); - return false; - } - - if (!apply_relocation( - module, data_addr, data_size, relocation->relocation_offset, - relocation->relocation_addend, relocation->relocation_type, - symbol_addr, -1, error_buf, error_buf_size)) - return false; - } - - return true; -} - -static bool -validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count, - char *error_buf, uint32 error_buf_size) -{ - uint32 i, str_len_addr = 0; - uint16 str_len; - - for (i = 0; i < count; i++) { - if (offsets[i] != str_len_addr) - return false; - - read_uint16(buf, buf_end, str_len); - str_len_addr += (uint32)sizeof(uint16) + str_len; - str_len_addr = align_uint(str_len_addr, 2); - buf += str_len; - buf = (uint8 *)align_ptr(buf, 2); - } - - if (buf == buf_end) - return true; -fail: - return false; -} - -static bool -load_relocation_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, bool is_load_from_file_buf, - char *error_buf, uint32 error_buf_size) -{ - AOTRelocationGroup *groups = NULL, *group; - uint32 symbol_count = 0; - uint32 group_count = 0, i, j; - uint64 size; - uint32 *symbol_offsets, total_string_len; - uint8 *symbol_buf, *symbol_buf_end; - int map_prot, map_flags; - bool ret = false; - char **symbols = NULL; - - read_uint32(buf, buf_end, symbol_count); - - symbol_offsets = (uint32 *)buf; - for (i = 0; i < symbol_count; i++) { - CHECK_BUF(buf, buf_end, sizeof(uint32)); - buf += sizeof(uint32); - } - - read_uint32(buf, buf_end, total_string_len); - symbol_buf = (uint8 *)buf; - symbol_buf_end = symbol_buf + total_string_len; - - if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets, - symbol_count, error_buf, error_buf_size)) { - set_error_buf(error_buf, error_buf_size, - "validate symbol table failed"); - goto fail; - } - - if (symbol_count > 0) { - symbols = loader_malloc((uint64)sizeof(*symbols) * symbol_count, - error_buf, error_buf_size); - if (symbols == NULL) { - goto fail; - } - } - -#if defined(BH_PLATFORM_WINDOWS) - buf = symbol_buf_end; - read_uint32(buf, buf_end, group_count); - - for (i = 0; i < group_count; i++) { - uint32 name_index, relocation_count; - uint16 group_name_len; - uint8 *group_name; - - /* section name address is 4 bytes aligned. */ - buf = (uint8 *)align_ptr(buf, sizeof(uint32)); - read_uint32(buf, buf_end, name_index); - - if (name_index >= symbol_count) { - set_error_buf(error_buf, error_buf_size, - "symbol index out of range"); - goto fail; - } - - group_name = symbol_buf + symbol_offsets[name_index]; - group_name_len = *(uint16 *)group_name; - group_name += sizeof(uint16); - - read_uint32(buf, buf_end, relocation_count); - - for (j = 0; j < relocation_count; j++) { - AOTRelocation relocation = { 0 }; - uint32 symbol_index, offset32; - int32 addend32; - uint16 symbol_name_len; - uint8 *symbol_name; - - if (sizeof(void *) == 8) { - read_uint64(buf, buf_end, relocation.relocation_offset); - read_uint64(buf, buf_end, relocation.relocation_addend); - } - else { - read_uint32(buf, buf_end, offset32); - relocation.relocation_offset = (uint64)offset32; - read_uint32(buf, buf_end, addend32); - relocation.relocation_addend = (int64)addend32; - } - read_uint32(buf, buf_end, relocation.relocation_type); - read_uint32(buf, buf_end, symbol_index); - - if (symbol_index >= symbol_count) { - set_error_buf(error_buf, error_buf_size, - "symbol index out of range"); - goto fail; - } - - symbol_name = symbol_buf + symbol_offsets[symbol_index]; - symbol_name_len = *(uint16 *)symbol_name; - symbol_name += sizeof(uint16); - - char group_name_buf[128] = { 0 }; - char symbol_name_buf[128] = { 0 }; - memcpy(group_name_buf, group_name, group_name_len); - memcpy(symbol_name_buf, symbol_name, symbol_name_len); - - if ((group_name_len == strlen(".text") - || (module->is_indirect_mode - && group_name_len == strlen(".text") + 1)) - && !strncmp(group_name, ".text", strlen(".text"))) { - if ((symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 - || (module->is_indirect_mode - && symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 + 1)) - && !strncmp(symbol_name, YMM_PLT_PREFIX, - strlen(YMM_PLT_PREFIX))) { - module->ymm_plt_count++; - } - else if ((symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 - || (module->is_indirect_mode - && symbol_name_len - == strlen(XMM_PLT_PREFIX) + 32 + 1)) - && !strncmp(symbol_name, XMM_PLT_PREFIX, - strlen(XMM_PLT_PREFIX))) { - module->xmm_plt_count++; - } - else if ((symbol_name_len == strlen(REAL_PLT_PREFIX) + 16 - || (module->is_indirect_mode - && symbol_name_len - == strlen(REAL_PLT_PREFIX) + 16 + 1)) - && !strncmp(symbol_name, REAL_PLT_PREFIX, - strlen(REAL_PLT_PREFIX))) { - module->real_plt_count++; - } - else if ((symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8 - || (module->is_indirect_mode - && symbol_name_len - == strlen(REAL_PLT_PREFIX) + 8 + 1)) - && !strncmp(symbol_name, REAL_PLT_PREFIX, - strlen(REAL_PLT_PREFIX))) { - module->float_plt_count++; - } - } - } - } - - /* Allocate memory for extra plt data */ - size = sizeof(uint64) * 4 * module->ymm_plt_count - + sizeof(uint64) * 2 * module->xmm_plt_count - + sizeof(uint64) * module->real_plt_count - + sizeof(uint32) * module->float_plt_count; - if (size > 0) { - map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; - /* aot code and data in x86_64 must be in range 0 to 2G due to - relocation for R_X86_64_32/32S/PC32 */ - map_flags = MMAP_MAP_32BIT; - - if (size > UINT32_MAX - || !(module->extra_plt_data = - os_mmap(NULL, (uint32)size, map_prot, map_flags))) { - set_error_buf(error_buf, error_buf_size, "mmap memory failed"); - goto fail; - } - module->extra_plt_data_size = (uint32)size; - } -#endif /* end of defined(BH_PLATFORM_WINDOWS) */ - - buf = symbol_buf_end; - read_uint32(buf, buf_end, group_count); - - /* Allocate memory for relocation groups */ - size = sizeof(AOTRelocationGroup) * (uint64)group_count; - if (size > 0 - && !(groups = loader_malloc(size, error_buf, error_buf_size))) { - goto fail; - } - - /* Load each relocation group */ - for (i = 0, group = groups; i < group_count; i++, group++) { - AOTRelocation *relocation; - uint32 name_index; - - /* section name address is 4 bytes aligned. */ - buf = (uint8 *)align_ptr(buf, sizeof(uint32)); - read_uint32(buf, buf_end, name_index); - - if (name_index >= symbol_count) { - set_error_buf(error_buf, error_buf_size, - "symbol index out of range"); - goto fail; - } - - if (symbols[name_index] == NULL) { - uint8 *name_addr = symbol_buf + symbol_offsets[name_index]; - - read_string(name_addr, buf_end, symbols[name_index]); - } - group->section_name = symbols[name_index]; - - read_uint32(buf, buf_end, group->relocation_count); - - /* Allocate memory for relocations */ - size = sizeof(AOTRelocation) * (uint64)group->relocation_count; - if (!(group->relocations = relocation = - loader_malloc(size, error_buf, error_buf_size))) { - ret = false; - goto fail; - } - - /* Load each relocation */ - for (j = 0; j < group->relocation_count; j++, relocation++) { - uint32 symbol_index; - - if (sizeof(void *) == 8) { - read_uint64(buf, buf_end, relocation->relocation_offset); - read_uint64(buf, buf_end, relocation->relocation_addend); - } - else { - uint32 offset32, addend32; - read_uint32(buf, buf_end, offset32); - relocation->relocation_offset = (uint64)offset32; - read_uint32(buf, buf_end, addend32); - relocation->relocation_addend = (uint64)addend32; - } - read_uint32(buf, buf_end, relocation->relocation_type); - read_uint32(buf, buf_end, symbol_index); - - if (symbol_index >= symbol_count) { - set_error_buf(error_buf, error_buf_size, - "symbol index out of range"); - goto fail; - } - - if (symbols[symbol_index] == NULL) { - uint8 *symbol_addr = symbol_buf + symbol_offsets[symbol_index]; - - read_string(symbol_addr, buf_end, symbols[symbol_index]); - } - relocation->symbol_name = symbols[symbol_index]; - } - - if (!strcmp(group->section_name, ".rel.text") - || !strcmp(group->section_name, ".rela.text") - || !strcmp(group->section_name, ".rela.literal") -#ifdef BH_PLATFORM_WINDOWS - || !strcmp(group->section_name, ".text") -#endif - ) { -#if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \ - && !defined(BH_PLATFORM_DARWIN) && !defined(BH_PLATFORM_WINDOWS) - if (module->is_indirect_mode) { - set_error_buf(error_buf, error_buf_size, - "cannot apply relocation to text section " - "for aot file generated with " - "\"--enable-indirect-mode\" flag"); - goto fail; - } -#endif - if (!do_text_relocation(module, group, error_buf, error_buf_size)) - goto fail; - } - else { - if (!do_data_relocation(module, group, error_buf, error_buf_size)) - goto fail; - } - } - - /* Set read only for AOT code and some data sections */ - map_prot = MMAP_PROT_READ | MMAP_PROT_EXEC; - - if (module->code) { - /* The layout is: literal size + literal + code (with plt table) */ - uint8 *mmap_addr = module->literal - sizeof(uint32); - uint32 total_size = - sizeof(uint32) + module->literal_size + module->code_size; - os_mprotect(mmap_addr, total_size, map_prot); - } - - map_prot = MMAP_PROT_READ; - -#if defined(BH_PLATFORM_WINDOWS) - if (module->extra_plt_data) { - os_mprotect(module->extra_plt_data, module->extra_plt_data_size, - map_prot); - } -#endif - - for (i = 0; i < module->data_section_count; i++) { - AOTObjectDataSection *data_section = module->data_sections + i; - if (!strcmp(data_section->name, ".rdata") - || !strcmp(data_section->name, ".rodata") - /* ".rodata.cst4/8/16/.." */ - || !strncmp(data_section->name, ".rodata.cst", - strlen(".rodata.cst")) - /* ".rodata.strn.m" */ - || !strncmp(data_section->name, ".rodata.str", - strlen(".rodata.str"))) { - os_mprotect(data_section->data, data_section->size, map_prot); - } - } - - ret = true; - -fail: - if (symbols) { - wasm_runtime_free(symbols); - } - if (groups) { - for (i = 0, group = groups; i < group_count; i++, group++) - if (group->relocations) - wasm_runtime_free(group->relocations); - wasm_runtime_free(groups); - } - - (void)map_flags; - return ret; -} - -static bool -load_from_sections(AOTModule *module, AOTSection *sections, - bool is_load_from_file_buf, char *error_buf, - uint32 error_buf_size) -{ - AOTSection *section = sections; - const uint8 *buf, *buf_end; - uint32 last_section_type = (uint32)-1, section_type; - uint32 i, func_index, func_type_index; - AOTFuncType *func_type; - AOTExport *exports; - - while (section) { - buf = section->section_body; - buf_end = buf + section->section_body_size; - /* Check sections */ - section_type = (uint32)section->section_type; - if ((last_section_type == (uint32)-1 - && section_type != AOT_SECTION_TYPE_TARGET_INFO) - || (last_section_type != (uint32)-1 - && (section_type != last_section_type + 1 - && section_type != AOT_SECTION_TYPE_CUSTOM))) { - set_error_buf(error_buf, error_buf_size, "invalid section order"); - return false; - } - last_section_type = section_type; - switch (section_type) { - case AOT_SECTION_TYPE_TARGET_INFO: - if (!load_target_info_section(buf, buf_end, module, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_INIT_DATA: - if (!load_init_data_section(buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_TEXT: - if (!load_text_section(buf, buf_end, module, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_FUNCTION: - if (!load_function_section(buf, buf_end, module, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_RELOCATION: - if (!load_relocation_section(buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - break; - case AOT_SECTION_TYPE_CUSTOM: - if (!load_custom_section(buf, buf_end, module, - is_load_from_file_buf, error_buf, - error_buf_size)) - return false; - break; - default: - set_error_buf(error_buf, error_buf_size, - "invalid aot section type"); - return false; - } - - section = section->next; - } - - if (last_section_type != AOT_SECTION_TYPE_RELOCATION - && last_section_type != AOT_SECTION_TYPE_CUSTOM) { - set_error_buf(error_buf, error_buf_size, "section missing"); - return false; - } - - /* Resolve malloc and free function */ - module->malloc_func_index = (uint32)-1; - module->free_func_index = (uint32)-1; - module->retain_func_index = (uint32)-1; - - exports = module->exports; - for (i = 0; i < module->export_count; i++) { - if (exports[i].kind == EXPORT_KIND_FUNC - && exports[i].index >= module->import_func_count) { - if (!strcmp(exports[i].name, "malloc")) { - func_index = exports[i].index - module->import_func_count; - func_type_index = module->func_type_indexes[func_index]; - func_type = module->func_types[func_type_index]; - if (func_type->param_count == 1 && func_type->result_count == 1 - && func_type->types[0] == VALUE_TYPE_I32 - && func_type->types[1] == VALUE_TYPE_I32) { - bh_assert(module->malloc_func_index == (uint32)-1); - module->malloc_func_index = func_index; - LOG_VERBOSE("Found malloc function, name: %s, index: %u", - exports[i].name, exports[i].index); - } - } - else if (!strcmp(exports[i].name, "__new")) { - func_index = exports[i].index - module->import_func_count; - func_type_index = module->func_type_indexes[func_index]; - func_type = module->func_types[func_type_index]; - if (func_type->param_count == 2 && func_type->result_count == 1 - && func_type->types[0] == VALUE_TYPE_I32 - && func_type->types[1] == VALUE_TYPE_I32 - && func_type->types[2] == VALUE_TYPE_I32) { - uint32 j; - WASMExport *export_tmp; - - bh_assert(module->malloc_func_index == (uint32)-1); - module->malloc_func_index = func_index; - LOG_VERBOSE("Found malloc function, name: %s, index: %u", - exports[i].name, exports[i].index); - - /* resolve retain function. - If not find, reset malloc function index */ - export_tmp = module->exports; - for (j = 0; j < module->export_count; j++, export_tmp++) { - if ((export_tmp->kind == EXPORT_KIND_FUNC) - && (!strcmp(export_tmp->name, "__retain") - || !strcmp(export_tmp->name, "__pin"))) { - func_index = - export_tmp->index - module->import_func_count; - func_type_index = - module->func_type_indexes[func_index]; - func_type = module->func_types[func_type_index]; - if (func_type->param_count == 1 - && func_type->result_count == 1 - && func_type->types[0] == VALUE_TYPE_I32 - && func_type->types[1] == VALUE_TYPE_I32) { - bh_assert(module->retain_func_index - == (uint32)-1); - module->retain_func_index = export_tmp->index; - LOG_VERBOSE("Found retain function, name: %s, " - "index: %u", - export_tmp->name, - export_tmp->index); - break; - } - } - } - if (j == module->export_count) { - module->malloc_func_index = (uint32)-1; - LOG_VERBOSE("Can't find retain function," - "reset malloc function index to -1"); - } - } - } - else if ((!strcmp(exports[i].name, "free")) - || (!strcmp(exports[i].name, "__release")) - || (!strcmp(exports[i].name, "__unpin"))) { - func_index = exports[i].index - module->import_func_count; - func_type_index = module->func_type_indexes[func_index]; - func_type = module->func_types[func_type_index]; - if (func_type->param_count == 1 && func_type->result_count == 0 - && func_type->types[0] == VALUE_TYPE_I32) { - bh_assert(module->free_func_index == (uint32)-1); - module->free_func_index = func_index; - LOG_VERBOSE("Found free function, name: %s, index: %u", - exports[i].name, exports[i].index); - } - } - } - } - - /* Flush data cache before executing AOT code, - * otherwise unpredictable behavior can occur. */ - os_dcache_flush(); - -#if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module); -#endif - -#if WASM_ENABLE_DEBUG_AOT != 0 - if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) { - set_error_buf(error_buf, error_buf_size, - "create jit code entry failed"); - return false; - } -#endif - return true; -} - -static AOTModule * -create_module(char *error_buf, uint32 error_buf_size) -{ - AOTModule *module = - loader_malloc(sizeof(AOTModule), error_buf, error_buf_size); - - if (!module) { - return NULL; - } - - module->module_type = Wasm_Module_AoT; - - return module; -} - -AOTModule * -aot_load_from_sections(AOTSection *section_list, char *error_buf, - uint32 error_buf_size) -{ - AOTModule *module = create_module(error_buf, error_buf_size); - - if (!module) - return NULL; - - if (!load_from_sections(module, section_list, false, error_buf, - error_buf_size)) { - aot_unload(module); - return NULL; - } - - LOG_VERBOSE("Load module from sections success.\n"); - return module; -} - -static void -destroy_sections(AOTSection *section_list, bool destroy_aot_text) -{ - AOTSection *section = section_list, *next; - while (section) { - next = section->next; - if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT - && section->section_body) - os_munmap((uint8 *)section->section_body, - section->section_body_size); - wasm_runtime_free(section); - section = next; - } -} - -static bool -resolve_execute_mode(const uint8 *buf, uint32 size, bool *p_mode, - char *error_buf, uint32 error_buf_size) -{ - const uint8 *p = buf, *p_end = buf + size; - uint32 section_type; - uint32 section_size = 0; - uint16 e_type = 0; - - p += 8; - while (p < p_end) { - read_uint32(p, p_end, section_type); - if (section_type <= AOT_SECTION_TYPE_SIGANATURE - || section_type == AOT_SECTION_TYPE_TARGET_INFO) { - read_uint32(p, p_end, section_size); - CHECK_BUF(p, p_end, section_size); - if (section_type == AOT_SECTION_TYPE_TARGET_INFO) { - p += 4; - read_uint16(p, p_end, e_type); - if (e_type == E_TYPE_XIP) { - *p_mode = true; - } - else { - *p_mode = false; - } - break; - } - } - else if (section_type > AOT_SECTION_TYPE_SIGANATURE) { - set_error_buf(error_buf, error_buf_size, - "resolve execute mode failed"); - break; - } - p += section_size; - } - return true; -fail: - return false; -} - -static bool -create_sections(AOTModule *module, const uint8 *buf, uint32 size, - AOTSection **p_section_list, char *error_buf, - uint32 error_buf_size) -{ - AOTSection *section_list = NULL, *section_list_end = NULL, *section; - const uint8 *p = buf, *p_end = buf + size; - bool destroy_aot_text = false; - bool is_indirect_mode = false; - uint32 section_type; - uint32 section_size; - uint64 total_size; - uint8 *aot_text; - - if (!resolve_execute_mode(buf, size, &is_indirect_mode, error_buf, - error_buf_size)) { - goto fail; - } - - module->is_indirect_mode = is_indirect_mode; - - p += 8; - while (p < p_end) { - read_uint32(p, p_end, section_type); - if (section_type < AOT_SECTION_TYPE_SIGANATURE - || section_type == AOT_SECTION_TYPE_CUSTOM) { - read_uint32(p, p_end, section_size); - CHECK_BUF(p, p_end, section_size); - - if (!(section = loader_malloc(sizeof(AOTSection), error_buf, - error_buf_size))) { - goto fail; - } - - memset(section, 0, sizeof(AOTSection)); - section->section_type = (int32)section_type; - section->section_body = (uint8 *)p; - section->section_body_size = section_size; - - if (section_type == AOT_SECTION_TYPE_TEXT) { - if ((section_size > 0) && !module->is_indirect_mode) { - int map_prot = - MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ - || defined(BUILD_TARGET_RISCV64_LP64) - /* aot code and data in x86_64 must be in range 0 to 2G due - to relocation for R_X86_64_32/32S/PC32 */ - int map_flags = MMAP_MAP_32BIT; -#else - int map_flags = MMAP_MAP_NONE; -#endif - total_size = - (uint64)section_size + aot_get_plt_table_size(); - total_size = (total_size + 3) & ~((uint64)3); - if (total_size >= UINT32_MAX - || !(aot_text = os_mmap(NULL, (uint32)total_size, - map_prot, map_flags))) { - wasm_runtime_free(section); - set_error_buf(error_buf, error_buf_size, - "mmap memory failed"); - goto fail; - } -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) -#if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \ - && !defined(BH_PLATFORM_DARWIN) - /* address must be in the first 2 Gigabytes of - the process address space */ - bh_assert((uintptr_t)aot_text < INT32_MAX); -#endif -#endif - bh_memcpy_s(aot_text, (uint32)total_size, - section->section_body, (uint32)section_size); - section->section_body = aot_text; - destroy_aot_text = true; - - if ((uint32)total_size > section->section_body_size) { - memset(aot_text + (uint32)section_size, 0, - (uint32)total_size - section_size); - section->section_body_size = (uint32)total_size; - } - } - } - - if (!section_list) - section_list = section_list_end = section; - else { - section_list_end->next = section; - section_list_end = section; - } - - p += section_size; - } - else { - set_error_buf(error_buf, error_buf_size, "invalid section id"); - goto fail; - } - } - - if (!section_list) { - set_error_buf(error_buf, error_buf_size, "create section list failed"); - return false; - } - - *p_section_list = section_list; - return true; -fail: - if (section_list) - destroy_sections(section_list, destroy_aot_text); - return false; -} - -static bool -load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf, - uint32 error_buf_size) -{ - const uint8 *buf_end = buf + size; - const uint8 *p = buf, *p_end = buf_end; - uint32 magic_number, version; - AOTSection *section_list = NULL; - bool ret; - - read_uint32(p, p_end, magic_number); - if (magic_number != AOT_MAGIC_NUMBER) { - set_error_buf(error_buf, error_buf_size, "magic header not detected"); - return false; - } - - read_uint32(p, p_end, version); - if (version != AOT_CURRENT_VERSION) { - set_error_buf(error_buf, error_buf_size, "unknown binary version"); - return false; - } - - if (!create_sections(module, buf, size, §ion_list, error_buf, - error_buf_size)) - return false; - - ret = load_from_sections(module, section_list, true, error_buf, - error_buf_size); - if (!ret) { - /* If load_from_sections() fails, then aot text is destroyed - in destroy_sections() */ - destroy_sections(section_list, module->is_indirect_mode ? false : true); - /* aot_unload() won't destroy aot text again */ - module->code = NULL; - } - else { - /* If load_from_sections() succeeds, then aot text is set to - module->code and will be destroyed in aot_unload() */ - destroy_sections(section_list, false); - } - return ret; -fail: - return false; -} - -AOTModule * -aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf, - uint32 error_buf_size) -{ - AOTModule *module = create_module(error_buf, error_buf_size); - - if (!module) - return NULL; - - if (!load(buf, size, module, error_buf, error_buf_size)) { - aot_unload(module); - return NULL; - } - - LOG_VERBOSE("Load module success.\n"); - return module; -} - -void -aot_unload(AOTModule *module) -{ - if (module->import_memories) - destroy_import_memories(module->import_memories); - - if (module->memories) - wasm_runtime_free(module->memories); - - if (module->mem_init_data_list) - destroy_mem_init_data_list(module->mem_init_data_list, - module->mem_init_data_count); - - if (module->native_symbol_list) - wasm_runtime_free(module->native_symbol_list); - - if (module->import_tables) - destroy_import_tables(module->import_tables); - - if (module->tables) - destroy_tables(module->tables); - - if (module->table_init_data_list) - destroy_table_init_data_list(module->table_init_data_list, - module->table_init_data_count); - - if (module->func_types) - destroy_func_types(module->func_types, module->func_type_count); - - if (module->import_globals) - destroy_import_globals(module->import_globals); - - if (module->globals) - destroy_globals(module->globals); - - if (module->import_funcs) - destroy_import_funcs(module->import_funcs); - - if (module->exports) - destroy_exports(module->exports); - - if (module->func_type_indexes) - wasm_runtime_free(module->func_type_indexes); - - if (module->func_ptrs) - wasm_runtime_free(module->func_ptrs); - - if (module->const_str_set) - bh_hash_map_destroy(module->const_str_set); - - if (module->code && !module->is_indirect_mode) { - /* The layout is: literal size + literal + code (with plt table) */ - uint8 *mmap_addr = module->literal - sizeof(uint32); - uint32 total_size = - sizeof(uint32) + module->literal_size + module->code_size; - os_munmap(mmap_addr, total_size); - } - -#if defined(BH_PLATFORM_WINDOWS) - if (module->extra_plt_data) { - os_munmap(module->extra_plt_data, module->extra_plt_data_size); - } -#endif - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - if (module->rtl_func_table) { - if (module->rtl_func_table_registered) - RtlDeleteFunctionTable(module->rtl_func_table); - wasm_runtime_free(module->rtl_func_table); - } -#endif - - if (module->data_sections) - destroy_object_data_sections(module->data_sections, - module->data_section_count); -#if WASM_ENABLE_DEBUG_AOT != 0 - jit_code_entry_destroy(module->elf_hdr); -#endif - -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - if (module->aux_func_indexes) { - wasm_runtime_free(module->aux_func_indexes); - } - if (module->aux_func_names) { - wasm_runtime_free((void *)module->aux_func_names); - } -#endif - -#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 - wasm_runtime_destroy_custom_sections(module->custom_section_list); -#endif - - wasm_runtime_free(module); -} - -uint32 -aot_get_plt_table_size() -{ - return get_plt_table_size(); -} - -#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 -const uint8 * -aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len) -{ - WASMCustomSection *section = module->custom_section_list; - - while (section) { - if (strcmp(section->name_addr, name) == 0) { - if (len) { - *len = section->content_len; - } - return section->content_addr; - } - - section = section->next; - } - - return NULL; -} -#endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_reloc.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_reloc.h deleted file mode 100644 index 9f5c2d57f..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_reloc.h +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _AOT_RELOC_H_ -#define _AOT_RELOC_H_ - -#include "aot_runtime.h" -#include "aot_intrinsic.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - const char *symbol_name; - void *symbol_addr; -} SymbolMap; - -/* clang-format off */ -#define REG_SYM(symbol) { #symbol, (void *)symbol } - -#if WASM_ENABLE_BULK_MEMORY != 0 -#define REG_BULK_MEMORY_SYM() \ - REG_SYM(aot_memory_init), \ - REG_SYM(aot_data_drop), -#else -#define REG_BULK_MEMORY_SYM() -#endif - -#if WASM_ENABLE_SHARED_MEMORY != 0 -#include "wasm_shared_memory.h" -#define REG_ATOMIC_WAIT_SYM() \ - REG_SYM(wasm_runtime_atomic_wait), \ - REG_SYM(wasm_runtime_atomic_notify), -#else -#define REG_ATOMIC_WAIT_SYM() -#endif - -#if WASM_ENABLE_REF_TYPES != 0 -#define REG_REF_TYPES_SYM() \ - REG_SYM(aot_drop_table_seg), \ - REG_SYM(aot_table_init), \ - REG_SYM(aot_table_copy), \ - REG_SYM(aot_table_fill), \ - REG_SYM(aot_table_grow), -#else -#define REG_REF_TYPES_SYM() -#endif - -#if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0) -#define REG_AOT_TRACE_SYM() \ - REG_SYM(aot_alloc_frame), \ - REG_SYM(aot_free_frame), -#else -#define REG_AOT_TRACE_SYM() -#endif - -#define REG_INTRINSIC_SYM() \ - REG_SYM(aot_intrinsic_fabs_f32), \ - REG_SYM(aot_intrinsic_fabs_f64), \ - REG_SYM(aot_intrinsic_floor_f32), \ - REG_SYM(aot_intrinsic_floor_f64), \ - REG_SYM(aot_intrinsic_ceil_f32), \ - REG_SYM(aot_intrinsic_ceil_f64), \ - REG_SYM(aot_intrinsic_trunc_f32), \ - REG_SYM(aot_intrinsic_trunc_f64), \ - REG_SYM(aot_intrinsic_rint_f32), \ - REG_SYM(aot_intrinsic_rint_f64), \ - REG_SYM(aot_intrinsic_sqrt_f32), \ - REG_SYM(aot_intrinsic_sqrt_f64), \ - REG_SYM(aot_intrinsic_copysign_f32), \ - REG_SYM(aot_intrinsic_copysign_f64), \ - REG_SYM(aot_intrinsic_fadd_f32), \ - REG_SYM(aot_intrinsic_fadd_f64), \ - REG_SYM(aot_intrinsic_fsub_f32), \ - REG_SYM(aot_intrinsic_fsub_f64), \ - REG_SYM(aot_intrinsic_fmul_f32), \ - REG_SYM(aot_intrinsic_fmul_f64), \ - REG_SYM(aot_intrinsic_fdiv_f32), \ - REG_SYM(aot_intrinsic_fdiv_f64), \ - REG_SYM(aot_intrinsic_fmin_f32), \ - REG_SYM(aot_intrinsic_fmin_f64), \ - REG_SYM(aot_intrinsic_fmax_f32), \ - REG_SYM(aot_intrinsic_fmax_f64), \ - REG_SYM(aot_intrinsic_clz_i32), \ - REG_SYM(aot_intrinsic_clz_i64), \ - REG_SYM(aot_intrinsic_ctz_i32), \ - REG_SYM(aot_intrinsic_ctz_i64), \ - REG_SYM(aot_intrinsic_popcnt_i32), \ - REG_SYM(aot_intrinsic_popcnt_i64), \ - REG_SYM(aot_intrinsic_i32_to_f32), \ - REG_SYM(aot_intrinsic_u32_to_f32), \ - REG_SYM(aot_intrinsic_i32_to_f64), \ - REG_SYM(aot_intrinsic_u32_to_f64), \ - REG_SYM(aot_intrinsic_i64_to_f32), \ - REG_SYM(aot_intrinsic_u64_to_f32), \ - REG_SYM(aot_intrinsic_i64_to_f64), \ - REG_SYM(aot_intrinsic_u64_to_f64), \ - REG_SYM(aot_intrinsic_f64_to_f32), \ - REG_SYM(aot_intrinsic_f32_to_i32), \ - REG_SYM(aot_intrinsic_f32_to_u32), \ - REG_SYM(aot_intrinsic_f32_to_i64), \ - REG_SYM(aot_intrinsic_f32_to_u64), \ - REG_SYM(aot_intrinsic_f64_to_i32), \ - REG_SYM(aot_intrinsic_f64_to_u32), \ - REG_SYM(aot_intrinsic_f64_to_i64), \ - REG_SYM(aot_intrinsic_f64_to_u64), \ - REG_SYM(aot_intrinsic_f32_to_f64), \ - REG_SYM(aot_intrinsic_f32_cmp), \ - REG_SYM(aot_intrinsic_f64_cmp), \ - REG_SYM(aot_intrinsic_i64_div_s), \ - REG_SYM(aot_intrinsic_i64_div_u), \ - REG_SYM(aot_intrinsic_i64_rem_s), \ - REG_SYM(aot_intrinsic_i64_rem_u), \ - REG_SYM(aot_intrinsic_i64_bit_or), \ - REG_SYM(aot_intrinsic_i64_bit_and), \ - REG_SYM(aot_intrinsic_i32_div_s), \ - REG_SYM(aot_intrinsic_i32_div_u), \ - REG_SYM(aot_intrinsic_i32_rem_s), \ - REG_SYM(aot_intrinsic_i32_rem_u), \ - -#define REG_COMMON_SYMBOLS \ - REG_SYM(aot_set_exception_with_id), \ - REG_SYM(aot_invoke_native), \ - REG_SYM(aot_call_indirect), \ - REG_SYM(aot_enlarge_memory), \ - REG_SYM(aot_set_exception), \ - REG_SYM(aot_check_app_addr_and_convert),\ - { "memset", (void*)aot_memset }, \ - { "memmove", (void*)aot_memmove }, \ - { "memcpy", (void*)aot_memmove }, \ - { "sqrt", (void*)aot_sqrt }, \ - { "sqrtf", (void*)aot_sqrtf }, \ - REG_SYM(fmin), \ - REG_SYM(fminf), \ - REG_SYM(fmax), \ - REG_SYM(fmaxf), \ - REG_SYM(ceil), \ - REG_SYM(ceilf), \ - REG_SYM(floor), \ - REG_SYM(floorf), \ - REG_SYM(trunc), \ - REG_SYM(truncf), \ - REG_SYM(rint), \ - REG_SYM(rintf), \ - REG_BULK_MEMORY_SYM() \ - REG_ATOMIC_WAIT_SYM() \ - REG_REF_TYPES_SYM() \ - REG_AOT_TRACE_SYM() \ - REG_INTRINSIC_SYM() \ - -#define CHECK_RELOC_OFFSET(data_size) do { \ - if (!check_reloc_offset(target_section_size, \ - reloc_offset, data_size, \ - error_buf, error_buf_size)) \ - return false; \ - } while (0) - -SymbolMap * -get_target_symbol_map(uint32 *sym_num); - -uint32 -get_plt_table_size(); - -void -init_plt_table(uint8 *plt); - -void -get_current_target(char *target_buf, uint32 target_buf_size); - -bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, int64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size); -/* clang-format off */ - -#ifdef __cplusplus -} -#endif - -#endif /* end of _AOT_RELOC_H_ */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.c deleted file mode 100644 index b5c406b96..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.c +++ /dev/null @@ -1,2836 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_runtime.h" -#include "bh_log.h" -#include "mem_alloc.h" -#include "../common/wasm_runtime_common.h" -#include "../interpreter/wasm_runtime.h" -#if WASM_ENABLE_SHARED_MEMORY != 0 -#include "../common/wasm_shared_memory.h" -#endif -#if WASM_ENABLE_THREAD_MGR != 0 -#include "../libraries/thread-mgr/thread_manager.h" -#endif - -/* - * Note: These offsets need to match the values hardcoded in - * AoT compilation code: aot_create_func_context, check_suspend_flags. - */ - -bh_static_assert(offsetof(WASMExecEnv, module_inst) == 2 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, argv_buf) == 3 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, native_stack_boundary) - == 4 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, suspend_flags) == 5 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, aux_stack_boundary) - == 6 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, aux_stack_bottom) - == 7 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, native_symbol) == 8 * sizeof(uintptr_t)); -bh_static_assert(offsetof(WASMExecEnv, native_stack_top_min) - == 9 * sizeof(uintptr_t)); - -bh_static_assert(offsetof(AOTModuleInstance, memories) == 1 * sizeof(uint64)); -bh_static_assert(offsetof(AOTModuleInstance, func_ptrs) == 5 * sizeof(uint64)); -bh_static_assert(offsetof(AOTModuleInstance, func_type_indexes) - == 6 * sizeof(uint64)); -bh_static_assert(offsetof(AOTModuleInstance, cur_exception) - == 13 * sizeof(uint64)); -bh_static_assert(offsetof(AOTModuleInstance, global_table_data) - == 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64)); - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) { - snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s", - string); - } -} - -static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) -{ - va_list args; - char buf[128]; - - if (error_buf != NULL) { - va_start(args, format); - vsnprintf(buf, sizeof(buf), format, args); - va_end(args); - snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s", - buf); - } -} - -static void * -runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) -{ - void *mem; - - if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, "allocate memory failed"); - return NULL; - } - - memset(mem, 0, (uint32)size); - return mem; -} - -static bool -check_global_init_expr(const AOTModule *module, uint32 global_index, - char *error_buf, uint32 error_buf_size) -{ - if (global_index >= module->import_global_count + module->global_count) { - set_error_buf_v(error_buf, error_buf_size, "unknown global %d", - global_index); - return false; - } - - /** - * Currently, constant expressions occurring as initializers of - * globals are further constrained in that contained global.get - * instructions are only allowed to refer to imported globals. - * - * And initializer expression cannot reference a mutable global. - */ - if (global_index >= module->import_global_count - || module->import_globals->is_mutable) { - set_error_buf(error_buf, error_buf_size, - "constant expression required"); - return false; - } - - return true; -} - -static void -init_global_data(uint8 *global_data, uint8 type, WASMValue *initial_value) -{ - switch (type) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: -#if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: -#endif - *(int32 *)global_data = initial_value->i32; - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - bh_memcpy_s(global_data, sizeof(int64), &initial_value->i64, - sizeof(int64)); - break; -#if WASM_ENABLE_SIMD != 0 - case VALUE_TYPE_V128: - bh_memcpy_s(global_data, sizeof(V128), &initial_value->v128, - sizeof(V128)); - break; -#endif - default: - bh_assert(0); - } -} - -static bool -global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - uint32 i; - InitializerExpression *init_expr; - uint8 *p = module_inst->global_data; - AOTImportGlobal *import_global = module->import_globals; - AOTGlobal *global = module->globals; - - /* Initialize import global data */ - for (i = 0; i < module->import_global_count; i++, import_global++) { - bh_assert(import_global->data_offset - == (uint32)(p - module_inst->global_data)); - init_global_data(p, import_global->type, - &import_global->global_data_linked); - p += import_global->size; - } - - /* Initialize defined global data */ - for (i = 0; i < module->global_count; i++, global++) { - bh_assert(global->data_offset - == (uint32)(p - module_inst->global_data)); - init_expr = &global->init_expr; - switch (init_expr->init_expr_type) { - case INIT_EXPR_TYPE_GET_GLOBAL: - { - if (!check_global_init_expr(module, init_expr->u.global_index, - error_buf, error_buf_size)) { - return false; - } - init_global_data( - p, global->type, - &module->import_globals[init_expr->u.global_index] - .global_data_linked); - break; - } -#if WASM_ENABLE_REF_TYPES != 0 - case INIT_EXPR_TYPE_REFNULL_CONST: - { - *(uint32 *)p = NULL_REF; - break; - } -#endif - default: - { - init_global_data(p, global->type, &init_expr->u); - break; - } - } - p += global->size; - } - - bh_assert(module_inst->global_data_size - == (uint32)(p - module_inst->global_data)); - return true; -} - -static bool -tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, - AOTTableInstance *first_tbl_inst, char *error_buf, - uint32 error_buf_size) -{ - uint32 i, global_index, global_data_offset, base_offset, length; - uint64 total_size; - AOTTableInitData *table_seg; - AOTTableInstance *tbl_inst = first_tbl_inst; - - total_size = (uint64)sizeof(WASMTableInstance *) * module_inst->table_count; - if (total_size > 0 - && !(module_inst->tables = - runtime_malloc(total_size, error_buf, error_buf_size))) { - return false; - } - - /* - * treat import table like a local one until we enable module linking - * in AOT mode - */ - for (i = 0; i != module_inst->table_count; ++i) { - if (i < module->import_table_count) { - AOTImportTable *import_table = module->import_tables + i; - tbl_inst->cur_size = import_table->table_init_size; - tbl_inst->max_size = - aot_get_imp_tbl_data_slots(import_table, false); - } - else { - AOTTable *table = module->tables + (i - module->import_table_count); - tbl_inst->cur_size = table->table_init_size; - tbl_inst->max_size = aot_get_tbl_data_slots(table, false); - } - - /* Set all elements to -1 to mark them as uninitialized elements */ - memset(tbl_inst->elems, 0xff, sizeof(uint32) * tbl_inst->max_size); - - module_inst->tables[i] = tbl_inst; - tbl_inst = (AOTTableInstance *)((uint8 *)tbl_inst - + offsetof(AOTTableInstance, elems) - + sizeof(uint32) * tbl_inst->max_size); - } - - /* fill table with element segment content */ - for (i = 0; i < module->table_init_data_count; i++) { - table_seg = module->table_init_data_list[i]; - -#if WASM_ENABLE_REF_TYPES != 0 - if (!wasm_elem_is_active(table_seg->mode)) - continue; -#endif - - bh_assert(table_seg->table_index < module_inst->table_count); - - tbl_inst = module_inst->tables[table_seg->table_index]; - bh_assert(tbl_inst); - -#if WASM_ENABLE_REF_TYPES != 0 - bh_assert( - table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST - || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL - || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST - || table_seg->offset.init_expr_type - == INIT_EXPR_TYPE_REFNULL_CONST); -#else - bh_assert(table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST - || table_seg->offset.init_expr_type - == INIT_EXPR_TYPE_GET_GLOBAL); -#endif - - /* Resolve table data base offset */ - if (table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) { - global_index = table_seg->offset.u.global_index; - - if (!check_global_init_expr(module, global_index, error_buf, - error_buf_size)) { - return false; - } - - if (global_index < module->import_global_count) - global_data_offset = - module->import_globals[global_index].data_offset; - else - global_data_offset = - module->globals[global_index - module->import_global_count] - .data_offset; - - base_offset = - *(uint32 *)(module_inst->global_data + global_data_offset); - } - else - base_offset = (uint32)table_seg->offset.u.i32; - - /* Copy table data */ - /* base_offset only since length might negative */ - if (base_offset > tbl_inst->cur_size) { -#if WASM_ENABLE_REF_TYPES != 0 - set_error_buf(error_buf, error_buf_size, - "out of bounds table access"); -#else - set_error_buf(error_buf, error_buf_size, - "elements segment does not fit"); -#endif - return false; - } - - /* base_offset + length(could be zero) */ - length = table_seg->func_index_count; - if (base_offset + length > tbl_inst->cur_size) { -#if WASM_ENABLE_REF_TYPES != 0 - set_error_buf(error_buf, error_buf_size, - "out of bounds table access"); -#else - set_error_buf(error_buf, error_buf_size, - "elements segment does not fit"); -#endif - return false; - } - - /** - * Check function index in the current module inst for now. - * will check the linked table inst owner in future - */ - bh_memcpy_s(tbl_inst->elems + base_offset, - (tbl_inst->max_size - base_offset) * sizeof(uint32), - table_seg->func_indexes, length * sizeof(uint32)); - } - - return true; -} - -static void -memories_deinstantiate(AOTModuleInstance *module_inst) -{ - uint32 i; - AOTMemoryInstance *memory_inst; - - for (i = 0; i < module_inst->memory_count; i++) { - memory_inst = module_inst->memories[i]; - if (memory_inst) { -#if WASM_ENABLE_SHARED_MEMORY != 0 - if (memory_inst->is_shared) { - int32 ref_count = shared_memory_dec_reference( - (WASMModuleCommon *)module_inst->module); - bh_assert(ref_count >= 0); - - /* if the reference count is not zero, - don't free the memory */ - if (ref_count > 0) - continue; - } -#endif - if (memory_inst->heap_handle) { - mem_allocator_destroy(memory_inst->heap_handle); - wasm_runtime_free(memory_inst->heap_handle); - } - - if (memory_inst->memory_data) { -#ifndef OS_ENABLE_HW_BOUND_CHECK - wasm_runtime_free(memory_inst->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(memory_inst->memory_data, - memory_inst->num_bytes_per_page - * memory_inst->cur_page_count); -#endif - os_munmap(memory_inst->memory_data, 8 * (uint64)BH_GB); -#endif - } - } - } - wasm_runtime_free(module_inst->memories); -} - -static AOTMemoryInstance * -memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, - AOTMemoryInstance *memory_inst, AOTMemory *memory, - uint32 heap_size, char *error_buf, uint32 error_buf_size) -{ - void *heap_handle; - uint32 num_bytes_per_page = memory->num_bytes_per_page; - uint32 init_page_count = memory->mem_init_page_count; - uint32 max_page_count = memory->mem_max_page_count; - uint32 inc_page_count, aux_heap_base, global_idx; - uint32 bytes_of_last_page, bytes_to_page_end; - uint32 heap_offset = num_bytes_per_page * init_page_count; - uint64 total_size; - uint8 *p = NULL, *global_addr; -#ifdef OS_ENABLE_HW_BOUND_CHECK - uint8 *mapped_mem; - uint64 map_size = 8 * (uint64)BH_GB; - uint64 page_size = os_getpagesize(); -#endif - -#if WASM_ENABLE_SHARED_MEMORY != 0 - bool is_shared_memory = memory->memory_flags & 0x02 ? true : false; - - /* Shared memory */ - if (is_shared_memory) { - AOTMemoryInstance *shared_memory_instance; - WASMSharedMemNode *node = - wasm_module_get_shared_memory((WASMModuleCommon *)module); - /* If the memory of this module has been instantiated, - return the memory instance directly */ - if (node) { - uint32 ref_count; - ref_count = shared_memory_inc_reference((WASMModuleCommon *)module); - bh_assert(ref_count > 0); - shared_memory_instance = - (AOTMemoryInstance *)shared_memory_get_memory_inst(node); - bh_assert(shared_memory_instance); - - (void)ref_count; - return shared_memory_instance; - } - } -#endif - - if (heap_size > 0 && module->malloc_func_index != (uint32)-1 - && module->free_func_index != (uint32)-1) { - /* Disable app heap, use malloc/free function exported - by wasm app to allocate/free memory instead */ - heap_size = 0; - } - - if (init_page_count == max_page_count && init_page_count == 1) { - /* If only one page and at most one page, we just append - the app heap to the end of linear memory, enlarge the - num_bytes_per_page, and don't change the page count */ - heap_offset = num_bytes_per_page; - num_bytes_per_page += heap_size; - if (num_bytes_per_page < heap_size) { - set_error_buf(error_buf, error_buf_size, - "failed to insert app heap into linear memory, " - "try using `--heap_size=0` option"); - return NULL; - } - } - else if (heap_size > 0) { - if (init_page_count == max_page_count && init_page_count == 0) { - /* If the memory data size is always 0, we resize it to - one page for app heap */ - num_bytes_per_page = heap_size; - heap_offset = 0; - inc_page_count = 1; - } - else if (module->aux_heap_base_global_index != (uint32)-1 - && module->aux_heap_base - < num_bytes_per_page * init_page_count) { - /* Insert app heap before __heap_base */ - aux_heap_base = module->aux_heap_base; - bytes_of_last_page = aux_heap_base % num_bytes_per_page; - if (bytes_of_last_page == 0) - bytes_of_last_page = num_bytes_per_page; - bytes_to_page_end = num_bytes_per_page - bytes_of_last_page; - inc_page_count = - (heap_size - bytes_to_page_end + num_bytes_per_page - 1) - / num_bytes_per_page; - heap_offset = aux_heap_base; - aux_heap_base += heap_size; - - bytes_of_last_page = aux_heap_base % num_bytes_per_page; - if (bytes_of_last_page == 0) - bytes_of_last_page = num_bytes_per_page; - bytes_to_page_end = num_bytes_per_page - bytes_of_last_page; - if (bytes_to_page_end < 1 * BH_KB) { - aux_heap_base += 1 * BH_KB; - inc_page_count++; - } - - /* Adjust __heap_base global value */ - global_idx = module->aux_heap_base_global_index - - module->import_global_count; - global_addr = module_inst->global_data - + module->globals[global_idx].data_offset; - *(uint32 *)global_addr = aux_heap_base; - LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base); - } - else { - /* Insert app heap before new page */ - inc_page_count = - (heap_size + num_bytes_per_page - 1) / num_bytes_per_page; - heap_offset = num_bytes_per_page * init_page_count; - heap_size = num_bytes_per_page * inc_page_count; - if (heap_size > 0) - heap_size -= 1 * BH_KB; - } - init_page_count += inc_page_count; - max_page_count += inc_page_count; - if (init_page_count > DEFAULT_MAX_PAGES) { - set_error_buf(error_buf, error_buf_size, - "failed to insert app heap into linear memory, " - "try using `--heap_size=0` option"); - return NULL; - } - else if (init_page_count == DEFAULT_MAX_PAGES) { - num_bytes_per_page = UINT32_MAX; - init_page_count = max_page_count = 1; - } - if (max_page_count > DEFAULT_MAX_PAGES) - max_page_count = DEFAULT_MAX_PAGES; - } - - LOG_VERBOSE("Memory instantiate:"); - LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u", - num_bytes_per_page, init_page_count, max_page_count); - LOG_VERBOSE(" data offset: %u, stack size: %d", module->aux_data_end, - module->aux_stack_size); - LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size); - - total_size = (uint64)num_bytes_per_page * init_page_count; -#if WASM_ENABLE_SHARED_MEMORY != 0 - if (is_shared_memory) { - /* Allocate max page for shared memory */ - total_size = (uint64)num_bytes_per_page * max_page_count; - } -#endif - bh_assert(total_size <= UINT32_MAX); - -#ifndef OS_ENABLE_HW_BOUND_CHECK - /* Allocate memory */ - if (total_size > 0 - && !(p = runtime_malloc(total_size, error_buf, error_buf_size))) { - return NULL; - } -#else - total_size = (total_size + page_size - 1) & ~(page_size - 1); - - /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G: - * ea = i + memarg.offset - * both i and memarg.offset are u32 in range 0 to 4G - * so the range of ea is 0 to 8G - */ - if (!(p = mapped_mem = - os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { - set_error_buf(error_buf, error_buf_size, "mmap memory failed"); - return NULL; - } - -#ifdef BH_PLATFORM_WINDOWS - if (!os_mem_commit(p, total_size, MMAP_PROT_READ | MMAP_PROT_WRITE)) { - set_error_buf(error_buf, error_buf_size, "commit memory failed"); - os_munmap(mapped_mem, map_size); - return NULL; - } -#endif - - if (os_mprotect(p, total_size, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { - set_error_buf(error_buf, error_buf_size, "mprotect memory failed"); -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(p, total_size); -#endif - os_munmap(mapped_mem, map_size); - return NULL; - } - /* Newly allocated pages are filled with zero by the OS, we don't fill it - * again here */ -#endif /* end of OS_ENABLE_HW_BOUND_CHECK */ - - if (total_size > UINT32_MAX) - total_size = UINT32_MAX; - - memory_inst->module_type = Wasm_Module_AoT; - memory_inst->num_bytes_per_page = num_bytes_per_page; - memory_inst->cur_page_count = init_page_count; - memory_inst->max_page_count = max_page_count; - memory_inst->memory_data_size = (uint32)total_size; - - /* Init memory info */ - memory_inst->memory_data = p; - memory_inst->memory_data_end = p + (uint32)total_size; - - /* Initialize heap info */ - memory_inst->heap_data = p + heap_offset; - memory_inst->heap_data_end = p + heap_offset + heap_size; - if (heap_size > 0) { - uint32 heap_struct_size = mem_allocator_get_heap_struct_size(); - - if (!(heap_handle = runtime_malloc((uint64)heap_struct_size, error_buf, - error_buf_size))) { - goto fail1; - } - - memory_inst->heap_handle = heap_handle; - - if (!mem_allocator_create_with_struct_and_pool( - heap_handle, heap_struct_size, memory_inst->heap_data, - heap_size)) { - set_error_buf(error_buf, error_buf_size, "init app heap failed"); - goto fail2; - } - } - - if (total_size > 0) { -#if UINTPTR_MAX == UINT64_MAX - memory_inst->mem_bound_check_1byte.u64 = total_size - 1; - memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; - memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; - memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; - memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; -#else - memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; - memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; - memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; - memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; - memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16; -#endif - } - -#if WASM_ENABLE_SHARED_MEMORY != 0 - if (is_shared_memory) { - memory_inst->is_shared = true; - if (!shared_memory_set_memory_inst( - (WASMModuleCommon *)module, - (WASMMemoryInstanceCommon *)memory_inst)) { - set_error_buf(error_buf, error_buf_size, "allocate memory failed"); - goto fail3; - } - } -#endif - - return memory_inst; - -#if WASM_ENABLE_SHARED_MEMORY != 0 -fail3: - if (heap_size > 0) - mem_allocator_destroy(memory_inst->heap_handle); -#endif -fail2: - if (heap_size > 0) - wasm_runtime_free(memory_inst->heap_handle); -fail1: -#ifndef OS_ENABLE_HW_BOUND_CHECK - if (memory_inst->memory_data) - wasm_runtime_free(memory_inst->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - if (memory_inst->memory_data) - os_mem_decommit(p, total_size); -#endif - os_munmap(mapped_mem, map_size); -#endif - memory_inst->memory_data = NULL; - return NULL; -} - -static AOTMemoryInstance * -aot_get_default_memory(AOTModuleInstance *module_inst) -{ - if (module_inst->memories) - return module_inst->memories[0]; - else - return NULL; -} - -static bool -memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module, - uint32 heap_size, char *error_buf, uint32 error_buf_size) -{ - uint32 global_index, global_data_offset, base_offset, length; - uint32 i, memory_count = module->memory_count; - AOTMemoryInstance *memories, *memory_inst; - AOTMemInitData *data_seg; - uint64 total_size; - - module_inst->memory_count = memory_count; - total_size = sizeof(AOTMemoryInstance *) * (uint64)memory_count; - if (!(module_inst->memories = - runtime_malloc(total_size, error_buf, error_buf_size))) { - return false; - } - - memories = module_inst->global_table_data.memory_instances; - for (i = 0; i < memory_count; i++, memories++) { - memory_inst = memory_instantiate(module_inst, module, memories, - &module->memories[i], heap_size, - error_buf, error_buf_size); - if (!memory_inst) { - return false; - } - - module_inst->memories[i] = memory_inst; - } - - /* Get default memory instance */ - memory_inst = aot_get_default_memory(module_inst); - if (!memory_inst) { - /* Ignore setting memory init data if no memory inst is created */ - return true; - } - - for (i = 0; i < module->mem_init_data_count; i++) { - data_seg = module->mem_init_data_list[i]; -#if WASM_ENABLE_BULK_MEMORY != 0 - if (data_seg->is_passive) - continue; -#endif - - bh_assert(data_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST - || data_seg->offset.init_expr_type - == INIT_EXPR_TYPE_GET_GLOBAL); - - /* Resolve memory data base offset */ - if (data_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) { - global_index = data_seg->offset.u.global_index; - - if (!check_global_init_expr(module, global_index, error_buf, - error_buf_size)) { - return false; - } - - if (global_index < module->import_global_count) - global_data_offset = - module->import_globals[global_index].data_offset; - else - global_data_offset = - module->globals[global_index - module->import_global_count] - .data_offset; - - base_offset = - *(uint32 *)(module_inst->global_data + global_data_offset); - } - else { - base_offset = (uint32)data_seg->offset.u.i32; - } - - /* Copy memory data */ - bh_assert(memory_inst->memory_data - || memory_inst->memory_data_size == 0); - - /* Check memory data */ - /* check offset since length might negative */ - if (base_offset > memory_inst->memory_data_size) { - LOG_DEBUG("base_offset(%d) > memory_data_size(%d)", base_offset, - memory_inst->memory_data_size); -#if WASM_ENABLE_REF_TYPES != 0 - set_error_buf(error_buf, error_buf_size, - "out of bounds memory access"); -#else - set_error_buf(error_buf, error_buf_size, - "data segment does not fit"); -#endif - return false; - } - - /* check offset + length(could be zero) */ - length = data_seg->byte_count; - if (base_offset + length > memory_inst->memory_data_size) { - LOG_DEBUG("base_offset(%d) + length(%d) > memory_data_size(%d)", - base_offset, length, memory_inst->memory_data_size); -#if WASM_ENABLE_REF_TYPES != 0 - set_error_buf(error_buf, error_buf_size, - "out of bounds memory access"); -#else - set_error_buf(error_buf, error_buf_size, - "data segment does not fit"); -#endif - return false; - } - - if (memory_inst->memory_data) { - bh_memcpy_s((uint8 *)memory_inst->memory_data + base_offset, - memory_inst->memory_data_size - base_offset, - data_seg->bytes, length); - } - } - - return true; -} - -static bool -init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - uint32 i; - void **func_ptrs; - uint64 total_size = ((uint64)module->import_func_count + module->func_count) - * sizeof(void *); - - if (module->import_func_count + module->func_count == 0) - return true; - - /* Allocate memory */ - if (!(module_inst->func_ptrs = - runtime_malloc(total_size, error_buf, error_buf_size))) { - return false; - } - - /* Set import function pointers */ - func_ptrs = (void **)module_inst->func_ptrs; - for (i = 0; i < module->import_func_count; i++, func_ptrs++) { - *func_ptrs = (void *)module->import_funcs[i].func_ptr_linked; - if (!*func_ptrs) { - const char *module_name = module->import_funcs[i].module_name; - const char *field_name = module->import_funcs[i].func_name; - LOG_WARNING("warning: failed to link import function (%s, %s)", - module_name, field_name); - } - } - - /* Set defined function pointers */ - bh_memcpy_s(func_ptrs, sizeof(void *) * module->func_count, - module->func_ptrs, sizeof(void *) * module->func_count); - return true; -} - -static bool -init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - uint32 i; - uint32 *func_type_index; - uint64 total_size = ((uint64)module->import_func_count + module->func_count) - * sizeof(uint32); - - if (module->import_func_count + module->func_count == 0) - return true; - - /* Allocate memory */ - if (!(module_inst->func_type_indexes = - runtime_malloc(total_size, error_buf, error_buf_size))) { - return false; - } - - /* Set import function type indexes */ - func_type_index = module_inst->func_type_indexes; - for (i = 0; i < module->import_func_count; i++, func_type_index++) - *func_type_index = module->import_funcs[i].func_type_index; - - bh_memcpy_s(func_type_index, sizeof(uint32) * module->func_count, - module->func_type_indexes, sizeof(uint32) * module->func_count); - return true; -} - -static bool -create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - AOTExport *exports = module->exports; - AOTFunctionInstance *export_func; - uint64 size; - uint32 i, func_index, ftype_index; - - if (module_inst->export_func_count > 0) { - /* Allocate memory */ - size = sizeof(AOTFunctionInstance) - * (uint64)module_inst->export_func_count; - if (!(export_func = runtime_malloc(size, error_buf, error_buf_size))) { - return false; - } - module_inst->export_functions = (void *)export_func; - - for (i = 0; i < module->export_count; i++) { - if (exports[i].kind == EXPORT_KIND_FUNC) { - export_func->func_name = exports[i].name; - export_func->func_index = exports[i].index; - if (export_func->func_index < module->import_func_count) { - export_func->is_import_func = true; - export_func->u.func_import = - &module->import_funcs[export_func->func_index]; - } - else { - export_func->is_import_func = false; - func_index = - export_func->func_index - module->import_func_count; - ftype_index = module->func_type_indexes[func_index]; - export_func->u.func.func_type = - module->func_types[ftype_index]; - export_func->u.func.func_ptr = - module->func_ptrs[func_index]; - } - export_func++; - } - } - } - - return true; -} - -static bool -create_exports(AOTModuleInstance *module_inst, AOTModule *module, - char *error_buf, uint32 error_buf_size) -{ - AOTExport *exports = module->exports; - uint32 i; - - for (i = 0; i < module->export_count; i++) { - switch (exports[i].kind) { - case EXPORT_KIND_FUNC: - module_inst->export_func_count++; - break; - case EXPORT_KIND_GLOBAL: - module_inst->export_global_count++; - break; - case EXPORT_KIND_TABLE: - module_inst->export_table_count++; - break; - case EXPORT_KIND_MEMORY: - module_inst->export_memory_count++; - break; - default: - return false; - } - } - - return create_export_funcs(module_inst, module, error_buf, error_buf_size); -} - -static AOTFunctionInstance * -lookup_post_instantiate_func(AOTModuleInstance *module_inst, - const char *func_name) -{ - AOTFunctionInstance *func; - AOTFuncType *func_type; - - if (!(func = aot_lookup_function(module_inst, func_name, NULL))) - /* Not found */ - return NULL; - - func_type = func->u.func.func_type; - if (!(func_type->param_count == 0 && func_type->result_count == 0)) - /* Not a valid function type, ignore it */ - return NULL; - - return func; -} - -static bool -execute_post_instantiate_functions(AOTModuleInstance *module_inst, - bool is_sub_inst, WASMExecEnv *exec_env_main) -{ - AOTModule *module = (AOTModule *)module_inst->module; - AOTFunctionInstance *initialize_func = NULL; - AOTFunctionInstance *post_inst_func = NULL; - AOTFunctionInstance *call_ctors_func = NULL; - WASMModuleInstanceCommon *module_inst_main = NULL; -#ifdef OS_ENABLE_HW_BOUND_CHECK - WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls(); -#endif - WASMExecEnv *exec_env = NULL, *exec_env_created = NULL; - bool ret = false; - -#if WASM_ENABLE_LIBC_WASI != 0 - /* - * WASI reactor instances may assume that _initialize will be called by - * the environment at most once, and that none of their other exports - * are accessed before that call. - */ - if (!is_sub_inst && module->import_wasi_api) { - initialize_func = - lookup_post_instantiate_func(module_inst, "_initialize"); - } -#endif - - /* Execute possible "__post_instantiate" function if wasm app is - compiled by emsdk's early version */ - if (!is_sub_inst) { - post_inst_func = - lookup_post_instantiate_func(module_inst, "__post_instantiate"); - } - -#if WASM_ENABLE_BULK_MEMORY != 0 - /* Only execute the memory init function for main instance since - the data segments will be dropped once initialized */ - if (!is_sub_inst -#if WASM_ENABLE_LIBC_WASI != 0 - && !module->import_wasi_api -#endif - ) { - call_ctors_func = - lookup_post_instantiate_func(module_inst, "__wasm_call_ctors"); - } -#endif - - if (!module->start_function && !initialize_func && !post_inst_func - && !call_ctors_func) { - /* No post instantiation functions to call */ - return true; - } - - if (is_sub_inst) { - bh_assert(exec_env_main); -#ifdef OS_ENABLE_HW_BOUND_CHECK - bh_assert(exec_env_tls == exec_env_main); - (void)exec_env_tls; -#endif - exec_env = exec_env_main; - - /* Temporarily replace parent exec_env's module inst to current - module inst to avoid checking failure when calling the - wasm functions, and ensure that the exec_env's module inst - is the correct one. */ - module_inst_main = exec_env_main->module_inst; - exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst; - } - else { - /* Try using the existing exec_env */ -#ifdef OS_ENABLE_HW_BOUND_CHECK - exec_env = exec_env_tls; -#endif -#if WASM_ENABLE_THREAD_MGR != 0 - if (!exec_env) - exec_env = wasm_clusters_search_exec_env( - (WASMModuleInstanceCommon *)module_inst); -#endif - if (!exec_env) { - if (!(exec_env = exec_env_created = wasm_exec_env_create( - (WASMModuleInstanceCommon *)module_inst, - module_inst->default_wasm_stack_size))) { - aot_set_exception(module_inst, "allocate memory failed"); - return false; - } - } - else { - /* Temporarily replace exec_env's module inst with current - module inst to ensure that the exec_env's module inst - is the correct one. */ - module_inst_main = exec_env->module_inst; - exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst; - } - } - - /* Execute start function for both main insance and sub instance */ - if (module->start_function) { - AOTFunctionInstance start_func = { 0 }; - uint32 func_type_idx; - - start_func.func_name = ""; - start_func.func_index = module->start_func_index; - start_func.is_import_func = false; - func_type_idx = module->func_type_indexes[module->start_func_index - - module->import_func_count]; - start_func.u.func.func_type = module->func_types[func_type_idx]; - start_func.u.func.func_ptr = module->start_function; - if (!aot_call_function(exec_env, &start_func, 0, NULL)) { - goto fail; - } - } - - if (initialize_func - && !aot_call_function(exec_env, initialize_func, 0, NULL)) { - goto fail; - } - - if (post_inst_func - && !aot_call_function(exec_env, post_inst_func, 0, NULL)) { - goto fail; - } - - if (call_ctors_func - && !aot_call_function(exec_env, call_ctors_func, 0, NULL)) { - goto fail; - } - - ret = true; - -fail: - if (is_sub_inst) { - /* Restore the parent exec_env's module inst */ - exec_env_main->module_inst = module_inst_main; - } - else { - if (module_inst_main) - /* Restore the existing exec_env's module inst */ - exec_env->module_inst = module_inst_main; - if (exec_env_created) - wasm_exec_env_destroy(exec_env_created); - } - - return ret; -} - -static bool -check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size) -{ - uint32 i; - - /* init_func_ptrs() will go through import functions */ - - for (i = 0; i < module->import_global_count; i++) { - AOTImportGlobal *global = module->import_globals + i; - if (!global->is_linked) { - set_error_buf_v(error_buf, error_buf_size, - "failed to link import global (%s, %s)", - global->module_name, global->global_name); - return false; - } - } - - return true; -} - -AOTModuleInstance * -aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main, - uint32 stack_size, uint32 heap_size, char *error_buf, - uint32 error_buf_size) -{ - AOTModuleInstance *module_inst; - const uint32 module_inst_struct_size = - offsetof(AOTModuleInstance, global_table_data.bytes); - const uint64 module_inst_mem_inst_size = - (uint64)module->memory_count * sizeof(AOTMemoryInstance); - uint64 total_size, table_size = 0; - uint8 *p; - uint32 i, extra_info_offset; - - /* Check heap size */ - heap_size = align_uint(heap_size, 8); - if (heap_size > APP_HEAP_SIZE_MAX) - heap_size = APP_HEAP_SIZE_MAX; - - total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size - + module->global_data_size; - - /* - * calculate size of table data - */ - for (i = 0; i != module->import_table_count; ++i) { - table_size += offsetof(AOTTableInstance, elems); - table_size += (uint64)sizeof(uint32) - * (uint64)aot_get_imp_tbl_data_slots( - module->import_tables + i, false); - } - - for (i = 0; i != module->table_count; ++i) { - table_size += offsetof(AOTTableInstance, elems); - table_size += - (uint64)sizeof(uint32) - * (uint64)aot_get_tbl_data_slots(module->tables + i, false); - } - total_size += table_size; - - /* The offset of AOTModuleInstanceExtra, make it 8-byte aligned */ - total_size = (total_size + 7LL) & ~7LL; - extra_info_offset = (uint32)total_size; - total_size += sizeof(AOTModuleInstanceExtra); - - /* Allocate module instance, global data, table data and heap data */ - if (!(module_inst = - runtime_malloc(total_size, error_buf, error_buf_size))) { - return NULL; - } - - module_inst->module_type = Wasm_Module_AoT; - module_inst->module = (void *)module; - module_inst->e = - (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset); - - /* Initialize global info */ - p = (uint8 *)module_inst + module_inst_struct_size - + module_inst_mem_inst_size; - module_inst->global_data = p; - module_inst->global_data_size = module->global_data_size; - if (!global_instantiate(module_inst, module, error_buf, error_buf_size)) - goto fail; - - /* Initialize table info */ - p += module->global_data_size; - module_inst->table_count = module->table_count + module->import_table_count; - if (!tables_instantiate(module_inst, module, (AOTTableInstance *)p, - error_buf, error_buf_size)) - goto fail; - - /* Initialize memory space */ - if (!memories_instantiate(module_inst, module, heap_size, error_buf, - error_buf_size)) - goto fail; - - /* Initialize function pointers */ - if (!init_func_ptrs(module_inst, module, error_buf, error_buf_size)) - goto fail; - - /* Initialize function type indexes */ - if (!init_func_type_indexes(module_inst, module, error_buf, error_buf_size)) - goto fail; - - if (!check_linked_symbol(module, error_buf, error_buf_size)) - goto fail; - - if (!create_exports(module_inst, module, error_buf, error_buf_size)) - goto fail; - -#if WASM_ENABLE_LIBC_WASI != 0 - if (!is_sub_inst) { - if (!wasm_runtime_init_wasi( - (WASMModuleInstanceCommon *)module_inst, - module->wasi_args.dir_list, module->wasi_args.dir_count, - module->wasi_args.map_dir_list, module->wasi_args.map_dir_count, - module->wasi_args.env, module->wasi_args.env_count, - module->wasi_args.addr_pool, module->wasi_args.addr_count, - module->wasi_args.ns_lookup_pool, - module->wasi_args.ns_lookup_count, module->wasi_args.argv, - module->wasi_args.argc, module->wasi_args.stdio[0], - module->wasi_args.stdio[1], module->wasi_args.stdio[2], - error_buf, error_buf_size)) - goto fail; - } -#endif - -#if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) { - if (!(((AOTModuleInstanceExtra *)module_inst->e)->wasi_nn_ctx = - wasi_nn_initialize())) { - set_error_buf(error_buf, error_buf_size, - "wasi nn initialization failed"); - goto fail; - } - } -#endif - - /* Initialize the thread related data */ - if (stack_size == 0) - stack_size = DEFAULT_WASM_STACK_SIZE; -#if WASM_ENABLE_SPEC_TEST != 0 - if (stack_size < 48 * 1024) - stack_size = 48 * 1024; -#endif - module_inst->default_wasm_stack_size = stack_size; - -#if WASM_ENABLE_PERF_PROFILING != 0 - total_size = (uint64)sizeof(AOTFuncPerfProfInfo) - * (module->import_func_count + module->func_count); - if (!(module_inst->func_perf_profilings = - runtime_malloc(total_size, error_buf, error_buf_size))) { - goto fail; - } -#endif - -#if WASM_ENABLE_DUMP_CALL_STACK != 0 - if (!(module_inst->frames = - runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) { - goto fail; - } -#endif - - if (!execute_post_instantiate_functions(module_inst, is_sub_inst, - exec_env_main)) { - set_error_buf(error_buf, error_buf_size, module_inst->cur_exception); - goto fail; - } - -#if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_inst_mem_consumption( - (WASMModuleInstanceCommon *)module_inst); -#endif - - return module_inst; - -fail: - aot_deinstantiate(module_inst, is_sub_inst); - return NULL; -} - -void -aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) -{ - if (module_inst->exec_env_singleton) { - /* wasm_exec_env_destroy will call - wasm_cluster_wait_for_all_except_self to wait for other - threads, so as to destroy their exec_envs and module - instances first, and avoid accessing the shared resources - of current module instance after it is deinstantiated. */ - wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton); - } - -#if WASM_ENABLE_LIBC_WASI != 0 - /* Destroy wasi resource before freeing app heap, since some fields of - wasi contex are allocated from app heap, and if app heap is freed, - these fields will be set to NULL, we cannot free their internal data - which may allocated from global heap. */ - /* Only destroy wasi ctx in the main module instance */ - if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); -#endif - -#if WASM_ENABLE_PERF_PROFILING != 0 - if (module_inst->func_perf_profilings) - wasm_runtime_free(module_inst->func_perf_profilings); -#endif - -#if WASM_ENABLE_DUMP_CALL_STACK != 0 - if (module_inst->frames) { - bh_vector_destroy(module_inst->frames); - wasm_runtime_free(module_inst->frames); - module_inst->frames = NULL; - } -#endif - - if (module_inst->tables) - wasm_runtime_free(module_inst->tables); - - if (module_inst->memories) - memories_deinstantiate(module_inst); - - if (module_inst->export_functions) - wasm_runtime_free(module_inst->export_functions); - - if (module_inst->func_ptrs) - wasm_runtime_free(module_inst->func_ptrs); - - if (module_inst->func_type_indexes) - wasm_runtime_free(module_inst->func_type_indexes); - - if (((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports) - wasm_runtime_free( - ((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports); - -#if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) { - WASINNContext *wasi_nn_ctx = - ((AOTModuleInstanceExtra *)module_inst->e)->wasi_nn_ctx; - if (wasi_nn_ctx) - wasi_nn_destroy(wasi_nn_ctx); - } -#endif - - wasm_runtime_free(module_inst); -} - -AOTFunctionInstance * -aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, - const char *signature) -{ - uint32 i; - AOTFunctionInstance *export_funcs = - (AOTFunctionInstance *)module_inst->export_functions; - - for (i = 0; i < module_inst->export_func_count; i++) - if (!strcmp(export_funcs[i].func_name, name)) - return &export_funcs[i]; - (void)signature; - return NULL; -} - -#ifdef OS_ENABLE_HW_BOUND_CHECK - -static bool -invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, - const WASMType *func_type, - const char *signature, void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) -{ - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls(); - WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop; - uint32 page_size = os_getpagesize(); - uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT; - uint16 param_count = func_type->param_count; - uint16 result_count = func_type->result_count; - const uint8 *types = func_type->types; -#ifdef BH_PLATFORM_WINDOWS - int result; - bool has_exception; - char exception[EXCEPTION_BUF_LEN]; -#endif - bool ret; - - /* Check native stack overflow firstly to ensure we have enough - native stack to run the following codes before actually calling - the aot function in invokeNative function. */ - RECORD_STACK_USAGE(exec_env, (uint8 *)&module_inst); - if ((uint8 *)&module_inst < exec_env->native_stack_boundary - + page_size * (guard_page_count + 1)) { - aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW); - return false; - } - - if (exec_env_tls && (exec_env_tls != exec_env)) { - aot_set_exception(module_inst, "invalid exec env"); - return false; - } - - if (!os_thread_signal_inited()) { - aot_set_exception(module_inst, "thread signal env not inited"); - return false; - } - - wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node); - - wasm_runtime_set_exec_env_tls(exec_env); - if (os_setjmp(jmpbuf_node.jmpbuf) == 0) { - /* Quick call with func_ptr if the function signature is simple */ - if (!signature && param_count == 1 && types[0] == VALUE_TYPE_I32) { - if (result_count == 0) { - void (*NativeFunc)(WASMExecEnv *, uint32) = - (void (*)(WASMExecEnv *, uint32))func_ptr; - NativeFunc(exec_env, argv[0]); - ret = aot_copy_exception(module_inst, NULL) ? false : true; - } - else if (result_count == 1 - && types[param_count] == VALUE_TYPE_I32) { - uint32 (*NativeFunc)(WASMExecEnv *, uint32) = - (uint32(*)(WASMExecEnv *, uint32))func_ptr; - argv_ret[0] = NativeFunc(exec_env, argv[0]); - ret = aot_copy_exception(module_inst, NULL) ? false : true; - } - else { - ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type, - signature, attachment, argv, - argc, argv_ret); - } - } - else { - ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type, - signature, attachment, argv, argc, - argv_ret); - } -#ifdef BH_PLATFORM_WINDOWS - has_exception = aot_copy_exception(module_inst, exception); - if (has_exception && strstr(exception, "native stack overflow")) { - /* After a stack overflow, the stack was left - in a damaged state, let the CRT repair it */ - result = _resetstkoflw(); - bh_assert(result != 0); - } -#endif - } - else { - /* Exception has been set in signal handler before calling longjmp */ - ret = false; - } - - jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env); - bh_assert(&jmpbuf_node == jmpbuf_node_pop); - if (!exec_env->jmpbuf_stack_top) { - wasm_runtime_set_exec_env_tls(NULL); - } - if (!ret) { - os_sigreturn(); - os_signal_unmask(); - } - (void)jmpbuf_node_pop; - return ret; -} - -#define invoke_native_internal invoke_native_with_hw_bound_check -#else /* else of OS_ENABLE_HW_BOUND_CHECK */ -#define invoke_native_internal wasm_runtime_invoke_native -#endif /* end of OS_ENABLE_HW_BOUND_CHECK */ - -bool -aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, - unsigned argc, uint32 argv[]) -{ - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - AOTFuncType *func_type = function->u.func.func_type; - uint32 result_count = func_type->result_count; - uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0; - bool ret; - - if (argc < func_type->param_cell_num) { - char buf[108]; - snprintf(buf, sizeof(buf), - "invalid argument count %u, must be no smaller than %u", argc, - func_type->param_cell_num); - aot_set_exception(module_inst, buf); - return false; - } - argc = func_type->param_cell_num; - - /* func pointer was looked up previously */ - bh_assert(function->u.func.func_ptr != NULL); - - /* set thread handle and stack boundary */ - wasm_exec_env_set_thread_info(exec_env); - - if (ext_ret_count > 0) { - uint32 cell_num = 0, i; - uint8 *ext_ret_types = func_type->types + func_type->param_count + 1; - uint32 argv1_buf[32], *argv1 = argv1_buf, *ext_rets = NULL; - uint32 *argv_ret = argv; - uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count); - uint64 size; - - /* Allocate memory all arguments */ - size = - sizeof(uint32) * (uint64)argc /* original arguments */ - + sizeof(void *) - * (uint64)ext_ret_count /* extra result values' addr */ - + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ - if (size > sizeof(argv1_buf) - && !(argv1 = runtime_malloc(size, module_inst->cur_exception, - sizeof(module_inst->cur_exception)))) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY); - return false; - } - - /* Copy original arguments */ - bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc); - - /* Get the extra result value's address */ - ext_rets = - argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; - - /* Append each extra result value's address to original arguments */ - for (i = 0; i < ext_ret_count; i++) { - *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) = - (uintptr_t)(ext_rets + cell_num); - cell_num += wasm_value_type_cell_num(ext_ret_types[i]); - } - -#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) - if (!aot_alloc_frame(exec_env, function->func_index)) { - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - return false; - } -#endif - - ret = invoke_native_internal(exec_env, function->u.func.func_ptr, - func_type, NULL, NULL, argv1, argc, argv); - -#if WASM_ENABLE_DUMP_CALL_STACK != 0 - if (!ret) { - if (aot_create_call_stack(exec_env)) { - aot_dump_call_stack(exec_env, true, NULL, 0); - } - } -#endif - -#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) - aot_free_frame(exec_env); -#endif - if (!ret) { - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - return ret; - } - - /* Get extra result values */ - switch (func_type->types[func_type->param_count]) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: -#if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: -#endif - argv_ret++; - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - argv_ret += 2; - break; -#if WASM_ENABLE_SIMD != 0 - case VALUE_TYPE_V128: - argv_ret += 4; - break; -#endif - default: - bh_assert(0); - break; - } - ext_rets = - argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; - bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets, - sizeof(uint32) * cell_num); - - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - return true; - } - else { -#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) - if (!aot_alloc_frame(exec_env, function->func_index)) { - return false; - } -#endif - - ret = invoke_native_internal(exec_env, function->u.func.func_ptr, - func_type, NULL, NULL, argv, argc, argv); - -#if WASM_ENABLE_DUMP_CALL_STACK != 0 - if (aot_copy_exception(module_inst, NULL)) { - if (aot_create_call_stack(exec_env)) { - aot_dump_call_stack(exec_env, true, NULL, 0); - } - } -#endif - -#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) - aot_free_frame(exec_env); -#endif - - return ret && !aot_copy_exception(module_inst, NULL) ? true : false; - } -} - -void -aot_set_exception(AOTModuleInstance *module_inst, const char *exception) -{ - wasm_set_exception(module_inst, exception); -} - -void -aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id) -{ - if (id != EXCE_ALREADY_THROWN) - wasm_set_exception_with_id(module_inst, id); -#ifdef OS_ENABLE_HW_BOUND_CHECK - wasm_runtime_access_exce_check_guard_page(); -#endif -} - -const char * -aot_get_exception(AOTModuleInstance *module_inst) -{ - return wasm_get_exception(module_inst); -} - -bool -aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf) -{ - /* The field offsets of cur_exception in AOTModuleInstance and - WASMModuleInstance are the same */ - return wasm_copy_exception(module_inst, exception_buf); -} - -static bool -execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env, - AOTFunctionInstance *malloc_func, - AOTFunctionInstance *retain_func, uint32 size, - uint32 *p_result) -{ -#ifdef OS_ENABLE_HW_BOUND_CHECK - WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls(); -#endif - WASMExecEnv *exec_env_created = NULL; - WASMModuleInstanceCommon *module_inst_old = NULL; - uint32 argv[2], argc; - bool ret; - - argv[0] = size; - argc = 1; - if (retain_func) { - argv[1] = 0; - argc = 2; - } - - if (exec_env) { -#ifdef OS_ENABLE_HW_BOUND_CHECK - if (exec_env_tls) { - bh_assert(exec_env_tls == exec_env); - } -#endif - bh_assert(exec_env->module_inst - == (WASMModuleInstanceCommon *)module_inst); - } - else { - /* Try using the existing exec_env */ -#ifdef OS_ENABLE_HW_BOUND_CHECK - exec_env = exec_env_tls; -#endif -#if WASM_ENABLE_THREAD_MGR != 0 - if (!exec_env) - exec_env = wasm_clusters_search_exec_env( - (WASMModuleInstanceCommon *)module_inst); -#endif - if (!exec_env) { - if (!(exec_env = exec_env_created = wasm_exec_env_create( - (WASMModuleInstanceCommon *)module_inst, - module_inst->default_wasm_stack_size))) { - wasm_set_exception(module_inst, "allocate memory failed"); - return false; - } - } - else { - /* Temporarily replace exec_env's module inst with current - module inst to ensure that the exec_env's module inst - is the correct one. */ - module_inst_old = exec_env->module_inst; - exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst; - } - } - - ret = aot_call_function(exec_env, malloc_func, argc, argv); - - if (retain_func && ret) - ret = aot_call_function(exec_env, retain_func, 1, argv); - - if (module_inst_old) - /* Restore the existing exec_env's module inst */ - exec_env->module_inst = module_inst_old; - - if (exec_env_created) - wasm_exec_env_destroy(exec_env_created); - - if (ret) - *p_result = argv[0]; - return ret; -} - -static bool -execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env, - AOTFunctionInstance *free_func, uint32 offset) -{ -#ifdef OS_ENABLE_HW_BOUND_CHECK - WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls(); -#endif - WASMExecEnv *exec_env_created = NULL; - WASMModuleInstanceCommon *module_inst_old = NULL; - uint32 argv[2]; - bool ret; - - argv[0] = offset; - - if (exec_env) { -#ifdef OS_ENABLE_HW_BOUND_CHECK - if (exec_env_tls) { - bh_assert(exec_env_tls == exec_env); - } -#endif - bh_assert(exec_env->module_inst - == (WASMModuleInstanceCommon *)module_inst); - } - else { - /* Try using the existing exec_env */ -#ifdef OS_ENABLE_HW_BOUND_CHECK - exec_env = exec_env_tls; -#endif -#if WASM_ENABLE_THREAD_MGR != 0 - if (!exec_env) - exec_env = wasm_clusters_search_exec_env( - (WASMModuleInstanceCommon *)module_inst); -#endif - if (!exec_env) { - if (!(exec_env = exec_env_created = wasm_exec_env_create( - (WASMModuleInstanceCommon *)module_inst, - module_inst->default_wasm_stack_size))) { - wasm_set_exception(module_inst, "allocate memory failed"); - return false; - } - } - else { - /* Temporarily replace exec_env's module inst with current - module inst to ensure that the exec_env's module inst - is the correct one. */ - module_inst_old = exec_env->module_inst; - exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst; - } - } - - ret = aot_call_function(exec_env, free_func, 1, argv); - - if (module_inst_old) - /* Restore the existing exec_env's module inst */ - exec_env->module_inst = module_inst_old; - - if (exec_env_created) - wasm_exec_env_destroy(exec_env_created); - - return ret; -} - -uint32 -aot_module_malloc_internal(AOTModuleInstance *module_inst, - WASMExecEnv *exec_env, uint32 size, - void **p_native_addr) -{ - AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); - AOTModule *module = (AOTModule *)module_inst->module; - uint8 *addr = NULL; - uint32 offset = 0; - - if (!memory_inst) { - aot_set_exception(module_inst, "uninitialized memory"); - return 0; - } - - if (memory_inst->heap_handle) { - addr = mem_allocator_malloc(memory_inst->heap_handle, size); - } - else if (module->malloc_func_index != (uint32)-1 - && module->free_func_index != (uint32)-1) { - AOTFunctionInstance *malloc_func, *retain_func = NULL; - char *malloc_func_name; - char *malloc_func_sig; - - if (module->retain_func_index != (uint32)-1) { - malloc_func_name = "__new"; - malloc_func_sig = "(ii)i"; - retain_func = aot_lookup_function(module_inst, "__retain", "(i)i"); - if (!retain_func) - retain_func = aot_lookup_function(module_inst, "__pin", "(i)i"); - bh_assert(retain_func); - } - else { - malloc_func_name = "malloc"; - malloc_func_sig = "(i)i"; - } - malloc_func = - aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig); - - if (!malloc_func - || !execute_malloc_function(module_inst, exec_env, malloc_func, - retain_func, size, &offset)) { - return 0; - } - addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL; - } - - if (!addr) { - if (memory_inst->heap_handle - && mem_allocator_is_heap_corrupted(memory_inst->heap_handle)) { - wasm_runtime_show_app_heap_corrupted_prompt(); - aot_set_exception(module_inst, "app heap corrupted"); - } - else { - LOG_WARNING("warning: allocate %u bytes memory failed", size); - } - return 0; - } - if (p_native_addr) - *p_native_addr = addr; - return (uint32)(addr - memory_inst->memory_data); -} - -uint32 -aot_module_realloc_internal(AOTModuleInstance *module_inst, - WASMExecEnv *exec_env, uint32 ptr, uint32 size, - void **p_native_addr) -{ - AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); - uint8 *addr = NULL; - - if (!memory_inst) { - aot_set_exception(module_inst, "uninitialized memory"); - return 0; - } - - if (memory_inst->heap_handle) { - addr = mem_allocator_realloc( - memory_inst->heap_handle, - ptr ? memory_inst->memory_data + ptr : NULL, size); - } - - /* Only support realloc in WAMR's app heap */ - (void)exec_env; - - if (!addr) { - if (memory_inst->heap_handle - && mem_allocator_is_heap_corrupted(memory_inst->heap_handle)) { - aot_set_exception(module_inst, "app heap corrupted"); - } - else { - aot_set_exception(module_inst, "out of memory"); - } - return 0; - } - - if (p_native_addr) - *p_native_addr = addr; - return (uint32)(addr - memory_inst->memory_data); -} - -void -aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env, - uint32 ptr) -{ - AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); - AOTModule *module = (AOTModule *)module_inst->module; - - if (!memory_inst) { - return; - } - - if (ptr) { - uint8 *addr = memory_inst->memory_data + ptr; - if (memory_inst->heap_handle && memory_inst->heap_data < addr - && addr < memory_inst->heap_data_end) { - mem_allocator_free(memory_inst->heap_handle, addr); - } - else if (module->malloc_func_index != (uint32)-1 - && module->free_func_index != (uint32)-1 - && memory_inst->memory_data <= addr - && addr < memory_inst->memory_data_end) { - AOTFunctionInstance *free_func; - char *free_func_name; - - if (module->retain_func_index != (uint32)-1) { - free_func_name = "__release"; - } - else { - free_func_name = "free"; - } - free_func = - aot_lookup_function(module_inst, free_func_name, "(i)i"); - if (!free_func && module->retain_func_index != (uint32)-1) - free_func = aot_lookup_function(module_inst, "__unpin", "(i)i"); - - if (free_func) - execute_free_function(module_inst, exec_env, free_func, ptr); - } - } -} - -uint32 -aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, - void **p_native_addr) -{ - return aot_module_malloc_internal(module_inst, NULL, size, p_native_addr); -} - -uint32 -aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size, - void **p_native_addr) -{ - return aot_module_realloc_internal(module_inst, NULL, ptr, size, - p_native_addr); -} - -void -aot_module_free(AOTModuleInstance *module_inst, uint32 ptr) -{ - aot_module_free_internal(module_inst, NULL, ptr); -} - -uint32 -aot_module_dup_data(AOTModuleInstance *module_inst, const char *src, - uint32 size) -{ - char *buffer; - uint32 buffer_offset = - aot_module_malloc(module_inst, size, (void **)&buffer); - - if (buffer_offset != 0) { - buffer = wasm_runtime_addr_app_to_native( - (WASMModuleInstanceCommon *)module_inst, buffer_offset); - bh_memcpy_s(buffer, size, src, size); - } - return buffer_offset; -} - -bool -aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) -{ - return wasm_enlarge_memory(module_inst, inc_page_count); -} - -bool -aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, - uint32 *argv) -{ - AOTModuleInstance *module_inst = - (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env); - AOTModule *aot_module = (AOTModule *)module_inst->module; - AOTModuleInstanceExtra *module_inst_extra = - (AOTModuleInstanceExtra *)module_inst->e; - CApiFuncImport *c_api_func_import = - module_inst_extra->c_api_func_imports - ? module_inst_extra->c_api_func_imports + func_idx - : NULL; - uint32 *func_type_indexes = module_inst->func_type_indexes; - uint32 func_type_idx = func_type_indexes[func_idx]; - AOTFuncType *func_type = aot_module->func_types[func_type_idx]; - void **func_ptrs = module_inst->func_ptrs; - void *func_ptr = func_ptrs[func_idx]; - AOTImportFunc *import_func; - const char *signature; - void *attachment; - char buf[96]; - bool ret = false; - - bh_assert(func_idx < aot_module->import_func_count); - - import_func = aot_module->import_funcs + func_idx; - if (import_func->call_conv_wasm_c_api) - func_ptr = - c_api_func_import ? c_api_func_import->func_ptr_linked : NULL; - - if (!func_ptr) { - snprintf(buf, sizeof(buf), - "failed to call unlinked import function (%s, %s)", - import_func->module_name, import_func->func_name); - aot_set_exception(module_inst, buf); - goto fail; - } - - attachment = import_func->attachment; - if (import_func->call_conv_wasm_c_api) { - ret = wasm_runtime_invoke_c_api_native( - (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc, - argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg); - } - else if (!import_func->call_conv_raw) { - signature = import_func->signature; - ret = - wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature, - attachment, argv, argc, argv); - } - else { - signature = import_func->signature; - ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type, - signature, attachment, argv, argc, - argv); - } - -fail: -#ifdef OS_ENABLE_HW_BOUND_CHECK - if (!ret) - wasm_runtime_access_exce_check_guard_page(); -#endif - return ret; -} - -bool -aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx, - uint32 argc, uint32 *argv) -{ - AOTModuleInstance *module_inst = - (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env); - AOTModule *aot_module = (AOTModule *)module_inst->module; - uint32 *func_type_indexes = module_inst->func_type_indexes; - AOTTableInstance *tbl_inst; - AOTFuncType *func_type; - void **func_ptrs = module_inst->func_ptrs, *func_ptr; - uint32 func_type_idx, func_idx, ext_ret_count; - AOTImportFunc *import_func; - const char *signature = NULL; - void *attachment = NULL; - char buf[96]; - bool ret; - - /* this function is called from native code, so exec_env->handle and - exec_env->native_stack_boundary must have been set, we don't set - it again */ - - RECORD_STACK_USAGE(exec_env, (uint8 *)&module_inst); - if ((uint8 *)&module_inst < exec_env->native_stack_boundary) { - aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW); - goto fail; - } - - tbl_inst = module_inst->tables[tbl_idx]; - bh_assert(tbl_inst); - - if (table_elem_idx >= tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, EXCE_UNDEFINED_ELEMENT); - goto fail; - } - - func_idx = tbl_inst->elems[table_elem_idx]; - if (func_idx == NULL_REF) { - aot_set_exception_with_id(module_inst, EXCE_UNINITIALIZED_ELEMENT); - goto fail; - } - - func_type_idx = func_type_indexes[func_idx]; - func_type = aot_module->func_types[func_type_idx]; - - if (func_idx >= aot_module->import_func_count) { - /* func pointer was looked up previously */ - bh_assert(func_ptrs[func_idx] != NULL); - } - - if (!(func_ptr = func_ptrs[func_idx])) { - bh_assert(func_idx < aot_module->import_func_count); - import_func = aot_module->import_funcs + func_idx; - snprintf(buf, sizeof(buf), - "failed to call unlinked import function (%s, %s)", - import_func->module_name, import_func->func_name); - aot_set_exception(module_inst, buf); - goto fail; - } - - if (func_idx < aot_module->import_func_count) { - /* Call native function */ - import_func = aot_module->import_funcs + func_idx; - signature = import_func->signature; - if (import_func->call_conv_raw) { - attachment = import_func->attachment; - ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type, - signature, attachment, argv, - argc, argv); - if (!ret) - goto fail; - - return true; - } - } - - ext_ret_count = - func_type->result_count > 1 ? func_type->result_count - 1 : 0; - if (ext_ret_count > 0) { - uint32 argv1_buf[32], *argv1 = argv1_buf; - uint32 *ext_rets = NULL, *argv_ret = argv; - uint32 cell_num = 0, i; - uint8 *ext_ret_types = func_type->types + func_type->param_count + 1; - uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count); - uint64 size; - - /* Allocate memory all arguments */ - size = - sizeof(uint32) * (uint64)argc /* original arguments */ - + sizeof(void *) - * (uint64)ext_ret_count /* extra result values' addr */ - + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ - if (size > sizeof(argv1_buf) - && !(argv1 = runtime_malloc(size, module_inst->cur_exception, - sizeof(module_inst->cur_exception)))) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY); - goto fail; - } - - /* Copy original arguments */ - bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc); - - /* Get the extra result value's address */ - ext_rets = - argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; - - /* Append each extra result value's address to original arguments */ - for (i = 0; i < ext_ret_count; i++) { - *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) = - (uintptr_t)(ext_rets + cell_num); - cell_num += wasm_value_type_cell_num(ext_ret_types[i]); - } - - ret = invoke_native_internal(exec_env, func_ptr, func_type, signature, - attachment, argv1, argc, argv); - if (!ret) { - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - goto fail; - } - - /* Get extra result values */ - switch (func_type->types[func_type->param_count]) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: -#if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: -#endif - argv_ret++; - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - argv_ret += 2; - break; -#if WASM_ENABLE_SIMD != 0 - case VALUE_TYPE_V128: - argv_ret += 4; - break; -#endif - default: - bh_assert(0); - break; - } - ext_rets = - argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; - bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets, - sizeof(uint32) * cell_num); - - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - - return true; - } - else { - ret = invoke_native_internal(exec_env, func_ptr, func_type, signature, - attachment, argv, argc, argv); - if (!ret) - goto fail; - - return true; - } - -fail: -#ifdef OS_ENABLE_HW_BOUND_CHECK - wasm_runtime_access_exce_check_guard_page(); -#endif - return false; -} - -bool -aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str, - uint32 app_buf_addr, uint32 app_buf_size, - void **p_native_addr) -{ - bool ret; - - ret = wasm_check_app_addr_and_convert(module_inst, is_str, app_buf_addr, - app_buf_size, p_native_addr); - -#ifdef OS_ENABLE_HW_BOUND_CHECK - if (!ret) - wasm_runtime_access_exce_check_guard_page(); -#endif - - return ret; -} - -void * -aot_memmove(void *dest, const void *src, size_t n) -{ - return memmove(dest, src, n); -} - -void * -aot_memset(void *s, int c, size_t n) -{ - return memset(s, c, n); -} - -double -aot_sqrt(double x) -{ - return sqrt(x); -} - -float -aot_sqrtf(float x) -{ - return sqrtf(x); -} - -#if WASM_ENABLE_BULK_MEMORY != 0 -bool -aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset, - uint32 len, uint32 dst) -{ - AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); - AOTModule *aot_module; - uint8 *data = NULL; - uint8 *maddr; - uint64 seg_len = 0; - - aot_module = (AOTModule *)module_inst->module; - seg_len = aot_module->mem_init_data_list[seg_index]->byte_count; - data = aot_module->mem_init_data_list[seg_index]->bytes; - - if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst, - dst, len)) - return false; - - if ((uint64)offset + (uint64)len > seg_len) { - aot_set_exception(module_inst, "out of bounds memory access"); - return false; - } - - maddr = wasm_runtime_addr_app_to_native( - (WASMModuleInstanceCommon *)module_inst, dst); - - bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len); - return true; -} - -bool -aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index) -{ - AOTModule *aot_module = (AOTModule *)module_inst->module; - - aot_module->mem_init_data_list[seg_index]->byte_count = 0; - /* Currently we can't free the dropped data segment - as the mem_init_data_count is a continuous array */ - return true; -} -#endif /* WASM_ENABLE_BULK_MEMORY */ - -#if WASM_ENABLE_THREAD_MGR != 0 -bool -aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size) -{ - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - AOTModule *module = (AOTModule *)module_inst->module; - - uint32 stack_top_idx = module->aux_stack_top_global_index; - uint32 data_end = module->aux_data_end; - uint32 stack_bottom = module->aux_stack_bottom; - bool is_stack_before_data = stack_bottom < data_end ? true : false; - - /* Check the aux stack space, currently we don't allocate space in heap */ - if ((is_stack_before_data && (size > start_offset)) - || ((!is_stack_before_data) && (start_offset - data_end < size))) - return false; - - if (stack_top_idx != (uint32)-1) { - /* The aux stack top is a wasm global, - set the initial value for the global */ - uint32 global_offset = module->globals[stack_top_idx].data_offset; - uint8 *global_addr = module_inst->global_data + global_offset; - *(int32 *)global_addr = start_offset; - - /* The aux stack boundary is a constant value, - set the value to exec_env */ - exec_env->aux_stack_boundary.boundary = start_offset - size; - exec_env->aux_stack_bottom.bottom = start_offset; - return true; - } - - return false; -} - -bool -aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size) -{ - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - AOTModule *module = (AOTModule *)module_inst->module; - - /* The aux stack information is resolved in loader - and store in module */ - uint32 stack_bottom = module->aux_stack_bottom; - uint32 total_aux_stack_size = module->aux_stack_size; - - if (stack_bottom != 0 && total_aux_stack_size != 0) { - if (start_offset) - *start_offset = stack_bottom; - if (size) - *size = total_aux_stack_size; - return true; - } - return false; -} -#endif - -#if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0) -static void -const_string_node_size_cb(void *key, void *value, void *p_const_string_size) -{ - uint32 const_string_size = 0; - const_string_size += bh_hash_map_get_elem_struct_size(); - const_string_size += strlen((const char *)value) + 1; - *(uint32 *)p_const_string_size += const_string_size; -} - -void -aot_get_module_mem_consumption(const AOTModule *module, - WASMModuleMemConsumption *mem_conspn) -{ - uint32 i, size; - - memset(mem_conspn, 0, sizeof(*mem_conspn)); - - mem_conspn->module_struct_size = sizeof(AOTModule); - - mem_conspn->types_size = sizeof(AOTFuncType *) * module->func_type_count; - for (i = 0; i < module->func_type_count; i++) { - AOTFuncType *type = module->func_types[i]; - size = offsetof(AOTFuncType, types) - + sizeof(uint8) * (type->param_count + type->result_count); - mem_conspn->types_size += size; - } - - mem_conspn->imports_size = - sizeof(AOTImportMemory) * module->import_memory_count - + sizeof(AOTImportTable) * module->import_table_count - + sizeof(AOTImportGlobal) * module->import_global_count - + sizeof(AOTImportFunc) * module->import_func_count; - - /* func_ptrs and func_type_indexes */ - mem_conspn->functions_size = - (sizeof(void *) + sizeof(uint32)) * module->func_count; - - mem_conspn->tables_size = sizeof(AOTTable) * module->table_count; - - mem_conspn->memories_size = sizeof(AOTMemory) * module->memory_count; - mem_conspn->globals_size = sizeof(AOTGlobal) * module->global_count; - mem_conspn->exports_size = sizeof(AOTExport) * module->export_count; - - mem_conspn->table_segs_size = - sizeof(AOTTableInitData *) * module->table_init_data_count; - for (i = 0; i < module->table_init_data_count; i++) { - AOTTableInitData *init_data = module->table_init_data_list[i]; - size = offsetof(AOTTableInitData, func_indexes) - + sizeof(uint32) * init_data->func_index_count; - mem_conspn->table_segs_size += size; - } - - mem_conspn->data_segs_size = - sizeof(AOTMemInitData *) * module->mem_init_data_count; - for (i = 0; i < module->mem_init_data_count; i++) { - mem_conspn->data_segs_size += sizeof(AOTMemInitData); - } - - if (module->const_str_set) { - uint32 const_string_size = 0; - - mem_conspn->const_strs_size = - bh_hash_map_get_struct_size(module->const_str_set); - - bh_hash_map_traverse(module->const_str_set, const_string_node_size_cb, - (void *)&const_string_size); - mem_conspn->const_strs_size += const_string_size; - } - - /* code size + literal size + object data section size */ - mem_conspn->aot_code_size = - module->code_size + module->literal_size - + sizeof(AOTObjectDataSection) * module->data_section_count; - for (i = 0; i < module->data_section_count; i++) { - AOTObjectDataSection *obj_data = module->data_sections + i; - mem_conspn->aot_code_size += sizeof(uint8) * obj_data->size; - } - - mem_conspn->total_size += mem_conspn->module_struct_size; - mem_conspn->total_size += mem_conspn->types_size; - mem_conspn->total_size += mem_conspn->imports_size; - mem_conspn->total_size += mem_conspn->functions_size; - mem_conspn->total_size += mem_conspn->tables_size; - mem_conspn->total_size += mem_conspn->memories_size; - mem_conspn->total_size += mem_conspn->globals_size; - mem_conspn->total_size += mem_conspn->exports_size; - mem_conspn->total_size += mem_conspn->table_segs_size; - mem_conspn->total_size += mem_conspn->data_segs_size; - mem_conspn->total_size += mem_conspn->const_strs_size; - mem_conspn->total_size += mem_conspn->aot_code_size; -} - -void -aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst, - WASMModuleInstMemConsumption *mem_conspn) -{ - AOTTableInstance *tbl_inst; - uint32 i; - - memset(mem_conspn, 0, sizeof(*mem_conspn)); - - mem_conspn->module_inst_struct_size = sizeof(AOTModuleInstance); - - mem_conspn->memories_size = - sizeof(void *) * module_inst->memory_count - + sizeof(AOTMemoryInstance) * module_inst->memory_count; - for (i = 0; i < module_inst->memory_count; i++) { - AOTMemoryInstance *mem_inst = module_inst->memories[i]; - mem_conspn->memories_size += - mem_inst->num_bytes_per_page * mem_inst->cur_page_count; - mem_conspn->app_heap_size = - mem_inst->heap_data_end - mem_inst->heap_data; - /* size of app heap structure */ - mem_conspn->memories_size += mem_allocator_get_heap_struct_size(); - } - - mem_conspn->tables_size += - sizeof(AOTTableInstance *) * module_inst->table_count; - for (i = 0; i < module_inst->table_count; i++) { - tbl_inst = module_inst->tables[i]; - mem_conspn->tables_size += offsetof(AOTTableInstance, elems); - mem_conspn->tables_size += sizeof(uint32) * tbl_inst->max_size; - } - - /* func_ptrs and func_type_indexes */ - mem_conspn->functions_size = - (sizeof(void *) + sizeof(uint32)) - * (((AOTModule *)module_inst->module)->import_func_count - + ((AOTModule *)module_inst->module)->func_count); - - mem_conspn->globals_size = module_inst->global_data_size; - - mem_conspn->exports_size = - sizeof(AOTFunctionInstance) * (uint64)module_inst->export_func_count; - - mem_conspn->total_size += mem_conspn->module_inst_struct_size; - mem_conspn->total_size += mem_conspn->memories_size; - mem_conspn->total_size += mem_conspn->functions_size; - mem_conspn->total_size += mem_conspn->tables_size; - mem_conspn->total_size += mem_conspn->globals_size; - mem_conspn->total_size += mem_conspn->exports_size; -} -#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \ - || (WASM_ENABLE_MEMORY_TRACING != 0) */ - -#if WASM_ENABLE_REF_TYPES != 0 -void -aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx) -{ - AOTModule *module = (AOTModule *)module_inst->module; - AOTTableInitData *tbl_seg = module->table_init_data_list[tbl_seg_idx]; - tbl_seg->is_dropped = true; -} - -void -aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 tbl_seg_idx, uint32 length, uint32 src_offset, - uint32 dst_offset) -{ - AOTTableInstance *tbl_inst; - AOTTableInitData *tbl_seg; - const AOTModule *module = (AOTModule *)module_inst->module; - - tbl_inst = module_inst->tables[tbl_idx]; - bh_assert(tbl_inst); - - tbl_seg = module->table_init_data_list[tbl_seg_idx]; - bh_assert(tbl_seg); - - if (!length) { - return; - } - - if (length + src_offset > tbl_seg->func_index_count - || dst_offset + length > tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - if (tbl_seg->is_dropped) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - if (!wasm_elem_is_passive(tbl_seg->mode)) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems) - + dst_offset * sizeof(uint32), - (tbl_inst->cur_size - dst_offset) * sizeof(uint32), - tbl_seg->func_indexes + src_offset, length * sizeof(uint32)); -} - -void -aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx, - uint32 dst_tbl_idx, uint32 length, uint32 src_offset, - uint32 dst_offset) -{ - AOTTableInstance *src_tbl_inst, *dst_tbl_inst; - - src_tbl_inst = module_inst->tables[src_tbl_idx]; - bh_assert(src_tbl_inst); - - dst_tbl_inst = module_inst->tables[dst_tbl_idx]; - bh_assert(dst_tbl_inst); - - if ((uint64)dst_offset + length > dst_tbl_inst->cur_size - || (uint64)src_offset + length > src_tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - /* if src_offset >= dst_offset, copy from front to back */ - /* if src_offset < dst_offset, copy from back to front */ - /* merge all together */ - bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(AOTTableInstance, elems) - + dst_offset * sizeof(uint32), - (dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32), - (uint8 *)src_tbl_inst + offsetof(AOTTableInstance, elems) - + src_offset * sizeof(uint32), - length * sizeof(uint32)); -} - -void -aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length, - uint32 val, uint32 data_offset) -{ - AOTTableInstance *tbl_inst; - - tbl_inst = module_inst->tables[tbl_idx]; - bh_assert(tbl_inst); - - if (data_offset + length > tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - for (; length != 0; data_offset++, length--) { - tbl_inst->elems[data_offset] = val; - } -} - -uint32 -aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 inc_entries, uint32 init_val) -{ - uint32 entry_count, i, orig_tbl_sz; - AOTTableInstance *tbl_inst; - - tbl_inst = module_inst->tables[tbl_idx]; - if (!tbl_inst) { - return (uint32)-1; - } - - orig_tbl_sz = tbl_inst->cur_size; - - if (!inc_entries) { - return orig_tbl_sz; - } - - if (tbl_inst->cur_size > UINT32_MAX - inc_entries) { - return (uint32)-1; - } - - entry_count = tbl_inst->cur_size + inc_entries; - if (entry_count > tbl_inst->max_size) { - return (uint32)-1; - } - - /* fill in */ - for (i = 0; i < inc_entries; ++i) { - tbl_inst->elems[tbl_inst->cur_size + i] = init_val; - } - - tbl_inst->cur_size = entry_count; - return orig_tbl_sz; -} -#endif /* WASM_ENABLE_REF_TYPES != 0 */ - -#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 -static const char * -lookup_func_name(const char **func_names, uint32 *func_indexes, - uint32 func_index_count, uint32 func_index) -{ - int64 low = 0, mid; - int64 high = func_index_count - 1; - - if (!func_names || !func_indexes || func_index_count == 0) - return NULL; - - while (low <= high) { - mid = (low + high) / 2; - if (func_index == func_indexes[mid]) { - return func_names[mid]; - } - else if (func_index < func_indexes[mid]) - high = mid - 1; - else - low = mid + 1; - } - - return NULL; -} -#endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */ - -static const char * -get_func_name_from_index(const AOTModuleInstance *module_inst, - uint32 func_index) -{ - const char *func_name = NULL; - AOTModule *module = (AOTModule *)module_inst->module; - -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - if ((func_name = - lookup_func_name(module->aux_func_names, module->aux_func_indexes, - module->aux_func_name_count, func_index))) { - return func_name; - } -#endif - - if (func_index < module->import_func_count) { - func_name = module->import_funcs[func_index].func_name; - } - else { - uint32 i; - - for (i = 0; i < module->export_count; i++) { - AOTExport export = module->exports[i]; - if (export.index == func_index && export.kind == EXPORT_KIND_FUNC) { - func_name = export.name; - break; - } - } - } - - return func_name; -} - -bool -aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index) -{ - AOTFrame *frame = - wasm_exec_env_alloc_wasm_frame(exec_env, sizeof(AOTFrame)); -#if WASM_ENABLE_PERF_PROFILING != 0 - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - AOTFuncPerfProfInfo *func_perf_prof = - module_inst->func_perf_profilings + func_index; -#endif - - if (!frame) { - aot_set_exception((AOTModuleInstance *)exec_env->module_inst, - "auxiliary call stack overflow"); - return false; - } - -#if WASM_ENABLE_PERF_PROFILING != 0 - frame->time_started = os_time_get_boot_microsecond(); - frame->func_perf_prof_info = func_perf_prof; -#endif - - frame->prev_frame = (AOTFrame *)exec_env->cur_frame; - exec_env->cur_frame = (struct WASMInterpFrame *)frame; - - frame->func_index = func_index; - return true; -} - -void -aot_free_frame(WASMExecEnv *exec_env) -{ - AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame; - AOTFrame *prev_frame = cur_frame->prev_frame; - -#if WASM_ENABLE_PERF_PROFILING != 0 - cur_frame->func_perf_prof_info->total_exec_time += - os_time_get_boot_microsecond() - cur_frame->time_started; - cur_frame->func_perf_prof_info->total_exec_cnt++; -#endif - - wasm_exec_env_free_wasm_frame(exec_env, cur_frame); - exec_env->cur_frame = (struct WASMInterpFrame *)prev_frame; -} -#endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \ - || (WASM_ENABLE_PERF_PROFILING != 0) */ - -#if WASM_ENABLE_DUMP_CALL_STACK != 0 -bool -aot_create_call_stack(struct WASMExecEnv *exec_env) -{ - AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame, - *first_frame = cur_frame; - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - uint32 n = 0; - - while (cur_frame) { - cur_frame = cur_frame->prev_frame; - n++; - } - - /* release previous stack frames and create new ones */ - if (!bh_vector_destroy(module_inst->frames) - || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame), - false)) { - return false; - } - - cur_frame = first_frame; - while (cur_frame) { - WASMCApiFrame frame = { 0 }; - frame.instance = module_inst; - frame.module_offset = 0; - frame.func_index = cur_frame->func_index; - frame.func_offset = 0; - frame.func_name_wp = - get_func_name_from_index(module_inst, cur_frame->func_index); - - if (!bh_vector_append(module_inst->frames, &frame)) { - bh_vector_destroy(module_inst->frames); - return false; - } - - cur_frame = cur_frame->prev_frame; - } - - return true; -} - -#define PRINT_OR_DUMP() \ - do { \ - total_len += \ - wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \ - if ((!print) && buf && (len == 0)) { \ - return total_len; \ - } \ - } while (0) - -uint32 -aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len) -{ - AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; - uint32 n = 0, total_len = 0, total_frames; - /* reserve 256 bytes for line buffer, any line longer than 256 bytes - * will be truncated */ - char line_buf[256]; - - if (!module_inst->frames) { - return 0; - } - - total_frames = (uint32)bh_vector_size(module_inst->frames); - if (total_frames == 0) { - return 0; - } - - snprintf(line_buf, sizeof(line_buf), "\n"); - PRINT_OR_DUMP(); - - while (n < total_frames) { - WASMCApiFrame frame = { 0 }; - uint32 line_length, i; - - if (!bh_vector_get(module_inst->frames, n, &frame)) { - return 0; - } - - /* function name not exported, print number instead */ - if (frame.func_name_wp == NULL) { - line_length = snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", - n, frame.func_index); - } - else { - line_length = snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n, - frame.func_name_wp); - } - - if (line_length >= sizeof(line_buf)) { - uint32 line_buffer_len = sizeof(line_buf); - /* If line too long, ensure the last character is '\n' */ - for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) { - line_buf[i] = '.'; - } - line_buf[line_buffer_len - 2] = '\n'; - } - - PRINT_OR_DUMP(); - - n++; - } - snprintf(line_buf, sizeof(line_buf), "\n"); - PRINT_OR_DUMP(); - - return total_len + 1; -} -#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */ - -#if WASM_ENABLE_PERF_PROFILING != 0 -void -aot_dump_perf_profiling(const AOTModuleInstance *module_inst) -{ - AOTFuncPerfProfInfo *perf_prof = - (AOTFuncPerfProfInfo *)module_inst->func_perf_profilings; - AOTModule *module = (AOTModule *)module_inst->module; - uint32 total_func_count = module->import_func_count + module->func_count, i; - const char *func_name; - - os_printf("Performance profiler data:\n"); - for (i = 0; i < total_func_count; i++, perf_prof++) { - func_name = get_func_name_from_index(module_inst, i); - - if (func_name) - os_printf(" func %s, execution time: %.3f ms, execution count: %d " - "times\n", - func_name, perf_prof->total_exec_time / 1000.0f, - perf_prof->total_exec_cnt); - else - os_printf(" func %d, execution time: %.3f ms, execution count: %d " - "times\n", - i, perf_prof->total_exec_time / 1000.0f, - perf_prof->total_exec_cnt); - } -} -#endif /* end of WASM_ENABLE_PERF_PROFILING */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.h deleted file mode 100644 index bcd06534e..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/aot_runtime.h +++ /dev/null @@ -1,571 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _AOT_RUNTIME_H_ -#define _AOT_RUNTIME_H_ - -#include "bh_platform.h" -#include "../common/wasm_runtime_common.h" -#include "../interpreter/wasm_runtime.h" -#include "../compilation/aot.h" - -#if WASM_ENABLE_WASI_NN != 0 -#include "../libraries/wasi-nn/src/wasi_nn_private.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum AOTSectionType { - AOT_SECTION_TYPE_TARGET_INFO = 0, - AOT_SECTION_TYPE_INIT_DATA = 1, - AOT_SECTION_TYPE_TEXT = 2, - AOT_SECTION_TYPE_FUNCTION = 3, - AOT_SECTION_TYPE_EXPORT = 4, - AOT_SECTION_TYPE_RELOCATION = 5, - AOT_SECTION_TYPE_SIGANATURE = 6, - AOT_SECTION_TYPE_CUSTOM = 100, -} AOTSectionType; - -typedef enum AOTCustomSectionType { - AOT_CUSTOM_SECTION_RAW = 0, - AOT_CUSTOM_SECTION_NATIVE_SYMBOL = 1, - AOT_CUSTOM_SECTION_ACCESS_CONTROL = 2, - AOT_CUSTOM_SECTION_NAME = 3, -} AOTCustomSectionType; - -typedef struct AOTObjectDataSection { - char *name; - uint8 *data; - uint32 size; -} AOTObjectDataSection; - -/* Relocation info */ -typedef struct AOTRelocation { - uint64 relocation_offset; - int64 relocation_addend; - uint32 relocation_type; - char *symbol_name; - /* index in the symbol offset field */ - uint32 symbol_index; -} AOTRelocation; - -/* Relocation Group */ -typedef struct AOTRelocationGroup { - char *section_name; - /* index in the symbol offset field */ - uint32 name_index; - uint32 relocation_count; - AOTRelocation *relocations; -} AOTRelocationGroup; - -/* AOT function instance */ -typedef struct AOTFunctionInstance { - char *func_name; - uint32 func_index; - bool is_import_func; - union { - struct { - AOTFuncType *func_type; - /* function pointer linked */ - void *func_ptr; - } func; - AOTImportFunc *func_import; - } u; -} AOTFunctionInstance; - -typedef struct AOTModuleInstanceExtra { - CApiFuncImport *c_api_func_imports; -#if WASM_ENABLE_WASI_NN != 0 - WASINNContext *wasi_nn_ctx; -#endif -} AOTModuleInstanceExtra; - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) -/* clang-format off */ -typedef struct AOTUnwindInfo { - uint8 Version : 3; - uint8 Flags : 5; - uint8 SizeOfProlog; - uint8 CountOfCodes; - uint8 FrameRegister : 4; - uint8 FrameOffset : 4; - struct { - struct { - uint8 CodeOffset; - uint8 UnwindOp : 4; - uint8 OpInfo : 4; - }; - uint16 FrameOffset; - } UnwindCode[1]; -} AOTUnwindInfo; -/* clang-format on */ - -/* size of mov instruction and jmp instruction */ -#define PLT_ITEM_SIZE 12 -#endif - -typedef struct AOTModule { - uint32 module_type; - - /* import memories */ - uint32 import_memory_count; - AOTImportMemory *import_memories; - - /* memory info */ - uint32 memory_count; - AOTMemory *memories; - - /* init data */ - uint32 mem_init_data_count; - AOTMemInitData **mem_init_data_list; - - /* native symbol */ - void **native_symbol_list; - - /* import tables */ - uint32 import_table_count; - AOTImportTable *import_tables; - - /* tables */ - uint32 table_count; - AOTTable *tables; - - /* table init data info */ - uint32 table_init_data_count; - AOTTableInitData **table_init_data_list; - - /* function type info */ - uint32 func_type_count; - AOTFuncType **func_types; - - /* import global variable info */ - uint32 import_global_count; - AOTImportGlobal *import_globals; - - /* global variable info */ - uint32 global_count; - AOTGlobal *globals; - - /* total global variable size */ - uint32 global_data_size; - - /* import function info */ - uint32 import_func_count; - AOTImportFunc *import_funcs; - - /* function info */ - uint32 func_count; - /* func pointers of AOTed (un-imported) functions */ - void **func_ptrs; - /* func type indexes of AOTed (un-imported) functions */ - uint32 *func_type_indexes; - - /* export info */ - uint32 export_count; - AOTExport *exports; - - /* start function index, -1 denotes no start function */ - uint32 start_func_index; - /* start function, point to AOTed function */ - void *start_function; - - uint32 malloc_func_index; - uint32 free_func_index; - uint32 retain_func_index; - - /* AOTed code */ - void *code; - uint32 code_size; - - /* literal for AOTed code */ - uint8 *literal; - uint32 literal_size; - -#if defined(BH_PLATFORM_WINDOWS) - /* extra plt data area for __ymm, __xmm and __real constants - in Windows platform */ - uint8 *extra_plt_data; - uint32 extra_plt_data_size; - uint32 ymm_plt_count; - uint32 xmm_plt_count; - uint32 real_plt_count; - uint32 float_plt_count; -#endif - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - /* dynamic function table to be added by RtlAddFunctionTable(), - used to unwind the call stack and register exception handler - for AOT functions */ - RUNTIME_FUNCTION *rtl_func_table; - bool rtl_func_table_registered; -#endif - - /* data sections in AOT object file, including .data, .rodata - and .rodata.cstN. */ - AOTObjectDataSection *data_sections; - uint32 data_section_count; - - /* constant string set */ - HashMap *const_str_set; - - /* the index of auxiliary __data_end global, - -1 means unexported */ - uint32 aux_data_end_global_index; - /* auxiliary __data_end exported by wasm app */ - uint32 aux_data_end; - - /* the index of auxiliary __heap_base global, - -1 means unexported */ - uint32 aux_heap_base_global_index; - /* auxiliary __heap_base exported by wasm app */ - uint32 aux_heap_base; - - /* the index of auxiliary stack top global, - -1 means unexported */ - uint32 aux_stack_top_global_index; - /* auxiliary stack bottom resolved */ - uint32 aux_stack_bottom; - /* auxiliary stack size resolved */ - uint32 aux_stack_size; - - /* is indirect mode or not */ - bool is_indirect_mode; - -#if WASM_ENABLE_LIBC_WASI != 0 - WASIArguments wasi_args; - bool import_wasi_api; -#endif -#if WASM_ENABLE_DEBUG_AOT != 0 - void *elf_hdr; - uint32 elf_size; -#endif -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - const char **aux_func_names; - uint32 *aux_func_indexes; - uint32 aux_func_name_count; -#endif -#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 - WASMCustomSection *custom_section_list; -#endif -} AOTModule; - -#define AOTMemoryInstance WASMMemoryInstance -#define AOTTableInstance WASMTableInstance -#define AOTModuleInstance WASMModuleInstance - -/* Target info, read from ELF header of object file */ -typedef struct AOTTargetInfo { - /* Binary type, elf32l/elf32b/elf64l/elf64b */ - uint16 bin_type; - /* ABI type */ - uint16 abi_type; - /* Object file type */ - uint16 e_type; - /* Architecture */ - uint16 e_machine; - /* Object file version */ - uint32 e_version; - /* Processor-specific flags */ - uint32 e_flags; - /* Reserved */ - uint32 reserved; - /* Arch name */ - char arch[16]; -} AOTTargetInfo; - -typedef struct AOTFuncPerfProfInfo { - /* total execution time */ - uint64 total_exec_time; - /* total execution count */ - uint32 total_exec_cnt; -} AOTFuncPerfProfInfo; - -/* AOT auxiliary call stack */ -typedef struct AOTFrame { - struct AOTFrame *prev_frame; - uint32 func_index; -#if WASM_ENABLE_PERF_PROFILING != 0 - uint64 time_started; - AOTFuncPerfProfInfo *func_perf_prof_info; -#endif -} AOTFrame; - -/** - * Load a AOT module from aot file buffer - * @param buf the byte buffer which contains the AOT file data - * @param size the size of the buffer - * @param error_buf output of the error info - * @param error_buf_size the size of the error string - * - * @return return AOT module loaded, NULL if failed - */ -AOTModule * -aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf, - uint32 error_buf_size); - -/** - * Load a AOT module from a specified AOT section list. - * - * @param section_list the section list which contains each section data - * @param error_buf output of the error info - * @param error_buf_size the size of the error string - * - * @return return AOT module loaded, NULL if failed - */ -AOTModule * -aot_load_from_sections(AOTSection *section_list, char *error_buf, - uint32 error_buf_size); - -/** - * Unload a AOT module. - * - * @param module the module to be unloaded - */ -void -aot_unload(AOTModule *module); - -/** - * Instantiate a AOT module. - * - * @param module the AOT module to instantiate - * @param is_sub_inst the flag of sub instance - * @param heap_size the default heap size of the module instance, a heap will - * be created besides the app memory space. Both wasm app and native - * function can allocate memory from the heap. If heap_size is 0, the - * default heap size will be used. - * @param error_buf buffer to output the error info if failed - * @param error_buf_size the size of the error buffer - * - * @return return the instantiated AOT module instance, NULL if failed - */ -AOTModuleInstance * -aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main, - uint32 stack_size, uint32 heap_size, char *error_buf, - uint32 error_buf_size); - -/** - * Deinstantiate a AOT module instance, destroy the resources. - * - * @param module_inst the AOT module instance to destroy - * @param is_sub_inst the flag of sub instance - */ -void -aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst); - -/** - * Lookup an exported function in the AOT module instance. - * - * @param module_inst the module instance - * @param name the name of the function - * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64" - * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32" - * - * @return the function instance found - */ -AOTFunctionInstance * -aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, - const char *signature); -/** - * Call the given AOT function of a AOT module instance with - * arguments. - * - * @param exec_env the execution environment - * @param function the function to be called - * @param argc the number of arguments - * @param argv the arguments. If the function method has return value, - * the first (or first two in case 64-bit return value) element of - * argv stores the return value of the called AOT function after this - * function returns. - * - * @return true if success, false otherwise and exception will be thrown, - * the caller can call aot_get_exception to get exception info. - */ -bool -aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, - unsigned argc, uint32 argv[]); - -/** - * Set AOT module instance exception with exception string - * - * @param module the AOT module instance - * - * @param exception current exception string - */ -void -aot_set_exception(AOTModuleInstance *module_inst, const char *exception); - -void -aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id); - -/** - * Get exception info of the AOT module instance. - * - * @param module_inst the AOT module instance - * - * @return the exception string - */ -const char * -aot_get_exception(AOTModuleInstance *module_inst); - -/** - * @brief Copy exception in buffer passed as parameter. Thread-safe version of - * `aot_get_exception()` - * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN - * @return true if exception found, false otherwise - */ -bool -aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf); - -uint32 -aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env, - uint32 size, void **p_native_addr); - -uint32 -aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env, - uint32 ptr, uint32 size, void **p_native_addr); - -void -aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env, - uint32 ptr); - -uint32 -aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, - void **p_native_addr); - -uint32 -aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size, - void **p_native_addr); - -void -aot_module_free(AOTModuleInstance *module_inst, uint32 ptr); - -uint32 -aot_module_dup_data(AOTModuleInstance *module_inst, const char *src, - uint32 size); - -bool -aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count); - -/** - * Invoke native function from aot code - */ -bool -aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, - uint32 *argv); - -bool -aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx, - uint32 argc, uint32 *argv); - -/** - * Check whether the app address and the buf is inside the linear memory, - * and convert the app address into native address - */ -bool -aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str, - uint32 app_buf_addr, uint32 app_buf_size, - void **p_native_addr); - -uint32 -aot_get_plt_table_size(); - -void * -aot_memmove(void *dest, const void *src, size_t n); - -void * -aot_memset(void *s, int c, size_t n); - -double -aot_sqrt(double x); - -float -aot_sqrtf(float x); - -#if WASM_ENABLE_BULK_MEMORY != 0 -bool -aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset, - uint32 len, uint32 dst); - -bool -aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index); -#endif - -#if WASM_ENABLE_THREAD_MGR != 0 -bool -aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size); - -bool -aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size); -#endif - -void -aot_get_module_mem_consumption(const AOTModule *module, - WASMModuleMemConsumption *mem_conspn); - -void -aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst, - WASMModuleInstMemConsumption *mem_conspn); - -#if WASM_ENABLE_REF_TYPES != 0 -void -aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx); - -void -aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 tbl_seg_idx, uint32 length, uint32 src_offset, - uint32 dst_offset); - -void -aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx, - uint32 dst_tbl_idx, uint32 length, uint32 src_offset, - uint32 dst_offset); - -void -aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length, - uint32 val, uint32 data_offset); - -uint32 -aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 inc_entries, uint32 init_val); -#endif - -bool -aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index); - -void -aot_free_frame(WASMExecEnv *exec_env); - -bool -aot_create_call_stack(struct WASMExecEnv *exec_env); - -/** - * @brief Dump wasm call stack or get the size - * - * @param exec_env the execution environment - * @param print whether to print to stdout or not - * @param buf buffer to store the dumped content - * @param len length of the buffer - * - * @return when print is true, return the bytes printed out to stdout; when - * print is false and buf is NULL, return the size required to store the - * callstack content; when print is false and buf is not NULL, return the size - * dumped to the buffer, 0 means error and data in buf may be invalid - */ -uint32 -aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len); - -void -aot_dump_perf_profiling(const AOTModuleInstance *module_inst); - -const uint8 * -aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len); - -#ifdef __cplusplus -} /* end of extern "C" */ -#endif - -#endif /* end of _AOT_RUNTIME_H_ */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_aarch64.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_aarch64.c deleted file mode 100644 index 4c46eaa00..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_aarch64.c +++ /dev/null @@ -1,405 +0,0 @@ -/* - * Copyright (C) 2020 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_AARCH64_MOVW_UABS_G0 263 -#define R_AARCH64_MOVW_UABS_G0_NC 264 -#define R_AARCH64_MOVW_UABS_G1 265 -#define R_AARCH64_MOVW_UABS_G1_NC 266 -#define R_AARCH64_MOVW_UABS_G2 267 -#define R_AARCH64_MOVW_UABS_G2_NC 268 -#define R_AARCH64_MOVW_UABS_G3 269 - -#define R_AARCH64_MOVW_SABS_G0 270 -#define R_AARCH64_MOVW_SABS_G1 271 -#define R_AARCH64_MOVW_SABS_G2 272 - -#define R_AARCH64_ADR_PREL_LO19 273 -#define R_AARCH64_ADR_PREL_LO21 274 -#define R_AARCH64_ADR_PREL_PG_HI21 275 -#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 - -#define R_AARCH64_ADD_ABS_LO12_NC 277 - -#define R_AARCH64_LDST8_ABS_LO12_NC 278 -#define R_AARCH64_LDST16_ABS_LO12_NC 284 -#define R_AARCH64_LDST32_ABS_LO12_NC 285 -#define R_AARCH64_LDST64_ABS_LO12_NC 286 -#define R_AARCH64_LDST128_ABS_LO12_NC 299 - -#define R_AARCH64_JUMP26 282 -#define R_AARCH64_CALL26 283 - -/* clang-format off */ -static SymbolMap target_sym_map[] = { - REG_COMMON_SYMBOLS -}; -/* clang-format on */ - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -#define BUILD_TARGET_AARCH64_DEFAULT "aarch64v8" -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - const char *s = BUILD_TARGET; - size_t s_size = sizeof(BUILD_TARGET); - char *d = target_buf; - - /* Set to "aarch64v8" by default if sub version isn't specified */ - if (strcmp(s, "AARCH64") == 0) { - s = BUILD_TARGET_AARCH64_DEFAULT; - s_size = sizeof(BUILD_TARGET_AARCH64_DEFAULT); - } - if (target_buf_size < s_size) { - s_size = target_buf_size; - } - while (--s_size) { - if (*s >= 'A' && *s <= 'Z') - *d++ = *s++ + 'a' - 'A'; - else - *d++ = *s++; - } - /* Ensure the string is null byte ('\0') terminated */ - *d = '\0'; -} -#undef BUILD_TARGET_AARCH64_DEFAULT - -static uint32 -get_plt_item_size() -{ - /* 6*4 bytes instructions and 8 bytes symbol address */ - return 32; -} - -void -init_plt_table(uint8 *plt) -{ - uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); - for (i = 0; i < num; i++) { - uint32 *p = (uint32 *)plt; - *p++ = 0xf81f0ffe; /* str x30, [sp, #-16]! */ - *p++ = 0x100000be; /* adr x30, #20; symbol addr is PC + 5 instructions - below */ - *p++ = 0xf94003de; /* ldr x30, [x30] */ - *p++ = 0xd63f03c0; /* blr x30 */ - *p++ = 0xf84107fe; /* ldr x30, [sp], #16 */ - *p++ = 0xd61f03c0; /* br x30 */ - /* symbol addr */ - *(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; - p += 2; - plt += get_plt_item_size(); - } -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -#define SIGN_EXTEND_TO_INT64(val, bits, val_ext) \ - do { \ - int64 m = (int64)((uint64)1 << (bits - 1)); \ - val_ext = ((int64)val ^ m) - m; \ - } while (0) - -#define Page(expr) ((expr) & ~0xFFF) - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_AARCH64_CALL26: - case R_AARCH64_JUMP26: - { - void *S, *P = (void *)(target_section_addr + reloc_offset); - int64 X, A, initial_addend; - int32 insn, imm26; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - insn = *(int32 *)P; - imm26 = insn & 0x3FFFFFF; - SIGN_EXTEND_TO_INT64(imm26 << 2, 28, initial_addend); - A = initial_addend; - A += (int64)reloc_addend; - - if (symbol_index < 0) { - /* Symbol address itself is an AOT function. - * Apply relocation with the symbol directly. - * Suppose the symbol address is in +-128MB relative - * to the relocation address. - */ - S = symbol_addr; - } - else { - uint8 *plt; - if (reloc_addend > 0) { - set_error_buf( - error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; - } - /* Symbol address is not an AOT function, - * but a function of runtime or native. Its address is - * beyond of the +-128MB space. Apply relocation with - * the PLT which branch to the target symbol address. - */ - S = plt = (uint8 *)module->code + module->code_size - - get_plt_table_size() - + get_plt_item_size() * symbol_index; - } - - /* S + A - P */ - X = (int64)S + A - (int64)P; - - /* Check overflow: +-128MB */ - if (X > (128 * BH_MB) || X < (-128 * BH_MB)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "target address out of range."); - return false; - } - - /* write the imm26 back to instruction */ - *(int32 *)P = (insn & 0xFC000000) | ((int32)((X >> 2) & 0x3FFFFFF)); - break; - } - - case R_AARCH64_MOVW_UABS_G0: - case R_AARCH64_MOVW_UABS_G0_NC: - case R_AARCH64_MOVW_UABS_G1: - case R_AARCH64_MOVW_UABS_G1_NC: - case R_AARCH64_MOVW_UABS_G2: - case R_AARCH64_MOVW_UABS_G2_NC: - case R_AARCH64_MOVW_UABS_G3: - { - void *S = symbol_addr, - *P = (void *)(target_section_addr + reloc_offset); - int64 X, A, initial_addend; - int32 insn, imm16; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - insn = *(int32 *)P; - imm16 = (insn >> 5) & 0xFFFF; - - SIGN_EXTEND_TO_INT64(imm16, 16, initial_addend); - A = initial_addend; - A += (int64)reloc_addend; - - /* S + A */ - X = (int64)S + A; - - /* No need to check overflow for this relocation type */ - switch (reloc_type) { - case R_AARCH64_MOVW_UABS_G0: - if (X < 0 || X >= (1LL << 16)) - goto overflow_check_fail; - break; - case R_AARCH64_MOVW_UABS_G1: - if (X < 0 || X >= (1LL << 32)) - goto overflow_check_fail; - break; - case R_AARCH64_MOVW_UABS_G2: - if (X < 0 || X >= (1LL << 48)) - goto overflow_check_fail; - break; - default: - break; - } - - /* write the imm16 back to bits[5:20] of instruction */ - switch (reloc_type) { - case R_AARCH64_MOVW_UABS_G0: - case R_AARCH64_MOVW_UABS_G0_NC: - *(int32 *)P = - (insn & 0xFFE0001F) | ((int32)((X & 0xFFFF) << 5)); - break; - case R_AARCH64_MOVW_UABS_G1: - case R_AARCH64_MOVW_UABS_G1_NC: - *(int32 *)P = (insn & 0xFFE0001F) - | ((int32)(((X >> 16) & 0xFFFF) << 5)); - break; - case R_AARCH64_MOVW_UABS_G2: - case R_AARCH64_MOVW_UABS_G2_NC: - *(int32 *)P = (insn & 0xFFE0001F) - | ((int32)(((X >> 32) & 0xFFFF) << 5)); - break; - case R_AARCH64_MOVW_UABS_G3: - *(int32 *)P = (insn & 0xFFE0001F) - | ((int32)(((X >> 48) & 0xFFFF) << 5)); - break; - default: - bh_assert(0); - break; - } - break; - } - - case R_AARCH64_ADR_PREL_PG_HI21: - case R_AARCH64_ADR_PREL_PG_HI21_NC: - { - void *S = symbol_addr, - *P = (void *)(target_section_addr + reloc_offset); - int64 X, A, initial_addend; - int32 insn, immhi19, immlo2, imm21; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - insn = *(int32 *)P; - immhi19 = (insn >> 5) & 0x7FFFF; - immlo2 = (insn >> 29) & 0x3; - imm21 = (immhi19 << 2) | immlo2; - - SIGN_EXTEND_TO_INT64(imm21 << 12, 33, initial_addend); - A = initial_addend; - A += (int64)reloc_addend; - - /* Page(S+A) - Page(P) */ - X = Page((int64)S + A) - Page((int64)P); - - /* Check overflow: +-4GB */ - if (reloc_type == R_AARCH64_ADR_PREL_PG_HI21 - && (X > ((int64)4 * BH_GB) || X < ((int64)-4 * BH_GB))) - goto overflow_check_fail; - - /* write the imm21 back to instruction */ - immhi19 = (int32)(((X >> 12) >> 2) & 0x7FFFF); - immlo2 = (int32)((X >> 12) & 0x3); - *(int32 *)P = (insn & 0x9F00001F) | (immlo2 << 29) | (immhi19 << 5); - - break; - } - - case R_AARCH64_ADD_ABS_LO12_NC: - { - void *S = symbol_addr, - *P = (void *)(target_section_addr + reloc_offset); - int64 X, A, initial_addend; - int32 insn, imm12; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - insn = *(int32 *)P; - imm12 = (insn >> 10) & 0xFFF; - - SIGN_EXTEND_TO_INT64(imm12, 12, initial_addend); - A = initial_addend; - A += (int64)reloc_addend; - - /* S + A */ - X = (int64)S + A; - - /* No need to check overflow for this relocation type */ - - /* write the imm12 back to instruction */ - *(int32 *)P = (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); - break; - } - - case R_AARCH64_LDST8_ABS_LO12_NC: - case R_AARCH64_LDST16_ABS_LO12_NC: - case R_AARCH64_LDST32_ABS_LO12_NC: - case R_AARCH64_LDST64_ABS_LO12_NC: - case R_AARCH64_LDST128_ABS_LO12_NC: - { - void *S = symbol_addr, - *P = (void *)(target_section_addr + reloc_offset); - int64 X, A, initial_addend; - int32 insn, imm12; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - insn = *(int32 *)P; - imm12 = (insn >> 10) & 0xFFF; - - SIGN_EXTEND_TO_INT64(imm12, 12, initial_addend); - A = initial_addend; - A += (int64)reloc_addend; - - /* S + A */ - X = (int64)S + A; - - /* No need to check overflow for this relocation type */ - - /* write the imm12 back to instruction */ - switch (reloc_type) { - case R_AARCH64_LDST8_ABS_LO12_NC: - *(int32 *)P = - (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); - break; - case R_AARCH64_LDST16_ABS_LO12_NC: - *(int32 *)P = (insn & 0xFFC003FF) - | ((int32)(((X & 0xFFF) >> 1) << 10)); - break; - case R_AARCH64_LDST32_ABS_LO12_NC: - *(int32 *)P = (insn & 0xFFC003FF) - | ((int32)(((X & 0xFFF) >> 2) << 10)); - break; - case R_AARCH64_LDST64_ABS_LO12_NC: - *(int32 *)P = (insn & 0xFFC003FF) - | ((int32)(((X & 0xFFF) >> 3) << 10)); - break; - case R_AARCH64_LDST128_ABS_LO12_NC: - *(int32 *)P = (insn & 0xFFC003FF) - | ((int32)(((X & 0xFFF) >> 4) << 10)); - break; - default: - bh_assert(0); - break; - } - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - - return true; - -overflow_check_fail: - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "target address out of range."); - return false; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arc.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arc.c deleted file mode 100644 index 332962998..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arc.c +++ /dev/null @@ -1,419 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_ARC_S21H_PCREL 14 -#define R_ARC_S21W_PCREL 15 -#define R_ARC_S25H_PCREL 16 -#define R_ARC_S25W_PCREL 17 -#define R_ARC_32 4 -#define R_ARC_32_ME 27 - -/* clang-format off */ -#ifndef __CCAC__ -void __st_r13_to_r15(); -void __st_r13_to_r16(); -void __st_r13_to_r17(); -void __st_r13_to_r18(); -void __st_r13_to_r19(); -void __st_r13_to_r20(); -void __st_r13_to_r21(); -void __st_r13_to_r22(); -void __st_r13_to_r23(); -void __st_r13_to_r24(); -void __st_r13_to_r25(); -void __ld_r13_to_r15(); -void __ld_r13_to_r16(); -void __ld_r13_to_r17(); -void __ld_r13_to_r18(); -void __ld_r13_to_r19(); -void __ld_r13_to_r20(); -void __ld_r13_to_r21(); -void __ld_r13_to_r22(); -void __ld_r13_to_r23(); -void __ld_r13_to_r24(); -void __ld_r13_to_r25(); -void __adddf3(); -void __addsf3(); -void __divdf3(); -void __divdi3(); -void __divsf3(); -void __divsi3(); -void __extendsfdf2(); -void __fixdfsi(); -void __floatsidf(); -void __floatsisf(); -void __muldf3(); -void __mulsf3(); -void __subdf3(); -void __subsf3(); -void __truncdfsf2(); -void __floatunsisf(); -void __fixunsdfsi(); -void __floatdisf(); -void __floatdidf(); -void __fixdfdi(); -void __ltsf2(); -void __gesf2(); -void __eqdf2(); -void __nedf2(); -void __ltsf2(); -void __nesf2(); -void __unordsf2(); -void __fixunssfsi(); -#else -void __ac_push_13_to_13(); -void __ac_push_13_to_14(); -void __ac_push_13_to_15(); -void __ac_push_13_to_16(); -void __ac_push_13_to_17(); -void __ac_push_13_to_18(); -void __ac_push_13_to_19(); -void __ac_push_13_to_20(); -void __ac_push_13_to_21(); -void __ac_push_13_to_22(); -void __ac_push_13_to_23(); -void __ac_push_13_to_24(); -void __ac_push_13_to_25(); -void __ac_push_13_to_26(); -void __ac_push_none(); -void __ac_pop_13_to_26(); -void __ac_pop_13_to_26v(); -void __ac_pop_13_to_25(); -void __ac_pop_13_to_25v(); -void __ac_pop_13_to_24(); -void __ac_pop_13_to_24v(); -void __ac_pop_13_to_23(); -void __ac_pop_13_to_23v(); -void __ac_pop_13_to_22(); -void __ac_pop_13_to_22v(); -void __ac_pop_13_to_21(); -void __ac_pop_13_to_21v(); -void __ac_pop_13_to_20(); -void __ac_pop_13_to_20v(); -void __ac_pop_13_to_19(); -void __ac_pop_13_to_19v(); -void __ac_pop_13_to_18(); -void __ac_pop_13_to_18v(); -void __ac_pop_13_to_17(); -void __ac_pop_13_to_17v(); -void __ac_pop_13_to_16(); -void __ac_pop_13_to_16v(); -void __ac_pop_13_to_15(); -void __ac_pop_13_to_15v(); -void __ac_pop_13_to_14(); -void __ac_pop_13_to_14v(); -void __ac_pop_13_to_13(); -void __ac_pop_13_to_13v(); -void __ac_pop_none(); -void __ac_pop_nonev(); -void __eqdf2(); -void __nedf2(); -void __ltsf2(); -void __nesf2(); -void __gesf2(); -void __gtsf2(); -void __unordsf2(); -void __truncdfhf2(); -void __truncsfhf2(); -#endif /* end of __CCAC__ */ - -void __ledf2(); -void __ltdf2(); -void __gedf2(); -void __gtdf2(); -void __eqsf2(); -void __lesf2(); -void __unorddf2(); -/* clang-format on */ - -static SymbolMap target_sym_map[] = { - /* clang-format off */ - REG_COMMON_SYMBOLS -#ifndef __CCAC__ - REG_SYM(__st_r13_to_r15), - REG_SYM(__st_r13_to_r16), - REG_SYM(__st_r13_to_r17), - REG_SYM(__st_r13_to_r18), - REG_SYM(__st_r13_to_r19), - REG_SYM(__st_r13_to_r20), - REG_SYM(__st_r13_to_r21), - REG_SYM(__st_r13_to_r22), - REG_SYM(__st_r13_to_r23), - REG_SYM(__st_r13_to_r24), - REG_SYM(__st_r13_to_r25), - REG_SYM(__ld_r13_to_r15), - REG_SYM(__ld_r13_to_r16), - REG_SYM(__ld_r13_to_r17), - REG_SYM(__ld_r13_to_r18), - REG_SYM(__ld_r13_to_r19), - REG_SYM(__ld_r13_to_r20), - REG_SYM(__ld_r13_to_r21), - REG_SYM(__ld_r13_to_r22), - REG_SYM(__ld_r13_to_r23), - REG_SYM(__ld_r13_to_r24), - REG_SYM(__ld_r13_to_r25), - REG_SYM(__adddf3), - REG_SYM(__addsf3), - REG_SYM(__divdf3), - REG_SYM(__divdi3), - REG_SYM(__divsf3), - REG_SYM(__divsi3), - REG_SYM(__extendsfdf2), - REG_SYM(__fixdfsi), - REG_SYM(__floatsidf), - REG_SYM(__floatsisf), - REG_SYM(__muldf3), - REG_SYM(__mulsf3), - REG_SYM(__subdf3), - REG_SYM(__subsf3), - REG_SYM(__truncdfsf2), - REG_SYM(__floatunsisf), - REG_SYM(__fixunsdfsi), - REG_SYM(__floatdisf), - REG_SYM(__floatdidf), - REG_SYM(__fixdfdi), - REG_SYM(__ltsf2), - REG_SYM(__gesf2), - REG_SYM(__eqdf2), - REG_SYM(__nedf2), - REG_SYM(__ltsf2), - REG_SYM(__nesf2), - REG_SYM(__unordsf2), - REG_SYM(__fixunssfsi), -#else - REG_SYM(__ac_push_13_to_13), - REG_SYM(__ac_push_13_to_14), - REG_SYM(__ac_push_13_to_15), - REG_SYM(__ac_push_13_to_16), - REG_SYM(__ac_push_13_to_17), - REG_SYM(__ac_push_13_to_18), - REG_SYM(__ac_push_13_to_19), - REG_SYM(__ac_push_13_to_20), - REG_SYM(__ac_push_13_to_21), - REG_SYM(__ac_push_13_to_22), - REG_SYM(__ac_push_13_to_23), - REG_SYM(__ac_push_13_to_24), - REG_SYM(__ac_push_13_to_25), - REG_SYM(__ac_push_13_to_26), - REG_SYM(__ac_push_none), - REG_SYM(__ac_pop_13_to_26), - REG_SYM(__ac_pop_13_to_26v), - REG_SYM(__ac_pop_13_to_25), - REG_SYM(__ac_pop_13_to_25v), - REG_SYM(__ac_pop_13_to_24), - REG_SYM(__ac_pop_13_to_24v), - REG_SYM(__ac_pop_13_to_23), - REG_SYM(__ac_pop_13_to_23v), - REG_SYM(__ac_pop_13_to_22), - REG_SYM(__ac_pop_13_to_22v), - REG_SYM(__ac_pop_13_to_21), - REG_SYM(__ac_pop_13_to_21v), - REG_SYM(__ac_pop_13_to_20), - REG_SYM(__ac_pop_13_to_20v), - REG_SYM(__ac_pop_13_to_19), - REG_SYM(__ac_pop_13_to_19v), - REG_SYM(__ac_pop_13_to_18), - REG_SYM(__ac_pop_13_to_18v), - REG_SYM(__ac_pop_13_to_17), - REG_SYM(__ac_pop_13_to_17v), - REG_SYM(__ac_pop_13_to_16), - REG_SYM(__ac_pop_13_to_16v), - REG_SYM(__ac_pop_13_to_15), - REG_SYM(__ac_pop_13_to_15v), - REG_SYM(__ac_pop_13_to_14), - REG_SYM(__ac_pop_13_to_14v), - REG_SYM(__ac_pop_13_to_13), - REG_SYM(__ac_pop_13_to_13v), - REG_SYM(__ac_pop_none), - REG_SYM(__ac_pop_nonev), - REG_SYM(__eqdf2), - REG_SYM(__nedf2), - REG_SYM(__ltsf2), - REG_SYM(__nesf2), - REG_SYM(__gesf2), - REG_SYM(__gtsf2), - REG_SYM(__unordsf2), - REG_SYM(__truncdfhf2), - REG_SYM(__truncsfhf2), -#endif /* end of __CCAC__ */ - - REG_SYM(__ledf2), - REG_SYM(__ltdf2), - REG_SYM(__gedf2), - REG_SYM(__gtdf2), - REG_SYM(__eqsf2), - REG_SYM(__lesf2), - REG_SYM(__unorddf2), - /* clang-format on */ -}; - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "arc"); -} - -uint32 -get_plt_table_size() -{ - return 0; -} - -void -init_plt_table(uint8 *plt) -{ - (void)plt; -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -static uint32 -middle_endian_convert(uint32 insn) -{ - return ((insn & 0xFFFF0000) >> 16) | ((insn & 0x0000FFFF) << 16); -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_ARC_S25H_PCREL: - { - uint32 insn = LOAD_I32(target_section_addr + reloc_offset); - int32 addend, value; - uintptr_t S, P; - intptr_t A; - - CHECK_RELOC_OFFSET(sizeof(void *)); - - /* Convert from middle endian */ - insn = middle_endian_convert(insn); - - addend = ((insn << 28) >> 28) << 10; - /* Extract the next 10 bits from Position 6 to 15 in insn */ - addend |= ((insn << 16) >> 22); - addend = addend << 10; - /* Extract the remaining 10 bits from Position 17 to 26 in insn */ - addend |= ((insn << 5) >> 22); - /* Fill in 1 bits to get the 25 bit Offset Value */ - addend = addend << 1; - - /* (S + A) - P */ - S = (uintptr_t)(uint8 *)symbol_addr; - A = (intptr_t)reloc_addend; - P = (uintptr_t)(target_section_addr + reloc_offset); - P &= (uintptr_t)~1; - value = (int32)(S + A + addend - P); - - insn = insn & 0xf8010030; - insn |= ((((value >> 1) & 0x3ff) << 17) - | (((value >> 1) & 0xffc00) >> 3) - | (((value >> 1) & 0xf00000) >> 19)); - - /* Convert to middle endian */ - insn = middle_endian_convert(insn); - - STORE_U32(target_section_addr + reloc_offset, insn); - break; - } - case R_ARC_S25W_PCREL: - { - uint32 insn = LOAD_I32(target_section_addr + reloc_offset); - int32 addend, value; - uintptr_t S, P; - intptr_t A; - - CHECK_RELOC_OFFSET(sizeof(void *)); - - /* Convert from middle endian */ - insn = middle_endian_convert(insn); - - addend = ((insn << 28) >> 28) << 10; - /* Extract the next 10 bits from Position 6 to 15 in insn */ - addend |= ((insn << 16) >> 22); - addend = addend << 9; - /* Extract the remaining 9 bits from Position 18 to 26 in insn */ - addend |= ((insn << 5) >> 23); - /* Fill in 2 bits to get the 25 bit Offset Value */ - addend = addend << 2; - - /* (S + A) - P */ - S = (uintptr_t)(uint8 *)symbol_addr; - A = (intptr_t)reloc_addend; - P = (uintptr_t)(target_section_addr + reloc_offset); - P &= (uintptr_t)~3; - value = (int32)(S + A + addend - P); - - insn = insn & 0xf8030030; - insn |= ((((value >> 2) & 0x1ff) << 18) - | (((value >> 2) & 0x7fe00) >> 3) - | (((value >> 2) & 0x780000) >> 19)); - - /* Convert to middle endian */ - insn = middle_endian_convert(insn); - - STORE_U32(target_section_addr + reloc_offset, insn); - break; - } - case R_ARC_32: - case R_ARC_32_ME: - { - uint32 insn; - - CHECK_RELOC_OFFSET(sizeof(void *)); - - /* (S + A) */ - insn = (uint32)((uintptr_t)symbol_addr + (intptr_t)reloc_addend); - - if (reloc_type == R_ARC_32_ME) - /* Convert to middle endian */ - insn = middle_endian_convert(insn); - - STORE_U32(target_section_addr + reloc_offset, insn); - break; - } - default: - { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - } - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arm.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arm.c deleted file mode 100644 index fbf9be13c..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_arm.c +++ /dev/null @@ -1,376 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_ARM_CALL 28 /* PC relative 24 bit (BL, BLX). */ -#define R_ARM_JMP24 29 /* PC relative 24 bit (B/BL<cond>). */ -#define R_ARM_ABS32 2 /* Direct 32 bit */ -#define R_ARM_MOVW_ABS_NC 43 -#define R_ARM_MOVT_ABS 44 - -/* clang-format off */ -void __adddf3(); -void __addsf3(); -void __aeabi_d2iz(); -void __aeabi_d2lz(); -void __aeabi_d2ulz(); -void __aeabi_dadd(); -void __aeabi_dcmpge(); -void __aeabi_dcmple(); -void __aeabi_dcmplt(); -void __aeabi_dcmpun(); -void __aeabi_ddiv(); -void __aeabi_f2d(); -void __aeabi_f2iz(); -void __aeabi_f2lz(); -void __aeabi_f2ulz(); -void __aeabi_fcmpge(); -void __aeabi_fcmple(); -void __aeabi_fcmplt(); -void __aeabi_fcmpun(); -void __aeabi_i2d(); -void __aeabi_idiv(); -void __aeabi_idivmod(); -void __aeabi_l2d(); -void __aeabi_l2f(); -void __aeabi_ldivmod(); -void __aeabi_memcpy(); -void __aeabi_memmove(); -void __aeabi_memset(); -void __aeabi_uidiv(); -void __aeabi_uidivmod(); -void __aeabi_ul2d(); -void __aeabi_ul2f(); -void __aeabi_uldivmod(); -void __clzsi2(); -void __divdf3(); -void __divdi3(); -void __divsf3(); -void __divsi3(); -void __eqdf2(); -void __extendsfdf2(); -void __fixdfdi(); -void __fixdfsi(); -void __fixsfdi(); -void __fixsfsi(); -void __fixunsdfdi(); -void __fixunsdfsi(); -void __fixunssfdi(); -void __floatdidf(); -void __floatdisf(); -void __floatsidf(); -void __floatsisf(); -void __floatundidf(); -void __floatundisf(); -void __floatunsidf(); -void __floatunsisf(); -void __gedf2(); -void __gesf2(); -void __gtdf2(); -void __gtsf2(); -void __ledf2(); -void __lesf2(); -void __ltdf2(); -void __ltsf2(); -void __moddi3(); -void __modsi3(); -void __muldf3(); -void __mulsf3(); -void __nedf2(); -void __nesf2(); -void __subdf3(); -void __subsf3(); -void __truncdfsf2(); -void __udivdi3(); -void __udivmoddi4(); -void __udivsi3(); -void __umoddi3(); -void __umodsi3(); -void __unorddf2(); -void __unordsf2(); -/* clang-format on */ - -static SymbolMap target_sym_map[] = { - /* clang-format off */ - REG_COMMON_SYMBOLS - /* compiler-rt symbols that come from compiler(e.g. gcc) */ - REG_SYM(__adddf3), - REG_SYM(__addsf3), - /* clang-format on */ - REG_SYM(__aeabi_d2iz), - REG_SYM(__aeabi_d2lz), - REG_SYM(__aeabi_d2ulz), - REG_SYM(__aeabi_dadd), - REG_SYM(__aeabi_dcmpge), - REG_SYM(__aeabi_dcmple), - REG_SYM(__aeabi_dcmplt), - REG_SYM(__aeabi_dcmpun), - REG_SYM(__aeabi_ddiv), - REG_SYM(__aeabi_f2d), - REG_SYM(__aeabi_f2iz), - REG_SYM(__aeabi_f2lz), - REG_SYM(__aeabi_f2ulz), - REG_SYM(__aeabi_fcmpge), - REG_SYM(__aeabi_fcmple), - REG_SYM(__aeabi_fcmplt), - REG_SYM(__aeabi_fcmpun), - REG_SYM(__aeabi_i2d), - REG_SYM(__aeabi_idiv), - REG_SYM(__aeabi_idivmod), - REG_SYM(__aeabi_l2d), - REG_SYM(__aeabi_l2f), - REG_SYM(__aeabi_ldivmod), - REG_SYM(__aeabi_memcpy), - REG_SYM(__aeabi_memmove), - REG_SYM(__aeabi_memset), - REG_SYM(__aeabi_uidiv), - REG_SYM(__aeabi_uidivmod), - REG_SYM(__aeabi_ul2d), - REG_SYM(__aeabi_ul2f), - REG_SYM(__aeabi_uldivmod), - REG_SYM(__clzsi2), - REG_SYM(__divdf3), - REG_SYM(__divdi3), - REG_SYM(__divsf3), - REG_SYM(__divsi3), - REG_SYM(__eqdf2), - REG_SYM(__extendsfdf2), - REG_SYM(__fixdfdi), - REG_SYM(__fixdfsi), - REG_SYM(__fixsfdi), - REG_SYM(__fixsfsi), - REG_SYM(__fixunsdfdi), - REG_SYM(__fixunsdfsi), - REG_SYM(__fixunssfdi), - REG_SYM(__floatdidf), - REG_SYM(__floatdisf), - REG_SYM(__floatsidf), - REG_SYM(__floatsisf), - REG_SYM(__floatundidf), - REG_SYM(__floatundisf), - REG_SYM(__floatunsidf), - REG_SYM(__floatunsisf), - REG_SYM(__gedf2), - REG_SYM(__gesf2), - REG_SYM(__gtdf2), - REG_SYM(__gtsf2), - REG_SYM(__ledf2), - REG_SYM(__lesf2), - REG_SYM(__ltdf2), - REG_SYM(__ltsf2), - REG_SYM(__moddi3), - REG_SYM(__modsi3), - REG_SYM(__muldf3), - REG_SYM(__muldf3), - REG_SYM(__mulsf3), - REG_SYM(__nedf2), - REG_SYM(__nesf2), - REG_SYM(__subdf3), - REG_SYM(__subsf3), - REG_SYM(__truncdfsf2), - REG_SYM(__udivdi3), - REG_SYM(__udivmoddi4), - REG_SYM(__udivsi3), - REG_SYM(__umoddi3), - REG_SYM(__umodsi3), - REG_SYM(__unorddf2), - REG_SYM(__unordsf2), -}; - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -#define BUILD_TARGET_ARM_DEFAULT "armv4" -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - const char *s = BUILD_TARGET; - size_t s_size = sizeof(BUILD_TARGET); - char *d = target_buf; - - /* Set to "armv4" by default if sub version isn't specified */ - if (strcmp(s, "ARM") == 0) { - s = BUILD_TARGET_ARM_DEFAULT; - s_size = sizeof(BUILD_TARGET_ARM_DEFAULT); - } - if (target_buf_size < s_size) { - s_size = target_buf_size; - } - while (--s_size) { - if (*s >= 'A' && *s <= 'Z') - *d++ = *s++ + 'a' - 'A'; - else - *d++ = *s++; - } - /* Ensure the string is null byte ('\0') terminated */ - *d = '\0'; -} -#undef BUILD_TARGET_ARM_DEFAULT - -uint32 -get_plt_item_size() -{ - /* 8 bytes instructions and 4 bytes symbol address */ - return 12; -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -void -init_plt_table(uint8 *plt) -{ - uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); - for (i = 0; i < num; i++) { - uint32 *p = (uint32 *)plt; - /* ldr pc, [pc] */ - *p++ = 0xe59ff000; - /* nop */ - *p++ = 0xe1a00000; - /* symbol addr */ - *p++ = (uint32)(uintptr_t)target_sym_map[i].symbol_addr; - plt += get_plt_item_size(); - } -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_ARM_CALL: - case R_ARM_JMP24: - { - intptr_t result; - int32 RESULT_MASK = 0x03FFFFFE; - int32 insn = *(int32 *)(target_section_addr + reloc_offset); - /* Initial addend: sign_extend(insn[23:0] << 2) */ - int32 initial_addend = - ((insn & 0xFFFFFF) << 2) | ((insn & 0x800000) ? 0xFC000000 : 0); - - CHECK_RELOC_OFFSET(sizeof(int32)); - - if (symbol_index < 0) { - /* Symbol address itself is an AOT function. - * Apply relocation with the symbol directly. - * Suppose the symbol address is in +-32MB relative - * to the relocation address. - */ - /* operation: ((S + A) | T) - P where S is symbol address and T - * is 0 */ - result = - (intptr_t)((uintptr_t)symbol_addr + (intptr_t)reloc_addend - - (uintptr_t)(target_section_addr - + reloc_offset)); - } - else { - if (reloc_addend > 0) { - set_error_buf( - error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; - } - - /* Symbol address is not an AOT function, - * but a function of runtime or native. Its address is - * beyond of the +-32MB space. Apply relocation with - * the PLT which branch to the target symbol address. - */ - /* operation: ((S + A) | T) - P where S is PLT address and T is - * 0 */ - uint8 *plt = (uint8 *)module->code + module->code_size - - get_plt_table_size() - + get_plt_item_size() * symbol_index; - result = (intptr_t)((uintptr_t)plt + (intptr_t)reloc_addend - - (uintptr_t)(target_section_addr - + reloc_offset)); - } - - result += initial_addend; - - /* Check overflow: +-32MB */ - if (result > (32 * BH_MB) || result < (-32 * BH_MB)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "target address out of range."); - return false; - } - - *(int32 *)(target_section_addr + reloc_offset) = - (int32)((insn & 0xff000000) - | (((int32)result & RESULT_MASK) >> 2)); - break; - } - case R_ARM_ABS32: - { - intptr_t initial_addend; - /* (S + A) | T where T is 0 */ - CHECK_RELOC_OFFSET(sizeof(void *)); - initial_addend = - *(intptr_t *)(target_section_addr + (uint32)reloc_offset); - *(uintptr_t *)(target_section_addr + reloc_offset) = - (uintptr_t)symbol_addr + initial_addend - + (intptr_t)reloc_addend; - break; - } - case R_ARM_MOVW_ABS_NC: - case R_ARM_MOVT_ABS: - { - uintptr_t *loc; - uintptr_t addr; - CHECK_RELOC_OFFSET(sizeof(void *)); - loc = (uintptr_t *)(target_section_addr + (uint32)reloc_offset); - addr = (uintptr_t)symbol_addr + (intptr_t)reloc_addend; - if (reloc_type == R_ARM_MOVT_ABS) { - addr >>= 16; - } - *loc = ((*loc) & 0xfff0f000) | ((addr << 4) & 0x000f0000) - | (addr & 0x00000fff); - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_mips.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_mips.c deleted file mode 100644 index f9f06a053..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_mips.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_MIPS_32 2 /* Direct 32 bit */ -#define R_MIPS_26 4 /* Direct 26 bit shifted */ - -/* clang-format off */ -static SymbolMap target_sym_map[] = { - REG_COMMON_SYMBOLS -}; -/* clang-format on */ - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "mips"); -} - -static uint32 -get_plt_item_size() -{ - return 0; -} - -void -init_plt_table(uint8 *plt) -{ - (void)plt; -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - /* TODO: implement relocation for mips */ - case R_MIPS_26: - case R_MIPS_32: - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_riscv.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_riscv.c deleted file mode 100644 index 75fee56fd..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_riscv.c +++ /dev/null @@ -1,440 +0,0 @@ -/* - * Copyright (C) 2021 XiaoMi Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_RISCV_32 1 -#define R_RISCV_64 2 -#define R_RISCV_CALL 18 -#define R_RISCV_CALL_PLT 19 -#define R_RISCV_HI20 26 -#define R_RISCV_LO12_I 27 -#define R_RISCV_LO12_S 28 - -#define RV_OPCODE_SW 0x23 - -/* clang-format off */ -void __adddf3(); -void __addsf3(); -void __divdi3(); -void __divsi3(); -void __divdf3(); -void __divsf3(); -void __eqsf2(); -void __eqdf2(); -void __extendsfdf2(); -void __fixdfdi(); -void __fixdfsi(); -void __fixsfdi(); -void __fixsfsi(); -void __fixunsdfdi(); -void __fixunsdfsi(); -void __fixunssfdi(); -void __fixunssfsi(); -void __floatdidf(); -void __floatdisf(); -void __floatsisf(); -void __floatsidf(); -void __floatundidf(); -void __floatundisf(); -void __floatunsisf(); -void __floatunsidf(); -void __gedf2(); -void __gesf2(); -void __gtdf2(); -void __gtsf2(); -void __ledf2(); -void __lesf2(); -void __ltdf2(); -void __ltsf2(); -void __moddi3(); -void __modsi3(); -void __muldf3(); -void __muldi3(); -void __mulsf3(); -void __mulsi3(); -void __nedf2(); -void __nesf2(); -void __subdf3(); -void __subsf3(); -void __truncdfsf2(); -void __udivdi3(); -void __udivsi3(); -void __umoddi3(); -void __umodsi3(); -void __unorddf2(); -void __unordsf2(); -/* clang-format on */ - -static SymbolMap target_sym_map[] = { - /* clang-format off */ - REG_COMMON_SYMBOLS -#ifndef __riscv_flen - REG_SYM(__adddf3), - REG_SYM(__addsf3), - REG_SYM(__divdf3), - REG_SYM(__divsf3), - REG_SYM(__gedf2), - REG_SYM(__gesf2), - REG_SYM(__gtdf2), - REG_SYM(__gtsf2), - REG_SYM(__ledf2), - REG_SYM(__lesf2), - REG_SYM(__ltdf2), - REG_SYM(__ltsf2), - REG_SYM(__muldf3), - REG_SYM(__nedf2), - REG_SYM(__nesf2), - REG_SYM(__eqsf2), - REG_SYM(__eqdf2), - REG_SYM(__extendsfdf2), - REG_SYM(__fixunsdfdi), - REG_SYM(__fixunsdfsi), - REG_SYM(__fixunssfsi), - REG_SYM(__subdf3), - REG_SYM(__subsf3), - REG_SYM(__truncdfsf2), - REG_SYM(__unorddf2), - REG_SYM(__unordsf2), -#endif - REG_SYM(__divdi3), - REG_SYM(__divsi3), -#if __riscv_xlen == 32 - REG_SYM(__fixdfdi), - REG_SYM(__fixdfsi), - REG_SYM(__fixsfdi), - REG_SYM(__fixsfsi), -#endif - REG_SYM(__fixunssfdi), -#if __riscv_xlen == 32 - REG_SYM(__floatdidf), - REG_SYM(__floatdisf), - REG_SYM(__floatsisf), - REG_SYM(__floatsidf), - REG_SYM(__floatundidf), - REG_SYM(__floatundisf), - REG_SYM(__floatunsisf), - REG_SYM(__floatunsidf), -#endif - REG_SYM(__moddi3), - REG_SYM(__modsi3), - REG_SYM(__muldi3), -#if __riscv_xlen == 32 - REG_SYM(__mulsf3), - REG_SYM(__mulsi3), -#endif - REG_SYM(__udivdi3), - REG_SYM(__udivsi3), - REG_SYM(__umoddi3), - REG_SYM(__umodsi3), - /* clang-format on */ -}; - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "riscv"); -} - -uint32 -get_plt_item_size() -{ -#if __riscv_xlen == 64 - /* auipc + ld + jalr + nop + addr */ - return 20; -#else - return 0; -#endif -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -/* Get a val from given address */ -static uint32 -rv_get_val(uint16 *addr) -{ - uint32 ret; - ret = *addr | (*(addr + 1)) << 16; - return ret; -} - -/* Set a val to given address */ -static void -rv_set_val(uint16 *addr, uint32 val) -{ - *addr = (val & 0xffff); - *(addr + 1) = (val >> 16); - - __asm__ volatile("fence.i"); -} - -/* Add a val to given address */ -static void -rv_add_val(uint16 *addr, uint32 val) -{ - uint32 cur = rv_get_val(addr); - rv_set_val(addr, cur + val); -} - -/** - * Get imm_hi and imm_lo from given integer - * - * @param imm given integer, signed 32bit - * @param imm_hi signed 20bit - * @param imm_lo signed 12bit - * - */ -static void -rv_calc_imm(int32 imm, int32 *imm_hi, int32 *imm_lo) -{ - int32 lo; - int32 hi = imm / 4096; - int32 r = imm % 4096; - - if (2047 < r) { - hi++; - } - else if (r < -2048) { - hi--; - } - - lo = imm - (hi * 4096); - - *imm_lo = lo; - *imm_hi = hi; -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -void -init_plt_table(uint8 *plt) -{ -#if __riscv_xlen == 64 - uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); - uint8 *p; - - for (i = 0; i < num; i++) { - p = plt; - /* auipc t1, 0 */ - *(uint16 *)p = 0x0317; - p += 2; - *(uint16 *)p = 0x0000; - p += 2; - /* ld t1, 8(t1) */ - *(uint16 *)p = 0x3303; - p += 2; - *(uint16 *)p = 0x00C3; - p += 2; - /* jr t1 */ - *(uint16 *)p = 0x8302; - p += 2; - /* nop */ - *(uint16 *)p = 0x0001; - p += 2; - bh_memcpy_s(p, 8, &target_sym_map[i].symbol_addr, 8); - p += 8; - plt += get_plt_item_size(); - } -#endif -} - -typedef struct RelocTypeStrMap { - uint32 reloc_type; - char *reloc_str; -} RelocTypeStrMap; - -#define RELOC_TYPE_MAP(reloc_type) \ - { \ - reloc_type, #reloc_type \ - } - -static RelocTypeStrMap reloc_type_str_maps[] = { - RELOC_TYPE_MAP(R_RISCV_32), RELOC_TYPE_MAP(R_RISCV_CALL), - RELOC_TYPE_MAP(R_RISCV_CALL_PLT), RELOC_TYPE_MAP(R_RISCV_HI20), - RELOC_TYPE_MAP(R_RISCV_LO12_I), RELOC_TYPE_MAP(R_RISCV_LO12_S), -}; - -static const char * -reloc_type_to_str(uint32 reloc_type) -{ - uint32 i; - - for (i = 0; i < sizeof(reloc_type_str_maps) / sizeof(RelocTypeStrMap); - i++) { - if (reloc_type_str_maps[i].reloc_type == reloc_type) - return reloc_type_str_maps[i].reloc_str; - } - - return "Unknown_Reloc_Type"; -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - int32 val, imm_hi, imm_lo, insn; - uint8 *addr = target_section_addr + reloc_offset; - char buf[128]; - - switch (reloc_type) { - case R_RISCV_32: - { - uint32 val_32 = - (uint32)((uintptr_t)symbol_addr + (intptr_t)reloc_addend); - - CHECK_RELOC_OFFSET(sizeof(uint32)); - if (val_32 != ((uintptr_t)symbol_addr + (intptr_t)reloc_addend)) { - goto fail_addr_out_of_range; - } - - rv_set_val((uint16 *)addr, val_32); - break; - } - case R_RISCV_64: - { - uint64 val_64 = - (uint64)((uintptr_t)symbol_addr + (intptr_t)reloc_addend); - CHECK_RELOC_OFFSET(sizeof(uint64)); - bh_memcpy_s(addr, 8, &val_64, 8); - break; - } - case R_RISCV_CALL: - case R_RISCV_CALL_PLT: - { - val = (int32)(intptr_t)((uint8 *)symbol_addr - addr); - - CHECK_RELOC_OFFSET(sizeof(uint32)); - if (val != (intptr_t)((uint8 *)symbol_addr - addr)) { - if (symbol_index >= 0) { - /* Call runtime function by plt code */ - symbol_addr = (uint8 *)module->code + module->code_size - - get_plt_table_size() - + get_plt_item_size() * symbol_index; - val = (int32)(intptr_t)((uint8 *)symbol_addr - addr); - } - } - - if (val != (intptr_t)((uint8 *)symbol_addr - addr)) { - goto fail_addr_out_of_range; - } - - rv_calc_imm(val, &imm_hi, &imm_lo); - - rv_add_val((uint16 *)addr, (imm_hi << 12)); - if ((rv_get_val((uint16 *)(addr + 4)) & 0x7f) == RV_OPCODE_SW) { - /* Adjust imm for SW : S-type */ - val = (((int32)imm_lo >> 5) << 25) - + (((int32)imm_lo & 0x1f) << 7); - - rv_add_val((uint16 *)(addr + 4), val); - } - else { - /* Adjust imm for MV(ADDI)/JALR : I-type */ - rv_add_val((uint16 *)(addr + 4), ((int32)imm_lo << 20)); - } - break; - } - - case R_RISCV_HI20: - { - val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend); - - CHECK_RELOC_OFFSET(sizeof(uint32)); - if (val != ((intptr_t)symbol_addr + (intptr_t)reloc_addend)) { - goto fail_addr_out_of_range; - } - - addr = target_section_addr + reloc_offset; - insn = rv_get_val((uint16 *)addr); - rv_calc_imm(val, &imm_hi, &imm_lo); - insn = (insn & 0x00000fff) | (imm_hi << 12); - rv_set_val((uint16 *)addr, insn); - break; - } - - case R_RISCV_LO12_I: - { - val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend); - - CHECK_RELOC_OFFSET(sizeof(uint32)); - if (val != (intptr_t)symbol_addr + (intptr_t)reloc_addend) { - goto fail_addr_out_of_range; - } - - addr = target_section_addr + reloc_offset; - insn = rv_get_val((uint16 *)addr); - rv_calc_imm(val, &imm_hi, &imm_lo); - insn = (insn & 0x000fffff) | (imm_lo << 20); - rv_set_val((uint16 *)addr, insn); - break; - } - - case R_RISCV_LO12_S: - { - val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend); - - CHECK_RELOC_OFFSET(sizeof(uint32)); - if (val != ((intptr_t)symbol_addr + (intptr_t)reloc_addend)) { - goto fail_addr_out_of_range; - } - - addr = target_section_addr + reloc_offset; - rv_calc_imm(val, &imm_hi, &imm_lo); - val = (((int32)imm_lo >> 5) << 25) + (((int32)imm_lo & 0x1f) << 7); - rv_add_val((uint16 *)addr, val); - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %" PRIu32 ".", - reloc_type); - return false; - } - - return true; - -fail_addr_out_of_range: - snprintf(buf, sizeof(buf), - "AOT module load failed: " - "relocation truncated to fit %s failed.", - reloc_type_to_str(reloc_type)); - set_error_buf(error_buf, error_buf_size, buf); - return false; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_thumb.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_thumb.c deleted file mode 100644 index 26614863b..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_thumb.c +++ /dev/null @@ -1,431 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_ARM_ABS32 2 /* Direct 32 bit */ -#define R_ARM_THM_CALL 10 /* PC relative (Thumb BL and ARMv5 Thumb BLX). */ -#define R_ARM_THM_JMP24 30 /* B.W */ -#define R_ARM_THM_MOVW_ABS_NC 47 -#define R_ARM_THM_MOVT_ABS 48 -#define R_ARM_THM_MOVW_PREL_NC 49 -#define R_ARM_THM_MOVT_PREL 50 - -/* clang-format off */ -void __adddf3(); -void __addsf3(); -void __aeabi_d2iz(); -void __aeabi_d2lz(); -void __aeabi_d2uiz(); -void __aeabi_d2ulz(); -void __aeabi_dadd(); -void __aeabi_dcmpge(); -void __aeabi_dcmpgt(); -void __aeabi_dcmple(); -void __aeabi_dcmplt(); -void __aeabi_dcmpun(); -void __aeabi_ddiv(); -void __aeabi_f2d(); -void __aeabi_f2iz(); -void __aeabi_f2lz(); -void __aeabi_f2ulz(); -void __aeabi_fcmpge(); -void __aeabi_fcmple(); -void __aeabi_fcmplt(); -void __aeabi_fcmpun(); -void __aeabi_i2d(); -void __aeabi_idiv(); -void __aeabi_idivmod(); -void __aeabi_l2d(); -void __aeabi_l2f(); -void __aeabi_ldivmod(); -void __aeabi_ui2d(); -void __aeabi_uidiv(); -void __aeabi_uidivmod(); -void __aeabi_ul2d(); -void __aeabi_ul2f(); -void __aeabi_uldivmod(); -void __ashldi3(); -void __clzsi2(); -void __divdf3(); -void __divdi3(); -void __divsi3(); -void __eqdf2(); -void __eqsf2(); -void __extendsfdf2(); -void __fixdfdi(); -void __fixdfsi(); -void __fixsfdi(); -void __fixunsdfdi(); -void __fixunsdfsi(); -void __fixunssfdi(); -void __floatdidf(); -void __floatdisf(); -void __floatsidf(); -void __floatsisf(); -void __floatundidf(); -void __floatundisf(); -void __floatunsidf(); -void __floatunsisf(); -void __gedf2(); -void __gesf2(); -void __gtdf2(); -void __gtsf2(); -void __ledf2(); -void __lesf2(); -void __lshrdi3(); -void __ltdf2(); -void __ltsf2(); -void __moddi3(); -void __modsi3(); -void __muldf3(); -void __muldi3(); -void __mulsf3(); -void __nedf2(); -void __nesf2(); -void __subdf3(); -void __subsf3(); -void __truncdfsf2(); -void __udivdi3(); -void __udivmoddi4(); -void __udivsi3(); -void __umoddi3(); -void __umodsi3(); -void __unorddf2(); -void __unordsf2(); -/* clang-format on */ - -static SymbolMap target_sym_map[] = { - /* clang-format off */ - REG_COMMON_SYMBOLS - /* compiler-rt symbols that come from compiler(e.g. gcc) */ -#if __ARM_ARCH != 6 - REG_SYM(__adddf3), - REG_SYM(__addsf3), - REG_SYM(__divdf3), - REG_SYM(__extendsfdf2), - REG_SYM(__fixdfsi), - REG_SYM(__floatsidf), - REG_SYM(__floatsisf), - REG_SYM(__floatunsidf), - REG_SYM(__floatunsisf), - REG_SYM(__muldf3), - REG_SYM(__mulsf3), - REG_SYM(__subdf3), - REG_SYM(__subsf3), - REG_SYM(__truncdfsf2), - REG_SYM(__unorddf2), - REG_SYM(__unordsf2), -#endif - /* clang-format on */ - REG_SYM(__aeabi_d2iz), - REG_SYM(__aeabi_d2lz), - REG_SYM(__aeabi_d2uiz), - REG_SYM(__aeabi_d2ulz), - REG_SYM(__aeabi_dadd), - REG_SYM(__aeabi_dcmpge), - REG_SYM(__aeabi_dcmpgt), - REG_SYM(__aeabi_dcmple), - REG_SYM(__aeabi_dcmplt), - REG_SYM(__aeabi_dcmpun), - REG_SYM(__aeabi_ddiv), - REG_SYM(__aeabi_f2d), - REG_SYM(__aeabi_f2iz), - REG_SYM(__aeabi_f2lz), - REG_SYM(__aeabi_f2ulz), - REG_SYM(__aeabi_fcmpge), - REG_SYM(__aeabi_fcmple), - REG_SYM(__aeabi_fcmplt), - REG_SYM(__aeabi_fcmpun), - REG_SYM(__aeabi_i2d), - REG_SYM(__aeabi_idiv), - REG_SYM(__aeabi_idivmod), - REG_SYM(__aeabi_l2d), - REG_SYM(__aeabi_l2f), - REG_SYM(__aeabi_ldivmod), - REG_SYM(__aeabi_ui2d), - REG_SYM(__aeabi_uidiv), - REG_SYM(__aeabi_uidivmod), - REG_SYM(__aeabi_ul2d), - REG_SYM(__aeabi_ul2f), - REG_SYM(__aeabi_uldivmod), - REG_SYM(__ashldi3), - REG_SYM(__clzsi2), - REG_SYM(__divdi3), - REG_SYM(__divsi3), - REG_SYM(__eqdf2), - REG_SYM(__eqsf2), - REG_SYM(__fixdfdi), - REG_SYM(__fixsfdi), - REG_SYM(__fixunsdfdi), - REG_SYM(__fixunsdfsi), - REG_SYM(__fixunssfdi), - REG_SYM(__floatdidf), - REG_SYM(__floatdisf), - REG_SYM(__floatundidf), - REG_SYM(__floatundisf), - REG_SYM(__gedf2), - REG_SYM(__gesf2), - REG_SYM(__gtdf2), - REG_SYM(__gtsf2), - REG_SYM(__ledf2), - REG_SYM(__lesf2), - REG_SYM(__lshrdi3), - REG_SYM(__ltdf2), - REG_SYM(__ltsf2), - REG_SYM(__moddi3), - REG_SYM(__modsi3), - REG_SYM(__muldi3), - REG_SYM(__nedf2), - REG_SYM(__nesf2), - REG_SYM(__udivdi3), - REG_SYM(__udivmoddi4), - REG_SYM(__udivsi3), - REG_SYM(__umoddi3), - REG_SYM(__umodsi3), -}; - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -#define BUILD_TARGET_THUMB_V4T "thumbv4t" -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - const char *s = BUILD_TARGET; - size_t s_size = sizeof(BUILD_TARGET); - char *d = target_buf; - - /* Set to "thumbv4t" by default if sub version isn't specified */ - if (strcmp(s, "THUMB") == 0) { - s = BUILD_TARGET_THUMB_V4T; - s_size = sizeof(BUILD_TARGET_THUMB_V4T); - } - if (target_buf_size < s_size) { - s_size = target_buf_size; - } - while (--s_size) { - if (*s >= 'A' && *s <= 'Z') - *d++ = *s++ + 'a' - 'A'; - else - *d++ = *s++; - } - /* Ensure the string is null byte ('\0') terminated */ - *d = '\0'; -} -#undef BUILD_TARGET_THUMB_V4T - -uint32 -get_plt_item_size() -{ - /* 16 bytes instructions and 4 bytes symbol address */ - return 20; -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -void -init_plt_table(uint8 *plt) -{ - uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); - for (i = 0; i < num; i++) { - uint16 *p = (uint16 *)plt; - /* nop */ - *p++ = 0xbf00; - /* push {r4} */ - *p++ = 0xb410; - /* add r4, pc, #8 */ - *p++ = 0xa402; - /* ldr r4, [r4, #0] */ - *p++ = 0x6824; - /* mov ip, r4 */ - *p++ = 0x46a4; - /* pop {r4} */ - *p++ = 0xbc10; - /* mov pc, ip */ - *p++ = 0x46e7; - /* nop */ - *p++ = 0xbf00; - /* symbol addr */ - *(uint32 *)p = (uint32)(uintptr_t)target_sym_map[i].symbol_addr; - plt += get_plt_item_size(); - } -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_ARM_THM_CALL: - case R_ARM_THM_JMP24: - { - int32 RESULT_MASK = 0x01FFFFFE; - int32 result, result_masked; - int16 *reloc_addr; - int32 initial_addend_0, initial_addend_1, initial_addend; - bool sign; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - reloc_addr = (int16 *)(target_section_addr + reloc_offset); - initial_addend_0 = (*reloc_addr) & 0x7FF; - initial_addend_1 = (*(reloc_addr + 1)) & 0x7FF; - sign = (initial_addend_0 & 0x400) ? true : false; - initial_addend = (initial_addend_0 << 12) | (initial_addend_1 << 1) - | (sign ? 0xFF800000 : 0); - - if (symbol_index < 0) { - /* Symbol address itself is an AOT function. - * Apply relocation with the symbol directly. - * Suppose the symbol address is in +-4MB relative - * to the relocation address. - */ - /* operation: ((S + A) | T) - P where S is symbol address - and T is 1 */ - result = - (int32)(((intptr_t)((uintptr_t)symbol_addr - + (intptr_t)reloc_addend) - | 1) - - (intptr_t)(target_section_addr + reloc_offset)); - } - else { - if (reloc_addend > 0) { - set_error_buf( - error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; - } - - /* Symbol address is not an AOT function, - * but a function of runtime or native. Its address is - * beyond of the +-4MB space. Apply relocation with - * the PLT which branch to the target symbol address. - */ - /* operation: ((S + A) | T) - P where S is PLT address - and T is 1 */ - uint8 *plt = (uint8 *)module->code + module->code_size - - get_plt_table_size() - + get_plt_item_size() * symbol_index + 1; - result = - (int32)(((intptr_t)plt | 1) - - (intptr_t)(target_section_addr + reloc_offset)); - } - - result += initial_addend; - - /* Check overflow: +-4MB */ - if (result > (4 * BH_MB) || result < (-4 * BH_MB)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "target address out of range."); - return false; - } - - result_masked = (int32)result & RESULT_MASK; - initial_addend_0 = (result_masked >> 12) & 0x7FF; - initial_addend_1 = (result_masked >> 1) & 0x7FF; - - *reloc_addr = (*reloc_addr & ~0x7FF) | initial_addend_0; - *(reloc_addr + 1) = (*(reloc_addr + 1) & ~0x7FF) | initial_addend_1; - break; - } - case R_ARM_ABS32: - { - intptr_t initial_addend; - /* (S + A) | T where T is 0 */ - CHECK_RELOC_OFFSET(sizeof(void *)); - initial_addend = - *(intptr_t *)(target_section_addr + (uint32)reloc_offset); - *(uintptr_t *)(target_section_addr + reloc_offset) = - (uintptr_t)symbol_addr + initial_addend - + (intptr_t)reloc_addend; - break; - } - case R_ARM_THM_MOVW_ABS_NC: - case R_ARM_THM_MOVT_ABS: - case R_ARM_THM_MOVW_PREL_NC: - case R_ARM_THM_MOVT_PREL: - { - uint16 upper = *(uint16 *)(target_section_addr + reloc_offset); - uint16 lower = *(uint16 *)(target_section_addr + reloc_offset + 2); - int32 offset; - - /* - * MOVT/MOVW instructions encoding in Thumb-2: - * - * i = upper[10] - * imm4 = upper[3:0] - * imm3 = lower[14:12] - * imm8 = lower[7:0] - * - * imm16 = imm4:i:imm3:imm8 - */ - - offset = ((upper & 0x000f) << 12) | ((upper & 0x0400) << 1) - | ((lower & 0x7000) >> 4) | (lower & 0x00ff); - offset = (offset ^ 0x8000) - 0x8000; - - offset += (symbol_addr + reloc_addend); - - if (reloc_type == R_ARM_THM_MOVT_PREL - || reloc_type == R_ARM_THM_MOVW_PREL_NC) - offset -= (int32)(target_section_addr + reloc_offset); - if (reloc_type == R_ARM_THM_MOVT_ABS - || reloc_type == R_ARM_THM_MOVT_PREL) - offset >>= 16; - - upper = (uint16)((upper & 0xfbf0) | ((offset & 0xf000) >> 12) - | ((offset & 0x0800) >> 1)); - lower = (uint16)((lower & 0x8f00) | ((offset & 0x0700) << 4) - | (offset & 0x00ff)); - - *(uint16 *)(target_section_addr + reloc_offset) = upper; - *(uint16 *)(target_section_addr + reloc_offset + 2) = lower; - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %" PRId32 ".", - reloc_type); - return false; - } - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_32.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_32.c deleted file mode 100644 index af3e0bb8e..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_32.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_386_32 1 /* Direct 32 bit */ -#define R_386_PC32 2 /* PC relative 32 bit */ -#define R_386_PLT32 4 /* 32-bit address ProcedureLinkageTable */ - -#if !defined(_WIN32) && !defined(_WIN32_) -/* clang-format off */ -void __divdi3(); -void __udivdi3(); -void __moddi3(); -void __umoddi3(); -/* clang-format on */ -#else -#pragma function(floor) -#pragma function(ceil) - -static int64 -__divdi3(int64 a, int64 b) -{ - return a / b; -} - -static uint64 -__udivdi3(uint64 a, uint64 b) -{ - return a / b; -} - -static int64 -__moddi3(int64 a, int64 b) -{ - return a % b; -} - -static uint64 -__umoddi3(uint64 a, uint64 b) -{ - return a % b; -} -#endif - -/* clang-format off */ -static SymbolMap target_sym_map[] = { - REG_COMMON_SYMBOLS - /* compiler-rt symbols that come from compiler(e.g. gcc) */ - REG_SYM(__divdi3), - REG_SYM(__udivdi3), - REG_SYM(__moddi3), - REG_SYM(__umoddi3) -}; -/* clang-format on */ - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "i386"); -} - -uint32 -get_plt_table_size() -{ - return 0; -} - -void -init_plt_table(uint8 *plt) -{ - (void)plt; -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_386_32: - { - intptr_t value; - - CHECK_RELOC_OFFSET(sizeof(void *)); - value = *(intptr_t *)(target_section_addr + (uint32)reloc_offset); - *(uintptr_t *)(target_section_addr + reloc_offset) = - (uintptr_t)symbol_addr + (intptr_t)reloc_addend - + value; /* S + A */ - break; - } - - /* - * Handle R_386_PLT32 like R_386_PC32 since it should be able to reach - * any 32 bit address - */ - case R_386_PLT32: - case R_386_PC32: - { - int32 value; - - CHECK_RELOC_OFFSET(sizeof(void *)); - value = *(int32 *)(target_section_addr + (uint32)reloc_offset); - *(uint32 *)(target_section_addr + (uint32)reloc_offset) = - (uint32)((uintptr_t)symbol_addr + (intptr_t)reloc_addend - - (uintptr_t)(target_section_addr - + (uint32)reloc_offset) - + value); /* S + A - P */ - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_64.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_64.c deleted file mode 100644 index f4d8eeabd..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_x86_64.c +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#if !defined(BH_PLATFORM_WINDOWS) -#define R_X86_64_64 1 /* Direct 64 bit */ -#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ -#define R_X86_64_PLT32 4 /* 32 bit PLT address */ -#define R_X86_64_32 10 /* Direct 32 bit zero extended */ -#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ -#else -#ifndef IMAGE_REL_AMD64_ADDR64 -#define IMAGE_REL_AMD64_ADDR64 1 /* The 64-bit VA of the relocation target */ -#define IMAGE_REL_AMD64_ADDR32 2 /* The 32-bit VA of the relocation target */ -/* clang-format off */ -#define IMAGE_REL_AMD64_REL32 4 /* The 32-bit relative address from - the byte following the relocation*/ -/* clang-format on */ -#endif -#endif - -#if defined(BH_PLATFORM_WINDOWS) -#pragma function(floor) -#pragma function(ceil) -#pragma function(floorf) -#pragma function(ceilf) -#endif - -/* clang-format off */ -static SymbolMap target_sym_map[] = { - REG_COMMON_SYMBOLS -}; -/* clang-format on */ - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "x86_64"); -} - -static uint32 -get_plt_item_size() -{ - /* size of mov instruction and jmp instruction */ - return 12; -} - -uint32 -get_plt_table_size() -{ - uint32 size = - get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - size += get_plt_item_size() + sizeof(AOTUnwindInfo); -#endif - return size; -} - -void -init_plt_table(uint8 *plt) -{ - uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); - uint8 *p; - - for (i = 0; i < num; i++) { - p = plt; - /* mov symbol_addr, rax */ - *p++ = 0x48; - *p++ = 0xB8; - *(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; - p += sizeof(uint64); - /* jmp rax */ - *p++ = 0xFF; - *p++ = 0xE0; - plt += get_plt_item_size(); - } - -#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - p = plt; - /* mov exception_handler, rax */ - *p++ = 0x48; - *p++ = 0xB8; - *(uint64 *)p = 0; /*(uint64)(uintptr_t)aot_exception_handler;*/ - p += sizeof(uint64); - /* jmp rax */ - *p++ = 0xFF; - *p++ = 0xE0; -#endif -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { -#if !defined(BH_PLATFORM_WINDOWS) - case R_X86_64_64: -#else - case IMAGE_REL_AMD64_ADDR64: -#endif - { - intptr_t value; - - CHECK_RELOC_OFFSET(sizeof(void *)); - value = *(intptr_t *)(target_section_addr + (uint32)reloc_offset); - *(uintptr_t *)(target_section_addr + reloc_offset) = - (uintptr_t)symbol_addr + reloc_addend + value; /* S + A */ - break; - } -#if defined(BH_PLATFORM_WINDOWS) - case IMAGE_REL_AMD64_ADDR32: - { - int32 value; - uintptr_t target_addr; - - CHECK_RELOC_OFFSET(sizeof(void *)); - value = *(int32 *)(target_section_addr + (uint32)reloc_offset); - target_addr = (uintptr_t)symbol_addr + reloc_addend + value; - if ((int32)target_addr != target_addr) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit " - "IMAGE_REL_AMD64_ADDR32 failed. " - "Try using wamrc with --size-level=1 option."); - return false; - } - - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; - break; - } -#endif -#if !defined(BH_PLATFORM_WINDOWS) - case R_X86_64_PC32: - { - intptr_t target_addr = (intptr_t) /* S + A - P */ - ((uintptr_t)symbol_addr + reloc_addend - - (uintptr_t)(target_section_addr + reloc_offset)); - - CHECK_RELOC_OFFSET(sizeof(int32)); - if ((int32)target_addr != target_addr) { - set_error_buf( - error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit R_X86_64_PC32 failed. " - "Try using wamrc with --size-level=1 option."); - return false; - } - - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; - break; - } - case R_X86_64_32: - case R_X86_64_32S: - { - char buf[128]; - uintptr_t target_addr = /* S + A */ - (uintptr_t)symbol_addr + reloc_addend; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - if ((reloc_type == R_X86_64_32 - && (uint32)target_addr != (uint64)target_addr) - || (reloc_type == R_X86_64_32S - && (int32)target_addr != (int64)target_addr)) { - snprintf(buf, sizeof(buf), - "AOT module load failed: " - "relocation truncated to fit %s failed. " - "Try using wamrc with --size-level=1 option.", - reloc_type == R_X86_64_32 ? "R_X86_64_32" - : "R_X86_64_32S"); - set_error_buf(error_buf, error_buf_size, buf); - return false; - } - - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; - break; - } -#endif -#if !defined(BH_PLATFORM_WINDOWS) - case R_X86_64_PLT32: -#else - case IMAGE_REL_AMD64_REL32: -#endif - { - uint8 *plt; - intptr_t target_addr = 0; - - CHECK_RELOC_OFFSET(sizeof(int32)); - - if (symbol_index >= 0) { - plt = (uint8 *)module->code + module->code_size - - get_plt_table_size() - + get_plt_item_size() * symbol_index; - target_addr = (intptr_t) /* L + A - P */ - ((uintptr_t)plt + reloc_addend - - (uintptr_t)(target_section_addr + reloc_offset)); - } - else { - target_addr = (intptr_t) /* L + A - P */ - ((uintptr_t)symbol_addr + reloc_addend - - (uintptr_t)(target_section_addr + reloc_offset)); - } - -#if defined(BH_PLATFORM_WINDOWS) - target_addr -= sizeof(int32); -#endif - if ((int32)target_addr != target_addr) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit " -#if !defined(BH_PLATFORM_WINDOWS) - "R_X86_64_PLT32 failed. " -#else - "IMAGE_REL_AMD64_32 failed." -#endif - "Try using wamrc with --size-level=1 option."); - return false; - } - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - reloc_type); - return false; - } - - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_xtensa.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_xtensa.c deleted file mode 100644 index 3327c396f..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/arch/aot_reloc_xtensa.c +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "aot_reloc.h" - -#define R_XTENSA_32 1 /* Direct 32 bit */ -#define R_XTENSA_SLOT0_OP 20 /* PC relative */ - -/* clang-format off */ -/* for soft-float */ -void __floatsidf(); -void __divdf3(); -void __ltdf2(); - -/* for mul32 */ -void __mulsi3(); -void __muldi3(); - -void __modsi3(); - -void __divdi3(); - -void __udivdi3(); -void __unorddf2(); -void __adddf3(); -void __eqdf2(); -void __muldf3(); -void __gedf2(); -void __ledf2(); -void __fixunsdfsi(); -void __floatunsidf(); -void __subdf3(); -void __nedf2(); -void __fixdfsi(); -void __moddi3(); -void __extendsfdf2(); -void __truncdfsf2(); -void __gtdf2(); -void __umoddi3(); -void __floatdidf(); -void __divsf3(); -void __fixdfdi(); -void __floatundidf(); - - -static SymbolMap target_sym_map[] = { - REG_COMMON_SYMBOLS - - /* API's for soft-float */ - /* TODO: only register these symbols when Floating-Point Coprocessor - * Option is not enabled */ - REG_SYM(__floatsidf), - REG_SYM(__divdf3), - REG_SYM(__ltdf2), - - /* API's for 32-bit integer multiply */ - /* TODO: only register these symbols when 32-bit Integer Multiply Option - * is not enabled */ - REG_SYM(__mulsi3), - REG_SYM(__muldi3), - - REG_SYM(__modsi3), - REG_SYM(__divdi3), - - REG_SYM(__udivdi3), - REG_SYM(__unorddf2), - REG_SYM(__adddf3), - REG_SYM(__eqdf2), - REG_SYM(__muldf3), - REG_SYM(__gedf2), - REG_SYM(__ledf2), - REG_SYM(__fixunsdfsi), - REG_SYM(__floatunsidf), - REG_SYM(__subdf3), - REG_SYM(__nedf2), - REG_SYM(__fixdfsi), - REG_SYM(__moddi3), - REG_SYM(__extendsfdf2), - REG_SYM(__truncdfsf2), - REG_SYM(__gtdf2), - REG_SYM(__umoddi3), - REG_SYM(__floatdidf), - REG_SYM(__divsf3), - REG_SYM(__fixdfdi), - REG_SYM(__floatundidf), -}; -/* clang-format on */ - -static void -set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) -{ - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); -} - -SymbolMap * -get_target_symbol_map(uint32 *sym_num) -{ - *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); - return target_sym_map; -} - -void -get_current_target(char *target_buf, uint32 target_buf_size) -{ - snprintf(target_buf, target_buf_size, "xtensa"); -} - -static uint32 -get_plt_item_size() -{ - return 0; -} - -void -init_plt_table(uint8 *plt) -{ - (void)plt; -} - -uint32 -get_plt_table_size() -{ - return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); -} - -static bool -check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, - uint32 reloc_data_size, char *error_buf, - uint32 error_buf_size) -{ - if (!(reloc_offset < (uint64)target_section_size - && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid relocation offset."); - return false; - } - return true; -} - -/* - * CPU like esp32 can read and write data through the instruction bus, but only - * in a word aligned manner; non-word-aligned access will cause a CPU exception. - * This function uses a world aligned manner to write 16bit value to instruction - * addreess. - */ -static void -put_imm16_to_addr(int16 imm16, int16 *addr) -{ - int8 bytes[8]; - int32 *addr_aligned1, *addr_aligned2; - - addr_aligned1 = (int32 *)((intptr_t)addr & ~3); - - if ((intptr_t)addr % 4 != 3) { - *(int32 *)bytes = *addr_aligned1; - *(int16 *)(bytes + ((intptr_t)addr % 4)) = imm16; - *addr_aligned1 = *(int32 *)bytes; - } - else { - addr_aligned2 = (int32 *)(((intptr_t)addr + 3) & ~3); - *(int32 *)bytes = *addr_aligned1; - *(int32 *)(bytes + 4) = *addr_aligned2; - *(int16 *)(bytes + 3) = imm16; - memcpy(addr_aligned1, bytes, 8); - } -} - -static union { - int a; - char b; -} __ue = { .a = 1 }; - -#define is_little_endian() (__ue.b == 1) - -#if !defined(__packed) -/* - * Note: This version check is a bit relaxed. - * The __packed__ attribute has been there since gcc 2 era. - */ -#if __GNUC__ >= 3 -#define __packed __attribute__((__packed__)) -#endif -#endif - -typedef union { - struct l32r_le { - int8 other; - int16 imm16; - } __packed l; - - struct l32r_be { - int16 imm16; - int8 other; - } __packed b; -} l32r_insn_t; - -bool -apply_relocation(AOTModule *module, uint8 *target_section_addr, - uint32 target_section_size, uint64 reloc_offset, - int64 reloc_addend, uint32 reloc_type, void *symbol_addr, - int32 symbol_index, char *error_buf, uint32 error_buf_size) -{ - switch (reloc_type) { - case R_XTENSA_32: - { - uint8 *insn_addr = target_section_addr + reloc_offset; - int32 initial_addend; - /* (S + A) */ - if ((intptr_t)insn_addr & 3) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "instruction address unaligned."); - return false; - } - CHECK_RELOC_OFFSET(4); - initial_addend = *(int32 *)insn_addr; - *(uintptr_t *)insn_addr = (uintptr_t)symbol_addr + initial_addend - + (intptr_t)reloc_addend; - break; - } - - case R_XTENSA_SLOT0_OP: - { - uint8 *insn_addr = target_section_addr + reloc_offset; - /* Currently only l32r instruction generates R_XTENSA_SLOT0_OP - * relocation */ - l32r_insn_t *l32r_insn = (l32r_insn_t *)insn_addr; - uint8 *reloc_addr; - int32 relative_offset /*, initial_addend */; - int16 imm16; - - CHECK_RELOC_OFFSET(3); /* size of l32r instruction */ - - /* - imm16 = is_little_endian() ? - l32r_insn->l.imm16 : l32r_insn->b.imm16; - initial_addend = (int32)imm16 << 2; - */ - - reloc_addr = - (uint8 *)((uintptr_t)symbol_addr + (intptr_t)reloc_addend); - - if ((intptr_t)reloc_addr & 3) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "relocation address unaligned."); - return false; - } - - relative_offset = - (int32)((intptr_t)reloc_addr - - (((intptr_t)insn_addr + 3) & ~(intptr_t)3)); - /* relative_offset += initial_addend; */ - - /* check relative offset boundary */ - if (relative_offset < -256 * BH_KB || relative_offset > -4) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "target address out of range.\n" - "Try using `wamrc --size-level=0` to generate " - ".literal island."); - return false; - } - - imm16 = (int16)(relative_offset >> 2); - - /* write back the imm16 to the l32r instruction */ - - /* GCC >= 9 complains if we have a pointer that could be - * unaligned. This can happen because the struct is packed. - * These pragma are to suppress the warnings because the - * function put_imm16_to_addr already handles unaligned - * pointers correctly. */ -#if __GNUC__ >= 9 -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Waddress-of-packed-member" -#endif - if (is_little_endian()) - put_imm16_to_addr(imm16, &l32r_insn->l.imm16); - else - put_imm16_to_addr(imm16, &l32r_insn->b.imm16); -#if __GNUC__ >= 9 -#pragma GCC diagnostic pop -#endif - - break; - } - - default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "Load relocation section failed: " - "invalid relocation type %d.", - (int)reloc_type); - return false; - } - - return true; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/LICENSE_NUTTX b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/LICENSE_NUTTX deleted file mode 100644 index 43fa6abd1..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/LICENSE_NUTTX +++ /dev/null @@ -1,202 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/NOTICE_NUTTX b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/NOTICE_NUTTX deleted file mode 100644 index 17227cb29..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/NOTICE_NUTTX +++ /dev/null @@ -1,5 +0,0 @@ -Apache NuttX -Copyright 2020 The Apache Software Foundation - -This product includes software developed at -The Apache Software Foundation (http://www.apache.org/). diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf.h deleted file mode 100644 index 9bdad6521..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf.h +++ /dev/null @@ -1,368 +0,0 @@ -/**************************************************************************** - * include/elf.h - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -#ifndef __INCLUDE_ELF_H -#define __INCLUDE_ELF_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <stdint.h> - -#define EI_NIDENT 16 /* Size of e_ident[] */ - -/* NOTE: elf64.h and elf32.h refer EI_NIDENT defined above */ - -#include "elf64.h" -#include "elf32.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Values for Elf_Ehdr::e_type */ - -#define ET_NONE 0 /* No file type */ -#define ET_REL 1 /* Relocatable file */ -#define ET_EXEC 2 /* Executable file */ -#define ET_DYN 3 /* Shared object file */ -#define ET_CORE 4 /* Core file */ -#define ET_LOPROC 0xff00 /* Processor-specific */ -#define ET_HIPROC 0xffff /* Processor-specific */ - -/* Values for Elf_Ehdr::e_machine (most of this were not included in the - * original SCO document but have been gleaned from elsewhere). - */ - -#define EM_NONE 0 /* No machine */ -#define EM_M32 1 /* AT&T WE 32100 */ -#define EM_SPARC 2 /* SPARC */ -#define EM_386 3 /* Intel 80386 */ -#define EM_68K 4 /* Motorola 68000 */ -#define EM_88K 5 /* Motorola 88000 */ -#define EM_486 6 /* Intel 486+ */ -#define EM_860 7 /* Intel 80860 */ -#define EM_MIPS 8 /* MIPS R3000 Big-Endian */ -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ -#define EM_PARISC 15 /* HPPA */ -#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ -#define EM_PPC 20 /* PowerPC */ -#define EM_PPC64 21 /* PowerPC64 */ -#define EM_ARM 40 /* ARM */ -#define EM_SH 42 /* SuperH */ -#define EM_SPARCV9 43 /* SPARC v9 64-bit */ -#define EM_H8_300 46 -#define EM_IA_64 50 /* HP/Intel IA-64 */ -#define EM_X86_64 62 /* AMD x86-64 */ -#define EM_S390 22 /* IBM S/390 */ -#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ -#define EM_V850 87 /* NEC v850 */ -#define EM_M32R 88 /* Renesas M32R */ -#define EM_XTENSA 94 /* Tensilica Xtensa */ -#define EM_RISCV 243 /* RISC-V */ -#define EM_ALPHA 0x9026 -#define EM_CYGNUS_V850 0x9080 -#define EM_CYGNUS_M32R 0x9041 -#define EM_S390_OLD 0xa390 -#define EM_FRV 0x5441 - -/* Values for Elf_Ehdr::e_version */ - -#define EV_NONE 0 /* Invalid version */ -#define EV_CURRENT 1 /* The current version */ - -/* Table 2. Ehe ELF identifier */ - -#define EI_MAG0 0 /* File identification */ -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 /* File class */ -#define EI_DATA 5 /* Data encoding */ -#define EI_VERSION 6 /* File version */ -#define EI_OSABI 7 /* OS ABI */ -#define EI_PAD 8 /* Start of padding bytes */ - -/* EI_NIDENT is defined in "Included Files" section */ - -#define EI_MAGIC_SIZE 4 -#define EI_MAGIC \ - { \ - 0x7f, 'E', 'L', 'F' \ - } - -#define ELFMAG0 0x7f /* EI_MAG */ -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' -#define ELFMAG "\177ELF" - -/* Table 3. Values for EI_CLASS */ - -#define ELFCLASSNONE 0 /* Invalid class */ -#define ELFCLASS32 1 /* 32-bit objects */ -#define ELFCLASS64 2 /* 64-bit objects */ - -/* Table 4. Values for EI_DATA */ - -#define ELFDATANONE 0 /* Invalid data encoding */ -#define ELFDATA2LSB \ - 1 /* Least significant byte occupying the lowest address \ - */ -#define ELFDATA2MSB 2 /* Most significant byte occupying the lowest address */ - -/* Table 6. Values for EI_OSABI */ - -#define ELFOSABI_NONE 0 /* UNIX System V ABI */ -#define ELFOSABI_SYSV 0 /* Alias. */ -#define ELFOSABI_HPUX 1 /* HP-UX */ -#define ELFOSABI_NETBSD 2 /* NetBSD. */ -#define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */ -#define ELFOSABI_LINUX ELFOSABI_GNU -/* Compatibility alias. */ -#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ -#define ELFOSABI_AIX 7 /* IBM AIX. */ -#define ELFOSABI_IRIX 8 /* SGI Irix. */ -#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ -#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ -#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ -#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ -#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */ -#define ELFOSABI_ARM 97 /* ARM */ -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ - -#ifndef ELF_OSABI -#define ELF_OSABI ELFOSABI_NONE -#endif - -/* Table 7: Special Section Indexes */ - -#define SHN_UNDEF 0 -#define SHN_LOPROC 0xff00 -#define SHN_HIPROC 0xff1f -#define SHN_ABS 0xfff1 -#define SHN_COMMON 0xfff2 - -/* Figure 4-9: Section Types, sh_type */ - -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_HASH 5 -#define SHT_DYNAMIC 6 -#define SHT_NOTE 7 -#define SHT_NOBITS 8 -#define SHT_REL 9 -#define SHT_SHLIB 10 -#define SHT_DYNSYM 11 -#define SHT_LOPROC 0x70000000 -#define SHT_HIPROC 0x7fffffff -#define SHT_LOUSER 0x80000000 -#define SHT_HIUSER 0xffffffff - -/* Figure 4-11: Section Attribute Flags, sh_flags */ - -#define SHF_WRITE 1 -#define SHF_ALLOC 2 -#define SHF_EXECINSTR 4 -#define SHF_MASKPROC 0xf0000000 - -/* Figure 4-16: Symbol Binding, ELF_ST_BIND */ - -#define STB_LOCAL 0 -#define STB_GLOBAL 1 -#define STB_WEAK 2 -#define STB_LOPROC 13 -#define STB_HIPROC 15 - -/* Figure 4-17: Symbol Types, ELF_ST_TYPE */ - -#define STT_NOTYPE 0 -#define STT_OBJECT 1 -#define STT_FUNC 2 -#define STT_SECTION 3 -#define STT_FILE 4 -#define STT_LOPROC 13 -#define STT_HIPROC 15 - -/* Figure 5-2: Segment Types, p_type */ - -#define PT_NULL 0 -#define PT_LOAD 1 -#define PT_DYNAMIC 2 -#define PT_INTERP 3 -#define PT_NOTE 4 -#define PT_SHLIB 5 -#define PT_PHDR 6 -#define PT_LOPROC 0x70000000 -#define PT_HIPROC 0x7fffffff - -/* Figure 5-3: Segment Flag Bits, p_flags */ - -#define PF_X 1 /* Execute */ -#define PF_W 2 /* Write */ -#define PF_R 4 /* Read */ -#define PF_MASKPROC 0xf0000000 /* Unspecified */ - -/* Figure 5-10: Dynamic Array Tags, d_tag */ - -#define DT_NULL 0 /* d_un=ignored */ -#define DT_NEEDED 1 /* d_un=d_val */ -#define DT_PLTRELSZ 2 /* d_un=d_val */ -#define DT_PLTGOT 3 /* d_un=d_ptr */ -#define DT_HASH 4 /* d_un=d_ptr */ -#define DT_STRTAB 5 /* d_un=d_ptr */ -#define DT_SYMTAB 6 /* d_un=d_ptr */ -#define DT_RELA 7 /* d_un=d_ptr */ -#define DT_RELASZ 8 /* d_un=d_val */ -#define DT_RELAENT 9 /* d_un=d_val */ -#define DT_STRSZ 10 /* d_un=d_val */ -#define DT_SYMENT 11 /* d_un=d_val */ -#define DT_INIT 12 /* d_un=d_ptr */ -#define DT_FINI 13 /* d_un=d_ptr */ -#define DT_SONAME 14 /* d_un=d_val */ -#define DT_RPATH 15 /* d_un=d_val */ -#define DT_SYMBOLIC 16 /* d_un=ignored */ -#define DT_REL 17 /* d_un=d_ptr */ -#define DT_RELSZ 18 /* d_un=d_val */ -#define DT_RELENT 19 /* d_un=d_val */ -#define DT_PLTREL 20 /* d_un=d_val */ -#define DT_DEBUG 21 /* d_un=d_ptr */ -#define DT_TEXTREL 22 /* d_un=ignored */ -#define DT_JMPREL 23 /* d_un=d_ptr */ -#define DT_BINDNOW 24 /* d_un=ignored */ -#define DT_LOPROC 0x70000000 /* d_un=unspecified */ -#define DT_HIPROC 0x7fffffff /* d_un= unspecified */ - -/* Legal values for note segment descriptor types for core files. */ - -#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */ -#define NT_PRFPREG 2 /* Contains copy of fpregset struct. */ -#define NT_FPREGSET 2 /* Contains copy of fpregset struct */ -#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */ -#define NT_PRXREG 4 /* Contains copy of prxregset struct */ -#define NT_TASKSTRUCT 4 /* Contains copy of task structure */ -#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */ -#define NT_AUXV 6 /* Contains copy of auxv array */ -#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */ -#define NT_ASRS 8 /* Contains copy of asrset struct */ -#define NT_PSTATUS 10 /* Contains copy of pstatus struct */ -#define NT_PSINFO 13 /* Contains copy of psinfo struct */ -#define NT_PRCRED 14 /* Contains copy of prcred struct */ -#define NT_UTSNAME 15 /* Contains copy of utsname struct */ -#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */ -#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */ -#define NT_PRFPXREG 20 /* Contains copy of fprxregset struct */ -#define NT_SIGINFO 0x53494749 -/* Contains copy of siginfo_t, - * size might increase - */ -#define NT_FILE 0x46494c45 -/* Contains information about mapped - * files - */ -#define NT_PRXFPREG 0x46e62b7f -/* Contains copy of user_fxsr_struct */ -#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ -#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ -#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ -#define NT_PPC_TAR 0x103 /* Target Address Register */ -#define NT_PPC_PPR 0x104 /* Program Priority Register */ -#define NT_PPC_DSCR 0x105 /* Data Stream Control Register */ -#define NT_PPC_EBB 0x106 /* Event Based Branch Registers */ -#define NT_PPC_PMU 0x107 /* Performance Monitor Registers */ -#define NT_PPC_TM_CGPR 0x108 /* TM checkpointed GPR Registers */ -#define NT_PPC_TM_CFPR 0x109 /* TM checkpointed FPR Registers */ -#define NT_PPC_TM_CVMX 0x10a /* TM checkpointed VMX Registers */ -#define NT_PPC_TM_CVSX 0x10b /* TM checkpointed VSX Registers */ -#define NT_PPC_TM_SPR 0x10c /* TM Special Purpose Registers */ -#define NT_PPC_TM_CTAR \ - 0x10d /* TM checkpointed Target Address \ - * Register \ - */ -#define NT_PPC_TM_CPPR \ - 0x10e /* TM checkpointed Program Priority \ - * Register \ - */ -#define NT_PPC_TM_CDSCR \ - 0x10f /* TM checkpointed Data Stream Control \ - * Register \ - */ -#define NT_PPC_PKEY \ - 0x110 /* Memory Protection Keys \ - * registers. \ - */ -#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ -#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ -#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ -#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ -#define NT_S390_TIMER 0x301 /* s390 timer register */ -#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ -#define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */ -#define NT_S390_CTRS 0x304 /* s390 control registers */ -#define NT_S390_PREFIX 0x305 /* s390 prefix register */ -#define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */ -#define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */ -#define NT_S390_TDB 0x308 /* s390 transaction diagnostic block */ -#define NT_S390_VXRS_LOW \ - 0x309 /* s390 vector registers 0-15 \ - * upper half. \ - */ -#define NT_S390_VXRS_HIGH 0x30a /* s390 vector registers 16-31. */ -#define NT_S390_GS_CB 0x30b /* s390 guarded storage registers. */ -#define NT_S390_GS_BC \ - 0x30c /* s390 guarded storage \ - * broadcast control block. \ - */ -#define NT_S390_RI_CB 0x30d /* s390 runtime instrumentation. */ -#define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */ -#define NT_ARM_TLS 0x401 /* ARM TLS register */ -#define NT_ARM_HW_BREAK 0x402 /* ARM hardware breakpoint registers */ -#define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registers */ -#define NT_ARM_SYSTEM_CALL 0x404 /* ARM system call number */ -#define NT_ARM_SVE \ - 0x405 /* ARM Scalable Vector Extension \ - * registers \ - */ -#define NT_ARM_PAC_MASK \ - 0x406 /* ARM pointer authentication \ - * code masks. \ - */ -#define NT_ARM_PACA_KEYS \ - 0x407 /* ARM pointer authentication \ - * address keys. \ - */ -#define NT_ARM_PACG_KEYS \ - 0x408 /* ARM pointer authentication \ - * generic key. \ - */ -#define NT_VMCOREDD 0x700 /* Vmcore Device Dump Note. */ -#define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers. */ -#define NT_MIPS_FP_MODE 0x801 /* MIPS floating-point mode. */ -#define NT_MIPS_MSA 0x802 /* MIPS SIMD registers. */ - -/* Legal values for the note segment descriptor types for object files. */ - -#define NT_VERSION 1 /* Contains a version string. */ - -#endif /* __INCLUDE_ELF_H */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf32.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf32.h deleted file mode 100644 index b4b27948e..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf32.h +++ /dev/null @@ -1,161 +0,0 @@ -/**************************************************************************** - * include/elf32.h - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -#ifndef __INCLUDE_ELF32_H -#define __INCLUDE_ELF32_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <stdint.h> - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define ELF32_ST_BIND(i) ((i) >> 4) -#define ELF32_ST_TYPE(i) ((i)&0xf) -#define ELF32_ST_INFO(b, t) (((b) << 4) | ((t)&0xf)) - -/* Definitions for Elf32_Rel*::r_info */ - -#define ELF32_R_SYM(i) ((i) >> 8) -#define ELF32_R_TYPE(i) ((i)&0xff) -#define ELF32_R_INFO(s, t) (((s) << 8) | ((t)&0xff)) - -#if 0 -#define ELF_R_SYM(i) ELF32_R_SYM(i) -#endif - -/**************************************************************************** - * Public Type Definitions - ****************************************************************************/ - -/* Figure 4.2: 32-Bit Data Types */ - -typedef uint32_t Elf32_Addr; /* Unsigned program address */ -typedef uint16_t Elf32_Half; /* Unsigned medium integer */ -typedef uint32_t Elf32_Off; /* Unsigned file offset */ -typedef int32_t Elf32_Sword; /* Signed large integer */ -typedef uint32_t Elf32_Word; /* Unsigned large integer */ - -/* Figure 4-3: ELF Header */ - -typedef struct { - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; - Elf32_Half e_phentsize; - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; -} Elf32_Ehdr; - -/* Figure 4-8: Section Header */ - -typedef struct { - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; -} Elf32_Shdr; - -/* Figure 4-15: Symbol Table Entry */ - -typedef struct { - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - unsigned char st_info; - unsigned char st_other; - Elf32_Half st_shndx; -} Elf32_Sym; - -/* Figure 4-19: Relocation Entries */ - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; -} Elf32_Rel; - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; - Elf32_Sword r_addend; -} Elf32_Rela; - -/* Figure 5-1: Program Header */ - -typedef struct { - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; -} Elf32_Phdr; - -/* Figure 5-7: Note Information */ - -typedef struct { - Elf32_Word n_namesz; /* Length of the note's name. */ - Elf32_Word n_descsz; /* Length of the note's descriptor. */ - Elf32_Word n_type; /* Type of the note. */ -} Elf32_Nhdr; - -/* Figure 5-9: Dynamic Structure */ - -typedef struct { - Elf32_Sword d_tag; - union { - Elf32_Word d_val; - Elf32_Addr d_ptr; - } d_un; -} Elf32_Dyn; - -#if 0 -typedef Elf32_Addr Elf_Addr; -typedef Elf32_Ehdr Elf_Ehdr; -typedef Elf32_Rel Elf_Rel; -typedef Elf32_Rela Elf_Rela; -typedef Elf32_Nhdr Elf_Nhdr; -typedef Elf32_Phdr Elf_Phdr; -typedef Elf32_Sym Elf_Sym; -typedef Elf32_Shdr Elf_Shdr; -typedef Elf32_Word Elf_Word; -#endif - -#endif /* __INCLUDE_ELF32_H */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf64.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf64.h deleted file mode 100644 index 499c737c1..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf64.h +++ /dev/null @@ -1,161 +0,0 @@ -/**************************************************************************** - * include/elf64.h - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -#ifndef __INCLUDE_ELF64_H -#define __INCLUDE_ELF64_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <stdint.h> - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* See ELF-64 Object File Format: Version 1.5 Draft 2 */ - -/* Definitions for Elf64_Rel*::r_info */ - -#define ELF64_R_SYM(i) ((i) >> 32) -#define ELF64_R_TYPE(i) ((i)&0xffffffffL) -#define ELF64_R_INFO(s, t) (((s) << 32) + ((t)&0xffffffffL)) - -#if 0 -#define ELF_R_SYM(i) ELF64_R_SYM(i) -#endif - -/**************************************************************************** - * Public Type Definitions - ****************************************************************************/ - -/* Table 1: ELF-64 Data Types */ - -typedef uint64_t Elf64_Addr; /* Unsigned program address */ -typedef uint64_t Elf64_Off; /* Unsigned file offset */ -typedef uint16_t Elf64_Half; /* Unsigned medium integer */ -typedef uint32_t Elf64_Word; /* Unsigned long integer */ -typedef int32_t Elf64_Sword; /* Signed integer */ -typedef uint64_t Elf64_Xword; /* Unsigned long integer */ -typedef int64_t Elf64_Sxword; /* Signed large integer */ - -/* Figure 2: ELF-64 Header */ - -typedef struct { - unsigned char e_ident[EI_NIDENT]; /* ELF identification */ - Elf64_Half e_type; /* Object file type */ - Elf64_Half e_machine; /* Machine type */ - Elf64_Word e_version; /* Object file version */ - Elf64_Addr e_entry; /* Entry point address */ - Elf64_Off e_phoff; /* Program header offset */ - Elf64_Off e_shoff; /* Section header offset */ - Elf64_Word e_flags; /* Processor-specific flags */ - Elf64_Half e_ehsize; /* ELF header size */ - Elf64_Half e_phentsize; /* Size of program header entry */ - Elf64_Half e_phnum; /* Number of program header entry */ - Elf64_Half e_shentsize; /* Size of section header entry */ - Elf64_Half e_shnum; /* Number of section header entries */ - Elf64_Half e_shstrndx; /* Section name string table index */ -} Elf64_Ehdr; - -/* Figure 3: ELF-64 Section Header */ - -typedef struct { - Elf64_Word sh_name; /* Section name */ - Elf64_Word sh_type; /* Section type */ - Elf64_Xword sh_flags; /* Section attributes */ - Elf64_Addr sh_addr; /* Virtual address in memory */ - Elf64_Off sh_offset; /* Offset in file */ - Elf64_Xword sh_size; /* Size of section */ - Elf64_Word sh_link; /* Link to other section */ - Elf64_Word sh_info; /* Miscellaneous information */ - Elf64_Xword sh_addralign; /* Address alignment boundary */ - Elf64_Xword sh_entsize; /* Size of entries, if section has table */ -} Elf64_Shdr; - -/* Figure 4: ELF-64 Symbol Table Entry */ - -typedef struct { - Elf64_Word st_name; /* Symbol name */ - unsigned char st_info; /* Type and Binding attributes */ - unsigned char st_other; /* Reserved */ - Elf64_Half st_shndx; /* Section table index */ - Elf64_Addr st_value; /* Symbol value */ - Elf64_Xword st_size; /* Size of object (e.g., common) */ -} Elf64_Sym; - -/* Figure 5: ELF-64 Relocation Entries */ - -typedef struct { - Elf64_Addr r_offset; /* Address of reference */ - Elf64_Xword r_info; /* Symbol index and type of relocation */ -} Elf64_Rel; - -typedef struct { - Elf64_Addr r_offset; /* Address of reference */ - Elf64_Xword r_info; /* Symbol index and type of relocation */ - Elf64_Sxword r_addend; /* Constant part of expression */ -} Elf64_Rela; - -/* Figure 6: ELF-64 Program Header Table Entry */ - -typedef struct { - Elf64_Word p_type; /* Type of segment */ - Elf64_Word p_flags; /* Segment attributes */ - Elf64_Off p_offset; /* Offset in file */ - Elf64_Addr p_vaddr; /* Virtual address in memory */ - Elf64_Addr p_paddr; /* Reserved */ - Elf64_Word p_filesz; /* Size of segment in file */ - Elf64_Word p_memsz; /* Size of segment in memory */ - Elf64_Word p_align; /* Alignment of segment */ -} Elf64_Phdr; - -/* Figure 7. Format of a Note Section */ - -typedef struct { - Elf64_Word n_namesz; /* Length of the note's name. */ - Elf64_Word n_descsz; /* Length of the note's descriptor. */ - Elf64_Word n_type; /* Type of the note. */ -} Elf64_Nhdr; - -/* Figure 8: Dynamic Table Structure */ - -typedef struct { - Elf64_Sxword d_tag; - union { - Elf64_Xword d_val; - Elf64_Addr d_ptr; - } d_un; -} Elf64_Dyn; - -#if 0 -typedef Elf64_Addr Elf_Addr; -typedef Elf64_Ehdr Elf_Ehdr; -typedef Elf64_Rel Elf_Rel; -typedef Elf64_Rela Elf_Rela; -typedef Elf64_Nhdr Elf_Nhdr; -typedef Elf64_Phdr Elf_Phdr; -typedef Elf64_Sym Elf_Sym; -typedef Elf64_Shdr Elf_Shdr; -typedef Elf64_Word Elf_Word; -#endif - -#endif /* __INCLUDE_ELF64_H */ diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.c deleted file mode 100644 index 9fec281d3..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2021 Ant Group. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include <stdio.h> -#include <assert.h> -#include <fcntl.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <errno.h> -#include <stdbool.h> - -#include "elf.h" - -#include "aot_runtime.h" -#include "bh_log.h" -#include "elf_parser.h" - -bool -is_ELF(void *buf) -{ - Elf32_Ehdr *eh = (Elf32_Ehdr *)buf; - if (!strncmp((char *)eh->e_ident, "\177ELF", 4)) { - LOG_VERBOSE("the buffer is ELF entry!"); - return true; - } - LOG_VERBOSE("the buffer is not ELF entry!"); - return false; -} - -static bool -is64Bit(Elf32_Ehdr *eh) -{ - if (eh->e_ident[EI_CLASS] == ELFCLASS64) - return true; - else - return false; -} - -static bool -is32Bit(Elf32_Ehdr *eh) -{ - if (eh->e_ident[EI_CLASS] == ELFCLASS32) - return true; - else - return false; -} - -bool -is_ELF64(void *buf) -{ - Elf64_Ehdr *eh = (Elf64_Ehdr *)buf; - if (!strncmp((char *)eh->e_ident, "\177ELF", 4)) { - LOG_VERBOSE("the buffer is ELF entry!"); - return true; - } - LOG_VERBOSE("the buffer is not ELF entry!"); - return false; -} - -static void -read_section_header_table(Elf32_Ehdr *eh, Elf32_Shdr *sh_table[]) -{ - uint32_t i; - char *buf = (char *)eh; - buf += eh->e_shoff; - LOG_VERBOSE("str index = %d count=%d", eh->e_shstrndx, eh->e_shnum); - for (i = 0; i < eh->e_shnum; i++) { - sh_table[i] = (Elf32_Shdr *)buf; - buf += eh->e_shentsize; - } -} - -static void -read_section_header_table64(Elf64_Ehdr *eh, Elf64_Shdr *sh_table[]) -{ - uint32_t i; - char *buf = (char *)eh; - buf += eh->e_shoff; - - for (i = 0; i < eh->e_shnum; i++) { - sh_table[i] = (Elf64_Shdr *)buf; - buf += eh->e_shentsize; - } -} - -static char * -get_section(Elf32_Ehdr *eh, Elf32_Shdr *section_header) -{ - char *buf = (char *)eh; - return buf + section_header->sh_offset; -} - -static char * -get_section64(Elf64_Ehdr *eh, Elf64_Shdr *section_header) -{ - char *buf = (char *)eh; - return buf + section_header->sh_offset; -} - -bool -get_text_section(void *buf, uint64_t *offset, uint64_t *size) -{ - bool ret = false; - uint32 i; - char *sh_str; - - if (is64Bit(buf)) { - Elf64_Ehdr *eh = (Elf64_Ehdr *)buf; - Elf64_Shdr **sh_table = - wasm_runtime_malloc(eh->e_shnum * sizeof(Elf64_Shdr *)); - if (sh_table) { - read_section_header_table64(eh, sh_table); - sh_str = get_section64(eh, sh_table[eh->e_shstrndx]); - for (i = 0; i < eh->e_shnum; i++) { - if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { - *offset = sh_table[i]->sh_offset; - *size = sh_table[i]->sh_size; - sh_table[i]->sh_addr = - (Elf64_Addr)(uintptr_t)((char *)buf - + sh_table[i]->sh_offset); - ret = true; - break; - } - } - wasm_runtime_free(sh_table); - } - } - else if (is32Bit(buf)) { - Elf32_Ehdr *eh = (Elf32_Ehdr *)buf; - Elf32_Shdr **sh_table = - wasm_runtime_malloc(eh->e_shnum * sizeof(Elf32_Shdr *)); - if (sh_table) { - read_section_header_table(eh, sh_table); - sh_str = get_section(eh, sh_table[eh->e_shstrndx]); - for (i = 0; i < eh->e_shnum; i++) { - if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { - *offset = sh_table[i]->sh_offset; - *size = sh_table[i]->sh_size; - sh_table[i]->sh_addr = - (Elf32_Addr)(uintptr_t)((char *)buf - + sh_table[i]->sh_offset); - ret = true; - break; - } - } - wasm_runtime_free(sh_table); - } - } - - return ret; -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.h deleted file mode 100644 index 887d82fa9..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/elf_parser.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2021 Ant Group. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _ELF_PARSER_H_ -#define _ELF_PARSER_H_ -#include <stdbool.h> - -#ifdef __cplusplus -extern "C" { -#endif - -bool -is_ELF(void *buf); - -bool -is_ELF64(void *buf); - -bool -get_text_section(void *buf, uint64_t *offset, uint64_t *size); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.c deleted file mode 100644 index 310662f55..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.c +++ /dev/null @@ -1,255 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2021 Ant Group. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "bh_log.h" -#include "bh_platform.h" -#include "../../interpreter/wasm_runtime.h" - -#include <stdio.h> -#include <assert.h> -#include <fcntl.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <errno.h> -#include <stdbool.h> - -/* This must be kept in sync with gdb/gdb/jit.h */ -#ifdef __cplusplus -extern "C" { -#endif - -/* clang-format off */ -typedef enum JITAction { - JIT_NOACTION = 0, - JIT_REGISTER_FN, - JIT_UNREGISTER_FN -} JITAction; -/* clang-format on */ - -typedef struct JITCodeEntry { - struct JITCodeEntry *next_; - struct JITCodeEntry *prev_; - const uint8 *symfile_addr_; - uint64 symfile_size_; -} JITCodeEntry; - -typedef struct JITDescriptor { - uint32 version_; - uint32 action_flag_; - JITCodeEntry *relevant_entry_; - JITCodeEntry *first_entry_; -} JITDescriptor; - -/* LLVM has already define this */ -#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) -/** - * GDB will place breakpoint into this function. - * To prevent GCC from inlining or removing it we place noinline attribute - * and inline assembler statement inside. - */ -void __attribute__((noinline)) __jit_debug_register_code(); - -void __attribute__((noinline)) __jit_debug_register_code() -{ - int x; - *(char *)&x = '\0'; -} - -/** - * GDB will inspect contents of this descriptor. - * Static initialization is necessary to prevent GDB from seeing - * uninitialized descriptor. - */ - -JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, NULL, NULL }; -#else -extern void -__jit_debug_register_code(); -extern JITDescriptor __jit_debug_descriptor; -#endif - -/** - * Call __jit_debug_register_code indirectly via global variable. - * This gives the debugger an easy way to inject custom code to - * handle the events. - */ -void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; - -#ifdef __cplusplus -} -#endif - -typedef struct WASMJITDebugEngine { - korp_mutex jit_entry_lock; - bh_list jit_entry_list; -} WASMJITDebugEngine; - -typedef struct WASMJITEntryNode { - struct WASMJITEntryNode *next; - JITCodeEntry *entry; -} WASMJITEntryNode; - -static WASMJITDebugEngine *jit_debug_engine; - -static JITCodeEntry * -CreateJITCodeEntryInternal(const uint8 *symfile_addr, uint64 symfile_size) -{ - JITCodeEntry *entry; - - os_mutex_lock(&jit_debug_engine->jit_entry_lock); - - if (!(entry = wasm_runtime_malloc(sizeof(JITCodeEntry)))) { - LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory"); - os_mutex_unlock(&jit_debug_engine->jit_entry_lock); - return NULL; - } - entry->symfile_addr_ = symfile_addr; - entry->symfile_size_ = symfile_size; - entry->prev_ = NULL; - - entry->next_ = __jit_debug_descriptor.first_entry_; - if (entry->next_ != NULL) { - entry->next_->prev_ = entry; - } - __jit_debug_descriptor.first_entry_ = entry; - __jit_debug_descriptor.relevant_entry_ = entry; - - __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; - - (*__jit_debug_register_code_ptr)(); - - os_mutex_unlock(&jit_debug_engine->jit_entry_lock); - return entry; -} - -static void -DestroyJITCodeEntryInternal(JITCodeEntry *entry) -{ - os_mutex_lock(&jit_debug_engine->jit_entry_lock); - - if (entry->prev_ != NULL) { - entry->prev_->next_ = entry->next_; - } - else { - __jit_debug_descriptor.first_entry_ = entry->next_; - } - - if (entry->next_ != NULL) { - entry->next_->prev_ = entry->prev_; - } - - __jit_debug_descriptor.relevant_entry_ = entry; - __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; - (*__jit_debug_register_code_ptr)(); - - wasm_runtime_free(entry); - - os_mutex_unlock(&jit_debug_engine->jit_entry_lock); -} - -bool -jit_debug_engine_init() -{ - if (jit_debug_engine) { - return true; - } - - if (!(jit_debug_engine = wasm_runtime_malloc(sizeof(WASMJITDebugEngine)))) { - LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory"); - return false; - } - memset(jit_debug_engine, 0, sizeof(WASMJITDebugEngine)); - - if (os_mutex_init(&jit_debug_engine->jit_entry_lock) != 0) { - wasm_runtime_free(jit_debug_engine); - jit_debug_engine = NULL; - return false; - } - - bh_list_init(&jit_debug_engine->jit_entry_list); - return true; -} - -void -jit_debug_engine_destroy() -{ - if (jit_debug_engine) { - WASMJITEntryNode *node, *node_next; - - /* Destroy all nodes */ - node = bh_list_first_elem(&jit_debug_engine->jit_entry_list); - while (node) { - node_next = bh_list_elem_next(node); - DestroyJITCodeEntryInternal(node->entry); - bh_list_remove(&jit_debug_engine->jit_entry_list, node); - wasm_runtime_free(node); - node = node_next; - } - - /* Destroy JIT Debug Engine */ - os_mutex_destroy(&jit_debug_engine->jit_entry_lock); - wasm_runtime_free(jit_debug_engine); - jit_debug_engine = NULL; - } -} - -bool -jit_code_entry_create(const uint8 *symfile_addr, uint64 symfile_size) -{ - JITCodeEntry *entry; - WASMJITEntryNode *node; - - if (!(node = wasm_runtime_malloc(sizeof(WASMJITEntryNode)))) { - LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory"); - return false; - } - - entry = CreateJITCodeEntryInternal(symfile_addr, symfile_size); - - if (!entry) { - wasm_runtime_free(node); - return false; - } - - node->entry = entry; - os_mutex_lock(&jit_debug_engine->jit_entry_lock); - bh_list_insert(&jit_debug_engine->jit_entry_list, node); - os_mutex_unlock(&jit_debug_engine->jit_entry_lock); - return true; -} - -void -jit_code_entry_destroy(const uint8 *symfile_addr) -{ - WASMJITEntryNode *node; - - node = bh_list_first_elem(&jit_debug_engine->jit_entry_list); - while (node) { - WASMJITEntryNode *next_node = bh_list_elem_next(node); - if (node->entry->symfile_addr_ == symfile_addr) { - DestroyJITCodeEntryInternal(node->entry); - os_mutex_lock(&jit_debug_engine->jit_entry_lock); - bh_list_remove(&jit_debug_engine->jit_entry_list, node); - os_mutex_unlock(&jit_debug_engine->jit_entry_lock); - wasm_runtime_free(node); - } - node = next_node; - } -} diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.h deleted file mode 100644 index 5e3e36519..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/debug/jit_debug.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2021 Ant Group. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _JIT_DEBUG_H_ -#define _JIT_DEBUG_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -bool -jit_debug_engine_init(); - -void -jit_debug_engine_destroy(); - -bool -jit_code_entry_create(const uint8 *symfile_addr, uint64 symfile_size); - -void -jit_code_entry_destroy(const uint8 *symfile_addr); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/iwasm_aot.cmake b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/iwasm_aot.cmake deleted file mode 100644 index 8014f2372..000000000 --- a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/aot/iwasm_aot.cmake +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -set (IWASM_AOT_DIR ${CMAKE_CURRENT_LIST_DIR}) - -add_definitions (-DWASM_ENABLE_AOT=1) - -include_directories (${IWASM_AOT_DIR}) - -file (GLOB c_source_all ${IWASM_AOT_DIR}/*.c) - -if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_x86_64.c) -elseif (WAMR_BUILD_TARGET STREQUAL "X86_32") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_x86_32.c) -elseif (WAMR_BUILD_TARGET MATCHES "AARCH64.*") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_aarch64.c) -elseif (WAMR_BUILD_TARGET MATCHES "ARM.*") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_arm.c) -elseif (WAMR_BUILD_TARGET MATCHES "THUMB.*") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_thumb.c) -elseif (WAMR_BUILD_TARGET STREQUAL "MIPS") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_mips.c) -elseif (WAMR_BUILD_TARGET STREQUAL "XTENSA") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_xtensa.c) -elseif (WAMR_BUILD_TARGET MATCHES "RISCV*") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_riscv.c) -elseif (WAMR_BUILD_TARGET STREQUAL "ARC") - set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_arc.c) -else () - message (FATAL_ERROR "Build target isn't set") -endif () - -if (WAMR_BUILD_DEBUG_AOT EQUAL 1) - add_definitions(-DWASM_ENABLE_DEBUG_AOT=1) - file(GLOB debug_source ${IWASM_AOT_DIR}/debug/*.c) -endif() - -set (IWASM_AOT_SOURCE ${c_source_all} ${arch_source} ${debug_source}) - |