Hello!

While moving a large-scaled software product to static linking due to many
problems with incompatible system libraries (like libc / glibc) among the
linux distributions, I discovered the following what seems to me like a BUG:

gethostbyname() does not seem to work when I use it in a shared object on
both of my redhat 6.1 and redhat 6.0 boxes with standard RPMs from CD.
It does work, however, when I do not compile my modules with static linking.

I have the three files (Makefile, shared.c, and loader.c) that repoduce this
bug appended to this email.

I tried to debug the libc resolving mechanism, but unfortunately gdb does not
know sharedFunc contained in the .so file even after dlopen() (using 
GNU gdb 4.18 and supplied "-g" to gcc).

I greatly appreciate your hints, since this problem buggers me for weeks! :-)

If it is rather improblable that my question can be answered in this list,
feel free to forward it to forums that will do. 

The 6.1 box's configuration:

sven@irving:c/shared.objects > uname -a
Linux irving.XXXXX.XX 2.2.12-20 #1 Mon Sep 27 10:40:35 EDT 1999 i686 unknown
sven@irving:c/shared.objects > cat /etc/redhat-release
Red Hat Linux release 6.1 (Cartman)
sven@irving:c/shared.objects > gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

The output of the appended programs looks like this:

sven@irving:c/shared.objects > make clean && make dynamic && ./loader
rm -f loader shared.so
gcc -g -shared -fPIC shared.c -o shared.so
gcc -g -Wall loader.c -o loader -ldl
Entered loader
    gethostbyname() info for www.ansi.org:
        hostname => www.ansi.org
        ip       => 165.254.114.5
Entered shared.so
    gethostbyname() info for www.ansi.org:
        hostname => www.ansi.org
        ip       => 165.254.114.5

[ So far it works ok, but now look at what follows! ]

sven@irving:c/shared.objects > make clean && make static && ./loader
rm -f loader shared.so
gcc -g -shared -Wall -fPIC shared.c -o shared.so -Wl,"-Bstatic"
gcc -g -Wall loader.c -o loader -ldl -static
Entered loader
    gethostbyname() info for www.ansi.org:
        hostname => www.ansi.org
        ip       => 165.254.114.5
Entered shared.so
    gethostbyname() info for www.ansi.org:
        www.ansi.org: Unknown host                  /* XXXXXXXXXXXX */

Regards, 

  Sven Koehler

### Makefile

static: shared.static loader.static
        @true

dynamic: shared.dynamic loader.dynamic
        @true

shared.static:
        gcc -g -shared -Wall -fPIC shared.c -o shared.so -Wl,"-Bstatic"

shared.dynamic:
        gcc -g -shared -fPIC shared.c -o shared.so

loader.static:
        gcc -g -Wall loader.c -o loader -ldl -static

loader.dynamic:
        gcc -g -Wall loader.c -o loader -ldl

clean:
        rm -f loader shared.so

### shared.c

#include <stdio.h>

#include <netdb.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


extern int h_errno;


void test_gethostbyname() {
    struct hostent *host;
    char *hostQuery = "www.ansi.org";
    printf("    gethostbyname() info for %s:\n", hostQuery);
    host = gethostbyname(hostQuery);
    if (host) {
        struct in_addr ia = *(struct in_addr *) host->h_addr_list[0];
        printf("        hostname => %s\n", host->h_name);
        printf("        ip       => %s\n", inet_ntoa(ia));
    } else {
        fprintf(stderr, "        %s: %s\n", hostQuery, hstrerror(h_errno));
    }
}



int sharedFunc () {
    printf("Entered shared.so\n");
    test_gethostbyname();
    return 0;
}

### loader.c

#include <stdio.h>
#include <dlfcn.h>

#include <netdb.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


extern int h_errno;


void test_gethostbyname() {
    struct hostent *host;
    char *hostQuery = "www.ansi.org";
    printf("    gethostbyname() info for %s:\n", hostQuery);
    host = gethostbyname(hostQuery);
    if (host) {
        struct in_addr ia = *(struct in_addr *) host->h_addr_list[0];
        printf("        hostname => %s\n", host->h_name);
        printf("        ip       => %s\n", inet_ntoa(ia));
    } else {
        fprintf(stderr, "        %s: %s\n", hostQuery, hstrerror(h_errno));
    }
}


int main(int argc, char **argv) {
        const char *lib = "./shared.so";
        void *handle = dlopen(lib, RTLD_LAZY);
    printf("Entered loader\n");
    test_gethostbyname();
        if (handle) {
                const char *error;
                int (*sFunc)()=dlsym(handle, "sharedFunc");
                error = dlerror();
                if (error) {
                        fprintf(stderr, "sharedFunc() not found in shared object '%s': 
%s\n",
                                lib, error);
                        exit(1);
                } else {
            sFunc();
                }
        } else {
                fprintf(stderr, "cannot load shared object '%s': %s\n", lib, 
dlerror());
                exit(1);
        }
    exit(0);
}

EOE

-- 
To unsubscribe:
mail -s unsubscribe [EMAIL PROTECTED] < /dev/null

Reply via email to