Skip to content

#![feature(default_alloc_error_handler)] caues linker error for target x86_64-unknown-uefi #96793

Closed
@lukas-code

Description

@lukas-code

Using the feature default_alloc_error_handler in combination with the x64 UEFI target causes a liker error on the symbol rust_oom.

I tried this code:

#![no_std]
#![no_main]
#![feature(default_alloc_error_handler)]

extern crate alloc;

use core::alloc::{GlobalAlloc, Layout};
use core::ptr;

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

struct Allocator;

unsafe impl GlobalAlloc for Allocator {
    unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
        ptr::null_mut()
    }

    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

#[global_allocator]
static GLOBAL_ALLOCATOR: Allocator = Allocator;

#[no_mangle]
fn efi_main() -> usize {
    0
}

compile with:

cargo build --target x86_64-unknown-uefi -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem

I expected the code to compile.

Instead, I get the following linker error:

   Compiling uefi-alloc v0.1.0 (/home/lukas/code/uefi-alloc)
error: linking with `rust-lld` failed: exit status: 1
  |
  = note: "rust-lld" "-flavor" "link" "/NOLOGO" "/entry:efi_main" "/subsystem:efi_application" "/tmp/rustcUUmIif/symbols.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.1g358rj6ylhokqfc.rcgu.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.1judjdymnzeixt0f.rcgu.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.51as7tznkc4mah8j.rcgu.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.53em3tm29bukpsq9.rcgu.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.nesbmk0kc6l1uzo.rcgu.o" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.1ap5gthmin3k44xv.rcgu.o" "/LIBPATH:/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps" "/LIBPATH:/home/lukas/code/uefi-alloc/target/debug/deps" "/LIBPATH:/home/lukas/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-uefi/lib" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/liballoc-bcc97ab67ecf48f2.rlib" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/librustc_std_workspace_core-b30965c0259176a4.rlib" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/libcore-3c00ffb103f7704e.rlib" "/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/libcompiler_builtins-2879504fb94f71a0.rlib" "/NXCOMPAT" "/LIBPATH:/home/lukas/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-uefi/lib" "/OUT:/home/lukas/code/uefi-alloc/target/x86_64-unknown-uefi/debug/deps/uefi_alloc-1fd4f6fcbea41623.efi" "/OPT:REF,NOICF" "/DEBUG" "/NODEFAULTLIB"
  = note: rust-lld: error: undefined symbol: rust_oom
          >>> referenced by /home/lukas/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:413
          >>>               liballoc-bcc97ab67ecf48f2.rlib(alloc-bcc97ab67ecf48f2.alloc.c09e36c1-cgu.11.rcgu.o):(__rg_oom)
          

error: could not compile `uefi-alloc` due to previous error

Using #![feature(alloc_error_handler)] with an error handler like this compiles as expected:

#[alloc_error_handler]
fn alloc_error_handler(layout: Layout) -> ! {
    panic!("out of memory: {:?}", layout);
}

Meta

rustc --version --verbose:

rustc 1.62.0-nightly (30f386087 2022-05-05)
binary: rustc
commit-hash: 30f386087564243ab88a93c984c265290a31580b
commit-date: 2022-05-05
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.1

related: #95113
cc: #66741

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions