4 msgQuestions about __attribute((cold))
3 msghow to initialize union?
4 msgOn the C99 Status
1 msgProblem with GCC 4.2 and above
5 msgHelp!!! build failed for GCC-3.2.3
1 msgPassing array variable as Input Constraint to l...
7 msgException handling on AIX5.3 with gcc 3.4.6
1 msgINT64
1 msgPassing array variable address as Input Constra...

Dynamic compilation using gcc
\ Klaus Grue (20 Mar 2008)
. \ Brian Dessent (20 Mar 2008)
. . \ Klaus Grue (20 Mar 2008)

3 msgWhat does the version number of gcc mean ?
1 msgCome join me on ssbbw4u...
3 msg64-bit write() system call
23 msgtry, finally
3 msgstack variables and loops
2 msgHow can I get the dump file which is in SSA form ?
1 msgSie wurden ins RotlichtVZ eingeladen
2 msgModifying the std C libraries
1 msgRe: An error occured when building gcc4.3.0
5 msgStrange syntax error problem with mmorpg-framework
Subject:Dynamic compilation using gcc
Group:Gcc-help
From:Klaus Grue
Date:20 Mar 2008


Hi gcc-help,

I have a three questions about dynamic compilation using gcc which I post
to gcc-help since I have been unable to find much on the issue using FAQs
and Google. I hope someone can provide answers or pointers to information.

Background: I need a function

void *source2dl(const char *source,int flag);

which, given a C source text, compiles the source into a dynamic library
and returns a handle to that library. The signature of source2dl() above
is identical to that of dlopen(), but source2dl() takes a C source as
input where dlopen() takes the pathname of a shared object.

I include a working but in some respects inconvenient implementation of
source2dl() below. My questions come after the implementation of
source2dl().

---------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dlfcn.h>

//Die with last word from perror
void pdie(const char *msg){perror(msg);exit(0);}

//Die with last word from dlerror
void dldie(const char *msg){printf("%s: %s\n",msg,dlerror());exit(0);}

//Convert source text to dynamic library
void *source2dl(const char *source,int flag){
int fd[2];pid_t pid;int status;void *dl;
//Create pipe for piping source to gcc
if(pipe(fd)<0) pdie("pipe()");
//Fork a gcc, pipe source to it, wait for gcc to exit
pid=fork();
if(pid<0) pdie("fork()");
if(pid==0){
// Child process: close write end of pipe
close(fd[1]);
// Send pipe input to stdin
close(STDIN_FILENO);
dup2(fd[0],STDIN_FILENO);
// Invoke gcc with the following arguments:
// -x c Tell gcc that we provide C source
// -pipe Use pipes between gcc stages
// -fPIC -shared Make shared object
// -Wl,-soname,dl.so Pass suitable options to linker
// -o dl.so Write shared object to dl.so
execl("/usr/bin/gcc","gcc","-x","c","-pipe","-","-fPIC","-shared",
"-Wl,-soname,dl.so","-o","dl.so",(char *)NULL);
// Complain if execl() returns
pdie("execl()");}
//Parent process: close read end of pipe
close(fd[0]);
//Pipe source to gcc
write(fd[1],source,strlen(source));
close(fd[1]);
//Wait for gcc to exit
if(wait(&status)<0) pdie("wait()");
//Load output from gcc and return it
dl=dlopen("./dl.so",flag);
if(!dl) dldie("dlopen()");
return dl;}

//Main program for testing source2dl()
int main(int argc,char *argv[]){
//Set prog to
//#include <stdio.h>
//void fct(){printf("Hello world\n");}
char *prog="#include <stdio.h>\nvoid fct(){printf(\"Hello
world\\n\");}\n";
//Convert prog to dl and extract fct
void (*fct)()=dlsym(source2dl(prog,RTLD_NOW),"fct");
if(!fct) dldie("dlsym()");
//Invoke fct
(*fct)();
return 0;}

---------------------------------------------------------------------

The program above prints "Hello world" to stdout as intended. However,
here are my questions:

Question 1. The dlopen(), dlclose(), dlsym(), and dlerror() functions from
libdl provide a library interface to the dynamic loader. Does there exist
a similar library interface to gcc so that one can avoid the
pipe-fork-execve-wait overhead of running gcc in a separate process?

Question 2. The implementation of source2dl() above uses a temporary file
named dl.so for the shared object produced by gcc and read by dlopen. It
would be convenient if communication from gcc to dlopen could go via a
pipe. Does anyone know a way to tell gcc to write the shared object to
stdout?

Question 3. Following up on Question 2, does anyone know a way to tell
glopen to read the shared object from stdin?

I hope someone listening at gcc-help can help on these questions or give
pointers to information or give suggestions for alternative ways of doing
dynamic compilation.

Cheers,
Klaus


© 2004-2008 readlist.com