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 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
21 resolveObjs, -- :: IO SuccessFlag
22 lookupDataCon -- :: Ptr a -> IO (Maybe String)
25 import ByteCodeItbls ( StgInfoTable )
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 import Constants ( wORD_SIZE )
37 import Foreign ( plusPtr )
40 -- ---------------------------------------------------------------------------
41 -- RTS Linker Interface
42 -- ---------------------------------------------------------------------------
44 insertSymbol :: String -> String -> Ptr a -> IO ()
45 insertSymbol obj_name key symbol
46 = let str = prefixUnderscore key
47 in withCString obj_name $ \c_obj_name ->
48 withCString str $ \c_str ->
49 c_insertSymbol c_obj_name c_str symbol
51 lookupSymbol :: String -> IO (Maybe (Ptr a))
52 lookupSymbol str_in = do
53 let str = prefixUnderscore str_in
54 withCString str $ \c_str -> do
55 addr <- c_lookupSymbol c_str
58 else return (Just addr)
60 -- | Expects a Ptr to an info table, not to a closure
61 lookupDataCon :: Ptr StgInfoTable -> IO (Maybe String)
62 lookupDataCon ptr = do
63 name <- c_lookupDataCon (ptr `plusPtr` (wORD_SIZE*2))
66 else peekCString name >>= return . Just
68 prefixUnderscore :: String -> String
70 | cLeadingUnderscore == "YES" = ('_':)
73 loadDLL :: String -> IO (Maybe String)
75 -- Just err_msg => failure
77 maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
78 if maybe_errmsg == nullPtr
80 else do str <- peekCString maybe_errmsg
83 loadObj :: String -> IO ()
85 withCString str $ \c_str -> do
87 when (r == 0) (panic "loadObj: failed")
89 unloadObj :: String -> IO ()
91 withCString str $ \c_str -> do
92 r <- c_unloadObj c_str
93 when (r == 0) (panic "unloadObj: failed")
95 resolveObjs :: IO SuccessFlag
98 return (successIf (r /= 0))
100 -- ---------------------------------------------------------------------------
101 -- Foreign declaractions to RTS entry points which does the real work;
102 -- ---------------------------------------------------------------------------
104 foreign import ccall unsafe "addDLL" c_addDLL :: CString -> IO CString
105 foreign import ccall unsafe "initLinker" initObjLinker :: IO ()
106 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: 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
111 foreign import ccall unsafe "lookupDataCon" c_lookupDataCon :: Ptr a -> IO CString