Subject: shared object problem
To: None <port-alpha@NetBSD.ORG, port-pmax@NetBSD.ORG>
From: Arne H Juul <arnej@math.ntnu.no>
List: port-pmax
Date: 07/25/1998 02:17:18
I've been looking at perl and trying to figure out why dynamic
module loading doesn't work, and I've found that it's because
the dynamic modules can't get the symbols back in the main program
right.  Appended is a test case, compile with:

# cc main.c -o main
# cc -c -fPIC fred.c
# ld -x -shared fred.o -o fred.so
# ./main

I think this should work, at least it does on NetBSD/i386 and
IRIX/mips, so I guess it's ld.elf_so that doesn't work right.

Does other NetBSD/pmax people get the same results with ld.elf_so?
And could someone please test in on NetBSD/alpha?  I've spent some time
trying to figure out what's going on but I understand zip, so I hope
some Real Gurus can look at it :-)

  -  Arne H. J.

<main.c program>

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

extern int fun();
int fun() { return 3; }

int main(void)
{
    void * handle ;
    int (*symbol)(int) ;
    int mode = RTLD_NOW | RTLD_GLOBAL;
    int fres;

    handle = dlopen("./fred.so", mode) ;
    if (handle == NULL) exit(1);

    symbol = dlsym(handle, "fred") ;
    if (symbol == NULL) {
        /* try putting a leading underscore */
        symbol = dlsym(handle, "_fred") ;
        if (symbol == NULL) exit(2);
    }

    printf ("dlopen/dlsym OK\n") ;
    fflush (stdout) ;

    /* call function */
    fres = (*symbol)(7);

    printf ("ret %d\n", fres);
    fflush (stdout) ;

    exit(0);
}

<fred.c module>

#include <stdio.h>
extern int fun();

int fred (int i) {
        printf("fun at %p\n", fun);
        fflush(stdout);

        return (i+2+fun());
}