From 64f9409f9c46a72d0cc804683db06d02c8ea96fa Mon Sep 17 00:00:00 2001 From: CSDUMMI Date: Sat, 10 May 2025 19:47:03 +0200 Subject: [PATCH] Add parsing of export table --- wai.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/wai.c b/wai.c index 28bad13..aac65ca 100644 --- a/wai.c +++ b/wai.c @@ -28,6 +28,7 @@ #include #include #include +#include enum section { Section_Custom, @@ -51,10 +52,24 @@ struct stack { size_t count; }; +#define MAX_FUNCTIONS 128 +enum export_desc { + Export_Func, + Export_Table, + Export_Mem, + Export_Global, +}; + +struct export_t { + u_char name[128]; + size_t name_length; + uint32_t index; + enum export_desc description; +}; struct module { struct type_t *types; - u_char *funcs[128]; + u_char *funcs[MAX_FUNCTIONS]; struct table_t *tables; struct mem_t *mems; struct global_t *globals; @@ -62,7 +77,7 @@ struct module { struct data_t *datas; struct start_t *start; struct import_t *imports; - struct export_t *exports; + struct export_t exports[MAX_FUNCTIONS]; u_char *binary; struct stack stack; int scope; @@ -271,19 +286,27 @@ int parse_section(struct module *module, u_char *binary, int len) { case Section_Export: printf("section: exports\n"); int num_exports = binary[i]; + + if(num_exports > MAX_FUNCTIONS) { + printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS); + return -1; + } + incr(i, len); for (int exports_i = 0; exports_i < num_exports; exports_i++) { - int string_len = binary[i]; + struct export_t *export = &module->exports[i]; + + export->name_length = binary[i]; incr(i, len); - printf("export name: "); - for (int si = 0; si < string_len; si++) { - putchar(binary[i]); + + for (int si = 0; si < export->name_length; si++) { + export->name[si] = binary[i]; incr(i, len); } - putchar('\n'); - // export kind + export->description = (int) binary[i]; incr(i, len); - // export func index + export->index = (uint32_t) binary[i]; + printf("export name: %s of type %d\n", export->name, export->description); incr(i, len); } break;