Subject: Re: weak symbols on Cygwin
From: Bruno Haible <[email-masked]>
To: Dave Korn <[email-masked]>
Cc: Eric Blake <[email-masked]>, [email-masked]
Date: Wed, 7 Apr 2010 11:06:10 +0200
List-Id: <cygwin.cygwin.com>

Hi Dave,

> on ELF platforms, you can leave undefined references in an executable, to
> be filled in by ld.so at runtime according to what the loader finds present
> once it's loaded libc.
> ...
>   On windows ...  There's no run-time process of
> scanning all available symbols from all loaded libraries against undefined
> references in the executable.
> ...
> on Windows, all the library functions imported
> from libc come in the form of import stubs from the (static) import library.
> So weak references won't cause any of those symbols to be pulled in, even if
> the library does provide them.  But if something else in the program causes
> them to be pulled in, the weak references will resolve to the imported
> function.

Thanks for these explanations. So, "#pragma weak" is very different on PE
systems than on ELF systems.

> > On ELF platforms, I use "#pragma weak" in order to detect whether a symbol
> > is defined in the libraries which are linked in with the executable
> > (including libc).
> 
>   This whole concept isn't going to translate directly to Windows platforms.
> There's no interposing or providing missing definitions at runtime; if it's
> not already there at final link time, it's not going to be there at runtime.
> The only way windows can determine this sort of thing is through dlsym()ing
> all the open libraries, I think.

I was implicitly hoping that
  - gcc would collect all weak-declared symbols in some table, and
    generate code so that the reference to these symbols makes an indirection
    through this table,
  - the Cygwin runtime would call dlsym or GetProcAddress at startup and
    fill in addresses into said table.

>   What's the larger-scale goal you're trying to support by this method?  Are
> you after implementing some sort of plug-in architecture or something?

I've got some code, written for ELF platforms, that detects whether the pthread
library (often a separate library from libc) is linked, by doing

  #pragma weak pthread_cancel
  bool pthread_in_use = (pthread_cancel != NULL);

This code breaks in Cygwin 1.7, because the autoconf test determines that gcc
accepts #pragma weak. Not a big problem - I can surround that code with
  #ifdef __ELF__

Simply, it's an issue to be aware of. I'll suggest a documentation improvement
for the GCC manual.

Bruno

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple