Bug 13476 - Incorrect TLS relocations created in DSOs on hppa
Summary: Incorrect TLS relocations created in DSOs on hppa
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-07 13:29 UTC by Nick Hudson
Modified: 2012-01-28 02:25 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Example code for DSO (68 bytes, text/x-csrc)
2011-12-07 13:29 UTC, Nick Hudson
Details
elf32-hppa.c.d.txt (1.52 KB, text/plain)
2011-12-08 15:06 UTC, dave.anglin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Hudson 2011-12-07 13:29:58 UTC
Created attachment 6092 [details]
Example code for DSO

This commit

http://sourceware.org/ml/binutils/2010-01/msg00594.html

now means that calls to __tls_get_addr are incorrect as the relocation is no longer adjusted to be relative to the GOT.

Looking at the final link object from, e.g.

    cc -c -O2 -fPIC -o h_tls.o h_tls.c
    cc -shared -o dso.so h_tls.o

note the addil/ldo sequence is beyond the GOT.

000004f4 <testf>:
 4f4:   6b c2 3f d9     stw rp,-14(sp)
 4f8:   6f c4 01 00     stw,ma r4,80(sp)
 4fc:   6b c3 3f 09     stw r3,-7c(sp)
 500:   08 1a 02 43     copy r26,r3
 504:   2a 60 20 00     addil L%1000,r19,r1
 508:   34 3a 00 70     ldo 38(r1),r26
Comment 1 John David Anglin 2011-12-08 01:43:58 UTC
1) The testcase doesn't show the bug.

2) The register is still r19 (PIC register).  The addil hasn't been changed
   to dp or r0, so the I fail to see how the commit referenced changed
   the situation.

The first hunk doesn't apply because this is a shared link.  The second
hunk doesn't apply because the register isn't dp.  So, we should fallthru
to the original code.
Comment 2 John David Anglin 2011-12-08 02:08:56 UTC
Looking a bit more, I think this break is questionable:

          /* Now try to make things easy for the dynamic linker.  */

          break;
Comment 3 Nick Hudson 2011-12-08 07:43:50 UTC
To hopefully show the bug clearer here's some more output.

$ cc -c -O2 -fPIC -o h_tls.o h_tls.c                                            
$
$ ../prefix/bin/hppa--netbsd-ld -v
GNU ld (GNU Binutils) 2.22.51.20111207
$
$ ../prefix/bin/hppa--netbsd-ld --eh-frame-hdr -shared -o dso.so -L /usr/lib
/usr/lib/crti.o /usr/lib/crtbeginS.o h_tls.o -lgcc -lc -lgcc /usr/lib/crtendS.o
/usr/lib/crtn.o    
$
$ nm dso.so | grep test                                                         
000004e8 T testf                                                                
$                                                                               
$ objdump -D dso.so --start-address=0x4e8 --stop-address=0x504                  

dso.so:     file format elf32-hppa-netbsd


Disassembly of section .text:

000004e8 <testf>:
 4e8:   6b c2 3f d9     stw rp,-14(sp)
 4ec:   6f c4 01 00     stw,ma r4,80(sp)
 4f0:   6b c3 3f 09     stw r3,-7c(sp)  
 4f4:   08 1a 02 43     copy r26,r3     
 4f8:   2a 60 20 00     addil L%1000,r19,r1             ! r1  = gp + 0x1000
 4fc:   34 3a 00 70     ldo 38(r1),r26                  ! r26 = gp + 0x1038
 500:   e8 5f 1d 1d     b,l 394 <_init+0x40>,rp
$ 
$ # On elf32-hppa-netbsd gp is always set to point to the GOT. GOT + 0x1038 is
$ # well outside the got
$ # 
$ objdump -h dso.so                                            


