You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "pussuw (via GitHub)" <gi...@apache.org> on 2023/12/05 10:39:14 UTC

[PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

pussuw opened a new pull request, #11322:
URL: https://github.com/apache/nuttx/pull/11322

   ## Summary
   There is a problem with the current elf loader for risc-v: when a pair of PCREL_HI20 / LO12 relocations are encountered, it is assumed that these will follow each other immediately, as follows:
   
   label:
   	auipc      a0, %pcrel_hi(symbol)    // R_RISCV_PCREL_HI20
   	load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
   
   With this assumption, the hi/lo relocations are both done when a hi20 relocation entry is encountered, first to the current instruction (addr) and to the next instruction (addr + 4).
   
   However, this assumption is wrong. There is nothing in the elf relocation specification[1] that mandates this. Thus, the hi/lo relocation always needs to first fixup the hi-part, and when the lo-part is encountered, it needs to find the corresponding hi relocation entry, via the given "label". This necessitates (re-)visiting the relocation entries for the current section as well as looking for "label" in the symbol table.
   
   The NuttX elf loader does not allow such operations to be done in the machine specific part, so this patch fixes the relocation issue by introducing an architecture specific cache for the hi20 relocation and symbol table entries. When a lo12 relocation is encountered, the cache can be consulted to find the hi20 part.
   
   [1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
   ## Impact
   Fix elf loader for risc-v
   ## Testing
   qemu-rv:knsh64
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424295571


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   @pkarashchenko should we merge this patch or wait your investigation?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#issuecomment-1840500157

   I made this PR into a draft to get comments before I fix the build errors (up_relocate[add] points to many places). @xiaoxiang781216 or @masayuki2009 can you take a look please ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #11322:
URL: https://github.com/apache/nuttx/pull/11322


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424526580


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   No. Please move forward with merge.
   @pussuw Both `medlow` (https://github.com/riscv-collab/riscv-gnu-toolchain/issues/103) and `medany` (https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733) modes are affected on RV64. Only RV32 is not affected as there is not place for sign bit extension obviously. The `medlow` is affected more compared to `medany` because of its nature.
   The https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 describes clearly how this can be detected by linker. From my investigations it is enough to have bit 31 set in symbol address to get linker error, so it highly depends on the SoC address mapping.
   I would not argue if this is or is not related to this PR, but rather spotted things I've met recently, so please feel free to make a merge decision.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1418773187


##########
include/nuttx/elf.h:
##########
@@ -144,7 +144,7 @@ bool up_checkarch(FAR const Elf_Ehdr *hdr);
 int up_relocate(FAR const Elf_Rel *rel, FAR const Elf_Sym *sym,
                 uintptr_t addr);

Review Comment:
   Yes of course for consistency



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1422424015


##########
include/nuttx/elf.h:
##########
@@ -142,9 +142,9 @@ bool up_checkarch(FAR const Elf_Ehdr *hdr);
 
 #ifdef CONFIG_LIBC_ARCH_ELF
 int up_relocate(FAR const Elf_Rel *rel, FAR const Elf_Sym *sym,
-                uintptr_t addr);
-int up_relocateadd(FAR const Elf_Rela *rel,
-                   FAR const Elf_Sym *sym, uintptr_t addr);
+                uintptr_t addr, FAR void *arch_data);

Review Comment:
   The indent in the file is correct, unified diff renders this change wrong.
   
   Try using split diff



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424930414


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, it might be that I'm missing something, not having had to debug this issue myself :)
   
   Regardless, we must patch the HI20 relocations, because user can manually write a lui instruction that uses a symbol with the msbit set. I can do a patch for it, should be simple enough to test as well (with the instructions from 733).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423234438


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Reference: https://github.com/riscv-collab/riscv-gnu-toolchain/commit/a36a4fba58a3f04e9fa439c16e8d6e453ef6067d



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with RV64 based SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol (defined in the linker script) that was a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation truncation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi(symbol)`/`hi(symbol)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1422393716


##########
include/nuttx/elf.h:
##########
@@ -142,9 +142,9 @@ bool up_checkarch(FAR const Elf_Ehdr *hdr);
 
 #ifdef CONFIG_LIBC_ARCH_ELF
 int up_relocate(FAR const Elf_Rel *rel, FAR const Elf_Sym *sym,
-                uintptr_t addr);
-int up_relocateadd(FAR const Elf_Rela *rel,
-                   FAR const Elf_Sym *sym, uintptr_t addr);
+                uintptr_t addr, FAR void *arch_data);

Review Comment:
   ```suggestion
                      uintptr_t addr, FAR void *arch_data);
   ```



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
 
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
+
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
           _add_val((uint16_t *)addr, (imm_hi << 12));
 
-          if ((_get_val((uint16_t *)(addr + 4)) & 0x7f) == OPCODE_SW)
-            {
-              /* Adjust imm for SW : S-type */
+          /* Add the hi20 value to the cache */
 
-              uint32_t val =
-                (((int32_t)imm_lo >> 5) << 25) +
-                (((int32_t)imm_lo & 0x1f) << 7);
+          _add_hi20(arch_data, addr, offset);
+        }
+        break;
 
-              binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n",
-                    imm_lo, imm_lo, val);
+      case R_RISCV_CALL:
+      case R_RISCV_CALL_PLT:
+        {
+          long imm_hi;
+          long imm_lo;
+
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          /* Adjust auipc (add upper immediate to pc) : 20bit */
+
+          _add_val((uint16_t *)addr, (imm_hi << 12));

Review Comment:
   ```suggestion
             _add_val((uint16_t *)addr, imm_hi << 12);
   ```



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));

