238d0094f054c30896233d0c76e20d14678f9497
[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 CTypes           ( CChar )
19 import Foreign          ( Ptr, nullPtr )
20 import PrelByteArr
21 import PrelPack         (packString)
22 import Panic            ( panic )
23 import DriverUtil       ( prefixUnderscore )
24
25 -- ---------------------------------------------------------------------------
26 -- RTS Linker Interface
27 -- ---------------------------------------------------------------------------
28
29 lookupSymbol str_in = do
30    let str = prefixUnderscore str_in
31    addr <- c_lookupSymbol (packString str)
32    if addr == nullPtr
33         then return Nothing
34         else return (Just addr)
35
36 loadObj str = do
37    r <- c_loadObj (packString str)
38    if (r == 0)
39         then panic "loadObj: failed"
40         else return ()
41
42 unloadObj str = do
43    r <- c_unloadObj (packString str)
44    if (r == 0)
45         then panic "unloadObj: failed"
46         else return ()
47
48 resolveObjs = do
49    r <- c_resolveObjs
50    return (r /= 0)  -- returns True <=> success
51
52 addDLL path lib = do
53    maybe_errmsg <- c_addDLL (packString path) (packString lib)
54    return maybe_errmsg
55
56 type PackedString = ByteArray Int
57
58 foreign import "lookupSymbol" unsafe
59    c_lookupSymbol :: PackedString -> IO (Ptr a)
60
61 foreign import "loadObj" unsafe
62    c_loadObj :: PackedString -> IO Int
63
64 foreign import "unloadObj" unsafe
65    c_unloadObj :: PackedString -> IO Int
66
67 foreign import "resolveObjs" unsafe
68    c_resolveObjs :: IO Int
69
70 foreign import "initLinker" unsafe
71    initLinker :: IO ()
72
73 foreign import "addDLL" unsafe 
74    c_addDLL :: PackedString -> PackedString -> IO (Ptr CChar)
75 \end{code}