tags:

views:

504

answers:

2

Hello, I am trying to do something like the following

  enum types {None, Bool, Short, Char, Integer, Double, Long, Ptr};
  int main(int argc, char ** args) {
     enum types params[10] = {0};
     void* triangle = dlopen("./foo.so", RTLD_LAZY);
     void * fun = dlsym(triangle, ars[1]);

     <<pseudo code>>
  }

Where pseudo code is something like

fun = {}
for param in params:
      if param == None:
         fun += void
      if param == Bool:
          fun += Boolean
      if param == Integer:
          fun += int
      ...
 returnVal = fun.pop()
 funSignature = returnval + " " + funName + "(" + Riffle(fun, ",") + ")"
 exec funSignature

Thank you

+3  A: 

What dlsym() returns is normally a function pointer - disguised as a void *. (If you ask it for the name of a global variable, it will return you a pointer to that global variable, too.)

You then invoke that function just as you might using any other pointer to function:

int (*fun)(int, char *) = (int (*)(int, char *))dlysm(triangle, "function");

(*fun)(1, "abc");    # Old school - pre-C89 standard, but explicit
fun(1, "abc");       # New school - C89/C99 standard, but implicit

I'm old school; I prefer the explicit notation so that the reader knows that 'fun' is a pointer to a function without needing to see its declaration. With the new school notation, you have to remember to look for a variable 'fun' before trying to find a function called 'fun()'.

Note that you cannot build the function call dynamically as you are doing - or, not in general. To do that requires a lot more work. You have to know ahead of time what the function pointer expects in the way of arguments and what it returns and how to interpret it all.

Systems that manage more dynamic function calls, such as Perl, have special rules about how functions are called and arguments are passed and do not call (arguably cannot call) functions with arbitrary signatures. They can only call functions with signatures that are known about in advance. One mechanism (not used by Perl) is to push the arguments onto a stack, and then call a function that knows how to collect values off the stack. But even if that called function manipulates those values and then calls an arbitrary other function, that called function provides the correct calling sequence for the arbitrary other function.

Reflection in C is hard - very hard. It is not undoable - but it requires infrastructure to support it and discipline to use it, and it can only call functions that support the infrastructure's rules.

Jonathan Leffler
I know what the arguments ahead of time are and the return types, my question is whether I can create the casts dynamically.Thanks
adk
No; you can't create the casts dynamically. C is a compiled language; what you are seeking to do is interpret C code, which is not a trivial proposition!
Jonathan Leffler
thank you. This is what I wanted to know
adk
+1  A: 

Actually, you can do nearly all you want. In C language (unlike C++, for example), the functions in shared objects are referenced merely by their names. So, to find--and, what is most important, to call--the proper function, you don't need its full signature. You only need its name! It's both an advantage and disadvantage --but that's the nature of a language you chose.

Let me demonstrate, how it works.

#include <dlfcn.h>

typedef void* (*arbitrary)();
// do not mix this with   typedef void* (*arbitrary)(void); !!!

int main()
{
    arbitrary my_printf;
    // Introduce already loaded functions to runtime linker's space
    void* handle = dlopen(0,RTLD_NOW|RTLD_GLOBAL);
    // Load printf function to our pointer, which doesn't know how many arguments there sould be
    *(void**)(&my_printf) = dlsym(handle,"printf");
    // Call printf via my_printf
    (void)  my_printf("Hello, world!\nHere's what 2+2 is: %d\n",(int)2*2);
    return 0;
}

In fact, you can call any function that way. However, there's one drawback. You actually need to know the return type of your function in compile time. By default, if you omit void* in that typedef, int is assumed as return type--and, yes, it's a correct C code. The thing is that the compiler needs to know the size of the return type to operate the stack properly.

You can workaround it by tricks, for example, by pre-declaring several function types with different sizes of return types in advance and then selecting which one you actually are going to call. But the easier solution is to require functions in your plugin to return void* or int always; the actual result being returned via pointers given as arguments.

Pavel Shved
@pavel: Could I do something likeunion type { int i; double d; float f;} type;(type) my_printf(...) ?
adk
@adk: I don't see anything wrong with unions. In fact, I completely forgot about them, so thanks for improving my answer! :)
Pavel Shved
Pavel: `printf` is a bad example, because the empty parameter list declaration isn't compatible with varargs functions. (And the return type of `printf` is `int`, by the way, not `void *`).
caf
@pavel: what if I do not know the number of arguments at compile time?
adk
@adk: then you're in trouble. Actually, in this case the funciton you dlsym() should take an array, or a list, or something like that. You have some variability, but it's still C.
Pavel Shved
@pavel: thanks. I was trying to evaluate whether this is a difficult problem.
adk
@adk: Unions are completely different types, so you'd be in trouble unless the function actually returned a union.
aib