From 18bf56434d1adabbd82fa6a33d08a60ef2d71001 Mon Sep 17 00:00:00 2001
From: Nick Clifton <nickc@redhat.com>
Date: Mon, 31 Oct 2022 09:35:16 +0000
Subject: [PATCH] objdump: Add configure time option to enable colored
 disassembly output by default.

	PR 29457
	* configure.ac: Add --enable-colored-disassembly.
	* objdump.c: Add --disassembler-color=terminal.
	* doc/binutils.texi (objdump): Document the new option.
	* NEWS: Mention new feature.
	* config.in: Regenerate in.
	* configure: Regenerate.
---
 binutils/ChangeLog         | 10 ++++++++++
 binutils/NEWS              |  4 ++++
 binutils/config.in         |  3 +++
 binutils/configure         | 30 ++++++++++++++++++++++++++++--
 binutils/configure.ac      | 15 +++++++++++++++
 binutils/doc/binutils.texi | 30 +++++++++++++++++++-----------
 binutils/objdump.c         | 34 +++++++++++++++++++++++++++-------
 7 files changed, 106 insertions(+), 20 deletions(-)

diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 807ac82a02f..6c4fa9808e6 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,13 @@
+2022-10-31  Nick Clifton  <nickc@redhat.com>
+
+	PR 29457
+	* configure.ac: Add --enable-colored-disassembly.
+	* objdump.c: Add --disassembler-color=terminal.
+	* doc/binutils.texi (objdump): Document the new option.
+	* NEWS: Mention new feature.
+	* config.in: Regenerate in.
+	* configure: Regenerate.
+
 2022-10-19  Nick Clifton  <nickc@redhat.com>
 
 	* MAINTAINERS: Add section on patches, copyright and DCO.
diff --git a/binutils/NEWS b/binutils/NEWS
index 9ccf401027b..b4978f59086 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -1,5 +1,9 @@
 -*- text -*-
 
+* Add --enable-colored-disassembly configure time option to enable colored
+  disassembly output by default, if the output device is a terminal.  Note,
+  this configure option is disabled by default.
+  
 * DCO signed contributions are now accepted.
 
 * objcopy --decompress-debug-sections now supports zstd compressed debug
diff --git a/binutils/config.in b/binutils/config.in
index bee8c07e2f7..4d22a80971b 100644
--- a/binutils/config.in
+++ b/binutils/config.in
@@ -10,6 +10,9 @@
 /* Should ar and ranlib use -D behavior by default? */
 #undef DEFAULT_AR_DETERMINISTIC
 
+/* Have objdump use colors in its disassembly by default */
+#undef DEFAULT_FOR_COLORED_DISASSEMBLY
+
 /* Have readelf and objdump follow debug links by default */
 #undef DEFAULT_FOR_FOLLOW_LINKS
 
diff --git a/binutils/configure b/binutils/configure
index 7a45037a82f..2e33584e548 100755
--- a/binutils/configure
+++ b/binutils/configure
@@ -827,6 +827,7 @@ enable_deterministic_archives
 enable_default_strings_all
 enable_f_for_ifunc_symbols
 enable_follow_debug_links
+enable_colored_disassembly
 with_debuginfod
 enable_libctf
 enable_werror
@@ -1504,6 +1505,9 @@ Optional Features:
   --enable-follow-debug-links
                           Have readelf and objdump follow debug links by
                           default
+  --enable-colored-disassembly
+                          Have objdump use colors in its disassembly by
+                          default
   --enable-libctf         Handle .ctf type-info sections [default=yes]
   --enable-werror         treat compile warnings as errors
   --enable-build-warnings enable build-time compiler warnings
@@ -10813,7 +10817,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10816 "configure"
+#line 10820 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -10919,7 +10923,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10922 "configure"
+#line 10926 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11633,6 +11637,28 @@ _ACEOF
 
 
 
+# Check whether --enable-colored-disassembly was given.
+if test "${enable_colored_disassembly+set}" = set; then :
+  enableval=$enable_colored_disassembly;
+if test "${enableval}" = no; then
+  default_for_colored_disassembly=0
+else
+  default_for_colored_disassembly=1
+fi
+else
+  default_for_colored_disassembly=0
+fi
+
+
+
+cat >>confdefs.h <<_ACEOF
+#define DEFAULT_FOR_COLORED_DISASSEMBLY $default_for_colored_disassembly
+_ACEOF
+
+
+
+
+
 
 
 
diff --git a/binutils/configure.ac b/binutils/configure.ac
index 9b75396d07e..97789a59519 100644
--- a/binutils/configure.ac
+++ b/binutils/configure.ac
@@ -109,6 +109,21 @@ AC_DEFINE_UNQUOTED(DEFAULT_FOR_FOLLOW_LINKS, $default_for_follow_links,
 		   [Have readelf and objdump follow debug links by default])
 
 
+
+
+AC_ARG_ENABLE(colored-disassembly,
+[AS_HELP_STRING([--enable-colored-disassembly],
+	[Have objdump use colors in its disassembly by default])], [
+if test "${enableval}" = no; then
+  default_for_colored_disassembly=0
+else
+  default_for_colored_disassembly=1
+fi], [default_for_colored_disassembly=0])
+
+AC_DEFINE_UNQUOTED(DEFAULT_FOR_COLORED_DISASSEMBLY, $default_for_colored_disassembly,
+		   [Have objdump use colors in its disassembly by default])
+
+
 AC_DEBUGINFOD
 
 GCC_ENABLE([libctf], [yes], [], [Handle .ctf type-info sections])
diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 6eea08fb91b..2ca114d5774 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -2282,7 +2282,7 @@ objdump [@option{-a}|@option{--archive-headers}]
         [@option{--prefix-strip=}@var{level}]
         [@option{--insn-width=}@var{width}]
         [@option{--visualize-jumps[=color|=extended-color|=off]}
-        [@option{--disassembler-color=[color|extended-color|off]}
+        [@option{--disassembler-color=[off|terminal|on|extended]}
         [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
         [@option{-V}|@option{--version}]
         [@option{-H}|@option{--help}]
@@ -2821,18 +2821,26 @@ If it is necessary to disable the @option{visualize-jumps} option
 after it has previously been enabled then use
 @option{visualize-jumps=off}.
 
-@item --disassembler-color=[color|extended-color|off]
-Apply syntax highlighting to the disassembler output.  The
-@option{color} argument adds color using simple terminal colors.
-Alternatively the @option{extended-color} argument will use 8bit
-colors, but these might not work on all terminals.
+@item --disassembler-color=off
+@itemx --disassembler-color=terminal
+@itemx --disassembler-color=on|color|colour
+@itemx --disassembler-color=extened|extended-color|extened-colour
+Enables or disables the use of colored syntax highlighting in
+disassembly output.  The default behaviour is determined via a
+configure time option.  Note, not all architectures support colored
+syntax highlighting, and depending upon the terminal used, colored
+output may not actually be legible.
 
-If it is necessary to disable the @option{--disassembler-color} option
-after it has previously been enabled then use
-@option{--disassembler-color=off}.
+The @option{on} argument adds colors using simple terminal colors.
 
-If this option is not specified then the default is to enable color
-output if displaying to a terminal, but not otherwise.
+The @option{terminal} argument does the same, but only if the output
+device is a terminal.
+
+The @option{extended-color} argument is similar to the @option{on}
+argument, but it uses 8-bit colors.  These may not work on all
+terminals.
+
+The @option{off} argument disables colored disassembly.
 
 @item -W[lLiaprmfFsoORtUuTgAckK]
 @itemx --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,=trace_info,=trace_abbrev,=trace_aranges,=gdb_index,=addr,=cu_index,=links,=follow-links]
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 0825e051ef6..7630986f59a 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -141,7 +141,12 @@ static enum color_selection
     on,   				/* --disassembler-color=color.  */
     off, 				/* --disassembler-color=off.  */
     extended				/* --disassembler-color=extended-color.  */
-  } disassembler_color = on_if_terminal_output;
+  } disassembler_color =
+#if DEFAULT_FOR_COLORED_DISASSEMBLY
+  on_if_terminal_output;
+#else
+  off;
+#endif
 
 static int dump_any_debugging;
 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
@@ -414,13 +419,22 @@ usage (FILE *stream, int status)
       --visualize-jumps=extended-color\n\
                                  Use extended 8-bit color codes\n"));
       fprintf (stream, _("\
-      --visualize-jumps=off      Disable jump visualization\n\n"));
+      --visualize-jumps=off      Disable jump visualization\n"));
+#if DEFAULT_FOR_COLORED_DISASSEMBLY
       fprintf (stream, _("\
-      --disassembler-color=off   Disable disassembler color output.\n\n"));
+      --disassembler-color=off       Disable disassembler color output.\n"));
       fprintf (stream, _("\
-      --disassembler-color=color Use basic colors in disassembler output.\n\n"));
+      --disassembler-color=terminal  Enable disassembler color output if displaying on a terminal. (default)\n"));
+#else
       fprintf (stream, _("\
-      --disassembler-color=extended-color Use 8-bit colors in disassembler output.\n\n"));
+      --disassembler-color=off       Disable disassembler color output. (default)\n"));
+      fprintf (stream, _("\
+      --disassembler-color=terminal  Enable disassembler color output if displaying on a terminal.\n"));
+#endif
+      fprintf (stream, _("\
+      --disassembler-color=on        Enable disassembler color output.\n"));
+      fprintf (stream, _("\
+      --disassembler-color=extended  Use 8-bit colors in disassembler output.\n\n"));
 
       list_supported_targets (program_name, stream);
       list_supported_architectures (program_name, stream);
@@ -5890,9 +5904,15 @@ main (int argc, char **argv)
 	case OPTION_DISASSEMBLER_COLOR:
 	  if (streq (optarg, "off"))
 	    disassembler_color = off;
-	  else if (streq (optarg, "color"))
+	  else if (streq (optarg, "terminal"))
+	    disassembler_color = on_if_terminal_output;
+	  else if (streq (optarg, "color")
+		   || streq (optarg, "colour")
+		   || streq (optarg, "on")) 
 	    disassembler_color = on;
-	  else if (streq (optarg, "extended-color"))
+	  else if (streq (optarg, "extended")
+		   || streq (optarg, "extended-color")
+		   || streq (optarg, "extended-colour"))
 	    disassembler_color = extended;
 	  else
 	    nonfatal (_("unrecognized argument to --disassembler-color"));