Skip to contentSkip to conversation

rseq/selftests: Use weak symbol reference, not definition, to link with glibc

PATCH Applied Sean Christopherson 4 participants 5 messages Aug 19, 2025 – Sep 13, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rseq/selftests: Use weak symbol reference, not definition, to link with glibc

Add "extern" to the glibc-defined weak rseq symbols to convert the rseq selftest's usage from weak symbol definitions to weak symbol _references_. Effectively re-defining the glibc symbols wreaks havoc when building with -fno-common, e.g. generates segfaults when running multi-threaded programs, as dynamically linked applications end up with multiple versions of the symbols.

Building with -fcommon, which until recently has the been the default for GCC and clang, papers over the bug by allowing the linker to resolve the weak/tentative definition to glibc's "real" definition.

Note, the symbol itself (or rather its address), not the value of the symbol, is set to 0/NULL for unresolved weak symbol references, as the symbol doesn't exist and thus can't have a value. Check for a NULL rseq size pointer to handle the scenario where the test is statically linked against a libc that doesn't support rseq in any capacity.

Fixes 3bcbc20942db ("selftests/rseq: Play nice with binaries statically linked against glibc 2.35+")
---
 tools/testing/selftests/rseq/rseq.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/rseq/rseq.c +4 -4
index 663a9cef1952..dcac5cbe7933 100644
@@ -40,9 +40,9 @@
  * Define weak versions to play nice with binaries that are statically linked
  * against a libc that doesn't support registering its own rseq.
  */
-__weak ptrdiff_t __rseq_offset;
-__weak unsigned int __rseq_size;
-__weak unsigned int __rseq_flags;
+extern __weak ptrdiff_t __rseq_offset;
+extern __weak unsigned int __rseq_size;
+extern __weak unsigned int __rseq_flags;
 
 static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
 static const unsigned int *libc_rseq_size_p = &__rseq_size;
@@ -209,7 +209,7 @@ void rseq_init(void)
 	 * libc not having registered a restartable sequence.  Try to find the
 	 * symbols if that's the case.
 	 */
