2 % (c) The University of Glasgow 2005-2006
5 -- | The dynamic linker for GHCi.
7 -- This module deals with the top-level issues of dynamic linking,
8 -- calling the object-code linker and the byte-code linker where
11 {-# OPTIONS -fno-cse #-}
12 -- -fno-cse is needed for GLOBAL_VAR's to behave properly
14 module Linker ( HValue, getHValue, showLinkerState,
15 linkExpr, unload, withExtendedLinkEnv,
16 extendLinkEnv, deleteFromLinkEnv,
18 linkPackages,initDynLinker,
22 #include "HsVersions.h"
40 import qualified OccName
52 import qualified Maybes
57 import Config ( cProjectVersion )
66 import Control.Concurrent.MVar
68 import System.FilePath
70 import System.Directory
72 import Distribution.Package hiding (depends, PackageId)
78 %************************************************************************
82 %************************************************************************
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.
88 The PersistentLinkerState maps Names to actual closures (for
89 interpreted code only), for use during linking.
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
95 data PersistentLinkerState
96 = PersistentLinkerState {
98 -- Current global mapping from Names to their true values
99 closure_env :: ClosureEnv,
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
108 -- The currently loaded interpreted modules (home package)
109 bcos_loaded :: [Linkable],
111 -- And the currently-loaded compiled modules (home package)
112 objs_loaded :: [Linkable],
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]
120 emptyPLS :: DynFlags -> PersistentLinkerState
121 emptyPLS _ = PersistentLinkerState {
122 closure_env = emptyNameEnv,
123 itbl_env = emptyNameEnv,
124 pkgs_loaded = init_pkgs,
128 -- Packages that don't need loading, because the compiler
129 -- shares them with the interpreted program.
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]
137 extendLoadedPkgs :: [PackageId] -> IO ()
138 extendLoadedPkgs pkgs =
139 modifyMVar_ v_PersistentLinkerState $ \s ->
140 return s{ pkgs_loaded = pkgs ++ pkgs_loaded s }
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 }
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 }
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:
160 -- > Package:Module.Name
162 -- We use this string to lookup the interpreter's internal representation of the name
163 -- using the lookupOrig.
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)
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.
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.
189 The diagrams below show where the string is to be found relative to
190 the normal info table of the closure.
192 1) Code next to table:
195 | | <- pointer to the start of the string
197 | | <- the (start of the) info table structure
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
208 2) Code NOT next to table:
211 info table structure -> | *------------------> --------------
215 ptr to start of str -> | |
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
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))
228 peek $ intPtrToPtr $ (ptrToIntPtr ptr) + fromIntegral stdInfoTableSizeB
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])
239 = ASSERT (all (>0) (map length [pkg, mod, occ])) (pkg, mod, occ)
241 dot = fromIntegral (ord '.')
242 (pkg, rest1) = break (== fromIntegral (ord ':')) input
244 = (concat $ intersperse [dot] $ reverse modWords, occWord)
246 (modWords, occWord) = ASSERT (length rest1 > 0) (parseModOcc [] (tail rest1))
247 parseModOcc :: [[Word8]] -> [Word8] -> ([[Word8]], [Word8])
249 = case break (== dot) str of
250 (top, []) -> (acc, top)
251 (top, _:bot) -> parseModOcc (top : acc) bot
253 -- | Get the 'HValue' associated with the given name.
255 -- May cause loading the module that contains the name.
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')
267 lookupName (closure_env pls) name
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
281 -- Find what packages and linkables are required
282 (lnks, pkgs) <- getLinkDeps hsc_env hpt pls
283 maybe_normal_osuf span needed_mods
285 -- Link the packages and modules required
286 pls1 <- linkPackages' dflags pkgs pls
287 linkModules dflags pls1 lnks
290 -- | Temporarily extend the linker state.
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)
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
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 }
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
316 filterNameMap :: [Module] -> NameEnv (Name, a) -> NameEnv (Name, a)
317 filterNameMap mods env
318 = filterNameEnv keep_elt env
320 keep_elt (n,_) = isExternalName n
321 && (nameModule n `elem` mods)
326 -- | Display the persistent linker state.
327 showLinkerState :: IO ()
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)])
339 %************************************************************************
341 \subsection{Initialisation}
343 %************************************************************************
346 -- | Initialise the dynamic linker. This entails
348 -- a) Calling the C initialisation procedure,
350 -- b) Loading any packages specified on the command line,
352 -- c) Loading any packages specified on the command line, now held in the
353 -- @-l@ options in @v_Opt_l@,
355 -- d) Loading any @.o\/.dll@ files specified on the command line, now held
358 -- e) Loading any MacOS frameworks.
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
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
372 reallyInitDynLinker :: DynFlags -> IO PersistentLinkerState
373 reallyInitDynLinker dflags =
374 do { -- Initialise the linker state
375 let pls0 = emptyPLS dflags
377 -- (a) initialise the C dynamic linker
380 -- (b) Load packages from the command-line
381 ; pls <- linkPackages' dflags (preloadPackages (pkgState dflags)) pls0
383 -- (c) Link libraries from the command-line
384 ; let optl = getOpts dflags opt_l
385 ; let minus_ls = [ lib | '-':'l':lib <- optl ]
387 -- (d) Link .o files from the command-line
388 ; let lib_paths = libraryPaths dflags
389 ; cmdline_ld_inputs <- readIORef v_Ld_inputs
391 ; classified_ld_inputs <- mapM classifyLdInput cmdline_ld_inputs
393 -- (e) Link any MacOS frameworks
394 ; let framework_paths
395 | isDarwinTarget = frameworkPaths dflags
398 | isDarwinTarget = cmdlineFrameworks dflags
400 -- Finally do (c),(d),(e)
401 ; let cmdline_lib_specs = [ l | Just l <- classified_ld_inputs ]
403 ++ map Framework frameworks
404 ; if null cmdline_lib_specs then return pls
407 { mapM_ (preloadLib dflags lib_paths framework_paths) cmdline_lib_specs
408 ; maybePutStr dflags "final link ... "
411 ; if succeeded ok then maybePutStrLn dflags "done"
412 else ghcError (ProgramError "linking extra libraries/objects failed")
417 classifyLdInput :: FilePath -> IO (Maybe LibrarySpec)
419 | isObjectFilename f = return (Just (Object f))
420 | isDynLibFilename f = return (Just (DLLPath f))
422 hPutStrLn stderr ("Warning: ignoring unrecognised input `" ++ f ++ "'")
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 ++ " ... ")
430 -> do b <- preload_static lib_paths static_ish
431 maybePutStrLn dflags (if b then "done"
435 -> do maybe_errstr <- loadDynamic lib_paths dll_unadorned
437 Nothing -> maybePutStrLn dflags "done"
438 Just mm -> preloadFailed mm lib_paths lib_spec
441 -> do maybe_errstr <- loadDLL dll_path
443 Nothing -> maybePutStrLn dflags "done"
444 Just mm -> preloadFailed mm lib_paths lib_spec
448 -> do maybe_errstr <- loadFramework framework_paths framework
450 Nothing -> maybePutStrLn dflags "done"
451 Just mm -> preloadFailed mm framework_paths lib_spec
452 | otherwise -> panic "preloadLib Framework"
455 preloadFailed :: String -> [String] -> LibrarySpec -> IO ()
456 preloadFailed sys_errmsg paths spec
457 = do maybePutStr dflags "failed.\n"
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)))))
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
474 %************************************************************************
476 Link a byte-code expression
478 %************************************************************************
481 -- | Link a single expression, /including/ first linking packages and
482 -- modules that this expression depends on.
484 -- Raises an IO exception ('ProgramError') if it can't find a compiled
485 -- version of the dependents to link.
487 linkExpr :: HscEnv -> SrcSpan -> UnlinkedBCO -> IO HValue
488 linkExpr hsc_env span root_ul_bco
490 -- Initialise the linker (if it's not been done already)
491 let dflags = hsc_dflags hsc_env
492 ; initDynLinker dflags
494 -- Take lock for the actual work.
495 ; modifyMVar v_PersistentLinkerState $ \pls0 -> do {
497 -- Link the packages and modules required
498 ; (pls, ok) <- linkDependencies hsc_env pls0 span needed_mods
500 ghcError (ProgramError "")
503 -- Link the expression itself
504 let ie = itbl_env pls
507 -- Link the necessary packages and linkables
508 ; (_, (root_hval:_)) <- linkSomeBCOs False ie ce [root_ul_bco]
509 ; return (pls, root_hval)
512 free_names = nameSetToList (bcoFreeNames root_ul_bco)
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.
524 dieWith :: SrcSpan -> Message -> IO a
525 dieWith span msg = ghcError (ProgramError (showSDoc (mkLocMessage span msg)))
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 let default_osuf = phaseInputExt StopLn
533 if objectSuf dflags == default_osuf
534 then failNonStd srcspan
535 else return (Just default_osuf)
537 failNonStd :: SrcSpan -> IO (Maybe String)
538 failNonStd srcspan = dieWith srcspan $
539 ptext (sLit "Dynamic linking required, but this is a non-standard build (eg. prof).") $$
540 ptext (sLit "You need to build the program twice: once the normal way, and then") $$
541 ptext (sLit "in the desired way using -osuf to set the object file suffix.")
544 getLinkDeps :: HscEnv -> HomePackageTable
545 -> PersistentLinkerState
546 -> Maybe String -- the "normal" object suffix
547 -> SrcSpan -- for error messages
548 -> [Module] -- If you need these
549 -> IO ([Linkable], [PackageId]) -- ... then link these first
550 -- Fails with an IO exception if it can't find enough files
552 getLinkDeps hsc_env hpt pls maybe_normal_osuf span mods
553 -- Find all the packages and linkables that a set of modules depends on
555 -- 1. Find the dependent home-pkg-modules/packages from each iface
556 (mods_s, pkgs_s) <- follow_deps mods emptyUniqSet emptyUniqSet;
559 -- 2. Exclude ones already linked
560 -- Main reason: avoid findModule calls in get_linkable
561 mods_needed = mods_s `minusList` linked_mods ;
562 pkgs_needed = pkgs_s `minusList` pkgs_loaded pls ;
564 linked_mods = map (moduleName.linkableModule)
565 (objs_loaded pls ++ bcos_loaded pls)
568 -- putStrLn (showSDoc (ppr mods_s)) ;
569 -- 3. For each dependent module, find its linkable
570 -- This will either be in the HPT or (in the case of one-shot
571 -- compilation) we may need to use maybe_getFileLinkable
572 lnks_needed <- mapM (get_linkable maybe_normal_osuf) mods_needed ;
574 return (lnks_needed, pkgs_needed) }
576 dflags = hsc_dflags hsc_env
577 this_pkg = thisPackage dflags
579 -- The ModIface contains the transitive closure of the module dependencies
580 -- within the current package, *except* for boot modules: if we encounter
581 -- a boot module, we have to find its real interface and discover the
582 -- dependencies of that. Hence we need to traverse the dependency
583 -- tree recursively. See bug #936, testcase ghci/prog007.
584 follow_deps :: [Module] -- modules to follow
585 -> UniqSet ModuleName -- accum. module dependencies
586 -> UniqSet PackageId -- accum. package dependencies
587 -> IO ([ModuleName], [PackageId]) -- result
588 follow_deps [] acc_mods acc_pkgs
589 = return (uniqSetToList acc_mods, uniqSetToList acc_pkgs)
590 follow_deps (mod:mods) acc_mods acc_pkgs
592 mb_iface <- initIfaceCheck hsc_env $
593 loadInterface msg mod (ImportByUser False)
594 iface <- case mb_iface of
595 Maybes.Failed err -> ghcError (ProgramError (showSDoc err))
596 Maybes.Succeeded iface -> return iface
598 when (mi_boot iface) $ link_boot_mod_error mod
601 pkg = modulePackageId mod
604 pkg_deps = dep_pkgs deps
605 (boot_deps, mod_deps) = partitionWith is_boot (dep_mods deps)
606 where is_boot (m,True) = Left m
607 is_boot (m,False) = Right m
609 boot_deps' = filter (not . (`elementOfUniqSet` acc_mods)) boot_deps
610 acc_mods' = addListToUniqSet acc_mods (moduleName mod : mod_deps)
611 acc_pkgs' = addListToUniqSet acc_pkgs pkg_deps
614 then follow_deps mods acc_mods (addOneToUniqSet acc_pkgs' pkg)
615 else follow_deps (map (mkModule this_pkg) boot_deps' ++ mods)
618 msg = text "need to link module" <+> ppr mod <+>
619 text "due to use of Template Haskell"
622 link_boot_mod_error mod =
623 ghcError (ProgramError (showSDoc (
624 text "module" <+> ppr mod <+>
625 text "cannot be linked; it is only available as a boot module")))
627 no_obj mod = dieWith span $
628 ptext (sLit "cannot find object file for module ") <>
632 while_linking_expr = ptext (sLit "while linking an interpreted expression")
634 -- This one is a build-system bug
636 get_linkable maybe_normal_osuf mod_name -- A home-package module
637 | Just mod_info <- lookupUFM hpt mod_name
638 = adjust_linkable (Maybes.expectJust "getLinkDeps" (hm_linkable mod_info))
640 = do -- It's not in the HPT because we are in one shot mode,
641 -- so use the Finder to get a ModLocation...
642 mb_stuff <- findHomeModule hsc_env mod_name
644 Found loc mod -> found loc mod
648 -- ...and then find the linkable for it
649 mb_lnk <- findObjectLinkableMaybe mod loc ;
651 Nothing -> no_obj mod ;
652 Just lnk -> adjust_linkable lnk
656 | Just osuf <- maybe_normal_osuf = do
657 new_uls <- mapM (adjust_ul osuf) (linkableUnlinked lnk)
658 return lnk{ linkableUnlinked=new_uls }
662 adjust_ul osuf (DotO file) = do
663 let new_file = replaceExtension file osuf
664 ok <- doesFileExist new_file
667 ptext (sLit "cannot find normal object file ")
668 <> quotes (text new_file) $$ while_linking_expr
669 else return (DotO new_file)
670 adjust_ul _ _ = panic "adjust_ul"
674 %************************************************************************
677 The linkables may consist of a mixture of
678 byte-code modules and object modules
680 %************************************************************************
683 linkModules :: DynFlags -> PersistentLinkerState -> [Linkable]
684 -> IO (PersistentLinkerState, SuccessFlag)
685 linkModules dflags pls linkables
686 = block $ do -- don't want to be interrupted by ^C in here
688 let (objs, bcos) = partition isObjectLinkable
689 (concatMap partitionLinkable linkables)
691 -- Load objects first; they can't depend on BCOs
692 (pls1, ok_flag) <- dynLinkObjs dflags pls objs
694 if failed ok_flag then
695 return (pls1, Failed)
697 pls2 <- dynLinkBCOs pls1 bcos
698 return (pls2, Succeeded)
701 -- HACK to support f-x-dynamic in the interpreter; no other purpose
702 partitionLinkable :: Linkable -> [Linkable]
704 = let li_uls = linkableUnlinked li
705 li_uls_obj = filter isObject li_uls
706 li_uls_bco = filter isInterpretable li_uls
708 case (li_uls_obj, li_uls_bco) of
709 (_:_, _:_) -> [li {linkableUnlinked=li_uls_obj},
710 li {linkableUnlinked=li_uls_bco}]
713 findModuleLinkable_maybe :: [Linkable] -> Module -> Maybe Linkable
714 findModuleLinkable_maybe lis mod
715 = case [LM time nm us | LM time nm us <- lis, nm == mod] of
718 _ -> pprPanic "findModuleLinkable" (ppr mod)
720 linkableInSet :: Linkable -> [Linkable] -> Bool
721 linkableInSet l objs_loaded =
722 case findModuleLinkable_maybe objs_loaded (linkableModule l) of
724 Just m -> linkableTime l == linkableTime m
728 %************************************************************************
730 \subsection{The object-code linker}
732 %************************************************************************
735 dynLinkObjs :: DynFlags -> PersistentLinkerState -> [Linkable]
736 -> IO (PersistentLinkerState, SuccessFlag)
737 dynLinkObjs dflags pls objs = do
738 -- Load the object files and link them
739 let (objs_loaded', new_objs) = rmDupLinkables (objs_loaded pls) objs
740 pls1 = pls { objs_loaded = objs_loaded' }
741 unlinkeds = concatMap linkableUnlinked new_objs
743 mapM_ loadObj (map nameOfObject unlinkeds)
745 -- Link the all together
748 -- If resolving failed, unload all our
749 -- object modules and carry on
750 if succeeded ok then do
751 return (pls1, Succeeded)
753 pls2 <- unload_wkr dflags [] pls1
754 return (pls2, Failed)
757 rmDupLinkables :: [Linkable] -- Already loaded
758 -> [Linkable] -- New linkables
759 -> ([Linkable], -- New loaded set (including new ones)
760 [Linkable]) -- New linkables (excluding dups)
761 rmDupLinkables already ls
764 go already extras [] = (already, extras)
765 go already extras (l:ls)
766 | linkableInSet l already = go already extras ls
767 | otherwise = go (l:already) (l:extras) ls
770 %************************************************************************
772 \subsection{The byte-code linker}
774 %************************************************************************
777 dynLinkBCOs :: PersistentLinkerState -> [Linkable] -> IO PersistentLinkerState
778 dynLinkBCOs pls bcos = do
780 let (bcos_loaded', new_bcos) = rmDupLinkables (bcos_loaded pls) bcos
781 pls1 = pls { bcos_loaded = bcos_loaded' }
782 unlinkeds :: [Unlinked]
783 unlinkeds = concatMap linkableUnlinked new_bcos
785 cbcs :: [CompiledByteCode]
786 cbcs = map byteCodeOfObject unlinkeds
789 ul_bcos = [b | ByteCode bs _ <- cbcs, b <- bs]
790 ies = [ie | ByteCode _ ie <- cbcs]
791 gce = closure_env pls
792 final_ie = foldr plusNameEnv (itbl_env pls) ies
794 (final_gce, _linked_bcos) <- linkSomeBCOs True final_ie gce ul_bcos
795 -- XXX What happens to these linked_bcos?
797 let pls2 = pls1 { closure_env = final_gce,
798 itbl_env = final_ie }
802 -- Link a bunch of BCOs and return them + updated closure env.
803 linkSomeBCOs :: Bool -- False <=> add _all_ BCOs to returned closure env
804 -- True <=> add only toplevel BCOs to closure env
808 -> IO (ClosureEnv, [HValue])
809 -- The returned HValues are associated 1-1 with
810 -- the incoming unlinked BCOs. Each gives the
811 -- value of the corresponding unlinked BCO
813 linkSomeBCOs toplevs_only ie ce_in ul_bcos
814 = do let nms = map unlinkedBCOName ul_bcos
816 ( \ hvs -> let ce_out = extendClosureEnv ce_in (zipLazy nms hvs)
817 in mapM (linkBCO ie ce_out) ul_bcos )
818 let ce_all_additions = zip nms hvals
819 ce_top_additions = filter (isExternalName.fst) ce_all_additions
820 ce_additions = if toplevs_only then ce_top_additions
821 else ce_all_additions
822 ce_out = -- make sure we're not inserting duplicate names into the
823 -- closure environment, which leads to trouble.
824 ASSERT (all (not . (`elemNameEnv` ce_in)) (map fst ce_additions))
825 extendClosureEnv ce_in ce_additions
826 return (ce_out, hvals)
831 %************************************************************************
833 Unload some object modules
835 %************************************************************************
838 -- ---------------------------------------------------------------------------
839 -- | Unloading old objects ready for a new compilation sweep.
841 -- The compilation manager provides us with a list of linkables that it
842 -- considers \"stable\", i.e. won't be recompiled this time around. For
843 -- each of the modules current linked in memory,
845 -- * if the linkable is stable (and it's the same one -- the user may have
846 -- recompiled the module on the side), we keep it,
848 -- * otherwise, we unload it.
850 -- * we also implicitly unload all temporary bindings at this point.
853 -> [Linkable] -- ^ The linkables to *keep*.
855 unload dflags linkables
856 = block $ do -- block, so we're safe from Ctrl-C in here
858 -- Initialise the linker (if it's not been done already)
862 <- modifyMVar v_PersistentLinkerState $ \pls -> do
863 pls1 <- unload_wkr dflags linkables pls
866 debugTraceMsg dflags 3 (text "unload: retaining objs" <+> ppr (objs_loaded new_pls))
867 debugTraceMsg dflags 3 (text "unload: retaining bcos" <+> ppr (bcos_loaded new_pls))
870 unload_wkr :: DynFlags
871 -> [Linkable] -- stable linkables
872 -> PersistentLinkerState
873 -> IO PersistentLinkerState
874 -- Does the core unload business
875 -- (the wrapper blocks exceptions and deals with the PLS get and put)
877 unload_wkr _ linkables pls
878 = do let (objs_to_keep, bcos_to_keep) = partition isObjectLinkable linkables
880 objs_loaded' <- filterM (maybeUnload objs_to_keep) (objs_loaded pls)
881 bcos_loaded' <- filterM (maybeUnload bcos_to_keep) (bcos_loaded pls)
883 let bcos_retained = map linkableModule bcos_loaded'
884 itbl_env' = filterNameMap bcos_retained (itbl_env pls)
885 closure_env' = filterNameMap bcos_retained (closure_env pls)
886 new_pls = pls { itbl_env = itbl_env',
887 closure_env = closure_env',
888 bcos_loaded = bcos_loaded',
889 objs_loaded = objs_loaded' }
893 maybeUnload :: [Linkable] -> Linkable -> IO Bool
894 maybeUnload keep_linkables lnk
895 | linkableInSet lnk keep_linkables = return True
897 = do mapM_ unloadObj [f | DotO f <- linkableUnlinked lnk]
898 -- The components of a BCO linkable may contain
899 -- dot-o files. Which is very confusing.
901 -- But the BCO parts can be unlinked just by
902 -- letting go of them (plus of course depopulating
903 -- the symbol table which is done in the main body)
908 %************************************************************************
912 %************************************************************************
917 = Object FilePath -- Full path name of a .o file, including trailing .o
918 -- For dynamic objects only, try to find the object
919 -- file in all the directories specified in
920 -- v_Library_paths before giving up.
922 | DLL String -- "Unadorned" name of a .DLL/.so
923 -- e.g. On unix "qt" denotes "libqt.so"
924 -- On WinDoze "burble" denotes "burble.DLL"
925 -- loadDLL is platform-specific and adds the lib/.so/.DLL
926 -- suffixes platform-dependently
928 | DLLPath FilePath -- Absolute or relative pathname to a dynamic library
929 -- (ends with .dll or .so).
931 | Framework String -- Only used for darwin, but does no harm
933 -- If this package is already part of the GHCi binary, we'll already
934 -- have the right DLLs for this package loaded, so don't try to
937 -- But on Win32 we must load them 'again'; doing so is a harmless no-op
938 -- as far as the loader is concerned, but it does initialise the list
939 -- of DLL handles that rts/Linker.c maintains, and that in turn is
940 -- used by lookupSymbol. So we must call addDLL for each library
941 -- just to get the DLL handle into the list.
942 partOfGHCi :: [PackageName]
944 | isWindowsTarget || isDarwinTarget = []
945 | otherwise = map PackageName
946 ["base", "haskell98", "template-haskell", "editline"]
948 showLS :: LibrarySpec -> String
949 showLS (Object nm) = "(static) " ++ nm
950 showLS (DLL nm) = "(dynamic) " ++ nm
951 showLS (DLLPath nm) = "(dynamic) " ++ nm
952 showLS (Framework nm) = "(framework) " ++ nm
954 -- | Link exactly the specified packages, and their dependents (unless of
955 -- course they are already linked). The dependents are linked
956 -- automatically, and it doesn't matter what order you specify the input
959 linkPackages :: DynFlags -> [PackageId] -> IO ()
960 -- NOTE: in fact, since each module tracks all the packages it depends on,
961 -- we don't really need to use the package-config dependencies.
963 -- However we do need the package-config stuff (to find aux libs etc),
964 -- and following them lets us load libraries in the right order, which
965 -- perhaps makes the error message a bit more localised if we get a link
966 -- failure. So the dependency walking code is still here.
968 linkPackages dflags new_pkgs = do
969 -- It's probably not safe to try to load packages concurrently, so we take
971 modifyMVar_ v_PersistentLinkerState $ \pls -> do
972 linkPackages' dflags new_pkgs pls
974 linkPackages' :: DynFlags -> [PackageId] -> PersistentLinkerState
975 -> IO PersistentLinkerState
976 linkPackages' dflags new_pks pls = do
977 pkgs' <- link (pkgs_loaded pls) new_pks
978 return $! pls { pkgs_loaded = pkgs' }
980 pkg_map = pkgIdMap (pkgState dflags)
981 ipid_map = installedPackageIdMap (pkgState dflags)
983 link :: [PackageId] -> [PackageId] -> IO [PackageId]
985 foldM link_one pkgs new_pkgs
987 link_one pkgs new_pkg
988 | new_pkg `elem` pkgs -- Already linked
991 | Just pkg_cfg <- lookupPackage pkg_map new_pkg
992 = do { -- Link dependents first
993 pkgs' <- link pkgs [ Maybes.expectJust "link_one" $
994 lookupFM ipid_map ipid
995 | ipid <- depends pkg_cfg ]
996 -- Now link the package itself
997 ; linkPackage dflags pkg_cfg
998 ; return (new_pkg : pkgs') }
1001 = ghcError (CmdLineError ("unknown package: " ++ packageIdString new_pkg))
1004 linkPackage :: DynFlags -> PackageConfig -> IO ()
1005 linkPackage dflags pkg
1007 let dirs = Packages.libraryDirs pkg
1009 let libs = Packages.hsLibraries pkg
1010 -- Because of slight differences between the GHC dynamic linker and
1011 -- the native system linker some packages have to link with a
1012 -- different list of libraries when using GHCi. Examples include: libs
1013 -- that are actually gnu ld scripts, and the possability that the .a
1014 -- libs do not exactly match the .so/.dll equivalents. So if the
1015 -- package file provides an "extra-ghci-libraries" field then we use
1016 -- that instead of the "extra-libraries" field.
1017 ++ (if null (Packages.extraGHCiLibraries pkg)
1018 then Packages.extraLibraries pkg
1019 else Packages.extraGHCiLibraries pkg)
1020 ++ [ lib | '-':'l':lib <- Packages.ldOptions pkg ]
1021 classifieds <- mapM (locateOneObj dirs) libs
1023 -- Complication: all the .so's must be loaded before any of the .o's.
1024 let dlls = [ dll | DLL dll <- classifieds ]
1025 objs = [ obj | Object obj <- classifieds ]
1027 maybePutStr dflags ("Loading package " ++ display (sourcePackageId pkg) ++ " ... ")
1029 -- See comments with partOfGHCi
1030 when (packageName pkg `notElem` partOfGHCi) $ do
1032 -- When a library A needs symbols from a library B, the order in
1033 -- extra_libraries/extra_ld_opts is "-lA -lB", because that's the
1034 -- way ld expects it for static linking. Dynamic linking is a
1035 -- different story: When A has no dependency information for B,
1036 -- dlopen-ing A with RTLD_NOW (see addDLL in Linker.c) will fail
1037 -- when B has not been loaded before. In a nutshell: Reverse the
1038 -- order of DLLs for dynamic linking.
1039 -- This fixes a problem with the HOpenGL package (see "Compiling
1040 -- HOpenGL under recent versions of GHC" on the HOpenGL list).
1041 mapM_ (load_dyn dirs) (reverse dlls)
1043 -- After loading all the DLLs, we can load the static objects.
1044 -- Ordering isn't important here, because we do one final link
1045 -- step to resolve everything.
1048 maybePutStr dflags "linking ... "
1050 if succeeded ok then maybePutStrLn dflags "done."
1051 else ghcError (InstallationError ("unable to load package `" ++ display (sourcePackageId pkg) ++ "'"))
1053 load_dyn :: [FilePath] -> FilePath -> IO ()
1054 load_dyn dirs dll = do r <- loadDynamic dirs dll
1056 Nothing -> return ()
1057 Just err -> ghcError (CmdLineError ("can't load .so/.DLL for: "
1058 ++ dll ++ " (" ++ err ++ ")" ))
1060 loadFrameworks :: InstalledPackageInfo_ ModuleName -> IO ()
1062 | isDarwinTarget = mapM_ load frameworks
1063 | otherwise = return ()
1065 fw_dirs = Packages.frameworkDirs pkg
1066 frameworks = Packages.frameworks pkg
1068 load fw = do r <- loadFramework fw_dirs fw
1070 Nothing -> return ()
1071 Just err -> ghcError (CmdLineError ("can't load framework: "
1072 ++ fw ++ " (" ++ err ++ ")" ))
1074 -- Try to find an object file for a given library in the given paths.
1075 -- If it isn't present, we assume it's a dynamic library.
1076 locateOneObj :: [FilePath] -> String -> IO LibrarySpec
1077 locateOneObj dirs lib
1079 -- When the GHC package was not compiled as dynamic library
1080 -- (=__PIC__ not set), we search for .o libraries first.
1081 = do { mb_obj_path <- findFile mk_obj_path dirs
1082 ; case mb_obj_path of
1083 Just obj_path -> return (Object obj_path)
1085 do { mb_lib_path <- findFile mk_dyn_lib_path dirs
1086 ; case mb_lib_path of
1087 Just _ -> return (DLL dyn_lib_name)
1088 Nothing -> return (DLL lib) }} -- We assume
1090 -- When the GHC package was compiled as dynamic library (=__PIC__ set),
1091 -- we search for .so libraries first.
1092 = do { mb_lib_path <- findFile mk_dyn_lib_path dirs
1093 ; case mb_lib_path of
1094 Just _ -> return (DLL (lib ++ "-ghc" ++ cProjectVersion))
1096 do { mb_obj_path <- findFile mk_obj_path dirs
1097 ; case mb_obj_path of
1098 Just obj_path -> return (Object obj_path)
1099 Nothing -> return (DLL lib) }} -- We assume
1101 mk_obj_path dir = dir </> (lib <.> "o")
1102 dyn_lib_name = lib ++ "-ghc" ++ cProjectVersion
1103 mk_dyn_lib_path dir = dir </> mkSOName dyn_lib_name
1105 -- ----------------------------------------------------------------------------
1106 -- Loading a dyanmic library (dlopen()-ish on Unix, LoadLibrary-ish on Win32)
1108 -- return Nothing == success, else Just error message from dlopen
1109 loadDynamic :: [FilePath] -> FilePath -> IO (Maybe String)
1110 loadDynamic paths rootname
1111 = do { mb_dll <- findFile mk_dll_path paths
1113 Just dll -> loadDLL dll
1114 Nothing -> loadDLL (mkSOName rootname) }
1115 -- Tried all our known library paths, so let
1116 -- dlopen() search its own builtin paths now.
1118 mk_dll_path dir = dir </> mkSOName rootname
1120 mkSOName :: FilePath -> FilePath
1122 | isDarwinTarget = ("lib" ++ root) <.> "dylib"
1123 | isWindowsTarget = -- Win32 DLLs have no .dll extension here, because
1124 -- addDLL tries both foo.dll and foo.drv
1126 | otherwise = ("lib" ++ root) <.> "so"
1128 -- Darwin / MacOS X only: load a framework
1129 -- a framework is a dynamic library packaged inside a directory of the same
1130 -- name. They are searched for in different paths than normal libraries.
1131 loadFramework :: [FilePath] -> FilePath -> IO (Maybe String)
1132 loadFramework extraPaths rootname
1133 = do { either_dir <- tryIO getHomeDirectory
1134 ; let homeFrameworkPath = case either_dir of
1136 Right dir -> [dir ++ "/Library/Frameworks"]
1137 ps = extraPaths ++ homeFrameworkPath ++ defaultFrameworkPaths
1138 ; mb_fwk <- findFile mk_fwk ps
1140 Just fwk_path -> loadDLL fwk_path
1141 Nothing -> return (Just "not found") }
1142 -- Tried all our known library paths, but dlopen()
1143 -- has no built-in paths for frameworks: give up
1145 mk_fwk dir = dir </> (rootname ++ ".framework/" ++ rootname)
1146 -- sorry for the hardcoded paths, I hope they won't change anytime soon:
1147 defaultFrameworkPaths = ["/Library/Frameworks", "/System/Library/Frameworks"]
1150 %************************************************************************
1154 %************************************************************************
1157 findFile :: (FilePath -> FilePath) -- Maps a directory path to a file path
1158 -> [FilePath] -- Directories to look in
1159 -> IO (Maybe FilePath) -- The first file path to match
1162 findFile mk_file_path (dir:dirs)
1163 = do { let file_path = mk_file_path dir
1164 ; b <- doesFileExist file_path
1166 return (Just file_path)
1168 findFile mk_file_path dirs }
1172 maybePutStr :: DynFlags -> String -> IO ()
1173 maybePutStr dflags s | verbosity dflags > 0 = putStr s
1174 | otherwise = return ()
1176 maybePutStrLn :: DynFlags -> String -> IO ()
1177 maybePutStrLn dflags s | verbosity dflags > 0 = putStrLn s
1178 | otherwise = return ()