Subject: [PATCH] main/gcc: fix -static and -static-pie on mips*

From: alpine-mips-patches <[email-masked]>
Date: Fri, 13 Sep 2019 11:21:32 +0000

1) Fix '--as-needed' spelling for mips (and riscv, while here). The
'-static -pie' (with '-pie' implied or explicit) support was somehow
broken on mips* without this correction.

2) Fix missing '-static-pie' case handling on mips (i.e. actually pass
'-static -pie' to linker when needed).

With this change and binutils-2.32 toolchain is able to create working
static PIE binaries.
---
 main/gcc/0012-static-pie-mips.patch      | 18 ++++++++++++++++++
 main/gcc/005_all_default-as-needed.patch |  4 ++--
 main/gcc/APKBUILD                        |  6 ++++--
 3 files changed, 24 insertions(+), 4 deletions(-)
 create mode 100644 main/gcc/0012-static-pie-mips.patch

diff --git a/main/gcc/0012-static-pie-mips.patch b/main/gcc/0012-static-pie-mips.patch
new file mode 100644
index 0000000000..1a4ec56a04
--- /dev/null
+++ b/main/gcc/0012-static-pie-mips.patch
@@ -0,0 +1,18 @@
+--- a/gcc/config/mips/gnu-user.h
++++ b/gcc/config/mips/gnu-user.h
+@@ -56,12 +56,12 @@
+ #define GNU_USER_TARGET_LINK_SPEC "\
+   %{G*} %{EB} %{EL} %{mips*} %{shared} \
+   %{!shared: \
+-    %{!static: \
++    %{!static: %{!static-pie: \
+       %{rdynamic:-export-dynamic} \
+       %{mabi=n32: -dynamic-linker " GNU_USER_DYNAMIC_LINKERN32 "} \
+       %{mabi=64: -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
+-      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}} \
+-    %{static}} \
++      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
++    %{static} %{static-pie: -Bstatic -pie --no-dynamic-linker -z text}} \
+   %{mabi=n32:-m" GNU_USER_LINK_EMULATIONN32 "} \
+   %{mabi=64:-m" GNU_USER_LINK_EMULATION64 "} \
+   %{mabi=32:-m" GNU_USER_LINK_EMULATION32 "}"
diff --git a/main/gcc/005_all_default-as-needed.patch b/main/gcc/005_all_default-as-needed.patch
index 2eced0f06c..959ed1e595 100644
--- a/main/gcc/005_all_default-as-needed.patch
+++ b/main/gcc/005_all_default-as-needed.patch
@@ -226,7 +226,7 @@ Index: b/gcc/config/mips/gnu-user.h
  #undef GNU_USER_TARGET_LINK_SPEC
  #define GNU_USER_TARGET_LINK_SPEC "\
    %{G*} %{EB} %{EL} %{mips*} %{shared} \
-+ -as-needed \
++  --as-needed \
    %{!shared: \
      %{!static: \
        %{rdynamic:-export-dynamic} \
@@ -238,7 +238,7 @@ Index: b/gcc/config/riscv/linux.h
    "%{mabi=ilp32:_ilp32}"
  
  #define LINK_SPEC "\
-+-as-needed \
++--as-needed \
  -melf" XLEN_SPEC "lriscv" LD_EMUL_SUFFIX " \
  %{mno-relax:--no-relax} \
  %{shared} \
diff --git a/main/gcc/APKBUILD b/main/gcc/APKBUILD
index 2961f239d4..0d8bc38fff 100644
--- a/main/gcc/APKBUILD
+++ b/main/gcc/APKBUILD
@@ -6,7 +6,7 @@ pkgver=9.2.0
 [ "$CHOST" != "$CTARGET" ] && _target="-$CTARGET_ARCH" || _target=""
 
 pkgname="$pkgname$_target"
-pkgrel=1
+pkgrel=2
 pkgdesc="The GNU Compiler Collection"
 url="http://gcc.gnu.org"
 arch="all"
@@ -157,6 +157,7 @@ source="https://gcc.gnu.org/pub/gcc/releases/gcc-${_pkgbase:-$pkgver}/gcc-${_pkg
 	0008-s390x-muslldso.patch
 	0010-ldbl128-config.patch
 	0012-static-pie.patch
+	0012-static-pie-mips.patch
 
 	libgcc-always-build-gcceh.a.patch
 	gcc-4.9-musl-fortify.patch
@@ -555,7 +556,7 @@ gnat() {
 sha512sums="a12dff52af876aee0fd89a8d09cdc455f35ec46845e154023202392adc164848faf8ee881b59b681b696e27c69fd143a214014db4214db62f9891a1c8365c040  gcc-9.2.0.tar.xz
 e36e95b81489163abd6fe9d58f7867bdca43e61143afacbfb17f4698c0b16ec5fd0061d8fab7b2ae615540bebd721c2e2227f80401b4e7fc253da9da62e6b513  002_all_default-relro.patch
 f86466c62b8291fac46f9c250c7ad8fa5ab7b1cce2504442fd07ddc4543665b317ae28951f244e39aba29aeaf3fff252ec4f6a147aa16adb2b7aed747dd89188  003_all_default-fortify-source.patch
-5e4794d1caddb1050d8935ae5a53ba275b653ad2c9954df89a8c47ac694e6139011829f440eb7e5b24fd57ea25daf25ac83f317871841a78ffb74d167d6f8bf9  005_all_default-as-needed.patch
+53840035399dc4ce592998cd85d97df7b5e23b9188b782a8de4714321335da437801c273194b0f82c7d99932ad68aa22191d4c042d4723d1240fab3196523f53  005_all_default-as-needed.patch
 6c9a5068f6eea8a85d297a3555b1d2333ffd4b9ddbfb7560a3e643f8577603871ca123373ef7f96e894225291d88f7bf575c7f8386bcfdd1dabcd7ba12598b65  gcc-as-needed-gold.patch
 e026bf9f73ee254528423c6cabdfd7794960c567161581b7d4b7c61c45027e12a6efef79662d4288b9cbaecb4798f01af6a5b4a13b766c2d501444ac1846fd43  011_all_default-warn-format-security.patch
 37de4cc9061bfe1963e6e6570e1a6bbfd58204bf90e0eef819882599a9a80ea28f3eb815c20c1f173dc25d4bd9971b7c7f7f9512d6f8f91f04de1e1175114d4d  012_all_default-warn-trampolines.patch
@@ -572,6 +573,7 @@ d1c2d7ff7f673375dc7cd84ae9d8515853b5af6390c70bb763412c012101344e4ee1ce155151ce3f
 7f434a7350c9b06d0ae7cc18a569d813238483afa34b0801d112844a0dfe6164ae36b0416955fd4da7a8caa54672247f319a8ec7ce4b6a97a5f4e17ec083112a  0008-s390x-muslldso.patch
 66085c5555e6b91b6874d1782d5a1dc0ab1792889f9400f48cde9483f82b51b9e3a5de1efbba21a19fc5e664334f2188d0c2bc988d42335efa26118b3c85cc7f  0010-ldbl128-config.patch
 d91c5eba37166cf34ca38e03808e3960c091b3627008a0c68c127cdf52e3827f8cfbe478ababe64c3cbb4d5eb8ed6230a21a74b6908549e05e08646de09df37c  0012-static-pie.patch
+a82645202602f192ef722ca062a17050ed609ba4465173f9c23674491f42d67f9c9151b1e32b263c2846e6d0d6474c1a7521db0defe2d2576e8d6f1632b3296f  0012-static-pie-mips.patch
 d08d7ead2de0429e5c9055d5b029ec2be9a8c821d22cecaf9b51f633652c493333f98963d9267fa2fa63850c50ae5eefd5f59e5910ec10d20044dac082182a8b  libgcc-always-build-gcceh.a.patch
 600fe5098dc54edaa9808fd5717af9dec058953f9ad37d49cfba1db4f7e9a7a8f02019342f75157fc575946fa693259422184de27b7ecc8386d9f3ecc0f7cc5d  gcc-4.9-musl-fortify.patch
 dbe0ee917fc7668571722364ab7c806731e3a31e8bfa30b4941b28b16b877d2a32b4a3897ef533399a28f82d43cac9b28e92de0493f0e779046db56584e07fa4  gcc-6.1-musl-libssp.patch
-- 
2.23.0