[PATCH] libsanitizer: Fix linkage errors for cross toolchains

2022-07-01 Thread Dimitrije Milosevic
When we use cross toolchains, in which the GCC libraries are not installed 
within a designated system root, the shared sanitizer libraries link against 
libstdc++.so* within the same libraries. This directory, however, is not in 
RPATH, 
so attempting to build a dynamically linked application with -fsanitize=... 
gives a linkage error.
More information can be found here: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69839.

GCC, even when configured with -with-sysroot, by default, doesn't install 
libstdc++.so*
within the sysroot, as GCC's installation process isn't designed to help 
construct sysroot trees.
This has to be done manually. 
Furthermore, if we are using a multiarch/multilib configuration (mips-mti*, for 
example), 
we may not even want to install them within the sysroot.
Would love to hear your thoughts on this, as I'm not sure myself that this is 
the best solution.

gcc/ChangeLog:

* gcc.cc (LIBSAN_RPATH): New macro.
(LIBASAN_SPEC): Add LIBSAN_RPATH.
(LIBTSAN_SPEC): Likewise.
(LIBLSAN_SPEC): Likewise.
(LIBUBSAN_SPEC): Likewise.

libsanitizer/ChangeLog:

* Makefile.in: New Makefile variable.
* asan/Makefile.in: Likewise.
* configure: Regenerate.
* configure.ac: New config variable.
* hwasan/Makefile.in: New Makefile variable.
* interception/Makefile.in: Likewise.
* libbacktrace/Makefile.in: Likewise.
* libsanitizer.spec.in: New spec.
* lsan/Makefile.in: New Makefile variable.
* sanitizer_common/Makefile.in: Likewise.
* tsan/Makefile.in: Likewise.
* ubsan/Makefile.in: Likewise.

---

 gcc/gcc.cc| 20 
 libsanitizer/Makefile.in  |  1 +
 libsanitizer/asan/Makefile.in |  1 +
 libsanitizer/configure| 10 --
 libsanitizer/configure.ac |  7 +++
 libsanitizer/hwasan/Makefile.in   |  1 +
 libsanitizer/interception/Makefile.in |  1 +
 libsanitizer/libbacktrace/Makefile.in |  1 +
 libsanitizer/libsanitizer.spec.in |  2 ++
 libsanitizer/lsan/Makefile.in |  1 +
 libsanitizer/sanitizer_common/Makefile.in |  1 +
 libsanitizer/tsan/Makefile.in |  1 +
 libsanitizer/ubsan/Makefile.in|  1 +
 13 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 299e09c4f54..0d2d361b9a4 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -738,17 +738,21 @@ proper position among the other output files.  */
 #define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
 #endif
 
+#ifndef LIBSAN_RPATH
+#define LIBSAN_RPATH " %:include(libsanitizer.spec)%(link_libsan_rpath)"
+#endif
+
 #ifndef LIBASAN_SPEC
 #define STATIC_LIBASAN_LIBS \
   " %{static-libasan|static:%:include(libsanitizer.spec)%(link_libasan)}"
 #ifdef LIBASAN_EARLY_SPEC
-#define LIBASAN_SPEC STATIC_LIBASAN_LIBS
+#define LIBASAN_SPEC STATIC_LIBASAN_LIBS LIBSAN_RPATH
 #elif defined(HAVE_LD_STATIC_DYNAMIC)
 #define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
 "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBASAN_LIBS
 #else
-#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
+#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS LIBSAN_RPATH
 #endif
 #endif
 
@@ -778,13 +782,13 @@ proper position among the other output files.  */
 #define STATIC_LIBTSAN_LIBS \
   " %{static-libtsan|static:%:include(libsanitizer.spec)%(link_libtsan)}"
 #ifdef LIBTSAN_EARLY_SPEC
-#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
+#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS LIBSAN_RPATH
 #elif defined(HAVE_LD_STATIC_DYNAMIC)
 #define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
 "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBTSAN_LIBS
 #else
