main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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\n");
  141. printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
  142. printf(" --enable-dump-call-stack Enable stack trace feature\n");
  143. printf(" --enable-perf-profiling Enable function performance profiling\n");
  144. printf(" --enable-memory-profiling Enable memory usage profiling\n");
  145. printf(" --xip A shorthand of --enable-indirect-mode --disable-llvm-intrinsics\n");
  146. printf(" --enable-indirect-mode Enable call function through symbol table but not direct call\n");
  147. printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
  148. printf(" --enable-builtin-intrinsics=<flags>\n");
  149. printf(" Enable the specified built-in intrinsics, it will override the default\n");
  150. printf(" settings. It only takes effect when --disable-llvm-intrinsics is set.\n");
  151. printf(" Available flags: all, i32.common, i64.common, f32.common, f64.common,\n");
  152. printf(" i32.clz, i32.ctz, etc, refer to doc/xip.md for full list\n");
  153. printf(" Use comma to separate, please refer to doc/xip.md for full list.\n");
  154. printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
  155. printf(" --enable-llvm-pgo Enable LLVM PGO (Profile-Guided Optimization)\n");
  156. printf(" --enable-llvm-passes=<passes>\n");
  157. printf(" Enable the specified LLVM passes, using comma to separate\n");
  158. printf(" --use-prof-file=<file> Use profile file collected by LLVM PGO (Profile-Guided Optimization)\n");
  159. printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of linear memory,\n");
  160. printf(" only available on linux/linux-sgx x86-64, which may improve performance,\n");
  161. printf(" flags can be: i32.load, i64.load, f32.load, f64.load, v128.load,\n");
  162. printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n");
  163. printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
  164. printf(" and --enable-segue means all flags are added.\n");
  165. printf(" --emit-custom-sections=<section names>\n");
  166. printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
  167. printf(" multiple names, e.g.\n");
  168. printf(" --emit-custom-sections=section1,section2,sectionN\n");
  169. #if BH_HAS_DLFCN
  170. printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
  171. printf(" are shared object (.so) files, for example:\n");
  172. printf(" --native-lib=test1.so --native-lib=test2.so\n");
  173. #endif
  174. printf(" --invoke-c-api-import Treat unknown import function as wasm-c-api import function and\n");
  175. printf(" quick call it from AOT code\n");
  176. #if WASM_ENABLE_LINUX_PERF != 0
  177. printf(" --enable-linux-perf Enable linux perf support\n");
  178. #endif
  179. printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
  180. printf(" --version Show version information\n");
  181. printf("Examples: wamrc -o test.aot test.wasm\n");
  182. printf(" wamrc --target=i386 -o test.aot test.wasm\n");
  183. printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
  184. printf(" wamrc --target-abi=help\n");
  185. printf(" wamrc --target=x86_64 --cpu=help\n");
  186. }
  187. /* clang-format on */
  188. #define PRINT_HELP_AND_EXIT() \
  189. do { \
  190. print_help(); \
  191. goto fail0; \
  192. } while (0)
  193. /**
  194. * Split a string into an array of strings
  195. * Returns NULL on failure
  196. * Memory must be freed by caller
  197. * Based on: http://stackoverflow.com/a/11198630/471795
  198. */
  199. static char **
  200. split_string(char *str, int *count, const char *delimer)
  201. {
  202. char **res = NULL, **res1;
  203. char *p;
  204. int idx = 0;
  205. /* split string and append tokens to 'res' */
  206. do {
  207. p = strtok(str, delimer);
  208. str = NULL;
  209. res1 = res;
  210. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  211. if (res == NULL) {
  212. free(res1);
  213. return NULL;
  214. }
  215. res[idx++] = p;
  216. } while (p);
  217. /**
  218. * Due to the section name,
  219. * res[0] might contain a '\' to indicate a space
  220. * func\name -> func name
  221. */
  222. p = strchr(res[0], '\\');
  223. while (p) {
  224. *p = ' ';
  225. p = strchr(p, '\\');
  226. }
  227. if (count) {
  228. *count = idx - 1;
  229. }
  230. return res;
  231. }
  232. static uint32
  233. resolve_segue_flags(char *str_flags)
  234. {
  235. uint32 segue_flags = 0;
  236. int32 flag_count, i;
  237. char **flag_list;
  238. flag_list = split_string(str_flags, &flag_count, ",");
  239. if (flag_list) {
  240. for (i = 0; i < flag_count; i++) {
  241. if (!strcmp(flag_list[i], "i32.load")) {
  242. segue_flags |= 1 << 0;
  243. }
  244. else if (!strcmp(flag_list[i], "i64.load")) {
  245. segue_flags |= 1 << 1;
  246. }
  247. else if (!strcmp(flag_list[i], "f32.load")) {
  248. segue_flags |= 1 << 2;
  249. }
  250. else if (!strcmp(flag_list[i], "f64.load")) {
  251. segue_flags |= 1 << 3;
  252. }
  253. else if (!strcmp(flag_list[i], "v128.load")) {
  254. segue_flags |= 1 << 4;
  255. }
  256. else if (!strcmp(flag_list[i], "i32.store")) {
  257. segue_flags |= 1 << 8;
  258. }
  259. else if (!strcmp(flag_list[i], "i64.store")) {
  260. segue_flags |= 1 << 9;
  261. }
  262. else if (!strcmp(flag_list[i], "f32.store")) {
  263. segue_flags |= 1 << 10;
  264. }
  265. else if (!strcmp(flag_list[i], "f64.store")) {
  266. segue_flags |= 1 << 11;
  267. }
  268. else if (!strcmp(flag_list[i], "v128.store")) {
  269. segue_flags |= 1 << 12;
  270. }
  271. else {
  272. /* invalid flag */
  273. segue_flags = (uint32)-1;
  274. break;
  275. }
  276. }
  277. free(flag_list);
  278. }
  279. return segue_flags;
  280. }
  281. /* When print help info for target/cpu/target-abi/cpu-features, load this dummy
  282. * wasm file content rather than from an input file, the dummy wasm file content
  283. * is: magic header + version number */
  284. static unsigned char dummy_wasm_file[8] = { 0x00, 0x61, 0x73, 0x6D,
  285. 0x01, 0x00, 0x00, 0x00 };
  286. int
  287. main(int argc, char *argv[])
  288. {
  289. char *wasm_file_name = NULL, *out_file_name = NULL;
  290. uint8 *wasm_file = NULL;
  291. uint32 wasm_file_size;
  292. wasm_module_t wasm_module = NULL;
  293. aot_comp_data_t comp_data = NULL;
  294. aot_comp_context_t comp_ctx = NULL;
  295. RuntimeInitArgs init_args;
  296. AOTCompOption option = { 0 };
  297. char error_buf[128];
  298. int log_verbose_level = 2;
  299. bool sgx_mode = false, size_level_set = false, use_dummy_wasm = false;
  300. int exit_status = EXIT_FAILURE;
  301. #if BH_HAS_DLFCN
  302. const char *native_lib_list[8] = { NULL };
  303. uint32 native_lib_count = 0;
  304. void *native_handle_list[8] = { NULL };
  305. uint32 native_handle_count = 0;
  306. #endif
  307. #if WASM_ENABLE_LINUX_PERF != 0
  308. bool enable_linux_perf = false;
  309. #endif
  310. option.opt_level = 3;
  311. option.size_level = 3;
  312. option.output_format = AOT_FORMAT_FILE;
  313. /* default value, enable or disable depends on the platform */
  314. option.bounds_checks = 2;
  315. /* default value, enable or disable depends on the platform */
  316. option.stack_bounds_checks = 2;
  317. option.enable_simd = true;
  318. option.enable_aux_stack_check = true;
  319. option.enable_bulk_memory = true;
  320. option.enable_ref_types = true;
  321. /* Process options */
  322. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  323. if (!strcmp(argv[0], "-o")) {
  324. argc--, argv++;
  325. if (argc < 2)
  326. PRINT_HELP_AND_EXIT();
  327. out_file_name = argv[0];
  328. }
  329. else if (!strncmp(argv[0], "--target=", 9)) {
  330. if (argv[0][9] == '\0')
  331. PRINT_HELP_AND_EXIT();
  332. option.target_arch = argv[0] + 9;
  333. if (!strcmp(option.target_arch, "help")) {
  334. use_dummy_wasm = true;
  335. }
  336. }
  337. else if (!strncmp(argv[0], "--target-abi=", 13)) {
  338. if (argv[0][13] == '\0')
  339. PRINT_HELP_AND_EXIT();
  340. option.target_abi = argv[0] + 13;
  341. if (!strcmp(option.target_abi, "help")) {
  342. use_dummy_wasm = true;
  343. }
  344. }
  345. else if (!strncmp(argv[0], "--cpu=", 6)) {
  346. if (argv[0][6] == '\0')
  347. PRINT_HELP_AND_EXIT();
  348. option.target_cpu = argv[0] + 6;
  349. if (!strcmp(option.target_cpu, "help")) {
  350. use_dummy_wasm = true;
  351. }
  352. }
  353. else if (!strncmp(argv[0], "--cpu-features=", 15)) {
  354. if (argv[0][15] == '\0')
  355. PRINT_HELP_AND_EXIT();
  356. option.cpu_features = argv[0] + 15;
  357. if (!strcmp(option.cpu_features, "+help")) {
  358. use_dummy_wasm = true;
  359. }
  360. }
  361. else if (!strncmp(argv[0], "--opt-level=", 12)) {
  362. if (argv[0][12] == '\0')
  363. PRINT_HELP_AND_EXIT();
  364. option.opt_level = (uint32)atoi(argv[0] + 12);
  365. if (option.opt_level > 3)
  366. option.opt_level = 3;
  367. }
  368. else if (!strncmp(argv[0], "--size-level=", 13)) {
  369. if (argv[0][13] == '\0')
  370. PRINT_HELP_AND_EXIT();
  371. option.size_level = (uint32)atoi(argv[0] + 13);
  372. if (option.size_level > 3)
  373. option.size_level = 3;
  374. size_level_set = true;
  375. }
  376. else if (!strcmp(argv[0], "-sgx")) {
  377. sgx_mode = true;
  378. }
  379. else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
  380. option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
  381. }
  382. else if (!strncmp(argv[0], "--stack-bounds-checks=", 22)) {
  383. option.stack_bounds_checks = (atoi(argv[0] + 22) == 1) ? 1 : 0;
  384. }
  385. else if (!strncmp(argv[0], "--stack-usage=", 14)) {
  386. option.stack_usage_file = argv[0] + 14;
  387. }
  388. else if (!strncmp(argv[0], "--format=", 9)) {
  389. if (argv[0][9] == '\0')
  390. PRINT_HELP_AND_EXIT();
  391. if (!strcmp(argv[0] + 9, "aot"))
  392. option.output_format = AOT_FORMAT_FILE;
  393. else if (!strcmp(argv[0] + 9, "object"))
  394. option.output_format = AOT_OBJECT_FILE;
  395. else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
  396. option.output_format = AOT_LLVMIR_UNOPT_FILE;
  397. else if (!strcmp(argv[0] + 9, "llvmir-opt"))
  398. option.output_format = AOT_LLVMIR_OPT_FILE;
  399. else {
  400. printf("Invalid format %s.\n", argv[0] + 9);
  401. PRINT_HELP_AND_EXIT();
  402. }
  403. }
  404. else if (!strncmp(argv[0], "-v=", 3)) {
  405. log_verbose_level = atoi(argv[0] + 3);
  406. if (log_verbose_level < 0 || log_verbose_level > 5)
  407. PRINT_HELP_AND_EXIT();
  408. }
  409. else if (!strcmp(argv[0], "--disable-bulk-memory")) {
  410. option.enable_bulk_memory = false;
  411. }
  412. else if (!strcmp(argv[0], "--enable-multi-thread")) {
  413. option.enable_bulk_memory = true;
  414. option.enable_thread_mgr = true;
  415. option.enable_ref_types = false;
  416. }
  417. else if (!strcmp(argv[0], "--enable-tail-call")) {
  418. option.enable_tail_call = true;
  419. }
  420. else if (!strcmp(argv[0], "--enable-simd")) {
  421. /* obsolete option, kept for compatibility */
  422. option.enable_simd = true;
  423. }
  424. else if (!strcmp(argv[0], "--disable-simd")) {
  425. option.enable_simd = false;
  426. }
  427. else if (!strcmp(argv[0], "--disable-ref-types")) {
  428. option.enable_ref_types = false;
  429. }
  430. else if (!strcmp(argv[0], "--disable-aux-stack-check")) {
  431. option.enable_aux_stack_check = false;
  432. }
  433. else if (!strcmp(argv[0], "--enable-dump-call-stack")) {
  434. option.enable_aux_stack_frame = true;
  435. }
  436. else if (!strcmp(argv[0], "--enable-perf-profiling")) {
  437. option.enable_aux_stack_frame = true;
  438. }
  439. else if (!strcmp(argv[0], "--enable-memory-profiling")) {
  440. option.enable_stack_estimation = true;
  441. }
  442. else if (!strcmp(argv[0], "--xip")) {
  443. option.is_indirect_mode = true;
  444. option.disable_llvm_intrinsics = true;
  445. }
  446. else if (!strcmp(argv[0], "--enable-indirect-mode")) {
  447. option.is_indirect_mode = true;
  448. }
  449. else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
  450. option.disable_llvm_intrinsics = true;
  451. }
  452. else if (!strncmp(argv[0], "--enable-builtin-intrinsics=", 28)) {
  453. if (argv[0][28] == '\0')
  454. PRINT_HELP_AND_EXIT();
  455. option.builtin_intrinsics = argv[0] + 28;
  456. }
  457. else if (!strcmp(argv[0], "--disable-llvm-lto")) {
  458. option.disable_llvm_lto = true;
  459. }
  460. else if (!strcmp(argv[0], "--enable-llvm-pgo")) {
  461. option.enable_llvm_pgo = true;
  462. }
  463. else if (!strncmp(argv[0], "--enable-llvm-passes=", 21)) {
  464. if (argv[0][21] == '\0')
  465. PRINT_HELP_AND_EXIT();
  466. option.llvm_passes = argv[0] + 21;
  467. }
  468. else if (!strncmp(argv[0], "--use-prof-file=", 16)) {
  469. if (argv[0][16] == '\0')
  470. PRINT_HELP_AND_EXIT();
  471. option.use_prof_file = argv[0] + 16;
  472. }
  473. else if (!strcmp(argv[0], "--enable-segue")) {
  474. /* all flags are enabled */
  475. option.segue_flags = 0x1F1F;
  476. }
  477. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  478. option.segue_flags = resolve_segue_flags(argv[0] + 15);
  479. if (option.segue_flags == (uint32)-1)
  480. PRINT_HELP_AND_EXIT();
  481. }
  482. else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
  483. int len = 0;
  484. if (option.custom_sections) {
  485. free(option.custom_sections);
  486. }
  487. option.custom_sections = split_string(argv[0] + 23, &len, ",");
  488. if (!option.custom_sections) {
  489. printf("Failed to process emit-custom-sections: alloc "
  490. "memory failed\n");
  491. PRINT_HELP_AND_EXIT();
  492. }
  493. option.custom_sections_count = len;
  494. }
  495. #if BH_HAS_DLFCN
  496. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  497. if (argv[0][13] == '\0')
  498. PRINT_HELP_AND_EXIT();
  499. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  500. printf("Only allow max native lib number %d\n",
  501. (int)(sizeof(native_lib_list) / sizeof(char *)));
  502. goto fail0;
  503. }
  504. native_lib_list[native_lib_count++] = argv[0] + 13;
  505. }
  506. #endif
  507. else if (!strcmp(argv[0], "--invoke-c-api-import")) {
  508. option.quick_invoke_c_api_import = true;
  509. }
  510. #if WASM_ENABLE_LINUX_PERF != 0
  511. else if (!strcmp(argv[0], "--enable-linux-perf")) {
  512. enable_linux_perf = true;
  513. }
  514. #endif
  515. else if (!strcmp(argv[0], "--version")) {
  516. uint32 major, minor, patch;
  517. wasm_runtime_get_version(&major, &minor, &patch);
  518. printf("wamrc %u.%u.%u\n", major, minor, patch);
  519. return 0;
  520. }
  521. else
  522. PRINT_HELP_AND_EXIT();
  523. }
  524. if (!use_dummy_wasm && (argc == 0 || !out_file_name))
  525. PRINT_HELP_AND_EXIT();
  526. if (!size_level_set) {
  527. /**
  528. * Set opt level to 1 by default for Windows and MacOS as
  529. * they can not memory map out 0-2GB memory and might not
  530. * be able to meet the requirements of some AOT relocation
  531. * operations.
  532. */
  533. if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
  534. LOG_VERBOSE("Set size level to 1 for Windows AOT file");
  535. option.size_level = 1;
  536. }
  537. #if defined(_WIN32) || defined(_WIN32_) || defined(__APPLE__) \
  538. || defined(__MACH__)
  539. if (!option.target_abi) {
  540. LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
  541. option.size_level = 1;
  542. }
  543. #endif
  544. }
  545. if (sgx_mode) {
  546. option.size_level = 1;
  547. option.is_sgx_platform = true;
  548. }
  549. if (!use_dummy_wasm) {
  550. wasm_file_name = argv[0];
  551. if (!strcmp(wasm_file_name, out_file_name)) {
  552. printf("Error: input file and output file are the same");
  553. return -1;
  554. }
  555. }
  556. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  557. init_args.mem_alloc_type = Alloc_With_Allocator;
  558. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  559. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  560. init_args.mem_alloc_option.allocator.free_func = free;
  561. #if WASM_ENABLE_LINUX_PERF != 0
  562. init_args.enable_linux_perf = enable_linux_perf;
  563. #endif
  564. /* initialize runtime environment */
  565. if (!wasm_runtime_full_init(&init_args)) {
  566. printf("Init runtime environment failed.\n");
  567. return -1;
  568. }
  569. bh_log_set_verbose_level(log_verbose_level);
  570. #if BH_HAS_DLFCN
  571. bh_print_time("Begin to load native libs");
  572. native_handle_count = load_and_register_native_libs(
  573. native_lib_list, native_lib_count, native_handle_list);
  574. #endif
  575. bh_print_time("Begin to load wasm file");
  576. if (use_dummy_wasm) {
  577. /* load WASM byte buffer from dummy buffer */
  578. wasm_file_size = sizeof(dummy_wasm_file);
  579. wasm_file = dummy_wasm_file;
  580. }
  581. else {
  582. /* load WASM byte buffer from WASM bin file */
  583. if (!(wasm_file = (uint8 *)bh_read_file_to_buffer(wasm_file_name,
  584. &wasm_file_size)))
  585. goto fail1;
  586. }
  587. if (wasm_file_size >= 4 /* length of MAGIC NUMBER */
  588. && get_package_type(wasm_file, wasm_file_size)
  589. != Wasm_Module_Bytecode) {
  590. printf("Invalid wasm file: magic header not detected\n");
  591. goto fail2;
  592. }
  593. /* load WASM module */
  594. if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
  595. sizeof(error_buf)))) {
  596. printf("%s\n", error_buf);
  597. goto fail2;
  598. }
  599. if (!(comp_data = aot_create_comp_data(wasm_module))) {
  600. printf("%s\n", aot_get_last_error());
  601. goto fail3;
  602. }
  603. #if WASM_ENABLE_DEBUG_AOT != 0
  604. if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
  605. printf("%s:create dwarf extractor failed\n", wasm_file_name);
  606. }
  607. #endif
  608. bh_print_time("Begin to create compile context");
  609. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  610. printf("%s\n", aot_get_last_error());
  611. goto fail4;
  612. }
  613. bh_print_time("Begin to compile");
  614. if (!aot_compile_wasm(comp_ctx)) {
  615. printf("%s\n", aot_get_last_error());
  616. goto fail5;
  617. }
  618. switch (option.output_format) {
  619. case AOT_LLVMIR_UNOPT_FILE:
  620. case AOT_LLVMIR_OPT_FILE:
  621. if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
  622. printf("%s\n", aot_get_last_error());
  623. goto fail5;
  624. }
  625. break;
  626. case AOT_OBJECT_FILE:
  627. if (!aot_emit_object_file(comp_ctx, out_file_name)) {
  628. printf("%s\n", aot_get_last_error());
  629. goto fail5;
  630. }
  631. break;
  632. case AOT_FORMAT_FILE:
  633. if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
  634. printf("%s\n", aot_get_last_error());
  635. goto fail5;
  636. }
  637. break;
  638. default:
  639. break;
  640. }
  641. bh_print_time("Compile end");
  642. printf("Compile success, file %s was generated.\n", out_file_name);
  643. exit_status = EXIT_SUCCESS;
  644. fail5:
  645. /* Destroy compiler context */
  646. aot_destroy_comp_context(comp_ctx);
  647. fail4:
  648. /* Destroy compile data */
  649. aot_destroy_comp_data(comp_data);
  650. fail3:
  651. /* Unload WASM module */
  652. wasm_runtime_unload(wasm_module);
  653. fail2:
  654. /* free the file buffer */
  655. if (!use_dummy_wasm) {
  656. wasm_runtime_free(wasm_file);
  657. }
  658. fail1:
  659. #if BH_HAS_DLFCN
  660. unregister_and_unload_native_libs(native_handle_count, native_handle_list);
  661. #endif
  662. /* Destroy runtime environment */
  663. wasm_runtime_destroy();
  664. fail0:
  665. /* free option.custom_sections */
  666. if (option.custom_sections) {
  667. free(option.custom_sections);
  668. }
  669. bh_print_time("wamrc return");
  670. return exit_status;
  671. }