Possible workaround for dlsym() and cast from/to void*

A

anonimo

Hello everyone,

i want to dinamically load one new function with dlopen()+dlsym(). As
we all know, the problem is that ISO C++ does not allow to safely cast
from void* to pointer-to-function and viceversa.

But I had an idea about a possible workaround and I would like to hear
your opinions: instead of getting a pointer to function with dlsym()
from the object file, I could get a pointer to an
array-of-pointers-to-functions and reference the first element to obtain
the pointer to the function I need. On the other side, in the object
file, I could use a global instance of a "proxy class" to dinamically
initialize this array at loading time, so that the first element will
contain the correct address of the function.

Thanks in advance.
 
J

Joshua Maurice

Hello everyone,

        i want to dinamically load one new function with dlopen()+dlsym(). As
we all know, the problem is that ISO C++ does not allow to safely cast
from void* to pointer-to-function and viceversa.
[snip]

Just because it's not defined by one standard (the C++ standard) does
not make it undefined. It's defined by the POSIX standard. So, it's
ok.

However, it still lacks all kinds of type safety, but there's no way
to really have type safety with the current system. The object file
format does not store the type of function, so you have to know
beforehand the function type, and thus the dynamic loader and dlsym
cannot help you out.
 
J

James Kanze

i want to dinamically load one new function with
dlopen()+dlsym(). As we all know, the problem is that ISO
C++ does not allow to safely cast from void* to
pointer-to-function and viceversa.

Just because it's not defined by one standard (the C++
standard) does not make it undefined. It's defined by the
POSIX standard. So, it's ok.

No it's not, and it's illegal in C++ (and C), so a compiler is
required to diagnose the error. (Only in strict conformance
mode, of course, and having done so, it can go on an compile
your code correctly.)

Posix does require all function, void and object pointers to
have the same size and representation, and suggests:

void (*pf)();

*(void**)( &pf ) = dlsym...

They also hint that they are looking into a cleaner solution.
However, it still lacks all kinds of type safety, but there's
no way to really have type safety with the current system. The
object file format does not store the type of function, so you
have to know beforehand the function type, and thus the
dynamic loader and dlsym cannot help you out.

That is, regretfully, true for most systems of explicit dynamic
loading.
 
B

BGB / cr88192

anonimo said:
Hello everyone,

i want to dinamically load one new function with dlopen()+dlsym(). As we
all know, the problem is that ISO C++ does not allow to safely cast from
void* to pointer-to-function and viceversa.

well, it works, and that is good enough for most things.

as a general rule, most people probably aren't using systems where this
particular difference will matter.
even then, on most systems where it does matter (say, code and data in
different address spaces), since the pointer would be (presumably) valid for
code (if not valid for data), the cast should still work just fine.

so, either way, it is "safe enough"...

But I had an idea about a possible workaround and I would like to hear
your opinions: instead of getting a pointer to function with dlsym() from
the object file, I could get a pointer to an
array-of-pointers-to-functions and reference the first element to obtain
the pointer to the function I need. On the other side, in the object file,
I could use a global instance of a "proxy class" to dinamically initialize
this array at loading time, so that the first element will contain the
correct address of the function.

simple solution:
just cast the thing, it works for everyone else...

but, anyways, if a person deals much with "real" coding, they will generally
be faced with plenty of far nastier things than this, and it is generally
not worthwhile to tip-toe around issues of this sort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top