git: 9684658e35ab - main - libc/csu: Unify INIT_RELOCS across architectures

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Fri, 18 Oct 2024 23:50:28 UTC
The branch main has been updated by jrtc27:

URL: https://cgit.FreeBSD.org/src/commit/?id=9684658e35ab033c79e0519e3681d9a194976b71

commit 9684658e35ab033c79e0519e3681d9a194976b71
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2024-10-18 23:48:52 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2024-10-18 23:48:52 +0000

    libc/csu: Unify INIT_RELOCS across architectures
    
    Some architectures don't need any arguments, whilst others need auxargs,
    which they get by passing in env thanks to INIT_RELOCS referencing the
    local variable in __libc_start1(_gcrt) by name. This is unnecessarily
    confusing, fragile (one has to look at INIT_IRELOCS's definition to see
    that it uses env) and duplicates code between architectures.
    
    Instead, implement it more like rtld-elf. Each architecture provides an
    ifunc_init that takes the auxargs directly, and those that don't need it
    can just ignore it.
    
    Reviewed by:    kib
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D47188
---
 lib/libc/csu/aarch64/Makefile.inc    |  3 +--
 lib/libc/csu/aarch64/reloc.c         |  7 +++++++
 lib/libc/csu/amd64/Makefile.inc      |  3 +--
 lib/libc/csu/amd64/reloc.c           |  4 +++-
 lib/libc/csu/arm/Makefile.inc        |  3 +--
 lib/libc/csu/i386/Makefile.inc       |  3 +--
 lib/libc/csu/i386/reloc.c            |  4 +++-
 lib/libc/csu/libc_start1.c           | 28 ++++++++++++++++++++--------
 lib/libc/csu/powerpc/Makefile.inc    |  3 +--
 lib/libc/csu/powerpc64/Makefile.inc  |  3 +--
 lib/libc/csu/powerpc64/reloc.c       |  9 +--------
 lib/libc/csu/powerpcspe/Makefile.inc |  3 +--
 lib/libc/csu/riscv/Makefile.inc      |  3 +--
 lib/libc/csu/riscv/reloc.c           |  9 +--------
 14 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/lib/libc/csu/aarch64/Makefile.inc b/lib/libc/csu/aarch64/Makefile.inc
index b3420a638164..6c315c5e2624 100644
--- a/lib/libc/csu/aarch64/Makefile.inc
+++ b/lib/libc/csu/aarch64/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_RELA \
-		-DINIT_IRELOCS=""
+CFLAGS+=	-DCRT_IRELOC_RELA
diff --git a/lib/libc/csu/aarch64/reloc.c b/lib/libc/csu/aarch64/reloc.c
index ead48a8ad4fb..4ba7920bcb07 100644
--- a/lib/libc/csu/aarch64/reloc.c
+++ b/lib/libc/csu/aarch64/reloc.c
@@ -24,6 +24,13 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+
+static void
+ifunc_init(const Elf_Auxinfo *aux __unused)
+{
+}
+
 static void
 crt1_handle_rela(const Elf_Rela *r)
 {
diff --git a/lib/libc/csu/amd64/Makefile.inc b/lib/libc/csu/amd64/Makefile.inc
index f14033217580..6c315c5e2624 100644
--- a/lib/libc/csu/amd64/Makefile.inc
+++ b/lib/libc/csu/amd64/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_RELA \
-		-DINIT_IRELOCS="init_cpu_features()"
+CFLAGS+=	-DCRT_IRELOC_RELA
diff --git a/lib/libc/csu/amd64/reloc.c b/lib/libc/csu/amd64/reloc.c
index 6424d69fbd5c..f1f83db9a391 100644
--- a/lib/libc/csu/amd64/reloc.c
+++ b/lib/libc/csu/amd64/reloc.c
@@ -23,6 +23,8 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+
 #include <machine/specialreg.h>
 #include <machine/cpufunc.h>
 
@@ -30,7 +32,7 @@ static uint32_t cpu_feature, cpu_feature2;
 static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
 
 static void
-init_cpu_features(void)
+ifunc_init(const Elf_Auxinfo *aux __unused)
 {
 	u_int p[4];
 
diff --git a/lib/libc/csu/arm/Makefile.inc b/lib/libc/csu/arm/Makefile.inc
index 2534e6579f38..ddead75f874d 100644
--- a/lib/libc/csu/arm/Makefile.inc
+++ b/lib/libc/csu/arm/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_SUPPRESS \
-		-DINIT_IRELOCS=""
+CFLAGS+=	-DCRT_IRELOC_SUPPRESS
diff --git a/lib/libc/csu/i386/Makefile.inc b/lib/libc/csu/i386/Makefile.inc
index f3f8c2b176ce..32018000e1f2 100644
--- a/lib/libc/csu/i386/Makefile.inc
+++ b/lib/libc/csu/i386/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_REL \
-		-DINIT_IRELOCS="init_cpu_features()"
+CFLAGS+=	-DCRT_IRELOC_REL
diff --git a/lib/libc/csu/i386/reloc.c b/lib/libc/csu/i386/reloc.c
index 1c9ec173facc..7097f58d8f26 100644
--- a/lib/libc/csu/i386/reloc.c
+++ b/lib/libc/csu/i386/reloc.c
@@ -23,6 +23,8 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+
 #include <machine/specialreg.h>
 #include <machine/cpufunc.h>
 
@@ -30,7 +32,7 @@ static uint32_t cpu_feature, cpu_feature2;
 static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
 
 static void
-init_cpu_features(void)
+ifunc_init(const Elf_Auxinfo *aux __unused)
 {
 	u_int cpuid_supported, p[4];
 
diff --git a/lib/libc/csu/libc_start1.c b/lib/libc/csu/libc_start1.c
index f0e708e405ce..045ea1e68141 100644
--- a/lib/libc/csu/libc_start1.c
+++ b/lib/libc/csu/libc_start1.c
@@ -137,6 +137,24 @@ handle_argv(int argc, char *argv[], char **env)
 	}
 }
 
+static void
+handle_irelocs(char *env[])
+{
+#ifndef CRT_IRELOC_SUPPRESS
+	const Elf_Auxinfo *aux;
+
+	/* Find the auxiliary vector on the stack. */
+	while (*env++ != 0)	/* Skip over environment, and NULL terminator */
+		;
+	aux = (const Elf_Auxinfo *)env;
+
+	ifunc_init(aux);
+	process_irelocs();
+#else
+	(void)env;
+#endif
+}
+
 void
 __libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void),
     int (*mainX)(int, char *[], char *[]))
