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

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