Jan Kratochvil d08a20f
http://sourceware.org/ml/gdb-patches/2016-10/msg00207.html
Jan Kratochvil d08a20f
Subject: [patch+7.12.1 2/2] Fix TLS (such as 'errno') regression
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
--3Pql8miugIZX0722
Jan Kratochvil d08a20f
Content-Type: text/plain; charset=us-ascii
Jan Kratochvil d08a20f
Content-Disposition: inline
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
Hi,
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
2273f0ac95a79ce29ef42025c63f90e82cf907d7 is the first bad commit
Jan Kratochvil d08a20f
commit 2273f0ac95a79ce29ef42025c63f90e82cf907d7
Jan Kratochvil d08a20f
Author: Tom Tromey <tromey@redhat.com>
Jan Kratochvil d08a20f
Date:   Tue Oct 15 13:28:57 2013 -0600
Jan Kratochvil d08a20f
    change minsyms not to be relocated at read-time
Jan Kratochvil d08a20f
[FYI v3 06/10] change minsyms not to be relocated at read-time
Jan Kratochvil d08a20f
Message-Id: <1393441273-32268-7-git-send-email-tromey@redhat.com>
Jan Kratochvil d08a20f
https://sourceware.org/ml/gdb-patches/2014-02/msg00798.html
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
mv /usr/lib/debug /usr/lib/debug-x
Jan Kratochvil d08a20f
echo 'int main(){}'|gcc -pthread -x c -
Jan Kratochvil d08a20f
./gdb -q -ex start -ex 'set debug expr 1' -ex 'p errno' ./a.out 
Jan Kratochvil d08a20f
	    0  UNOP_MEMVAL_TLS       TLS type @0x35df7e0 (__thread /* "/lib64/libc.so.6" */ <thread local variable, no debug info>)
Jan Kratochvil d08a20f
	    4    OP_LONG               Type @0x35df850 (__CORE_ADDR), value 140737345728528 (0x7ffff77fb010)
Jan Kratochvil d08a20f
Cannot access memory at address 0xffffef7c9698
Jan Kratochvil d08a20f
->
Jan Kratochvil d08a20f
	    0  UNOP_MEMVAL_TLS       TLS type @0x3ad9520 (__thread /* "/lib64/libc.so.6" */ <thread local variable, no debug info>)
Jan Kratochvil d08a20f
	    4    OP_LONG               Type @0x3ad9590 (__CORE_ADDR), value 16 (0x10)
Jan Kratochvil d08a20f
$1 = 0
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
With glibc debuginfo, that is without: mv /usr/lib/debug /usr/lib/debug-x
Jan Kratochvil d08a20f
	    0  OP_VAR_VALUE          Block @0x3b30e70, symbol @0x3b30d10 (errno)
Jan Kratochvil d08a20f
$1 = 0
Jan Kratochvil d08a20f
So such case is unrelated to this patch and the regression is not visible with
Jan Kratochvil d08a20f
glibc debuginfo installed.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
I guess all these issues will be solved by Gary Benson's Infinity.
Jan Kratochvil d08a20f
But at least for older non-Infinity glibcs GDB should not regress.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
For the testcase it is important the variable is in objfile with non-zero base
Jan Kratochvil d08a20f
address.  glibc is a shared library for 'errno' but I found easier for the
Jan Kratochvil d08a20f
testcase to use PIE instead of a shlib.  For TLS variables in PT_EXEC the
Jan Kratochvil d08a20f
regression obviously does not happen.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
It has been found by a more complete testcase present in Fedora, the fix there
Jan Kratochvil d08a20f
also solves more cases where FSF GDB currently cannot resolve 'errno':
Jan Kratochvil d08a20f
	http://pkgs.fedoraproject.org/cgit/rpms/gdb.git/tree/gdb-6.5-bz185337-resolve-tls-without-debuginfo-v2.patch
Jan Kratochvil d08a20f
	FAIL: gdb.dwarf2/dw2-errno2.exp: macros=N threads=Y: print errno for core
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
No regressions on {x86_64,x86_64-m32,i686}-fedora26pre-linux-gnu.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
OK for check-in?
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
Thanks,
Jan Kratochvil d08a20f
Jan
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
--3Pql8miugIZX0722
Jan Kratochvil d08a20f
Content-Type: text/plain; charset=us-ascii
Jan Kratochvil d08a20f
Content-Disposition: inline; filename="tls1-2.patch"
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
gdb/ChangeLog
Jan Kratochvil d08a20f
2016-10-09  Jan Kratochvil  <jan.kratochvil@redhat.com>
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
	* parse.c (write_exp_msymbol): Fix ADDR computation.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
