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