RFC: Ignore TLS symbols for non-TLS programs

Jan Kratochvil
2006-08-25T02:24:10Z

[-- Attachment #1: Type: text/plain, Size: 978 bytes --]

Hi,

https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=185337

currently for trivia nonthreaded helloworld with no debug info up to -ggdb2 you
will get:
(gdb) p errno
Cannot access memory at address 0x8

* with -ggdb3 "errno" gets resolved as _macro_ and the resulting
"(*__errno_location ())" expression is always fine.

* with -ggdb2 and less "errno" in fact does not exist anywhere as it was
compiled to "(*__errno_location ())" and the macro definition is not present.
Unfortunately gdb will find the TLS symbol and it will try to access it but
as the program has been compiled without -lpthread the TLS base register
(%gs on i386) is not setup and it will result in:
Cannot access memory at address 0x8

IMO the right way is to ignore TLS symbols for inferiors without activated
threading. Patch attached.

Also attached suggestion patch how to deal with the most common "errno" symbol
for the most common under-ggdb3 compiled programs.

Regards,
Jan

[-- Attachment #2: gdb-6.5-bz185337-tls-ignore-symbols-without-startup.patch --]
[-- Type: text/plain, Size: 4378 bytes --]

Index: gdb/minsyms.c
===================================================================
RCS file: /cvs/src/src/gdb/minsyms.c,v
retrieving revision 1.46
diff -u -p -r1.46 minsyms.c
--- gdb/minsyms.c 19 Jul 2006 02:17:23 -0000 1.46
+++ gdb/minsyms.c 25 Aug 2006 01:42:27 -0000
@@ -50,6 +50,7 @@
#include "demangle.h"
#include "value.h"
#include "cp-abi.h"
+#include "target.h"

/* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
At the end, copy them all into one newly allocated location on an objfile's
@@ -202,10 +203,18 @@ lookup_minimal_symbol (const char *name,
you want to test the linkage names with strcmp,
do that. If you want to test the natural names
with strcmp_iw, use SYMBOL_MATCHES_NATURAL_NAME. */
- if (strcmp (DEPRECATED_SYMBOL_NAME (msymbol), (name)) == 0
+ if ((strcmp (DEPRECATED_SYMBOL_NAME (msymbol), (name)) == 0
|| (SYMBOL_DEMANGLED_NAME (msymbol) != NULL
&& strcmp_iw (SYMBOL_DEMANGLED_NAME (msymbol),
(name)) == 0))
+ /* Ignore TLS based symbols if inferior does not run in
+ threaded mode.
+ https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=185337
+ Otherwise we would resolve glibc TLS `errno' even for
+ nonthreaded programs without any TLS base set. */
+ && ((SYMBOL_BFD_SECTION (msymbol)->flags
+ & SEC_THREAD_LOCAL) == 0
+ || target_get_thread_local_address_p()))
{
switch (MSYMBOL_TYPE (msymbol))
{
Index: gdb/testsuite/gdb.threads/tls-nopthread.c
===================================================================
RCS file: gdb/testsuite/gdb.threads/tls-nopthread.c
diff -N gdb/testsuite/gdb.threads/tls-nopthread.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gdb/testsuite/gdb.threads/tls-nopthread.c 25 Aug 2006 01:42:52 -0000
@@ -0,0 +1,10 @@
+/* Test accessing TLS based variable without -lpthread / TLS setup. */
+
+#include <pthread.h>
+
+__thread int thread_local = 42;
+
+int main(void)
+{
+ return 0;
+}
Index: gdb/testsuite/gdb.threads/tls-nopthread.exp
===================================================================
RCS file: gdb/testsuite/gdb.threads/tls-nopthread.exp
diff -N gdb/testsuite/gdb.threads/tls-nopthread.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gdb/testsuite/gdb.threads/tls-nopthread.exp 25 Aug 2006 01:42:52 -0000
@@ -0,0 +1,56 @@
+# tls.exp -- Expect script to test thread-local storage without TLS setup
+# Copyright (C) 2006 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 2 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, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+# Please email any bugs, comments, and/or additions to this file to:
+# bug-gdb@prep.ai.mit.edu
+
+set testfile tls-nopthread
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+
+if [istarget "*-*-linux"] then {
+ set target_cflags "-D_MIT_POSIX_THREADS"
+} else {
+ set target_cflags ""
+}
+
+if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable []] != "" } {
+ return -1
+}
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+
+gdb_load ${binfile}
+if ![runto_main] then {
+ fail "Can't run to main"
+ return 0
+}
+
+# formerly no debug: Cannot access memory at address 0x0
+# formerly with debug: Cannot find thread-local variables on this target
+# patched no debug: Address of symbol "thread_local" is unknown.
+# OR No symbol table is loaded. Use the "file" command.
+# patched with debug: Cannot find thread-local variables on this target
+gdb_test "p thread_local" {.*Address of symbol "thread_local" is unknown..*|.*No symbol table is loaded. Use the "file" command..*} "thread local storage"
+
+# Done!
+#
+gdb_exit
+
+return 0

[-- Attachment #3: gdb-6.5-bz185337-missing-errno-suggestion.patch --]
[-- Type: text/plain, Size: 923 bytes --]

Index: gdb/c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.35
diff -u -p -r1.35 c-exp.y
--- gdb/c-exp.y 2 Aug 2006 03:13:20 -0000 1.35
+++ gdb/c-exp.y 25 Aug 2006 01:42:21 -0000
@@ -719,8 +719,13 @@ variable: name_not_typename
else if (!have_full_symbols () && !have_partial_symbols ())
error ("No symbol table is loaded. Use the \"file\" command.");
else
- error ("No symbol \"%s\" in current context.",
- copy_name ($1.stoken));
+ {
+ /* https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=185337 */
+ if (strcmp (arg, "errno") == 0)
+ warning ("You should use symbol \"(*__errno_location ())\" or"
+ " compile the program with `gcc -ggdb3' or `gcc -pthread'.");
+ error ("No symbol \"%s\" in current context.", arg);
+ }
}
}
;