Email List: Xaustin-group-lX
[All Lists]

RE: Return type of dlsym

To: "Matt Seitz" <yyyyyy@xxxxxxxxx>, <yyyyyyyyyyyyyy@xxxxxxxxxxxxx>
Subject: RE: Return type of dlsym
From: "Donn Terry" <yyyyyy@xxxxxxxxxxxxx>
Date: Fri, 12 Apr 2002 15:06:10 -0700
Thread-index: AcHiZ2/ZNYGvdwRWSEex4bSe+MkmdwAATshQ
Thread-topic: Return type of dlsym
Ouch.

Matt makes a good point: C doesn't allow data* <->function* casts
because some
architectures just plain can't do that.  (It's too long ago to be sure,
but I suspect
that that's true for the B5500/6500 (and possibly other Burroughs
machines) and
it's child-in-spirit the HP3000.)  

We have several ways to go here:
1) Follow the suggestion below of having two functions (likely with
identical implementations
   on most machines).  This is, in my mind, the right answer because it
doesn't preclude
   an implementation where function pointers and data pointers are not
"the same".
2) Profile the C standard to allow this.  (I didn't do nearly the work
required to confirm
   that that would be a legal profile.)
3) (I think) Require casting the pointer "through" an integer type.
(Ugh.)
4) Marc LaFrance's alternative (and probably several things like it).
(Marc: creative,
   but forgive me for saying it, it's ugly.)
(I think that all of the last three options take us thru an
"unspecified" or "implementation
defined" path in the C standard.)

We could also ignore the problem, but I don't think we can really get
away with that.
(I can't say I like any of these options, but if we DO have to touch
this stuff, I again
plead that we fix the totally useless error handling stuff at the same
time!)

Donn

-----Original Message-----
From: Matt Seitz [mailto:yyyyyy@xxxxxxxxx] 
Sent: Friday, April 12, 2002 1:04 PM
To: yyyyyyyyyyyyyy@xxxxxxxxxxxxx
Subject: Return type of dlsym


The "dlsym" function returns "void *".  The "Examples" section
recommends casting the return value to a pointer to a function when
using "dlsym" to find the address of a function.  Unfortunately, casting
any data pointer (including "void *") to a function pointer violates the
ISO C Standard and causes warnings on some compilers.  

It is legal to convert from a pointer to a function type into a pointer
to a function of another type, and then convert the pointer back to the
original type.  Therefore, I would like to see a function added that
instead returns a "void (*)(void)".  Something like:
     
void (*)(void) dlfunc(void *restrict handle, const char *restrict name);

This return type could then be converted into the actual function type
of the function.  So the example given in the Specification would be
changed to:

void    *handle;
int     *iptr, (*fptr)(int);


/* open the needed object */
handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);


/* find the address of function and data objects */
fptr = (int (*)(int))dlfunc(handle, "my_function");
iptr = (int *)dlsym(handle, "my_object");


/* invoke function, passing value of integer as a parameter */
(*fptr)(*iptr);

Any suggestions for improving this idea?


=====
Sincerely,
Matt Seitz

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax http://taxes.yahoo.com/

<Prev in Thread] Current Thread [Next in Thread>