llvm / llvm/llvm-project

lld --as-needed sometimes leaves behind stale DT_NEEDED entry, difference from BFD linker

Open
#222,844 0 comments 0 reactions 0 assignees View on GitHub
lld
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Hi! Downstream at Arch Linux we have noticed a slight difference in the behaviour of `lld` vs the BFD linker. Essentially, some binaries linked by `lld` leave behind a stale DT_NEEDED entry for `ld-linux-x86-64.so.2`. This causes us a (minor) problem, in that running `ldd -u` on the binary complains about "Unused direct dependencies".

The issue came to light as part of our ELF linting procedure when run on binaries produced by our Rust toolchain, which now defaults to `lld` as the linker. But please note, the issue can be reproduced with only a C toolchain. Side note: it appears the `mold` linker behaves similarly to `lld`.

Please find attached a minimal reproducer that was put together with LLM assistance by one of our valued contributors. Downstream issue here: https://gitlab.archlinux.org/archlinux/packaging/packages/lld/-/work_items/15

Thanks in advance
(apologies if it's come up previously.. but I couldn't find anything...)

[tls_gd.c](https://github.com/user-attachments/files/32087491/tls_gd.c)

[repro.sh](https://github.com/user-attachments/files/32087496/repro.sh)

Contributor guide

Open the contributing guide

Research direction

Start by running the attached repro.sh with tls_gd.c and compare lld's output with the BFD linker. Inspect the generated DT_NEEDED entries and the result of ldd -u; done means lld no longer leaves an unused ld-linux-x86-64.so.2 dependency in the reproduced binary.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.