This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
dlopen from a child process to access parent process functions
- From: Ravindra Jejurikar <jezziejezz at yahoo dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 21 Jun 2008 22:51:14 -0700 (PDT)
- Subject: dlopen from a child process to access parent process functions
- Reply-to: jezziejezz at yahoo dot com
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