2

I am attempting to get the function pointer by using dlopen and dlsym, however I have been unable to get it working correctly. It fails when trying to doing the dlsym call. Following is my code.

Any help please?

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

int test() {
    printf("%s", "test()");
    return 123;
}

int main() {
    char * functionname = "test";
    void* handle = dlopen(NULL,RTLD_LAZY|RTLD_GLOBAL);
    if (!handle) {
        fprintf(stderr, "Couldn't open handle: %s\n",
            dlerror());
        exit(1);
    }

    int (*fun)() = (int (*)())dlsym(handle, functionname);
    if (fun == NULL) { 
        fprintf(stderr, "Couldn't find function: %s\n",functionname); 
        exit(1);
    }
    int a = fun();
    printf("result: %d \n", a);
}
Jacob
  • 3,521
  • 6
  • 26
  • 34
  • What makes you certain the symbol `test` exists in the library? – mah Mar 21 '14 at 14:13
  • should it not be exist as I defined it in the main? From dlopen "If filename is a NULL pointer, then the returned handle is for the main program." – Jacob Mar 21 '14 at 14:16
  • I see, I misunderstood the question; I didn't notice you were looking to get the symbol from your own executable. I'm not positive it's done the way you're trying to though... what happens if you search for the symbol via `dlsym(NULL, function name);`? – mah Mar 21 '14 at 14:18
  • same thing happens if I call dlsym(NULL,..) though it does not really make sense to that that either. dlsym expects the handle obtained by the dlopen call. – Jacob Mar 21 '14 at 14:22
  • Other valid handles for use by dlsym include RTLD_SELF and RTLD_MAIN_ONLY, both of which may be helpful. I was thinking (wrongly, it seems) that NULL would be the same as asking to locate the symbol in the main executable. – mah Mar 21 '14 at 14:26
  • Do you happen to know where these where RTLD_SELF and RTLD_MAIN_ONLY are defined? can't seem to find them anywhere. I also attempted using these with the same result however "There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find the first occurrence of the desired symbol using the default library search order. The latter will find the next occurrence of a function in the search order after the current library." – Jacob Mar 21 '14 at 14:36
  • I found them in my dlfcn.h (along with DEFAULT and NEXT). RTLD_DEFAULT should also work for you. (I guess SELF and MAIN_ONLY could be OS X / iOS specific.). RTLD_NEXT is used when the same symbol exists in multiple libraries and one wants to find the next one. – mah Mar 21 '14 at 14:42
  • right okay. default and next both give the same result. E.g. it can't find the symbol. – Jacob Mar 21 '14 at 14:53
  • 1
    You may need to apply some linker options when you build. See http://stackoverflow.com/questions/6292473/how-to-call-function-in-executable-from-my-library for possibilities to try. – mah Mar 21 '14 at 14:55

1 Answers1

7

Probably you need to specify to the linker to export the symbols as dynamic. With gcc you have to use -rdynamic.

You can check the exported dynamic symbols with objdump -T.

Sigi
  • 4,826
  • 1
  • 19
  • 23