git: 0303938539f3 - main - x86: microoptimize static PIE startup

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 11 Mar 2023 22:51:17 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=0303938539f3f12da65128fc67f883efe82dc125

commit 0303938539f3f12da65128fc67f883efe82dc125
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-11-01 01:42:50 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-03-11 22:50:04 +0000

    x86: microoptimize static PIE startup
    
    Do not call CPUID on each ireloc, instead call it once and cache
    results, similar to how it is done on powerpc64.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 weeks
    Differential revision:  https://reviews.freebsd.org/D37220
---
 lib/libc/csu/amd64/Makefile.inc |  2 +-
 lib/libc/csu/amd64/reloc.c      | 14 ++++++++++----
 lib/libc/csu/i386/Makefile.inc  |  2 +-
 lib/libc/csu/i386/reloc.c       | 14 ++++++++++----
 4 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/lib/libc/csu/amd64/Makefile.inc b/lib/libc/csu/amd64/Makefile.inc
index b3420a638164..f14033217580 100644
--- a/lib/libc/csu/amd64/Makefile.inc
+++ b/lib/libc/csu/amd64/Makefile.inc
@@ -1,4 +1,4 @@
 #
 
 CFLAGS+=	-DCRT_IRELOC_RELA \
-		-DINIT_IRELOCS=""
+		-DINIT_IRELOCS="init_cpu_features()"
diff --git a/lib/libc/csu/amd64/reloc.c b/lib/libc/csu/amd64/reloc.c
index adb52e42a32c..0a5a24929954 100644
--- a/lib/libc/csu/amd64/reloc.c
+++ b/lib/libc/csu/amd64/reloc.c
@@ -29,13 +29,13 @@ __FBSDID("$FreeBSD$");
 #include <machine/specialreg.h>
 #include <machine/cpufunc.h>
 
+static uint32_t cpu_feature, cpu_feature2;
+static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
+
 static void
-crt1_handle_rela(const Elf_Rela *r)
+init_cpu_features(void)
 {
-	Elf_Addr *ptr, *where, target;
 	u_int p[4];
-	uint32_t cpu_feature, cpu_feature2;
-	uint32_t cpu_stdext_feature, cpu_stdext_feature2;
 
 	do_cpuid(1, p);
 	cpu_feature = p[3];
@@ -49,6 +49,12 @@ crt1_handle_rela(const Elf_Rela *r)
 		cpu_stdext_feature = 0;
 		cpu_stdext_feature2 = 0;
 	}
+}
+
+static void
+crt1_handle_rela(const Elf_Rela *r)
+{
+	Elf_Addr *ptr, *where, target;
 
 	switch (ELF_R_TYPE(r->r_info)) {
 	case R_X86_64_IRELATIVE:
diff --git a/lib/libc/csu/i386/Makefile.inc b/lib/libc/csu/i386/Makefile.inc
index ac0984df2349..f3f8c2b176ce 100644
--- a/lib/libc/csu/i386/Makefile.inc
+++ b/lib/libc/csu/i386/Makefile.inc
@@ -1,4 +1,4 @@
 #
 
 CFLAGS+=	-DCRT_IRELOC_REL \
-		-DINIT_IRELOCS=""
+		-DINIT_IRELOCS="init_cpu_features()"
diff --git a/lib/libc/csu/i386/reloc.c b/lib/libc/csu/i386/reloc.c
index 13438035841d..f99b1089cf47 100644
--- a/lib/libc/csu/i386/reloc.c
+++ b/lib/libc/csu/i386/reloc.c
@@ -29,13 +29,13 @@ __FBSDID("$FreeBSD$");
 #include <machine/specialreg.h>
 #include <machine/cpufunc.h>
 
+static uint32_t cpu_feature, cpu_feature2;
+static uint32_t cpu_stdext_feature, cpu_stdext_feature2;
+
 static void
-crt1_handle_rel(const Elf_Rel *r)
+init_cpu_features(void)
 {
-	Elf_Addr *where, target;
 	u_int cpuid_supported, p[4];
-	uint32_t cpu_feature, cpu_feature2;
-	uint32_t cpu_stdext_feature, cpu_stdext_feature2;
 
 	__asm __volatile(
 	    "	pushfl\n"
@@ -72,6 +72,12 @@ crt1_handle_rel(const Elf_Rel *r)
 		cpu_stdext_feature = 0;
 		cpu_stdext_feature2 = 0;
 	}
+}
+
+static void
+crt1_handle_rel(const Elf_Rel *r)
+{
+	Elf_Addr *where, target;
 
 	switch (ELF_R_TYPE(r->r_info)) {
 	case R_386_IRELATIVE: