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

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