Blame SOURCES/gdb-rhbz1971095-libthread_db-update-2of5.patch

405ea9
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
405ea9
From: Kevin Buettner <kevinb@redhat.com>
405ea9
Date: Wed, 9 Jun 2021 18:07:45 -0700
405ea9
Subject: gdb-rhbz1971095-libthread_db-update-2of5.patch
405ea9
405ea9
;; Backport "libthread_db initialization changes related to upcoming
405ea9
;; glibc-2.34"
405ea9
;; (Kevin Buettner, RH BZ 19710950
405ea9
405ea9
This commit makes some adjustments to accomodate the upcoming
405ea9
glibc-2.34 release.  Beginning with glibc-2.34, functionality formerly
405ea9
contained in libpthread has been moved to libc.  For the time being,
405ea9
libpthread.so still exists in the file system, but it won't show up in
405ea9
ldd output and therefore won't be able to trigger initialization of
405ea9
libthread_db related code.  E.g...
405ea9
405ea9
Fedora 34 / glibc-2.33.9000:
405ea9
405ea9
[kev@f34-2 gdb]$ ldd testsuite/outputs/gdb.threads/tls/tls
405ea9
	linux-vdso.so.1 (0x00007ffcf94fa000)
405ea9
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007ff0ba9af000)
405ea9
	libm.so.6 => /lib64/libm.so.6 (0x00007ff0ba8d4000)
405ea9
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007ff0ba8b9000)
405ea9
	libc.so.6 => /lib64/libc.so.6 (0x00007ff0ba6c6000)
405ea9
	/lib64/ld-linux-x86-64.so.2 (0x00007ff0babf0000)
405ea9
405ea9
Fedora 34 / glibc-2.33:
405ea9
405ea9
[kev@f34-1 gdb]$ ldd testsuite/outputs/gdb.threads/tls/tls
405ea9
	linux-vdso.so.1 (0x00007fff32dc0000)
405ea9
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f815f6de000)
405ea9
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f815f4bf000)
405ea9
	libm.so.6 => /lib64/libm.so.6 (0x00007f815f37b000)
405ea9
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f815f360000)
405ea9
	libc.so.6 => /lib64/libc.so.6 (0x00007f815f191000)
405ea9
	/lib64/ld-linux-x86-64.so.2 (0x00007f815f721000)
405ea9
405ea9
Note that libpthread is missing from the ldd output for the
405ea9
glibc-2.33.9000 machine.
405ea9
405ea9
This means that (unless we happen to think of some entirely different
405ea9
mechanism), we'll now need to potentially match "libc" in addition to
405ea9
"libpthread" as libraries which might be thread libraries.  This
405ea9
accounts for the change made in solib.c.  Note that the new code
405ea9
attempts to match "/libc." via strstr().  That trailing dot (".")
405ea9
avoids inadvertently matching libraries such as libcrypt (and
405ea9
all the other many libraries which begin with "libc").
405ea9
405ea9
To avoid attempts to load libthread_db when encountering older
405ea9
versions of libc, we now attempt to find "pthread_create" (which is a
405ea9
symbol that we'd expect to be in any pthread library) in the
405ea9
associated objfile.  This accounts for the changes in
405ea9
linux-thread-db.c.
405ea9
405ea9
I think that other small adjustments will need to be made elsewhere
405ea9
too.  I've been working through regressions on my glibc-2.33.9000
405ea9
machine; I've fixed some fairly "obvious" changes in the testsuite
405ea9
(which are in other commits).  For the rest, it's not yet clear to me
405ea9
whether the handful of remaining failures represent a problem in glibc
405ea9
or gdb.  I'm still investigating, however, I'll note that these are
405ea9
problems that I only see on my glibc-2.33.9000 machine.
405ea9
405ea9
gdb/ChangeLog:
405ea9
405ea9
	* solib.c (libpthread_name_p): Match "libc" in addition
405ea9
	to "libpthread".
405ea9
	* linux-thread-db.c (libpthread_objfile_p): New function.
405ea9
	(libpthread_name_p): Adjust preexisting callers to use
405ea9
	libpthread_objfile_p().
405ea9
405ea9
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
405ea9
--- a/gdb/linux-thread-db.c
405ea9
+++ b/gdb/linux-thread-db.c
405ea9
@@ -800,6 +800,24 @@ check_thread_db (struct thread_db_info *info, bool log_progress)
405ea9
   return test_passed;
405ea9
 }
405ea9
 
405ea9
+/* Predicate which tests whether objfile OBJ refers to the library
405ea9
+   containing pthread related symbols.  Historically, this library has
405ea9
+   been named in such a way that looking for "libpthread" in the name
405ea9
+   was sufficient to identify it.  As of glibc-2.34, the C library
405ea9
+   (libc) contains the thread library symbols.  Therefore we check
405ea9
+   that the name matches a possible thread library, but we also check
405ea9
+   that it contains at least one of the symbols (pthread_create) that
405ea9
+   we'd expect to find in the thread library.  */
405ea9
+
405ea9
+static bool
405ea9
+libpthread_objfile_p (objfile *obj)
405ea9
+{
405ea9
+  return (libpthread_name_p (objfile_name (obj))
405ea9
+          && lookup_minimal_symbol ("pthread_create",
405ea9
+	                            NULL,
405ea9
+				    obj).minsym != NULL);
405ea9
+}
405ea9
+
405ea9
 /* Attempt to initialize dlopen()ed libthread_db, described by INFO.
405ea9
    Return true on success.
405ea9
    Failure could happen if libthread_db does not have symbols we expect,
405ea9
@@ -1072,7 +1090,7 @@ try_thread_db_load_from_pdir (const char *subdir)
405ea9
     return false;
405ea9
 
405ea9
   for (objfile *obj : current_program_space->objfiles ())
405ea9
-    if (libpthread_name_p (objfile_name (obj)))
405ea9
+    if (libpthread_objfile_p (obj))
405ea9
       {
405ea9
 	if (try_thread_db_load_from_pdir_1 (obj, subdir))
405ea9
 	  return true;
405ea9
@@ -1181,7 +1199,7 @@ static bool
405ea9
 has_libpthread (void)
405ea9
 {
405ea9
   for (objfile *obj : current_program_space->objfiles ())
405ea9
-    if (libpthread_name_p (objfile_name (obj)))
405ea9
+    if (libpthread_objfile_p (obj))
405ea9
       return true;
405ea9
 
405ea9
   return false;
405ea9
@@ -1295,7 +1313,7 @@ thread_db_new_objfile (struct objfile *objfile)
405ea9
 	 of the list of shared libraries to load, and in an app of several
405ea9
 	 thousand shared libraries, this can otherwise be painful.  */
405ea9
       && ((objfile->flags & OBJF_MAINLINE) != 0
405ea9
-	  || libpthread_name_p (objfile_name (objfile))))
405ea9
+	  || libpthread_objfile_p (objfile)))
405ea9
     check_for_thread_db ();
405ea9
 }
405ea9
 
405ea9
diff --git a/gdb/solib.c b/gdb/solib.c
405ea9
--- a/gdb/solib.c
405ea9
+++ b/gdb/solib.c
405ea9
@@ -906,12 +906,17 @@ Do you need \"set solib-search-path\" or \"set sysroot\"?"),
405ea9
 
405ea9
    Uses a fairly simplistic heuristic approach where we check
405ea9
    the file name against "/libpthread".  This can lead to false
405ea9
-   positives, but this should be good enough in practice.  */
405ea9
+   positives, but this should be good enough in practice.
405ea9
+
405ea9
+   As of glibc-2.34, functions formerly residing in libpthread have
405ea9
+   been moved to libc, so "/libc." needs to be checked too.  (Matching
405ea9
+   the "." will avoid matching libraries such as libcrypt.) */
405ea9
 
405ea9
 bool
405ea9
 libpthread_name_p (const char *name)
405ea9
 {
405ea9
-  return (strstr (name, "/libpthread") != NULL);
405ea9
+  return (strstr (name, "/libpthread") != NULL
405ea9
+          || strstr (name, "/libc.") != NULL );
405ea9
 }
405ea9
 
405ea9
 /* Return non-zero if SO is the libpthread shared library.  */