Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lukas-code opened this issue May 6, 2022 · 5 comments
Labels
C-bug Category: This is a bug.

Comments

@lukas-code
Copy link
Member

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

@lukas-code lukas-code added the C-bug Category: This is a bug. label May 6, 2022
@OuYangPaste
Copy link

I also encountered this problem when I wrapped .a for c program

@ghost
Copy link

ghost commented Nov 3, 2022

+1 I am running into the same issue, trying to link a no_std+alloc static lib into a c program

@nicholasbishop
Copy link
Contributor

I tested the code above on x86_64-unknown-uefi and it seems to be compiling and linking successfully now (it was broken until the 2022-11-02 nightly). I think #103061 fixed it.

For the two most recent comments, I would suggest retesting with the current nightly. If it is still reproing, you might want to file a new bug with full repro steps, since the original repro in this issue is fixed.

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@lukas-code
Copy link
Member Author

The feature default_alloc_error_handler was made default and this seems to link correctly now. Closing as fixed.

@rustbot label -needs-triage-legacy

@rustbot
Copy link
Collaborator

rustbot commented Dec 1, 2023

Error: Label needs-triage-legacy can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@fmease fmease removed the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants