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 */
void load();
Code:
/* command.c */
#include "command.h"
#include <stdio.h>
void load() { printf("defined\n"); } /* sometimes defined, sometimes not */
Code:
> gcc -fPIC -shared command.c -o libcommand.so
Code:
/* additional.c */
#include "command.h"
#include <stdio.h>
void __attribute__ ((weak)) load() { printf("default\n"); }
Code:
> gcc -fPIC -shared additional.c -o libadditional.so
Code:
/* user.h */
void load_everything();
Code:
/* user.c */
#include "command.h"
void load_everything() { load(); }
Code:
> gcc -fPIC -shared user.c ./libcommand.so ./libadditional.so -o libuser.so
Code:
/* userprogram.c */
#include "user.h"
int main()
{
load_everything();
}
Code:
> gcc userprogram.c -Xlinker -rpath-link=. ./libuser.so -o userprogram
> ./userprogram
> gcc -fPIC -shared user.c ./libadditional.so -o libuser.so
> ./userprogram
The idea is that the command libraries can be excluded from the user library at link time by just not linking to them, or by not defining the load function while it's in development.
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.