Bug 31200 - Surprising behavior of DISCARD section and explicitly mentioned archive file
Summary: Surprising behavior of DISCARD section and explicitly mentioned archive file
Status: UNCONFIRMED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.38
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-31 06:00 UTC by Pete Dietl
Modified: 2023-12-31 06:10 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
minimal reproduction of problem (1.19 KB, application/x-tar)
2023-12-31 06:00 UTC, Pete Dietl
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pete Dietl 2023-12-31 06:00:55 UTC
Created attachment 15274 [details]
minimal reproduction of problem

I am observing that the sections of an archive file explicitly mentioned in an input section of a linker script within the /DISCARD/ output section does not get discarded if the -T option on an ld invocation is given after mentioning a .o file that has an undefined symbol which is found in a section of an object file within said archive. It was not clear to me in the documentation what the expected behavior is. Attached is a Makefile and sources reproduction on an x86_64 Ubuntu machine.

When I run `make`, I get an error like so: undefined reference to `libfoo_getmsg'/
When I run `make clean && make SCRIPT_LAST=1`, it works.

It seems that indeed the position of the linker script is a key factor.

Furthermore, if one changes the linker script from `libfoo.a (*)` to `foo.o (*)`, the `make` command succeeds regardless of the position of the linker script argument.

I suspect that all of this has some complex explanation involving wildcard matches, but I couldn't figure it out.
Comment 1 Pete Dietl 2023-12-31 06:03:23 UTC
Another interesting note, a `.o` file mentioned on the command line is only searched for the current directory, but a `.o` file mentioned in an input section description gets searched for in the current directory and in the library search path. Perhaps this distinction should be mentioned in the manual?
Comment 2 Pete Dietl 2023-12-31 06:10:45 UTC
Oh one more note, `gold` discards the section no matter what.