From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14614 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: About those weak aliases Date: Thu, 5 Sep 2019 14:18:46 -0400 Message-ID: <20190905181846.GG9017@brightrain.aerifal.cx> References: <20190902190359.GA6472@voyager> <20190902201009.GV22009@port70.net> <20190905165008.GA9317@voyager> <20190905165822.GA22009@port70.net> <20190905172904.GB9317@voyager> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="142039"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-14630-gllmg-musl=m.gmane.org@lists.openwall.com Thu Sep 05 20:19:01 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1i5wLV-000aq3-Kt for gllmg-musl@m.gmane.org; Thu, 05 Sep 2019 20:19:01 +0200 Original-Received: (qmail 26075 invoked by uid 550); 5 Sep 2019 18:18:59 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26023 invoked from network); 5 Sep 2019 18:18:58 -0000 Content-Disposition: inline In-Reply-To: <20190905172904.GB9317@voyager> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:14614 Archived-At: 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