__attribute__ ((weak)) suddenly stops working / starts working
I am progressively building a program which has 7 or so very similar libraries which load commands. About 3 have the load functions written and for all of them I have default "weak" definitions. Just today they started to cover the real definitions in some cases but not in others. Here is the basic layout:
Code:
[command lib X]]]]---------+ +-->[using lib 1]------------->[program type 1] Am I erroneously relying on this attribute, or is there a specific way you have to use it? It's worked for several months so far, and my diff output shows nothing to indicate that this should happen. Thanks. ta0kira |
I must be missing something. When you emit a weak symbol it allows the calling program to override the symbol at run-time.
weak symbols are declared that way in the calling procedure. Yours seem to be the libraries. Or are you calling other libraries' modules with the same symbol name? In that case, call dld to force symbol definition from a specific library at runtime. If that's what you are doing -- what amounts to symbol overloading. |
You don't have to define a symbol as weak for the final program to override it if the symbol is in a shared library. That entirely depends on link order, though. Basically what I'm doing is declaring the load functions in each of the command library headers and a separate library provides weak definitions (among many other things.) If I define the load function in a command library I expect it to override the weak symbol when loaded at run time, which it has until now. Example:
Code:
/* command.h */ Code:
/* command.c */ Code:
> gcc -fPIC -shared command.c -o libcommand.so Code:
/* additional.c */ Code:
> gcc -fPIC -shared additional.c -o libadditional.so Code:
/* user.h */ Code:
/* user.c */ Code:
> gcc -fPIC -shared user.c ./libcommand.so ./libadditional.so -o libuser.so Code:
/* userprogram.c */ Code:
> gcc userprogram.c -Xlinker -rpath-link=. ./libuser.so -o userprogram The opposite of this attribute would be "protected" visibility, but that only works for calls within the same library as the protected definition, so that won't work. ta0kira PS I think it has to do with dynamic load order. I think the real command lib depends on the additional lib, so it loads that lib before itself, even though the additional lib comes after it when linking the user lib. |
Ok, here is what I think is happening. The library corresponding to "using lib 2" somehow has a dependency in the additional lib, so that lib is loaded before the command libs. "using lib 1" has no direct dependency to the additional lib, so it's only loaded as needed after the command libs are loaded. I'll track down that dependency later and see if I'm right.
ta0kira |
All times are GMT -5. The time now is 03:02 PM. |