From 4b32fd0d43c3a8d45d802e03ede4bff7e96d033b Mon Sep 17 00:00:00 2001
From: Ulrich Weigand <weigand@informatik.uni-erlangen.de>
Date: Thu, 15 Jul 1999 14:51:40 +0000
Subject: [PATCH] Use position-independent code in assembly glue when building
 .so. Include if1632/ and graphics/win16drv/ into WineLib. Move
 miscemu/emulate.c into dlls/win87em/.

---
 Make.rules.in                       |   4 +-
 Makefile.in                         |  10 +-
 configure                           | 165 ++++++++++-----------
 configure.in                        |   5 +
 dlls/Makefile.in                    |   1 +
 dlls/win87em/.cvsignore             |   1 +
 dlls/win87em/Makefile.in            |  15 ++
 {miscemu => dlls/win87em}/emulate.c |   2 +
 graphics/win16drv/Makefile.in       |   2 +-
 if1632/Makefile.in                  |   4 +-
 memory/instr.c                      |   7 +-
 miscemu/Makefile.in                 |   1 -
 miscemu/main.c                      |   8 --
 relay32/Makefile.in                 |   2 +-
 tools/build.c                       | 213 +++++++++++++++++++---------
 15 files changed, 273 insertions(+), 167 deletions(-)
 create mode 100644 dlls/win87em/.cvsignore
 create mode 100644 dlls/win87em/Makefile.in
 rename {miscemu => dlls/win87em}/emulate.c (99%)

diff --git a/Make.rules.in b/Make.rules.in
index 20b74bb150e..c341ba4ce75 100644
--- a/Make.rules.in
+++ b/Make.rules.in
@@ -103,10 +103,10 @@ OBJS = $(C_SRCS:.c=.o) $(GEN_ASM_SRCS:.s=.o) $(ASM_SRCS:.S=.o) $(RC_SRCS:.rc=.o)
 	$(WRC) $(WRCFLAGS) $(WRCEXTRA) -bnh $<
 
 .spec.spec.c:
-	$(BUILD) -o $@ -spec $<
+	$(BUILD) @BUILDFLAGS@ -o $@ -spec $<
 
 .c.glue.s:
-	$(BUILD) -o $@ -glue $<
+	$(BUILD) @BUILDFLAGS@ -o $@ -glue $<
 
 # Rule to rebuild the resource compiler
 
diff --git a/Makefile.in b/Makefile.in
index a322cd7085b..40b483cd603 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -42,6 +42,7 @@ LIBSUBDIRS = \
 	dlls/tapi32 \
 	dlls/ver \
 	dlls/version \
+	dlls/win87em \
 	dlls/winaspi \
 	dlls/winspool \
 	dlls/wnaspi32 \
@@ -51,6 +52,8 @@ LIBSUBDIRS = \
 	graphics/metafiledrv \
 	graphics/psdrv \
 	graphics/ttydrv \
+	graphics/win16drv \
+	if1632 \
 	library \
 	loader \
 	loader/ne \
@@ -76,8 +79,6 @@ X11SUBDIRS = \
 
 EMUSUBDIRS = \
 	debugger \
-	graphics/win16drv \
-	if1632 \
 	miscemu
 
 PROGSUBDIRS = libtest programs
@@ -117,6 +118,7 @@ LIBOBJS = \
 	dlls/tapi32/tapi32.o \
 	dlls/ver/ver.o \
 	dlls/version/version.o \
+	dlls/win87em/win87em.o \
 	dlls/winaspi/winaspi.o \
 	dlls/winspool/winspool.o \
 	dlls/wnaspi32/wnaspi32.o \
@@ -126,6 +128,8 @@ LIBOBJS = \
 	graphics/metafiledrv/metafiledrv.o \
 	graphics/psdrv/psdrv.o \
 	graphics/ttydrv/ttydrv.o \
+	graphics/win16drv/win16drv.o \
+	if1632/if1632.o \
 	loader/loader.o \
 	loader/ne/ne.o \
 	loader/dos/dos.o \
@@ -150,8 +154,6 @@ X11OBJS = \
 
 EMUOBJS = \
 	debugger/debugger.o \
-	graphics/win16drv/win16drv.o \
-	if1632/if1632.o \
 	miscemu/miscemu.o
 
 LIB_TARGET  = @LIB_TARGET@
diff --git a/configure b/configure
index 8761a3f750f..1cd7392ad05 100755
--- a/configure
+++ b/configure
@@ -3819,25 +3819,26 @@ fi
 
 
 DLLFLAGS=""
+BUILDFLAGS=""
 LDSHARED=""
 if test "$LIB_TARGET" = "libwine.so.1.0"
 then
   echo $ac_n "checking "whether we can build a Linux dll"""... $ac_c" 1>&6
-echo "configure:3827: checking "whether we can build a Linux dll"" >&5
+echo "configure:3828: checking "whether we can build a Linux dll"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_dll_linux'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   saved_cflags=$CFLAGS
   CFLAGS="$CFLAGS -fPIC -shared -Wl,-soname,conftest.so.1.0"
   cat > conftest.$ac_ext <<EOF
-#line 3834 "configure"
+#line 3835 "configure"
 #include "confdefs.h"
 
 int main() {
 return 1
 ; return 0; }
 EOF
-if { (eval echo configure:3841: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:3842: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   ac_cv_c_dll_linux="yes"
 else
@@ -3854,25 +3855,26 @@ fi
 echo "$ac_t""$ac_cv_c_dll_linux" 1>&6
   if test "$ac_cv_c_dll_linux" = "yes"
   then
+    BUILDFLAGS="-pic"
     DLLFLAGS="-fPIC"
     LDSHARED="\$(CC) -shared -Wl,-soname,libwine.so"
   else
     echo $ac_n "checking "whether we can build a NetBSD dll"""... $ac_c" 1>&6
