From: Julian Seward Date: Wed, 31 Mar 2004 23:25:47 +0000 (+0000) Subject: Make FFI stuff work well enough to get the C linker to load and X-Git-Tag: svn/VALGRIND_3_0_1^2~1327 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fd53aa5ad8728922a31d825f842e67f74f9388e;p=thirdparty%2Fvalgrind.git Make FFI stuff work well enough to get the C linker to load and link some object modules. git-svn-id: svn://svn.valgrind.org/vex/trunk@7 --- diff --git a/VEX/Linker.hs b/VEX/Linker.hs index 0f4bc7aa1a..f0a954fa8d 100644 --- a/VEX/Linker.hs +++ b/VEX/Linker.hs @@ -1,8 +1,26 @@ -module Linker ( linker ) +module Linker ( linkObjects ) where -foreign import ccall "linker_top_level" linker_top_level :: Int -> IO Int +import Foreign.C.String +import Foreign.Ptr -linker = linker_top_level +foreign import ccall "linker_top_level_INIT" + linker_top_level_INIT :: IO () + +foreign import ccall "linker_top_level_ADD" + linker_top_level_ADD :: CString -> IO () + +foreign import ccall "linker_top_level_LINK" + linker_top_level_LINK :: IO (Ptr ()) + +linkObjects :: [String] -> IO (Ptr ()) +linkObjects objs + = do linker_top_level_INIT + mapM add_obj objs + main_addr <- linker_top_level_LINK + return main_addr + where + add_obj :: String -> IO () + add_obj obj_name = withCString obj_name linker_top_level_ADD diff --git a/VEX/Main.hs b/VEX/Main.hs new file mode 100644 index 0000000000..7e3c7b28a4 --- /dev/null +++ b/VEX/Main.hs @@ -0,0 +1,7 @@ + +module Main where + +import Linker ( linkObjects ) + + +test1 = linkObjects ["tests/test1.o"] diff --git a/VEX/Makefile b/VEX/Makefile index 7ba37c5548..81b571df5a 100644 --- a/VEX/Makefile +++ b/VEX/Makefile @@ -1,4 +1,4 @@ # Preps the FFI stuff (sigh) all: - ffihugs +G +L"linker.c" +L"-Darm_TARGET_ARCH" Linker.hs + ffihugs +G +L"linker.c" +L"-Darm_TARGET_ARCH" +L"-Wall" Linker.hs diff --git a/VEX/linker.c b/VEX/linker.c index 448a695866..93c393d1a9 100644 --- a/VEX/linker.c +++ b/VEX/linker.c @@ -1279,16 +1279,40 @@ int resolveObjs( void ) * Top-level linker. */ +#define N_OBJS 10 + +static char* object_names[N_OBJS]; +static int n_object_names = 0; + +/* Somewhat naff interface resulting from difficulties in + marshalling arrays of strings from Haskell to C. +*/ +void linker_top_level_INIT ( void ) +{ + n_object_names = 0; +} + +void linker_top_level_ADD ( char* obj_name ) +{ + if (n_object_names >= N_OBJS) { + fprintf(stderr, "linker: linker_top_level_ADD: too many objs"); + exit(1); + } + //fprintf(stderr, "ADD %d %s\n", n_object_names, obj_name); + object_names[n_object_names++] = strdup(obj_name); +} + /* Load and link a bunch of .o's, and return the address of 'main'. Or NULL if something borks. */ -void* linker_top_level ( int n_objs, char** object_names ) +void* linker_top_level_LINK ( void ) { int i, r; void* mainp; initLinker(); - for (i = 0; i < n_objs; i++) { + for (i = 0; i < n_object_names; i++) { + //fprintf(stderr, "linkloop %d %s\n", i, object_names[i] ); r = loadObj( object_names[i] ); if (r != 1) return NULL; }