On Fri, 12 Apr 2002, Matt Seitz wrote:
> 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?
Oddly enough, I recently ran into this in my own work. What I ended up
with is instead of
fptr = (int (*)(int))dlsym(handle, "my_function");
I did
*(void **)(&fptr) = dlsym(handle, "my_function");
... which is, I'll grant, a little kludgy, but is perfectly legal in
ANSI or ISO C terms.
Marc.
+----------------------------------+-----------------------------------+
| Marc Aurele La France | work: 1-780-492-9310 |
| Computing and Network Services | fax: 1-780-492-1729 |
| 352 General Services Building | email: yyy@xxxxxxxxxxx |
| University of Alberta +-----------------------------------+
| Edmonton, Alberta | |
| T6G 2H1 | Standard disclaimers apply |
| CANADA | |
+----------------------------------+-----------------------------------+
XFree86 Core Team member. ATI driver and X server internals.
|