2 % (c) The University of Glasgow, 2000
5 -- ---------------------------------------------------------------------------
6 -- The dynamic linker for object code (.o .so .dll files)
7 -- ---------------------------------------------------------------------------
9 Primarily, this module consists of an interface to the C-land dynamic linker.
12 {-# OPTIONS -#include "Linker.h" #-}
15 initObjLinker, -- :: IO ()
16 loadDLL, -- :: String -> IO (Maybe String)
17 loadObj, -- :: String -> IO ()
18 unloadObj, -- :: String -> IO ()
19 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
20 resolveObjs -- :: IO SuccessFlag
26 import Foreign ( Ptr, nullPtr )
27 import Panic ( panic )
28 import BasicTypes ( SuccessFlag, successIf )
29 import Config ( cLeadingUnderscore )
32 -- ---------------------------------------------------------------------------
33 -- RTS Linker Interface
34 -- ---------------------------------------------------------------------------
36 lookupSymbol :: String -> IO (Maybe (Ptr a))
37 lookupSymbol str_in = do
38 let str = prefixUnderscore str_in
39 withCString str $ \c_str -> do
40 addr <- c_lookupSymbol c_str
43 else return (Just addr)
45 prefixUnderscore :: String -> String
47 | cLeadingUnderscore == "YES" = ('_':)
50 loadDLL :: String -> IO (Maybe String)
52 -- Just err_msg => failure
54 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
55 if maybe_errmsg == nullPtr
57 else do str <- peekCString maybe_errmsg
60 loadObj :: String -> IO ()
62 withCString str $ \c_str -> do
64 when (r == 0) (panic "loadObj: failed")
66 unloadObj :: String -> IO ()
68 withCString str $ \c_str -> do
69 r <- c_unloadObj c_str
70 when (r == 0) (panic "unloadObj: failed")
72 resolveObjs :: IO SuccessFlag
75 return (successIf (r /= 0))
77 -- ---------------------------------------------------------------------------
78 -- Foreign declaractions to RTS entry points which does the real work;
79 -- ---------------------------------------------------------------------------
81 #if __GLASGOW_HASKELL__ >= 504
82 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
83 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
84 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
85 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
86 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
87 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int
89 foreign import "addDLL" unsafe c_addDLL :: CString -> IO CString
90 foreign import "initLinker" unsafe initLinker :: IO ()
91 foreign import "lookupSymbol" unsafe c_lookupSymbol :: CString -> IO (Ptr a)
92 foreign import "loadObj" unsafe c_loadObj :: CString -> IO Int
93 foreign import "unloadObj" unsafe c_unloadObj :: CString -> IO Int
94 foreign import "resolveObjs" unsafe c_resolveObjs :: IO Int