94084c
commit b693d75f0c611bce9b0ad984bad306121d42c535
94084c
Author: Florian Weimer <fweimer@redhat.com>
94084c
Date:   Fri Jan 14 20:16:05 2022 +0100
94084c
94084c
    elf: Split dl-printf.c from dl-misc.c
94084c
    
94084c
    This allows to use different compiler flags for the diagnostics
94084c
    code.
94084c
    
94084c
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
94084c
94084c
diff --git a/elf/Makefile b/elf/Makefile
94084c
index 124905f96c88ab53..52aafc89cec835ab 100644
94084c
--- a/elf/Makefile
94084c
+++ b/elf/Makefile
94084c
@@ -64,6 +64,7 @@ dl-routines = \
94084c
   dl-object \
94084c
   dl-open \
94084c
   dl-origin \
94084c
+  dl-printf \
94084c
   dl-profile \
94084c
   dl-reloc \
94084c
   dl-runtime \
94084c
diff --git a/elf/dl-misc.c b/elf/dl-misc.c
94084c
index b256d792c6198683..f17140b129343f7b 100644
94084c
--- a/elf/dl-misc.c
94084c
+++ b/elf/dl-misc.c
94084c
@@ -16,24 +16,16 @@
94084c
    License along with the GNU C Library; if not, see
94084c
    <https://www.gnu.org/licenses/>.  */
94084c
 
94084c
-#include <assert.h>
94084c
+#include <_itoa.h>
94084c
 #include <fcntl.h>
94084c
 #include <ldsodefs.h>
94084c
-#include <limits.h>
94084c
 #include <link.h>
94084c
-#include <stdarg.h>
94084c
-#include <stdlib.h>
94084c
-#include <string.h>
94084c
-#include <unistd.h>
94084c
+#include <not-cancel.h>
94084c
 #include <stdint.h>
94084c
+#include <stdlib.h>
94084c
 #include <sys/mman.h>
94084c
-#include <sys/param.h>
94084c
 #include <sys/stat.h>
94084c
-#include <sys/uio.h>
94084c
-#include <sysdep.h>
94084c
-#include <_itoa.h>
94084c
-#include <dl-writev.h>
94084c
-#include <not-cancel.h>
94084c
+#include <unistd.h>
94084c
 
