public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Using "weak externals" with ld on cygwin?
@ 2006-04-12  8:40 Brooks Moses
  0 siblings, 0 replies; 3+ messages in thread
From: Brooks Moses @ 2006-04-12  8:40 UTC (permalink / raw)
  To: binutils

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?

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.  I'm not sure I've got 
a very good idea at all how to do it correctly, though; the ld manual 
seems to provide no useful details beyond the above statement, and I 
haven't found much helpful in the gcc manual either.

If this is possible on Cygwin, can anyone provide a simple but complete 
example of how to make it work?  Ideally a C++ example, but I'll be 
happy with anything I can compile and run.  :)

Thanks muchly,
- Brooks

[1] http://sourceware.org/binutils/docs-2.16/ld/WIN32.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using "weak externals" with ld on cygwin?
  2006-04-17  6:19 Danny Smith
@ 2006-04-17 12:52 ` Brooks Moses
  0 siblings, 0 replies; 3+ messages in thread
From: Brooks Moses @ 2006-04-17 12:52 UTC (permalink / raw)
  To: binutils

Danny Smith wrote:
> Brooks Moses <bmoses at stanford dot edu> 
> wrote on: Tue, 11 Apr 2006 23:34:14 -0700 
>>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:
[...]
> 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.

Thanks!  Sounds like the best answer to my "How do I do this with dlls?" 
question is "Don't; find a workaround instead" -- making my code work 
with the LoadLibrary API would probably be as much work as simply fixing 
it so I don't need the circularly-referencing dlls, and the latter is a 
much more elegant solution.

>>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:
[...]

Should I file a bug report on this, then?  If so, where?

Thanks much!
- Brooks

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using "weak externals" with ld on cygwin?
@ 2006-04-17  6:19 Danny Smith
  2006-04-17 12:52 ` Brooks Moses
  0 siblings, 1 reply; 3+ messages in thread
From: Danny Smith @ 2006-04-17  6:19 UTC (permalink / raw)
  To: bmoses; +Cc: Binutils

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-04-17  6:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-12  8:40 Using "weak externals" with ld on cygwin? Brooks Moses
2006-04-17  6:19 Danny Smith
2006-04-17 12:52 ` Brooks Moses

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).