dso.so:     file format elf32-hppa-netbsd

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .hash         0000004c  000000b4  000000b4  000000b4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA       
  1 .dynsym       000000e0  00000100  00000100  00000100  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA       
  2 .dynstr       00000083  000001e0  000001e0  000001e0  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA       
  3 .rela.dyn     000000cc  00000264  00000264  00000264  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA       
  4 .rela.plt     00000024  00000330  00000330  00000330  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA       
  5 .init         00000030  00000354  00000354  00000354  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE       
  6 .text         00000384  00000384  00000384  00000384  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE       
  7 .fini         00000028  00000708  00000708  00000708  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE       
  8 .note.netbsd.ident 00000018  00000730  00000730  00000730  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA            
  9 .note.netbsd.pax 00000014  00000748  00000748  00000748  2**2  
                  CONTENTS, ALLOC, LOAD, READONLY, DATA            
 10 .PARISC.unwind 000000b0  0000075c  0000075c  0000075c  2**2    
                  CONTENTS, ALLOC, LOAD, READONLY, DATA            
 11 .eh_frame     00000004  0000080c  0000080c  0000080c  2**2     
                  CONTENTS, ALLOC, LOAD, READONLY, DATA            
 12 .ctors        00000008  00001000  00001000  00001000  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 13 .dtors        00000008  00001008  00001008  00001008  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 14 .jcr          00000004  00001010  00001010  00001010  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 15 .data.rel.ro  00000008  00001014  00001014  00001014  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 16 .dynamic      000000a8  0000101c  0000101c  0000101c  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 17 .data         00000004  000010c4  000010c4  000010c4  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 18 .plt          00000034  000010c8  000010c8  000010c8  2**2     
                  CONTENTS, ALLOC, LOAD, CODE                      
 19 .got          00000040  000010fc  000010fc  000010fc  2**2     
                  CONTENTS, ALLOC, LOAD, DATA                      
 20 .bss          00000014  0000113c  0000113c  0000113c  2**2     
                  ALLOC                                            
 21 .comment      00000042  00000000  00000000  0000113c  2**0     
                  CONTENTS, READONLY                               
 22 .debug_aranges 00000020  00000000  00000000  0000117e  2**0    
                  CONTENTS, READONLY, DEBUGGING                    
 23 .debug_pubnames 00000039  00000000  00000000  0000119e  2**0   
                  CONTENTS, READONLY, DEBUGGING                    
 24 .debug_info   000001d9  00000000  00000000  000011d7  2**0     
                  CONTENTS, READONLY, DEBUGGING                    
 25 .debug_abbrev 00000120  00000000  00000000  000013b0  2**0     
                  CONTENTS, READONLY, DEBUGGING                    
 26 .debug_line   000000e7  00000000  00000000  000014d0  2**0     
                  CONTENTS, READONLY, DEBUGGING                    
 27 .debug_frame  0000002c  00000000  00000000  000015b8  2**2
                  CONTENTS, READONLY, DEBUGGING
 28 .debug_str    00000145  00000000  00000000  000015e4  2**0
                  CONTENTS, READONLY, DEBUGGING
 29 .debug_loc    00000140  00000000  00000000  00001729  2**0
                  CONTENTS, READONLY, DEBUGGING
 30 .debug_pubtypes 0000003b  00000000  00000000  00001869  2**0
                  CONTENTS, READONLY, DEBUGGING
$ objdump -D dso.so --start-address=0x394 --stop-address=0x3a4

dso.so:     file format elf32-hppa-netbsd


Disassembly of section .text:

00000394 <__do_global_dtors_aux-0x14>:
 394:   2a 7f ff ff     addil L%-800,r19,r1         ! r1  = GOT - 0x800
 398:   48 35 0f b8     ldw 7dc(r1),r21             ! r21 = GOT - 0x14
 39c:   ea a0 c0 00     bv r0(r21)                  !     = 0x10d8
 3a0:   48 33 0f c0     ldw 7e0(r1),r19
$
$ objdump -R dso.so

dso.so:     file format elf32-hppa-netbsd

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
00001104 R_PARISC_DIR32    *ABS*+0x00001008
00001108 R_PARISC_DIR32    *ABS*+0x00001010
0000110c R_PARISC_DIR32    *ABS*+0x0000113c
00001110 R_PARISC_DIR32    *ABS*+0x00001140
00001114 R_PARISC_DIR32    *ABS*+0x00001014
00001118 R_PARISC_DIR32    *ABS*+0x00001018
0000111c R_PARISC_DIR32    *ABS*+0x00001144
00001120 R_PARISC_DIR32    *ABS*+0x00001148
00001124 R_PARISC_DIR32    *ABS*+0x00001004
00001128 R_PARISC_DIR32    *ABS*+0x0000100c
00001130 R_PARISC_DIR32    *ABS*+0x000010c4
00001014 R_PARISC_PLABEL32  __cxa_finalize
00001018 R_PARISC_PLABEL32  _Jv_RegisterClasses
000010c4 R_PARISC_DIR32    .init+0x00000d70
0000112c R_PARISC_DIR32    _GLOBAL_OFFSET_TABLE_
00001134 R_PARISC_TLS_DTPMOD32  var1
00001138 R_PARISC_TLS_DTPOFF32  var1
000010c8 R_PARISC_IPLT     _Jv_RegisterClasses
000010d0 R_PARISC_IPLT     __cxa_finalize
000010d8 R_PARISC_IPLT     __tls_get_addr             ! <--- calling this
Comment 4 dave.anglin 2011-12-08 11:54:44 UTC
Created attachment 6097 [details]
elf32-hppa.c.d.txt

