I have an application which loads liba.so and at run time liba.so opens libb.so. Starting with SUSE Linux Enterprise Server 15 SP6 the code fails to load libb.so and does not use RUNPATH on the shared library. What has changed in SP6 from SP4 and how to make this code work?
-
Create a file a.c with the following code:
#include <stdio.h>
#include <dlfcn.h>
void a_func(void) {
printf(“a.so: Trying to load b.so …\n”);
void *handle = dlopen(“libb.so”, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
printf(“Failed to load b.so: %s\n”, dlerror());
return;
}
void (*b_func)(void) = dlsym(handle, “b_func”);
b_func();
}
Compile it with the following command:
gcc -fPIC -shared -o liba.so a.c -Wl,-rpath,‘$ORIGIN’ -
Create another file b.c in the same directory as a.c, with the following code:
#include <stdio.h>
void b_func(void) {
printf(“Hello from b.so!\n”);
}
Compile it with the following command:
gcc -fPIC -shared -o libb.so b.c
- Create another file main.c in another directory with the following code:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
// Load the shared library
void *handle = dlopen(“/path/to/liba.so”, RTLD_LAZY);
if (!handle) {
fprintf(stderr, “Error loading library: %s\n”, dlerror());
exit(EXIT_FAILURE);
}
// Clear any existing errors
dlerror();
// Get the function pointer
void (*a_func)() = (void (*)())dlsym(handle, "a_func");
char *error = dlerror();
if (error != NULL) {
fprintf(stderr, "Error locating function: %s\n", error);
dlclose(handle);
exit(EXIT_FAILURE);
}
// Call the function
a_func();
// Close the library
dlclose(handle);
return 0;
}
Compile it with the following command:
gcc -o main.exe main.c -ldl
on SUSE 15SP4
main.exe
a.so: Trying to load b.so …
Hello from b.so!
on SUSE 15SP6
main.exe
a.so: Trying to load b.so …
Failed to load b.so: libb.so: cannot open shared object file: No such file or directory