diff mbox

[RFC] PIE support

Message ID 20130705181808.5c9a7fb4@vostro
State New
Headers show

Commit Message

Timo Teras July 5, 2013, 3:18 p.m. UTC
This is also mainly for comments. The piepatches definitely need to be
handled somewhat better - would be nice if CT_Patch could be called
with the patches just downloaded.

The config option names and description also need improvement, but not
really sure how they should be named.

All in all, this enables:
1. to build for host that needs PIE executables
2. to build toolchain that generates PIE executables for target

This is also tested to be functional.

# HG changeset patch
# User Timo Teras <timo.teras@iki.fi>
# Date 1373036916 0
#      Fri Jul 05 15:08:36 2013 +0000
# Node ID 241d803894fb9a0724edb415e8fe59b2f6473c3e
# Parent  ea74ae048cecafd6a9078fe9c621934bc23e39f3
RFC: PIC/PIE support


--
For unsubscribe information see http://sourceware.org/lists.html#faq
diff mbox

Patch

diff -r ea74ae048cec -r 241d803894fb config/toolchain.in
--- a/config/toolchain.in	Tue Jul 02 05:34:04 2013 +0000
+++ b/config/toolchain.in	Fri Jul 05 15:08:36 2013 +0000
@@ -146,6 +146,13 @@ 
       You shouldn't need to enter anything here, unless you plan to manually
       call the tools (autotools-based ./configure will use the standard name).
 
+config TARGET_PIE
+    bool
+    prompt "Enable PIE"
+    default n
+    help
+      Enable this if target exepects PIC/PIE enabled.
+
 comment "Toolchain type"
 
 choice
@@ -245,6 +252,13 @@ 
       for that by checking the tools without the suffix in case it can
       not find some of the tool.
 
+config BUILD_PIE
+    bool
+    default n
+    prompt "|  Enforce PIC/PIE support"
+    help
+      Check this if *build system* expects to have PIC/PIE enabled.
+
 if CANADIAN
 
 comment "Host system"
@@ -299,6 +313,13 @@ 
       for that by checking the tools without the suffix in case it can
       not find some of the tool.
 
+config HOST_PIE
+    bool
+    default n
+    prompt "|  Enforce PIC/PIE support"
+    help
+      Check this if *host system* expects to have PIC/PIE enabled.
+
 endif # CANADIAN
 
 comment "Misc options"
diff -r ea74ae048cec -r 241d803894fb scripts/build/cc/gcc.sh
--- a/scripts/build/cc/gcc.sh	Tue Jul 02 05:34:04 2013 +0000
+++ b/scripts/build/cc/gcc.sh	Fri Jul 05 15:08:36 2013 +0000
@@ -39,6 +39,11 @@ 
         CT_GetFile ecj-latest .jar ftp://gcc.gnu.org/pub/java   \
                                    ftp://sourceware.org/pub/java
     fi
+
+    if [ "${CT_TARGET_PIE}" = "y" ]; then
+        CT_GetFile "gcc-4.7.1-piepatches-v0.5.3" \
+		http://distfiles.gentoo.org/distfiles/
+    fi
 }
 
 # Extract gcc
@@ -58,6 +63,17 @@ 
        ]; then
         CT_DoExecLog ALL cp -v "${CT_TARBALLS_DIR}/ecj-latest.jar" "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/ecj.jar"
     fi