Review Comment:
   ```suggestion
             _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
   ```



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;

Review Comment:
   should we use long



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
 
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
+
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);

Review Comment:
   PRIx32 -> lx



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
 
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
+
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
           _add_val((uint16_t *)addr, (imm_hi << 12));
 
-          if ((_get_val((uint16_t *)(addr + 4)) & 0x7f) == OPCODE_SW)
-            {
-              /* Adjust imm for SW : S-type */
+          /* Add the hi20 value to the cache */
 
-              uint32_t val =
-                (((int32_t)imm_lo >> 5) << 25) +
-                (((int32_t)imm_lo & 0x1f) << 7);
+          _add_hi20(arch_data, addr, offset);
+        }
+        break;
 
-              binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n",
-                    imm_lo, imm_lo, val);
+      case R_RISCV_CALL:
+      case R_RISCV_CALL_PLT:
+        {
+          long imm_hi;
+          long imm_lo;
+
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          /* Adjust auipc (add upper immediate to pc) : 20bit */
+
+          _add_val((uint16_t *)addr, (imm_hi << 12));
 
-              _add_val((uint16_t *)(addr + 4), val);
-            }
-          else
-            {
-              /* Adjust imm for MV(ADDI)/JALR : I-type */
+          /* Adjust imm for CALL (JALR) : I-type */
 
-              _add_val((uint16_t *)(addr + 4), ((int32_t)imm_lo << 20));
-            }
+          _add_val((uint16_t *)(addr + 4), ((int32_t)imm_lo << 20));

