Page 1 of 1

[Rust] Linker error when using loops

Posted: Sun Nov 29, 2020 9:02 am
by asdfasdfasdf
I've been trying to fix this for hours now and I'm not sure where to go from here. I'm working on an OS for an aarch64-unknown-none target, and whenever I put code in any sort of loop, the code compiles then I get an undefined reference. The symbol it gives is mangled, but I think it's

Code: Select all

core::panicking::panic_bounds_check
.
Here's my full build script:

Code: Select all

aarch64-elf-as -c boot.S -o boot.o
cargo build --target aarch64-unknown-none
aarch64-elf-gcc -T linker.ld -o os.elf -ffreestanding -O2 -nostdlib boot.o $HOME/Documents/os/kernel/target/aarch64-unknown-none/debug/libkernel.rlib
qemu-system-aarch64 -m 1024 -M raspi3 -kernel os.elf -serial stdio
If I remove the loop, it runs fine. Any insights into why this could be happening?

Re: [Rust] Linker error when using loops

Posted: Mon Nov 30, 2020 7:43 pm
by Octocontrabass
asdfasdfasdf wrote:Any insights into why this could be happening?
You're not linking against the Rust core library. You probably need to do that, since the core library provides several language features. I'm not all that familiar with Rust, but I found this page which might help.

Re: [Rust] Linker error when using loops

Posted: Tue Dec 01, 2020 5:33 am
by eekee
What Octocontrabass said, but there's a clue in the error message that it may not be the loop itself.
asdfasdfasdf wrote:core::panicking::panic_bounds_check
"Bounds checking" is the classic term for making sure an array index isn't outside the array. It also applies to strings (which I mention in case Rust pretends they're not arrays), and may apply to some other data types too. It takes a little bit of code to do the bounds checking, and it's likely Rust keeps this code in a core library. If you have some difficulty including the core library at this stage, you might be able to avoid the issue by working directly with memory rather than an array... unless Rust also bounds-checks all pointer dereferences. :)