[commit: ghc] ghc-8.0: Pass --no-pie to GCC (60c299a)

git at git.haskell.org git at git.haskell.org
Thu Nov 10 21:42:52 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-8.0
Link       : http://ghc.haskell.org/trac/ghc/changeset/60c299a2278c351abed1ea51a1c2ee5459033fa9/ghc

>---------------------------------------------------------------

commit 60c299a2278c351abed1ea51a1c2ee5459033fa9
Author: Ben Gamari <ben at smart-cactus.org>
Date:   Mon Nov 7 17:54:39 2016 -0500

    Pass --no-pie to GCC
    
    Certain distributions (e.g. Debian and Ubuntu) have enabled PIE be
    default in their GCC packaging. This breaks our abuse of GCC as a linker
    which requires that we pass -Wl,-r, which is incompatible with
    PIE (since the former implies that we are generating a relocatable
    object file and the latter an executable).


>---------------------------------------------------------------

60c299a2278c351abed1ea51a1c2ee5459033fa9
 aclocal.m4                      | 18 ++++++++++++++++++
 compiler/main/DriverPipeline.hs |  9 +++++++++
 compiler/main/SysTools.hs       | 13 +++++++++++++
 configure.ac                    |  3 +++
 distrib/configure.ac.in         |  1 +
 5 files changed, 44 insertions(+)

diff --git a/aclocal.m4 b/aclocal.m4
index 0c842d5..2d42925 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -1258,6 +1258,24 @@ AC_SUBST(GccIsClang)
 rm -f conftest.txt
 ])
 
+# FP_GCC_SUPPORTS_NO_PIE
+# ----------------------
+# Does gcc support the -no-pie option? If so we should pass it to gcc when
+# joining objects since -pie may be enabled by default.
+AC_DEFUN([FP_GCC_SUPPORTS_NO_PIE],
+[
+   AC_REQUIRE([AC_PROG_CC])
+   AC_MSG_CHECKING([whether GCC supports -no-pie])
+   echo 'int main() { return 0; }' > conftest.c
+   if ${CC-cc} -o conftest -no-pie conftest.c > /dev/null 2>&1; then
+       AC_DEFINE(GCC_SUPPORTS_NO_PIE, 1, [gcc supports -no-pie])
+       AC_MSG_RESULT([yes])
+   else
+       AC_MSG_RESULT([no])
+   fi
+   rm -f conftest.c conftest.o conftest
+])
+
 dnl Small feature test for perl version. Assumes PerlCmd
 dnl contains path to perl binary.
 dnl
diff --git a/compiler/main/DriverPipeline.hs b/compiler/main/DriverPipeline.hs
index f6e5b8b..2e322a8 100644
--- a/compiler/main/DriverPipeline.hs
+++ b/compiler/main/DriverPipeline.hs
@@ -1850,6 +1850,11 @@ linkBinary' staticLink dflags o_files dep_packages = do
                       ++ map SysTools.Option (
                          []
 
+#if GCC_SUPPORTS_NO_PIE
+                      -- See Note [No PIE eating when linking]
+                      ++ ["-no-pie"]
+#endif
+
                       -- Permit the linker to auto link _symbol to _imp_symbol.
                       -- This lets us link against DLLs without needing an "import library".
                       ++ (if platformOS platform == OSMinGW32
@@ -2151,6 +2156,10 @@ joinObjectFiles dflags o_files output_fn = do
                        SysTools.Option "-nostdlib",
                        SysTools.Option "-Wl,-r"
                      ]
+#if GCC_SUPPORTS_NO_PIE
+                        -- See Note [No PIE eating while linking] in SysTools
+                     ++ [SysTools.Option "-no-pie"]
+#endif
                      ++ (if any (cc ==) [Clang, AppleClang, AppleClang51]
                           then []
                           else [SysTools.Option "-nodefaultlibs"])
diff --git a/compiler/main/SysTools.hs b/compiler/main/SysTools.hs
index 236bcfd..ba384c9 100644
--- a/compiler/main/SysTools.hs
+++ b/compiler/main/SysTools.hs
@@ -1578,6 +1578,15 @@ linesPlatform xs =
 
 #endif
 
+{-
+Note [No PIE eating while linking]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+As of 2016 some Linux distributions (e.g. Debian) have started enabling -pie by
+default in their gcc builds. This is incompatible with -r as it implies that we
+are producing an executable. Consequently, we must manually pass -no-pie to gcc
+when joining object files or linking dynamic libraries. See #12759.
+-}
+
 linkDynLib :: DynFlags -> [String] -> [UnitId] -> IO ()
 linkDynLib dflags0 o_files dep_packages
  = do
@@ -1743,6 +1752,10 @@ linkDynLib dflags0 o_files dep_packages
                  ++ [ Option "-o"
                     , FileOption "" output_fn
                     ]
+#if GCC_SUPPORTS_NO_PIE
+                    -- See Note [No PIE eating when linking]
+                 ++ [ Option "-no-pie" ]
+#endif
                  ++ map Option o_files
                  ++ [ Option "-shared" ]
                  ++ map Option bsymbolicFlag
diff --git a/configure.ac b/configure.ac
index 57726d6..bbb39eb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -647,6 +647,9 @@ dnl     If gcc, make sure it's at least 3.0
 dnl
 FP_GCC_VERSION
 
+dnl ** See whether gcc supports -no-pie
+FP_GCC_SUPPORTS_NO_PIE
+
 dnl ** look to see if we have a C compiler using an llvm back end.
 dnl
 FP_CC_LLVM_BACKEND
diff --git a/distrib/configure.ac.in b/distrib/configure.ac.in
index d50ce86..fa6a177 100644
--- a/distrib/configure.ac.in
+++ b/distrib/configure.ac.in
@@ -92,6 +92,7 @@ FIND_LD([LdCmd])
 AC_SUBST([LdCmd])
 
 FP_GCC_VERSION
+FP_GCC_SUPPORTS_NO_PIE
 AC_PROG_CPP
 
 FP_PROG_LD_IS_GNU



More information about the ghc-commits mailing list