diff --git a/cross/gcc-aarch64/APKBUILD b/cross/gcc-aarch64/APKBUILD
index 65b4d553085580c34b43c050f7ea47f27b85a063..471e3cadb075604c37c36359a87fbbd53d735f42 100644
--- a/cross/gcc-aarch64/APKBUILD
+++ b/cross/gcc-aarch64/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-aarch64
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for aarch64"
 url="https://gcc.gnu.org"
 arch="x86_64"
@@ -44,9 +44,10 @@ depends="binutils-aarch64 mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-aarch64 binutils-aarch64"
 subpackages="g++-aarch64:gpp libstdc++-dev-aarch64:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-x86_64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +136,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +170,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +218,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +233,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +243,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +282,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +290,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +328,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +357,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +379,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +387,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +398,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +414,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +429,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +473,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +538,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +551,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +569,12 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
-
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-x86_64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +593,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +620,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +637,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +664,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +690,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +699,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +731,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +785,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-aarch64/riscv64-improve-build-time.patch b/cross/gcc-aarch64/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-aarch64/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+
diff --git a/cross/gcc-armhf/APKBUILD b/cross/gcc-armhf/APKBUILD
index ea7e0af3fb085f14f0e5f0c400e681953b3d90a9..e4e849b4a40af217dedc5827d10ca81c99c7f350 100644
--- a/cross/gcc-armhf/APKBUILD
+++ b/cross/gcc-armhf/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-armhf
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for armhf"
 url="https://gcc.gnu.org"
 arch="x86_64"
@@ -44,9 +44,10 @@ depends="binutils-armhf mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-armhf binutils-armhf"
 subpackages="g++-armhf:gpp libstdc++-dev-armhf:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-x86_64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +136,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +170,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +218,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +233,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +243,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +282,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +290,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +328,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +357,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +379,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +387,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +398,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +414,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +429,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +473,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +538,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +551,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +569,12 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
-
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-x86_64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +593,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +620,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +637,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +664,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +690,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +699,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +731,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +785,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-armhf/riscv64-improve-build-time.patch b/cross/gcc-armhf/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-armhf/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+
diff --git a/cross/gcc-armv7/APKBUILD b/cross/gcc-armv7/APKBUILD
index 3ec4c4d1237f70f740f89df71679d44d86901241..7ef4e5509487fdf79751869d592c961061f3221b 100644
--- a/cross/gcc-armv7/APKBUILD
+++ b/cross/gcc-armv7/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-armv7
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for armv7"
 url="https://gcc.gnu.org"
 arch="x86_64"
@@ -44,9 +44,10 @@ depends="binutils-armv7 mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-armv7 binutils-armv7"
 subpackages="g++-armv7:gpp libstdc++-dev-armv7:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-x86_64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +136,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +170,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +218,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +233,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +243,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +282,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +290,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +328,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +357,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +379,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +387,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +398,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +414,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +429,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +473,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +538,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +551,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +569,12 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
-
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-x86_64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +593,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +620,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +637,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +664,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +690,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +699,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +731,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +785,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-armv7/riscv64-improve-build-time.patch b/cross/gcc-armv7/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-armv7/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+
diff --git a/cross/gcc-riscv64/APKBUILD b/cross/gcc-riscv64/APKBUILD
index ee71a133e2769d8da30ed301085b5677189f2828..4767e2e13b1152262a16869efe33d95205a22aef 100644
--- a/cross/gcc-riscv64/APKBUILD
+++ b/cross/gcc-riscv64/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-riscv64
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for riscv64"
 url="https://gcc.gnu.org"
 arch="x86_64"
@@ -44,9 +44,10 @@ depends="binutils-riscv64 mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-riscv64 binutils-riscv64"
 subpackages="g++-riscv64:gpp libstdc++-dev-riscv64:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-x86_64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +136,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +170,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +218,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +233,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +243,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +282,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +290,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +328,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +357,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +379,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +387,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +398,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +414,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +429,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +473,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +538,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +551,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +569,12 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
