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