Bug 11037

Summary: invalid code generation depending on code position
Product: binutils Reporter: serpilliere <serpilliere>
Component: gasAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: amodra, bug-binutils, hjl.tools
Priority: P2    
Version: 2.20   
Target Milestone: ---   
Host: Target: i486-*
Build: Last reconfirmed:
Bug Depends on: 10856    
Bug Blocks:    
Attachments: A patch

Description serpilliere 2009-12-01 09:06:56 UTC
good code:

.intel_syntax
.globl main
main:
	jmp	DWORD PTR[ %edx + var_1 - 0x1000 ]
var_1:
	.byte 0x11, 0x22, 0x33, 0x44

code generated: (objdump rip)

08048394 <main>:
 8048394:       ff a2 9a 73 04 08       jmp    *0x804739a(%edx)
0804839a <var_1>:
...
the jmp correctly references var_1 - 0x1000 (0804839a -0x1000 = 0x804739a)

but modified code: 

.intel_syntax
.globl main
var_1:
	.byte 0x11, 0x22, 0x33, 0x44
main:
	jmp	DWORD PTR[ %edx + var_1 - 0x1000 ]

code generated: (objdump rip)
08048394 <var_1>:
 8048394:       11 22
 8048396:       33 44

08048398 <main>:
 8048398:       ff a2 94 83 04 08       jmp    *0x8048394(%edx)

the jmp directly accesses var_1 

it seems to forget to add -0x1000 to memory deref

$ as --version
GNU assembler (GNU Binutils for Debian) 2.20
Comment 1 H.J. Lu 2009-12-05 03:38:41 UTC
It is a regression. Binutils 2.19 works correctly.
Comment 2 H.J. Lu 2009-12-05 03:40:26 UTC
It is an assembler bug.
Comment 3 H.J. Lu 2009-12-05 04:54:20 UTC
It is caused by:

http://sourceware.org/ml/binutils-cvs/2009-10/msg00245.html
Comment 4 H.J. Lu 2009-12-08 00:19:44 UTC
Created attachment 4449 [details]
A patch

The problem is in

else if (seg_left == undefined_section
	&& add_symbol != orig_add_symbol)

seg_left == undefined_section is wrong. However, add_symbol != orig_add_symbol
isn't reliable since local symbol may be converted. This patch works for the
testcase. If it is OK, I will submit a complete patch with some testcases.
Comment 5 Alan Modra 2009-12-08 01:23:24 UTC
Ah ha!  I wanted to get rid of seg_left == undefined_section too, but hadn't
spotted the problem with local symbols.  Your patch looks good except for the
calls to local_symbol_convert, which are unnecessary work and memory usage.
Comment 6 H.J. Lu 2009-12-08 01:56:01 UTC
A patch is posted at

http://sourceware.org/ml/binutils/2009-12/msg00095.html
Comment 7 Sourceware Commits 2009-12-08 03:14:51 UTC
Subject: Bug 11037

CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2009-12-08 03:14:29

Modified files:
	gas            : ChangeLog expr.c symbols.c symbols.h 
	gas/testsuite  : ChangeLog 
	gas/testsuite/gas/i386: intelpic.d intelpic.s 

Log message:
	Call symbol_same_p to check to if 2 symbols are the same.
	
	gas/
	
	2009-12-07  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR gas/11037
	* expr.c (resolve_expression): Call symbol_same_p to check
	if 2 symbols are the same.
	
	* symbols.c (symbol_same_p): New.
	* symbols.h (symbol_same_p): Likewise.
	
	gas/testsuite/
	
	2009-12-07  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR gas/11037
	* gas/i386/intelpic.s: Add testcases.
	* gas/i386/intelpic.d: Updated.

Patches:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/ChangeLog.diff?cvsroot=src&r1=1.4034&r2=1.4035
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/expr.c.diff?cvsroot=src&r1=1.79&r2=1.80
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/symbols.c.diff?cvsroot=src&r1=1.100&r2=1.101
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/symbols.h.diff?cvsroot=src&r1=1.31&r2=1.32
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1601&r2=1.1602
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/i386/intelpic.d.diff?cvsroot=src&r1=1.4&r2=1.5
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/i386/intelpic.s.diff?cvsroot=src&r1=1.2&r2=1.3

Comment 8 Alan Modra 2009-12-16 00:12:16 UTC
HJ, please apply your patch to the 2.20 branch so the bug can be closed.
Comment 9 Sourceware Commits 2009-12-16 01:52:36 UTC
Subject: Bug 11037

CVSROOT:	/cvs/src
Module name:	src
Branch: 	binutils-2_20-branch
Changes by:	hjl@sourceware.org	2009-12-16 01:52:14

Modified files:
	gas            : ChangeLog expr.c symbols.c symbols.h 
	gas/testsuite  : ChangeLog 
	gas/testsuite/gas/i386: intelpic.d intelpic.s 

Log message:
	Fix PR gas/11037.
	
	gas/
	
	2009-12-15  H.J. Lu  <hongjiu.lu@intel.com>
	
	Backport from trunk:
	2009-12-07  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR gas/11037
	* expr.c (resolve_expression): Call symbol_same_p to check
	if 2 symbols are the same.
	
	* symbols.c (symbol_same_p): New.
	* symbols.h (symbol_same_p): Likewise.
	
	gas/testsuite/
	
	2009-12-15  H.J. Lu  <hongjiu.lu@intel.com>
	
	Backport from trunk:
	2009-12-07  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR gas/11037
	* gas/i386/intelpic.s: Add testcases.
	* gas/i386/intelpic.d: Updated.
	
	2009-10-28  Alan Modra  <amodra@bigpond.net.au>
	
	* gas/i386/intelpic.d: Correct.
	
	2009-10-08  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR gas/10704
	* gas/i386/intelpic.s: Add 2 new tests.
	* gas/i386/intelpic.d: Updated.

Patches:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.3938.2.29&r2=1.3938.2.30
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/expr.c.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.77.2.2&r2=1.77.2.3
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/symbols.c.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.96.2.4&r2=1.96.2.5
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/symbols.h.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.31&r2=1.31.2.1
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.1537.2.14&r2=1.1537.2.15
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/i386/intelpic.d.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.2&r2=1.2.4.1
http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/i386/intelpic.s.diff?cvsroot=src&only_with_tag=binutils-2_20-branch&r1=1.1&r2=1.1.34.1

Comment 10 H.J. Lu 2009-12-16 01:54:06 UTC
Fixed.