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

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