diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 0997ccea8ac8bc84fd480cb9785f8a9c358ef14e..71ae9814260411d686b5976bac5a94f658d9f301 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,17 @@
+2011-01-26  Dave Korn  <dave.korn.cygwin@gmail.com>
+
+	PR target/40125
+	* config.gcc (i[34567]86-*-pe | i[34567]86-*-cygwin*): Select suitable
+	t-dlldir{,-x} fragment for build and add it to tmake_file.
+	(i[34567]86-*-mingw* | x86_64-*-mingw*): Likewise.
+	* Makefile.in (libgcc.mvars): Also export SHLIB_DLLDIR to libgcc.
+	* config/i386/t-dlldir: New file.
+	(SHLIB_DLLDIR): Define.
+	* config/i386/t-dlldir-x: New file.
+	(SHLIB_DLLDIR): Define.
+	* config/i386/t-cygming: Error out if SHLIB_DLLDIR is not set.
+	(SHLIB_INSTALL): Use it.
+
 2011-01-26  Chung-Lin Tang  <cltang@codesourcery.com>
 
 	PR target/47246
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 48b49e9fa517df9b86a773ecd701445977b27ca4..9a8262a0b74117af4896ae5327ec9b7ce6b98b4f 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1909,6 +1909,7 @@ libgcc.mvars: config.status Makefile $(LIB2ADD) $(LIB2ADD_ST) specs \
 	echo GCC_EXTRA_PARTS = '$(GCC_EXTRA_PARTS)' >> tmp-libgcc.mvars
 	echo SHLIB_LINK = '$(subst $(GCC_FOR_TARGET),$$(GCC_FOR_TARGET),$(SHLIB_LINK))' >> tmp-libgcc.mvars
 	echo SHLIB_INSTALL = '$(SHLIB_INSTALL)' >> tmp-libgcc.mvars
+	echo SHLIB_DLLDIR = '$(SHLIB_DLLDIR)' >> tmp-libgcc.mvars
 	echo SHLIB_EXT = '$(SHLIB_EXT)' >> tmp-libgcc.mvars
 	echo SHLIB_MKMAP = '$(call srcdirify,$(SHLIB_MKMAP))' >> tmp-libgcc.mvars
 	echo SHLIB_MKMAP_OPTS = '$(SHLIB_MKMAP_OPTS)' >> tmp-libgcc.mvars
diff --git a/gcc/config.gcc b/gcc/config.gcc
index 0f6aae17717d661e3a51eb6312fd601f2e8b5a72..516c41f9bdf76cc4ed48d19f6539ff4a574ae916 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1424,7 +1424,13 @@ i[34567]86-*-pe | i[34567]86-*-cygwin*)
 	else
 		tmake_eh_file="i386/t-sjlj-eh"
 	fi
-	tmake_file="${tmake_file} ${tmake_eh_file} i386/t-cygming i386/t-cygwin"
+	# Shared libgcc DLL install dir depends on cross/native build.
+	if test x${host} = x${target} ; then
+		tmake_dlldir_file="i386/t-dlldir"
+	else
+		tmake_dlldir_file="i386/t-dlldir-x"
+	fi
+	tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file} i386/t-cygming i386/t-cygwin"
 	target_gtfiles="\$(srcdir)/config/i386/winnt.c"
 	extra_options="${extra_options} i386/cygming.opt"
 	extra_objs="winnt.o winnt-stubs.o"
@@ -1480,7 +1486,13 @@ i[34567]86-*-mingw* | x86_64-*-mingw*)
 	else
 		tmake_eh_file="i386/t-sjlj-eh"
 	fi
-	tmake_file="${tmake_file} ${tmake_eh_file} i386/t-cygming t-dfprules"
+	# Shared libgcc DLL install dir depends on cross/native build.
+	if test x${host} = x${target} ; then
+		tmake_dlldir_file="i386/t-dlldir"
+	else
+		tmake_dlldir_file="i386/t-dlldir-x"
+	fi
+	tmake_file="${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file} i386/t-cygming t-dfprules"
         case ${target} in
                x86_64-w64-*)
                		tmake_file="${tmake_file} i386/t-mingw-w64"
diff --git a/gcc/config/i386/t-cygming b/gcc/config/i386/t-cygming
index 6d54e4971a6f6ec18df389d65bfe3e66da2d2f95..ccae237d3015617728f569aa3b14d7eeaaaa9694 100644
--- a/gcc/config/i386/t-cygming
+++ b/gcc/config/i386/t-cygming
@@ -72,6 +72,11 @@ SHLIB_MAP = @shlib_map_file@
 SHLIB_OBJS = @shlib_objs@
 SHLIB_DIR = @multilib_dir@/shlib
 SHLIB_SLIBDIR_QUAL = @shlib_slibdir_qual@
+# SHLIB_DLLDIR is defined by including one of either t-dlldir or t-dlldir-x
+# (native/cross build respectively) in the tmake_file list in gcc/config.gcc.
+ifndef SHLIB_DLLDIR
+$(error SHLIB_DLLDIR must be defined)
+endif
 
 SHLIB_LINK = $(LN_S) -f $(SHLIB_MAP) $(SHLIB_MAP).def && \
 	if [ ! -d $(SHLIB_DIR) ]; then \
@@ -91,9 +96,10 @@ SHLIB_LINK = $(LN_S) -f $(SHLIB_MAP) $(SHLIB_MAP).def && \
 # $(slibdir) double quoted to protect it from expansion while building
 # libgcc.mk.  We want this delayed until actual install time.
 SHLIB_INSTALL = \
-	$$(mkinstalldirs) $$(DESTDIR)$$(slibdir)$(SHLIB_SLIBDIR_QUAL); \
+	$$(mkinstalldirs) $$(DESTDIR)$$(SHLIB_DLLDIR) \
+	  $$(DESTDIR)$$(slibdir)$(SHLIB_SLIBDIR_QUAL); \
 	$(INSTALL) $(SHLIB_DIR)/$(SHLIB_SONAME) \
-	  $$(DESTDIR)$$(bindir)/$(SHLIB_SONAME); \
+	  $$(DESTDIR)$$(SHLIB_DLLDIR)/$(SHLIB_SONAME); \
 	$(INSTALL_DATA) $(SHLIB_DIR)/$(SHLIB_IMPLIB) \
 	  $$(DESTDIR)$$(slibdir)$(SHLIB_SLIBDIR_QUAL)/$(SHLIB_IMPLIB)
 SHLIB_MKMAP = $(srcdir)/mkmap-flat.awk
diff --git a/gcc/config/i386/t-dlldir b/gcc/config/i386/t-dlldir
new file mode 100644
index 0000000000000000000000000000000000000000..a3e03317a040cc8827cfafc3449133de6e243595
--- /dev/null
+++ b/gcc/config/i386/t-dlldir
@@ -0,0 +1,6 @@
+
+# In a native build, target DLLs go in bindir, where they can be executed.
+# Note double quoting to prevent variables from being evaluated until install
+# time; we don't want to expand them during libgcc.mvars generation.
+
+SHLIB_DLLDIR = $$(bindir)
diff --git a/gcc/config/i386/t-dlldir-x b/gcc/config/i386/t-dlldir-x
new file mode 100644
index 0000000000000000000000000000000000000000..07dd845f0a6004dcc3fd3e83aa4372f631d0dd90
--- /dev/null
+++ b/gcc/config/i386/t-dlldir-x
@@ -0,0 +1,9 @@
+
+# In a cross build, bindir contains host not target binaries, so target DLLs
+# instead go in toolexeclibdir, alongside other target binaries and static libs.
+# Note double quoting to prevent variables from being evaluated until install
+# time; we don't want to expand them during libgcc.mvars generation, and in
+# any case, $toolexeclibdir is not defined in the gcc/ subdirectory, only in
+# target lib directories.
+
+SHLIB_DLLDIR = $$(toolexeclibdir)
diff --git a/libgcc/ChangeLog b/libgcc/ChangeLog
index 309ada4ea6c8a2bbc5733c0c86b1ae4ba1b31c3e..3368decd997f7c7b850710a029006975992b5a4e 100644
--- a/libgcc/ChangeLog
+++ b/libgcc/ChangeLog
@@ -1,3 +1,14 @@
+2011-01-26  Dave Korn  <dave.korn.cygwin@gmail.com>
+
+	PR target/40125
+	* configure.ac: Call ACX_NONCANONICAL_TARGET.
+	(toolexecdir): Calculate and AC_SUBST.
+	(toolexeclibdir): Likewise.
+	* Makefile.in (target_noncanonical): Import.
+	(toolexecdir): Likewise.
+	(toolexeclibdir): Likewise.
+	* configure: Regenerate.
+
 2010-12-13  Nathan Froyd  <froydnj@codesourcery.com>
 
 	PR target/46040
diff --git a/libgcc/Makefile.in b/libgcc/Makefile.in
index dc4838fdbe139029666644220b5046a60cf869fb..7e2ab93fc42f6771d3d448099632807f69b32cd9 100644
--- a/libgcc/Makefile.in
+++ b/libgcc/Makefile.in
@@ -41,6 +41,7 @@ enable_decimal_float = @enable_decimal_float@
 fixed_point = @fixed_point@
 
 host_noncanonical = @host_noncanonical@
+target_noncanonical = @target_noncanonical@
 
 # List of extra object files that should be compiled for this target machine.
 # The rules for compiling them should be in the t-* file for the machine.
@@ -175,6 +176,9 @@ STRIP_FOR_TARGET = $(STRIP)
 libsubdir = $(libdir)/gcc/$(host_noncanonical)/$(version)
 # Used to install the shared libgcc.
 slibdir = @slibdir@
+# Maybe used for DLLs on Windows targets.
+toolexecdir = @toolexecdir@
+toolexeclibdir = @toolexeclibdir@
 
 export AR_FOR_TARGET
 export AR_CREATE_FOR_TARGET
@@ -194,6 +198,8 @@ export STRIP_FOR_TARGET
 export RANLIB_FOR_TARGET
 export libsubdir
 export slibdir
+export toolexecdir
+export toolexeclibdir
 
 version := $(shell $(CC) -dumpversion)
 
diff --git a/libgcc/configure b/libgcc/configure
index 0344380b92959d0a574fbb2e37c8032556e2f5e1..8a27aab188602e8b058d3724b8b5b2e2130e03e9 100644
--- a/libgcc/configure
+++ b/libgcc/configure
@@ -576,10 +576,13 @@ RANLIB
 NM
 LIPO
 AR
+toolexeclibdir
+toolexecdir
 target_subdir
 host_subdir
 build_subdir
 build_libsubdir
+target_noncanonical
 host_noncanonical
 host_os
 host_vendor
@@ -2174,6 +2177,8 @@ esac
 esac
 
 
+
+
 # post-stage1 host modules use a different CC_FOR_BUILD so, in order to
 # have matching libraries, they should use host libraries: Makefile.tpl
 # arranges to pass --with-build-libsubdir=$(HOST_SUBDIR).
@@ -2204,6 +2209,36 @@ fi
 target_subdir=${target_noncanonical}
 
 
+# Calculate toolexeclibdir
+# Also toolexecdir, though it's only used in toolexeclibdir
+case ${version_specific_libs} in
+  yes)
+    # Need the gcc compiler version to know where to install libraries
+    # and header files if --enable-version-specific-runtime-libs option
+    # is selected.
+    toolexecdir='$(libdir)/gcc/$(target_noncanonical)'
+    toolexeclibdir='$(toolexecdir)/$(gcc_version)$(MULTISUBDIR)'
+    ;;
+  no)
+    if test -n "$with_cross_host" &&
+       test x"$with_cross_host" != x"no"; then
+      # Install a library built with a cross compiler in tooldir, not libdir.
+      toolexecdir='$(exec_prefix)/$(target_noncanonical)'
+      toolexeclibdir='$(toolexecdir)/lib'
+    else
+      toolexecdir='$(libdir)/gcc-lib/$(target_noncanonical)'
+      toolexeclibdir='$(libdir)'
+    fi
+    multi_os_directory=`$CC -print-multi-os-directory`
+    case $multi_os_directory in
+      .) ;; # Avoid trailing /.
+      *) toolexeclibdir=$toolexeclibdir/$multi_os_directory ;;
+    esac
+    ;;
+esac
+
+
+
 if test -n "$ac_tool_prefix"; then
   # Extract the first word of "${ac_tool_prefix}ar", so it can be a program name with args.
 set dummy ${ac_tool_prefix}ar; ac_word=$2
diff --git a/libgcc/configure.ac b/libgcc/configure.ac
index fc157d30a794206a7c6f7e2c6e101fe7e6d447f3..7dbe114e39438f1aa803bfd3a449bc689334141e 100644
--- a/libgcc/configure.ac
+++ b/libgcc/configure.ac
@@ -104,8 +104,39 @@ esac
 
 AC_CANONICAL_HOST
 ACX_NONCANONICAL_HOST
+ACX_NONCANONICAL_TARGET
 GCC_TOPLEV_SUBDIRS
 
+# Calculate toolexeclibdir
+# Also toolexecdir, though it's only used in toolexeclibdir
+case ${version_specific_libs} in
+  yes)
+    # Need the gcc compiler version to know where to install libraries
+    # and header files if --enable-version-specific-runtime-libs option
+    # is selected.
+    toolexecdir='$(libdir)/gcc/$(target_noncanonical)'
+    toolexeclibdir='$(toolexecdir)/$(gcc_version)$(MULTISUBDIR)'
+    ;;
+  no)
+    if test -n "$with_cross_host" &&
+       test x"$with_cross_host" != x"no"; then
+      # Install a library built with a cross compiler in tooldir, not libdir.
+      toolexecdir='$(exec_prefix)/$(target_noncanonical)'
+      toolexeclibdir='$(toolexecdir)/lib'
+    else
+      toolexecdir='$(libdir)/gcc-lib/$(target_noncanonical)'
+      toolexeclibdir='$(libdir)'
+    fi
+    multi_os_directory=`$CC -print-multi-os-directory`
+    case $multi_os_directory in
+      .) ;; # Avoid trailing /.
+      *) toolexeclibdir=$toolexeclibdir/$multi_os_directory ;;
+    esac
+    ;;
+esac
+AC_SUBST(toolexecdir)
+AC_SUBST(toolexeclibdir)
+
 dnl These must be called before AM_PROG_LIBTOOL, because it may want
 dnl to call AC_CHECK_PROG.
 AC_CHECK_TOOL(AR, ar)