Blame SOURCES/binutils.unicode.patch

b7afb1
diff -rup binutils.orig/binutils/NEWS binutils-2.35/binutils/NEWS
b7afb1
--- binutils.orig/binutils/NEWS	2021-10-21 15:15:57.838969981 +0100
b7afb1
+++ binutils-2.35/binutils/NEWS	2021-10-21 15:39:14.475071663 +0100
b7afb1
@@ -122,6 +122,15 @@ Changes in 2.32:
b7afb1
 
b7afb1
 Changes in 2.31:
b7afb1
 
b7afb1
+* Tools which display names or strings (readelf, strings, nm, objdump)
b7afb1
+  have a new command line option which controls how unicode characters are
b7afb1
+  handled.  By default they are treated as normal for the tool.  Using
b7afb1
+  --unicode=locale will display them according to the current locale.
b7afb1
+  Using --unicode=hex will display them as hex byte values, whilst
b7afb1
+  --unicode=escape will display them as escape sequences.  In addition
b7afb1
+  using --unicode=highlight will display them as unicode escape sequences
b7afb1
+  highlighted in red (if supported by the output device).
b7afb1
+
b7afb1
 * Add support for disassembling netronome Flow Processor (NFP) firmware files.
b7afb1
 
b7afb1
 * The AArch64 port now supports showing disassembly notes which are emitted
b7afb1
diff -rup binutils.orig/binutils/doc/binutils.texi binutils-2.35/binutils/doc/binutils.texi
b7afb1
--- binutils.orig/binutils/doc/binutils.texi	2021-10-21 15:15:57.839969974 +0100
b7afb1
+++ binutils-2.35/binutils/doc/binutils.texi	2021-10-21 15:39:14.476071657 +0100
b7afb1
@@ -787,6 +787,7 @@ nm [@option{-A}|@option{-o}|@option{--pr
b7afb1
    [@option{-D}|@option{--dynamic}] [@option{-f}@var{format}|@option{--format=}@var{format}]
b7afb1
    [@option{-g}|@option{--extern-only}] [@option{-h}|@option{--help}]
b7afb1
    [@option{-l}|@option{--line-numbers}] [@option{--inlines}]
b7afb1
+   [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
b7afb1
    [@option{-n}|@option{-v}|@option{--numeric-sort}]
b7afb1
    [@option{-P}|@option{--portability}] [@option{-p}|@option{--no-sort}]
b7afb1
    [@option{-r}|@option{--reverse-sort}] [@option{-S}|@option{--print-size}]
b7afb1
@@ -1075,6 +1076,21 @@ Use @var{radix} as the radix for printin
b7afb1
 @cindex undefined symbols
b7afb1
 Display only undefined symbols (those external to each object file).
b7afb1
 
b7afb1
+@item -U @var{[d|i|l|e|x|h]}
b7afb1
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
b7afb1
+Controls the display of UTF-8 encoded mulibyte characters in strings.
b7afb1
+The default (@option{--unicode=default}) is to give them no special
b7afb1
+treatment.  The @option{--unicode=locale} option displays the sequence
b7afb1
+in the current locale, which may or may not support them.  The options
b7afb1
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
b7afb1
+hex byte sequences enclosed by either angle brackets or curly braces.
b7afb1
+
b7afb1
+The @option{--unicode=escape} option displays them as escape sequences
b7afb1
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
b7afb1
+them as escape sequences highlighted in red (if supported by the
b7afb1
+output device).  The colouring is intended to draw attention to the
b7afb1
+presence of unicode sequences where they might not be expected.
b7afb1
+
b7afb1
 @item -V
b7afb1
 @itemx --version
b7afb1
 Show the version number of @command{nm} and exit.
b7afb1
@@ -2176,6 +2192,7 @@ objdump [@option{-a}|@option{--archive-h
b7afb1
         [@option{--prefix-strip=}@var{level}]
b7afb1
         [@option{--insn-width=}@var{width}]
b7afb1
         [@option{--visualize-jumps[=color|=extended-color|=off]}
b7afb1
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
b7afb1
         [@option{-V}|@option{--version}]
b7afb1
         [@option{-H}|@option{--help}]
b7afb1
         @var{objfile}@dots{}
b7afb1
@@ -2843,6 +2860,21 @@ When displaying symbols include those wh
b7afb1
 special in some way and which would not normally be of interest to the
b7afb1
 user.
b7afb1
 
b7afb1
+@item -U @var{[d|i|l|e|x|h]}
b7afb1
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
b7afb1
+Controls the display of UTF-8 encoded mulibyte characters in strings.
b7afb1
+The default (@option{--unicode=default}) is to give them no special
b7afb1
+treatment.  The @option{--unicode=locale} option displays the sequence
b7afb1
+in the current locale, which may or may not support them.  The options
b7afb1
+@option{--unicode=hex} and @option{--unicode=invalid} display them as
b7afb1
+hex byte sequences enclosed by either angle brackets or curly braces.
b7afb1
+
b7afb1
+The @option{--unicode=escape} option displays them as escape sequences
b7afb1
+(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
b7afb1
+them as escape sequences highlighted in red (if supported by the
b7afb1
+output device).  The colouring is intended to draw attention to the
b7afb1
+presence of unicode sequences where they might not be expected.
b7afb1
+
b7afb1
 @item -V
b7afb1
 @itemx --version
b7afb1
 Print the version number of @command{objdump} and exit.
b7afb1
@@ -3119,6 +3151,7 @@ strings [@option{-afovV}] [@option{-}@va
b7afb1
         [@option{-n} @var{min-len}] [@option{--bytes=}@var{min-len}]
b7afb1
         [@option{-t} @var{radix}] [@option{--radix=}@var{radix}]
b7afb1
         [@option{-e} @var{encoding}] [@option{--encoding=}@var{encoding}]
b7afb1
+        [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
b7afb1
         [@option{-}] [@option{--all}] [@option{--print-file-name}]
b7afb1
         [@option{-T} @var{bfdname}] [@option{--target=}@var{bfdname}]
b7afb1
         [@option{-w}] [@option{--include-all-whitespace}]
b7afb1
@@ -3210,6 +3243,28 @@ single-8-bit-byte characters, @samp{b} =
b7afb1
 littleendian.  Useful for finding wide character strings. (@samp{l}
b7afb1
 and @samp{b} apply to, for example, Unicode UTF-16/UCS-2 encodings).
b7afb1
 
b7afb1
+@item -U @var{[d|i|l|e|x|h]}
b7afb1
+@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
b7afb1
+Controls the display of UTF-8 encoded mulibyte characters in strings.
b7afb1
+The default (@option{--unicode=default}) is to give them no special
b7afb1
+treatment, and instead rely upon the setting of the
b7afb1
+@option{--encoding} option.  The other values for this option
b7afb1
+automatically enable @option{--encoding=S}.
b7afb1
+
b7afb1
+The @option{--unicode=invalid} option treats them as non-graphic
b7afb1
+characters and hence not part of a valid string.  All the remaining
b7afb1
+options treat them as valid string characters.
b7afb1
+
b7afb1
+The @option{--unicode=locale} option displays them in the current
b7afb1
+locale, which may or may not support UTF-8 encoding.  The
b7afb1
+@option{--unicode=hex} option displays them as hex byte sequences
b7afb1
+enclosed between @var{<>} characters.  The @option{--unicode=escape}
b7afb1
+option displays them as escape sequences (@var{\uxxxx}) and the
b7afb1
+@option{--unicode=highlight} option displays them as escape sequences
b7afb1
+highlighted in red (if supported by the output device).  The colouring
b7afb1
+is intended to draw attention to the presence of unicode sequences
b7afb1
+where they might not be expected.
b7afb1
+
b7afb1
 @item -T @var{bfdname}
b7afb1
 @itemx --target=@var{bfdname}
b7afb1
 @cindex object code format
b7afb1
@@ -4728,6 +4783,7 @@ readelf [@option{-a}|@option{--all}]
b7afb1
         [@option{-W}|@option{--wide}]
b7afb1
         [@option{-T}|@option{--silent-truncation}]
b7afb1
         [@option{-H}|@option{--help}]
b7afb1
+        [@option{-U} @var{method}|@option{--unicode=}@var{method}]
b7afb1
         @var{elffile}@dots{}
b7afb1
 @c man end
b7afb1
 @end smallexample
b7afb1
@@ -4817,6 +4873,28 @@ Displays the entries in dynamic symbol t
b7afb1
 has one.  The output format is the same as the format used by the
b7afb1
 @option{--syms} option.
b7afb1
 
b7afb1
+@item -U @var{[d|i|l|e|x|h]}
b7afb1
+@itemx --unicode=[default|invalid|locale|escape|hex|highlight]
b7afb1
+Controls the display of non-ASCII characters in identifier names.
b7afb1
+The default (@option{--unicode=locale} or @option{--unicode=default}) is
b7afb1
+to treat them as multibyte characters and display them in the current
b7afb1
+locale.  All other versions of this option treat the bytes as UTF-8
b7afb1
+encoded values and attempt to interpret them.  If they cannot be
b7afb1
+interpreted or if the @option{--unicode=invalid} option is used then
b7afb1
+they are displayed as a sequence of hex bytes, encloses in curly
b7afb1
+parethesis characters.
b7afb1
+
b7afb1
+Using the @option{--unicode=escape} option will display the characters
b7afb1
+as as unicode escape sequences (@var{\uxxxx}).  Using the
b7afb1
+@option{--unicode=hex} will display the characters as hex byte
b7afb1
+sequences enclosed between angle brackets.
b7afb1
+
b7afb1
+Using the @option{--unicode=highlight} will display the characters as 
b7afb1
+unicode escape sequences but it will also highlighted them in red,
b7afb1
+assuming that colouring is supported by the output device.  The
b7afb1
+colouring is intended to draw attention to the presence of unicode
b7afb1
+sequences when they might not be expected.
b7afb1
+
b7afb1
 @item -e
b7afb1
 @itemx --headers
b7afb1
 Display all the headers in the file.  Equivalent to @option{-h -l -S}.
b7afb1
diff -rup binutils.orig/binutils/nm.c binutils-2.35/binutils/nm.c
b7afb1
--- binutils.orig/binutils/nm.c	2021-10-21 15:15:57.835970002 +0100
b7afb1
+++ binutils-2.35/binutils/nm.c	2021-10-21 15:39:14.476071657 +0100
b7afb1
@@ -38,6 +38,11 @@
b7afb1
 #include "bucomm.h"
b7afb1
 #include "plugin-api.h"
b7afb1
 #include "plugin.h"
b7afb1
+#include "safe-ctype.h"
b7afb1
+
b7afb1
+#ifndef streq
b7afb1
+#define streq(a,b) (strcmp ((a),(b)) == 0)
b7afb1
+#endif
b7afb1
 
b7afb1
 /* When sorting by size, we use this structure to hold the size and a
b7afb1
    pointer to the minisymbol.  */
b7afb1
@@ -186,6 +191,18 @@ static const char *plugin_target = NULL;
b7afb1
 static bfd *lineno_cache_bfd;
b7afb1
 static bfd *lineno_cache_rel_bfd;
b7afb1
 
b7afb1
+typedef enum unicode_display_type
b7afb1
+{
b7afb1
+  unicode_default = 0,
b7afb1
+  unicode_locale,
b7afb1
+  unicode_escape,
b7afb1
+  unicode_hex,
b7afb1
+  unicode_highlight,
b7afb1
+  unicode_invalid
b7afb1
+} unicode_display_type;
b7afb1
+
b7afb1
+static unicode_display_type unicode_display = unicode_default;
b7afb1
+
b7afb1
 enum long_option_values
b7afb1
 {
b7afb1
   OPTION_TARGET = 200,
b7afb1
@@ -225,6 +242,7 @@ static struct option long_options[] =
b7afb1
   {"target", required_argument, 0, OPTION_TARGET},
b7afb1
   {"defined-only", no_argument, &defined_only, 1},
b7afb1
   {"undefined-only", no_argument, &undefined_only, 1},
b7afb1
+  {"unicode", required_argument, NULL, 'U'},
b7afb1
   {"version", no_argument, &show_version, 1},
b7afb1
   {"with-symbol-versions", no_argument, &with_symbol_versions, 1},
b7afb1
   {0, no_argument, 0, 0}
b7afb1
@@ -274,6 +292,8 @@ usage (FILE *stream, int status)
b7afb1
   -t, --radix=RADIX      Use RADIX for printing symbol values\n\
b7afb1
       --target=BFDNAME   Specify the target object format as BFDNAME\n\
b7afb1
   -u, --undefined-only   Display only undefined symbols\n\
b7afb1
+  -U {d|s|i|x|e|h}       Specify how to treat UTF-8 encoded unicode characters\n\
b7afb1
+      --unicode={default|show|invalid|hex|escape|highlight}\n\
b7afb1
       --with-symbol-versions  Display version strings after symbol names\n\
b7afb1
   -X 32_64               (ignored)\n\
b7afb1
   @FILE                  Read options from FILE\n\
b7afb1
@@ -389,6 +409,189 @@ get_coff_symbol_type (const struct inter
b7afb1
   return bufp;
b7afb1
 }
b7afb1
 
b7afb1
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
b7afb1
+   The conversion format is controlled by the unicode_display variable.
b7afb1
+   Returns the number of characters added to OUT.
b7afb1
+   Returns the number of bytes consumed from IN in CONSUMED.
b7afb1
+   Always consumes at least one byte and displays at least one character.  */
b7afb1
+   
b7afb1
+static unsigned int
b7afb1
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
b7afb1
+{
b7afb1
+  char *        orig_out = out;
b7afb1
+  unsigned int  nchars = 0;
b7afb1
+
b7afb1
+  if (unicode_display == unicode_default)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if (in[0] < 0xc0)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[1] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[0] & 0x20) == 0)
b7afb1
+    {
b7afb1
+      nchars = 2;
b7afb1
+      goto valid;
b7afb1
+    }
b7afb1
+
b7afb1
+  if ((in[2] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[0] & 0x10) == 0)
b7afb1
+    {
b7afb1
+      nchars = 3;
b7afb1
+      goto valid;
b7afb1
+    }
b7afb1
+
b7afb1
+  if ((in[3] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  nchars = 4;
b7afb1
+
b7afb1
+ valid:
b7afb1
+  switch (unicode_display)
b7afb1
+    {
b7afb1
+    case unicode_locale:
b7afb1
+      /* Copy the bytes into the output buffer as is.  */
b7afb1
+      memcpy (out, in, nchars);
b7afb1
+      out += nchars;
b7afb1
+      break;
b7afb1
+
b7afb1
+    case unicode_invalid:
b7afb1
+    case unicode_hex:
b7afb1
+      {
b7afb1
+      unsigned int j;
b7afb1
+
b7afb1
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
b7afb1
+      for (j = 0; j < nchars; j++)
b7afb1
+	out += sprintf (out, "%02x", in [j]);
b7afb1
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
b7afb1
+      }
b7afb1
+      break;
b7afb1
+      
b7afb1
+    case unicode_highlight:
b7afb1
+      if (isatty (1))
b7afb1
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
b7afb1
+      /* Fall through.  */
b7afb1
+    case unicode_escape:
b7afb1
+      switch (nchars)
b7afb1
+	{
b7afb1
+	case 2:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x",
b7afb1
+		  ((in[0] & 0x1c) >> 2), 
b7afb1
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 3:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x",
b7afb1
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
b7afb1
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 4:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x%02x",
b7afb1
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
b7afb1
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
b7afb1
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
b7afb1
+	  break;
b7afb1
+	default:
b7afb1
+	  /* URG.  */
b7afb1
+	  break;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
b7afb1
+      break;
b7afb1
+
b7afb1
+    default:
b7afb1
+      /* URG */
b7afb1
+      break;
b7afb1
+    }
b7afb1
+
b7afb1
+  * consumed = nchars;
b7afb1
+  return out - orig_out;
b7afb1
+
b7afb1
+ invalid:
b7afb1
+  /* Not a valid UTF-8 sequence.  */
b7afb1
+  *out = *in;
b7afb1
+  * consumed = 1;
b7afb1
+  return 1;
b7afb1
+}
b7afb1
+
b7afb1
+/* Convert any UTF-8 encoded characters in NAME into the form specified by
b7afb1
+   unicode_display.  Also converts control characters.  Returns a static
b7afb1
+   buffer if conversion was necessary.
b7afb1
+   Code stolen from objdump.c:sanitize_string().  */
b7afb1
+
b7afb1
+static const char *
b7afb1
+convert_utf8 (const char * in)
b7afb1
+{
b7afb1
+  static char *  buffer = NULL;
b7afb1
+  static size_t  buffer_len = 0;
b7afb1
+  const char *   original = in;
b7afb1
+  char *         out;
b7afb1
+
b7afb1
+  /* Paranoia.  */
b7afb1
+  if (in == NULL)
b7afb1
+    return "";
b7afb1
+
b7afb1
+  /* See if any conversion is necessary.
b7afb1
+     In the majority of cases it will not be needed.  */
b7afb1
+  do
b7afb1
+    {
b7afb1
+      unsigned char c = *in++;
b7afb1
+
b7afb1
+      if (c == 0)
b7afb1
+	return original;
b7afb1
+
b7afb1
+      if (ISCNTRL (c))
b7afb1
+	break;
b7afb1
+
b7afb1
+      if (unicode_display != unicode_default && c >= 0xc0)
b7afb1
+	break;
b7afb1
+    }
b7afb1
+  while (1);
b7afb1
+
b7afb1
+  /* Copy the input, translating as needed.  */
b7afb1
+  in = original;
b7afb1
+  if (buffer_len < (strlen (in) * 9))
b7afb1
+    {
b7afb1
+      free ((void *) buffer);
b7afb1
+      buffer_len = strlen (in) * 9;
b7afb1
+      buffer = xmalloc (buffer_len + 1);
b7afb1
+    }
b7afb1
+
b7afb1
+  out = buffer;
b7afb1
+  do
b7afb1
+    {
b7afb1
+      unsigned char c = *in++;
b7afb1
+
b7afb1
+      if (c == 0)
b7afb1
+	break;
b7afb1
+
b7afb1
+      if (ISCNTRL (c))
b7afb1
+	{
b7afb1
+	  *out++ = '^';
b7afb1
+	  *out++ = c + 0x40;
b7afb1
+	}
b7afb1
+      else if (unicode_display != unicode_default && c >= 0xc0)
b7afb1
+	{
b7afb1
+	  unsigned int num_consumed;
b7afb1
+
b7afb1
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
b7afb1
+	  in += num_consumed - 1;
b7afb1
+	}
b7afb1
+      else
b7afb1
+	*out++ = c;
b7afb1
+    }
b7afb1
+  while (1);
b7afb1
+
b7afb1
+  *out = 0;
b7afb1
+  return buffer;
b7afb1
+}
b7afb1
+
b7afb1
 /* Print symbol name NAME, read from ABFD, with printf format FORM,
b7afb1
    demangling it if requested.  */
b7afb1
 
b7afb1
@@ -410,6 +613,9 @@ print_symname (const char *form, struct
b7afb1
 	}
b7afb1
     }
b7afb1
 
b7afb1
+  if (unicode_display != unicode_default)
b7afb1
+    name = convert_utf8 (name);
b7afb1
+
b7afb1
   printf (form, name);
b7afb1
   if (info != NULL && info->elfinfo)
b7afb1
     {
b7afb1
@@ -1726,7 +1932,7 @@ main (int argc, char **argv)
b7afb1
     fatal (_("fatal error: libbfd ABI mismatch"));
b7afb1
   set_default_bfd_target ();
b7afb1
 
b7afb1
-  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
b7afb1
+  while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uU:vVvX:",
b7afb1
 			   long_options, (int *) 0)) != EOF)
b7afb1
     {
b7afb1
       switch (c)
b7afb1
@@ -1813,6 +2019,24 @@ main (int argc, char **argv)
b7afb1
 	case 'u':
b7afb1
 	  undefined_only = 1;
b7afb1
 	  break;
b7afb1
+
b7afb1
+	case 'U':
b7afb1
+	  if (streq (optarg, "default") || streq (optarg, "d"))
b7afb1
+	    unicode_display = unicode_default;
b7afb1
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
b7afb1
+	    unicode_display = unicode_locale;
b7afb1
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
b7afb1
+	    unicode_display = unicode_escape;
b7afb1
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
b7afb1
+	    unicode_display = unicode_invalid;
b7afb1
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
b7afb1
+	    unicode_display = unicode_hex;
b7afb1
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
b7afb1
+	    unicode_display = unicode_highlight;
b7afb1
+	  else
b7afb1
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
b7afb1
+	  break;
b7afb1
+
b7afb1
 	case 'V':
b7afb1
 	  show_version = 1;
b7afb1
 	  break;
b7afb1
diff -rup binutils.orig/binutils/objdump.c binutils-2.35/binutils/objdump.c
b7afb1
--- binutils.orig/binutils/objdump.c	2021-10-21 15:15:57.836969995 +0100
b7afb1
+++ binutils-2.35/binutils/objdump.c	2021-10-21 15:40:27.689549518 +0100
b7afb1
@@ -205,6 +205,18 @@ static const struct objdump_private_desc
b7afb1
 
b7afb1
 /* The list of detected jumps inside a function.  */
b7afb1
 static struct jump_info *detected_jumps = NULL;
b7afb1
+
b7afb1
+typedef enum unicode_display_type
b7afb1
+{
b7afb1
+  unicode_default = 0,
b7afb1
+  unicode_locale,
b7afb1
+  unicode_escape,
b7afb1
+  unicode_hex,
b7afb1
+  unicode_highlight,
b7afb1
+  unicode_invalid
b7afb1
+} unicode_display_type;
b7afb1
+
b7afb1
+static unicode_display_type unicode_display = unicode_default;
b7afb1
 
b7afb1
 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
b7afb1
 static void
b7afb1
@@ -247,6 +259,9 @@ usage (FILE *stream, int status)
b7afb1
   -r, --reloc              Display the relocation entries in the file\n\
b7afb1
   -R, --dynamic-reloc      Display the dynamic relocation entries in the file\n\
b7afb1
   @<file>                  Read options from <file>\n\
b7afb1
+  -U[d|l|i|x|e|h]          Controls the display of UTF-8 unicode characters\n\
b7afb1
+  --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
b7afb1
+      fprintf (stream, _("\
b7afb1
   -v, --version            Display this program's version number\n\
b7afb1
   -i, --info               List object formats and architectures supported\n\
b7afb1
   -H, --help               Display this information\n\
b7afb1
@@ -395,6 +410,7 @@ static struct option long_options[]=
b7afb1
   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
b7afb1
   {"syms", no_argument, NULL, 't'},
b7afb1
   {"target", required_argument, NULL, 'b'},
b7afb1
+  {"unicode", required_argument, NULL, 'U'},
b7afb1
   {"version", no_argument, NULL, 'V'},
b7afb1
   {"wide", no_argument, NULL, 'w'},
b7afb1
   {"prefix", required_argument, NULL, OPTION_PREFIX},
b7afb1
@@ -414,10 +430,124 @@ nonfatal (const char *msg)
b7afb1
   bfd_nonfatal (msg);
b7afb1
   exit_status = 1;
b7afb1
 }
b7afb1
+
b7afb1
+/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
b7afb1
+   The conversion format is controlled by the unicode_display variable.
b7afb1
+   Returns the number of characters added to OUT.
b7afb1
+   Returns the number of bytes consumed from IN in CONSUMED.
b7afb1
+   Always consumes at least one byte and displays at least one character.  */
b7afb1
+   
b7afb1
+static unsigned int
b7afb1
+display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
b7afb1
+{
b7afb1
+  char *        orig_out = out;
b7afb1
+  unsigned int  nchars = 0;
b7afb1
+
b7afb1
+  if (unicode_display == unicode_default)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if (in[0] < 0xc0)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[1] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[0] & 0x20) == 0)
b7afb1
+    {
b7afb1
+      nchars = 2;
b7afb1
+      goto valid;
b7afb1
+    }
b7afb1
+
b7afb1
+  if ((in[2] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  if ((in[0] & 0x10) == 0)
b7afb1
+    {
b7afb1
+      nchars = 3;
b7afb1
+      goto valid;
b7afb1
+    }
b7afb1
+
b7afb1
+  if ((in[3] & 0xc0) != 0x80)
b7afb1
+    goto invalid;
b7afb1
+
b7afb1
+  nchars = 4;
b7afb1
+
b7afb1
+ valid:
b7afb1
+  switch (unicode_display)
b7afb1
+    {
b7afb1
+    case unicode_locale:
b7afb1
+      /* Copy the bytes into the output buffer as is.  */
b7afb1
+      memcpy (out, in, nchars);
b7afb1
+      out += nchars;
b7afb1
+      break;
b7afb1
+
b7afb1
+    case unicode_invalid:
b7afb1
+    case unicode_hex:
b7afb1
+      {
b7afb1
+      unsigned int j;
b7afb1
+
b7afb1
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
b7afb1
+      for (j = 0; j < nchars; j++)
b7afb1
+	out += sprintf (out, "%02x", in [j]);
b7afb1
+      out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
b7afb1
+      }
b7afb1
+      break;
b7afb1
+      
b7afb1
+    case unicode_highlight:
b7afb1
+      if (isatty (1))
b7afb1
+	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
b7afb1
+      /* Fall through.  */
b7afb1
+    case unicode_escape:
b7afb1
+      switch (nchars)
b7afb1
+	{
b7afb1
+	case 2:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x",
b7afb1
+		  ((in[0] & 0x1c) >> 2), 
b7afb1
+		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 3:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x",
b7afb1
+		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
b7afb1
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 4:
b7afb1
+	  out += sprintf (out, "\\u%02x%02x%02x",
b7afb1
+		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
b7afb1
+		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
b7afb1
+		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
b7afb1
+	  break;
b7afb1
+	default:
b7afb1
+	  /* URG.  */
b7afb1
+	  break;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+	out += sprintf (out, "\033[0m"); /* Default colour.  */
b7afb1
+      break;
b7afb1
+
b7afb1
+    default:
b7afb1
+      /* URG */
b7afb1
+      break;
b7afb1
+    }
b7afb1
+
b7afb1
+  * consumed = nchars;
b7afb1
+  return out - orig_out;
b7afb1
+
b7afb1
+ invalid:
b7afb1
+  /* Not a valid UTF-8 sequence.  */
b7afb1
+  *out = *in;
b7afb1
+  * consumed = 1;
b7afb1
+  return 1;
b7afb1
+}
b7afb1
 
b7afb1
 /* Returns a version of IN with any control characters
b7afb1
    replaced by escape sequences.  Uses a static buffer
b7afb1
-   if necessary.  */
b7afb1
+   if necessary.
b7afb1
+
b7afb1
+   If unicode display is enabled, then also handles the
b7afb1
+   conversion of unicode characters.  */
b7afb1
 
b7afb1
 static const char *
b7afb1
 sanitize_string (const char * in)
b7afb1
@@ -435,40 +565,50 @@ sanitize_string (const char * in)
b7afb1
      of cases it will not be needed.  */
b7afb1
   do
b7afb1
     {
b7afb1
-      char c = *in++;
b7afb1
+      unsigned char c = *in++;
b7afb1
 
b7afb1
       if (c == 0)
b7afb1
 	return original;
b7afb1
 
b7afb1
       if (ISCNTRL (c))
b7afb1
 	break;
b7afb1
+
b7afb1
+      if (unicode_display != unicode_default && c >= 0xc0)
b7afb1
+	break;
b7afb1
     }
b7afb1
   while (1);
b7afb1
 
b7afb1
   /* Copy the input, translating as needed.  */
b7afb1
   in = original;
b7afb1
-  if (buffer_len < (strlen (in) * 2))
b7afb1
+  if (buffer_len < (strlen (in) * 9))
b7afb1
     {
b7afb1
       free ((void *) buffer);
b7afb1
-      buffer_len = strlen (in) * 2;
b7afb1
+      buffer_len = strlen (in) * 9;
b7afb1
       buffer = xmalloc (buffer_len + 1);
b7afb1
     }
b7afb1
 
b7afb1
   out = buffer;
b7afb1
   do
b7afb1
     {
b7afb1
-      char c = *in++;
b7afb1
+      unsigned char c = *in++;
b7afb1
 
b7afb1
       if (c == 0)
b7afb1
 	break;
b7afb1
 
b7afb1
-      if (!ISCNTRL (c))
b7afb1
-	*out++ = c;
b7afb1
-      else
b7afb1
+      if (ISCNTRL (c))
b7afb1
 	{
b7afb1
 	  *out++ = '^';
b7afb1
 	  *out++ = c + 0x40;
b7afb1
 	}
b7afb1
+      else if (unicode_display != unicode_default && c >= 0xc0)
b7afb1
+	{
b7afb1
+	  unsigned int num_consumed;
b7afb1
+
b7afb1
+	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
b7afb1
+	  in += num_consumed - 1;
b7afb1
+	}
b7afb1
+      else
b7afb1
+	*out++ = c;
b7afb1
     }
b7afb1
   while (1);
b7afb1
 
b7afb1
@@ -476,7 +616,6 @@ sanitize_string (const char * in)
b7afb1
   return buffer;
b7afb1
 }
b7afb1
 
b7afb1
-
b7afb1
 /* Returns TRUE if the specified section should be dumped.  */
b7afb1
 
b7afb1
 static bfd_boolean
b7afb1
@@ -1055,6 +1194,8 @@ objdump_print_symname (bfd *abfd, struct
b7afb1
 
b7afb1
   name = sanitize_string (name);
b7afb1
 
b7afb1
+  name = sanitize_string (name);
b7afb1
+
b7afb1
   if (inf != NULL)
b7afb1
     {
b7afb1
       (*inf->fprintf_func) (inf->stream, "%s", name);
b7afb1
@@ -3136,7 +3277,7 @@ disassemble_section (bfd *abfd, asection
b7afb1
   if (!bfd_malloc_and_get_section (abfd, section, &data))
b7afb1
     {
b7afb1
       non_fatal (_("Reading section %s failed because: %s"),
b7afb1
-		 section->name, bfd_errmsg (bfd_get_error ()));
b7afb1
+		 sanitize_string (section->name), bfd_errmsg (bfd_get_error ()));
b7afb1
       return;
b7afb1
     }
b7afb1
 
b7afb1
@@ -4307,7 +4448,7 @@ dump_section (bfd *abfd, asection *secti
b7afb1
   if (!bfd_get_full_section_contents (abfd, section, &data))
b7afb1
     {
b7afb1
       non_fatal (_("Reading section %s failed because: %s"),
b7afb1
-		 section->name, bfd_errmsg (bfd_get_error ()));
b7afb1
+		 sanitize_string (section->name), bfd_errmsg (bfd_get_error ()));
b7afb1
       return;
b7afb1
     }
b7afb1
 
b7afb1
@@ -4447,6 +4588,24 @@ dump_symbols (bfd *abfd ATTRIBUTE_UNUSED
b7afb1
 		  free (alloc);
b7afb1
 		}
b7afb1
 	    }
b7afb1
+	  else if (unicode_display != unicode_default
b7afb1
+		   && name != NULL && *name != '\0')
b7afb1
+	    {
b7afb1
+	      const char * sanitized_name;
b7afb1
+
b7afb1
+	      /* If we want to sanitize the name, we do it here, and
b7afb1
+		 temporarily clobber it while calling bfd_print_symbol.
b7afb1
+		 FIXME: This is a gross hack.  */
b7afb1
+	      sanitized_name = sanitize_string (name);
b7afb1
+	      if (sanitized_name != name)
b7afb1
+		(*current)->name = sanitized_name;
b7afb1
+	      else
b7afb1
+		sanitized_name = NULL;
b7afb1
+	      bfd_print_symbol (cur_bfd, stdout, *current,
b7afb1
+				bfd_print_symbol_all);
b7afb1
+	      if (sanitized_name != NULL)
b7afb1
+		(*current)->name = name;
b7afb1
+	    }
b7afb1
 	  else
b7afb1
 	    bfd_print_symbol (cur_bfd, stdout, *current,
b7afb1
 			      bfd_print_symbol_all);
b7afb1
@@ -5128,7 +5287,7 @@ main (int argc, char **argv)
b7afb1
   set_default_bfd_target ();
b7afb1
 
b7afb1
   while ((c = getopt_long (argc, argv,
b7afb1
-			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
b7afb1
+			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::U:",
b7afb1
 			   long_options, (int *) 0))
b7afb1
 	 != EOF)
b7afb1
     {
b7afb1
@@ -5407,6 +5566,23 @@ main (int argc, char **argv)
b7afb1
 	  seenflag = TRUE;
b7afb1
 	  break;
b7afb1
 
b7afb1
+	case 'U':
b7afb1
+	  if (streq (optarg, "default") || streq (optarg, "d"))
b7afb1
+	    unicode_display = unicode_default;
b7afb1
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
b7afb1
+	    unicode_display = unicode_locale;
b7afb1
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
b7afb1
+	    unicode_display = unicode_escape;
b7afb1
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
b7afb1
+	    unicode_display = unicode_invalid;
b7afb1
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
b7afb1
+	    unicode_display = unicode_hex;
b7afb1
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
b7afb1
+	    unicode_display = unicode_highlight;
b7afb1
+	  else
b7afb1
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
b7afb1
+	  break;
b7afb1
+
b7afb1
 	case 'H':
b7afb1
 	  usage (stdout, 0);
b7afb1
 	  /* No need to set seenflag or to break - usage() does not return.  */
b7afb1
diff -rup binutils.orig/binutils/readelf.c binutils-2.35/binutils/readelf.c
b7afb1
--- binutils.orig/binutils/readelf.c	2021-10-21 15:15:57.835970002 +0100
b7afb1
+++ binutils-2.35/binutils/readelf.c	2021-10-21 15:39:14.479071635 +0100
b7afb1
@@ -317,6 +317,18 @@ typedef enum print_mode
b7afb1
 }
b7afb1
 print_mode;
b7afb1
 
b7afb1
+typedef enum unicode_display_type
b7afb1
+{
b7afb1
+  unicode_locale,
b7afb1
+  unicode_escape,
b7afb1
+  unicode_hex,
b7afb1
+  unicode_highlight,
b7afb1
+  unicode_invalid
b7afb1
+} unicode_display_type;
b7afb1
+
b7afb1
+static unicode_display_type unicode_display = unicode_locale;
b7afb1
+
b7afb1
+  
b7afb1
 /* Versioned symbol info.  */
b7afb1
 enum versioned_symbol_info
b7afb1
 {
b7afb1
@@ -592,11 +604,18 @@ print_symbol (signed int width, const ch
b7afb1
       if (c == 0)
b7afb1
 	break;
b7afb1
 
b7afb1
-      /* Do not print control characters directly as they can affect terminal
b7afb1
-	 settings.  Such characters usually appear in the names generated
b7afb1
-	 by the assembler for local labels.  */
b7afb1
-      if (ISCNTRL (c))
b7afb1
+      if (ISPRINT (c))
b7afb1
+	{
b7afb1
+	  putchar (c);
b7afb1
+	  width_remaining --;
b7afb1
+	  num_printed ++;
b7afb1
+	}
b7afb1
+      else if (ISCNTRL (c))
b7afb1
 	{
b7afb1
+	  /* Do not print control characters directly as they can affect terminal
b7afb1
+	     settings.  Such characters usually appear in the names generated
b7afb1
+	     by the assembler for local labels.  */
b7afb1
+
b7afb1
 	  if (width_remaining < 2)
b7afb1
 	    break;
b7afb1
 
b7afb1
@@ -604,11 +623,135 @@ print_symbol (signed int width, const ch
b7afb1
 	  width_remaining -= 2;
b7afb1
 	  num_printed += 2;
b7afb1
 	}
b7afb1
-      else if (ISPRINT (c))
b7afb1
+      else if (c == 0x7f)
b7afb1
 	{
b7afb1
-	  putchar (c);
b7afb1
-	  width_remaining --;
b7afb1
-	  num_printed ++;
b7afb1
+	  if (width_remaining < 5)
b7afb1
+	    break;
b7afb1
+	  printf ("");
b7afb1
+	  width_remaining -= 5;
b7afb1
+	  num_printed += 5;
b7afb1
+	}
b7afb1
+      else if (unicode_display != unicode_locale)
b7afb1
+	{
b7afb1
+	  /* Display unicode characters as something else.  */
b7afb1
+	  unsigned char bytes[4];
b7afb1
+	  bfd_boolean   is_utf8;
b7afb1
+	  unsigned int  nbytes;
b7afb1
+
b7afb1
+	  bytes[0] = c;
b7afb1
+
b7afb1
+	  if (bytes[0] < 0xc0)
b7afb1
+	    {
b7afb1
+	      nbytes = 1;
b7afb1
+	      is_utf8 = FALSE;
b7afb1
+	    }
b7afb1
+	  else
b7afb1
+	    {
b7afb1
+	      bytes[1] = *symbol++;
b7afb1
+
b7afb1
+	      if ((bytes[1] & 0xc0) != 0x80)
b7afb1
+		{
b7afb1
+		  is_utf8 = FALSE;
b7afb1
+		  /* Do not consume this character.  It may only
b7afb1
+		     be the first byte in the sequence that was
b7afb1
+		     corrupt.  */
b7afb1
+		  --symbol;
b7afb1
+		  nbytes = 1;
b7afb1
+		}
b7afb1
+	      else if ((bytes[0] & 0x20) == 0)
b7afb1
+		{
b7afb1
+		  is_utf8 = TRUE;
b7afb1
+		  nbytes = 2;
b7afb1
+		}
b7afb1
+	      else
b7afb1
+		{
b7afb1
+		  bytes[2] = *symbol++;
b7afb1
+
b7afb1
+		  if ((bytes[2] & 0xc0) != 0x80)
b7afb1
+		    {
b7afb1
+		      is_utf8 = FALSE;
b7afb1
+		      symbol -= 2;
b7afb1
+		      nbytes = 1;
b7afb1
+		    }
b7afb1
+		  else if ((bytes[0] & 0x10) == 0)
b7afb1
+		    {
b7afb1
+		      is_utf8 = TRUE;
b7afb1
+		      nbytes = 3;
b7afb1
+		    }
b7afb1
+		  else
b7afb1
+		    {
b7afb1
+		      bytes[3] = *symbol++;
b7afb1
+
b7afb1
+		      nbytes = 4;
b7afb1
+
b7afb1
+		      if ((bytes[3] & 0xc0) != 0x80)
b7afb1
+			{
b7afb1
+			  is_utf8 = FALSE;
b7afb1
+			  symbol -= 3;
b7afb1
+			  nbytes = 1;
b7afb1
+			}
b7afb1
+		      else
b7afb1
+			is_utf8 = TRUE;
b7afb1
+		    }
b7afb1
+		}
b7afb1
+	    }
b7afb1
+
b7afb1
+	  if (unicode_display == unicode_invalid)
b7afb1
+	    is_utf8 = FALSE;
b7afb1
+
b7afb1
+	  if (unicode_display == unicode_hex || ! is_utf8)
b7afb1
+	    {
b7afb1
+	      unsigned int i;
b7afb1
+
b7afb1
+	      if (width_remaining < (nbytes * 2) + 2)
b7afb1
+		break;
b7afb1
+	  
b7afb1
+	      putchar (is_utf8 ? '<' : '{');
b7afb1
+	      for (i = 0; i < nbytes; i++)
b7afb1
+		printf ("%02x", bytes[i]);
b7afb1
+	      putchar (is_utf8 ? '>' : '}');
b7afb1
+	    }
b7afb1
+	  else
b7afb1
+	    {
b7afb1
+	      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+		printf ("\x1B[31;47m"); /* Red.  */
b7afb1
+	      
b7afb1
+	      switch (nbytes)
b7afb1
+		{
b7afb1
+		case 2:
b7afb1
+		  if (width_remaining < 6)
b7afb1
+		    break;
b7afb1
+		  printf ("\\u%02x%02x",
b7afb1
+			  (bytes[0] & 0x1c) >> 2, 
b7afb1
+			  ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
b7afb1
+		  break;
b7afb1
+		case 3:
b7afb1
+		  if (width_remaining < 6)
b7afb1
+		    break;
b7afb1
+		  printf ("\\u%02x%02x",
b7afb1
+			  ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
b7afb1
+			  ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
b7afb1
+		  break;
b7afb1
+		case 4:
b7afb1
+		  if (width_remaining < 8)
b7afb1
+		    break;
b7afb1
+		  printf ("\\u%02x%02x%02x",
b7afb1
+			  ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
b7afb1
+			  ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
b7afb1
+			  ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
b7afb1
+		  
b7afb1
+		  break;
b7afb1
+		default:
b7afb1
+		  /* URG.  */
b7afb1
+		  break;
b7afb1
+		}
b7afb1
+
b7afb1
+	      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+		printf ("\033[0m"); /* Default colour.  */
b7afb1
+	    }
b7afb1
+	  
b7afb1
+	  if (bytes[nbytes - 1] == 0)
b7afb1
+	    break;
b7afb1
 	}
b7afb1
       else
b7afb1
 	{
b7afb1
@@ -4528,6 +4671,7 @@ static struct option options[] =
b7afb1
   {"ctf-parent",       required_argument, 0, OPTION_CTF_PARENT},
b7afb1
 #endif
b7afb1
 
b7afb1
+  {"unicode",          required_argument, 0, 'U'},
b7afb1
   {"version",	       no_argument, 0, 'v'},
b7afb1
   {"wide",	       no_argument, 0, 'W'},
b7afb1
   {"silent-truncation",no_argument, 0, 'T'},
b7afb1
@@ -4597,6 +4741,11 @@ usage (FILE * stream)
b7afb1
 #endif
b7afb1
   fprintf (stream, _("\
b7afb1
   -I --histogram         Display histogram of bucket list lengths\n\
b7afb1
+  -U --unicode=[locale|escape|hex|highlight|invalid]\n\
b7afb1
+                         Display unicode characters as determined by the current locale\n\
b7afb1
+                          (default), escape sequences, \"<hex sequences>\", highlighted\n\
b7afb1
+                          escape sequences, or treat them as invalid and display as\n\
b7afb1
+                          \"{hex sequences}\"\n\
b7afb1
   -W --wide              Allow output width to exceed 80 characters\n\
b7afb1
   -T --silent-truncation If a symbol name is truncated, do not add a suffix [...]\n\
b7afb1
   @<file>                Read options from <file>\n\
b7afb1
@@ -4693,7 +4842,7 @@ parse_args (struct dump_data *dumpdata,
b7afb1
     usage (stderr);
b7afb1
 
b7afb1
   while ((c = getopt_long
b7afb1
-	  (argc, argv, "ADHILNR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
b7afb1
+	  (argc, argv, "ADHILNR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
b7afb1
     {
b7afb1
       switch (c)
b7afb1
 	{
b7afb1
@@ -4843,6 +4992,25 @@ parse_args (struct dump_data *dumpdata,
b7afb1
 	  request_dump (dumpdata, DISASS_DUMP);
b7afb1
 	  break;
b7afb1
 #endif
b7afb1
+	case 'U':
b7afb1
+	  if (optarg == NULL)
b7afb1
+	    error (_("Missing arg to -U/--unicode")); /* Can this happen ?  */
b7afb1
+	  else if (streq (optarg, "default") || streq (optarg, "d"))
b7afb1
+	    unicode_display = unicode_locale;
b7afb1
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
b7afb1
+	    unicode_display = unicode_locale;
b7afb1
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
b7afb1
+	    unicode_display = unicode_escape;
b7afb1
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
b7afb1
+	    unicode_display = unicode_invalid;
b7afb1
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
b7afb1
+	    unicode_display = unicode_hex;
b7afb1
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
b7afb1
+	    unicode_display = unicode_highlight;
b7afb1
+	  else
b7afb1
+	    error (_("unknown argument to -U/--unicode: %s"), optarg);
b7afb1
+	  break;
b7afb1
+
b7afb1
 	case 'v':
b7afb1
 	  print_version (program_name);
b7afb1
 	  break;
b7afb1
diff -rup binutils.orig/binutils/strings.c binutils-2.35/binutils/strings.c
b7afb1
--- binutils.orig/binutils/strings.c	2021-10-21 15:15:57.837969988 +0100
b7afb1
+++ binutils-2.35/binutils/strings.c	2021-10-21 15:39:14.479071635 +0100
b7afb1
@@ -55,6 +55,19 @@
b7afb1
    -T {bfdname}
b7afb1
 		Specify a non-default object file format.
b7afb1
 
b7afb1
+  --unicode={default|locale|invalid|hex|escape|highlight}
b7afb1
+  -U {d|l|i|x|e|h}
b7afb1
+                Determine how to handle UTF-8 unicode characters.  The default
b7afb1
+		is no special treatment.  All other versions of this option
b7afb1
+		only apply if the encoding is valid and enabling the option
b7afb1
+		implies --encoding=S.
b7afb1
+		The 'locale' option displays the characters according to the
b7afb1
+		current locale.  The 'invalid' option treats them as
b7afb1
+		non-string characters.  The 'hex' option displays them as hex
b7afb1
+		byte sequences.  The 'escape' option displays them as escape
b7afb1
+		sequences and the 'highlight' option displays them as
b7afb1
+		coloured escape sequences.
b7afb1
+
b7afb1
   --output-separator=sep_string
b7afb1
   -s sep_string	String used to separate parsed strings in output.
b7afb1
 		Default is newline.
b7afb1
@@ -76,6 +89,22 @@
b7afb1
 #include "safe-ctype.h"
b7afb1
 #include "bucomm.h"
b7afb1
 
b7afb1
+#ifndef streq
b7afb1
+#define streq(a,b) (strcmp ((a),(b)) == 0)
b7afb1
+#endif
b7afb1
+
b7afb1
+typedef enum unicode_display_type
b7afb1
+{
b7afb1
+  unicode_default = 0,
b7afb1
+  unicode_locale,
b7afb1
+  unicode_escape,
b7afb1
+  unicode_hex,
b7afb1
+  unicode_highlight,
b7afb1
+  unicode_invalid
b7afb1
+} unicode_display_type;
b7afb1
+
b7afb1
+static unicode_display_type unicode_display = unicode_default;
b7afb1
+
b7afb1
 #define STRING_ISGRAPHIC(c) \
b7afb1
       (   (c) >= 0 \
b7afb1
        && (c) <= 255 \
b7afb1
@@ -94,7 +123,7 @@ extern int errno;
b7afb1
 static int address_radix;
b7afb1
 
b7afb1
 /* Minimum length of sequence of graphic chars to trigger output.  */
b7afb1
-static int string_min;
b7afb1
+static uint string_min;
b7afb1
 
b7afb1
 /* Whether or not we include all whitespace as a graphic char.   */
b7afb1
 static bfd_boolean include_all_whitespace;
b7afb1
@@ -130,6 +159,7 @@ static struct option long_options[] =
b7afb1
   {"target", required_argument, NULL, 'T'},
b7afb1
   {"output-separator", required_argument, NULL, 's'},
b7afb1
   {"help", no_argument, NULL, 'h'},
b7afb1
+  {"unicode", required_argument, NULL, 'U'},
b7afb1
   {"version", no_argument, NULL, 'v'},
b7afb1
   {NULL, 0, NULL, 0}
b7afb1
 };
b7afb1
@@ -173,7 +203,7 @@ main (int argc, char **argv)
b7afb1
   encoding = 's';
b7afb1
   output_separator = NULL;
b7afb1
 
b7afb1
-  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:Vv0123456789",
b7afb1
+  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:U:Vv0123456789",
b7afb1
 			      long_options, (int *) 0)) != EOF)
b7afb1
     {
b7afb1
       switch (optc)
b7afb1
@@ -246,6 +276,23 @@ main (int argc, char **argv)
b7afb1
 	  output_separator = optarg;
b7afb1
           break;
b7afb1
 
b7afb1
+	case 'U':
b7afb1
+	  if (streq (optarg, "default") || streq (optarg, "d"))
b7afb1
+	    unicode_display = unicode_default;
b7afb1
+	  else if (streq (optarg, "locale") || streq (optarg, "l"))
b7afb1
+	    unicode_display = unicode_locale;
b7afb1
+	  else if (streq (optarg, "escape") || streq (optarg, "e"))
b7afb1
+	    unicode_display = unicode_escape;
b7afb1
+	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
b7afb1
+	    unicode_display = unicode_invalid;
b7afb1
+	  else if (streq (optarg, "hex") || streq (optarg, "x"))
b7afb1
+	    unicode_display = unicode_hex;
b7afb1
+	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
b7afb1
+	    unicode_display = unicode_highlight;
b7afb1
+	  else
b7afb1
+	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
b7afb1
+	  break;
b7afb1
+
b7afb1
 	case 'V':
b7afb1
 	case 'v':
b7afb1
 	  print_version ("strings");
b7afb1
@@ -260,6 +307,9 @@ main (int argc, char **argv)
b7afb1
 	}
b7afb1
     }
b7afb1
 
b7afb1
+  if (unicode_display != unicode_default)
b7afb1
+    encoding = 'S';
b7afb1
+
b7afb1
   if (numeric_opt != 0)
b7afb1
     {
b7afb1
       string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
b7afb1
@@ -553,11 +603,629 @@ unget_part_char (long c, file_ptr *addre
b7afb1
 	}
b7afb1
     }
b7afb1
 }
b7afb1
+
b7afb1
+static void
b7afb1
+print_filename_and_address (const char * filename, file_ptr address)
b7afb1
+{
b7afb1
+  if (print_filenames)
b7afb1
+    printf ("%s: ", filename);
b7afb1
+
b7afb1
+  if (! print_addresses)
b7afb1
+    return;
b7afb1
+
b7afb1
+  switch (address_radix)
b7afb1
+    {
b7afb1
+    case 8:
b7afb1
+      if (sizeof (address) > sizeof (long))
b7afb1
+	{
b7afb1
+#ifndef __MSVCRT__
b7afb1
+	  printf ("%7llo ", (unsigned long long) address);
b7afb1
+#else
b7afb1
+	  printf ("%7I64o ", (unsigned long long) address);
b7afb1
+#endif
b7afb1
+	}
b7afb1
+      else
b7afb1
+	printf ("%7lo ", (unsigned long) address);
b7afb1
+      break;
b7afb1
+
b7afb1
+    case 10:
b7afb1
+      if (sizeof (address) > sizeof (long))
b7afb1
+	{
b7afb1
+#ifndef __MSVCRT__
b7afb1
+	  printf ("%7llu ", (unsigned long long) address);
b7afb1
+#else
b7afb1
+	  printf ("%7I64d ", (unsigned long long) address);
b7afb1
+#endif
b7afb1
+	}
b7afb1
+      else
b7afb1
+	printf ("%7ld ", (long) address);
b7afb1
+      break;
b7afb1
+
b7afb1
+    case 16:
b7afb1
+      if (sizeof (address) > sizeof (long))
b7afb1
+	{
b7afb1
+#ifndef __MSVCRT__
b7afb1
+	  printf ("%7llx ", (unsigned long long) address);
b7afb1
+#else
b7afb1
+	  printf ("%7I64x ", (unsigned long long) address);
b7afb1
+#endif
b7afb1
+	}
b7afb1
+      else
b7afb1
+	printf ("%7lx ", (unsigned long) address);
b7afb1
+      break;
b7afb1
+    }
b7afb1
+}
b7afb1
+
b7afb1
+/* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
b7afb1
+   If the encoding is valid then returns the number of bytes it uses.  */
b7afb1
+
b7afb1
+static unsigned int
b7afb1
+is_valid_utf8 (const unsigned char * buffer, unsigned long buflen)
b7afb1
+{
b7afb1
+  if (buffer[0] < 0xc0)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  if (buflen < 2)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  if ((buffer[1] & 0xc0) != 0x80)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  if ((buffer[0] & 0x20) == 0)
b7afb1
+    return 2;
b7afb1
+
b7afb1
+  if (buflen < 3)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  if ((buffer[2] & 0xc0) != 0x80)
b7afb1
+    return 0;
b7afb1
+  
b7afb1
+  if ((buffer[0] & 0x10) == 0)
b7afb1
+    return 3;
b7afb1
+
b7afb1
+  if (buflen < 4)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  if ((buffer[3] & 0xc0) != 0x80)
b7afb1
+    return 0;
b7afb1
+
b7afb1
+  return 4;
b7afb1
+}
b7afb1
+
b7afb1
+/* Display a UTF-8 encoded character in BUFFER according to the setting
b7afb1
+   of unicode_display.  The character is known to be valid.
b7afb1
+   Returns the number of bytes consumed.  */
b7afb1
+
b7afb1
+static unsigned int
b7afb1
+display_utf8_char (const unsigned char * buffer)
b7afb1
+{
b7afb1
+  unsigned int j;
b7afb1
+  unsigned int utf8_len;
b7afb1
+
b7afb1
+  switch (buffer[0] & 0x30)
b7afb1
+    {
b7afb1
+    case 0x00:
b7afb1
+    case 0x10:
b7afb1
+      utf8_len = 2;
b7afb1
+      break;
b7afb1
+    case 0x20:
b7afb1
+      utf8_len = 3;
b7afb1
+      break;
b7afb1
+    default:
b7afb1
+      utf8_len = 4;
b7afb1
+    }
b7afb1
+      
b7afb1
+  switch (unicode_display)
b7afb1
+    {
b7afb1
+    default:
b7afb1
+      fprintf (stderr, "ICE: unexpected unicode display type\n");
b7afb1
+      break;
b7afb1
+
b7afb1
+    case unicode_escape:
b7afb1
+    case unicode_highlight:
b7afb1
+      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+	printf ("\x1B[31;47m"); /* Red.  */
b7afb1
+
b7afb1
+      switch (utf8_len)
b7afb1
+	{
b7afb1
+	case 2:
b7afb1
+	  printf ("\\u%02x%02x",
b7afb1
+		  ((buffer[0] & 0x1c) >> 2), 
b7afb1
+		  ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 3:
b7afb1
+	  printf ("\\u%02x%02x",
b7afb1
+		  ((buffer[0] & 0x0f) << 4) | ((buffer[1] & 0x3c) >> 2),
b7afb1
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3f)));
b7afb1
+	  break;
b7afb1
+
b7afb1
+	case 4:
b7afb1
+	  printf ("\\u%02x%02x%02x",
b7afb1
+		  ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
b7afb1
+		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
b7afb1
+		  ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
b7afb1
+	  break;
b7afb1
+	default:
b7afb1
+	  /* URG.  */
b7afb1
+	  break;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (unicode_display == unicode_highlight && isatty (1))
b7afb1
+	printf ("\033[0m"); /* Default colour.  */
b7afb1
+      break;
b7afb1
+
b7afb1
+    case unicode_hex:
b7afb1
+      putchar ('<');
b7afb1
+      for (j = 0; j < utf8_len; j++)
b7afb1
+	printf ("%02x", buffer [j]);
b7afb1
+      putchar ('>');
b7afb1
+      break;
b7afb1
+
b7afb1
+    case unicode_locale:
b7afb1
+      printf ("%.1s", buffer);
b7afb1
+      break;
b7afb1
+    }
b7afb1
+
b7afb1
+  return utf8_len;
b7afb1
+}
b7afb1
+
b7afb1
+/* Display strings in BUFFER.  Treat any UTF-8 encoded characters encountered
b7afb1
+   according to the setting of the unicode_display variable.  The buffer
b7afb1
+   contains BUFLEN bytes.
b7afb1
+
b7afb1
+   Display the characters as if they started at ADDRESS and are contained in
b7afb1
+   FILENAME.  */
b7afb1
+
b7afb1
+static void
b7afb1
+print_unicode_buffer (const char *            filename,
b7afb1
+		      file_ptr                address,
b7afb1
+		      const unsigned char *   buffer,
b7afb1
+		      unsigned long           buflen)
b7afb1
+{
b7afb1
+  /* Paranoia checks...  */
b7afb1
+  if (filename == NULL
b7afb1
+      || buffer == NULL
b7afb1
+      || unicode_display == unicode_default
b7afb1
+      || encoding != 'S'
b7afb1
+      || encoding_bytes != 1)
b7afb1
+    {
b7afb1
+      fprintf (stderr, "ICE: bad arguments to print_unicode_buffer\n");
b7afb1
+      return;
b7afb1
+    }
b7afb1
+
b7afb1
+  if (buflen == 0)
b7afb1
+    return;
b7afb1
+
b7afb1
+  /* We must only display strings that are at least string_min *characters*
b7afb1
+     long.  So we scan the buffer in two stages.  First we locate the start
b7afb1
+     of a potential string.  Then we walk along it until we have found
b7afb1
+     string_min characters.  Then we go back to the start point and start
b7afb1
+     displaying characters according to the unicode_display setting.  */
b7afb1
+
b7afb1
+  unsigned long start_point = 0;
b7afb1
+  unsigned long i = 0;
b7afb1
+  unsigned int char_len = 1;
b7afb1
+  unsigned int num_found = 0;
b7afb1
+
b7afb1
+  for (i = 0; i < buflen; i += char_len)
b7afb1
+    {
b7afb1
+      int c = buffer[i];
b7afb1
+
b7afb1
+      char_len = 1;
b7afb1
+
b7afb1
+      /* Find the first potential character of a string.  */
b7afb1
+      if (! STRING_ISGRAPHIC (c))
b7afb1
+	{
b7afb1
+	  num_found = 0;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (c > 126)
b7afb1
+	{
b7afb1
+	  if (c < 0xc0)
b7afb1
+	    {
b7afb1
+	      num_found = 0;
b7afb1
+	      continue;
b7afb1
+	    }
b7afb1
+
b7afb1
+	  if ((char_len = is_valid_utf8 (buffer + i, buflen - i)) == 0)
b7afb1
+	    {
b7afb1
+	      char_len = 1;
b7afb1
+	      num_found = 0;
b7afb1
+	      continue;
b7afb1
+	    }
b7afb1
+
b7afb1
+	  if (unicode_display == unicode_invalid)
b7afb1
+	    {
b7afb1
+	      /* We have found a valid UTF-8 character, but we treat it as non-graphic.  */
b7afb1
+	      num_found = 0;
b7afb1
+	      continue;
b7afb1
+	    }
b7afb1
+	}
b7afb1
+
b7afb1
+      if (num_found == 0)
b7afb1
+	/* We have found a potential starting point for a string.  */
b7afb1
+	start_point = i;
b7afb1
+
b7afb1
+      ++ num_found;
b7afb1
+
b7afb1
+      if (num_found >= string_min)
b7afb1
+	break;
b7afb1
+    }
b7afb1
+
b7afb1
+  if (num_found < string_min)
b7afb1
+    return;
b7afb1
+
b7afb1
+  print_filename_and_address (filename, address + start_point);
b7afb1
+  
b7afb1
+  /* We have found string_min characters.  Display them and any
b7afb1
+     more that follow.  */
b7afb1
+  for (i = start_point; i < buflen; i += char_len)
b7afb1
+    {
b7afb1
+      int c = buffer[i];
b7afb1
+
b7afb1
+      char_len = 1;
b7afb1
+
b7afb1
+      if (! STRING_ISGRAPHIC (c))
b7afb1
+	break;
b7afb1
+      else if (c < 127)
b7afb1
+	putchar (c);
b7afb1
+      else if (! is_valid_utf8 (buffer + i, buflen - i))
b7afb1
+	break;
b7afb1
+      else if (unicode_display == unicode_invalid)
b7afb1
+	break;
b7afb1
+      else
b7afb1
+	char_len = display_utf8_char (buffer + i);
b7afb1
+    }
b7afb1
+
b7afb1
+  if (output_separator)
b7afb1
+    fputs (output_separator, stdout);
b7afb1
+  else
b7afb1
+    putchar ('\n');
b7afb1
+
b7afb1
+  /* FIXME: Using tail recursion here is lazy programming...  */
b7afb1
+  print_unicode_buffer (filename, address + i, buffer + i, buflen - i);
b7afb1
+}
b7afb1
+
b7afb1
+static int
b7afb1
+get_unicode_byte (FILE *           stream,
b7afb1
+		  unsigned char *  putback,
b7afb1
+		  unsigned int *   num_putback,
b7afb1
+		  unsigned int *   num_read)
b7afb1
+{
b7afb1
+  if (* num_putback > 0)
b7afb1
+    {
b7afb1
+      * num_putback = * num_putback - 1;
b7afb1
+      return putback [* num_putback];
b7afb1
+    }
b7afb1
+
b7afb1
+  * num_read = * num_read + 1;
b7afb1
+
b7afb1
+#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
b7afb1
+  return getc_unlocked (stream);
b7afb1
+#else
b7afb1
+  return getc (stream);
b7afb1
+#endif
b7afb1
+}
b7afb1
+
b7afb1
+/* Helper function for print_unicode_stream.  */
b7afb1
+
b7afb1
+static void
b7afb1
+print_unicode_stream_body (const char *     filename,
b7afb1
+			   file_ptr         address,
b7afb1
+			   FILE *           stream,
b7afb1
+			   unsigned char *  putback_buf,
b7afb1
+			   unsigned int     num_putback,
b7afb1
+			   unsigned char *  print_buf)
b7afb1
+{
b7afb1
+  /* It would be nice if we could just read the stream into a buffer
b7afb1
+     and then process if with print_unicode_buffer.  But the input
b7afb1
+     might be huge or it might time-locked (eg stdin).  So instead
b7afb1
+     we go one byte at a time...  */
b7afb1
+
b7afb1
+  file_ptr start_point = 0;
b7afb1
+  unsigned int num_read = 0;
b7afb1
+  unsigned int num_chars = 0;
b7afb1
+  unsigned int num_print = 0;
b7afb1
+  int c;
b7afb1
+
b7afb1
+  /* Find a series of string_min characters.  Put them into print_buf.  */
b7afb1
+  do
b7afb1
+    {
b7afb1
+      if (num_chars >= string_min)
b7afb1
+	break;
b7afb1
+
b7afb1
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+      if (c == EOF)
b7afb1
+	break;
b7afb1
+
b7afb1
+      if (! STRING_ISGRAPHIC (c))
b7afb1
+	{
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (num_chars == 0)
b7afb1
+	start_point = num_read - 1;
b7afb1
+
b7afb1
+      if (c < 127)
b7afb1
+	{
b7afb1
+	  print_buf[num_print] = c;
b7afb1
+	  num_chars ++;
b7afb1
+	  num_print ++;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      if (c < 0xc0)
b7afb1
+	{
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
b7afb1
+      char utf8[4];
b7afb1
+
b7afb1
+      utf8[0] = c;
b7afb1
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+      if (c == EOF)
b7afb1
+	break;
b7afb1
+      utf8[1] = c;
b7afb1
+
b7afb1
+      if ((utf8[1] & 0xc0) != 0x80)
b7afb1
+	{
b7afb1
+	  /* Invalid UTF-8.  */
b7afb1
+	  putback_buf[num_putback++] = utf8[1];
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+      else if ((utf8[0] & 0x20) == 0)
b7afb1
+	{
b7afb1
+	  /* A valid 2-byte UTF-8 encoding.  */
b7afb1
+	  if (unicode_display == unicode_invalid)
b7afb1
+	    {
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      num_chars = num_print = 0;
b7afb1
+	    }
b7afb1
+	  else
b7afb1
+	    {
b7afb1
+	      print_buf[num_print ++] = utf8[0];
b7afb1
+	      print_buf[num_print ++] = utf8[1];
b7afb1
+	      num_chars ++;
b7afb1
+	    }
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+      if (c == EOF)
b7afb1
+	break;
b7afb1
+      utf8[2] = c;
b7afb1
+
b7afb1
+      if ((utf8[2] & 0xc0) != 0x80)
b7afb1
+	{
b7afb1
+	  /* Invalid UTF-8.  */
b7afb1
+	  putback_buf[num_putback++] = utf8[2];
b7afb1
+	  putback_buf[num_putback++] = utf8[1];
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+      else if ((utf8[0] & 0x10) == 0)
b7afb1
+	{
b7afb1
+	  /* A valid 3-byte UTF-8 encoding.  */
b7afb1
+	  if (unicode_display == unicode_invalid)
b7afb1
+	    {
b7afb1
+	      putback_buf[num_putback++] = utf8[2];
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      num_chars = num_print = 0;
b7afb1
+	    }
b7afb1
+	  else
b7afb1
+	    {
b7afb1
+	      print_buf[num_print ++] = utf8[0];
b7afb1
+	      print_buf[num_print ++] = utf8[1];
b7afb1
+	      print_buf[num_print ++] = utf8[2];
b7afb1
+	      num_chars ++;
b7afb1
+	    }
b7afb1
+	  continue;
b7afb1
+	}
b7afb1
+
b7afb1
+      c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+      if (c == EOF)
b7afb1
+	break;
b7afb1
+      utf8[3] = c;
b7afb1
+
b7afb1
+      if ((utf8[3] & 0xc0) != 0x80)
b7afb1
+	{
b7afb1
+	  /* Invalid UTF-8.  */
b7afb1
+	  putback_buf[num_putback++] = utf8[3];
b7afb1
+	  putback_buf[num_putback++] = utf8[2];
b7afb1
+	  putback_buf[num_putback++] = utf8[1];
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	}
b7afb1
+      /* We have a valid 4-byte UTF-8 encoding.  */
b7afb1
+      else if (unicode_display == unicode_invalid)
b7afb1
+	{
b7afb1
+	  putback_buf[num_putback++] = utf8[3];
b7afb1
+	  putback_buf[num_putback++] = utf8[1];
b7afb1
+	  putback_buf[num_putback++] = utf8[2];
b7afb1
+	  num_chars = num_print = 0;
b7afb1
+	}
b7afb1
+      else
b7afb1
+	{
b7afb1
+	  print_buf[num_print ++] = utf8[0];
b7afb1
+	  print_buf[num_print ++] = utf8[1];
b7afb1
+	  print_buf[num_print ++] = utf8[2];
b7afb1
+	  print_buf[num_print ++] = utf8[3];
b7afb1
+	  num_chars ++;
b7afb1
+	}
b7afb1
+    }
b7afb1
+  while (1);
b7afb1
+
b7afb1
+  if (num_chars >= string_min)
b7afb1
+    {
b7afb1
+      /* We know that we have string_min valid characters in print_buf,
b7afb1
+	 and there may be more to come in the stream.  Start displaying
b7afb1
+	 them.  */
b7afb1
+
b7afb1
+      print_filename_and_address (filename, address + start_point);
b7afb1
+
b7afb1
+      unsigned int i;
b7afb1
+      for (i = 0; i < num_print;)
b7afb1
+	{
b7afb1
+	  if (print_buf[i] < 127)
b7afb1
+	    putchar (print_buf[i++]);
b7afb1
+	  else
b7afb1
+	    i += display_utf8_char (print_buf + i);
b7afb1
+	}
b7afb1
+
b7afb1
+      /* OK so now we have to start read unchecked bytes.  */
b7afb1
+
b7afb1
+        /* Find a series of string_min characters.  Put them into print_buf.  */
b7afb1
+      do
b7afb1
+	{
b7afb1
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+	  if (c == EOF)
b7afb1
+	    break;
b7afb1
+
b7afb1
+	  if (! STRING_ISGRAPHIC (c))
b7afb1
+	    break;
b7afb1
+
b7afb1
+	  if (c < 127)
b7afb1
+	    {
b7afb1
+	      putchar (c);
b7afb1
+	      continue;
b7afb1
+	    }
b7afb1
+
b7afb1
+	  if (c < 0xc0)
b7afb1
+	    break;
b7afb1
+
b7afb1
+	  /* We *might* have a UTF-8 sequence.  Time to start peeking.  */
b7afb1
+	  unsigned char utf8[4];
b7afb1
+
b7afb1
+	  utf8[0] = c;
b7afb1
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+	  if (c == EOF)
b7afb1
+	    break;
b7afb1
+	  utf8[1] = c;
b7afb1
+
b7afb1
+	  if ((utf8[1] & 0xc0) != 0x80)
b7afb1
+	    {
b7afb1
+	      /* Invalid UTF-8.  */
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      break;
b7afb1
+	    }
b7afb1
+	  else if ((utf8[0] & 0x20) == 0)
b7afb1
+	    {
b7afb1
+	      /* Valid 2-byte UTF-8.  */
b7afb1
+	      if (unicode_display == unicode_invalid)
b7afb1
+		{
b7afb1
+		  putback_buf[num_putback++] = utf8[1];
b7afb1
+		  break;
b7afb1
+		}
b7afb1
+	      else
b7afb1
+		{
b7afb1
+		  (void) display_utf8_char (utf8);
b7afb1
+		  continue;
b7afb1
+		}
b7afb1
+	    }
b7afb1
+
b7afb1
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+	  if (c == EOF)
b7afb1
+	    break;
b7afb1
+	  utf8[2] = c;
b7afb1
+
b7afb1
+	  if ((utf8[2] & 0xc0) != 0x80)
b7afb1
+	    {
b7afb1
+	      /* Invalid UTF-8.  */
b7afb1
+	      putback_buf[num_putback++] = utf8[2];
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      break;
b7afb1
+	    }
b7afb1
+	  else if ((utf8[0] & 0x10) == 0)
b7afb1
+	    {
b7afb1
+	      /* Valid 3-byte UTF-8.  */
b7afb1
+	      if (unicode_display == unicode_invalid)
b7afb1
+		{
b7afb1
+		  putback_buf[num_putback++] = utf8[2];
b7afb1
+		  putback_buf[num_putback++] = utf8[1];
b7afb1
+		  break;
b7afb1
+		}
b7afb1
+	      else
b7afb1
+		{
b7afb1
+		  (void) display_utf8_char (utf8);
b7afb1
+		  continue;
b7afb1
+		}
b7afb1
+	    }
b7afb1
+
b7afb1
+	  c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
b7afb1
+	  if (c == EOF)
b7afb1
+	    break;
b7afb1
+	  utf8[3] = c;
b7afb1
+
b7afb1
+	  if ((utf8[3] & 0xc0) != 0x80)
b7afb1
+	    {
b7afb1
+	      /* Invalid UTF-8.  */
b7afb1
+	      putback_buf[num_putback++] = utf8[3];
b7afb1
+	      putback_buf[num_putback++] = utf8[2];
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      break;
b7afb1
+	    }
b7afb1
+	  else if (unicode_display == unicode_invalid)
b7afb1
+	    {
b7afb1
+	      putback_buf[num_putback++] = utf8[3];
b7afb1
+	      putback_buf[num_putback++] = utf8[2];
b7afb1
+	      putback_buf[num_putback++] = utf8[1];
b7afb1
+	      break;
b7afb1
+	    }
b7afb1
+	  else
b7afb1
+	    /* A valid 4-byte UTF-8 encoding.  */
b7afb1
+	    (void) display_utf8_char (utf8);
b7afb1
+	}
b7afb1
+      while (1);
b7afb1
+
b7afb1
+      if (output_separator)
b7afb1
+	fputs (output_separator, stdout);
b7afb1
+      else
b7afb1
+	putchar ('\n');
b7afb1
+    }
b7afb1
+
b7afb1
+  if (c != EOF)
b7afb1
+    /* FIXME: Using tail recursion here is lazy, but it works.  */
b7afb1
+    print_unicode_stream_body (filename, address + num_read, stream, putback_buf, num_putback, print_buf);
b7afb1
+}
b7afb1
+
b7afb1
+/* Display strings read in from STREAM.  Treat any UTF-8 encoded characters
b7afb1
+   encountered according to the setting of the unicode_display variable.
b7afb1
+   The stream is positioned at ADDRESS and is attached to FILENAME.  */
b7afb1
+
b7afb1
+static void
b7afb1
+print_unicode_stream (const char * filename,
b7afb1
+		      file_ptr     address,
b7afb1
+		      FILE *       stream)
b7afb1
+{
b7afb1
+  /* Paranoia checks...  */
b7afb1
+  if (filename == NULL
b7afb1
+      || stream == NULL
b7afb1
+      || unicode_display == unicode_default
b7afb1
+      || encoding != 'S'
b7afb1
+      || encoding_bytes != 1)
b7afb1
+    {
b7afb1
+      fprintf (stderr, "ICE: bad arguments to print_unicode_stream\n");
b7afb1
+      return;
b7afb1
+    }
b7afb1
+
b7afb1
+  /* Allocate space for string_min 4-byte utf-8 characters.  */
b7afb1
+  unsigned char * print_buf = xmalloc ((4 * string_min) + 1);
b7afb1
+  /* We should never have to put back more than 4 bytes.  */
b7afb1
+  unsigned char putback_buf[5];
b7afb1
+  unsigned int num_putback = 0;
b7afb1
+
b7afb1
+  print_unicode_stream_body (filename, address, stream, putback_buf, num_putback, print_buf);
b7afb1
+  free (print_buf);
b7afb1
+}
b7afb1
 
b7afb1
 /* Find the strings in file FILENAME, read from STREAM.
b7afb1
    Assume that STREAM is positioned so that the next byte read
b7afb1
    is at address ADDRESS in the file.
b7afb1
-   Stop reading at address STOP_POINT in the file, if nonzero.
b7afb1
 
b7afb1
    If STREAM is NULL, do not read from it.
b7afb1
    The caller can supply a buffer of characters
b7afb1
@@ -570,18 +1238,27 @@ static void
b7afb1
 print_strings (const char *filename, FILE *stream, file_ptr address,
b7afb1
 	       int stop_point, int magiccount, char *magic)
b7afb1
 {
b7afb1
+  if (unicode_display != unicode_default)
b7afb1
+    {
b7afb1
+      if (magic != NULL)
b7afb1
+	print_unicode_buffer (filename, address,
b7afb1
+			      (const unsigned char *) magic, magiccount);
b7afb1
+
b7afb1
+      if (stream != NULL)
b7afb1
+	print_unicode_stream (filename, address, stream);
b7afb1
+      return;
b7afb1
+    }
b7afb1
+
b7afb1
   char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
b7afb1
 
b7afb1
   while (1)
b7afb1
     {
b7afb1
       file_ptr start;
b7afb1
-      int i;
b7afb1
+      unsigned int i;
b7afb1
       long c;
b7afb1
 
b7afb1
       /* See if the next `string_min' chars are all graphic chars.  */
b7afb1
     tryline:
b7afb1
-      if (stop_point && address >= stop_point)
b7afb1
-	break;
b7afb1
       start = address;
b7afb1
       for (i = 0; i < string_min; i++)
b7afb1
 	{
b7afb1
@@ -718,6 +1395,8 @@ usage (FILE *stream, int status)
b7afb1
   -T --target=<BFDNAME>     Specify the binary file format\n\
b7afb1
   -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
b7afb1
                             s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
b7afb1
+  --unicode={default|show|invalid|hex|escape|highlight}\n\
b7afb1
+  -U {d|s|i|x|e|h}          Specify how to treat UTF-8 encoded unicode characters\n\
b7afb1
   -s --output-separator=<string> String used to separate strings in output.\n\
b7afb1
   @<file>                   Read options from <file>\n\
b7afb1
   -h --help                 Display this information\n\