Procházet zdrojové kódy

aot: Move stack_sizes table to a dedicated section (#2317)

To solve the "AOT module load failed: resolve symbol stack_sizes failed" issue.

This PR partly fixes #2312 and was lightly tested on qemu armhf.
YAMAMOTO Takashi před 2 roky
rodič
revize
5831531449

+ 1 - 0
core/iwasm/aot/aot_loader.c

@@ -2020,6 +2020,7 @@ do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
                  || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))
                  /* ".rodata.strn.m" */
                  || !strncmp(symbol, ".rodata.str", strlen(".rodata.str"))
+                 || !strcmp(symbol, AOT_STACK_SIZES_SECTION_NAME)
 #if WASM_ENABLE_STATIC_PGO != 0
                  || !strncmp(symbol, "__llvm_prf_cnts", 15)
                  || !strncmp(symbol, "__llvm_prf_data", 15)

+ 10 - 0
core/iwasm/compilation/aot.h

@@ -28,6 +28,16 @@ extern "C" {
 #endif
 extern const char *aot_stack_sizes_name;
 
+#ifndef AOT_STACK_SIZES_ALIAS_NAME
+#define AOT_STACK_SIZES_ALIAS_NAME "aot_stack_sizes_alias"
+#endif
+extern const char *aot_stack_sizes_alias_name;
+
+#ifndef AOT_STACK_SIZES_SECTION_NAME
+#define AOT_STACK_SIZES_SECTION_NAME ".aot_stack_sizes"
+#endif
+extern const char *aot_stack_sizes_section_name;
+
 typedef InitializerExpression AOTInitExpr;
 typedef WASMType AOTFuncType;
 typedef WASMExport AOTExport;

+ 16 - 1
core/iwasm/compilation/aot_emit_aot_file.c

@@ -2333,6 +2333,7 @@ is_data_section(AOTObjectData *obj_data, LLVMSectionIteratorRef sec_itr,
             || (!strcmp(section_name, ".rdata")
                 && get_relocations_count(sec_itr, &relocation_count)
                 && relocation_count > 0)
+            || !strcmp(section_name, aot_stack_sizes_section_name)
             || (obj_data->comp_ctx->enable_llvm_pgo
                 && (!strncmp(section_name, "__llvm_prf_cnts", 15)
                     || !strncmp(section_name, "__llvm_prf_data", 15)
@@ -2604,7 +2605,7 @@ aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
 
     while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
         if ((name = LLVMGetSymbolName(sym_itr))
-            && !strcmp(name, aot_stack_sizes_name)) {
+            && !strcmp(name, aot_stack_sizes_alias_name)) {
             uint64 sz = LLVMGetSymbolSize(sym_itr);
             if (sz != sizeof(uint32) * obj_data->func_count) {
                 aot_set_last_error("stack_sizes had unexpected size.");
@@ -2620,6 +2621,11 @@ aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
             const char *sec_name = LLVMGetSectionName(sec_itr);
             LOG_VERBOSE("stack_sizes found in section %s offset %" PRIu64 ".",
                         sec_name, addr);
+            if (strcmp(sec_name, aot_stack_sizes_section_name) || addr != 0) {
+                aot_set_last_error(
+                    "stack_sizes found at an unexpected location.");
+                goto fail;
+            }
             /*
              * Note: We can't always modify stack_sizes in-place.
              * Eg. When WAMRC_LLC_COMPILER is used, LLVM sometimes uses
@@ -2930,6 +2936,15 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
                 + align_uint(obj_data->text_unlikely_size, 4);
         }
 
+        /*
+         * Note: aot_stack_sizes_section_name section only contains
+         * stack_sizes table.
+         */
+        if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)) {
+            /* discard const */
+            relocation->symbol_name = (char *)aot_stack_sizes_section_name;
+        }
+
         if (obj_data->comp_ctx->enable_llvm_pgo
             && (!strcmp(relocation->symbol_name, "__llvm_prf_cnts")
                 || !strcmp(relocation->symbol_name, "__llvm_prf_data"))) {

+ 6 - 4
core/iwasm/compilation/aot_llvm.c

@@ -1373,11 +1373,12 @@ create_func_ptrs(const AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
 }
 
 const char *aot_stack_sizes_name = AOT_STACK_SIZES_NAME;
+const char *aot_stack_sizes_alias_name = AOT_STACK_SIZES_ALIAS_NAME;
+const char *aot_stack_sizes_section_name = AOT_STACK_SIZES_SECTION_NAME;
 
 static bool
 aot_create_stack_sizes(const AOTCompData *comp_data, AOTCompContext *comp_ctx)
 {
-    const char *stack_sizes_name = "stack_sizes";
     LLVMValueRef stack_sizes, *values, array, alias;
     LLVMTypeRef stack_sizes_type;
 #if LLVM_VERSION_MAJOR <= 13
@@ -1393,7 +1394,7 @@ aot_create_stack_sizes(const AOTCompData *comp_data, AOTCompContext *comp_ctx)
     }
 
     stack_sizes =
-        LLVMAddGlobal(comp_ctx->module, stack_sizes_type, stack_sizes_name);
+        LLVMAddGlobal(comp_ctx->module, stack_sizes_type, aot_stack_sizes_name);
     if (!stack_sizes) {
         aot_set_last_error("failed to create stack_sizes global.");
         return false;
@@ -1429,7 +1430,7 @@ aot_create_stack_sizes(const AOTCompData *comp_data, AOTCompContext *comp_ctx)
      */
 #if LLVM_VERSION_MAJOR > 13
     alias = LLVMAddAlias2(comp_ctx->module, stack_sizes_type, 0, stack_sizes,
-                          aot_stack_sizes_name);
+                          aot_stack_sizes_alias_name);
 #else
     alias_type = LLVMPointerType(stack_sizes_type, 0);
     if (!alias_type) {
@@ -1437,7 +1438,7 @@ aot_create_stack_sizes(const AOTCompData *comp_data, AOTCompContext *comp_ctx)
         return false;
     }
     alias = LLVMAddAlias(comp_ctx->module, alias_type, stack_sizes,
-                         aot_stack_sizes_name);
+                         aot_stack_sizes_alias_name);
 #endif
     if (!alias) {
         aot_set_last_error("failed to create stack_sizes alias.");
@@ -1449,6 +1450,7 @@ aot_create_stack_sizes(const AOTCompData *comp_data, AOTCompContext *comp_ctx)
      * avoid creating extra relocations in the precheck functions.
      */
     LLVMSetLinkage(stack_sizes, LLVMInternalLinkage);
+    LLVMSetSection(stack_sizes, aot_stack_sizes_section_name);
     comp_ctx->stack_sizes_type = stack_sizes_type;
     comp_ctx->stack_sizes = stack_sizes;
     return true;