strip seems to have always behaved this way. versions 2.16 and older stripped both types of objects, but with 2.17 and newer, "j" is correctly left. tested 2.1{5,6,7,8,9} and 2.19.51.0.2. $ cat test.c int i; int j = 1; $ gcc -c test.c $ readelf -s test.o | grep OBJ 7: 0000000000000000 4 OBJECT GLOBAL DEFAULT 2 j 8: 0000000000000004 4 OBJECT GLOBAL DEFAULT COM i $ strip --strip-unneeded test.o $ readelf -s test.o | grep OBJ 7: 0000000000000000 4 OBJECT GLOBAL DEFAULT 2 j "i" has wrongly been stripped
A patch is posted at http://sourceware.org/ml/binutils/2009-03/msg00148.html
Subject: Bug 9933 CVSROOT: /cvs/src Module name: src Changes by: hjl@sourceware.org 2009-03-10 00:48:10 Modified files: binutils : ChangeLog objcopy.c binutils/testsuite: ChangeLog binutils/testsuite/binutils-all: objcopy.exp Added files: binutils/testsuite/binutils-all: copy-4.d Log message: binutils/ 2009-03-09 H.J. Lu <hongjiu.lu@intel.com> PR binutils/9933 * objcopy.c (filter_symbols): Properly handle common symbols in relocatable file. binutils/testsuite/ 2009-03-09 H.J. Lu <hongjiu.lu@intel.com> PR binutils/9933 * binutils-all/copy-4.d: New. * binutils-all/objcopy.exp: Run copy-4. Patches: http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/binutils/ChangeLog.diff?cvsroot=src&r1=1.1437&r2=1.1438 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/binutils/objcopy.c.diff?cvsroot=src&r1=1.125&r2=1.126 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/binutils/testsuite/ChangeLog.diff?cvsroot=src&r1=1.166&r2=1.167 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/binutils/testsuite/binutils-all/copy-4.d.diff?cvsroot=src&r1=NONE&r2=1.1 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/binutils/testsuite/binutils-all/objcopy.exp.diff?cvsroot=src&r1=1.53&r2=1.54
Fixed.
The new testcase copy-4 fails on target m68k-netbsd (using a.out). The test seems to be valid, maybe the fix didn't work for a.out objects.
The fix doesn't work on aout because EXEC_P is set for the bfd flags on your object file, thus "relocatable" in objcopy.c:filter_symbols is not set. EXEC_P is set incorrectly at bfd/aoutx.h:633, and it's been that way since rev 1.1 of the file.