unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra via Libc-alpha <libc-alpha@sourceware.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org, bug-gnulib@gnu.org, binutils@sourceware.org
Subject: Re: Undefined use of weak symbols in gnulib
Date: Mon, 3 May 2021 11:14:54 +0930	[thread overview]
Message-ID: <20210503014454.GA5774@bubble.grove.modra.org> (raw)
In-Reply-To: <87o8e0p92r.fsf@oldenburg.str.redhat.com>

On Tue, Apr 27, 2021 at 07:53:16AM +0200, Florian Weimer via Binutils wrote:
> 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.

Yes, this construct is not handled in ppc32 and ppc64 ld.  It also
doesn't work with some combination of compiler flags on x86.  Don't
allow yourself to be fooled by only testing with compilers that
default to -fpie..

For example, this on x86_64 with gcc-11 (and previous versions).
$ cat ~/src/tmp/weakfunc2.c 
extern void func1 (void) __attribute__((weak));
extern void func2 (void) __attribute__((weak));

int
main (void)
{
  if (func1)
    func2 ();
  return 0;
}
$ gcc -O2 -c -fno-pie ~/src/tmp/weakfunc2.c 
$ gas/as-new -o empty.o /dev/null
$ ld/ld-new -shared -o empty.so empty.o
$ ld/ld-new -o weakfunc2 weakfunc2.o empty.so 
ld/ld-new: warning: cannot find entry symbol _start; defaulting to 0000000000401020
$ binutils/nm-new --dynamic weakfunc2
                 w func2

So func1 is resolved to zero at link time.  Well, I guess that isn't
unexpected.  powerpc-linux and powerpc64-linux (both ABIs) have
further problems that I'll fix.  In particular the above testcase
ought to work even though it looks wrong to test func1 non-NULL and
then call func2.  Slightly less weird is

  if (func1)
    {
      func1 ();
      func2 ();
    }

which might be reasonable user code when it is known that func1 and
func2 both exist or are both NULL.  ppc gets that one wrong too.

-- 
Alan Modra
Australia Development Lab, IBM

  parent reply	other threads:[~2021-05-03  1:45 UTC|newest]

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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210503014454.GA5774@bubble.grove.modra.org \
    --to=libc-alpha@sourceware.org \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=bug-gnulib@gnu.org \
    --cc=fweimer@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).