diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 00ba880cb..b6e0a1090 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,22 @@
+2013-06-18  Corinna Vinschen  <corinna@vinschen.de>
+
+	* Makefile.in (VPATH): Drop CONFIG_DIR.
+	(EXTRA_DLL_OFILES): Remove.
+	(DLL_OFILES): Remove EXTRA_DLL_OFILES.
+	(ASFLAGS): Define as -D_WIN64 on x86_64.
+	(GMON_OFILES): Add mcountFunc.o.
+	($(srcdir)/$(TLSOFFSETS_H)): Use target_cpu rather than CONFIG_DIR.
+	* configure.ac (CONFIG_DIR): Remove definition.
+	* configure: Regenerate.
+	* gcrt0.c: Use latest version from Mingw-w64 project.
+	* gmon.c: Ditto.
+	* gmon.h: Ditto.
+	* mcount.c: Ditto.
+	* mcountFunc.S: Ditto, new file.
+	* profil.c: Ditto.
+	* profil.h: Ditto.
+	* config: Remove entire directory.
+
 2013-06-17  Corinna Vinschen  <corinna@vinschen.de>
 
 	* path.cc (cnt_bs): New inline function.
diff --git a/winsup/cygwin/Makefile.in b/winsup/cygwin/Makefile.in
index acb327e69..76906c942 100644
--- a/winsup/cygwin/Makefile.in
+++ b/winsup/cygwin/Makefile.in
@@ -29,7 +29,7 @@ export CCWRAP_HEADERS:=. ${srcdir}
 export CCWRAP_SYSTEM_HEADERS:=@cygwin_headers@ @newlib_headers@
 export CCWRAP_DIRAFTER_HEADERS:=@windows_headers@
 
-VPATH+=$(CONFIG_DIR) $(srcdir)/regex $(srcdir)/lib $(srcdir)/libc
+VPATH+=$(srcdir)/regex $(srcdir)/lib $(srcdir)/libc
 
 target_cpu:=@target_cpu@
 target_alias:=@target_alias@
@@ -145,8 +145,6 @@ LIBCOS:=${sort ${addsuffix .o,${basename ${notdir ${wildcard $(srcdir)/lib/*.c}}
 
 # Build all source files in the config directory
 
-EXTRA_DLL_OFILES:=${addsuffix .o,${basename ${notdir ${wildcard $(CONFIG_DIR)/*.c}}}}
-
 EXTRA_OFILES:=
 
 MALLOC_OFILES:=malloc.o
@@ -179,7 +177,7 @@ DLL_OFILES:=advapi32.o arc4random.o assert.o autoload.o base64.o bsdlib.o ctype.
 	strfuncs.o strptime.o strsep.o strsig.o sync.o syscalls.o sysconf.o \
 	syslog.o termios.o thread.o timer.o times.o tls_pbuf.o tty.o uinfo.o \
 	uname.o wait.o wincap.o window.o winf.o wow64.o xsique.o \
-	$(EXTRA_DLL_OFILES) $(EXTRA_OFILES) $(MALLOC_OFILES) $(MT_SAFE_OBJECTS)
+	$(EXTRA_OFILES) $(MALLOC_OFILES) $(MT_SAFE_OBJECTS)
 
 EXCLUDE_STATIC_OFILES:=$(addprefix --exclude=,\
 	cygtls.o \
@@ -197,7 +195,12 @@ override EXTRA_OFILES=$(patsubst %.o,%_E,${DLL_OFILES}))
 override MALLOC_OFILES:=$(patsubst %.o,%.E,${MALLOC_OFILES})
 endif #PREPROCESS
 
-GMON_OFILES:=gmon.o mcount.o profil.o
+ifeq ($(target_cpu),x86_64)
+# Needed by mcountFunc.S to choose the right code path and symbol names
+ASFLAGS:=-D_WIN64
+endif
+
+GMON_OFILES:=gmon.o mcount.o profil.o mcountFunc.o
 
 ifeq ($(target_cpu),x86_64)
 NEW_FUNCTIONS:=
@@ -520,7 +523,7 @@ $(DEF_FILE): gendef $(srcdir)/$(TLSOFFSETS_H) $(DIN_FILE)
 	$(word 1,$^) --cpu=${target_cpu} --output-def=$@  --tlsoffsets=$(word 2,$^) $(wordlist 3,99,$^)
 
 $(srcdir)/$(TLSOFFSETS_H): gentls_offsets cygtls.h
-	$^ $@ @CONFIG_DIR@ $(COMPILE.cc) -c
+	$^ $@ $(target_cpu) $(COMPILE.cc) -c
 
 sigfe.s: $(DEF_FILE)
 	@[ -s $@ ] || \
diff --git a/winsup/cygwin/config/i386/profile.h b/winsup/cygwin/config/i386/profile.h
deleted file mode 100644
index 8a9d11ab8..000000000
--- a/winsup/cygwin/config/i386/profile.h
+++ /dev/null
@@ -1,83 +0,0 @@
-/*	$NetBSD: profile.h,v 1.6 1995/03/28 18:17:08 jtc Exp $	*/
-
-/*
- * Copyright (c) 1992, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *	@(#)profile.h	8.1 (Berkeley) 6/11/93
- */
-
-/*
- * This file is taken from Cygwin distribution. Please keep it in sync.
- * The differences should be within __MINGW32__ guard.
- */
-/* If compiler doesn't inline, at least avoid passing args on the stack. */
-#define _MCOUNT_CALL __attribute__ ((regparm (2)))
-#define _MCOUNT_DECL static __inline__  void _MCOUNT_CALL _mcount
-
-/* FIXME: This works, but it would be cleaner to convert mcount into an
-   assembler stub that calls an extern  _mcount.
-   Older versions of GCC (pre-4.1) will still fail with regparm since the
-   compiler used %edx to store an unneeded counter variable.  */
-
-#define	MCOUNT \
-void									\
-mcount()								\
-{									\
-	u_long selfpc, frompcindex;					\
-	/*								\
-	 * Save registers, since this may be called from		\
-	 * the prologue of a regparm function.				\
-	 */								\
-	__asm __volatile__ ("pushl %eax\n\t"				\
-			    "pushl %ecx\n\t"				\
-		 	    "pushl %edx");				\
-	/*								\
-	 * find the return address for mcount,				\
-	 * and the return address for mcount's caller.			\
-	 *								\
-	 * selfpc = pc pushed by mcount call				\
-	 */								\
-	/* __asm  ("movl 4(%%ebp),%0" : "=r" (selfpc));	*/		\
-	selfpc = (u_long) __builtin_return_address (0);			\
-	/*								\
-	 * frompcindex = pc pushed by call into self.			\
-	 */								\
-	/*  __asm ("movl (%%ebp),%0;movl 4(%0),%0" : "=r" (frompcindex)); */  \
-	frompcindex = (u_long) __builtin_return_address (1);		\
-	_mcount(frompcindex, selfpc);					\
-	/*								\
-	 * Restore registers.						\
-	 */								\
-	__asm __volatile__ ("popl %edx\n\t"				\
-			    "popl %ecx\n\t"				\
-			    "popl %eax");				\
-}
diff --git a/winsup/cygwin/configure b/winsup/cygwin/configure
index e60f85846..a81aea704 100755
--- a/winsup/cygwin/configure
+++ b/winsup/cygwin/configure
@@ -1,9 +1,11 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.69.
+# Generated by GNU Autoconf 2.68.
 #
 #
-# Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
+# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
+# 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+# Foundation, Inc.
 #
 #
 # This configure script is free software; the Free Software Foundation
@@ -132,31 +134,6 @@ export LANGUAGE
 # CDPATH.
 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
 
-# Use a proper internal environment variable to ensure we don't fall
-  # into an infinite loop, continuously re-executing ourselves.
-  if test x"${_as_can_reexec}" != xno && test "x$CONFIG_SHELL" != x; then
-    _as_can_reexec=no; export _as_can_reexec;
-    # We cannot yet assume a decent shell, so we have to provide a
-# neutralization value for shells without unset; and this also
-# works around shells that cannot unset nonexistent variables.
-# Preserve -v and -x to the replacement shell.
-BASH_ENV=/dev/null
-ENV=/dev/null
-(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV
-case $- in # ((((
-  *v*x* | *x*v* ) as_opts=-vx ;;
-  *v* ) as_opts=-v ;;
-  *x* ) as_opts=-x ;;
-  * ) as_opts= ;;
-esac
-exec $CONFIG_SHELL $as_opts "$as_myself" ${1+"$@"}
-# Admittedly, this is quite paranoid, since all the known shells bail
-# out after a failed `exec'.
-$as_echo "$0: could not re-execute with $CONFIG_SHELL" >&2
-as_fn_exit 255
-  fi
-  # We don't want this to propagate to other subprocesses.
-          { _as_can_reexec=; unset _as_can_reexec;}
 if test "x$CONFIG_SHELL" = x; then
   as_bourne_compatible="if test -n \"\${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then :
   emulate sh
@@ -190,8 +167,7 @@ if ( set x; as_fn_ret_success y && test x = \"\$1\" ); then :
 else
   exitcode=1; echo positional parameters were not saved.
 fi
-test x\$exitcode = x0 || exit 1
-test -x / || exit 1"
+test x\$exitcode = x0 || exit 1"
   as_suggested="  as_lineno_1=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_1a=\$LINENO
   as_lineno_2=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_2a=\$LINENO
   eval 'test \"x\$as_lineno_1'\$as_run'\" != \"x\$as_lineno_2'\$as_run'\" &&
@@ -235,25 +211,21 @@ IFS=$as_save_IFS
 
 
       if test "x$CONFIG_SHELL" != x; then :
-  export CONFIG_SHELL
-             # We cannot yet assume a decent shell, so we have to provide a
-# neutralization value for shells without unset; and this also
-# works around shells that cannot unset nonexistent variables.
-# Preserve -v and -x to the replacement shell.
-BASH_ENV=/dev/null
-ENV=/dev/null
-(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV
-case $- in # ((((
-  *v*x* | *x*v* ) as_opts=-vx ;;
-  *v* ) as_opts=-v ;;
-  *x* ) as_opts=-x ;;
-  * ) as_opts= ;;
-esac
-exec $CONFIG_SHELL $as_opts "$as_myself" ${1+"$@"}
-# Admittedly, this is quite paranoid, since all the known shells bail
-# out after a failed `exec'.
-$as_echo "$0: could not re-execute with $CONFIG_SHELL" >&2
-exit 255
+  # We cannot yet assume a decent shell, so we have to provide a
+	# neutralization value for shells without unset; and this also
+	# works around shells that cannot unset nonexistent variables.
+	# Preserve -v and -x to the replacement shell.
+	BASH_ENV=/dev/null
+	ENV=/dev/null
+	(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV
+	export CONFIG_SHELL
+	case $- in # ((((
+	  *v*x* | *x*v* ) as_opts=-vx ;;
+	  *v* ) as_opts=-v ;;
+	  *x* ) as_opts=-x ;;
+	  * ) as_opts= ;;
+	esac
+	exec "$CONFIG_SHELL" $as_opts "$as_myself" ${1+"$@"}
 fi
 
     if test x$as_have_required = xno; then :
@@ -355,14 +327,6 @@ $as_echo X"$as_dir" |
 
 
 } # as_fn_mkdir_p
-
-# as_fn_executable_p FILE
-# -----------------------
-# Test if FILE is an executable regular file.
-as_fn_executable_p ()
-{
-  test -f "$1" && test -x "$1"
-} # as_fn_executable_p
 # as_fn_append VAR VALUE
 # ----------------------
 # Append the text in VALUE to the end of the definition contained in VAR. Take
@@ -484,10 +448,6 @@ as_cr_alnum=$as_cr_Letters$as_cr_digits
   chmod +x "$as_me.lineno" ||
     { $as_echo "$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&2; as_fn_exit 1; }
 
-  # If we had to re-execute with $CONFIG_SHELL, we're ensured to have
-  # already done that, so ensure we don't try to do so again and fall
-  # in an infinite loop.  This has already happened in practice.
-  _as_can_reexec=no; export _as_can_reexec
   # Don't try to exec as it changes $[0], causing all sort of problems
   # (the dirname of $[0] is not the place where we might find the
   # original and so on.  Autoconf is especially sensitive to this).
@@ -522,16 +482,16 @@ if (echo >conf$$.file) 2>/dev/null; then
     # ... but there are two gotchas:
     # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.
     # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.
-    # In both cases, we have to default to `cp -pR'.
+    # In both cases, we have to default to `cp -p'.
     ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||
-      as_ln_s='cp -pR'
+      as_ln_s='cp -p'
   elif ln conf$$.file conf$$ 2>/dev/null; then
     as_ln_s=ln
   else
-    as_ln_s='cp -pR'
+    as_ln_s='cp -p'
   fi
 else
-  as_ln_s='cp -pR'
+  as_ln_s='cp -p'
 fi
 rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file
 rmdir conf$$.dir 2>/dev/null
@@ -543,8 +503,28 @@ else
   as_mkdir_p=false
 fi
 
-as_test_x='test -x'
-as_executable_p=as_fn_executable_p
+if test -x / >/dev/null 2>&1; then
+  as_test_x='test -x'
+else
+  if ls -dL / >/dev/null 2>&1; then
+    as_ls_L_option=L
+  else
+    as_ls_L_option=
+  fi
+  as_test_x='
+    eval sh -c '\''
+      if test -d "$1"; then
+	test -d "$1/.";
+      else
+	case $1 in #(
+	-*)set "./$1";;
+	esac;
+	case `ls -ld'$as_ls_L_option' "$1" 2>/dev/null` in #((
+	???[sx]*):;;*)false;;esac;fi
+    '\'' sh
+  '
+fi
+as_executable_p=$as_test_x
 
 # Sed expression to map a string onto a valid CPP name.
 as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
@@ -585,7 +565,6 @@ ac_unique_file="Makefile.in"
 ac_no_link=no
 ac_subst_vars='LTLIBOBJS
 LIBOBJS
-CONFIG_DIR
 TLSOFFSETS_H
 DIN_FILE
 DEF_DLL_ENTRY
@@ -1150,6 +1129,8 @@ target=$target_alias
 if test "x$host_alias" != x; then
   if test "x$build_alias" = x; then
     cross_compiling=maybe
+    $as_echo "$as_me: WARNING: if you wanted to set the --build type, don't use --host.
+    If a cross compiler is detected then cross compile mode will be used" >&2
   elif test "x$build_alias" != "x$host_alias"; then
     cross_compiling=yes
   fi
@@ -1395,9 +1376,9 @@ test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
 configure
-generated by GNU Autoconf 2.69
+generated by GNU Autoconf 2.68
 
-Copyright (C) 2012 Free Software Foundation, Inc.
+Copyright (C) 2010 Free Software Foundation, Inc.
 This configure script is free software; the Free Software Foundation
 gives unlimited permission to copy, distribute and modify it.
 _ACEOF
@@ -1525,7 +1506,7 @@ This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
 It was created by $as_me, which was
-generated by GNU Autoconf 2.69.  Invocation command line was
+generated by GNU Autoconf 2.68.  Invocation command line was
 
   $ $0 $@
 
@@ -1971,7 +1952,7 @@ case $as_dir/ in #((
     # by default.
     for ac_prog in ginstall scoinst install; do
       for ac_exec_ext in '' $ac_executable_extensions; do
-	if as_fn_executable_p "$as_dir/$ac_prog$ac_exec_ext"; then
+	if { test -f "$as_dir/$ac_prog$ac_exec_ext" && $as_test_x "$as_dir/$ac_prog$ac_exec_ext"; }; then
 	  if test $ac_prog = install &&
 	    grep dspmsg "$as_dir/$ac_prog$ac_exec_ext" >/dev/null 2>&1; then
 	    # AIX install.  It has an incompatible calling convention.
@@ -2162,7 +2143,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_CC="${ac_tool_prefix}gcc"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -2202,7 +2183,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_CC="gcc"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -2255,7 +2236,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_CC="${ac_tool_prefix}cc"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -2296,7 +2277,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     if test "$as_dir/$ac_word$ac_exec_ext" = "/usr/ucb/cc"; then
        ac_prog_rejected=yes
        continue
@@ -2354,7 +2335,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_CC="$ac_tool_prefix$ac_prog"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -2398,7 +2379,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_CC="$ac_prog"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -2920,7 +2901,8 @@ cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 #include <stdarg.h>
 #include <stdio.h>
-struct stat;
+#include <sys/types.h>
+#include <sys/stat.h>
 /* Most of the following tests are stolen from RCS 5.7's src/conf.sh.  */
 struct buf { int x; };
 FILE * (*rcsopen) (struct buf *, struct stat *, int);
@@ -3033,7 +3015,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_CXX="$ac_tool_prefix$ac_prog"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3077,7 +3059,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_CXX="$ac_prog"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3482,7 +3464,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_AR="${ac_tool_prefix}ar"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3522,7 +3504,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_AR="ar"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3574,7 +3556,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_AS="${ac_tool_prefix}as"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3614,7 +3596,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_AS="as"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3666,7 +3648,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_DLLTOOL="${ac_tool_prefix}dlltool"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3706,7 +3688,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_DLLTOOL="dlltool"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3758,7 +3740,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_LD="${ac_tool_prefix}ld"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3798,7 +3780,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_LD="ld"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3850,7 +3832,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_NM="${ac_tool_prefix}nm"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3890,7 +3872,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_NM="nm"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3942,7 +3924,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_OBJCOPY="${ac_tool_prefix}objcopy"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -3982,7 +3964,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_OBJCOPY="objcopy"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4034,7 +4016,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_OBJDUMP="${ac_tool_prefix}objdump"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4074,7 +4056,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_OBJDUMP="objdump"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4126,7 +4108,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_RANLIB="${ac_tool_prefix}ranlib"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4166,7 +4148,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_RANLIB="ranlib"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4218,7 +4200,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_STRIP="${ac_tool_prefix}strip"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4258,7 +4240,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_STRIP="strip"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4310,7 +4292,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_WINDRES="${ac_tool_prefix}windres"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4350,7 +4332,7 @@ do
   IFS=$as_save_IFS
   test -z "$as_dir" && as_dir=.
     for ac_exec_ext in '' $ac_executable_extensions; do
-  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
     ac_cv_prog_ac_ct_WINDRES="windres"
     $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
@@ -4438,14 +4420,14 @@ case "$target_cpu" in
 		DEF_DLL_ENTRY="dll_entry@12"
 		DIN_FILE="i686.din"
 		TLSOFFSETS_H="tlsoffsets.h"
-		CONFIG_DIR="i386"  ;;
+		;;
    x86_64)
 		DLL_NAME="cygwin1.dll"
 		DLL_ENTRY="dll_entry"
 		DEF_DLL_ENTRY="dll_entry"
 		DIN_FILE="x86_64.din"
 		TLSOFFSETS_H="tlsoffsets64.h"
-		CONFIG_DIR="x86_64"  ;;
+		;;
    *)		as_fn_error $? "Invalid target processor \"$target_cpu\"" "$LINENO" 5 ;;
 esac
 
@@ -4466,7 +4448,6 @@ configure_args=$(/usr/bin/expr "$configure_args" : 'X \(.*\)')
 
 
 
-
 ac_config_files="$ac_config_files Makefile"
 
 cat >confcache <<\_ACEOF
@@ -4876,16 +4857,16 @@ if (echo >conf$$.file) 2>/dev/null; then
     # ... but there are two gotchas:
     # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.
     # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.
-    # In both cases, we have to default to `cp -pR'.
+    # In both cases, we have to default to `cp -p'.
     ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||
-      as_ln_s='cp -pR'
+      as_ln_s='cp -p'
   elif ln conf$$.file conf$$ 2>/dev/null; then
     as_ln_s=ln
   else
-    as_ln_s='cp -pR'
+    as_ln_s='cp -p'
   fi
 else
-  as_ln_s='cp -pR'
+  as_ln_s='cp -p'
 fi
 rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file
 rmdir conf$$.dir 2>/dev/null
@@ -4945,16 +4926,28 @@ else
   as_mkdir_p=false
 fi
 
-
-# as_fn_executable_p FILE
-# -----------------------
-# Test if FILE is an executable regular file.
-as_fn_executable_p ()
-{
-  test -f "$1" && test -x "$1"
-} # as_fn_executable_p
-as_test_x='test -x'
-as_executable_p=as_fn_executable_p
+if test -x / >/dev/null 2>&1; then
+  as_test_x='test -x'
+else
+  if ls -dL / >/dev/null 2>&1; then
+    as_ls_L_option=L
+  else
+    as_ls_L_option=
+  fi
+  as_test_x='
+    eval sh -c '\''
+      if test -d "$1"; then
+	test -d "$1/.";
+      else
+	case $1 in #(
+	-*)set "./$1";;
+	esac;
+	case `ls -ld'$as_ls_L_option' "$1" 2>/dev/null` in #((
+	???[sx]*):;;*)false;;esac;fi
+    '\'' sh
+  '
+fi
+as_executable_p=$as_test_x
 
 # Sed expression to map a string onto a valid CPP name.
 as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
@@ -4976,7 +4969,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 # values after options handling.
 ac_log="
 This file was extended by $as_me, which was
-generated by GNU Autoconf 2.69.  Invocation command line was
+generated by GNU Autoconf 2.68.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
   CONFIG_HEADERS  = $CONFIG_HEADERS
@@ -5038,10 +5031,10 @@ cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
 config.status
-configured by $0, generated by GNU Autoconf 2.69,
+configured by $0, generated by GNU Autoconf 2.68,
   with options \\"\$ac_cs_config\\"
 
-Copyright (C) 2012 Free Software Foundation, Inc.
+Copyright (C) 2010 Free Software Foundation, Inc.
 This config.status script is free software; the Free Software Foundation
 gives unlimited permission to copy, distribute and modify it."
 
