[project @ 2002-02-12 15:17:13 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / Linker.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[Linker]{The In-Memory Object File Linker}
5
6 \begin{code}
7 {-# OPTIONS -#include "Linker.h" #-}
8
9 module Linker ( 
10    initLinker,   -- :: IO ()
11    loadObj,      -- :: String -> IO ()
12    unloadObj,    -- :: String -> IO ()
13    lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
14    resolveObjs,  -- :: IO Bool
15    addDLL        -- :: String -> IO (Ptr CChar)
16   )  where
17
18 import Monad            ( when )
19
20 import Foreign.C
21 import Foreign          ( Ptr, nullPtr )
22 import Panic            ( panic )
23 import DriverUtil       ( prefixUnderscore )
24
25 -- ---------------------------------------------------------------------------
26 -- RTS Linker Interface
27 -- ---------------------------------------------------------------------------
28
29 lookupSymbol :: String -> IO (Maybe (Ptr a))
30 lookupSymbol str_in = do
31    let str = prefixUnderscore str_in
32    withCString str $ \c_str -> do
33      addr <- c_lookupSymbol c_str
34      if addr == nullPtr
35         then return Nothing
36         else return (Just addr)
37
38 loadObj :: String -> IO ()
39 loadObj str =
40    withCString str $ \c_str -> do
41      r <- c_loadObj c_str
42      when (r == 0) (panic "loadObj: failed")
43
44 unloadObj :: String -> IO ()
45 unloadObj str =
46    withCString str $ \c_str -> do
47      r <- c_unloadObj c_str
48      when (r == 0) (panic "unloadObj: failed")
49
50 resolveObjs :: IO Bool
51 resolveObjs = do
52    r <- c_resolveObjs
53    return (r /= 0)  -- returns True <=> success
54
55 addDLL :: String -> String -> IO (Ptr CChar)
56 addDLL path lib = do
57   withCString path $ \c_path -> do
58   withCString lib $ \c_lib -> do
59     maybe_errmsg <- c_addDLL c_path c_lib
60     return maybe_errmsg
61
62 -- ---------------------------------------------------------------------------
63 -- Foreign declaractions to RTS entry points which does the real work;
64 -- ---------------------------------------------------------------------------
65
66 foreign import "initLinker" unsafe
67    initLinker :: IO ()
68
69 foreign import "lookupSymbol" unsafe
70    c_lookupSymbol :: CString -> IO (Ptr a)
71
72 foreign import "loadObj" unsafe
73    c_loadObj :: CString -> IO Int
74
75 foreign import "unloadObj" unsafe
76    c_unloadObj :: CString -> IO Int
77
78 foreign import "resolveObjs" unsafe
79    c_resolveObjs :: IO Int
80
81 foreign import "addDLL" unsafe 
82    c_addDLL :: CString -> CString -> IO (Ptr CChar)
83 \end{code}