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

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