On 8-Dec-11, at 2:43 AM, skrll at netbsd dot org wrote:

> --- Comment #3 from Nick Hudson <skrll at netbsd dot org> 2011-12-08  
> 07:43:50 UTC ---
> To hopefully show the bug clearer here's some more output.


I believe that I have a patch.  Will send when it has some testing.

Dave
--
John David Anglin	dave.anglin@bell.net
Comment 5 dave.anglin 2011-12-08 15:06:37 UTC
Could you try the attached change?

The PIC TLS relocs are now only changed to dp relative when !info->shared.
I looked briefly at this case this morning and it seems to work.  
However, there
are some subtle aspects to this.

Dave
Comment 6 cvs-commit@gcc.gnu.org 2011-12-11 15:42:17 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	danglin@sourceware.org	2011-12-11 15:42:11

Modified files:
	bfd            : ChangeLog elf32-hppa.c 

Log message:
	PR binutils/13476
	* elf32-hppa.c (final_link_relocate): Convert R_PARISC_TLS_GD21L,
	R_PARISC_TLS_LDM21L and R_PARISC_TLS_IE21L relocations to
	R_PARISC_DPREL21L when not doing a shared link.  Likewise convert
	R_PARISC_TLS_GD14R, R_PARISC_TLS_LDM14R and R_PARISC_TLS_IE14R to
	R_PARISC_DPREL14R.  Handle R_PARISC_TLS_GD21L, R_PARISC_TLS_LDM21L
	and R_PARISC_TLS_IE21L with R_PARISC_DLTIND21L.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5536&r2=1.5537
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-hppa.c.diff?cvsroot=src&r1=1.182&r2=1.183
Comment 7 John David Anglin 2011-12-11 15:54:11 UTC
Fixed.
Comment 8 John David Anglin 2011-12-11 15:54:35 UTC
...
Comment 9 nick.hudson 2011-12-12 07:48:11 UTC
On Sunday 11 December 2011 15:54:11 danglin at gcc dot gnu.org wrote:
> http://sourceware.org/bugzilla/show_bug.cgi?id=13476
> 
> --- Comment #7 from John David Anglin <danglin at gcc dot gnu.org>
>  2011-12-11 15:54:11 UTC --- Fixed.
> 
I can confirm this fixes the problem I saw.

Thanks,
Nick
Comment 10 cvs-commit@gcc.gnu.org 2012-01-28 02:25:40 UTC
CVSROOT:	/cvs/src
Module name:	src
Branch: 	binutils-2_22-branch
Changes by:	danglin@sourceware.org	2012-01-28 02:25:35

Modified files:
	bfd            : ChangeLog elf32-hppa.c 

Log message:
	Backport from mainline:
	2011-12-11  John Davis Anglin  <dave.anglin@nrc-cnrc.gc.ca>
	
	PR binutils/13476
	* elf32-hppa.c (final_link_relocate): Convert R_PARISC_TLS_GD21L,
	R_PARISC_TLS_LDM21L and R_PARISC_TLS_IE21L relocations to
	R_PARISC_DPREL21L when not doing a shared link.  Likewise convert
	R_PARISC_TLS_GD14R, R_PARISC_TLS_LDM14R and R_PARISC_TLS_IE14R to
	R_PARISC_DPREL14R.  Handle R_PARISC_TLS_GD21L, R_PARISC_TLS_LDM21L
	and R_PARISC_TLS_IE21L with R_PARISC_DLTIND21L.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.5473.2.34&r2=1.5473.2.35
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-hppa.c.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.179.2.1&r2=1.179.2.2