RFC: Can static executables contain relocations against symbols ?

Michael Matz matz@suse.de
Wed Mar 29 16:00:12 GMT 2023


Hello,

On Wed, 29 Mar 2023, Nick Clifton via Binutils wrote:

>   Can static executables contain relocations against symbols ?

Generally I agree with you that they should not.  Only symbol-less 
relocations make sense (not all of them will be absolute, though.  In PIEs 
they will be base-relative, still symbolless of course).  But for the sake 
of discussion let's assume we somehow want to allow symbol-based relocs 
nevertheless.  Then:

In final-linked ELF files symbol headers always have to be optional.  The 
place of relocations, if necessary, (and symbols) needs to be communicated 
in a different way anyway (on dynamic ELF files via the program headers 
and dynamic entries, on static files via some other side channel, like 
hardcoding the offset/addresses of the interesting pieces).

But: there is another observation that makes the Rust output invalid.  An 
allocated (SHF_ALLOC) section (here .got.rela) _must_ only refer to 
allocated sections via its sh_link field.  The .symtab section in the 
example at hand was not SHF_ALLOC.  So that's another bug.

Leaving a sh_link field of an SHT_RELA section to be zero seems a bit 
dubious.  Normally client code would be able to rely on sh_link containing 
an existing section index.  OTOH SHN_UNDEF (i.e. zero) _is_ somewhat of a 
section index, so leaving it as zero might just be fine.

So, even if the above problems would be fixed I think your patch still 
makes sense.  Perhaps add another check to only leave it zero if 
elf_onesymtab is not allocated itself.


Ciao,
Michael.


More information about the Binutils mailing list