Skip to content

Regression in 1.91-stable: riscv64gc-unknown-none-elf fails to link with "undefined symbol: trap_entry" referenced by libcore #148745

@Misakait

Description

@Misakait

Summary

I am developing a bare-metal OS for the riscv64gc-unknown-none-elf target. My project compiles and links successfully using toolchains up to and including 1.90-stable (e.g., 1.89-stable also works fine).

However, when I switch to 1.91-stable (or any newer version like 1.92-nightly, 1.93-nightly), the build fails during the final linking stage with rust-lld.
Another point is that even if the 1.91 release mode fails to compile, when I use 1.91, the dev version can compile and run successfully (as for 1.92 and 1.93, I have not tested them).
The linker reports an undefined symbol: trap_entry. The error log clearly indicates this symbol is now being referenced by libcore, specifically from core/src/num/uint_macros.rs.

Code

My trap_entry() function is defined like this:

#[unsafe(naked)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn trap_entry() {.....}

I use it here:

use crate::trap::{trap_entry};
#[unsafe(no_mangle)]
pub extern "C" fn rust_main() {
  ....
    unsafe {
        ....
        let mtvec_addr = (trap_entry as usize) & !0x3;
       ....
    }
    ....
}

At this time, a compilation error occurs.

However, when I change trap_entry to __trap_entry, even the 1.91 release build compiles successfully, as shown below:

#[unsafe(naked)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __trap_entry() {.....}

I use it here:

use crate::trap::{__trap_entry};
#[unsafe(no_mangle)]
pub extern "C" fn rust_main() {
  ....
    unsafe {
        ....
        let mtvec_addr = (__trap_entry as usize) & !0x3;
       ....
    }
    ....
}

Backtrace

Below is the error report when I run RUST_BACKTRACE=1 cargo run --verbose --release with version 1.91, without renaming trap_entry().

error: linking with rust-lld failed: exit status: 1
|
= note: "rust-lld" "-flavor" "gnu" "/tmp/rustcKovQFS/symbols.o" "<3 object files omitted>" "--as-needed" "-Bstatic" "/home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps/{liblazy_static-c23fca99382881da,libspin-480aea9da6d9e2bf,libspin-03a57e0c84f41b15,liblock_api-35af32450c360a8f,libscopeguard-883a9af2a9053ea5}.rlib" "/lib/rustlib/riscv64gc-unknown-none-elf/lib/{liballoc-,libcore-,libcompiler_builtins-*}.rlib" "-L" "/tmp/rustcKovQFS/raw-dylibs" "-Bdynamic" "-z" "noexecstack" "-o" "/home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps/charlotte_os-ffb778e7d3c7b460" "--gc-sections" "-O1" "-Tsrc/linker.ld"
= note: some arguments are omitted. use --verbose to show all linker arguments
= note: rust-lld: error: undefined symbol: trap_entry
>>> referenced by uint_macros.rs:3505 (/home/wanan/.rustup/toolchains/1.91-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/num/uint_macros.rs:3505)
>>> /home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps/charlotte_os-ffb778e7d3c7b460.charlotte_os.f74381362b79c950-cgu.0.rcgu.o:(rust_main)

For more information about this error, try rustc --explain E0133.
warning: charlotte_os (bin "charlotte_os") generated 44 warnings
error: could not compile charlotte_os (bin "charlotte_os") due to 1 previous error; 44 warnings emitted

Caused by:
process didn't exit successfully: /home/wanan/.rustup/toolchains/1.91-x86_64-unknown-linux-gnu/bin/rustc --crate-name charlotte_os --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=107 --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="uart_interrupt"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("default", "uart_interrupt", "uart_polling"))' -C metadata=39ed958f302bd95d -C extra-filename=-ffb778e7d3c7b460 --out-dir /home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps --target riscv64gc-unknown-none-elf -L dependency=/home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps -L dependency=/home/wanan/charlotte_os/target/release/deps --extern lazy_static=/home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps/liblazy_static-c23fca99382881da.rlib --extern spin=/home/wanan/charlotte_os/target/riscv64gc-unknown-none-elf/release/deps/libspin-03a57e0c84f41b15.rlib -Clink-arg=-Tsrc/linker.ld -Cforce-frame-pointers=yes (exit status: 1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleI-prioritizeIssue: Indicates that prioritization has been requested for this issue.O-riscvTarget: RISC-V architectureT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions