Constructor names in info tables
[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    lookupSymbol,         -- :: String -> IO (Maybe (Ptr a))
21    resolveObjs           -- :: IO SuccessFlag
22   )  where
23
24 import Panic            ( panic )
25 import BasicTypes       ( SuccessFlag, successIf )
26 import Config           ( cLeadingUnderscore )
27 import Outputable
28
29 import Control.Monad    ( when )
30 import Foreign.C
31 import Foreign          ( nullPtr )
32 import GHC.Exts         ( Ptr(..), unsafeCoerce# )
33
34
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 lookupSymbol :: String -> IO (Maybe (Ptr a))
48 lookupSymbol str_in = do
49    let str = prefixUnderscore str_in
50    withCString str $ \c_str -> do
51      addr <- c_lookupSymbol c_str
52      if addr == nullPtr
53         then return Nothing
54         else return (Just addr)
55
56 prefixUnderscore :: String -> String
57 prefixUnderscore
58  | cLeadingUnderscore == "YES" = ('_':)
59  | otherwise                   = id
60
61 loadDLL :: String -> IO (Maybe String)
62 -- Nothing      => success
63 -- Just err_msg => failure
64 loadDLL str = do
65   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
66   if maybe_errmsg == nullPtr
67         then return Nothing
68         else do str <- peekCString maybe_errmsg
69                 return (Just str)
70
71 loadObj :: String -> IO ()
72 loadObj str = do
73    withCString str $ \c_str -> do
74      r <- c_loadObj c_str
75      when (r == 0) (panic "loadObj: failed")
76
77 unloadObj :: String -> IO ()
78 unloadObj str =
79    withCString str $ \c_str -> do
80      r <- c_unloadObj c_str
81      when (r == 0) (panic "unloadObj: failed")
82
83 resolveObjs :: IO SuccessFlag
84 resolveObjs = do
85    r <- c_resolveObjs
86    return (successIf (r /= 0))
87
88 -- ---------------------------------------------------------------------------
89 -- Foreign declaractions to RTS entry points which does the real work;
90 -- ---------------------------------------------------------------------------
91
92 foreign import ccall unsafe "addDLL"       c_addDLL :: CString -> IO CString
93 foreign import ccall unsafe "initLinker"   initObjLinker :: IO ()
94 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
95 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
96 foreign import ccall unsafe "loadObj"      c_loadObj :: CString -> IO Int
97 foreign import ccall unsafe "unloadObj"    c_unloadObj :: CString -> IO Int
98 foreign import ccall unsafe "resolveObjs"  c_resolveObjs :: IO Int
99 \end{code}