Skip to content
systemcoding.dev
Toolchains

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.

1 min read
Summarize in AI

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.

Shell
gcc main.c -Wl,--trace -o app
Text
/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.

Taggedcld
Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.