b1dca6
commit a5275ba5378c9256d18e582572b4315e8edfcbfb
b1dca6
Author: H.J. Lu <hjl.tools@gmail.com>
b1dca6
Date:   Thu Nov 29 14:15:01 2018 -0800
b1dca6
b1dca6
    _dl_exception_create_format: Support %x/%lx/%zx
b1dca6
    
b1dca6
    Add support for %x, %lx and %zx to _dl_exception_create_format and pad
b1dca6
    to the full width with 0.
b1dca6
    
b1dca6
            * elf/Makefile (tests-internal): Add tst-create_format1.
b1dca6
            * elf/dl-exception.c (_dl_exception_create_format): Support
b1dca6
            %x, %lx and %zx.
b1dca6
            * elf/tst-create_format1.c: New file.
b1dca6
b1dca6
Conflicts:
b1dca6
	elf/Makefile
b1dca6
	  (Different backport order of tests.)
b1dca6
b1dca6
diff --git a/elf/Makefile b/elf/Makefile
b1dca6
index 89dff92adfc417f5..6d1962b2e4deb871 100644
b1dca6
--- a/elf/Makefile
b1dca6
+++ b/elf/Makefile
b1dca6
@@ -198,7 +198,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
b1dca6
 tests-internal += loadtest unload unload2 circleload1 \
b1dca6
 	 neededtest neededtest2 neededtest3 neededtest4 \
b1dca6
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
b1dca6
-	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym
b1dca6
+	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \
b1dca6
+	 tst-create_format1
b1dca6
 tests-container += tst-pldd
b1dca6
 ifeq ($(build-hardcoded-path-in-tests),yes)
b1dca6
 tests += tst-dlopen-aout
b1dca6
diff --git a/elf/dl-exception.c b/elf/dl-exception.c
b1dca6
index 1c63e4a3a65b6d55..1e41d89a7db52683 100644
b1dca6
--- a/elf/dl-exception.c
b1dca6
+++ b/elf/dl-exception.c
b1dca6
@@ -111,6 +111,20 @@ _dl_exception_create_format (struct dl_exception *exception, const char *objname
b1dca6
             case 's':
b1dca6
               length += strlen (va_arg (ap, const char *));
b1dca6
               break;
b1dca6
+	      /* Recognize the l modifier.  It is only important on some
b1dca6
+		 platforms where long and int have a different size.  We
b1dca6
+		 can use the same code for size_t.  */
b1dca6
+	    case 'l':
b1dca6
+	    case 'z':
b1dca6
+	      if (p[1] == 'x')
b1dca6
+		{
b1dca6
+		  length += LONG_WIDTH / 4;
b1dca6
+		  ++p;
b1dca6
+		  break;
b1dca6
+		}
b1dca6
+	    case 'x':
b1dca6
+	      length += INT_WIDTH / 4;
b1dca6
+	      break;
b1dca6
             default:
b1dca6
               /* Assumed to be '%'.  */
b1dca6
               ++length;
b1dca6
@@ -167,6 +181,32 @@ _dl_exception_create_format (struct dl_exception *exception, const char *objname
b1dca6
               *wptr = '%';
b1dca6
               ++wptr;
b1dca6
               break;
b1dca6
+	    case 'x':
b1dca6
+	      {
b1dca6
+		unsigned long int num = va_arg (ap, unsigned int);
b1dca6
+		char *start = wptr;
b1dca6
+		wptr += INT_WIDTH / 4;
b1dca6
+		char *cp = _itoa (num, wptr, 16, 0);
b1dca6
+		/* Pad to the full width with 0.  */
b1dca6
+		while (cp != start)
b1dca6
+		  *--cp = '0';
b1dca6
+	      }
b1dca6
+	      break;
b1dca6
+	    case 'l':
b1dca6
+	    case 'z':
b1dca6
+	      if (p[1] == 'x')
b1dca6
+		{
b1dca6
+		  unsigned long int num = va_arg (ap, unsigned long int);
b1dca6
+		  char *start = wptr;
b1dca6
+		  wptr += LONG_WIDTH / 4;
b1dca6
+		  char *cp = _itoa (num, wptr, 16, 0);
b1dca6
+		  /* Pad to the full width with 0.  */
b1dca6
+		  while (cp != start)
b1dca6
+		    *--cp = '0';
b1dca6
+		  ++p;
b1dca6
+		  break;
b1dca6
+		}
b1dca6
+	       /* FALLTHROUGH */
b1dca6
             default:
b1dca6
               _dl_fatal_printf ("Fatal error:"
b1dca6
                                 " invalid format in exception string\n");
b1dca6
diff --git a/elf/tst-create_format1.c b/elf/tst-create_format1.c
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..8b9edfdc69ea4ced
b1dca6
--- /dev/null
b1dca6
+++ b/elf/tst-create_format1.c
b1dca6
@@ -0,0 +1,103 @@
b1dca6
+/* Check _dl_exception_create_format.
b1dca6
+   Copyright (C) 2018 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <http://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+#include <ldsodefs.h>
b1dca6
+#include <array_length.h>
b1dca6
+
b1dca6
+#include <support/check.h>
b1dca6
+#include <support/xunistd.h>
b1dca6
+#include <support/capture_subprocess.h>
b1dca6
+
b1dca6
+#define TEST(es, objn, fmt, ...)					\
b1dca6
+  ({									\
b1dca6
+     struct dl_exception exception;					\
b1dca6
+     _dl_exception_create_format (&exception, objn, fmt, __VA_ARGS__);	\
b1dca6
+     TEST_COMPARE_STRING (exception.objname, objn == NULL ? "" : objn);	\
b1dca6
+     TEST_COMPARE_STRING (exception.errstring, es);			\
b1dca6
+     _dl_exception_free (&exception);					\
b1dca6
+   })
b1dca6
+
b1dca6
+static void
b1dca6
+do_test_invalid_conversion (void *closure)
b1dca6
+{
b1dca6
+  TEST ("(null)", NULL, "%p", NULL);
b1dca6
+}
b1dca6
+
b1dca6
+/* Exit status after abnormal termination.  */
b1dca6
+static int invalid_status;
b1dca6
+
b1dca6
+static void
b1dca6
+init_invalid_status (void)
b1dca6
+{
b1dca6
+  pid_t pid = xfork ();
b1dca6
+  if (pid == 0)
b1dca6
+    _exit (127);
b1dca6
+  xwaitpid (pid, &invalid_status, 0);
b1dca6
+  if (WIFEXITED (invalid_status))
b1dca6
+    invalid_status = WEXITSTATUS (invalid_status);
b1dca6
+}
b1dca6
+
b1dca6
+static int
b1dca6
+do_test (void)
b1dca6
+{
b1dca6
+  init_invalid_status ();
b1dca6
+
b1dca6
+  TEST ("test",      NULL,   "%s",      "test");
b1dca6
+  TEST ("test-test", NULL,   "%s-test", "test");
b1dca6
+  TEST ("test",      "test", "%s",      "test");
b1dca6
+  TEST ("test-test", "test", "%s-test", "test");
b1dca6
+
b1dca6
+  TEST ("test%",      NULL,   "%s%%",      "test");
b1dca6
+  TEST ("test%-test", NULL,   "%s%%-test", "test");
b1dca6
+  TEST ("test%",      "test", "%s%%",      "test");
b1dca6
+  TEST ("test%-test", "test", "%s%%-test", "test");
b1dca6
+
b1dca6
+  TEST ("0000007b",      NULL,   "%x",      123);
b1dca6
+  TEST ("0000007b-test", NULL,   "%x-test", 123);
b1dca6
+  TEST ("0000007b",      "test", "%x",      123);
b1dca6
+  TEST ("0000007b-test", "test", "%x-test", 123);
b1dca6
+
b1dca6
+#define TEST_LONG(es, objn, fmt, ...)				\
b1dca6
+  ({								\
b1dca6
+     if (sizeof (int) == sizeof (long int))			\
b1dca6
+       TEST (es, objn, fmt, __VA_ARGS__);			\
b1dca6
+     else							\
b1dca6
+       TEST ("ffffffff" es, objn, fmt, __VA_ARGS__);		\
b1dca6
+   })
b1dca6
+
b1dca6
+  TEST_LONG ("fffffffd",      NULL,   "%lx",      (long int)~2ul);
b1dca6
+  TEST_LONG ("fffffffd-test", NULL,   "%lx-test", (long int)~2ul);
b1dca6
+  TEST_LONG ("fffffffd",      "test", "%lx",      (long int)~2ul);
b1dca6
+  TEST_LONG ("fffffffd-test", "test", "%lx-test", (long int)~2ul);
b1dca6
+
b1dca6
+  TEST_LONG ("fffffffe",      NULL,   "%zx",      (size_t)~1ul);
b1dca6
+  TEST_LONG ("fffffffe-test", NULL,   "%zx-test", (size_t)~1ul);
b1dca6
+  TEST_LONG ("fffffffe",      "test", "%zx",      (size_t)~1ul);
b1dca6
+  TEST_LONG ("fffffffe-test", "test", "%zx-test", (size_t)~1ul);
b1dca6
+
b1dca6
+  struct support_capture_subprocess result;
b1dca6
+  result = support_capture_subprocess (do_test_invalid_conversion, NULL);
b1dca6
+  support_capture_subprocess_check (&result, "dl-exception",
b1dca6
+				    invalid_status, sc_allow_stderr);
b1dca6
+  TEST_COMPARE_STRING (result.err.buffer,
b1dca6
+		       "Fatal error: invalid format in exception string\n");
b1dca6
+
b1dca6
+  return 0;
b1dca6
+}
b1dca6
+
b1dca6
+#include <support/test-driver.c>