0

I'm compiling a static executable like this:

ld.lld out/main.o -o out/sm -Tstatic.ld -static
strip --strip-all out/sm

This is the linker script I'm using:

ENTRY(_start)
SECTIONS
{
    . = 0x100e8;
    .all : {
       *(.bss*)
       *(.text*)
       *(.data*)
       *(.rodata*)
       *(COMMON*)
    } :code
    .shstrtab : {
       *(.shstrtab)
    }
    /DISCARD/ : {
      *(*)
    }
}
PHDRS
{
  code PT_LOAD FILEHDR PHDRS ;
}

The executable works as expected, but the strip command doesn't remove .shstrtab section from the executable.

If I remove the .shstrtab section from the linker script, I get this error:

ld.lld out/main.o -o out/.sm -Tstatic.ld -static
ld.lld: error: discarding .shstrtab section is not allowed

Why is the .shstrtab section necessary? I've replaced all the standard section names and the executable still works as expected, so the program loading code doesn't care about the section names.

As an aside, is it possible to completely exclude the section headers in a linker script, since it isn't needed for a static executable.

note: GNU linkers silently put .shstrtab in the output executable even if it is discarded.

fctorial
  • 765
  • 1
  • 6
  • 11

1 Answers1

0

Why is '.shstrtab' section mandatory?

Each section in the section table has a section name. It is stored as a reference to the section name table (.shstrtab).

So as long as there is at least one section in an ELF file, there must be a .shstrtab section (however, it might by named differently).

Indeed, it would be allowed to build an ELF file without any sections (but only with program headers).

However, I have never seen such an ELF file linked by a regular linker (only files that were intentionally created to be as small as possible or similar).

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38