d5e0f7bd926bbe97722cfc62e90531e2b9ed2ac1
[ghc-hetmet.git] / compiler / ghci / ObjLink.lhs
1 %
2 % (c) The University of Glasgow, 2000-2006
3 %
4
5 -- ---------------------------------------------------------------------------
6 --      The dynamic linker for object code (.o .so .dll files)
7 -- ---------------------------------------------------------------------------
8
9 Primarily, this module consists of an interface to the C-land dynamic linker.
10
11 \begin{code}
12 {-# OPTIONS -#include "Linker.h" #-}
13
14 module ObjLink ( 
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
23   )  where
24
25 import Panic            ( panic )
26 import BasicTypes       ( SuccessFlag, successIf )
27 import Config           ( cLeadingUnderscore )
28 import Outputable
29
30 import Control.Monad    ( when )
31 import Foreign.C
32 import Foreign          ( nullPtr )
33 import GHC.Exts         ( Ptr(..), unsafeCoerce# )
34
35 -- ---------------------------------------------------------------------------
36 -- RTS Linker Interface
37 -- ---------------------------------------------------------------------------
38
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
45
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))
52
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
58      if addr == nullPtr
59         then return Nothing
60         else return (Just addr)
61
62 prefixUnderscore :: String -> String
63 prefixUnderscore
64  | cLeadingUnderscore == "YES" = ('_':)
65  | otherwise                   = id
66
67 loadDLL :: String -> IO (Maybe String)
68 -- Nothing      => success
69 -- Just err_msg => failure
70 loadDLL str = do
71   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
72   if maybe_errmsg == nullPtr
73         then return Nothing
74         else do str <- peekCString maybe_errmsg
75                 return (Just str)
76
77 loadObj :: String -> IO ()
78 loadObj str = do
79    withCString str $ \c_str -> do
80      r <- c_loadObj c_str
81      when (r == 0) (panic "loadObj: failed")
82
83 unloadObj :: String -> IO ()
84 unloadObj str =
85    withCString str $ \c_str -> do
86      r <- c_unloadObj c_str
87      when (r == 0) (panic "unloadObj: failed")
88
89 resolveObjs :: IO SuccessFlag
90 resolveObjs = do
91    r <- c_resolveObjs
92    return (successIf (r /= 0))
93
94 -- ---------------------------------------------------------------------------
95 -- Foreign declaractions to RTS entry points which does the real work;
96 -- ---------------------------------------------------------------------------
97
98 foreign import ccall unsafe "addDLL"       c_addDLL :: CString -> IO CString
99 foreign import ccall unsafe "initLinker"   initObjLinker :: IO ()
100 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
101 foreign import ccall unsafe "insertStableSymbol" c_insertStableSymbol
102     :: CString -> CString -> Ptr a -> IO ()
103 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
104 foreign import ccall unsafe "loadObj"      c_loadObj :: CString -> IO Int
105 foreign import ccall unsafe "unloadObj"    c_unloadObj :: CString -> IO Int
106 foreign import ccall unsafe "resolveObjs"  c_resolveObjs :: IO Int
107
108 \end{code}