summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/binutils
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-devtools/binutils')
-rw-r--r--meta/recipes-devtools/binutils/binutils-2.36.inc9
-rw-r--r--meta/recipes-devtools/binutils/binutils/0001-CVE-2021-20197.patch201
-rw-r--r--meta/recipes-devtools/binutils/binutils/0001-CVE-2021-42574.patch2006
-rw-r--r--meta/recipes-devtools/binutils/binutils/0001-CVE-2021-45078.patch255
-rw-r--r--meta/recipes-devtools/binutils/binutils/0002-CVE-2021-20197.patch170
-rw-r--r--meta/recipes-devtools/binutils/binutils/0003-CVE-2021-20197.patch171
-rw-r--r--meta/recipes-devtools/binutils/binutils/0017-CVE-2021-3530.patch102
-rw-r--r--meta/recipes-devtools/binutils/binutils/0018-CVE-2021-3530.patch64
8 files changed, 2432 insertions, 546 deletions
diff --git a/meta/recipes-devtools/binutils/binutils-2.36.inc b/meta/recipes-devtools/binutils/binutils-2.36.inc
index 9d770db5a8..fa28358c2d 100644
--- a/meta/recipes-devtools/binutils/binutils-2.36.inc
+++ b/meta/recipes-devtools/binutils/binutils-2.36.inc
@@ -24,7 +24,7 @@ BRANCH ?= "binutils-2_36-branch"
UPSTREAM_CHECK_GITTAGREGEX = "binutils-(?P<pver>\d+_(\d_?)*)"
-SRCREV ?= "7651a4871c225925ffdfda0a8c91a6ed370cd9a1"
+SRCREV ?= "a281816c8aeb12619d34eec8959a43dfa5c6b4ec"
BINUTILS_GIT_URI ?= "git://sourceware.org/git/binutils-gdb.git;branch=${BRANCH};protocol=git"
SRC_URI = "\
${BINUTILS_GIT_URI} \
@@ -41,8 +41,9 @@ SRC_URI = "\
file://0014-Fix-rpath-in-libtool-when-sysroot-is-enabled.patch \
file://0015-sync-with-OE-libtool-changes.patch \
file://0016-Check-for-clang-before-checking-gcc-version.patch \
- file://0001-CVE-2021-20197.patch \
- file://0002-CVE-2021-20197.patch \
- file://0003-CVE-2021-20197.patch \
+ file://0017-CVE-2021-3530.patch \
+ file://0018-CVE-2021-3530.patch \
+ file://0001-CVE-2021-42574.patch \
+ file://0001-CVE-2021-45078.patch \
"
S = "${WORKDIR}/git"
diff --git a/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-20197.patch b/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-20197.patch
deleted file mode 100644
index 2b4eaba26d..0000000000
--- a/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-20197.patch
+++ /dev/null
@@ -1,201 +0,0 @@
-From 8e03235147a9e774d3ba084e93c2da1aa94d1cec Mon Sep 17 00:00:00 2001
-From: Siddhesh Poyarekar <siddhesh@gotplt.org>
-Date: Mon, 22 Feb 2021 20:45:50 +0530
-Subject: [PATCH] binutils: Avoid renaming over existing files
-
-Renaming over existing files needs additional care to restore
-permissions and ownership, which may not always succeed.
-Additionally, other properties of the file such as extended attributes
-may be lost, making the operation flaky.
-
-For predictable results, resort to rename() only if the file does not
-exist, otherwise copy the file contents into the existing file. This
-ensures that no additional tricks are needed to retain file
-properties.
-
-This also allows dropping of the redundant set_times on the tmpfile in
-objcopy/strip since now we no longer rename over existing files.
-
-binutils/
-
- * ar.c (write_archive): Adjust call to SMART_RENAME.
- * arsup.c (ar_save): Likewise.
- * objcopy (strip_main): Don't set times on temporary file and
- adjust call to SMART_RENAME.
- (copy_main): Likewise.
- * rename.c [!S_ISLNK]: Remove definitions.
- (try_preserve_permissions): Remove function.
- (smart_rename): Replace PRESERVE_DATES argument with
- TARGET_STAT. Use rename system call only if TO does not exist.
- * bucomm.h (smart_rename): Adjust declaration.
-
-(cherry picked from commit 3685de750e6a091663a0abe42528cad29e960e35)
-
-Upstream-Status: Backport [https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8e03235147a9e774d3ba084e93c2da1aa94d1cec]
-CVE: CVE-2021-20197
-Signed-off-by: Vinay Kumar <vinay.m.engg@gmail.com>
----
- binutils/ar.c | 2 +-
- binutils/arsup.c | 2 +-
- binutils/bucomm.h | 3 ++-
- binutils/objcopy.c | 8 ++-----
- binutils/rename.c | 55 +++++++++-------------------------------------
- 6 files changed, 29 insertions(+), 54 deletions(-)
-
-diff --git a/binutils/ar.c b/binutils/ar.c
-index 45a34e3a6cf..3a91708b51c 100644
---- a/binutils/ar.c
-+++ b/binutils/ar.c
-@@ -1308,7 +1308,7 @@ write_archive (bfd *iarch)
- /* We don't care if this fails; we might be creating the archive. */
- bfd_close (iarch);
-
-- if (smart_rename (new_name, old_name, 0) != 0)
-+ if (smart_rename (new_name, old_name, NULL) != 0)
- xexit (1);
- free (old_name);
- free (new_name);
-diff --git a/binutils/arsup.c b/binutils/arsup.c
-index 5403a0c5d74..0a1f63f6456 100644
---- a/binutils/arsup.c
-+++ b/binutils/arsup.c
-@@ -351,7 +351,7 @@ ar_save (void)
-
- bfd_close (obfd);
-
-- smart_rename (ofilename, real_name, 0);
-+ smart_rename (ofilename, real_name, NULL);
- obfd = 0;
- free (ofilename);
- }
-diff --git a/binutils/bucomm.h b/binutils/bucomm.h
-index 91f6a5b228f..aa7e33d8cd1 100644
---- a/binutils/bucomm.h
-+++ b/binutils/bucomm.h
-@@ -71,7 +71,8 @@ extern void print_version (const char *);
- /* In rename.c. */
- extern void set_times (const char *, const struct stat *);
-
--extern int smart_rename (const char *, const char *, int);
-+extern int smart_rename (const char *, const char *, struct stat *);
-+
-
- /* In libiberty. */
- void *xmalloc (size_t);
-diff --git a/binutils/objcopy.c b/binutils/objcopy.c
-index eab3b6db585..07a872b5a80 100644
---- a/binutils/objcopy.c
-+++ b/binutils/objcopy.c
-@@ -4861,12 +4861,10 @@ strip_main (int argc, char *argv[])
- output_target, NULL);
- if (status == 0)
- {
-- if (preserve_dates)
-- set_times (tmpname, &statbuf);
- if (output_file != tmpname)
- status = (smart_rename (tmpname,
- output_file ? output_file : argv[i],
-- preserve_dates) != 0);
-+ preserve_dates ? &statbuf : NULL) != 0);
- if (status == 0)
- status = hold_status;
- }
-@@ -5931,11 +5929,9 @@ copy_main (int argc, char *argv[])
- output_target, input_arch);
- if (status == 0)
- {
-- if (preserve_dates)
-- set_times (tmpname, &statbuf);
- if (tmpname != output_filename)
- status = (smart_rename (tmpname, input_filename,
-- preserve_dates) != 0);
-+ preserve_dates ? &statbuf : NULL) != 0);
- }
- else
- unlink_if_ordinary (tmpname);
-diff --git a/binutils/rename.c b/binutils/rename.c
-index 65ad5bf52c4..f471b45fd3f 100644
---- a/binutils/rename.c
-+++ b/binutils/rename.c
-@@ -122,20 +122,13 @@ set_times (const char *destination, const struct stat *statbuf)
- non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
- }
-
--#ifndef S_ISLNK
--#ifdef S_IFLNK
--#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
--#else
--#define S_ISLNK(m) 0
--#define lstat stat
--#endif
--#endif
--
--/* Rename FROM to TO, copying if TO is a link.
-- Return 0 if ok, -1 if error. */
-+/* Rename FROM to TO, copying if TO exists. TARGET_STAT has the file status
-+ that, if non-NULL, is used to fix up timestamps after rename. Return 0 if
-+ ok, -1 if error. */
-
- int
--smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
-+smart_rename (const char *from, const char *to,
-+ struct stat *target_stat ATTRIBUTE_UNUSED)
- {
- bfd_boolean exists;
- struct stat s;
-@@ -158,38 +151,10 @@ smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNU
- unlink (from);
- }
- #else
-- /* Use rename only if TO is not a symbolic link and has
-- only one hard link, and we have permission to write to it. */
-- if (! exists
-- || (!S_ISLNK (s.st_mode)
-- && S_ISREG (s.st_mode)
-- && (s.st_mode & S_IWUSR)
-- && s.st_nlink == 1)
-- )
-+ /* Avoid a full copy and use rename if TO does not exist. */
-+ if (!exists)
- {
-- ret = rename (from, to);
-- if (ret == 0)
-- {
-- if (exists)
-- {
-- /* Try to preserve the permission bits and ownership of
-- TO. First get the mode right except for the setuid
-- bit. Then change the ownership. Then fix the setuid
-- bit. We do the chmod before the chown because if the
-- chown succeeds, and we are a normal user, we won't be
-- able to do the chmod afterward. We don't bother to
-- fix the setuid bit first because that might introduce
-- a fleeting security problem, and because the chown
-- will clear the setuid bit anyhow. We only fix the
-- setuid bit if the chown succeeds, because we don't
-- want to introduce an unexpected setuid file owned by
-- the user running objcopy. */
-- chmod (to, s.st_mode & 0777);
-- if (chown (to, s.st_uid, s.st_gid) >= 0)
-- chmod (to, s.st_mode & 07777);
-- }
-- }
-- else
-+ if ((ret = rename (from, to)) != 0)
- {
- /* We have to clean up here. */
- non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
-@@ -202,8 +167,8 @@ smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNU
- if (ret != 0)
- non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
-
-- if (preserve_dates)
-- set_times (to, &s);
-+ if (target_stat != NULL)
-+ set_times (to, target_stat);
- unlink (from);
- }
- #endif /* _WIN32 && !__CYGWIN32__ */
---
-2.31.1
-
diff --git a/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-42574.patch b/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-42574.patch
new file mode 100644
index 0000000000..6adc438b5c
--- /dev/null
+++ b/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-42574.patch
@@ -0,0 +1,2006 @@
+From b3aa80b45c4f46029efeb204bb9f2d2c4278a0e5 Mon Sep 17 00:00:00 2001
+From: Nick Clifton <nickc@redhat.com>
+Date: Tue, 9 Nov 2021 13:25:42 +0000
+Subject: [PATCH] Add --unicode option to control how unicode characters are
+ handled by display tools.
+
+ * nm.c: Add --unicode option to control how unicode characters are
+ handled.
+ * objdump.c: Likewise.
+ * readelf.c: Likewise.
+ * strings.c: Likewise.
+ * binutils.texi: Document the new feature.
+ * NEWS: Document the new feature.
+ * testsuite/binutils-all/unicode.exp: New file.
+ * testsuite/binutils-all/nm.hex.unicode
+ * testsuite/binutils-all/strings.escape.unicode
+ * testsuite/binutils-all/objdump.highlight.unicode
+ * testsuite/binutils-all/readelf.invalid.unicode
+
+CVE: CVE-2021-42574
+Upstream-Status: Backport [https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=b3aa80b45c4f46029efeb204bb9f2d2c4278a0e5]
+
+Signed-off-by: pgowda <pgowda.cve@gmail.com>
+---
+ binutils/ChangeLog | 15 +
+ binutils/NEWS | 9 +
+ binutils/doc/binutils.texi | 78 ++++
+ binutils/nm.c | 228 ++++++++++-
+ binutils/objdump.c | 235 ++++++++++--
+ binutils/readelf.c | 190 +++++++++-
+ binutils/strings.c | 757 ++++++++++++++++++++++++++++++++++---
+ 7 files changed, 1409 insertions(+), 103 deletions(-)
+
+diff --git a/binutils/ChangeLog b/binutils/ChangeLog
+--- a/binutils/ChangeLog 2021-12-23 03:23:38.425843662 -0800
++++ b/binutils/ChangeLog 2021-12-23 03:30:31.094968942 -0800
+@@ -1,3 +1,18 @@
++2021-11-09 Nick Clifton <nickc@redhat.com>
++
++ * nm.c: Add --unicode option to control how unicode characters are
++ handled.
++ * objdump.c: Likewise.
++ * readelf.c: Likewise.
++ * strings.c: Likewise.
++ * binutils.texi: Document the new feature.
++ * NEWS: Document the new feature.
++ * testsuite/binutils-all/unicode.exp: New file.
++ * testsuite/binutils-all/nm.hex.unicode
++ * testsuite/binutils-all/strings.escape.unicode
++ * testsuite/binutils-all/objdump.highlight.unicode
++ * testsuite/binutils-all/readelf.invalid.unicode
++
+ 2021-04-15 Alan Modra <amodra@gmail.com>
+
+ PR 27456
+diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
+--- a/binutils/doc/binutils.texi 2021-12-23 03:23:38.441843395 -0800
++++ b/binutils/doc/binutils.texi 2021-12-23 03:30:31.094968942 -0800
+@@ -805,6 +805,7 @@ nm [@option{-A}|@option{-o}|@option{--pr
+ [@option{-s}|@option{--print-armap}] [@option{-t} @var{radix}|@option{--radix=}@var{radix}]
+ [@option{-u}|@option{--undefined-only}] [@option{-V}|@option{--version}]
+ [@option{-X 32_64}] [@option{--defined-only}] [@option{--no-demangle}]
++ [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
+ [@option{--plugin} @var{name}]
+ [@option{--no-recurse-limit}|@option{--recurse-limit}]]
+ [@option{--size-sort}] [@option{--special-syms}]
+@@ -1114,6 +1115,21 @@ Use @var{radix} as the radix for printin
+ @cindex undefined symbols
+ Display only undefined symbols (those external to each object file).
+
++@item -U @var{[d|i|l|e|x|h]}
++@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
++Controls the display of UTF-8 encoded mulibyte characters in strings.
++The default (@option{--unicode=default}) is to give them no special
++treatment. The @option{--unicode=locale} option displays the sequence
++in the current locale, which may or may not support them. The options
++@option{--unicode=hex} and @option{--unicode=invalid} display them as
++hex byte sequences enclosed by either angle brackets or curly braces.
++
++The @option{--unicode=escape} option displays them as escape sequences
++(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
++them as escape sequences highlighted in red (if supported by the
++output device). The colouring is intended to draw attention to the
++presence of unicode sequences where they might not be expected.
++
+ @item -V
+ @itemx --version
+ Show the version number of @command{nm} and exit.
+@@ -2210,6 +2226,7 @@ objdump [@option{-a}|@option{--archive-h
+ [@option{--prefix-strip=}@var{level}]
+ [@option{--insn-width=}@var{width}]
+ [@option{--visualize-jumps[=color|=extended-color|=off]}
++ [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
+ [@option{-V}|@option{--version}]
+ [@option{-H}|@option{--help}]
+ @var{objfile}@dots{}
+@@ -2877,6 +2894,21 @@ When displaying symbols include those wh
+ special in some way and which would not normally be of interest to the
+ user.
+
++@item -U @var{[d|i|l|e|x|h]}
++@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
++Controls the display of UTF-8 encoded mulibyte characters in strings.
++The default (@option{--unicode=default}) is to give them no special
++treatment. The @option{--unicode=locale} option displays the sequence
++in the current locale, which may or may not support them. The options
++@option{--unicode=hex} and @option{--unicode=invalid} display them as
++hex byte sequences enclosed by either angle brackets or curly braces.
++
++The @option{--unicode=escape} option displays them as escape sequences
++(@var{\uxxxx}) and the @option{--unicode=highlight} option displays
++them as escape sequences highlighted in red (if supported by the
++output device). The colouring is intended to draw attention to the
++presence of unicode sequences where they might not be expected.
++
+ @item -V
+ @itemx --version
+ Print the version number of @command{objdump} and exit.
+@@ -3153,6 +3185,7 @@ strings [@option{-afovV}] [@option{-}@va
+ [@option{-n} @var{min-len}] [@option{--bytes=}@var{min-len}]
+ [@option{-t} @var{radix}] [@option{--radix=}@var{radix}]
+ [@option{-e} @var{encoding}] [@option{--encoding=}@var{encoding}]
++ [@option{-U} @var{method}] [@option{--unicode=}@var{method}]
+ [@option{-}] [@option{--all}] [@option{--print-file-name}]
+ [@option{-T} @var{bfdname}] [@option{--target=}@var{bfdname}]
+ [@option{-w}] [@option{--include-all-whitespace}]
+@@ -3244,6 +3277,28 @@ single-8-bit-byte characters, @samp{b} =
+ littleendian. Useful for finding wide character strings. (@samp{l}
+ and @samp{b} apply to, for example, Unicode UTF-16/UCS-2 encodings).
+
++@item -U @var{[d|i|l|e|x|h]}
++@itemx --unicode=@var{[default|invalid|locale|escape|hex|highlight]}
++Controls the display of UTF-8 encoded mulibyte characters in strings.
++The default (@option{--unicode=default}) is to give them no special
++treatment, and instead rely upon the setting of the
++@option{--encoding} option. The other values for this option
++automatically enable @option{--encoding=S}.
++
++The @option{--unicode=invalid} option treats them as non-graphic
++characters and hence not part of a valid string. All the remaining
++options treat them as valid string characters.
++
++The @option{--unicode=locale} option displays them in the current
++locale, which may or may not support UTF-8 encoding. The
++@option{--unicode=hex} option displays them as hex byte sequences
++enclosed between @var{<>} characters. The @option{--unicode=escape}
++option displays them as escape sequences (@var{\uxxxx}) and the
++@option{--unicode=highlight} option displays them as escape sequences
++highlighted in red (if supported by the output device). The colouring
++is intended to draw attention to the presence of unicode sequences
++where they might not be expected.
++
+ @item -T @var{bfdname}
+ @itemx --target=@var{bfdname}
+ @cindex object code format
+@@ -4740,6 +4795,7 @@ readelf [@option{-a}|@option{--all}]
+ [@option{--dyn-syms}|@option{--lto-syms}]
+ [@option{--demangle@var{=style}}|@option{--no-demangle}]
+ [@option{--recurse-limit}|@option{--no-recurse-limit}]
++ [@option{-U} @var{method}|@option{--unicode=}@var{method}]
+ [@option{-n}|@option{--notes}]
+ [@option{-r}|@option{--relocs}]
+ [@option{-u}|@option{--unwind}]
+@@ -4887,6 +4943,28 @@ necessary in order to demangle truly com
+ that if the recursion limit is disabled then stack exhaustion is
+ possible and any bug reports about such an event will be rejected.
+
++@item -U @var{[d|i|l|e|x|h]}
++@itemx --unicode=[default|invalid|locale|escape|hex|highlight]
++Controls the display of non-ASCII characters in identifier names.
++The default (@option{--unicode=locale} or @option{--unicode=default}) is
++to treat them as multibyte characters and display them in the current
++locale. All other versions of this option treat the bytes as UTF-8
++encoded values and attempt to interpret them. If they cannot be
++interpreted or if the @option{--unicode=invalid} option is used then
++they are displayed as a sequence of hex bytes, encloses in curly
++parethesis characters.
++
++Using the @option{--unicode=escape} option will display the characters
++as as unicode escape sequences (@var{\uxxxx}). Using the
++@option{--unicode=hex} will display the characters as hex byte
++sequences enclosed between angle brackets.
++
++Using the @option{--unicode=highlight} will display the characters as
++unicode escape sequences but it will also highlighted them in red,
++assuming that colouring is supported by the output device. The
++colouring is intended to draw attention to the presence of unicode
++sequences when they might not be expected.
++
+ @item -e
+ @itemx --headers
+ Display all the headers in the file. Equivalent to @option{-h -l -S}.
+diff --git a/binutils/NEWS b/binutils/NEWS
+--- a/binutils/NEWS 2021-12-23 03:23:38.433843528 -0800
++++ b/binutils/NEWS 2021-12-23 03:30:31.094968942 -0800
+@@ -1,5 +1,14 @@
+ -*- text -*-
+
++* Tools which display symbols or strings (readelf, strings, nm, objdump)
++ have a new command line option which controls how unicode characters are
++ handled. By default they are treated as normal for the tool. Using
++ --unicode=locale will display them according to the current locale.
++ Using --unicode=hex will display them as hex byte values, whilst
++ --unicode=escape will display them as escape sequences. In addition
++ using --unicode=highlight will display them as unicode escape sequences
++ highlighted in red (if supported by the output device).
++
+ Changes in 2.36:
+
+ * Update elfedit and readelf with LAM_U48 and LAM_U57 support.
+diff --git a/binutils/nm.c b/binutils/nm.c
+--- a/binutils/nm.c 2021-12-23 03:23:38.441843395 -0800
++++ b/binutils/nm.c 2021-12-23 03:30:31.098968875 -0800
+@@ -38,6 +38,11 @@
+ #include "bucomm.h"
+ #include "plugin-api.h"
+ #include "plugin.h"
++#include "safe-ctype.h"
++
++#ifndef streq
++#define streq(a,b) (strcmp ((a),(b)) == 0)
++#endif
+
+ /* When sorting by size, we use this structure to hold the size and a
+ pointer to the minisymbol. */
+@@ -192,6 +197,18 @@ static const char *plugin_target = NULL;
+ static bfd *lineno_cache_bfd;
+ static bfd *lineno_cache_rel_bfd;
+
++typedef enum unicode_display_type
++{
++ unicode_default = 0,
++ unicode_locale,
++ unicode_escape,
++ unicode_hex,
++ unicode_highlight,
++ unicode_invalid
++} unicode_display_type;
++
++static unicode_display_type unicode_display = unicode_default;
++
+ enum long_option_values
+ {
+ OPTION_TARGET = 200,
+@@ -234,6 +251,7 @@ static struct option long_options[] =
+ {"target", required_argument, 0, OPTION_TARGET},
+ {"defined-only", no_argument, &defined_only, 1},
+ {"undefined-only", no_argument, &undefined_only, 1},
++ {"unicode", required_argument, NULL, 'U'},
+ {"version", no_argument, &show_version, 1},
+ {"with-symbol-versions", no_argument, NULL,
+ OPTION_WITH_SYMBOL_VERSIONS},
+@@ -285,6 +303,8 @@ usage (FILE *stream, int status)
+ -t, --radix=RADIX Use RADIX for printing symbol values\n\
+ --target=BFDNAME Specify the target object format as BFDNAME\n\
+ -u, --undefined-only Display only undefined symbols\n\
++ -U {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
++ --unicode={default|show|invalid|hex|escape|highlight}\n\
+ --with-symbol-versions Display version strings after symbol names\n\
+ -X 32_64 (ignored)\n\
+ @FILE Read options from FILE\n\
+@@ -399,6 +419,188 @@ get_coff_symbol_type (const struct inter
+ fatal ("%s", xstrerror (errno));
+ return bufp;
+ }
++
++/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
++ The conversion format is controlled by the unicode_display variable.
++ Returns the number of characters added to OUT.
++ Returns the number of bytes consumed from IN in CONSUMED.
++ Always consumes at least one byte and displays at least one character. */
++
++static unsigned int
++display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
++{
++ char * orig_out = out;
++ unsigned int nchars = 0;
++ unsigned int j;
++
++ if (unicode_display == unicode_default)
++ goto invalid;
++
++ if (in[0] < 0xc0)
++ goto invalid;
++
++ if ((in[1] & 0xc0) != 0x80)
++ goto invalid;
++
++ if ((in[0] & 0x20) == 0)
++ {
++ nchars = 2;
++ goto valid;
++ }
++
++ if ((in[2] & 0xc0) != 0x80)
++ goto invalid;
++
++ if ((in[0] & 0x10) == 0)
++ {
++ nchars = 3;
++ goto valid;
++ }
++
++ if ((in[3] & 0xc0) != 0x80)
++ goto invalid;
++
++ nchars = 4;
++
++ valid:
++ switch (unicode_display)
++ {
++ case unicode_locale:
++ /* Copy the bytes into the output buffer as is. */
++ memcpy (out, in, nchars);
++ out += nchars;
++ break;
++
++ case unicode_invalid:
++ case unicode_hex:
++ out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
++ out += sprintf (out, "0x");
++ for (j = 0; j < nchars; j++)
++ out += sprintf (out, "%02x", in [j]);
++ out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
++ break;
++
++ case unicode_highlight:
++ if (isatty (1))
++ out += sprintf (out, "\x1B[31;47m"); /* Red. */
++ /* Fall through. */
++ case unicode_escape:
++ switch (nchars)
++ {
++ case 2:
++ out += sprintf (out, "\\u%02x%02x",
++ ((in[0] & 0x1c) >> 2),
++ ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
++ break;
++
++ case 3:
++ out += sprintf (out, "\\u%02x%02x",
++ ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
++ ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
++ break;
++
++ case 4:
++ out += sprintf (out, "\\u%02x%02x%02x",
++ ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
++ ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
++ ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
++ break;
++ default:
++ /* URG. */
++ break;
++ }
++
++ if (unicode_display == unicode_highlight && isatty (1))
++ out += sprintf (out, "\033[0m"); /* Default colour. */
++ break;
++
++ default:
++ /* URG */
++ break;
++ }
++
++ * consumed = nchars;
++ return out - orig_out;
++
++ invalid:
++ /* Not a valid UTF-8 sequence. */
++ *out = *in;
++ * consumed = 1;
++ return 1;
++}
++
++/* Convert any UTF-8 encoded characters in NAME into the form specified by
++ unicode_display. Also converts control characters. Returns a static
++ buffer if conversion was necessary.
++ Code stolen from objdump.c:sanitize_string(). */
++
++static const char *
++convert_utf8 (const char * in)
++{
++ static char * buffer = NULL;
++ static size_t buffer_len = 0;
++ const char * original = in;
++ char * out;
++
++ /* Paranoia. */
++ if (in == NULL)
++ return "";
++
++ /* See if any conversion is necessary.
++ In the majority of cases it will not be needed. */
++ do
++ {
++ unsigned char c = *in++;
++
++ if (c == 0)
++ return original;
++
++ if (ISCNTRL (c))
++ break;
++
++ if (unicode_display != unicode_default && c >= 0xc0)
++ break;
++ }
++ while (1);
++
++ /* Copy the input, translating as needed. */
++ in = original;
++ if (buffer_len < (strlen (in) * 9))
++ {
++ free ((void *) buffer);
++ buffer_len = strlen (in) * 9;
++ buffer = xmalloc (buffer_len + 1);
++ }
++
++ out = buffer;
++ do
++ {
++ unsigned char c = *in++;
++
++ if (c == 0)
++ break;
++
++ if (ISCNTRL (c))
++ {
++ *out++ = '^';
++ *out++ = c + 0x40;
++ }
++ else if (unicode_display != unicode_default && c >= 0xc0)
++ {
++ unsigned int num_consumed;
++
++ out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
++ in += num_consumed - 1;
++ }
++ else
++ *out++ = c;
++ }
++ while (1);
++
++ *out = 0;
++ return buffer;
++}
++
+
+ /* Print symbol name NAME, read from ABFD, with printf format FORM,
+ demangling it if requested. */
+@@ -418,6 +620,11 @@ print_symname (const char *form, struct
+ name = alloc;
+ }
+
++ if (unicode_display != unicode_default)
++ {
++ name = convert_utf8 (name);
++ }
++
+ if (info != NULL && info->elfinfo)
+ {
+ const char *version_string;
+@@ -1738,7 +1945,7 @@ main (int argc, char **argv)
+ fatal (_("fatal error: libbfd ABI mismatch"));
+ set_default_bfd_target ();
+
+- while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
++ while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uU:vVvX:",
+ long_options, (int *) 0)) != EOF)
+ {
+ switch (c)
+@@ -1828,6 +2035,24 @@ main (int argc, char **argv)
+ case 'u':
+ undefined_only = 1;
+ break;
++
++ case 'U':
++ if (streq (optarg, "default") || streq (optarg, "d"))
++ unicode_display = unicode_default;
++ else if (streq (optarg, "locale") || streq (optarg, "l"))
++ unicode_display = unicode_locale;
++ else if (streq (optarg, "escape") || streq (optarg, "e"))
++ unicode_display = unicode_escape;
++ else if (streq (optarg, "invalid") || streq (optarg, "i"))
++ unicode_display = unicode_invalid;
++ else if (streq (optarg, "hex") || streq (optarg, "x"))
++ unicode_display = unicode_hex;
++ else if (streq (optarg, "highlight") || streq (optarg, "h"))
++ unicode_display = unicode_highlight;
++ else
++ fatal (_("invalid argument to -U/--unicode: %s"), optarg);
++ break;
++
+ case 'V':
+ show_version = 1;
+ break;
+diff --git a/binutils/objdump.c b/binutils/objdump.c
+--- a/binutils/objdump.c 2021-12-23 03:23:38.445843329 -0800
++++ b/binutils/objdump.c 2021-12-23 03:30:31.098968875 -0800
+@@ -205,6 +205,19 @@ static const struct objdump_private_desc
+
+ /* The list of detected jumps inside a function. */
+ static struct jump_info *detected_jumps = NULL;
++
++typedef enum unicode_display_type
++{
++ unicode_default = 0,
++ unicode_locale,
++ unicode_escape,
++ unicode_hex,
++ unicode_highlight,
++ unicode_invalid
++} unicode_display_type;
++
++static unicode_display_type unicode_display = unicode_default;
++
+
+ static void usage (FILE *, int) ATTRIBUTE_NORETURN;
+ static void
+@@ -274,6 +287,8 @@ usage (FILE *stream, int status)
+ --recurse-limit Enable a limit on recursion whilst demangling. [Default]\n\
+ --no-recurse-limit Disable a limit on recursion whilst demangling\n\
+ -w, --wide Format output for more than 80 columns\n\
++ -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
++ --unicode=[default|locale|invalid|hex|escape|highlight]\n\
+ -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
+ --start-address=ADDR Only process data whose address is >= ADDR\n\
+ --stop-address=ADDR Only process data whose address is < ADDR\n\
+@@ -348,17 +363,23 @@ static struct option long_options[]=
+ {
+ {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
+ {"all-headers", no_argument, NULL, 'x'},
+- {"private-headers", no_argument, NULL, 'p'},
+- {"private", required_argument, NULL, 'P'},
+ {"architecture", required_argument, NULL, 'm'},
+ {"archive-headers", no_argument, NULL, 'a'},
++#ifdef ENABLE_LIBCTF
++ {"ctf", required_argument, NULL, OPTION_CTF},
++ {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
++#endif
+ {"debugging", no_argument, NULL, 'g'},
+ {"debugging-tags", no_argument, NULL, 'e'},
+ {"demangle", optional_argument, NULL, 'C'},
+ {"disassemble", optional_argument, NULL, 'd'},
+ {"disassemble-all", no_argument, NULL, 'D'},
+- {"disassembler-options", required_argument, NULL, 'M'},
+ {"disassemble-zeroes", no_argument, NULL, 'z'},
++ {"disassembler-options", required_argument, NULL, 'M'},
++ {"dwarf", optional_argument, NULL, OPTION_DWARF},
++ {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
++ {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
++ {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
+ {"dynamic-reloc", no_argument, NULL, 'R'},
+ {"dynamic-syms", no_argument, NULL, 'T'},
+ {"endian", required_argument, NULL, OPTION_ENDIAN},
+@@ -368,15 +389,22 @@ static struct option long_options[]=
+ {"full-contents", no_argument, NULL, 's'},
+ {"headers", no_argument, NULL, 'h'},
+ {"help", no_argument, NULL, 'H'},
++ {"include", required_argument, NULL, 'I'},
+ {"info", no_argument, NULL, 'i'},
++ {"inlines", no_argument, 0, OPTION_INLINES},
++ {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
+ {"line-numbers", no_argument, NULL, 'l'},
+- {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
+ {"no-addresses", no_argument, &no_addresses, 1},
++ {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
++ {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
++ {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
++ {"prefix", required_argument, NULL, OPTION_PREFIX},
+ {"prefix-addresses", no_argument, &prefix_addresses, 1},
++ {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
++ {"private", required_argument, NULL, 'P'},
++ {"private-headers", no_argument, NULL, 'p'},
+ {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
+ {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
+- {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
+- {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
+ {"reloc", no_argument, NULL, 'r'},
+ {"section", required_argument, NULL, 'j'},
+ {"section-headers", no_argument, NULL, 'h'},
+@@ -384,28 +412,16 @@ static struct option long_options[]=
+ {"source", no_argument, NULL, 'S'},
+ {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
+ {"special-syms", no_argument, &dump_special_syms, 1},
+- {"include", required_argument, NULL, 'I'},
+- {"dwarf", optional_argument, NULL, OPTION_DWARF},
+-#ifdef ENABLE_LIBCTF
+- {"ctf", required_argument, NULL, OPTION_CTF},
+- {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
+-#endif
+ {"stabs", no_argument, NULL, 'G'},
+ {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
+ {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
+ {"syms", no_argument, NULL, 't'},
+ {"target", required_argument, NULL, 'b'},
++ {"unicode", required_argument, NULL, 'U'},
+ {"version", no_argument, NULL, 'V'},
+- {"wide", no_argument, NULL, 'w'},
+- {"prefix", required_argument, NULL, OPTION_PREFIX},
+- {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
+- {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
+- {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
+- {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
+- {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
+- {"inlines", no_argument, 0, OPTION_INLINES},
+ {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
+- {0, no_argument, 0, 0}
++ {"wide", no_argument, NULL, 'w'},
++ {NULL, no_argument, NULL, 0}
+ };
+
+ static void
+@@ -415,9 +431,121 @@ nonfatal (const char *msg)
+ exit_status = 1;
+ }
+
++/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
++ The conversion format is controlled by the unicode_display variable.
++ Returns the number of characters added to OUT.
++ Returns the number of bytes consumed from IN in CONSUMED.
++ Always consumes at least one byte and displays at least one character. */
++
++static unsigned int
++display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
++{
++ char * orig_out = out;
++ unsigned int nchars = 0;
++ unsigned int j;
++
++ if (unicode_display == unicode_default)
++ goto invalid;
++
++ if (in[0] < 0xc0)
++ goto invalid;
++
++ if ((in[1] & 0xc0) != 0x80)
++ goto invalid;
++
++ if ((in[0] & 0x20) == 0)
++ {
++ nchars = 2;
++ goto valid;
++ }
++
++ if ((in[2] & 0xc0) != 0x80)
++ goto invalid;
++
++ if ((in[0] & 0x10) == 0)
++ {
++ nchars = 3;
++ goto valid;
++ }
++
++ if ((in[3] & 0xc0) != 0x80)
++ goto invalid;
++
++ nchars = 4;
++
++ valid:
++ switch (unicode_display)
++ {
++ case unicode_locale:
++ /* Copy the bytes into the output buffer as is. */
++ memcpy (out, in, nchars);
++ out += nchars;
++ break;
++
++ case unicode_invalid:
++ case unicode_hex:
++ out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
++ out += sprintf (out, "0x");
++ for (j = 0; j < nchars; j++)
++ out += sprintf (out, "%02x", in [j]);
++ out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
++ break;
++
++ case unicode_highlight:
++ if (isatty (1))
++ out += sprintf (out, "\x1B[31;47m"); /* Red. */
++ /* Fall through. */
++ case unicode_escape:
++ switch (nchars)
++ {
++ case 2:
++ out += sprintf (out, "\\u%02x%02x",
++ ((in[0] & 0x1c) >> 2),
++ ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
++ break;
++
++ case 3:
++ out += sprintf (out, "\\u%02x%02x",
++ ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
++ ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
++ break;
++
++ case 4:
++ out += sprintf (out, "\\u%02x%02x%02x",
++ ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
++ ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
++ ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
++ break;
++ default:
++ /* URG. */
++ break;
++ }
++
++ if (unicode_display == unicode_highlight && isatty (1))
++ out += sprintf (out, "\033[0m"); /* Default colour. */
++ break;
++
++ default:
++ /* URG */
++ break;
++ }
++
++ * consumed = nchars;
++ return out - orig_out;
++
++ invalid:
++ /* Not a valid UTF-8 sequence. */
++ *out = *in;
++ * consumed = 1;
++ return 1;
++}
++
+ /* Returns a version of IN with any control characters
+ replaced by escape sequences. Uses a static buffer
+- if necessary. */
++ if necessary.
++
++ If unicode display is enabled, then also handles the
++ conversion of unicode characters. */
+
+ static const char *
+ sanitize_string (const char * in)
+@@ -435,40 +563,50 @@ sanitize_string (const char * in)
+ of cases it will not be needed. */
+ do
+ {
+- char c = *in++;
++ unsigned char c = *in++;
+
+ if (c == 0)
+ return original;
+
+ if (ISCNTRL (c))
+ break;
++
++ if (unicode_display != unicode_default && c >= 0xc0)
++ break;
+ }
+ while (1);
+
+ /* Copy the input, translating as needed. */
+ in = original;
+- if (buffer_len < (strlen (in) * 2))
++ if (buffer_len < (strlen (in) * 9))
+ {
+ free ((void *) buffer);
+- buffer_len = strlen (in) * 2;
++ buffer_len = strlen (in) * 9;
+ buffer = xmalloc (buffer_len + 1);
+ }
+
+ out = buffer;
+ do
+ {
+- char c = *in++;
++ unsigned char c = *in++;
+
+ if (c == 0)
+ break;
+
+- if (!ISCNTRL (c))
+- *out++ = c;
+- else
++ if (ISCNTRL (c))
+ {
+ *out++ = '^';
+ *out++ = c + 0x40;
+ }
++ else if (unicode_display != unicode_default && c >= 0xc0)
++ {
++ unsigned int num_consumed;
++
++ out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
++ in += num_consumed - 1;
++ }
++ else
++ *out++ = c;
+ }
+ while (1);
+
+@@ -4481,6 +4619,24 @@ dump_symbols (bfd *abfd ATTRIBUTE_UNUSED
+ free (alloc);
+ }
+ }
++ else if (unicode_display != unicode_default
++ && name != NULL && *name != '\0')
++ {
++ const char * sanitized_name;
++
++ /* If we want to sanitize the name, we do it here, and
++ temporarily clobber it while calling bfd_print_symbol.
++ FIXME: This is a gross hack. */
++ sanitized_name = sanitize_string (name);
++ if (sanitized_name != name)
++ (*current)->name = sanitized_name;
++ else
++ sanitized_name = NULL;
++ bfd_print_symbol (cur_bfd, stdout, *current,
++ bfd_print_symbol_all);
++ if (sanitized_name != NULL)
++ (*current)->name = name;
++ }
+ else
+ bfd_print_symbol (cur_bfd, stdout, *current,
+ bfd_print_symbol_all);
+@@ -5162,7 +5318,7 @@ main (int argc, char **argv)
+ set_default_bfd_target ();
+
+ while ((c = getopt_long (argc, argv,
+- "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
++ "pP:ib:m:U:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
+ long_options, (int *) 0))
+ != EOF)
+ {
+@@ -5441,6 +5597,23 @@ main (int argc, char **argv)
+ seenflag = TRUE;
+ break;
+
++ case 'U':
++ if (streq (optarg, "default") || streq (optarg, "d"))
++ unicode_display = unicode_default;
++ else if (streq (optarg, "locale") || streq (optarg, "l"))
++ unicode_display = unicode_locale;
++ else if (streq (optarg, "escape") || streq (optarg, "e"))
++ unicode_display = unicode_escape;
++ else if (streq (optarg, "invalid") || streq (optarg, "i"))
++ unicode_display = unicode_invalid;
++ else if (streq (optarg, "hex") || streq (optarg, "x"))
++ unicode_display = unicode_hex;
++ else if (streq (optarg, "highlight") || streq (optarg, "h"))
++ unicode_display = unicode_highlight;
++ else
++ fatal (_("invalid argument to -U/--unicode: %s"), optarg);
++ break;
++
+ case 'H':
+ usage (stdout, 0);
+ /* No need to set seenflag or to break - usage() does not return. */
+diff --git a/binutils/readelf.c b/binutils/readelf.c
+--- a/binutils/readelf.c 2021-12-23 03:23:42.065783023 -0800
++++ b/binutils/readelf.c 2021-12-23 03:30:36.582877519 -0800
+@@ -321,6 +321,19 @@ typedef enum print_mode
+ }
+ print_mode;
+
++typedef enum unicode_display_type
++{
++ unicode_default = 0,
++ unicode_locale,
++ unicode_escape,
++ unicode_hex,
++ unicode_highlight,
++ unicode_invalid
++} unicode_display_type;
++
++static unicode_display_type unicode_display = unicode_default;
++
++
+ /* Versioned symbol info. */
+ enum versioned_symbol_info
+ {
+@@ -613,11 +626,18 @@ print_symbol (signed int width, const ch
+ if (c == 0)
+ break;
+
+- /* Do not print control characters directly as they can affect terminal
+- settings. Such characters usually appear in the names generated
+- by the assembler for local labels. */
+- if (ISCNTRL (c))
++ if (ISPRINT (c))
++ {
++ putchar (c);
++ width_remaining --;
++ num_printed ++;
++ }
++ else if (ISCNTRL (c))
+ {
++ /* Do not print control characters directly as they can affect terminal
++ settings. Such characters usually appear in the names generated
++ by the assembler for local labels. */
++
+ if (width_remaining < 2)
+ break;
+
+@@ -625,11 +645,137 @@ print_symbol (signed int width, const ch
+ width_remaining -= 2;
+ num_printed += 2;
+ }
+- else if (ISPRINT (c))
++ else if (c == 0x7f)
+ {
+- putchar (c);
+- width_remaining --;
+- num_printed ++;
++ if (width_remaining < 5)
++ break;
++ printf ("<DEL>");
++ width_remaining -= 5;
++ num_printed += 5;
++ }
++ else if (unicode_display != unicode_locale
++ && unicode_display != unicode_default)
++ {
++ /* Display unicode characters as something else. */
++ unsigned char bytes[4];
++ bfd_boolean is_utf8;
++ unsigned int nbytes;
++
++ bytes[0] = c;
++
++ if (bytes[0] < 0xc0)
++ {
++ nbytes = 1;
++ is_utf8 = FALSE;
++ }
++ else
++ {
++ bytes[1] = *symbol++;
++
++ if ((bytes[1] & 0xc0) != 0x80)
++ {
++ is_utf8 = FALSE;
++ /* Do not consume this character. It may only
++ be the first byte in the sequence that was
++ corrupt. */
++ --symbol;
++ nbytes = 1;
++ }
++ else if ((bytes[0] & 0x20) == 0)
++ {
++ is_utf8 = TRUE;
++ nbytes = 2;
++ }
++ else
++ {
++ bytes[2] = *symbol++;
++
++ if ((bytes[2] & 0xc0) != 0x80)
++ {
++ is_utf8 = FALSE;
++ symbol -= 2;
++ nbytes = 1;
++ }
++ else if ((bytes[0] & 0x10) == 0)
++ {
++ is_utf8 = TRUE;
++ nbytes = 3;
++ }
++ else
++ {
++ bytes[3] = *symbol++;
++
++ nbytes = 4;
++
++ if ((bytes[3] & 0xc0) != 0x80)
++ {
++ is_utf8 = FALSE;
++ symbol -= 3;
++ nbytes = 1;
++ }
++ else
++ is_utf8 = TRUE;
++ }
++ }
++ }
++
++ if (unicode_display == unicode_invalid)
++ is_utf8 = FALSE;
++
++ if (unicode_display == unicode_hex || ! is_utf8)
++ {
++ unsigned int i;
++
++ if (width_remaining < (nbytes * 2) + 2)
++ break;
++
++ putchar (is_utf8 ? '<' : '{');
++ printf ("0x");
++ for (i = 0; i < nbytes; i++)
++ printf ("%02x", bytes[i]);
++ putchar (is_utf8 ? '>' : '}');
++ }
++ else
++ {
++ if (unicode_display == unicode_highlight && isatty (1))
++ printf ("\x1B[31;47m"); /* Red. */
++
++ switch (nbytes)
++ {
++ case 2:
++ if (width_remaining < 6)
++ break;
++ printf ("\\u%02x%02x",
++ (bytes[0] & 0x1c) >> 2,
++ ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
++ break;
++ case 3:
++ if (width_remaining < 6)
++ break;
++ printf ("\\u%02x%02x",
++ ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
++ ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
++ break;
++ case 4:
++ if (width_remaining < 8)
++ break;
++ printf ("\\u%02x%02x%02x",
++ ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
++ ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
++ ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
++
++ break;
++ default:
++ /* URG. */
++ break;
++ }
++
++ if (unicode_display == unicode_highlight && isatty (1))
++ printf ("\033[0m"); /* Default colour. */
++ }
++
++ if (bytes[nbytes - 1] == 0)
++ break;
+ }
+ else
+ {
+@@ -4556,6 +4702,7 @@ static struct option options[] =
+ {"syms", no_argument, 0, 's'},
+ {"silent-truncation",no_argument, 0, 'T'},
+ {"section-details", no_argument, 0, 't'},
++ {"unicode", required_argument, NULL, 'U'},
+ {"unwind", no_argument, 0, 'u'},
+ {"version-info", no_argument, 0, 'V'},
+ {"version", no_argument, 0, 'v'},
+@@ -4609,6 +4756,11 @@ usage (FILE * stream)
+ --no-demangle Do not demangle low-level symbol names. (This is the default)\n\
+ --recurse-limit Enable a demangling recursion limit. (This is the default)\n\
+ --no-recurse-limit Disable a demangling recursion limit\n\
++ -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
++ Display unicode characters as determined by the current locale\n\
++ (default), escape sequences, \"<hex sequences>\", highlighted\n\
++ escape sequences, or treat them as invalid and display as\n\
++ \"{hex sequences}\"\n\
+ -n --notes Display the core notes (if present)\n\
+ -r --relocs Display the relocations (if present)\n\
+ -u --unwind Display the unwind info (if present)\n\
+@@ -4749,7 +4901,7 @@ parse_args (struct dump_data *dumpdata,
+ usage (stderr);
+
+ while ((c = getopt_long
+- (argc, argv, "ACDHILNR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
++ (argc, argv, "ACDHILNR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
+ {
+ switch (c)
+ {
+@@ -4943,6 +5095,24 @@ parse_args (struct dump_data *dumpdata,
+ case OPTION_WITH_SYMBOL_VERSIONS:
+ /* Ignored for backward compatibility. */
+ break;
++ case 'U':
++ if (optarg == NULL)
++ error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
++ else if (streq (optarg, "default") || streq (optarg, "d"))
++ unicode_display = unicode_default;
++ else if (streq (optarg, "locale") || streq (optarg, "l"))
++ unicode_display = unicode_locale;
++ else if (streq (optarg, "escape") || streq (optarg, "e"))
++ unicode_display = unicode_escape;
++ else if (streq (optarg, "invalid") || streq (optarg, "i"))
++ unicode_display = unicode_invalid;
++ else if (streq (optarg, "hex") || streq (optarg, "x"))
++ unicode_display = unicode_hex;
++ else if (streq (optarg, "highlight") || streq (optarg, "h"))
++ unicode_display = unicode_highlight;
++ else
++ error (_("invalid argument to -U/--unicode: %s"), optarg);
++ break;
+
+ default:
+ /* xgettext:c-format */
+diff --git a/binutils/strings.c b/binutils/strings.c
+--- a/binutils/strings.c 2021-12-23 03:23:38.485842662 -0800
++++ b/binutils/strings.c 2021-12-23 03:30:36.586877452 -0800
+@@ -55,6 +55,19 @@
+ -T {bfdname}
+ Specify a non-default object file format.
+
++ --unicode={default|locale|invalid|hex|escape|highlight}
++ -u {d|l|i|x|e|h}
++ Determine how to handle UTF-8 unicode characters. The default
++ is no special treatment. All other versions of this option
++ only apply if the encoding is valid and enabling the option
++ implies --encoding=S.
++ The 'locale' option displays the characters according to the
++ current locale. The 'invalid' option treats them as
++ non-string characters. The 'hex' option displays them as hex
++ byte sequences. The 'escape' option displays them as escape
++ sequences and the 'highlight' option displays them as
++ coloured escape sequences.
++
+ --output-separator=sep_string
+ -s sep_string String used to separate parsed strings in output.
+ Default is newline.
+@@ -76,6 +89,22 @@
+ #include "safe-ctype.h"
+ #include "bucomm.h"
+
++#ifndef streq
++#define streq(a,b) (strcmp ((a),(b)) == 0)
++#endif
++
++typedef enum unicode_display_type
++{
++ unicode_default = 0,
++ unicode_locale,
++ unicode_escape,
++ unicode_hex,
++ unicode_highlight,
++ unicode_invalid
++} unicode_display_type;
++
++static unicode_display_type unicode_display = unicode_default;
++
+ #define STRING_ISGRAPHIC(c) \
+ ( (c) >= 0 \
+ && (c) <= 255 \
+@@ -94,7 +123,7 @@ extern int errno;
+ static int address_radix;
+
+ /* Minimum length of sequence of graphic chars to trigger output. */
+-static int string_min;
++static unsigned int string_min;
+
+ /* Whether or not we include all whitespace as a graphic char. */
+ static bfd_boolean include_all_whitespace;
+@@ -121,21 +150,22 @@ static char *output_separator;
+ static struct option long_options[] =
+ {
+ {"all", no_argument, NULL, 'a'},
++ {"bytes", required_argument, NULL, 'n'},
+ {"data", no_argument, NULL, 'd'},
++ {"encoding", required_argument, NULL, 'e'},
++ {"help", no_argument, NULL, 'h'},
++ {"include-all-whitespace", no_argument, NULL, 'w'},
++ {"output-separator", required_argument, NULL, 's'},
+ {"print-file-name", no_argument, NULL, 'f'},
+- {"bytes", required_argument, NULL, 'n'},
+ {"radix", required_argument, NULL, 't'},
+- {"include-all-whitespace", no_argument, NULL, 'w'},
+- {"encoding", required_argument, NULL, 'e'},
+ {"target", required_argument, NULL, 'T'},
+- {"output-separator", required_argument, NULL, 's'},
+- {"help", no_argument, NULL, 'h'},
++ {"unicode", required_argument, NULL, 'U'},
+ {"version", no_argument, NULL, 'v'},
+ {NULL, 0, NULL, 0}
+ };
+
+ static bfd_boolean strings_file (char *);
+-static void print_strings (const char *, FILE *, file_ptr, int, int, char *);
++static void print_strings (const char *, FILE *, file_ptr, int, char *);
+ static void usage (FILE *, int) ATTRIBUTE_NORETURN;
+
+ int main (int, char **);
+@@ -173,7 +203,7 @@ main (int argc, char **argv)
+ encoding = 's';
+ output_separator = NULL;
+
+- while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:Vv0123456789",
++ while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:U:Vv0123456789",
+ long_options, (int *) 0)) != EOF)
+ {
+ switch (optc)
+@@ -244,7 +274,24 @@ main (int argc, char **argv)
+
+ case 's':
+ output_separator = optarg;
+- break;
++ break;
++
++ case 'U':
++ if (streq (optarg, "default") || streq (optarg, "d"))
++ unicode_display = unicode_default;
++ else if (streq (optarg, "locale") || streq (optarg, "l"))
++ unicode_display = unicode_locale;
++ else if (streq (optarg, "escape") || streq (optarg, "e"))
++ unicode_display = unicode_escape;
++ else if (streq (optarg, "invalid") || streq (optarg, "i"))
++ unicode_display = unicode_invalid;
++ else if (streq (optarg, "hex") || streq (optarg, "x"))
++ unicode_display = unicode_hex;
++ else if (streq (optarg, "highlight") || streq (optarg, "h"))
++ unicode_display = unicode_highlight;
++ else
++ fatal (_("invalid argument to -U/--unicode: %s"), optarg);
++ break;
+
+ case 'V':
+ case 'v':
+@@ -260,6 +307,9 @@ main (int argc, char **argv)
+ }
+ }
+
++ if (unicode_display != unicode_default)
++ encoding = 'S';
++
+ if (numeric_opt != 0)
+ {
+ string_min = (int) strtoul (argv[numeric_opt - 1] + 1, &s, 0);
+@@ -295,14 +345,14 @@ main (int argc, char **argv)
+ {
+ datasection_only = FALSE;
+ SET_BINARY (fileno (stdin));
+- print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
++ print_strings ("{standard input}", stdin, 0, 0, (char *) NULL);
+ files_given = TRUE;
+ }
+ else
+ {
+ for (; optind < argc; ++optind)
+ {
+- if (strcmp (argv[optind], "-") == 0)
++ if (streq (argv[optind], "-"))
+ datasection_only = FALSE;
+ else
+ {
+@@ -344,7 +394,7 @@ strings_a_section (bfd *abfd, asection *
+ }
+
+ *got_a_section = TRUE;
+- print_strings (filename, NULL, sect->filepos, 0, sectsize, (char *) mem);
++ print_strings (filename, NULL, sect->filepos, sectsize, (char *) mem);
+ free (mem);
+ }
+
+@@ -429,7 +479,7 @@ strings_file (char *file)
+ return FALSE;
+ }
+
+- print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
++ print_strings (file, stream, (file_ptr) 0, 0, (char *) NULL);
+
+ if (fclose (stream) == EOF)
+ {
+@@ -553,11 +603,632 @@ unget_part_char (long c, file_ptr *addre
+ }
+ }
+ }
++
++
++static void
++print_filename_and_address (const char * filename, file_ptr address)
++{
++ if (print_filenames)
++ printf ("%s: ", filename);
++
++ if (! print_addresses)
++ return;
++
++ switch (address_radix)
++ {
++ case 8:
++ if (sizeof (address) > sizeof (long))
++ {
++#ifndef __MSVCRT__
++ printf ("%7llo ", (unsigned long long) address);
++#else
++ printf ("%7I64o ", (unsigned long long) address);
++#endif
++ }
++ else
++ printf ("%7lo ", (unsigned long) address);
++ break;
++
++ case 10:
++ if (sizeof (address) > sizeof (long))
++ {
++#ifndef __MSVCRT__
++ printf ("%7llu ", (unsigned long long) address);
++#else
++ printf ("%7I64d ", (unsigned long long) address);
++#endif
++ }
++ else
++ printf ("%7ld ", (long) address);
++ break;
++
++ case 16:
++ if (sizeof (address) > sizeof (long))
++ {
++#ifndef __MSVCRT__
++ printf ("%7llx ", (unsigned long long) address);
++#else
++ printf ("%7I64x ", (unsigned long long) address);
++#endif
++ }
++ else
++ printf ("%7lx ", (unsigned long) address);
++ break;
++ }
++}
++
++/* Return non-zero if the bytes starting at BUFFER form a valid UTF-8 encoding.
++ If the encoding is valid then returns the number of bytes it uses. */
++
++static unsigned int
++is_valid_utf8 (const unsigned char * buffer, unsigned long buflen)
++{
++ if (buffer[0] < 0xc0)
++ return 0;
++
++ if (buflen < 2)
++ return 0;
++
++ if ((buffer[1] & 0xc0) != 0x80)
++ return 0;
++
++ if ((buffer[0] & 0x20) == 0)
++ return 2;
++
++ if (buflen < 3)
++ return 0;
++
++ if ((buffer[2] & 0xc0) != 0x80)
++ return 0;
++
++ if ((buffer[0] & 0x10) == 0)
++ return 3;
++
++ if (buflen < 4)
++ return 0;
++
++ if ((buffer[3] & 0xc0) != 0x80)
++ return 0;
++
++ return 4;
++}
++
++/* Display a UTF-8 encoded character in BUFFER according to the setting
++ of unicode_display. The character is known to be valid.
++ Returns the number of bytes consumed. */
++
++static unsigned int
++display_utf8_char (const unsigned char * buffer)
++{
++ unsigned int j;
++ unsigned int utf8_len;
++
++ switch (buffer[0] & 0x30)
++ {
++ case 0x00:
++ case 0x10:
++ utf8_len = 2;
++ break;
++ case 0x20:
++ utf8_len = 3;
++ break;
++ default:
++ utf8_len = 4;
++ }
++
++ switch (unicode_display)
++ {
++ default:
++ fprintf (stderr, "ICE: unexpected unicode display type\n");
++ break;
++
++ case unicode_escape:
++ case unicode_highlight:
++ if (unicode_display == unicode_highlight && isatty (1))
++ printf ("\x1B[31;47m"); /* Red. */
++
++ switch (utf8_len)
++ {
++ case 2:
++ printf ("\\u%02x%02x",
++ ((buffer[0] & 0x1c) >> 2),
++ ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
++ break;
++
++ case 3:
++ printf ("\\u%02x%02x",
++ ((buffer[0] & 0x0f) << 4) | ((buffer[1] & 0x3c) >> 2),
++ ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3f)));
++ break;
++
++ case 4:
++ printf ("\\u%02x%02x%02x",
++ ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
++ ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
++ ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
++ break;
++ default:
++ /* URG. */
++ break;
++ }
++
++ if (unicode_display == unicode_highlight && isatty (1))
++ printf ("\033[0m"); /* Default colour. */
++ break;
++
++ case unicode_hex:
++ putchar ('<');
++ printf ("0x");
++ for (j = 0; j < utf8_len; j++)
++ printf ("%02x", buffer [j]);
++ putchar ('>');
++ break;
++
++ case unicode_locale:
++ printf ("%.1s", buffer);
++ break;
++ }
++
++ return utf8_len;
++}
++
++/* Display strings in BUFFER. Treat any UTF-8 encoded characters encountered
++ according to the setting of the unicode_display variable. The buffer
++ contains BUFLEN bytes.
++
++ Display the characters as if they started at ADDRESS and are contained in
++ FILENAME. */
++
++static void
++print_unicode_buffer (const char * filename,
++ file_ptr address,
++ const unsigned char * buffer,
++ unsigned long buflen)
++{
++ /* Paranoia checks... */
++ if (filename == NULL
++ || buffer == NULL
++ || unicode_display == unicode_default
++ || encoding != 'S'
++ || encoding_bytes != 1)
++ {
++ fprintf (stderr, "ICE: bad arguments to print_unicode_buffer\n");
++ return;
++ }
++
++ if (buflen == 0)
++ return;
++
++ /* We must only display strings that are at least string_min *characters*
++ long. So we scan the buffer in two stages. First we locate the start
++ of a potential string. Then we walk along it until we have found
++ string_min characters. Then we go back to the start point and start
++ displaying characters according to the unicode_display setting. */
++
++ unsigned long start_point = 0;
++ unsigned long i = 0;
++ unsigned int char_len = 1;
++ unsigned int num_found = 0;
++
++ for (i = 0; i < buflen; i += char_len)
++ {
++ int c = buffer[i];
++
++ char_len = 1;
++
++ /* Find the first potential character of a string. */
++ if (! STRING_ISGRAPHIC (c))
++ {
++ num_found = 0;
++ continue;
++ }
++
++ if (c > 126)
++ {
++ if (c < 0xc0)
++ {
++ num_found = 0;
++ continue;
++ }
++
++ if ((char_len = is_valid_utf8 (buffer + i, buflen - i)) == 0)
++ {
++ char_len = 1;
++ num_found = 0;
++ continue;
++ }
++
++ if (unicode_display == unicode_invalid)
++ {
++ /* We have found a valid UTF-8 character, but we treat it as non-graphic. */
++ num_found = 0;
++ continue;
++ }
++ }
++
++ if (num_found == 0)
++ /* We have found a potential starting point for a string. */
++ start_point = i;
++
++ ++ num_found;
++
++ if (num_found >= string_min)
++ break;
++ }
++
++ if (num_found < string_min)
++ return;
++
++ print_filename_and_address (filename, address + start_point);
++
++ /* We have found string_min characters. Display them and any
++ more that follow. */
++ for (i = start_point; i < buflen; i += char_len)
++ {
++ int c = buffer[i];
++
++ char_len = 1;
++
++ if (! STRING_ISGRAPHIC (c))
++ break;
++ else if (c < 127)
++ putchar (c);
++ else if (! is_valid_utf8 (buffer + i, buflen - i))
++ break;
++ else if (unicode_display == unicode_invalid)
++ break;
++ else
++ char_len = display_utf8_char (buffer + i);
++ }
++
++ if (output_separator)
++ fputs (output_separator, stdout);
++ else
++ putchar ('\n');
++
++ /* FIXME: Using tail recursion here is lazy programming... */
++ print_unicode_buffer (filename, address + i, buffer + i, buflen - i);
++}
++
++static int
++get_unicode_byte (FILE * stream,
++ unsigned char * putback,
++ unsigned int * num_putback,
++ unsigned int * num_read)
++{
++ if (* num_putback > 0)
++ {
++ * num_putback = * num_putback - 1;
++ return putback [* num_putback];
++ }
++
++ * num_read = * num_read + 1;
++
++#if defined(HAVE_GETC_UNLOCKED) && HAVE_DECL_GETC_UNLOCKED
++ return getc_unlocked (stream);
++#else
++ return getc (stream);
++#endif
++}
++
++/* Helper function for print_unicode_stream. */
++
++static void
++print_unicode_stream_body (const char * filename,
++ file_ptr address,
++ FILE * stream,
++ unsigned char * putback_buf,
++ unsigned int num_putback,
++ unsigned char * print_buf)
++{
++ /* It would be nice if we could just read the stream into a buffer
++ and then process if with print_unicode_buffer. But the input
++ might be huge or it might time-locked (eg stdin). So instead
++ we go one byte at a time... */
++
++ file_ptr start_point = 0;
++ unsigned int num_read = 0;
++ unsigned int num_chars = 0;
++ unsigned int num_print = 0;
++ int c;
++
++ /* Find a series of string_min characters. Put them into print_buf. */
++ do
++ {
++ if (num_chars >= string_min)
++ break;
++
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++
++ if (! STRING_ISGRAPHIC (c))
++ {
++ num_chars = num_print = 0;
++ continue;
++ }
++
++ if (num_chars == 0)
++ start_point = num_read - 1;
++
++ if (c < 127)
++ {
++ print_buf[num_print] = c;
++ num_chars ++;
++ num_print ++;
++ continue;
++ }
++
++ if (c < 0xc0)
++ {
++ num_chars = num_print = 0;
++ continue;
++ }
++
++ /* We *might* have a UTF-8 sequence. Time to start peeking. */
++ char utf8[4];
++
++ utf8[0] = c;
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[1] = c;
++
++ if ((utf8[1] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[1];
++ num_chars = num_print = 0;
++ continue;
++ }
++ else if ((utf8[0] & 0x20) == 0)
++ {
++ /* A valid 2-byte UTF-8 encoding. */
++ if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[1];
++ num_chars = num_print = 0;
++ }
++ else
++ {
++ print_buf[num_print ++] = utf8[0];
++ print_buf[num_print ++] = utf8[1];
++ num_chars ++;
++ }
++ continue;
++ }
++
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[2] = c;
++
++ if ((utf8[2] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ num_chars = num_print = 0;
++ continue;
++ }
++ else if ((utf8[0] & 0x10) == 0)
++ {
++ /* A valid 3-byte UTF-8 encoding. */
++ if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ num_chars = num_print = 0;
++ }
++ else
++ {
++ print_buf[num_print ++] = utf8[0];
++ print_buf[num_print ++] = utf8[1];
++ print_buf[num_print ++] = utf8[2];
++ num_chars ++;
++ }
++ continue;
++ }
++
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[3] = c;
++
++ if ((utf8[3] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[3];
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ num_chars = num_print = 0;
++ }
++ /* We have a valid 4-byte UTF-8 encoding. */
++ else if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[3];
++ putback_buf[num_putback++] = utf8[1];
++ putback_buf[num_putback++] = utf8[2];
++ num_chars = num_print = 0;
++ }
++ else
++ {
++ print_buf[num_print ++] = utf8[0];
++ print_buf[num_print ++] = utf8[1];
++ print_buf[num_print ++] = utf8[2];
++ print_buf[num_print ++] = utf8[3];
++ num_chars ++;
++ }
++ }
++ while (1);
++
++ if (num_chars >= string_min)
++ {
++ /* We know that we have string_min valid characters in print_buf,
++ and there may be more to come in the stream. Start displaying
++ them. */
++
++ print_filename_and_address (filename, address + start_point);
++
++ unsigned int i;
++ for (i = 0; i < num_print;)
++ {
++ if (print_buf[i] < 127)
++ putchar (print_buf[i++]);
++ else
++ i += display_utf8_char (print_buf + i);
++ }
++
++ /* OK so now we have to start read unchecked bytes. */
++
++ /* Find a series of string_min characters. Put them into print_buf. */
++ do
++ {
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++
++ if (! STRING_ISGRAPHIC (c))
++ break;
++
++ if (c < 127)
++ {
++ putchar (c);
++ continue;
++ }
++
++ if (c < 0xc0)
++ break;
++
++ /* We *might* have a UTF-8 sequence. Time to start peeking. */
++ unsigned char utf8[4];
++
++ utf8[0] = c;
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[1] = c;
++
++ if ((utf8[1] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else if ((utf8[0] & 0x20) == 0)
++ {
++ /* Valid 2-byte UTF-8. */
++ if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else
++ {
++ (void) display_utf8_char (utf8);
++ continue;
++ }
++ }
++
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[2] = c;
++
++ if ((utf8[2] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else if ((utf8[0] & 0x10) == 0)
++ {
++ /* Valid 3-byte UTF-8. */
++ if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else
++ {
++ (void) display_utf8_char (utf8);
++ continue;
++ }
++ }
++
++ c = get_unicode_byte (stream, putback_buf, & num_putback, & num_read);
++ if (c == EOF)
++ break;
++ utf8[3] = c;
++
++ if ((utf8[3] & 0xc0) != 0x80)
++ {
++ /* Invalid UTF-8. */
++ putback_buf[num_putback++] = utf8[3];
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else if (unicode_display == unicode_invalid)
++ {
++ putback_buf[num_putback++] = utf8[3];
++ putback_buf[num_putback++] = utf8[2];
++ putback_buf[num_putback++] = utf8[1];
++ break;
++ }
++ else
++ /* A valid 4-byte UTF-8 encoding. */
++ (void) display_utf8_char (utf8);
++ }
++ while (1);
++
++ if (output_separator)
++ fputs (output_separator, stdout);
++ else
++ putchar ('\n');
++ }
++
++ if (c != EOF)
++ /* FIXME: Using tail recursion here is lazy, but it works. */
++ print_unicode_stream_body (filename, address + num_read, stream, putback_buf, num_putback, print_buf);
++}
++
++/* Display strings read in from STREAM. Treat any UTF-8 encoded characters
++ encountered according to the setting of the unicode_display variable.
++ The stream is positioned at ADDRESS and is attached to FILENAME. */
++
++static void
++print_unicode_stream (const char * filename,
++ file_ptr address,
++ FILE * stream)
++{
++ /* Paranoia checks... */
++ if (filename == NULL
++ || stream == NULL
++ || unicode_display == unicode_default
++ || encoding != 'S'
++ || encoding_bytes != 1)
++ {
++ fprintf (stderr, "ICE: bad arguments to print_unicode_stream\n");
++ return;
++ }
++
++ /* Allocate space for string_min 4-byte utf-8 characters. */
++ unsigned char * print_buf = xmalloc ((4 * string_min) + 1);
++ /* We should never have to put back more than 4 bytes. */
++ unsigned char putback_buf[5];
++ unsigned int num_putback = 0;
++
++ print_unicode_stream_body (filename, address, stream, putback_buf, num_putback, print_buf);
++ free (print_buf);
++}
++
+
+ /* Find the strings in file FILENAME, read from STREAM.
+ Assume that STREAM is positioned so that the next byte read
+ is at address ADDRESS in the file.
+- Stop reading at address STOP_POINT in the file, if nonzero.
+
+ If STREAM is NULL, do not read from it.
+ The caller can supply a buffer of characters
+@@ -568,20 +1239,29 @@ unget_part_char (long c, file_ptr *addre
+
+ static void
+ print_strings (const char *filename, FILE *stream, file_ptr address,
+- int stop_point, int magiccount, char *magic)
++ int magiccount, char *magic)
+ {
++ if (unicode_display != unicode_default)
++ {
++ if (magic != NULL)
++ print_unicode_buffer (filename, address,
++ (const unsigned char *) magic, magiccount);
++
++ if (stream != NULL)
++ print_unicode_stream (filename, address, stream);
++ return;
++ }
++
+ char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
+
+ while (1)
+ {
+ file_ptr start;
+- int i;
++ unsigned int i;
+ long c;
+
+ /* See if the next `string_min' chars are all graphic chars. */
+ tryline:
+- if (stop_point && address >= stop_point)
+- break;
+ start = address;
+ for (i = 0; i < string_min; i++)
+ {
+@@ -604,69 +1284,7 @@ print_strings (const char *filename, FIL
+ /* We found a run of `string_min' graphic characters. Print up
+ to the next non-graphic character. */
+
+- if (print_filenames)
+- printf ("%s: ", filename);
+- if (print_addresses)
+- switch (address_radix)
+- {
+- case 8:
+-#ifdef HAVE_LONG_LONG
+- if (sizeof (start) > sizeof (long))
+- {
+-# ifndef __MSVCRT__
+- printf ("%7llo ", (unsigned long long) start);
+-# else
+- printf ("%7I64o ", (unsigned long long) start);
+-# endif
+- }
+- else
+-#elif !BFD_HOST_64BIT_LONG
+- if (start != (unsigned long) start)
+- printf ("++%7lo ", (unsigned long) start);
+- else
+-#endif
+- printf ("%7lo ", (unsigned long) start);
+- break;
+-
+- case 10:
+-#ifdef HAVE_LONG_LONG
+- if (sizeof (start) > sizeof (long))
+- {
+-# ifndef __MSVCRT__
+- printf ("%7llu ", (unsigned long long) start);
+-# else
+- printf ("%7I64d ", (unsigned long long) start);
+-# endif
+- }
+- else
+-#elif !BFD_HOST_64BIT_LONG
+- if (start != (unsigned long) start)
+- printf ("++%7lu ", (unsigned long) start);
+- else
+-#endif
+- printf ("%7ld ", (long) start);
+- break;
+-
+- case 16:
+-#ifdef HAVE_LONG_LONG
+- if (sizeof (start) > sizeof (long))
+- {
+-# ifndef __MSVCRT__
+- printf ("%7llx ", (unsigned long long) start);
+-# else
+- printf ("%7I64x ", (unsigned long long) start);
+-# endif
+- }
+- else
+-#elif !BFD_HOST_64BIT_LONG
+- if (start != (unsigned long) start)
+- printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
+- (unsigned long) (start & 0xffffffff));
+- else
+-#endif
+- printf ("%7lx ", (unsigned long) start);
+- break;
+- }
++ print_filename_and_address (filename, start);
+
+ buf[i] = '\0';
+ fputs (buf, stdout);
+@@ -718,6 +1336,8 @@ usage (FILE *stream, int status)
+ -T --target=<BFDNAME> Specify the binary file format\n\
+ -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
+ s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
++ --unicode={default|show|invalid|hex|escape|highlight}\n\
++ -u {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
+ -s --output-separator=<string> String used to separate strings in output.\n\
+ @<file> Read options from <file>\n\
+ -h --help Display this information\n\
diff --git a/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-45078.patch b/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-45078.patch
new file mode 100644
index 0000000000..f118e2599b
--- /dev/null
+++ b/meta/recipes-devtools/binutils/binutils/0001-CVE-2021-45078.patch
@@ -0,0 +1,255 @@
+From 161e87d12167b1e36193385485c1f6ce92f74f02 Mon Sep 17 00:00:00 2001
+From: Alan Modra <amodra@gmail.com>
+Date: Wed, 15 Dec 2021 11:48:42 +1030
+Subject: [PATCH] PR28694, Out-of-bounds write in stab_xcoff_builtin_type
+
+ PR 28694
+ * stabs.c (stab_xcoff_builtin_type): Make typenum unsigned.
+ Negate typenum earlier, simplifying bounds checking. Correct
+ off-by-one indexing. Adjust switch cases.
+
+
+CVE: CVE-2021-45078
+Upstream-Status: Backport [https://sourceware.org/git/?p=binutils-gdb.git;a=patch;h=161e87d12167b1e36193385485c1f6ce92f74f02]
+
+Signed-off-by: Sundeep KOKKONDA <sundeep.kokkonda@gmail.com>
+---
+ binutils/stabs.c | 87 ++++++++++++++++++++++++------------------------
+ 1 file changed, 43 insertions(+), 44 deletions(-)
+
+
+diff --git a/binutils/stabs.c b/binutils/stabs.c
+index 274bfb0e7fa..83ee3ea5fa4 100644
+--- a/binutils/stabs.c
++++ b/binutils/stabs.c
+@@ -202,7 +202,7 @@ static debug_type stab_find_type (void *, struct stab_handle *, const int *);
+ static bfd_boolean stab_record_type
+ (void *, struct stab_handle *, const int *, debug_type);
+ static debug_type stab_xcoff_builtin_type
+- (void *, struct stab_handle *, int);
++ (void *, struct stab_handle *, unsigned int);
+ static debug_type stab_find_tagged_type
+ (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
+ static debug_type *stab_demangle_argtypes
+@@ -3496,166 +3496,167 @@ stab_record_type (void *dhandle ATTRIBUTE_UNUSED, struct stab_handle *info,
+
+ static debug_type
+ stab_xcoff_builtin_type (void *dhandle, struct stab_handle *info,
+- int typenum)
++ unsigned int typenum)
+ {
+ debug_type rettype;
+ const char *name;
+
+- if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
++ typenum = -typenum - 1;
++ if (typenum >= XCOFF_TYPE_COUNT)
+ {
+- fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
++ fprintf (stderr, _("Unrecognized XCOFF type %d\n"), -typenum - 1);
+ return DEBUG_TYPE_NULL;
+ }
+- if (info->xcoff_types[-typenum] != NULL)
+- return info->xcoff_types[-typenum];
++ if (info->xcoff_types[typenum] != NULL)
++ return info->xcoff_types[typenum];
+
+- switch (-typenum)
++ switch (typenum)
+ {
+- case 1:
++ case 0:
+ /* The size of this and all the other types are fixed, defined
+ by the debugging format. */
+ name = "int";
+ rettype = debug_make_int_type (dhandle, 4, FALSE);
+ break;
+- case 2:
++ case 1:
+ name = "char";
+ rettype = debug_make_int_type (dhandle, 1, FALSE);
+ break;
+- case 3:
++ case 2:
+ name = "short";
+ rettype = debug_make_int_type (dhandle, 2, FALSE);
+ break;
+- case 4:
++ case 3:
+ name = "long";
+ rettype = debug_make_int_type (dhandle, 4, FALSE);
+ break;
+- case 5:
++ case 4:
+ name = "unsigned char";
+ rettype = debug_make_int_type (dhandle, 1, TRUE);
+ break;
+- case 6:
++ case 5:
+ name = "signed char";
+ rettype = debug_make_int_type (dhandle, 1, FALSE);
+ break;
+- case 7:
++ case 6:
+ name = "unsigned short";
+ rettype = debug_make_int_type (dhandle, 2, TRUE);
+ break;
+- case 8:
++ case 7:
+ name = "unsigned int";
+ rettype = debug_make_int_type (dhandle, 4, TRUE);
+ break;
+- case 9:
++ case 8:
+ name = "unsigned";
+ rettype = debug_make_int_type (dhandle, 4, TRUE);
+ break;
+- case 10:
++ case 9:
+ name = "unsigned long";
+ rettype = debug_make_int_type (dhandle, 4, TRUE);
+ break;
+- case 11:
++ case 10:
+ name = "void";
+ rettype = debug_make_void_type (dhandle);
+ break;
+- case 12:
++ case 11:
+ /* IEEE single precision (32 bit). */
+ name = "float";
+ rettype = debug_make_float_type (dhandle, 4);
+ break;
+- case 13:
++ case 12:
+ /* IEEE double precision (64 bit). */
+ name = "double";
+ rettype = debug_make_float_type (dhandle, 8);
+ break;
+- case 14:
++ case 13:
+ /* This is an IEEE double on the RS/6000, and different machines
+ with different sizes for "long double" should use different
+ negative type numbers. See stabs.texinfo. */
+ name = "long double";
+ rettype = debug_make_float_type (dhandle, 8);
+ break;
+- case 15:
++ case 14:
+ name = "integer";
+ rettype = debug_make_int_type (dhandle, 4, FALSE);
+ break;
+- case 16:
++ case 15:
+ name = "boolean";
+ rettype = debug_make_bool_type (dhandle, 4);
+ break;
+- case 17:
++ case 16:
+ name = "short real";
+ rettype = debug_make_float_type (dhandle, 4);
+ break;
+- case 18:
++ case 17:
+ name = "real";
+ rettype = debug_make_float_type (dhandle, 8);
+ break;
+- case 19:
++ case 18:
+ /* FIXME */
+ name = "stringptr";
+ rettype = NULL;
+ break;
+- case 20:
++ case 19:
+ /* FIXME */
+ name = "character";
+ rettype = debug_make_int_type (dhandle, 1, TRUE);
+ break;
+- case 21:
++ case 20:
+ name = "logical*1";
+ rettype = debug_make_bool_type (dhandle, 1);
+ break;
+- case 22:
++ case 21:
+ name = "logical*2";
+ rettype = debug_make_bool_type (dhandle, 2);
+ break;
+- case 23:
++ case 22:
+ name = "logical*4";
+ rettype = debug_make_bool_type (dhandle, 4);
+ break;
+- case 24:
++ case 23:
+ name = "logical";
+ rettype = debug_make_bool_type (dhandle, 4);
+ break;
+- case 25:
++ case 24:
+ /* Complex type consisting of two IEEE single precision values. */
+ name = "complex";
+ rettype = debug_make_complex_type (dhandle, 8);
+ break;
+- case 26:
++ case 25:
+ /* Complex type consisting of two IEEE double precision values. */
+ name = "double complex";
+ rettype = debug_make_complex_type (dhandle, 16);
+ break;
+- case 27:
++ case 26:
+ name = "integer*1";
+ rettype = debug_make_int_type (dhandle, 1, FALSE);
+ break;
+- case 28:
++ case 27:
+ name = "integer*2";
+ rettype = debug_make_int_type (dhandle, 2, FALSE);
+ break;
+- case 29:
++ case 28:
+ name = "integer*4";
+ rettype = debug_make_int_type (dhandle, 4, FALSE);
+ break;
+- case 30:
++ case 29:
+ /* FIXME */
+ name = "wchar";
+ rettype = debug_make_int_type (dhandle, 2, FALSE);
+ break;
+- case 31:
++ case 30:
+ name = "long long";
+ rettype = debug_make_int_type (dhandle, 8, FALSE);
+ break;
+- case 32:
++ case 31:
+ name = "unsigned long long";
+ rettype = debug_make_int_type (dhandle, 8, TRUE);
+ break;
+- case 33:
++ case 32:
+ name = "logical*8";
+ rettype = debug_make_bool_type (dhandle, 8);
+ break;
+- case 34:
++ case 33:
+ name = "integer*8";
+ rettype = debug_make_int_type (dhandle, 8, FALSE);
+ break;
+@@ -3664,9 +3665,7 @@ stab_xcoff_builtin_type (void *dhandle, struct stab_handle *info,
+ }
+
+ rettype = debug_name_type (dhandle, name, rettype);
+-
+- info->xcoff_types[-typenum] = rettype;
+-
++ info->xcoff_types[typenum] = rettype;
+ return rettype;
+ }
+
+--
+2.27.0
+
diff --git a/meta/recipes-devtools/binutils/binutils/0002-CVE-2021-20197.patch b/meta/recipes-devtools/binutils/binutils/0002-CVE-2021-20197.patch
deleted file mode 100644
index 3771f571eb..0000000000
--- a/meta/recipes-devtools/binutils/binutils/0002-CVE-2021-20197.patch
+++ /dev/null
@@ -1,170 +0,0 @@
-From d3edaa91d4cf7202ec14342410194841e2f67f12 Mon Sep 17 00:00:00 2001
-From: Alan Modra <amodra@gmail.com>
-Date: Fri, 26 Feb 2021 11:30:32 +1030
-Subject: [PATCH] Reinstate various pieces backed out from smart_rename changes
-
-In the interests of a stable release various last minute smart_rename
-patches were backed out of the 2.36 branch. The main reason to
-reinstate some of those backed out changes here is to make necessary
-followup fixes to commit 8e03235147a9 simple cherry-picks from
-mainline. A secondary reason is that ar -M support isn't fixed for
-pr26945 without this patch.
-
- PR 26945
- * ar.c: Don't include libbfd.h.
- (write_archive): Replace xmalloc+strcpy with xstrdup.
- * arsup.c (temp_name, real_ofd): New static variables.
- (ar_open): Use make_tempname and bfd_fdopenw.
- (ar_save): Adjust to suit ar_open changes.
- * objcopy.c: Don't include libbfd.h.
- * rename.c: Rename and reorder variables.
-
-(cherry picked from commit 95b91a043aeaeb546d2fea556d84a2de1e917770)
-
-Upstream-Status: Backport [https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d3edaa91d4cf7202ec14342410194841e2f67f12]
-CVE: CVE-2021-20197
-Signed-off-by: Vinay Kumar <vinay.m.engg@gmail.com>
----
- binutils/ar.c | 4 +---
- binutils/arsup.c | 37 +++++++++++++++++++++++++------------
- binutils/objcopy.c | 1 -
- binutils/rename.c | 6 +++---
- 5 files changed, 42 insertions(+), 19 deletions(-)
-
-diff --git a/binutils/ar.c b/binutils/ar.c
-index 3a91708b51c..44df48c5c67 100644
---- a/binutils/ar.c
-+++ b/binutils/ar.c
-@@ -25,7 +25,6 @@
-
- #include "sysdep.h"
- #include "bfd.h"
--#include "libbfd.h"
- #include "libiberty.h"
- #include "progress.h"
- #include "getopt.h"
-@@ -1255,8 +1254,7 @@ write_archive (bfd *iarch)
- bfd *contents_head = iarch->archive_next;
- int ofd = -1;
-
-- old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
-- strcpy (old_name, bfd_get_filename (iarch));
-+ old_name = xstrdup (bfd_get_filename (iarch));
- new_name = make_tempname (old_name, &ofd);
-
- if (new_name == NULL)
-diff --git a/binutils/arsup.c b/binutils/arsup.c
-index 0a1f63f6456..f7ce8f0bc82 100644
---- a/binutils/arsup.c
-+++ b/binutils/arsup.c
-@@ -42,6 +42,8 @@ extern int deterministic;
-
- static bfd *obfd;
- static char *real_name;
-+static char *temp_name;
-+static int real_ofd;
- static FILE *outfile;
-
- static void
-@@ -149,27 +151,24 @@ maybequit (void)
- void
- ar_open (char *name, int t)
- {
-- char *tname;
-- const char *bname = lbasename (name);
-- real_name = name;
-+ real_name = xstrdup (name);
-+ temp_name = make_tempname (real_name, &real_ofd);
-
-- /* Prepend tmp- to the beginning, to avoid file-name clashes after
-- truncation on filesystems with limited namespaces (DOS). */
-- if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
-+ if (temp_name == NULL)
- {
-- fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
-+ fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
- program_name, strerror(errno));
- maybequit ();
- return;
- }
-
-- obfd = bfd_openw (tname, NULL);
-+ obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
-
- if (!obfd)
- {
- fprintf (stderr,
- _("%s: Can't open output archive %s\n"),
-- program_name, tname);
-+ program_name, temp_name);
-
- maybequit ();
- }
-@@ -344,16 +343,30 @@ ar_save (void)
- }
- else
- {
-- char *ofilename = xstrdup (bfd_get_filename (obfd));
-+ struct stat target_stat;
-
- if (deterministic > 0)
- obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
-
- bfd_close (obfd);
-
-- smart_rename (ofilename, real_name, NULL);
-+ if (stat (real_name, &target_stat) != 0)
-+ {
-+ /* The temp file created in ar_open has mode 0600 as per mkstemp.
-+ Create the real empty output file here so smart_rename will
-+ update the mode according to the process umask. */
-+ obfd = bfd_openw (real_name, NULL);
-+ if (obfd != NULL)
-+ {
-+ bfd_set_format (obfd, bfd_archive);
-+ bfd_close (obfd);
-+ }
-+ }
-+
-+ smart_rename (temp_name, real_name, NULL);
- obfd = 0;
-- free (ofilename);
-+ free (temp_name);
-+ free (real_name);
- }
- }
-
-diff --git a/binutils/objcopy.c b/binutils/objcopy.c
-index 07a872b5a80..73aa8bc2514 100644
---- a/binutils/objcopy.c
-+++ b/binutils/objcopy.c
-@@ -20,7 +20,6 @@
-
- #include "sysdep.h"
- #include "bfd.h"
--#include "libbfd.h"
- #include "progress.h"
- #include "getopt.h"
- #include "libiberty.h"
-diff --git a/binutils/rename.c b/binutils/rename.c
-index f471b45fd3f..2ff092ee22b 100644
---- a/binutils/rename.c
-+++ b/binutils/rename.c
-@@ -130,11 +130,11 @@ int
- smart_rename (const char *from, const char *to,
- struct stat *target_stat ATTRIBUTE_UNUSED)
- {
-- bfd_boolean exists;
-- struct stat s;
- int ret = 0;
-+ struct stat to_stat;
-+ bfd_boolean exists;
-
-- exists = lstat (to, &s) == 0;
-+ exists = lstat (to, &to_stat) == 0;
-
- #if defined (_WIN32) && !defined (__CYGWIN32__)
- /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
---
-2.31.1
-
diff --git a/meta/recipes-devtools/binutils/binutils/0003-CVE-2021-20197.patch b/meta/recipes-devtools/binutils/binutils/0003-CVE-2021-20197.patch
deleted file mode 100644
index 082b28b29c..0000000000
--- a/meta/recipes-devtools/binutils/binutils/0003-CVE-2021-20197.patch
+++ /dev/null
@@ -1,171 +0,0 @@
-From 8b69e61d4be276bb862698aaafddc3e779d23c8f Mon Sep 17 00:00:00 2001
-From: Alan Modra <amodra@gmail.com>
-Date: Tue, 23 Feb 2021 09:37:39 +1030
-Subject: [PATCH] PR27456, lstat in rename.c on MinGW
-
- PR 27456
- * rename.c: Tidy throughout.
- (smart_rename): Always copy. Remove windows specific code.
-
-(cherry picked from commit cca8873dd5a6015d5557ea44bc1ea9c252435a29)
-
-Upstream-Status: Backport [https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8b69e61d4be276bb862698aaafddc3e779d23c8f]
-CVE: CVE-2021-20197
-Signed-off-by: Vinay Kumar <vinay.m.engg@gmail.com>
----
- binutils/rename.c | 111 ++++++++++++++-------------------------------
- 2 files changed, 40 insertions(+), 76 deletions(-)
-
-diff --git a/binutils/rename.c b/binutils/rename.c
-index 2ff092ee22b..72a9323d72c 100644
---- a/binutils/rename.c
-+++ b/binutils/rename.c
-@@ -24,14 +24,9 @@
-
- #ifdef HAVE_GOOD_UTIME_H
- #include <utime.h>
--#else /* ! HAVE_GOOD_UTIME_H */
--#ifdef HAVE_UTIMES
-+#elif defined HAVE_UTIMES
- #include <sys/time.h>
--#endif /* HAVE_UTIMES */
--#endif /* ! HAVE_GOOD_UTIME_H */
--
--#if ! defined (_WIN32) || defined (__CYGWIN32__)
--static int simple_copy (const char *, const char *);
-+#endif
-
- /* The number of bytes to copy at once. */
- #define COPY_BUF 8192
-@@ -82,7 +77,6 @@ simple_copy (const char *from, const char *to)
- }
- return 0;
- }
--#endif /* __CYGWIN32__ or not _WIN32 */
-
- /* Set the times of the file DESTINATION to be the same as those in
- STATBUF. */
-@@ -91,87 +85,52 @@ void
- set_times (const char *destination, const struct stat *statbuf)
- {
- int result;
--
-- {
- #ifdef HAVE_GOOD_UTIME_H
-- struct utimbuf tb;
--
-- tb.actime = statbuf->st_atime;
-- tb.modtime = statbuf->st_mtime;
-- result = utime (destination, &tb);
--#else /* ! HAVE_GOOD_UTIME_H */
--#ifndef HAVE_UTIMES
-- long tb[2];
--
-- tb[0] = statbuf->st_atime;
-- tb[1] = statbuf->st_mtime;
-- result = utime (destination, tb);
--#else /* HAVE_UTIMES */
-- struct timeval tv[2];
--
-- tv[0].tv_sec = statbuf->st_atime;
-- tv[0].tv_usec = 0;
-- tv[1].tv_sec = statbuf->st_mtime;
-- tv[1].tv_usec = 0;
-- result = utimes (destination, tv);
--#endif /* HAVE_UTIMES */
--#endif /* ! HAVE_GOOD_UTIME_H */
-- }
-+ struct utimbuf tb;
-+
-+ tb.actime = statbuf->st_atime;
-+ tb.modtime = statbuf->st_mtime;
-+ result = utime (destination, &tb);
-+#elif defined HAVE_UTIMES
-+ struct timeval tv[2];
-+
-+ tv[0].tv_sec = statbuf->st_atime;
-+ tv[0].tv_usec = 0;
-+ tv[1].tv_sec = statbuf->st_mtime;
-+ tv[1].tv_usec = 0;
-+ result = utimes (destination, tv);
-+#else
-+ long tb[2];
-+
-+ tb[0] = statbuf->st_atime;
-+ tb[1] = statbuf->st_mtime;
-+ result = utime (destination, tb);
-+#endif
-
- if (result != 0)
- non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
- }
-
--/* Rename FROM to TO, copying if TO exists. TARGET_STAT has the file status
-- that, if non-NULL, is used to fix up timestamps after rename. Return 0 if
-- ok, -1 if error. */
-+/* Copy FROM to TO. TARGET_STAT has the file status that, if non-NULL,
-+ is used to fix up timestamps. Return 0 if ok, -1 if error.
-+ At one time this function renamed files, but file permissions are
-+ tricky to update given the number of different schemes used by
-+ various systems. So now we just copy. */
-
- int
- smart_rename (const char *from, const char *to,
-- struct stat *target_stat ATTRIBUTE_UNUSED)
-+ struct stat *target_stat)
- {
-- int ret = 0;
-- struct stat to_stat;
-- bfd_boolean exists;
--
-- exists = lstat (to, &to_stat) == 0;
--
--#if defined (_WIN32) && !defined (__CYGWIN32__)
-- /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
-- fail instead. Also, chown is not present. */
--
-- if (exists)
-- remove (to);
-+ int ret;
-
-- ret = rename (from, to);
-+ ret = simple_copy (from, to);
- if (ret != 0)
-- {
-- /* We have to clean up here. */
-- non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
-- unlink (from);
-- }
--#else
-- /* Avoid a full copy and use rename if TO does not exist. */
-- if (!exists)
-- {
-- if ((ret = rename (from, to)) != 0)
-- {
-- /* We have to clean up here. */
-- non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
-- unlink (from);
-- }
-- }
-- else
-- {
-- ret = simple_copy (from, to);
-- if (ret != 0)
-- non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
-+ non_fatal (_("unable to copy file '%s'; reason: %s"),
-+ to, strerror (errno));
-
-- if (target_stat != NULL)
-- set_times (to, target_stat);
-- unlink (from);
-- }
--#endif /* _WIN32 && !__CYGWIN32__ */
-+ if (target_stat != NULL)
-+ set_times (to, target_stat);
-+ unlink (from);
-
- return ret;
- }
---
-2.31.1
-
diff --git a/meta/recipes-devtools/binutils/binutils/0017-CVE-2021-3530.patch b/meta/recipes-devtools/binutils/binutils/0017-CVE-2021-3530.patch
new file mode 100644
index 0000000000..fa10a6c99a
--- /dev/null
+++ b/meta/recipes-devtools/binutils/binutils/0017-CVE-2021-3530.patch
@@ -0,0 +1,102 @@
+From 25162c795b1a2becf936bb3581d86a307ea491eb Mon Sep 17 00:00:00 2001
+From: Nick Clifton <nickc@redhat.com>
+Date: Thu, 15 Jul 2021 16:51:56 +0100
+Subject: [PATCH] Fix a stack exhaustion problem in the Rust demangling code in
+ the libiberty library.
+
+ PR 99935
+ * rust-demangle.c: Add recursion limit.
+
+CVE: CVE-2021-3530
+Upstream-Status: Backport[https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=25162c795b1a2becf936bb3581d86a307ea491eb]
+Signed-off-by: Pgowda <pgowda.cve@gmail.com>
+
+---
+ libiberty/ChangeLog | 5 +++++
+ libiberty/rust-demangle.c | 31 +++++++++++++++++++++++++------
+ 2 files changed, 30 insertions(+), 6 deletions(-)
+
+diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
+index bc1b35b97c4..8e39fd28eba 100644
+--- a/libiberty/ChangeLog
++++ b/libiberty/ChangeLog
+@@ -1,3 +1,8 @@
++2021-07-15 Nick Clifton <nickc@redhat.com>
++
++ PR 99935
++ * rust-demangle.c: Add recursion limit.
++
+ 2021-01-04 Martin Liska <mliska@suse.cz>
+
+ * strverscmp.c: Convert to utf8 from iso8859.
+diff --git a/libiberty/rust-demangle.c b/libiberty/rust-demangle.c
+index 449941b56dc..df09b7b8fdd 100644
+--- a/libiberty/rust-demangle.c
++++ b/libiberty/rust-demangle.c
+@@ -74,6 +74,12 @@ struct rust_demangler
+ /* Rust mangling version, with legacy mangling being -1. */
+ int version;
+
++ /* Recursion depth. */
++ uint recursion;
++ /* Maximum number of times demangle_path may be called recursively. */
++#define RUST_MAX_RECURSION_COUNT 1024
++#define RUST_NO_RECURSION_LIMIT ((uint) -1)
++
+ uint64_t bound_lifetime_depth;
+ };
+
+@@ -671,6 +677,15 @@ demangle_path (struct rust_demangler *rd
+ if (rdm->errored)
+ return;
+
++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
++ {
++ ++ rdm->recursion;
++ if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
++ /* FIXME: There ought to be a way to report
++ that the recursion limit has been reached. */
++ goto fail_return;
++ }
++
+ switch (tag = next (rdm))
+ {
+ case 'C':
+@@ -688,10 +703,7 @@ demangle_path (struct rust_demangler *rd
+ case 'N':
+ ns = next (rdm);
+ if (!ISLOWER (ns) && !ISUPPER (ns))
+- {
+- rdm->errored = 1;
+- return;
+- }
++ goto fail_return;
+
+ demangle_path (rdm, in_value);
+
+@@ -776,9 +788,15 @@ demangle_path (struct rust_demangler *rd
+ }
+ break;
+ default:
+- rdm->errored = 1;
+- return;
++ goto fail_return;
+ }
++ goto pass_return;
++
++ fail_return:
++ rdm->errored = 1;
++ pass_return:
++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
++ -- rdm->recursion;
+ }
+
+ static void
+@@ -1317,6 +1335,7 @@ rust_demangle_callback (const char *mang
+ rdm.skipping_printing = 0;
+ rdm.verbose = (options & DMGL_VERBOSE) != 0;
+ rdm.version = 0;
++ rdm.recursion = (options & DMGL_NO_RECURSE_LIMIT) ? RUST_NO_RECURSION_LIMIT : 0;
+ rdm.bound_lifetime_depth = 0;
+
+ /* Rust symbols always start with _R (v0) or _ZN (legacy). */
diff --git a/meta/recipes-devtools/binutils/binutils/0018-CVE-2021-3530.patch b/meta/recipes-devtools/binutils/binutils/0018-CVE-2021-3530.patch
new file mode 100644
index 0000000000..e569a6bc47
--- /dev/null
+++ b/meta/recipes-devtools/binutils/binutils/0018-CVE-2021-3530.patch
@@ -0,0 +1,64 @@
+From 999566402e3d7c69032bbf47e28b44fc0926fe62 Mon Sep 17 00:00:00 2001
+From: Christopher Wellons <wellons@nullprogram.com>
+Date: Sun, 18 Jul 2021 16:57:19 -0400
+Subject: [PATCH] Change "uint" to "unsigned"
+
+This fixes a defect introduced in 25162c795. The "uint" type has not
+been explicitly defined here on mingw, causing compilation to fail.
+
+On linux we have this in /usr/include/sys/types.h
+
+/* Old compatibility names for C types. */
+typedef unsigned long int ulong;
+typedef unsigned short int ushort;
+typedef unsigned int uint;
+
+So it's easy to see how such bugs can creep in.
+
+ * rust-demangle.c (struct rust_demangler): Change type of
+ "recursion" to unsigned.
+ (RUST_NO_RECURSION_LIMIT): Similarly in cast.
+
+CVE: CVE-2021-3530
+Upstream-Status: Backport[https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=999566402e3]
+Signed-off-by: Pgowda <pgowda.cve@gmail.com>
+
+---
+ libiberty/ChangeLog | 6 ++++++
+ libiberty/rust-demangle.c | 4 ++--
+ 2 files changed, 8 insertions(+), 2 deletions(-)
+
+diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
+index 8e39fd28eba..3f749455f05 100644
+--- a/libiberty/ChangeLog
++++ b/libiberty/ChangeLog
+@@ -1,3 +1,9 @@
++2021-07-19 Christopher Wellons <wellons@nullprogram.com>
++
++ * rust-demangle.c (struct rust_demangler): Change type of
++ "recursion" to unsigned.
++ (RUST_NO_RECURSION_LIMIT): Similarly in cast.
++
+ 2021-07-15 Nick Clifton <nickc@redhat.com>
+
+ PR 99935
+diff --git a/libiberty/rust-demangle.c b/libiberty/rust-demangle.c
+index df09b7b8fdd..ac1eb8eb02c 100644
+--- a/libiberty/rust-demangle.c
++++ b/libiberty/rust-demangle.c
+@@ -75,10 +75,10 @@ struct rust_demangler
+ int version;
+
+ /* Recursion depth. */
+- uint recursion;
++ unsigned recursion;
+ /* Maximum number of times demangle_path may be called recursively. */
+ #define RUST_MAX_RECURSION_COUNT 1024
+-#define RUST_NO_RECURSION_LIMIT ((uint) -1)
++#define RUST_NO_RECURSION_LIMIT ((unsigned) -1)
+
+ uint64_t bound_lifetime_depth;
+ };
+--
+2.27.0
+