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