[PATCH] libsanitizer: asan: Always skip first object from dl_iterate_phdr

2022-04-19 Thread Michael Forney
All platforms return the main executable as the first dl_phdr_info.
FreeBSD, NetBSD, Solaris, and Linux-musl place the executable name
in the dlpi_name field of this entry. It appears that only Linux-glibc
uses the empty string.

To make this work generically on all platforms, unconditionally
skip the first object (like is currently done for FreeBSD and NetBSD).
This fixes first DSO detection on Linux-musl. It also would likely
fix detection on Solaris/Illumos if it were to gain PIE support
(since dlpi_addr would not be NULL).

Additionally, only skip the Linux vDSO on Linux.

Finally, use the empty string as the "seen first dl_phdr_info"
marker rather than (char *)-1. If there was no other object, we
would try to dereference it for a string comparison.

Cherry-picked from upstream commit 795b07f5498c.

libsanitizer/

* asan/asan_linux.cpp: Always skip first object from
dl_iterate_phdr.
---
Is it possible that this change might make gcc 12? It fixes asan
on musl (without setting ASAN_OPTIONS=verify_asan_link_order=false),
which would be quite nice since this will be the first release that
libsanitizer works on musl.

 libsanitizer/asan/asan_linux.cpp | 30 --
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/libsanitizer/asan/asan_linux.cpp b/libsanitizer/asan/asan_linux.cpp
index ad3693d5e6a..89ee48db7a2 100644
--- a/libsanitizer/asan/asan_linux.cpp
+++ b/libsanitizer/asan/asan_linux.cpp
@@ -131,30 +131,24 @@ static int FindFirstDSOCallback(struct dl_phdr_info 
*info, size_t size,
   VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", info->dlpi_name,
   (void *)info->dlpi_addr);
 
-  // Continue until the first dynamic library is found
-  if (!info->dlpi_name || info->dlpi_name[0] == 0)
-return 0;
-
-  // Ignore vDSO
-  if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
-return 0;
+  const char **name = (const char **)data;
 
-#if SANITIZER_FREEBSD || SANITIZER_NETBSD
   // Ignore first entry (the main program)
-  char **p = (char **)data;
-  if (!(*p)) {
-*p = (char *)-1;
+  if (!*name) {
+*name = "";
 return 0;
   }
-#endif
 
-#if SANITIZER_SOLARIS
-  // Ignore executable on Solaris
-  if (info->dlpi_addr == 0)
+#if SANITIZER_LINUX
+  // Ignore vDSO. glibc versions earlier than 2.15 (and some patched
+  // by distributors) return an empty name for the vDSO entry, so
+  // detect this as well.
+  if (!info->dlpi_name[0] ||
+  internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
 return 0;
-#endif
+#endif
 
-  *(const char **)data = info->dlpi_name;
+  *name = info->dlpi_name;
   return 1;
 }
 
@@ -175,7 +169,7 @@ void AsanCheckDynamicRTPrereqs() {
   // Ensure that dynamic RT is the first DSO in the list
   const char *first_dso_name = nullptr;
   dl_iterate_phdr(FindFirstDSOCallback, _dso_name);
-  if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
+  if (first_dso_name && first_dso_name[0] && !IsDynamicRTName(first_dso_name)) 
{
 Report("ASan runtime does not come first in initial library list; "
"you should either link runtime to your application or "
"manually preload it with LD_PRELOAD.\n");
-- 
2.35.1



[PATCH] config: Backport "Rely less on internal symbols" (serial 68) to gettext.m4

2021-05-23 Thread Michael Forney
GNU gettext introduced this change[0] in version 0.19.8 to fix
gettext detection with musl libc, since it does not define these
internal symbols.

This allows gcc to build with musl gettext rather than falling back
to the bundled version.

[0] https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commit;h=b67399b4

2021-05-23  Michael Forney  

config/ChangeLog:

* gettext.m4 (AM_GNU_GETTEXT): Skip checks for the internal
symbols _nl_msg_cat_cntr, _nl_domain_bindings, and
_nl_expand_alias, if __GNU_GETTEXT_SUPPORTED_REVISION is defined.
Backport of gettext serial 68 patch.

intl/ChangeLog:

* configure: Regenerate.
---
 config/gettext.m4 | 52 +++
 1 file changed, 35 insertions(+), 17 deletions(-)

diff --git a/config/gettext.m4 b/config/gettext.m4
index 45fa6b4ab76..5b42bb16523 100644
--- a/config/gettext.m4
+++ b/config/gettext.m4
@@ -128,6 +128,13 @@ AC_DEFUN([AM_GNU_GETTEXT],
 
 AC_CACHE_CHECK([for GNU gettext in libc], gt_cv_func_gnugettext_libc,
  [AC_TRY_LINK([#include 
+#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
+extern int _nl_msg_cat_cntr;
+extern int *_nl_domain_bindings;
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION (_nl_msg_cat_cntr + 
*_nl_domain_bindings)
+#else
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION 0
+#endif
 ]ifelse([$2], [need-formatstring-macros],
 [#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
 #define __GNU_GETTEXT_SUPPORTED_REVISION(major) ((major) == 0 ? 0 : -1)
@@ -135,10 +142,9 @@ AC_DEFUN([AM_GNU_GETTEXT],
 changequote(,)dnl
 typedef int array [2 * (__GNU_GETTEXT_SUPPORTED_REVISION(0) >= 1) - 1];
 changequote([,])dnl
-], [])[extern int _nl_msg_cat_cntr;
-extern int *_nl_domain_bindings;],
+], []),
 [bindtextdomain ("", "");
-return (int) gettext ("")]ifelse([$2], [need-ngettext], [ + (int) ngettext 
("", "", 0)], [])[ + _nl_msg_cat_cntr + *_nl_domain_bindings],
+return (int) gettext ("")]ifelse([$2], [need-ngettext], [ + (int) ngettext 
("", "", 0)], [])[ + __GNU_GETTEXT_SYMBOL_EXPRESSION],
 gt_cv_func_gnugettext_libc=yes,
 gt_cv_func_gnugettext_libc=no)])
 
@@ -160,6 +166,17 @@ return (int) gettext ("")]ifelse([$2], [need-ngettext], [ 
+ (int) ngettext ("",
 LIBS="$LIBS $LIBINTL"
 dnl Now see whether libintl exists and does not depend on libiconv.
 AC_TRY_LINK([#include 
+#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
+extern int _nl_msg_cat_cntr;
+extern
+#ifdef __cplusplus
+"C"
+#endif
+const char *_nl_expand_alias ();
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION (_nl_msg_cat_cntr + *_nl_expand_alias 
(0))
+#else
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION 0
+#endif
 ]ifelse([$2], [need-formatstring-macros],
 [#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
 #define __GNU_GETTEXT_SUPPORTED_REVISION(major) ((major) == 0 ? 0 : -1)
@@ -167,20 +184,26 @@ return (int) gettext ("")]ifelse([$2], [need-ngettext], [ 
+ (int) ngettext ("",
 changequote(,)dnl
 typedef int array [2 * (__GNU_GETTEXT_SUPPORTED_REVISION(0) >= 1) - 1];
 changequote([,])dnl
-], [])[extern int _nl_msg_cat_cntr;
-extern
-#ifdef __cplusplus
-"C"
-#endif
-const char *_nl_expand_alias ();],
+], []),
   [bindtextdomain ("", "");
-return (int) gettext ("")]ifelse([$2], [need-ngettext], [ + (int) ngettext 
("", "", 0)], [])[ + _nl_msg_cat_cntr + *_nl_expand_alias (0)],
+return (int) gettext ("")]ifelse([$2], [need-ngettext], [ + (int) ngettext 
("", "", 0)], [])[ + __GNU_GETTEXT_SYMBOL_EXPRESSION],
   gt_cv_func_gnugettext_libintl=yes,
   gt_cv_func_gnugettext_libintl=no)
 dnl Now see whether libintl exists and depends on libiconv.
 if test "$gt_cv_func_gnugettext_libintl" != yes && test -n 
"$LIBICONV"; then
   LIBS="$LIBS $LIBICONV"
   AC_TRY_LINK([#include 
+#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
+extern int _nl_msg_cat_cntr;
+extern
+#ifdef __cplusplus
+"C"
+#endif
+const char *_nl_expand_alias ();
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION (_nl_msg_cat_cntr + *_nl_expand_alias 
(0))
+#else
+#define __GNU_GETTEXT_SYMBOL_EXPRESSION 0
+#endif
 ]ifelse([$2], [need-formatstring-macros],
 [#ifndef __GNU_GETTEXT_SUPPORTED_REVISION
 #define __GNU_GETTEXT_SUPPORTED_REVISION(major) ((major) == 0 ? 0 : -1)
@@ -188,14 +211,9 @@ return (int) gettext ("")]ifelse([$2], [need-ngettext], [ 
+ (int) ngettext ("",
 changequote(,)dnl
 typedef int array [2 * (__GNU_GETTEXT_SUPPORTED_REVISION(0) >= 1) - 1];
 changequote([,])dnl
-], [])[extern int _nl_msg_cat_cntr;
-extern
-#ifdef __cplusplus
-"C"
-#endif
-const char *_nl_expand_alias ();],
+], []),
 [bindtextdomain ("&qu

[PATCH] Add XGCC_FLAGS_FOR_TARGET to CXX in POSTSTAGE1_CXX_EXPORT

2020-03-25 Thread Michael Forney
This contains the necessary sysroot flags if configured with
--with-build-sysroot, and matches the corresponding value for CC
in POSTSTAGE1_HOST_EXPORTS.

2020-03-25  Michael Forney  

* Makefile.tpl (POSTSTAGE1_HOST_EXPORTS): Add XGCC_FLAGS_FOR_TARGET
and TFLAGS to CXX.
* Makefile.in: Regenerate.
---
I ran into this when building gcc with --enable-bootstrap and
--with-build-sysroot. Since the sysroot flag doesn't get passed to
xg++, the build fails at configure-stage2-gcc, since it can't find
the right headers:

checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/tmp/gcc/host-x86_64-linux-musl/gcc':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details
make[2]: *** [Makefile:4434: configure-stage2-gcc] Error 1

 Makefile.in  | 4 ++--
 Makefile.tpl | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Makefile.in b/Makefile.in
index 86c0c6d5b2d..686983de233 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -248,8 +248,8 @@ POSTSTAGE1_CXX_EXPORT = \
  `if $(LEAN); then echo ' -isystem '; else echo ' -I'; 
fi`$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/include \
  `if $(LEAN); then echo ' -isystem '; else echo ' -I'; 
fi`$$s/libstdc++-v3/libsupc++ \
  -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/src/.libs \
- -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/libsupc++/.libs"; \
- export CXX; \
+ -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/libsupc++/.libs \
+ $(XGCC_FLAGS_FOR_TARGET) $$TFLAGS"; export CXX; \
CXX_FOR_BUILD="$$CXX"; export CXX_FOR_BUILD;
 @endif target-libstdc++-v3-bootstrap
 
diff --git a/Makefile.tpl b/Makefile.tpl
index efed1511750..b627069e8c8 100644
--- a/Makefile.tpl
+++ b/Makefile.tpl
@@ -251,8 +251,8 @@ POSTSTAGE1_CXX_EXPORT = \
  `if $(LEAN); then echo ' -isystem '; else echo ' -I'; 
fi`$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/include \
  `if $(LEAN); then echo ' -isystem '; else echo ' -I'; 
fi`$$s/libstdc++-v3/libsupc++ \
  -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/src/.libs \
- -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/libsupc++/.libs"; \
- export CXX; \
+ -L$$r/prev-$(TARGET_SUBDIR)/libstdc++-v3/libsupc++/.libs \
+ $(XGCC_FLAGS_FOR_TARGET) $$TFLAGS"; export CXX; \
CXX_FOR_BUILD="$$CXX"; export CXX_FOR_BUILD;
 @endif target-libstdc++-v3-bootstrap
 
-- 
2.26.0



[PATCH] Add CXX_FOR_BUILD to HOST_EXPORTS

2019-06-18 Thread Michael Forney
gcc/configure needs this to generate auto-build.h using the right C++
compiler.

2019-06-18  Michael Forney  

* Makefile.tpl (HOST_EXPORTS): Add CXX_FOR_BUILD.
* Makefile.in: Regenerate.

---
I ran into this since I needed to pass some special flags to the build
C++ compiler for my system and was getting the mysterious error

make[3]: *** No rule to make target 'auto-build.h', needed by 
'build/genmddeps.o'.  Stop.

Turns out this was because the sub-configure run by gcc/configure was
failing because my CXX_FOR_BUILD was being ignored, but it is not treated
as a fatal error (#65794). The configure directory containing config.log
is subsequently deleted, making it difficult to figure out what actually
went wrong.

 Makefile.in  | 1 +
 Makefile.tpl | 1 +
 2 files changed, 2 insertions(+)

diff --git a/Makefile.in b/Makefile.in
index 02cc7a39094..86c0c6d5b2d 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -198,6 +198,7 @@ HOST_EXPORTS = \
AR="$(AR)"; export AR; \
AS="$(AS)"; export AS; \
CC_FOR_BUILD="$(CC_FOR_BUILD)"; export CC_FOR_BUILD; \
+   CXX_FOR_BUILD="$(CXX_FOR_BUILD)"; export CXX_FOR_BUILD; \
DLLTOOL="$(DLLTOOL)"; export DLLTOOL; \
LD="$(LD)"; export LD; \
LDFLAGS="$(STAGE1_LDFLAGS) $(LDFLAGS)"; export LDFLAGS; \
diff --git a/Makefile.tpl b/Makefile.tpl
index 1cdc023c82f..efed1511750 100644
--- a/Makefile.tpl
+++ b/Makefile.tpl
@@ -201,6 +201,7 @@ HOST_EXPORTS = \
AR="$(AR)"; export AR; \
AS="$(AS)"; export AS; \
CC_FOR_BUILD="$(CC_FOR_BUILD)"; export CC_FOR_BUILD; \
+   CXX_FOR_BUILD="$(CXX_FOR_BUILD)"; export CXX_FOR_BUILD; \
DLLTOOL="$(DLLTOOL)"; export DLLTOOL; \
LD="$(LD)"; export LD; \
LDFLAGS="$(STAGE1_LDFLAGS) $(LDFLAGS)"; export LDFLAGS; \
-- 
2.20.1



[PATCH] libiberty: Don't use VLAs if __STDC_NO_VLA__ is non-zero

2019-05-30 Thread Michael Forney
VLAs are optional in C11, and an implementation that does not support
them will define __STDC_NO_VLA__ to 1.

2019-05-30  Michael Forney  

* cp-demangle.c: Don't define CP_DYNAMIC_ARRAYS if __STDC_NO_VLA__
is non-zero.
---
 libiberty/cp-demangle.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libiberty/cp-demangle.c b/libiberty/cp-demangle.c
index 4e5b733b548..aa78c86dd44 100644
--- a/libiberty/cp-demangle.c
+++ b/libiberty/cp-demangle.c
@@ -192,9 +192,9 @@ static void d_init_info (const char *, int, size_t, struct 
d_info *);
 #else
 #ifdef __STDC__
 #ifdef __STDC_VERSION__
-#if __STDC_VERSION__ >= 199901L
+#if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
 #define CP_DYNAMIC_ARRAYS
-#endif /* __STDC__VERSION >= 199901L */
+#endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
 #endif /* defined (__STDC_VERSION__) */
 #endif /* defined (__STDC__) */
 #endif /* ! defined (__GNUC__) */
-- 
2.20.1



[PATCH] libiberty: Only declare vasprintf if it was checked for

2019-04-23 Thread Michael Forney
This matches how the the rest of the libc functions are declared and
prevents a potential conflict with the system declaration when libiberty.h
is included from a directory that did not check for vasprintf, but it is
available. It also corresponds to a similar previous change for asprintf
(r223589).

2019-04-22  Michael Forney  

* libiberty.h (vasprintf): Don't declare if HAVE_DECL_VASPRINTF
is not defined.

---
 include/libiberty.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/libiberty.h b/include/libiberty.h
index 57476135026..e11aa97f0ba 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -649,7 +649,7 @@ extern int asprintf (char **, const char *, ...) 
ATTRIBUTE_PRINTF_2;
 
 extern char *xasprintf (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_PRINTF_1;
 
-#if !HAVE_DECL_VASPRINTF
+#if defined(HAVE_DECL_VASPRINTF) && !HAVE_DECL_VASPRINTF
 /* Like vsprintf but provides a pointer to malloc'd storage, which
must be freed by the caller.  */
 
-- 
2.20.1