简体   繁体   English

当共享库已经映射到进程空间时,是否可以编写 XS 模块?

[英]Is it possible to write an XS module when the shared library is already mapped into the process space?

This is on the limits of what I know, please correct any confusion here.这是在我所知道的范围内,请在此处纠正任何混淆。 In this question I ask why functionality provided by libm isn't already exposed to the user with a Perl interface.在这个问题中,我问为什么libm提供的功能尚未通过 Perl 接口向用户公开。 Now I want to know how it's done.现在我想知道它是如何完成的。

There is a module on CPAN that claims to do called Math::Libm . CPAN 上有一个声称可以做的模块Math::Libm This works fine, but as a question on implementation when I use this I find that not just is libm mapped into the process as with any running Perl interpreter on Debian,这很好用,但是作为一个关于实现的问题,当我使用它时,我发现不仅 libm 被映射到进程中,就像在 Debian 上运行的任何 Perl 解释器一样,

7f719246d000-7f719247c000 r--p 00000000 fe:01 86513750                   /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f719247c000-7f7192516000 r-xp 0000f000 fe:01 86513750                   /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f7192516000-7f71925af000 r--p 000a9000 fe:01 86513750                   /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f71925af000-7f71925b0000 r--p 00141000 fe:01 86513750                   /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f71925b0000-7f71925b1000 rw-p 00142000 fe:01 86513750                   /usr/lib/x86_64-linux-gnu/libm-2.31.so

But Math::Libm also maps in another libm shared object,但是Math::Libm也映射到另一个 libm 共享 object,

7fb649043000-7fb649044000 r--p 00000000 fe:01 11143927                   /home/ecarroll/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/auto/Math/Libm/Libm.so
7fb649044000-7fb649049000 r-xp 00001000 fe:01 11143927                   /home/ecarroll/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/auto/Math/Libm/Libm.so
7fb649049000-7fb64904a000 r--p 00006000 fe:01 11143927                   /home/ecarroll/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/auto/Math/Libm/Libm.so
7fb64904a000-7fb64904b000 r--p 00006000 fe:01 11143927                   /home/ecarroll/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/auto/Math/Libm/Libm.so
7fb64904b000-7fb64904c000 rw-p 00007000 fe:01 11143927                   /home/ecarroll/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/auto/Math/Libm/Libm.so

This seems to be because Autoloader loads the external shared object.这似乎是因为Autoloader加载了外部共享 object。

Is it possible to access the symbols already in the resident process without loading an external shared object?是否可以在不加载外部共享 object 的情况下访问驻留进程中已经存在的符号?

Is it possible to access the symbols already in the resident process without loading an external shared object?是否可以在不加载外部共享 object 的情况下访问驻留进程中已经存在的符号?

You need to load the XS Libm.so to access C library functions from Perl, but when Libm.so is loaded at runtime using dlopen() (see dl_load_file() ) the dynamic loader will recognize that Libm.so requires libm.so but since libm.so has already been loaded by the dynamic linker (when running perl itself), it will not be reloaded but an existing filehandle will be returned, see this Q&A, so the shared library libm.so is not loaded twice.您需要加载 XS Libm.so以从 Perl 访问 C 库函数,但是当Libm.so在运行时使用dlopen()加载时(请参阅dl_load_file() ),动态加载器将识别libm.soLibm.so但由于libm.so已经被动态 linker加载(运行 perl 本身时),它不会被重新加载,但会返回现有的文件句柄,请参阅Q&A,因此共享库libm.so不会加载两次。

Footnotes :脚注

†: But see also C::DynaLib , and FFI::Platypus for alternatives that does not require XS †:但另请参阅C::DynaLibFFI::Platypus以获得不需要 XS 的替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM