[project @ 2001-08-13 15:43:36 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 -- 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 Bool
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    return (r /= 0)  -- returns True <=> success
57
58 addDLL path lib = do
59    maybe_errmsg <- c_addDLL (packString path) (packString lib)
60    return maybe_errmsg
61
62 type PackedString = ByteArray Int
63
64 foreign import "lookupSymbol" unsafe
65    c_lookupSymbol :: PackedString -> IO (Ptr a)
66
67 foreign import "loadObj" unsafe
68    c_loadObj :: PackedString -> IO Int
69
70 foreign import "unloadObj" unsafe
71    c_unloadObj :: PackedString -> IO Int
72
73 foreign import "resolveObjs" unsafe
74    c_resolveObjs :: IO Int
75
76 foreign import "initLinker" unsafe
77    initLinker :: IO ()
78
79 foreign import "addDLL" unsafe 
80    c_addDLL :: PackedString -> PackedString -> IO (Ptr CChar)
81 \end{code}