Skip to content

Commit 1b348bf

Browse files
committed
Have bfd_thread_init fail when thread-local storage is unavailable
If thread-local storage is unavailable, bfd_thread_init should fail, because in this case BFD can't be used from multiple threads -- it relies on TLS working.
1 parent a16f37e commit 1b348bf

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

bfd/bfd.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,10 @@ EXTERNAL
719719
#define EXIT_FAILURE 1
720720
#endif
721721

722-
/* Configure will leave this undefined, but it's unconditionally used
723-
in a definition later. */
724-
#ifndef TLS
725-
#define TLS
722+
#ifdef TLS
723+
#define THREAD_LOCAL TLS
724+
#else
725+
#define THREAD_LOCAL
726726
#endif
727727

728728

@@ -815,8 +815,8 @@ const char *const bfd_errmsgs[] =
815815
N_("#<invalid error code>")
816816
};
817817

818-
static TLS bfd_error_type bfd_error;
819-
static TLS char *_bfd_error_buf;
818+
static THREAD_LOCAL bfd_error_type bfd_error;
819+
static THREAD_LOCAL char *_bfd_error_buf;
820820

821821
/* Free any data associated with the BFD error. */
822822

@@ -1695,7 +1695,7 @@ _bfd_per_xvec_warn (struct per_xvec_messages *messages, size_t alloc)
16951695
error_handler_sprintf; when NULL, _bfd_error_internal will be used
16961696
instead. */
16971697

1698-
static TLS struct per_xvec_messages *error_handler_messages;
1698+
static THREAD_LOCAL struct per_xvec_messages *error_handler_messages;
16991699

17001700
/* A special value for error_handler_messages that indicates that the
17011701
error should simply be ignored. */
@@ -2028,18 +2028,20 @@ DESCRIPTION
20282028
Initialize BFD threading. The functions passed in will be
20292029
used to lock and unlock global data structures. This may only
20302030
be called a single time in a given process. Returns true on
2031-
success and false on error. DATA is passed verbatim to the
2032-
lock and unlock functions. The lock and unlock functions
2033-
should return true on success, or set the BFD error and return
2034-
false on failure. Note also that the lock must be a recursive
2035-
lock: BFD may attempt to acquire the lock when it is already
2036-
held by the current thread.
2031+
success and false on error. On error, the caller should
2032+
assume that BFD cannot be used by multiple threads. DATA is
2033+
passed verbatim to the lock and unlock functions. The lock
2034+
and unlock functions should return true on success, or set the
2035+
BFD error and return false on failure. Note also that the
2036+
lock must be a recursive lock: BFD may attempt to acquire the
2037+
lock when it is already held by the current thread.
20372038
*/
20382039

20392040
bool
20402041
bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
20412042
void *data)
20422043
{
2044+
#ifdef TLS
20432045
/* Both functions must be set, and this cannot have been called
20442046
before. */
20452047
if (lock == NULL || unlock == NULL || unlock_fn != NULL)
@@ -2052,6 +2054,12 @@ bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
20522054
unlock_fn = unlock;
20532055
lock_data = data;
20542056
return true;
2057+
#else /* TLS */
2058+
/* If thread-local storage wasn't found by configure, we disallow
2059+
threaded operation. */
2060+
bfd_set_error (bfd_error_invalid_operation);
2061+
return false;
2062+
#endif /* TLS */
20552063
}
20562064

20572065
/*

0 commit comments

Comments
 (0)