aot_reloc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_RELOC_H_
  6. #define _AOT_RELOC_H_
  7. #include "aot_runtime.h"
  8. #include "aot_intrinsic.h"
  9. #if WASM_ENABLE_STRINGREF != 0
  10. #include "string_object.h"
  11. #endif
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. typedef struct {
  16. const char *symbol_name;
  17. void *symbol_addr;
  18. } SymbolMap;
  19. /* clang-format off */
  20. #define REG_SYM(symbol) { #symbol, (void *)symbol }
  21. #if WASM_ENABLE_BULK_MEMORY != 0
  22. #define REG_BULK_MEMORY_SYM() \
  23. REG_SYM(aot_memory_init), \
  24. REG_SYM(aot_data_drop),
  25. #else
  26. #define REG_BULK_MEMORY_SYM()
  27. #endif
  28. #if WASM_ENABLE_SHARED_MEMORY != 0
  29. #include "wasm_shared_memory.h"
  30. #define REG_ATOMIC_WAIT_SYM() \
  31. REG_SYM(wasm_runtime_atomic_wait), \
  32. REG_SYM(wasm_runtime_atomic_notify),
  33. #else
  34. #define REG_ATOMIC_WAIT_SYM()
  35. #endif
  36. #if WASM_ENABLE_REF_TYPES != 0
  37. #define REG_REF_TYPES_SYM() \
  38. REG_SYM(aot_drop_table_seg), \
  39. REG_SYM(aot_table_init), \
  40. REG_SYM(aot_table_copy), \
  41. REG_SYM(aot_table_fill), \
  42. REG_SYM(aot_table_grow),
  43. #else
  44. #define REG_REF_TYPES_SYM()
  45. #endif
  46. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  47. #define REG_AOT_TRACE_SYM() \
  48. REG_SYM(aot_alloc_frame), \
  49. REG_SYM(aot_free_frame), \
  50. REG_SYM(aot_frame_update_profile_info),
  51. #else
  52. #define REG_AOT_TRACE_SYM()
  53. #endif
  54. #if WASM_ENABLE_AOT_INTRINSICS != 0
  55. #define REG_INTRINSIC_SYM() \
  56. REG_SYM(aot_intrinsic_fabs_f32), \
  57. REG_SYM(aot_intrinsic_fabs_f64), \
  58. REG_SYM(aot_intrinsic_floor_f32), \
  59. REG_SYM(aot_intrinsic_floor_f64), \
  60. REG_SYM(aot_intrinsic_ceil_f32), \
  61. REG_SYM(aot_intrinsic_ceil_f64), \
  62. REG_SYM(aot_intrinsic_trunc_f32), \
  63. REG_SYM(aot_intrinsic_trunc_f64), \
  64. REG_SYM(aot_intrinsic_rint_f32), \
  65. REG_SYM(aot_intrinsic_rint_f64), \
  66. REG_SYM(aot_intrinsic_sqrt_f32), \
  67. REG_SYM(aot_intrinsic_sqrt_f64), \
  68. REG_SYM(aot_intrinsic_copysign_f32), \
  69. REG_SYM(aot_intrinsic_copysign_f64), \
  70. REG_SYM(aot_intrinsic_fadd_f32), \
  71. REG_SYM(aot_intrinsic_fadd_f64), \
  72. REG_SYM(aot_intrinsic_fsub_f32), \
  73. REG_SYM(aot_intrinsic_fsub_f64), \
  74. REG_SYM(aot_intrinsic_fmul_f32), \
  75. REG_SYM(aot_intrinsic_fmul_f64), \
  76. REG_SYM(aot_intrinsic_fdiv_f32), \
  77. REG_SYM(aot_intrinsic_fdiv_f64), \
  78. REG_SYM(aot_intrinsic_fmin_f32), \
  79. REG_SYM(aot_intrinsic_fmin_f64), \
  80. REG_SYM(aot_intrinsic_fmax_f32), \
  81. REG_SYM(aot_intrinsic_fmax_f64), \
  82. REG_SYM(aot_intrinsic_clz_i32), \
  83. REG_SYM(aot_intrinsic_clz_i64), \
  84. REG_SYM(aot_intrinsic_ctz_i32), \
  85. REG_SYM(aot_intrinsic_ctz_i64), \
  86. REG_SYM(aot_intrinsic_popcnt_i32), \
  87. REG_SYM(aot_intrinsic_popcnt_i64), \
  88. REG_SYM(aot_intrinsic_i32_to_f32), \
  89. REG_SYM(aot_intrinsic_u32_to_f32), \
  90. REG_SYM(aot_intrinsic_i32_to_f64), \
  91. REG_SYM(aot_intrinsic_u32_to_f64), \
  92. REG_SYM(aot_intrinsic_i64_to_f32), \
  93. REG_SYM(aot_intrinsic_u64_to_f32), \
  94. REG_SYM(aot_intrinsic_i64_to_f64), \
  95. REG_SYM(aot_intrinsic_u64_to_f64), \
  96. REG_SYM(aot_intrinsic_f64_to_f32), \
  97. REG_SYM(aot_intrinsic_f32_to_i32), \
  98. REG_SYM(aot_intrinsic_f32_to_u32), \
  99. REG_SYM(aot_intrinsic_f32_to_i64), \
  100. REG_SYM(aot_intrinsic_f32_to_u64), \
  101. REG_SYM(aot_intrinsic_f64_to_i32), \
  102. REG_SYM(aot_intrinsic_f64_to_u32), \
  103. REG_SYM(aot_intrinsic_f64_to_i64), \
  104. REG_SYM(aot_intrinsic_f64_to_u64), \
  105. REG_SYM(aot_intrinsic_f32_to_f64), \
  106. REG_SYM(aot_intrinsic_f32_cmp), \
  107. REG_SYM(aot_intrinsic_f64_cmp), \
  108. REG_SYM(aot_intrinsic_i64_div_s), \
  109. REG_SYM(aot_intrinsic_i64_div_u), \
  110. REG_SYM(aot_intrinsic_i64_rem_s), \
  111. REG_SYM(aot_intrinsic_i64_rem_u), \
  112. REG_SYM(aot_intrinsic_i64_bit_or), \
  113. REG_SYM(aot_intrinsic_i64_bit_and), \
  114. REG_SYM(aot_intrinsic_i64_mul), \
  115. REG_SYM(aot_intrinsic_i64_shl), \
  116. REG_SYM(aot_intrinsic_i64_shr_s), \
  117. REG_SYM(aot_intrinsic_i64_shr_u), \
  118. REG_SYM(aot_intrinsic_i32_div_s), \
  119. REG_SYM(aot_intrinsic_i32_div_u), \
  120. REG_SYM(aot_intrinsic_i32_rem_s), \
  121. REG_SYM(aot_intrinsic_i32_rem_u),
  122. #else
  123. #define REG_INTRINSIC_SYM()
  124. #endif
  125. #if WASM_ENABLE_STATIC_PGO != 0
  126. #define REG_LLVM_PGO_SYM() \
  127. { "__llvm_profile_instrument_target", llvm_profile_instrument_target }, \
  128. { "__llvm_profile_instrument_memop", llvm_profile_instrument_memop },
  129. #else
  130. #define REG_LLVM_PGO_SYM()
  131. #endif
  132. #if WASM_ENABLE_GC != 0
  133. #define REG_GC_SYM() \
  134. REG_SYM(aot_array_init_with_data), \
  135. REG_SYM(aot_create_func_obj), \
  136. REG_SYM(aot_obj_is_instance_of), \
  137. REG_SYM(aot_func_type_is_super_of), \
  138. REG_SYM(aot_rtt_type_new), \
  139. REG_SYM(wasm_array_obj_copy), \
  140. REG_SYM(wasm_array_obj_new), \
  141. REG_SYM(wasm_externref_obj_to_internal_obj), \
  142. REG_SYM(wasm_internal_obj_to_externref_obj), \
  143. REG_SYM(wasm_obj_is_type_of), \
  144. REG_SYM(wasm_struct_obj_new),
  145. #else
  146. #define REG_GC_SYM()
  147. #endif
  148. #if WASM_ENABLE_STRINGREF != 0
  149. #define REG_STRINGREF_SYM() \
  150. REG_SYM(wasm_stringref_obj_new), \
  151. REG_SYM(wasm_stringview_wtf8_obj_new), \
  152. REG_SYM(wasm_stringview_wtf16_obj_new), \
  153. REG_SYM(wasm_stringview_iter_obj_new), \
  154. REG_SYM(wasm_string_destroy), \
  155. REG_SYM(wasm_string_new_const), \
  156. REG_SYM(wasm_string_new_with_encoding), \
  157. REG_SYM(wasm_string_measure), \
  158. REG_SYM(wasm_string_wtf16_get_length), \
  159. REG_SYM(wasm_string_encode), \
  160. REG_SYM(wasm_string_concat), \
  161. REG_SYM(wasm_string_eq), \
  162. REG_SYM(wasm_string_is_usv_sequence), \
  163. REG_SYM(wasm_string_create_view), \
  164. REG_SYM(wasm_string_advance), \
  165. REG_SYM(wasm_string_slice), \
  166. REG_SYM(wasm_string_get_wtf16_codeunit),\
  167. REG_SYM(wasm_string_next_codepoint), \
  168. REG_SYM(wasm_string_rewind), \
  169. REG_SYM(wasm_string_dump),
  170. #else
  171. #define REG_STRINGREF_SYM()
  172. #endif
  173. #if WASM_ENABLE_SHARED_HEAP != 0
  174. #define REG_SHARED_HEAP_SYM() \
  175. REG_SYM(wasm_runtime_check_and_update_last_used_shared_heap),
  176. #else
  177. #define REG_SHARED_HEAP_SYM()
  178. #endif
  179. #define REG_COMMON_SYMBOLS \
  180. REG_SYM(aot_set_exception_with_id), \
  181. REG_SYM(aot_invoke_native), \
  182. REG_SYM(aot_call_indirect), \
  183. REG_SYM(aot_enlarge_memory), \
  184. REG_SYM(aot_set_exception), \
  185. REG_SYM(aot_check_app_addr_and_convert),\
  186. REG_SYM(wasm_runtime_quick_invoke_c_api_native),\
  187. { "memset", (void*)aot_memset }, \
  188. { "memmove", (void*)aot_memmove }, \
  189. { "memcpy", (void*)aot_memmove }, \
  190. { "sqrt", (void*)aot_sqrt }, \
  191. { "sqrtf", (void*)aot_sqrtf }, \
  192. REG_SYM(fmin), \
  193. REG_SYM(fminf), \
  194. REG_SYM(fmax), \
  195. REG_SYM(fmaxf), \
  196. REG_SYM(ceil), \
  197. REG_SYM(ceilf), \
  198. REG_SYM(floor), \
  199. REG_SYM(floorf), \
  200. REG_SYM(trunc), \
  201. REG_SYM(truncf), \
  202. REG_SYM(rint), \
  203. REG_SYM(rintf), \
  204. REG_BULK_MEMORY_SYM() \
  205. REG_ATOMIC_WAIT_SYM() \
  206. REG_REF_TYPES_SYM() \
  207. REG_AOT_TRACE_SYM() \
  208. REG_INTRINSIC_SYM() \
  209. REG_LLVM_PGO_SYM() \
  210. REG_GC_SYM() \
  211. REG_STRINGREF_SYM() \
  212. REG_SHARED_HEAP_SYM() \
  213. #define CHECK_RELOC_OFFSET(data_size) do { \
  214. if (!check_reloc_offset(target_section_size, \
  215. reloc_offset, data_size, \
  216. error_buf, error_buf_size)) \
  217. return false; \
  218. } while (0)
  219. SymbolMap *
  220. get_target_symbol_map(uint32 *sym_num);
  221. uint32
  222. get_plt_table_size(void);
  223. void
  224. init_plt_table(uint8 *plt);
  225. void
  226. get_current_target(char *target_buf, uint32 target_buf_size);
  227. bool
  228. apply_relocation(AOTModule *module,
  229. uint8 *target_section_addr, uint32 target_section_size,
  230. uint64 reloc_offset, int64 reloc_addend,
  231. uint32 reloc_type, void *symbol_addr, int32 symbol_index,
  232. char *error_buf, uint32 error_buf_size);
  233. /* clang-format off */
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* end of _AOT_RELOC_H_ */