[project @ 2001-05-15 15:01:48 by sewardj]
[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 -- so that we can see defn of LEADING_UNDERSCORE
10 #include "../includes/config.h"
11
12 module Linker ( 
13    initLinker,   -- :: IO ()
14    loadObj,      -- :: String -> IO ()
15    unloadObj,    -- :: String -> IO ()
16    lookupSymbol, -- :: String -> IO (Maybe (Ptr a))
17    resolveObjs,  -- :: IO ()
18    addDLL        -- :: String -> IO (Ptr CChar)
19   )  where
20
21 import CTypes           ( CChar )
22 import Foreign          ( Ptr, nullPtr )
23 import PrelByteArr
24 import PrelPack         (packString)
25 import Panic            ( panic )
26
27 -- ---------------------------------------------------------------------------
28 -- RTS Linker Interface
29 -- ---------------------------------------------------------------------------
30
31 lookupSymbol str_in = do
32 #  ifdef LEADING_UNDERSCORE
33    let str = '_':str_in
34 #  else
35    let str = str_in
36 #  endif
37    addr <- c_lookupSymbol (packString str)
38    if addr == nullPtr
39         then return Nothing
40         else return (Just addr)
41
42 loadObj str = do
43    r <- c_loadObj (packString str)
44    if (r == 0)
45         then panic "loadObj: failed"
46         else return ()
47
48 unloadObj str = do
49    r <- c_unloadObj (packString str)
50    if (r == 0)
51         then panic "unloadObj: failed"
52         else return ()
53
54 resolveObjs = do
55    r <- c_resolveObjs
56    if (r == 0)
57         then panic "resolveObjs: failed"
58         else return ()
59
60 addDLL str = do
61    maybe_errmsg <- c_addDLL (packString str)
62    return maybe_errmsg
63
64 type PackedString = ByteArray Int
65
66 foreign import "lookupSymbol" unsafe
67    c_lookupSymbol :: PackedString -> IO (Ptr a)
68
69 foreign import "loadObj" unsafe
70    c_loadObj :: PackedString -> IO Int
71
72 foreign import "unloadObj" unsafe
73    c_unloadObj :: PackedString -> IO Int
74
75 foreign import "resolveObjs" unsafe
76    c_resolveObjs :: IO Int
77
78 foreign import "initLinker" unsafe
79    initLinker :: IO ()
80
81 foreign import "addDLL" unsafe 
82    c_addDLL :: PackedString -> IO (Ptr CChar)
83
84 \end{code}