[comp.sys.sun] dlopen broken?

John Lancaster <John.Lancaster@brunel.ac.uk> (08/06/90)

Is the program interface to the dynamic linker (dlopen) broken?

The following program will cause a segmentation error when the second call
to dlopen is made.  If the first call to dlclose is deleted the program
will run.  It appears that after a call to dlclose dlopen can no longer be
called.  This is not the behaviour implied by the manual.

Has anyone got this to work?  Have I misunderstood the documentation?

Test program:

#include<dlfcn.h>

main()
{
     int closeResult;
     static int *handle;
     char *file, *symbol;
     int (*symAdr)();

     p1();

     file = "p2.out";
     handle = dlopen(file,1);
     printf("returned handle of %s is %x\n",file,handle);
     printf("dlerror: %s\n",dlerror());

     symbol = "_p2";
     symAdr = dlsym(handle,symbol);
     printf("returned symbol address of %s is %x\n",symbol,symAdr);
     (*symAdr)();

     closeResult = dlclose(handle); /** this line the problem **/
     printf("dlclose call returned %d\n",closeResult);
     printf("dlerror: %s\n",dlerror());

     file = "p3.out";
     handle = dlopen(file,1);
     printf("returned handle of %s is %x\n",file,handle);

     symbol = "_p3";
     symAdr = dlsym(handle,symbol);
     printf("returned symbol address of %s is %x\n",symbol,symAdr);
     (*symAdr)();

     closeResult = dlclose(handle);
     printf("dlclose call returned %d\n",closeResult);
     printf("dlerror: %s\n",dlerror());

     exit(0);
}

p1()
{
     printf("inside p1\n");
     return;
}

John Lancaster      Email: J.Lancaster@brunel.ac.uk
Brunel University   Phone: +44 (0)895 74000 ext 2330
Uxbridge            Fax: +44 (0)895 58728
England             Telex: 261173 G
UB8 3PH

guy@uunet.uu.net (Guy Harris) (08/08/90)

>Is the program interface to the dynamic linker (dlopen) broken?

If "dlopen()" drops core, it's pretty much by definition broken.
Unfortunately, I don't know what the fix is.  However, you are making what
may be another error in your sample code:

>     symbol = "_p2";
>     symAdr = dlsym(handle,symbol);
>     printf("returned symbol address of %s is %x\n",symbol,symAdr);
>     (*symAdr)();

Presumably, the function "_p2()" is defined as

	_p2()
	{
		...
	}

in the ".so" file you've loaded.  If it's "p2()", not "_p2()", you should
hand "p2", not "_p2", to "dlsym".  The loader may happen to let you get
away with sticking the underscore in front of it, but you're not supposed
to put it there, the fact that in Sun's C implementation on non-386-based
machines the compiler sticks an underscore there nonwithstanding.  Not all
C implementations put the underscore there, and if the second argument to
"dlsym()" were supposed to have the underscore there iff the C
implementation puts it there, portability would be blown out of the water
(remember, S5R4 has "dlopen()", too, and most S5R4 implementations
probably won't put the underscore there).