Blame SOURCES/binutils.unicode.patch

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