-	if (!*libc_rseq_size_p) {
+	if (!libc_rseq_size_p || !*libc_rseq_size_p) {
 		libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
 		libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
 		libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");

base-commit: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9

Signature · 1 line
2.51.0.rc1.167.g924127e9c0-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Sean Christopherson

Add "extern" to the glibc-defined weak rseq symbols to convert the rseq selftest's usage from weak symbol definitions to weak symbol _references_. Effectively re-defining the glibc symbols wreaks havoc when building with -fno-common, e.g. generates segfaults when running multi-threaded programs, as dynamically linked applications end up with multiple versions of the symbols.

Building with -fcommon, which until recently has the been the default for GCC and clang, papers over the bug by allowing the linker to resolve the weak/tentative definition to glibc's "real" definition.

Note, the symbol itself (or rather its address), not the value of the symbol, is set to 0/NULL for unresolved weak symbol references, as the symbol doesn't exist and thus can't have a value. Check for a NULL rseq size pointer to handle the scenario where the test is statically linked against a libc that doesn't support rseq in any capacity.

Quoted 6 lines
Fixes 3bcbc20942db ("selftests/rseq: Play nice with binaries statically linked against glibc 2.35+")

+ CC: Michael Jeanson <[email protected]>

tools/testing/selftests/rseq/rseq.c line 41

Quoted 11 lines
---
  tools/testing/selftests/rseq/rseq.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/rseq/rseq.c +0 -0
index 663a9cef1952..dcac5cbe7933 100644
@@ -40,9 +40,9 @@
   * Define weak versions to play nice with binaries that are statically linked
   * against a libc that doesn't support registering its own rseq.

We should therefore update the comment above to e.g.:

/*
* Define weak symbol references to play nice with binaries that are
* statically linked against a libc that doesn't support registering its
* own rseq.
*/

For the rest:

Michael, can you try it out ?

Kienan, we may want to add a configuration forcing "-fno-common" to our CI, this problematic pattern may be hiding other issues elsewhere. I'm thinking of LTTng-UST tracepoint headers and libside headers.

Sean, do you want to contribute the fix to librseq as well ?

Thanks everyone for looking into this. I'll be back from vacation next week and will resume normal operations. :-)

Mathieu

Quoted 21 lines
tools/testing/selftests/rseq/rseq.c +4 -4
   */
-__weak ptrdiff_t __rseq_offset;
-__weak unsigned int __rseq_size;
-__weak unsigned int __rseq_flags;
+extern __weak ptrdiff_t __rseq_offset;
+extern __weak unsigned int __rseq_size;
+extern __weak unsigned int __rseq_flags;
 
  static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
  static const unsigned int *libc_rseq_size_p = &__rseq_size;
@@ -209,7 +209,7 @@ void rseq_init(void)
  	 * libc not having registered a restartable sequence.  Try to find the
  	 * symbols if that's the case.
  	 */
-	if (!*libc_rseq_size_p) {
+	if (!libc_rseq_size_p || !*libc_rseq_size_p) {
  		libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
  		libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
  		libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");

base-commit: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#2 lore raw
Quoted 2 lines · Mathieu Desnoyers

Michael, can you try it out ?

Will do.

Quoted 4 lines

Kienan, we may want to add a configuration forcing "-fno-common" to our CI, this problematic pattern may be hiding other issues elsewhere. I'm thinking of LTTng-UST tracepoint headers and libside headers.

AFAIK, -fno-common has been the default since GCC 10 so it's already well tested in CI, there are a few fixes from around 2020 in LTTng related to this.

Quoted 2 lines

Sean, do you want to contribute the fix to librseq as well ?

Librseq currently doesn't have the weak symbols like the selftests so the fix doesn't really apply.

Quoted 29 lines

Thanks everyone for looking into this. I'll be back from vacation next week and will resume normal operations. :-)

Mathieu

Quoted 21 lines
    */
tools/testing/selftests/rseq/rseq.c +4 -4
-__weak ptrdiff_t __rseq_offset;
-__weak unsigned int __rseq_size;
-__weak unsigned int __rseq_flags;
+extern __weak ptrdiff_t __rseq_offset;
+extern __weak unsigned int __rseq_size;
+extern __weak unsigned int __rseq_flags;
 
   static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
   static const unsigned int *libc_rseq_size_p = &__rseq_size;
@@ -209,7 +209,7 @@ void rseq_init(void)
   	 * libc not having registered a restartable sequence.  Try to find the
   	 * symbols if that's the case.
   	 */
-	if (!*libc_rseq_size_p) {
+	if (!libc_rseq_size_p || !*libc_rseq_size_p) {
   		libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
   		libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
   		libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");

base-commit: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9

#4 lore raw
In reply to Sean Christopherson
The following commit has been merged into the core/rseq branch of tip:

The following commit has been merged into the core/rseq branch of tip:

Commit-ID:     a001cd248ab244633c5fabe4f7c707e13fc1d1cc
Gitweb:        https://git.kernel.org/tip/a001cd248ab244633c5fabe4f7c707e13fc1d1cc
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Tue, 19 Aug 2025 15:29:44 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 13 Sep 2025 19:51:59 +02:00

rseq/selftests: Use weak symbol reference, not definition, to link with glibc

Add "extern" to the glibc-defined weak rseq symbols to convert the rseq selftest's usage from weak symbol definitions to weak symbol _references_. Effectively re-defining the glibc symbols wreaks havoc when building with -fno-common, e.g. generates segfaults when running multi-threaded programs, as dynamically linked applications end up with multiple versions of the symbols.

Building with -fcommon, which until recently has the been the default for GCC and clang, papers over the bug by allowing the linker to resolve the weak/tentative definition to glibc's "real" definition.

Note, the symbol itself (or rather its address), not the value of the symbol, is set to 0/NULL for unresolved weak symbol references, as the symbol doesn't exist and thus can't have a value. Check for a NULL rseq size pointer to handle the scenario where the test is statically linked against a libc that doesn't support rseq in any capacity.

Fixes 3bcbc20942db ("selftests/rseq: Play nice with binaries statically linked against glibc 2.35+")
---
 tools/testing/selftests/rseq/rseq.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/rseq/rseq.c +4 -4
index 663a9ce..dcac5cb 100644
@@ -40,9 +40,9 @@
  * Define weak versions to play nice with binaries that are statically linked
  * against a libc that doesn't support registering its own rseq.
  */
-__weak ptrdiff_t __rseq_offset;
-__weak unsigned int __rseq_size;
-__weak unsigned int __rseq_flags;
+extern __weak ptrdiff_t __rseq_offset;
+extern __weak unsigned int __rseq_size;
+extern __weak unsigned int __rseq_flags;
 
 static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
 static const unsigned int *libc_rseq_size_p = &__rseq_size;
@@ -209,7 +209,7 @@ void rseq_init(void)
 	 * libc not having registered a restartable sequence.  Try to find the
 	 * symbols if that's the case.
 	 */
-	if (!*libc_rseq_size_p) {
+	if (!libc_rseq_size_p || !*libc_rseq_size_p) {
 		libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
 		libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
 		libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");