#! /bin/sh -e
 
# DP: biarch patches for i386/x86_64
 
dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p1 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p1 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0


diff -urN src/gcc/config/i386/biarch.h src-bak/gcc/config/i386/biarch.h
--- src/gcc/config/i386/biarch.h	1970-01-01 01:00:00.000000000 +0100
+++ src-bak/gcc/config/i386/biarch.h	2003-04-19 18:02:40.000000000 +0200
@@ -0,0 +1,25 @@
+/* Make configure files to produce biarch compiler defaulting to 64bit mode.
+   This file must be included very first, while the OS specific file later
+   to overwrite otherwise wrong defaults. 
+   Copyright (C) 2001 Free Software Foundation, Inc.
+   Contributed by Bo Thorsen <bo@suse.de>.
+
+This file is part of GNU CC.
+
+GNU CC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU CC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU CC; see the file COPYING.  If not, write to
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+#define TARGET_64BIT_DEFAULT 0
+#define TARGET_BI_ARCH 1
diff -urN src/gcc/config/i386/linux64.h src-bak/gcc/config/i386/linux64.h
--- src/gcc/config/i386/linux64.h	2003-03-14 08:33:46.000000000 +0100
+++ src-bak/gcc/config/i386/linux64.h	2003-04-19 18:17:05.000000000 +0200
@@ -21,8 +21,11 @@
 
 #define LINUX_DEFAULT_ELF
 
+#ifndef TARGET_VERSION
 #define TARGET_VERSION fprintf (stderr, " (x86-64 Linux/ELF)");
+#endif
 
+#undef TARGET_OS_CPP_BUILTINS
 #define TARGET_OS_CPP_BUILTINS()				\
   do								\
     {								\
@@ -62,6 +62,7 @@
    done.  */
 
 #undef	LINK_SPEC
+#if TARGET_64BIT_DEFAULT
 #define LINK_SPEC "%{!m32:-m elf_x86_64} %{m32:-m elf_i386} \
   %{shared:-shared} \
   %{!shared: \
@@ -70,6 +71,16 @@
       %{m32:%{!dynamic-linker:-dynamic-linker /lib/ld-linux.so.2}} \
       %{!m32:%{!dynamic-linker:-dynamic-linker /lib64/ld-linux-x86-64.so.2}}} \
     %{static:-static}}"
+#else
+#define LINK_SPEC "%{m64:-m elf_x86_64} %{!m64:-m elf_i386} \
+  %{shared:-shared} \
+  %{!shared: \
+    %{!static: \
+      %{rdynamic:-export-dynamic} \
+      %{!m64:%{!dynamic-linker:-dynamic-linker /lib/ld-linux.so.2}} \
+      %{m64:%{!dynamic-linker:-dynamic-linker /lib64/ld-linux-x86-64.so.2}}} \
+    %{static:-static}}"
+#endif
 
 #undef  STARTFILE_SPEC
 #define STARTFILE_SPEC \
@@ -82,7 +93,11 @@
 #undef  ENDFILE_SPEC
 #define ENDFILE_SPEC "%{!shared:crtend.o%s} %{shared:crtendS.o%s} crtn.o%s"
 
+#if TARGET_64BIT_DEFAULT
 #define MULTILIB_DEFAULTS { "m64" }
+#else
+#define MULTILIB_DEFAULTS { "m32" }
+#endif
 
 /* Do code reading to identify a signal frame, and set the frame
    state data appropriately.  See unwind-dw2.c for the structs.  
diff -urN src/gcc/config/i386/x86-64.h src-bak/gcc/config/i386/x86-64.h
--- src/gcc/config/i386/x86-64.h	2002-11-16 23:35:27.000000000 +0100
+++ src-bak/gcc/config/i386/x86-64.h	2003-04-19 18:02:40.000000000 +0200
@@ -49,7 +49,7 @@
 
 #undef ASM_SPEC
 #define ASM_SPEC "%{v:-V} %{Qy:} %{!Qn:-Qy} %{n} %{T} %{Ym,*} %{Yd,*} \
- %{Wa,*:%*} %{m32:--32}"
+ %{Wa,*:%*} %{m32:--32} %{m64:--64}"
 
 /* A C statement (sans semicolon) to output to the stdio stream
    FILE the assembler definition of uninitialized global DECL named
diff -urN src/gcc/config.gcc src-bak/gcc/config.gcc
--- src/gcc/config.gcc	2003-03-02 08:39:03.000000000 +0100
+++ src-bak/gcc/config.gcc	2003-04-19 18:10:34.000000000 +0200
@@ -1134,8 +1134,9 @@
 i[34567]86-*-linux*)	# Intel 80386's running GNU/Linux
 			# with ELF format using glibc 2
 			# aka GNU/Linux C library 6
-	tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h svr4.h linux.h i386/linux.h"
-	tmake_file="t-slibgcc-elf-ver t-linux i386/t-crtstuff"
+	tm_file="i386/biarch.h ${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h \
+		 svr4.h linux.h i386/linux.h i386/x86-64.h i386/linux64.h"
+	tmake_file="t-slibgcc-elf-ver t-linux i386/t-linux64"
 	;;
 x86_64-*-linux*)
 	tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h svr4.h linux.h \
--- src/gcc/config/i386/linux.h	2002-11-16 23:35:27.000000000 +0100
+++ src-64/gcc/config/i386/linux.h	2003-04-20 17:37:26.000000000 +0200
@@ -200,6 +200,7 @@
       }									\
   } while (0)
 
+#ifdef THIS_IS_SUPPORTED_ON_X86_64_AS_WELL
 /* Used by crtstuff.c to initialize the base of data-relative relocations.
    These are GOT relative on x86, so return the pic register.  */
 #ifdef __PIC__
@@ -219,6 +220,7 @@
 	   "addl\t$_GLOBAL_OFFSET_TABLE_+[.-.LPR%=],%0"			\
 	   : "=d"(BASE))
 #endif
+#endif
 
 /* Do code reading to identify a signal frame, and set the frame
    state data appropriately.  See unwind-dw2.c for the structs.  */
--- src/libffi/Makefile.am	2003-04-20 17:40:09.000000000 +0200
+++ src/libffi/Makefile.am.64	2003-04-20 19:06:04.000000000 +0200
@@ -88,7 +88,7 @@
 TARGET_SRC_MIPS_GCC = src/mips/ffi.c src/mips/o32.S src/mips/n32.S
 TARGET_SRC_MIPS_LINUX = src/mips/ffi.c src/mips/o32.S
 TARGET_SRC_MIPS_SGI = src/mips/ffi.c src/mips/o32.s src/mips/n32.s
-TARGET_SRC_X86 = src/x86/ffi.c src/x86/sysv.S
+TARGET_SRC_X86 = src/x86/ffi64.c src/x86/unix64.S src/x86/ffi.c src/x86/sysv.S
 TARGET_SRC_X86_WIN32 = src/x86/ffi.c src/x86/win32.S
 TARGET_SRC_SPARC = src/sparc/ffi.c src/sparc/v8.S src/sparc/v9.S
 TARGET_SRC_ALPHA = src/alpha/ffi.c src/alpha/osf.S
--- src/libffi/Makefile.in	2003-04-20 17:40:09.000000000 +0200
+++ src/libffi/Makefile.in.64	2003-04-20 19:06:45.000000000 +0200
@@ -167,7 +167,7 @@
 TARGET_SRC_MIPS_GCC = src/mips/ffi.c src/mips/o32.S src/mips/n32.S
 TARGET_SRC_MIPS_LINUX = src/mips/ffi.c src/mips/o32.S
 TARGET_SRC_MIPS_SGI = src/mips/ffi.c src/mips/o32.s src/mips/n32.s
-TARGET_SRC_X86 = src/x86/ffi.c src/x86/sysv.S
+TARGET_SRC_X86 = src/x86/ffi64.c src/x86/unix64.S src/x86/ffi.c src/x86/sysv.S
 TARGET_SRC_X86_WIN32 = src/x86/ffi.c src/x86/win32.S
 TARGET_SRC_SPARC = src/sparc/ffi.c src/sparc/v8.S src/sparc/v9.S
 TARGET_SRC_ALPHA = src/alpha/ffi.c src/alpha/osf.S
