Subject: bug#15793: Bug: linking shared libraries on Cygwin results in undefined references to __stack_chck_guard for code compiled with -fstack-protector

From: Richard PALO <[email-masked]>
Date: Thu, 24 Sep 2015 15:15:57 +0200

Le 02/11/13 23:45, Richard PALO a écrit :
> Le 02/06/10 05:24, Yaakov (Cygwin/X) a écrit :
>> On Sat, 15 May 2010 09:07:49 +0200
>> Bart Van Assche <[email-masked]> wrote:
>>> This behavior has been observed with libtool version 2.2.6.
>>
>> Bug confirmed.  When code is compiled with -fstack-protector{,-all},
>> GCC "emits extra code to check for buffer overflows, such as stack
>> smashing attacks".  This extra code uses symbols from libssp, and
>> therefore (at least) Cygwin's GCC specs contain:
>>
>> *link_ssp:
>> %{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}
>>
>> Therefore, when libtool fails to pass -fstack-protector{,-all} at link
>> stage, the link fails.
>>
>> Patch attached.  (Yes, I have a copyright assignment on file.)
>>
>>
>> Yaakov
>> Cygwin/X
>>
> 
> I've done some limited testing of this patch on a SunOS distro with 
> pkgsrc, and it certainly helps a number of cases since it is gcc that 
> generates the necessary '-lssp_nonshared -lssp' libs for linking (at 
> least in absence of '-nostdlib').
> 
> Please include in the upcoming version.
> 
> 
> 
> 

I'd like to propose a slight modification this which, in addition, seems to get over the
issue (at least on solaris) of c++ shared libraries linking '-nostdlib'.  

This could easily be adapted for other hosts using g++. Note, it is harmless if '-dumpspecs'
or no spec with fstack-protector* is available.

It appears there was no warm welcome in tossing the '-nostdlib' setup for g++ shared libraries 
for solaris, so this seems to be a reasonable workaround.

-- 
Richard PALO

>From 5dfcf84c8243441f30a3c852791b8d46bc4d2c53 Mon Sep 17 00:00:00 2001
From: Richard PALO <[email-masked]>
Date: Thu, 24 Sep 2015 13:23:56 +0200
Subject: [PATCH] workaround for -nostdlib with -fstack-protector on solaris

---
 build-aux/ltmain.in | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in
index 0c40da0..7a6356b 100644
--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -5364,8 +5364,7 @@ func_mode_link ()
       # -stdlib=*            select c++ std lib with clang
       -64|-mips[0-9]|-r[0-9][0-9]*|-xarch=*|-xtarget=*|+DA*|+DD*|-q*|-m*| \
       -t[45]*|-txscale*|-p|-pg|--coverage|-fprofile-*|-F*|@*|-tp=*|--sysroot=*| \
-      -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-fstack-protector*|-stdlib=*| \
-      -specs=*)
+      -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-stdlib=*|-specs=*)
         func_quote_for_eval "$arg"
 	arg=$func_quote_for_eval_result
         func_append compile_command " $arg"
@@ -5374,6 +5373,25 @@ func_mode_link ()
         continue
         ;;
 
+      -fstack-protector*)
+        func_quote_for_eval "$arg"
+        arg=$func_quote_for_eval_result
+        func_append compile_command " $arg"
+        func_append finalize_command " $arg"
+        func_append compiler_flags " $arg"
+
+        # Explicitly add solaris ssp libs to postdeps to counter
+        # linking with g++ -nostdlib when supplied -fstack-protector*
+        test CXX = "$tagname" && {
+          case $host_os in
+          solaris*)
+            func_append postdeps " `$compile_command -dumpspecs 2>/dev/null | $SED -n '/fstack-protector/{s/^.*://;s/}$//;p;}'`"
+            ;;
+          esac
+        }
+        continue
+        ;;
+
       -Z*)
         if test os2 = "`expr $host : '.*\(os2\)'`"; then
           # OS/2 uses -Zxxx to specify OS/2-specific options
-- 
2.5.2