@@ -5130,7 +5123,7 @@ fi
 _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 if \$ac_cs_recheck; then
-  set X $SHELL '$0' $ac_configure_args \$ac_configure_extra_args --no-create --no-recursion
+  set X '$SHELL' '$0' $ac_configure_args \$ac_configure_extra_args --no-create --no-recursion
   shift
   \$as_echo "running CONFIG_SHELL=$SHELL \$*" >&6
   CONFIG_SHELL='$SHELL'
diff --git a/winsup/cygwin/configure.ac b/winsup/cygwin/configure.ac
index f59a5ae32..4de46ef35 100644
--- a/winsup/cygwin/configure.ac
+++ b/winsup/cygwin/configure.ac
@@ -77,14 +77,14 @@ case "$target_cpu" in
 		DEF_DLL_ENTRY="dll_entry@12"
 		DIN_FILE="i686.din"
 		TLSOFFSETS_H="tlsoffsets.h"
-		CONFIG_DIR="i386"  ;;
+		;;
    x86_64)
 		DLL_NAME="cygwin1.dll"
 		DLL_ENTRY="dll_entry"
 		DEF_DLL_ENTRY="dll_entry"
 		DIN_FILE="x86_64.din"
 		TLSOFFSETS_H="tlsoffsets64.h"
-		CONFIG_DIR="x86_64"  ;;
+		;;
    *)		AC_MSG_ERROR(Invalid target processor \"$target_cpu\") ;;
 esac
 
@@ -95,5 +95,4 @@ AC_SUBST(DLL_ENTRY)
 AC_SUBST(DEF_DLL_ENTRY)
 AC_SUBST(DIN_FILE)
 AC_SUBST(TLSOFFSETS_H)
-AC_SUBST(CONFIG_DIR)
 AC_OUTPUT(Makefile)
diff --git a/winsup/cygwin/gcrt0.c b/winsup/cygwin/gcrt0.c
index 19a941676..87c7d36ad 100644
--- a/winsup/cygwin/gcrt0.c
+++ b/winsup/cygwin/gcrt0.c
@@ -1,6 +1,6 @@
 /* gcrt0.c
 
-   Copyright 1998, 1999, 2000, 2001 Red Hat, Inc.
+   Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -8,14 +8,22 @@ This software is a copyrighted work licensed under the terms of the
 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
 details. */
 
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
+
 #include <sys/types.h>
 #include <stdlib.h>
 
+#ifdef __MINGW32__
+#include <_bsd_types.h>
+#endif
+
 extern u_char etext asm ("etext");
 extern u_char eprol asm ("__eprol");
 extern void _mcleanup (void);
-extern void monstartup (u_long, u_long);
-
+extern void monstartup (size_t, size_t);
 void _monstartup (void) __attribute__((__constructor__));
 
 /* startup initialization for -pg support */
@@ -32,7 +40,7 @@ _monstartup (void)
   if (called++)
     return;
 
-  monstartup ((u_long) &eprol, (u_long) &etext);
+  monstartup ((size_t) &eprol, (size_t) &etext);
   atexit (&_mcleanup);
 }
 
diff --git a/winsup/cygwin/gmon.c b/winsup/cygwin/gmon.c
index 56f9440dd..139aa4482 100644
--- a/winsup/cygwin/gmon.c
+++ b/winsup/cygwin/gmon.c
@@ -10,10 +10,6 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -35,19 +31,39 @@
 static char rcsid[] = "$OpenBSD: gmon.c,v 1.8 1997/07/23 21:11:27 kstailey Exp $";
 #endif
 
-#include "winlean.h"
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <gmon.h>
-#include <stdlib.h>
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
 
-#include <profil.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <strings.h>
+#ifndef __MINGW32__
+#include <unistd.h>
+#include <sys/param.h>
+#endif
+#include <sys/types.h>
+#include "gmon.h"
+#include "profil.h"
 
 /* XXX needed? */
 //extern char *minbrk __asm ("minbrk");
 
-struct gmonparam _gmonparam = { GMON_PROF_OFF };
+#ifdef _WIN64
+#define MINUS_ONE_P (-1LL)
+#else
+#define MINUS_ONE_P (-1)
+#endif
+
+#ifdef __MINGW32__
+#include <string.h>
+#define bzero(ptr,size) memset (ptr, 0, size);
+#endif
+
+struct gmonparam _gmonparam = { GMON_PROF_OFF, NULL, 0, NULL, 0, NULL, 0, 0L,
+  0, 0, 0, 0};
 
 static int	s_scale;
 /* see profil(2) where this is describe (incorrectly) */
@@ -64,15 +80,15 @@ fake_sbrk(int size)
     if (rv)
       return rv;
     else
-      return (void *) -1;
+      return (void *) MINUS_ONE_P;
 }
 
+void monstartup (size_t, size_t);
+
 void
