Created
June 10, 2012 09:17
-
-
Save lewurm/2904609 to your computer and use it in GitHub Desktop.
haskell: get address of a haskell function (at runtime)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.hi | |
*_stub.h | |
*.o | |
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/10967598/get-the-address-of-a-function-without-ffi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void func(void); | |
unsigned long getFuncAddr(void) | |
{ | |
return (unsigned long) func; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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