Dyn::Load - FFI to Load Libaries and Access Their Symbols
use Dyn::Load qw[:all]; # Exports nothing by default use Dyn::Call; my $lib = dlLoadLibrary( 'path/to/lib.so' ); my $ptr = dlFindSymbol( $lib, 'add' ); my $cvm = Dyn::Call::dcNewCallVM(1024); Dyn::Call::dcMode( $cvm, 0 ); Dyn::Call::dcReset( $cvm ); Dyn::Call::dcArgInt( $cvm, 5 ); Dyn::Call::dcArgInt( $cvm, 6 ); Dyn::Call::dcCallInt( $cvm, $ptr ); # '5 + 6 == 11'; dlFreeSymbol( $lib );
Dyn::Load wraps the dynload library encapsulates dynamic loading mechanisms and gives access to functions in foreign dynamic libraries and code modules.
dynload
Everything listed here may be imported by name or with the :all tag.
:all
dlLoadLibrary( ... )
Loads a dynamic library at libpath and returns a handle to it for use in dlFreeLibrary( ... ) and dlFindSymbol( ... ) calls.
libpath
dlFreeLibrary( ... )
dlFindSymbol( ... )
my $lib = dlLoadLibrary( 'blah.dll' ); # Or .so, or just... "libmath", idk...
Passing undef for the libpath argument is valid, and returns a handle to the main executable of the calling code. Also, searching libraries in library paths (e.g. by just passing the library's leaf name) should work, however, they are OS specific. Returns a undef on error.
undef
Expected parameters include:
Frees the loaded library.
libhandle
This function returns a pointer to a symbol with name symbol in the library with handle libhandle, or returns a undef pointer if the symbol cannot be found. The name is specified as it would appear in C source code (mangled if C++, etc.).
symbol
dlGetLibraryPath( ... )
This function can be used to get a copy of the path to the library loaded with handle libhandle.
my $len = dlGetLibraryPath($libhandle, $sOut, 1024);
The parameter sOut is a pointer to a buffer of size bufSize (in bytes), to hold the output string.
sOut
bufSize
The return value is the size of the buffer (in bytes) needed to hold the null-terminated string, or 0 if it can't be looked up. If bufSize >= return value >1, a null-terminated string with the path to the library should be in sOut. If it returns 0, the library name wasn't able to be found. Please note that this might happen in some rare cases, so make sure to always check.
0
bufSize >= return value >1
dlSymsInit( ... )
Returns a handle to the shared object specified by libPath for use with other dlSyms* functions.
libPath
dlSyms*
my $dlsym = dlSymsInit();
The dlSyms* functions can be used to iterate over symbols. Since they can be used on libraries that are not linked, they are made for symbol name lookups, not to get symbols' addresses. For that refer to dlFindSymbol( ... ).
The handle must be freed with dlSymsCleanup( ... ).
dlSymsCleanup( ... )
Frees the handle to the shared object created by dlSymsInit( ... ).
dlSymsCleanup( $dlsym );
pSyms
dlSymsCount( ... )
Returns the number of exported symbols in the library.
my $count = dlSymsCount( $dlsym );
dlSymsName( ... )
Returns the name of the symbol at a certain index.
my $name = dlSymsName( $dlsym, 1 );
index
The name is returned as it would appear in C source code (mangled if C++, etc.).
dlSymsNameFromValue( ... )
Returns the name of the symbol at a certain address.
my $name = dlSymsNameFromValue( $dlsym, ... );
value
Copyright (C) Sanko Robinson.
This library is free software; you can redistribute it and/or modify it under the terms found in the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted through this module.
Sanko Robinson <sanko@cpan.org>
To install Dyn, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dyn
CPAN shell
perl -MCPAN -e shell install Dyn
For more information on module installation, please visit the detailed CPAN module installation guide.