Commit 681b19bc7b for openssl.org

commit 681b19bc7b1f917d4433bb36da503e3a7ca6dbf8
Author: Bob Beck <beck@openssl.org>
Date:   Tue Mar 24 12:26:37 2026 -0600

    Revert "Make cpuid_setup non-constructor"

    This reverts commit 1d770fc6a9a0a7d6e20f3232180b80c366c2d4df.

    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    MergeDate: Thu Apr  2 07:18:05 2026
    (Merged from https://github.com/openssl/openssl/pull/30557)

diff --git a/crypto/armcap.c b/crypto/armcap.c
index aa098a83ee..e5ba2132ff 100644
--- a/crypto/armcap.c
+++ b/crypto/armcap.c
@@ -69,6 +69,10 @@ uint32_t OPENSSL_rdtsc(void)

 /* First determine if getauxval() is available (OSSL_IMPLEMENT_GETAUXVAL) */

+#if defined(__GNUC__) && __GNUC__ >= 2
+void OPENSSL_cpuid_setup(void) __attribute__((constructor));
+#endif
+
 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 16)
 #include <sys/auxv.h>
diff --git a/crypto/dllmain.c b/crypto/dllmain.c
index a0effb9619..0ca19e63ec 100644
--- a/crypto/dllmain.c
+++ b/crypto/dllmain.c
@@ -30,13 +30,14 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 {
     switch (fdwReason) {
     case DLL_PROCESS_ATTACH:
+        OPENSSL_cpuid_setup();
         break;
     case DLL_THREAD_ATTACH:
         break;
     case DLL_THREAD_DETACH:
-# ifndef __CYGWIN__
+#ifndef __CYGWIN__
         OPENSSL_thread_stop();
-# endif
+#endif
         break;
     case DLL_PROCESS_DETACH:
 #if defined(OSSL_DLLMAIN_DESTRUCTOR)
diff --git a/crypto/perlasm/x86gas.pl b/crypto/perlasm/x86gas.pl
index d810fd5479..04e0d043b4 100644
--- a/crypto/perlasm/x86gas.pl
+++ b/crypto/perlasm/x86gas.pl
@@ -14,6 +14,8 @@ package x86gas;
 $::lbdecor=$::aout?"L":".L";		# local label decoration
 $nmdecor=($::aout or $::coff)?"_":"";	# external name decoration

+$initseg="";
+
 $align=16;
 $align=log($align)/log(2) if ($::aout);
 $com_start="#" if ($::aout or $::coff);
@@ -171,6 +173,7 @@ sub ::file_end
 	elsif ($::elf)	{ push (@out,"$tmp,4\n"); }
 	else		{ push (@out,"$tmp\n"); }
     }
+    push(@out,$initseg) if ($initseg);
     if ($::elf) {
 	push(@out,"
 	.section \".note.gnu.property\", \"a\"
@@ -234,6 +237,48 @@ sub ::picmeup
     {	&::lea($dst,&::DWP($sym));	}
 }

+sub ::initseg
+{ my $f=$nmdecor.shift;
+
+    if ($::android)
+    {	$initseg.=<<___;
+.section	.init_array
+.align	4
+.long	$f
+___
+    }
+    elsif ($::elf)
+    {	$initseg.=<<___;
+.section	.init
+	call	$f
+___
+    }
+    elsif ($::coff)
+    {   $initseg.=<<___;	# applies to both Cygwin and Mingw
+.section	.ctors
+.long	$f
+___
+    }
+    elsif ($::macosx)
+    {	$initseg.=<<___;
+.mod_init_func
+.align 2
+.long   $f
+___
+    }
+    elsif ($::aout)
+    {	my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
+	$initseg.=".text\n";
+	$initseg.=".type	$ctor,\@function\n" if ($::pic);
+	$initseg.=<<___;	# OpenBSD way...
+.globl	$ctor
+.align	2
+$ctor:
+	jmp	$f
+___
+    }
+}
+
 sub ::dataseg
 {   push(@out,".data\n");   }

diff --git a/crypto/perlasm/x86masm.pl b/crypto/perlasm/x86masm.pl
index 1c567f23dd..98dedec8de 100644
--- a/crypto/perlasm/x86masm.pl
+++ b/crypto/perlasm/x86masm.pl
@@ -14,6 +14,7 @@ package x86masm;
 $::lbdecor="\$L";	# local label decoration
 $nmdecor="_";		# external name decoration

+$initseg="";
 $segment="";

 sub ::generic
@@ -148,6 +149,7 @@ ___
 	grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
 	push (@out,$comm);
     }
+    push (@out,$initseg) if ($initseg);
     push (@out,"END\n");
 }

@@ -181,6 +183,17 @@ sub ::picmeup
     &::lea($dst,&::DWP($sym));
 }

