aboutsummaryrefslogtreecommitdiff
path: root/bfd/libbfd.h
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2024-02-12 18:06:56 -0700
committerTom Tromey <tom@tromey.com>2024-04-16 14:01:43 -0600
commitbacc61fd3e6fd61a59fb59bcc657be17a381520d (patch)
tree62c1fb63d1e326c4a8f921b7a5a16819626920ef /bfd/libbfd.h
parent6732c57eeea61d72ad1046fae1dd0d00920150e1 (diff)
downloadgdb-bacc61fd3e6fd61a59fb59bcc657be17a381520d.zip
gdb-bacc61fd3e6fd61a59fb59bcc657be17a381520d.tar.gz
gdb-bacc61fd3e6fd61a59fb59bcc657be17a381520d.tar.bz2
Thread-safety improvements for bfd_check_format_matches
A gdb bug found that bfd_check_format_matches has some data races when called from multiple threads. In particular, it changes the BFD error handler, which is a global. It also has a local static variable ("in_check_format") that is used for recursion detection. And, finally, it may emit warnings to the per-xvec warning array, which is a global. This patch removes all the races here. The first part of patch is to change _bfd_error_handler to directly handle the needs of bfd_check_format_matches. This way, the error handler does not need to be changed. This change lets us use the new per-thread global (error_handler_messages, replacing error_handler_bfd) to also remove the need for in_check_format -- a single variable suffices. Finally, the global per-xvec array is replaced with a new type that holds the error messages. The outermost such type is stack-allocated in bfd_check_format_matches. I tested this using the binutils test suite. I also built gdb with thread sanitizer and ran the test case that was noted as failing. Finally, Alan sent me the test file that caused the addition of the xvec warning code in the first place, and I confirmed that "nm-new" has the same behavior on this file both before and after this patch. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31264 Co-Authored-By: Alan Modra <amodra@gmail.com>
Diffstat (limited to 'bfd/libbfd.h')
-rw-r--r--bfd/libbfd.h33
1 files changed, 23 insertions, 10 deletions
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
index 5f5ad2d..d106262 100644
--- a/bfd/libbfd.h
+++ b/bfd/libbfd.h
@@ -963,7 +963,29 @@ void _bfd_clear_error_data (void) ATTRIBUTE_HIDDEN;
char *bfd_asprintf (const char *fmt, ...) ATTRIBUTE_HIDDEN;
-bfd_error_handler_type _bfd_set_error_handler_caching (bfd *) ATTRIBUTE_HIDDEN;
+/* Cached _bfd_check_format messages are put in this. */
+struct per_xvec_message
+{
+ struct per_xvec_message *next;
+ char message[];
+};
+
+/* A list of per_xvec_message objects. The targ field indicates
+ which xvec this list holds; PER_XVEC_NO_TARGET is only set for the
+ root of the list and indicates that the entry isn't yet used. The
+ abfd field is only needed in the root entry of the list. */
+struct per_xvec_messages
+{
+ bfd *abfd;
+ const bfd_target *targ;
+ struct per_xvec_message *messages;
+ struct per_xvec_messages *next;
+};
+
+#define PER_XVEC_NO_TARGET ((const bfd_target *) -1)
+struct per_xvec_messages *_bfd_set_error_handler_caching (struct per_xvec_messages *) ATTRIBUTE_HIDDEN;
+
+void _bfd_restore_error_handler_caching (struct per_xvec_messages *) ATTRIBUTE_HIDDEN;
const char *_bfd_get_error_program_name (void) ATTRIBUTE_HIDDEN;
@@ -3708,15 +3730,6 @@ bool _bfd_write_stab_strings (bfd *, struct stab_info *) ATTRIBUTE_HIDDEN;
bfd_vma _bfd_stab_section_offset (asection *, void *, bfd_vma) ATTRIBUTE_HIDDEN;
/* Extracted from targets.c. */
-/* Cached _bfd_check_format messages are put in this. */
-struct per_xvec_message
-{
- struct per_xvec_message *next;
- char message[];
-};
-
-struct per_xvec_message **_bfd_per_xvec_warn (const bfd_target *, size_t) ATTRIBUTE_HIDDEN;
-
#ifdef __cplusplus
}
#endif