tags:

views:

32

answers:

2

Hi, I need to load two dynamic libraries and there is one function name confliction. So I use the the command "objcopy --redefine-sym add=new_add libmy_test.so libmy_test_new.so" to modify the symbol name.

But it still reports "Error: ./libmy_test_new.so: undefined symbol: new_add"

The following are my test codes.

void *lib_handle2 = dlopen("./libmy_test_new.so", RTLD_NOW);
if (NULL == lib_handle2) {
    printf("Error: %s\n", dlerror());
    goto err1;
}

fp_add f_add2 = dlsym(lib_handle2, "new_add");
if (NULL == f_add2) {
    printf("Error: %s\n", dlerror());
    goto err2;
}
A: 

According to this page, it seems it does not work with dynamic symbol. More explanation are available in the original thread. If you want to use both symbol, then you somehow need to relink one of the libraries. However if you want only one of the symbol, then linking order might help you.

Maybe the solution is creating a wrapper library, in which you dlsopen the two libs, create two new symbol, and assign them using dlsym with the correct handle.

void *lib_handle1 = dlopen("./lib1.so", RTLD_NOW);
void *lib_handle2 = dlopen("./lib2.so", RTLD_NOW);

fp_add f_add1 = dlsym((lib_handle1, "add");
fp_add f_add2 = dlsym(lib_handle2, "add");

Of course it does not solve the problem of call generated inside the libraries.

shodanex
Thanks your answer. But is there an alternative method to resolve my problem. My purpose is that load the two libraries at the same time.
Thank you. Actually I tried the method that get two pinter to the same functions in the two libraries. But the second dlsym always returned the pointer returned by the first dlsym, not the real pointer to the function in the second library. So I thought maybe because the same symbol already had existed in the process space. So I wanted to change the symbol name of dynamic library to resolve it.Now I know I could use compile flag "-fPIC" to resolve it.
A: 

Thank you. Actually I tried the method that get two pinter to the same functions in the two libraries. But the second dlsym always returned the pointer returned by the first dlsym, not the real pointer to the function in the second library. So I thought maybe because the same symbol already had existed in the process space. So I wanted to change the symbol name of dynamic library to resolve it.Now I know I could use compile flag "-fPIC" to resolve it.

Certainly thank you all the same.