[project @ 2005-03-18 13:37:27 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / ObjLink.lhs
1 %
2 % (c) The University of Glasgow, 2000
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    lookupSymbol,         -- :: String -> IO (Maybe (Ptr a))
20    resolveObjs           -- :: IO SuccessFlag
21   )  where
22
23 import Monad            ( when )
24
25 import Foreign.C
26 import Foreign          ( Ptr, nullPtr )
27 import Panic            ( panic )
28 import BasicTypes       ( SuccessFlag, successIf )
29 import Outputable
30
31 -- ---------------------------------------------------------------------------
32 -- RTS Linker Interface
33 -- ---------------------------------------------------------------------------
34
35 lookupSymbol :: String -> IO (Maybe (Ptr a))
36 lookupSymbol str_in = do
37    let str = prefixUnderscore str_in
38    withCString str $ \c_str -> do
39      addr <- c_lookupSymbol c_str
40      if addr == nullPtr
41         then return Nothing
42         else return (Just addr)
43
44 prefixUnderscore :: String -> String
45 prefixUnderscore
46  | cLeadingUnderscore == "YES" = ('_':)
47  | otherwise                   = id
48
49 loadDLL :: String -> IO (Maybe String)
50 -- Nothing      => success
51 -- Just err_msg => failure
52 loadDLL str = do
53   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
54   if maybe_errmsg == nullPtr
55         then return Nothing
56         else do str <- peekCString maybe_errmsg
57                 return (Just str)
58
59 loadObj :: String -> IO ()
60 loadObj str = do
61    withCString str $ \c_str -> do
62      r <- c_loadObj c_str
63      when (r == 0) (panic "loadObj: failed")
64
65 unloadObj :: String -> IO ()
66 unloadObj str =
67    withCString str $ \c_str -> do
68      r <- c_unloadObj c_str
69      when (r == 0) (panic "unloadObj: failed")
70
71 resolveObjs :: IO SuccessFlag
72 resolveObjs = do
73    r <- c_resolveObjs
74    return (successIf (r /= 0))
75
76 -- ---------------------------------------------------------------------------
77 -- Foreign declaractions to RTS entry points which does the real work;
78 -- ---------------------------------------------------------------------------
79
80 #if __GLASGOW_HASKELL__ >= 504
81 foreign import ccall unsafe "addDLL"       c_addDLL :: CString -> IO CString
82 foreign import ccall unsafe "initLinker"   initObjLinker :: IO ()
83 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
84 foreign import ccall unsafe "loadObj"      c_loadObj :: CString -> IO Int
85 foreign import ccall unsafe "unloadObj"    c_unloadObj :: CString -> IO Int
86 foreign import ccall unsafe "resolveObjs"  c_resolveObjs :: IO Int
87 #else
88 foreign import "addDLL"       unsafe    c_addDLL :: CString -> IO CString
89 foreign import "initLinker"   unsafe    initLinker :: IO ()
90 foreign import "lookupSymbol" unsafe    c_lookupSymbol :: CString -> IO (Ptr a)
91 foreign import "loadObj"      unsafe    c_loadObj :: CString -> IO Int
92 foreign import "unloadObj"    unsafe    c_unloadObj :: CString -> IO Int
93 foreign import "resolveObjs"  unsafe    c_resolveObjs :: IO Int
94 #endif
95
96 \end{code}