[binutils-gdb] RISC-V: Don't overwrite got entry to zero for IRELATIVE/RELATIVE relocation

Nelson Chu nelsonc1225@sourceware.org
Thu Sep 4 08:43:00 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=672a326e2c1a0eb5e318637df205fcdf10cc48a0

commit 672a326e2c1a0eb5e318637df205fcdf10cc48a0
Author: Nelson Chu <nelson@rivosinc.com>
Date:   Thu Aug 21 12:33:03 2025 +0800

    RISC-V: Don't overwrite got entry to zero for IRELATIVE/RELATIVE relocation
    
    Even the final result is correct since we encode the relocation into r_addend,
    so glibc won't need to get the content of got entry when resolving relative
    relocation, it still werid that finish_dynamic_symbol cleans what we filled in
    the relocate_section.

Diff:
---
 bfd/elfnn-riscv.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index af08f7d46f6..6ff64325f85 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -3605,19 +3605,13 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
 	      else
 		{
 		  /* Generate R_RISCV_NN.  */
-		  BFD_ASSERT ((h->got.offset & 1) == 0);
-		  BFD_ASSERT (h->dynindx != -1);
-		  rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
-		  rela.r_addend = 0;
+		  goto do_reloc_nn;
 		}
 	    }
 	  else if (bfd_link_pic (info))
 	    {
 	      /* Generate R_RISCV_NN.  */
-	      BFD_ASSERT ((h->got.offset & 1) == 0);
-	      BFD_ASSERT (h->dynindx != -1);
-	      rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
-	      rela.r_addend = 0;
+	      goto do_reloc_nn;
 	    }
 	  else
 	    {
@@ -3655,15 +3649,15 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
 	}
       else
 	{
+  do_reloc_nn:
 	  BFD_ASSERT ((h->got.offset & 1) == 0);
 	  BFD_ASSERT (h->dynindx != -1);
 	  rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
 	  rela.r_addend = 0;
+	  bfd_put_NN (output_bfd, 0,
+		      sgot->contents + (h->got.offset & ~(bfd_vma) 1));
 	}
 
-      bfd_put_NN (output_bfd, 0,
-		  sgot->contents + (h->got.offset & ~(bfd_vma) 1));
-
       if (use_elf_append_rela)
 	riscv_elf_append_rela (output_bfd, srela, &rela);
       else


More information about the Binutils-cvs mailing list