Dynamically Linking Foreign Functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamically Linking Foreign Functions

Richard Warburton
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically Linking Foreign Functions

Bulat Ziganshin-2
Hello Richard,

Thursday, December 31, 2009, 12:28:01 PM, you wrote:

> 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


--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically Linking Foreign Functions

Richard Warburton
>> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Dynamically Linking Foreign Functions

Bulat Ziganshin-2
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:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe