ld mixing -s and --emit-relocs

Fangrui Song maskray@sourceware.org
Sat Sep 20 17:57:06 GMT 2025


On Sat, Sep 20, 2025 at 7:37 AM Paul Edwards <mutazilah@gmail.com> wrote:
>
> I would have thought that it was perfectly fine to mix -s
> and --emit-relocs when using ld, and the result should
> be the same as
>
> strip --strip-unneeded
>
> But it simply says "invalid operation" and I have no idea
> what the complaint is.
>
> I worked around the problem for years by using
> gcc -s -L,--emit-relocs
>
> and it figured it out for itself.
>
> But tonight I stumbled upon the reason.
>
> Not sure if you consider it a bug or not, but reporting it anyway.
>
> Platform showing the problem is x64, but there was
> another platform too (loongarch64), but not arm64
> (ie arm64 lets me combine them).
>
> I can provide example binaries if you think it is a bug
> and you need them.
>
> And I'm using binutils 2.45
>
> Thanks. Paul.

In ld.lld we just reject it when --emit-relocs & -s are used together.
Output from a few linkers

% ld.lld --emit-relocs -s a1.o -o a && readelf -WS a
ld.lld: error: --strip-all and --emit-relocs may not be used together
% ld.bfd --emit-relocs -s a1.o -o a && readelf -WS a   # x86-64 ; ISTM
emit-relocs needed symbols are retained while other symbols are
stripped.
ld.bfd: warning: cannot find entry symbol _start; defaulting to 0000000000401000
There are 6 section headers, starting at offset 0x1108:

Section Headers:
  [Nr] Name              Type            Address          Off    Size
 ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000
000000 00      0   0  0
  [ 1] .text             PROGBITS        0000000000401000 001000
000018 00  AX  0   0  4
  [ 2] .rela.text        RELA            0000000000000000 001080
000060 18   I  3   1  8
  [ 3] .symtab           SYMTAB          0000000000000000 001018
000060 18      4   2  8
  [ 4] .strtab           STRTAB          0000000000000000 001078
000008 00      0   0  1
  [ 5] .shstrtab         STRTAB          0000000000000000 0010e0
000026 00      0   0  1
% gold  --emit-relocs -s a1.o -o a && readelf -WS a
There are 5 section headers, starting at offset 0x140:

Section Headers:
  [Nr] Name              Type            Address          Off    Size
 ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000
000000 00      0   0  0
  [ 1] .text             PROGBITS        0000000000400078 000078
000018 00  AX  0   0  4
  [ 2] .rela.text        RELA            0000000000000000 000090
000060 18   I  0   1  8
  [ 3] .note.gnu.gold-version NOTE            0000000000000000 0000f0
00001c 00      0   0  4
  [ 4] .shstrtab         STRTAB          0000000000000000 00010c
00002d 00      0   0  1
...
% ~/Dev/mold/out/release/mold  --emit-relocs -s a1.o -o a && readelf -WS a
mold: warning: entry symbol is not defined: _start
There are 10 section headers, starting at offset 0x2f8:

Section Headers:
  [Nr] Name              Type            Address          Off    Size
 ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000
000000 00      0   0  0
  [ 1] .eh_frame         PROGBITS        0000000000200200 000200
000004 00   A  0   0  8
  [ 2] .eh_frame_hdr     PROGBITS        0000000000200204 000204
00000c 00   A  0   0  4
  [ 3] .text             PROGBITS        0000000000201210 000210
000018 00  AX  0   0  4
  [ 4] .got              PROGBITS        0000000000202228 000228
000008 00  WA  0   0  8
  [ 5] .relro_padding    NOBITS          0000000000202230 000230
000dd0 00  WA  0   0  1
  [ 6] .got.plt          PROGBITS        0000000000203230 000230
000018 00  WA  0   0  8
  [ 7] .comment          PROGBITS        0000000000000000 000248
00004f 01  MS  0   0  1
  [ 8] .shstrtab         STRTAB          0000000000000000 000297
00005a 00      0   0  1
  [ 9] .rela.text        RELA            0000000000000000 000578
000060 18   I  0   3  8


More information about the Binutils mailing list