git: 7125bd6e9f20 - stable/13 - linux(4): Deduplicate linux_trans_osrel().

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Thu, 09 Feb 2023 07:56:46 UTC
The branch stable/13 has been updated by dchagin:

URL: https://cgit.FreeBSD.org/src/commit/?id=7125bd6e9f20473b1b161499e8235843fc62ae5e

commit 7125bd6e9f20473b1b161499e8235843fc62ae5e
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2023-02-02 14:58:07 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2023-02-09 07:55:24 +0000

    linux(4): Deduplicate linux_trans_osrel().
    
    MFC after:              1 week
    
    (cherry picked from commit 95b8603427d5759c5d6090ffaacfaf7f3b8fb926)
---
 sys/amd64/linux/linux_sysvec.c     | 31 ++-----------------------------
 sys/amd64/linux32/linux32_sysvec.c | 29 +----------------------------
 sys/arm64/linux/linux_sysvec.c     | 22 ----------------------
 sys/compat/linux/linux_elf.c       | 23 +++++++++++++++++++++++
 sys/compat/linux/linux_elf.h       |  5 +++++
 sys/i386/linux/linux_sysvec.c      | 27 ---------------------------
 6 files changed, 31 insertions(+), 106 deletions(-)

diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c
index 45d940a42b51..4fe2bfb78f7f 100644
--- a/sys/amd64/linux/linux_sysvec.c
+++ b/sys/amd64/linux/linux_sysvec.c
@@ -115,7 +115,6 @@ extern const char *linux_syscallnames[];
 
 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
 
-static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
 static void	linux_vdso_install(const void *param);
 static void	linux_vdso_deinstall(const void *param);
 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
@@ -736,37 +735,11 @@ linux_vdso_reloc(char *mapping, Elf_Addr offset)
 	}
 }
 
-static char GNULINUX_ABI_VENDOR[] = "GNU";
-static int GNULINUX_ABI_DESC = 0;
-
-static bool
-linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
-{
-	const Elf32_Word *desc;
-	uintptr_t p;
-
-	p = (uintptr_t)(note + 1);
-	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
-
-	desc = (const Elf32_Word *)p;
-	if (desc[0] != GNULINUX_ABI_DESC)
-		return (false);
-
-	/*
-	 * For Linux we encode osrel using the Linux convention of
-	 * 	(version << 16) | (major << 8) | (minor)
-	 * See macro in linux_mib.h
-	 */
-	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
-
-	return (true);
-}
-
 static Elf_Brandnote linux64_brandnote = {
-	.hdr.n_namesz	= sizeof(GNULINUX_ABI_VENDOR),
+	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
 	.hdr.n_descsz	= 16,
 	.hdr.n_type	= 1,
-	.vendor		= GNULINUX_ABI_VENDOR,
+	.vendor		= GNU_ABI_VENDOR,
 	.flags		= BN_TRANSLATE_OSREL,
 	.trans_osrel	= linux_trans_osrel
 };
diff --git a/sys/amd64/linux32/linux32_sysvec.c b/sys/amd64/linux32/linux32_sysvec.c
index 432ed51cbb7e..c2b54369552f 100644
--- a/sys/amd64/linux32/linux32_sysvec.c
+++ b/sys/amd64/linux32/linux32_sysvec.c
@@ -127,7 +127,6 @@ static void	linux_exec_sysvec_init(void *param);
 static int	linux_on_exec_vmspace(struct proc *p,
 		    struct image_params *imgp);
 static void	linux32_fixlimit(struct rlimit *rl, int which);
-static bool	linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
 static void	linux_vdso_install(const void *param);
 static void	linux_vdso_deinstall(const void *param);
 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
@@ -1015,39 +1014,13 @@ linux_vdso_reloc(char *mapping, Elf_Addr offset)
 	}
 }
 
-static char GNU_ABI_VENDOR[] = "GNU";
-static int GNULINUX_ABI_DESC = 0;
-
-static bool
-linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
-{
-	const Elf32_Word *desc;
-	uintptr_t p;
-
-	p = (uintptr_t)(note + 1);
-	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
-
-	desc = (const Elf32_Word *)p;
-	if (desc[0] != GNULINUX_ABI_DESC)
-		return (false);
-
-	/*
-	 * For Linux we encode osrel using the Linux convention of
-	 * 	(version << 16) | (major << 8) | (minor)
-	 * See macro in linux_mib.h
-	 */
-	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
-
-	return (true);
-}
-
 static Elf_Brandnote linux32_brandnote = {
 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
 	.hdr.n_type	= 1,
 	.vendor		= GNU_ABI_VENDOR,
 	.flags		= BN_TRANSLATE_OSREL,
-	.trans_osrel	= linux32_trans_osrel
+	.trans_osrel	= linux_trans_osrel
 };
 
 static Elf32_Brandinfo linux_brand = {
diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c
index 7bf0dbca6eda..0bd8d60dcd93 100644
--- a/sys/arm64/linux/linux_sysvec.c
+++ b/sys/arm64/linux/linux_sysvec.c
@@ -105,7 +105,6 @@ extern const char *linux_syscallnames[];
 
 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
 
-static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
 static void	linux_vdso_install(const void *param);
 static void	linux_vdso_deinstall(const void *param);
 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
@@ -569,27 +568,6 @@ linux_vdso_reloc(char *mapping, Elf_Addr offset)
 	}
 }
 
