main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include "bh_platform.h"
  7. #include "bh_read_file.h"
  8. #include "wasm_export.h"
  9. #include "aot_export.h"
  10. #if BH_HAS_DLFCN
  11. #include <dlfcn.h>
  12. typedef uint32 (*get_native_lib_func)(char **p_module_name,
  13. NativeSymbol **p_native_symbols);
  14. static uint32
  15. load_and_register_native_libs(const char **native_lib_list,
  16. uint32 native_lib_count,
  17. void **native_handle_list)
  18. {
  19. uint32 i, native_handle_count = 0, n_native_symbols;
  20. NativeSymbol *native_symbols;
  21. char *module_name;
  22. void *handle;
  23. for (i = 0; i < native_lib_count; i++) {
  24. /* open the native library */
  25. if (!(handle = dlopen(native_lib_list[i], RTLD_NOW | RTLD_GLOBAL))
  26. && !(handle = dlopen(native_lib_list[i], RTLD_LAZY))) {
  27. LOG_WARNING("warning: failed to load native library %s",
  28. native_lib_list[i]);
  29. continue;
  30. }
  31. /* lookup get_native_lib func */
  32. get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
  33. if (!get_native_lib) {
  34. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  35. "from native lib %s",
  36. native_lib_list[i]);
  37. dlclose(handle);
  38. continue;
  39. }
  40. n_native_symbols = get_native_lib(&module_name, &native_symbols);
  41. /* register native symbols */
  42. if (!(n_native_symbols > 0 && module_name && native_symbols
  43. && wasm_runtime_register_natives(module_name, native_symbols,
  44. n_native_symbols))) {
  45. LOG_WARNING("warning: failed to register native lib %s",
  46. native_lib_list[i]);
  47. dlclose(handle);
  48. continue;
  49. }
  50. native_handle_list[native_handle_count++] = handle;
  51. }
  52. return native_handle_count;
  53. }
  54. static void
  55. unregister_and_unload_native_libs(uint32 native_lib_count,
  56. void **native_handle_list)
  57. {
  58. uint32 i, n_native_symbols;
  59. NativeSymbol *native_symbols;
  60. char *module_name;
  61. void *handle;
  62. for (i = 0; i < native_lib_count; i++) {
  63. handle = native_handle_list[i];
  64. /* lookup get_native_lib func */
  65. get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
  66. if (!get_native_lib) {
  67. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  68. "from native lib %p",
  69. handle);
  70. continue;
  71. }
  72. n_native_symbols = get_native_lib(&module_name, &native_symbols);
  73. if (n_native_symbols == 0 || module_name == NULL
  74. || native_symbols == NULL) {
  75. LOG_WARNING("warning: get_native_lib returned different values for "
  76. "native lib %p",
  77. handle);
  78. continue;
  79. }
  80. /* unregister native symbols */
  81. if (!wasm_runtime_unregister_natives(module_name, native_symbols)) {
  82. LOG_WARNING("warning: failed to unregister native lib %p", handle);
  83. continue;
  84. }
  85. dlclose(handle);
  86. }
  87. }
  88. #endif
  89. /* clang-format off */
  90. static void
  91. print_help()
  92. {
  93. printf("Usage: wamrc [options] -o output_file wasm_file\n");
  94. printf(" --target=<arch-name> Set the target arch, which has the general format: <arch><sub>\n");
  95. printf(" <arch> = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n");
  96. printf(" riscv64, riscv32.\n");
  97. printf(" Default is host arch, e.g. x86_64\n");
  98. printf(" <sub> = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n");
  99. printf(" Use --target=help to list supported targets\n");
  100. printf(" --target-abi=<abi> Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n");
  101. printf(" Default is gnu if target isn't riscv64 or riscv32\n");
  102. printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n");
  103. printf(" Use --target-abi=help to list all the ABI supported\n");
  104. printf(" --cpu=<cpu> Set the target CPU (default: host CPU, e.g. skylake)\n");
  105. printf(" Use --cpu=help to list all the CPU supported\n");
  106. printf(" --cpu-features=<features> Enable or disable the CPU features\n");
  107. printf(" Use +feature to enable a feature, or -feature to disable it\n");
  108. printf(" For example, --cpu-features=+feature1,-feature2\n");
  109. printf(" Use --cpu-features=+help to list all the features supported\n");
  110. printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n");
  111. printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n");
  112. printf(" -sgx Generate code for SGX platform (Intel Software Guard Extensions)\n");
  113. printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n");
  114. printf(" by default it is disabled in all 64-bit platforms except SGX and\n");
  115. printf(" in these platforms runtime does bounds checks with hardware trap,\n");
  116. printf(" and by default it is enabled in all 32-bit platforms\n");
  117. printf(" CAVEAT: --bounds-checks=0 enables some optimizations\n");
  118. printf(" which make the compiled AOT module incompatible\n");
  119. printf(" with a runtime without the hardware bounds checks.\n");
  120. printf(" --stack-bounds-checks=1/0 Enable or disable the bounds checks for native stack:\n");
  121. printf(" if the option isn't set, the status is same as `--bounds-check`,\n");
  122. printf(" if the option is set:\n");
  123. printf(" (1) it is always enabled when `--bounds-checks` is enabled,\n");
  124. printf(" (2) else it is enabled/disabled according to the option value\n");
  125. printf(" --stack-usage=<file> Generate a stack-usage file.\n");
  126. printf(" Similarly to `clang -fstack-usage`.\n");
  127. printf(" --format=<format> Specifies the format of the output file\n");
  128. printf(" The format supported:\n");
  129. printf(" aot (default) AoT file\n");
  130. printf(" object Native object file\n");
  131. printf(" llvmir-unopt Unoptimized LLVM IR\n");
  132. printf(" llvmir-opt Optimized LLVM IR\n");
  133. printf(" --disable-bulk-memory Disable the MVP bulk memory feature\n");
  134. printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n");
  135. printf(" thread-mgr will be enabled automatically\n");
  136. printf(" --enable-tail-call Enable the post-MVP tail call feature\n");
  137. printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n");
  138. printf(" currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
  139. printf(" and by default it is enabled in them and disabled in other targets\n");
  140. printf(" --disable-ref-types Disable the MVP reference types feature, it will be disabled forcibly if\n");
  141. printf(" GC is enabled\n");
  142. printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
  143. printf(" --enable-dump-call-stack Enable stack trace feature\n");
  144. printf(" --enable-perf-profiling Enable function performance profiling\n");
  145. printf(" --enable-memory-profiling Enable memory usage profiling\n");
  146. printf(" --xip A shorthand of --enalbe-indirect-mode --disable-llvm-intrinsics\n");
  147. printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n");
  148. printf(" --enable-gc Enalbe GC (Garbage Collection) feature\n");
  149. printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
  150. printf(" --enable-builtin-intrinsics=<flags>\n");
  151. printf(" Enable the specified built-in intrinsics, it will override the default\n");
  152. printf(" settings. It only takes effect when --disable-llvm-intrinsics is set.\n");
  153. printf(" Available flags: all, i32.common, i64.common, f32.common, f64.common,\n");
  154. printf(" i32.clz, i32.ctz, etc, refer to doc/xip.md for full list\n");
  155. printf(" Use comma to separate, please refer to doc/xip.md for full list.\n");
  156. printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
  157. printf(" --enable-llvm-pgo Enable LLVM PGO (Profile-Guided Optimization)\n");
  158. printf(" --enable-llvm-passes=<passes>\n");
  159. printf(" Enable the specified LLVM passes, using comma to separate\n");
  160. printf(" --use-prof-file=<file> Use profile file collected by LLVM PGO (Profile-Guided Optimization)\n");
  161. printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of linear memory,\n");
  162. printf(" only available on linux x86-64, which may improve performance,\n");
  163. printf(" flags can be: i32.load, i64.load, f32.load, f64.load, v128.load,\n");
  164. printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n");
  165. printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
  166. printf(" and --enable-segue means all flags are added.\n");
  167. printf(" --emit-custom-sections=<section names>\n");
  168. printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
  169. printf(" multiple names, e.g.\n");
  170. printf(" --emit-custom-sections=section1,section2,sectionN\n");
  171. #if BH_HAS_DLFCN
  172. printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
  173. printf(" are shared object (.so) files, for example:\n");
  174. printf(" --native-lib=test1.so --native-lib=test2.so\n");
  175. #endif
  176. printf(" --invoke-c-api-import Treat unknown import function as wasm-c-api import function and\n");
  177. printf(" quick call it from AOT code\n");
  178. #if WASM_ENABLE_LINUX_PERF != 0
  179. printf(" --enable-linux-perf Enable linux perf support\n");
  180. #endif
  181. printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
  182. printf(" --version Show version information\n");
  183. printf("Examples: wamrc -o test.aot test.wasm\n");
  184. printf(" wamrc --target=i386 -o test.aot test.wasm\n");
  185. printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
  186. printf(" wamrc --target-abi=help\n");
  187. printf(" wamrc --target=x86_64 --cpu=help\n");
  188. }
  189. /* clang-format on */
  190. #define PRINT_HELP_AND_EXIT() \
  191. do { \
  192. print_help(); \
  193. goto fail0; \
  194. } while (0)
  195. /**
  196. * Split a string into an array of strings
  197. * Returns NULL on failure
  198. * Memory must be freed by caller
  199. * Based on: http://stackoverflow.com/a/11198630/471795
  200. */
  201. static char **
  202. split_string(char *str, int *count, const char *delimer)
  203. {
  204. char **res = NULL, **res1;
  205. char *p;
  206. int idx = 0;
  207. /* split string and append tokens to 'res' */
  208. do {
  209. p = strtok(str, delimer);
  210. str = NULL;
  211. res1 = res;
  212. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  213. if (res == NULL) {
  214. free(res1);
  215. return NULL;
  216. }
  217. res[idx++] = p;
  218. } while (p);
  219. /**
  220. * Due to the section name,
  221. * res[0] might contain a '\' to indicate a space
  222. * func\name -> func name
  223. */
  224. p = strchr(res[0], '\\');
  225. while (p) {
  226. *p = ' ';
  227. p = strchr(p, '\\');
  228. }
  229. if (count) {
  230. *count = idx - 1;
  231. }
  232. return res;
  233. }
  234. static uint32
  235. resolve_segue_flags(char *str_flags)
  236. {
  237. uint32 segue_flags = 0;
  238. int32 flag_count, i;
  239. char **flag_list;
  240. flag_list = split_string(str_flags, &flag_count, ",");
  241. if (flag_list) {
  242. for (i = 0; i < flag_count; i++) {
  243. if (!strcmp(flag_list[i], "i32.load")) {
  244. segue_flags |= 1 << 0;
  245. }
  246. else if (!strcmp(flag_list[i], "i64.load")) {
  247. segue_flags |= 1 << 1;
  248. }
  249. else if (!strcmp(flag_list[i], "f32.load")) {
  250. segue_flags |= 1 << 2;
  251. }
  252. else if (!strcmp(flag_list[i], "f64.load")) {
  253. segue_flags |= 1 << 3;
  254. }
  255. else if (!strcmp(flag_list[i], "v128.load")) {
  256. segue_flags |= 1 << 4;
  257. }
  258. else if (!strcmp(flag_list[i], "i32.store")) {
  259. segue_flags |= 1 << 8;
  260. }
  261. else if (!strcmp(flag_list[i], "i64.store")) {
  262. segue_flags |= 1 << 9;
  263. }
  264. else if (!strcmp(flag_list[i], "f32.store")) {
  265. segue_flags |= 1 << 10;
  266. }
  267. else if (!strcmp(flag_list[i], "f64.store")) {
  268. segue_flags |= 1 << 11;
  269. }
  270. else if (!strcmp(flag_list[i], "v128.store")) {
  271. segue_flags |= 1 << 12;
  272. }
  273. else {
  274. /* invalid flag */
  275. segue_flags = (uint32)-1;
  276. break;
  277. }
  278. }
  279. free(flag_list);
  280. }
  281. return segue_flags;
  282. }
  283. /* When print help info for target/cpu/target-abi/cpu-features, load this dummy
  284. * wasm file content rather than from an input file, the dummy wasm file content
  285. * is: magic header + version number */
  286. static unsigned char dummy_wasm_file[8] = { 0x00, 0x61, 0x73, 0x6D,
  287. 0x01, 0x00, 0x00, 0x00 };
  288. int
  289. main(int argc, char *argv[])
  290. {
  291. char *wasm_file_name = NULL, *out_file_name = NULL;
  292. uint8 *wasm_file = NULL;
  293. uint32 wasm_file_size;
  294. wasm_module_t wasm_module = NULL;
  295. aot_comp_data_t comp_data = NULL;
  296. aot_comp_context_t comp_ctx = NULL;
  297. RuntimeInitArgs init_args;
  298. AOTCompOption option = { 0 };
  299. char error_buf[128];
  300. int log_verbose_level = 2;
  301. bool sgx_mode = false, size_level_set = false, use_dummy_wasm = false;
  302. int exit_status = EXIT_FAILURE;
  303. #if BH_HAS_DLFCN
  304. const char *native_lib_list[8] = { NULL };
  305. uint32 native_lib_count = 0;
  306. void *native_handle_list[8] = { NULL };
  307. uint32 native_handle_count = 0;
  308. #endif
  309. #if WASM_ENABLE_LINUX_PERF != 0
  310. bool enable_linux_perf = false;
  311. #endif
  312. option.opt_level = 3;
  313. option.size_level = 3;
  314. option.output_format = AOT_FORMAT_FILE;
  315. /* default value, enable or disable depends on the platform */
  316. option.bounds_checks = 2;
  317. /* default value, enable or disable depends on the platform */
  318. option.stack_bounds_checks = 2;
  319. option.enable_simd = true;
  320. option.enable_aux_stack_check = true;
  321. option.enable_bulk_memory = true;
  322. option.enable_ref_types = true;
  323. option.enable_gc = false;
  324. /* Process options */
  325. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  326. if (!strcmp(argv[0], "-o")) {
  327. argc--, argv++;
  328. if (argc < 2)
  329. PRINT_HELP_AND_EXIT();
  330. out_file_name = argv[0];
  331. }
  332. else if (!strncmp(argv[0], "--target=", 9)) {
  333. if (argv[0][9] == '\0')
  334. PRINT_HELP_AND_EXIT();
  335. option.target_arch = argv[0] + 9;
  336. if (!strcmp(option.target_arch, "help")) {
  337. use_dummy_wasm = true;
  338. }
  339. }
  340. else if (!strncmp(argv[0], "--target-abi=", 13)) {
  341. if (argv[0][13] == '\0')
  342. PRINT_HELP_AND_EXIT();
  343. option.target_abi = argv[0] + 13;
  344. if (!strcmp(option.target_abi, "help")) {
  345. use_dummy_wasm = true;
  346. }
  347. }
  348. else if (!strncmp(argv[0], "--cpu=", 6)) {
  349. if (argv[0][6] == '\0')
  350. PRINT_HELP_AND_EXIT();
  351. option.target_cpu = argv[0] + 6;
  352. if (!strcmp(option.target_cpu, "help")) {
  353. use_dummy_wasm = true;
  354. }
  355. }
  356. else if (!strncmp(argv[0], "--cpu-features=", 15)) {
  357. if (argv[0][15] == '\0')
  358. PRINT_HELP_AND_EXIT();
  359. option.cpu_features = argv[0] + 15;
  360. if (!strcmp(option.cpu_features, "+help")) {
  361. use_dummy_wasm = true;
  362. }
  363. }
  364. else if (!strncmp(argv[0], "--opt-level=", 12)) {
  365. if (argv[0][12] == '\0')
  366. PRINT_HELP_AND_EXIT();
  367. option.opt_level = (uint32)atoi(argv[0] + 12);
  368. if (option.opt_level > 3)
  369. option.opt_level = 3;
  370. }
  371. else if (!strncmp(argv[0], "--size-level=", 13)) {
  372. if (argv[0][13] == '\0')
  373. PRINT_HELP_AND_EXIT();
  374. option.size_level = (uint32)atoi(argv[0] + 13);
  375. if (option.size_level > 3)
  376. option.size_level = 3;
  377. size_level_set = true;
  378. }
  379. else if (!strcmp(argv[0], "-sgx")) {
  380. sgx_mode = true;
  381. }
  382. else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
  383. option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
  384. }
  385. else if (!strncmp(argv[0], "--stack-bounds-checks=", 22)) {
  386. option.stack_bounds_checks = (atoi(argv[0] + 22) == 1) ? 1 : 0;
  387. }
  388. else if (!strncmp(argv[0], "--stack-usage=", 14)) {
  389. option.stack_usage_file = argv[0] + 14;
  390. }
  391. else if (!strncmp(argv[0], "--format=", 9)) {
  392. if (argv[0][9] == '\0')
  393. PRINT_HELP_AND_EXIT();
  394. if (!strcmp(argv[0] + 9, "aot"))
  395. option.output_format = AOT_FORMAT_FILE;
  396. else if (!strcmp(argv[0] + 9, "object"))
  397. option.output_format = AOT_OBJECT_FILE;
  398. else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
  399. option.output_format = AOT_LLVMIR_UNOPT_FILE;
  400. else if (!strcmp(argv[0] + 9, "llvmir-opt"))
  401. option.output_format = AOT_LLVMIR_OPT_FILE;
  402. else {
  403. printf("Invalid format %s.\n", argv[0] + 9);
  404. PRINT_HELP_AND_EXIT();
  405. }
  406. }
  407. else if (!strncmp(argv[0], "-v=", 3)) {
  408. log_verbose_level = atoi(argv[0] + 3);
  409. if (log_verbose_level < 0 || log_verbose_level > 5)
  410. PRINT_HELP_AND_EXIT();
  411. }
  412. else if (!strcmp(argv[0], "--disable-bulk-memory")) {
  413. option.enable_bulk_memory = false;
  414. }
  415. else if (!strcmp(argv[0], "--enable-multi-thread")) {
  416. option.enable_bulk_memory = true;
  417. option.enable_thread_mgr = true;
  418. }
  419. else if (!strcmp(argv[0], "--enable-tail-call")) {
  420. option.enable_tail_call = true;
  421. }
  422. else if (!strcmp(argv[0], "--enable-simd")) {
  423. /* obsolete option, kept for compatibility */
  424. option.enable_simd = true;
  425. }
  426. else if (!strcmp(argv[0], "--disable-simd")) {
  427. option.enable_simd = false;
  428. }
  429. else if (!strcmp(argv[0], "--disable-ref-types")) {
  430. option.enable_ref_types = false;
  431. }
  432. else if (!strcmp(argv[0], "--disable-aux-stack-check")) {
  433. option.enable_aux_stack_check = false;
  434. }
  435. else if (!strcmp(argv[0], "--enable-dump-call-stack")) {
  436. option.enable_aux_stack_frame = true;
  437. }
  438. else if (!strcmp(argv[0], "--enable-perf-profiling")) {
  439. option.enable_aux_stack_frame = true;
  440. option.enable_perf_profiling = true;
  441. }
  442. else if (!strcmp(argv[0], "--enable-memory-profiling")) {
  443. option.enable_memory_profiling = true;
  444. option.enable_stack_estimation = true;
  445. }
  446. else if (!strcmp(argv[0], "--xip")) {
  447. option.is_indirect_mode = true;
  448. option.disable_llvm_intrinsics = true;
  449. }
  450. else if (!strcmp(argv[0], "--enable-indirect-mode")) {
  451. option.is_indirect_mode = true;
  452. }
  453. else if (!strcmp(argv[0], "--enable-gc")) {
  454. option.enable_aux_stack_frame = true;
  455. option.enable_gc = true;
  456. }
  457. else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
  458. option.disable_llvm_intrinsics = true;
  459. }
  460. else if (!strncmp(argv[0], "--enable-builtin-intrinsics=", 28)) {
  461. if (argv[0][28] == '\0')
  462. PRINT_HELP_AND_EXIT();
  463. option.builtin_intrinsics = argv[0] + 28;
  464. }
  465. else if (!strcmp(argv[0], "--disable-llvm-lto")) {
  466. option.disable_llvm_lto = true;
  467. }
  468. else if (!strcmp(argv[0], "--enable-llvm-pgo")) {
  469. option.enable_llvm_pgo = true;
  470. }
  471. else if (!strncmp(argv[0], "--enable-llvm-passes=", 21)) {
  472. if (argv[0][21] == '\0')
  473. PRINT_HELP_AND_EXIT();
  474. option.llvm_passes = argv[0] + 21;
  475. }
  476. else if (!strncmp(argv[0], "--use-prof-file=", 16)) {
  477. if (argv[0][16] == '\0')
  478. PRINT_HELP_AND_EXIT();
  479. option.use_prof_file = argv[0] + 16;
  480. }
  481. else if (!strcmp(argv[0], "--enable-segue")) {
  482. /* all flags are enabled */
  483. option.segue_flags = 0x1F1F;
  484. }
  485. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  486. option.segue_flags = resolve_segue_flags(argv[0] + 15);
  487. if (option.segue_flags == (uint32)-1)
  488. PRINT_HELP_AND_EXIT();
  489. }
  490. else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
  491. int len = 0;
  492. if (option.custom_sections) {
  493. free(option.custom_sections);
  494. }
  495. option.custom_sections = split_string(argv[0] + 23, &len, ",");
  496. if (!option.custom_sections) {
  497. printf("Failed to process emit-custom-sections: alloc "
  498. "memory failed\n");
  499. PRINT_HELP_AND_EXIT();
  500. }
  501. option.custom_sections_count = len;
  502. }
  503. #if BH_HAS_DLFCN
  504. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  505. if (argv[0][13] == '\0')
  506. PRINT_HELP_AND_EXIT();
  507. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  508. printf("Only allow max native lib number %d\n",
  509. (int)(sizeof(native_lib_list) / sizeof(char *)));
  510. goto fail0;
  511. }
  512. native_lib_list[native_lib_count++] = argv[0] + 13;
  513. }
  514. #endif
  515. else if (!strcmp(argv[0], "--invoke-c-api-import")) {
  516. option.quick_invoke_c_api_import = true;
  517. }
  518. #if WASM_ENABLE_LINUX_PERF != 0
  519. else if (!strcmp(argv[0], "--enable-linux-perf")) {
  520. enable_linux_perf = true;
  521. }
  522. #endif
  523. else if (!strcmp(argv[0], "--version")) {
  524. uint32 major, minor, patch;
  525. wasm_runtime_get_version(&major, &minor, &patch);
  526. printf("wamrc %u.%u.%u\n", major, minor, patch);
  527. return 0;
  528. }
  529. else
  530. PRINT_HELP_AND_EXIT();
  531. }
  532. if (!use_dummy_wasm && (argc == 0 || !out_file_name))
  533. PRINT_HELP_AND_EXIT();
  534. if (!size_level_set) {
  535. /**
  536. * Set opt level to 1 by default for Windows and MacOS as
  537. * they can not memory map out 0-2GB memory and might not
  538. * be able to meet the requirements of some AOT relocation
  539. * operations.
  540. */
  541. if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
  542. LOG_VERBOSE("Set size level to 1 for Windows AOT file");
  543. option.size_level = 1;
  544. }
  545. #if defined(_WIN32) || defined(_WIN32_) || defined(__APPLE__) \
  546. || defined(__MACH__)
  547. if (!option.target_abi) {
  548. LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
  549. option.size_level = 1;
  550. }
  551. #endif
  552. }
  553. if (sgx_mode) {
  554. option.size_level = 1;
  555. option.is_sgx_platform = true;
  556. }
  557. if (option.enable_gc) {
  558. option.enable_ref_types = false;
  559. }
  560. if (!use_dummy_wasm) {
  561. wasm_file_name = argv[0];
  562. if (!strcmp(wasm_file_name, out_file_name)) {
  563. printf("Error: input file and output file are the same");
  564. return -1;
  565. }
  566. }
  567. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  568. init_args.mem_alloc_type = Alloc_With_Allocator;
  569. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  570. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  571. init_args.mem_alloc_option.allocator.free_func = free;
  572. #if WASM_ENABLE_LINUX_PERF != 0
  573. init_args.enable_linux_perf = enable_linux_perf;
  574. #endif
  575. /* initialize runtime environment */
  576. if (!wasm_runtime_full_init(&init_args)) {
  577. printf("Init runtime environment failed.\n");
  578. return -1;
  579. }
  580. bh_log_set_verbose_level(log_verbose_level);
  581. #if BH_HAS_DLFCN
  582. bh_print_time("Begin to load native libs");
  583. native_handle_count = load_and_register_native_libs(
  584. native_lib_list, native_lib_count, native_handle_list);
  585. #endif
  586. bh_print_time("Begin to load wasm file");
  587. if (use_dummy_wasm) {
  588. /* load WASM byte buffer from dummy buffer */
  589. wasm_file_size = sizeof(dummy_wasm_file);
  590. wasm_file = dummy_wasm_file;
  591. }
  592. else {
  593. /* load WASM byte buffer from WASM bin file */
  594. if (!(wasm_file = (uint8 *)bh_read_file_to_buffer(wasm_file_name,
  595. &wasm_file_size)))
  596. goto fail1;
  597. }
  598. if (wasm_file_size >= 4 /* length of MAGIC NUMBER */
  599. && get_package_type(wasm_file, wasm_file_size)
  600. != Wasm_Module_Bytecode) {
  601. printf("Invalid wasm file: magic header not detected\n");
  602. goto fail2;
  603. }
  604. /* load WASM module */
  605. if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
  606. sizeof(error_buf)))) {
  607. printf("%s\n", error_buf);
  608. goto fail2;
  609. }
  610. if (!(comp_data = aot_create_comp_data(wasm_module, option.target_arch,
  611. option.enable_gc))) {
  612. printf("%s\n", aot_get_last_error());
  613. goto fail3;
  614. }
  615. #if WASM_ENABLE_DEBUG_AOT != 0
  616. if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
  617. printf("%s:create dwarf extractor failed\n", wasm_file_name);
  618. }
  619. #endif
  620. bh_print_time("Begin to create compile context");
  621. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  622. printf("%s\n", aot_get_last_error());
  623. goto fail4;
  624. }
  625. bh_print_time("Begin to compile");
  626. if (!aot_compile_wasm(comp_ctx)) {
  627. printf("%s\n", aot_get_last_error());
  628. goto fail5;
  629. }
  630. switch (option.output_format) {
  631. case AOT_LLVMIR_UNOPT_FILE:
  632. case AOT_LLVMIR_OPT_FILE:
  633. if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
  634. printf("%s\n", aot_get_last_error());
  635. goto fail5;
  636. }
  637. break;
  638. case AOT_OBJECT_FILE:
  639. if (!aot_emit_object_file(comp_ctx, out_file_name)) {
  640. printf("%s\n", aot_get_last_error());
  641. goto fail5;
  642. }
  643. break;
  644. case AOT_FORMAT_FILE:
  645. if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
  646. printf("%s\n", aot_get_last_error());
  647. goto fail5;
  648. }
  649. break;
  650. default:
  651. break;
  652. }
  653. bh_print_time("Compile end");
  654. printf("Compile success, file %s was generated.\n", out_file_name);
  655. exit_status = EXIT_SUCCESS;
  656. fail5:
  657. /* Destroy compiler context */
  658. aot_destroy_comp_context(comp_ctx);
  659. fail4:
  660. /* Destroy compile data */
  661. aot_destroy_comp_data(comp_data);
  662. fail3:
  663. /* Unload WASM module */
  664. wasm_runtime_unload(wasm_module);
  665. fail2:
  666. /* free the file buffer */
  667. if (!use_dummy_wasm) {
  668. wasm_runtime_free(wasm_file);
  669. }
  670. fail1:
  671. #if BH_HAS_DLFCN
  672. unregister_and_unload_native_libs(native_handle_count, native_handle_list);
  673. #endif
  674. /* Destroy runtime environment */
  675. wasm_runtime_destroy();
  676. fail0:
  677. /* free option.custom_sections */
  678. if (option.custom_sections) {
  679. free(option.custom_sections);
  680. }
  681. bh_print_time("wamrc return");
  682. return exit_status;
  683. }