+
+    if [ "${CT_TARGET_PIE}" = "y" ]; then
+        CT_Extract "gcc-4.7.1-piepatches-v0.5.3"
+        cd "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/"
+        for p in "${CT_SRC_DIR}"/gcc-4.7.1-piepatches-v0.5.3/*.patch; do
+            CT_DoExecLog ALL \
+		patch --no-backup-if-mismatch -f -p0 -i "${p}" ||
+		patch --no-backup-if-mismatch -f -p1 -i "${p}" ||
+		return 1
+        done
+    fi
 }
 
 #------------------------------------------------------------------------------
@@ -592,6 +608,7 @@ 
     local prefix
     local complibs
     local cflags
+    local piecflags
     local ldflags
     local lang_list
     local build_manuals
@@ -793,6 +810,8 @@ 
     esac
 
     [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] && extra_config+=("--disable-nls")
+    [ "${CT_TARGET_PIE}" = "y" ] && extra_config+=("--enable-esp")
+    [ "${CT_TARGET_PIE}" = "y" ] && piecflags="-DEFAULT_PIE_SSP -DEFAULT_RELRO -DEFAULT_BIND_NOW"
 
     if [ "${CT_CC_GCC_SYSTEM_ZLIB}" = "y" ]; then
         extra_config+=("--with-system-zlib")
@@ -808,7 +827,7 @@ 
 
     CT_DoExecLog CFG                                \
     CC_FOR_BUILD="${CT_BUILD}-gcc"                  \
-    CFLAGS="${cflags}"                              \
+    CFLAGS="${cflags} ${piecflags}"                 \
     LDFLAGS="${final_LDFLAGS[*]}"                   \
     CFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"         \
     CXXFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"       \
diff -r ea74ae048cec -r 241d803894fb scripts/build/companion_libs/100-gmp.sh
--- a/scripts/build/companion_libs/100-gmp.sh	Tue Jul 02 05:34:04 2013 +0000
+++ b/scripts/build/companion_libs/100-gmp.sh	Fri Jul 05 15:08:36 2013 +0000
@@ -39,6 +39,7 @@ 
     gmp_opts+=( "prefix=${CT_BUILDTOOLS_PREFIX_DIR}" )
     gmp_opts+=( "cflags=${CT_CFLAGS_FOR_BUILD}" )
     gmp_opts+=( "ldflags=${CT_LDFLAGS_FOR_BUILD}" )
+    gmp_opts+=( "pie=${CT_BUILD_PIE}" )
     do_gmp_backend "${gmp_opts[@]}"
 
     CT_Popd
@@ -56,6 +57,7 @@ 
     gmp_opts+=( "prefix=${CT_HOST_COMPLIBS_DIR}" )
     gmp_opts+=( "cflags=${CT_CFLAGS_FOR_HOST}" )
     gmp_opts+=( "ldflags=${CT_LDFLAGS_FOR_HOST}" )
+    gmp_opts+=( "pie=${CT_HOST_PIE}" )
     do_gmp_backend "${gmp_opts[@]}"
 
     CT_Popd
@@ -68,12 +70,15 @@ 
 #     prefix        : prefix to install into    : dir       : (none)
 #     cflags        : cflags to use             : string    : (empty)
 #     ldflags       : ldflags to use            : string    : (empty)
+#     pie           : enable pic/pie            : string    : (empty)
 do_gmp_backend() {
     local host
     local prefix
     local cflags
     local ldflags
+    local pie
     local arg
+    local confopts
 
     for arg in "$@"; do
         eval "${arg// /\\ }"
@@ -81,6 +86,8 @@ 
 
     CT_DoLog EXTRA "Configuring GMP"
 
+    [ "$pie" = "y" ] && confopts="$confopts --with-pic"
+
     CT_DoExecLog CFG                                \
     CFLAGS="${cflags} -fexceptions"                 \
     LDFLAGS="${ldflags}"                            \
@@ -88,6 +95,7 @@ 
         --build=${CT_BUILD}                         \
         --host=${host}                              \
         --prefix="${prefix}"                        \
+        $confopts                                   \
         --enable-fft                                \
         --enable-mpbsd                              \
         --enable-cxx                                \
diff -r ea74ae048cec -r 241d803894fb scripts/crosstool-NG.sh.in
--- a/scripts/crosstool-NG.sh.in	Tue Jul 02 05:34:04 2013 +0000
+++ b/scripts/crosstool-NG.sh.in	Fri Jul 05 15:08:36 2013 +0000
@@ -360,6 +360,7 @@ 
         cross)
             # A cross-compiler runs on the same machine it is built on
             CT_HOST="${CT_BUILD}"
+            CT_HOST_PIE="${CT_BUILD_PIE}"
             build_mangle="build_"
             host_mangle="build_"
             target_mangle=""