[PATCH 1/3] Make several more BFD globals thread-local

Tom Tromey tom@tromey.com
Tue Jan 30 01:03:31 GMT 2024


Among other things, PR gdb/31264 points out a race in bfd_check_format
-- it sets the error handler, which is a global.

Looking into this a bit more, I found several other possible races:
the "in_check_format" static local variable in
bfd_check_format_matches, and the contents of per_xvec_warn.

This patch makes all of these thread-local.

I don't actually think this is the best way to approach this.
"in_check_format" and the per-xvec warnings could be done by setting a
flag on the BFD, avoiding globals entirely.  I can do that if you
want; I just wasn't sure if that is desirable or not.

Note that this bug also points out a race between bfd_cache_close_all
and bfd_check_format; I am not sure yet how this should be handled.

bfd/ChangeLog
2024-01-29  Tom Tromey  <tom@tromey.com>

	PR gdb/31264
	* targets.c (per_xvec_warn): Now thread-local.
	* format.c (bfd_check_format_matches): Mark "in_check_format" as
	thread-local.
	* bfd.c (_bfd_error_internal, error_handler_bfd): Now
	thread-local.
---
 bfd/ChangeLog | 9 +++++++++
 bfd/bfd.c     | 4 ++--
 bfd/format.c  | 2 +-
 bfd/targets.c | 4 ++--
 4 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/bfd/bfd.c b/bfd/bfd.c
index 0776145af52..5619799e403 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -1504,7 +1504,7 @@ err_sprintf (void *stream, const char *fmt, ...)
 /* Communicate the bfd processed by bfd_check_format_matches to the
    error handling function error_handler_sprintf.  */
 
-static bfd *error_handler_bfd;
+static TLS bfd *error_handler_bfd;
 
 /* An error handler that prints to a string, then dups that string to
    a per-xvec cache.  */
@@ -1538,7 +1538,7 @@ error_handler_sprintf (const char *fmt, va_list ap)
    function pointer permits a program linked against BFD to intercept
    the messages and deal with them itself.  */
 
-static bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
+static TLS bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
 
 /*
 FUNCTION
diff --git a/bfd/format.c b/bfd/format.c
index 47c3e9ba35a..ec37441904c 100644
--- a/bfd/format.c
+++ b/bfd/format.c
@@ -321,7 +321,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   struct bfd_preserve preserve, preserve_match;
   bfd_cleanup cleanup = NULL;
   bfd_error_handler_type orig_error_handler;
-  static int in_check_format;
+  static TLS int in_check_format;
 
   if (matching != NULL)
     *matching = NULL;
diff --git a/bfd/targets.c b/bfd/targets.c
index 3addf2fe373..ca2a2039217 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -1458,8 +1458,8 @@ const bfd_target *const *const bfd_associated_vector = _bfd_associated_vector;
 const size_t _bfd_target_vector_entries = ARRAY_SIZE (_bfd_target_vector);
 
 /* A place to stash a warning from _bfd_check_format.  */
-static struct per_xvec_message *per_xvec_warn[ARRAY_SIZE (_bfd_target_vector)
-					      + 1];
+static TLS struct per_xvec_message *per_xvec_warn[ARRAY_SIZE (_bfd_target_vector)
+						  + 1];
 
 /* This array maps configuration triplets onto BFD vectors.  */
 
-- 
2.43.0



More information about the Binutils mailing list