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?


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


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