2 % (c) The University of Glasgow, 2000-2006
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.
13 initObjLinker, -- :: IO ()
14 loadDLL, -- :: String -> IO (Maybe String)
15 loadArchive, -- :: String -> IO ()
16 loadObj, -- :: String -> IO ()
17 unloadObj, -- :: String -> IO ()
18 insertSymbol, -- :: String -> String -> Ptr a -> IO ()
19 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
20 resolveObjs -- :: IO SuccessFlag
24 import BasicTypes ( SuccessFlag, successIf )
25 import Config ( cLeadingUnderscore )
27 import Control.Monad ( when )
29 import Foreign ( nullPtr )
30 import GHC.Exts ( Ptr(..) )
34 -- ---------------------------------------------------------------------------
35 -- RTS Linker Interface
36 -- ---------------------------------------------------------------------------
38 insertSymbol :: String -> String -> Ptr a -> IO ()
39 insertSymbol obj_name key symbol
40 = let str = prefixUnderscore key
41 in withCString obj_name $ \c_obj_name ->
42 withCString str $ \c_str ->
43 c_insertSymbol c_obj_name c_str symbol
45 lookupSymbol :: String -> IO (Maybe (Ptr a))
46 lookupSymbol str_in = do
47 let str = prefixUnderscore str_in
48 withCString str $ \c_str -> do
49 addr <- c_lookupSymbol c_str
52 else return (Just addr)
54 prefixUnderscore :: String -> String
56 | cLeadingUnderscore == "YES" = ('_':)
59 loadDLL :: String -> IO (Maybe String)
61 -- Just err_msg => failure
63 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
64 if maybe_errmsg == nullPtr
66 else do str <- peekCString maybe_errmsg
69 loadArchive :: String -> IO ()
71 withCString str $ \c_str -> do
72 r <- c_loadArchive c_str
73 when (r == 0) (panic ("loadArchive " ++ show str ++ ": failed"))
75 loadObj :: String -> IO ()
77 withCString str $ \c_str -> do
79 when (r == 0) (panic ("loadObj " ++ show str ++ ": failed"))
81 unloadObj :: String -> IO ()
83 withCString str $ \c_str -> do
84 r <- c_unloadObj c_str
85 when (r == 0) (panic ("unloadObj " ++ show str ++ ": failed"))
87 resolveObjs :: IO SuccessFlag
90 return (successIf (r /= 0))
92 -- ---------------------------------------------------------------------------
93 -- Foreign declarations to RTS entry points which does the real work;
94 -- ---------------------------------------------------------------------------
96 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
97 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
98 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
99 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
100 foreign import ccall unsafe "loadArchive" c_loadArchive :: CString -> IO Int
101 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
102 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
103 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int