]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix accessing TLS variables with no debug info
authorJan Kratochvil <jan.kratochvil@redhat.com>
Wed, 6 Sep 2017 11:32:46 +0000 (12:32 +0100)
committerPedro Alves <palves@redhat.com>
Wed, 6 Sep 2017 11:32:46 +0000 (12:32 +0100)
Since 2273f0ac95a7 ("change minsyms not to be relocated at
read-time"), printing TLS symbols of objfiles with a non-zero base
address, without debug info, fails.

E.g., with:

 $ mv /usr/lib/debug /usr/lib/debug-x

to get debug info out of the way, we get:

 $ echo 'int main(){}' | gcc -pthread -x c -
 $ ./gdb -q -ex start -ex 'p (int) errno' ./a.out
 Cannot access memory at address 0xffffef7c0698

instead of the expected:

 $1 = 0

The regression is not visible with glibc debuginfo installed.

The problem is that we compute the address of TLS minsyms incorrectly.

To trigger the problem, it is important that the variable is in an
objfile with a non-zero base address.  While glibc is a shared library
for 'errno', it's easier for the testcase to use PIE instead of a
shlib.  For TLS variables in PT_EXEC the regression obviously does not
happen.

gdb/ChangeLog
2017-09-06  Jan Kratochvil  <jan.kratochvil@redhat.com>

* parse.c (find_minsym_type_and_address): Don't relocate addresses
of TLS symbols.

gdb/testsuite/ChangeLog
2017-09-06  Jan Kratochvil  <jan.kratochvil@redhat.com>

* gdb.threads/tls-nodebug-pie.c: New file.
* gdb.threads/tls-nodebug-pie.exp: New file.

gdb/ChangeLog
gdb/parse.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.threads/tls-nodebug-pie.c [new file with mode: 0644]
gdb/testsuite/gdb.threads/tls-nodebug-pie.exp [new file with mode: 0644]

index 6d2eae58bc8dfecc5dc23269857d37b7cbabbfd9..ee15c603af473956b12cef07279ffb72d798bf84 100644 (file)
@@ -1,3 +1,8 @@
+2017-09-06  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       * parse.c (find_minsym_type_and_address): Don't relocate addresses
+       of TLS symbols.
+
 2017-09-05  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
 
        * objfiles.c (get_objfile_bfd_data): Remove useless obstack_init
index 7971f6c78c7c589247bfc4b5808977edc5cfc0a5..a11689ba7ce5053ee50a0f069f73d9b6750d0788 100644 (file)
@@ -491,11 +491,19 @@ find_minsym_type_and_address (minimal_symbol *msymbol,
 {
   bound_minimal_symbol bound_msym = {msymbol, objfile};
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
-  CORE_ADDR addr = BMSYMBOL_VALUE_ADDRESS (bound_msym);
   struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
   CORE_ADDR pc;
 
+  bool is_tls = (section != NULL
+                && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
+
+  /* Addresses of TLS symbols are really offsets into a
+     per-objfile/per-thread storage block.  */
+  CORE_ADDR addr = (is_tls
+                   ? MSYMBOL_VALUE_RAW_ADDRESS (bound_msym.minsym)
+                   : BMSYMBOL_VALUE_ADDRESS (bound_msym));
+
   /* The minimal symbol might point to a function descriptor;
      resolve it to the actual code address instead.  */
   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
@@ -525,7 +533,7 @@ find_minsym_type_and_address (minimal_symbol *msymbol,
   if (overlay_debugging)
     addr = symbol_overlayed_address (addr, section);
 
-  if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
+  if (is_tls)
     {
       /* Skip translation if caller does not need the address.  */
       if (address_p != NULL)
index b3bed5ce98a02e014c0bd16f7111f806b8d73730..3f64c6cf9e70d6f454625b84a31b868b3d94bb8e 100644 (file)
@@ -1,3 +1,8 @@
+2017-09-06  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       * gdb.threads/tls-nodebug-pie.c: New file.
+       * gdb.threads/tls-nodebug-pie.exp: New file.
+
 2017-09-05  Tom Tromey  <tom@tromey.com>
 
        * lib/gdb.exp (gdb_compile): Don't use universal_compile_options
diff --git a/gdb/testsuite/gdb.threads/tls-nodebug-pie.c b/gdb/testsuite/gdb.threads/tls-nodebug-pie.c
new file mode 100644 (file)
index 0000000..c2f62f2
--- /dev/null
@@ -0,0 +1,28 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2016-2017 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <pthread.h>
+
+__thread int thread_local = 42;
+
+int
+main (void)
+{
+  /* Ensure we link against pthreads even with --as-needed.  */
+  pthread_testcancel ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.threads/tls-nodebug-pie.exp b/gdb/testsuite/gdb.threads/tls-nodebug-pie.exp
new file mode 100644 (file)
index 0000000..ca384a0
--- /dev/null
@@ -0,0 +1,29 @@
+# Copyright 2016-2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
+                         [list "additional_flags=-fPIE -pie"]] != "" } {
+    return -1
+}
+
+clean_restart ${binfile}
+if ![runto_main] then {
+    return 0
+}
+
+# Formerly: Cannot access memory at address 0xffffef7c0698
+gdb_test "p (int) thread_local" " = 42" "thread local storage"