Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-cafe] Dynamically Linking Foreign Functions

2 views
Skip to first unread message

Richard Warburton

unread,
Dec 31, 2009, 4:28:25 AM12/31/09
to haskel...@haskell.org
Apologies in advance for the length of this email, but I've tried to
be as clear as possible. Any help on the matter most appreciated.

Goal: I have a simple interpreter for a language that I've written,
and I wish to implement an FFI. My ideal api would be like 'ctypes'
in python. Here's an example:

from ctypes import *
libc = CDLL("libc.so.6")
libc.printf("An int %d, a double %f\n", 1234, c_double(3.14))

Prints out:

An int 1234, a double 3.140000

Implementation: I am currently trying to use the dynamic linker in
order to specify the library string, for example the following seems
to work:

import Foreign
import Foreign.C.Types
import Foreign.C.String
import Monad (liftM)
import System.Posix.DynamicLinker

type Fun = CString -> IO CInt
foreign import ccall unsafe "dynamic" c_printf :: FunPtr Fun -> Fun

printf :: String -> IO Int
printf str = do
withDL "libc.so.6" [RTLD_NOW] $ \dl -> do
printf_ptr <- dlsym dl "printf"
let fun = c_printf printf_ptr
liftM fromIntegral $ withCString str fun

main = printf "hello world"

Problem: I don't understand how I can generate the foreign import
statements at runtime. I am currently considering generating strings
of such expressions, and using hs-plugins in order to compile them.
This seems like a complete hack though - I'm dynamically loading a
haskell library in order to dynamically load a foreign library.

Another option is to write a dynamic loader in C, and then call this
from haskell, but how would I type the loading function - obviously I
can give it a CString for the library, and another for the function in
question - but what would the return type be?

regards,

Richard Warburton
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Richard Warburton

unread,
Dec 31, 2009, 6:28:25 AM12/31/09
to Bulat Ziganshin, haskel...@haskell.org
>> Problem: I don't understand how I can generate the foreign import
>> statements at runtime.
>
> there are special C libraries that doest it. one of them is libffi,
> another one (can't recall its name) is used by ghc itself. libffi
> isn't x64-compatible, unlike second one

Thanks a lot. Though the haskell wiki [0] claims that libffi works on x64.

regards,

Richard

[0] http://www.haskell.org/haskellwiki/Library/libffi#Does_it_work.3F

Bulat Ziganshin

unread,
Dec 31, 2009, 7:13:15 AM12/31/09
to Richard Warburton, Bulat Ziganshin, haskel...@haskell.org
Hello Richard,


C/Invoke is another library whose name i forget.


> Thanks a lot. Though the haskell wiki [0] claims that libffi works on x64.

i don't know, just read yesterday on Lua list:

> A question for Fabio: what are the issues with Alien for 64-bit Windows?

I can answer part of that. Libffi [1] (the C library on top of which
Alien is built) has no support for 64-bit windows. More specifically
there is a need for some runtime generated glue code (mainly for
callbacks), and assembler support for x64 is very poor so far (many
Visual C++ compilers come without assembler and/or lack support for
inline asm).

Python and Java (in the JNA package) have their own port of libffi
specifically for x64. However I never managed to compile any of these
two for Alien (usually because of the lack of assembler).

I tried all this about a year ago, so things may have changed since then.

[1] http://sourceware.org/libffi/


--
Best regards,
Bulat mailto:Bulat.Z...@gmail.com

0 new messages