Skip to content
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

Failed to find symbols in linked external libraries on Linux #132023

Closed
inflation opened this issue Oct 22, 2024 · 3 comments
Closed

Failed to find symbols in linked external libraries on Linux #132023

inflation opened this issue Oct 22, 2024 · 3 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@inflation
Copy link

Code

Not sure if it's Rustc's problem or cargo's problem, but 1.82.0 failed to find symbols in external libraries when linking. This only happens on Linux, macOS works fine on the CI, and Windows works fine by testing locally.

git clone https://github.com/inflation/jpegxl-rs --recursive
cd jpegxl-rs
cargo test --all-features

inflation/jpegxl-rs#104

I expected to see this happen: links fine in testing

Instead, this happened: 1.82.0 failed to find symbols in libbrotlicommon.a

Linker commands related to external libraries:

"-Wl,--as-needed" "-Wl,-Bstatic" "-ljxl" "-ljxl_cms" "-ljxl_threads" "-lhwy" "-lbrotlicommon" "-lbrotlidec" "-lbrotlienc" "-Wl,-Bdynamic" "-lstdc++"

Version it worked on

It most recently worked on: 1.81.0

Version with regression

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

@inflation inflation added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Oct 22, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-untriaged Untriaged performance or correctness regression. labels Oct 22, 2024
@saethlin
Copy link
Member

saethlin commented Oct 22, 2024

Until 1.82, rustc passed the linker flag --whole-archive when building tests. This flag was added a while ago as a hack, and since it's now possible to always add it yourself if you actually need it, the hack was removed.

The actual reason you have a linker error is probably a link order issue. The GNU linker which I think every distro ships by default is really stupid and processes the contents of libraries only in the order they are passed. If a later library depends on an earlier one in the argument list, you either need to change the order of your flags, pass a -l argument multiple times, use --whole-archive, or use a linker that doesn't have this design flaw. I'm pretty sure that's why Windows and MacOS work fine.

I suspect the issue with your link is that the other brotli libraries depend on libbrotlicommon.

I spot-checked the regressions in crater that looked like this, and they all linked fine with LLD.

@saethlin saethlin added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 22, 2024
@inflation
Copy link
Author

Huh, I've stumped on the --whole-archive issue in another occasion, thought it was intended and had to find workarounds. Good to know.

Changing the order does fix the problem.

Also, does it only affect tests? Since I haven't recieved any report downstream, I assume all the symbols are present in the *.rlib nontheless.

@saethlin
Copy link
Member

Also, does it only affect tests?

Yeah, only tests. Your spelunking on the history of this is as good as mine, I just know a little bit of linker arcana but none of the relevant rustc history. You could start from #128400.

@apiraino apiraino removed the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants