#! /bin/sh -e

# DP: GNU/k*BSD support
# Author: Robert Millan <rmh@aybabtu.com>
# Status: Not yet submitted


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

# append the patch here and adjust the -p? flag in the patch calls.

diff -Nurd gcc-4.2.orig/gcc/config/i386/kfreebsd-gnu.h gcc-4.2/gcc/config/i386/kfreebsd-gnu.h
--- gcc-4.2.orig/gcc/config/i386/kfreebsd-gnu.h	2006-01-18 22:46:30.000000000 +0100
+++ gcc-4.2/gcc/config/i386/kfreebsd-gnu.h	2007-05-23 05:34:47.000000000 +0200
@@ -20,7 +20,29 @@
 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.  */
 
+#ifdef GLIBC_DYNAMIC_LINKER32
+#undef GLIBC_DYNAMIC_LINKER32
+#define GLIBC_DYNAMIC_LINKER32 "/lib/ld.so.1"
+#endif
+
+#ifdef GLIBC_DYNAMIC_LINKER64
+#undef GLIBC_DYNAMIC_LINKER64
+#define GLIBC_DYNAMIC_LINKER64 "/lib/ld-kfreebsd-x86-64.so.1"
+#endif
+
 #undef LINK_EMULATION
 #define LINK_EMULATION "elf_i386_fbsd"
+
+#ifdef LINK_EMULATION32
+#undef LINK_EMULATION32
+#define LINK_EMULATION32 LINK_EMULATION
+#endif
+
+#ifdef LINK_EMULATION64
+#undef LINK_EMULATION64
+#define LINK_EMULATION64 "elf_x86_64_fbsd"
+#endif
+
 #undef REG_NAME
 #define REG_NAME(reg) sc_ ## reg
+
diff -Nurd gcc-4.2.orig/gcc/config/i386/linux64.h gcc-4.2/gcc/config/i386/linux64.h
--- gcc-4.2.orig/gcc/config/i386/linux64.h	2007-05-22 22:25:11.000000000 +0200
+++ gcc-4.2/gcc/config/i386/linux64.h	2007-05-23 05:39:11.000000000 +0200
@@ -53,8 +53,11 @@
    When the -shared link option is used a final link is not being
    done.  */
 
+/* These macros may be overridden in k*bsd-gnu.h and i386/k*bsd-gnu.h. */
 #define GLIBC_DYNAMIC_LINKER32 "/lib/ld-linux.so.2"
 #define GLIBC_DYNAMIC_LINKER64 "/lib64/ld-linux-x86-64.so.2"
+#define LINK_EMULATION32 "elf_i386"
+#define LINK_EMULATION64 "elf_x86_64"
 
 #if TARGET_64BIT_DEFAULT
 #define SPEC_32 "m32"
@@ -65,7 +68,7 @@
 #endif
 
 #undef	LINK_SPEC
-#define LINK_SPEC "%{" SPEC_64 ":-m elf_x86_64} %{" SPEC_32 ":-m elf_i386} --hash-style=both \
+#define LINK_SPEC "%{" SPEC_64 ":-m " LINK_EMULATION64 "} %{" SPEC_32 ":-m " LINK_EMULATION32 "} --hash-style=both \
   %{shared:-shared} \
   %{!shared: \
     %{!static: \
diff -Nurd gcc-4.2.orig/gcc/config.gcc gcc-4.2/gcc/config.gcc
--- gcc-4.2.orig/gcc/config.gcc	2007-02-03 10:03:58.000000000 +0100
+++ gcc-4.2/gcc/config.gcc	2007-05-23 05:37:10.000000000 +0200
@@ -1102,7 +1102,7 @@
 	tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h svr4.h linux.h \
 		 i386/x86-64.h i386/linux64.h"
 	case ${target} in
-	x86_64-*-kfreebsd*-gnu) tm_file="${tm_file} kfreebsd-gnu.h" ;;
+	x86_64-*-kfreebsd*-gnu) tm_file="${tm_file} kfreebsd-gnu.h i386/kfreebsd-gnu.h" ;;
 	x86_64-*-knetbsd*-gnu) tm_file="${tm_file} knetbsd-gnu.h" ;;
 	esac
 	tmake_file="${tmake_file} i386/t-linux64 i386/t-crtfm t-dfprules"
