[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: glibc and egcs....



Mikolaj J. Habryn writes:
 >   If I remember rightly (although this was in the context of weak
 > aliases, which may or may not be the same thing), a weak symbol, in
 > the absence of a definition, resolves to zero. So, you can do things
 > like:
 > 
 >   extern void * (*weak_function)();
 >   if(weak_function) weak_function(foo);
 > 
 >   and, provided that weak_function is indeed a weak alias, it is
 > guaranteed to link and not try to call an unresolved symbol. Consider
 > s/weak_function/__register_frame_info/g?
 > 
 >   At least, I *think* that's how it works - feel free to correct me :)

Ok, I am the stupid one, weak and undefined weak have indeed
some differences.

But anyway, I am not sure their semantics is appropriate in our case.
because the resolution of weak undefined symbols  occur mostly at
compile-time only. Try the following example, if the weak symbol did
not exist at compile time, it will never be bind at run-time, and if
it does exists at compile-time, it be equivalent to an undefined one,
and eventually will generate a link error at run-time.

main.c:
  #pragma weak foo
  void foo();
  int main()
  {
  printf("0x%x\n",foo);
  }
%cc -c main.c
%objdump --syms a.out | grep foo
0000000000000000  w      *UND*  0000000000000000 foo
%echo '/* nothing */' > foo.c
%cc -shared foo.c -o foo.so
%cc -s main.o foo.so
%objdump --dynamic-syms a.out | grep foo
%a.out
0x0
%echo  'void foo () { /* it is real */ }' > foo.c
%cc -shared foo.c -o foo.so
%ldd a.out
        foo.so => foo.so (0x0000015555556000)
        libc.so.6.1 => /lib/libc.so.6.1 (0x000001555565c000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x0000000000000000)
%a.out
0x0

If the symbol is found at compile-time, the program will abort if it
is not found at run-time:
/* continuing after the sequence above */
%cc -s main.o foo.so
%objdump --dynamic-syms a.out | grep foo
0000000000000000      DF *UND*  0000000000000024 foo
/* not the weak qualifier has disapearred !!! */
%echo '/* nothing */' > foo.c
%cc -shared foo.c -o foo.so
% a.out
a.out: error in loading shared libraries
: undefined symbol: foo


Before my previous mail, I only tested the last situation where weak
behaves like plain undefined. Sorry for my mistake.

So weak symbol does not fill our need for flexibility for the end-user 
(at run-time). Also they have yet another behaviour when used inside a 
shared lib, maybe this can be seen as a linker bug. 


Loic


Reply to: