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 insertSymbol, -- :: String -> String -> Ptr a -> IO ()
20 insertStableSymbol, -- :: String -> String -> a -> IO ()
21 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
22 resolveObjs -- :: IO SuccessFlag
28 import Foreign ( nullPtr )
29 import Panic ( panic )
30 import BasicTypes ( SuccessFlag, successIf )
31 import Config ( cLeadingUnderscore )
34 import GHC.Exts ( Ptr(..), unsafeCoerce# )
36 -- ---------------------------------------------------------------------------
37 -- RTS Linker Interface
38 -- ---------------------------------------------------------------------------
40 insertSymbol :: String -> String -> Ptr a -> IO ()
41 insertSymbol obj_name key symbol
42 = let str = prefixUnderscore key
43 in withCString obj_name $ \c_obj_name ->
44 withCString str $ \c_str ->
45 c_insertSymbol c_obj_name c_str symbol
47 insertStableSymbol :: String -> String -> a -> IO ()
48 insertStableSymbol obj_name key symbol
49 = let str = prefixUnderscore key
50 in withCString obj_name $ \c_obj_name ->
51 withCString str $ \c_str ->
52 c_insertStableSymbol c_obj_name c_str (Ptr (unsafeCoerce# symbol))
54 lookupSymbol :: String -> IO (Maybe (Ptr a))
55 lookupSymbol str_in = do
56 let str = prefixUnderscore str_in
57 withCString str $ \c_str -> do
58 addr <- c_lookupSymbol c_str
61 else return (Just addr)
63 prefixUnderscore :: String -> String
65 | cLeadingUnderscore == "YES" = ('_':)
68 loadDLL :: String -> IO (Maybe String)
70 -- Just err_msg => failure
72 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
73 if maybe_errmsg == nullPtr
75 else do str <- peekCString maybe_errmsg
78 loadObj :: String -> IO ()
80 withCString str $ \c_str -> do
82 when (r == 0) (panic "loadObj: failed")
84 unloadObj :: String -> IO ()
86 withCString str $ \c_str -> do
87 r <- c_unloadObj c_str
88 when (r == 0) (panic "unloadObj: failed")
90 resolveObjs :: IO SuccessFlag
93 return (successIf (r /= 0))
95 -- ---------------------------------------------------------------------------
96 -- Foreign declaractions to RTS entry points which does the real work;
97 -- ---------------------------------------------------------------------------
99 #if __GLASGOW_HASKELL__ >= 504
100 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
101 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
102 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
103 foreign import ccall unsafe "insertStableSymbol" c_insertStableSymbol
104 :: CString -> CString -> Ptr a -> IO ()
105 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
106 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
107 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
108 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int
110 foreign import "addDLL" unsafe c_addDLL :: CString -> IO CString
111 foreign import "initLinker" unsafe initLinker :: IO ()
112 foreign import "insertSymbol" unsafe c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
113 foreign import "insertStableSymbol" unsafe c_insertStableSymbol
114 :: CString -> CString -> Ptr a -> IO ()
115 foreign import "lookupSymbol" unsafe c_lookupSymbol :: CString -> IO (Ptr a)
116 foreign import "loadObj" unsafe c_loadObj :: CString -> IO Int
117 foreign import "unloadObj" unsafe c_unloadObj :: CString -> IO Int
118 foreign import "resolveObjs" unsafe c_resolveObjs :: IO Int