mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: About those weak aliases
Date: Thu, 5 Sep 2019 14:18:46 -0400	[thread overview]
Message-ID: <20190905181846.GG9017@brightrain.aerifal.cx> (raw)
In-Reply-To: <20190905172904.GB9317@voyager>

On Thu, Sep 05, 2019 at 07:29:04PM +0200, Markus Wichmann wrote:
> On Thu, Sep 05, 2019 at 06:58:22PM +0200, Szabolcs Nagy wrote:
> > can you show an example use of open in musl code
> > where it is called form an api implementation
> > that is defined by iso c?
> >
> 
> No, I can't. And I think I understand now.
> 
> musl is trying to prevent linker errors from namespace pollution. More
> specifically, to prevent double definition errors. Such an error would
> happen during static linking, if a strong symbol from an unrelated
> standard were pulled in. To that end, weak aliases are handed out on an
> as-needed basis. open() is not needed to implement any interface from a
> standard it is not a part of (fopen() inlines the syscall), so it gets
> no alias. mmap() is needed to implement malloc(), so it gets one. Repeat
> for all other functions.
> 
> How close am I?

Correct, but not complete. It's also a matter of preventing not
diagnosable linker errors, but calling the *wrong function*, in the
case of both static and dynamic linking.

If for example fopen called open but the application defined open,
that would not be a linker error. For static linking, the file
defining open from libc.a (open.o) simply wouldn't get pulled into the
link since there would be no outstanding unresolved reference to open
another .o file already defined it. For dynamic linking, semantically
the same thing would happen -- ELF semantics duplicate static linking
semantics, requiring that functions from shared libraries be
interposable. Now, musl uses --dynamic-list (previously
-Bsymbolic-functions) which binds these symbols at libc.so link-time,
but that's just an optimization, relying on the fact that redefining
them would be UB so we can assume it doesn't happen. It's not required
and won't be done if configure determines that the tooling doesn't
support it.

The case where a link error would come up is rarer, and happens when
the reserved symbol is defined in the same file or pulled in by
something else. For example, if you use mtx_lock, that will pull in
__pthread_mutex_timedlock. If pthread_mutex_timedlock were also
defined in the same file and were not weak, this could be a duplicate
definition error at link time when the main program defines its own
pthread_mutex_timedlock.

One example where they appear in the same file is pthread_join with
the pthread_*join_np functions in the same file -- if they were
strong definitions rather than just weak aliases that would produce
link errors on certain valid usage.

Rich


      reply	other threads:[~2019-09-05 18:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-02 19:04 Markus Wichmann
2019-09-02 20:10 ` Szabolcs Nagy
2019-09-02 23:01   ` Rich Felker
2019-09-03 10:13     ` Szabolcs Nagy
2019-09-03 12:08       ` Rich Felker
2019-09-05 16:50   ` Markus Wichmann
2019-09-05 16:58     ` Szabolcs Nagy
2019-09-05 17:29       ` Markus Wichmann
2019-09-05 18:18         ` Rich Felker [this message]

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

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

  git send-email \
    --in-reply-to=20190905181846.GG9017@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).