Libcore custom allocator: undefined reference


#1

Hey guys,

I’m currently working on a hobby kernel in Rust. I wanted to have heap allocations available, so I wrote an allocator. I need access to certain other functions like mapping pages, so it needs to be in the same crate. I used the allocator_stub crate to be able to do that, which mostly works well.

Now to the problem. This both works fine:

unsafe {
    let test = __rust_allocate(100, 8);
    __rust_deallocate(test, 100, 8);
 }

use alloc::boxed::Box;
let test = Box::new(1);

This however does not:

let mut test = vec![1, 2, 3];
test.push(4);

It throws the following linker errors:

target/x86_64-unknown-none-gnu/debug/lib.a(alloc-984520ec882c3d6f.0.o): In function `<alloc::heap::HeapAlloc as alloc::allocator::Alloc>::alloc':
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$5alloc17h273c94b01c9b313aE+0x16): undefined reference to `__rust_allocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$5alloc17h273c94b01c9b313aE+0x16): relocation truncated to fit: R_X86_64_PLT32 against undefined symbol `__rust_allocate'
target/x86_64-unknown-none-gnu/debug/lib.a(alloc-984520ec882c3d6f.0.o): In function `<alloc::heap::HeapAlloc as alloc::allocator::Alloc>::realloc':
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x31): undefined reference to `__rust_reallocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x31): relocation truncated to fit: R_X86_64_PLT32 against undefined symbol `__rust_reallocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x4e): undefined reference to `__rust_allocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x4e): relocation truncated to fit: R_X86_64_PLT32 against undefined symbol `__rust_allocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x7a): undefined reference to `__rust_deallocate'
alloc.cgu-0.rs:(.text._ZN66_$LT$alloc..heap..HeapAlloc$u20$as$u20$alloc..allocator..Alloc$GT$7realloc17hd92952b5b2f04157E+0x7a): relocation truncated to fit: R_X86_64_PLT32 against undefined symbol `__rust_deallocate'

Similar error occurred when trying to use format!.

I use ld to link the binary, because of some assembly files I need for initialization. The crate type is staticlib and the linker flags used are -n -T linker_script --gc-section -o output_file file1 ... fileN.

Some more background info:
Everything in the alloc and collection crates used to work with a previous version of rustc, except for anything string related, which had similar issues. Unfortunately I do not know which version that was.
After updating it also broke vec, but it still works for Box (as you can see above). I did not test any other types of the alloc crate in the new compiler version.

Does anybody have an idea why the linker would recognize the functions for handwritten Code and Boxes, but not for vec! and format!? I also tested the non-macro versions so it has nothing to do with it being a macro.
I’m glad for any help!