-static char GNU_ABI_VENDOR[] = "GNU";
-static int GNU_ABI_LINUX = 0;
-
-/* LINUXTODO: deduplicate */
-static bool
-linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
-{
-	const Elf32_Word *desc;
-	uintptr_t p;
-
-	p = (uintptr_t)(note + 1);
-	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
-
-	desc = (const Elf32_Word *)p;
-	if (desc[0] != GNU_ABI_LINUX)
-		return (false);
-
-	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
-	return (true);
-}
-
 static Elf_Brandnote linux64_brandnote = {
 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
 	.hdr.n_descsz	= 16,
diff --git a/sys/compat/linux/linux_elf.c b/sys/compat/linux/linux_elf.c
index 3e0582e2b8c3..43df9508d470 100644
--- a/sys/compat/linux/linux_elf.c
+++ b/sys/compat/linux/linux_elf.c
@@ -67,6 +67,7 @@ __FBSDID("$FreeBSD$");
 #include <machine/../linux/linux.h>
 #endif
 #include <compat/linux/linux_elf.h>
+#include <compat/linux/linux_mib.h>
 #include <compat/linux/linux_misc.h>
 
 struct l_elf_siginfo {
@@ -465,3 +466,25 @@ __linuxN(copyout_strings)(struct image_params *imgp, uintptr_t *stack_base)
 
 	return (0);
 }
+
+bool
+linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
+{
+	const Elf32_Word *desc;
+	uintptr_t p;
+
+	p = (uintptr_t)(note + 1);
+	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
+
+	desc = (const Elf32_Word *)p;
+	if (desc[0] != GNU_ABI_LINUX)
+		return (false);
+	/*
+	 * For Linux we encode osrel using the Linux convention of
+	 * 	(version << 16) | (major << 8) | (minor)
+	 * See macro in linux_mib.h
+	 */
+	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
+
+	return (true);
+}
diff --git a/sys/compat/linux/linux_elf.h b/sys/compat/linux/linux_elf.h
index e0905983070b..87b21c4a14b2 100644
--- a/sys/compat/linux/linux_elf.h
+++ b/sys/compat/linux/linux_elf.h
@@ -33,11 +33,16 @@ struct note_info_list;
 /* Linux core notes are labeled "CORE" */
 #define	LINUX_ABI_VENDOR	"CORE"
 
+/* Elf notes */
+#define	GNU_ABI_VENDOR		"GNU"
+#define	GNU_ABI_LINUX		0
+
 /* This adds "linux32_" and "linux64_" prefixes. */
 #define	__linuxN(x)	__CONCAT(__CONCAT(__CONCAT(linux,__ELF_WORD_SIZE),_),x)
 
 void 	__linuxN(prepare_notes)(struct thread *, struct note_info_list *,
 	    size_t *);
 int	__linuxN(copyout_strings)(struct image_params *, uintptr_t *);
+bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
 
 #endif
diff --git a/sys/i386/linux/linux_sysvec.c b/sys/i386/linux/linux_sysvec.c
index dc41b9595003..39249cb25320 100644
--- a/sys/i386/linux/linux_sysvec.c
+++ b/sys/i386/linux/linux_sysvec.c
@@ -112,7 +112,6 @@ static void	linux_exec_sysvec_init(void *param);
 static int	linux_on_exec_vmspace(struct proc *p,
 		    struct image_params *imgp);
 static void	linux_set_fork_retval(struct thread *td);
-static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
 static void	linux_vdso_install(const void *param);
 static void	linux_vdso_deinstall(const void *param);
 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
@@ -839,32 +838,6 @@ linux_vdso_reloc(char *mapping, Elf_Addr offset)
 	}
 }
 
-static char GNU_ABI_VENDOR[] = "GNU";
-static int GNULINUX_ABI_DESC = 0;
-
-static bool
-linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
-{
-	const Elf32_Word *desc;
-	uintptr_t p;
-
-	p = (uintptr_t)(note + 1);
-	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
-
-	desc = (const Elf32_Word *)p;
-	if (desc[0] != GNULINUX_ABI_DESC)
-		return (false);
-
-	/*
-	 * For Linux we encode osrel using the Linux convention of
-	 * 	(version << 16) | (major << 8) | (minor)
-	 * See macro in linux_mib.h
-	 */
-	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
-
-	return (true);
-}
-
 static Elf_Brandnote linux_brandnote = {
 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
 	.hdr.n_descsz	= 16,	/* XXX at least 16 */