-
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-x86_64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +593,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +620,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +637,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +664,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +690,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +699,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +731,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +785,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-riscv64/riscv64-improve-build-time.patch b/cross/gcc-riscv64/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-riscv64/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+
diff --git a/cross/gcc-x86/APKBUILD b/cross/gcc-x86/APKBUILD
index 8f1611a097c00da2acf2b1732034a1848f6cc7a0..e53e84d040c10147404f293de57117d3ce05a7ab 100644
--- a/cross/gcc-x86/APKBUILD
+++ b/cross/gcc-x86/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-x86
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for x86"
 url="https://gcc.gnu.org"
 arch="x86_64"
@@ -44,9 +44,10 @@ depends="binutils-x86 mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-x86 binutils-x86"
 subpackages="g++-x86:gpp libstdc++-dev-x86:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-x86_64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +136,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +170,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +218,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +233,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +243,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +282,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +290,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +328,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +357,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +379,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +387,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +398,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +414,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +429,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +473,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +538,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +551,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +569,12 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
-
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-x86_64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +593,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +620,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +637,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +664,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +690,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +699,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +731,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +785,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-x86/riscv64-improve-build-time.patch b/cross/gcc-x86/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-x86/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+
diff --git a/cross/gcc-x86_64/APKBUILD b/cross/gcc-x86_64/APKBUILD
index e26dfbcab3ec99844353d0d52834f9fa1feaa50b..f7e2ca428edbb883654dc334bdbadf3091a7d6d4 100644
--- a/cross/gcc-x86_64/APKBUILD
+++ b/cross/gcc-x86_64/APKBUILD
@@ -34,7 +34,7 @@ _pkgsnap="${pkgver##*_git}"
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname=gcc-x86_64
-pkgrel=3
+pkgrel=4
 pkgdesc="Stage2 cross-compiler for x86_64"
 url="https://gcc.gnu.org"
 arch="aarch64"
@@ -44,9 +44,11 @@ depends="binutils-x86_64 mpc1"
 makedepends_build="gcc g++ bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev"
 makedepends_host="linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev musl-dev-x86_64 binutils-x86_64"
 subpackages="g++-x86_64:gpp libstdc++-dev-x86_64:libcxx_dev"
-[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc$_target"
+[ "$CHOST" = "$CTARGET" ] && subpackages="gcc-gdb gcc-doc"
 replaces="libstdc++ binutils"
-options="!strip"
+options="!strip !tracedeps"
+depends="$depends so:libc.musl-aarch64.so.1 so:libgcc_s.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libstdc++.so.6 so:libz.so.1"
+
 
 : "${LANG_CXX:=true}"
 : "${LANG_D:=true}"
@@ -135,7 +137,6 @@ else
 	export CXXFLAGS_FOR_TARGET="$CXXFLAGS"
 	export LDFLAGS_FOR_TARGET="$LDFLAGS"
 	export BOOT_CFLAGS="$CFLAGS"
-	export BOOT_CXXFLAGS="$CXXFLAGS"
 	export BOOT_LDFLAGS="$LDFLAGS"
 fi
 
@@ -170,7 +171,7 @@ esac
 # currently on x86, x86_64 and ia64
 _libquadmath=$LANG_FORTRAN
 case "$CTARGET_ARCH" in
-x86 | x86_64 | ppc64le)	_libquadmath=$LANG_FORTRAN ;;
+x86 | x86_64 | ppc64le) ;;
 *)		_libquadmath=false ;;
 esac
 
@@ -218,13 +219,12 @@ if $LANG_FORTRAN; then
 	_languages="$_languages,fortran"
 fi
 if $LANG_ADA; then
-	subpackages="$subpackages gcc-gnat$_target:gnat"
+	subpackages="$subpackages gcc-gnat$_target:gnat libgnat::$CTARGET_ARCH"
 	_languages="$_languages,ada"
 	if [ "$CBUILD" = "$CTARGET" ]; then
 		makedepends_build="$makedepends_build gcc-gnat-bootstrap"
-		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH libgnat::$CTARGET_ARCH"
+		subpackages="$subpackages libgnat-static:libgnatstatic:$CTARGET_ARCH"
 	else
-		subpackages="$subpackages libgnat::$CTARGET_ARCH"
 		makedepends_build="$makedepends_build gcc-gnat gcc-gnat$_cross"
 	fi
 fi