@@ -146,10 +164,7 @@ __libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void),
 	if (&_DYNAMIC != NULL) {
 		atexit(cleanup);
 	} else {
-#ifndef CRT_IRELOC_SUPPRESS
-		INIT_IRELOCS;
-		process_irelocs();
-#endif
+		handle_irelocs(env);
 		_init_tls();
 	}
 
@@ -171,10 +186,7 @@ __libc_start1_gcrt(int argc, char *argv[], char *env[],
 	if (&_DYNAMIC != NULL) {
 		atexit(cleanup);
 	} else {
-#ifndef CRT_IRELOC_SUPPRESS
-		INIT_IRELOCS;
-		process_irelocs();
-#endif
+		handle_irelocs(env);
 		_init_tls();
 	}
 
diff --git a/lib/libc/csu/powerpc/Makefile.inc b/lib/libc/csu/powerpc/Makefile.inc
index 2534e6579f38..ddead75f874d 100644
--- a/lib/libc/csu/powerpc/Makefile.inc
+++ b/lib/libc/csu/powerpc/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_SUPPRESS \
-		-DINIT_IRELOCS=""
+CFLAGS+=	-DCRT_IRELOC_SUPPRESS
diff --git a/lib/libc/csu/powerpc64/Makefile.inc b/lib/libc/csu/powerpc64/Makefile.inc
index 5d59d40eb393..6c315c5e2624 100644
--- a/lib/libc/csu/powerpc64/Makefile.inc
+++ b/lib/libc/csu/powerpc64/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_RELA \
-		-DINIT_IRELOCS="init_cpu_features(env)"
+CFLAGS+=	-DCRT_IRELOC_RELA
diff --git a/lib/libc/csu/powerpc64/reloc.c b/lib/libc/csu/powerpc64/reloc.c
index 41419bf0e6c2..5637e6534197 100644
--- a/lib/libc/csu/powerpc64/reloc.c
+++ b/lib/libc/csu/powerpc64/reloc.c
@@ -24,15 +24,8 @@ static uint32_t cpu_features;
 static uint32_t cpu_features2;
 
 static void
-init_cpu_features(char **env)
+ifunc_init(const Elf_Auxinfo *aux)
 {
-	const Elf_Auxinfo *aux;
-
-	/* Find the auxiliary vector on the stack. */
-	while (*env++ != 0)	/* Skip over environment, and NULL terminator */
-		;
-	aux = (const Elf_Auxinfo *)env;
-
 	/* Digest the auxiliary vector. */
 	for (;  aux->a_type != AT_NULL; aux++) {
 		switch (aux->a_type) {
diff --git a/lib/libc/csu/powerpcspe/Makefile.inc b/lib/libc/csu/powerpcspe/Makefile.inc
index 2534e6579f38..ddead75f874d 100644
--- a/lib/libc/csu/powerpcspe/Makefile.inc
+++ b/lib/libc/csu/powerpcspe/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_SUPPRESS \
-		-DINIT_IRELOCS=""
+CFLAGS+=	-DCRT_IRELOC_SUPPRESS
diff --git a/lib/libc/csu/riscv/Makefile.inc b/lib/libc/csu/riscv/Makefile.inc
index 5d59d40eb393..6c315c5e2624 100644
--- a/lib/libc/csu/riscv/Makefile.inc
+++ b/lib/libc/csu/riscv/Makefile.inc
@@ -1,4 +1,3 @@
 #
 
-CFLAGS+=	-DCRT_IRELOC_RELA \
-		-DINIT_IRELOCS="init_cpu_features(env)"
+CFLAGS+=	-DCRT_IRELOC_RELA
diff --git a/lib/libc/csu/riscv/reloc.c b/lib/libc/csu/riscv/reloc.c
index 036ea3de8701..6ae85085089b 100644
--- a/lib/libc/csu/riscv/reloc.c
+++ b/lib/libc/csu/riscv/reloc.c
@@ -24,15 +24,8 @@
 static unsigned long elf_hwcap;
 
 static void
-init_cpu_features(char **env)
+ifunc_init(const Elf_Auxinfo *aux)
 {
-	const Elf_Auxinfo *aux;
-
-	/* Find the auxiliary vector on the stack. */
-	while (*env++ != 0)	/* Skip over environment, and NULL terminator */
-		;
-	aux = (const Elf_Auxinfo *)env;
-
 	/* Digest the auxiliary vector. */
 	for (; aux->a_type != AT_NULL; aux++) {
 		switch (aux->a_type) {