-#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
+#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS LIBSAN_RPATH
 #endif
 #endif
 
@@ -796,13 +800,13 @@ proper position among the other output files.  */
 #define STATIC_LIBLSAN_LIBS \
   " %{static-liblsan|static:%:include(libsanitizer.spec)%(link_liblsan)}"
 #ifdef LIBLSAN_EARLY_SPEC
-#define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
+#define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS LIBSAN_RPATH
 #elif defined(HAVE_LD_STATIC_DYNAMIC)
 #define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION \
 "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBLSAN_LIBS
 #else
-#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
+#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS LIBSAN_RPATH
 #endif
 #endif
 
@@ -816,9 +820,9 @@ proper position among the other output files.  */
 #ifdef HAVE_LD_STATIC_DYNAMIC
 #define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \
 "} -lubsan %{static-libubsan:" 

Re: [PATCH] libsanitizer: Fix linkage errors for cross toolchains

2022-07-01 Thread Xi Ruoyao via Gcc-patches
Again, please send patch as plain text.

On Fri, 2022-07-01 at 08:18 +, Dimitrije Milosevic wrote:
> When we use cross toolchains, in which the GCC libraries are not
> installed within a designated system root, the shared sanitizer
> libraries link against libstdc++.so* within the same libraries.
> This directory, however, is not in RPATH, so attempting to build a
> dynamically linked application with -fsanitize=... gives a linkage
> error.
> More information can be found here:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69839.

Hmm... Is anyone really using a cross compiler without a sysroot?  PR
69839 is already closed as INVALID.  If you don't want to install GCC
runtime libraries into the sysroot (for example, for multiple GCC builds
using a shared sysroot), we have --enable-version-specific-runtime-libs
but unfortunately it's broken (PR 32415).

Please explain the actual use case and let us elaborate to see if there
is a better solution.

> gcc/ChangeLog:
>   * gcc.c (LIBSAN_RPATH): New macro.
>   (LIBASAN_SPEC): Add LIBSAN_RPATH.
>   (LIBUBSAN_SPEC): Likewise.
>  (LIBTSAN_SPEC): Likewise.
>  (LIBLSAN_SPEC): Likewise.
> 
> libsanitizer/ChangeLog:
>   * configure.ac (link_libsan_rpath): New config variable.
>   * libsanitizer.spec.in (link_libsan_rpath): New spec.
>   * configure (link_libsan_rpath): New config variable.
>   * Makefile.in (link_libsan_rpath): Define new Makefile
> variable.
>   * asan/Makefile.in: Likewise.
>   * interception/Makefile.in: Likewise.
>   * libbacktrace/Makefile.in: Likewise.
>   * lsan/Makefile.in: Likewise.
>   * sanitizer_common/Makefile.in: Likewise.
>   * tsan/Makefile.in: Likewise.
>   * ubsan/Makefile.in: Likewise.
>   * hwasan/Makefile.in: Likewise.
> 
> ---
> 
>  gcc/gcc.cc                                | 20 
>  libsanitizer/Makefile.in                  |  1 +
>  libsanitizer/asan/Makefile.in             |  1 +
>  libsanitizer/configure                    | 10 --
>  libsanitizer/configure.ac                 |  7 +++
>  libsanitizer/hwasan/Makefile.in           |  1 +
>  libsanitizer/interception/Makefile.in     |  1 +
>  libsanitizer/libbacktrace/Makefile.in     |  1 +
>  libsanitizer/libsanitizer.spec.in         |  2 ++
>  libsanitizer/lsan/Makefile.in             |  1 +
>  libsanitizer/sanitizer_common/Makefile.in |  1 +
>  libsanitizer/tsan/Makefile.in             |  1 +
>  libsanitizer/ubsan/Makefile.in            |  1 +
>  13 files changed, 38 insertions(+), 10 deletions(-)
> 
> 
> diff --git a/gcc/gcc.cc b/gcc/gcc.ccindex 299e09c4f54..37ff75f1ad5
> 100644
> --- a/gcc/gcc.cc
> +++ b/gcc/gcc.cc
> @@ -738,17 +738,21 @@ proper position among the other output files.
>  */
>  #define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
>  #endif
>  
> +#ifndef LIBSAN_RPATH
> +#define LIBSAN_RPATH "
> %:include(libsanitizer.spec)%(link_libsan_rpath)"
> +#endif
> +
>  #ifndef LIBASAN_SPEC
>  #define STATIC_LIBASAN_LIBS \
>    " %{static-
> libasan|static:%:include(libsanitizer.spec)%(link_libasan)}"
>  #ifdef LIBASAN_EARLY_SPEC
> -#define LIBASAN_SPEC STATIC_LIBASAN_LIBS
> +#define LIBASAN_SPEC STATIC_LIBASAN_LIBS LIBSAN_RPATH
>  #elif defined(HAVE_LD_STATIC_DYNAMIC)
>  #define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
>                      "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION
> "}" \
>                      STATIC_LIBASAN_LIBS
>  #else
> -#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
> +#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS LIBSAN_RPATH
>  #endif
>  #endif
>  
> @@ -778,13 +782,13 @@ proper position among the other output files.
>  */
>  #define STATIC_LIBTSAN_LIBS \
>    " %{static-
> libtsan|static:%:include(libsanitizer.spec)%(link_libtsan)}"
>  #ifdef LIBTSAN_EARLY_SPEC
> -#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
> +#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS LIBSAN_RPATH
>  #elif defined(HAVE_LD_STATIC_DYNAMIC)
>  #define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
>                      "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION
> "}" \
>                      STATIC_LIBTSAN_LIBS
>  #else
> -#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
> +#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS LIBSAN_RPATH
>  #endif
>  #endif
>  
> @@ -793,7 +797,7 @@ proper position among the other output files.  */
>  #endif
>  
>  #ifndef LIBLSAN_SPEC
> -#define STATIC_LIBLSAN_LIBS \
> +#define STATIC_LIBLSAN_LIBS LIBSAN_RPATH \
>    " %{static-
> liblsan|static:%:include(libsanitizer.spec)%(link_liblsan)}"
>  #ifdef LIBLSAN_EARLY_SPEC
>  #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
> @@ -802,7 +806,7 @@ proper position among the other output files.  */
>                      "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION
> "}" \
>                      STATIC_LIBLSAN_LIBS
>  #else
> -#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
> +#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS LIBSAN_RPATH

[PATCH] libsanitizer: Fix linkage errors for cross toolchains

2022-07-01 Thread Dimitrije Milosevic
When we use cross toolchains, in which the GCC libraries are not installed 
within a designated system root, the shared sanitizer libraries link against 
libstdc++.so* within the same libraries. This directory, however, is not in 
RPATH, so attempting to build a dynamically linked application with 
-fsanitize=... gives a linkage error.
More information can be found here: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69839.


gcc/ChangeLog:
* gcc.c (LIBSAN_RPATH): New macro.
(LIBASAN_SPEC): Add LIBSAN_RPATH.
(LIBUBSAN_SPEC): Likewise.
 (LIBTSAN_SPEC): Likewise.
 (LIBLSAN_SPEC): Likewise.

libsanitizer/ChangeLog:
* configure.ac (link_libsan_rpath): New config variable.
* libsanitizer.spec.in (link_libsan_rpath): New spec.
* configure (link_libsan_rpath): New config variable.
* Makefile.in (link_libsan_rpath): Define new Makefile variable.
* asan/Makefile.in: Likewise.
* interception/Makefile.in: Likewise.
* libbacktrace/Makefile.in: Likewise.
* lsan/Makefile.in: Likewise.
* sanitizer_common/Makefile.in: Likewise.
* tsan/Makefile.in: Likewise.
* ubsan/Makefile.in: Likewise.
* hwasan/Makefile.in: Likewise.

