Przeglądaj źródła

Fix aot large model (--size-level=0) with LLVM 18 (#3057)

The recent versions LLVM uses ".ltext" section for X86 large model.

cf. https://github.com/llvm/llvm-project/commit/d8a04398f9492f043ffd8fbaf2458778f7d0fcd5

This fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/3034
YAMAMOTO Takashi 2 lat temu
rodzic
commit
bc35602004

+ 10 - 2
core/iwasm/aot/aot_loader.c

@@ -1975,6 +1975,12 @@ str2uint64(const char *buf, uint64 *p_res)
 
 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative offset to GOT */
 
+static bool
+is_text_section(const char *section_name)
+{
+    return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext");
+}
+
 static bool
 do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
                    char *error_buf, uint32 error_buf_size)
@@ -2063,7 +2069,7 @@ do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
             symbol_addr = module->func_ptrs[func_index];
         }
 #endif
-        else if (!strcmp(symbol, ".text")) {
+        else if (is_text_section(symbol)) {
             symbol_addr = module->code;
         }
         else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata")
@@ -2235,7 +2241,7 @@ do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
 
     for (i = 0; i < group->relocation_count; i++, relocation++) {
         symbol = relocation->symbol_name;
-        if (!strcmp(symbol, ".text")) {
+        if (is_text_section(symbol)) {
             symbol_addr = module->code;
         }
 #if WASM_ENABLE_STATIC_PGO != 0
@@ -2696,6 +2702,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
 
         if (!strcmp(group->section_name, ".rel.text")
             || !strcmp(group->section_name, ".rela.text")
+            || !strcmp(group->section_name, ".rel.ltext")
+            || !strcmp(group->section_name, ".rela.ltext")
             || !strcmp(group->section_name, ".rela.literal")
 #ifdef BH_PLATFORM_WINDOWS
             || !strcmp(group->section_name, ".text")

+ 15 - 4
core/iwasm/aot/debug/elf_parser.c

@@ -100,6 +100,12 @@ get_section64(Elf64_Ehdr *eh, Elf64_Shdr *section_header)
     return buf + section_header->sh_offset;
 }
 
+static bool
+is_text_section(const char *section_name)
+{
+    return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext");
+}
+
 bool
 get_text_section(void *buf, uint64_t *offset, uint64_t *size)
 {
@@ -107,6 +113,7 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size)
     uint32 i;
     char *sh_str;
 
+    /* Assumption: Only one of .text or .ltext is non-empty. */
     if (is64Bit(buf)) {
         Elf64_Ehdr *eh = (Elf64_Ehdr *)buf;
         Elf64_Shdr **sh_table =
@@ -115,14 +122,16 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size)
             read_section_header_table64(eh, sh_table);
             sh_str = get_section64(eh, sh_table[eh->e_shstrndx]);
             for (i = 0; i < eh->e_shnum; i++) {
-                if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) {
+                if (is_text_section(sh_str + sh_table[i]->sh_name)) {
                     *offset = sh_table[i]->sh_offset;
                     *size = sh_table[i]->sh_size;
                     sh_table[i]->sh_addr =
                         (Elf64_Addr)(uintptr_t)((char *)buf
                                                 + sh_table[i]->sh_offset);
                     ret = true;
-                    break;
+                    if (*size > 0) {
+                        break;
+                    }
                 }
             }
             wasm_runtime_free(sh_table);
@@ -136,14 +145,16 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size)
             read_section_header_table(eh, sh_table);
             sh_str = get_section(eh, sh_table[eh->e_shstrndx]);
             for (i = 0; i < eh->e_shnum; i++) {
-                if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) {
+                if (is_text_section(sh_str + sh_table[i]->sh_name)) {
                     *offset = sh_table[i]->sh_offset;
                     *size = sh_table[i]->sh_size;
                     sh_table[i]->sh_addr =
                         (Elf32_Addr)(uintptr_t)((char *)buf
                                                 + sh_table[i]->sh_offset);
                     ret = true;
-                    break;
+                    if (*size > 0) {
+                        break;
+                    }
                 }
             }
             wasm_runtime_free(sh_table);

+ 38 - 10
core/iwasm/compilation/aot_emit_aot_file.c

@@ -654,7 +654,8 @@ get_relocations_size(AOTObjectData *obj_data,
         /* ignore the relocations to aot_func_internal#n in text section
            for windows platform since they will be applied in
            aot_emit_text_section */
-        if (!strcmp(relocation_group->section_name, ".text")
+        if ((!strcmp(relocation_group->section_name, ".text")
+             || !strcmp(relocation_group->section_name, ".ltext"))
             && !strncmp(relocation->symbol_name, AOT_FUNC_INTERNAL_PREFIX,
                         strlen(AOT_FUNC_INTERNAL_PREFIX))
             && ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
@@ -1819,7 +1820,8 @@ aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
         for (i = 0; i < obj_data->relocation_group_count;
              i++, relocation_group++) {
             /* relocation in text section */
-            if (!strcmp(relocation_group->section_name, ".text")) {
+            if ((!strcmp(relocation_group->section_name, ".text")
+                 || !strcmp(relocation_group->section_name, ".ltext"))) {
                 relocation = relocation_group->relocations;
                 relocation_count = relocation_group->relocation_count;
                 for (j = 0; j < relocation_count; j++) {
@@ -2380,17 +2382,19 @@ aot_resolve_text(AOTObjectData *obj_data)
         while (
             !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) {
             if ((name = (char *)LLVMGetSectionName(sec_itr))) {
-                if (!strcmp(name, ".text")) {
+                if (!strcmp(name, ".text") || !strcmp(name, ".ltext")) {
                     obj_data->text = (char *)LLVMGetSectionContents(sec_itr);
                     obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr);
                 }
-                else if (!strcmp(name, ".text.unlikely.")) {
+                else if (!strcmp(name, ".text.unlikely.")
+                         || !strcmp(name, ".ltext.unlikely.")) {
                     obj_data->text_unlikely =
                         (char *)LLVMGetSectionContents(sec_itr);
                     obj_data->text_unlikely_size =
                         (uint32)LLVMGetSectionSize(sec_itr);
                 }
-                else if (!strcmp(name, ".text.hot.")) {
+                else if (!strcmp(name, ".text.hot.")
+                         || !strcmp(name, ".ltext.hot.")) {
                     obj_data->text_hot =
                         (char *)LLVMGetSectionContents(sec_itr);
                     obj_data->text_hot_size =
@@ -2909,11 +2913,13 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
                     (char *)LLVMGetSectionName(contain_section);
                 LLVMDisposeSectionIterator(contain_section);
 
-                if (!strcmp(contain_section_name, ".text.unlikely.")) {
+                if (!strcmp(contain_section_name, ".text.unlikely.")
+                    || !strcmp(contain_section_name, ".ltext.unlikely.")) {
                     func->text_offset = align_uint(obj_data->text_size, 4)
                                         + LLVMGetSymbolAddress(sym_itr);
                 }
-                else if (!strcmp(contain_section_name, ".text.hot.")) {
+                else if (!strcmp(contain_section_name, ".text.hot.")
+                         || !strcmp(contain_section_name, ".ltext.hot.")) {
                     func->text_offset =
                         align_uint(obj_data->text_size, 4)
                         + align_uint(obj_data->text_unlikely_size, 4)
@@ -2945,12 +2951,14 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
                     (char *)LLVMGetSectionName(contain_section);
                 LLVMDisposeSectionIterator(contain_section);
 
-                if (!strcmp(contain_section_name, ".text.unlikely.")) {
+                if (!strcmp(contain_section_name, ".text.unlikely.")
+                    || !strcmp(contain_section_name, ".ltext.unlikely.")) {
                     func->text_offset_of_aot_func_internal =
                         align_uint(obj_data->text_size, 4)
                         + LLVMGetSymbolAddress(sym_itr);
                 }
-                else if (!strcmp(contain_section_name, ".text.hot.")) {
+                else if (!strcmp(contain_section_name, ".text.hot.")
+                         || !strcmp(contain_section_name, ".ltext.hot.")) {
                     func->text_offset_of_aot_func_internal =
                         align_uint(obj_data->text_size, 4)
                         + align_uint(obj_data->text_unlikely_size, 4)
@@ -3211,6 +3219,12 @@ is_relocation_section_name(AOTObjectData *obj_data, char *section_name)
             || !strcmp(section_name, ".rel.text.unlikely.")
             || !strcmp(section_name, ".rela.text.hot.")
             || !strcmp(section_name, ".rel.text.hot.")
+            || !strcmp(section_name, ".rela.ltext")
+            || !strcmp(section_name, ".rel.ltext")
+            || !strcmp(section_name, ".rela.ltext.unlikely.")
+            || !strcmp(section_name, ".rel.ltext.unlikely.")
+            || !strcmp(section_name, ".rela.ltext.hot.")
+            || !strcmp(section_name, ".rel.ltext.hot.")
             || !strcmp(section_name, ".rela.literal")
             || !strcmp(section_name, ".rela.data")
             || !strcmp(section_name, ".rel.data")
@@ -3249,7 +3263,9 @@ static bool
 is_readonly_section(const char *name)
 {
     return !strcmp(name, ".rel.text") || !strcmp(name, ".rela.text")
-           || !strcmp(name, ".rela.literal") || !strcmp(name, ".text");
+           || !strcmp(name, ".rel.ltext") || !strcmp(name, ".rela.ltext")
+           || !strcmp(name, ".rela.literal") || !strcmp(name, ".text")
+           || !strcmp(name, ".ltext");
 }
 
 static bool
@@ -3342,12 +3358,24 @@ aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
                 || !strcmp(relocation_group->section_name, ".rela.text.hot.")) {
                 relocation_group->section_name = ".rela.text";
             }
+            else if (!strcmp(relocation_group->section_name,
+                             ".rela.ltext.unlikely.")
+                     || !strcmp(relocation_group->section_name,
+                                ".rela.ltext.hot.")) {
+                relocation_group->section_name = ".rela.ltext";
+            }
             else if (!strcmp(relocation_group->section_name,
                              ".rel.text.unlikely.")
                      || !strcmp(relocation_group->section_name,
                                 ".rel.text.hot.")) {
                 relocation_group->section_name = ".rel.text";
             }
+            else if (!strcmp(relocation_group->section_name,
+                             ".rel.ltext.unlikely.")
+                     || !strcmp(relocation_group->section_name,
+                                ".rel.ltext.hot.")) {
+                relocation_group->section_name = ".rel.ltext";
+            }
 
             /*
              * Relocations in read-only sections are problematic,