446cf2
commit 533dd2acf7eefa969fb770fa782b20519bd4bc0f
446cf2
Author: H.J. Lu <hjl.tools@gmail.com>
446cf2
Date:   Tue Jun 9 12:15:01 2020 -0700
446cf2
446cf2
    Add "%d" support to _dl_debug_vdprintf
446cf2
    
446cf2
    "%d" will be used to print out signed value.
446cf2
446cf2
diff --git a/elf/dl-misc.c b/elf/dl-misc.c
446cf2
index 2eb81eeb0231368d..3f28de3ee9d68368 100644
446cf2
--- a/elf/dl-misc.c
446cf2
+++ b/elf/dl-misc.c
446cf2
@@ -167,6 +167,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
446cf2
 	  switch (*fmt)
446cf2
 	    {
446cf2
 	      /* Integer formatting.  */
446cf2
+	    case 'd':
446cf2
 	    case 'u':
446cf2
 	    case 'x':
446cf2
 	      {
446cf2
@@ -179,11 +180,34 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
446cf2
 #else
446cf2
 		unsigned long int num = va_arg (arg, unsigned int);
446cf2
 #endif
446cf2
+		bool negative = false;
446cf2
+		if (*fmt == 'd')
446cf2
+		  {
446cf2
+#if LONG_MAX != INT_MAX
446cf2
+		    if (long_mod)
446cf2
+		      {
446cf2
+			if ((long int) num < 0)
446cf2
+			  negative = true;
446cf2
+		      }
446cf2
+		    else
446cf2
+		      {
446cf2
+			if ((int) num < 0)
446cf2
+			  {
446cf2
+			    num = (unsigned int) num;
446cf2
+			    negative = true;
446cf2
+			  }
446cf2
+		      }
446cf2
+#else
446cf2
+		    if ((int) num < 0)
446cf2
+		      negative = true;
446cf2
+#endif
446cf2
+		  }
446cf2
+
446cf2
 		/* We use alloca() to allocate the buffer with the most
446cf2
 		   pessimistic guess for the size.  Using alloca() allows
446cf2
 		   having more than one integer formatting in a call.  */
446cf2
-		char *buf = (char *) alloca (3 * sizeof (unsigned long int));
446cf2
-		char *endp = &buf[3 * sizeof (unsigned long int)];
446cf2
+		char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int));
446cf2
+		char *endp = &buf[1 + 3 * sizeof (unsigned long int)];
446cf2
 		char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
446cf2
 
446cf2
 		/* Pad to the width the user specified.  */
446cf2
@@ -191,6 +215,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
446cf2
 		  while (endp - cp < width)
446cf2
 		    *--cp = fill;
446cf2
 
446cf2
+		if (negative)
446cf2
+		  *--cp = '-';
446cf2
+
446cf2
 		iov[niov].iov_base = cp;
446cf2
 		iov[niov].iov_len = endp - cp;
446cf2
 		++niov;