error at dlsym(): ./dynlibrary: undefined symbol: method

2.8k Views Asked by At

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

1

There are 1 best solutions below

3
On

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)