gdb/testsuite/ChangeLog
Jan Kratochvil d08a20f
2016-10-09  Jan Kratochvil  <jan.kratochvil@redhat.com>
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
	* gdb.threads/tls-nodebug-pie.c: New file.
Jan Kratochvil d08a20f
	* gdb.threads/tls-nodebug-pie.exp: New file.
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
--- a/gdb/parse.c
Jan Kratochvil d08a20f
+++ b/gdb/parse.c
Jan Kratochvil d08a20f
@@ -480,13 +480,17 @@ write_exp_msymbol (struct parser_state *ps,
Jan Kratochvil d08a20f
   struct objfile *objfile = bound_msym.objfile;
Jan Kratochvil d08a20f
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
Jan Kratochvil d08a20f
 
Jan Kratochvil d08a20f
-  CORE_ADDR addr = BMSYMBOL_VALUE_ADDRESS (bound_msym);
Jan Kratochvil d08a20f
   struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
Jan Kratochvil d08a20f
   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
Jan Kratochvil d08a20f
-  CORE_ADDR pc;
Jan Kratochvil d08a20f
+  CORE_ADDR addr, pc;
Jan Kratochvil d08a20f
   const int is_tls = (section != NULL
Jan Kratochvil d08a20f
 		      && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
Jan Kratochvil d08a20f
 
Jan Kratochvil d08a20f
+  if (is_tls)
Jan Kratochvil d08a20f
+    addr = MSYMBOL_VALUE_RAW_ADDRESS (bound_msym.minsym);
Jan Kratochvil d08a20f
+  else
Jan Kratochvil d08a20f
+    addr = BMSYMBOL_VALUE_ADDRESS (bound_msym);
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
   /* The minimal symbol might point to a function descriptor;
Jan Kratochvil d08a20f
      resolve it to the actual code address instead.  */
Jan Kratochvil d08a20f
   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
Jan Kratochvil d08a20f
--- /dev/null
Jan Kratochvil d08a20f
+++ b/gdb/testsuite/gdb.threads/tls-nodebug-pie.c
Jan Kratochvil d08a20f
@@ -0,0 +1,27 @@
Jan Kratochvil d08a20f
+/* This testcase is part of GDB, the GNU debugger.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+   Copyright 2016 Free Software Foundation, Inc.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+   This program is free software; you can redistribute it and/or modify
Jan Kratochvil d08a20f
+   it under the terms of the GNU General Public License as published by
Jan Kratochvil d08a20f
+   the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil d08a20f
+   (at your option) any later version.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+   This program is distributed in the hope that it will be useful,
Jan Kratochvil d08a20f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil d08a20f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil d08a20f
+   GNU General Public License for more details.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+   You should have received a copy of the GNU General Public License
Jan Kratochvil d08a20f
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+#include <pthread.h>
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+__thread int thread_local = 42;
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+int main(void)
Jan Kratochvil d08a20f
+{
Jan Kratochvil d08a20f
+  /* Ensure we link against pthreads even with --as-needed.  */
Jan Kratochvil d08a20f
+  pthread_testcancel();
Jan Kratochvil d08a20f
+  return 0;
Jan Kratochvil d08a20f
+}
Jan Kratochvil d08a20f
--- /dev/null
Jan Kratochvil d08a20f
+++ b/gdb/testsuite/gdb.threads/tls-nodebug-pie.exp
Jan Kratochvil d08a20f
@@ -0,0 +1,29 @@
Jan Kratochvil d08a20f
+# Copyright 2016 Free Software Foundation, Inc.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil d08a20f
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil d08a20f
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil d08a20f
+# (at your option) any later version.
Jan Kratochvil d08a20f
+#
Jan Kratochvil d08a20f
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil d08a20f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil d08a20f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil d08a20f
+# GNU General Public License for more details.
Jan Kratochvil d08a20f
+#
Jan Kratochvil d08a20f
+# You should have received a copy of the GNU General Public License
Jan Kratochvil d08a20f
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+standard_testfile
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
Jan Kratochvil d08a20f
+			  [list "additional_flags=-fPIE -pie"]] != "" } {
Jan Kratochvil d08a20f
+    return -1
Jan Kratochvil d08a20f
+}
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+clean_restart ${binfile}
Jan Kratochvil d08a20f
+if ![runto_main] then {
Jan Kratochvil d08a20f
+   return 0
Jan Kratochvil d08a20f
+}
Jan Kratochvil d08a20f
+
Jan Kratochvil d08a20f
+# Formerly: Cannot access memory at address 0xd5554d5216fc
Jan Kratochvil d08a20f
+gdb_test "p thread_local" " = 42" "thread local storage"
Jan Kratochvil d08a20f
Jan Kratochvil d08a20f
--3Pql8miugIZX0722--
Jan Kratochvil d08a20f