6c0556
commit 851f32cf7bf7067f73b991610778915edd57d7b4
6c0556
Author: Florian Weimer <fweimer@redhat.com>
6c0556
Date:   Tue Mar 2 14:38:42 2021 +0100
6c0556
6c0556
    ld.so: Implement the --list-diagnostics option
6c0556
6c0556
diff --git a/elf/Makefile b/elf/Makefile
6c0556
index aa65ec59f143bccf..d246f1c0d9e019fd 100644
6c0556
--- a/elf/Makefile
6c0556
+++ b/elf/Makefile
6c0556
@@ -64,7 +64,7 @@ elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \
6c0556
 # interpreter and operating independent of libc.
6c0556
 rtld-routines	= rtld $(all-dl-routines) dl-sysdep dl-environ dl-minimal \
6c0556
   dl-error-minimal dl-conflict dl-hwcaps dl-hwcaps_split dl-hwcaps-subdirs \
6c0556
-  dl-usage
6c0556
+  dl-usage dl-diagnostics dl-diagnostics-kernel dl-diagnostics-cpu
6c0556
 all-rtld-routines = $(rtld-routines) $(sysdep-rtld-routines)
6c0556
 
6c0556
 CFLAGS-dl-runtime.c += -fexceptions -fasynchronous-unwind-tables
6c0556
@@ -672,6 +672,9 @@ CFLAGS-cache.c += $(SYSCONF-FLAGS)
6c0556
 CFLAGS-rtld.c += $(SYSCONF-FLAGS)
6c0556
 CFLAGS-dl-usage.c += $(SYSCONF-FLAGS) \
6c0556
   -D'RTLD="$(rtlddir)/$(rtld-installed-name)"'
6c0556
+CFLAGS-dl-diagnostics.c += $(SYSCONF-FLAGS) \
6c0556
+  -D'PREFIX="$(prefix)"' \
6c0556
+  -D'RTLD="$(rtlddir)/$(rtld-installed-name)"'
6c0556
 
6c0556
 cpp-srcs-left := $(all-rtld-routines:=.os)
6c0556
 lib := rtld
