45cbdc095fd468851ed0b9e272e10fa35c69b6b4
[ghc-hetmet.git] / compiler / ghci / Linker.lhs
1 %
2 % (c) The University of Glasgow 2005-2006
3 %
4 \begin{code}
5 -- | The dynamic linker for GHCi.
6 --
7 -- This module deals with the top-level issues of dynamic linking,
8 -- calling the object-code linker and the byte-code linker where
9 -- necessary.
10
11 {-# OPTIONS -fno-cse #-}
12 -- -fno-cse is needed for GLOBAL_VAR's to behave properly
13
14 module Linker ( HValue, getHValue, showLinkerState,
15                 linkExpr, unload, withExtendedLinkEnv,
16                 extendLinkEnv, deleteFromLinkEnv,
17                 extendLoadedPkgs, 
18                 linkPackages,initDynLinker,
19                 dataConInfoPtrToName
20         ) where
21
22 #include "HsVersions.h"
23
24 import LoadIface
25 import ObjLink
26 import ByteCodeLink
27 import ByteCodeItbls
28 import ByteCodeAsm
29 import CgInfoTbls
30 import SMRep
31 import IfaceEnv
32 import TcRnMonad
33 import Packages
34 import DriverPhases
35 import Finder
36 import HscTypes
37 import Name
38 import NameEnv
39 import NameSet
40 import qualified OccName
41 import UniqFM
42 import Module
43 import ListSetOps
44 import DynFlags
45 import BasicTypes
46 import Outputable
47 import Panic
48 import Util
49 import StaticFlags
50 import ErrUtils
51 import SrcLoc
52 import qualified Maybes
53 import UniqSet
54 import FiniteMap
55 import Constants
56 import FastString
57 import Config           ( cProjectVersion )
58
59 -- Standard libraries
60 import Control.Monad
61
62 import Data.Char
63 import Data.IORef
64 import Data.List
65 import Foreign
66 import Control.Concurrent.MVar
67
68 import System.FilePath
69 import System.IO
70 import System.Directory
71
72 import Distribution.Package hiding (depends, PackageId)
73
74 import Exception
75 \end{code}
76
77
78 %************************************************************************
79 %*                                                                      *
80                         The Linker's state
81 %*                                                                      *
82 %************************************************************************
83
84 The persistent linker state *must* match the actual state of the 
85 C dynamic linker at all times, so we keep it in a private global variable.
86
87
88 The PersistentLinkerState maps Names to actual closures (for
89 interpreted code only), for use during linking.
90
91 \begin{code}
92 GLOBAL_MVAR(v_PersistentLinkerState, panic "Dynamic linker not initialised", PersistentLinkerState)
93 GLOBAL_VAR(v_InitLinkerDone, False, Bool)       -- Set True when dynamic linker is initialised
94
95 data PersistentLinkerState
96    = PersistentLinkerState {
97
98         -- Current global mapping from Names to their true values
99         closure_env :: ClosureEnv,
100
101         -- The current global mapping from RdrNames of DataCons to
102         -- info table addresses.
103         -- When a new Unlinked is linked into the running image, or an existing
104         -- module in the image is replaced, the itbl_env must be updated
105         -- appropriately.
106         itbl_env    :: !ItblEnv,
107
108         -- The currently loaded interpreted modules (home package)
109         bcos_loaded :: ![Linkable],
110
111         -- And the currently-loaded compiled modules (home package)
112         objs_loaded :: ![Linkable],
113
114         -- The currently-loaded packages; always object code
115         -- Held, as usual, in dependency order; though I am not sure if
116         -- that is really important
117         pkgs_loaded :: ![PackageId]
118      }
119
120 emptyPLS :: DynFlags -> PersistentLinkerState
121 emptyPLS _ = PersistentLinkerState { 
122                         closure_env = emptyNameEnv,
123                         itbl_env    = emptyNameEnv,
124                         pkgs_loaded = init_pkgs,
125                         bcos_loaded = [],
126                         objs_loaded = [] }
127                     
128   -- Packages that don't need loading, because the compiler 
129   -- shares them with the interpreted program.
130   --
131   -- The linker's symbol table is populated with RTS symbols using an
132   -- explicit list.  See rts/Linker.c for details.
133   where init_pkgs = [rtsPackageId]
134 \end{code}
135
136 \begin{code}
137 extendLoadedPkgs :: [PackageId] -> IO ()
138 extendLoadedPkgs pkgs =
139   modifyMVar_ v_PersistentLinkerState $ \s ->
140       return s{ pkgs_loaded = pkgs ++ pkgs_loaded s }
141
142 extendLinkEnv :: [(Name,HValue)] -> IO ()
143 -- Automatically discards shadowed bindings
144 extendLinkEnv new_bindings =
145   modifyMVar_ v_PersistentLinkerState $ \pls ->
146     let new_closure_env = extendClosureEnv (closure_env pls) new_bindings
147     in return pls{ closure_env = new_closure_env }
148
149 deleteFromLinkEnv :: [Name] -> IO ()
150 deleteFromLinkEnv to_remove =
151   modifyMVar_ v_PersistentLinkerState $ \pls ->
152     let new_closure_env = delListFromNameEnv (closure_env pls) to_remove
153     in return pls{ closure_env = new_closure_env }
154
155 -- | Given a data constructor in the heap, find its Name.
156 --   The info tables for data constructors have a field which records
157 --   the source name of the constructor as a Ptr Word8 (UTF-8 encoded
158 --   string). The format is:
159 --
160 --   > Package:Module.Name
161 --
162 --   We use this string to lookup the interpreter's internal representation of the name
163 --   using the lookupOrig.    
164 --
165 dataConInfoPtrToName :: Ptr () -> TcM (Either String Name)
166 dataConInfoPtrToName x = do 
167    theString <- liftIO $ do
168       let ptr = castPtr x :: Ptr StgInfoTable
169       conDescAddress <- getConDescAddress ptr 
170       peekArray0 0 conDescAddress  
171    let (pkg, mod, occ) = parse theString 
172        pkgFS = mkFastStringByteList pkg
173        modFS = mkFastStringByteList mod
174        occFS = mkFastStringByteList occ
175        occName = mkOccNameFS OccName.dataName occFS
176        modName = mkModule (fsToPackageId pkgFS) (mkModuleNameFS modFS) 
177    return (Left$ showSDoc$ ppr modName <> dot <> ppr occName ) 
178     `recoverM` (Right `fmap` lookupOrig modName occName)
179
180    where
181
182    {- To find the string in the constructor's info table we need to consider 
183       the layout of info tables relative to the entry code for a closure.
184
185       An info table can be next to the entry code for the closure, or it can
186       be separate. The former (faster) is used in registerised versions of ghc, 
187       and the latter (portable) is for non-registerised versions. 
188
189       The diagrams below show where the string is to be found relative to 
190       the normal info table of the closure.
191
192       1) Code next to table:
193
194          --------------
195          |            |   <- pointer to the start of the string
196          --------------
197          |            |   <- the (start of the) info table structure
198          |            |
199          |            |
200          --------------
201          | entry code | 
202          |    ....    |
203
204          In this case the pointer to the start of the string can be found in
205          the memory location _one word before_ the first entry in the normal info 
206          table.
207
208       2) Code NOT next to table:
209
210                                  --------------
211          info table structure -> |     *------------------> --------------
212                                  |            |             | entry code |
213                                  |            |             |    ....    | 
214                                  --------------
215          ptr to start of str ->  |            |   
216                                  --------------
217
218          In this case the pointer to the start of the string can be found
219          in the memory location: info_table_ptr + info_table_size
220    -}
221
222    getConDescAddress :: Ptr StgInfoTable -> IO (Ptr Word8)
223    getConDescAddress ptr
224     | ghciTablesNextToCode = do
225        offsetToString <- peek $ ptr `plusPtr` (- wORD_SIZE)
226        return $ (ptr `plusPtr` stdInfoTableSizeB) `plusPtr` (fromIntegral (offsetToString :: StgWord))
227     | otherwise =
228        peek $ intPtrToPtr $ (ptrToIntPtr ptr) + fromIntegral stdInfoTableSizeB
229
230    -- parsing names is a little bit fiddly because we have a string in the form: 
231    -- pkg:A.B.C.foo, and we want to split it into three parts: ("pkg", "A.B.C", "foo").
232    -- Thus we split at the leftmost colon and the rightmost occurrence of the dot.
233    -- It would be easier if the string was in the form pkg:A.B.C:foo, but alas
234    -- this is not the conventional way of writing Haskell names. We stick with
235    -- convention, even though it makes the parsing code more troublesome.
236    -- Warning: this code assumes that the string is well formed.
237    parse :: [Word8] -> ([Word8], [Word8], [Word8])
238    parse input 
239       = ASSERT (all (>0) (map length [pkg, mod, occ])) (pkg, mod, occ)
240       where
241       dot = fromIntegral (ord '.')
242       (pkg, rest1) = break (== fromIntegral (ord ':')) input 
243       (mod, occ) 
244          = (concat $ intersperse [dot] $ reverse modWords, occWord)
245          where
246          (modWords, occWord) = ASSERT (length rest1 > 0) (parseModOcc [] (tail rest1))
247       parseModOcc :: [[Word8]] -> [Word8] -> ([[Word8]], [Word8])
248       parseModOcc acc str
249          = case break (== dot) str of
250               (top, []) -> (acc, top)
251               (top, _:bot) -> parseModOcc (top : acc) bot
252        
253 -- | Get the 'HValue' associated with the given name.
254 --
255 -- May cause loading the module that contains the name.
256 --
257 -- Throws a 'ProgramError' if loading fails or the name cannot be found.
258 getHValue :: HscEnv -> Name -> IO HValue
259 getHValue hsc_env name = do
260   pls <- modifyMVar v_PersistentLinkerState $ \pls -> do
261            if (isExternalName name) then do
262              (pls', ok) <- linkDependencies hsc_env pls noSrcSpan [nameModule name]
263              if (failed ok) then ghcError (ProgramError "")
264                             else return (pls', pls')
265             else
266              return (pls, pls)
267   lookupName (closure_env pls) name
268         
269 linkDependencies :: HscEnv -> PersistentLinkerState
270                  -> SrcSpan -> [Module]
271                  -> IO (PersistentLinkerState, SuccessFlag)
272 linkDependencies hsc_env pls span needed_mods = do
273    let hpt = hsc_HPT hsc_env
274        dflags = hsc_dflags hsc_env
275         -- The interpreter and dynamic linker can only handle object code built
276         -- the "normal" way, i.e. no non-std ways like profiling or ticky-ticky.
277         -- So here we check the build tag: if we're building a non-standard way
278         -- then we need to find & link object files built the "normal" way.
279    maybe_normal_osuf <- checkNonStdWay dflags span
280
281         -- Find what packages and linkables are required
282    (lnks, pkgs) <- getLinkDeps hsc_env hpt pls
283                                 maybe_normal_osuf span needed_mods
284
285         -- Link the packages and modules required
286    pls1 <- linkPackages' dflags pkgs pls
287    linkModules dflags pls1 lnks
288
289
290 -- | Temporarily extend the linker state.
291
292 withExtendedLinkEnv :: (MonadIO m, ExceptionMonad m) =>
293                        [(Name,HValue)] -> m a -> m a
294 withExtendedLinkEnv new_env action
295     = gbracket (liftIO $ extendLinkEnv new_env)
296                (\_ -> reset_old_env)
297                (\_ -> action)
298     where
299         -- Remember that the linker state might be side-effected
300         -- during the execution of the IO action, and we don't want to
301         -- lose those changes (we might have linked a new module or
302         -- package), so the reset action only removes the names we
303         -- added earlier.
304           reset_old_env = liftIO $ do
305             modifyMVar_ v_PersistentLinkerState $ \pls ->
306                 let cur = closure_env pls
307                     new = delListFromNameEnv cur (map fst new_env)
308                 in return pls{ closure_env = new }
309
310 -- filterNameMap removes from the environment all entries except 
311 --      those for a given set of modules;
312 -- Note that this removes all *local* (i.e. non-isExternal) names too 
313 --      (these are the temporary bindings from the command line).
314 -- Used to filter both the ClosureEnv and ItblEnv
315
316 filterNameMap :: [Module] -> NameEnv (Name, a) -> NameEnv (Name, a)
317 filterNameMap mods env 
318    = filterNameEnv keep_elt env
319    where
320      keep_elt (n,_) = isExternalName n 
321                       && (nameModule n `elem` mods)
322 \end{code}
323
324
325 \begin{code}
326 -- | Display the persistent linker state.
327 showLinkerState :: IO ()
328 showLinkerState
329   = do pls <- readMVar v_PersistentLinkerState
330        printDump (vcat [text "----- Linker state -----",
331                         text "Pkgs:" <+> ppr (pkgs_loaded pls),
332                         text "Objs:" <+> ppr (objs_loaded pls),
333                         text "BCOs:" <+> ppr (bcos_loaded pls)])
334 \end{code}
335                         
336         
337
338
339 %************************************************************************
340 %*                                                                      *
341 \subsection{Initialisation}
342 %*                                                                      *
343 %************************************************************************
344
345 \begin{code}
346 -- | Initialise the dynamic linker.  This entails
347 --
348 --  a) Calling the C initialisation procedure,
349 --
350 --  b) Loading any packages specified on the command line,
351 --
352 --  c) Loading any packages specified on the command line, now held in the
353 --     @-l@ options in @v_Opt_l@,
354 --
355 --  d) Loading any @.o\/.dll@ files specified on the command line, now held
356 --     in @v_Ld_inputs@,
357 --
358 --  e) Loading any MacOS frameworks.
359 --
360 -- NOTE: This function is idempotent; if called more than once, it does
361 -- nothing.  This is useful in Template Haskell, where we call it before
362 -- trying to link.
363 --
364 initDynLinker :: DynFlags -> IO ()
365 initDynLinker dflags =
366   modifyMVar_ v_PersistentLinkerState $ \pls0 -> do
367     done <- readIORef v_InitLinkerDone
368     if done then return pls0
369             else do writeIORef v_InitLinkerDone True
370                     reallyInitDynLinker dflags
371
372 reallyInitDynLinker :: DynFlags -> IO PersistentLinkerState
373 reallyInitDynLinker dflags =
374     do  {  -- Initialise the linker state
375           let pls0 = emptyPLS dflags
376
377                 -- (a) initialise the C dynamic linker
378         ; initObjLinker 
379
380                 -- (b) Load packages from the command-line
381         ; pls <- linkPackages' dflags (preloadPackages (pkgState dflags)) pls0
382
383                 -- (c) Link libraries from the command-line
384         ; let optl = getOpts dflags opt_l
385         ; let minus_ls = [ lib | '-':'l':lib <- optl ]
386
387                 -- (d) Link .o files from the command-line
388         ; let lib_paths = libraryPaths dflags
389         ; cmdline_ld_inputs <- readIORef v_Ld_inputs
390
391         ; classified_ld_inputs <- mapM classifyLdInput cmdline_ld_inputs
392
393                 -- (e) Link any MacOS frameworks
394         ; let framework_paths
395                | isDarwinTarget = frameworkPaths dflags
396                | otherwise      = []
397         ; let frameworks
398                | isDarwinTarget = cmdlineFrameworks dflags
399                | otherwise      = []
400                 -- Finally do (c),(d),(e)       
401         ; let cmdline_lib_specs = [ l | Just l <- classified_ld_inputs ]
402                                ++ map DLL       minus_ls 
403                                ++ map Framework frameworks
404         ; if null cmdline_lib_specs then return pls
405                                     else do
406
407         { mapM_ (preloadLib dflags lib_paths framework_paths) cmdline_lib_specs
408         ; maybePutStr dflags "final link ... "
409         ; ok <- resolveObjs
410
411         ; if succeeded ok then maybePutStrLn dflags "done"
412           else ghcError (ProgramError "linking extra libraries/objects failed")
413
414         ; return pls
415         }}
416
417 classifyLdInput :: FilePath -> IO (Maybe LibrarySpec)
418 classifyLdInput f
419   | isObjectFilename f = return (Just (Object f))
420   | isDynLibFilename f = return (Just (DLLPath f))
421   | otherwise          = do
422         hPutStrLn stderr ("Warning: ignoring unrecognised input `" ++ f ++ "'")
423         return Nothing
424
425 preloadLib :: DynFlags -> [String] -> [String] -> LibrarySpec -> IO ()
426 preloadLib dflags lib_paths framework_paths lib_spec
427   = do maybePutStr dflags ("Loading object " ++ showLS lib_spec ++ " ... ")
428        case lib_spec of
429           Object static_ish
430              -> do b <- preload_static lib_paths static_ish
431                    maybePutStrLn dflags (if b  then "done"
432                                                 else "not found")
433          
434           DLL dll_unadorned
435              -> do maybe_errstr <- loadDynamic lib_paths dll_unadorned
436                    case maybe_errstr of
437                       Nothing -> maybePutStrLn dflags "done"
438                       Just mm -> preloadFailed mm lib_paths lib_spec
439
440           DLLPath dll_path
441              -> do maybe_errstr <- loadDLL dll_path
442                    case maybe_errstr of
443                       Nothing -> maybePutStrLn dflags "done"
444                       Just mm -> preloadFailed mm lib_paths lib_spec
445
446           Framework framework
447            | isDarwinTarget
448              -> do maybe_errstr <- loadFramework framework_paths framework
449                    case maybe_errstr of
450                       Nothing -> maybePutStrLn dflags "done"
451                       Just mm -> preloadFailed mm framework_paths lib_spec
452            | otherwise -> panic "preloadLib Framework"
453
454   where
455     preloadFailed :: String -> [String] -> LibrarySpec -> IO ()
456     preloadFailed sys_errmsg paths spec
457        = do maybePutStr dflags "failed.\n"
458             ghcError $
459               CmdLineError (
460                     "user specified .o/.so/.DLL could not be loaded ("
461                     ++ sys_errmsg ++ ")\nWhilst trying to load:  "
462                     ++ showLS spec ++ "\nAdditional directories searched:"
463                     ++ (if null paths then " (none)" else
464                         (concat (intersperse "\n" (map ("   "++) paths)))))
465     
466     -- Not interested in the paths in the static case.
467     preload_static _paths name
468        = do b <- doesFileExist name
469             if not b then return False
470                      else loadObj name >> return True
471 \end{code}
472
473
474 %************************************************************************
475 %*                                                                      *
476                 Link a byte-code expression
477 %*                                                                      *
478 %************************************************************************
479
480 \begin{code}
481 -- | Link a single expression, /including/ first linking packages and
482 -- modules that this expression depends on.
483 --
484 -- Raises an IO exception ('ProgramError') if it can't find a compiled
485 -- version of the dependents to link.
486 --
487 linkExpr :: HscEnv -> SrcSpan -> UnlinkedBCO -> IO HValue
488 linkExpr hsc_env span root_ul_bco
489   = do {  
490         -- Initialise the linker (if it's not been done already)
491      let dflags = hsc_dflags hsc_env
492    ; initDynLinker dflags
493
494         -- Take lock for the actual work.
495    ; modifyMVar v_PersistentLinkerState $ \pls0 -> do {
496
497         -- Link the packages and modules required
498    ; (pls, ok) <- linkDependencies hsc_env pls0 span needed_mods
499    ; if failed ok then
500         ghcError (ProgramError "")
501      else do {
502
503         -- Link the expression itself
504      let ie = itbl_env pls
505          ce = closure_env pls
506
507         -- Link the necessary packages and linkables
508    ; (_, (root_hval:_)) <- linkSomeBCOs False ie ce [root_ul_bco]
509    ; return (pls, root_hval)
510    }}}
511    where
512      free_names = nameSetToList (bcoFreeNames root_ul_bco)
513
514      needed_mods :: [Module]
515      needed_mods = [ nameModule n | n <- free_names, 
516                                     isExternalName n,           -- Names from other modules
517                                     not (isWiredInName n)       -- Exclude wired-in names
518                    ]                                            -- (see note below)
519         -- Exclude wired-in names because we may not have read
520         -- their interface files, so getLinkDeps will fail
521         -- All wired-in names are in the base package, which we link
522         -- by default, so we can safely ignore them here.
523  
524 dieWith :: SrcSpan -> Message -> IO a
525 dieWith span msg = ghcError (ProgramError (showSDoc (mkLocMessage span msg)))
526
527
528 checkNonStdWay :: DynFlags -> SrcSpan -> IO (Maybe String)
529 checkNonStdWay dflags srcspan = do
530   let tag = buildTag dflags
531   if null tag {-  || tag == "dyn" -} then return Nothing else do
532     -- see #3604: object files compiled for way "dyn" need to link to the
533     -- dynamic packages, so we can't load them into a statically-linked GHCi.
534     -- we have to treat "dyn" in the same way as "prof".
535     --
536     -- In the future when GHCi is dynamically linked we should be able to relax
537     -- this, but they we may have to make it possible to load either ordinary
538     -- .o files or -dynamic .o files into GHCi (currently that's not possible
539     -- because the dynamic objects contain refs to e.g. __stginit_base_Prelude_dyn
540     -- whereas we have __stginit_base_Prelude_.
541   let default_osuf = phaseInputExt StopLn
542   if objectSuf dflags == default_osuf
543         then failNonStd srcspan
544         else return (Just default_osuf)
545
546 failNonStd :: SrcSpan -> IO (Maybe String)
547 failNonStd srcspan = dieWith srcspan $
548   ptext (sLit "Dynamic linking required, but this is a non-standard build (eg. prof).") $$
549   ptext (sLit "You need to build the program twice: once the normal way, and then") $$
550   ptext (sLit "in the desired way using -osuf to set the object file suffix.")
551   
552
553 getLinkDeps :: HscEnv -> HomePackageTable
554             -> PersistentLinkerState
555             -> Maybe String                     -- the "normal" object suffix
556             -> SrcSpan                          -- for error messages
557             -> [Module]                         -- If you need these
558             -> IO ([Linkable], [PackageId])     -- ... then link these first
559 -- Fails with an IO exception if it can't find enough files
560
561 getLinkDeps hsc_env hpt pls maybe_normal_osuf span mods
562 -- Find all the packages and linkables that a set of modules depends on
563  = do {
564         -- 1.  Find the dependent home-pkg-modules/packages from each iface
565         (mods_s, pkgs_s) <- follow_deps mods emptyUniqSet emptyUniqSet;
566
567         let {
568         -- 2.  Exclude ones already linked
569         --      Main reason: avoid findModule calls in get_linkable
570             mods_needed = mods_s `minusList` linked_mods     ;
571             pkgs_needed = pkgs_s `minusList` pkgs_loaded pls ;
572
573             linked_mods = map (moduleName.linkableModule) 
574                                 (objs_loaded pls ++ bcos_loaded pls)
575         } ;
576         
577 --        putStrLn (showSDoc (ppr mods_s)) ;
578         -- 3.  For each dependent module, find its linkable
579         --     This will either be in the HPT or (in the case of one-shot
580         --     compilation) we may need to use maybe_getFileLinkable
581         lnks_needed <- mapM (get_linkable maybe_normal_osuf) mods_needed ;
582
583         return (lnks_needed, pkgs_needed) }
584   where
585     dflags = hsc_dflags hsc_env
586     this_pkg = thisPackage dflags
587
588         -- The ModIface contains the transitive closure of the module dependencies
589         -- within the current package, *except* for boot modules: if we encounter
590         -- a boot module, we have to find its real interface and discover the
591         -- dependencies of that.  Hence we need to traverse the dependency
592         -- tree recursively.  See bug #936, testcase ghci/prog007.
593     follow_deps :: [Module]             -- modules to follow
594                 -> UniqSet ModuleName         -- accum. module dependencies
595                 -> UniqSet PackageId          -- accum. package dependencies
596                 -> IO ([ModuleName], [PackageId]) -- result
597     follow_deps []     acc_mods acc_pkgs
598         = return (uniqSetToList acc_mods, uniqSetToList acc_pkgs)
599     follow_deps (mod:mods) acc_mods acc_pkgs
600         = do
601           mb_iface <- initIfaceCheck hsc_env $
602                         loadInterface msg mod (ImportByUser False)
603           iface <- case mb_iface of
604                     Maybes.Failed err      -> ghcError (ProgramError (showSDoc err))
605                     Maybes.Succeeded iface -> return iface
606
607           when (mi_boot iface) $ link_boot_mod_error mod
608
609           let
610             pkg = modulePackageId mod
611             deps  = mi_deps iface
612
613             pkg_deps = dep_pkgs deps
614             (boot_deps, mod_deps) = partitionWith is_boot (dep_mods deps)
615                     where is_boot (m,True)  = Left m
616                           is_boot (m,False) = Right m
617
618             boot_deps' = filter (not . (`elementOfUniqSet` acc_mods)) boot_deps
619             acc_mods'  = addListToUniqSet acc_mods (moduleName mod : mod_deps)
620             acc_pkgs'  = addListToUniqSet acc_pkgs pkg_deps
621           --
622           if pkg /= this_pkg
623              then follow_deps mods acc_mods (addOneToUniqSet acc_pkgs' pkg)
624              else follow_deps (map (mkModule this_pkg) boot_deps' ++ mods)
625                               acc_mods' acc_pkgs'
626         where
627             msg = text "need to link module" <+> ppr mod <+>
628                   text "due to use of Template Haskell"
629
630
631     link_boot_mod_error mod = 
632         ghcError (ProgramError (showSDoc (
633             text "module" <+> ppr mod <+> 
634             text "cannot be linked; it is only available as a boot module")))
635
636     no_obj mod = dieWith span $
637                      ptext (sLit "cannot find object file for module ") <> 
638                         quotes (ppr mod) $$
639                      while_linking_expr
640                 
641     while_linking_expr = ptext (sLit "while linking an interpreted expression")
642
643         -- This one is a build-system bug
644
645     get_linkable maybe_normal_osuf mod_name     -- A home-package module
646         | Just mod_info <- lookupUFM hpt mod_name 
647         = adjust_linkable (Maybes.expectJust "getLinkDeps" (hm_linkable mod_info))
648         | otherwise     
649         = do    -- It's not in the HPT because we are in one shot mode, 
650                 -- so use the Finder to get a ModLocation...
651              mb_stuff <- findHomeModule hsc_env mod_name
652              case mb_stuff of
653                   Found loc mod -> found loc mod
654                   _ -> no_obj mod_name
655         where
656             found loc mod = do {
657                 -- ...and then find the linkable for it
658                mb_lnk <- findObjectLinkableMaybe mod loc ;
659                case mb_lnk of {
660                   Nothing -> no_obj mod ;
661                   Just lnk -> adjust_linkable lnk
662               }}
663
664             adjust_linkable lnk
665                 | Just osuf <- maybe_normal_osuf = do
666                         new_uls <- mapM (adjust_ul osuf) (linkableUnlinked lnk)
667                         return lnk{ linkableUnlinked=new_uls }
668                 | otherwise =
669                         return lnk
670
671             adjust_ul osuf (DotO file) = do
672                 let new_file = replaceExtension file osuf
673                 ok <- doesFileExist new_file
674                 if (not ok)
675                    then dieWith span $
676                           ptext (sLit "cannot find normal object file ")
677                                 <> quotes (text new_file) $$ while_linking_expr
678                    else return (DotO new_file)
679             adjust_ul _ _ = panic "adjust_ul"
680 \end{code}
681
682
683 %************************************************************************
684 %*                                                                      *
685                 Link some linkables
686         The linkables may consist of a mixture of 
687         byte-code modules and object modules
688 %*                                                                      *
689 %************************************************************************
690
691 \begin{code}
692 linkModules :: DynFlags -> PersistentLinkerState -> [Linkable]
693             -> IO (PersistentLinkerState, SuccessFlag)
694 linkModules dflags pls linkables
695   = mask_ $ do  -- don't want to be interrupted by ^C in here
696         
697         let (objs, bcos) = partition isObjectLinkable 
698                               (concatMap partitionLinkable linkables)
699
700                 -- Load objects first; they can't depend on BCOs
701         (pls1, ok_flag) <- dynLinkObjs dflags pls objs
702
703         if failed ok_flag then 
704                 return (pls1, Failed)
705           else do
706                 pls2 <- dynLinkBCOs pls1 bcos
707                 return (pls2, Succeeded)
708                 
709
710 -- HACK to support f-x-dynamic in the interpreter; no other purpose
711 partitionLinkable :: Linkable -> [Linkable]
712 partitionLinkable li
713    = let li_uls = linkableUnlinked li
714          li_uls_obj = filter isObject li_uls
715          li_uls_bco = filter isInterpretable li_uls
716      in 
717          case (li_uls_obj, li_uls_bco) of
718             (_:_, _:_) -> [li {linkableUnlinked=li_uls_obj},
719                            li {linkableUnlinked=li_uls_bco}]
720             _ -> [li]
721
722 findModuleLinkable_maybe :: [Linkable] -> Module -> Maybe Linkable
723 findModuleLinkable_maybe lis mod
724    = case [LM time nm us | LM time nm us <- lis, nm == mod] of
725         []   -> Nothing
726         [li] -> Just li
727         _    -> pprPanic "findModuleLinkable" (ppr mod)
728
729 linkableInSet :: Linkable -> [Linkable] -> Bool
730 linkableInSet l objs_loaded =
731   case findModuleLinkable_maybe objs_loaded (linkableModule l) of
732         Nothing -> False
733         Just m  -> linkableTime l == linkableTime m
734 \end{code}
735
736
737 %************************************************************************
738 %*                                                                      *
739 \subsection{The object-code linker}
740 %*                                                                      *
741 %************************************************************************
742
743 \begin{code}
744 dynLinkObjs :: DynFlags -> PersistentLinkerState -> [Linkable]
745             -> IO (PersistentLinkerState, SuccessFlag)
746 dynLinkObjs dflags pls objs = do
747         -- Load the object files and link them
748         let (objs_loaded', new_objs) = rmDupLinkables (objs_loaded pls) objs
749             pls1                     = pls { objs_loaded = objs_loaded' }
750             unlinkeds                = concatMap linkableUnlinked new_objs
751
752         mapM_ loadObj (map nameOfObject unlinkeds)
753
754         -- Link the all together
755         ok <- resolveObjs
756
757         -- If resolving failed, unload all our 
758         -- object modules and carry on
759         if succeeded ok then do
760                 return (pls1, Succeeded)
761           else do
762                 pls2 <- unload_wkr dflags [] pls1
763                 return (pls2, Failed)
764
765
766 rmDupLinkables :: [Linkable]    -- Already loaded
767                -> [Linkable]    -- New linkables
768                -> ([Linkable],  -- New loaded set (including new ones)
769                    [Linkable])  -- New linkables (excluding dups)
770 rmDupLinkables already ls
771   = go already [] ls
772   where
773     go already extras [] = (already, extras)
774     go already extras (l:ls)
775         | linkableInSet l already = go already     extras     ls
776         | otherwise               = go (l:already) (l:extras) ls
777 \end{code}
778
779 %************************************************************************
780 %*                                                                      *
781 \subsection{The byte-code linker}
782 %*                                                                      *
783 %************************************************************************
784
785 \begin{code}
786 dynLinkBCOs :: PersistentLinkerState -> [Linkable] -> IO PersistentLinkerState
787 dynLinkBCOs pls bcos = do
788
789         let (bcos_loaded', new_bcos) = rmDupLinkables (bcos_loaded pls) bcos
790             pls1                     = pls { bcos_loaded = bcos_loaded' }
791             unlinkeds :: [Unlinked]
792             unlinkeds                = concatMap linkableUnlinked new_bcos
793
794             cbcs :: [CompiledByteCode]
795             cbcs      = map byteCodeOfObject unlinkeds
796                       
797                       
798             ul_bcos    = [b | ByteCode bs _  <- cbcs, b <- bs]
799             ies        = [ie | ByteCode _ ie <- cbcs]
800             gce       = closure_env pls
801             final_ie  = foldr plusNameEnv (itbl_env pls) ies
802
803         (final_gce, _linked_bcos) <- linkSomeBCOs True final_ie gce ul_bcos
804                 -- XXX What happens to these linked_bcos?
805
806         let pls2 = pls1 { closure_env = final_gce,
807                           itbl_env    = final_ie }
808
809         return pls2
810
811 -- Link a bunch of BCOs and return them + updated closure env.
812 linkSomeBCOs :: Bool    -- False <=> add _all_ BCOs to returned closure env
813                         -- True  <=> add only toplevel BCOs to closure env
814              -> ItblEnv 
815              -> ClosureEnv 
816              -> [UnlinkedBCO]
817              -> IO (ClosureEnv, [HValue])
818                         -- The returned HValues are associated 1-1 with
819                         -- the incoming unlinked BCOs.  Each gives the
820                         -- value of the corresponding unlinked BCO
821                                         
822 linkSomeBCOs toplevs_only ie ce_in ul_bcos
823    = do let nms = map unlinkedBCOName ul_bcos
824         hvals <- fixIO 
825                     ( \ hvs -> let ce_out = extendClosureEnv ce_in (zipLazy nms hvs)
826                                in  mapM (linkBCO ie ce_out) ul_bcos )
827         let ce_all_additions = zip nms hvals
828             ce_top_additions = filter (isExternalName.fst) ce_all_additions
829             ce_additions     = if toplevs_only then ce_top_additions 
830                                                else ce_all_additions
831             ce_out = -- make sure we're not inserting duplicate names into the 
832                      -- closure environment, which leads to trouble.
833                      ASSERT (all (not . (`elemNameEnv` ce_in)) (map fst ce_additions))
834                      extendClosureEnv ce_in ce_additions
835         return (ce_out, hvals)
836
837 \end{code}
838
839
840 %************************************************************************
841 %*                                                                      *
842                 Unload some object modules
843 %*                                                                      *
844 %************************************************************************
845
846 \begin{code}
847 -- ---------------------------------------------------------------------------
848 -- | Unloading old objects ready for a new compilation sweep.
849 --
850 -- The compilation manager provides us with a list of linkables that it
851 -- considers \"stable\", i.e. won't be recompiled this time around.  For
852 -- each of the modules current linked in memory,
853 --
854 --   * if the linkable is stable (and it's the same one -- the user may have
855 --     recompiled the module on the side), we keep it,
856 --
857 --   * otherwise, we unload it.
858 --
859 --   * we also implicitly unload all temporary bindings at this point.
860 --
861 unload :: DynFlags
862        -> [Linkable] -- ^ The linkables to *keep*.
863        -> IO ()
864 unload dflags linkables
865   = mask_ $ do -- mask, so we're safe from Ctrl-C in here
866   
867         -- Initialise the linker (if it's not been done already)
868         initDynLinker dflags
869
870         new_pls
871             <- modifyMVar v_PersistentLinkerState $ \pls -> do
872                  pls1 <- unload_wkr dflags linkables pls
873                  return (pls1, pls1)
874
875         debugTraceMsg dflags 3 (text "unload: retaining objs" <+> ppr (objs_loaded new_pls))
876         debugTraceMsg dflags 3 (text "unload: retaining bcos" <+> ppr (bcos_loaded new_pls))
877         return ()
878
879 unload_wkr :: DynFlags
880            -> [Linkable]                -- stable linkables
881            -> PersistentLinkerState
882            -> IO PersistentLinkerState
883 -- Does the core unload business
884 -- (the wrapper blocks exceptions and deals with the PLS get and put)
885
886 unload_wkr _ linkables pls
887   = do  let (objs_to_keep, bcos_to_keep) = partition isObjectLinkable linkables
888
889         objs_loaded' <- filterM (maybeUnload objs_to_keep) (objs_loaded pls)
890         bcos_loaded' <- filterM (maybeUnload bcos_to_keep) (bcos_loaded pls)
891
892         let bcos_retained = map linkableModule bcos_loaded'
893             itbl_env'     = filterNameMap bcos_retained (itbl_env pls)
894             closure_env'  = filterNameMap bcos_retained (closure_env pls)
895             new_pls = pls { itbl_env = itbl_env',
896                             closure_env = closure_env',
897                             bcos_loaded = bcos_loaded',
898                             objs_loaded = objs_loaded' }
899
900         return new_pls
901   where
902     maybeUnload :: [Linkable] -> Linkable -> IO Bool
903     maybeUnload keep_linkables lnk
904       | linkableInSet lnk keep_linkables = return True
905       | otherwise                   
906       = do mapM_ unloadObj [f | DotO f <- linkableUnlinked lnk]
907                 -- The components of a BCO linkable may contain
908                 -- dot-o files.  Which is very confusing.
909                 --
910                 -- But the BCO parts can be unlinked just by 
911                 -- letting go of them (plus of course depopulating
912                 -- the symbol table which is done in the main body)
913            return False
914 \end{code}
915
916
917 %************************************************************************
918 %*                                                                      *
919                 Loading packages
920 %*                                                                      *
921 %************************************************************************
922
923
924 \begin{code}
925 data LibrarySpec 
926    = Object FilePath    -- Full path name of a .o file, including trailing .o
927                         -- For dynamic objects only, try to find the object 
928                         -- file in all the directories specified in 
929                         -- v_Library_paths before giving up.
930
931    | DLL String         -- "Unadorned" name of a .DLL/.so
932                         --  e.g.    On unix     "qt"  denotes "libqt.so"
933                         --          On WinDoze  "burble"  denotes "burble.DLL"
934                         --  loadDLL is platform-specific and adds the lib/.so/.DLL
935                         --  suffixes platform-dependently
936
937    | DLLPath FilePath   -- Absolute or relative pathname to a dynamic library
938                         -- (ends with .dll or .so).
939
940    | Framework String   -- Only used for darwin, but does no harm
941
942 -- If this package is already part of the GHCi binary, we'll already
943 -- have the right DLLs for this package loaded, so don't try to
944 -- load them again.
945 -- 
946 -- But on Win32 we must load them 'again'; doing so is a harmless no-op
947 -- as far as the loader is concerned, but it does initialise the list
948 -- of DLL handles that rts/Linker.c maintains, and that in turn is 
949 -- used by lookupSymbol.  So we must call addDLL for each library 
950 -- just to get the DLL handle into the list.
951 partOfGHCi :: [PackageName]
952 partOfGHCi
953  | isWindowsTarget || isDarwinTarget = []
954  | otherwise = map PackageName
955                    ["base", "haskell98", "template-haskell", "editline"]
956
957 showLS :: LibrarySpec -> String
958 showLS (Object nm)    = "(static) " ++ nm
959 showLS (DLL nm)       = "(dynamic) " ++ nm
960 showLS (DLLPath nm)   = "(dynamic) " ++ nm
961 showLS (Framework nm) = "(framework) " ++ nm
962
963 -- | Link exactly the specified packages, and their dependents (unless of
964 -- course they are already linked).  The dependents are linked
965 -- automatically, and it doesn't matter what order you specify the input
966 -- packages.
967 --
968 linkPackages :: DynFlags -> [PackageId] -> IO ()
969 -- NOTE: in fact, since each module tracks all the packages it depends on,
970 --       we don't really need to use the package-config dependencies.
971 --
972 -- However we do need the package-config stuff (to find aux libs etc),
973 -- and following them lets us load libraries in the right order, which 
974 -- perhaps makes the error message a bit more localised if we get a link
975 -- failure.  So the dependency walking code is still here.
976
977 linkPackages dflags new_pkgs = do
978   -- It's probably not safe to try to load packages concurrently, so we take
979   -- a lock.
980   modifyMVar_ v_PersistentLinkerState $ \pls -> do
981     linkPackages' dflags new_pkgs pls
982
983 linkPackages' :: DynFlags -> [PackageId] -> PersistentLinkerState
984              -> IO PersistentLinkerState
985 linkPackages' dflags new_pks pls = do
986     pkgs' <- link (pkgs_loaded pls) new_pks
987     return $! pls { pkgs_loaded = pkgs' }
988   where
989      pkg_map = pkgIdMap (pkgState dflags)
990      ipid_map = installedPackageIdMap (pkgState dflags)
991
992      link :: [PackageId] -> [PackageId] -> IO [PackageId]
993      link pkgs new_pkgs =
994          foldM link_one pkgs new_pkgs
995
996      link_one pkgs new_pkg
997         | new_pkg `elem` pkgs   -- Already linked
998         = return pkgs
999
1000         | Just pkg_cfg <- lookupPackage pkg_map new_pkg
1001         = do {  -- Link dependents first
1002                pkgs' <- link pkgs [ Maybes.expectJust "link_one" $
1003                                     lookupFM ipid_map ipid
1004                                   | ipid <- depends pkg_cfg ]
1005                 -- Now link the package itself
1006              ; linkPackage dflags pkg_cfg
1007              ; return (new_pkg : pkgs') }
1008
1009         | otherwise
1010         = ghcError (CmdLineError ("unknown package: " ++ packageIdString new_pkg))
1011
1012
1013 linkPackage :: DynFlags -> PackageConfig -> IO ()
1014 linkPackage dflags pkg
1015    = do 
1016         let dirs      =  Packages.libraryDirs pkg
1017
1018         let libs      =  Packages.hsLibraries pkg
1019         -- Because of slight differences between the GHC dynamic linker and
1020         -- the native system linker some packages have to link with a
1021         -- different list of libraries when using GHCi. Examples include: libs
1022         -- that are actually gnu ld scripts, and the possability that the .a
1023         -- libs do not exactly match the .so/.dll equivalents. So if the
1024         -- package file provides an "extra-ghci-libraries" field then we use
1025         -- that instead of the "extra-libraries" field.
1026                       ++ (if null (Packages.extraGHCiLibraries pkg)
1027                             then Packages.extraLibraries pkg
1028                             else Packages.extraGHCiLibraries pkg)
1029                       ++ [ lib | '-':'l':lib <- Packages.ldOptions pkg ]
1030         classifieds   <- mapM (locateOneObj dirs) libs
1031
1032         -- Complication: all the .so's must be loaded before any of the .o's.  
1033         let dlls = [ dll | DLL dll    <- classifieds ]
1034             objs = [ obj | Object obj <- classifieds ]
1035
1036         maybePutStr dflags ("Loading package " ++ display (sourcePackageId pkg) ++ " ... ")
1037
1038         -- See comments with partOfGHCi
1039         when (packageName pkg `notElem` partOfGHCi) $ do
1040             loadFrameworks pkg
1041             -- When a library A needs symbols from a library B, the order in
1042             -- extra_libraries/extra_ld_opts is "-lA -lB", because that's the
1043             -- way ld expects it for static linking. Dynamic linking is a
1044             -- different story: When A has no dependency information for B,
1045             -- dlopen-ing A with RTLD_NOW (see addDLL in Linker.c) will fail
1046             -- when B has not been loaded before. In a nutshell: Reverse the
1047             -- order of DLLs for dynamic linking.
1048             -- This fixes a problem with the HOpenGL package (see "Compiling
1049             -- HOpenGL under recent versions of GHC" on the HOpenGL list).
1050             mapM_ (load_dyn dirs) (reverse dlls)
1051         
1052         -- After loading all the DLLs, we can load the static objects.
1053         -- Ordering isn't important here, because we do one final link
1054         -- step to resolve everything.
1055         mapM_ loadObj objs
1056
1057         maybePutStr dflags "linking ... "
1058         ok <- resolveObjs
1059         if succeeded ok then maybePutStrLn dflags "done."
1060               else ghcError (InstallationError ("unable to load package `" ++ display (sourcePackageId pkg) ++ "'"))
1061
1062 load_dyn :: [FilePath] -> FilePath -> IO ()
1063 load_dyn dirs dll = do r <- loadDynamic dirs dll
1064                        case r of
1065                          Nothing  -> return ()
1066                          Just err -> ghcError (CmdLineError ("can't load .so/.DLL for: " 
1067                                                               ++ dll ++ " (" ++ err ++ ")" ))
1068
1069 loadFrameworks :: InstalledPackageInfo_ ModuleName -> IO ()
1070 loadFrameworks pkg
1071  | isDarwinTarget = mapM_ load frameworks
1072  | otherwise = return ()
1073   where
1074     fw_dirs    = Packages.frameworkDirs pkg
1075     frameworks = Packages.frameworks pkg
1076
1077     load fw = do  r <- loadFramework fw_dirs fw
1078                   case r of
1079                     Nothing  -> return ()
1080                     Just err -> ghcError (CmdLineError ("can't load framework: " 
1081                                                         ++ fw ++ " (" ++ err ++ ")" ))
1082
1083 -- Try to find an object file for a given library in the given paths.
1084 -- If it isn't present, we assume it's a dynamic library.
1085 locateOneObj :: [FilePath] -> String -> IO LibrarySpec
1086 locateOneObj dirs lib
1087   | not isDynamicGhcLib
1088     -- When the GHC package was not compiled as dynamic library 
1089     -- (=DYNAMIC not set), we search for .o libraries.
1090   = do  { mb_obj_path <- findFile mk_obj_path dirs 
1091         ; case mb_obj_path of
1092             Just obj_path -> return (Object obj_path)
1093             Nothing       -> return (DLL lib) }
1094
1095   | otherwise
1096     -- When the GHC package was compiled as dynamic library (=DYNAMIC set),
1097     -- we search for .so libraries first.
1098   = do  { mb_lib_path <- findFile mk_dyn_lib_path dirs
1099         ; case mb_lib_path of
1100             Just _ -> return (DLL dyn_lib_name)
1101             Nothing       ->
1102                 do { mb_obj_path <- findFile mk_obj_path dirs
1103                    ; case mb_obj_path of
1104                        Just obj_path -> return (Object obj_path)
1105                        Nothing       -> return (DLL lib) }}             -- We assume
1106    where
1107      mk_obj_path dir = dir </> (lib <.> "o")
1108      dyn_lib_name = lib ++ "-ghc" ++ cProjectVersion
1109      mk_dyn_lib_path dir = dir </> mkSOName dyn_lib_name
1110
1111 -- ----------------------------------------------------------------------------
1112 -- Loading a dyanmic library (dlopen()-ish on Unix, LoadLibrary-ish on Win32)
1113
1114 -- return Nothing == success, else Just error message from dlopen
1115 loadDynamic :: [FilePath] -> FilePath -> IO (Maybe String)
1116 loadDynamic paths rootname
1117   = do  { mb_dll <- findFile mk_dll_path paths
1118         ; case mb_dll of
1119             Just dll -> loadDLL dll
1120             Nothing  -> loadDLL (mkSOName rootname) }
1121                         -- Tried all our known library paths, so let 
1122                         -- dlopen() search its own builtin paths now.
1123   where
1124     mk_dll_path dir = dir </> mkSOName rootname
1125
1126 mkSOName :: FilePath -> FilePath
1127 mkSOName root
1128  | isDarwinTarget  = ("lib" ++ root) <.> "dylib"
1129  | isWindowsTarget = -- Win32 DLLs have no .dll extension here, because
1130                      -- addDLL tries both foo.dll and foo.drv
1131                      root
1132  | otherwise       = ("lib" ++ root) <.> "so"
1133
1134 -- Darwin / MacOS X only: load a framework
1135 -- a framework is a dynamic library packaged inside a directory of the same
1136 -- name. They are searched for in different paths than normal libraries.
1137 loadFramework :: [FilePath] -> FilePath -> IO (Maybe String)
1138 loadFramework extraPaths rootname
1139    = do { either_dir <- tryIO getHomeDirectory
1140         ; let homeFrameworkPath = case either_dir of
1141                                   Left _ -> []
1142                                   Right dir -> [dir ++ "/Library/Frameworks"]
1143               ps = extraPaths ++ homeFrameworkPath ++ defaultFrameworkPaths
1144         ; mb_fwk <- findFile mk_fwk ps
1145         ; case mb_fwk of
1146             Just fwk_path -> loadDLL fwk_path
1147             Nothing       -> return (Just "not found") }
1148                 -- Tried all our known library paths, but dlopen()
1149                 -- has no built-in paths for frameworks: give up
1150    where
1151      mk_fwk dir = dir </> (rootname ++ ".framework/" ++ rootname)
1152         -- sorry for the hardcoded paths, I hope they won't change anytime soon:
1153      defaultFrameworkPaths = ["/Library/Frameworks", "/System/Library/Frameworks"]
1154 \end{code}
1155
1156 %************************************************************************
1157 %*                                                                      *
1158                 Helper functions
1159 %*                                                                      *
1160 %************************************************************************
1161
1162 \begin{code}
1163 findFile :: (FilePath -> FilePath)      -- Maps a directory path to a file path
1164          -> [FilePath]                  -- Directories to look in
1165          -> IO (Maybe FilePath)         -- The first file path to match
1166 findFile _ [] 
1167   = return Nothing
1168 findFile mk_file_path (dir:dirs)
1169   = do  { let file_path = mk_file_path dir
1170         ; b <- doesFileExist file_path
1171         ; if b then 
1172              return (Just file_path)
1173           else
1174              findFile mk_file_path dirs }
1175 \end{code}
1176
1177 \begin{code}
1178 maybePutStr :: DynFlags -> String -> IO ()
1179 maybePutStr dflags s | verbosity dflags > 0 = putStr s
1180                      | otherwise            = return ()
1181
1182 maybePutStrLn :: DynFlags -> String -> IO ()
1183 maybePutStrLn dflags s | verbosity dflags > 0 = putStrLn s
1184                        | otherwise            = return ()
1185 \end{code}