bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* Undefined use of weak symbols in gnulib
@ 2021-04-27  5:53 Florian Weimer
  2021-04-27  6:50 ` Paul Eggert
                   ` (4 more replies)
  0 siblings, 5 replies; 36+ messages in thread
From: Florian Weimer @ 2021-04-27  5:53 UTC (permalink / raw)
  To: bug-gnulib; +Cc: libc-alpha, binutils

lib/glthread/lock.h has this:

| /* The way to test at runtime whether libpthread is present is to test
|    whether a function pointer's value, such as &pthread_mutex_init, is
|    non-NULL.  However, some versions of GCC have a bug through which, in
|    PIC mode, &foo != NULL always evaluates to true if there is a direct
|    call to foo(...) in the same function.  To avoid this, we test the
|    address of a function in libpthread that we don't use.  */
| 
| #  pragma weak pthread_mutex_init
| #  pragma weak pthread_mutex_lock
| #  pragma weak pthread_mutex_unlock
| #  pragma weak pthread_mutex_destroy
| #  pragma weak pthread_rwlock_init
| #  pragma weak pthread_rwlock_rdlock
| #  pragma weak pthread_rwlock_wrlock
| #  pragma weak pthread_rwlock_unlock
| #  pragma weak pthread_rwlock_destroy
| #  pragma weak pthread_once
| […]

And:

| #  if !PTHREAD_IN_USE_DETECTION_HARD
| #   pragma weak pthread_mutexattr_gettype
| #   define pthread_in_use() \
|       (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
| #  endif

As far as I can tell gnulib uses this macro definition to implement
gl_once on glibc targets:

| #  define glthread_once(ONCE_CONTROL, INITFUNCTION) \
|      (pthread_in_use ()                                                        \
|       ? pthread_once (ONCE_CONTROL, INITFUNCTION)                              \
|       : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))

So the net effect is this:

  if (pthread_mutexattr_gettype != NULL)
    pthread_once (control, callback);

Dynamic linking with weak symbols is not very well-defined.  On x86-64,
the link editor produces the expected dynamic symbol relocation for the
pthread_once call.  On other targets (notably POWER), no dynamic
relocation is produced, and the code will crash if
pthread_mutexattr_gettype is ever defined.

There is an old thread here covering related issues:

  Specify how undefined weak symbol should be resolved in executable
  <https://sourceware.org/legacy-ml/gnu-gabi/2016-q1/msg00004.html>

On glibc targets, there is another problem: weak references do not carry
symbol versions, so they can bind to base versions unexpectedly.

This will become an urgent issue with glibc 2.34, which defines
pthread_mutexattr_gettype unconditionally.  Certain gnulib modules will
stop working until the binaries are relinked.  I expect the issue is
already visible with earlier glibc versions if libpthread is
unexpectedly present at run time.

I think we can provide an libBrokenGnulib.so preload module which
defines pthread_mutexattr_gettype to zero (as an absolute address), so
there is a kludge to keep old binaries working, but this is really
something that must be fixed in gnulib.

Thanks,
Florian



^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2021-07-27 23:39 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-27  5:53 Undefined use of weak symbols in gnulib Florian Weimer
2021-04-27  6:50 ` Paul Eggert
2021-04-27  6:58   ` Florian Weimer
2021-04-27  7:13     ` Paul Eggert
2021-04-27  7:24 ` Andreas Schwab
2021-04-27 11:06   ` Florian Weimer
2021-04-28  0:09     ` Bruno Haible
2021-04-28  2:10       ` H.J. Lu
2021-04-28  2:13         ` H.J. Lu
2021-05-05 20:31           ` Fangrui Song
2021-04-28  8:35         ` Florian Weimer
2021-04-28 13:15           ` Michael Matz
2021-04-28  7:44       ` Florian Weimer
2021-04-28 14:48         ` Bruno Haible
2021-04-28 17:44           ` Florian Weimer
2021-07-17 14:38         ` Bruno Haible
2021-07-17 14:55           ` Florian Weimer
2021-07-17 16:39             ` Bruno Haible
2021-07-27 20:02           ` Joseph Myers
2021-07-27 20:19             ` Florian Weimer
2021-07-27 23:38               ` Paul Eggert
2021-07-17 16:21         ` Bruno Haible
2021-04-27 23:22   ` Bruno Haible
2021-04-27 23:47 ` Bruno Haible
2021-04-28  7:57   ` Florian Weimer
2021-04-28 14:40     ` Bruno Haible
2021-04-28 17:43       ` Florian Weimer
2021-04-29 15:15         ` Bruno Haible
2021-04-30  9:55           ` Florian Weimer
2021-04-29  6:33       ` Ben Pfaff
2021-05-03  1:44 ` Alan Modra
2021-07-12 10:04 ` Michael Hudson-Doyle
2021-07-12 15:03   ` Florian Weimer
2021-07-12 15:30     ` Matthias Klose
2021-07-12 15:37       ` Florian Weimer
2021-07-13  0:22         ` Michael Hudson-Doyle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).