Blame SOURCES/gdb-6.6-buildid-locate-solib-missing-ids.patch

a8223e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
a8223e
From: Fedora GDB patches <invalid@email.com>
a8223e
Date: Fri, 27 Oct 2017 21:07:50 +0200
a8223e
Subject: gdb-6.6-buildid-locate-solib-missing-ids.patch
a8223e
a8223e
;; Fix loading of core files without build-ids but with build-ids in executables.
a8223e
;; Load strictly build-id-checked core files only if no executable is specified
a8223e
;; (Jan Kratochvil, RH BZ 1339862).
a8223e
;;=push+jan
a8223e
a8223e
gdb returns an incorrect back trace when applying a debuginfo
a8223e
https://bugzilla.redhat.com/show_bug.cgi?id=1339862
a8223e
a8223e
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
a8223e
--- a/gdb/solib-svr4.c
a8223e
+++ b/gdb/solib-svr4.c
a8223e
@@ -1340,14 +1340,27 @@ struct svr4_library_list
a8223e
 	}
a8223e
 
a8223e
       {
a8223e
-	struct bfd_build_id *build_id;
a8223e
+	struct bfd_build_id *build_id = NULL;
a8223e
 
a8223e
 	strncpy (newobj->so_original_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
a8223e
 	newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
a8223e
 	/* May get overwritten below.  */
a8223e
 	strcpy (newobj->so_name, newobj->so_original_name);
a8223e
 
a8223e
-	build_id = build_id_addr_get (((lm_info_svr4 *) newobj->lm_info)->l_ld);
a8223e
+	/* In the case the main executable was found according to its build-id
a8223e
+	   (from a core file) prevent loading a different build of a library
a8223e
+	   with accidentally the same SO_NAME.
a8223e
+
a8223e
+	   It suppresses bogus backtraces (and prints "??" there instead) if
a8223e
+	   the on-disk files no longer match the running program version.
a8223e
+
a8223e
+	   If the main executable was not loaded according to its build-id do
a8223e
+	   not do any build-id checking of the libraries.  There may be missing
a8223e
+	   build-ids dumped in the core file and we would map all the libraries
a8223e
+	   to the only existing file loaded that time - the executable.  */
a8223e
+	if (symfile_objfile != NULL
a8223e
+	    && (symfile_objfile->flags & OBJF_BUILD_ID_CORE_LOADED) != 0)
a8223e
+	  build_id = build_id_addr_get (li->l_ld);
a8223e
 	if (build_id != NULL)
a8223e
 	  {
a8223e
 	    char *name, *build_id_filename;
a8223e
@@ -1362,23 +1375,7 @@ struct svr4_library_list
a8223e
 		xfree (name);
a8223e
 	      }
a8223e
 	    else
a8223e
-	      {
a8223e
-		debug_print_missing (newobj->so_name, build_id_filename);
a8223e
-
a8223e
-		/* In the case the main executable was found according to
a8223e
-		   its build-id (from a core file) prevent loading
a8223e
-		   a different build of a library with accidentally the
a8223e
-		   same SO_NAME.
a8223e
-
a8223e
-		   It suppresses bogus backtraces (and prints "??" there
a8223e
-		   instead) if the on-disk files no longer match the
a8223e
-		   running program version.  */
a8223e
-
a8223e
-		if (symfile_objfile != NULL
a8223e
-		    && (symfile_objfile->flags
a8223e
-			& OBJF_BUILD_ID_CORE_LOADED) != 0)
a8223e
-		  newobj->so_name[0] = 0;
a8223e
-	      }
a8223e
+	      debug_print_missing (newobj->so_name, build_id_filename);
a8223e
 
a8223e
 	    xfree (build_id_filename);
a8223e
 	    xfree (build_id);
a8223e
diff --git a/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-lib.c b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-lib.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-lib.c
a8223e
@@ -0,0 +1,21 @@
a8223e
+/* Copyright 2010 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+void
a8223e
+lib (void)
a8223e
+{
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-main.c b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-main.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib-main.c
a8223e
@@ -0,0 +1,25 @@
a8223e
+/* Copyright 2010 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+extern void lib (void);
a8223e
+
a8223e
+int
a8223e
+main (void)
a8223e
+{
a8223e
+  lib ();
a8223e
+  return 0;
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib.exp b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib.exp
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gcore-buildid-exec-but-not-solib.exp
a8223e
@@ -0,0 +1,105 @@
a8223e
+# Copyright 2016 Free Software Foundation, Inc.
a8223e
+
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+if {[skip_shlib_tests]} {
a8223e
+    return 0
a8223e
+}
a8223e
+
a8223e
+set testfile "gcore-buildid-exec-but-not-solib"
a8223e
+set srcmainfile ${testfile}-main.c
a8223e
+set srclibfile ${testfile}-lib.c
a8223e
+set libfile [standard_output_file ${testfile}-lib.so]
a8223e
+set objfile [standard_output_file ${testfile}-main.o]
a8223e
+set executable ${testfile}-main
a8223e
+set binfile [standard_output_file ${executable}]
a8223e
+set gcorefile [standard_output_file ${executable}.gcore]
a8223e
+set outdir [file dirname $binfile]
a8223e
+
a8223e
+if { [gdb_compile_shlib ${srcdir}/${subdir}/${srclibfile} ${libfile} "debug additional_flags=-Wl,--build-id"] != ""
a8223e
+     || [gdb_compile ${srcdir}/${subdir}/${srcmainfile} ${objfile} object {debug}] != "" } {
a8223e
+     unsupported "-Wl,--build-id compilation failed"
a8223e
+     return -1
a8223e
+}
a8223e
+set opts [list debug shlib=${libfile} "additional_flags=-Wl,--build-id"]
a8223e
+if { [gdb_compile ${objfile} ${binfile} executable $opts] != "" } {
a8223e
+     unsupported "-Wl,--build-id compilation failed"
a8223e
+     return -1
a8223e
+}
a8223e
+
a8223e
+clean_restart $executable
a8223e
+gdb_load_shlib $libfile
a8223e
+
a8223e
+# Does this gdb support gcore?
a8223e
+set test "help gcore"
a8223e
+gdb_test_multiple $test $test {
a8223e
+    -re "Undefined command: .gcore.*\r\n$gdb_prompt $" {
a8223e
+	# gcore command not supported -- nothing to test here.
a8223e
+	unsupported "gdb does not support gcore on this target"
a8223e
+	return -1;
a8223e
+    }
a8223e
+    -re "Save a core file .*\r\n$gdb_prompt $" {
a8223e
+	pass $test
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+if { ![runto lib] } then {
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+set escapedfilename [string_to_regexp ${gcorefile}]
a8223e
+
a8223e
+set test "save a corefile"
a8223e
+gdb_test_multiple "gcore ${gcorefile}" $test {
a8223e
+    -re "Saved corefile ${escapedfilename}\r\n$gdb_prompt $" {
a8223e
+	pass $test
a8223e
+    }
a8223e
+    -re "Can't create a corefile\r\n$gdb_prompt $" {
a8223e
+	unsupported $test
a8223e
+	return -1
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# Now restart gdb and load the corefile.
a8223e
+
a8223e
+clean_restart $executable
a8223e
+gdb_load_shlib $libfile
a8223e
+
a8223e
+set buildid [build_id_debug_filename_get $libfile]
a8223e
+
a8223e
+regsub {\.debug$} $buildid {} buildid
a8223e
+
a8223e
+set debugdir [standard_output_file ${testfile}-debugdir]
a8223e
+file delete -force -- $debugdir
a8223e
+
a8223e
+file mkdir $debugdir/[file dirname $libfile]
a8223e
+file copy $libfile $debugdir/${libfile}
a8223e
+
a8223e
+file mkdir $debugdir/[file dirname $buildid]
a8223e
+file copy $libfile $debugdir/${buildid}
a8223e
+
a8223e
+remote_exec build "ln -s /lib       ${debugdir}/"
a8223e
+remote_exec build "ln -s /lib64     ${debugdir}/"
a8223e
+# /usr is not needed, all the libs are in /lib64: libm.so.6 libc.so.6 ld-linux-x86-64.so.2
a8223e
+
a8223e
+gdb_test "set solib-absolute-prefix $debugdir"
a8223e
+
a8223e
+gdb_test_no_output "set debug-file-directory $debugdir" "set debug-file-directory"
a8223e
+
a8223e
+gdb_test "core ${gcorefile}" "Core was generated by .*" "re-load generated corefile"
a8223e
+
a8223e
+gdb_test "frame" "#0 \[^\r\n\]* lib .*" "library got loaded"
a8223e
+
a8223e
+gdb_test "bt"
a8223e
+gdb_test "info shared"