Bug 225890 - 'strip' corrupts static libraries
Summary: 'strip' corrupts static libraries
Status: Closed Works As Intended
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 11.1-STABLE
Hardware: amd64 Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL: http://bpaste.net/show/8y6wgjoHTK5tpa...
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-14 13:48 UTC by Sergey
Modified: 2018-02-14 20:11 UTC (History)
14 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey 2018-02-14 13:48:04 UTC
+++ This bug was initially created as a clone of Bug #191587 +++

the issue is similar to Bug #191587, 'strip' removes not requested sections

$ echo 'int fun(int x){return x+42;}' > misc.c && cc -c misc.c && ar -rc misc.a misc.o && cp misc.a misc2.a
$ strip -R .comment misc.a
$ ranlib misc.a
ranlib: warning: can't find .strtab section
$ readelf -e misc.a

File: misc.a(misc.o)
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 09 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            FreeBSD
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Advanced Micro Devices x86-64
  Version:                           0x1
  Entry point address:               0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          192 (bytes into file)
  Flags:                             0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           64 (bytes)
  Number of section headers:         6
  Section header string table index: 5

There are no program headers in this file.
There are 6 section headers, starting at offset 0xc0:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000000000  00000040
       0000000000000011  0000000000000000  AX       0     0     16
  [ 2] .note.GNU-stack   PROGBITS         0000000000000000  00000051
       0000000000000000  0000000000000000           0     0     1
  [ 3] .eh_frame         X86_64_UNWIND    0000000000000000  00000058
       0000000000000038  0000000000000000   A       0     0     8
  [ 4] .rela.eh_frame    RELA             0000000000000000  00000090
       0000000000000000  0000000000000018   I       0     3     8
  [ 5] .shstrtab         STRTAB           0000000000000000  00000090
       0000000000000030  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
$ readelf --string-dump .strtab misc.a

File: misc.a(misc.o)
Comment 1 Ed Maste freebsd_committer freebsd_triage 2018-02-14 14:27:42 UTC
GNU strip behaves the same way, and I would suggest using objcopy instead. GNU strip's man page reports that -R <sectionnme> removes "any section named <sectionname> from the output file, in addition to whatever sections would otherwise be removed."

WRT ELF Tool Chain strip, it appears it defaults to STRIP_ALL (-s) unless one of these options is set:
-S,-g,-d, --only-keep-debug, --strip-unneeded, -x, -X, -N

It seems to me that -R should be included in the list, and that the behaviour currently provided by -R should instead be obtained with -R -s, but that would be a divergence from GNU strip that we probably do not want to have.
Comment 2 Sergey 2018-02-14 18:44:45 UTC
Thank you Ed, then I'm closing it, as explained :)