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