@@ -234,7 +234,7 @@ fi
 makedepends="$makedepends_build $makedepends_host"
 
 # when using upstream releases, use this URI template
-# https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+# https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 #
 # right now, we are using a git snapshot. snapshots are taken from gcc.gnu.org/pub/gcc/snapshots.
 # However, since they are periodically deleted from the GCC mirrors the utilized snapshots are
@@ -244,7 +244,7 @@ makedepends="$makedepends_build $makedepends_host"
 # PLEASE submit all patches to gcc to https://gitlab.alpinelinux.org/kaniini/alpine-gcc-patches,
 # so that they can be properly tracked and easily rebased if needed.
 #source="https://dev.alpinelinux.org/archive/gcc/${_pkgbase%%.*}-$_pkgsnap/gcc-${_pkgbase%%.*}-$_pkgsnap.tar.xz
-source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkgbase:-$pkgver}.tar.xz
+source="https://gcc.gnu.org/pub/gcc/releases/gcc-$_pkgbase/gcc-$_pkgbase.tar.xz
 	0001-posix_memalign.patch
 	0002-gcc-poison-system-directories.patch
 	0003-specs-turn-on-Wl-z-now-by-default.patch
@@ -283,6 +283,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0036-libphobos-add-riscv64-and-loongarch64-support.patch
 	fix-arm64.patch
 	ppc64le-quadmath.patch
+	riscv64-improve-build-time.patch
 	"
 
 # secfixes:
@@ -290,44 +291,21 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 #     - CVE-2023-4039
 
 # we build out-of-tree
-#_gccdir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
-_gccdir="$srcdir"/gcc-${_pkgbase:-$pkgver}
-_gcclibdir="/usr/lib/gcc/$CTARGET/${_pkgbase:-$pkgver}"
-_gcclibexec="/usr/libexec/gcc/$CTARGET/${_pkgbase:-$pkgver}"
+#builddir="$srcdir"/gcc-${_pkgbase%%.*}-$_pkgsnap
+builddir="$srcdir"/gcc-$_pkgbase
+_gcclibdir="/usr/lib/gcc/$CTARGET/$_pkgbase"
+_gcclibexec="/usr/libexec/gcc/$CTARGET/$_pkgbase"
 
 prepare() {
-	cd "$_gccdir"
-
-	_err=
-	for i in $source; do
-		case "$i" in
-		*.patch)
-			msg "Applying $i"
-			patch -p1 -i "$srcdir"/$i || _err="$_err $i"
-			;;
-		esac
-	done
-
-	if [ -n "$_err" ]; then
-		error "The following patches failed:"
-		for i in $_err; do
-			echo "  $i"
-		done
-		return 1
-	fi
-
-	echo ${_pkgbase:-$pkgver} > gcc/BASE-VER
+	default_prepare
+	echo $_pkgbase > gcc/BASE-VER
 }
 
 build() {
 	local _arch_configure=
-	local _libc_configure=
 	local _bootstrap_configure=
-	local _symvers=
 	local _jit_configure=
 
-	cd "$_gccdir"
-
 	case "$CTARGET" in
 	aarch64-*-*-*)		_arch_configure="--with-arch=armv8-a --with-abi=lp64";;
 	armv5-*-*-*eabi)	_arch_configure="--with-arch=armv5te --with-tune=arm926ej-s --with-float=soft --with-abi=aapcs-linux";;
@@ -351,15 +329,7 @@ build() {
 	*)	_hash_style_configure="--with-linker-hash-style=gnu" ;;
 	esac
 
-	case "$CTARGET_LIBC" in
-	musl)
-		# musl does not support libsanitizer
-		# alpine musl provides libssp_nonshared.a, so we don't need libssp either
-		_libc_configure="--disable-libssp --disable-libsanitizer"
-		_symvers="--disable-symvers"
-		export libat_cv_have_ifunc=no
-		;;
-	esac
+	export libat_cv_have_ifunc=no
 
 
 	case "$BOOTSTRAP" in
@@ -388,10 +358,8 @@ build() {
 	echo "  CHOST=$CHOST"
 	echo "  CTARGET=$CTARGET"
 	echo "  CTARGET_ARCH=$CTARGET_ARCH"
-	echo "  CTARGET_LIBC=$CTARGET_LIBC"
 	echo "  languages=$_languages"
 	echo "  arch_configure=$_arch_configure"
-	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
 	echo "  hash_style_configure=$_hash_style_configure"
@@ -412,7 +380,7 @@ build() {
 		--disable-multilib
 		--disable-nls
 		--disable-werror
-		$_symvers
+		--disable-symvers
 		--enable-__cxa_atexit
 		--enable-default-pie
 		--enable-default-ssp
@@ -420,7 +388,8 @@ build() {
 		--enable-link-serialization=2
 		--enable-linker-build-id
 		$_arch_configure
-		$_libc_configure
+		--disable-libssp
+		--disable-libsanitizer
 		$_cross_configure
 		$_bootstrap_configure
 		--with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues
@@ -430,7 +399,7 @@ build() {
 
 	mkdir -p "$_builddir"
 	cd "$_builddir"
-	"$_gccdir"/configure $gccconfiguration \
+	"$builddir"/configure $gccconfiguration \
 		--with-pkgversion="$version"
 
 	msg "building gcc"
@@ -446,7 +415,7 @@ build() {
 	if $LANG_JIT; then
 		mkdir -p "$_builddir"/libgccjit-build
 		cd "$_builddir"/libgccjit-build
-		"$_gccdir"/configure $gccconfiguration \
+		"$builddir"/configure $gccconfiguration \
 			--disable-bootstrap \
 			--enable-host-shared \
 			--enable-languages=jit \
@@ -461,7 +430,7 @@ package() {
 	cd "$_builddir"
 	make -j1 DESTDIR="$pkgdir" install
 
-	ln -s gcc "$pkgdir"/usr/bin/cc
+	[ "$CHOST" = "$CTARGET" ] && ln -s gcc "$pkgdir"/usr/bin/cc
 	ln -s ${CTARGET}-gcc "$pkgdir"/usr/bin/${CTARGET}-cc
 
 	if $LANG_JIT; then
@@ -505,25 +474,23 @@ package() {
 
 	# move ada runtime libs
 	if $LANG_ADA; then
-		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.so"); do
+		for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 \( -name "libgna*.so" -o -name "libgna*.a" \) ); do
 			mv "$i" "$pkgdir"/usr/lib/
 			ln -s ../../../../${i##*/} $i
 		done
-		if [ "$CHOST" = "$CTARGET" ]; then
-			for i in $(find "$pkgdir"/$_gcclibdir/adalib/ -type f -maxdepth 1 -name "libgna*.a"); do
-				mv "$i" "$pkgdir"/usr/lib/
-				ln -s ../../../../${i##*/} $i
-			done
-		fi
 	fi
 
 	if [ "$CHOST" != "$CTARGET" ]; then
 		# cross-gcc: remove any files that would conflict with the
 		# native gcc package
-		rm -rf "$pkgdir"/usr/bin/cc "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
+		rm -rf "$pkgdir"/usr/include "${pkgdir:?}"/usr/share
 		# libcc1 does not depend on target, don't ship it
 		rm -rf "$pkgdir"/usr/lib/libcc1.so*
 
+		# These are moved into packages with arch=$CTARGET_ARCH so remove
+		# cross prefix
+		#mv "$pkgdir"/usr/$CTARGET/lib/*.so.* "$pkgdir"/usr/lib/ # FIXME
+
 		# fixup gcc library symlinks to be linker scripts so
 		# linker finds the libs from relocated sysroot
 		for so in "$pkgdir"/usr/"$CTARGET"/lib/*.so; do
@@ -572,8 +539,7 @@ libatomic() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libatomic.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libatomic.so.*
 }
 
 libcxx() {
@@ -586,8 +552,7 @@ libcxx() {
 		nm -D "$pkgdir"/usr/lib/libstdc++.so.* | grep clock_gettime
 	fi
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libstdc++.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libstdc++.so.*
 }
 
 libcxx_dev() {
@@ -605,15 +570,13 @@ libcxx_dev() {
 
 gpp() {
 	pkgdesc="GNU C++ standard library and compiler"
-	depends="libstdc++=$_gccrel libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel libc-dev"
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/usr/bin \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}include \
-		"$subpkgdir"/usr/${_target:+$CTARGET/}lib \
+	depends="libstdc++-dev$_target=$_gccrel gcc$_target=$_gccrel musl-dev"
+	depends="$depends so:libc.musl-aarch64.so.1 so:libgmp.so.10 so:libisl.so.23 so:libmpc.so.3 so:libmpfr.so.6 so:libz.so.1"
 
-	mv "$pkgdir/$_gcclibexec/cc1plus" "$subpkgdir/$_gcclibexec/"
+	[ "$CHOST" = "$CTARGET" ] && depends="$depends libstdc++=$_gccrel"
 
-	mv "$pkgdir"/usr/bin/*++ "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/cc1plus
+	amove usr/bin/*++
 }
 
 jit() {
@@ -632,30 +595,26 @@ libobjc() {
 	pkgdesc="GNU Objective-C runtime"
 	replaces="objc"
 	depends=
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libobjc.so.* "$subpkgdir"/usr/lib/
+
+	amove usr/lib/libobjc.so.*
 }
 
 objc() {
 	pkgdesc="GNU Objective-C"
 	replaces="gcc"
-	depends="libc-dev gcc=$_gccrel libobjc=$_gccrel"
-
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include \
-		"$subpkgdir"/usr/lib
-	mv "$pkgdir/$_gcclibexec/cc1obj" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/objc "$subpkgdir"/$_gcclibdir/include/
-	mv "$pkgdir"/usr/lib/libobjc.so "$pkgdir"/usr/lib/libobjc.a \
-		"$subpkgdir"/usr/lib/
+	depends="musl-dev gcc=$_gccrel libobjc=$_gccrel"
+
+	amove $_gcclibexec/cc1obj
+	amove $_gcclibdir/include/objc
+	amove usr/lib/libobjc.so
+	amove usr/lib/libobjc.a
 }
 
 libgcc() {
 	pkgdesc="GNU C compiler runtime libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgcc_s.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgcc_s.so.*
 }
 
 libgomp() {
@@ -663,17 +622,15 @@ libgomp() {
 	depends=
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/${_target:+$CTARGET/}lib/libgomp.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgomp.so.*
 }
 
 libgphobos() {
 	pkgdesc="D programming language standard library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgdruntime.so.* "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/lib/libgphobos.so.*  "$subpkgdir"/usr/lib/
+	amove usr/lib/libgdruntime.so.*
+	amove usr/lib/libgphobos.so.*
 }
 
 gdc() {
@@ -682,42 +639,26 @@ gdc() {
 	[ "$CBUILD" = "$CTARGET" ] && depends="$depends libgphobos=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && provides="gcc-gdc-bootstrap=$_gccrel"
 
-	mkdir -p "$subpkgdir/$_gcclibexec" \
-		"$subpkgdir"/$_gcclibdir/include/d/ \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
 	# Copy: The installed '.d' files, the static lib, the binary itself
 	# The shared libs are part of 'libgphobos' so one can run program
 	# without installing the compiler
-	mv "$pkgdir/$_gcclibexec/d21" "$subpkgdir/$_gcclibexec/"
-	mv "$pkgdir"/$_gcclibdir/include/d/* "$subpkgdir"/$_gcclibdir/include/d/
+	amove $_gcclibexec/d21
+	amove $_gcclibdir/include/d
 	if [ "$CBUILD" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.so "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.so "$subpkgdir"/usr/lib/
-	fi
-	if [ "$CHOST" = "$CTARGET" ]; then
-		mv "$pkgdir"/usr/lib/libgdruntime.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.a "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/lib/libgphobos.spec "$subpkgdir"/usr/lib/
-		mv "$pkgdir"/usr/bin/gdc "$subpkgdir"/usr/bin/
-	else
-		mkdir -p "$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgdruntime.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.a \
-			"$subpkgdir"/usr/$CTARGET/lib/
-		mv "$pkgdir"/usr/$CTARGET/lib/libgphobos.spec \
-			"$subpkgdir"/usr/$CTARGET/lib/
+		amove usr/lib/libgdruntime.so
+		amove usr/lib/libgphobos.so
 	fi
-	mv "$pkgdir"/usr/bin/$CTARGET-gdc "$subpkgdir"/usr/bin/
+	amove usr/${_target:+$CTARGET/}lib/libgdruntime.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.a
+	amove usr/${_target:+$CTARGET/}lib/libgphobos.spec
+	amove usr/bin/*gdc
 }
 
 libgo() {
 	pkgdesc="Go runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgo.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgo.so.*
 }
 
 go() {
@@ -725,31 +666,25 @@ go() {
 	depends="gcc=$_gccrel libgo=$_gccrel !go"
 	install="$pkgname-go.post-install"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/lib/go "$subpkgdir"/usr/lib/
-	mv "$pkgdir"/usr/bin/*gccgo "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/bin/*go "$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gofmt "$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/go1 "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/cgo "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/buildid "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/test2json "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibexec/vet "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/usr/lib/libgo.a \
-		"$pkgdir"/usr/lib/libgo.so \
-		"$pkgdir"/usr/lib/libgobegin.a \
-		"$pkgdir"/usr/lib/libgolibbegin.a \
-		"$subpkgdir"/usr/lib/
+	amove usr/lib/go
+	amove usr/bin/*go
+	amove usr/bin/*gofmt
+	amove $_gcclibexec/go1
+	amove $_gcclibexec/cgo
+	amove $_gcclibexec/buildid
+	amove $_gcclibexec/test2json
+	amove $_gcclibexec/vet
+	amove usr/lib/libgo.a
+	amove usr/lib/libgo.so
+	amove usr/lib/libgobegin.a
+	amove usr/lib/libgolibbegin.a
 }
 
 libgfortran() {
 	pkgdesc="Fortran runtime library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgfortran.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libgfortran.so.*
 }
 
 libquadmath() {
@@ -757,8 +692,7 @@ libquadmath() {
 	pkgdesc="128-bit math library for GCC"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libquadmath.so.* "$subpkgdir"/usr/lib/
+	amove usr/lib/libquadmath.so.*
 }
 
 gfortran() {
@@ -767,38 +701,30 @@ gfortran() {
 	$_libquadmath && depends="$depends libquadmath=$_gccrel"
 	replaces="gcc"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/lib \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/usr/bin/*gfortran "$subpkgdir"/usr/bin/
-	mv "$pkgdir"/usr/lib/libgfortran.a \
-		"$pkgdir"/usr/lib/libgfortran.so \
-		"$subpkgdir"/usr/lib/
+	amove usr/bin/*gfortran
+	amove usr/lib/libgfortran.a
+	amove usr/lib/libgfortran.so
 	if $_libquadmath; then
-		mv "$pkgdir"/usr/lib/libquadmath.a \
-			"$pkgdir"/usr/lib/libquadmath.so \
-			"$subpkgdir"/usr/lib/
+		amove usr/lib/libquadmath.a
+		amove usr/lib/libquadmath.so
 	fi
-	mv "$pkgdir"/$_gcclibdir/finclude "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/$_gcclibexec/f951 "$subpkgdir"/$_gcclibexec
-	mv "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir
+	amove $_gcclibdir/finclude
+	amove $_gcclibexec/f951
+	mv -v "$pkgdir"/usr/lib/libgfortran.spec "$subpkgdir"/$_gcclibdir/
 }
 
 libgnat() {
 	pkgdesc="GNU Ada runtime shared libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.so "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.so
 }
 
 libgnatstatic() {
 	pkgdesc="GNU Ada static libraries"
 	depends=
 
-	mkdir -p "$subpkgdir"/usr/lib
-	mv "$pkgdir"/usr/lib/libgna*.a "$subpkgdir"/usr/lib/
+	amove usr/lib/libgna*.a
 }
 
 gnat() {
@@ -807,12 +733,9 @@ gnat() {
 	provides="$pkgname-gnat-bootstrap=$_gccrel"
 	[ "$CHOST" = "$CTARGET" ] && depends="$depends libgnat=$_gccrel"
 
-	mkdir -p "$subpkgdir"/$_gcclibexec \
-		"$subpkgdir"/$_gcclibdir \
-		"$subpkgdir"/usr/bin
-	mv "$pkgdir"/$_gcclibexec/*gnat* "$subpkgdir"/$_gcclibexec/
-	mv "$pkgdir"/$_gcclibdir/*ada* "$subpkgdir"/$_gcclibdir/
-	mv "$pkgdir"/usr/bin/*gnat* "$subpkgdir"/usr/bin/
+	amove $_gcclibexec/*gnat*
+	amove $_gcclibdir/*ada*
+	amove usr/bin/*gnat*
 }
 
 gdb() {
@@ -864,4 +787,5 @@ c4482ffc36e7894b2140800159f4cbc9a3e9011e43a69b69f4fa92d5a11e2ee645c7e21df4423dd1
 06f64544fd8bc4b6c8b06410c5198aa8fb4e82c232afc0cad83b2c69ac5d39399968206defa78297222700cd18e7fd7e8244afa76d343406a01f657c86306453  0036-libphobos-add-riscv64-and-loongarch64-support.patch
 00319c9770362520e8e93bd03fda1769dfa3622eba77d620e11d8af45a38e6d804a5e924046ae388e1cfaea605696e89c343fca507aaaa5edd58d80744fff14f  fix-arm64.patch
 815265826742400f86336f0c2d3e4edc37398dbc6505ee81107c0103c13ac0f406cff851438d2323033c6c6d0c0323765ee15b975eab8d1dd465f9e988168fb9  ppc64le-quadmath.patch
+ea55e0fa6813fc95c4ca2e87f059182edc54dc61515ebb92bf6eb31dbbd176d0fe81edcc7ea28d32f84ca37e8d48a544623d3d9bc94aa41ffc0859326a616aa2  riscv64-improve-build-time.patch
 "
diff --git a/cross/gcc-x86_64/riscv64-improve-build-time.patch b/cross/gcc-x86_64/riscv64-improve-build-time.patch
new file mode 100644
index 0000000000000000000000000000000000000000..2c8c52a2c0e0eadcc609cd9bfdcf3596ebb81a7f
--- /dev/null
+++ b/cross/gcc-x86_64/riscv64-improve-build-time.patch
@@ -0,0 +1,149 @@
+Patch-Source: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2cf89ae83225f932b226cd57ef2d083a59bcf8a3
+--
+From 2cf89ae83225f932b226cd57ef2d083a59bcf8a3 Mon Sep 17 00:00:00 2001
+From: Richard Biener <rguenther@suse.de>
+Date: Tue, 6 Aug 2024 14:56:26 +0200
+Subject: [PATCH] tree-optimization/116166 - forward jump-threading going wild
+
+Currently the forward threader isn't limited as to the search space
+it explores and with it now using path-ranger for simplifying
+conditions it runs into it became pretty slow for degenerate cases
+like compiling insn-emit.cc for RISC-V esp. when compiling for
+a host with LOGICAL_OP_NON_SHORT_CIRCUIT disabled.
+
+The following makes the forward threader honor the search space
+limit I introduced for the backward threader.  This reduces
+compile-time from minutes to seconds for the testcase in PR116166.
+
+Note this wasn't necessary before we had ranger but with ranger
+the work we do is quadatic in the length of the threading path
+we build up (the same is true for the backwards threader).
+
+	PR tree-optimization/116166
+	* tree-ssa-threadedge.h (jump_threader::thread_around_empty_blocks):
+	Add limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	* tree-ssa-threadedge.cc (jump_threader::thread_around_empty_blocks):
+	Honor and decrement limit parameter.
+	(jump_threader::thread_through_normal_block): Likewise.
+	(jump_threader::thread_across_edge): Initialize limit from
+	param_max_jump_thread_paths and pass it down to workers.
+---
+ gcc/tree-ssa-threadedge.cc | 30 ++++++++++++++++++++++--------
+ gcc/tree-ssa-threadedge.h  |  4 ++--
+ 2 files changed, 24 insertions(+), 10 deletions(-)
+
+diff --git a/gcc/tree-ssa-threadedge.cc b/gcc/tree-ssa-threadedge.cc
+index 7f82639b8ecc..0aa2aa851430 100644
+--- a/gcc/tree-ssa-threadedge.cc
++++ b/gcc/tree-ssa-threadedge.cc
+@@ -786,13 +786,17 @@ propagate_threaded_block_debug_into (basic_block dest, basic_block src)
+ bool
+ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 					   edge taken_edge,
+-					   bitmap visited)
++					   bitmap visited, unsigned &limit)
+ {
+   basic_block bb = taken_edge->dest;
+   gimple_stmt_iterator gsi;
+   gimple *stmt;
+   tree cond;
+ 
++  if (limit == 0)
++    return false;
++  --limit;
++
+   /* The key property of these blocks is that they need not be duplicated
+      when threading.  Thus they cannot have visible side effects such
+      as PHI nodes.  */
+@@ -830,7 +834,8 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 	      m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+ 	      m_state->append_path (taken_edge->dest);
+ 	      bitmap_set_bit (visited, taken_edge->dest->index);
+-	      return thread_around_empty_blocks (path, taken_edge, visited);
++	      return thread_around_empty_blocks (path, taken_edge, visited,
++						 limit);
+ 	    }
+ 	}
+ 
+@@ -872,7 +877,7 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+       m_registry->push_edge (path, taken_edge, EDGE_NO_COPY_SRC_BLOCK);
+       m_state->append_path (taken_edge->dest);
+ 
+-      thread_around_empty_blocks (path, taken_edge, visited);
++      thread_around_empty_blocks (path, taken_edge, visited, limit);
+       return true;
+     }
+ 
+@@ -899,8 +904,13 @@ jump_threader::thread_around_empty_blocks (vec<jump_thread_edge *> *path,
+ 
+ int
+ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+-					    edge e, bitmap visited)
++					    edge e, bitmap visited,
++					    unsigned &limit)
+ {
++  if (limit == 0)
++    return 0;
++  limit--;
++
+   m_state->register_equivs_edge (e);
+ 
+   /* PHIs create temporary equivalences.
+@@ -989,7 +999,7 @@ jump_threader::thread_through_normal_block (vec<jump_thread_edge *> *path,
+  	     visited.  This may be overly conservative.  */
+ 	  bitmap_set_bit (visited, dest->index);
+ 	  bitmap_set_bit (visited, e->dest->index);
+-	  thread_around_empty_blocks (path, taken_edge, visited);
++	  thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 	  return 1;
+ 	}
+     }
+@@ -1075,9 +1085,12 @@ jump_threader::thread_across_edge (edge e)
+   bitmap_set_bit (visited, e->src->index);
+   bitmap_set_bit (visited, e->dest->index);
+ 
++  /* Limit search space.  */
++  unsigned limit = param_max_jump_thread_paths;
++
+   int threaded = 0;
+   if ((e->flags & EDGE_DFS_BACK) == 0)
+-    threaded = thread_through_normal_block (path, e, visited);
++    threaded = thread_through_normal_block (path, e, visited, limit);
+ 
+   if (threaded > 0)
+     {
+@@ -1148,11 +1161,12 @@ jump_threader::thread_across_edge (edge e)
+ 	m_registry->push_edge (path, e, EDGE_START_JUMP_THREAD);
+ 	m_registry->push_edge (path, taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
+ 
+-	found = thread_around_empty_blocks (path, taken_edge, visited);
++	found = thread_around_empty_blocks (path, taken_edge, visited, limit);
+ 
+ 	if (!found)
+ 	  found = thread_through_normal_block (path,
+-					       path->last ()->e, visited) > 0;
++					       path->last ()->e, visited,
++					       limit) > 0;
+ 
+ 	/* If we were able to thread through a successor of E->dest, then
+ 	   record the jump threading opportunity.  */
+diff --git a/gcc/tree-ssa-threadedge.h b/gcc/tree-ssa-threadedge.h
+index 9f6cbfe93307..245b3506a550 100644
+--- a/gcc/tree-ssa-threadedge.h
++++ b/gcc/tree-ssa-threadedge.h
+@@ -101,9 +101,9 @@ private:
+ 					  unsigned limit);
+ 
+   bool thread_around_empty_blocks (vec<class jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   int thread_through_normal_block (vec<jump_thread_edge *> *path,
+-				   edge, bitmap visited);
++				   edge, bitmap visited, unsigned &limit);
+   void thread_across_edge (edge);
+   bool record_temporary_equivalences_from_phis (edge);
+   gimple *record_temporary_equivalences_from_stmts_at_dest (edge);
+-- 
+2.43.5
+