@@ -253,7 +253,7 @@
 @M68K_TRUE@src/m68k/ffi.lo src/m68k/sysv.lo
 @X86_TRUE@libffi_convenience_la_OBJECTS =  src/debug.lo src/prep_cif.lo \
 @X86_TRUE@src/types.lo src/raw_api.lo src/java_raw_api.lo \
-@X86_TRUE@src/x86/ffi.lo src/x86/sysv.lo
+@X86_TRUE@src/x86/ffi64.lo src/x86/unix64.lo src/x86/ffi.lo src/x86/sysv.lo
 @POWERPC_TRUE@libffi_convenience_la_OBJECTS =  src/debug.lo \
 @POWERPC_TRUE@src/prep_cif.lo src/types.lo src/raw_api.lo \
 @POWERPC_TRUE@src/java_raw_api.lo src/powerpc/ffi.lo \
@@ -296,8 +296,8 @@
 @IA64_TRUE@src/types.lo src/raw_api.lo src/java_raw_api.lo \
 @IA64_TRUE@src/ia64/ffi.lo src/ia64/unix.lo
 @X86_TRUE@libffi_la_OBJECTS =  src/debug.lo src/prep_cif.lo src/types.lo \
-@X86_TRUE@src/raw_api.lo src/java_raw_api.lo src/x86/ffi.lo \
-@X86_TRUE@src/x86/sysv.lo
+@X86_TRUE@src/raw_api.lo src/java_raw_api.lo src/x86/ffi64.lo src/x86/unix64.lo \
+@X86_TRUE@src/x86/ffi.lo src/x86/sysv.lo
 @POWERPC_TRUE@libffi_la_OBJECTS =  src/debug.lo src/prep_cif.lo \
 @POWERPC_TRUE@src/types.lo src/raw_api.lo src/java_raw_api.lo \
 @POWERPC_TRUE@src/powerpc/ffi.lo src/powerpc/sysv.lo \
--- src/libjava/include/i386-signal.h.orig	2003-04-20 22:33:21.000000000 +0200
+++ src/libjava/include/i386-signal.h	2003-04-20 22:42:55.000000000 +0200
@@ -11,6 +11,10 @@
 
 
 #ifndef JAVA_SIGNAL_H
+#ifdef __x86_64__
+#include "x86_64-signal.h"
+#else
+
 #define JAVA_SIGNAL_H 1
 
 #include <signal.h>
@@ -151,6 +155,6 @@
 
  * Finally, the code that glibc uses to return from a signal handler
  * is subject to change.  */
-
+#endif /* ! __x86_64__ */
 #endif /* JAVA_SIGNAL_H */
   
--- src/libjava/sysdep/i386/locks.h.orig	2002-05-02 00:47:07.000000000 +0200
+++ src/libjava/sysdep/i386/locks.h	2002-10-01 23:36:10.000000000 +0200
@@ -24,10 +24,17 @@
 					      obj_addr_t new_val) 
 {
   char result;
-  __asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
+#ifdef __x86_64__
+  __asm__ __volatile__("lock; cmpxchgq %2, %0; setz %1"
 	      : "+m"(*(addr)), "=q"(result)
 	      : "r" (new_val), "a"(old)
 	      : "memory");
+#else
+  __asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
+		       : "+m"(*(addr)), "=q"(result)
+		       : "r" (new_val), "a"(old)
+		       : "memory");
+#endif
   return (bool) result;
 }
 
--- src/libtool.m4.orig	2003-04-21 00:39:17.000000000 +0200
+++ src/libtool.m4	2003-04-21 00:39:50.000000000 +0200
@@ -189,14 +189,14 @@
   rm -rf conftest*
   ;;
 
