From df4b97c70892ea1d3fd8e9c7db5eba0dc070b625 Mon Sep 17 00:00:00 2001
From: Oliver Gutierrez <ogutsua@gmail.com>
Date: Tue, 14 Sep 2021 00:11:06 +0200
Subject: [PATCH 1/4] build: Split out the names of the wrapped C library
 functions

A subsequent commit will add more wrappers to cover new symbol versions
introduced in glibc-2.34. This will make it easier to read because it
won't disrupt the rest of the build parameters.

https://github.com/containers/toolbox/issues/821
---
 src/go-build-wrapper | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/go-build-wrapper b/src/go-build-wrapper
index 0d27120da..83d4fcb98 100755
--- a/src/go-build-wrapper
+++ b/src/go-build-wrapper
@@ -16,6 +16,8 @@
 #
 
 
+readonly LIBC_WRAPPERS="--wrap,pthread_sigmask"
+
 if [ "$#" -ne 4 ]; then
     echo "go-build-wrapper: wrong arguments" >&2
     echo "Usage: go-build-wrapper [SOURCE DIR] [OUTPUT DIR] [VERSION] [libc-wrappers.a]" >&2
@@ -27,5 +29,5 @@ if ! cd "$1"; then
     exit 1
 fi
 
-go build -trimpath -ldflags "-extldflags '-Wl,--wrap,pthread_sigmask $4' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"
+go build -trimpath -ldflags "-extldflags '-Wl,$LIBC_WRAPPERS $4' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"
 exit "$?"

From 9ce8134ec8ca654ed89e9d14dbc8dfc79509c8ef Mon Sep 17 00:00:00 2001
From: Oliver Gutierrez <ogutsua@gmail.com>
Date: Tue, 14 Sep 2021 00:35:39 +0200
Subject: [PATCH 2/4] libc-wrappers: Add a comment

A subsequent commit will add more wrappers to cover new symbol versions
introduced in glibc-2.34. This will make it easier to read by clearly
segregating the file into separate sections for each wrapper.

https://github.com/containers/toolbox/issues/821
---
 src/libc-wrappers/libc-wrappers.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/libc-wrappers/libc-wrappers.c b/src/libc-wrappers/libc-wrappers.c
index 7b402bc2f..3842f1fa6 100644
--- a/src/libc-wrappers/libc-wrappers.c
+++ b/src/libc-wrappers/libc-wrappers.c
@@ -18,6 +18,11 @@
 #include <signal.h>
 
 
+/*
+ * pthread_sigmask < GLIBC_2.32
+ */
+
+
 #if defined __aarch64__
 __asm__(".symver pthread_sigmask,pthread_sigmask@GLIBC_2.17");
 #elif defined __arm__

From 2f9f2db63a6b3897c8d1f9c294b556b1852b26ad Mon Sep 17 00:00:00 2001
From: Oliver Gutierrez <ogutsua@gmail.com>
Date: Fri, 2 Jul 2021 13:45:01 +0100
Subject: [PATCH 3/4] libc-wrappers: Avoid new versions of pthread_* symbols
 from glibc-2.34

Recently, glibc-2.34, which is used by Fedora 35 onwards, added new
versions of the pthread_attr_getstacksize [1], pthread_create [2] and
pthread_detach [3] symbols as part of the libpthread removal
project [4]:
  $ objdump -T /usr/bin/toolbox | grep GLIBC_2.34
  ...
  0000000000000000      DF *UND*	0000000000000000  GLIBC_2.34
    pthread_detach
  0000000000000000      DF *UND*	0000000000000000  GLIBC_2.34
    pthread_create
  0000000000000000      DF *UND*	0000000000000000  GLIBC_2.34
    pthread_attr_getstacksize

This means that /usr/bin/toolbox binaries built against glibc-2.34 on
newer Fedoras pick up the latest version of the symbols and fail to run
against older glibcs in older Fedoras.

[1] glibc commit ee092efed40d667b
    https://sourceware.org/git/?p=glibc.git;a=commit;h=ee092efed40d667b

[2] glibc commit f47f1d91af985a90
    https://sourceware.org/git/?p=glibc.git;a=commit;h=f47f1d91af985a90

[3] glibc commit df65f897e9501aa5
    https://sourceware.org/git/?p=glibc.git;a=commit;h=df65f897e9501aa5

[4] https://sourceware.org/ml/libc-alpha/2019-10/msg00080.html

https://github.com/containers/toolbox/issues/821
---
 src/go-build-wrapper              |  2 +-
 src/libc-wrappers/libc-wrappers.c | 88 +++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/src/go-build-wrapper b/src/go-build-wrapper
index 83d4fcb98..2503d7f2f 100755
--- a/src/go-build-wrapper
+++ b/src/go-build-wrapper
@@ -16,7 +16,7 @@
 #
 
 
-readonly LIBC_WRAPPERS="--wrap,pthread_sigmask"
+readonly LIBC_WRAPPERS="--wrap,pthread_attr_getstacksize,--wrap,pthread_create,--wrap,pthread_detach,--wrap,pthread_sigmask"
 
 if [ "$#" -ne 4 ]; then
     echo "go-build-wrapper: wrong arguments" >&2
