Resolve symbols
authorLuke Lau <luke_lau@icloud.com>
Sat, 9 Nov 2019 18:08:33 +0000 (18:08 +0000)
committerLuke Lau <luke_lau@icloud.com>
Sat, 9 Nov 2019 18:28:28 +0000 (18:28 +0000)
Now that the standard library is loaded into our process, we can finally
being to fill out our symbol resolver. The symbol resolver takes in a
symbol, and returns a pointer wrapped inside a JITSymbol if it can find
the address of it. Otherwise it returns an error.

You can use the symbol resolver to do much fancier stuff in OrcJIT, but
for now we are just going to look up symbols that we have previously
loaded into our process via loadLibraryPermanently.

Main.hs

diff --git a/Main.hs b/Main.hs
index c2fff06f82a811a3cc789fc37a3836fb70fa5d26..64d73e60e0a059864222aec09f8b2ad0803adcfd 100644 (file)
--- a/Main.hs
+++ b/Main.hs
@@ -26,6 +26,7 @@ import LLVM.OrcJIT.CompileLayer
 import LLVM.PassManager
 import LLVM.Pretty
 import LLVM.Target
+import Numeric
 import System.IO
 import System.IO.Error
 import Text.Read (readMaybe)
@@ -53,7 +54,10 @@ main = do
 
 -- This can eventually be used to resolve external functions, e.g. a stdlib call
 symResolver :: MangledSymbol -> IO (Either JITSymbolError JITSymbol)
-symResolver sym = undefined
+symResolver sym = do
+  ptr <- getSymbolAddressInProcess sym
+  putStrLn $ "Resolving " <> show sym <> " to 0x" <> showHex ptr ""
+  return (Right (JITSymbol ptr defaultJITSymbolFlags))
 
 repl :: ModuleBuilderT (ReaderT JITEnv IO) ()
 repl = do