git: 0cc04135734c - stable/13 - riscv: fix relocation handling for R_RISCV_64

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Fri, 28 Oct 2022 14:10:54 UTC
The branch stable/13 has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=0cc04135734c84558aa7b9386c1eda569b55b981

commit 0cc04135734c84558aa7b9386c1eda569b55b981
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2022-10-20 14:56:59 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2022-10-28 14:10:37 +0000

    riscv: fix relocation handling for R_RISCV_64
    
    It requires the addend. In practice this doesn't seem to be a problem,
    since relocations of this type are all with an addend of zero.
    Obviously, we still want to handle this correctly if that ever changes.
    
    Reviewed by:    markj
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D37039
    
    (cherry picked from commit a9b24e4dc28abfe668038e4c053d77b6dd4dce19)
---
 sys/riscv/riscv/elf_machdep.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/sys/riscv/riscv/elf_machdep.c b/sys/riscv/riscv/elf_machdep.c
index 68c7afb4d09f..b3d8ac54a36d 100644
--- a/sys/riscv/riscv/elf_machdep.c
+++ b/sys/riscv/riscv/elf_machdep.c
@@ -340,15 +340,25 @@ elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
 		break;
 
 	case R_RISCV_64:
+		error = lookup(lf, symidx, 1, &addr);
+		if (error != 0)
+			return (-1);
+
+		before64 = *where;
+		*where = addr + addend;
+		if (debug_kld)
+			printf("%p %c %-24s %016lx -> %016lx\n", where,
+			    (local ? 'l' : 'g'), reloctype_to_str(rtype),
+			    before64, *where);
+		break;
+
 	case R_RISCV_JUMP_SLOT:
 		error = lookup(lf, symidx, 1, &addr);
 		if (error != 0)
 			return (-1);
 
-		val = addr;
 		before64 = *where;
-		if (*where != val)
-			*where = val;
+		*where = addr;
 		if (debug_kld)
 			printf("%p %c %-24s %016lx -> %016lx\n", where,
 			    (local ? 'l' : 'g'), reloctype_to_str(rtype),