Say I have hello1.c
char *greeting = "Hello, Version 1";
char *greet(void) {
return greeting;
}
and hello2.c
int greeting = 42;
int greet(void) {
return greeting;
}
My host.c
looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>
#define LIBHELLO_SO "libhello.so"
void load_and_call_greet() {
// Open the shared library
void *dynamic_lib = dlopen(LIBHELLO_SO, RTLD_LAZY);
if (!dynamic_lib) {
fprintf(stderr, "%sn", dlerror());
return;
}
// Define a function pointer for the function we want to call
char *(*greet)(void);
// Get the function from the shared library
greet = (char *(*)(void)) dlsym(dynamic_lib, "greet");
// Check for errors
char *error;
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%sn", error);
dlclose(dynamic_lib); // Close the library before exiting
return;
}
// Call the function and print the result
printf("%sn", greet());
// Close the shared library
dlclose(dynamic_lib);
}
int main() {
while (1) {
load_and_call_greet();
sleep(5); // Wait for 5 seconds before reloading the library
}
return 0;
}
Then, I compile and run host file and without terminating host file I want to swap my dynamic library with new signature.
Is it possible to change host.c
code to load function with changing signature from dynamic library?
I tried couple of approaches with ChatGPT but it gives SIGSEGV (Address boundary error)
.