[erlang-questions] EEP7 FFI patch for R11B-5

Alceste Scalas <>
Mon Mar 30 12:41:00 CEST 2009


Quoting Zvi <>:
> 3> erl_ddll:load_library("/usr/lib/libc.so", mylib, [{preload,  
> [{malloc, {void, pointer}},{free, {void, nonnull}}]}]).
> {error,{open_error,-13}}

There are three problems here.

First of all, erl_ddll:load_library/3 expects a path as first
argument, and a filename (excluding the .so extension) as second
argument (just like erl_ddll:load/2).  You should, thus, call
something like this:

     erl_ddll:load_library("/usr/lib", libc, [{preload, [{malloc,  
{void, pointer}},{free, {void, nonnull}}]}]).

Another problem is that /usr/lib/libc.so is not a regular .so file as
one might expect.  Here are its contents on my laptop (Ubuntu 8.10):

     [ ~]$ cat /usr/lib/libc.so
     /* GNU ld script
        Use the shared library, but some functions are only in
        the static library, so try that secondarily.  */
     OUTPUT_FORMAT(elf32-i386)
     GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a  AS_NEEDED (  
/lib/ld-linux.so.2 ) )

The real file you should open is /lib/libc.so.6.

And here comes the final problem: erl_ddll:load_library/3, just like
other erl_ddll functions, expects the file to end with .so.  And since
/lib/libc.so.6 does not respect this requirement, you should create a
symlink with the proper name, for example:

     ln -s /lib/libc.so.6 /tmp/libc.so

After that, you can simply execute:

     erl_ddll:load_library("/tmp", libc, [{preload, [{malloc, {void,  
pointer}},{free, {void, nonnull}}]}]).

And it should work as expected.

Note, however, that all .so files are (usually) automatically linked
to libc: you can open any regular .so file and just use it to call
libc functions through the FFI (thus avoiding the symlink cruft above).

Regards,
-- 
Alceste Scalas <>

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the erlang-questions mailing list