Bug 18599 - Use .strtab section for section names in ELF object files
Summary: Use .strtab section for section names in ELF object files
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-25 16:09 UTC by H.J. Lu
Modified: 2021-11-02 06:49 UTC (History)
2 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2015-06-25 16:09:53 UTC
[hjl@gnu-6 xxx]$ cat x.S
#define GLOBAL(x) \
  .section .data.##x,"aw",%progbits; \
  .globl x; x: .byte 0

GLOBAL(xxxxxxxxxxxxxxxxx1)
GLOBAL(xxxxxxxxxxxxxxxxx2)
GLOBAL(xxxxxxxxxxxxxxxxx3)
GLOBAL(xxxxxxxxxxxxxxxxx4)
[hjl@gnu-6 xxx]$ make
gcc -B./  -c -o x.o x.S
./ld  -shared -o x.so x.o
readelf -x .strtab -x .shstrtab x.o

Hex dump of section '.shstrtab':
  0x00000000 002e7379 6d746162 002e7374 72746162 ..symtab..strtab
  0x00000010 002e7368 73747274 6162002e 74657874 ..shstrtab..text
  0x00000020 002e6461 7461002e 62737300 2e646174 ..data..bss..dat
  0x00000030 612e7878 78787878 78787878 78787878 a.xxxxxxxxxxxxxx
  0x00000040 78787831 002e6461 74612e78 78787878 xxx1..data.xxxxx
  0x00000050 78787878 78787878 78787878 32002e64 xxxxxxxxxxxx2..d
  0x00000060 6174612e 78787878 78787878 78787878 ata.xxxxxxxxxxxx
  0x00000070 78787878 7833002e 64617461 2e787878 xxxxx3..data.xxx
  0x00000080 78787878 78787878 78787878 78783400 xxxxxxxxxxxxxx4.


Hex dump of section '.strtab':
  0x00000000 00787878 78787878 78787878 78787878 .xxxxxxxxxxxxxxx
  0x00000010 78783100 78787878 78787878 78787878 xx1.xxxxxxxxxxxx
  0x00000020 78787878 78320078 78787878 78787878 xxxxx2.xxxxxxxxx
  0x00000030 78787878 78787878 33007878 78787878 xxxxxxxx3.xxxxxx
  0x00000040 78787878 78787878 78787834 00       xxxxxxxxxxx4.

[hjl@gnu-6 xxx]$ 

If we use .strtab, instead of .shstrtab, for section names in object
files, it will reduce file size for object files compiled with
"-ffunction-sections -fdata-sections".
Comment 1 Andreas Schwab 2015-06-25 17:02:18 UTC
You can save much more by stripping the object.
Comment 2 H.J. Lu 2015-06-25 17:11:13 UTC
(In reply to Andreas Schwab from comment #1)
> You can save much more by stripping the object.

I want to reduce the size of object files with symbols.
Comment 3 H.J. Lu 2015-06-26 22:20:16 UTC
I tried it on libstdc++.  For src/c++11/limits.o

Old: 93064 bytes
New: 76040 bytes

It reduces file size by 18%.
Comment 4 Fangrui Song 2021-11-02 06:49:10 UTC
This is a fairly useful object file size saving technique.

LLVM integrated assembler implemented this in 2015-05 (https://reviews.llvm.org/rG5960cee1f5299a5e7233c35fcb2518c08b761374)

"""
Building llvm+clang+lld with master and with this patch the results were:

master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes

So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
"""


See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095 for GCC -fno-unique-section-names feature request