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 -- Suspicious; see defn
21 -- insertStableSymbol, -- :: String -> String -> a -> IO ()
22 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
23 resolveObjs -- :: IO SuccessFlag
26 import Panic ( panic )
27 import BasicTypes ( SuccessFlag, successIf )
28 import Config ( cLeadingUnderscore )
31 import Control.Monad ( when )
33 import Foreign ( nullPtr )
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 {- Deeply suspicious use of unsafeCoerce#; should use makeStablePtr#
48 insertStableSymbol :: String -> String -> a -> IO ()
49 insertStableSymbol obj_name key symbol
50 = let str = prefixUnderscore key
51 in withCString obj_name $ \c_obj_name ->
52 withCString str $ \c_str ->
53 c_insertStableSymbol c_obj_name c_str (Ptr (unsafeCoerce# symbol))
56 lookupSymbol :: String -> IO (Maybe (Ptr a))
57 lookupSymbol str_in = do
58 let str = prefixUnderscore str_in
59 withCString str $ \c_str -> do
60 addr <- c_lookupSymbol c_str
63 else return (Just addr)
65 prefixUnderscore :: String -> String
67 | cLeadingUnderscore == "YES" = ('_':)
70 loadDLL :: String -> IO (Maybe String)
72 -- Just err_msg => failure
74 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
75 if maybe_errmsg == nullPtr
77 else do str <- peekCString maybe_errmsg
80 loadObj :: String -> IO ()
82 withCString str $ \c_str -> do
84 when (r == 0) (panic "loadObj: failed")
86 unloadObj :: String -> IO ()
88 withCString str $ \c_str -> do
89 r <- c_unloadObj c_str
90 when (r == 0) (panic "unloadObj: failed")
92 resolveObjs :: IO SuccessFlag
95 return (successIf (r /= 0))
97 -- ---------------------------------------------------------------------------
98 -- Foreign declaractions to RTS entry points which does the real work;
99 -- ---------------------------------------------------------------------------
101 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
102 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
103 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
104 -- Suspicious: should take a stable pointer
105 -- foreign import ccall unsafe "insertStableSymbol" c_insertStableSymbol
106 -- :: CString -> CString -> Ptr a -> IO ()
107 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
108 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
109 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
110 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int