rust: link LLVM dynamically
cover rust: link LLVM dynamically
From: Sunil Dora <[email protected]>
This is the scarthgap backport for the rustc segfault, YOCTO #16058. It follows the RFC and discussion from June/July [1].
Root cause is a binutils bug, PR ld/32991 [2]. From binutils 2.45 gas emits R_X86_64_GOTPCRELX for "push foo@GOTPCREL(%rip)". An older ld which does not have the fix converts that push into a call. So when rust-llvm-native comes from sstate built on a >= 2.45 host and rust-native gets linked on a host with an older ld, librustc_driver jumps into LLVM data and rustc crashes. Details are in the bug [3].
master fixed this with [4], rustc links libLLVM.so there so the host ld never touches the LLVM objects, without it scarthgap is broken whenever sstate is shared between >= 2.45 and older hosts (Debian 12/13, Ubuntu 22.04/24.04, the pinned buildtools) and the distros have not picked up the ld fix.
It is opt-out via PACKAGECONFIG, and Alex also suggested to just take the backport.
master only needed rust changes because the llvm recipe already builds the shared lib (rust-llvm was dropped there [5]). scarthgap still has rust 1.75 with rust-llvm, which builds only static libs. Hence two patches:
1/2 rust-llvm: build libLLVM-17.so, install in ${libdir}, package as
rust-llvm-libllvm. rust is not touched here.
2/2 rust: llvm-shared PACKAGECONFIG, default on for native, nativesdk
and target. Native only does not work, Yoann already hit that in the RFC, target rust bootstraps its own rustc with target settings. Remove llvm-shared from PACKAGECONFIG to get static back.
Tested on scarthgap with both patches:
- rust-native, target rust, SDK build. librustc_driver has libLLVM-17.so in NEEDED now, size 142MB -> 70MB (native)
- rustc and cargo on qemux86-64 and from the SDK, testsdk passes
- multilib, musl, qemuarm, qemuppc, qemuarm64 build
- llvm-shared removed: librustc_driver static again, no libLLVM-17.so in DT_NEEDED, 142MB like before
- oe-selftest rust: same result with and without the series
Size: An image with rust is about 15MB bigger.
[1] https://lists.openembedded.org/g/openembedded-core/topic/119988126?msg=241069
[2] https://sourceware.org/bugzilla/show_bug.cgi?id=32991
[3] https://bugzilla.yoctoproject.org/show_bug.cgi?id=16058#c86
[4] https://git.openembedded.org/openembedded-core/commit/?id=d0671c3dad87a063b3a41dd07cde89b5684e692c
rust: enable dynamic LLVM linking by default
[5] https://git.openembedded.org/openembedded-core/commit/?id=c116828a876853cd5b53f7bd4607f0037fef4ee4
rust: Drop rust-llvm
Sunil Dora (2):
rust-llvm: build and install the LLVM shared library
rust: enable dynamic LLVM linking by default
meta/lib/oeqa/selftest/cases/rust.py | 2 +- meta/recipes-devtools/rust/rust-llvm_1.75.0.bb | 15 ++++++++++++++- meta/recipes-devtools/rust/rust_1.75.0.bb | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/2 rust-llvm: build and install the LLVM shared library
From: Sunil Dora <[email protected]>
For the rustc segfault in [YOCTO #16058] rust has to link LLVM dynamically. On master libLLVM.so comes from the llvm recipe, rust-llvm was dropped there [1]. scarthgap still builds LLVM for rust from this recipe and it builds only static libs. So add the shared lib here.
Need both LLVM_BUILD_LLVM_DYLIB and LLVM_LINK_LLVM_DYLIB. With only the first one "llvm-config --link-shared --libs" gives the per component libs (which don't exist) instead of -lLLVM-17. llvm-config and llvm-tblgen stay static, so the native llvm-config rust uses keeps working. master had the same two options for rust-llvm [2] when it still had the recipe.
LLVM installs the lib as ${libdir}/llvm-rust/lib/libLLVM-17.so. Nothing looks there at runtime, it is not a default loader path and the RUNPATH of the rust binaries does not point there. So move it to ${libdir} and keep a symlink behind, the -L path from llvm-config still works at link time.
Package it as ${PN}-libllvm. Otherwise the .so goes to ${PN}-dev by the default rules and rust would rdepend on a -dev package. Static libs are still installed, rust without llvm-shared is not affected.
[1] https://git.openembedded.org/openembedded-core/commit/?id=c116828a876853cd5b53f7bd4607f0037fef4ee4
[2] https://git.openembedded.org/openembedded-core/commit/?id=2b6a6d4199e4eed65f20351d077a04a7cf9da460
[YOCTO #16058]
--- meta/recipes-devtools/rust/rust-llvm_1.75.0.bb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
meta/recipes-devtools/rust/rust-llvm_1.75.0.bb +14 -1
@@ -47,6 +47,8 @@ EXTRA_OECMAKE = " \ -DLLVM_ENABLE_LIBXML2=OFF \ -DLLVM_ENABLE_FFI=OFF \ -DLLVM_INSTALL_UTILS=ON \ + -DLLVM_BUILD_LLVM_DYLIB=ON \ + -DLLVM_LINK_LLVM_DYLIB=ON \ -DLLVM_BUILD_EXAMPLES=OFF \ -DLLVM_INCLUDE_EXAMPLES=OFF \ -DLLVM_BUILD_TESTS=OFF \
@@ -84,15 +86,26 @@ export YOCTO_ALTERNATE_EXE_PATH = "${STAGING_LIBDIR}/llvm-rust/bin/llvm-config" do_install:append () { # we don't need any of this stuff to build Rust rm -rf "${D}/usr/lib/cmake" + + # rustc links to libLLVM (llvm-shared in rust). Nothing searches + # llvm-rust/lib at runtime, so move the library to ${libdir} and keep + # a symlink for the -L path given by llvm-config. + for lib in ${D}${libdir}/llvm-rust/lib*/libLLVM-*.so; do + [ -f "$lib" ] && [ ! -L "$lib" ] || continue + mv "$lib" ${D}${libdir}/ + ln -s ../../$(basename "$lib") "$lib" + done } -PACKAGES =+ "${PN}-bugpointpasses ${PN}-llvmhello ${PN}-liblto" +PACKAGES =+ "${PN}-bugpointpasses ${PN}-llvmhello ${PN}-liblto ${PN}-libllvm" # Add the extra locations to avoid the complaints about unpackaged files FILES:${PN}-bugpointpasses = "${libdir}/llvm-rust/lib/BugpointPasses.so" FILES:${PN}-llvmhello = "${libdir}/llvm-rust/lib/LLVMHello.so" FILES:${PN}-liblto = "${libdir}/llvm-rust/lib/libLTO.so.*" +FILES:${PN}-libllvm = "${libdir}/libLLVM-*.so" FILES:${PN}-staticdev =+ "${libdir}/llvm-rust/*/*.a" +RDEPENDS:${PN}-dev += "${PN}-libllvm" FILES:${PN} += "${libdir}/libLLVM*.so.* ${libdir}/llvm-rust/lib/*.so.* ${libdir}/llvm-rust/bin" FILES:${PN}-dev += "${datadir}/llvm ${libdir}/llvm-rust/lib/*.so ${libdir}/llvm-rust/include ${libdir}/llvm-rust/share ${libdir}/llvm-rust/lib/cmake"
Signature · 1 line
2.49.0
PATCH 2/2 rust: enable dynamic LLVM linking by default
In reply to [email protected]From: Sunil Dora <[email protected]>
Backport of master "rust: enable dynamic LLVM linking by default" [1]. On master rust links against the llvm recipe, here against rust-llvm which provides libLLVM-17.so now.
rustc segfaults (e.g. in llvm::X86ReadAdvanceTable) when rust-llvm-native comes from sstate built on a host with binutils >= 2.45 and rust-native is linked on a host with an older ld.
This is binutils PR ld/32991 [2]: newer gas emits R_X86_64_GOTPCRELX for "push foo@GOTPCREL(%rip)" and an ld without the fix rewrites the push into a call, so the statically linked LLVM code in librustc_driver jumps into data at runtime. scarthgap links the static rust-llvm libs the same way master did before the fix.
With dynamic linking the host ld only sees the exported symbols of libLLVM and does not relink any LLVM code.
Add llvm-shared PACKAGECONFIG, default on for native, nativesdk and target like master. Native only is not enough, target rust bootstraps its own rustc with the target settings and crashes the same way. Remove llvm-shared to get the static build back.
Selftest image installs rust-llvm-libllvm now, the test binaries need it (master installs llvm there).
[1] https://git.openembedded.org/openembedded-core/commit/?id=d0671c3dad87a063b3a41dd07cde89b5684e692c
[2] https://sourceware.org/bugzilla/show_bug.cgi?id=32991
[YOCTO #16058]
--- meta/lib/oeqa/selftest/cases/rust.py | 2 +- meta/recipes-devtools/rust/rust_1.75.0.bb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-)
meta/lib/oeqa/selftest/cases/rust.py +1 -1
@@ -51,7 +51,7 @@ class RustSelfTestSystemEmulated(OESelftestTestCase, OEPTestResultTestCase): bitbake("{} -c test_compile".format(recipe)) builddir = get_bb_var("RUSTSRC", "rust") # build core-image-minimal with required packages - default_installed_packages = ["libgcc", "libstdc++", "libatomic", "libgomp"] + default_installed_packages = ["libgcc", "libstdc++", "libatomic", "libgomp", "rust-llvm-libllvm"] features = [] features.append('IMAGE_FEATURES += "ssh-server-dropbear"') features.append('CORE_IMAGE_EXTRA_INSTALL += "{0}"'.format(" ".join(default_installed_packages)))
meta/recipes-devtools/rust/rust_1.75.0.bb +4 -0
@@ -20,6 +20,9 @@ DEPENDS += "rust-llvm (=${PV})" RDEPENDS:${PN}:append:class-target = " gcc g++ binutils" +PACKAGECONFIG ??= "llvm-shared" +PACKAGECONFIG[llvm-shared] = ",,," + # Otherwise we'll depend on what we provide INHIBIT_DEFAULT_RUST_DEPS:class-native = "1" # We don't need to depend on gcc-native because yocto assumes it exists
@@ -128,6 +131,7 @@ python do_configure() { # [llvm] config.add_section("llvm") + config.set("llvm", "link-shared", e(bb.utils.contains('PACKAGECONFIG', 'llvm-shared', True, False, d))) config.set("llvm", "static-libstdcpp", e(False)) if "llvm" in (d.getVar('TC_CXX_RUNTIME') or ""): config.set("llvm", "use-libcxx", e(True))
Signature · 1 line
2.49.0