--- qemu-0.9.0/linux-user/path.c.nativelib	2004-09-13 23:39:32.000000000 +0200
+++ qemu-0.9.0/linux-user/path.c	2007-02-18 18:07:01.000000000 +0100
@@ -11,6 +11,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include "qemu.h"
+#include "nativelib.h"
 
 struct pathelem
 {
@@ -138,10 +139,18 @@ follow_path(const struct pathelem *curso
 /* Look for path in emulation dir, otherwise return name. */
 const char *path(const char *name)
 {
+    const char *p;
+
+    if (name[0] == '/' && (p = nativelib_path(name)) != NULL)
+	return p;
+
     /* Only do absolute paths: quick and dirty, but should mostly be OK.
        Could do relative by tracking cwd. */
     if (!base || name[0] != '/')
 	return name;
 
-    return follow_path(base, name) ?: name;
+    if ((p = follow_path(base, name)) != NULL)
+	return p;
+
+    return name;
 }
--- qemu-0.9.0/linux-user/qemu.h.nativelib	2006-11-19 21:29:35.000000000 +0100
+++ qemu-0.9.0/linux-user/qemu.h	2007-02-20 00:40:37.000000000 +0100
@@ -79,6 +79,7 @@ typedef struct TaskState {
     int sim_syscalls;
 #endif
     int used; /* non zero if used */
+    jmp_buf jmp_thread;
     struct image_info *info;
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
--- qemu-0.9.0/linux-user/main.c.nativelib	2007-02-18 14:40:33.000000000 +0100
+++ qemu-0.9.0/linux-user/main.c	2007-02-20 00:39:50.000000000 +0100
@@ -25,6 +25,7 @@
 #include <unistd.h>
 
 #include "qemu.h"
+#include "nativelib.h"
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
@@ -255,6 +256,12 @@ void cpu_loop(CPUX86State *env)
                 queue_signal(info.si_signo, &info);
             }
             break;
+	case EXCP_NATIVELIB:
+	    if (do_nativelib(env) == 0) {
+		env->eip = env->exception_next_eip;
+		break;
+	    }
+	    /* fall-through */
         case EXCP06_ILLOP:
             info.si_signo = SIGILL;
             info.si_errno = 0;
--- qemu-0.9.0/linux-user/syscall.c.nativelib	2007-02-13 14:41:12.000000000 +0100
+++ qemu-0.9.0/linux-user/syscall.c	2007-02-20 01:36:10.000000000 +0100
@@ -1720,8 +1720,7 @@ int do_fork(CPUState *env, unsigned int 
         ts->next = first_task_state;
         first_task_state = ts;
         /* we create a new CPU instance. */
-        new_env = cpu_init();
-        memcpy(new_env, env, sizeof(CPUState));
+        new_env = cpu_copy(env);
 #if defined(TARGET_I386)
         if (!newsp)
             newsp = env->regs[R_ESP];
--- qemu-0.9.0/target-i386/translate.c.nativelib	2007-01-16 20:28:58.000000000 +0100
+++ qemu-0.9.0/target-i386/translate.c	2007-02-18 17:27:43.000000000 +0100
@@ -5542,6 +5542,17 @@ static target_ulong disas_insn(DisasCont
             gen_eob(s);
         }
         break;
+    case 0x13c: /* nativelib */
+        val = ldub_code(s->pc++);
+        if (val == 0xb8) { /* mov <nativelib-id>,%eax */
+          s->pc += 4;
+          val = ldub_code(s->pc++);
+          if (val == 0xbb) { /* mov <nativelib-func>,%ebx */
+            s->pc += 4;
+            gen_interrupt(s, EXCP_NATIVELIB, pc_start - s->cs_base, s->pc - s->cs_base);
+          }
+        }
+        break;
 #ifdef TARGET_X86_64
     case 0x105: /* syscall */
         /* XXX: is it usable in real mode ? */
--- qemu-0.9.0/Makefile.target.nativelib	2007-02-18 14:40:33.000000000 +0100
+++ qemu-0.9.0/Makefile.target	2007-02-19 08:13:02.000000000 +0100
@@ -312,6 +312,19 @@ ifdef CONFIG_GDBSTUB
 OBJS+=gdbstub.o
 endif
 
+# native libraries bridge
+ifdef CONFIG_LINUX_USER
+native-libs = libpthread librt
+LIBS += -lpthread
+LIBOBJS += nativelib.o nativelib_helper.o
+LIBOBJS += $(foreach lib,$(native-libs),$(lib)-bridge.o)
+VPATH += $(SRC_PATH)/linux-user/nativelib
+VPATH += $(foreach lib,$(native-libs),$(SRC_PATH)/linux-user/nativelib/$(lib))
+CPPFLAGS += -I$(SRC_PATH)/linux-user/nativelib
+NATIVE_LIBS = $(foreach lib,$(native-libs),nativelib-$(lib).so)
+PROGS += $(NATIVE_LIBS)
+endif
+
 all: $(PROGS)
 
 $(QEMU_USER): $(OBJS)
@@ -592,3 +605,10 @@ audio.o sdlaudio.o dsoundaudio.o ossaudi
 fmodaudio.o alsaaudio.o mixeng.o sb16.o es1370.o gus.o adlib.o: \
 CFLAGS := $(CFLAGS) -Wall -Werror -W -Wsign-compare
 endif
+
+nativelib-%.o: %.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -nostdinc -fno-builtin -c $< -o $@
+nativelib-%.so: nativelib-%.o
+	$(CC) -nostdlib -shared $< -o $@ \
+		-Wl,-soname,`cat "$(patsubst nativelib-%.o,$(patsubst nativelib-%.o,$(SRC_PATH)/linux-user/nativelib/%,$<)/%.soname,$<)"` \
+		-Wl,--version-script,"$(patsubst nativelib-%.o,$(patsubst nativelib-%.o,$(SRC_PATH)/linux-user/nativelib/%,$<)/%.Version,$<)"
--- qemu-0.9.0/cpu-defs.h.nativelib	2006-11-12 21:40:55.000000000 +0100
+++ qemu-0.9.0/cpu-defs.h	2007-02-18 17:05:27.000000000 +0100
@@ -75,6 +75,7 @@ typedef unsigned long ram_addr_t;
 #define EXCP_HLT        0x10001 /* hlt instruction reached */
 #define EXCP_DEBUG      0x10002 /* cpu stopped after a breakpoint or singlestep */
 #define EXCP_HALTED     0x10003 /* cpu is halted (waiting for external event) */
+#define EXCP_NATIVELIB  0x10004 /* nativelib bridge requested */
 #define MAX_BREAKPOINTS 32
 
 #define TB_JMP_CACHE_BITS 12
--- qemu-0.9.0/configure.nativelib	2007-02-13 14:41:12.000000000 +0100
+++ qemu-0.9.0/configure	2007-02-18 17:29:47.000000000 +0100
@@ -645,6 +645,7 @@ echo "mandir=$mandir" >> $config_mak
 echo "datadir=$datadir" >> $config_mak
 echo "docdir=$docdir" >> $config_mak
 echo "#define CONFIG_QEMU_SHAREDIR \"$datadir\"" >> $config_h
+echo "#define CONFIG_QEMU_LIBDIR \"$prefix/lib/qemu\"" >> $config_h
 echo "MAKE=$make" >> $config_mak
 echo "INSTALL=$install" >> $config_mak
 echo "CC=$cc" >> $config_mak
--- qemu-0.9.0/exec.c.nativelib	2007-02-20 01:31:45.000000000 +0100
+++ qemu-0.9.0/exec.c	2007-02-20 01:36:32.000000000 +0100
@@ -1222,6 +1222,18 @@ void cpu_abort(CPUState *env, const char
     abort();
 }
 
+CPUState *cpu_copy(CPUState *env)
+{
+    CPUState *new_env = cpu_init();
+    /* preserve chaining and index */
+    CPUState *next_cpu = new_env->next_cpu;
+    int cpu_index = new_env->cpu_index;
+    memcpy(new_env, env, sizeof(CPUState));
+    new_env->next_cpu = next_cpu;
+    new_env->cpu_index = cpu_index;
+    return new_env;
+}
+
 #if !defined(CONFIG_USER_ONLY)
 
 /* NOTE: if flush_global is true, also flush global entries (not
--- qemu-0.9.0/cpu-all.h.nativelib	2007-02-18 14:40:33.000000000 +0100
+++ qemu-0.9.0/cpu-all.h	2007-02-20 01:34:53.000000000 +0100
@@ -760,6 +760,8 @@ void page_unprotect_range(target_ulong d
 
 #endif /* SINGLE_CPU_DEFINES */
 
+CPUState *cpu_copy(CPUState *env);
+
 void cpu_dump_state(CPUState *env, FILE *f, 
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags);
