After further discussion with tigger^ on IRC, and some tinkering, I came up with the following.
/*
gcc -Wall -c perl-embed.c `perl -MExtUtils::Embed -e ccopts`
gcc -Wall perl-embed.o `perl -MExtUtils::Embed -e ldopts` -o perl
+-embed
*/
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
EXTERN_C void xs_init _((pTHX));
EXTERN_C void boot_DynaLoader _((pTHX_ CV* cv));
EXTERN_C void
xs_init(pTHX)
{
dXSUB_SYS;
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
}
int
main(int argc, char **argv, char **env)
{
char *embedding[] = { "", "-e" };
SV *my_sv;
double (*my_symbol)(double, double);
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, xs_init, 3, embedding, (char **)NULL);
perl_run(my_perl);
my_sv = eval_pv(
"use DynaLoader;"
"my $h = DynaLoader::dl_load_file(\"libm.so\");"
"DynaLoader::dl_find_symbol($h, \"pow\");",
TRUE
);
my_symbol = (void *)SvIV(my_sv);
printf(
"Symbol %p, (*my_symbol)(4,4) = %f\n",
my_symbol,
(*my_symbol)(4,4)
);
perl_destruct(my_perl);
perl_free(my_perl);
return 0;
}
Note, I'm using the standard C function pow() as a stand-in for the original PmmNodeToSv. The important part is that the library is loaded and the symbol retrieved using DynaLoader.
The problem with this solution is that it uses a specifically unspecified feature of DynaLoader. The documentation specifically states the symbol returned by dl_find_symbol() or dl_find_symbol_anywhere() should only be used in a call to dl_install_xsub(), you should not rely on it for anything else.
The alternative to this approach seems even less portable though, namely to find the library (auto/XML/LibXML.so) in @INC, dlopen() it, then fetch the symbol with dlsym(). At least with the DynaLoader method you get some level of abstraction between operating systems.
Are there any alternatives to these methods? How portable is using the symbol returned by DynaLoader, really? From my reading of the source (ext/DynaLoader/dl_*.xs) it seems like this method should work on every OS. Perhaps the documentation is simply leaving a window open for later changes.
update: removed extraneous include of dlfcn.h, a leftover from earlier attempts at a solution.
|