{-# LINE 1 "System/Posix/DynamicLinker/Common.hsc" #-}{-# LANGUAGE Safe #-}------------------------------------------------------------------------------- |-- Module : System.Posix.DynamicLinker.Common-- Copyright : (c) Volker Stolz <vs@foldr.org> 2003-- License : BSD-style (see the file libraries/base/LICENSE)---- Maintainer : vs@foldr.org-- Stability : provisional-- Portability : non-portable (requires POSIX)---- Dynamic linker support through dlopen()-----------------------------------------------------------------------------moduleSystem.Posix.DynamicLinker.Common(moduleSystem.Posix.DynamicLinker.Prim,dlsym,dlerror,dlclose,undl,throwDLErrorIf,Module(..))-- Usage:-- ******---- Let's assume you want to open a local shared library \'foo\' (.\/libfoo.so)-- offering a function-- @char \* mogrify (char\*,int)@-- and invoke @str = mogrify("test",1)@:------ type Fun = CString -> Int -> IO CString-- foreign import dynamic unsafe fun__ :: FunPtr Fun -> Fun---- withDL "libfoo.so" [RTLD_NOW] \$ \\ mod -> do-- funptr <- dlsym mod "mogrify"-- let fun = fun__ funptr-- withCString "test" \$ \\ str -> do-- strptr <- fun str 1-- strstr <- peekCString strptr-- ...--whereimportSystem.Posix.DynamicLinker.PrimimportForeignimportForeign.Cdlclose::DL->IO()dlclose(DLHandleh)=throwDLErrorIf_"dlclose"(/=0)$c_dlclosehdlcloseh=error$"dlclose: invalid argument"++(showh)dlerror::IOStringdlerror=c_dlerror>>=peekCString-- |'dlsym' returns the address binding of the symbol described in @symbol@,-- as it occurs in the shared object identified by @source@.dlsym::DL->String->IO(FunPtra)dlsymsourcesymbol=dowithCAStringsymbol$\s->dothrowDLErrorIf"dlsym"(==nullFunPtr)$c_dlsym(packDLsource)s-- |'undl' obtains the raw handle. You mustn't do something like-- @withDL mod flags $ liftM undl >>= \ p -> use p@undl::DL->Ptr()undl=packDLthrowDLErrorIf::String->(a->Bool)->IOa->IOathrowDLErrorIfspf=dor<-fif(pr)thendlerror>>=\err->ioError(userError(s++": "++err))elsereturnrthrowDLErrorIf_::String->(a->Bool)->IOa->IO()throwDLErrorIf_spf=throwDLErrorIfspf>>return()-- abstract handle for dynamically loaded module (EXPORTED)--newtypeModule=Module(Ptr())