]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Make FFI stuff work well enough to get the C linker to load and
authorJulian Seward <jseward@acm.org>
Wed, 31 Mar 2004 23:25:47 +0000 (23:25 +0000)
committerJulian Seward <jseward@acm.org>
Wed, 31 Mar 2004 23:25:47 +0000 (23:25 +0000)
link some object modules.

git-svn-id: svn://svn.valgrind.org/vex/trunk@7

VEX/Linker.hs
VEX/Main.hs [new file with mode: 0644]
VEX/Makefile
VEX/linker.c

index 0f4bc7aa1aeb5d40d1acd28de38dbe99083cd4b0..f0a954fa8d8c0ea944eb6e35265854ead399f9e0 100644 (file)
@@ -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 (file)
index 0000000..7e3c7b2
--- /dev/null
@@ -0,0 +1,7 @@
+
+module Main where
+
+import Linker          ( linkObjects )
+
+
+test1 = linkObjects ["tests/test1.o"]
index 7ba37c554830ae3b2ac1d534954af525de1c8654..81b571df5ae1b0fb13195e65162a3141af21695d 100644 (file)
@@ -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
index 448a69586692b34a45065afe74d9ca6c34ad267e..93c393d1a9aa563b59ee7be9e74e7c9c271b623e 100644 (file)
@@ -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;
    }