Add ssp_nonshared to link commandline for musl targets

Message ID 20230406113322.3182296-1-yashinde145@gmail.com
State Accepted
Headers
Series Add ssp_nonshared to link commandline for musl targets |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Yash Shinde April 6, 2023, 11:33 a.m. UTC
  From: Khem Raj <raj.khem@gmail.com>

When -fstack-protector options are enabled we need to
link with ssp_shared on musl since it does not provide
the __stack_chk_fail_local() so essentially it provides
libssp but not libssp_nonshared something like
TARGET_LIBC_PROVIDES_SSP_BUT_NOT_SSP_NONSHARED
 where-as for glibc the needed symbols
are already present in libc_nonshared library therefore
we do not need any library helper on glibc based systems
but musl needs the libssp_noshared from gcc

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Yash Shinde <yash.shinde@windriver.com>
---
 gcc/config/linux.h          |  7 +++++++
 gcc/config/rs6000/linux.h   | 10 ++++++++++
 gcc/config/rs6000/linux64.h | 10 ++++++++++
 3 files changed, 27 insertions(+)
---
 gcc/config/linux.h          |  7 +++++++
 gcc/config/rs6000/linux.h   | 10 ++++++++++
 gcc/config/rs6000/linux64.h | 10 ++++++++++
 3 files changed, 27 insertions(+)
  

Comments

Jakub Jelinek April 6, 2023, 11:51 a.m. UTC | #1
On Thu, Apr 06, 2023 at 05:03:22PM +0530, Yash Shinde via Gcc-patches wrote:
> When -fstack-protector options are enabled we need to
> link with ssp_shared on musl since it does not provide
> the __stack_chk_fail_local() so essentially it provides
> libssp but not libssp_nonshared something like
> TARGET_LIBC_PROVIDES_SSP_BUT_NOT_SSP_NONSHARED
>  where-as for glibc the needed symbols
> are already present in libc_nonshared library therefore
> we do not need any library helper on glibc based systems
> but musl needs the libssp_noshared from gcc
> 
> diff --git a/gcc/config/linux.h b/gcc/config/linux.h
> index e3aca79cccc..33f9265bb93 100644
> --- a/gcc/config/linux.h
> +++ b/gcc/config/linux.h
> @@ -189,6 +189,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
>    }
>  #endif
>  
> +#ifdef TARGET_LIBC_PROVIDES_SSP
> +#undef LINK_SSP_SPEC
> +#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
> +		      "|fstack-protector-strong|fstack-protector-explicit" \
> +		      ":-lssp_nonshared}"
> +#endif

This links with -lssp_nonshared even for glibc, that certainly shouldn't be done.

	Jakub
  

Patch

diff --git a/gcc/config/linux.h b/gcc/config/linux.h
index e3aca79cccc..33f9265bb93 100644
--- a/gcc/config/linux.h
+++ b/gcc/config/linux.h
@@ -189,6 +189,13 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   }
 #endif
 
+#ifdef TARGET_LIBC_PROVIDES_SSP
+#undef LINK_SSP_SPEC
+#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
+		      "|fstack-protector-strong|fstack-protector-explicit" \
+		      ":-lssp_nonshared}"
+#endif
+
 #if (DEFAULT_LIBC == LIBC_UCLIBC) && defined (SINGLE_LIBC) /* uClinux */
 /* This is a *uclinux* target.  We don't define below macros to normal linux
    versions, because doing so would require *uclinux* targets to include
diff --git a/gcc/config/rs6000/linux.h b/gcc/config/rs6000/linux.h
index 5d21befe8e4..4fc17e781ba 100644
--- a/gcc/config/rs6000/linux.h
+++ b/gcc/config/rs6000/linux.h
@@ -99,6 +99,16 @@ 
 					 " -m elf32ppclinux")
 #endif
 
+/* link libssp_nonshared.a with musl */
+#if DEFAULT_LIBC == LIBC_MUSL
+#ifdef TARGET_LIBC_PROVIDES_SSP
+#undef LINK_SSP_SPEC
+#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
+		      "|fstack-protector-strong|fstack-protector-explicit" \
+		      ":-lssp_nonshared}"
+#endif
+#endif
+
 #undef LINK_OS_LINUX_SPEC
 #define LINK_OS_LINUX_SPEC LINK_OS_LINUX_EMUL " %{!shared: %{!static: \
   %{!static-pie: \
diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
index 9e457033d11..49c9f6e2105 100644
--- a/gcc/config/rs6000/linux64.h
+++ b/gcc/config/rs6000/linux64.h
@@ -377,6 +377,16 @@  extern int dot_symbols;
 					   " -m elf64ppc")
 #endif
 
+/* link libssp_nonshared.a with musl */
+#if DEFAULT_LIBC == LIBC_MUSL
+#ifdef TARGET_LIBC_PROVIDES_SSP
+#undef LINK_SSP_SPEC
+#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
+		      "|fstack-protector-strong|fstack-protector-explicit" \
+		      ":-lssp_nonshared}"
+#endif
+#endif
+
 #define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " %{!shared: %{!static: \
   %{!static-pie: \
     %{rdynamic:-export-dynamic} \