Print every input file the linker opens with -Wl,--trace
See every object, archive and shared library GNU ld 2.43 actually opens during a link with -Wl,--trace, in the order it reads them.
When a symbol resolves to the wrong library, or a link picks up a stale archive, the quickest evidence is the list of files the linker opened. --trace (short form -t) prints each input as ld loads it, in command-line order, including the startup files the driver added behind your back.
gcc main.c -Wl,--trace -o app/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-linux-gnu/14/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/14/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/14/crtbeginS.o
/tmp/ccKfN3xQ.o
-lc (/usr/lib/gcc/x86_64-linux-gnu/14/../../../x86_64-linux-gnu/libc.so)The list is abbreviated: the real output continues with the files libc.so pulls in and the closing crtendS.o and crtn.o. The temporary object name is different every run.
Archives show up when a member is used. -lm that appears here but contributes nothing means the linker opened it and found no needed symbol. A library missing from the list was never reached, which is the usual explanation when -lfoo is placed before the object that needs it.
For a linker with the same flag, ld.lld --trace and mold --trace print their own lists. To find where one symbol came from rather than every file, --trace-symbol=name (-y name) reports each file that defines or references it.