dlopen from a child process to access parent process functions
Ravindra Jejurikar
jezziejezz@yahoo.com
Sun Jun 22 05:51:00 GMT 2008
Hello,
I would like a child process to have access to the parent process's functions using dlopen(). Is this possible?
With the below code, foo() cannot be resolved.
(It is working - foo() can be resoved - when all code is in a single binary (instead of two as below))
dlmain.c
#include<stdio.h>
int main()
{
printf("Hello Main: \n");
system("dlsample");
}
int foo(int num) {
printf("Hello foo\n");
}
-------------------------------------------------------------
dlsample.c
typedef int (*ptrFunc)(int);
main()
char * errstr = NULL;
int mode = RTLD_NOW | RTLD_PARENT | RTLD_GLOBAL ;
void * lh = NULL;
char * symname = "foo";
ptrFunc func = NULL;
printf("Hello WOrld\n");
// Open the GLOBAL executing object namespace
lh = dlopen(NULL, mode);
if (lh == NULL) {printf("Failed to dlopen()\n");return(1);}
// Find the function symbol
func = dlsym(lh, symname);
errstr = dlerror();
if (errstr) {printf("Error: \"%s\" in resolving symbol\n", errstr);return(1);}
if (func == NULL) {printf("Failed to resolve %s", symname);return(2);}
printf("Symbol %s found at %d\n", symname, func);
(*func)(10);
return(0);
}
FYI: linking as below
gcc -Wl,-export-dynamic -o dlsample dlsample.o
Reason to have two binaries: Eventually, I want to call some perl code from the C code and the perl code should be able to access some C functions. So I am trying with two different C binaries to start with.
Is there an easier way to do what I am trying. I cannot have my perl code as the master with the C code as a .so (complex legacy C code - in short)
Thank you
More information about the Gcc-help
mailing list