Blame SOURCES/gdb-rhbz1080657-tls-variable-static-linked-binary-1of3.patch

01917d
commit 9e0aa64f5510861b2517c5841b59adde8e423540
01917d
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
01917d
Date:   Mon May 19 20:20:27 2014 +0200
01917d
01917d
    Fix gdbserver qGetTLSAddr for x86_64 -m32
01917d
    
01917d
    gdbserver makes libthread_db to access uninitialized memory.  Surprisingly it
01917d
    does not harm normally, even -fsanitize=address works with current gdbserver.
01917d
    I have found just valgrind detects it as a very first warning for gdbserver:
01917d
    
01917d
    Syscall param ptrace(addr) contains uninitialised byte(s)
01917d
       at 0x3721EECEBE: ptrace (ptrace.c:45)
01917d
       by 0x436EE5: ps_get_thread_area (linux-x86-low.c:252)
01917d
       by 0x5559D02: __td_ta_lookup_th_unique (td_ta_map_lwp2thr.c:157)
01917d
       by 0x5559EC3: td_ta_map_lwp2thr (td_ta_map_lwp2thr.c:207)
01917d
       by 0x43F87D: find_one_thread (thread-db.c:281)
01917d
       by 0x440038: thread_db_get_tls_address (thread-db.c:505)
01917d
       by 0x40F6D0: handle_query (server.c:2004)
01917d
       by 0x4124CF: process_serial_event (server.c:3445)
01917d
       by 0x4136B6: handle_serial_event (server.c:3889)
01917d
       by 0x419571: handle_file_event (event-loop.c:434)
01917d
       by 0x418D38: process_event (event-loop.c:189)
01917d
       by 0x419AB7: start_event_loop (event-loop.c:552)
01917d
    
01917d
    Reproducible with:
01917d
    cd gdb/testsuite
01917d
    g++ -o gdb.threads/tls gdb.threads/tls{,2}.c -m32 -pthread
01917d
    ../gdbserver/gdbserver :1234 gdb.threads/tls
01917d
    ../gdb -batch gdb.threads/tls -ex 'target remote :1234' -ex 'b spin' -ex c -ex 'p a_thread_local'
01917d
    
01917d
    It is more easily reproducible even without valgrind using s/0x00/0xff/ in the
01917d
    attached patch.  It will then turn the output of reproducer above:
01917d
    $1 = 0
01917d
    ->
01917d
    Cannot find thread-local storage for Thread 29044, executable file .../gdb/testsuite/gdb.threads/tls:
01917d
    Remote target failed to process qGetTLSAddr request
01917d
    
01917d
    gdb/gdbserver/
01917d
    2014-05-19  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
    
01917d
    	Fix gdbserver qGetTLSAddr for x86_64 -m32.
01917d
    	* linux-x86-low.c (X86_64_USER_REGS): New.
01917d
    	(x86_fill_gregset): Call memset for BUF first in x86_64 -m32 case.
01917d
    
01917d
    Message-ID: <20140410114901.GA16411@host2.jankratochvil.net>
01917d
01917d
Index: gdb-7.6.1/gdb/gdbserver/linux-x86-low.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/linux-x86-low.c
01917d
+++ gdb-7.6.1/gdb/gdbserver/linux-x86-low.c
01917d
@@ -144,6 +144,7 @@ static const int x86_64_regmap[] =
01917d
 };
01917d
 
01917d
 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
01917d
+#define X86_64_USER_REGS (GS + 1)
01917d
 
01917d
 #else /* ! __x86_64__ */
01917d
 
01917d
@@ -275,6 +276,10 @@ x86_fill_gregset (struct regcache *regca
01917d
 	  collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
01917d
       return;
01917d
     }
01917d
+
01917d
+  /* 32-bit inferior registers need to be zero-extended.
01917d
+     Callers would read uninitialized memory otherwise.  */
01917d
+  memset (buf, 0x00, X86_64_USER_REGS * 8);
01917d
 #endif
01917d
 
01917d
   for (i = 0; i < I386_NUM_REGS; i++)