Skip to content

Instantly share code, notes, and snippets.

@lewurm
Created June 10, 2012 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewurm/2904609 to your computer and use it in GitHub Desktop.
Save lewurm/2904609 to your computer and use it in GitHub Desktop.
haskell: get address of a haskell function (at runtime)
*.hi
*_stub.h
*.o
main
http://stackoverflow.com/questions/10967598/get-the-address-of-a-function-without-ffi
void func(void);
unsigned long getFuncAddr(void)
{
return (unsigned long) func;
}
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
import Foreign.C.Types
import Text.Printf
foreign import ccall "getFuncAddr"
getFuncAddr :: CULong
main :: IO ()
main = do
printf "0x%016x\n" (fromIntegral getFuncAddr :: Word64)
foreign export ccall func :: IO ()
func :: IO ()
func = do
printf "hello world\n"
all: main
./$<
objdump -D $< | grep '<func>'
main: main.hs ffi.c
ghc --make -O2 $^ -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment