Bug 19547 - strip --strip-unneeded adds back section symbols
Summary: strip --strip-unneeded adds back section symbols
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.27
: P2 normal
Target Milestone: 2.27
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-01 11:48 UTC by Andrei Borzenkov
Modified: 2016-02-02 11:38 UTC (History)
1 user (show)

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


Attachments
Source module before strip (2.42 KB, application/x-object)
2016-02-01 17:31 UTC, Andrei Borzenkov
Details
After strip using binutils 2.17 (202 bytes, application/x-object)
2016-02-01 17:32 UTC, Andrei Borzenkov
Details
After strip using binutils 2.24 (258 bytes, application/x-object)
2016-02-01 17:32 UTC, Andrei Borzenkov
Details
A patch (459 bytes, patch)
2016-02-01 19:05 UTC, H.J. Lu
Details | Diff
An updated patch (1.14 KB, patch)
2016-02-01 20:16 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrei Borzenkov 2016-02-01 11:48:03 UTC
As discussed on mailing list  (https://sourceware.org/ml/binutils/2016-01/msg00330.html) strip from recent binutils will add back section symbols even though they are not needed for relocations. Opening bug report as requested.
Comment 1 H.J. Lu 2016-02-01 14:22:14 UTC
Please provide a testcase.
Comment 2 Andrei Borzenkov 2016-02-01 17:31:43 UTC
Created attachment 8949 [details]
Source module before strip
Comment 3 Andrei Borzenkov 2016-02-01 17:32:32 UTC
Created attachment 8950 [details]
After strip using binutils 2.17
Comment 4 Andrei Borzenkov 2016-02-01 17:32:58 UTC
Created attachment 8951 [details]
After strip using binutils 2.24
Comment 5 Andrei Borzenkov 2016-02-01 17:35:45 UTC
Attached is all_video.module which is postprocessed for final run-time object. Post processing is done using

bor@bor-Latitude-E5450:/tmp$ objcopy -R .modename -R .moddeps all_video.module all_video.mod.tmp
bor@bor-Latitude-E5450:/tmp$ printf "all_video\0" > modname
bor@bor-Latitude-E5450:/tmp$ printf "efi_gop\0efi_uga\0video_bochs\0video_cirrus\0" > moddeps
bor@bor-Latitude-E5450:/tmp$ objcopy --add-section .modname=modname --add-section .moddeps=moddeps all_video.mod.tmp
bor@bor-Latitude-E5450:/tmp$ strip --strip-unneeded -K grub_mod_init -K grub_mod_fini -K _grub_mod_init -K _grub_mod_fini -R .note.gnu.gold-version -R .note.GNU-stack -R .note -R .comment -R .ARM.exidx all_video.mod.tmp

One version is produced using binutils 2.17, another - 2.24.

bor@bor-Latitude-E5450:/tmp$ LC_ALL=C objdump -t all_video.mod.2.17 

all_video.mod.2.17:     file format elf32-i386

SYMBOL TABLE:
no symbols


bor@bor-Latitude-E5450:/tmp$ LC_ALL=C objdump -t all_video.mod.2.24

all_video.mod.2.24:     file format elf32-i386

SYMBOL TABLE:
00000000 l    d  .text	00000000 .text
00000000 l    d  .data	00000000 .data
00000000 l    d  .module_license	00000000 .module_license
00000000 l    d  .bss	00000000 .bss
00000000 l    d  .moddeps	00000000 .moddeps
00000000 l    d  .modname	00000000 .modname
Comment 6 H.J. Lu 2016-02-01 17:42:03 UTC
Please provide a testcase in C source or x86 object.
Comment 7 Andrei Borzenkov 2016-02-01 17:56:18 UTC
I attached 3 x86 objects.
Comment 8 H.J. Lu 2016-02-01 18:35:47 UTC
Please provide the pre-processed source file.
Comment 9 H.J. Lu 2016-02-01 18:39:07 UTC
A testcase:

[hjl@gnu-6 pr19547]$ cat bar.c
static char grub_module_license[]__attribute__ ((used)) ="foobar";
[hjl@gnu-6 pr19547]$ make
gcc  -fno-asynchronous-unwind-tables -g -O   -c -o bar.o bar.c
./objcopy --strip-unneeded  bar.o x.o
readelf -s -r x.o

There are no relocations in this file.

Symbol table '.symtab' contains 6 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
[hjl@gnu-6 pr19547]$ 

The .symtab section isn't needed since there are no relocations.
Comment 10 Andrei Borzenkov 2016-02-01 18:47:46 UTC
(In reply to H.J. Lu from comment #9)
> A testcase:
> 

Thanks! Do you still need it from me or is it OK?
Comment 11 H.J. Lu 2016-02-01 18:50:46 UTC
(In reply to Andrei Borzenkov from comment #10)
> (In reply to H.J. Lu from comment #9)
> > A testcase:
> > 
> 
> Thanks! Do you still need it from me or is it OK?

Is my testcase close to yours? If yes, no need for yours. If not, please
provide yours.
Comment 12 Andrei Borzenkov 2016-02-01 18:56:12 UTC
It's pretty much the same. Thank you!
Comment 13 H.J. Lu 2016-02-01 19:05:38 UTC
Created attachment 8952 [details]
A patch

Please try this.
Comment 14 Andrei Borzenkov 2016-02-01 19:37:31 UTC
Comment on attachment 8952 [details]
A patch

Yes, this patch restores previous (2.17) behavior.
Comment 15 H.J. Lu 2016-02-01 20:16:47 UTC
Created attachment 8953 [details]
An updated patch

Please test this updated patch.
Comment 16 cvs-commit@gcc.gnu.org 2016-02-02 11:35:42 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=14f2c699ddca1e2f706342dffc59a6c7e23e844c

commit 14f2c699ddca1e2f706342dffc59a6c7e23e844c
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Feb 2 03:30:21 2016 -0800

    Clear HAS_RELOC if there are no relocations
    
    The HAS_RELOC bit should be cleared when relocations are removed from
    relocatable files.
    
    bfd/
    
    	PR binutils/19547
    	* elf.c (assign_section_numbers): Clear HAS_RELOC if there are
    	no relocations in relocatable files.
    
    binutils/
    
    	PR binutils/19547
    	* testsuite/binutils-all/objcopy.exp
    	(objcopy_test_without_global_symbol): New proc.
    	Run objcopy_test_without_global_symbol.
    	* testsuite/binutils-all/pr19547.c: New file.
Comment 17 H.J. Lu 2016-02-02 11:38:25 UTC
Fixed for 2.27.