git: 72f29c6a1e45 - stable/12 - Reuse the amd64 loader relocation code on arm64

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 08 Oct 2021 01:16:49 UTC
The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=72f29c6a1e45d7dab25a8a32c483265e3fb41c15

commit 72f29c6a1e45d7dab25a8a32c483265e3fb41c15
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2021-01-17 18:11:11 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2021-10-08 01:16:00 +0000

    Reuse the amd64 loader relocation code on arm64
    
    There is no need to keep multiple copies of the relocation code. The
    amd64 code works on arm64 with a few small changes to relocation types.
    
    (cherry picked from commit f6f0b849fb2683feebf2416a793964be0bd05cc5)
---
 stand/common/reloc_elf.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/stand/common/reloc_elf.c b/stand/common/reloc_elf.c
index b45c3295966e..c7dd11eac904 100644
--- a/stand/common/reloc_elf.c
+++ b/stand/common/reloc_elf.c
@@ -77,7 +77,8 @@ __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
 	}
 
 	return (0);
-#elif (defined(__i386__) || defined(__amd64__)) && __ELF_WORD_SIZE == 64
+#elif (defined(__aarch64__) || defined(__amd64__) || defined(__i386__)) && \
+    __ELF_WORD_SIZE == 64
 	Elf64_Addr *where, val;
 	Elf_Addr addend, addr;
 	Elf_Size rtype, symidx;
@@ -112,12 +113,29 @@ __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
 	if (reltype == ELF_RELOC_REL)
 		addend = *where;
 
+#if defined(__aarch64__)
+#define	RELOC_RELATIVE		R_AARCH64_RELATIVE
+#define	RELOC_IRELATIVE		R_AARCH64_IRELATIVE
+#elif defined(__amd64__) || defined(__i386__)
 /* XXX, definitions not available on i386. */
 #define	R_X86_64_64		1
 #define	R_X86_64_RELATIVE	8
 #define	R_X86_64_IRELATIVE	37
 
+#define	RELOC_RELATIVE		R_X86_64_RELATIVE
+#define	RELOC_IRELATIVE		R_X86_64_IRELATIVE
+#endif
+
 	switch (rtype) {
+	case RELOC_RELATIVE:
+		addr = (Elf_Addr)addend + relbase;
+		val = addr;
+		memcpy(where, &val, sizeof(val));
+		break;
+	case RELOC_IRELATIVE:
+		/* leave it to kernel */
+		break;
+#if defined(__amd64__) || defined(__i386__)
 	case R_X86_64_64:		/* S + A */
 		addr = symaddr(ef, symidx);
 		if (addr == 0)
@@ -125,14 +143,7 @@ __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
 		val = addr + addend;
 		*where = val;
 		break;
-	case R_X86_64_RELATIVE:
-		addr = (Elf_Addr)addend + relbase;
-		val = addr;
-		*where = val;
-		break;
-	case R_X86_64_IRELATIVE:
-		/* leave it to kernel */
-		break;
+#endif
 	default:
 		printf("\nunhandled relocation type %u\n", (u_int)rtype);
 		return (EFTYPE);
@@ -200,7 +211,7 @@ __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
 	}
 
 	return (0);
-#elif defined(__aarch64__) || defined(__powerpc__) || defined(__riscv)
+#elif defined(__powerpc__) || defined(__riscv)
 	Elf_Size w;
 	const Elf_Rela *rela;
 
@@ -210,9 +221,7 @@ __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
 		if (relbase + rela->r_offset >= dataaddr &&
 		    relbase + rela->r_offset < dataaddr + len) {
 			switch (ELF_R_TYPE(rela->r_info)) {
-#if defined(__aarch64__)
-			case R_AARCH64_RELATIVE:
-#elif defined(__powerpc__)
+#if defined(__powerpc__)
 			case R_PPC_RELATIVE:
 #elif defined(__riscv)
 			case R_RISCV_RELATIVE: