Skip to content

riscv: link libgcc for jemalloc's __ffsdi2 - #180

Merged
BusyJay merged 2 commits into
tikv:mainfrom
kali:riscv-link-libgcc
Sep 25, 2026
Merged

BusyJay merged 2 commits into
tikv:mainfrom
kali:riscv-link-libgcc

Conversation

@kali

@kali kali commented Sep 7, 2026 •

Copy link
Copy Markdown

Base rv64gc has no count-trailing-zeros instruction, so gcc lowers the ffs in bit_util.h to a __ffsdi2 libgcc call. rust's compiler_builtins ships the rest of the *di2 family but not that one, so linking a static riscv64 binary fails with undefined reference to `__ffsdi2'.

Name libgcc for riscv as is already done for android, guarded on a gnu-like compiler so a clang/compiler-rt toolchain is left alone.

Summary by CodeRabbit

  • Bug Fixes
    • Improved builds for RISC-V targets using GNU-like compilers by ensuring the required compiler support library is linked.
    • Resolved linker failures on base RV64GC systems lacking the Zbb extension.

Base rv64gc has no count-trailing-zeros instruction, so gcc lowers the
ffs in bit_util.h to a __ffsdi2 libgcc call. rust's compiler_builtins
ships the rest of the *di2 family but not that one, so linking a static
riscv64 binary fails with undefined reference to `__ffsdi2'.

Name libgcc for riscv as is already done for android, guarded on a
gnu-like compiler so a clang/compiler-rt toolchain is left alone.

Signed-off-by: Mathieu Poumeyrol <kali@zoy.org>
@ti-chi-bot

ti-chi-bot Bot commented Sep 7, 2026

Copy link
Copy Markdown

Welcome @kali! It looks like this is your first PR to tikv/jemallocator 🎉

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 1828cedd-21f9-486d-89a9-9feebbad9df2

📥 Commits

Reviewing files that changed from the base of the PR and between 0e91291 and 3db58f2.

📒 Files selected for processing (1)
  • jemalloc-sys/build.rs

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The RISC-V build script now links gcc when it detects a GNU-like compiler. This supplies __ffsdi2 for rv64gc builds while retaining the existing atomic linkage.

Changes

RISC-V linker configuration

Layer / File(s) Summary
GNU libgcc linkage
jemalloc-sys/build.rs
The RISC-V branch checks compiler.is_like_gnu() and links gcc when the condition is true.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 0a020

GNU RISC-V builds now link libgcc to resolve the missing __ffsdi2 symbol for base rv64gc targets, without changing Clang/compiler-rt linkage. No current merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: linking libgcc for jemalloc's RISC-V __ffsdi2 dependency.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@BusyJay

BusyJay commented Sep 24, 2026

Copy link
Copy Markdown
Member

Do you have a minimal reproduction? It seems rustc will link gcc automatically (tested on Rust 1.71 和 Rust 1.95).

@kali

kali commented Sep 24, 2026

Copy link
Copy Markdown
Author

Yes. Sorry, there was a missing bit. The discriminator is a static link: with -C target-feature=+crt-static rustc passes -nodefaultlibs and, on musl, uses its self-contained rlibs (libunwind, compiler_builtins), so nothing names libgcc. A default dynamic build links libgcc_s.so.1, which exports __ffsdi2 — that is why it looks like rustc links gcc automatically.

Minimal repro, riscv64gc-unknown-linux-musl, rustc 1.98.0, Bootlin riscv64-lp64d--musl--stable-2025.08-1 (gcc 14.3.0), stock tikv-jemallocator 0.7.0 / tikv-jemalloc-sys 0.7.1 from crates.io.

TC comes from https://toolchains.bootlin.com/downloads/releases/toolchains/riscv64-lp64d/tarballs/riscv64-lp64d--musl--stable-2025.08-1.tar.xz .

# Cargo.toml
[dependencies]
tikv-jemallocator = "0.7"
// src/main.rs
#[global_allocator]
static A: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
fn main() { println!("{}", vec![1u8; 32].len()); }
TC=<bootlin-toolchain>
export CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_MUSL_LINKER=$TC/bin/riscv64-buildroot-linux-musl-gcc
export CC_riscv64gc_unknown_linux_musl=$TC/bin/riscv64-buildroot-linux-musl-gcc
export AR_riscv64gc_unknown_linux_musl=$TC/bin/riscv64-buildroot-linux-musl-ar
export CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-C target-feature=+crt-static"
cargo build --release --target riscv64gc-unknown-linux-musl
ld: libtikv_jemalloc_sys-*.rlib(bin.pic.o): in function `cfs_lu':
    .../include/jemalloc/internal/bit_util.h:290:(.text._rjem_je_bin_slab_reg_alloc_batch+0x132):
    undefined reference to `__ffsdi2'

(5 such references, all from bin_slab_reg_alloc_batch.)

Same command with -C target-feature=-crt-static links fine, and readelf -d on that binary shows NEEDED libgcc_s.so.1.

Symbol side of it:

$ nm --defined-only .../riscv64gc-unknown-linux-musl/lib/libcompiler_builtins-*.rlib | grep -oE '__[a-z]+[sd]i2' | sort -u
__absvdi2 __absvsi2 __bswapdi2 __bswapsi2 __clzdi2 __clzsi2 __cmpdi2 __ctzdi2 __ctzsi2
__negdi2 __negvdi2 __negvsi2 __paritydi2 __paritysi2 __popcountdi2 __popcountsi2 __ucmpdi2
$ nm --defined-only $(riscv64-buildroot-linux-musl-gcc -print-libgcc-file-name) | grep ffsdi2
0000000000000000 T __ffsdi2

So compiler_builtins carries the rest of the family but not __ffsdi2, and gcc emits a call to it for the ffs in bit_util.h because base rv64gc has no ctz (that is Zbb).

With this PR's build.rs applied ([patch.crates-io] tikv-jemalloc-sys = { path = ... }), the +crt-static build links and the binary runs correctly under qemu-riscv64 — no -C link-arg=-lgcc needed on the user side.

@BusyJay BusyJay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BusyJay
BusyJay merged commit 3c6b6ba into tikv:main Sep 25, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants