Rustでベアメタル向けにビルドしたrlibファイルをldでリンクするとundundefined reference to `core::panicking::panic'となる

前提

Rustで簡単なOSの開発を行っています。Cargoで以下のターゲットを使用してビルドしたrlibファイルをldでブートローダーなどその他のオブジェクトファイルとリンクしようとしたところエラーが発生しました。

また、このエラーはcore::panicking::panicのメソッドを内部的(?)に呼び出すような操作(配列のインデックス番号を指定して要素を参照するなど)をすると発生します。
他にもmemcpyなどの関数も見つからないらしく、そういった類のエラーも発生しています。

json

1{2 "llvm-target": "i686-unknown-none",3 "data-layout": "e-m:e-i32:32-f80:128-n8:16:32-S128-p:32:32",4 "arch": "x86",5 "target-endian": "little",6 "target-pointer-width": "32",7 "target-c-int-width": "32",8 "os": "none",9 "executables": true,10 "linker-flavor": "ld.lld",11 "linker": "rust-lld",12 "panic-strategy": "abort",13 "disable-redzone": true,14 "features": "+soft-float,+sse"15}

発生している問題・エラーメッセージ

ld: target/x86_64_foundos_target/release/libfoundos_kernel.rlib(foundos_kernel-6a56ec757663feb9.foundos_kernel.a9321a8b6a2f49c6-cgu.0.rcgu.o): in function `spin::once::Once<T>::call_once': foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN4spin4once13Once$LT$T$GT$9call_once17hdcf47874c260858cE+0x72): undefined reference to `memcpy' ld: foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN4spin4once13Once$LT$T$GT$9call_once17hdcf47874c260858cE+0x86): undefined reference to `memcpy' ld: foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN4spin4once13Once$LT$T$GT$9call_once17hdcf47874c260858cE+0x9a): undefined reference to `<spin::once::Finish as core::ops::drop::Drop>::drop' ld: foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN4spin4once13Once$LT$T$GT$9call_once17hdcf47874c260858cE+0xbc): undefined reference to `core::panicking::panic' ld: target/x86_64_foundos_target/release/libfoundos_kernel.rlib(foundos_kernel-6a56ec757663feb9.foundos_kernel.a9321a8b6a2f49c6-cgu.0.rcgu.o): in function `spin::mutex::Mutex<T>::new': foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN4spin5mutex14Mutex$LT$T$GT$3new17h9e8db9090ef9b879E+0x17): undefined reference to `memcpy' ld: target/x86_64_foundos_target/release/libfoundos_kernel.rlib(foundos_kernel-6a56ec757663feb9.foundos_kernel.a9321a8b6a2f49c6-cgu.0.rcgu.o): in function `foundos_kernel::vga::print_str': foundos_kernel.a9321a8b6a2f49c6-cgu.0:(.text._ZN14foundos_kernel3vga9print_str17h476833c655cb93f8E+0x3e): undefined reference to `core::slice::iter::<impl core::iter::traits::collect::IntoIterator for &[T]>::into_iter'

該当のソースコード

lib.rs

Rust

1#![no_std]2#![no_main]3 4use core::panic::PanicInfo;5use core::arch::asm;6use crate::vga::{clear_screen, Color, ColorCode, print_str};7 8pub mod vga;9pub mod port;10pub mod interrupt;11 12 13#[no_mangle]14pub extern "C" fn kernel_main() -> ! {15 let color = ColorCode::new(Color::White,Color::Black);16 clear_screen();17 // VGAテキストバッファで文字を出力する18 print_str("Hello World!\n",color);19 loop {20 unsafe {21 asm!("hlt")22 }23 }24}25 26/// This function is called on panic.27#[panic_handler]28fn panic(_info: &PanicInfo) -> ! {29 loop {}30}31

Cargo.toml

Toml

1[package]2name = "foundos_kernel"3version = "0.1.0"4edition = "2021"5 6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html7 8[dependencies]9spin = "0.5.2"10 11[dependencies.lazy_static]12version = "1.0"13features = ["spin_no_std"]14 15[profile.dev]16panic = "abort"17 18[profile.release]19panic = "abort"20opt-level = 'z'

.cargo/config.toml

Toml

1[build]2target = "./x86_64_foundos_target.json"3rustflags = ["-C","relocation-model=static","--emit","obj"]4 5[unstable]6build-std = ["core", "compiler_builtins"]7build-std-features = ["compiler-builtins-mem"]

試したこと

rlibファイルではなくtarget/release/depsにあるrlibファイルになる前のオブジェクトファイルを使用してリンクしてみましたが、undefined reference to...というエラーはなくなりビルドできるようになりましたが、ビルドしたディスクイメージをqemuで起動するとブートループに陥ります。

補足情報(FW/ツールのバージョンなど)

x86_64-unknown-linux-gnu
rustc 1.72.0-nightly (f0411ffce 2023-05-30)
GNU ld (GNU Binutils for Ubuntu) 2.38

コメントを投稿

0 コメント