Subject: [alpine-devel] GCC stuff and porting Alpine to MIPS

From: "A. Wilcox" <[email-masked]>
Date: Sat, 26 Aug 2017 18:10:55 -0500

Hi there Alpinists,

One of Adélie's goal architectures (and one that musl libc supports
well, from experience) is MIPS.  Both the 32-bit and 64-bit varieties of
MIPS would ideally be supported.

I've made some patches to abuild and the musl/gcc/binutils APKBUILD
files that allow MIPS support to be somewhat created.  However, there
are a few issues that I am seeing.  I am not sure the best way to
proceed with fixing them.

1) binutils ld.gold does not support MIPS architecture.

This necessitated a special case of $subpackages in the binutils
APKBUILD and moving --enable-gold=[yes/no] to _arch_configure.  An
example patch is attached, but I am not convinced this is the best way.
 In particular, the subpackages conditional feels ugly to me.

2) MIPS ABI does not support GNU-style hash in ld.

Since having default --hash-style=gnu in binutils build broke its test
suite, we no longer ship that patch in Adélie binutils APK.  However,
gcc still has --with-linker-hash-style=gnu.  This caused the musl
bootstrap package to fail with:

/usr/lib/gcc/mips-foxkit-linux-musl/6.4.0/../../../../mips-foxkit-linux-musl/bin/ld:
.gnu.hash is incompatible with the MIPS ABI

An example patch for the GCC APKBUILD to conditionalise the specified
hash style is therefore also attached.  This one seems much less ugly to
me and might be acceptable.

3) libgcc in final cross build stage requires linux-headers.

The libunwind shipped in libgcc, on mips*-*-linux targets, has

#include <asm/unistd.h>

It seems to specifically need the kernel header version and not the
version musl ships with, but as I was unable to find exactly where the
file was generated (it 'appears' in
build-final-cross/mips-foxkit-linux-musl/libgcc/) I cannot be sure.  The
linux-headers package wouldn't cooperate in generating an early
arch-specific package for gcc to depend on (the package itself built,
but adding 'linux-headers' to EXTRADEPENDS_TARGET in bootstrap.sh
errored with missing dependency).

I'm not sure where to even start to make a worthwhile patch for this.

Any comments on the patches, and a solution to #3, would be greatly
appreciated.  Thanks so much.

Best to you and yours,
--arw
-- 
A. Wilcox (awilfox)
Project Lead, Adélie Linux
http://adelielinux.org
diff --git a/main/gcc/APKBUILD b/main/gcc/APKBUILD
index 7a0633a..ad64f8f 100644
--- a/main/gcc/APKBUILD
+++ b/main/gcc/APKBUILD
@@ -227,6 +227,7 @@ build() {
 	local _libc_configure=
 	local _cross_configure=
 	local _bootstrap_configure=
+	local _hash_style=gnu
 	local _symvers=
 
 	cd "$_gccdir"
@@ -237,6 +238,8 @@ build() {
 	armv6-*-*-*eabihf)	_arch_configure="--with-arch=armv6zk --with-tune=arm1176jzf-s --with-fpu=vfp --with-float=hard --with-abi=aapcs-linux";;
 	armv7-*-*-*eabihf)	_arch_configure="--with-arch=armv7-a --with-tune=generic-armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-abi=aapcs-linux --with-mode=thumb";;
 	mipsel-*-*-*)		_arch_configure="--with-arch-32=mips2 --with-tune-32=mips32 --with-fp-32=32 --with-mips-plt --with-float=hard --with-abi=32";;
+	mips-*-*-*)		_arch_configure="--with-arch=mips3 --with-mips-plt --with-abi=32"; _hash_style="sysv";;
+	mips32el-*-*-*)		_arch_configure="--with-arch=mips32 --with-mips-plt --with-abi=32"; _hash_style="sysv";;
 	powerpc-*-*-*)		_arch_configure="--enable-secureplt --enable-decimal-float=no";;
 	powerpc64*-*-*-*)	_arch_configure="--with-abi=elfv2 --enable-secureplt --enable-decimal-float=no";;
 	powerpc64le*-*-*-*)	_arch_configure="--with-abi=elfv2 --enable-secureplt --enable-decimal-float=no";;
@@ -282,6 +285,7 @@ build() {
 	echo "  libc_configure=$_libc_configure"
 	echo "  cross_configure=$_cross_configure"
 	echo "  bootstrap_configure=$_bootstrap_configure"
+	echo "  hash_style=$_hash_style"
 	echo ""
 
 	mkdir -p "$_builddir"
@@ -310,7 +314,7 @@ build() {
 		$_cross_configure \
 		$_bootstrap_configure \
 		--with-system-zlib \
-		--with-linker-hash-style=gnu
+		--with-linker-hash-style=$_hash_style
 	make
 }
 
diff --git a/main/binutils/APKBUILD b/main/binutils/APKBUILD
index 34e12c3..ea27f01 100644
--- a/main/binutils/APKBUILD
+++ b/main/binutils/APKBUILD
@@ -11,12 +11,14 @@ makedepends="$makedepends_build $makedepends_host"
 checkdepends="dejagnu"
 arch="all"
 license="GPL2 GPL3+ LGPL2 BSD"
-subpackages="$pkgname-dev $pkgname-doc $pkgname-libs $pkgname-gold"
+subpackages="$pkgname-dev $pkgname-doc $pkgname-libs"
+[ "${CARCH}" != "mips" ] && subpackages="$subpackages $pkgname-gold"
 source="http://ftp.gnu.org/gnu/$pkgname/$pkgname-$pkgver.tar.bz2
 	binutils-ld-fix-static-linking.patch
 	disable-gnu-mbind.patch
 	disable-preinit-array-tests.patch
 	fix-plt-main-bnd-test.patch
+	mips-illegal-memcpy.patch
 	remove-no-static-plt-test.patch
 	remove-pr2404-tests.patch
 	remove-pr19553c-test.patch
@@ -26,6 +28,7 @@ builddir="$srcdir/$pkgname-$pkgver"
 if [ "$CHOST" != "$CTARGET" ]; then
 	pkgname="$pkgname-$CTARGET_ARCH"
 	subpackages=""
+	options="!check"
 	sonameprefix="$pkgname:"
 fi
 
@@ -47,6 +50,11 @@ build() {
 		_arch_configure="--enable-targets=x86_64-pep"
 	fi
 
+	case "$CTARGET_ARCH" in
+	mips*)	_arch_configure="$_arch_configure --enable-gold=no" ;;
+	*)	_arch_configure="$_arch_configure --enable-gold=yes" ;;
+	esac
+
 	cd "$builddir"
 	./configure \
 		--build=$CBUILD \
@@ -60,7 +68,6 @@ build() {
 		--disable-multilib \
 		--enable-shared \
 		--enable-ld=default \
-		--enable-gold=yes \
 		--enable-64-bit-bfd \
 		--enable-plugins \
 		--enable-relro \
@@ -121,6 +128,7 @@ ecee33b0e435aa704af1c334e560f201638ff79e199aa11ed78a72f7c9b46f85fbb227af5748e735
 d378fdf1964f8f2bd0b1e62827ac5884bdf943aa435ec89c29fc84bb045d406b733fffaff8fdd8bd1cba8ddea7701c4cf6ccf3ed76a8a3df9c72b447737575a6  disable-gnu-mbind.patch
 3537752e63cef0b5ef136d003ff7e814ba66b12624d817430112d0f291a792e8960fa69a78036f526af835441b3ee483d6a53d55c7b3dd8ee96f0399682dbcbe  disable-preinit-array-tests.patch
 01149e3b772c92783048cc65a4fe6d63770c9f8351ff087901679ba886ccad0bdaeec83033be4537651570de0b042f2cb8e2fad0bcab70afae773053dfda11be  fix-plt-main-bnd-test.patch
+06422157349abf02e79a5ef8bd9f51100e7e996aab65250d518e0cf0d7ac8ed922d3bf1603c4f5b4fd8fb179266b7b4c41db32dcb241d60a7f1c21d1df0c36dd  mips-illegal-memcpy.patch
 b40f9a3841a7af8fc12e8a4044cd672df5614bfda8461b0ca45efa57a42c3bc8490e491ea490c6c05d319a52d69993c4fca33a0aeb044090e7b7f4e4e30c6517  remove-no-static-plt-test.patch
 32ab4215669c728648179c124632467573a3d4675e79f0f0d221c22eb2ec1ca5488b79910bd09142f90a1e0d0b81d99ca4846297f4f9561f158db63745facb66  remove-pr2404-tests.patch
 39ef9c76dd5db6b15f11ffa8061f7ca844fb79c3fb9879c3b1466eef332a28b833597c87003ab9f260b1b85023fae264659088aee27cad7e5aa77b2d58b9a3f6  remove-pr19553c-test.patch"