Blame SOURCES/gdb-6.5-bz185337-resolve-tls-without-debuginfo-v2.patch

ab2726
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
ab2726
From: Jan Kratochvil <jan.kratochvil@redhat.com>
ab2726
Date: Fri, 27 Oct 2017 21:07:50 +0200
ab2726
Subject: gdb-6.5-bz185337-resolve-tls-without-debuginfo-v2.patch
ab2726
ab2726
;; Support TLS symbols (+`errno' suggestion if no pthread is found) (BZ 185337).
ab2726
;;=push+jan: It should be replaced by Infinity project.
ab2726
ab2726
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=185337
ab2726
ab2726
2008-02-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
ab2726
ab2726
	Port to GDB-6.8pre.
ab2726
ab2726
currently for trivial nonthreaded helloworld with no debug info up to -ggdb2 you
ab2726
will get:
ab2726
        (gdb) p errno
ab2726
        [some error]
ab2726
ab2726
* with -ggdb2 and less "errno" in fact does not exist anywhere as it was
ab2726
  compiled to "(*__errno_location ())" and the macro definition is not present.
ab2726
  Unfortunately gdb will find the TLS symbol and it will try to access it but
ab2726
  as the program has been compiled without -lpthread the TLS base register
ab2726
  (%gs on i386) is not setup and it will result in:
ab2726
        Cannot access memory at address 0x8
ab2726
ab2726
Attached suggestion patch how to deal with the most common "errno" symbol
ab2726
for the most common under-ggdb3 compiled programs.
ab2726
ab2726
Original patch hooked into target_translate_tls_address.  But its inferior
ab2726
call invalidates `struct frame *' in the callers - RH BZ 690908.
ab2726
ab2726
https://bugzilla.redhat.com/show_bug.cgi?id=1166549
ab2726
ab2726
2007-11-03  Jan Kratochvil  <jan.kratochvil@redhat.com>
ab2726
ab2726
	* ./gdb/dwarf2read.c (read_partial_die, dwarf2_linkage_name): Prefer
ab2726
	DW_AT_MIPS_linkage_name over DW_AT_name now only for non-C.
ab2726
ab2726
glibc-debuginfo-2.7-2.x86_64: /usr/lib/debug/lib64/libc.so.6.debug:
ab2726
  <81a2>     DW_AT_name        : (indirect string, offset: 0x280e): __errno_location
ab2726
  <81a8>     DW_AT_MIPS_linkage_name: (indirect string, offset: 0x2808): *__GI___errno_location
ab2726
ab2726
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
ab2726
--- a/gdb/printcmd.c
ab2726
+++ b/gdb/printcmd.c
ab2726
@@ -1306,6 +1306,10 @@ process_print_command_args (const char *args, value_print_options *print_opts,
ab2726
 
ab2726
   if (exp != nullptr && *exp)
ab2726
     {
ab2726
+      /* '*((int *(*) (void)) __errno_location) ()' is incompatible with
ab2726
+	 function descriptors.  */
ab2726
+      if (target_has_execution () && strcmp (exp, "errno") == 0)
ab2726
+	exp = "*(*(int *(*)(void)) __errno_location) ()";
ab2726
       /* VOIDPRINT is true to indicate that we do want to print a void
ab2726
 	 value, so invert it for parse_expression.  */
ab2726
       expression_up expr = parse_expression (exp, nullptr, !voidprint);
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-errno.c b/gdb/testsuite/gdb.dwarf2/dw2-errno.c
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/dw2-errno.c
ab2726
@@ -0,0 +1,28 @@
ab2726
+/* This testcase is part of GDB, the GNU debugger.
ab2726
+
ab2726
+   Copyright 2005, 2007 Free Software Foundation, Inc.
ab2726
+
ab2726
+   This program is free software; you can redistribute it and/or modify
ab2726
+   it under the terms of the GNU General Public License as published by
ab2726
+   the Free Software Foundation; either version 3 of the License, or
ab2726
+   (at your option) any later version.
ab2726
+
ab2726
+   This program is distributed in the hope that it will be useful,
ab2726
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+   GNU General Public License for more details.
ab2726
+
ab2726
+   You should have received a copy of the GNU General Public License
ab2726
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
ab2726
+
ab2726
+   Please email any bugs, comments, and/or additions to this file to:
ab2726
+   bug-gdb@prep.ai.mit.edu  */
ab2726
+
ab2726
+#include <errno.h>
ab2726
+
ab2726
+int main()
ab2726
+{
ab2726
+  errno = 42;
ab2726
+
ab2726
+  return 0;	/* breakpoint */
ab2726
+}
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-errno.exp b/gdb/testsuite/gdb.dwarf2/dw2-errno.exp
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/dw2-errno.exp
ab2726
@@ -0,0 +1,60 @@
ab2726
+# Copyright 2007 Free Software Foundation, Inc.
ab2726
+
ab2726
+# This program is free software; you can redistribute it and/or modify
ab2726
+# it under the terms of the GNU General Public License as published by
ab2726
+# the Free Software Foundation; either version 3 of the License, or
ab2726
+# (at your option) any later version.
ab2726
+#
ab2726
+# This program is distributed in the hope that it will be useful,
ab2726
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+# GNU General Public License for more details.
ab2726
+#
ab2726
+# You should have received a copy of the GNU General Public License
ab2726
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ab2726
+
ab2726
+set testfile dw2-errno
ab2726
+set srcfile ${testfile}.c
ab2726
+set binfile [standard_output_file ${testfile}]
ab2726
+
ab2726
+proc prep {} {
ab2726
+    global srcdir subdir binfile
ab2726
+    gdb_exit
ab2726
+    gdb_start
ab2726
+    gdb_reinitialize_dir $srcdir/$subdir
ab2726
+    gdb_load ${binfile}
ab2726
+
ab2726
+    runto_main
ab2726
+
ab2726
+    gdb_breakpoint [gdb_get_line_number "breakpoint"]
ab2726
+    gdb_continue_to_breakpoint "breakpoint"
ab2726
+}
ab2726
+
ab2726
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "additional_flags=-g2"] != "" } {
ab2726
+    untested "Couldn't compile test program"
ab2726
+    return -1
ab2726
+}
ab2726
+prep
ab2726
+gdb_test "print errno" ".* = 42" "errno with macros=N threads=N"
ab2726
+
ab2726
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "additional_flags=-g3"] != "" } {
ab2726
+    untested "Couldn't compile test program"
ab2726
+    return -1
ab2726
+}
ab2726
+prep
ab2726
+gdb_test "print errno" ".* = 42" "errno with macros=Y threads=N"
ab2726
+
ab2726
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "additional_flags=-g2"] != "" } {
ab2726
+    return -1
ab2726
+}
ab2726
+prep
ab2726
+gdb_test "print errno" ".* = 42" "errno with macros=N threads=Y"
ab2726
+
ab2726
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "additional_flags=-g3"] != "" } {
ab2726
+    return -1
ab2726
+}
ab2726
+prep
ab2726
+gdb_test "print errno" ".* = 42" "errno with macros=Y threads=Y"
ab2726
+
ab2726
+# TODO: Test the error on resolving ERRNO with only libc loaded.
ab2726
+# Just how to find the current libc filename?
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-errno2.c b/gdb/testsuite/gdb.dwarf2/dw2-errno2.c
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/dw2-errno2.c
ab2726
@@ -0,0 +1,28 @@
ab2726
+/* This testcase is part of GDB, the GNU debugger.
ab2726
+
ab2726
+   Copyright 2005, 2007 Free Software Foundation, Inc.
ab2726
+
ab2726
+   This program is free software; you can redistribute it and/or modify
ab2726
+   it under the terms of the GNU General Public License as published by
ab2726
+   the Free Software Foundation; either version 3 of the License, or
ab2726
+   (at your option) any later version.
ab2726
+
ab2726
+   This program is distributed in the hope that it will be useful,
ab2726
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+   GNU General Public License for more details.
ab2726
+
ab2726
+   You should have received a copy of the GNU General Public License
ab2726
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
ab2726
+
ab2726
+   Please email any bugs, comments, and/or additions to this file to:
ab2726
+   bug-gdb@prep.ai.mit.edu  */
ab2726
+
ab2726
+#include <errno.h>
ab2726
+
ab2726
+int main()
ab2726
+{
ab2726
+  errno = 42;
ab2726
+
ab2726
+  return 0;	/* breakpoint */
ab2726
+}
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-errno2.exp b/gdb/testsuite/gdb.dwarf2/dw2-errno2.exp
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/dw2-errno2.exp
ab2726
@@ -0,0 +1,71 @@
ab2726
+# Copyright 2007 Free Software Foundation, Inc.
ab2726
+
ab2726
+# This program is free software; you can redistribute it and/or modify
ab2726
+# it under the terms of the GNU General Public License as published by
ab2726
+# the Free Software Foundation; either version 3 of the License, or
ab2726
+# (at your option) any later version.
ab2726
+#
ab2726
+# This program is distributed in the hope that it will be useful,
ab2726
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+# GNU General Public License for more details.
ab2726
+#
ab2726
+# You should have received a copy of the GNU General Public License
ab2726
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ab2726
+
ab2726
+set testfile dw2-errno2
ab2726
+set srcfile ${testfile}.c
ab2726
+set binfile [standard_output_file ${testfile}]
ab2726
+
ab2726
+proc prep { message {do_xfail 0} } { with_test_prefix $message {
ab2726
+    global srcdir subdir binfile variant
ab2726
+    gdb_exit
ab2726
+    gdb_start
ab2726
+    gdb_reinitialize_dir $srcdir/$subdir
ab2726
+    gdb_load ${binfile}${variant}
ab2726
+
ab2726
+    runto_main
ab2726
+
ab2726
+    gdb_breakpoint [gdb_get_line_number "breakpoint"]
ab2726
+    gdb_continue_to_breakpoint "breakpoint"
ab2726
+
ab2726
+    gdb_test "gcore ${binfile}${variant}.core" "\r\nSaved corefile .*" "gcore $variant"
ab2726
+
ab2726
+    gdb_test "print errno" ".* = 42"
ab2726
+
ab2726
+    gdb_test "kill" ".*" "kill" {Kill the program being debugged\? \(y or n\) } "y"
ab2726
+    gdb_test "core-file ${binfile}${variant}.core" "\r\nCore was generated by .*" "core-file"
ab2726
+    if $do_xfail {
ab2726
+	setup_xfail "*-*-*"
ab2726
+    }
ab2726
+    gdb_test "print (int) errno" ".* = 42" "print errno for core"
ab2726
+}}
ab2726
+
ab2726
+set variant g2thrN
ab2726
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}${variant}" executable "additional_flags=-g2"] != "" } {
ab2726
+    untested "Couldn't compile test program"
ab2726
+    return -1
ab2726
+}
ab2726
+prep "macros=N threads=N" 1
ab2726
+
ab2726
+set variant g3thrN
ab2726
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}${variant}" executable "additional_flags=-g3"] != "" } {
ab2726
+    untested "Couldn't compile test program"
ab2726
+    return -1
ab2726
+}
ab2726
+prep "macros=Y threads=N" 1
ab2726
+
ab2726
+set variant g2thrY
ab2726
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}${variant}" executable "additional_flags=-g2"] != "" } {
ab2726
+    return -1
ab2726
+}
ab2726
+prep "macros=N threads=Y"
ab2726
+
ab2726
+set variant g3thrY
ab2726
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}${variant}" executable "additional_flags=-g3"] != "" } {
ab2726
+    return -1
ab2726
+}
ab2726
+prep "macros=Y threads=Y" 1
ab2726
+
ab2726
+# TODO: Test the error on resolving ERRNO with only libc loaded.
ab2726
+# Just how to find the current libc filename?