Review Comment:
   ```suggestion
             _add_val((uint16_t *)(addr + 4), (int32_t)imm_lo << 20);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423232456


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   LGTM! But in the light of the my recent investigations I wonder if we should assert or return an error in case of RV64 and `imm_hi` having a sign bit set?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423646862


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   I think this merits further investigation, yes. It seems it depends on the code model https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
   
   The affected type is medlow, medany should work (I / we use medany for RV64 AFAIK) ? So this error should not affect us if I understand correctly.
   
   Regardless, not related to this PR.
   
   I wonder if this medlow/medany type can be detected by the elf linker somehow, I did not spend time investigating...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423235286


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);
 
-          if ((_get_val((uint16_t *)(addr + 4)) & 0x7f) == OPCODE_SW)
-            {
-              /* Adjust imm for SW : S-type */
+          /* Add the hi20 value to the cache */
 
-              uint32_t val =
-                (((int32_t)imm_lo >> 5) << 25) +
-                (((int32_t)imm_lo & 0x1f) << 7);
+          _add_hi20(arch_data, addr, offset);
+        }
+        break;
+
+      case R_RISCV_CALL:
+      case R_RISCV_CALL_PLT:
+        {
+          long imm_hi;
+          long imm_lo;
+
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-              binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n",
-                    imm_lo, imm_lo, val);
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Same as for the `PCREL_HI20` case



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x3000000`, so I had a linker defined symbol defined as a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi`/`hi`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with RV64 based SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol (defined in the linker script) that was a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi(symbol)`/`hi(symbol)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1415915843


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -325,13 +387,25 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          long imm_hi;
+          long imm_lo;

Review Comment:
   move to line 384



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -325,13 +387,25 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          long imm_hi;
+          long imm_lo;
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          uint32_t val =

Review Comment:
   ditto



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,6 +360,25 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
+        {
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          long imm_hi;
+          long imm_lo;

Review Comment:
   move to linee 364



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -272,8 +272,51 @@ int up_relocate(const Elf_Rel *rel, const Elf_Sym *sym, uintptr_t addr)
   return -ENOSYS;
 }
 
+void _add_hi20(void *arch_data, uintptr_t hi20_rel, uintptr_t hi20_offset)

Review Comment:
   add static



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -272,8 +272,51 @@ int up_relocate(const Elf_Rel *rel, const Elf_Sym *sym, uintptr_t addr)
   return -ENOSYS;
 }
 
+void _add_hi20(void *arch_data, uintptr_t hi20_rel, uintptr_t hi20_offset)
+{
+  arch_elfdata_t *data = (arch_elfdata_t *)arch_data;
+  int i;
+
+  /* Try to find a free slot from the list */
+
+  for (i = 0; i < ARCH_ELF_RELCNT; i++)
+    {
+      struct hi20_rels_s *hi20 = &data->hi20_rels[i];
+
+      if (hi20->hi20_rel == 0)
+        {
+          hi20->hi20_rel = hi20_rel;
+          hi20->hi20_offset = hi20_offset;
+          break;
+        }
+    }
+}
+
+uintptr_t _find_hi20(void *arch_data, uintptr_t hi20_rel)

Review Comment:
   add static



##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -350,25 +424,35 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
 
           _add_val((uint16_t *)addr, (imm_hi << 12));
 
-          if ((_get_val((uint16_t *)(addr + 4)) & 0x7f) == OPCODE_SW)
-            {
-              /* Adjust imm for SW : S-type */
+          /* Add the hi20 value to the cache */
+
+          _add_hi20(arch_data, addr, offset);
+        }
+        break;
+
+      case R_RISCV_CALL:
+      case R_RISCV_CALL_PLT:
+        {
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
 
-              uint32_t val =
-                (((int32_t)imm_lo >> 5) << 25) +
-                (((int32_t)imm_lo & 0x1f) << 7);
+          long imm_hi;
+          long imm_lo;

Review Comment:
   ditto



##########
include/nuttx/elf.h:
##########
@@ -144,7 +144,7 @@ bool up_checkarch(FAR const Elf_Ehdr *hdr);
 int up_relocate(FAR const Elf_Rel *rel, FAR const Elf_Sym *sym,
                 uintptr_t addr);

Review Comment:
   should we add arch_data too



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#issuecomment-1845866376

   Not sure if this is related, but recently I faced an issue of relocation truncation (with static linking of course) for RV64 arch when referenced symbol address had "sign bit" set (`0x80000000` bit set), so RV64 compiler populates the sign bit and address starts looking `0xffffffff80000000`. To overcome this I had to use a trick of masking sign bit from symbol label at compile time and then adding assembly instructions to restore it back after `%pcrel_hi(symbol-0x80000000)` value loaded to the register.
   Sorry for spamming here, but I spent fair amount of time to actually understand this non-logical behaviour of RV64 ISA that does not have instruction to say that I want to load `%pcrel_hi(symbol)` value as unsigned.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424558244


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   I suggest we merge this patch, it is not related to this sign extension stuff, it just fixes the linking of pcrel_hi to obey what the risc-v spec. says



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with RV64 based SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol (defined in the linker script) that was a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation truncation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and I agree with that, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi(symbol)`/`hi(symbol)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423646862


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   I think this merits further investigation, yes. It seems it depends on the code model https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
   
   The affected type is medlow, medany should work (I / we use medany for RV64 AFAIK) ? So this error should not affect us if I understand correctly.
   
   Regardless, not related to this PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol (defined in the linker script) that was a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi(symbol)`/`hi(symbol)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1422432498


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)(addr), ((int32_t)imm_lo << 20));
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;

Review Comment:
   These are the types used by the original code, I decided not to touch them.
   
   u32 is enough here (for adjusting the immediate)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol defined as a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi`/`hi`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424584855


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes, returning error seems to be reasonable. Lets merge the patch and the rest can be improved as a next step.
   I met similar error when I was working with SoC that had SRAM mapped starting from `0xb0000000` address while my initial loader code was executing from XIP mapped starting from `0x30000000`, so I had a linker defined symbol defined as a jump point for the next level SW that was `0xb0000XXX`, so as soon as I used that linker defined symbol in my C code (that was executed from XIP) to cast it to function pointer and make a jump the linker was generating a relocation error even with `medany` variant. I started my experiments and did many attempts in order to overcome this issue.
   I would disagree with your statement about assembly code no relation to `medany` as https://github.com/riscv-collab/riscv-gnu-toolchain/issues/733 description clearly states used cmodel `// in c, compile with -mcmodel=medany`. I think that is exactly to show that issue is not `medlow` specific. Usually compiler will generate a PC relative instructions and that I agree, but I also tried to overcome issue in my case (that by default was `auipc`+pair) by changing it to inline assembly of `lui`+pair and both variants lead to relocation error at link time because both `auipc` and `lui` have the same nature of sign bit propagation with `prcel_hi(symbol)`/`hi(symbol)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1424557387


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   Yes thanks @pkarashchenko. I think I described what I meant poorly.
   
   auipc + whatever pair works, the issue is with lui + whatever. When compiling C-code with medany the compiler should not produce lui + whatever pairs AFAIK. What the person in #733 did was write an assembly routine with lui + hi/prcel_hi, which is not medany.
   
   The reason I have not encountered such issues ever is that I never write the lui/auipc instructions directly, instead I use macro instructions (like `la`/ `call`/ `tail`) which produce the correct auipc + jr result when using medany.
   
   And yes, now that you pointed out the other topic I do agree with you; we need to check for msbit in all _HI20 relocation types (for RV64) to catch this type of usage. The linker will still produce an error though.
   
   I think returning an error code might be better (process spawn fails with errorcode), instead of assertion (coredump), what do you think ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#discussion_r1423236063


