Comment out deeply suspicious (and unused) function insertStableSymbol
[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 -- Suspicious; see defn
21 --    insertStableSymbol,   -- :: String -> String -> a -> IO ()
22    lookupSymbol,         -- :: String -> IO (Maybe (Ptr a))
23    resolveObjs           -- :: IO SuccessFlag
24   )  where
25
26 import Panic            ( panic )
27 import BasicTypes       ( SuccessFlag, successIf )
28 import Config           ( cLeadingUnderscore )
29 import Outputable
30
31 import Control.Monad    ( when )
32 import Foreign.C
33 import Foreign          ( nullPtr )
34 import GHC.Exts         ( Ptr(..), unsafeCoerce# )
35
36 -- ---------------------------------------------------------------------------
37 -- RTS Linker Interface
38 -- ---------------------------------------------------------------------------
39
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
46
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))
54 -}
55
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
61      if addr == nullPtr
62         then return Nothing
63         else return (Just addr)
64
65 prefixUnderscore :: String -> String
66 prefixUnderscore
67  | cLeadingUnderscore == "YES" = ('_':)
68  | otherwise                   = id
69
70 loadDLL :: String -> IO (Maybe String)
71 -- Nothing      => success
72 -- Just err_msg => failure
73 loadDLL str = do
74   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
75   if maybe_errmsg == nullPtr
76         then return Nothing
77         else do str <- peekCString maybe_errmsg
78                 return (Just str)
79
80 loadObj :: String -> IO ()
81 loadObj str = do
82    withCString str $ \c_str -> do
83      r <- c_loadObj c_str
84      when (r == 0) (panic "loadObj: failed")
85
86 unloadObj :: String -> IO ()
87 unloadObj str =
88    withCString str $ \c_str -> do
89      r <- c_unloadObj c_str
90      when (r == 0) (panic "unloadObj: failed")
91
92 resolveObjs :: IO SuccessFlag
93 resolveObjs = do
94    r <- c_resolveObjs
95    return (successIf (r /= 0))
96
97 -- ---------------------------------------------------------------------------
98 -- Foreign declaractions to RTS entry points which does the real work;
99 -- ---------------------------------------------------------------------------
100
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
111
112 \end{code}