From 1c65df304d264f1472384d1cf76c3de490b5578f Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@sandoe.co.uk>
Date: Fri, 12 Nov 2021 17:06:25 +0000
Subject: [PATCH 2/2] configure: Add top-level configure support for
 --enable-pie-tools.

This recognises and validates the --enable-pie-tools configure
option.  It is done here because the choice can affect the code-
gen decisions for dependent libraries (e.g. in the case of Dariwn
we need to switch PIC support on and, therefore, for 32bit tool-
chains disable mdynamic-no-pic).

The validated option is passed to sub-configures that use it
(currently just gcc/).

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>

ChangeLog:

	* Makefile.def: Pass enable-pie-tools configure option
	to gcc/ configure.
	* Makefile.in: Regenerated.
	* Makefile.tpl: Export enable_pie_tools value.
	* configure: Regenerate.
	* configure.ac: Add --enable-pie-tools option and
	validate it for Darwin.
---
 Makefile.def |  3 ++-
 Makefile.in  | 32 ++++++++++++++++++++++----------
 Makefile.tpl |  3 +++
 configure    | 42 ++++++++++++++++++++++++++++++++++++++++++
 configure.ac | 34 ++++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+), 11 deletions(-)

diff --git a/Makefile.def b/Makefile.def
index a504192e6d7..b9f86938ab3 100644
--- a/Makefile.def
+++ b/Makefile.def
@@ -47,7 +47,8 @@ host_modules= { module= fixincludes; bootstrap=true;
 host_modules= { module= flex; no_check_cross= true; };
 host_modules= { module= gas; bootstrap=true; };
 host_modules= { module= gcc; bootstrap=true; 
-		extra_make_flags="$(EXTRA_GCC_FLAGS)"; };
+		extra_make_flags="$(EXTRA_GCC_FLAGS)";
+		extra_configure_flags='--enable-pie-tools=@enable_pie_tools@'; };
 host_modules= { module= gmp; lib_path=.libs; bootstrap=true;
 		// Work around in-tree gmp configure bug with missing flex.
 		extra_configure_flags='--disable-shared LEX="touch lex.yy.c"';

diff --git a/Makefile.tpl b/Makefile.tpl
index 213052f8226..4c402759d36 100644
--- a/Makefile.tpl
+++ b/Makefile.tpl
@@ -115,6 +115,9 @@ GCC_SHLIB_SUBDIR = @GCC_SHLIB_SUBDIR@
 # If the build should make suitable code for shared host resources.
 host_shared = @host_shared@
 
+# If we should build compilers and supporting tools as PIE.
+enable_pie_tools = @enable_pie_tools@
+
 # Build programs are put under this directory.
 BUILD_SUBDIR = @build_subdir@
 # This is set by the configure script to the arguments to use when configuring

diff --git a/configure.ac b/configure.ac
index 550e6993b59..6859d8f950d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1860,7 +1860,41 @@ AC_ARG_ENABLE(host-shared,
 [AS_HELP_STRING([--enable-host-shared],
 		[build host code as shared libraries])],
 [host_shared=$enableval], [host_shared=no])
+
+# Check whether --enable-pie-tools was given.
+# Checked early because it can affect host make fragments.
+AC_ARG_ENABLE(pie-tools,
+[AS_HELP_STRING([--enable-pie-tools],
+  [build Position Independent Executables for the compilers and other tools])],
+[enable_pie_tools=$enableval
+ case $target in
+   aarch64-*-darwin1[[1-9]]*)
+     if test x$enable_pie_tools != xyes ; then
+       echo configure.ac: warning: aarch64-darwin must use PIE, pie-tools setting ignored. 1>&2
+       enable_pie_tools=yes
+       host_shared=yes
+     fi ;;
+    *) ;;
+ esac],
+[case $target in
+  # PIE is the default for macOS 10.7+ so reflect that in the configure.
+  # However, we build 32b toolchains mdynamic-no-pic by default which is
+  # not compatible with PIE.
+  x86_64-*-darwin1[[1-9]]* | *-*-darwin2*) enable_pie_tools=yes ;;
+  *) enable_pie_tools=no ;;
+ esac])
+
+case $target in
+  *-*-darwin*)
+    if test x$enable_pie_tools = xyes && test x$host_shared != xyes ; then
+      echo configure.ac: warning: for Darwin PIE requires PIC code, switching host-shared on 1>&2
+      host_shared=yes
+    fi ;;
+  *) ;;
+esac
+
 AC_SUBST(host_shared)
+AC_SUBST([enable_pie_tools])
 
 # By default, C and C++ are the only stage 1 languages.
 stage1_languages=,c,
-- 
2.24.3 (Apple Git-128)