##########
libs/libc/machine/risc-v/arch_elf.c:
##########
@@ -317,58 +396,101 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
         break;
 
       case R_RISCV_PCREL_LO12_I:
-      case R_RISCV_PCREL_LO12_S:
         {
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          /* NOTE: imm value for mv has been adjusted in previous HI20 */
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for MV(ADDI) / JR (JALR) : I-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          _add_val((uint16_t *)addr, (int32_t)imm_lo << 20);
         }
         break;
 
-      case R_RISCV_PCREL_HI20:
-      case R_RISCV_CALL:
-      case R_RISCV_CALL_PLT:
+      case R_RISCV_PCREL_LO12_S:
         {
+          uint32_t val;
+          long imm_hi;
+          long imm_lo;
+
           binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
                 "to sym=%p st_value=%08lx\n",
                 _get_rname(relotype),
                 addr, _get_val((uint16_t *)addr),
                 sym, sym->st_value);
 
-          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+          offset = _find_hi20(arch_data, sym->st_value);
+
+          /* Adjust imm for SW : S-type */
+
+          _calc_imm(offset, &imm_hi, &imm_lo);
+
+          val = (((int32_t)imm_lo >> 5) << 25) +
+                (((int32_t)imm_lo & 0x1f) << 7);
 
+          binfo("imm_lo=%ld (%lx), val=%" PRIx32 "\n", imm_lo, imm_lo, val);
+
+          _add_val((uint16_t *)addr, val);
+        }
+        break;
+
+      case R_RISCV_PCREL_HI20:
+        {
           long imm_hi;
           long imm_lo;
 
+          binfo("%s at %08" PRIxPTR " [%08" PRIx32 "] "
+                "to sym=%p st_value=%08lx\n",
+                _get_rname(relotype),
+                addr, _get_val((uint16_t *)addr),
+                sym, sym->st_value);
+
+          offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
+
           _calc_imm(offset, &imm_hi, &imm_lo);
 
           /* Adjust auipc (add upper immediate to pc) : 20bit */
 
-          _add_val((uint16_t *)addr, (imm_hi << 12));
+          _add_val((uint16_t *)addr, imm_hi << 12);

Review Comment:
   If yes, then we need to update `R_RISCV_HI20` as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#issuecomment-1840488348

   One example where the current elf linker fails:
   
   https://github.com/apache/nuttx/blob/bffe858e47a15e7f2b7fb7a3e61569afe0f77d05/libs/libc/machine/risc-v/gnu/arch_memset.S#L59-L64


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly [nuttx]

Posted by "pussuw (via GitHub)" <gi...@apache.org>.
pussuw commented on PR #11322:
URL: https://github.com/apache/nuttx/pull/11322#issuecomment-1846916845

   > Not sure if this is related, but recently I faced an issue of relocation truncation (with static linking of course) for RV64 arch when referenced symbol address had "sign bit" set (`0x80000000` bit set), so RV64 compiler populates the sign bit and address starts looking `0xffffffff80000000`. To overcome this I had to use a trick of masking sign bit from symbol label at compile time and then adding assembly instructions to restore it back after `%pcrel_hi(symbol-0x80000000)` value loaded to the register. Sorry for spamming here, but I spent fair amount of time to actually understand this non-logical behaviour of RV64 ISA that does not have instruction to say that I want to load `%pcrel_hi(symbol)` value as unsigned.
   
   The compiler is mandated to do sign extension for short addresses. It is a valid address, it being negative should not have any impact.
   
   This is not really related to this PR. Like I described, the hi/lo relocations must be done in pairs. The current implementation assumes that the hi/lo relocations follow each other as two contiguous instructions. This assumption is wrong, and thus when a pcrel_lo relocation is encountered, the matching pcrel_hi must be found from the relocation entries, and it is found via the symbol "label".
   
   This is why they have the form:
   ```
   label:
   auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
   /* Whatever goes here */
   load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
   ```
   
   Having the label: is not optional, it is _mandatory_, otherwise the pcrel_hi part cannot be found by the linker!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org