RFC: Can static executables contain relocations against symbols ?
Nick Clifton
nickc@redhat.com
Wed Mar 29 14:39:46 GMT 2023
Hi Guys,
Can static executables contain relocations against symbols ?
This question has come up in Fedora as part of the investigation into
a problem linking some binaries compiled with the Rust compiler:
https://bugzilla.redhat.com/show_bug.cgi?id=2166149
The issue appears to be that static-pie executables are being created
with a .rela.got section that has an sh_link field set to point to the
.symtab section. This in turns means that running strip on the
executables will not remove the .symtab section, which is then being
flagged as an error by the build system.
The problem is not happening for x86_64 binaries because they contain
a .dynsym section, and the code in bfd/elf.c:assign_section_numbers
will preferentially use that section if it exists. (It is not clear
to me *why* x86_64 static pie binaries contain a .dynsym section, but
they do).
It is possible for static executables to contain relocations - for
ifuncs for example - but I think that they should always be absolute.
So my question is: is it safe to set the sh_link field for relocation
sections in static executables to 0 ?
The attached patch is a suggestion for the change I am considering...
Thoughts ?
Cheers
Nick
diff --git a/bfd/elf.c b/bfd/elf.c
index 45e53640e8f..0b5c8c79fd6 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3884,7 +3884,25 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
d->this_hdr.sh_link = elf_section_data (s)->this_idx;
}
if (d->this_hdr.sh_link == 0)
- d->this_hdr.sh_link = elf_onesymtab (abfd);
+ {
+ /* In general static executables should not need relocations.
+ There are exceptions however, for ifuncs for example, but
+ in these cases the relocations should not need any symbols.
+ Hence it should be safe to leave the sh_link field as 0.
+ Not setting sh_link allows the symbol table to be stripped
+ from the executable, which is a desirable trait.
+
+ FIXME: Should we scan the relocations first to make sure
+ that there are no symbol references ? */
+ if (link_info != NULL
+ && bfd_link_executable (link_info)
+ && (abfd->flags & DYNAMIC) == 0)
+ ;
+ else
+ d->this_hdr.sh_link = elf_onesymtab (abfd);
+ }
s = elf_get_reloc_section (sec);
if (s != NULL)
More information about the Binutils
mailing list