Blame SOURCES/binutils.unicode.patch

f195d2
diff -rup binutils.orig/binutils/NEWS binutils-2.30/binutils/NEWS
f195d2
--- binutils.orig/binutils/NEWS	2021-10-20 12:39:41.715008273 +0100
f195d2
+++ binutils-2.30/binutils/NEWS	2021-10-20 12:41:30.895327968 +0100
f195d2
@@ -1,5 +1,14 @@
f195d2
 -*- text -*-
f195d2
 
f195d2
+* Tools which display names or strings (readelf, strings, nm, objdump)
f195d2
+  have a new command line option which controls how unicode characters are
f195d2
+  handled.  By default they are treated as normal for the tool.  Using
f195d2
+  --unicode=locale will display them according to the current locale.
f195d2
+  Using --unicode=hex will display them as hex byte values, whilst
f195d2
+  --unicode=escape will display them as escape sequences.  In addition
f195d2
+  using --unicode=highlight will display them as unicode escape sequences
f195d2
+  highlighted in red (if supported by the output device).
f195d2
+
f195d2
 * Add support for disassembling netronome Flow Processor (NFP) firmware files.
f195d2
 
f195d2
 Changes in 2.30:
f195d2
diff -rup binutils.orig/binutils/doc/binutils.texi binutils-2.30/binutils/doc/binutils.texi
f195d2
--- binutils.orig/binutils/doc/binutils.texi	2021-10-20 12:39:41.701008360 +0100
f195d2
+++ binutils-2.30/binutils/doc/binutils.texi	2021-10-20 13:00:11.801327501 +0100
f195d2
@@ -762,6 +762,7 @@ nm [@option{-A}|@option{-o}|@option{--pr
f195d2
    [@option{-D}|@option{--dynamic}] [@option{-f}@var{format}|@option{--format=}@var{format}]
f195d2
    [@option{-g}|@option{--extern-only}] [@option{-h}|@option{--help}]
f195d2
    [@option{-l}|@option{--line-numbers}] [@option{--inlines}]
f195d2
+   [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
f195d2
    [@option{-n}|@option{-v}|@option{--numeric-sort}]
f195d2
    [@option{-P}|@option{--portability}] [@option{-p}|@option{--no-sort}]
f195d2
    [@option{-r}|@option{--reverse-sort}] [@option{-S}|@option{--print-size}]
f195d2
@@ -1026,6 +1027,21 @@ Use @var{radix} as the radix for printin
f195d2
 @cindex undefined symbols
f195d2
 Display only undefined symbols (those external to each object file).
f195d2
 
f195d2
+@item -U @var{[d|i|l|e|x|h]}
f195d2
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
f195d2
+Controls the display of UTF-8 encoded mulibyte characters in strings.
f195d2
+The default (@option{--unicode=default}) is to give them no special
f195d2
+treatment.  The @option{--unicode=locale} option displays the sequence
f195d2
+in the current locale, which may or may not support them.  The options
f195d2
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
f195d2
+hex byte sequences enclosed by either angle brackets or curly braces.
f195d2
+
f195d2
+The @option{--unicode=escape} option displays them as escape sequences
f195d2
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
f195d2
+them as escape sequences highlighted in red (if supported by the
f195d2
+output device).  The colouring is intended to draw attention to the
f195d2
+presence of unicode sequences where they might not be expected.
f195d2
+
f195d2
 @item -V
f195d2
 @itemx --version
f195d2
 Show the version number of @command{nm} and exit.
f195d2
@@ -2093,6 +2109,7 @@ objdump [@option{-a}|@option{--archive-h
f195d2
         [@option{--prefix=}@var{prefix}]
f195d2
         [@option{--prefix-strip=}@var{level}]
f195d2
         [@option{--insn-width=}@var{width}]
f195d2
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
f195d2
         [@option{-V}|@option{--version}]
f195d2
         [@option{-H}|@option{--help}]
f195d2
         @var{objfile}@dots{}
f195d2
@@ -2697,6 +2714,21 @@ When displaying symbols include those wh
f195d2
 special in some way and which would not normally be of interest to the
f195d2
 user.
f195d2
 
f195d2
+@item -U @var{[d|i|l|e|x|h]}
f195d2
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
f195d2
+Controls the display of UTF-8 encoded mulibyte characters in strings.
f195d2
+The default (@option{--unicode=default}) is to give them no special
f195d2
+treatment.  The @option{--unicode=locale} option displays the sequence
f195d2
+in the current locale, which may or may not support them.  The options
f195d2
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
f195d2
+hex byte sequences enclosed by either angle brackets or curly braces.
f195d2
+
f195d2
+The @option{--unicode=escape} option displays them as escape sequences
f195d2
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
f195d2
+them as escape sequences highlighted in red (if supported by the
f195d2
+output device).  The colouring is intended to draw attention to the
f195d2
+presence of unicode sequences where they might not be expected.
f195d2
+
f195d2
 @item -V
f195d2
 @itemx --version
f195d2
 Print the version number of @command{objdump} and exit.
f195d2
@@ -2952,6 +2984,7 @@ strings [@option{-afovV}] [@option{-}@va
f195d2
         [@option{-n} @var{min-len}] [@option{--bytes=}@var{min-len}]
f195d2
         [@option{-t} @var{radix}] [@option{--radix=}@var{radix}]
f195d2
         [@option{-e} @var{encoding}] [@option{--encoding=}@var{encoding}]
f195d2
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
f195d2
         [@option{-}] [@option{--all}] [@option{--print-file-name}]
f195d2
         [@option{-T} @var{bfdname}] [@option{--target=}@var{bfdname}]
f195d2
         [@option{-w}] [@option{--include-all-whitespace}]
f195d2
@@ -3043,6 +3076,28 @@ single-8-bit-byte characters, @samp{b} =
f195d2
 littleendian.  Useful for finding wide character strings. (@samp{l}
f195d2
 and @samp{b} apply to, for example, Unicode UTF-16/UCS-2 encodings).
f195d2
 
f195d2
+@item -U @var{[d|i|l|e|x|h]}
f195d2
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
f195d2
+Controls the display of UTF-8 encoded mulibyte characters in strings.
f195d2
+The default (@option{--unicode=default}) is to give them no special
f195d2
+treatment, and instead rely upon the setting of the
f195d2
+@option{--encoding} option.  The other values for this option
f195d2
+automatically enable @option{--encoding=S}.
f195d2
+
f195d2
+The @option{--unicode=invalid} option treats them as non-graphic
f195d2
+characters and hence not part of a valid string.  All the remaining
f195d2
+options treat them as valid string characters.
f195d2
+
f195d2
+The @option{--unicode=locale} option displays them in the current
f195d2
+locale, which may or may not support UTF-8 encoding.  The
f195d2
+@option{--unicode=hex} option displays them as hex byte sequences
f195d2
+enclosed between @var{<>} characters.  The @option{--unicode=escape}
f195d2
+option displays them as escape sequences (@var{\uxxxx}) and the
f195d2
+@option{--unicode=highlight} option displays them as escape sequences
f195d2
+highlighted in red (if supported by the output device).  The colouring
f195d2
+is intended to draw attention to the presence of unicode sequences
f195d2
+where they might not be expected.
f195d2
+
f195d2
 @item -T @var{bfdname}
f195d2
 @itemx --target=@var{bfdname}
f195d2
 @cindex object code format
f195d2
@@ -4603,6 +4658,7 @@ readelf [@option{-a}|@option{--all}]
f195d2
         [@option{-v}|@option{--version}]
f195d2
         [@option{-W}|@option{--wide}]
f195d2
         [@option{-H}|@option{--help}]
f195d2
+        [@option{-U} @var{method}|@option{--unicode=}@var{method}]
f195d2
         @var{elffile}@dots{}
f195d2
 @c man end
f195d2
 @end smallexample
f195d2
@@ -4692,6 +4748,28 @@ Displays the entries in dynamic symbol t
f195d2
 has one.  The output format is the same as the format used by the
f195d2
 @option{--syms} option.
f195d2
 
f195d2
+@item -U @var{[d|i|l|e|x|h]}
f195d2
+@itemx --unicode=[default|invalid|locale|escape|hex|highlight]
f195d2
+Controls the display of non-ASCII characters in identifier names.
f195d2
+The default (@option{--unicode=locale} or @option{--unicode=default}) is
f195d2
+to treat them as multibyte characters and display them in the current
f195d2
+locale.  All other versions of this option treat the bytes as UTF-8
f195d2
+encoded values and attempt to interpret them.  If they cannot be
f195d2
+interpreted or if the @option{--unicode=invalid} option is used then
f195d2
+they are displayed as a sequence of hex bytes, encloses in curly
f195d2
+parethesis characters.
f195d2
+
f195d2
+Using the @option{--unicode=escape} option will display the characters
f195d2
+as as unicode escape sequences (@var{\uxxxx}).  Using the
f195d2
+@option{--unicode=hex} will display the characters as hex byte
f195d2
+sequences enclosed between angle brackets.
f195d2
+
f195d2
+Using the @option{--unicode=highlight} will display the characters as 
f195d2
+unicode escape sequences but it will also highlighted them in red,
f195d2
+assuming that colouring is supported by the output device.  The
f195d2
+colouring is intended to draw attention to the presence of unicode
f195d2
+sequences when they might not be expected.
f195d2
+
f195d2
 @item -e
f195d2
 @itemx --headers
f195d2
 Display all the headers in the file.  Equivalent to @option{-h -l -S}.
f195d2
diff -rup binutils.orig/binutils/nm.c binutils-2.30/binutils/nm.c
f195d2
--- binutils.orig/binutils/nm.c	2021-10-20 12:39:41.681008485 +0100
f195d2
+++ binutils-2.30/binutils/nm.c	2021-10-20 12:52:26.731237376 +0100
f195d2
@@ -38,6 +38,11 @@
f195d2
 #include "bucomm.h"
f195d2
 #include "plugin-api.h"
f195d2
 #include "plugin.h"
f195d2
+#include "safe-ctype.h"
f195d2
+
f195d2
+#ifndef streq
f195d2
+#define streq(a,b) (strcmp ((a),(b)) == 0)
f195d2
+#endif
f195d2
 
f195d2
 /* When sorting by size, we use this structure to hold the size and a
f195d2
    pointer to the minisymbol.  */
f195d2
@@ -194,6 +199,18 @@ static const char *plugin_target = NULL;
f195d2
 static bfd *lineno_cache_bfd;
f195d2
 static bfd *lineno_cache_rel_bfd;
f195d2
 
f195d2
+typedef enum unicode_display_type
f195d2
+{
f195d2
+  unicode_default = 0,
f195d2
+  unicode_locale,
f195d2
+  unicode_escape,
f195d2
+  unicode_hex,
f195d2
+  unicode_highlight,
f195d2
+  unicode_invalid
f195d2
+} unicode_display_type;
f195d2
+
f195d2
+static unicode_display_type unicode_display = unicode_default;
f195d2
+
f195d2
 #define OPTION_TARGET 200
f195d2
 #define OPTION_PLUGIN (OPTION_TARGET + 1)
f195d2
 #define OPTION_SIZE_SORT (OPTION_PLUGIN + 1)
f195d2
@@ -224,6 +241,7 @@ static struct option long_options[] =
f195d2
   {"target", required_argument, 0, OPTION_TARGET},
f195d2
   {"defined-only", no_argument, &defined_only, 1},
f195d2
   {"undefined-only", no_argument, &undefined_only, 1},
f195d2
+  {"unicode", required_argument, NULL, 'U'},
f195d2
   {"version", no_argument, &show_version, 1},
f195d2
   {"with-symbol-versions", no_argument, &with_symbol_versions, 1},
f195d2
   {0, no_argument, 0, 0}
f195d2
@@ -271,6 +289,8 @@ usage (FILE *stream, int status)
f195d2
   -t, --radix=RADIX      Use RADIX for printing symbol values\n\
f195d2
       --target=BFDNAME   Specify the target object format as BFDNAME\n\
f195d2
   -u, --undefined-only   Display only undefined symbols\n\
f195d2
+  -U {d|s|i|x|e|h}       Specify how to treat UTF-8 encoded unicode characters\n\
f195d2
+      --unicode={default|show|invalid|hex|escape|highlight}\n\
f195d2
       --with-symbol-versions  Display version strings after symbol names\n\
f195d2
   -X 32_64               (ignored)\n\
f195d2
   @FILE                  Read options from FILE\n\
f195d2
@@ -399,6 +419,189 @@ get_coff_symbol_type (const struct inter
f195d2
   return bufp;
f195d2
 }
f195d2
 
f195d2
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
f195d2
+   The conversion format is controlled by the unicode_display variable.
f195d2
+   Returns the number of characters added to OUT.
f195d2
+   Returns the number of bytes consumed from IN in CONSUMED.
f195d2
+   Always consumes at least one byte and displays at least one character.  */
f195d2
+   
f195d2
+static unsigned int
f195d2
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
f195d2
+{
f195d2
+  char *        orig_out = out;
f195d2
+  unsigned int  nchars = 0;
f195d2
+
f195d2
+  if (unicode_display == unicode_default)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if (in[0] < 0xc0)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[1] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[0] & 0x20) == 0)
f195d2
+    {
f195d2
+      nchars = 2;
f195d2
+      goto valid;
f195d2
+    }
f195d2
+
f195d2
+  if ((in[2] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[0] & 0x10) == 0)
f195d2
+    {
f195d2
+      nchars = 3;
f195d2
+      goto valid;
f195d2
+    }
f195d2
+
f195d2
+  if ((in[3] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  nchars = 4;
f195d2
+
f195d2
+ valid:
f195d2
+  switch (unicode_display)
f195d2
+    {
f195d2
+    case unicode_locale:
f195d2
+      /* Copy the bytes into the output buffer as is.  */
f195d2
+      memcpy (out, in, nchars);
f195d2
+      out += nchars;
f195d2
+      break;
f195d2
+
f195d2
+    case unicode_invalid:
f195d2
+    case unicode_hex:
f195d2
+      {
f195d2
+      unsigned int j;
f195d2
+
f195d2
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
f195d2
+      for (j = 0; j < nchars; j++)
f195d2
+	out += sprintf (out, "%02x", in [j]);
f195d2
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
f195d2
+      }
f195d2
+      break;
f195d2
+      
f195d2
+    case unicode_highlight:
f195d2
+      if (isatty (1))
f195d2
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
f195d2
+      /* Fall through.  */
f195d2
+    case unicode_escape:
f195d2
+      switch (nchars)
f195d2
+	{
f195d2
+	case 2:
f195d2
+	  out += sprintf (out, "\\u%02x%02x",
f195d2
+		  ((in[0] & 0x1c) >> 2), 
f195d2
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
f195d2
+	  break;
f195d2
+
f195d2
+	case 3:
f195d2
+	  out += sprintf (out, "\\u%02x%02x",
f195d2
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
f195d2
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
f195d2
+	  break;
f195d2
+
f195d2
+	case 4:
f195d2
+	  out += sprintf (out, "\\u%02x%02x%02x",
f195d2
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
f195d2
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
f195d2
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
f195d2
+	  break;
f195d2
+	default:
f195d2
+	  /* URG.  */
f195d2
+	  break;
f195d2
+	}
f195d2
+
f195d2
+      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
f195d2
+      break;
f195d2
+
f195d2
+    default:
f195d2
+      /* URG */
f195d2
+      break;
f195d2
+    }
f195d2
+
f195d2
+  * consumed = nchars;
f195d2
+  return out - orig_out;
f195d2
+
f195d2
+ invalid:
f195d2
+  /* Not a valid UTF-8 sequence.  */
f195d2
+  *out = *in;
f195d2
+  * consumed = 1;
f195d2
+  return 1;
f195d2
+}
f195d2
+
f195d2
+/* Convert any UTF-8 encoded characters in NAME into the form specified by
f195d2
+   unicode_display.  Also converts control characters.  Returns a static
f195d2
+   buffer if conversion was necessary.
f195d2
+   Code stolen from objdump.c:sanitize_string().  */
f195d2
+
f195d2
+static const char *
f195d2
+convert_utf8 (const char * in)
f195d2
+{
f195d2
+  static char *  buffer = NULL;
f195d2
+  static size_t  buffer_len = 0;
f195d2
+  const char *   original = in;
f195d2
+  char *         out;
f195d2
+
f195d2
+  /* Paranoia.  */
f195d2
+  if (in == NULL)
f195d2
+    return "";
f195d2
+
f195d2
+  /* See if any conversion is necessary.
f195d2
+     In the majority of cases it will not be needed.  */
f195d2
+  do
f195d2
+    {
f195d2
+      unsigned char c = *in++;
f195d2
+
f195d2
+      if (c == 0)
f195d2
+	return original;
f195d2
+
f195d2
+      if (ISCNTRL (c))
f195d2
+	break;
f195d2
+
f195d2
+      if (unicode_display != unicode_default && c >= 0xc0)
f195d2
+	break;
f195d2
+    }
f195d2
+  while (1);
f195d2
+
f195d2
+  /* Copy the input, translating as needed.  */
f195d2
+  in = original;
f195d2
+  if (buffer_len < (strlen (in) * 9))
f195d2
+    {
f195d2
+      free ((void *) buffer);
f195d2
+      buffer_len = strlen (in) * 9;
f195d2
+      buffer = xmalloc (buffer_len + 1);
f195d2
+    }
f195d2
+
f195d2
+  out = buffer;
f195d2
+  do
f195d2
+    {
f195d2
+      unsigned char c = *in++;
f195d2
+
f195d2
+      if (c == 0)
f195d2
+	break;
f195d2
+
f195d2
+      if (ISCNTRL (c))
f195d2
+	{
f195d2
+	  *out++ = '^';
f195d2
+	  *out++ = c + 0x40;
f195d2
+	}
f195d2
+      else if (unicode_display != unicode_default && c >= 0xc0)
f195d2
+	{
f195d2
+	  unsigned int num_consumed;
f195d2
+
f195d2
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
f195d2
+	  in += num_consumed - 1;
f195d2
+	}
f195d2
+      else
f195d2
+	*out++ = c;
f195d2
+    }
f195d2
+  while (1);
f195d2
+
f195d2
+  *out = 0;
f195d2
+  return buffer;
f195d2
+}
f195d2
+
f195d2
 /* Print symbol name NAME, read from ABFD, with printf format FORM,
f195d2
    demangling it if requested.  */
f195d2
 
f195d2
@@ -417,6 +618,9 @@ print_symname (const char *form, const c
f195d2
 	}
f195d2
     }
f195d2
 
f195d2
+  if (unicode_display != unicode_default)
f195d2
+    name = convert_utf8 (name);
f195d2
+
f195d2
   printf (form, name);
f195d2
 }
f195d2
 
f195d2
@@ -1657,7 +1861,7 @@ main (int argc, char **argv)
f195d2
   bfd_init ();
f195d2
   set_default_bfd_target ();
f195d2
 
f195d2
-  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
f195d2
+  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uU:vVvX:",
f195d2
 			   long_options, (int *) 0)) != EOF)
f195d2
     {
f195d2
       switch (c)
f195d2
@@ -1738,6 +1942,24 @@ main (int argc, char **argv)
f195d2
 	case 'u':
f195d2
 	  undefined_only = 1;
f195d2
 	  break;
f195d2
+
f195d2
+	case 'U':
f195d2
+	  if (streq (optarg, "default") || streq (optarg, "d"))
f195d2
+	    unicode_display = unicode_default;
f195d2
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
f195d2
+	    unicode_display = unicode_locale;
f195d2
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
f195d2
+	    unicode_display = unicode_escape;
f195d2
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
f195d2
+	    unicode_display = unicode_invalid;
f195d2
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
f195d2
+	    unicode_display = unicode_hex;
f195d2
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
f195d2
+	    unicode_display = unicode_highlight;
f195d2
+	  else
f195d2
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
f195d2
+	  break;
f195d2
+
f195d2
 	case 'V':
f195d2
 	  show_version = 1;
f195d2
 	  break;
f195d2
diff -rup binutils.orig/binutils/objdump.c binutils-2.30/binutils/objdump.c
f195d2
--- binutils.orig/binutils/objdump.c	2021-10-20 12:39:41.677008510 +0100
f195d2
+++ binutils-2.30/binutils/objdump.c	2021-10-20 13:24:11.009293189 +0100
f195d2
@@ -192,6 +192,18 @@ static const struct objdump_private_desc
f195d2
     OBJDUMP_PRIVATE_VECTORS
f195d2
     NULL
f195d2
   };
f195d2
+
f195d2
+typedef enum unicode_display_type
f195d2
+{
f195d2
+  unicode_default = 0,
f195d2
+  unicode_locale,
f195d2
+  unicode_escape,
f195d2
+  unicode_hex,
f195d2
+  unicode_highlight,
f195d2
+  unicode_invalid
f195d2
+} unicode_display_type;
f195d2
+
f195d2
+static unicode_display_type unicode_display = unicode_default;
f195d2
 
f195d2
 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
f195d2
 static void
f195d2
@@ -225,6 +237,9 @@ usage (FILE *stream, int status)
f195d2
   -r, --reloc              Display the relocation entries in the file\n\
f195d2
   -R, --dynamic-reloc      Display the dynamic relocation entries in the file\n\
f195d2
   @<file>                  Read options from <file>\n\
f195d2
+  -U[d|l|i|x|e|h]          Controls the display of UTF-8 unicode characters\n\
f195d2
+  --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
f195d2
+      fprintf (stream, _("\
f195d2
   -v, --version            Display this program's version number\n\
f195d2
   -i, --info               List object formats and architectures supported\n\
f195d2
   -H, --help               Display this information\n\
f195d2
@@ -343,6 +358,7 @@ static struct option long_options[]=
f195d2
   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
f195d2
   {"syms", no_argument, NULL, 't'},
f195d2
   {"target", required_argument, NULL, 'b'},
f195d2
+  {"unicode", required_argument, NULL, 'U'},
f195d2
   {"version", no_argument, NULL, 'V'},
f195d2
   {"wide", no_argument, NULL, 'w'},
f195d2
   {"prefix", required_argument, NULL, OPTION_PREFIX},
f195d2
@@ -362,6 +378,191 @@ nonfatal (const char *msg)
f195d2
   exit_status = 1;
f195d2
 }
f195d2
 
f195d2
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
f195d2
+   The conversion format is controlled by the unicode_display variable.
f195d2
+   Returns the number of characters added to OUT.
f195d2
+   Returns the number of bytes consumed from IN in CONSUMED.
f195d2
+   Always consumes at least one byte and displays at least one character.  */
f195d2
+   
f195d2
+static unsigned int
f195d2
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
f195d2
+{
f195d2
+  char *        orig_out = out;
f195d2
+  unsigned int  nchars = 0;
f195d2
+
f195d2
+  if (unicode_display == unicode_default)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if (in[0] < 0xc0)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[1] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[0] & 0x20) == 0)
f195d2
+    {
f195d2
+      nchars = 2;
f195d2
+      goto valid;
f195d2
+    }
f195d2
+
f195d2
+  if ((in[2] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  if ((in[0] & 0x10) == 0)
f195d2
+    {
f195d2
+      nchars = 3;
f195d2
+      goto valid;
f195d2
+    }
f195d2
+
f195d2
+  if ((in[3] & 0xc0) != 0x80)
f195d2
+    goto invalid;
f195d2
+
f195d2
+  nchars = 4;
f195d2
+
f195d2
+ valid:
f195d2
+  switch (unicode_display)
f195d2
+    {
f195d2
+    case unicode_locale:
f195d2
+      /* Copy the bytes into the output buffer as is.  */
f195d2
+      memcpy (out, in, nchars);
f195d2
+      out += nchars;
f195d2
+      break;
f195d2
+
f195d2
+    case unicode_invalid:
f195d2
+    case unicode_hex:
f195d2
+      {
f195d2
+      unsigned int j;
f195d2
+
f195d2
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
f195d2
+      for (j = 0; j < nchars; j++)
f195d2
+	out += sprintf (out, "%02x", in [j]);
f195d2
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
f195d2
+      }
f195d2
+      break;
f195d2
+      
f195d2
+    case unicode_highlight:
f195d2
+      if (isatty (1))
f195d2
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
f195d2
+      /* Fall through.  */
f195d2
+    case unicode_escape:
f195d2
+      switch (nchars)
f195d2
+	{
f195d2
+	case 2:
f195d2
+	  out += sprintf (out, "\\u%02x%02x",
f195d2
+		  ((in[0] & 0x1c) >> 2), 
f195d2
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
f195d2
+	  break;
f195d2
+
f195d2
+	case 3:
f195d2
+	  out += sprintf (out, "\\u%02x%02x",
f195d2
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
f195d2
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
f195d2
+	  break;
f195d2
+
f195d2
+	case 4:
f195d2
+	  out += sprintf (out, "\\u%02x%02x%02x",
f195d2
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
f195d2
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
f195d2
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
f195d2
+	  break;
f195d2
+	default:
f195d2
+	  /* URG.  */
f195d2
+	  break;
f195d2
+	}
f195d2
+
f195d2
+      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
f195d2
+      break;
f195d2
+
f195d2
+    default:
f195d2
+      /* URG */
f195d2
+      break;
f195d2
+    }
f195d2
+
f195d2
+  * consumed = nchars;
f195d2
+  return out - orig_out;
f195d2
+
f195d2
+ invalid:
f195d2
+  /* Not a valid UTF-8 sequence.  */
f195d2
+  *out = *in;
f195d2
+  * consumed = 1;
f195d2
+  return 1;
f195d2
+}
f195d2
+
f195d2
+/* Returns a version of IN with any control characters
f195d2
+   replaced by escape sequences.  Uses a static buffer
f195d2
+   if necessary.
f195d2
+
f195d2
+   If unicode display is enabled, then also handles the
f195d2
+   conversion of unicode characters.  */
f195d2
+
f195d2
+static const char *
f195d2
+sanitize_string (const char * in)
f195d2
+{
f195d2
+  static char *  buffer = NULL;
f195d2
+  static size_t  buffer_len = 0;
f195d2
+  const char *   original = in;
f195d2
+  char *         out;
f195d2
+
f195d2
+  /* Paranoia.  */
f195d2
+  if (in == NULL)
f195d2
+    return "";
f195d2
+
f195d2
+  /* See if any conversion is necessary.  In the majority
f195d2
+     of cases it will not be needed.  */
f195d2
+  do
f195d2
+    {
f195d2
+      unsigned char c = *in++;
f195d2
+
f195d2
+      if (c == 0)
f195d2
+	return original;
f195d2
+
f195d2
+      if (ISCNTRL (c))
f195d2
+	break;
f195d2
+
f195d2
+      if (unicode_display != unicode_default && c >= 0xc0)
f195d2
+	break;
f195d2
+    }
f195d2
+  while (1);
f195d2
+
f195d2
+  /* Copy the input, translating as needed.  */
f195d2
+  in = original;
f195d2
+  if (buffer_len < (strlen (in) * 9))
f195d2
+    {
f195d2
+      free ((void *) buffer);
f195d2
+      buffer_len = strlen (in) * 9;
f195d2
+      buffer = xmalloc (buffer_len + 1);
f195d2
+    }
f195d2
+
f195d2
+  out = buffer;
f195d2
+  do
f195d2
+    {
f195d2
+      unsigned char c = *in++;
f195d2
+
f195d2
+      if (c == 0)
f195d2
+	break;
f195d2
+
f195d2
+      if (ISCNTRL (c))
f195d2
+	{
f195d2
+	  *out++ = '^';
f195d2
+	  *out++ = c + 0x40;
f195d2
+	}
f195d2
+      else if (unicode_display != unicode_default && c >= 0xc0)
f195d2
+	{
f195d2
+	  unsigned int num_consumed;
f195d2
+
f195d2
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
f195d2
+	  in += num_consumed - 1;
f195d2
+	}
f195d2
+      else
f195d2
+	*out++ = c;
f195d2
+    }
f195d2
+  while (1);
f195d2
+
f195d2
+  *out = 0;
f195d2
+  return buffer;
f195d2
+}
f195d2
+
f195d2
 /* Returns TRUE if the specified section should be dumped.  */
f195d2
 
f195d2
 static bfd_boolean
f195d2
@@ -458,8 +657,8 @@ dump_section_header (bfd *abfd, asection
f195d2
     return;
f195d2
 
f195d2
   printf ("%3d %-*s %08lx  ", section->index, longest_section_name,
f195d2
-	  bfd_get_section_name (abfd, section),
f195d2
-	  (unsigned long) bfd_section_size (abfd, section) / opb);
f195d2
+	  sanitize_string (bfd_section_name (abfd, section)),
f195d2
+	  (unsigned long) bfd_section_size (abfd, section) / opb);
f195d2
   bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
f195d2
   printf ("  ");
f195d2
   bfd_printf_vma (abfd, section->lma);
f195d2
@@ -892,6 +1091,8 @@ objdump_print_symname (bfd *abfd, struct
f195d2
   if (bfd_is_und_section (bfd_get_section (sym)))
f195d2
     hidden = TRUE;
f195d2
 
f195d2
+  name = sanitize_string (name);
f195d2
+
f195d2
   if (inf != NULL)
f195d2
     {
f195d2
       (*inf->fprintf_func) (inf->stream, "%s", name);
f195d2
@@ -1141,7 +1342,7 @@ objdump_print_addr_with_sym (bfd *abfd,
f195d2
       bfd_vma secaddr;
f195d2
 
f195d2
       (*inf->fprintf_func) (inf->stream, " <%s",
f195d2
-			    bfd_get_section_name (abfd, sec));
f195d2
+			    sanitize_string (bfd_section_name (abfd, sec)));
f195d2
       secaddr = bfd_get_section_vma (abfd, sec);
f195d2
       if (vma < secaddr)
f195d2
 	{
f195d2
@@ -1563,7 +1764,7 @@ show_line (bfd *abfd, asection *section,
f195d2
 	  && (prev_functionname == NULL
f195d2
 	      || strcmp (functionname, prev_functionname) != 0))
f195d2
 	{
f195d2
-	  printf ("%s():\n", functionname);
f195d2
+	  printf ("%s():\n", sanitize_string (functionname));
f195d2
 	  prev_line = -1;
f195d2
 	}
f195d2
       if (linenumber > 0
f195d2
@@ -1572,10 +1773,10 @@ show_line (bfd *abfd, asection *section,
f195d2
 	{
f195d2
 	  if (discriminator > 0)
f195d2
 	    printf ("%s:%u (discriminator %u)\n",
f195d2
-		    filename == NULL ? "???" : filename,
f195d2
+		    filename == NULL ? "???" : sanitize_string (filename),
f195d2
 		    linenumber, discriminator);
f195d2
 	  else
f195d2
-	    printf ("%s:%u\n", filename == NULL ? "???" : filename,
f195d2
+	    printf ("%s:%u\n", filename == NULL ? "???" : sanitize_string (filename),
f195d2
 		    linenumber);
f195d2
 	}
f195d2
       if (unwind_inlines)
f195d2
@@ -1586,8 +1787,10 @@ show_line (bfd *abfd, asection *section,
f195d2
 
f195d2
 	  while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
f195d2
 					&line2))
f195d2
-	    printf ("inlined by %s:%u (%s)\n", filename2, line2,
f195d2
-		    functionname2);
f195d2
+	    {
f195d2
+	      printf ("inlined by %s:%u", sanitize_string (filename2), line2);
f195d2
+	      printf (" (%s)\n", sanitize_string (functionname2));
f195d2
+	    }
f195d2
 	}
f195d2
     }
f195d2
 
f195d2
@@ -2081,7 +2284,7 @@ disassemble_bytes (struct disassemble_in
f195d2
 		      sym_name = bfd_get_section_name (aux->abfd, sym_sec);
f195d2
 		      if (sym_name == NULL || *sym_name == '\0')
f195d2
 			sym_name = "*unknown*";
f195d2
-		      printf ("%s", sym_name);
f195d2
+		      printf ("%s", sanitize_string (sym_name));
f195d2
 		    }
f195d2
 		}
f195d2
 
f195d2
@@ -2228,7 +2431,7 @@ disassemble_section (bfd *abfd, asection
f195d2
 	 && (*rel_pp)->address < rel_offset + addr_offset)
f195d2
     ++rel_pp;
f195d2
 
f195d2
-  printf (_("\nDisassembly of section %s:\n"), section->name);
f195d2
+  printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
f195d2
 
f195d2
   /* Find the nearest symbol forwards from our current position.  */
f195d2
   paux->require_sec = TRUE;
f195d2
@@ -2512,7 +2715,7 @@ load_specific_debug_section (enum dwarf_
f195d2
     {
f195d2
       free_debug_section (debug);
f195d2
       printf (_("\nCan't get contents for section '%s'.\n"),
f195d2
-	      section->name);
f195d2
+	      sanitize_string (section->name));
f195d2
       return FALSE;
f195d2
     }
f195d2
   /* Ensure any string section has a terminating NUL.  */
f195d2
@@ -2534,7 +2737,7 @@ load_specific_debug_section (enum dwarf_
f195d2
         {
f195d2
           free_debug_section (debug);
f195d2
           printf (_("\nCan't get contents for section '%s'.\n"),
f195d2
-	          section->name);
f195d2
+	          sanitize_string (section->name));
f195d2
           return FALSE;
f195d2
         }
f195d2
 
f195d2
@@ -2780,14 +2983,14 @@ read_section_stabs (bfd *abfd, const cha
f195d2
   stabsect = bfd_get_section_by_name (abfd, sect_name);
f195d2
   if (stabsect == NULL)
f195d2
     {
f195d2
-      printf (_("No %s section present\n\n"), sect_name);
f195d2
+      printf (_("No %s section present\n\n"), sanitize_string (sect_name));
f195d2
       return FALSE;
f195d2
     }
f195d2
 
f195d2
   if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
f195d2
     {
f195d2
       non_fatal (_("reading %s section of %s failed: %s"),
f195d2
-		 sect_name, bfd_get_filename (abfd),
f195d2
+		 sanitize_string (sect_name), bfd_get_filename (abfd),
f195d2
 		 bfd_errmsg (bfd_get_error ()));
f195d2
       exit_status = 1;
f195d2
       free (contents);
f195d2
@@ -2830,7 +3033,7 @@ print_section_stabs (bfd *abfd,
f195d2
   stabp = stabs;
f195d2
   stabs_end = stabp + stab_size;
f195d2
 
f195d2
-  printf (_("Contents of %s section:\n\n"), stabsect_name);
f195d2
+  printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
f195d2
   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
f195d2
 
f195d2
   /* Loop through all symbols and print them.
f195d2
@@ -3093,7 +3296,7 @@ dump_section (bfd *abfd, asection *secti
f195d2
   if (start_offset >= stop_offset)
f195d2
     return;
f195d2
 
f195d2
-  printf (_("Contents of section %s:"), section->name);
f195d2
+  printf (_("Contents of section %s:"), sanitize_string (section->name));
f195d2
   if (display_file_offsets)
f195d2
     printf (_("  (Starting at file offset: 0x%lx)"),
f195d2
 	    (unsigned long) (section->filepos + start_offset));
f195d2
@@ -3102,7 +3305,7 @@ dump_section (bfd *abfd, asection *secti
f195d2
   if (!bfd_get_full_section_contents (abfd, section, &data))
f195d2
     {
f195d2
       non_fatal (_("Reading section %s failed because: %s"),
f195d2
-		 section->name, bfd_errmsg (bfd_get_error ()));
f195d2
+		 sanitize_string (section->name), bfd_errmsg (bfd_get_error ()));
f195d2
       return;
f195d2
     }
f195d2
 
f195d2
@@ -3242,6 +3445,24 @@ dump_symbols (bfd *abfd ATTRIBUTE_UNUSED
f195d2
 		  free (alloc);
f195d2
 		}
f195d2
 	    }
f195d2
+	  else if (unicode_display != unicode_default
f195d2
+		   && name != NULL && *name != '\0')
f195d2
+	    {
f195d2
+	      const char * sanitized_name;
f195d2
+
f195d2
+	      /* If we want to sanitize the name, we do it here, and
f195d2
+		 temporarily clobber it while calling bfd_print_symbol.
f195d2
+		 FIXME: This is a gross hack.  */
f195d2
+	      sanitized_name = sanitize_string (name);
f195d2
+	      if (sanitized_name != name)
f195d2
+		(*current)->name = sanitized_name;
f195d2
+	      else
f195d2
+		sanitized_name = NULL;
f195d2
+	      bfd_print_symbol (cur_bfd, stdout, *current,
f195d2
+				bfd_print_symbol_all);
f195d2
+	      if (sanitized_name != NULL)
f195d2
+		(*current)->name = name;
f195d2
+	    }
f195d2
 	  else
f195d2
 	    bfd_print_symbol (cur_bfd, stdout, *current,
f195d2
 			      bfd_print_symbol_all);
f195d2
@@ -3307,7 +3528,7 @@ dump_reloc_set (bfd *abfd, asection *sec
f195d2
 	      && (last_functionname == NULL
f195d2
 		  || strcmp (functionname, last_functionname) != 0))
f195d2
 	    {
f195d2
-	      printf ("%s():\n", functionname);
f195d2
+	      printf ("%s():\n", sanitize_string (functionname));
f195d2
 	      if (last_functionname != NULL)
f195d2
 		free (last_functionname);
f195d2
 	      last_functionname = xstrdup (functionname);
f195d2
@@ -3321,9 +3542,9 @@ dump_reloc_set (bfd *abfd, asection *sec
f195d2
                   || (discriminator != last_discriminator)))
f195d2
 	    {
f195d2
               if (discriminator > 0)
f195d2
-                printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
f195d2
+                printf ("%s:%u\n", filename == NULL ? "???" : sanitize_string (filename), linenumber);
f195d2
               else
f195d2
-                printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
f195d2
+                printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : sanitize_string (filename),
f195d2
                         linenumber, discriminator);
f195d2
 	      last_line = linenumber;
f195d2
 	      last_discriminator = discriminator;
f195d2
@@ -3393,7 +3614,7 @@ dump_reloc_set (bfd *abfd, asection *sec
f195d2
 	{
f195d2
 	  if (section_name == NULL)
f195d2
 	    section_name = "*unknown*";
f195d2
-	  printf ("[%s]", section_name);
f195d2
+	  printf ("[%s]", sanitize_string (section_name));
f195d2
 	}
f195d2
 
f195d2
       if (q->addend)
f195d2
@@ -3443,7 +3664,7 @@ dump_relocs_in_section (bfd *abfd,
f195d2
   if (relsize < 0)
f195d2
     bfd_fatal (bfd_get_filename (abfd));
f195d2
 
f195d2
-  printf ("RELOCATION RECORDS FOR [%s]:", section->name);
f195d2
+  printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
f195d2
 
f195d2
   if (relsize == 0)
f195d2
     {
f195d2
@@ -3835,7 +4056,7 @@ main (int argc, char **argv)
f195d2
   set_default_bfd_target ();
f195d2
 
f195d2
   while ((c = getopt_long (argc, argv,
f195d2
-			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
f195d2
+			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::U:",
f195d2
 			   long_options, (int *) 0))
f195d2
 	 != EOF)
f195d2
     {
f195d2
@@ -4069,6 +4290,23 @@ main (int argc, char **argv)
f195d2
 	  seenflag = TRUE;
f195d2
 	  break;
f195d2
 
f195d2
+	case 'U':
f195d2
+	  if (streq (optarg, "default") || streq (optarg, "d"))
f195d2
+	    unicode_display = unicode_default;
f195d2
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
f195d2
+	    unicode_display = unicode_locale;
f195d2
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
f195d2
+	    unicode_display = unicode_escape;
f195d2
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
f195d2
+	    unicode_display = unicode_invalid;
f195d2
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
f195d2
+	    unicode_display = unicode_hex;
f195d2
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
f195d2
+	    unicode_display = unicode_highlight;
f195d2
+	  else
f195d2
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
f195d2
+	  break;
f195d2
+
f195d2
 	case 'H':
f195d2
 	  usage (stdout, 0);
f195d2
 	  /* No need to set seenflag or to break - usage() does not return.  */
f195d2
diff -rup binutils.orig/binutils/strings.c binutils-2.30/binutils/strings.c
f195d2
--- binutils.orig/binutils/strings.c	2021-10-20 12:39:41.686008454 +0100
f195d2
+++ binutils-2.30/binutils/strings.c	2021-10-20 13:32:22.029182908 +0100
f195d2
@@ -55,6 +55,19 @@
f195d2
    -T {bfdname}
f195d2
 		Specify a non-default object file format.
f195d2
 
f195d2
+  --unicode={default|locale|invalid|hex|escape|highlight}
f195d2
+  -U {d|l|i|x|e|h}
f195d2
+                Determine how to handle UTF-8 unicode characters.  The default
f195d2
+		is no special treatment.  All other versions of this option
f195d2
+		only apply if the encoding is valid and enabling the option
f195d2
+		implies --encoding=S.
f195d2
+		The 'locale' option displays the characters according to the
f195d2
+		current locale.  The 'invalid' option treats them as
f195d2
+		non-string characters.  The 'hex' option displays them as hex
f195d2
+		byte sequences.  The 'escape' option displays them as escape
f195d2
+		sequences and the 'highlight' option displays them as
f195d2
+		coloured escape sequences.
f195d2
+
f195d2
   --output-separator=sep_string
f195d2
   -s sep_string	String used to separate parsed strings in output.
f195d2
 		Default is newline.
f195d2
@@ -76,6 +89,22 @@
f195d2
 #include "safe-ctype.h"
f195d2
 #include "bucomm.h"
f195d2
 
f195d2
+#ifndef streq
f195d2
+#define streq(a,b) (strcmp ((a),(b)) == 0)
f195d2
+#endif
f195d2
+
f195d2
+typedef enum unicode_display_type
f195d2
+{
f195d2
+  unicode_default = 0,
f195d2
+  unicode_locale,
f195d2
+  unicode_escape,
f195d2
+  unicode_hex,
f195d2
+  unicode_highlight,
f195d2
+  unicode_invalid
f195d2
+} unicode_display_type;
f195d2
+
f195d2
+static unicode_display_type unicode_display = unicode_default;
f195d2
+
f195d2
 #define STRING_ISGRAPHIC(c) \
f195d2
       (   (c) >= 0 \
f195d2
        && (c) <= 255 \
f195d2
@@ -94,7 +123,7 @@ extern int errno;
f195d2
 static int address_radix;
f195d2
 
f195d2
 /* Minimum length of sequence of graphic chars to trigger output.  */
f195d2
-static int string_min;
f195d2
+static uint string_min;
f195d2
 
f195d2
 /* Whether or not we include all whitespace as a graphic char.   */
f195d2
 static bfd_boolean include_all_whitespace;
f195d2
@@ -130,6 +159,7 @@ static struct option long_options[] =
f195d2
   {"target", required_argument, NULL, 'T'},
f195d2
   {"output-separator", required_argument, NULL, 's'},
f195d2
   {"help", no_argument, NULL, 'h'},
f195d2
+  {"unicode", required_argument, NULL, 'U'},
f195d2
   {"version", no_argument, NULL, 'v'},
f195d2
   {NULL, 0, NULL, 0}
f195d2
 };
f195d2
@@ -173,7 +203,7 @@ main (int argc, char **argv)
f195d2
   encoding = 's';
f195d2
   output_separator = NULL;
f195d2
 
f195d2
-  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:Vv0123456789",
f195d2
+  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:U:Vv0123456789",
f195d2
 			      long_options, (int *) 0)) != EOF)
f195d2
     {
f195d2
       switch (optc)
f195d2
@@ -246,6 +276,23 @@ main (int argc, char **argv)
f195d2
 	  output_separator = optarg;
f195d2
           break;
f195d2
 
f195d2
+	case 'U':
f195d2
+	  if (streq (optarg, "default") || streq (optarg, "d"))
f195d2
+	    unicode_display = unicode_default;
f195d2
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
f195d2
+	    unicode_display = unicode_locale;
f195d2
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
f195d2
+	    unicode_display = unicode_escape;
f195d2
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
f195d2
+	    unicode_display = unicode_invalid;
f195d2
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
f195d2
+	    unicode_display = unicode_hex;
f195d2
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
f195d2
+	    unicode_display = unicode_highlight;
f195d2
+	  else
f195d2
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
f195d2
+	  break;
f195d2
+
f195d2
 	case 'V':
f195d2
 	case 'v':
f195d2
 	  print_version ("strings");
f195d2
@@ -260,6 +307,9 @@ main (int argc, char **argv)
f195d2
 	}
f195d2
     }
f195d2
 
f195d2
+  if (unicode_display != unicode_default)
f195d2
+    encoding = 'S';
f195d2
+
f195d2
   if (numeric_opt != 0)
f195d2
     {
f195d2
       string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
f195d2
@@ -501,11 +551,629 @@ get_char (FILE *stream, file_ptr *addres
f195d2
 
f195d2
   return r;
f195d2
 }
f195d2
+
f195d2
+static void
f195d2
+print_filename_and_address (const char * filename, file_ptr address)
f195d2
+{
f195d2
+  if (print_filenames)
f195d2
+    printf ("%s: ", filename);
f195d2
+
f195d2
+  if (! print_addresses)
f195d2
+    return;
f195d2
+
f195d2
+  switch (address_radix)
f195d2
+    {
f195d2
+    case 8:
f195d2
+      if (sizeof (address) > sizeof (long))
f195d2
+	{
f195d2
+#ifndef __MSVCRT__
f195d2
+	  printf ("%7llo ", (unsigned long long) address);
f195d2
+#else
f195d2
+	  printf ("%7I64o ", (unsigned long long) address);
f195d2
+#endif
f195d2
+	}
f195d2
+      else
f195d2
+	printf ("%7lo ", (unsigned long) address);
f195d2
+      break;
f195d2
+
f195d2
+    case 10:
f195d2
+      if (sizeof (address) > sizeof (long))
f195d2
+	{
f195d2
+#ifndef __MSVCRT__
f195d2
+	  printf ("%7llu ", (unsigned long long) address);
f195d2
+#else
f195d2
+	  printf ("%7I64d ", (unsigned long long) address);
f195d2
+#endif
f195d2
+	}
f195d2
+      else
f195d2
+	printf ("%7ld ", (long) address);
f195d2
+      break;
f195d2
+
f195d2
+    case 16:
f195d2
+      if (sizeof (address) > sizeof (long))
f195d2
+	{
f195d2
+#ifndef __MSVCRT__
f195d2
+	  printf ("%7llx ", (unsigned long long) address);
f195d2
+#else
f195d2
+	  printf ("%7I64x ", (unsigned long long) address);
f195d2
+#endif
f195d2
+	}
f195d2
+      else
f195d2
+	printf ("%7lx ", (unsigned long) address);
f195d2
+      break;
f195d2
+    }
f195d2
+}
f195d2
+
f195d2
+/* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
f195d2
+   If the encoding is valid then returns the number of bytes it uses.  */
f195d2
+
f195d2
+static unsigned int
f195d2
+is_valid_utf8 (const unsigned char * buffer, unsigned long buflen)
f195d2
+{
f195d2
+  if (buffer[0] < 0xc0)
f195d2
+    return 0;
f195d2
+
f195d2
+  if (buflen < 2)
f195d2
+    return 0;
f195d2
+
f195d2
+  if ((buffer[1] & 0xc0) != 0x80)
f195d2
+    return 0;
f195d2
+
f195d2
+  if ((buffer[0] & 0x20) == 0)
f195d2
+    return 2;
f195d2
+
f195d2
+  if (buflen < 3)
f195d2
+    return 0;
f195d2
+
f195d2
+  if ((buffer[2] & 0xc0) != 0x80)
f195d2
+    return 0;
f195d2
+  
f195d2
+  if ((buffer[0] & 0x10) == 0)
f195d2
+    return 3;
f195d2
+
f195d2
+  if (buflen < 4)
f195d2
+    return 0;
f195d2
+
f195d2
+  if ((buffer[3] & 0xc0) != 0x80)
f195d2
+    return 0;
f195d2
+
f195d2
+  return 4;
f195d2
+}
f195d2
+
f195d2
+/* Display a UTF-8 encoded character in BUFFER according to the setting
f195d2
+   of unicode_display.  The character is known to be valid.
f195d2
+   Returns the number of bytes consumed.  */
f195d2
+
f195d2
+static unsigned int
f195d2
+display_utf8_char (const unsigned char * buffer)
f195d2
+{
f195d2
+  unsigned int j;
f195d2
+  unsigned int utf8_len;
f195d2
+
f195d2
+  switch (buffer[0] & 0x30)
f195d2
+    {
f195d2
+    case 0x00:
f195d2
+    case 0x10:
f195d2
+      utf8_len = 2;
f195d2
+      break;
f195d2
+    case 0x20:
f195d2
+      utf8_len = 3;
f195d2
+      break;
f195d2
+    default:
f195d2
+      utf8_len = 4;
f195d2
+    }
f195d2
+      
f195d2
+  switch (unicode_display)
f195d2
+    {
f195d2
+    default:
f195d2
+      fprintf (stderr, "ICE: unexpected unicode display type\n");
f195d2
+      break;
f195d2
+
f195d2
+    case unicode_escape:
f195d2
+    case unicode_highlight:
f195d2
+      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+	printf ("\x1B[31;47m"); /* Red.  */
f195d2
+
f195d2
+      switch (utf8_len)
f195d2
+	{
f195d2
+	case 2:
f195d2
+	  printf ("\\u%02x%02x",
f195d2
+		  ((buffer[0] & 0x1c) >> 2), 
f195d2
+		  ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
f195d2
+	  break;
f195d2
+
f195d2
+	case 3:
f195d2
+	  printf ("\\u%02x%02x",
f195d2
+		  ((buffer[0] & 0x0f) << 4) | ((buffer[1] & 0x3c) >> 2),
f195d2
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3f)));
f195d2
+	  break;
f195d2
+
f195d2
+	case 4:
f195d2
+	  printf ("\\u%02x%02x%02x",
f195d2
+		  ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
f195d2
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
f195d2
+		  ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
f195d2
+	  break;
f195d2
+	default:
f195d2
+	  /* URG.  */
f195d2
+	  break;
f195d2
+	}
f195d2
+
f195d2
+      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+	printf ("\033[0m"); /* Default colour.  */
f195d2
+      break;
f195d2
+
f195d2
+    case unicode_hex:
f195d2
+      putchar ('<');
f195d2
+      for (j = 0; j < utf8_len; j++)
f195d2
+	printf ("%02x", buffer [j]);
f195d2
+      putchar ('>');
f195d2
+      break;
f195d2
+
f195d2
+    case unicode_locale:
f195d2
+      printf ("%.1s", buffer);
f195d2
+      break;
f195d2
+    }
f195d2
+
f195d2
+  return utf8_len;
f195d2
+}
f195d2
+
f195d2
+/* Display strings in BUFFER.  Treat any UTF-8 encoded characters encountered
f195d2
+   according to the setting of the unicode_display variable.  The buffer
f195d2
+   contains BUFLEN bytes.
f195d2
+
f195d2
+   Display the characters as if they started at ADDRESS and are contained in
f195d2
+   FILENAME.  */
f195d2
+
f195d2
+static void
f195d2
+print_unicode_buffer (const char *            filename,
f195d2
+		      file_ptr                address,
f195d2
+		      const unsigned char *   buffer,
f195d2
+		      unsigned long           buflen)
f195d2
+{
f195d2
+  /* Paranoia checks...  */
f195d2
+  if (filename == NULL
f195d2
+      || buffer == NULL
f195d2
+      || unicode_display == unicode_default
f195d2
+      || encoding != 'S'
f195d2
+      || encoding_bytes != 1)
f195d2
+    {
f195d2
+      fprintf (stderr, "ICE: bad arguments to print_unicode_buffer\n");
f195d2
+      return;
f195d2
+    }
f195d2
+
f195d2
+  if (buflen == 0)
f195d2
+    return;
f195d2
+
f195d2
+  /* We must only display strings that are at least string_min *characters*
f195d2
+     long.  So we scan the buffer in two stages.  First we locate the start
f195d2
+     of a potential string.  Then we walk along it until we have found
f195d2
+     string_min characters.  Then we go back to the start point and start
f195d2
+     displaying characters according to the unicode_display setting.  */
f195d2
+
f195d2
+  unsigned long start_point = 0;
f195d2
+  unsigned long i = 0;
f195d2
+  unsigned int char_len = 1;
f195d2
+  unsigned int num_found = 0;
f195d2
+
f195d2
+  for (i = 0; i < buflen; i += char_len)
f195d2
+    {
f195d2
+      int c = buffer[i];
f195d2
+
f195d2
+      char_len = 1;
f195d2
+
f195d2
+      /* Find the first potential character of a string.  */
f195d2
+      if (! STRING_ISGRAPHIC (c))
f195d2
+	{
f195d2
+	  num_found = 0;
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      if (c > 126)
f195d2
+	{
f195d2
+	  if (c < 0xc0)
f195d2
+	    {
f195d2
+	      num_found = 0;
f195d2
+	      continue;
f195d2
+	    }
f195d2
+
f195d2
+	  if ((char_len = is_valid_utf8 (buffer + i, buflen - i)) == 0)
f195d2
+	    {
f195d2
+	      char_len = 1;
f195d2
+	      num_found = 0;
f195d2
+	      continue;
f195d2
+	    }
f195d2
+
f195d2
+	  if (unicode_display == unicode_invalid)
f195d2
+	    {
f195d2
+	      /* We have found a valid UTF-8 character, but we treat it as non-graphic.  */
f195d2
+	      num_found = 0;
f195d2
+	      continue;
f195d2
+	    }
f195d2
+	}
f195d2
+
f195d2
+      if (num_found == 0)
f195d2
+	/* We have found a potential starting point for a string.  */
f195d2
+	start_point = i;
f195d2
+
f195d2
+      ++ num_found;
f195d2
+
f195d2
+      if (num_found >= string_min)
f195d2
+	break;
f195d2
+    }
f195d2
+
f195d2
+  if (num_found < string_min)
f195d2
+    return;
f195d2
+
f195d2
+  print_filename_and_address (filename, address + start_point);
f195d2
+  
f195d2
+  /* We have found string_min characters.  Display them and any
f195d2
+     more that follow.  */
f195d2
+  for (i = start_point; i < buflen; i += char_len)
f195d2
+    {
f195d2
+      int c = buffer[i];
f195d2
+
f195d2
+      char_len = 1;
f195d2
+
f195d2
+      if (! STRING_ISGRAPHIC (c))
f195d2
+	break;
f195d2
+      else if (c < 127)
f195d2
+	putchar (c);
f195d2
+      else if (! is_valid_utf8 (buffer + i, buflen - i))
f195d2
+	break;
f195d2
+      else if (unicode_display == unicode_invalid)
f195d2
+	break;
f195d2
+      else
f195d2
+	char_len = display_utf8_char (buffer + i);
f195d2
+    }
f195d2
+
f195d2
+  if (output_separator)
f195d2
+    fputs (output_separator, stdout);
f195d2
+  else
f195d2
+    putchar ('\n');
f195d2
+
f195d2
+  /* FIXME: Using tail recursion here is lazy programming...  */
f195d2
+  print_unicode_buffer (filename, address + i, buffer + i, buflen - i);
f195d2
+}
f195d2
+
f195d2
+static int
f195d2
+get_unicode_byte (FILE *           stream,
f195d2
+		  unsigned char *  putback,
f195d2
+		  unsigned int *   num_putback,
f195d2
+		  unsigned int *   num_read)
f195d2
+{
f195d2
+  if (* num_putback > 0)
f195d2
+    {
f195d2
+      * num_putback = * num_putback - 1;
f195d2
+      return putback [* num_putback];
f195d2
+    }
f195d2
+
f195d2
+  * num_read = * num_read + 1;
f195d2
+
f195d2
+#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
f195d2
+  return getc_unlocked (stream);
f195d2
+#else
f195d2
+  return getc (stream);
f195d2
+#endif
f195d2
+}
f195d2
+
f195d2
+/* Helper function for print_unicode_stream.  */
f195d2
+
f195d2
+static void
f195d2
+print_unicode_stream_body (const char *     filename,
f195d2
+			   file_ptr         address,
f195d2
+			   FILE *           stream,
f195d2
+			   unsigned char *  putback_buf,
f195d2
+			   unsigned int     num_putback,
f195d2
+			   unsigned char *  print_buf)
f195d2
+{
f195d2
+  /* It would be nice if we could just read the stream into a buffer
f195d2
+     and then process if with print_unicode_buffer.  But the input
f195d2
+     might be huge or it might time-locked (eg stdin).  So instead
f195d2
+     we go one byte at a time...  */
f195d2
+
f195d2
+  file_ptr start_point = 0;
f195d2
+  unsigned int num_read = 0;
f195d2
+  unsigned int num_chars = 0;
f195d2
+  unsigned int num_print = 0;
f195d2
+  int c;
f195d2
+
f195d2
+  /* Find a series of string_min characters.  Put them into print_buf.  */
f195d2
+  do
f195d2
+    {
f195d2
+      if (num_chars >= string_min)
f195d2
+	break;
f195d2
+
f195d2
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+      if (c == EOF)
f195d2
+	break;
f195d2
+
f195d2
+      if (! STRING_ISGRAPHIC (c))
f195d2
+	{
f195d2
+	  num_chars = num_print = 0;
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      if (num_chars == 0)
f195d2
+	start_point = num_read - 1;
f195d2
+
f195d2
+      if (c < 127)
f195d2
+	{
f195d2
+	  print_buf[num_print] = c;
f195d2
+	  num_chars ++;
f195d2
+	  num_print ++;
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      if (c < 0xc0)
f195d2
+	{
f195d2
+	  num_chars = num_print = 0;
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
f195d2
+      char utf8[4];
f195d2
+
f195d2
+      utf8[0] = c;
f195d2
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+      if (c == EOF)
f195d2
+	break;
f195d2
+      utf8[1] = c;
f195d2
+
f195d2
+      if ((utf8[1] & 0xc0) != 0x80)
f195d2
+	{
f195d2
+	  /* Invalid UTF-8.  */
f195d2
+	  putback_buf[num_putback++] = utf8[1];
f195d2
+	  num_chars = num_print = 0;
f195d2
+	  continue;
f195d2
+	}
f195d2
+      else if ((utf8[0] & 0x20) == 0)
f195d2
+	{
f195d2
+	  /* A valid 2-byte UTF-8 encoding.  */
f195d2
+	  if (unicode_display == unicode_invalid)
f195d2
+	    {
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      num_chars = num_print = 0;
f195d2
+	    }
f195d2
+	  else
f195d2
+	    {
f195d2
+	      print_buf[num_print ++] = utf8[0];
f195d2
+	      print_buf[num_print ++] = utf8[1];
f195d2
+	      num_chars ++;
f195d2
+	    }
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+      if (c == EOF)
f195d2
+	break;
f195d2
+      utf8[2] = c;
f195d2
+
f195d2
+      if ((utf8[2] & 0xc0) != 0x80)
f195d2
+	{
f195d2
+	  /* Invalid UTF-8.  */
f195d2
+	  putback_buf[num_putback++] = utf8[2];
f195d2
+	  putback_buf[num_putback++] = utf8[1];
f195d2
+	  num_chars = num_print = 0;
f195d2
+	  continue;
f195d2
+	}
f195d2
+      else if ((utf8[0] & 0x10) == 0)
f195d2
+	{
f195d2
+	  /* A valid 3-byte UTF-8 encoding.  */
f195d2
+	  if (unicode_display == unicode_invalid)
f195d2
+	    {
f195d2
+	      putback_buf[num_putback++] = utf8[2];
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      num_chars = num_print = 0;
f195d2
+	    }
f195d2
+	  else
f195d2
+	    {
f195d2
+	      print_buf[num_print ++] = utf8[0];
f195d2
+	      print_buf[num_print ++] = utf8[1];
f195d2
+	      print_buf[num_print ++] = utf8[2];
f195d2
+	      num_chars ++;
f195d2
+	    }
f195d2
+	  continue;
f195d2
+	}
f195d2
+
f195d2
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+      if (c == EOF)
f195d2
+	break;
f195d2
+      utf8[3] = c;
f195d2
+
f195d2
+      if ((utf8[3] & 0xc0) != 0x80)
f195d2
+	{
f195d2
+	  /* Invalid UTF-8.  */
f195d2
+	  putback_buf[num_putback++] = utf8[3];
f195d2
+	  putback_buf[num_putback++] = utf8[2];
f195d2
+	  putback_buf[num_putback++] = utf8[1];
f195d2
+	  num_chars = num_print = 0;
f195d2
+	}
f195d2
+      /* We have a valid 4-byte UTF-8 encoding.  */
f195d2
+      else if (unicode_display == unicode_invalid)
f195d2
+	{
f195d2
+	  putback_buf[num_putback++] = utf8[3];
f195d2
+	  putback_buf[num_putback++] = utf8[1];
f195d2
+	  putback_buf[num_putback++] = utf8[2];
f195d2
+	  num_chars = num_print = 0;
f195d2
+	}
f195d2
+      else
f195d2
+	{
f195d2
+	  print_buf[num_print ++] = utf8[0];
f195d2
+	  print_buf[num_print ++] = utf8[1];
f195d2
+	  print_buf[num_print ++] = utf8[2];
f195d2
+	  print_buf[num_print ++] = utf8[3];
f195d2
+	  num_chars ++;
f195d2
+	}
f195d2
+    }
f195d2
+  while (1);
f195d2
+
f195d2
+  if (num_chars >= string_min)
f195d2
+    {
f195d2
+      /* We know that we have string_min valid characters in print_buf,
f195d2
+	 and there may be more to come in the stream.  Start displaying
f195d2
+	 them.  */
f195d2
+
f195d2
+      print_filename_and_address (filename, address + start_point);
f195d2
+
f195d2
+      unsigned int i;
f195d2
+      for (i = 0; i < num_print;)
f195d2
+	{
f195d2
+	  if (print_buf[i] < 127)
f195d2
+	    putchar (print_buf[i++]);
f195d2
+	  else
f195d2
+	    i += display_utf8_char (print_buf + i);
f195d2
+	}
f195d2
+
f195d2
+      /* OK so now we have to start read unchecked bytes.  */
f195d2
+
f195d2
+        /* Find a series of string_min characters.  Put them into print_buf.  */
f195d2
+      do
f195d2
+	{
f195d2
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+	  if (c == EOF)
f195d2
+	    break;
f195d2
+
f195d2
+	  if (! STRING_ISGRAPHIC (c))
f195d2
+	    break;
f195d2
+
f195d2
+	  if (c < 127)
f195d2
+	    {
f195d2
+	      putchar (c);
f195d2
+	      continue;
f195d2
+	    }
f195d2
+
f195d2
+	  if (c < 0xc0)
f195d2
+	    break;
f195d2
+
f195d2
+	  /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
f195d2
+	  unsigned char utf8[4];
f195d2
+
f195d2
+	  utf8[0] = c;
f195d2
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+	  if (c == EOF)
f195d2
+	    break;
f195d2
+	  utf8[1] = c;
f195d2
+
f195d2
+	  if ((utf8[1] & 0xc0) != 0x80)
f195d2
+	    {
f195d2
+	      /* Invalid UTF-8.  */
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      break;
f195d2
+	    }
f195d2
+	  else if ((utf8[0] & 0x20) == 0)
f195d2
+	    {
f195d2
+	      /* Valid 2-byte UTF-8.  */
f195d2
+	      if (unicode_display == unicode_invalid)
f195d2
+		{
f195d2
+		  putback_buf[num_putback++] = utf8[1];
f195d2
+		  break;
f195d2
+		}
f195d2
+	      else
f195d2
+		{
f195d2
+		  (void) display_utf8_char (utf8);
f195d2
+		  continue;
f195d2
+		}
f195d2
+	    }
f195d2
+
f195d2
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+	  if (c == EOF)
f195d2
+	    break;
f195d2
+	  utf8[2] = c;
f195d2
+
f195d2
+	  if ((utf8[2] & 0xc0) != 0x80)
f195d2
+	    {
f195d2
+	      /* Invalid UTF-8.  */
f195d2
+	      putback_buf[num_putback++] = utf8[2];
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      break;
f195d2
+	    }
f195d2
+	  else if ((utf8[0] & 0x10) == 0)
f195d2
+	    {
f195d2
+	      /* Valid 3-byte UTF-8.  */
f195d2
+	      if (unicode_display == unicode_invalid)
f195d2
+		{
f195d2
+		  putback_buf[num_putback++] = utf8[2];
f195d2
+		  putback_buf[num_putback++] = utf8[1];
f195d2
+		  break;
f195d2
+		}
f195d2
+	      else
f195d2
+		{
f195d2
+		  (void) display_utf8_char (utf8);
f195d2
+		  continue;
f195d2
+		}
f195d2
+	    }
f195d2
+
f195d2
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
f195d2
+	  if (c == EOF)
f195d2
+	    break;
f195d2
+	  utf8[3] = c;
f195d2
+
f195d2
+	  if ((utf8[3] & 0xc0) != 0x80)
f195d2
+	    {
f195d2
+	      /* Invalid UTF-8.  */
f195d2
+	      putback_buf[num_putback++] = utf8[3];
f195d2
+	      putback_buf[num_putback++] = utf8[2];
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      break;
f195d2
+	    }
f195d2
+	  else if (unicode_display == unicode_invalid)
f195d2
+	    {
f195d2
+	      putback_buf[num_putback++] = utf8[3];
f195d2
+	      putback_buf[num_putback++] = utf8[2];
f195d2
+	      putback_buf[num_putback++] = utf8[1];
f195d2
+	      break;
f195d2
+	    }
f195d2
+	  else
f195d2
+	    /* A valid 4-byte UTF-8 encoding.  */
f195d2
+	    (void) display_utf8_char (utf8);
f195d2
+	}
f195d2
+      while (1);
f195d2
+
f195d2
+      if (output_separator)
f195d2
+	fputs (output_separator, stdout);
f195d2
+      else
f195d2
+	putchar ('\n');
f195d2
+    }
f195d2
+
f195d2
+  if (c != EOF)
f195d2
+    /* FIXME: Using tail recursion here is lazy, but it works.  */
f195d2
+    print_unicode_stream_body (filename, address + num_read, stream, putback_buf, num_putback, print_buf);
f195d2
+}
f195d2
+
f195d2
+/* Display strings read in from STREAM.  Treat any UTF-8 encoded characters
f195d2
+   encountered according to the setting of the unicode_display variable.
f195d2
+   The stream is positioned at ADDRESS and is attached to FILENAME.  */
f195d2
+
f195d2
+static void
f195d2
+print_unicode_stream (const char * filename,
f195d2
+		      file_ptr     address,
f195d2
+		      FILE *       stream)
f195d2
+{
f195d2
+  /* Paranoia checks...  */
f195d2
+  if (filename == NULL
f195d2
+      || stream == NULL
f195d2
+      || unicode_display == unicode_default
f195d2
+      || encoding != 'S'
f195d2
+      || encoding_bytes != 1)
f195d2
+    {
f195d2
+      fprintf (stderr, "ICE: bad arguments to print_unicode_stream\n");
f195d2
+      return;
f195d2
+    }
f195d2
+
f195d2
+  /* Allocate space for string_min 4-byte utf-8 characters.  */
f195d2
+  unsigned char * print_buf = xmalloc ((4 * string_min) + 1);
f195d2
+  /* We should never have to put back more than 4 bytes.  */
f195d2
+  unsigned char putback_buf[5];
f195d2
+  unsigned int num_putback = 0;
f195d2
+
f195d2
+  print_unicode_stream_body (filename, address, stream, putback_buf, num_putback, print_buf);
f195d2
+  free (print_buf);
f195d2
+}
f195d2
 
f195d2
 /* Find the strings in file FILENAME, read from STREAM.
f195d2
    Assume that STREAM is positioned so that the next byte read
f195d2
    is at address ADDRESS in the file.
f195d2
-   Stop reading at address STOP_POINT in the file, if nonzero.
f195d2
 
f195d2
    If STREAM is NULL, do not read from it.
f195d2
    The caller can supply a buffer of characters
f195d2
@@ -516,20 +1184,29 @@ get_char (FILE *stream, file_ptr *addres
f195d2
 
f195d2
 static void
f195d2
 print_strings (const char *filename, FILE *stream, file_ptr address,
f195d2
-	       int stop_point, int magiccount, char *magic)
f195d2
+	       int stop_point, int magiccount, char *magic)
f195d2
 {
f195d2
+  if (unicode_display != unicode_default)
f195d2
+    {
f195d2
+      if (magic != NULL)
f195d2
+	print_unicode_buffer (filename, address,
f195d2
+			      (const unsigned char *) magic, magiccount);
f195d2
+
f195d2
+      if (stream != NULL)
f195d2
+	print_unicode_stream (filename, address, stream);
f195d2
+      return;
f195d2
+    }
f195d2
+
f195d2
   char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
f195d2
 
f195d2
   while (1)
f195d2
     {
f195d2
       file_ptr start;
f195d2
-      int i;
f195d2
+      unsigned int i;
f195d2
       long c;
f195d2
 
f195d2
       /* See if the next `string_min' chars are all graphic chars.  */
f195d2
     tryline:
f195d2
-      if (stop_point && address >= stop_point)
f195d2
-	break;
f195d2
       start = address;
f195d2
       for (i = 0; i < string_min; i++)
f195d2
 	{
f195d2
@@ -659,6 +1336,8 @@ usage (FILE *stream, int status)
f195d2
   -T --target=<BFDNAME>     Specify the binary file format\n\
f195d2
   -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
f195d2
                             s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
f195d2
+  --unicode={default|show|invalid|hex|escape|highlight}\n\
f195d2
+  -U {d|s|i|x|e|h}          Specify how to treat UTF-8 encoded unicode characters\n\
f195d2
   -s --output-separator=<string> String used to separate strings in output.\n\
f195d2
   @<file>                   Read options from <file>\n\
f195d2
   -h --help                 Display this information\n\
f195d2
--- binutils.orig/binutils/readelf.c	2021-10-20 12:39:41.715008273 +0100
f195d2
+++ binutils-2.30/binutils/readelf.c	2021-10-20 14:05:42.242326079 +0100
f195d2
@@ -287,6 +287,18 @@ typedef enum print_mode
f195d2
 }
f195d2
 print_mode;
f195d2
 
f195d2
+typedef enum unicode_display_type
f195d2
+{
f195d2
+  unicode_locale,
f195d2
+  unicode_escape,
f195d2
+  unicode_hex,
f195d2
+  unicode_highlight,
f195d2
+  unicode_invalid
f195d2
+} unicode_display_type;
f195d2
+
f195d2
+static unicode_display_type unicode_display = unicode_locale;
f195d2
+
f195d2
+  
f195d2
 /* Versioned symbol info.  */
f195d2
 enum versioned_symbol_info
f195d2
 {
f195d2
@@ -538,11 +550,18 @@ print_symbol (signed int width, const ch
f195d2
       if (c == 0)
f195d2
 	break;
f195d2
 
f195d2
-      /* Do not print control characters directly as they can affect terminal
f195d2
-	 settings.  Such characters usually appear in the names generated
f195d2
-	 by the assembler for local labels.  */
f195d2
-      if (ISCNTRL (c))
f195d2
+      if (ISPRINT (c))
f195d2
+	{
f195d2
+	  putchar (c);
f195d2
+	  width_remaining --;
f195d2
+	  num_printed ++;
f195d2
+	}
f195d2
+      else if (ISCNTRL (c))
f195d2
 	{
f195d2
+	  /* Do not print control characters directly as they can affect terminal
f195d2
+	     settings.  Such characters usually appear in the names generated
f195d2
+	     by the assembler for local labels.  */
f195d2
+
f195d2
 	  if (width_remaining < 2)
f195d2
 	    break;
f195d2
 
f195d2
@@ -550,11 +569,135 @@ print_symbol (signed int width, const ch
f195d2
 	  width_remaining -= 2;
f195d2
 	  num_printed += 2;
f195d2
 	}
f195d2
-      else if (ISPRINT (c))
f195d2
+      else if (c == 0x7f)
f195d2
 	{
f195d2
-	  putchar (c);
f195d2
-	  width_remaining --;
f195d2
-	  num_printed ++;
f195d2
+	  if (width_remaining < 5)
f195d2
+	    break;
f195d2
+	  printf ("");
f195d2
+	  width_remaining -= 5;
f195d2
+	  num_printed += 5;
f195d2
+	}
f195d2
+      else if (unicode_display != unicode_locale)
f195d2
+	{
f195d2
+	  /* Display unicode characters as something else.  */
f195d2
+	  unsigned char bytes[4];
f195d2
+	  bfd_boolean   is_utf8;
f195d2
+	  unsigned int  nbytes;
f195d2
+
f195d2
+	  bytes[0] = c;
f195d2
+
f195d2
+	  if (bytes[0] < 0xc0)
f195d2
+	    {
f195d2
+	      nbytes = 1;
f195d2
+	      is_utf8 = FALSE;
f195d2
+	    }
f195d2
+	  else
f195d2
+	    {
f195d2
+	      bytes[1] = *symbol++;
f195d2
+
f195d2
+	      if ((bytes[1] & 0xc0) != 0x80)
f195d2
+		{
f195d2
+		  is_utf8 = FALSE;
f195d2
+		  /* Do not consume this character.  It may only
f195d2
+		     be the first byte in the sequence that was
f195d2
+		     corrupt.  */
f195d2
+		  --symbol;
f195d2
+		  nbytes = 1;
f195d2
+		}
f195d2
+	      else if ((bytes[0] & 0x20) == 0)
f195d2
+		{
f195d2
+		  is_utf8 = TRUE;
f195d2
+		  nbytes = 2;
f195d2
+		}
f195d2
+	      else
f195d2
+		{
f195d2
+		  bytes[2] = *symbol++;
f195d2
+
f195d2
+		  if ((bytes[2] & 0xc0) != 0x80)
f195d2
+		    {
f195d2
+		      is_utf8 = FALSE;
f195d2
+		      symbol -= 2;
f195d2
+		      nbytes = 1;
f195d2
+		    }
f195d2
+		  else if ((bytes[0] & 0x10) == 0)
f195d2
+		    {
f195d2
+		      is_utf8 = TRUE;
f195d2
+		      nbytes = 3;
f195d2
+		    }
f195d2
+		  else
f195d2
+		    {
f195d2
+		      bytes[3] = *symbol++;
f195d2
+
f195d2
+		      nbytes = 4;
f195d2
+
f195d2
+		      if ((bytes[3] & 0xc0) != 0x80)
f195d2
+			{
f195d2
+			  is_utf8 = FALSE;
f195d2
+			  symbol -= 3;
f195d2
+			  nbytes = 1;
f195d2
+			}
f195d2
+		      else
f195d2
+			is_utf8 = TRUE;
f195d2
+		    }
f195d2
+		}
f195d2
+	    }
f195d2
+
f195d2
+	  if (unicode_display == unicode_invalid)
f195d2
+	    is_utf8 = FALSE;
f195d2
+
f195d2
+	  if (unicode_display == unicode_hex || ! is_utf8)
f195d2
+	    {
f195d2
+	      unsigned int i;
f195d2
+
f195d2
+	      if (width_remaining < (nbytes * 2) + 2)
f195d2
+		break;
f195d2
+	  
f195d2
+	      putchar (is_utf8 ? '<' : '{');
f195d2
+	      for (i = 0; i < nbytes; i++)
f195d2
+		printf ("%02x", bytes[i]);
f195d2
+	      putchar (is_utf8 ? '>' : '}');
f195d2
+	    }
f195d2
+	  else
f195d2
+	    {
f195d2
+	      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+		printf ("\x1B[31;47m"); /* Red.  */
f195d2
+	      
f195d2
+	      switch (nbytes)
f195d2
+		{
f195d2
+		case 2:
f195d2
+		  if (width_remaining < 6)
f195d2
+		    break;
f195d2
+		  printf ("\\u%02x%02x",
f195d2
+			  (bytes[0] & 0x1c) >> 2, 
f195d2
+			  ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
f195d2
+		  break;
f195d2
+		case 3:
f195d2
+		  if (width_remaining < 6)
f195d2
+		    break;
f195d2
+		  printf ("\\u%02x%02x",
f195d2
+			  ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
f195d2
+			  ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
f195d2
+		  break;
f195d2
+		case 4:
f195d2
+		  if (width_remaining < 8)
f195d2
+		    break;
f195d2
+		  printf ("\\u%02x%02x%02x",
f195d2
+			  ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
f195d2
+			  ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
f195d2
+			  ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
f195d2
+		  
f195d2
+		  break;
f195d2
+		default:
f195d2
+		  /* URG.  */
f195d2
+		  break;
f195d2
+		}
f195d2
+
f195d2
+	      if (unicode_display == unicode_highlight && isatty (1))
f195d2
+		printf ("\033[0m"); /* Default colour.  */
f195d2
+	    }
f195d2
+	  
f195d2
+	  if (bytes[nbytes - 1] == 0)
f195d2
+	    break;
f195d2
 	}
f195d2
       else
f195d2
 	{
f195d2
@@ -4375,6 +4518,7 @@ static struct option options[] =
f195d2
   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
f195d2
   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
f195d2
 
f195d2
+  {"unicode",          required_argument, 0, 'U'},
f195d2
   {"version",	       no_argument, 0, 'v'},
f195d2
   {"wide",	       no_argument, 0, 'W'},
f195d2
   {"help",	       no_argument, 0, 'H'},
f195d2
@@ -4432,6 +4576,11 @@ usage (FILE * stream)
f195d2
   fprintf (stream, _("\
f195d2
   -I --histogram         Display histogram of bucket list lengths\n\
f195d2
   -W --wide              Allow output width to exceed 80 characters\n\
f195d2
+  -U --unicode=[locale|escape|hex|highlight|invalid]\n\
f195d2
+                         Display unicode characters as determined by the current locale\n\
f195d2
+                          (default), escape sequences, \"<hex sequences>\", highlighted\n\
f195d2
+                          escape sequences, or treat them as invalid and display as\n\
f195d2
+                          \"{hex sequences}\"\n\
f195d2
   @<file>                Read options from <file>\n\
f195d2
   -H --help              Display this information\n\
f195d2
   -v --version           Display the version number of readelf\n"));
f195d2
@@ -4525,7 +4674,7 @@ parse_args (Filedata * filedata, int arg
f195d2
     usage (stderr);
f195d2
 
f195d2
   while ((c = getopt_long
f195d2
-	  (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
f195d2
+	  (argc, argv, "ADHINR:SU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
f195d2
     {
f195d2
       switch (c)
f195d2
 	{
f195d2
@@ -4659,6 +4808,25 @@ parse_args (Filedata * filedata, int arg
f195d2
 	  request_dump (filedata, DISASS_DUMP);
f195d2
 	  break;
f195d2
 #endif
f195d2
+	case 'U':
f195d2
+	  if (optarg == NULL)
f195d2
+	    error (_("Missing arg to -U/--unicode")); /* Can this happen ?  */
f195d2
+	  else if (streq (optarg, "default") || streq (optarg, "d"))
f195d2
+	    unicode_display = unicode_locale;
f195d2
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
f195d2
+	    unicode_display = unicode_locale;
f195d2
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
f195d2
+	    unicode_display = unicode_escape;
f195d2
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
f195d2
+	    unicode_display = unicode_invalid;
f195d2
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
f195d2
+	    unicode_display = unicode_hex;
f195d2
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
f195d2
+	    unicode_display = unicode_highlight;
f195d2
+	  else
f195d2
+	    error (_("unknown argument to -U/--unicode: %s"), optarg);
f195d2
+	  break;
f195d2
+
f195d2
 	case 'v':
f195d2
 	  print_version (program_name);
f195d2
 	  break;