Hi, i have a problem with dynamically loading shared objects which in turn are linked with perl libs and load DynaLoader using newXS.
I have this program that dynamically loads several modules, shared objects, which give the program its functionality. So far so good but yesterday i decided to add a module that would evaluate perl code using eval_pv and instructions i learned about in perlembed.
First of all, this required me to change my Makefile because perlembed states that i must compile my code EXACTLY the same as the perl interpreter was compiled, and i was using two different rules for compiling and linking. So now i'm using one rule and doing the linking through gcc(cc) in order to use the perl -MExtUtils::Embed -e ccopts -e ldopts command to generate my arguments.
I wanted to load Data::Dumper in the perl code being evaluated in order to return a general string for all sorts of variables returned so i followed the perlembed instructions for using xs_init and newXS to load DynaLoader so i could use Data::Dumper, which worked.
So to make a long story short i ran into a problem, running the code as a standalone executable file works fine but as soon as i turn it into a shared object and load it using dlopen from my main program i get this error: Undefined symbol "boot_DynaLoader"
So instead of forcing you through my project, i wrote up a small example program that reproduces the problem and mimicks the functionality of my program enough for you to understand what i'm trying to do and still read it through easily.
I hope someone can hint me as to what i'm doing wrong here and i'm sorry for any errors in this post but i finished this up pretty late in the evening. I was thinking about posting the same to perl-xs but i figured i'd see what you guys say about it first.
I found some other posts on perl-xs that resembled my problem but they were either unsolved or didn't even have any replies.
Here is the code for perl_test1.c.
/* compiled with cc -Wall -pedantic -ansi -rpath . -L. `perl -MExtUtil +s::Embed -e ccopts -e ldopts` -fPIC -shared -soname perl_test1.so -o +perl_test1.so -lc perl_test1.c */ #include <stdlib.h> #include <EXTERN.h> #include <perl.h> #ifndef PERL_NO_SHORT_NAMES #define PERL_NO_SHORT_NAMES #endif void pprint(void); static void xs_init(pTHX); EXTERN_C void boot_DynaLoader(pTHX_ CV* cv); static PerlInterpreter *perl; EXTERN_C void xs_init(pTHX) { char *file = __FILE__; newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); } void pprint(void) { int argc = 0; char **argv = NULL; char **env = NULL; SV *retval; STRLEN n_a; char *embedding[] = { "", "-e", "0" }; PL_origalen = 1; PERL_SYS_INIT3(&argc, &argv, &env); perl = perl_alloc(); perl_construct(perl); perl_parse(perl, xs_init, 3, embedding, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(perl); eval_pv("use Data::Dumper qw(Dumper);" "local $Data::Dumper::Terse = 1;" "local $Data::Dumper::Quotekeys = 0;" "local $Data::Dumper::Indent = 0;" "local $Data::Dumper::Useqq = 1;", TRUE); retval = eval_pv("my $code='\"this is a string\"';my $ret=eval $co +de;return(Dumper($ret));", TRUE); printf("%s", SvPV(retval, n_a)); perl_destruct(perl); perl_free(perl); PERL_SYS_TERM(); exit(0); }
And here is the code for perl_test2.c.
/* compiled with cc -Wall -pedantic -ansi -rpath . -L. -o perl_test2 p +erl_test2.c */ #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> int main(void) { void *mod = NULL; void (*func)(void); if((mod = dlopen("perl_test1.so", (RTLD_LAZY | RTLD_LOCAL))) == NU +LL) { fprintf(stderr, "Failed loading module: %s\n", dlerror()); exit(-1); } if((func = (void *)dlsym(mod, "pprint")) == NULL) { fprintf(stderr, "Failed locating symbol: %s\n", dlerror()); dlclose(mod); } else { (*func)(); } exit(0); }
While searching your database i found this post with the exact same problem but no replies. He mentions something i've been suspecting based on other finds on the net, that DynaLoader.a is not compiled to be shared like this, its name alone hints on this.
In reply to Problem with dlopen and shared objects using perl libs by swehack
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |