objdump: Fix for visualize-jumps in one case

Message ID 20200131225938.17053-1-andrew.burgess@embecosm.com
State New
Headers show
Series
  • objdump: Fix for visualize-jumps in one case
Related show

Commit Message

Andrew Burgess Jan. 31, 2020, 10:59 p.m.
Consider the following x86-64 assembler test:

          .global _start
          .text
  _start:
          je	1f
          nop
  	  nopl	foo(%rax,%rax,1)
          nop
  1:      nop

When assembler with 'as -o test.o test.s', then disassembled using
--visualize-jumps, we get this:

  Disassembly of section .text:

  0000000000000000 <_start>:
     0:   /-- 74 0a                       je     c <_start+0xc>
     2:   |   90                          nop
     3:   |   0f 1f 84 00 00 00 00        nopl   0x0(%rax,%rax,1)
     a:   00
     b:   |   90                          nop
     c:   \-> 90                          nop

Notice the incorrect formatting at address 0xa.  After this patch the
formatting now looks like this:

  Disassembly of section .text:

  0000000000000000 <_start>:
     0:   /-- 74 0a                       je     c <_start+0xc>
     2:   |   90                          nop
     3:   |   0f 1f 84 00 00 00 00        nopl   0x0(%rax,%rax,1)
     a:   |   00
     b:   |   90                          nop
     c:   \-> 90                          nop

binutils/ChangeLog:

	* objdump.c (print_jump_visualisation): New function.
	(disassemble_bytes): Call new function.

Change-Id: I38370cab3f6247bba330aac1b4717b6671708a45
---
 binutils/ChangeLog |  5 ++++
 binutils/objdump.c | 84 ++++++++++++++++++++++++++++++------------------------
 2 files changed, 52 insertions(+), 37 deletions(-)

-- 
2.14.5

Comments

Nick Clifton Feb. 6, 2020, 1:34 p.m. | #1
Hi Andrew,

> binutils/ChangeLog:

> 

> 	* objdump.c (print_jump_visualisation): New function.

> 	(disassemble_bytes): Call new function.


Approved and applied.  Thanks very much for contributing this patch.

Cheers
  Nick

Patch

diff --git a/binutils/objdump.c b/binutils/objdump.c
index 60a39671292..3bd62e5bdb4 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -2467,6 +2467,46 @@  null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_
   return 1;
 }
 
+/* Print out jump visualization.  */
+static void
+print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
+			  uint8_t *color_buffer)
+{
+  if (!line_buffer)
+    return;
+
+  jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
+
+  size_t line_buffer_size = strlen (line_buffer);
+  char last_color = 0;
+  size_t i;
+
+  for (i = 0; i <= line_buffer_size; ++i)
+    {
+      if (color_output)
+	{
+	  uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
+
+	  if (color != last_color)
+	    {
+	      if (color)
+		if (extended_color_output)
+		  /* Use extended 8bit color, but
+		     do not choose dark colors.  */
+		  printf ("\033[38;5;%dm", 124 + (color % 108));
+		else
+		  /* Use simple terminal colors.  */
+		  printf ("\033[%dm", 31 + (color % 7));
+	      else
+		/* Clear color.  */
+		printf ("\033[0m");
+	      last_color = color;
+	    }
+	}
+      putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
+    }
+}
+
 /* Disassemble some data in memory between given values.  */
 
 static void
@@ -2625,43 +2665,9 @@  disassemble_bytes (struct disassemble_info * inf,
 	      putchar (' ');
 	    }
 
-	  /* Visualize jumps. */
-	  if (line_buffer)
-	    {
-	      jump_info_visualize_address (section->vma + addr_offset,
-					   max_level,
-					   line_buffer,
-					   color_buffer);
-
-	      size_t line_buffer_size = strlen (line_buffer);
-	      char last_color = 0;
-	      size_t i;
-
-	      for (i = 0; i <= line_buffer_size; ++i)
-		{
-		  if (color_output)
-		    {
-		      uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
-
-		      if (color != last_color)
-			{
-			  if (color)
-			    if (extended_color_output)
-			      /* Use extended 8bit color, but
-			         do not choose dark colors.  */
-			      printf ("\033[38;5;%dm", 124 + (color % 108));
-			    else
-			      /* Use simple terminal colors.  */
-			      printf ("\033[%dm", 31 + (color % 7));
-			  else
-			    /* Clear color.  */
-			    printf ("\033[0m");
-			  last_color = color;
-			}
-		    }
-		  putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
-		}
-	    }
+	  print_jump_visualisation (section->vma + addr_offset,
+				    max_level, line_buffer,
+				    color_buffer);
 
 	  if (insns)
 	    {
@@ -2853,6 +2859,10 @@  disassemble_bytes (struct disassemble_info * inf,
 		    *--s = '0';
 		  printf ("%s:\t", buf + skip_addr_chars);
 
+		  print_jump_visualisation (section->vma + j / opb,
+					    max_level, line_buffer,
+					    color_buffer);
+
 		  pb += octets_per_line;
 		  if (pb > octets)
 		    pb = octets;