-
Notifications
You must be signed in to change notification settings - Fork 13.4k
lld: no error for referenced symbols defined in sections discarded by linker script #58891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@llvm/issue-subscribers-lld-elf |
I think the current behaviour isn't right. As you point out I think there are two logical positions, either error if there is a relocation to a discarded section or don't discard the section. My personal preference is to error as I would not want my DISCARDs from being silently undone, but I can see the argument for keeping if compatibility with ld.bfd is required. |
The temporary solution I opted for was to do something like this after processing the linker script:
This seemed to be similar to what is done in This doesn't give the "relocation refers to a symbol in a discarded section" that I would expect though, just "undefined symbol", since
I'm not confident enough in my lld knowledge to know if this is the/a correct solution. |
After linker script processing, it may be the case that sections were discarded via /DISCARD/, but relocations to symbols in those sections remain. Currently, such symbols are still considered Defined even though their section is not live and will not be placed anywhere. This results in a silently broken binary. This patch goes through the symbols after placement and changes them from Defined to Undefined if their section is no longer live at that point. During relocation processing, we will catch such undefined symbols and report an error. See llvm#58891.
lld has some very dubious behavior when dealing with symbols that would have been included in the binary, but weren't due to their section being discarded by a linker script.
I've constructed the following example; four objects, with one referencing the three others:
a.c
:b.c
,c.c
,d.c
:Linking these files normally with
main
as an entry point works fine:But if we add a linker script that discards the
.text
section ofd
:The link still succeeds, even though the binary no longer contains
baz
:Should it be like this? The relocation to
baz
inmain
will contain an invalid value, sincebaz
's section was never assigned an offset. I would expect an error about referenced symbols in discarded sections, but it doesn't appear. The binary is simply silently broken.Regular ld is also silent, but it keeps
baz
even though its section was/DISCARD/
:The text was updated successfully, but these errors were encountered: