2

I've got an issue I am not able to solve. Looked up everything I found so far. My problem is, I create a dyn library in my program a want to dlopen it and dlsym a method out of that lib.

It seems that dlopen works but dlsym return me the error "undefined symbol: method"

where "method" is the name of the method I passed to dlsym.

Here is how I create the library:

execl("/usr/bin/gcc", "gcc", "-fPIC", "-Wall", "-g", "-c", "userinput.c", NULL);

and:

execl("/usr/bin/gcc", "gcc", "-ggdb3", "-shared", 
      "-Wl,-soname,libuserinput.so.1", "-o", "libuserinput.so.1.0",
  "userinput.o", "-lc", NULL);

This should work as there is a library after running my code.

I open the library like this:

static void *my_load_dyn (const char *lib) {
static void *handle;
handle = dlopen ("./libuserinput.so.1.0", RTLD_NOW | RTLD_DEEPBIND);
if (handle == NULL) {
  printf ("error at dlopen(): %s\n", dlerror ());
  exit (EXIT_FAILURE);
}
return handle;
}

/* load func from dyn lib"*/
static void *my_load_func (void *handle, char *func) {
  void *funcptr = dlsym (handle, func);
  if (funcptr == NULL) {
    printf ("error at dlsym(): %s\n", dlerror ());
    exit (EXIT_FAILURE);
  }
  return funcptr;
}

and call those functions like this:

void *libhandle;
void (*userMethod) (unsigned char *d);

libhandle = my_load_dyn(LIBUSERINPUT);
userMethod = my_load_func(libhandle, "testMethod");
(*userMethod)(d);

EDIT: here is the code from the userinput.c:

#include <stdio.h>
#include <unistd.h>
void testMethod(unsigned char *d)
{
d[0] = 'Z';
}

It is generated in my programm and also compiled and linked in the running programm

Kindle Q
  • 944
  • 2
  • 19
  • 28
LongRon
  • 73
  • 1
  • 9

1 Answers1

0

i can see two possible problems

  • there's a problem in the code

    are you sure that you declare your method as a public function in userinput.c?

    e.g. if your method is declared static, then it won't be accessible from "outside". also there are other ways to hide functions from being seen outside of the library, but it's impossible to tell whether you are having that problem without seeing any code.

  • your compilation/linking of the library is broken

    e.g. you seem to be linking in a header-file (userinput.h) into the resulting libray?

    i'd suggest to use a proper build system until the problem is solved, and switch to on-the-fly compilation later (e.g. using make)

umläute
  • 28,885
  • 9
  • 68
  • 122
  • i want to compile/link in my programm. Ill post the code of the userinput.c above in my question. Yes the userinput.h is not necessary. Ill remove that one – LongRon Dec 19 '13 at 14:33
  • i'm suggesting that you *first* compile/link outside of your program, and once that works, move to doing it within your program. if you are having troubles with complicated setups, it's always a good idea to lower the complexity as a first step... – umläute Dec 19 '13 at 14:52
  • with compiling/linking myself it works. maybe my forking is bad – LongRon Dec 19 '13 at 15:23