Support emit specified custom sections into AoT file (#1207)

And add API to get the content of custom section with
section name for both wasm file and aot file.
This commit is contained in:
Xu Jun
2022-06-10 21:51:13 +08:00
committed by GitHub
parent d404107d85
commit 77595c9560
22 changed files with 420 additions and 31 deletions

View File

@ -35,6 +35,7 @@ add_definitions(-DWASM_ENABLE_REF_TYPES=1)
add_definitions(-DWASM_ENABLE_CUSTOM_NAME_SECTION=1)
add_definitions(-DWASM_ENABLE_DUMP_CALL_STACK=1)
add_definitions(-DWASM_ENABLE_PERF_PROFILING=1)
add_definitions(-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1)
if (WAMR_BUILD_LLVM_LEGACY_PM EQUAL 1)
add_definitions(-DWASM_ENABLE_LLVM_LEGACY_PM=1)

View File

@ -10,7 +10,7 @@
#include "aot_export.h"
/* clang-format off */
static int
static void
print_help()
{
printf("Usage: wamrc [options] -o output_file wasm_file\n");
@ -58,14 +58,66 @@ print_help()
printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n");
printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
printf(" --emit-custom-sections=<section names>\n");
printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
printf(" multiple names, e.g.\n");
printf(" --emit-custom-sections=section1,section2,sectionN\n");
printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
printf("Examples: wamrc -o test.aot test.wasm\n");
printf(" wamrc --target=i386 -o test.aot test.wasm\n");
printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
return 1;
}
/* clang-format on */
#define PRINT_HELP_AND_EXIT() \
do { \
print_help(); \
goto fail0; \
} while (0)
/**
* Split a strings into an array of strings
* Returns NULL on failure
* Memory must be freed by caller
* Based on: http://stackoverflow.com/a/11198630/471795
*/
static char **
split_string(char *str, int *count, const char *delimer)
{
char **res = NULL, **res1;
char *p;
int idx = 0;
/* split string and append tokens to 'res' */
do {
p = strtok(str, delimer);
str = NULL;
res1 = res;
res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
if (res == NULL) {
free(res1);
return NULL;
}
res[idx++] = p;
} while (p);
/**
* Due to the section name,
* res[0] might contain a '\' to indicate a space
* func\name -> func name
*/
p = strchr(res[0], '\\');
while (p) {
*p = ' ';
p = strchr(p, '\\');
}
if (count) {
*count = idx - 1;
}
return res;
}
int
main(int argc, char *argv[])
{
@ -97,39 +149,39 @@ main(int argc, char *argv[])
if (!strcmp(argv[0], "-o")) {
argc--, argv++;
if (argc < 2)
return print_help();
PRINT_HELP_AND_EXIT();
out_file_name = argv[0];
}
else if (!strncmp(argv[0], "--target=", 9)) {
if (argv[0][9] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.target_arch = argv[0] + 9;
}
else if (!strncmp(argv[0], "--target-abi=", 13)) {
if (argv[0][13] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.target_abi = argv[0] + 13;
}
else if (!strncmp(argv[0], "--cpu=", 6)) {
if (argv[0][6] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.target_cpu = argv[0] + 6;
}
else if (!strncmp(argv[0], "--cpu-features=", 15)) {
if (argv[0][15] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.cpu_features = argv[0] + 15;
}
else if (!strncmp(argv[0], "--opt-level=", 12)) {
if (argv[0][12] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.opt_level = (uint32)atoi(argv[0] + 12);
if (option.opt_level > 3)
option.opt_level = 3;
}
else if (!strncmp(argv[0], "--size-level=", 13)) {
if (argv[0][13] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
option.size_level = (uint32)atoi(argv[0] + 13);
if (option.size_level > 3)
option.size_level = 3;
@ -143,7 +195,7 @@ main(int argc, char *argv[])
}
else if (!strncmp(argv[0], "--format=", 9)) {
if (argv[0][9] == '\0')
return print_help();
PRINT_HELP_AND_EXIT();
if (!strcmp(argv[0] + 9, "aot"))
option.output_format = AOT_FORMAT_FILE;
else if (!strcmp(argv[0] + 9, "object"))
@ -154,13 +206,13 @@ main(int argc, char *argv[])
option.output_format = AOT_LLVMIR_OPT_FILE;
else {
printf("Invalid format %s.\n", argv[0] + 9);
return print_help();
PRINT_HELP_AND_EXIT();
}
}
else if (!strncmp(argv[0], "-v=", 3)) {
log_verbose_level = atoi(argv[0] + 3);
if (log_verbose_level < 0 || log_verbose_level > 5)
return print_help();
PRINT_HELP_AND_EXIT();
}
else if (!strcmp(argv[0], "--disable-bulk-memory")) {
option.enable_bulk_memory = false;
@ -201,12 +253,27 @@ main(int argc, char *argv[])
else if (!strcmp(argv[0], "--disable-llvm-lto")) {
option.disable_llvm_lto = true;
}
else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
int len = 0;
if (option.custom_sections) {
free(option.custom_sections);
}
option.custom_sections = split_string(argv[0] + 23, &len, ",");
if (!option.custom_sections) {
printf("Failed to process emit-custom-sections: alloc "
"memory failed\n");
PRINT_HELP_AND_EXIT();
}
option.custom_sections_count = len;
}
else
return print_help();
PRINT_HELP_AND_EXIT();
}
if (argc == 0 || !out_file_name)
return print_help();
PRINT_HELP_AND_EXIT();
if (!size_level_set) {
/**
@ -348,6 +415,12 @@ fail1:
/* Destroy runtime environment */
wasm_runtime_destroy();
fail0:
/* free option.custom_sections */
if (option.custom_sections) {
free(option.custom_sections);
}
bh_print_time("wamrc return");
return exit_status;
}