From 695dbf027c8daa81c8f3015f742dd89d9cab34f1 Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Fri, 26 Sep 2025 10:37:33 +0800 Subject: [PATCH 1/2] add new case for arc relocation --- core/iwasm/aot/arch/aot_reloc_arc.c | 73 ++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/core/iwasm/aot/arch/aot_reloc_arc.c b/core/iwasm/aot/arch/aot_reloc_arc.c index 3329629987..092017534d 100644 --- a/core/iwasm/aot/arch/aot_reloc_arc.c +++ b/core/iwasm/aot/arch/aot_reloc_arc.c @@ -310,6 +310,75 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { + case R_ARC_S21H_PCREL: + { + uint32 insn = LOAD_I32(target_section_addr + reloc_offset); + int32 addend, value; + uintptr_t S, P; + intptr_t A; + + CHECK_RELOC_OFFSET(sizeof(void *)); + + /* Convert from middle endian */ + insn = middle_endian_convert(insn); + + /**/ + addend = (insn << 16) >> 22; + addend = addend << 10; + addend |= ((insn << 5) >> 22); + addend = addend << 1; + + /* (S + A) - P */ + S = (uintptr_t)(uint8 *)symbol_addr; + A = (intptr_t)reloc_addend; + P = (uintptr_t)(target_section_addr + reloc_offset); + P &= (uintptr_t)~1; + value = (int32)(S + A + addend - P); + + insn = insn & 0xf801003f; + insn |= ((((value >> 1) & 0x3ff) << 17) + | (((value >> 1) & 0xffc00) >> 4)); + + /* Convert to middle endian */ + insn = middle_endian_convert(insn); + + STORE_U32(target_section_addr + reloc_offset, insn); + break; + } + case R_ARC_S21W_PCREL: + { + uint32 insn = LOAD_I32(target_section_addr + reloc_offset); + int32 addend, value; + uintptr_t S, P; + intptr_t A; + + CHECK_RELOC_OFFSET(sizeof(void *)); + + /* Convert from middle endian */ + insn = middle_endian_convert(insn); + + addend = (insn << 16) >> 22; + addend = addend << 9; + addend |= ((insn << 5) >> 23); + addend = addend << 2; + + /* (S + A) - P */ + S = (uintptr_t)(uint8 *)symbol_addr; + A = (intptr_t)reloc_addend; + P = (uintptr_t)(target_section_addr + reloc_offset); + P &= (uintptr_t)~3; + value = (int32)(S + A + addend - P); + + insn = insn & 0xf803003f; + insn |= ((((value >> 2) & 0x1ff) << 18) + | (((value >> 2) & 0x7fe00) >> 3)); + + /* Convert to middle endian */ + insn = middle_endian_convert(insn); + + STORE_U32(target_section_addr + reloc_offset, insn); + break; + } case R_ARC_S25H_PCREL: { uint32 insn = LOAD_I32(target_section_addr + reloc_offset); @@ -340,8 +409,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, insn = insn & 0xf8010030; insn |= ((((value >> 1) & 0x3ff) << 17) - | (((value >> 1) & 0xffc00) >> 3) - | (((value >> 1) & 0xf00000) >> 19)); + | (((value >> 1) & 0xffc00) >> 4) + | (((value >> 1) & 0xf00000) >> 20)); /* Convert to middle endian */ insn = middle_endian_convert(insn); From 49c826caffe9702cf7cc319e5afc7f3179ec441a Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Tue, 30 Sep 2025 14:31:10 +0800 Subject: [PATCH 2/2] add comments --- core/iwasm/aot/arch/aot_reloc_arc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/iwasm/aot/arch/aot_reloc_arc.c b/core/iwasm/aot/arch/aot_reloc_arc.c index 092017534d..2ce1154426 100644 --- a/core/iwasm/aot/arch/aot_reloc_arc.c +++ b/core/iwasm/aot/arch/aot_reloc_arc.c @@ -321,11 +321,13 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, /* Convert from middle endian */ insn = middle_endian_convert(insn); - - /**/ + + /* Extract the first 10 bits from Position 6 to 15 in insn */ addend = (insn << 16) >> 22; addend = addend << 10; + /* Extract the remaining 10 bits from Position 17 to 26 in insn */ addend |= ((insn << 5) >> 22); + /* Fill in 1 bits to get the 21 bit Offset Value */ addend = addend << 1; /* (S + A) - P */ @@ -338,7 +340,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, insn = insn & 0xf801003f; insn |= ((((value >> 1) & 0x3ff) << 17) | (((value >> 1) & 0xffc00) >> 4)); - + /* Convert to middle endian */ insn = middle_endian_convert(insn); @@ -357,9 +359,12 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, /* Convert from middle endian */ insn = middle_endian_convert(insn); + /* Extract the first 10 bits from Position 6 to 15 in insn */ addend = (insn << 16) >> 22; addend = addend << 9; + /* Extract the remaining 9 bits from Position 18 to 26 in insn */ addend |= ((insn << 5) >> 23); + /* Fill in 2 bits to get the 21 bit Offset Value */ addend = addend << 2; /* (S + A) - P */ @@ -368,7 +373,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, P = (uintptr_t)(target_section_addr + reloc_offset); P &= (uintptr_t)~3; value = (int32)(S + A + addend - P); - + insn = insn & 0xf803003f; insn |= ((((value >> 2) & 0x1ff) << 18) | (((value >> 2) & 0x7fe00) >> 3));