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

LLVMRust symbols not defined when building with -Zcross-crate-inline-threshold=always #118084

Closed
saethlin opened this issue Nov 20, 2023 · 10 comments · Fixed by #118142
Closed

LLVMRust symbols not defined when building with -Zcross-crate-inline-threshold=always #118084

saethlin opened this issue Nov 20, 2023 · 10 comments · Fixed by #118142
Assignees
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

I tried this code:

RUSTFLAGS_NOT_BOOTSTRAP=-Zcross-crate-inline-threshold=yes x build --stage 2 library

I expected to see this happen: build succeeds

Instead, this happened: huge linker error

  = note: /usr/bin/ld: /home/ben/rust-master/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_main-7ded3dee8b4f0982.rustc_main.8884e0661a07df8b-cgu.00.rcgu.o: in function `rustc_codegen_llvm::debuginfo::metadata::extend_scope_to_file':
          /home/ben/rust-master/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:1530:(.text._RINvMNtNtCs2lOlULwIAjb_17rustc_codegen_ssa3mir9debuginfoINtB3_10DebugScopeRNvNtNtCs4kfzHsID1rW_18rustc_codegen_llvm5llvm_3ffi8MetadataB1a_E25adjust_dbg_scope_for_spanNtNtB1h_7context9CodegenCxECsbIGDuTTUdex_10rustc_main+0x90): undefined reference to `LLVMRustDIBuilderCreateLexicalBlockFile'
          /usr/bin/ld: /home/ben/rust-master/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_main-7ded3dee8b4f0982.rustc_main.8884e0661a07df8b-cgu.00.rcgu.o: in function `rustc_codegen_llvm::llvm_util::configure_llvm':
          /home/ben/rust-master/compiler/rustc_codegen_llvm/src/llvm_util.rs:53:(.text.unlikely._RINvMs0_NtNtNtCs7JBb18HWky5_3std10sys_common4once5futexNtB6_4Once4callNCINvMs0_NtNtBc_4sync4onceNtB1f_4Once9call_onceNCNvNtCs4kfzHsID1rW_18rustc_codegen_llvm9llvm_util4init0E0ECsbIGDuTTUdex_10rustc_main+0x1eb): undefined reference to `LLVMRustInstallFatalErrorHandler'
          /usr/bin/ld: /home/ben/rust-master/compiler/rustc_codegen_llvm/src/llvm_util.rs:58:(.text.unlikely._RINvMs0_NtNtNtCs7JBb18HWky5_3std10sys_common4once5futexNtB6_4Once4callNCINvMs0_NtNtBc_4sync4onceNtB1f_4Once9call_onceNCNvNtCs4kfzHsID1rW_18rustc_codegen_llvm9llvm_util4init0E0ECsbIGDuTTUdex_10rustc_main+0x22b): undefined reference to `LLVMRustDisableSystemDialogsOnCrash'
          /usr/bin/ld: /home/ben/rust-master/compiler/rustc_codegen_llvm/src/llvm_util.rs:124:(.text.unlikely._RINvMs0_NtNtNtCs7JBb18HWky5_3std10sys_common4once5futexNtB6_4Once4callNCINvMs0_NtNtBc_4sync4onceNtB1f_4Once9call_onceNCNvNtCs4kfzHsID1rW_18rustc_codegen_llvm9llvm_util4init0E0ECsbIGDuTTUdex_10rustc_main+0x616): undefined reference to `LLVMTimeTraceProfilerInitialize'
          /usr/bin/ld: /home/ben/rust-master/compiler/rustc_codegen_llvm/src/llvm_util.rs:129:(.text.unlikely._RINvMs0_NtNtNtCs7JBb18HWky5_3std10sys_common4once5futexNtB6_4Once4callNCINvMs0_NtNtBc_4sync4onceNtB1f_4Once9call_onceNCNvNtCs4kfzHsID1rW_18rustc_codegen_llvm9llvm_util4init0E0ECsbIGDuTTUdex_10rustc_main+0x820): undefined reference to `LLVMRustSetLLVMOptions'
          /usr/bin/ld: /home/ben/rust-master/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_main-7ded3dee8b4f0982.rustc_main.8884e0661a07df8b-cgu.00.rcgu.o: in function `<rustc_codegen_llvm::context::CodegenCx as rustc_codegen_ssa::traits::consts::ConstMethods>::const_to_opt_uint::{closure#0}':
          /home/ben/rust-master/compiler/rustc_codegen_llvm/src/common.rs:223:(.text._RINvMs_NtNtCs2lOlULwIAjb_17rustc_codegen_ssa3mir5placeINtB5_8PlaceRefRNvNtNtCs4kfzHsID1rW_18rustc_codegen_llvm5llvm_3ffi5ValueE13project_indexNtNtB1c_7builder7BuilderECsbIGDuTTUdex_10rustc_main+0x63): undefined reference to `LLVMRustConstIntGetZExtValue'

... and on and on and on...

(it is entirely possible that the flag is broken, I haven't tried using it very many places yet)

@saethlin saethlin added the C-bug Category: This is a bug. label Nov 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 20, 2023
@Noratrieb
Copy link
Member

This flag causes all the rust code to be built downstream in rustc-main, I suspect the LLVMRust C++ symbols aren't being exported from librustc_driver.

@Noratrieb Noratrieb 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. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) requires-nightly This issue requires a nightly compiler in some way. link-dead-code Linkage: using -Clink-dead-code and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. link-dead-code Linkage: using -Clink-dead-code labels Nov 20, 2023
@bjorn3
Copy link
Member

bjorn3 commented Nov 20, 2023

Try adding #[link(name = "llvm-wrapper", kind = "static")] to

and every other such extern "C" block that declares a function. This tells rustc that it needs to re-export these functions of the static library from the produced dylib.

@saethlin
Copy link
Member Author

Progress! I now have a pile of

  = note: /usr/bin/ld: LLVMWriteBitcodeToFile: undefined version: 
          /usr/bin/ld: LLVMVoidTypeInContext: undefined version: 
          /usr/bin/ld: LLVMVectorType: undefined version: 
          /usr/bin/ld: LLVMValueAsMetadata: undefined version: 
          /usr/bin/ld: LLVMTypeOf: undefined version: 

@bjorn3
Copy link
Member

bjorn3 commented Nov 21, 2023

Is LLVM statically or dynamically linked for you? If it is dynamically linked, I think the #[link(name = "llvm-wrapper", kind = "static")] should be removed for all functions that come from libLLVM.so rather than llvm-wrapper. That is all functions whose name doesn't start with LLVMRust.

@saethlin
Copy link
Member Author

Dynamic. That got me down to just a few linker errors that now fit on my screen. I think I also need a link name on the normal LLVM extern block, but the name is something like "LLVM-17-rust-1.76.0-nightly".

@saethlin saethlin self-assigned this Nov 21, 2023
@bjorn3
Copy link
Member

bjorn3 commented Nov 21, 2023

For dynamic libraries it shouldn't matter. The build script already adds a -l with the library name and we don't need special handling for exporting symbols from dynamic libraries. What linker errors do you get now?

@saethlin
Copy link
Member Author

Sorry about this
Screenshot_2023-11-21-08-23-06-95_3d419158bad5872c40592a6c9956e692

@tmiasko
Copy link
Contributor

tmiasko commented Nov 21, 2023

Functions starting with LLVMTimeTraceProfiler despite the name are defined in our llvm-wrapper, so should get the same treatment as those starting with LLVMRust.

@Noratrieb
Copy link
Member

maybe they should be renamed instead 🙃

@saethlin
Copy link
Member Author

Yeah, I can do that

@bors bors closed this as completed in dd9f3ad Nov 22, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 22, 2023
Rollup merge of rust-lang#118142 - saethlin:llvm-linkage, r=tmiasko

Tighten up link attributes for llvm-wrapper bindings

Fixes rust-lang#118084 by moving all of the declarations of symbols from `llvm_rust` into a separate extern block with `#[link(name = "llvm-wrapper", kind = "static")]`.

This also renames `LLVMTimeTraceProfiler*` to `LLVMRustTimeTraceProfiler*` because those are functions from `llvm_rust`.

r? tmiasko
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Nov 23, 2023
Tighten up link attributes for llvm-wrapper bindings

Fixes rust-lang/rust#118084 by moving all of the declarations of symbols from `llvm_rust` into a separate extern block with `#[link(name = "llvm-wrapper", kind = "static")]`.

This also renames `LLVMTimeTraceProfiler*` to `LLVMRustTimeTraceProfiler*` because those are functions from `llvm_rust`.

r? tmiasko
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. requires-nightly This issue requires a nightly compiler in some way. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants