From 130ebd65884ae2ca4507ed6c689206d46a568e9f Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 25 Jun 2015 03:04:56 -0700
Subject: [PATCH] Use PIE_SPEC/NO_PIE_SPEC for crtend.o/crtendS.o
We need to link with crtend.o and crtendS.o properly for GCC configured
to generate PIE by default. When PIE is enabled by default, -pie is
treated as nop in driver. We should simply validate it, instead of issue
an error, since "pie" may not be used in any SPECs.
* gcc.c (driver_handle_option): Validate -pie if PIE is enabled
by default.
* config/gnu-user.h (GNU_USER_TARGET_ENDFILE_SPEC): Use
PIE_SPEC and NO_PIE_SPEC if HAVE_LD_PIE is defined.
---
gcc/config/gnu-user.h | 9 +++++++++
gcc/gcc.c | 5 +++++
2 files changed, 14 insertions(+)
@@ -67,11 +67,20 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
object constructed before entering `main', followed by a normal
GNU userspace "finalizer" file, `crtn.o'. */
+#if defined HAVE_LD_PIE
+#define GNU_USER_TARGET_ENDFILE_SPEC \
+ "%{fvtable-verify=none:%s; \
+ fvtable-verify=preinit:vtv_end_preinit.o%s; \
+ fvtable-verify=std:vtv_end.o%s} \
+ %{shared:crtendS.o%s;: %{" PIE_SPEC ":crtendS.o%s} \
+ %{" NO_PIE_SPEC ":crtend.o%s}} crtn.o%s"
+#else
#define GNU_USER_TARGET_ENDFILE_SPEC \
"%{fvtable-verify=none:%s; \
fvtable-verify=preinit:vtv_end_preinit.o%s; \
fvtable-verify=std:vtv_end.o%s} \
%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"
+#endif
#undef ENDFILE_SPEC
#define ENDFILE_SPEC GNU_USER_TARGET_ENDFILE_SPEC
@@ -3893,6 +3893,11 @@ driver_handle_option (struct gcc_options *opts,
save_switch ("-o", 1, &arg, validated, true);
return true;
+#ifdef ENABLE_DEFAULT_PIE
+ case OPT_pie:
+ /* -pie is turned on by default. */
+#endif
+
case OPT_static_libgcc:
case OPT_shared_libgcc:
case OPT_static_libgfortran:
--
2.1.0