diff --git a/src/libc-wrappers/libc-wrappers.c b/src/libc-wrappers/libc-wrappers.c
index 3842f1fa6..f501330a8 100644
--- a/src/libc-wrappers/libc-wrappers.c
+++ b/src/libc-wrappers/libc-wrappers.c
@@ -15,9 +15,97 @@
  */
 
 
+#include <pthread.h>
 #include <signal.h>
 
 
+/*
+ * pthread_attr_getstacksize < GLIBC_2.34
+ */
+
+
+#if defined __aarch64__
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.17");
+#elif defined __arm__
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.4");
+#elif defined __i386__
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.1");
+#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.17");
+#elif defined __s390x__
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.2");
+#elif defined __x86_64__
+__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.2.5");
+#else
+#error "Please specify symbol version for pthread_attr_getstacksize"
+#endif
+
+
+int
+__wrap_pthread_attr_getstacksize (const pthread_attr_t *attr, size_t *stacksize)
+{
+  return pthread_attr_getstacksize (attr, stacksize);
+}
+
+
+/*
+ * pthread_create < GLIBC_2.34
+ */
+
+
+#if defined __aarch64__
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.17");
+#elif defined __arm__
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.4");
+#elif defined __i386__
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.1");
+#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.17");
+#elif defined __s390x__
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.2");
+#elif defined __x86_64__
+__asm__(".symver pthread_create,pthread_create@GLIBC_2.2.5");
+#else
+#error "Please specify symbol version for pthread_create"
+#endif
+
+
+int
+__wrap_pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
+{
+  return pthread_create(thread, attr, start_routine, arg);
+}
+
+
+/*
+ * pthread_detach < GLIBC_2.34
+ */
+
+
+#if defined __aarch64__
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.17");
+#elif defined __arm__
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.4");
+#elif defined __i386__
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.0");
+#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.17");
+#elif defined __s390x__
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.2");
+#elif defined __x86_64__
+__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.2.5");
+#else
+#error "Please specify symbol version for pthread_detach"
+#endif
+
+
+int
+__wrap_pthread_detach (pthread_t thread)
+{
+  return pthread_detach (thread);
+}
+
+
 /*
  * pthread_sigmask < GLIBC_2.32
  */

From 216314dab5beb01b1667c6534f7bf9d613fd07b9 Mon Sep 17 00:00:00 2001
From: Oliver Gutierrez <ogutsua@gmail.com>
Date: Thu, 16 Sep 2021 16:46:46 +0200
Subject: [PATCH 4/4] libc-wrappers: Avoid the new __libc_start_main from
 glibc-2.34

https://github.com/containers/toolbox/issues/821
---
 src/go-build-wrapper              |  2 +-
 src/libc-wrappers/libc-wrappers.c | 53 +++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/src/go-build-wrapper b/src/go-build-wrapper
index 2503d7f2f..9520e96d3 100755
--- a/src/go-build-wrapper
+++ b/src/go-build-wrapper
@@ -16,7 +16,7 @@
 #
 
 
-readonly LIBC_WRAPPERS="--wrap,pthread_attr_getstacksize,--wrap,pthread_create,--wrap,pthread_detach,--wrap,pthread_sigmask"
+readonly LIBC_WRAPPERS="--wrap,__libc_start_main,--wrap,pthread_attr_getstacksize,--wrap,pthread_create,--wrap,pthread_detach,--wrap,pthread_sigmask"
 
 if [ "$#" -ne 4 ]; then
     echo "go-build-wrapper: wrong arguments" >&2
diff --git a/src/libc-wrappers/libc-wrappers.c b/src/libc-wrappers/libc-wrappers.c
index f501330a8..11af5ceef 100644
--- a/src/libc-wrappers/libc-wrappers.c
+++ b/src/libc-wrappers/libc-wrappers.c
@@ -15,10 +15,63 @@
  */
 
 
+#include <link.h>
 #include <pthread.h>
 #include <signal.h>
 
 
+/*
+ * __libc_start_main < GLIBC_2.34
+ */
+
+
+struct startup_info
+{
+  void *sda_base;
+  int (*main) (int, char **, char **, void *);
+  int (*init) (int, char **, char **, void *);
+  void (*fini) (void);
+};
+
+extern int __libc_start_main (int argc,
+                              char **argv,
+                              char **ev,
+                              ElfW (auxv_t) * auxvec,
+                              void (*rtld_fini) (void),
+                              struct startup_info * stinfo,
+                              char **stack_on_entry);
+
+
+#if defined __aarch64__
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.17");
+#elif defined __arm__
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.4");
+#elif defined __i386__
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.0");
+#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.17");
+#elif defined __s390x__
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.2");
+#elif defined __x86_64__
+__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.2.5");
+#else
+#error "Please specify symbol version for __libc_start_main"
+#endif
+
+
+int
+__wrap___libc_start_main (int argc,
+                          char **argv,
+                          char **ev,
+                          ElfW (auxv_t) * auxvec,
+                          void (*rtld_fini) (void),
+                          struct startup_info * stinfo,
+                          char **stack_on_entry)
+{
+  return __libc_start_main (argc, argv, ev, auxvec, rtld_fini, stinfo, stack_on_entry);
+}
+
+
 /*
  * pthread_attr_getstacksize < GLIBC_2.34
  */