---

 gcc/gcc.cc| 20 
 libsanitizer/Makefile.in  |  1 +
 libsanitizer/asan/Makefile.in |  1 +
 libsanitizer/configure| 10 --
 libsanitizer/configure.ac |  7 +++
 libsanitizer/hwasan/Makefile.in   |  1 +
 libsanitizer/interception/Makefile.in |  1 +
 libsanitizer/libbacktrace/Makefile.in |  1 +
 libsanitizer/libsanitizer.spec.in |  2 ++
 libsanitizer/lsan/Makefile.in |  1 +
 libsanitizer/sanitizer_common/Makefile.in |  1 +
 libsanitizer/tsan/Makefile.in |  1 +
 libsanitizer/ubsan/Makefile.in|  1 +
 13 files changed, 38 insertions(+), 10 deletions(-)


diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 299e09c4f54..37ff75f1ad5 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -738,17 +738,21 @@ proper position among the other output files.  */
 #define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
 #endif

+#ifndef LIBSAN_RPATH
+#define LIBSAN_RPATH " %:include(libsanitizer.spec)%(link_libsan_rpath)"
+#endif
+
 #ifndef LIBASAN_SPEC
 #define STATIC_LIBASAN_LIBS \
   " %{static-libasan|static:%:include(libsanitizer.spec)%(link_libasan)}"
 #ifdef LIBASAN_EARLY_SPEC
-#define LIBASAN_SPEC STATIC_LIBASAN_LIBS
+#define LIBASAN_SPEC STATIC_LIBASAN_LIBS LIBSAN_RPATH
 #elif defined(HAVE_LD_STATIC_DYNAMIC)
 #define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
 "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBASAN_LIBS
 #else
-#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
+#define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS LIBSAN_RPATH
 #endif
 #endif

@@ -778,13 +782,13 @@ proper position among the other output files.  */
 #define STATIC_LIBTSAN_LIBS \
   " %{static-libtsan|static:%:include(libsanitizer.spec)%(link_libtsan)}"
 #ifdef LIBTSAN_EARLY_SPEC
-#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
+#define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS LIBSAN_RPATH
 #elif defined(HAVE_LD_STATIC_DYNAMIC)
 #define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
 "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBTSAN_LIBS
 #else
-#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
+#define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS LIBSAN_RPATH
 #endif
 #endif

@@ -793,7 +797,7 @@ proper position among the other output files.  */
 #endif

 #ifndef LIBLSAN_SPEC
-#define STATIC_LIBLSAN_LIBS \
+#define STATIC_LIBLSAN_LIBS LIBSAN_RPATH \
   " %{static-liblsan|static:%:include(libsanitizer.spec)%(link_liblsan)}"
 #ifdef LIBLSAN_EARLY_SPEC
 #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
@@ -802,7 +806,7 @@ proper position among the other output files.  */
 "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
 STATIC_LIBLSAN_LIBS
 #else
-#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
+#define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS LIBSAN_RPATH
 #endif
 #endif

@@ -816,9 +820,9 @@ proper position among the other output files.  */
 #ifdef HAVE_LD_STATIC_DYNAMIC
 #define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \
 "} -lubsan %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
-STATIC_LIBUBSAN_LIBS
+STATIC_LIBUBSAN_LIBS LIBSAN_RPATH
 #else
-#define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS
+#define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS LIBSAN_RPATH
 #endif
 #endif

diff --git a/libsanitizer/Makefile.in b/libsanitizer/Makefile.in
index 65e7f2e9553..ef71407a512 100644
--- a/libsanitizer/Makefile.in
+++ b/libsanitizer/Makefile.in
@@ -333,6 +333,7 @@ libexecdir = @libexecdir@
 link_libasan =