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" #-}
14 {-# OPTIONS_GHC -w #-}
15 -- The above warning supression flag is a temporary kludge.
16 -- While working on this module you are encouraged to remove it and fix
17 -- any warnings in the module. See
18 -- http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
22 initObjLinker, -- :: IO ()
23 loadDLL, -- :: String -> IO (Maybe String)
24 loadObj, -- :: String -> IO ()
25 unloadObj, -- :: String -> IO ()
26 insertSymbol, -- :: String -> String -> Ptr a -> IO ()
27 lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
28 resolveObjs -- :: IO SuccessFlag
31 import Panic ( panic )
32 import BasicTypes ( SuccessFlag, successIf )
33 import Config ( cLeadingUnderscore )
36 import Control.Monad ( when )
38 import Foreign ( nullPtr )
39 import GHC.Exts ( Ptr(..), unsafeCoerce# )
43 -- ---------------------------------------------------------------------------
44 -- RTS Linker Interface
45 -- ---------------------------------------------------------------------------
47 insertSymbol :: String -> String -> Ptr a -> IO ()
48 insertSymbol obj_name key symbol
49 = let str = prefixUnderscore key
50 in withCString obj_name $ \c_obj_name ->
51 withCString str $ \c_str ->
52 c_insertSymbol c_obj_name c_str 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 declarations to RTS entry points which does the real work;
97 -- ---------------------------------------------------------------------------
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 "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
103 foreign import ccall unsafe "loadObj" c_loadObj :: CString -> IO Int
104 foreign import ccall unsafe "unloadObj" c_unloadObj :: CString -> IO Int
105 foreign import ccall unsafe "resolveObjs" c_resolveObjs :: IO Int