-echo "configure:3862: checking "whether we can build a NetBSD dll"" >&5
+echo "configure:3864: checking "whether we can build a NetBSD dll"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_dll_netbsd'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   saved_cflags=$CFLAGS
     CFLAGS="$CFLAGS -fPIC -Bshareable -Bforcearchive"
     cat > conftest.$ac_ext <<EOF
-#line 3869 "configure"
+#line 3871 "configure"
 #include "confdefs.h"
 
 int main() {
 return 1
 ; return 0; }
 EOF
-if { (eval echo configure:3876: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:3878: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   ac_cv_c_dll_netbsd="yes"
 else
@@ -3889,6 +3891,7 @@ fi
 echo "$ac_t""$ac_cv_c_dll_netbsd" 1>&6
     if test "$ac_cv_c_dll_netbsd" = "yes"
     then
+      BUILDFLAGS="-pic"
       DLLFLAGS="-fPIC"
       LDSHARED="ld -Bshareable -Bforcearchive"
     fi
@@ -3902,9 +3905,10 @@ fi
 
 
 
+
 wine_cv_libc_reentrant=no 
 echo $ac_n "checking "for reentrant libc: __errno_location"""... $ac_c" 1>&6
-echo "configure:3908: checking "for reentrant libc: __errno_location"" >&5
+echo "configure:3912: checking "for reentrant libc: __errno_location"" >&5
 if eval "test \"`echo '$''{'wine_cv_libc_r__errno_location'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -3912,14 +3916,14 @@ else
   wine_cv_libc_r__errno_location=yes 
 else
   cat > conftest.$ac_ext <<EOF
-#line 3916 "configure"
+#line 3920 "configure"
 #include "confdefs.h"
 int myerrno = 0;
 char buf[256];
 int *__errno_location(){return &myerrno;}
 main(){connect(0,buf,255); exit(!myerrno);}
 EOF
-if { (eval echo configure:3923: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:3927: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   wine_cv_libc_r__errno_location=yes
 else
@@ -3944,7 +3948,7 @@ EOF
     wine_cv_libc_reentrant=__errno_location 
 fi
 echo $ac_n "checking "for reentrant libc: __error"""... $ac_c" 1>&6
-echo "configure:3948: checking "for reentrant libc: __error"" >&5
+echo "configure:3952: checking "for reentrant libc: __error"" >&5
 if eval "test \"`echo '$''{'wine_cv_libc_r__error'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -3952,14 +3956,14 @@ else
   wine_cv_libc_r__error=yes 
 else
   cat > conftest.$ac_ext <<EOF
-#line 3956 "configure"
+#line 3960 "configure"
 #include "confdefs.h"
 int myerrno = 0;
 char buf[256];
 int *__error(){return &myerrno;}
 main(){connect(0,buf,255); exit(!myerrno);}
 EOF
-if { (eval echo configure:3963: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:3967: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   wine_cv_libc_r__error=yes
 else
@@ -3984,7 +3988,7 @@ EOF
     wine_cv_libc_reentrant=__error 
 fi
 echo $ac_n "checking "for reentrant libc: ___errno"""... $ac_c" 1>&6
-echo "configure:3988: checking "for reentrant libc: ___errno"" >&5
+echo "configure:3992: checking "for reentrant libc: ___errno"" >&5
 if eval "test \"`echo '$''{'wine_cv_libc_r___errno'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -3992,14 +3996,14 @@ else
   wine_cv_libc_r___errno=yes 
 else
   cat > conftest.$ac_ext <<EOF
-#line 3996 "configure"
+#line 4000 "configure"
 #include "confdefs.h"
 int myerrno = 0;
 char buf[256];
 int *___errno(){return &myerrno;}
 main(){connect(0,buf,255); exit(!myerrno);}
 EOF
-if { (eval echo configure:4003: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:4007: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   wine_cv_libc_r___errno=yes
 else
@@ -4035,7 +4039,7 @@ fi
 if test "$have_x" = "yes" -a "$wine_cv_libc_reentrant" != "no"
 then
 echo $ac_n "checking "for reentrant X libraries"""... $ac_c" 1>&6
-echo "configure:4039: checking "for reentrant X libraries"" >&5
+echo "configure:4043: checking "for reentrant X libraries"" >&5
 if eval "test \"`echo '$''{'wine_cv_x_reentrant'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -4083,19 +4087,19 @@ fi
 # The Ultrix 4.2 mips builtin alloca declared by alloca.h only works
 # for constant arguments.  Useless!
 echo $ac_n "checking for working alloca.h""... $ac_c" 1>&6
-echo "configure:4087: checking for working alloca.h" >&5
+echo "configure:4091: checking for working alloca.h" >&5
 if eval "test \"`echo '$''{'ac_cv_header_alloca_h'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4092 "configure"
+#line 4096 "configure"
 #include "confdefs.h"
 #include <alloca.h>
 int main() {
 char *p = alloca(2 * sizeof(int));
 ; return 0; }
 EOF
-if { (eval echo configure:4099: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:4103: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   ac_cv_header_alloca_h=yes
 else
@@ -4116,12 +4120,12 @@ EOF
 fi
 
 echo $ac_n "checking for alloca""... $ac_c" 1>&6
-echo "configure:4120: checking for alloca" >&5
+echo "configure:4124: checking for alloca" >&5
 if eval "test \"`echo '$''{'ac_cv_func_alloca_works'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4125 "configure"
+#line 4129 "configure"
 #include "confdefs.h"
 
 #ifdef __GNUC__
@@ -4144,7 +4148,7 @@ int main() {
 char *p = (char *) alloca(1);
 ; return 0; }
 EOF
-if { (eval echo configure:4148: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:4152: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   ac_cv_func_alloca_works=yes
 else
@@ -4176,12 +4180,12 @@ EOF
 
 
 echo $ac_n "checking whether alloca needs Cray hooks""... $ac_c" 1>&6
-echo "configure:4180: checking whether alloca needs Cray hooks" >&5
+echo "configure:4184: checking whether alloca needs Cray hooks" >&5
 if eval "test \"`echo '$''{'ac_cv_os_cray'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4185 "configure"
+#line 4189 "configure"
 #include "confdefs.h"
 #if defined(CRAY) && ! defined(CRAY2)
 webecray
@@ -4206,12 +4210,12 @@ echo "$ac_t""$ac_cv_os_cray" 1>&6
 if test $ac_cv_os_cray = yes; then
 for ac_func in _getb67 GETB67 getb67; do
   echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:4210: checking for $ac_func" >&5
+echo "configure:4214: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4215 "configure"
+#line 4219 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -4234,7 +4238,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:4238: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:4242: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -4261,7 +4265,7 @@ done
 fi
 
 echo $ac_n "checking stack direction for C alloca""... $ac_c" 1>&6
-echo "configure:4265: checking stack direction for C alloca" >&5
+echo "configure:4269: checking stack direction for C alloca" >&5
 if eval "test \"`echo '$''{'ac_cv_c_stack_direction'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -4269,7 +4273,7 @@ else
   ac_cv_c_stack_direction=0
 else
   cat > conftest.$ac_ext <<EOF
-#line 4273 "configure"
+#line 4277 "configure"
 #include "confdefs.h"
 find_stack_direction ()
 {
@@ -4288,7 +4292,7 @@ main ()
   exit (find_stack_direction() < 0);
 }
 EOF
-if { (eval echo configure:4292: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:4296: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_c_stack_direction=1
 else
@@ -4328,12 +4332,12 @@ for ac_func in \
 
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:4332: checking for $ac_func" >&5
+echo "configure:4336: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4337 "configure"
+#line 4341 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -4356,7 +4360,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:4360: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:4364: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -4418,17 +4422,17 @@ for ac_hdr in \
 do
 ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6
-echo "configure:4422: checking for $ac_hdr" >&5
+echo "configure:4426: checking for $ac_hdr" >&5
 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4427 "configure"
+#line 4431 "configure"
 #include "confdefs.h"
 #include <$ac_hdr>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:4432: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:4436: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -4455,12 +4459,12 @@ fi
 done
 
 echo $ac_n "checking whether stat file-mode macros are broken""... $ac_c" 1>&6
-echo "configure:4459: checking whether stat file-mode macros are broken" >&5
+echo "configure:4463: checking whether stat file-mode macros are broken" >&5
 if eval "test \"`echo '$''{'ac_cv_header_stat_broken'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4464 "configure"
+#line 4468 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -4513,12 +4517,12 @@ fi
 
 
 echo $ac_n "checking for working const""... $ac_c" 1>&6
-echo "configure:4517: checking for working const" >&5
+echo "configure:4521: checking for working const" >&5
 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4522 "configure"
+#line 4526 "configure"
 #include "confdefs.h"
 
 int main() {
@@ -4567,7 +4571,7 @@ ccp = (char const *const *) p;
 
 ; return 0; }
 EOF
-if { (eval echo configure:4571: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4575: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_const=yes
 else
@@ -4588,21 +4592,21 @@ EOF
 fi
 
 echo $ac_n "checking for inline""... $ac_c" 1>&6
-echo "configure:4592: checking for inline" >&5
+echo "configure:4596: checking for inline" >&5
 if eval "test \"`echo '$''{'ac_cv_c_inline'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_cv_c_inline=no
 for ac_kw in inline __inline__ __inline; do
   cat > conftest.$ac_ext <<EOF
-#line 4599 "configure"
+#line 4603 "configure"
 #include "confdefs.h"
 
 int main() {
 } $ac_kw foo() {
 ; return 0; }
 EOF
-if { (eval echo configure:4606: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4610: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_inline=$ac_kw; break
 else
@@ -4628,12 +4632,12 @@ EOF
 esac
 
 echo $ac_n "checking for ANSI C header files""... $ac_c" 1>&6
-echo "configure:4632: checking for ANSI C header files" >&5
+echo "configure:4636: checking for ANSI C header files" >&5
 if eval "test \"`echo '$''{'ac_cv_header_stdc'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4637 "configure"
+#line 4641 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 #include <stdarg.h>
@@ -4641,7 +4645,7 @@ else
 #include <float.h>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:4645: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:4649: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -4658,7 +4662,7 @@ rm -f conftest*
 if test $ac_cv_header_stdc = yes; then
   # SunOS 4.x string.h does not declare mem*, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 4662 "configure"
+#line 4666 "configure"
 #include "confdefs.h"
 #include <string.h>
 EOF
@@ -4676,7 +4680,7 @@ fi
 if test $ac_cv_header_stdc = yes; then
   # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 4680 "configure"
+#line 4684 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 EOF
@@ -4697,7 +4701,7 @@ if test "$cross_compiling" = yes; then
   :
 else
   cat > conftest.$ac_ext <<EOF
-#line 4701 "configure"
+#line 4705 "configure"
 #include "confdefs.h"
 #include <ctype.h>
 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
@@ -4708,7 +4712,7 @@ if (XOR (islower (i), ISLOWER (i)) || toupper (i) != TOUPPER (i)) exit(2);
 exit (0); }
 
 EOF
-if { (eval echo configure:4712: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:4716: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   :
 else
@@ -4732,12 +4736,12 @@ EOF
 fi
 
 echo $ac_n "checking for size_t""... $ac_c" 1>&6
-echo "configure:4736: checking for size_t" >&5
+echo "configure:4740: checking for size_t" >&5
 if eval "test \"`echo '$''{'ac_cv_type_size_t'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4741 "configure"
+#line 4745 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #if STDC_HEADERS
@@ -4765,7 +4769,7 @@ EOF
 fi
 
 echo $ac_n "checking size of long long""... $ac_c" 1>&6
-echo "configure:4769: checking size of long long" >&5
+echo "configure:4773: checking size of long long" >&5
 if eval "test \"`echo '$''{'ac_cv_sizeof_long_long'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -4773,7 +4777,7 @@ else
   ac_cv_sizeof_long_long=0
 else
   cat > conftest.$ac_ext <<EOF
-#line 4777 "configure"
+#line 4781 "configure"
 #include "confdefs.h"
 #include <stdio.h>
 main()
@@ -4784,7 +4788,7 @@ main()
   exit(0);
 }
 EOF
-if { (eval echo configure:4788: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:4792: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_sizeof_long_long=`cat conftestval`
 else
@@ -4808,12 +4812,12 @@ EOF
 if test "$ac_cv_header_sys_vfs_h" = "yes"
 then
     echo $ac_n "checking "whether sys/vfs.h defines statfs"""... $ac_c" 1>&6
-echo "configure:4812: checking "whether sys/vfs.h defines statfs"" >&5
+echo "configure:4816: checking "whether sys/vfs.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_vfs_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4817 "configure"
+#line 4821 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -4830,7 +4834,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:4834: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4838: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_vfs_has_statfs=yes
 else
@@ -4857,12 +4861,12 @@ fi
 if test "$ac_cv_header_sys_statfs_h" = "yes"
 then
     echo $ac_n "checking "whether sys/statfs.h defines statfs"""... $ac_c" 1>&6
-echo "configure:4861: checking "whether sys/statfs.h defines statfs"" >&5
+echo "configure:4865: checking "whether sys/statfs.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_statfs_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4866 "configure"
+#line 4870 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -4877,7 +4881,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:4881: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4885: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_statfs_has_statfs=yes
 else
@@ -4904,12 +4908,12 @@ fi
 if test "$ac_cv_header_sys_mount_h" = "yes"
 then
     echo $ac_n "checking "whether sys/mount.h defines statfs"""... $ac_c" 1>&6
-echo "configure:4908: checking "whether sys/mount.h defines statfs"" >&5
+echo "configure:4912: checking "whether sys/mount.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_mount_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4913 "configure"
+#line 4917 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -4924,7 +4928,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:4928: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4932: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_mount_has_statfs=yes
 else
@@ -4950,7 +4954,7 @@ fi
 
 
 echo $ac_n "checking "for statfs.f_bfree"""... $ac_c" 1>&6
-echo "configure:4954: checking "for statfs.f_bfree"" >&5
+echo "configure:4958: checking "for statfs.f_bfree"" >&5
 if eval "test \"`echo '$''{'wine_cv_statfs_bfree'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -4959,7 +4963,7 @@ else
         wine_cv_statfs_bfree=no
     else
     	cat > conftest.$ac_ext <<EOF
-#line 4963 "configure"
+#line 4967 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -4986,7 +4990,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:4990: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4994: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_statfs_bfree=yes
 else
@@ -5010,7 +5014,7 @@ EOF
 fi
 
 echo $ac_n "checking "for statfs.f_bavail"""... $ac_c" 1>&6
-echo "configure:5014: checking "for statfs.f_bavail"" >&5
+echo "configure:5018: checking "for statfs.f_bavail"" >&5
 if eval "test \"`echo '$''{'wine_cv_statfs_bavail'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5019,7 +5023,7 @@ else
         wine_cv_statfs_bavail=no
     else
     	cat > conftest.$ac_ext <<EOF
-#line 5023 "configure"
+#line 5027 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5046,7 +5050,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5050: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5054: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_statfs_bavail=yes
 else
@@ -5071,7 +5075,7 @@ fi
 
 
 echo $ac_n "checking "for working sigaltstack"""... $ac_c" 1>&6
-echo "configure:5075: checking "for working sigaltstack"" >&5
+echo "configure:5079: checking "for working sigaltstack"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_working_sigaltstack'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5080,7 +5084,7 @@ else
 
 else
   cat > conftest.$ac_ext <<EOF
-#line 5084 "configure"
+#line 5088 "configure"
 #include "confdefs.h"
 
 	#include <stdio.h>
@@ -5118,7 +5122,7 @@ else
 	}
 	
 EOF
-if { (eval echo configure:5122: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5126: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_c_working_sigaltstack="yes"
 else
@@ -5145,12 +5149,12 @@ fi
 
 
 echo $ac_n "checking "for msg_accrights in struct msghdr"""... $ac_c" 1>&6
-echo "configure:5149: checking "for msg_accrights in struct msghdr"" >&5
+echo "configure:5153: checking "for msg_accrights in struct msghdr"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_msg_accrights'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5154 "configure"
+#line 5158 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -5158,7 +5162,7 @@ int main() {
 struct msghdr hdr; hdr.msg_accrights=0
 ; return 0; }
 EOF
-if { (eval echo configure:5162: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5166: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_msg_accrights="yes"
 else
@@ -5181,12 +5185,12 @@ fi
 
 
 echo $ac_n "checking "whether we need to define __i386__"""... $ac_c" 1>&6
-echo "configure:5185: checking "whether we need to define __i386__"" >&5
+echo "configure:5189: checking "whether we need to define __i386__"" >&5
 if eval "test \"`echo '$''{'ac_cv_cpp_def_i386'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5190 "configure"
+#line 5194 "configure"
 #include "confdefs.h"
 #if (defined(i386) || defined(__i386)) && !defined(__i386__)
 yes
@@ -5343,6 +5347,7 @@ dlls/shell32/Makefile
 dlls/tapi32/Makefile
 dlls/ver/Makefile
 dlls/version/Makefile
+dlls/win87em/Makefile
 dlls/winaspi/Makefile
 dlls/winspool/Makefile
 dlls/wnaspi32/Makefile
@@ -5446,6 +5451,7 @@ s%@LN_S@%$LN_S%g
 s%@C2MAN@%$C2MAN%g
 s%@LDCONFIG@%$LDCONFIG%g
 s%@XLIB@%$XLIB%g
+s%@BUILDFLAGS@%$BUILDFLAGS%g
 s%@DLLFLAGS@%$DLLFLAGS%g
 s%@LDSHARED@%$LDSHARED%g
 s%@ALLOCA@%$ALLOCA%g
@@ -5513,6 +5519,7 @@ dlls/shell32/Makefile
 dlls/tapi32/Makefile
 dlls/ver/Makefile
 dlls/version/Makefile
+dlls/win87em/Makefile
 dlls/winaspi/Makefile
 dlls/winspool/Makefile
 dlls/wnaspi32/Makefile
diff --git a/configure.in b/configure.in
index d78f555f385..c3032872953 100644
--- a/configure.in
+++ b/configure.in
@@ -320,6 +320,7 @@ fi
 dnl **** Check for working dll ****
 
 DLLFLAGS=""
+BUILDFLAGS=""
 LDSHARED=""
 if test "$LIB_TARGET" = "libwine.so.1.0"
 then
@@ -332,6 +333,7 @@ then
   ])
   if test "$ac_cv_c_dll_linux" = "yes"
   then
+    BUILDFLAGS="-pic"
     DLLFLAGS="-fPIC"
     LDSHARED="\$(CC) -shared -Wl,-soname,libwine.so"
   else
@@ -344,6 +346,7 @@ then
     ])
     if test "$ac_cv_c_dll_netbsd" = "yes"
     then
+      BUILDFLAGS="-pic"
       DLLFLAGS="-fPIC"
       LDSHARED="ld -Bshareable -Bforcearchive"
     fi
@@ -353,6 +356,7 @@ then
     LIB_TARGET="libwine.a"
   fi
 fi
+AC_SUBST(BUILDFLAGS)
 AC_SUBST(DLLFLAGS)
 AC_SUBST(LDSHARED)
 
@@ -766,6 +770,7 @@ dlls/shell32/Makefile
 dlls/tapi32/Makefile
 dlls/ver/Makefile
 dlls/version/Makefile
+dlls/win87em/Makefile
 dlls/winaspi/Makefile
 dlls/winspool/Makefile
 dlls/wnaspi32/Makefile
diff --git a/dlls/Makefile.in b/dlls/Makefile.in
index 76cc186647d..3c9e4324d20 100644
--- a/dlls/Makefile.in
+++ b/dlls/Makefile.in
@@ -12,6 +12,7 @@ SUBDIRS = \
 	tapi32 \
 	ver \
 	version \
+	win87em \
 	winaspi \
 	wnaspi32
 
diff --git a/dlls/win87em/.cvsignore b/dlls/win87em/.cvsignore
new file mode 100644
index 00000000000..f3c7a7c5da6
--- /dev/null
+++ b/dlls/win87em/.cvsignore
@@ -0,0 +1 @@
+Makefile
diff --git a/dlls/win87em/Makefile.in b/dlls/win87em/Makefile.in
new file mode 100644
index 00000000000..62feebc7537
--- /dev/null
+++ b/dlls/win87em/Makefile.in
@@ -0,0 +1,15 @@
+DEFS      = @DLLFLAGS@ -D__WINE__
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+MODULE    = win87em
+
+C_SRCS = \
+	emulate.c
+
+all: $(MODULE).o
+
+@MAKE_RULES@
+
+### Dependencies:
diff --git a/miscemu/emulate.c b/dlls/win87em/emulate.c
similarity index 99%
rename from miscemu/emulate.c
rename to dlls/win87em/emulate.c
index 26384013ea1..15530431af0 100644
--- a/miscemu/emulate.c
+++ b/dlls/win87em/emulate.c
@@ -68,8 +68,10 @@ void WINAPI WIN87_fpmath( CONTEXT86 *context )
             /* I don't know much about asm() programming. This could be
              * wrong.
              */
+#ifdef __i386__
            __asm__ __volatile__("frndint");
            __asm__ __volatile__("fist %0;wait" : "=m" (dw) : : "memory");
+#endif
             TRACE("On top of stack is %ld\n",dw);
         }
         break;
diff --git a/graphics/win16drv/Makefile.in b/graphics/win16drv/Makefile.in
index 1f9cf229826..8d68ec5da15 100644
--- a/graphics/win16drv/Makefile.in
+++ b/graphics/win16drv/Makefile.in
@@ -1,4 +1,4 @@
-DEFS      = -D__WINE__
+DEFS      = @DLLFLAGS@ -D__WINE__
 TOPSRCDIR = @top_srcdir@
 TOPOBJDIR = ../..
 SRCDIR    = @srcdir@
diff --git a/if1632/Makefile.in b/if1632/Makefile.in
index 16227585b67..8dc4ff3c45a 100644
--- a/if1632/Makefile.in
+++ b/if1632/Makefile.in
@@ -1,4 +1,4 @@
-DEFS      = -D__WINE__
+DEFS      = @DLLFLAGS@ -D__WINE__
 TOPSRCDIR = @top_srcdir@
 TOPOBJDIR = ..
 SRCDIR    = @srcdir@
@@ -67,6 +67,6 @@ all: $(MODULE).o
 @MAKE_RULES@
 
 call16.s: $(BUILD)
-	$(BUILD) -o $@ -call16
+	$(BUILD) @BUILDFLAGS@ -o $@ -call16
 
 ### Dependencies:
diff --git a/memory/instr.c b/memory/instr.c
index 54162ea78c0..ac173b9e088 100644
--- a/memory/instr.c
+++ b/memory/instr.c
@@ -42,8 +42,6 @@ DECLARE_DEBUG_CHANNEL(io)
     (IS_SELECTOR_SYSTEM(SS_sig(context)) ? (void *)ESP_sig(context) : \
      (PTR_SEG_OFF_TO_LIN(SS_sig(context),STACK_sig(context)))))
 
-/* For invalid registers fixup */
-int (*INSTR_IsRelay)( const void *addr ) = NULL;
 
 /***********************************************************************
  *           INSTR_ReplaceSelector
@@ -58,8 +56,11 @@ int (*INSTR_IsRelay)( const void *addr ) = NULL;
  */
 static BOOL INSTR_ReplaceSelector( SIGCONTEXT *context, WORD *sel )
 {
+    extern char Call16_Start, Call16_End;
+
     if (IS_SELECTOR_SYSTEM(CS_sig(context)))
-        if (INSTR_IsRelay && INSTR_IsRelay( (void *)EIP_sig(context) ))
+        if (    (char *)EIP_sig(context) >= &Call16_Start 
+             && (char *)EIP_sig(context) <  &Call16_End   )
         {
             /* Saved selector may have become invalid when the relay code */
             /* tries to restore it. We simply clear it. */
diff --git a/miscemu/Makefile.in b/miscemu/Makefile.in
index 75542b4dfce..1030b0c3fda 100644
--- a/miscemu/Makefile.in
+++ b/miscemu/Makefile.in
@@ -6,7 +6,6 @@ VPATH     = @srcdir@
 MODULE    = miscemu
 
 C_SRCS = \
-	emulate.c \
 	main.c
 
 all: $(MODULE).o
diff --git a/miscemu/main.c b/miscemu/main.c
index bf6b90576da..8c8ce209097 100644
--- a/miscemu/main.c
+++ b/miscemu/main.c
@@ -23,13 +23,6 @@
 static int MAIN_argc;
 static char **MAIN_argv;
 
-extern int (*INSTR_IsRelay)( const void *addr );
-
-static int is_relay_addr( const void *addr )
-{
-    extern char Call16_Start, Call16_End;
-    return ((char *)addr >= &Call16_Start) && ((char *)addr < &Call16_End);
-}
 
 /***********************************************************************
  *           Emulator initialisation
@@ -149,7 +142,6 @@ int main( int argc, char *argv[] )
     MAIN_argc = argc; MAIN_argv = argv;
 
     /* Set up debugger hook */
-    INSTR_IsRelay = is_relay_addr;
     EXC_SetDebugEventHook( wine_debugger );
 
     if (Options.debug) 
diff --git a/relay32/Makefile.in b/relay32/Makefile.in
index c10e5dd192c..1b442975cf3 100644
--- a/relay32/Makefile.in
+++ b/relay32/Makefile.in
@@ -64,6 +64,6 @@ all: $(MODULE).o
 @MAKE_RULES@
 
 call32.s: $(BUILD)
-	$(BUILD) -o $@ -call32
+	$(BUILD) @BUILDFLAGS@ -o $@ -call32
 
 ### Dependencies:
diff --git a/tools/build.c b/tools/build.c
index 0845a56a56b..b10a5b44e9e 100644
--- a/tools/build.c
+++ b/tools/build.c
@@ -168,6 +168,8 @@ char *ParseNext;
 char ParseSaveChar;
 int Line;
 
+static int UsePIC = 0;
+
 static int debugging = 1;
 
   /* Offset of a structure field relative to the start of the struct */
@@ -870,6 +872,8 @@ static int BuildModule16( FILE *outfile, int max_code_offset,
     strcpy( pFileInfo->szPathName, DLLFileName );
     pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
         
+#ifdef __i386__  /* FIXME: Alignment problems! */
+
       /* Segment table */
 
     pSegment = (SEGTABLEENTRY *)pstr;
@@ -985,6 +989,7 @@ static int BuildModule16( FILE *outfile, int max_code_offset,
 	pstr += sizeof(ET_ENTRY);
     }
     *pstr++ = 0;
+#endif
 
       /* Dump the module content */
 
@@ -1852,13 +1857,14 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
     fprintf( outfile, "\tpushl %%ecx\n" );
     fprintf( outfile, "\tpushl %%edx\n" );
 
-#ifdef USE__PIC__
-    /* Get Global Offset Table into %ecx */
-    fprintf( outfile, "\tcall .LCallFrom16%s.getgot\n", name );
-    fprintf( outfile, ".LCallFrom16%s.getgot:\n", name );
-    fprintf( outfile, "\tpopl %%ecx\n" );
-    fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE+[.-.LCallFrom16%s.getgot], %%ecx\n" );
-#endif
+    if ( UsePIC )
+    {
+        /* Get Global Offset Table into %ecx */
+        fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
+        fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
+        fprintf( outfile, "\tpopl %%ecx\n" );
+        fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
+    }
 
     /* Load 32-bit segment registers */
     fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
@@ -1870,19 +1876,20 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
     fprintf( outfile, "\tdata16\n");
 #endif
     fprintf( outfile, "\tmovw %%dx, %%es\n" );
-#ifdef USE__PIC__
-    fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
-    fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
-#else
-    fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
-#endif
+
+    if ( UsePIC )
+    {
+        fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
+        fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
+    }
+    else
+        fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
 
     /* Get address of ldt_copy array into %ecx */
-#ifdef USE__PIC__
-    fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
-#else
-    fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
-#endif
+    if ( UsePIC )
+        fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
+    else
+        fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
 
     /* Translate STACK16FRAME base to flat offset in %edx */
     fprintf( outfile, "\tmovw %%ss, %%dx\n" );
@@ -1956,7 +1963,7 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
     /* Build register CONTEXT */
     if ( reg_func )
     {
-        fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT) );
+        fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
 
         fprintf( outfile, "\tpushfl\n" );
         fprintf( outfile, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags) );  
@@ -2001,6 +2008,17 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
     /* Print debug info before call */
     if ( debugging )
     {
+        if ( UsePIC )
+        {
+            fprintf( outfile, "\tpushl %%ebx\n" );
+
+            /* Get Global Offset Table into %ebx (for PLT call) */
+            fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
+            fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
+            fprintf( outfile, "\tpopl %%ebx\n" );
+            fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
+        }
+
         fprintf( outfile, "\tpushl %%ecx\n" );
         fprintf( outfile, "\tpushl %%edx\n" );
         if ( reg_func )
@@ -2008,14 +2026,18 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
         else
             fprintf( outfile, "\tpushl $0\n" );
-#if USE__PIC__
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
-#else
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
-#endif
+
+        if ( UsePIC )
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
+        else
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
+
         fprintf( outfile, "\tpopl %%edx\n" );
         fprintf( outfile, "\tpopl %%edx\n" );
         fprintf( outfile, "\tpopl %%ecx\n" );
+
+        if ( UsePIC )
+            fprintf( outfile, "\tpopl %%ebx\n" );
     }
 
     /* Call *Thunk* relay routine (which will call the API entry point) */
@@ -2024,19 +2046,34 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
     /* Print debug info after call */
     if ( debugging )
     {
+        if ( UsePIC )
+        {
+            fprintf( outfile, "\tpushl %%ebx\n" );
+
+            /* Get Global Offset Table into %ebx (for PLT call) */
+            fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
+            fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
+            fprintf( outfile, "\tpopl %%ebx\n" );
+            fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
+        }
+
         fprintf( outfile, "\tpushl %%eax\n" );
         if ( reg_func )
             fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
         else
             fprintf( outfile, "\tpushl $0\n" );
-#if USE__PIC__
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
-#else
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
-#endif
+
+        if ( UsePIC )
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
+        else
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
+
         fprintf( outfile, "\tpopl %%eax\n" );
         fprintf( outfile, "\tpopl %%eax\n" );
+
+        if ( UsePIC )
+            fprintf( outfile, "\tpopl %%ebx\n" );
     }
 
 
@@ -2083,6 +2120,9 @@ static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
         fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
 
+        /* Set flags according to return value */
+        fprintf( outfile, "\torl %%eax, %%eax\n" );
+
         /* Restore registers and return to *ThunkRet* routine */
         fprintf( outfile, "\tpopl %%edx\n" );
         fprintf( outfile, "\tpopl %%ecx\n" );
@@ -2144,24 +2184,24 @@ static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
     fprintf( outfile, "\tpushl %%esi\n" );
     fprintf( outfile, "\tpushl %%edi\n" );
 
-#ifdef USE__PIC__
-    /* Get Global Offset Table into %ebx */
-    fprintf( outfile, "\tcall .LCallTo16%s.getgot\n", name );
-    fprintf( outfile, ".LCallFrom16%s.getgot:\n", name );
-    fprintf( outfile, "\tpopl %%ebx\n" );
-    fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE+[.-.LCallFrom16%s.getgot], %%ebx\n" );
-#endif
+    if ( UsePIC )
+    {
+        /* Get Global Offset Table into %ebx */
+        fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
+        fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
+        fprintf( outfile, "\tpopl %%ebx\n" );
+        fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
+    }
 
     /* Move relay target address to %edi */
     if ( !reg_func )
         fprintf( outfile, "\tmovl %%eax, %%edi\n" );
 
     /* Enter Win16 Mutex */
-#ifdef USE__PIC__
-    fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
-#else
-    fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
-#endif
+    if ( UsePIC )
+        fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
+    else
+        fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
 
     /* Print debugging info */
     if (debugging)
@@ -2176,21 +2216,20 @@ static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
         fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
         fprintf( outfile, "\tpushl %%eax\n" );
 
-#ifdef USE__PIC__
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
-#else
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
-#endif
+        if ( UsePIC )
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
+        else
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
+
         fprintf( outfile, "\tpopl %%eax\n" );
         fprintf( outfile, "\tpopl %%eax\n" );
     }
 
     /* Get return address */
-#ifdef USE__PIC__
-    fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
-#else
-    fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
-#endif
+    if ( UsePIC )
+        fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
+    else
+        fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
 
     /* Call the actual CallTo16 routine (simulate a lcall) */
     fprintf( outfile, "\tpushl %%cs\n" );
@@ -2211,22 +2250,29 @@ static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
     else
         fprintf( outfile, "\tpushl %%eax\n" );
 
+    if ( UsePIC )
+    {
+        /* Get Global Offset Table into %ebx (might have been overwritten) */
+        fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
+        fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
+        fprintf( outfile, "\tpopl %%ebx\n" );
+        fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
+    }
+
     /* Print debugging info */
     if (debugging)
     {
-#ifdef USE__PIC__
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
-#else
-        fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
-#endif
+        if ( UsePIC )
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
+        else
+            fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
     }
 
     /* Leave Win16 Mutex */
-#ifdef USE__PIC__
-    fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
-#else
-    fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
-#endif
+    if ( UsePIC )
+        fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
+    else
+        fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
 
     /* Get return value */
     fprintf( outfile, "\tpopl %%eax\n" );
@@ -2791,7 +2837,7 @@ static void BuildCallFrom32Regs( FILE *outfile )
     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
 
-    fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT_FULL );
+    fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
 
     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
@@ -2895,6 +2941,8 @@ static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
     fprintf( outfile, "\t.text\n" );
 
+#ifdef __i386__
+
 #ifdef USE_STABS
     fprintf( outfile, "\t.file\t\"%s\"\n", outname );
     getcwd(buffer, sizeof(buffer));
@@ -2951,6 +2999,13 @@ static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
     fprintf( outfile, ".Letext:\n");
 #endif
 
+#else  /* __i386__ */
+
+    /* Just to avoid an empty file */
+    fprintf( outfile, "\t.long 0\n" );
+
+#endif  /* __i386__ */
+
     fclose( infile );
     return 0;
 }
@@ -2971,6 +3026,8 @@ static int BuildCall16( FILE *outfile, char * outname )
     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
     fprintf( outfile, "\t.text\n" );
 
+#ifdef __i386__
+
 #ifdef USE_STABS
     fprintf( outfile, "\t.file\t\"%s\"\n", outname );
     getcwd(buffer, sizeof(buffer));
@@ -3043,6 +3100,22 @@ static int BuildCall16( FILE *outfile, char * outname )
     fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
     fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
 
+#else  /* __i386__ */
+
+    fprintf( outfile, PREFIX"Call16_Start:\n" );
+    fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
+    fprintf( outfile, "\t.byte 0\n\n" );
+    fprintf( outfile, PREFIX"Call16_End:\n" );
+    fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
+
+    fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
+    fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
+    fprintf( outfile, "\t.byte 0\n\n" );
+    fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
+    fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
+
+#endif  /* __i386__ */
+
     return 0;
 }
 
@@ -3109,10 +3182,10 @@ static int BuildCall32( FILE *outfile, char * outname )
 static void usage(void)
 {
     fprintf( stderr,
-             "usage: build [-o outfile] -spec SPECNAMES\n"
-             "       build [-o outfile] -glue SOURCE_FILE\n"
-             "       build [-o outfile] -call16\n"
-             "       build [-o outfile] -call32\n" );
+             "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
+             "       build [-pic] [-o outfile] -glue SOURCE_FILE\n"
+             "       build [-pic] [-o outfile] -call16\n"
+             "       build [-pic] [-o outfile] -call32\n" );
     exit(1);
 }
 
@@ -3128,6 +3201,14 @@ int main(int argc, char **argv)
 
     if (argc < 2) usage();
 
+    if (!strcmp( argv[1], "-pic" ))
+    {
+        UsePIC = 1;
+        argv += 1;
+        argc -= 1;
+        if (argc < 2) usage();
+    }
+
     if (!strcmp( argv[1], "-o" ))
     {
         outname = argv[2];
-- 
GitLab