#! /bin/sh -e

# DP: Fix the linker error when creating an xcc for ia64

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
        cd ${dir}gcc && autoconf
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p1 < $0
        cd ${dir}gcc && autoconf
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.orig/gcc/config/ia64/fde-glibc.c gcc-4.0.0.new/gcc/config/ia64/fde-glibc.c
--- gcc-4.0.0.orig/gcc/config/ia64/fde-glibc.c	2003-11-02 18:35:20.000000000 +0100
+++ gcc-4.0.0.new/gcc/config/ia64/fde-glibc.c	2005-05-15 02:42:27.675247674 +0200
@@ -31,6 +31,7 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE 1
 #endif
+#ifndef inhibit_libc
 #include "config.h"
 #include <stddef.h>
 #include <stdlib.h>
@@ -162,3 +163,5 @@ _Unwind_FindTableEntry (void *pc, unsign
 
   return data.ret;
 }
+
+#endif
diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.orig/gcc/config/ia64/unwind-ia64.c gcc-4.0.0.new/gcc/config/ia64/unwind-ia64.c
--- gcc-4.0.0.orig/gcc/config/ia64/unwind-ia64.c	2005-04-06 05:50:36.000000000 +0200
+++ gcc-4.0.0.new/gcc/config/ia64/unwind-ia64.c	2005-05-15 02:43:10.842199954 +0200
@@ -30,7 +30,7 @@
    This exception does not however invalidate any other reasons why
    the executable file might be covered by the GNU General Public License.  */
 
-
+#ifndef inhibit_libc
 #include "tconfig.h"
 #include "tsystem.h"
 #include "coretypes.h"
@@ -2404,3 +2404,4 @@ alias (_Unwind_SetIP);
 #endif
 
 #endif
+#endif
diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.orig/gcc/config/sh/linux-unwind.h gcc-4.0.0.new/gcc/config/sh/linux-unwind.h
--- gcc-4.0.0.orig/gcc/config/sh/linux-unwind.h	2005-02-11 02:12:57.000000000 +0100
+++ gcc-4.0.0.new/gcc/config/sh/linux-unwind.h	2005-05-15 02:44:31.000000000 +0200
@@ -29,6 +29,7 @@ Boston, MA 02111-1307, USA.  */
 /* Do code reading to identify a signal frame, and set the frame
    state data appropriately.  See unwind-dw2.c for the structs.  */
 
+#ifndef inhibit_libc
 #include <signal.h>
 #include <sys/ucontext.h>
 #include "insn-constants.h"
@@ -169,3 +170,4 @@ sh_fallback_frame_state (struct _Unwind_
   return _URC_NO_REASON;
 }
 #endif /* defined (__SH5__) */
+#endif
diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.orig/gcc/unwind-compat.c gcc-4.0.0.new/gcc/unwind-compat.c
--- gcc-4.0.0.orig/gcc/unwind-compat.c	2004-09-04 02:15:40.000000000 +0200
+++ gcc-4.0.0.new/gcc/unwind-compat.c	2005-05-15 02:41:17.000000000 +0200
@@ -29,6 +29,7 @@
    02110-1301, USA.  */
 
 #if defined (USE_GAS_SYMVER) && defined (USE_LIBUNWIND_EXCEPTIONS)
+#ifndef inhibit_libc
 #include "tconfig.h"
 #include "tsystem.h"
 #include "unwind.h"
@@ -204,3 +205,4 @@ _Unwind_SetIP (struct _Unwind_Context *c
 }
 symver (_Unwind_SetIP, GCC_3.0);
 #endif
+#endif
diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.orig/gcc/unwind-generic.h gcc-4.0.0.new/gcc/unwind-generic.h
--- gcc-4.0.0.orig/gcc/unwind-generic.h	2004-11-30 09:15:39.000000000 +0100
+++ gcc-4.0.0.new/gcc/unwind-generic.h	2005-05-15 02:38:33.000000000 +0200
@@ -202,6 +202,7 @@ extern _Unwind_Reason_Code _Unwind_SjLj_
    compatible with the standard ABI for IA-64, we inline these.  */
 
 #ifdef __ia64__
+#ifndef inhibit_libc
 #include <stdlib.h>
 
 static inline _Unwind_Ptr
@@ -220,6 +221,7 @@ _Unwind_GetTextRelBase (struct _Unwind_C
 
 /* @@@ Retrieve the Backing Store Pointer of the given context.  */
 extern _Unwind_Word _Unwind_GetBSP (struct _Unwind_Context *);
+#endif
 #else
 extern _Unwind_Ptr _Unwind_GetDataRelBase (struct _Unwind_Context *);
 extern _Unwind_Ptr _Unwind_GetTextRelBase (struct _Unwind_Context *);
diff -NurpP -x '*.orig' -x '*.libc' gcc-4.0.0.new/gcc/config/alpha/linux-unwind.h gcc-4.0.0.new2/gcc/config/alpha/linux-unwind.h
--- gcc-4.0.0.new/gcc/config/alpha/linux-unwind.h	2005-02-11 02:12:54.000000000 +0100
+++ gcc-4.0.0.new2/gcc/config/alpha/linux-unwind.h	2005-05-15 05:09:16.000000000 +0200
@@ -29,6 +29,7 @@ Boston, MA 02111-1307, USA.  */
 /* Do code reading to identify a signal frame, and set the frame
    state data appropriately.  See unwind-dw2.c for the structs.  */
 
+#ifndef inhibit_libc
 #include <signal.h>
 #include <sys/ucontext.h>
 
@@ -80,3 +81,5 @@ alpha_fallback_frame_state (struct _Unwi
   fs->retaddr_column = 64;
   return _URC_NO_REASON;
 }
+
+#endif
