Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a generic function pointer in C that can be assigned/cast to a more restrictive prototype?

I have the need to dynamically link against a library at run-time and resolve a series of functions using dlsym. My first thought was to use an array of function pointers that can be easily iterated through by leveraging a secondary array of char * representing the symbol names.

However, the problem with this is that not all of the functions take the same arguments. Is there a way to use a generic function pointer in the array, but assign it to a more restrictive function pointer prototype? For example:

If I need to resolve these functions:

int (*functionA)(int)
char (*functionB)(char,int)

Is it possible to do something like (pseudo-ish .. )

void* functionList(...)[2] = {functionA, functionB};

Along with

char FuncNameA[] = "functionA";
char FuncNameB[] = "functionB";
char *functionNames[2] = {FuncNameA, FuncNameB};

For the purpose of looping though the call to dlsym for symbol resolution

int i = 0;
for(; i<2; i++)
    functionList[i] = dlsym(DL_OPEN_HANDLE, functionNames[i]);

Where DL_OPEN_HANDLE would be defined by an earlier call to dlopen.

like image 953
sherrellbc Avatar asked Jul 17 '15 18:07

sherrellbc


People also ask

Can we have a pointer to a function in C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

Which is used to declare generic pointer?

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is generic function in C?

A generic function is a function that is declared with type parameters. When called, actual types are used instead of the type parameters.


3 Answers

The C standard guarantees that any object pointer type can be converted to void* and back again without loss of information (meaning that the re-converted pointer will compare equal to the original one).

There is a different guarantee for function pointers: Any function pointer can be converted to any other function pointer type and back again without loss of information.

(There is no guarantee regarding conversions between function pointers and object pointers, or more specifically between function pointers and void*. An implementation could, for example, make void* 64 bits and function pointers 128 bits.)

You can use, for example, void(*)(void) as a generic function pointer type:

typedef void (*funcptr)(void);

You must convert back to the original pointer type before executing a call to avoid undefined behavior.

On the other hand, you're using dlsym(), which returns a void*. My understanding is that POSIX guarantees that the void* returned by dlsym() (if the name argument names a function) can be converted to a function pointer, which can be used to call the function. If the only functions you care about are those whose addresses are returned by dlsym(), then you can use void*.

(POSIX previously guaranteed, as an extension to the C standard, that a function pointer can be converted to void* and back again. That guarantee was later dropped. Thanks to Jonathan Leffler for pointing this out.)

In any case, using function pointers to store the addresses of functions probably makes for clearer code.

like image 103
Keith Thompson Avatar answered Oct 05 '22 00:10

Keith Thompson


dlsym returns a data pointer of type void *, but POSIX guarantees that this can be cast to a function pointer of the appropriate type:

Implementations supporting the XSI extension [...] require that an object of type void * can hold a pointer to a function. The result of converting a pointer to a function into a pointer to another data type (except void *) is still undefined, however.

Since Version 7 of POSIX, all implementations (not just XSI) are required to support the conversion.

Because conversion from a void * pointer to a function pointer via a direct cast can result in compiler warnings, older versions of POSIX recommend performing the conversion via aliasing:

int (*fptr)(int);
*(void **)(&fptr) = dlsym(handle, "my_function");

In the current version the recommendation is changed to:

int (*fptr)(int);
fptr = (int (*)(int))dlsym(handle, "my_function");
like image 23
ecatmur Avatar answered Oct 05 '22 00:10

ecatmur


You should probably define your list as void *functionList[2], since dlsym returns a void *. Once you know which function you have, you can cast it to the proper type.

void *functionList[2];

...

int (*functionA)(int) = (int(*)(int))functionList[0];
char (*functionB)(char,int) = (char(*)(char, int))functionList[1];
like image 26
dbush Avatar answered Oct 05 '22 00:10

dbush