[project @ 2000-10-06 15:48:30 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 module Linker ( 
9 #ifdef GHCI
10    loadObj,      -- :: String -> IO ()
11    unloadObj,    -- :: String -> IO ()
12    lookupSymbol, -- :: String -> IO (Maybe Addr)
13    resolveObjs,  -- :: IO ()
14    linkPrelude -- tmp
15 #endif
16   )  where
17
18 import IO
19 import Exception
20 import Addr
21 import PrelByteArr
22 import PrelPack (packString)
23
24 #ifdef GHCI
25 linkPrelude = do
26   hPutStr stderr "Loading HSstd_cbits.o..."
27   loadObj "/home/simonmar/builds/i386-unknown-linux-boot/ghc/lib/std/cbits/HSstd_cbits.o"
28   hPutStr stderr "done.\n"
29   hPutStr stderr "Resolving..."
30   resolveObjs
31   hPutStr stderr "done.\n"
32   hPutStr stderr "Loading HSstd.o..."
33   loadObj "/home/simonmar/builds/i386-unknown-linux-boot/ghc/lib/std/HSstd.o"
34   hPutStr stderr "done.\n"
35   hPutStr stderr "Resolving..."
36   resolveObjs
37   hPutStr stderr "done.\n"
38 {-
39   hPutStr stderr "Unloading HSstd.o..."
40   unloadObj "/home/simonmar/builds/i386-unknown-linux-boot/ghc/lib/std/HSstd.o"
41   hPutStr stderr "done.\n"
42   unloadObj "/home/simonmar/builds/i386-unknown-linux-boot/ghc/lib/std/cbits/HSstd_cbits.o"
43   hPutStr stderr "done.\n"
44 -}
45
46 -- ---------------------------------------------------------------------------
47 -- RTS Linker Interface
48 -- ---------------------------------------------------------------------------
49
50 lookupSymbol str = do
51    addr <- c_lookupSymbol (packString str)
52    if addr == nullAddr
53         then return Nothing
54         else return (Just addr)
55
56 loadObj str = do
57    r <- c_loadObj (packString str)
58    if (r == 0)
59         then error "loadObj: failed"
60         else return ()
61
62 unloadObj str = do
63    r <- c_unloadObj (packString str)
64    if (r == 0)
65         then error "unloadObj: failed"
66         else return ()
67
68 resolveObjs = do
69    r <- c_resolveObjs
70    if (r == 0)
71         then error "resolveObjs: failed"
72         else return ()
73
74
75 type PackedString = ByteArray Int
76
77 foreign import "lookupSymbol" unsafe
78    c_lookupSymbol :: PackedString -> IO Addr
79
80 foreign import "loadObj" unsafe
81    c_loadObj :: PackedString -> IO Int
82
83 foreign import "unloadObj" unsafe
84    c_unloadObj :: PackedString -> IO Int
85
86 foreign import "resolveObjs" unsafe
87    c_resolveObjs :: IO Int
88
89 #endif /* GHCI */
90 \end{code}