+sub ::initseg
+{ my $f=$nmdecor.shift;
+
+    $initseg.=<<___;
+.CRT\$XCU	SEGMENT DWORD PUBLIC 'DATA'
+EXTERN	$f:NEAR
+DD	$f
+.CRT\$XCU	ENDS
+___
+}
+
 sub ::dataseg
 {   push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA";   }

diff --git a/crypto/perlasm/x86nasm.pl b/crypto/perlasm/x86nasm.pl
index 110ecddab3..0d223a617b 100644
--- a/crypto/perlasm/x86nasm.pl
+++ b/crypto/perlasm/x86nasm.pl
@@ -15,6 +15,8 @@ $::lbdecor="L\$";		# local label decoration
 $nmdecor="_";			# external name decoration
 $drdecor=$::mwerks?".":"";	# directive decoration

+$initseg="";
+
 sub ::generic
 { my $opcode=shift;
   my $tmp;
@@ -131,6 +133,7 @@ ___
 	grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
 	push (@out,$comm)
     }
+    push (@out,$initseg) if ($initseg);
 }

 sub ::comment {   foreach (@_) { push(@out,"\t; $_\n"); }   }
@@ -158,6 +161,17 @@ sub ::picmeup
     &::lea($dst,&::DWP($sym));
 }

+sub ::initseg
+{ my $f=$nmdecor.shift;
+    if ($::win32)
+    {	$initseg=<<___;
+segment	.CRT\$XCU data align=4
+extern	$f
+dd	$f
+___
+    }
+}
+
 sub ::dataseg
 {   if ($mwerks)	{ push(@out,".section\t.data,4\n");   }
     else		{ push(@out,"section\t.data align=4\n"); }
diff --git a/crypto/ppccap.c b/crypto/ppccap.c
index 91a3e09573..69b3254b6d 100644
--- a/crypto/ppccap.c
+++ b/crypto/ppccap.c
@@ -134,6 +134,9 @@ static unsigned long getauxval(unsigned long key)
 #define HWCAP_ARCH_3_00 (1U << 23)
 #define HWCAP_ARCH_3_1 (1U << 18)

+#if defined(__GNUC__) && __GNUC__ >= 2
+__attribute__((constructor))
+#endif
 void OPENSSL_cpuid_setup(void)
 {
     char *e;
diff --git a/crypto/riscvcap.c b/crypto/riscvcap.c
index 8f62435d5b..b4cfbdc429 100644
--- a/crypto/riscvcap.c
+++ b/crypto/riscvcap.c
@@ -129,6 +129,9 @@ size_t riscv_vlen(void)
     return vlen;
 }

+#if defined(__GNUC__) && __GNUC__ >= 2
+__attribute__((constructor))
+#endif
 void OPENSSL_cpuid_setup(void)
 {
     char *e;
diff --git a/crypto/s390xcpuid.pl b/crypto/s390xcpuid.pl
index 36457d798a..2860a17048 100755
--- a/crypto/s390xcpuid.pl
+++ b/crypto/s390xcpuid.pl
@@ -527,6 +527,11 @@ s390x_flip_endian64:
 ___
 }

+$code.=<<___;
+.section	.init
+	brasl	$ra,OPENSSL_cpuid_setup
+___
+
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
 print $code;
 close STDOUT or die "error closing STDOUT: $!";	# force flush
diff --git a/crypto/sparccpuid.S b/crypto/sparccpuid.S
index fa58db98b4..aa92241197 100644
--- a/crypto/sparccpuid.S
+++ b/crypto/sparccpuid.S
@@ -422,3 +422,7 @@ _sparcv9_vis1_instrument_bus2:
 	sub	%o3,%o1,%o0
 .type	_sparcv9_vis1_instrument_bus2,#function
 .size	_sparcv9_vis1_instrument_bus2,.-_sparcv9_vis1_instrument_bus2
+
+.section	".init",#alloc,#execinstr
+	call	OPENSSL_cpuid_setup
+	nop
diff --git a/crypto/x86_64cpuid.pl b/crypto/x86_64cpuid.pl
index 6bfe09084d..cf8a7605a6 100644
--- a/crypto/x86_64cpuid.pl
+++ b/crypto/x86_64cpuid.pl
@@ -30,6 +30,9 @@ print<<___;
 #include crypto/cryptlib.h
 .extern		OPENSSL_cpuid_setup
 .hidden		OPENSSL_cpuid_setup
+.section	.init
+	call	OPENSSL_cpuid_setup
+
 .hidden	OPENSSL_ia32cap_P
 .comm	OPENSSL_ia32cap_P,40,4	# <--Should match with internal/cryptlib.h OPENSSL_IA32CAP_P_MAX_INDEXES
 .text
diff --git a/crypto/x86cpuid.pl b/crypto/x86cpuid.pl
index 329a57618f..32f6e43523 100644
--- a/crypto/x86cpuid.pl
+++ b/crypto/x86cpuid.pl
@@ -493,6 +493,8 @@ my $rdop = shift;
 &gen_random("rdrand");
 &gen_random("rdseed");

+&initseg("OPENSSL_cpuid_setup");
+
 &hidden("OPENSSL_cpuid_setup");
 &hidden("OPENSSL_ia32cap_P");