94084c
 /* Read the whole contents of FILE into new mmap'd space with given
94084c
    protections.  *SIZEP gets the size of the file.  On error MAP_FAILED
94084c
@@ -70,270 +62,6 @@ _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
94084c
   return result;
94084c
 }
94084c
 
94084c
-
94084c
-/* Bare-bones printf implementation.  This function only knows about
94084c
-   the formats and flags needed and can handle only up to 64 stripes in
94084c
-   the output.  */
94084c
-static void
94084c
-_dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
94084c
-{
94084c
-# define NIOVMAX 64
94084c
-  struct iovec iov[NIOVMAX];
94084c
-  int niov = 0;
94084c
-  pid_t pid = 0;
94084c
-  char pidbuf[12];
94084c
-
94084c
-  while (*fmt != '\0')
94084c
-    {
94084c
-      const char *startp = fmt;
94084c
-
94084c
-      if (tag_p > 0)
94084c
-	{
94084c
-	  /* Generate the tag line once.  It consists of the PID and a
94084c
-	     colon followed by a tab.  */
94084c
-	  if (pid == 0)
94084c
-	    {
94084c
-	      char *p;
94084c
-	      pid = __getpid ();
94084c
-	      assert (pid >= 0 && sizeof (pid_t) <= 4);
94084c
-	      p = _itoa (pid, &pidbuf[10], 10, 0);
94084c
-	      while (p > pidbuf)
94084c
-		*--p = ' ';
94084c
-	      pidbuf[10] = ':';
94084c
-	      pidbuf[11] = '\t';
94084c
-	    }
94084c
-
94084c
-	  /* Append to the output.  */
94084c
-	  assert (niov < NIOVMAX);
94084c
-	  iov[niov].iov_len = 12;
94084c
-	  iov[niov++].iov_base = pidbuf;
94084c
-
94084c
-	  /* No more tags until we see the next newline.  */
94084c
-	  tag_p = -1;
94084c
-	}
94084c
-
94084c
-      /* Skip everything except % and \n (if tags are needed).  */
94084c
-      while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
94084c
-	++fmt;
94084c
-
94084c
-      /* Append constant string.  */
94084c
-      assert (niov < NIOVMAX);
94084c
-      if ((iov[niov].iov_len = fmt - startp) != 0)
94084c
-	iov[niov++].iov_base = (char *) startp;
94084c
-
94084c
-      if (*fmt == '%')
94084c
-	{
94084c
-	  /* It is a format specifier.  */
94084c
-	  char fill = ' ';
94084c
-	  int width = -1;
94084c
-	  int prec = -1;
94084c
-#if LONG_MAX != INT_MAX
94084c
-	  int long_mod = 0;
94084c
-#endif
94084c
-
94084c
-	  /* Recognize zero-digit fill flag.  */
94084c
-	  if (*++fmt == '0')
94084c
-	    {
94084c
-	      fill = '0';
94084c
-	      ++fmt;
94084c
-	    }
94084c
-
94084c
-	  /* See whether with comes from a parameter.  Note that no other
94084c
-	     way to specify the width is implemented.  */
94084c
-	  if (*fmt == '*')
94084c
-	    {
94084c
-	      width = va_arg (arg, int);
94084c
-	      ++fmt;
94084c
-	    }
94084c
-
94084c
-	  /* Handle precision.  */
94084c
-	  if (*fmt == '.' && fmt[1] == '*')
94084c
-	    {
94084c
-	      prec = va_arg (arg, int);
94084c
-	      fmt += 2;
94084c
-	    }
94084c
-
94084c
-	  /* Recognize the l modifier.  It is only important on some
94084c
-	     platforms where long and int have a different size.  We
94084c
-	     can use the same code for size_t.  */
94084c
-	  if (*fmt == 'l' || *fmt == 'Z')
94084c
-	    {
94084c
-#if LONG_MAX != INT_MAX
94084c
-	      long_mod = 1;
94084c
-#endif
94084c
-	      ++fmt;
94084c
-	    }
94084c
-
94084c
-	  switch (*fmt)
94084c
-	    {
94084c
-	      /* Integer formatting.  */
94084c
-	    case 'd':
94084c
-	    case 'u':
94084c
-	    case 'x':
94084c
-	      {
94084c
-		/* We have to make a difference if long and int have a
94084c
-		   different size.  */
94084c
-#if LONG_MAX != INT_MAX
94084c
-		unsigned long int num = (long_mod
94084c
-					 ? va_arg (arg, unsigned long int)
94084c
-					 : va_arg (arg, unsigned int));
94084c
-#else
94084c
-		unsigned long int num = va_arg (arg, unsigned int);
94084c
-#endif
94084c
-		bool negative = false;
94084c
-		if (*fmt == 'd')
94084c
-		  {
94084c
-#if LONG_MAX != INT_MAX
94084c
-		    if (long_mod)
94084c
-		      {
94084c
-			if ((long int) num < 0)
94084c
-			  negative = true;
94084c
-		      }
94084c
-		    else
94084c
-		      {
94084c
-			if ((int) num < 0)
94084c
-			  {
94084c
-			    num = (unsigned int) num;
94084c
-			    negative = true;
94084c
-			  }
94084c
-		      }
94084c
-#else
94084c
-		    if ((int) num < 0)
94084c
-		      negative = true;
94084c
-#endif
94084c
-		  }
94084c
-
94084c
-		/* We use alloca() to allocate the buffer with the most
94084c
-		   pessimistic guess for the size.  Using alloca() allows
94084c
-		   having more than one integer formatting in a call.  */
94084c
-		char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int));
94084c
-		char *endp = &buf[1 + 3 * sizeof (unsigned long int)];
94084c
-		char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
94084c
-
94084c
-		/* Pad to the width the user specified.  */
94084c
-		if (width != -1)
94084c
-		  while (endp - cp < width)
94084c
-		    *--cp = fill;
94084c
-
94084c
-		if (negative)
94084c
-		  *--cp = '-';
94084c
-
94084c
-		iov[niov].iov_base = cp;
94084c
-		iov[niov].iov_len = endp - cp;
94084c
-		++niov;
94084c
-	      }
94084c
-	      break;
94084c
-
94084c
-	    case 's':
94084c
-	      /* Get the string argument.  */
94084c
-	      iov[niov].iov_base = va_arg (arg, char *);
94084c
-	      iov[niov].iov_len = strlen (iov[niov].iov_base);
94084c
-	      if (prec != -1)
94084c
-		iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
94084c
-	      ++niov;
94084c
-	      break;
94084c
-
94084c
-	    case '%':
94084c
-	      iov[niov].iov_base = (void *) fmt;
94084c
-	      iov[niov].iov_len = 1;
94084c
-	      ++niov;
94084c
-	      break;
94084c
-
94084c
-	    default:
94084c
-	      assert (! "invalid format specifier");
94084c
-	    }
94084c
-	  ++fmt;
94084c
-	}
94084c
-      else if (*fmt == '\n')
94084c
-	{
94084c
-	  /* See whether we have to print a single newline character.  */
94084c
-	  if (fmt == startp)
94084c
-	    {
94084c
-	      iov[niov].iov_base = (char *) startp;
94084c
-	      iov[niov++].iov_len = 1;
94084c
-	    }
94084c
-	  else
94084c
-	    /* No, just add it to the rest of the string.  */
94084c
-	    ++iov[niov - 1].iov_len;
94084c
-
94084c
-	  /* Next line, print a tag again.  */
94084c
-	  tag_p = 1;
94084c
-	  ++fmt;
94084c
-	}
94084c
-    }
94084c
-
94084c
-  /* Finally write the result.  */
94084c
-  _dl_writev (fd, iov, niov);
94084c
-}
94084c
-
94084c
-
94084c
-/* Write to debug file.  */
94084c
-void
94084c
-_dl_debug_printf (const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg);
94084c
-  va_end (arg);
94084c
-}
94084c
-
94084c
-
94084c
-/* Write to debug file but don't start with a tag.  */
94084c
-void
94084c
-_dl_debug_printf_c (const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg);
94084c
-  va_end (arg);
94084c
-}
94084c
-
94084c
-
94084c
-/* Write the given file descriptor.  */
94084c
-void
94084c
-_dl_dprintf (int fd, const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (fd, 0, fmt, arg);
94084c
-  va_end (arg);
94084c
-}
94084c
-
94084c
-void
94084c
-_dl_printf (const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (STDOUT_FILENO, 0, fmt, arg);
94084c
-  va_end (arg);
94084c
-}
94084c
-
94084c
-void
94084c
-_dl_error_printf (const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
94084c
-  va_end (arg);
94084c
-}
94084c
-
94084c
-void
94084c
-_dl_fatal_printf (const char *fmt, ...)
94084c
-{
94084c
-  va_list arg;
94084c
-
94084c
-  va_start (arg, fmt);
94084c
-  _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
94084c
-  va_end (arg);
94084c
-  _exit (127);
94084c
-}
94084c
-rtld_hidden_def (_dl_fatal_printf)
94084c
-
94084c
 /* Test whether given NAME matches any of the names of the given object.  */
94084c
 int
94084c
 _dl_name_match_p (const char *name, const struct link_map *map)
94084c
@@ -354,7 +82,6 @@ _dl_name_match_p (const char *name, const struct link_map *map)
94084c
   return 0;
94084c
 }
94084c
 
94084c
-
94084c
 unsigned long int
94084c
 _dl_higher_prime_number (unsigned long int n)
94084c
 {
94084c
diff --git a/elf/dl-printf.c b/elf/dl-printf.c
94084c
new file mode 100644
94084c
index 0000000000000000..d3264ba96cd959bf
94084c
--- /dev/null
94084c
+++ b/elf/dl-printf.c
94084c
@@ -0,0 +1,292 @@
94084c
+/* printf implementation for the dynamic loader.
94084c
+   Copyright (C) 1997-2022 Free Software Foundation, Inc.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+#include <_itoa.h>
94084c
+#include <assert.h>
94084c
+#include <dl-writev.h>
94084c
+#include <ldsodefs.h>
94084c
+#include <limits.h>
94084c
+#include <stdarg.h>
94084c
+#include <stdint.h>
94084c
+#include <stdlib.h>
94084c
+#include <string.h>
94084c
+#include <sys/uio.h>
94084c
+#include <unistd.h>
94084c
+
94084c
+/* Bare-bones printf implementation.  This function only knows about
94084c
+   the formats and flags needed and can handle only up to 64 stripes in
94084c
+   the output.  */
94084c
+static void
94084c
+_dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
94084c
+{
94084c
+# define NIOVMAX 64
94084c
+  struct iovec iov[NIOVMAX];
94084c
+  int niov = 0;
94084c
+  pid_t pid = 0;
94084c
+  char pidbuf[12];
94084c
+
94084c
+  while (*fmt != '\0')
94084c
+    {
94084c
+      const char *startp = fmt;
94084c
+
94084c
+      if (tag_p > 0)
94084c
+	{
94084c
+	  /* Generate the tag line once.  It consists of the PID and a
94084c
+	     colon followed by a tab.  */
94084c
+	  if (pid == 0)
94084c
+	    {
94084c
+	      char *p;
94084c
+	      pid = __getpid ();
94084c
+	      assert (pid >= 0 && sizeof (pid_t) <= 4);
94084c
+	      p = _itoa (pid, &pidbuf[10], 10, 0);
94084c
+	      while (p > pidbuf)
94084c
+		*--p = ' ';
94084c
+	      pidbuf[10] = ':';
94084c
+	      pidbuf[11] = '\t';
94084c
+	    }
94084c
+
94084c
+	  /* Append to the output.  */
94084c
+	  assert (niov < NIOVMAX);
94084c
+	  iov[niov].iov_len = 12;
94084c
+	  iov[niov++].iov_base = pidbuf;
94084c
+
94084c
+	  /* No more tags until we see the next newline.  */
94084c
+	  tag_p = -1;
94084c
+	}
94084c
+
94084c
+      /* Skip everything except % and \n (if tags are needed).  */
94084c
+      while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
94084c
+	++fmt;
94084c
+
94084c
+      /* Append constant string.  */
94084c
+      assert (niov < NIOVMAX);
94084c
+      if ((iov[niov].iov_len = fmt - startp) != 0)
94084c
+	iov[niov++].iov_base = (char *) startp;
94084c
+
94084c
+      if (*fmt == '%')
94084c
+	{
94084c
+	  /* It is a format specifier.  */
94084c
+	  char fill = ' ';
94084c
+	  int width = -1;
94084c
+	  int prec = -1;
94084c
+#if LONG_MAX != INT_MAX
94084c
+	  int long_mod = 0;
94084c
+#endif
94084c
+
94084c
+	  /* Recognize zero-digit fill flag.  */
94084c
+	  if (*++fmt == '0')
94084c
+	    {
94084c
+	      fill = '0';
94084c
+	      ++fmt;
94084c
+	    }
94084c
+
94084c
+	  /* See whether with comes from a parameter.  Note that no other
94084c
+	     way to specify the width is implemented.  */
94084c
+	  if (*fmt == '*')
94084c
+	    {
94084c
+	      width = va_arg (arg, int);
94084c
+	      ++fmt;
94084c
+	    }
94084c
+
94084c
+	  /* Handle precision.  */
94084c
+	  if (*fmt == '.' && fmt[1] == '*')
94084c
+	    {
94084c
+	      prec = va_arg (arg, int);
94084c
+	      fmt += 2;
94084c
+	    }
94084c
+
94084c
+	  /* Recognize the l modifier.  It is only important on some
94084c
+	     platforms where long and int have a different size.  We
94084c
+	     can use the same code for size_t.  */
94084c
+	  if (*fmt == 'l' || *fmt == 'Z')
94084c
+	    {
94084c
+#if LONG_MAX != INT_MAX
94084c
+	      long_mod = 1;
94084c
+#endif
94084c
+	      ++fmt;
94084c
+	    }
94084c
+
94084c
+	  switch (*fmt)
94084c
+	    {
94084c
+	      /* Integer formatting.  */
94084c
+	    case 'd':
94084c
+	    case 'u':
94084c
+	    case 'x':
94084c
+	      {
94084c
+		/* We have to make a difference if long and int have a
94084c
+		   different size.  */
94084c
+#if LONG_MAX != INT_MAX
94084c
+		unsigned long int num = (long_mod
94084c
+					 ? va_arg (arg, unsigned long int)
94084c
+					 : va_arg (arg, unsigned int));
94084c
+#else
94084c
+		unsigned long int num = va_arg (arg, unsigned int);
94084c
+#endif
94084c
+		bool negative = false;
94084c
+		if (*fmt == 'd')
94084c
+		  {
94084c
+#if LONG_MAX != INT_MAX
94084c
+		    if (long_mod)
94084c
+		      {
94084c
+			if ((long int) num < 0)
94084c
+			  negative = true;
94084c
+		      }
94084c
+		    else
94084c
+		      {
94084c
+			if ((int) num < 0)
94084c
+			  {
94084c
+			    num = (unsigned int) num;
94084c
+			    negative = true;
94084c
+			  }
94084c
+		      }
94084c
+#else
94084c
+		    if ((int) num < 0)
94084c
+		      negative = true;
94084c
+#endif
94084c
+		  }
94084c
+
94084c
+		/* We use alloca() to allocate the buffer with the most
94084c
+		   pessimistic guess for the size.  Using alloca() allows
94084c
+		   having more than one integer formatting in a call.  */
94084c
+		char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int));
94084c
+		char *endp = &buf[1 + 3 * sizeof (unsigned long int)];
94084c
+		char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
94084c
+
94084c
+		/* Pad to the width the user specified.  */
94084c
+		if (width != -1)
94084c
+		  while (endp - cp < width)
94084c
+		    *--cp = fill;
94084c
+
94084c
+		if (negative)
94084c
+		  *--cp = '-';
94084c
+
94084c
+		iov[niov].iov_base = cp;
94084c
+		iov[niov].iov_len = endp - cp;
94084c
+		++niov;
94084c
+	      }
94084c
+	      break;
94084c
+
94084c
+	    case 's':
94084c
+	      /* Get the string argument.  */
94084c
+	      iov[niov].iov_base = va_arg (arg, char *);
94084c
+	      iov[niov].iov_len = strlen (iov[niov].iov_base);
94084c
+	      if (prec != -1)
94084c
+		iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
94084c
+	      ++niov;
94084c
+	      break;
94084c
+
94084c
+	    case '%':
94084c
+	      iov[niov].iov_base = (void *) fmt;
94084c
+	      iov[niov].iov_len = 1;
94084c
+	      ++niov;
94084c
+	      break;
94084c
+
94084c
+	    default:
94084c
+	      assert (! "invalid format specifier");
94084c
+	    }
94084c
+	  ++fmt;
94084c
+	}
94084c
+      else if (*fmt == '\n')
94084c
+	{
94084c
+	  /* See whether we have to print a single newline character.  */
94084c
+	  if (fmt == startp)
94084c
+	    {
94084c
+	      iov[niov].iov_base = (char *) startp;
94084c
+	      iov[niov++].iov_len = 1;
94084c
+	    }
94084c
+	  else
94084c
+	    /* No, just add it to the rest of the string.  */
94084c
+	    ++iov[niov - 1].iov_len;
94084c
+
94084c
+	  /* Next line, print a tag again.  */
94084c
+	  tag_p = 1;
94084c
+	  ++fmt;
94084c
+	}
94084c
+    }
94084c
+
94084c
+  /* Finally write the result.  */
94084c
+  _dl_writev (fd, iov, niov);
94084c
+}
94084c
+
94084c
+
94084c
+/* Write to debug file.  */
94084c
+void
94084c
+_dl_debug_printf (const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg);
94084c
+  va_end (arg);
94084c
+}
94084c
+
94084c
+
94084c
+/* Write to debug file but don't start with a tag.  */
94084c
+void
94084c
+_dl_debug_printf_c (const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg);
94084c
+  va_end (arg);
94084c
+}
94084c
+
94084c
+
94084c
+/* Write the given file descriptor.  */
94084c
+void
94084c
+_dl_dprintf (int fd, const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (fd, 0, fmt, arg);
94084c
+  va_end (arg);
94084c
+}
94084c
+
94084c
+void
94084c
+_dl_printf (const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (STDOUT_FILENO, 0, fmt, arg);
94084c
+  va_end (arg);
94084c
+}
94084c
+
94084c
+void
94084c
+_dl_error_printf (const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
94084c
+  va_end (arg);
94084c
+}
94084c
+
94084c
+void
94084c
+_dl_fatal_printf (const char *fmt, ...)
94084c
+{
94084c
+  va_list arg;
94084c
+
94084c
+  va_start (arg, fmt);
94084c
+  _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg);
94084c
+  va_end (arg);
94084c
+  _exit (127);
94084c
+}
94084c
+rtld_hidden_def (_dl_fatal_printf)