85859e
diff -rup binutils.orig/binutils/NEWS binutils-2.27/binutils/NEWS
85859e
--- binutils.orig/binutils/NEWS	2021-10-22 10:12:11.129793164 +0100
85859e
+++ binutils-2.27/binutils/NEWS	2021-10-22 10:18:19.535441671 +0100
85859e
@@ -1,5 +1,14 @@
85859e
 -*- text -*-
85859e
 
85859e
+* Tools which display names or strings (readelf, strings, nm, objdump)
85859e
+  have a new command line option which controls how unicode characters are
85859e
+  handled.  By default they are treated as normal for the tool.  Using
85859e
+  --unicode=locale will display them according to the current locale.
85859e
+  Using --unicode=hex will display them as hex byte values, whilst
85859e
+  --unicode=escape will display them as escape sequences.  In addition
85859e
+  using --unicode=highlight will display them as unicode escape sequences
85859e
+  highlighted in red (if supported by the output device).
85859e
+
85859e
 Changes in 2.27:
85859e
 
85859e
 * Add a configure option, --enable-64-bit-archive, to force use of a
85859e
diff -rup binutils.orig/binutils/doc/binutils.texi binutils-2.27/binutils/doc/binutils.texi
85859e
--- binutils.orig/binutils/doc/binutils.texi	2021-10-22 10:12:11.131793151 +0100
85859e
+++ binutils-2.27/binutils/doc/binutils.texi	2021-10-22 10:18:19.535441671 +0100
85859e
@@ -741,6 +741,7 @@ nm [@option{-A}|@option{-o}|@option{--pr
85859e
    [@option{-r}|@option{--reverse-sort}] [@option{-S}|@option{--print-size}]
85859e
    [@option{-s}|@option{--print-armap}] [@option{-t} @var{radix}|@option{--radix=}@var{radix}]
85859e
    [@option{-u}|@option{--undefined-only}] [@option{-V}|@option{--version}]
85859e
+   [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
85859e
    [@option{-X 32_64}] [@option{--defined-only}] [@option{--no-demangle}]
85859e
    [@option{--plugin} @var{name}] [@option{--size-sort}] [@option{--special-syms}]
85859e
    [@option{--synthetic}] [@option{--target=}@var{bfdname}]
85859e
@@ -990,6 +991,21 @@ Use @var{radix} as the radix for printin
85859e
 @cindex undefined symbols
85859e
 Display only undefined symbols (those external to each object file).
85859e
 
85859e
+@item -U @var{[d|i|l|e|x|h]}
85859e
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
85859e
+Controls the display of UTF-8 encoded mulibyte characters in strings.
85859e
+The default (@option{--unicode=default}) is to give them no special
85859e
+treatment.  The @option{--unicode=locale} option displays the sequence
85859e
+in the current locale, which may or may not support them.  The options
85859e
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
85859e
+hex byte sequences enclosed by either angle brackets or curly braces.
85859e
+
85859e
+The @option{--unicode=escape} option displays them as escape sequences
85859e
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
85859e
+them as escape sequences highlighted in red (if supported by the
85859e
+output device).  The colouring is intended to draw attention to the
85859e
+presence of unicode sequences where they might not be expected.
85859e
+
85859e
 @item -V
85859e
 @itemx --version
85859e
 Show the version number of @command{nm} and exit.
85859e
@@ -1973,6 +1989,7 @@ objdump [@option{-a}|@option{--archive-h
85859e
         [@option{--prefix=}@var{prefix}]
85859e
         [@option{--prefix-strip=}@var{level}]
85859e
         [@option{--insn-width=}@var{width}]
85859e
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
85859e
         [@option{-V}|@option{--version}]
85859e
         [@option{-H}|@option{--help}]
85859e
         @var{objfile}@dots{}
85859e
@@ -2560,6 +2577,21 @@ When displaying symbols include those wh
85859e
 special in some way and which would not normally be of interest to the
85859e
 user.
85859e
 
85859e
+@item -U @var{[d|i|l|e|x|h]}
85859e
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
85859e
+Controls the display of UTF-8 encoded mulibyte characters in strings.
85859e
+The default (@option{--unicode=default}) is to give them no special
85859e
+treatment.  The @option{--unicode=locale} option displays the sequence
85859e
+in the current locale, which may or may not support them.  The options
85859e
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
85859e
+hex byte sequences enclosed by either angle brackets or curly braces.
85859e
+
85859e
+The @option{--unicode=escape} option displays them as escape sequences
85859e
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
85859e
+them as escape sequences highlighted in red (if supported by the
85859e
+output device).  The colouring is intended to draw attention to the
85859e
+presence of unicode sequences where they might not be expected.
85859e
+
85859e
 @item -V
85859e
 @itemx --version
85859e
 Print the version number of @command{objdump} and exit.
85859e
@@ -2815,6 +2847,7 @@ strings [@option{-afovV}] [@option{-}@va
85859e
         [@option{-n} @var{min-len}] [@option{--bytes=}@var{min-len}]
85859e
         [@option{-t} @var{radix}] [@option{--radix=}@var{radix}]
85859e
         [@option{-e} @var{encoding}] [@option{--encoding=}@var{encoding}]
85859e
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
85859e
         [@option{-}] [@option{--all}] [@option{--print-file-name}]
85859e
         [@option{-T} @var{bfdname}] [@option{--target=}@var{bfdname}]
85859e
         [@option{-w}] [@option{--include-all-whitespace}]
85859e
@@ -2906,6 +2939,28 @@ single-8-bit-byte characters, @samp{b} =
85859e
 littleendian.  Useful for finding wide character strings. (@samp{l}
85859e
 and @samp{b} apply to, for example, Unicode UTF-16/UCS-2 encodings).
85859e
 
85859e
+@item -U @var{[d|i|l|e|x|h]}
85859e
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
85859e
+Controls the display of UTF-8 encoded mulibyte characters in strings.
85859e
+The default (@option{--unicode=default}) is to give them no special
85859e
+treatment, and instead rely upon the setting of the
85859e
+@option{--encoding} option.  The other values for this option
85859e
+automatically enable @option{--encoding=S}.
85859e
+
85859e
+The @option{--unicode=invalid} option treats them as non-graphic
85859e
+characters and hence not part of a valid string.  All the remaining
85859e
+options treat them as valid string characters.
85859e
+
85859e
+The @option{--unicode=locale} option displays them in the current
85859e
+locale, which may or may not support UTF-8 encoding.  The
85859e
+@option{--unicode=hex} option displays them as hex byte sequences
85859e
+enclosed between @var{<>} characters.  The @option{--unicode=escape}
85859e
+option displays them as escape sequences (@var{\uxxxx}) and the
85859e
+@option{--unicode=highlight} option displays them as escape sequences
85859e
+highlighted in red (if supported by the output device).  The colouring
85859e
+is intended to draw attention to the presence of unicode sequences
85859e
+where they might not be expected.
85859e
+
85859e
 @item -T @var{bfdname}
85859e
 @itemx --target=@var{bfdname}
85859e
 @cindex object code format
85859e
@@ -4417,6 +4472,7 @@ readelf [@option{-a}|@option{--all}]
85859e
         [@option{-v}|@option{--version}]
85859e
         [@option{-W}|@option{--wide}]
85859e
         [@option{-H}|@option{--help}]
85859e
+        [@option{-U} @var{method}|@option{--unicode=}@var{method}]
85859e
         @var{elffile}@dots{}
85859e
 @c man end
85859e
 @end smallexample
85859e
@@ -4493,6 +4549,28 @@ Displays the entries in symbol table sec
85859e
 Displays the entries in dynamic symbol table section of the file, if it
85859e
 has one.
85859e
 
85859e
+@item -U @var{[d|i|l|e|x|h]}
85859e
+@itemx --unicode=[default|invalid|locale|escape|hex|highlight]
85859e
+Controls the display of non-ASCII characters in identifier names.
85859e
+The default (@option{--unicode=locale} or @option{--unicode=default}) is
85859e
+to treat them as multibyte characters and display them in the current
85859e
+locale.  All other versions of this option treat the bytes as UTF-8
85859e
+encoded values and attempt to interpret them.  If they cannot be
85859e
+interpreted or if the @option{--unicode=invalid} option is used then
85859e
+they are displayed as a sequence of hex bytes, encloses in curly
85859e
+parethesis characters.
85859e
+
85859e
+Using the @option{--unicode=escape} option will display the characters
85859e
+as as unicode escape sequences (@var{\uxxxx}).  Using the
85859e
+@option{--unicode=hex} will display the characters as hex byte
85859e
+sequences enclosed between angle brackets.
85859e
+
85859e
+Using the @option{--unicode=highlight} will display the characters as 
85859e
+unicode escape sequences but it will also highlighted them in red,
85859e
+assuming that colouring is supported by the output device.  The
85859e
+colouring is intended to draw attention to the presence of unicode
85859e
+sequences when they might not be expected.
85859e
+
85859e
 @item -e
85859e
 @itemx --headers
85859e
 Display all the headers in the file.  Equivalent to @option{-h -l -S}.
85859e
diff -rup binutils.orig/binutils/nm.c binutils-2.27/binutils/nm.c
85859e
--- binutils.orig/binutils/nm.c	2021-10-22 10:12:11.129793164 +0100
85859e
+++ binutils-2.27/binutils/nm.c	2021-10-22 10:18:19.536441665 +0100
85859e
@@ -37,6 +37,11 @@
85859e
 #include "libcoff.h"
85859e
 #include "bucomm.h"
85859e
 #include "plugin.h"
85859e
+#include "safe-ctype.h"
85859e
+
85859e
+#ifndef streq
85859e
+#define streq(a,b) (strcmp ((a),(b)) == 0)
85859e
+#endif
85859e
 
85859e
 /* When sorting by size, we use this structure to hold the size and a
85859e
    pointer to the minisymbol.  */
85859e
@@ -193,6 +198,18 @@ static const char *plugin_target = NULL;
85859e
 static bfd *lineno_cache_bfd;
85859e
 static bfd *lineno_cache_rel_bfd;
85859e
 
85859e
+typedef enum unicode_display_type
85859e
+{
85859e
+  unicode_default = 0,
85859e
+  unicode_locale,
85859e
+  unicode_escape,
85859e
+  unicode_hex,
85859e
+  unicode_highlight,
85859e
+  unicode_invalid
85859e
+} unicode_display_type;
85859e
+
85859e
+static unicode_display_type unicode_display = unicode_default;
85859e
+
85859e
 #define OPTION_TARGET 200
85859e
 #define OPTION_PLUGIN (OPTION_TARGET + 1)
85859e
 #define OPTION_SIZE_SORT (OPTION_PLUGIN + 1)
85859e
@@ -224,6 +241,7 @@ static struct option long_options[] =
85859e
   {"target", required_argument, 0, OPTION_TARGET},
85859e
   {"defined-only", no_argument, &defined_only, 1},
85859e
   {"undefined-only", no_argument, &undefined_only, 1},
85859e
+  {"unicode", required_argument, NULL, 'U'},
85859e
   {"version", no_argument, &show_version, 1},
85859e
   {0, no_argument, 0, 0}
85859e
 };
85859e
@@ -270,6 +288,8 @@ usage (FILE *stream, int status)
85859e
   -t, --radix=RADIX      Use RADIX for printing symbol values\n\
85859e
       --target=BFDNAME   Specify the target object format as BFDNAME\n\
85859e
   -u, --undefined-only   Display only undefined symbols\n\
85859e
+  -U {d|s|i|x|e|h}       Specify how to treat UTF-8 encoded unicode characters\n\
85859e
+      --unicode={default|show|invalid|hex|escape|highlight}\n\
85859e
   -X 32_64               (ignored)\n\
85859e
   @FILE                  Read options from FILE\n\
85859e
   -h, --help             Display this information\n\
85859e
@@ -389,6 +409,189 @@ get_coff_symbol_type (const struct inter
85859e
   return buff;
85859e
 }
85859e
 
85859e
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
85859e
+   The conversion format is controlled by the unicode_display variable.
85859e
+   Returns the number of characters added to OUT.
85859e
+   Returns the number of bytes consumed from IN in CONSUMED.
85859e
+   Always consumes at least one byte and displays at least one character.  */
85859e
+   
85859e
+static unsigned int
85859e
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
85859e
+{
85859e
+  char *        orig_out = out;
85859e
+  unsigned int  nchars = 0;
85859e
+
85859e
+  if (unicode_display == unicode_default)
85859e
+    goto invalid;
85859e
+
85859e
+  if (in[0] < 0xc0)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[1] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[0] & 0x20) == 0)
85859e
+    {
85859e
+      nchars = 2;
85859e
+      goto valid;
85859e
+    }
85859e
+
85859e
+  if ((in[2] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[0] & 0x10) == 0)
85859e
+    {
85859e
+      nchars = 3;
85859e
+      goto valid;
85859e
+    }
85859e
+
85859e
+  if ((in[3] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  nchars = 4;
85859e
+
85859e
+ valid:
85859e
+  switch (unicode_display)
85859e
+    {
85859e
+    case unicode_locale:
85859e
+      /* Copy the bytes into the output buffer as is.  */
85859e
+      memcpy (out, in, nchars);
85859e
+      out += nchars;
85859e
+      break;
85859e
+
85859e
+    case unicode_invalid:
85859e
+    case unicode_hex:
85859e
+      {
85859e
+      unsigned int j;
85859e
+
85859e
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
85859e
+      for (j = 0; j < nchars; j++)
85859e
+	out += sprintf (out, "%02x", in [j]);
85859e
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
85859e
+      }
85859e
+      break;
85859e
+      
85859e
+    case unicode_highlight:
85859e
+      if (isatty (1))
85859e
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
85859e
+      /* Fall through.  */
85859e
+    case unicode_escape:
85859e
+      switch (nchars)
85859e
+	{
85859e
+	case 2:
85859e
+	  out += sprintf (out, "\\u%02x%02x",
85859e
+		  ((in[0] & 0x1c) >> 2), 
85859e
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
85859e
+	  break;
85859e
+
85859e
+	case 3:
85859e
+	  out += sprintf (out, "\\u%02x%02x",
85859e
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
85859e
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
85859e
+	  break;
85859e
+
85859e
+	case 4:
85859e
+	  out += sprintf (out, "\\u%02x%02x%02x",
85859e
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
85859e
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
85859e
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
85859e
+	  break;
85859e
+	default:
85859e
+	  /* URG.  */
85859e
+	  break;
85859e
+	}
85859e
+
85859e
+      if (unicode_display == unicode_highlight && isatty (1))
85859e
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
85859e
+      break;
85859e
+
85859e
+    default:
85859e
+      /* URG */
85859e
+      break;
85859e
+    }
85859e
+
85859e
+  * consumed = nchars;
85859e
+  return out - orig_out;
85859e
+
85859e
+ invalid:
85859e
+  /* Not a valid UTF-8 sequence.  */
85859e
+  *out = *in;
85859e
+  * consumed = 1;
85859e
+  return 1;
85859e
+}
85859e
+
85859e
+/* Convert any UTF-8 encoded characters in NAME into the form specified by
85859e
+   unicode_display.  Also converts control characters.  Returns a static
85859e
+   buffer if conversion was necessary.
85859e
+   Code stolen from objdump.c:sanitize_string().  */
85859e
+
85859e
+static const char *
85859e
+convert_utf8 (const char * in)
85859e
+{
85859e
+  static char *  buffer = NULL;
85859e
+  static size_t  buffer_len = 0;
85859e
+  const char *   original = in;
85859e
+  char *         out;
85859e
+
85859e
+  /* Paranoia.  */
85859e
+  if (in == NULL)
85859e
+    return "";
85859e
+
85859e
+  /* See if any conversion is necessary.
85859e
+     In the majority of cases it will not be needed.  */
85859e
+  do
85859e
+    {
85859e
+      unsigned char c = *in++;
85859e
+
85859e
+      if (c == 0)
85859e
+	return original;
85859e
+
85859e
+      if (ISCNTRL (c))
85859e
+	break;
85859e
+
85859e
+      if (unicode_display != unicode_default && c >= 0xc0)
85859e
+	break;
85859e
+    }
85859e
+  while (1);
85859e
+
85859e
+  /* Copy the input, translating as needed.  */
85859e
+  in = original;
85859e
+  if (buffer_len < (strlen (in) * 9))
85859e
+    {
85859e
+      free ((void *) buffer);
85859e
+      buffer_len = strlen (in) * 9;
85859e
+      buffer = xmalloc (buffer_len + 1);
85859e
+    }
85859e
+
85859e
+  out = buffer;
85859e
+  do
85859e
+    {
85859e
+      unsigned char c = *in++;
85859e
+
85859e
+      if (c == 0)
85859e
+	break;
85859e
+
85859e
+      if (ISCNTRL (c))
85859e
+	{
85859e
+	  *out++ = '^';
85859e
+	  *out++ = c + 0x40;
85859e
+	}
85859e
+      else if (unicode_display != unicode_default && c >= 0xc0)
85859e
+	{
85859e
+	  unsigned int num_consumed;
85859e
+
85859e
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
85859e
+	  in += num_consumed - 1;
85859e
+	}
85859e
+      else
85859e
+	*out++ = c;
85859e
+    }
85859e
+  while (1);
85859e
+
85859e
+  *out = 0;
85859e
+  return buffer;
85859e
+}
85859e
+
85859e
 /* Print symbol name NAME, read from ABFD, with printf format FORM,
85859e
    demangling it if requested.  */
85859e
 
85859e
@@ -407,6 +610,9 @@ print_symname (const char *form, const c
85859e
 	}
85859e
     }
85859e
 
85859e
+  if (unicode_display != unicode_default)
85859e
+    name = convert_utf8 (name);
85859e
+
85859e
   printf (form, name);
85859e
 }
85859e
 
85859e
@@ -1630,7 +1836,7 @@ main (int argc, char **argv)
85859e
   bfd_init ();
85859e
   set_default_bfd_target ();
85859e
 
85859e
-  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
85859e
+  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uU:vVvX:",
85859e
 			   long_options, (int *) 0)) != EOF)
85859e
     {
85859e
       switch (c)
85859e
@@ -1711,6 +1917,24 @@ main (int argc, char **argv)
85859e
 	case 'u':
85859e
 	  undefined_only = 1;
85859e
 	  break;
85859e
+
85859e
+	case 'U':
85859e
+	  if (streq (optarg, "default") || streq (optarg, "d"))
85859e
+	    unicode_display = unicode_default;
85859e
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
85859e
+	    unicode_display = unicode_locale;
85859e
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
85859e
+	    unicode_display = unicode_escape;
85859e
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
85859e
+	    unicode_display = unicode_invalid;
85859e
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
85859e
+	    unicode_display = unicode_hex;
85859e
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
85859e
+	    unicode_display = unicode_highlight;
85859e
+	  else
85859e
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
85859e
+	  break;
85859e
+
85859e
 	case 'V':
85859e
 	  show_version = 1;
85859e
 	  break;
85859e
diff -rup binutils.orig/binutils/objdump.c binutils-2.27/binutils/objdump.c
85859e
--- binutils.orig/binutils/objdump.c	2021-10-22 10:12:11.140793094 +0100
85859e
+++ binutils-2.27/binutils/objdump.c	2021-10-22 10:30:20.572832752 +0100
85859e
@@ -191,6 +191,18 @@ static const struct objdump_private_desc
85859e
     OBJDUMP_PRIVATE_VECTORS
85859e
     NULL
85859e
   };
85859e
+
85859e
+typedef enum unicode_display_type
85859e
+{
85859e
+  unicode_default = 0,
85859e
+  unicode_locale,
85859e
+  unicode_escape,
85859e
+  unicode_hex,
85859e
+  unicode_highlight,
85859e
+  unicode_invalid
85859e
+} unicode_display_type;
85859e
+
85859e
+static unicode_display_type unicode_display = unicode_default;
85859e
 
85859e
 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
85859e
 static void
85859e
@@ -224,6 +236,9 @@ usage (FILE *stream, int status)
85859e
   -r, --reloc              Display the relocation entries in the file\n\
85859e
   -R, --dynamic-reloc      Display the dynamic relocation entries in the file\n\
85859e
   @<file>                  Read options from <file>\n\
85859e
+  -U[d|l|i|x|e|h]          Controls the display of UTF-8 unicode characters\n\
85859e
+  --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
85859e
+      fprintf (stream, _("\
85859e
   -v, --version            Display this program's version number\n\
85859e
   -i, --info               List object formats and architectures supported\n\
85859e
   -H, --help               Display this information\n\
85859e
@@ -340,6 +355,7 @@ static struct option long_options[]=
85859e
   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
85859e
   {"syms", no_argument, NULL, 't'},
85859e
   {"target", required_argument, NULL, 'b'},
85859e
+  {"unicode", required_argument, NULL, 'U'},
85859e
   {"version", no_argument, NULL, 'V'},
85859e
   {"wide", no_argument, NULL, 'w'},
85859e
   {"prefix", required_argument, NULL, OPTION_PREFIX},
85859e
@@ -358,6 +374,191 @@ nonfatal (const char *msg)
85859e
   exit_status = 1;
85859e
 }
85859e
 
85859e
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
85859e
+   The conversion format is controlled by the unicode_display variable.
85859e
+   Returns the number of characters added to OUT.
85859e
+   Returns the number of bytes consumed from IN in CONSUMED.
85859e
+   Always consumes at least one byte and displays at least one character.  */
85859e
+   
85859e
+static unsigned int
85859e
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
85859e
+{
85859e
+  char *        orig_out = out;
85859e
+  unsigned int  nchars = 0;
85859e
+
85859e
+  if (unicode_display == unicode_default)
85859e
+    goto invalid;
85859e
+
85859e
+  if (in[0] < 0xc0)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[1] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[0] & 0x20) == 0)
85859e
+    {
85859e
+      nchars = 2;
85859e
+      goto valid;
85859e
+    }
85859e
+
85859e
+  if ((in[2] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  if ((in[0] & 0x10) == 0)
85859e
+    {
85859e
+      nchars = 3;
85859e
+      goto valid;
85859e
+    }
85859e
+
85859e
+  if ((in[3] & 0xc0) != 0x80)
85859e
+    goto invalid;
85859e
+
85859e
+  nchars = 4;
85859e
+
85859e
+ valid:
85859e
+  switch (unicode_display)
85859e
+    {
85859e
+    case unicode_locale:
85859e
+      /* Copy the bytes into the output buffer as is.  */
85859e
+      memcpy (out, in, nchars);
85859e
+      out += nchars;
85859e
+      break;
85859e
+
85859e
+    case unicode_invalid:
85859e
+    case unicode_hex:
85859e
+      {
85859e
+      unsigned int j;
85859e
+
85859e
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
85859e
+      for (j = 0; j < nchars; j++)
85859e
+	out += sprintf (out, "%02x", in [j]);
85859e
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
85859e
+      }
85859e
+      break;
85859e
+      
85859e
+    case unicode_highlight:
85859e
+      if (isatty (1))
85859e
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
85859e
+      /* Fall through.  */
85859e
+    case unicode_escape:
85859e
+      switch (nchars)
85859e
+	{
85859e
+	case 2:
85859e
+	  out += sprintf (out, "\\u%02x%02x",
85859e
+		  ((in[0] & 0x1c) >> 2), 
85859e
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
85859e
+	  break;
85859e
+
85859e
+	case 3:
85859e
+	  out += sprintf (out, "\\u%02x%02x",
85859e
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
85859e
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
85859e
+	  break;
85859e
+
85859e
+	case 4:
85859e
+	  out += sprintf (out, "\\u%02x%02x%02x",
85859e
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
85859e
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
85859e
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
85859e
+	  break;
85859e
+	default:
85859e
+	  /* URG.  */
85859e
+	  break;
85859e
+	}
85859e
+
85859e
+      if (unicode_display == unicode_highlight && isatty (1))
85859e
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
85859e
+      break;
85859e
+
85859e
+    default:
85859e
+      /* URG */
85859e
+      break;
85859e
+    }
85859e
+
85859e
+  * consumed = nchars;
85859e
+  return out - orig_out;
85859e
+
85859e
+ invalid:
85859e
+  /* Not a valid UTF-8 sequence.  */
85859e
+  *out = *in;
85859e
+  * consumed = 1;
85859e
+  return 1;
85859e
+}
85859e
+
85859e
+/* Returns a version of IN with any control characters
85859e
+   replaced by escape sequences.  Uses a static buffer
85859e
+   if necessary.
85859e
+
85859e
+   If unicode display is enabled, then also handles the
85859e
+   conversion of unicode characters.  */
85859e
+
85859e
+static const char *
85859e
+sanitize_string (const char * in)
85859e
+{
85859e
+  static char *  buffer = NULL;
85859e
+  static size_t  buffer_len = 0;
85859e
+  const char *   original = in;
85859e
+  char *         out;
85859e
+
85859e
+  /* Paranoia.  */
85859e
+  if (in == NULL)
85859e
+    return "";
85859e
+
85859e
+  /* See if any conversion is necessary.  In the majority
85859e
+     of cases it will not be needed.  */
85859e
+  do
85859e
+    {
85859e
+      unsigned char c = *in++;
85859e
+
85859e
+      if (c == 0)
85859e
+	return original;
85859e
+
85859e
+      if (ISCNTRL (c))
85859e
+	break;
85859e
+
85859e
+      if (unicode_display != unicode_default && c >= 0xc0)
85859e
+	break;
85859e
+    }
85859e
+  while (1);
85859e
+
85859e
+  /* Copy the input, translating as needed.  */
85859e
+  in = original;
85859e
+  if (buffer_len < (strlen (in) * 9))
85859e
+    {
85859e
+      free ((void *) buffer);
85859e
+      buffer_len = strlen (in) * 9;
85859e
+      buffer = xmalloc (buffer_len + 1);
85859e
+    }
85859e
+
85859e
+  out = buffer;
85859e
+  do
85859e
+    {
85859e
+      unsigned char c = *in++;
85859e
+
85859e
+      if (c == 0)
85859e
+	break;
85859e
+
85859e
+      if (ISCNTRL (c))
85859e
+	{
85859e
+	  *out++ = '^';
85859e
+	  *out++ = c + 0x40;
85859e
+	}
85859e
+      else if (unicode_display != unicode_default && c >= 0xc0)
85859e
+	{
85859e
+	  unsigned int num_consumed;
85859e
+
85859e
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
85859e
+	  in += num_consumed - 1;
85859e
+	}
85859e
+      else
85859e
+	*out++ = c;
85859e
+    }
85859e
+  while (1);
85859e
+
85859e
+  *out = 0;
85859e
+  return buffer;
85859e
+}
85859e
+
85859e
 /* Returns TRUE if the specified section should be dumped.  */
85859e
 
85859e
 static bfd_boolean
85859e
@@ -842,6 +1043,8 @@ objdump_print_symname (bfd *abfd, struct
85859e
   if (bfd_is_und_section (bfd_get_section (sym)))
85859e
     hidden = TRUE;
85859e
 
85859e
+  name = sanitize_string (name);
85859e
+
85859e
   if (inf != NULL)
85859e
     {
85859e
       (*inf->fprintf_func) (inf->stream, "%s", name);
85859e
@@ -1063,7 +1266,7 @@ objdump_print_addr_with_sym (bfd *abfd,
85859e
       bfd_vma secaddr;
85859e
 
85859e
       (*inf->fprintf_func) (inf->stream, " <%s",
85859e
-			    bfd_get_section_name (abfd, sec));
85859e
+			    sanitize_string (bfd_section_name (abfd, sec)));
85859e
       secaddr = bfd_get_section_vma (abfd, sec);
85859e
       if (vma < secaddr)
85859e
 	{
85859e
@@ -1472,7 +1675,7 @@ show_line (bfd *abfd, asection *section,
85859e
       if (functionname != NULL
85859e
 	  && (prev_functionname == NULL
85859e
 	      || strcmp (functionname, prev_functionname) != 0))
85859e
-	printf ("%s():\n", functionname);
85859e
+	printf ("%s():\n", sanitize_string (functionname));
85859e
       if (linenumber > 0 && (linenumber != prev_line ||
85859e
                              (discriminator != prev_discriminator)))
85859e
         {
85859e
@@ -1968,7 +2171,7 @@ disassemble_bytes (struct disassemble_in
85859e
 		      sym_name = bfd_get_section_name (aux->abfd, sym_sec);
85859e
 		      if (sym_name == NULL || *sym_name == '\0')
85859e
 			sym_name = "*unknown*";
85859e
-		      printf ("%s", sym_name);
85859e
+		      printf ("%s", sanitize_string (sym_name));
85859e
 		    }
85859e
 		}
85859e
 
85859e
@@ -2112,7 +2315,7 @@ disassemble_section (bfd *abfd, asection
85859e
 	 && (*rel_pp)->address < rel_offset + addr_offset)
85859e
     ++rel_pp;
85859e
 
85859e
-  printf (_("\nDisassembly of section %s:\n"), section->name);
85859e
+  printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
85859e
 
85859e
   /* Find the nearest symbol forwards from our current position.  */
85859e
   paux->require_sec = TRUE;
85859e
@@ -2390,7 +2593,7 @@ load_specific_debug_section (enum dwarf_
85859e
     {
85859e
       free_debug_section (debug);
85859e
       printf (_("\nCan't get contents for section '%s'.\n"),
85859e
-	      section->name);
85859e
+	      sanitize_string (section->name));
85859e
       return 0;
85859e
     }
85859e
 
85859e
@@ -2407,7 +2610,7 @@ load_specific_debug_section (enum dwarf_
85859e
         {
85859e
           free_debug_section (debug);
85859e
           printf (_("\nCan't get contents for section '%s'.\n"),
85859e
-	          section->name);
85859e
+	          sanitize_string (section->name));
85859e
           return 0;
85859e
         }
85859e
 
85859e
@@ -2623,7 +2826,7 @@ read_section_stabs (bfd *abfd, const cha
85859e
   stabsect = bfd_get_section_by_name (abfd, sect_name);
85859e
   if (stabsect == NULL)
85859e
     {
85859e
-      printf (_("No %s section present\n\n"), sect_name);
85859e
+      printf (_("No %s section present\n\n"), sanitize_string (sect_name));
85859e
       return FALSE;
85859e
     }
85859e
 
85859e
@@ -2633,7 +2836,7 @@ read_section_stabs (bfd *abfd, const cha
85859e
   if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
85859e
     {
85859e
       non_fatal (_("reading %s section of %s failed: %s"),
85859e
-		 sect_name, bfd_get_filename (abfd),
85859e
+		 sanitize_string (sect_name), bfd_get_filename (abfd),
85859e
 		 bfd_errmsg (bfd_get_error ()));
85859e
       exit_status = 1;
85859e
       free (contents);
85859e
@@ -2676,7 +2879,7 @@ print_section_stabs (bfd *abfd,
85859e
   stabp = stabs;
85859e
   stabs_end = stabp + stab_size;
85859e
 
85859e
-  printf (_("Contents of %s section:\n\n"), stabsect_name);
85859e
+  printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
85859e
   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
85859e
 
85859e
   /* Loop through all symbols and print them.
85859e
@@ -2940,7 +3143,7 @@ dump_section (bfd *abfd, asection *secti
85859e
   if (start_offset >= stop_offset)
85859e
     return;
85859e
 
85859e
-  printf (_("Contents of section %s:"), section->name);
85859e
+  printf (_("Contents of section %s:"), sanitize_string (section->name));
85859e
   if (display_file_offsets)
85859e
     printf (_("  (Starting at file offset: 0x%lx)"),
85859e
 	    (unsigned long) (section->filepos + start_offset));
85859e
@@ -2949,7 +3152,7 @@ dump_section (bfd *abfd, asection *secti
85859e
   if (!bfd_get_full_section_contents (abfd, section, &data))
85859e
     {
85859e
       non_fatal (_("Reading section %s failed because: %s"),
85859e
-		 section->name, bfd_errmsg (bfd_get_error ()));
85859e
+		 sanitize_string (section->name), bfd_errmsg (bfd_get_error ()));
85859e
       return;
85859e
     }
85859e
 
85859e
@@ -3089,6 +3292,24 @@ dump_symbols (bfd *abfd ATTRIBUTE_UNUSED
85859e
 		  free (alloc);
85859e
 		}
85859e
 	    }
85859e
+	  else if (unicode_display != unicode_default
85859e
+		   && name != NULL && *name != '\0')
85859e
+	    {
85859e
+	      const char * sanitized_name;
85859e
+
85859e
+	      /* If we want to sanitize the name, we do it here, and
85859e
+		 temporarily clobber it while calling bfd_print_symbol.
85859e
+		 FIXME: This is a gross hack.  */
85859e
+	      sanitized_name = sanitize_string (name);
85859e
+	      if (sanitized_name != name)
85859e
+		(*current)->name = sanitized_name;
85859e
+	      else
85859e
+		sanitized_name = NULL;
85859e
+	      bfd_print_symbol (cur_bfd, stdout, *current,
85859e
+				bfd_print_symbol_all);
85859e
+	      if (sanitized_name != NULL)
85859e
+		(*current)->name = name;
85859e
+	    }
85859e
 	  else
85859e
 	    bfd_print_symbol (cur_bfd, stdout, *current,
85859e
 			      bfd_print_symbol_all);
85859e
@@ -3154,7 +3375,7 @@ dump_reloc_set (bfd *abfd, asection *sec
85859e
 	      && (last_functionname == NULL
85859e
 		  || strcmp (functionname, last_functionname) != 0))
85859e
 	    {
85859e
-	      printf ("%s():\n", functionname);
85859e
+	      printf ("%s():\n", sanitize_string (functionname));
85859e
 	      if (last_functionname != NULL)
85859e
 		free (last_functionname);
85859e
 	      last_functionname = xstrdup (functionname);
85859e
@@ -3168,9 +3389,9 @@ dump_reloc_set (bfd *abfd, asection *sec
85859e
                   || (discriminator != last_discriminator)))
85859e
 	    {
85859e
               if (discriminator > 0)
85859e
-                printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
85859e
+                printf ("%s:%u\n", filename == NULL ? "???" : sanitize_string (filename), linenumber);
85859e
               else
85859e
-                printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
85859e
+                printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : sanitize_string (filename),
85859e
                         linenumber, discriminator);
85859e
 	      last_line = linenumber;
85859e
 	      last_discriminator = discriminator;
85859e
@@ -3240,7 +3461,7 @@ dump_reloc_set (bfd *abfd, asection *sec
85859e
 	{
85859e
 	  if (section_name == NULL)
85859e
 	    section_name = "*unknown*";
85859e
-	  printf ("[%s]", section_name);
85859e
+	  printf ("[%s]", sanitize_string (section_name));
85859e
 	}
85859e
 
85859e
       if (q->addend)
85859e
@@ -3290,7 +3511,7 @@ dump_relocs_in_section (bfd *abfd,
85859e
   if (relsize < 0)
85859e
     bfd_fatal (bfd_get_filename (abfd));
85859e
 
85859e
-  printf ("RELOCATION RECORDS FOR [%s]:", section->name);
85859e
+  printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
85859e
 
85859e
   if (relsize == 0)
85859e
     {
85859e
@@ -3661,7 +3882,7 @@ main (int argc, char **argv)
85859e
   set_default_bfd_target ();
85859e
 
85859e
   while ((c = getopt_long (argc, argv,
85859e
-			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
85859e
+			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::U:",
85859e
 			   long_options, (int *) 0))
85859e
 	 != EOF)
85859e
     {
85859e
@@ -3888,6 +4109,23 @@ main (int argc, char **argv)
85859e
 	  seenflag = TRUE;
85859e
 	  break;
85859e
 
85859e
+	case 'U':
85859e
+	  if (streq (optarg, "default") || streq (optarg, "d"))
85859e
+	    unicode_display = unicode_default;
85859e
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
85859e
+	    unicode_display = unicode_locale;
85859e
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
85859e
+	    unicode_display = unicode_escape;
85859e
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
85859e
+	    unicode_display = unicode_invalid;
85859e
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
85859e
+	    unicode_display = unicode_hex;
85859e
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
85859e
+	    unicode_display = unicode_highlight;
85859e
+	  else
85859e
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
85859e
+	  break;
85859e
+
85859e
 	case 'H':
85859e
 	  usage (stdout, 0);
85859e
 	  /* No need to set seenflag or to break - usage() does not return.  */
85859e
diff -rup binutils.orig/binutils/readelf.c binutils-2.27/binutils/readelf.c
85859e
--- binutils.orig/binutils/readelf.c	2021-10-22 10:12:11.141793087 +0100
85859e
+++ binutils-2.27/binutils/readelf.c	2021-10-22 10:18:19.537441658 +0100
85859e
@@ -279,6 +279,18 @@ typedef enum print_mode
85859e
 }
85859e
 print_mode;
85859e
 
85859e
+typedef enum unicode_display_type
85859e
+{
85859e
+  unicode_locale,
85859e
+  unicode_escape,
85859e
+  unicode_hex,
85859e
+  unicode_highlight,
85859e
+  unicode_invalid
85859e
+} unicode_display_type;
85859e
+
85859e
+static unicode_display_type unicode_display = unicode_locale;
85859e
+
85859e
+  
85859e
 /* Versioned symbol info.  */
85859e
 enum versioned_symbol_info
85859e
 {
85859e
@@ -505,11 +517,18 @@ print_symbol (int width, const char *sym
85859e
       if (c == 0)
85859e
 	break;
85859e
 
85859e
-      /* Do not print control characters directly as they can affect terminal
85859e
-	 settings.  Such characters usually appear in the names generated
85859e
-	 by the assembler for local labels.  */
85859e
-      if (ISCNTRL (c))
85859e
+      if (ISPRINT (c))
85859e
+	{
85859e
+	  putchar (c);
85859e
+	  width_remaining --;
85859e
+	  num_printed ++;
85859e
+	}
85859e
+      else if (ISCNTRL (c))
85859e
 	{
85859e
+	  /* Do not print control characters directly as they can affect terminal
85859e
+	     settings.  Such characters usually appear in the names generated
85859e
+	     by the assembler for local labels.  */
85859e
+
85859e
 	  if (width_remaining < 2)
85859e
 	    break;
85859e
 
85859e
@@ -517,11 +536,135 @@ print_symbol (int width, const char *sym
85859e
 	  width_remaining -= 2;
85859e
 	  num_printed += 2;
85859e
 	}
85859e
-      else if (ISPRINT (c))
85859e
+      else if (c == 0x7f)
85859e
 	{
85859e
-	  putchar (c);
85859e
-	  width_remaining --;
85859e
-	  num_printed ++;
85859e
+	  if (width_remaining < 5)
85859e
+	    break;
85859e
+	  printf ("");
85859e
+	  width_remaining -= 5;
85859e
+	  num_printed += 5;
85859e
+	}
85859e
+      else if (unicode_display != unicode_locale)
85859e
+	{
85859e
+	  /* Display unicode characters as something else.  */
85859e
+	  unsigned char bytes[4];
85859e
+	  bfd_boolean   is_utf8;
85859e
+	  unsigned int  nbytes;
85859e
+
85859e
+	  bytes[0] = c;
85859e
+
85859e
+	  if (bytes[0] < 0xc0)
85859e
+	    {
85859e
+	      nbytes = 1;
85859e
+	      is_utf8 = FALSE;
85859e
+	    }
85859e
+	  else
85859e
+	    {
85859e
+	      bytes[1] = *symbol++;
85859e
+
85859e
+	      if ((bytes[1] & 0xc0) != 0x80)
85859e
+		{
85859e
+		  is_utf8 = FALSE;
85859e
+		  /* Do not consume this character.  It may only
85859e
+		     be the first byte in the sequence that was
85859e
+		     corrupt.  */
85859e
+		  --symbol;
85859e
+		  nbytes = 1;
85859e
+		}
85859e
+	      else if ((bytes[0] & 0x20) == 0)
85859e
+		{
85859e
+		  is_utf8 = TRUE;
85859e
+		  nbytes = 2;
85859e
+		}
85859e
+	      else
85859e
+		{
85859e
+		  bytes[2] = *symbol++;
85859e
+
85859e
+		  if ((bytes[2] & 0xc0) != 0x80)
85859e
+		    {
85859e
+		      is_utf8 = FALSE;
85859e
+		      symbol -= 2;
85859e
+		      nbytes = 1;
85859e
+		    }
85859e
+		  else if ((bytes[0] & 0x10) == 0)
85859e
+		    {
85859e
+		      is_utf8 = TRUE;
85859e
+		      nbytes = 3;
85859e
+		    }
85859e
+		  else
85859e
+		    {
85859e
+		      bytes[3] = *symbol++;
85859e
+
85859e
+		      nbytes = 4;
85859e
+
85859e
+		      if ((bytes[3] & 0xc0) != 0x80)
85859e
+			{
85859e
+			  is_utf8 = FALSE;
85859e
+			  symbol -= 3;
85859e
+			  nbytes = 1;
85859e
+			}
85859e
+		      else
85859e
+			is_utf8 = TRUE;
85859e
+		    }
85859e
+		}
85859e
+	    }
85859e
+
85859e
+	  if (unicode_display == unicode_invalid)
85859e
+	    is_utf8 = FALSE;
85859e
+
85859e
+	  if (unicode_display == unicode_hex || ! is_utf8)
85859e
+	    {
85859e
+	      unsigned int i;
85859e
+
85859e
+	      if (width_remaining < (nbytes * 2) + 2)
85859e
+		break;
85859e
+	  
85859e
+	      putchar (is_utf8 ? '<' : '{');
85859e
+	      for (i = 0; i < nbytes; i++)
85859e
+		printf ("%02x", bytes[i]);
85859e
+	      putchar (is_utf8 ? '>' : '}');
85859e
+	    }
85859e
+	  else
85859e
+	    {
85859e
+	      if (unicode_display == unicode_highlight && isatty (1))
85859e
+		printf ("\x1B[31;47m"); /* Red.  */
85859e
+	      
85859e
+	      switch (nbytes)
85859e
+		{
85859e
+		case 2:
85859e
+		  if (width_remaining < 6)
85859e
+		    break;
85859e
+		  printf ("\\u%02x%02x",
85859e
+			  (bytes[0] & 0x1c) >> 2, 
85859e
+			  ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
85859e
+		  break;
85859e
+		case 3:
85859e
+		  if (width_remaining < 6)
85859e
+		    break;
85859e
+		  printf ("\\u%02x%02x",
85859e
+			  ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
85859e
+			  ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
85859e
+		  break;
85859e
+		case 4:
85859e
+		  if (width_remaining < 8)
85859e
+		    break;
85859e
+		  printf ("\\u%02x%02x%02x",
85859e
+			  ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
85859e
+			  ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
85859e
+			  ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
85859e
+		  
85859e
+		  break;
85859e
+		default:
85859e
+		  /* URG.  */
85859e
+		  break;
85859e
+		}
85859e
+
85859e
+	      if (unicode_display == unicode_highlight && isatty (1))
85859e
+		printf ("\033[0m"); /* Default colour.  */
85859e
+	    }
85859e
+	  
85859e
+	  if (bytes[nbytes - 1] == 0)
85859e
+	    break;
85859e
 	}
85859e
       else
85859e
 	{
85859e
@@ -4172,6 +4315,7 @@ static struct option options[] =
85859e
   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
85859e
   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
85859e
 
85859e
+  {"unicode",          required_argument, 0, 'U'},
85859e
   {"version",	       no_argument, 0, 'v'},
85859e
   {"wide",	       no_argument, 0, 'W'},
85859e
   {"help",	       no_argument, 0, 'H'},
85859e
@@ -4229,6 +4373,11 @@ usage (FILE * stream)
85859e
   fprintf (stream, _("\
85859e
   -I --histogram         Display histogram of bucket list lengths\n\
85859e
   -W --wide              Allow output width to exceed 80 characters\n\
85859e
+  -U --unicode=[locale|escape|hex|highlight|invalid]\n\
85859e
+                         Display unicode characters as determined by the current locale\n\
85859e
+                          (default), escape sequences, \"<hex sequences>\", highlighted\n\
85859e
+                          escape sequences, or treat them as invalid and display as\n\
85859e
+                          \"{hex sequences}\"\n\
85859e
   @<file>                Read options from <file>\n\
85859e
   -H --help              Display this information\n\
85859e
   -v --version           Display the version number of readelf\n"));
85859e
@@ -4321,7 +4470,7 @@ parse_args (int argc, char ** argv)
85859e
     usage (stderr);
85859e
 
85859e
   while ((c = getopt_long
85859e
-	  (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
85859e
+	  (argc, argv, "ADHINR:SU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
85859e
     {
85859e
       switch (c)
85859e
 	{
85859e
@@ -4455,6 +4604,25 @@ parse_args (int argc, char ** argv)
85859e
 	  request_dump (DISASS_DUMP);
85859e
 	  break;
85859e
 #endif
85859e
+	case 'U':
85859e
+	  if (optarg == NULL)
85859e
+	    error (_("Missing arg to -U/--unicode")); /* Can this happen ?  */
85859e
+	  else if (streq (optarg, "default") || streq (optarg, "d"))
85859e
+	    unicode_display = unicode_locale;
85859e
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
85859e
+	    unicode_display = unicode_locale;
85859e
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
85859e
+	    unicode_display = unicode_escape;
85859e
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
85859e
+	    unicode_display = unicode_invalid;
85859e
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
85859e
+	    unicode_display = unicode_hex;
85859e
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
85859e
+	    unicode_display = unicode_highlight;
85859e
+	  else
85859e
+	    error (_("unknown argument to -U/--unicode: %s"), optarg);
85859e
+	  break;
85859e
+
85859e
 	case 'v':
85859e
 	  print_version (program_name);
85859e
 	  break;
85859e
diff -rup binutils.orig/binutils/strings.c binutils-2.27/binutils/strings.c
85859e
--- binutils.orig/binutils/strings.c	2021-10-22 10:12:11.130793157 +0100
85859e
+++ binutils-2.27/binutils/strings.c	2021-10-22 10:18:19.538441652 +0100
85859e
@@ -55,6 +55,19 @@
85859e
    -T {bfdname}
85859e
 		Specify a non-default object file format.
85859e
 
85859e
+  --unicode={default|locale|invalid|hex|escape|highlight}
85859e
+  -U {d|l|i|x|e|h}
85859e
+                Determine how to handle UTF-8 unicode characters.  The default
85859e
+		is no special treatment.  All other versions of this option
85859e
+		only apply if the encoding is valid and enabling the option
85859e
+		implies --encoding=S.
85859e
+		The 'locale' option displays the characters according to the
85859e
+		current locale.  The 'invalid' option treats them as
85859e
+		non-string characters.  The 'hex' option displays them as hex
85859e
+		byte sequences.  The 'escape' option displays them as escape
85859e
+		sequences and the 'highlight' option displays them as
85859e
+		coloured escape sequences.
85859e
+
85859e
   --output-separator=sep_string
85859e
   -s sep_string	String used to separate parsed strings in output.
85859e
 		Default is newline.
85859e
@@ -76,6 +89,22 @@
85859e
 #include "safe-ctype.h"
85859e
 #include "bucomm.h"
85859e
 
85859e
+#ifndef streq
85859e
+#define streq(a,b) (strcmp ((a),(b)) == 0)
85859e
+#endif
85859e
+
85859e
+typedef enum unicode_display_type
85859e
+{
85859e
+  unicode_default = 0,
85859e
+  unicode_locale,
85859e
+  unicode_escape,
85859e
+  unicode_hex,
85859e
+  unicode_highlight,
85859e
+  unicode_invalid
85859e
+} unicode_display_type;
85859e
+
85859e
+static unicode_display_type unicode_display = unicode_default;
85859e
+
85859e
 #define STRING_ISGRAPHIC(c) \
85859e
       (   (c) >= 0 \
85859e
        && (c) <= 255 \
85859e
@@ -94,7 +123,7 @@ extern int errno;
85859e
 static int address_radix;
85859e
 
85859e
 /* Minimum length of sequence of graphic chars to trigger output.  */
85859e
-static int string_min;
85859e
+static uint string_min;
85859e
 
85859e
 /* Whether or not we include all whitespace as a graphic char.   */
85859e
 static bfd_boolean include_all_whitespace;
85859e
@@ -133,6 +162,7 @@ static struct option long_options[] =
85859e
   {"target", required_argument, NULL, 'T'},
85859e
   {"output-separator", required_argument, NULL, 's'},
85859e
   {"help", no_argument, NULL, 'h'},
85859e
+  {"unicode", required_argument, NULL, 'U'},
85859e
   {"version", no_argument, NULL, 'v'},
85859e
   {NULL, 0, NULL, 0}
85859e
 };
85859e
@@ -188,7 +218,7 @@ main (int argc, char **argv)
85859e
   encoding = 's';
85859e
   output_separator = NULL;
85859e
 
85859e
-  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:Vv0123456789",
85859e
+  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:U:Vv0123456789",
85859e
 			      long_options, (int *) 0)) != EOF)
85859e
     {
85859e
       switch (optc)
85859e
@@ -261,6 +291,23 @@ main (int argc, char **argv)
85859e
 	  output_separator = optarg;
85859e
           break;
85859e
 
85859e
+	case 'U':
85859e
+	  if (streq (optarg, "default") || streq (optarg, "d"))
85859e
+	    unicode_display = unicode_default;
85859e
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
85859e
+	    unicode_display = unicode_locale;
85859e
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
85859e
+	    unicode_display = unicode_escape;
85859e
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
85859e
+	    unicode_display = unicode_invalid;
85859e
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
85859e
+	    unicode_display = unicode_hex;
85859e
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
85859e
+	    unicode_display = unicode_highlight;
85859e
+	  else
85859e
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
85859e
+	  break;
85859e
+
85859e
 	case 'V':
85859e
 	case 'v':
85859e
 	  print_version ("strings");
85859e
@@ -275,6 +322,9 @@ main (int argc, char **argv)
85859e
 	}
85859e
     }
85859e
 
85859e
+  if (unicode_display != unicode_default)
85859e
+    encoding = 'S';
85859e
+
85859e
   if (numeric_opt != 0)
85859e
     {
85859e
       string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
85859e
@@ -539,11 +589,629 @@ get_char (FILE *stream, file_ptr *addres
85859e
 
85859e
   return r;
85859e
 }
85859e
+
85859e
+static void
85859e
+print_filename_and_address (const char * filename, file_ptr address)
85859e
+{
85859e
+  if (print_filenames)
85859e
+    printf ("%s: ", filename);
85859e
+
85859e
+  if (! print_addresses)
85859e
+    return;
85859e
+
85859e
+  switch (address_radix)
85859e
+    {
85859e
+    case 8:
85859e
+      if (sizeof (address) > sizeof (long))
85859e
+	{
85859e
+#ifndef __MSVCRT__
85859e
+	  printf ("%7llo ", (unsigned long long) address);
85859e
+#else
85859e
+	  printf ("%7I64o ", (unsigned long long) address);
85859e
+#endif
85859e
+	}
85859e
+      else
85859e
+	printf ("%7lo ", (unsigned long) address);
85859e
+      break;
85859e
+
85859e
+    case 10:
85859e
+      if (sizeof (address) > sizeof (long))
85859e
+	{
85859e
+#ifndef __MSVCRT__
85859e
+	  printf ("%7llu ", (unsigned long long) address);
85859e
+#else
85859e
+	  printf ("%7I64d ", (unsigned long long) address);
85859e
+#endif
85859e
+	}
85859e
+      else
85859e
+	printf ("%7ld ", (long) address);
85859e
+      break;
85859e
+
85859e
+    case 16:
85859e
+      if (sizeof (address) > sizeof (long))
85859e
+	{
85859e
+#ifndef __MSVCRT__
85859e
+	  printf ("%7llx ", (unsigned long long) address);
85859e
+#else
85859e
+	  printf ("%7I64x ", (unsigned long long) address);
85859e
+#endif
85859e
+	}
85859e
+      else
85859e
+	printf ("%7lx ", (unsigned long) address);
85859e
+      break;
85859e
+    }
85859e
+}
85859e
+
85859e
+/* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
85859e
+   If the encoding is valid then returns the number of bytes it uses.  */
85859e
+
85859e
+static unsigned int
85859e
+is_valid_utf8 (const unsigned char * buffer, unsigned long buflen)
85859e
+{
85859e
+  if (buffer[0] < 0xc0)
85859e
+    return 0;
85859e
+
85859e
+  if (buflen < 2)
85859e
+    return 0;
85859e
+
85859e
+  if ((buffer[1] & 0xc0) != 0x80)
85859e
+    return 0;
85859e
+
85859e
+  if ((buffer[0] & 0x20) == 0)
85859e
+    return 2;
85859e
+
85859e
+  if (buflen < 3)
85859e
+    return 0;
85859e
+
85859e
+  if ((buffer[2] & 0xc0) != 0x80)
85859e
+    return 0;
85859e
+  
85859e
+  if ((buffer[0] & 0x10) == 0)
85859e
+    return 3;
85859e
+
85859e
+  if (buflen < 4)
85859e
+    return 0;
85859e
+
85859e
+  if ((buffer[3] & 0xc0) != 0x80)
85859e
+    return 0;
85859e
+
85859e
+  return 4;
85859e
+}
85859e
+
85859e
+/* Display a UTF-8 encoded character in BUFFER according to the setting
85859e
+   of unicode_display.  The character is known to be valid.
85859e
+   Returns the number of bytes consumed.  */
85859e
+
85859e
+static unsigned int
85859e
+display_utf8_char (const unsigned char * buffer)
85859e
+{
85859e
+  unsigned int j;
85859e
+  unsigned int utf8_len;
85859e
+
85859e
+  switch (buffer[0] & 0x30)
85859e
+    {
85859e
+    case 0x00:
85859e
+    case 0x10:
85859e
+      utf8_len = 2;
85859e
+      break;
85859e
+    case 0x20:
85859e
+      utf8_len = 3;
85859e
+      break;
85859e
+    default:
85859e
+      utf8_len = 4;
85859e
+    }
85859e
+      
85859e
+  switch (unicode_display)
85859e
+    {
85859e
+    default:
85859e
+      fprintf (stderr, "ICE: unexpected unicode display type\n");
85859e
+      break;
85859e
+
85859e
+    case unicode_escape:
85859e
+    case unicode_highlight:
85859e
+      if (unicode_display == unicode_highlight && isatty (1))
85859e
+	printf ("\x1B[31;47m"); /* Red.  */
85859e
+
85859e
+      switch (utf8_len)
85859e
+	{
85859e
+	case 2:
85859e
+	  printf ("\\u%02x%02x",
85859e
+		  ((buffer[0] & 0x1c) >> 2), 
85859e
+		  ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
85859e
+	  break;
85859e
+
85859e
+	case 3:
85859e
+	  printf ("\\u%02x%02x",
85859e
+		  ((buffer[0] & 0x0f) << 4) | ((buffer[1] & 0x3c) >> 2),
85859e
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3f)));
85859e
+	  break;
85859e
+
85859e
+	case 4:
85859e
+	  printf ("\\u%02x%02x%02x",
85859e
+		  ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
85859e
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
85859e
+		  ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
85859e
+	  break;
85859e
+	default:
85859e
+	  /* URG.  */
85859e
+	  break;
85859e
+	}
85859e
+
85859e
+      if (unicode_display == unicode_highlight && isatty (1))
85859e
+	printf ("\033[0m"); /* Default colour.  */
85859e
+      break;
85859e
+
85859e
+    case unicode_hex:
85859e
+      putchar ('<');
85859e
+      for (j = 0; j < utf8_len; j++)
85859e
+	printf ("%02x", buffer [j]);
85859e
+      putchar ('>');
85859e
+      break;
85859e
+
85859e
+    case unicode_locale:
85859e
+      printf ("%.1s", buffer);
85859e
+      break;
85859e
+    }
85859e
+
85859e
+  return utf8_len;
85859e
+}
85859e
+
85859e
+/* Display strings in BUFFER.  Treat any UTF-8 encoded characters encountered
85859e
+   according to the setting of the unicode_display variable.  The buffer
85859e
+   contains BUFLEN bytes.
85859e
+
85859e
+   Display the characters as if they started at ADDRESS and are contained in
85859e
+   FILENAME.  */
85859e
+
85859e
+static void
85859e
+print_unicode_buffer (const char *            filename,
85859e
+		      file_ptr                address,
85859e
+		      const unsigned char *   buffer,
85859e
+		      unsigned long           buflen)
85859e
+{
85859e
+  /* Paranoia checks...  */
85859e
+  if (filename == NULL
85859e
+      || buffer == NULL
85859e
+      || unicode_display == unicode_default
85859e
+      || encoding != 'S'
85859e
+      || encoding_bytes != 1)
85859e
+    {
85859e
+      fprintf (stderr, "ICE: bad arguments to print_unicode_buffer\n");
85859e
+      return;
85859e
+    }
85859e
+
85859e
+  if (buflen == 0)
85859e
+    return;
85859e
+
85859e
+  /* We must only display strings that are at least string_min *characters*
85859e
+     long.  So we scan the buffer in two stages.  First we locate the start
85859e
+     of a potential string.  Then we walk along it until we have found
85859e
+     string_min characters.  Then we go back to the start point and start
85859e
+     displaying characters according to the unicode_display setting.  */
85859e
+
85859e
+  unsigned long start_point = 0;
85859e
+  unsigned long i = 0;
85859e
+  unsigned int char_len = 1;
85859e
+  unsigned int num_found = 0;
85859e
+
85859e
+  for (i = 0; i < buflen; i += char_len)
85859e
+    {
85859e
+      int c = buffer[i];
85859e
+
85859e
+      char_len = 1;
85859e
+
85859e
+      /* Find the first potential character of a string.  */
85859e
+      if (! STRING_ISGRAPHIC (c))
85859e
+	{
85859e
+	  num_found = 0;
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      if (c > 126)
85859e
+	{
85859e
+	  if (c < 0xc0)
85859e
+	    {
85859e
+	      num_found = 0;
85859e
+	      continue;
85859e
+	    }
85859e
+
85859e
+	  if ((char_len = is_valid_utf8 (buffer + i, buflen - i)) == 0)
85859e
+	    {
85859e
+	      char_len = 1;
85859e
+	      num_found = 0;
85859e
+	      continue;
85859e
+	    }
85859e
+
85859e
+	  if (unicode_display == unicode_invalid)
85859e
+	    {
85859e
+	      /* We have found a valid UTF-8 character, but we treat it as non-graphic.  */
85859e
+	      num_found = 0;
85859e
+	      continue;
85859e
+	    }
85859e
+	}
85859e
+
85859e
+      if (num_found == 0)
85859e
+	/* We have found a potential starting point for a string.  */
85859e
+	start_point = i;
85859e
+
85859e
+      ++ num_found;
85859e
+
85859e
+      if (num_found >= string_min)
85859e
+	break;
85859e
+    }
85859e
+
85859e
+  if (num_found < string_min)
85859e
+    return;
85859e
+
85859e
+  print_filename_and_address (filename, address + start_point);
85859e
+  
85859e
+  /* We have found string_min characters.  Display them and any
85859e
+     more that follow.  */
85859e
+  for (i = start_point; i < buflen; i += char_len)
85859e
+    {
85859e
+      int c = buffer[i];
85859e
+
85859e
+      char_len = 1;
85859e
+
85859e
+      if (! STRING_ISGRAPHIC (c))
85859e
+	break;
85859e
+      else if (c < 127)
85859e
+	putchar (c);
85859e
+      else if (! is_valid_utf8 (buffer + i, buflen - i))
85859e
+	break;
85859e
+      else if (unicode_display == unicode_invalid)
85859e
+	break;
85859e
+      else
85859e
+	char_len = display_utf8_char (buffer + i);
85859e
+    }
85859e
+
85859e
+  if (output_separator)
85859e
+    fputs (output_separator, stdout);
85859e
+  else
85859e
+    putchar ('\n');
85859e
+
85859e
+  /* FIXME: Using tail recursion here is lazy programming...  */
85859e
+  print_unicode_buffer (filename, address + i, buffer + i, buflen - i);
85859e
+}
85859e
+
85859e
+static int
85859e
+get_unicode_byte (FILE *           stream,
85859e
+		  unsigned char *  putback,
85859e
+		  unsigned int *   num_putback,
85859e
+		  unsigned int *   num_read)
85859e
+{
85859e
+  if (* num_putback > 0)
85859e
+    {
85859e
+      * num_putback = * num_putback - 1;
85859e
+      return putback [* num_putback];
85859e
+    }
85859e
+
85859e
+  * num_read = * num_read + 1;
85859e
+
85859e
+#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
85859e
+  return getc_unlocked (stream);
85859e
+#else
85859e
+  return getc (stream);
85859e
+#endif
85859e
+}
85859e
+
85859e
+/* Helper function for print_unicode_stream.  */
85859e
+
85859e
+static void
85859e
+print_unicode_stream_body (const char *     filename,
85859e
+			   file_ptr         address,
85859e
+			   FILE *           stream,
85859e
+			   unsigned char *  putback_buf,
85859e
+			   unsigned int     num_putback,
85859e
+			   unsigned char *  print_buf)
85859e
+{
85859e
+  /* It would be nice if we could just read the stream into a buffer
85859e
+     and then process if with print_unicode_buffer.  But the input
85859e
+     might be huge or it might time-locked (eg stdin).  So instead
85859e
+     we go one byte at a time...  */
85859e
+
85859e
+  file_ptr start_point = 0;
85859e
+  unsigned int num_read = 0;
85859e
+  unsigned int num_chars = 0;
85859e
+  unsigned int num_print = 0;
85859e
+  int c;
85859e
+
85859e
+  /* Find a series of string_min characters.  Put them into print_buf.  */
85859e
+  do
85859e
+    {
85859e
+      if (num_chars >= string_min)
85859e
+	break;
85859e
+
85859e
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+      if (c == EOF)
85859e
+	break;
85859e
+
85859e
+      if (! STRING_ISGRAPHIC (c))
85859e
+	{
85859e
+	  num_chars = num_print = 0;
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      if (num_chars == 0)
85859e
+	start_point = num_read - 1;
85859e
+
85859e
+      if (c < 127)
85859e
+	{
85859e
+	  print_buf[num_print] = c;
85859e
+	  num_chars ++;
85859e
+	  num_print ++;
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      if (c < 0xc0)
85859e
+	{
85859e
+	  num_chars = num_print = 0;
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
85859e
+      char utf8[4];
85859e
+
85859e
+      utf8[0] = c;
85859e
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+      if (c == EOF)
85859e
+	break;
85859e
+      utf8[1] = c;
85859e
+
85859e
+      if ((utf8[1] & 0xc0) != 0x80)
85859e
+	{
85859e
+	  /* Invalid UTF-8.  */
85859e
+	  putback_buf[num_putback++] = utf8[1];
85859e
+	  num_chars = num_print = 0;
85859e
+	  continue;
85859e
+	}
85859e
+      else if ((utf8[0] & 0x20) == 0)
85859e
+	{
85859e
+	  /* A valid 2-byte UTF-8 encoding.  */
85859e
+	  if (unicode_display == unicode_invalid)
85859e
+	    {
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      num_chars = num_print = 0;
85859e
+	    }
85859e
+	  else
85859e
+	    {
85859e
+	      print_buf[num_print ++] = utf8[0];
85859e
+	      print_buf[num_print ++] = utf8[1];
85859e
+	      num_chars ++;
85859e
+	    }
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+      if (c == EOF)
85859e
+	break;
85859e
+      utf8[2] = c;
85859e
+
85859e
+      if ((utf8[2] & 0xc0) != 0x80)
85859e
+	{
85859e
+	  /* Invalid UTF-8.  */
85859e
+	  putback_buf[num_putback++] = utf8[2];
85859e
+	  putback_buf[num_putback++] = utf8[1];
85859e
+	  num_chars = num_print = 0;
85859e
+	  continue;
85859e
+	}
85859e
+      else if ((utf8[0] & 0x10) == 0)
85859e
+	{
85859e
+	  /* A valid 3-byte UTF-8 encoding.  */
85859e
+	  if (unicode_display == unicode_invalid)
85859e
+	    {
85859e
+	      putback_buf[num_putback++] = utf8[2];
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      num_chars = num_print = 0;
85859e
+	    }
85859e
+	  else
85859e
+	    {
85859e
+	      print_buf[num_print ++] = utf8[0];
85859e
+	      print_buf[num_print ++] = utf8[1];
85859e
+	      print_buf[num_print ++] = utf8[2];
85859e
+	      num_chars ++;
85859e
+	    }
85859e
+	  continue;
85859e
+	}
85859e
+
85859e
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+      if (c == EOF)
85859e
+	break;
85859e
+      utf8[3] = c;
85859e
+
85859e
+      if ((utf8[3] & 0xc0) != 0x80)
85859e
+	{
85859e
+	  /* Invalid UTF-8.  */
85859e
+	  putback_buf[num_putback++] = utf8[3];
85859e
+	  putback_buf[num_putback++] = utf8[2];
85859e
+	  putback_buf[num_putback++] = utf8[1];
85859e
+	  num_chars = num_print = 0;
85859e
+	}
85859e
+      /* We have a valid 4-byte UTF-8 encoding.  */
85859e
+      else if (unicode_display == unicode_invalid)
85859e
+	{
85859e
+	  putback_buf[num_putback++] = utf8[3];
85859e
+	  putback_buf[num_putback++] = utf8[1];
85859e
+	  putback_buf[num_putback++] = utf8[2];
85859e
+	  num_chars = num_print = 0;
85859e
+	}
85859e
+      else
85859e
+	{
85859e
+	  print_buf[num_print ++] = utf8[0];
85859e
+	  print_buf[num_print ++] = utf8[1];
85859e
+	  print_buf[num_print ++] = utf8[2];
85859e
+	  print_buf[num_print ++] = utf8[3];
85859e
+	  num_chars ++;
85859e
+	}
85859e
+    }
85859e
+  while (1);
85859e
+
85859e
+  if (num_chars >= string_min)
85859e
+    {
85859e
+      /* We know that we have string_min valid characters in print_buf,
85859e
+	 and there may be more to come in the stream.  Start displaying
85859e
+	 them.  */
85859e
+
85859e
+      print_filename_and_address (filename, address + start_point);
85859e
+
85859e
+      unsigned int i;
85859e
+      for (i = 0; i < num_print;)
85859e
+	{
85859e
+	  if (print_buf[i] < 127)
85859e
+	    putchar (print_buf[i++]);
85859e
+	  else
85859e
+	    i += display_utf8_char (print_buf + i);
85859e
+	}
85859e
+
85859e
+      /* OK so now we have to start read unchecked bytes.  */
85859e
+
85859e
+        /* Find a series of string_min characters.  Put them into print_buf.  */
85859e
+      do
85859e
+	{
85859e
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+	  if (c == EOF)
85859e
+	    break;
85859e
+
85859e
+	  if (! STRING_ISGRAPHIC (c))
85859e
+	    break;
85859e
+
85859e
+	  if (c < 127)
85859e
+	    {
85859e
+	      putchar (c);
85859e
+	      continue;
85859e
+	    }
85859e
+
85859e
+	  if (c < 0xc0)
85859e
+	    break;
85859e
+
85859e
+	  /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
85859e
+	  unsigned char utf8[4];
85859e
+
85859e
+	  utf8[0] = c;
85859e
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+	  if (c == EOF)
85859e
+	    break;
85859e
+	  utf8[1] = c;
85859e
+
85859e
+	  if ((utf8[1] & 0xc0) != 0x80)
85859e
+	    {
85859e
+	      /* Invalid UTF-8.  */
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      break;
85859e
+	    }
85859e
+	  else if ((utf8[0] & 0x20) == 0)
85859e
+	    {
85859e
+	      /* Valid 2-byte UTF-8.  */
85859e
+	      if (unicode_display == unicode_invalid)
85859e
+		{
85859e
+		  putback_buf[num_putback++] = utf8[1];
85859e
+		  break;
85859e
+		}
85859e
+	      else
85859e
+		{
85859e
+		  (void) display_utf8_char (utf8);
85859e
+		  continue;
85859e
+		}
85859e
+	    }
85859e
+
85859e
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+	  if (c == EOF)
85859e
+	    break;
85859e
+	  utf8[2] = c;
85859e
+
85859e
+	  if ((utf8[2] & 0xc0) != 0x80)
85859e
+	    {
85859e
+	      /* Invalid UTF-8.  */
85859e
+	      putback_buf[num_putback++] = utf8[2];
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      break;
85859e
+	    }
85859e
+	  else if ((utf8[0] & 0x10) == 0)
85859e
+	    {
85859e
+	      /* Valid 3-byte UTF-8.  */
85859e
+	      if (unicode_display == unicode_invalid)
85859e
+		{
85859e
+		  putback_buf[num_putback++] = utf8[2];
85859e
+		  putback_buf[num_putback++] = utf8[1];
85859e
+		  break;
85859e
+		}
85859e
+	      else
85859e
+		{
85859e
+		  (void) display_utf8_char (utf8);
85859e
+		  continue;
85859e
+		}
85859e
+	    }
85859e
+
85859e
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
85859e
+	  if (c == EOF)
85859e
+	    break;
85859e
+	  utf8[3] = c;
85859e
+
85859e
+	  if ((utf8[3] & 0xc0) != 0x80)
85859e
+	    {
85859e
+	      /* Invalid UTF-8.  */
85859e
+	      putback_buf[num_putback++] = utf8[3];
85859e
+	      putback_buf[num_putback++] = utf8[2];
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      break;
85859e
+	    }
85859e
+	  else if (unicode_display == unicode_invalid)
85859e
+	    {
85859e
+	      putback_buf[num_putback++] = utf8[3];
85859e
+	      putback_buf[num_putback++] = utf8[2];
85859e
+	      putback_buf[num_putback++] = utf8[1];
85859e
+	      break;
85859e
+	    }
85859e
+	  else
85859e
+	    /* A valid 4-byte UTF-8 encoding.  */
85859e
+	    (void) display_utf8_char (utf8);
85859e
+	}
85859e
+      while (1);
85859e
+
85859e
+      if (output_separator)
85859e
+	fputs (output_separator, stdout);
85859e
+      else
85859e
+	putchar ('\n');
85859e
+    }
85859e
+
85859e
+  if (c != EOF)
85859e
+    /* FIXME: Using tail recursion here is lazy, but it works.  */
85859e
+    print_unicode_stream_body (filename, address + num_read, stream, putback_buf, num_putback, print_buf);
85859e
+}
85859e
+
85859e
+/* Display strings read in from STREAM.  Treat any UTF-8 encoded characters
85859e
+   encountered according to the setting of the unicode_display variable.
85859e
+   The stream is positioned at ADDRESS and is attached to FILENAME.  */
85859e
+
85859e
+static void
85859e
+print_unicode_stream (const char * filename,
85859e
+		      file_ptr     address,
85859e
+		      FILE *       stream)
85859e
+{
85859e
+  /* Paranoia checks...  */
85859e
+  if (filename == NULL
85859e
+      || stream == NULL
85859e
+      || unicode_display == unicode_default
85859e
+      || encoding != 'S'
85859e
+      || encoding_bytes != 1)
85859e
+    {
85859e
+      fprintf (stderr, "ICE: bad arguments to print_unicode_stream\n");
85859e
+      return;
85859e
+    }
85859e
+
85859e
+  /* Allocate space for string_min 4-byte utf-8 characters.  */
85859e
+  unsigned char * print_buf = xmalloc ((4 * string_min) + 1);
85859e
+  /* We should never have to put back more than 4 bytes.  */
85859e
+  unsigned char putback_buf[5];
85859e
+  unsigned int num_putback = 0;
85859e
+
85859e
+  print_unicode_stream_body (filename, address, stream, putback_buf, num_putback, print_buf);
85859e
+  free (print_buf);
85859e
+}
85859e
 
85859e
 /* Find the strings in file FILENAME, read from STREAM.
85859e
    Assume that STREAM is positioned so that the next byte read
85859e
    is at address ADDRESS in the file.
85859e
-   Stop reading at address STOP_POINT in the file, if nonzero.
85859e
 
85859e
    If STREAM is NULL, do not read from it.
85859e
    The caller can supply a buffer of characters
85859e
@@ -556,18 +1224,27 @@ static void
85859e
 print_strings (const char *filename, FILE *stream, file_ptr address,
85859e
 	       int stop_point, int magiccount, char *magic)
85859e
 {
85859e
+  if (unicode_display != unicode_default)
85859e
+    {
85859e
+      if (magic != NULL)
85859e
+	print_unicode_buffer (filename, address,
85859e
+			      (const unsigned char *) magic, magiccount);
85859e
+
85859e
+      if (stream != NULL)
85859e
+	print_unicode_stream (filename, address, stream);
85859e
+      return;
85859e
+    }
85859e
+
85859e
   char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
85859e
 
85859e
   while (1)
85859e
     {
85859e
       file_ptr start;
85859e
-      int i;
85859e
+      unsigned int i;
85859e
       long c;
85859e
 
85859e
       /* See if the next `string_min' chars are all graphic chars.  */
85859e
     tryline:
85859e
-      if (stop_point && address >= stop_point)
85859e
-	break;
85859e
       start = address;
85859e
       for (i = 0; i < string_min; i++)
85859e
 	{
85859e
@@ -697,6 +1374,8 @@ usage (FILE *stream, int status)
85859e
   -T --target=<BFDNAME>     Specify the binary file format\n\
85859e
   -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
85859e
                             s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
85859e
+  --unicode={default|show|invalid|hex|escape|highlight}\n\
85859e
+  -U {d|s|i|x|e|h}          Specify how to treat UTF-8 encoded unicode characters\n\
85859e
   -s --output-separator=<string> String used to separate strings in output.\n\
85859e
   @<file>                   Read options from <file>\n\
85859e
   -h --help                 Display this information\n\