[project @ 2002-05-01 17:12:24 by sof]
[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   )  where
16
17 import Monad            ( when )
18
19 import Foreign.C
20 import Foreign          ( Ptr, nullPtr )
21 import Panic            ( panic )
22 import DriverUtil       ( prefixUnderscore )
23
24 -- ---------------------------------------------------------------------------
25 -- RTS Linker Interface
26 -- ---------------------------------------------------------------------------
27
28 lookupSymbol :: String -> IO (Maybe (Ptr a))
29 lookupSymbol str_in = do
30    let str = prefixUnderscore str_in
31    withCString str $ \c_str -> do
32      addr <- c_lookupSymbol c_str
33      if addr == nullPtr
34         then return Nothing
35         else return (Just addr)
36
37 loadObj :: String -> IO ()
38 loadObj str =
39    withCString str $ \c_str -> do
40      r <- c_loadObj c_str
41      when (r == 0) (panic "loadObj: failed")
42
43 unloadObj :: String -> IO ()
44 unloadObj str =
45    withCString str $ \c_str -> do
46      r <- c_unloadObj c_str
47      when (r == 0) (panic "unloadObj: failed")
48
49 resolveObjs :: IO Bool
50 resolveObjs = do
51    r <- c_resolveObjs
52    return (r /= 0)  -- returns True <=> success
53
54 -- ---------------------------------------------------------------------------
55 -- Foreign declaractions to RTS entry points which does the real work;
56 -- ---------------------------------------------------------------------------
57
58 foreign import "initLinker" unsafe
59    initLinker :: IO ()
60
61 foreign import "lookupSymbol" unsafe
62    c_lookupSymbol :: CString -> IO (Ptr a)
63
64 foreign import "loadObj" unsafe
65    c_loadObj :: CString -> IO Int
66
67 foreign import "unloadObj" unsafe
68    c_unloadObj :: CString -> IO Int
69
70 foreign import "resolveObjs" unsafe
71    c_resolveObjs :: IO Int
72
73 \end{code}