jdh@bu-it.bu.edu (Jason Heirtzler) (07/29/89)
Compile the following under SunOS 4.0 #include <stdio.h> #include <netdb.h> main() { struct hostent *h; h = gethostent( ); } sendto() { printf( "hello\n" ); } When you run it, it will print "hello" and then hang. In the bowels of gethostent there's a call to YP (no matter that I wasn't running YP..) which wants to call the sendto(2) in libc, but instead.. While this lends itself to all kinds of neat hacks, the question is: is ld doing the correct thing ? Should it complain about a doublely defined function ? Should it do something different ? Jason Heirtzler Boston University
mojo@uunet.uu.net (Joseph Moran) (08/17/89)
In article <626@brazos.Rice.edu> jdh@bu-it.bu.edu (Jason Heirtzler) writes: >X-Sun-Spots-Digest: Volume 8, Issue 89, message 10 of 14 > >Compile the following under SunOS 4.0 > > #include <stdio.h> > #include <netdb.h> > main() { struct hostent *h; h = gethostent(); } > sendto() { printf("hello\n"); } [edited for inews...] >When you run it, it will print "hello" and then hang. In the bowels of >gethostent there's a call to YP (no matter that I wasn't running YP..) >which wants to call the sendto(2) in libc, but instead.. > >While this lends itself to all kinds of neat hacks, the question is: is ld >doing the correct thing ? Should it complain about a doublely defined >function ? Should it do something different ? This behavior has nothing to do with dynamic linking. Try the same thing using -Bstatic on the cc command line on SunOS 4.0 or on a SunOS 3.5 system. If you are still not convinced, try it on a non Sun platform that uses yp and sockets. If the non Sun platform doesn't use yp, try converting "sendto" to "read". In all of these cases, you will see the gethostent() library routine call sendto() (or read()) and end up in your little alternate routine. The basic algorithm when linking executables is to scan each library and pull out what you need from it. There isn't really a "doublely defined function", the library version of "sendto" is pulled out *if* it is needed (which it is not in this case). As libraries gets larger and larger, this behavior could create more and more surprised programmers (as you apparently were). Having an easy way to identify a program defining another instance of a symbol that is defined in a library might be a nice thing to have for cases like this. Joseph Moran Legato Systems Inc. 260 Sheridan Avenue Palo Alto, CA 94306 (415) 329-7886 mojo@legato.com or {sun,uunet}!legato!mojo