-x86_64-*linux*|ppc*-*linux*|powerpc*-*linux*|s390*-*linux*|sparc*-*linux*)
+i[[3456]]86-*-linux*|x86_64-*linux*|ppc*-*linux*|powerpc*-*linux*|s390*-*linux*|sparc*-*linux*)
   # Find out which ABI we are using.
   echo 'int i;' > conftest.$ac_ext
   if AC_TRY_EVAL(ac_compile); then
     case "`/usr/bin/file conftest.o`" in
     *32-bit*)
       case $host in
-        x86_64-*linux*)
+        i[[3456]]86-*-linux*|x86_64-*linux*)
           LD="${LD-ld} -m elf_i386"
           ;;
         ppc64-*linux*)
@@ -212,7 +212,7 @@
       ;;
     *64-bit*)
       case $host in
-        x86_64-*linux*)
+        i[[3456]]86-*-linux*|x86_64-*linux*)
           LD="${LD-ld} -m elf_x86_64"
           ;;
         ppc*-*linux*|powerpc*-*linux*)
--- src/libjava/configure.in.orig	2003-04-20 20:59:00.000000000 +0200
+++ src/libjava/configure.in	2003-04-20 20:59:57.000000000 +0200
@@ -530,16 +530,17 @@
 
    AC_CHECK_LIB(dl, dladdr, [
      AC_DEFINE(HAVE_DLADDR)])
-   if test x"$build" = x"$host"; then
-     AC_CHECK_FILES(/proc/self/exe, [
-       AC_DEFINE(HAVE_PROC_SELF_EXE)])
-   else
-     case $host in
-     *-linux*)
-       AC_DEFINE(HAVE_PROC_SELF_EXE)
-       ;;
-     esac
-   fi
+   case $host in
+   *-linux*)
+     AC_DEFINE(HAVE_PROC_SELF_EXE)
+     ;;
+   *)
+     if test x"$build" = x"$host"; then
+       AC_CHECK_FILES(/proc/self/exe, [
+         AC_DEFINE(HAVE_PROC_SELF_EXE)])
+     fi
+     ;;
+   esac
 
    AM_ICONV
    AM_LC_MESSAGES
--- src/libjava/configure.orig	2003-04-20 21:01:30.000000000 +0200
+++ src/libjava/configure	2003-04-20 21:01:33.000000000 +0200
@@ -2475,14 +2475,14 @@
   rm -rf conftest*
   ;;
 
-x86_64-*linux*|ppc*-*linux*|powerpc*-*linux*|s390*-*linux*|sparc*-*linux*)
+i[3456]86-*-linux*|x86_64-*linux*|ppc*-*linux*|powerpc*-*linux*|s390*-*linux*|sparc*-*linux*)
   # Find out which ABI we are using.
   echo 'int i;' > conftest.$ac_ext
   if { (eval echo configure:2482: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
     case "`/usr/bin/file conftest.o`" in
     *32-bit*)
       case $host in
-        x86_64-*linux*)
+        i[3456]86-*-linux*|x86_64-*linux*)
           LD="${LD-ld} -m elf_i386"
           ;;
         ppc64-*linux*)
@@ -2498,7 +2498,7 @@
       ;;
     *64-bit*)
       case $host in
-        x86_64-*linux*)
+        i[3456]86-*-linux*|x86_64-*linux*)
           LD="${LD-ld} -m elf_x86_64"
           ;;
         ppc*-*linux*|powerpc*-*linux*)
@@ -4014,8 +4014,16 @@
   echo "$ac_t""no" 1>&6
 fi
 
-   if test x"$build" = x"$host"; then
-     for ac_file in /proc/self/exe
+   case $host in
+   *-linux*)
+     cat >> confdefs.h <<\EOF
+#define HAVE_PROC_SELF_EXE 1
+EOF
+
+     ;;
+   *)
+     if test x"$build" = x"$host"; then
+       for ac_file in /proc/self/exe
 do
 
 ac_safe=`echo "$ac_file" | sed 'y%./+-%__p_%'`
@@ -4051,16 +4059,9 @@
 fi
 done
 
-   else
-     case $host in
-     *-linux*)
-       cat >> confdefs.h <<\EOF
-#define HAVE_PROC_SELF_EXE 1
-EOF
-
-       ;;
-     esac
-   fi
+     fi
+     ;;
+   esac
 
    
     

