This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Using "weak externals" with ld on cygwin?


Brooks Moses <bmoses at stanford dot edu> 
wrote on: Tue, 11 Apr 2006 23:34:14 -0700 

> I've been pounding my head all day against trying to use weak
> symbols in libraries on Cygwin, and have finally gotten to the point
> where I need to admit defeat and ask for help.
> 
> In the online documentation for ld, version 2.16 [1], I find the
> following statement at the bottom of the page: "The Windows object
> format, PE, specifies a form of weak symbols called weak externals.
When
> a weak symbol is linked and the symbol is not defined, the weak symbol
> becomes an alias for some other symbol."
> 
> My understanding of this is that I should be able to create a libA.dll
> file with some code in it that references a function foo(), and define
> foo() as a weak symbol aliased to bar() such that, if an executable
> defines its own foo() or links to another dll that defines it, the
libA
> code will use that definition of foo(), but otherwise it will use
bar().
> 
> Is that actually a correct understanding? Or am I missing something
> somewhere?
> 


weak symbols do not work as expected with PE DLL's.  DLL's do not allow
undefined symbols when they are linked together.  DLLs are executables
with their own startup entry point, their own main, their own at_exit
table, etc. Hence a DLL built from this:

=================================================
void bar() {}

void foo()  __attribute__ ((weak, alias ("bar")));

void woo (void)
{ return foo(); }
=================================================

will always resolve 'foo' through the 'bar' alias since their is no
strong
'foo' defined at link time.

Weak extern synbols work fine in PE  with static objects and archives,
but I don't
know how to do exactly what you want with dll's.  Using
LoadLibrary/GetProcAddress API might work for you.
   
> I've been trying, with a copy of the release version of gcc 4.1.0 and
> the latest Cygwin version of ld (which reports GNU ld version 2.16.91
> 20050610 as its version), to write some code that will do that.
> Everything I try seems to either give me an error like "Cannot export
> .weak.__Z3foov.__Z3barv: symbol not found" or else always uses bar()
> regardless of whether I supply a foo() elsewhere. 

Exporting .weak._foo._bar in the above is an ld error.  I think this
kind of
'.'-concatted names get interpreted as a forward export, eg the loader
will look
for a symbol 'weak' in another dll module with the unsual name
'_foo._bar'. 
This can be fixed easily enough by adding ".weak." to the list of
prefixes that
exclude symbols from automatic export, like so:


Index: pe-dll.c
===================================================================
RCS file: /cvs/src/src/ld/pe-dll.c,v
retrieving revision 1.83
diff -c -3 -p -r1.83 pe-dll.c
*** pe-dll.c	31 Jan 2006 22:08:14 -0000	1.83
--- pe-dll.c	17 Apr 2006 04:03:34 -0000
*************** static autofilter_entry_type autofilter_
*** 277,282 ****
--- 277,283 ----
    { "cygwin_premain2", 15 },
    { "cygwin_premain3", 15 },
    { "environ", 7 },
+   { ".weak.", 6},
    { NULL, 0 }
  };


Danny


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]