-monstartup(lowpc, highpc)
-	u_long lowpc;
-	u_long highpc;
+monstartup (size_t lowpc, size_t highpc)
 {
-	register int o;
+	register size_t o;
 	char *cp;
 	struct gmonparam *p = &_gmonparam;
 
@@ -94,13 +110,14 @@ monstartup(lowpc, highpc)
 	p->tossize = p->tolimit * sizeof(struct tostruct);
 
 	cp = fake_sbrk(p->kcountsize + p->fromssize + p->tossize);
-	if (cp == (char *)-1) {
+	if (cp == (char *)MINUS_ONE_P) {
 		ERR("monstartup: out of memory\n");
 		return;
 	}
-#ifdef notdef
+
+	/* zero out cp as value will be added there */
 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
-#endif
+
 	p->tos = (struct tostruct *)cp;
 	cp += p->tossize;
 	p->kcount = (u_short *)cp;
@@ -133,22 +150,21 @@ monstartup(lowpc, highpc)
 	moncontrol(1);
 }
 
+void _mcleanup (void);
 void
-_mcleanup()
+_mcleanup(void)
 {
+	static char gmon_out[] = "gmon.out";
 	int fd;
 	int hz;
 	int fromindex;
 	int endfrom;
-	u_long frompc;
+	size_t frompc;
 	int toindex;
 	struct rawarc rawarc;
 	struct gmonparam *p = &_gmonparam;
 	struct gmonhdr gmonhdr, *hdr;
-	char *proffile;
-#ifndef nope
-	char gmon_out[] = "gmon.out";
-#endif
+	const char *proffile;
 #ifdef DEBUG
 	int log, len;
 	char dbuf[200];
@@ -203,7 +219,7 @@ _mcleanup()
 
 		proffile = buf;
 	} else {
-		proffile = "gmon.out";
+		proffile = gmon_out;
 	}
 #else
 	proffile = gmon_out;
@@ -263,8 +279,7 @@ _mcleanup()
  *	all the data structures are ready.
  */
 void
-moncontrol(mode)
-	int mode;
+moncontrol(int mode)
 {
 	struct gmonparam *p = &_gmonparam;
 
diff --git a/winsup/cygwin/gmon.h b/winsup/cygwin/gmon.h
index be016791e..919a1f21c 100644
--- a/winsup/cygwin/gmon.h
+++ b/winsup/cygwin/gmon.h
@@ -13,10 +13,6 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -36,6 +32,11 @@
  *	@(#)gmon.h	8.2 (Berkeley) 1/4/94
  */
 
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
+
 #ifndef _SYS_GMON_H_
 #define _SYS_GMON_H_
 
@@ -45,12 +46,16 @@
 
 #include <profile.h>
 
+#ifdef __MINGW32__
+#include <_bsd_types.h>
+#endif /* __MINGW32__*/
+
 /*
  * Structure prepended to gmon.out profiling data file.
  */
 struct gmonhdr {
-	u_long	lpc;		/* base pc address of sample buffer */
-	u_long	hpc;		/* max pc address of sampled buffer */
+	size_t	lpc;		/* base pc address of sample buffer */
+	size_t	hpc;		/* max pc address of sampled buffer */
 	int	ncnt;		/* size of sample buffer (plus this header) */
 	int	version;	/* version number */
 	int	profrate;	/* profiling clock rate */
@@ -106,7 +111,7 @@ struct gmonhdr {
 #define MAXARCS		((1 << (8 * sizeof(HISTCOUNTER))) - 2)
 
 struct tostruct {
-	u_long	selfpc;
+	size_t	selfpc;
 	long	count;
 	u_short	link;
 	u_short pad;
@@ -117,8 +122,8 @@ struct tostruct {
  * the called site and a count.
  */
 struct rawarc {
-	u_long	raw_frompc;
-	u_long	raw_selfpc;
+	size_t	raw_frompc;
+	size_t	raw_selfpc;
 	long	raw_count;
 };
 
@@ -134,16 +139,16 @@ struct rawarc {
 struct gmonparam {
 	int		state;
 	u_short		*kcount;
-	u_long		kcountsize;
+	size_t		kcountsize;
 	u_short		*froms;
-	u_long		fromssize;
+	size_t		fromssize;
 	struct tostruct	*tos;
-	u_long		tossize;
+	size_t		tossize;
 	long		tolimit;
-	u_long		lowpc;
-	u_long		highpc;
-	u_long		textsize;
-	u_long		hashfraction;
+	size_t		lowpc;
+	size_t		highpc;
+	size_t		textsize;
+	size_t		hashfraction;
 };
 extern struct gmonparam _gmonparam;
 
diff --git a/winsup/cygwin/mcount.c b/winsup/cygwin/mcount.c
index 4f69864b3..fad672881 100644
--- a/winsup/cygwin/mcount.c
+++ b/winsup/cygwin/mcount.c
@@ -10,10 +10,6 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -35,8 +31,16 @@
 static char rcsid[] = "$OpenBSD: mcount.c,v 1.6 1997/07/23 21:11:27 kstailey Exp $";
 #endif
 
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
+
+#ifndef __MINGW32__
+#include <sys/param.h>
+#endif
 #include <sys/types.h>
-#include <gmon.h>
+#include "gmon.h"
 
 /*
  * mcount is called on entry to each function compiled with the profiling
@@ -53,9 +57,10 @@ static char rcsid[] = "$OpenBSD: mcount.c,v 1.6 1997/07/23 21:11:27 kstailey Exp
  * both frompcindex and frompc.  Any reasonable, modern compiler will
  * perform this optimization.
  */
-//_MCOUNT_DECL __P((u_long frompc, u_long selfpc));
-_MCOUNT_DECL(frompc, selfpc)	/* _mcount; may be static, inline, etc */
-	register u_long frompc, selfpc;
+/* _mcount; may be static, inline, etc */
+_MCOUNT_DECL (size_t, size_t);
+
+_MCOUNT_DECL (size_t frompc, size_t selfpc)
 {
 	register u_short *frompcindex;
 	register struct tostruct *top, *prevtop;
diff --git a/winsup/cygwin/mcountFunc.S b/winsup/cygwin/mcountFunc.S
new file mode 100644
index 000000000..e1b9136cf
--- /dev/null
+++ b/winsup/cygwin/mcountFunc.S
@@ -0,0 +1,115 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+#include <_mingw_mac.h>
+
+	.file "mcountFunc.S"
+	.text
+#ifdef _WIN64
+	.align 8
+#else
+	.align 4
+#endif
+
+/* gcc always assumes the mcount public symbol has a single leading underscore
+   for our target.  See gcc/config/i386.h; it isn't overridden in
+   config/i386/cygming.h or any other places for mingw */
+.globl _mcount
+        .def    _mcount;  .scl    2;      .type   32;     .endef
+_mcount:
+#ifndef _WIN64
+	push %ebp
+	mov %esp, %ebp
+	push %eax
+	push %ecx
+	push %edx
+	movl 4(%ebp),%edx
+	movl (%ebp),%eax
+	movl 4(%eax),%eax
+	push %edx
+	push %eax
+	call __MINGW_USYMBOL(_mcount_private)
+	add $8, %esp
+	pop %edx
+	pop %ecx
+	pop %eax
+	leave
+	ret
+#else
+	push %rbp
+	mov %rsp, %rbp
+	push %rax
+	push %rcx
+	push %rdx
+	push %r8
+	push %r9
+	push %r10
+	push %r11
+	movq 8(%rbp),%rdx
+	movq (%rbp),%rax
+	movq 8(%rax),%rcx
+	sub $40, %rsp
+	call __MINGW_USYMBOL(_mcount_private)
+	add $40, %rsp
+	pop %r11
+	pop %r10
+	pop %r9
+	pop %r8
+	pop %rdx
+	pop %rcx
+	pop %rax
+	leave
+	ret
+#endif
+
+/* gcc always assumes the mcount public symbol has a single leading underscore
+   for our target.  See gcc/config/i386.h; it isn't overridden in
+   config/i386/cygming.h or any other places for mingw.  This is the entry
+   for new prologue mechanism required for x64 seh calling convention.  */
+.globl __fentry__
+        .def    _mcount_top;  .scl    2;      .type   32;     .endef
+__fentry__:
+#ifndef _WIN64
+	push %ebp
+	mov %esp, %ebp
+	push %eax
+	push %ecx
+	push %edx
+	movl 4(%ebp),%edx
+	movl 8(%ebp),%eax
+	push %edx
+	push %eax
+	call __MINGW_USYMBOL(_mcount_private)
+	add $8, %esp
+	pop %edx
+	pop %ecx
+	pop %eax
+	leave
+	ret
+#else
+	push %rbp
+	mov %rsp, %rbp
+	push %rax
+	push %rcx
+	push %rdx
+	push %r8
+	push %r9
+	push %r10
+	push %r11
+	movq 8(%rbp),%rdx
+	movq 16(%rbp),%rcx
+	sub $40, %rsp
+	call __MINGW_USYMBOL(_mcount_private)
+	add $40, %rsp
+	pop %r11
+	pop %r10
+	pop %r9
+	pop %r8
+	pop %rdx
+	pop %rcx
+	pop %rax
+	leave
+	ret
+#endif
diff --git a/winsup/cygwin/profil.c b/winsup/cygwin/profil.c
index 4af7d3ad2..eb41c0844 100644
--- a/winsup/cygwin/profil.c
+++ b/winsup/cygwin/profil.c
@@ -1,6 +1,6 @@
 /* profil.c -- win32 profil.c equivalent
 
-   Copyright 1998, 1999, 2000, 2001, 2003, 2009, 2010, 2012 Red Hat, Inc.
+   Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
 
    This file is part of Cygwin.
 
@@ -8,11 +8,20 @@
    Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
    details. */
 
-#include "winlean.h"
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#include <stdio.h>
 #include <sys/types.h>
 #include <errno.h>
-
-#include <profil.h>
+#include <math.h>
+#include "profil.h"
 
 #define SLEEPTIME (1000 / PROF_HZ)
 
@@ -21,24 +30,25 @@ static struct profinfo prof;
 
 /* Get the pc for thread THR */
 
-static uintptr_t
+static size_t
 get_thrpc (HANDLE thr)
 {
   CONTEXT ctx;
-  uintptr_t pc;
+  size_t pc;
   int res;
 
   res = SuspendThread (thr);
   if (res == -1)
-    return (uintptr_t) -1;
+    return (size_t) - 1;
   ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
-  pc = (uintptr_t) -1;
-  if (GetThreadContext (thr, &ctx))
-#ifdef __x86_64__
-    pc = ctx.Rip;
-#else
+  pc = (size_t) - 1;
+  if (GetThreadContext (thr, &ctx)) {
+#ifndef _WIN64
     pc = ctx.Eip;
+#else
+    pc = ctx.Rip;
 #endif
+  }
   ResumeThread (thr);
   return pc;
 }
@@ -58,18 +68,17 @@ print_prof (struct profinfo *p)
 /* Everytime we wake up use the main thread pc to hash into the cell in the
    profile buffer ARG. */
 
-static DWORD CALLBACK
+static void CALLBACK profthr_func (LPVOID);
+
+static void CALLBACK
 profthr_func (LPVOID arg)
 {
   struct profinfo *p = (struct profinfo *) arg;
-  uintptr_t pc;
-  size_t idx;
-
-  SetThreadPriority(p->profthr, THREAD_PRIORITY_TIME_CRITICAL);
+  size_t pc, idx;
 
   for (;;)
     {
-      pc = (uintptr_t) get_thrpc (p->targthr);
+      pc = (size_t) get_thrpc (p->targthr);
       if (pc >= p->lowpc && pc < p->highpc)
 	{
 	  idx = PROFIDX (pc, p->lowpc, p->scale);
@@ -78,9 +87,10 @@ profthr_func (LPVOID arg)
 #if 0
       print_prof (p);
 #endif
-      Sleep (SLEEPTIME);
+      /* Check quit condition, WAIT_OBJECT_0 or WAIT_TIMEOUT */
+      if (WaitForSingleObject (p->quitevt, SLEEPTIME) == WAIT_OBJECT_0)
+	return;
     }
-  return 0;
 }
 
 /* Stop profiling to the profiling buffer pointed to by P. */
@@ -90,7 +100,8 @@ profile_off (struct profinfo *p)
 {
   if (p->profthr)
     {
-      TerminateThread (p->profthr, 0);
+      SignalObjectAndWait (p->quitevt, p->profthr, INFINITE, FALSE);
+      CloseHandle (p->quitevt);
       CloseHandle (p->profthr);
     }
   if (p->targthr)
@@ -114,14 +125,34 @@ profile_on (struct profinfo *p)
       return -1;
     }
 
-  p->profthr = CreateThread (0, 0, profthr_func, (void *) p, 0, &thrid);
-  if (!p->profthr)
+  p->quitevt = CreateEvent (NULL, TRUE, FALSE, NULL);
+
+  if (!p->quitevt)
     {
-      CloseHandle (p->targthr);
+      CloseHandle (p->quitevt);
       p->targthr = 0;
       errno = EAGAIN;
       return -1;
     }
+
+  p->profthr = CreateThread (0, 0, (DWORD (WINAPI *)(LPVOID)) profthr_func,
+                             (void *) p, 0, &thrid);
+
+  if (!p->profthr)
+    {
+      CloseHandle (p->targthr);
+      CloseHandle (p->quitevt);
+      p->targthr = 0;
+      errno = EAGAIN;
+      return -1;
+    }
+
+  /* Set profiler thread priority to highest to be sure that it gets the
+     processor as soon it request it (i.e. when the Sleep terminate) to get
+     the next data out of the profile. */
+
+  SetThreadPriority (p->profthr, THREAD_PRIORITY_TIME_CRITICAL);
+
   return 0;
 }
 
@@ -139,7 +170,7 @@ profile_on (struct profinfo *p)
  */
 int
 profile_ctl (struct profinfo * p, char *samples, size_t size,
-	     size_t offset, unsigned int scale)
+	     size_t offset, u_int scale)
 {
   size_t maxbin;
 
@@ -155,7 +186,7 @@ profile_ctl (struct profinfo * p, char *samples, size_t size,
       memset (samples, 0, size);
       memset (p, 0, sizeof *p);
       maxbin = size >> 1;
-      prof.counter = (uint16_t *) samples;
+      prof.counter = (u_short *) samples;
       prof.lowpc = offset;
       prof.highpc = PROFADDR (maxbin, offset, scale);
       prof.scale = scale;
@@ -171,7 +202,8 @@ profile_ctl (struct profinfo * p, char *samples, size_t size,
    The word pointed to by this address is incremented.  Buf is unused. */
 
 int
-profil (char *samples, size_t size, size_t offset, unsigned int scale)
+profil (char *samples, size_t size, size_t offset, u_int scale)
 {
   return profile_ctl (&prof, samples, size, offset, scale);
 }
+
diff --git a/winsup/cygwin/profil.h b/winsup/cygwin/profil.h
index a25b6edf4..f33591d09 100644
--- a/winsup/cygwin/profil.h
+++ b/winsup/cygwin/profil.h
@@ -1,6 +1,6 @@
 /* profil.h: gprof profiling header file
 
-   Copyright 1998, 1999, 2000, 2001, 2003, 2012 Red Hat, Inc.
+   Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -8,7 +8,10 @@ This software is a copyrighted work licensed under the terms of the
 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
 details. */
 
-#include <stdint.h>
+/*
+ * This file is taken from Cygwin distribution. Please keep it in sync.
+ * The differences should be within __MINGW32__ guard.
+ */
 
 /* profiling frequency.  (No larger than 1000) */
 #define PROF_HZ			100
@@ -25,22 +28,28 @@ details. */
   })
 
 /* convert an index into an address */
-#define PROFADDR(idx, base, scale)	\
-	((base) + ((((unsigned long long)(idx) << 16) / (scale)) << 1))
+#define PROFADDR(idx, base, scale)		\
+  ((base)					\
+   + ((((unsigned long long)(idx) << 16)	\
+       / (unsigned long long)(scale)) << 1))
 
 /* convert a bin size into a scale */
 #define PROFSCALE(range, bins)		(((bins) << 16) / ((range) >> 1))
 
 typedef void *_WINHANDLE;
+#ifdef __MINGW32__
+#include <_bsd_types.h>
+#endif /* __MINGW32__*/
 
 struct profinfo {
     _WINHANDLE targthr;			/* thread to profile */
     _WINHANDLE profthr;			/* profiling thread */
-    uint16_t *counter;			/* profiling counters */
-    uintptr_t lowpc, highpc;		/* range to be profiled */
-    unsigned int scale;			/* scale value of bins */
+    _WINHANDLE quitevt;			/* quit event */
+    u_short *counter;			/* profiling counters */
+    size_t lowpc, highpc;		/* range to be profiled */
+    u_int scale;			/* scale value of bins */
 };
 
-int profile_ctl (struct profinfo *, char *, size_t, size_t, unsigned int);
-int profil (char *, size_t, size_t, unsigned int);
+int profile_ctl(struct profinfo *, char *, size_t, size_t, u_int);
+int profil(char *, size_t, size_t, u_int);