[project @ 2003-05-30 10:13:05 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 DriverUtil       ( prefixUnderscore )
29 import BasicTypes       ( SuccessFlag, successIf )
30 import Outputable
31
32 -- ---------------------------------------------------------------------------
33 -- RTS Linker Interface
34 -- ---------------------------------------------------------------------------
35
36 lookupSymbol :: String -> IO (Maybe (Ptr a))
37 lookupSymbol str_in = do
38    let str = prefixUnderscore str_in
39    withCString str $ \c_str -> do
40      addr <- c_lookupSymbol c_str
41      if addr == nullPtr
42         then return Nothing
43         else return (Just addr)
44
45 loadDLL :: String -> IO (Maybe String)
46 -- Nothing      => success
47 -- Just err_msg => failure
48 loadDLL str = do
49   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
50   if maybe_errmsg == nullPtr
51         then return Nothing
52         else do str <- peekCString maybe_errmsg
53                 return (Just str)
54
55 loadObj :: String -> IO ()
56 loadObj str = do
57    withCString str $ \c_str -> do
58      r <- c_loadObj c_str
59      when (r == 0) (panic "loadObj: failed")
60
61 unloadObj :: String -> IO ()
62 unloadObj str =
63    withCString str $ \c_str -> do
64      r <- c_unloadObj c_str
65      when (r == 0) (panic "unloadObj: failed")
66
67 resolveObjs :: IO SuccessFlag
68 resolveObjs = do
69    r <- c_resolveObjs
70    return (successIf (r /= 0))
71
72 -- ---------------------------------------------------------------------------
73 -- Foreign declaractions to RTS entry points which does the real work;
74 -- ---------------------------------------------------------------------------
75
76 #if __GLASGOW_HASKELL__ >= 504
77 foreign import ccall unsafe "addDLL"       c_addDLL :: CString -> IO CString
78 foreign import ccall unsafe "initLinker"   initObjLinker :: IO ()
79 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
80 foreign import ccall unsafe "loadObj"      c_loadObj :: CString -> IO Int
81 foreign import ccall unsafe "unloadObj"    c_unloadObj :: CString -> IO Int
82 foreign import ccall unsafe "resolveObjs"  c_resolveObjs :: IO Int
83 #else
84 foreign import "addDLL"       unsafe    c_addDLL :: CString -> IO CString
85 foreign import "initLinker"   unsafe    initLinker :: IO ()
86 foreign import "lookupSymbol" unsafe    c_lookupSymbol :: CString -> IO (Ptr a)
87 foreign import "loadObj"      unsafe    c_loadObj :: CString -> IO Int
88 foreign import "unloadObj"    unsafe    c_unloadObj :: CString -> IO Int
89 foreign import "resolveObjs"  unsafe    c_resolveObjs :: IO Int
90 #endif
91
92 \end{code}