6c0556
diff --git a/elf/dl-diagnostics-cpu.c b/elf/dl-diagnostics-cpu.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..f7d149764bcb35a1
6c0556
--- /dev/null
6c0556
+++ b/elf/dl-diagnostics-cpu.c
6c0556
@@ -0,0 +1,24 @@
6c0556
+/* Print CPU diagnostics data in ld.so.  Stub version.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#include <dl-diagnostics.h>
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_cpu (void)
6c0556
+{
6c0556
+}
6c0556
diff --git a/elf/dl-diagnostics-kernel.c b/elf/dl-diagnostics-kernel.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..831c358f1463cbf4
6c0556
--- /dev/null
6c0556
+++ b/elf/dl-diagnostics-kernel.c
6c0556
@@ -0,0 +1,24 @@
6c0556
+/* Print kernel diagnostics data in ld.so.  Stub version.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#include <dl-diagnostics.h>
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_kernel (void)
6c0556
+{
6c0556
+}
6c0556
diff --git a/elf/dl-diagnostics.c b/elf/dl-diagnostics.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..bef224b36cbf5fc3
6c0556
--- /dev/null
6c0556
+++ b/elf/dl-diagnostics.c
6c0556
@@ -0,0 +1,265 @@
6c0556
+/* Print diagnostics data in ld.so.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#include <gnu/lib-names.h>
6c0556
+#include <stdbool.h>
6c0556
+#include <stddef.h>
6c0556
+#include <unistd.h>
6c0556
+
6c0556
+#include <dl-diagnostics.h>
6c0556
+#include <dl-hwcaps.h>
6c0556
+#include <dl-main.h>
6c0556
+#include <dl-procinfo.h>
6c0556
+#include <dl-sysdep.h>
6c0556
+#include <ldsodefs.h>
6c0556
+#include "trusted-dirs.h"
6c0556
+#include "version.h"
6c0556
+
6c0556
+/* Write CH to standard output.  */
6c0556
+static void
6c0556
+_dl_putc (char ch)
6c0556
+{
6c0556
+  _dl_write (STDOUT_FILENO, &ch, 1);
6c0556
+}
6c0556
+
6c0556
+/* Print CH to standard output, quoting it if necessary.  */
6c0556
+static void
6c0556
+print_quoted_char (char ch)
6c0556
+{
6c0556
+  if (ch < ' ' || ch > '~')
6c0556
+    {
6c0556
+      char buf[4];
6c0556
+      buf[0] = '\\';
6c0556
+      buf[1] = '0' + ((ch >> 6) & 7);
6c0556
+      buf[2] = '0' + ((ch >> 6) & 7);
6c0556
+      buf[3] = '0' + (ch & 7);
6c0556
+      _dl_write (STDOUT_FILENO, buf, 4);
6c0556
+    }
6c0556
+  else
6c0556
+    {
6c0556
+      if (ch == '\\' || ch == '"')
6c0556
+        _dl_putc ('\\');
6c0556
+      _dl_putc (ch);
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+/* Print S of LEN bytes to standard output, quoting characters as
6c0556
+   needed.  */
6c0556
+static void
6c0556
+print_string_length (const char *s, size_t len)
6c0556
+{
6c0556
+  _dl_putc ('"');
6c0556
+  for (size_t i = 0; i < len; ++i)
6c0556
+    print_quoted_char (s[i]);
6c0556
+  _dl_putc ('"');
6c0556
+}
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_print_string (const char *s)
6c0556
+{
6c0556
+  if (s == NULL)
6c0556
+    {
6c0556
+      _dl_printf ("0x0");
6c0556
+      return;
6c0556
+    }
6c0556
+
6c0556
+  _dl_putc ('"');
6c0556
+  while (*s != '\0')
6c0556
+    {
6c0556
+      print_quoted_char (*s);
6c0556
+      ++s;
6c0556
+    }
6c0556
+  _dl_putc ('"');
6c0556
+}
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_print_labeled_string (const char *label, const char *s)
6c0556
+{
6c0556
+  _dl_printf ("%s=", label);
6c0556
+  _dl_diagnostics_print_string (s);
6c0556
+  _dl_putc ('\n');
6c0556
+}
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_print_labeled_value (const char *label, uint64_t value)
6c0556
+{
6c0556
+  if (sizeof (value) == sizeof (unsigned long int))
6c0556
+    /* _dl_printf can print 64-bit values directly.  */
6c0556
+    _dl_printf ("%s=0x%lx\n", label, (unsigned long int) value);
6c0556
+  else
6c0556
+    {
6c0556
+      uint32_t high = value >> 32;
6c0556
+      uint32_t low = value;
6c0556
+      if (high == 0)
6c0556
+        _dl_printf ("%s=0x%x\n", label, low);
6c0556
+      else
6c0556
+        _dl_printf ("%s=0x%x%08x\n", label, high, low);
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+/* Return true if ENV is an unfiltered environment variable.  */
6c0556
+static bool
6c0556
+unfiltered_envvar (const char *env, size_t *name_length)
6c0556
+{
6c0556
+  char *env_equal = strchr (env, '=');
6c0556
+  if (env_equal == NULL)
6c0556
+    {
6c0556
+      /* Always dump malformed entries.  */
6c0556
+      *name_length = strlen (env);
6c0556
+      return true;
6c0556
+    }
6c0556
+  size_t envname_length = env_equal - env;
6c0556
+  *name_length = envname_length;
6c0556
+
6c0556
+  /* LC_ and LD_ variables.  */
6c0556
+  if (env[0] == 'L' && (env[1] == 'C' || env[1] == 'D')
6c0556
+      && env[2] == '_')
6c0556
+    return true;
6c0556
+
6c0556
+  /* MALLOC_ variables.  */
6c0556
+  if (strncmp (env, "MALLOC_", strlen ("MALLOC_")) == 0)
6c0556
+    return true;
6c0556
+
6c0556
+  static const char unfiltered[] =
6c0556
+    "DATEMSK\0"
6c0556
+    "GCONV_PATH\0"
6c0556
+    "GETCONF_DIR\0"
6c0556
+    "GETCONF_DIR\0"
6c0556
+    "GLIBC_TUNABLES\0"
6c0556
+    "GMON_OUTPUT_PREFIX\0"
6c0556
+    "HESIOD_CONFIG\0"
6c0556
+    "HES_DOMAIN\0"
6c0556
+    "HOSTALIASES\0"
6c0556
+    "I18NPATH\0"
6c0556
+    "IFS\0"
6c0556
+    "LANG\0"
6c0556
+    "LOCALDOMAIN\0"
6c0556
+    "LOCPATH\0"
6c0556
+    "MSGVERB\0"
6c0556
+    "NIS_DEFAULTS\0"
6c0556
+    "NIS_GROUP\0"
6c0556
+    "NIS_PATH\0"
6c0556
+    "NLSPATH\0"
6c0556
+    "PATH\0"
6c0556
+    "POSIXLY_CORRECT\0"
6c0556
+    "RESOLV_HOST_CONF\0"
6c0556
+    "RES_OPTIONS\0"
6c0556
+    "SEV_LEVEL\0"
6c0556
+    "TMPDIR\0"
6c0556
+    "TZ\0"
6c0556
+    "TZDIR\0"
6c0556
+    /* Two null bytes at the end to mark the end of the list via an
6c0556
+       empty substring.  */
6c0556
+    ;
6c0556
+  for (const char *candidate = unfiltered; *candidate != '\0'; )
6c0556
+    {
6c0556
+      size_t candidate_length = strlen (candidate);
6c0556
+      if (candidate_length == envname_length
6c0556
+          && memcmp (candidate, env, candidate_length) == 0)
6c0556
+        return true;
6c0556
+      candidate += candidate_length + 1;
6c0556
+    }
6c0556
+
6c0556
+  return false;
6c0556
+}
6c0556
+
6c0556
+/* Dump the process environment.  */
6c0556
+static void
6c0556
+print_environ (char **environ)
6c0556
+{
6c0556
+  unsigned int index = 0;
6c0556
+  for (char **envp = environ; *envp != NULL; ++envp)
6c0556
+    {
6c0556
+      char *env = *envp;
6c0556
+      size_t name_length;
6c0556
+      bool unfiltered = unfiltered_envvar (env, &name_length);
6c0556
+      _dl_printf ("env%s[0x%x]=",
6c0556
+                  unfiltered ? "" : "_filtered", index);
6c0556
+      if (unfiltered)
6c0556
+        _dl_diagnostics_print_string (env);
6c0556
+      else
6c0556
+        print_string_length (env, name_length);
6c0556
+      _dl_putc ('\n');
6c0556
+      ++index;
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+/* Print configured paths and the built-in search path.  */
6c0556
+static void
6c0556
+print_paths (void)
6c0556
+{
6c0556
+  _dl_diagnostics_print_labeled_string ("path.prefix", PREFIX);
6c0556
+  _dl_diagnostics_print_labeled_string ("path.rtld", RTLD);
6c0556
+  _dl_diagnostics_print_labeled_string ("path.sysconfdir", SYSCONFDIR);
6c0556
+
6c0556
+  unsigned int index = 0;
6c0556
+  static const char *system_dirs = SYSTEM_DIRS "\0";
6c0556
+  for (const char *e = system_dirs; *e != '\0'; )
6c0556
+    {
6c0556
+      size_t len = strlen (e);
6c0556
+      _dl_printf ("path.system_dirs[0x%x]=", index);
6c0556
+      print_string_length (e, len);
6c0556
+      _dl_putc ('\n');
6c0556
+      ++index;
6c0556
+      e += len + 1;
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+/* Print information about the glibc version.  */
6c0556
+static void
6c0556
+print_version (void)
6c0556
+{
6c0556
+  _dl_diagnostics_print_labeled_string ("version.release", RELEASE);
6c0556
+  _dl_diagnostics_print_labeled_string ("version.version", VERSION);
6c0556
+}
6c0556
+
6c0556
+void
6c0556
+_dl_print_diagnostics (char **environ)
6c0556
+{
6c0556
+#ifdef HAVE_DL_DISCOVER_OSVERSION
6c0556
+  _dl_diagnostics_print_labeled_value
6c0556
+    ("dl_discover_osversion", _dl_discover_osversion ());
6c0556
+#endif
6c0556
+  _dl_diagnostics_print_labeled_string ("dl_dst_lib", DL_DST_LIB);
6c0556
+  _dl_diagnostics_print_labeled_value ("dl_hwcap", GLRO (dl_hwcap));
6c0556
+  _dl_diagnostics_print_labeled_value ("dl_hwcap_important", HWCAP_IMPORTANT);
6c0556
+  _dl_diagnostics_print_labeled_value ("dl_hwcap2", GLRO (dl_hwcap2));
6c0556
+  _dl_diagnostics_print_labeled_string
6c0556
+    ("dl_hwcaps_subdirs", _dl_hwcaps_subdirs);
6c0556
+  _dl_diagnostics_print_labeled_value
6c0556
+    ("dl_hwcaps_subdirs_active", _dl_hwcaps_subdirs_active ());
6c0556
+  _dl_diagnostics_print_labeled_value ("dl_osversion", GLRO (dl_osversion));
6c0556
+  _dl_diagnostics_print_labeled_value ("dl_pagesize", GLRO (dl_pagesize));
6c0556
+  _dl_diagnostics_print_labeled_string ("dl_platform", GLRO (dl_platform));
6c0556
+  _dl_diagnostics_print_labeled_string
6c0556
+    ("dl_profile_output", GLRO (dl_profile_output));
6c0556
+  _dl_diagnostics_print_labeled_value
6c0556
+    ("dl_string_platform", _dl_string_platform ( GLRO (dl_platform)));
6c0556
+
6c0556
+  _dl_diagnostics_print_labeled_string ("dso.ld", LD_SO);
6c0556
+  _dl_diagnostics_print_labeled_string ("dso.libc", LIBC_SO);
6c0556
+
6c0556
+  print_environ (environ);
6c0556
+  print_paths ();
6c0556
+  print_version ();
6c0556
+
6c0556
+  _dl_diagnostics_kernel ();
6c0556
+  _dl_diagnostics_cpu ();
6c0556
+
6c0556
+  _exit (EXIT_SUCCESS);
6c0556
+}
6c0556
diff --git a/elf/dl-diagnostics.h b/elf/dl-diagnostics.h
6c0556
new file mode 100644
6c0556
index 0000000000000000..27dcb12bca12e5b6
6c0556
--- /dev/null
6c0556
+++ b/elf/dl-diagnostics.h
6c0556
@@ -0,0 +1,46 @@
6c0556
+/* Interfaces for printing diagnostics in ld.so.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#ifndef _DL_DIAGNOSTICS_H
6c0556
+#define _DL_DIAGNOSTICS_H
6c0556
+
6c0556
+#include <stdint.h>
6c0556
+
6c0556
+/* Write the null-terminated string to standard output, surrounded in
6c0556
+   quotation marks.  */
6c0556
+void _dl_diagnostics_print_string (const char *s) attribute_hidden;
6c0556
+
6c0556
+/* Like _dl_diagnostics_print_string, but add a LABEL= prefix, and a
6c0556
+   newline character as a suffix.  */
6c0556
+void _dl_diagnostics_print_labeled_string (const char *label, const char *s)
6c0556
+  attribute_hidden;
6c0556
+
6c0556
+/* Print LABEL=VALUE to standard output, followed by a newline
6c0556
+   character.  */
6c0556
+void _dl_diagnostics_print_labeled_value (const char *label, uint64_t value)
6c0556
+  attribute_hidden;
6c0556
+
6c0556
+/* Print diagnostics data for the kernel.  Called from
6c0556
+   _dl_print_diagnostics.  */
6c0556
+void _dl_diagnostics_kernel (void) attribute_hidden;
6c0556
+
6c0556
+/* Print diagnostics data for the CPU(s).  Called from
6c0556
+   _dl_print_diagnostics.  */
6c0556
+void _dl_diagnostics_cpu (void) attribute_hidden;
6c0556
+
6c0556
+#endif /* _DL_DIAGNOSTICS_H */
6c0556
diff --git a/elf/dl-main.h b/elf/dl-main.h
6c0556
index 9e7b51d8f010e904..9fbbdb0fac09adf3 100644
6c0556
--- a/elf/dl-main.h
6c0556
+++ b/elf/dl-main.h
6c0556
@@ -63,7 +63,7 @@ struct audit_list
6c0556
 enum rtld_mode
6c0556
   {
6c0556
     rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
6c0556
-    rtld_mode_list_tunables, rtld_mode_help,
6c0556
+    rtld_mode_list_tunables, rtld_mode_list_diagnostics, rtld_mode_help,
6c0556
   };
6c0556
 
6c0556
 /* Aggregated state information extracted from environment variables
6c0556
@@ -121,4 +121,7 @@ _Noreturn void _dl_version (void) attribute_hidden;
6c0556
 _Noreturn void _dl_help (const char *argv0, struct dl_main_state *state)
6c0556
   attribute_hidden;
6c0556
 
6c0556
+/* Print a diagnostics dump.  */
6c0556
+_Noreturn void _dl_print_diagnostics (char **environ) attribute_hidden;
6c0556
+
6c0556
 #endif /* _DL_MAIN */
6c0556
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
6c0556
index 908b4894b3014b2d..e19e1791d9169da2 100644
6c0556
--- a/elf/dl-usage.c
6c0556
+++ b/elf/dl-usage.c
6c0556
@@ -261,6 +261,7 @@ setting environment variables (which would be inherited by subprocesses).\n\
6c0556
   --list-tunables       list all tunables with minimum and maximum values\n"
6c0556
 #endif
6c0556
 "\
6c0556
+  --list-diagnostics    list diagnostics information\n\
6c0556
   --help                display this help and exit\n\
6c0556
   --version             output version information and exit\n\
6c0556
 \n\
6c0556
diff --git a/elf/rtld.c b/elf/rtld.c
6c0556
index 54b621ec5ca014fa..d14c388f548d6d51 100644
6c0556
--- a/elf/rtld.c
6c0556
+++ b/elf/rtld.c
6c0556
@@ -138,6 +138,7 @@ static void dl_main_state_init (struct dl_main_state *state);
6c0556
 /* Process all environments variables the dynamic linker must recognize.
6c0556
    Since all of them start with `LD_' we are a bit smarter while finding
6c0556
    all the entries.  */
6c0556
+extern char **_environ attribute_hidden;
6c0556
 static void process_envvars (struct dl_main_state *state);
6c0556
 
6c0556
 #ifdef DL_ARGV_NOT_RELRO
6c0556
@@ -1273,6 +1274,14 @@ dl_main (const ElfW(Phdr) *phdr,
6c0556
 	    ++_dl_argv;
6c0556
 	  }
6c0556
 #endif
6c0556
+	else if (! strcmp (_dl_argv[1], "--list-diagnostics"))
6c0556
+	  {
6c0556
+	    state.mode = rtld_mode_list_diagnostics;
6c0556
+
6c0556
+	    ++_dl_skip_args;
6c0556
+	    --_dl_argc;
6c0556
+	    ++_dl_argv;
6c0556
+	  }
6c0556
 	else if (strcmp (_dl_argv[1], "--help") == 0)
6c0556
 	  {
6c0556
 	    state.mode = rtld_mode_help;
6c0556
@@ -1301,6 +1310,9 @@ dl_main (const ElfW(Phdr) *phdr,
6c0556
 	}
6c0556
 #endif
6c0556
 
6c0556
+      if (state.mode == rtld_mode_list_diagnostics)
6c0556
+	_dl_print_diagnostics (_environ);
6c0556
+
6c0556
       /* If we have no further argument the program was called incorrectly.
6c0556
 	 Grant the user some education.  */
6c0556
       if (_dl_argc < 2)
6c0556
@@ -2623,12 +2635,6 @@ a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
6c0556
     }
6c0556
 }
6c0556
 
6c0556
-/* Process all environments variables the dynamic linker must recognize.
6c0556
-   Since all of them start with `LD_' we are a bit smarter while finding
6c0556
-   all the entries.  */
6c0556
-extern char **_environ attribute_hidden;
6c0556
-
6c0556
-
6c0556
 static void
6c0556
 process_envvars (struct dl_main_state *state)
6c0556
 {
6c0556
diff --git a/sysdeps/unix/sysv/linux/dl-diagnostics-kernel.c b/sysdeps/unix/sysv/linux/dl-diagnostics-kernel.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..59f6402c547ba590
6c0556
--- /dev/null
6c0556
+++ b/sysdeps/unix/sysv/linux/dl-diagnostics-kernel.c
6c0556
@@ -0,0 +1,77 @@
6c0556
+/* Print kernel diagnostics data in ld.so.  Linux version.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#include <dl-diagnostics.h>
6c0556
+#include <ldsodefs.h>
6c0556
+#include <sys/utsname.h>
6c0556
+
6c0556
+/* Dump the auxiliary vector to standard output.  */
6c0556
+static void
6c0556
+print_auxv (void)
6c0556
+{
6c0556
+  /* See _dl_show_auxv.  The code below follows the general output
6c0556
+     format for diagnostic dumps.  */
6c0556
+  unsigned int index = 0;
6c0556
+  for (ElfW(auxv_t) *av = GLRO(dl_auxv); av->a_type != AT_NULL; ++av)
6c0556
+    {
6c0556
+      _dl_printf ("auxv[0x%x].a_type=0x%lx\n"
6c0556
+                  "auxv[0x%x].a_val=",
6c0556
+                  index, (unsigned long int) av->a_type, index);
6c0556
+      if (av->a_type == AT_EXECFN
6c0556
+          || av->a_type == AT_PLATFORM
6c0556
+          || av->a_type == AT_BASE_PLATFORM)
6c0556
+        /* The address of the strings is not useful at all, so print
6c0556
+           the strings themselvs.  */
6c0556
+        _dl_diagnostics_print_string ((const char *) av->a_un.a_val);
6c0556
+      else
6c0556
+        _dl_printf ("0x%lx", (unsigned long int) av->a_un.a_val);
6c0556
+      _dl_printf ("\n");
6c0556
+      ++index;
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+/* Print one uname entry.  */
6c0556
+static void
6c0556
+print_utsname_entry (const char *field, const char *value)
6c0556
+{
6c0556
+  _dl_printf ("uname.");
6c0556
+  _dl_diagnostics_print_labeled_string (field, value);
6c0556
+}
6c0556
+
6c0556
+/* Print information from uname, including the kernel version.  */
6c0556
+static void
6c0556
+print_uname (void)
6c0556
+{
6c0556
+  struct utsname uts;
6c0556
+  if (__uname (&uts) == 0)
6c0556
+    {
6c0556
+      print_utsname_entry ("sysname", uts.sysname);
6c0556
+      print_utsname_entry ("nodename", uts.nodename);
6c0556
+      print_utsname_entry ("release", uts.release);
6c0556
+      print_utsname_entry ("version", uts.version);
6c0556
+      print_utsname_entry ("machine", uts.machine);
6c0556
+      print_utsname_entry ("domainname", uts.domainname);
6c0556
+    }
6c0556
+}
6c0556
+
6c0556
+void
6c0556
+_dl_diagnostics_kernel (void)
6c0556
+{
6c0556
+  print_auxv ();
6c0556
+  print_uname ();
6c0556
+}