foreign import ccall "dynamic"
code_void :: FunPtr (CInt -> IO CInt) -> (CInt -> IO CInt)
+foreign import ccall "getaddr"
+ getaddr :: CUInt
+
+foreign import ccall "callertrap"
+ callertrap :: IO ()
+
$(callDecl "callAsWord32" [t|Word32|])
runstuff :: Ptr Int32 -> B.ByteString -> IO ()
runstuff env bytecode = do
- let emittedcode = compile $ codeInstructions $ decodeMethod bytecode
+ let emittedcode = (compile (fromIntegral getaddr)) $ codeInstructions $ decodeMethod bytecode
(_, Right ((entryPtr, endOffset), disasm)) <- runCodeGen emittedcode env ()
printf "entry point: 0x%08x\n" ((fromIntegral $ ptrToIntPtr entryPtr) :: Int)
Right newdisasm <- disassembleBlock entryPtr endOffset
mapM_ (putStrLn . showAtt) $ newdisasm
+ let addr :: Int; addr = (fromIntegral getaddr :: Int)
+ printf "getaddr: 0x%08x\n" addr
+
return ()
pop ebp
ret
-compile :: [J.Instruction] -> CodeGen (Ptr Int32) s ((Ptr Word8, Int), [Instruction])
-compile insn = do
+compile :: Word32 -> [J.Instruction] -> CodeGen (Ptr Int32) s ((Ptr Word8, Int), [Instruction])
+compile trapaddr insn = do
entryCode
mapM compile_ins insn
+ push eax
+ mov ecx (trapaddr :: Word32)
+ call ecx
+ -- call trapaddr -- Y U DON'T WORK? (ask mr. gdb for help)
+ pop eax
exitCode
d <- disassemble
c <- getEntryPoint
--- /dev/null
+#include <stdio.h>
+
+void callertrap(void)
+{
+ char buf[5];
+ unsigned int *ptr = (unsigned int) (buf + 1);
+
+ printf("callertrap by 0x%08x\n", *(ptr + 4));
+ /* TODO:
+ * call magic haskell function
+ * with environment information */
+}
+
+unsigned int getaddr(void)
+{
+ return (unsigned int) callertrap;
+}