Skip to content

Instantly share code, notes, and snippets.

@cocreature
Created May 20, 2016 17:59
Show Gist options
  • Save cocreature/2e3ca5d921d08f8e0704b19b7dd186a6 to your computer and use it in GitHub Desktop.
Save cocreature/2e3ca5d921d08f8e0704b19b7dd186a6 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MagicHash, UnboxedTuples #-}
module Main(main) where
import GHC.Exts ( addrToAny# )
import GHC.Ptr ( Ptr(..) )
import System.Info ( os, arch )
import Encoding
import ObjLink
main :: IO ()
main = do initObjLinker
loadObj "plugin.o"
_ret <- resolveObjs
ptr <- lookupSymbol (mangleSymbol Nothing "Plugin" "f")
case ptr of
Nothing -> putStrLn "Couldn’t load symbol"
Just (Ptr addr) -> case addrToAny# addr of
(# hval #) -> putStrLn hval
mangleSymbol :: Maybe String -> String -> String -> String
mangleSymbol pkg module' valsym =
prefixUnderscore ++
maybe "" (\p -> zEncodeString p ++ "_") pkg ++
zEncodeString module' ++ "_" ++ zEncodeString valsym ++ "_closure"
prefixUnderscore :: String
prefixUnderscore =
case (os,arch) of
("mingw32","x86_64") -> ""
("cygwin","x86_64") -> ""
("mingw32",_) -> "_"
("darwin",_) -> "_"
("cygwin",_) -> "_"
_ -> ""
module Plugin(f) where
f :: String
f = "Monads are just monoids in the category of endofunctors, what’s the problem?"
@homam
Copy link

homam commented Jun 18, 2017

Main Post: https://purelyfunctional.org/posts/2016-05-20-dynamic-loading-haskell-module.html

Compile with ghc -package ghc -rdynamic main.hs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment