in reply to Blending perl and C (two approaches)
Any advice is appreciated
OK, then I'd say: drop both approaches and embed the Perl interpreter in the C program (means: link the perl58.lib to the executable and initiate an internal perl instance after going trough main).
You could then read a totally garbled data file from there and decode it to an runable perl source internally and invoke it through the "internal" Perl instance.
I did this in Unix and Win32 environments and it works good and is perfectly portable (did it without the garbling/decoding step ;-).
see: perlembed
Regards
mwa
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Blending perl and C (two approaches)
by holandes777 (Scribe) on Oct 28, 2007 at 21:11 UTC | |
How to Embed Perl in C From perlembed's information: every C program that uses Perl must link in the perl library you can't use Perl from your C program unless Perl has been compiled on your machine, or installed properly--that's why you shouldn't blithely copy Perl executables from machine to machine without also copying the lib directory.) perl -V output: 1) find / -name perl.h - take note of the subdir, the perl library (and EXTERN.h and perl.h, which you'll also need) will reside in that directory 2) where? (confirm with perl -MConfig -e 'print $Config{archlib}') 3) what compiler? perl -MConfig -e 'print $Config{cc}' - usually gcc 4) what to add to the gcc compiler command, use perl -V and look at something like this: cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', 5) extra libraries are shown by perl -MConfig -e 'print $Config{libs}' -lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc[ Summary of my perl5 (revision 5 version 8 subversion 6) configuration: Platform: osname=linux, osvers=2.4.21-27.0.2.elsmp, archname=i386-linux-thread-multi uname='linux decompose.build.redhat.com 2.4.21-27.0.2.elsmp #1 smp wed jan 12 23:35:44 est 2005 i686 i686 i386 gnulinux ' config_args='-des -Doptimize=-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -Dversion=5.8.6 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Duseshrplib -Dusethreads -Duseithreads -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl=n -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr -Dd_gethostent_r_proto -Ud_endhostent_r_proto -Ud_endprotoent_r_proto -Ud_endservent_r_proto -Ud_sethostent_r_proto -Ud_setprotoent_r_proto -Ud_setservent_r_proto -Dinc_version_list=5.8.5 5.8.4 5.8.3' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemultiplicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', optimize='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables', cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/include/gdbm' ccversion='', gccversion='4.0.0 20050516 (Red Hat 4.0.0-6)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=/lib/libc-2.3.5.so, so=so, useshrplib=true, libperl=libperl.so gnulibc_version='2.3.5' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E -Wl,-rpath,/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: DEBUGGING MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT the simpler approach appeared to be: gcc -o interp interp.c 'perl -MExtUtils::Embed -e ccopts -e ldopts' produces these errors gcc: perl -MExtUtils::Embed -e ccopts -e ldopts: No such file or directory interp.c:1:20: error: EXTERN.h: No such file or directory interp.c:2:18: error: perl.h: No such file or directory interp.c:4: error: syntax error before â*â token interp.c:4: warning: data definition has no type or storage class interp.c: In function âmainâ: interp.c:8: warning: assignment makes pointer from integer without a cast interp.c:10: error: âNULLâ undeclared (first use in this function) interp.c:10: error: (Each undeclared identifier is reported only once interp.c:10: error: for each function it appears in.) ------------------------------------------------------------------ so I tried another approach (the file below should be read from the last line up and shows what went wrong: Here are the errors: /tmp/ccLefb5c.o(.text+0x12): In function `main': interp.c: undefined reference to `perl_alloc' /tmp/ccLefb5c.o(.text+0x20):interp.c: undefined reference to `perl_construct' /tmp/ccLefb5c.o(.text+0x36):interp.c: undefined reference to `perl_parse' /tmp/ccLefb5c.o(.text+0x44):interp.c: undefined reference to `perl_run' /tmp/ccLefb5c.o(.text+0x50):interp.c: undefined reference to `perl_destruct' /tmp/ccLefb5c.o(.text+0x66):interp.c: undefined reference to `perl_free' collect2: ld returned 1 exit status Here are the executions of the compiler (from bottom to top: gcc -O2 -D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread # still complains about not finding perl_alloc gcc -O2 -D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread # above, adding the ccflags # complains about not finding perl_alloc (again) gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread # complains about -lc[ gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc[ # complains about -ldb gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -ldb -ldl -lm -lcrypt -lutil -lpthread -lc[ # complais it cant find lgdbm gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc[ # complains about perl_alloc gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lm # complains about -lperl gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -L/usr/local/include -I/usr/lib/perl5/5.8.6/i386-linux-thread-multi/CORE -o interp interp.c -lperl -lm | [reply] |
by mwah (Hermit) on Oct 28, 2007 at 21:27 UTC | |
The perl-relevant part of the Makefile under Linux or *BSD would look like:
The 'MYHEADERS', 'F_MYSOURCE', 'F_MYTOOLS' etc. designate the apps source files and directories (and are set elsewhere), the Perl-stuff goes into 'PERLCC' (headers) and 'PERLLD' (libs). This will magically construct a perl.lib linked app ;-). Regards mwa | [reply] [d/l] |
|
Re^2: Blending perl and C (two approaches)
by Hercynium (Hermit) on Oct 28, 2007 at 18:00 UTC | |
This may help in preventing some clever hacker from attaching to the process via a debugger and capturing the program 's contents as it's being passed for the eval. | [reply] |
by diotalevi (Canon) on Oct 30, 2007 at 23:49 UTC | |
Perl does not have bytecode. The non-existent bytecode can't be stored and then retrieved later. If it existed, this bytecode could be deparsed back to perl code. ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊ | [reply] |
by Hercynium (Hermit) on Oct 31, 2007 at 00:57 UTC | |
| [reply] |
by diotalevi (Canon) on Oct 31, 2007 at 21:21 UTC | |