Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Blending perl and C (two approaches)

by mwah (Hermit)
on Oct 28, 2007 at 16:51 UTC ( [id://647710]=note: print w/replies, xml ) Need Help??


in reply to Blending perl and C (two approaches)

holandes777
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

  • Comment on Re: Blending perl and C (two approaches)

Replies are listed 'Best First'.
Re^2: Blending perl and C (two approaches)
by holandes777 (Scribe) on Oct 28, 2007 at 21:11 UTC
    This approach sems promising so I went through the steps and .. ran into a briick wall, HARD (ouch!) Here are the notes as I progressed:  
    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

      The perl-relevant part of the Makefile under Linux or *BSD would look like:

      ... VPATH = $(MYSOURCES) : $(MYTOOLS) ... #Linux + Freebsd: use the magic configurator "ExtUtils::Embed" PERLCC=`perl -MExtUtils::Embed -e perl_inc` PERLLD=`perl -MExtUtils::Embed -e ldopts -- -std` PLSTATIC= # -static # if static libperl.a, set this to -static, otherwise to (none) CXXFLAGS=-O3 COMPILE.cxx=$(CXX) $(CFLAGS) $(CXXFLAGS) $(PERLCC) -I$(MYHEADERS) -c .cxx.o: $(COMPILE.cxx) $< -o $(@F) $(EXE): $(F_MYSOURCE) $(F_MYTOOLS) Makefile $(CXX) -o $@ $(F_MYSOURCE) $(F_MYTOOLS) $(PERLLD) $(PLSTATIC) -lst +dc++ -s ...

      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

Re^2: Blending perl and C (two approaches)
by Hercynium (Hermit) on Oct 28, 2007 at 18:00 UTC
    Better yet, perhaps you could store the byte-code, and pass *that* through an embedded interpreter? (Heck, while you're at it, encrypt the byte-code as well!)

    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.

      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.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Well, what I was referring to was the info in here. Granted, I'm a neophyte in the department of perl internals, but I've head of the -B option being used for that sort of purpose.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://647710]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found