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.
12 {-# OPTIONS -#include "Linker.h" #-}
15 initObjLinker, -- :: IO ()
16 loadDLL, -- :: String -> IO (Maybe String)
17 loadObj, -- :: String -> IO ()
18 unloadObj, -- :: String -> IO ()
19 insertSymbol, -- :: String -> String -> Ptr a -> IO ()
20 insertStableSymbol, -- :: String -> String -> a -> IO ()
21 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
22 resolveObjs -- :: IO SuccessFlag
25 import Panic ( panic )
26 import BasicTypes ( SuccessFlag, successIf )
27 import Config ( cLeadingUnderscore )
30 import Control.Monad ( when )
32 import Foreign ( nullPtr )
33 import GHC.Exts ( Ptr(..), unsafeCoerce# )
35 -- ---------------------------------------------------------------------------
36 -- RTS Linker Interface
37 -- ---------------------------------------------------------------------------
39 insertSymbol :: String -> String -> Ptr a -> IO ()
40 insertSymbol obj_name key symbol
41 = let str = prefixUnderscore key
42 in withCString obj_name $ \c_obj_name ->
43 withCString str $ \c_str ->
44 c_insertSymbol c_obj_name c_str symbol
46 insertStableSymbol :: String -> String -> a -> IO ()
47 insertStableSymbol obj_name key symbol
48 = let str = prefixUnderscore key
49 in withCString obj_name $ \c_obj_name ->
50 withCString str $ \c_str ->
51 c_insertStableSymbol c_obj_name c_str (Ptr (unsafeCoerce# symbol))
53 lookupSymbol :: String -> IO (Maybe (Ptr a))
54 lookupSymbol str_in = do
55 let str = prefixUnderscore str_in
56 withCString str $ \c_str -> do
57 addr <- c_lookupSymbol c_str
60 else return (Just addr)
62 prefixUnderscore :: String -> String
64 | cLeadingUnderscore == "YES" = ('_':)
67 loadDLL :: String -> IO (Maybe String)
69 -- Just err_msg => failure
71 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
72 if maybe_errmsg == nullPtr
74 else do str <- peekCString maybe_errmsg
77 loadObj :: String -> IO ()
79 withCString str $ \c_str -> do
81 when (r == 0) (panic "loadObj: failed")
83 unloadObj :: String -> IO ()
85 withCString str $ \c_str -> do
86 r <- c_unloadObj c_str
87 when (r == 0) (panic "unloadObj: failed")
89 resolveObjs :: IO SuccessFlag
92 return (successIf (r /= 0))
94 -- ---------------------------------------------------------------------------
95 -- Foreign declaractions to RTS entry points which does the real work;
96 -- ---------------------------------------------------------------------------
98 #if __GLASGOW_HASKELL__ >= 504
99 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
100 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
101 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
102 foreign import ccall unsafe "insertStableSymbol" c_insertStableSymbol
103 :: CString -> CString -> Ptr a -> IO ()
104 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
105 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
106 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
107 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int
109 foreign import "addDLL" unsafe c_addDLL :: CString -> IO CString
110 foreign import "initLinker" unsafe initLinker :: IO ()
111 foreign import "insertSymbol" unsafe c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
112 foreign import "insertStableSymbol" unsafe c_insertStableSymbol
113 :: CString -> CString -> Ptr a -> IO ()
114 foreign import "lookupSymbol" unsafe c_lookupSymbol :: CString -> IO (Ptr a)
115 foreign import "loadObj" unsafe c_loadObj :: CString -> IO Int
116 foreign import "unloadObj" unsafe c_unloadObj :: CString -> IO Int
117 foreign import "resolveObjs" unsafe c_resolveObjs :: IO Int