Bug 12279

Summary: GOLD produces bad executable when mixing TLS and non-TLS symbols
Product: binutils Reporter: Dmitry Gorbachev <d.g.gorbachev>
Component: goldAssignee: Ian Lance Taylor <ian>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 2.22   
Target Milestone: ---   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Last reconfirmed:

Description Dmitry Gorbachev 2010-12-01 18:04:37 UTC
GNU gold (GNU Binutils 2.21.51.20101123) 1.10

===== 1.c =====
__thread int n;
===============

===== 2.c =====
extern int n;

int main(void)
{
  return n;
}
===============

$ gcc -c 1.c 2.c
$ ld.gold 1.o 2.o
$ ./a.out
Segmentation fault
Comment 1 Dmitry Gorbachev 2010-12-01 18:05:37 UTC
ld.bfd shows an error message:

ld.bfd: n: TLS definition in 1.o section .tbss mismatches non-TLS reference in 2.o
2.o: could not read symbols: Bad value
Comment 2 Sourceware Commits 2011-07-08 23:49:14 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	ian@sourceware.org	2011-07-08 23:49:11

Modified files:
	gold           : ChangeLog i386.cc resolve.cc symtab.cc symtab.h 
	                 x86_64.cc 

Log message:
	PR gold/12279
	* resolve.cc (Symbol_table::should_override): Add fromtype
	parameter.  Change all callers.  Give error when linking together
	TLS and non-TLS symbol.
	(Symbol_table::should_override_with_special): Add fromtype
	parameter.  Change all callers.
	* i386.cc (Target_i386::Relocate::relocate_tls): Don't crash if
	there is no TLS segment if we have reported some errors.
	* x86_64.cc (Target_x86_64::relocate_tls): Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/ChangeLog.diff?cvsroot=src&r1=1.797&r2=1.798
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/i386.cc.diff?cvsroot=src&r1=1.136&r2=1.137
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/resolve.cc.diff?cvsroot=src&r1=1.62&r2=1.63
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/symtab.cc.diff?cvsroot=src&r1=1.156&r2=1.157
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/symtab.h.diff?cvsroot=src&r1=1.123&r2=1.124
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gold/x86_64.cc.diff?cvsroot=src&r1=1.134&r2=1.135
Comment 3 Ian Lance Taylor 2011-07-08 23:50:13 UTC
This is now fixed in the development sources.

Thanks for reporting the problem.