Undefined symbol in no_std with alloc crate

Hello!

I am currently writing an Operating System in Rust. Right now, I am implementing the User space, creating a kind of "standard library" which is just a shared library. Currently, the structure of the user space is separated into a main lib.rs, then a folder called libs (with all the libraries separated into their respective folders - heap, mutex, system calls, etc), and finally a bin folder with the user programs themselves. The user programs can import the standard library and easily make system calls and access other features. The problem I am facing right now is that, whenever I try to use the heap allocator (import it in any way), I get the following error at compile time:

rust-lld: error: undefined symbol: memcpy
>>> referenced by intrinsics.rs:2673 (src/intrinsics.rs:2673)
>>>               core-fba509e186a057bc.core.49d12718f0831562-cgu.12.rcgu.o:(core::intrinsics::copy_nonoverlappingb
>>> referenced by mod.rs:421 (src/fmt/mod.rs:421)
>>>               core-fba509e186a057bc.core.49d12718f0831562-cgu.6.rcgu.o:(_$LT$core..fmt..Arguments$u20$as$u20$cb
          
rust-lld: error: undefined symbol: memset
>>> referenced by intrinsics.rs:2832 (src/intrinsics.rs:2832)
>>>               core-fba509e186a057bc.core.49d12718f0831562-cgu.12.rcgu.o:(core::intrinsics::write_bytes::h04da7b
>>> referenced by num.rs:76 (src/fmt/num.rs:76)
>>>               core-fba509e186a057bc.core.49d12718f0831562-cgu.0.rcgu.o:(core::fmt::num::GenericRadix::fmt_int:b
>>> referenced by num.rs:76 (src/fmt/num.rs:76)
>>>               core-fba509e186a057bc.core.49d12718f0831562-cgu.0.rcgu.o:(core::fmt::num::GenericRadix::fmt_int:b
>>> referenced 1 more times

I have my global_allocator implemented in heap.rs and user has the extern crate alloc. I have tried using extern crate user, extern crate alloc, user::alloc::(...), but whenever I dare to mention alloc in the user binary, it just fails to compile. I am aware that Rust tries to link binaries, but I am not sure why it would fail to link the allocator. How should I proceed?

Couple of important things to mention, I am using nightly, compiling core from source under no_std, Rust 1.71.0-nightly, and the alloc crate is included in .cargo config.

The core crate documents this:

This library is built on the assumption of a few existing symbols:

  • memcpy, memcmp, memset, strlen - These are core memory routines which are often generated by LLVM. Additionally, this library can make explicit calls to these functions. Their signatures are the same as found in C. These functions are often provided by the system libc, but can also be provided by the compiler-builtins crate.
3 Likes