Skip to content

ch11 build issues: "soft-float", data-layout and rust-lld #122

@LukasKnuth

Description

@LukasKnuth

I was facing the following issues building ch11-fledgos-0 on my MacBook M1 Pro (2021). I don't think this has anything to do with macos or the ARM architecture, just that tools have changed in the meanwhile.

NOTE: Before anything I applied #88 - I was not able to build the kernel with the current versions of bootloader and x86_64 crates - the serde dependency just does not compile.

Then, the first error I was getting is:

error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `/Users/lukasknuth/.rustup/toolchains/nightly-aarch64-apple-darwin/bin/rustc - --crate-name ___ --print=file-names --target /Users/lukasknuth/hobbies/rust_in_action/chapter11-fledge-os/fledge.json --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
  --- stderr
  error: error loading target specification: target feature `soft-float` is incompatible with the ABI but gets enabled in target spec
    |
    = help: run `rustc --print target-list` for a list of built-in targets

I found rust-osdev/bootloader#492 which describes that with recent toolchain/compiler changes, one must now also declare "rustc-abi": "x86-softfloat" in the custom target to allow enabling soft-floats.


After making this change, the next error is:

error: data-layout for target `fledge-14973612785232118897`, `e-m:e-i64:64-f80:128-n8:16:32:64-S128`, differs from LLVM target's `x86_64-unknown-none` default layout, `e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128`

Simply taking the expected data-layout from the error message and setting it in fledge.json fixes this error.


The next error is (shortened):

error: unsafe attribute used without unsafe
 --> src/main.rs:9:3
  |
9 | #[no_mangle]
  |   ^^^^^^^^^ usage of unsafe attribute
  |
help: wrap the attribute in `unsafe(...)`
  |
9 | #[unsafe(no_mangle)]
  |   +++++++         +

This is solved as described by switching to #[unsafe(no_mangle)] in the code.


The final error then is:

error: linking with `rust-lld` failed: exit status: 1
  |
  = note:  "rust-lld" "-flavor" "gnu" "/var/folders/01/lvsv345j2m16h2d2ks2w3nw40000gn/T/rustcBjevRz/symbols.o" "<7 object files omitted>" "--as-needed" "-Bstatic" "/Users/lukasknuth/hobbies/rust_in_action/chapter11-fledge-os/target/fledge/debug/deps/{librustc_std_workspace_core-94ce5ca059995fe4.rlib,libcore-a32fa862e11c7483.rlib,libcompiler_builtins-a86ee3a353d6be3b.rlib}.rlib" "-L" "/var/folders/01/lvsv345j2m16h2d2ks2w3nw40000gn/T/rustcBjevRz/raw-dylibs" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-o" "/Users/lukasknuth/hobbies/rust_in_action/chapter11-fledge-os/target/fledge/debug/deps/chapter11_fledge_os-5803033311904355" "--gc-sections"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: undefined symbol: __rustc::rust_begin_unwind
          >>> referenced by panicking.rs:117 (src/panicking.rs:117)
          >>>               core-a32fa862e11c7483.core.865a4b62d07742b3-cgu.12.rcgu.o:(core::panicking::panic_nounwind_fmt::runtime::he4cecbc9d0af3e80) in archive /Users/lukasknuth/hobbies/rust_in_action/chapter11-fledge-os/target/fledge/debug/deps/libcore-a32fa862e11c7483.rlib

This is again due to a recent change in the nightly rust compiler as described by rust-osdev/bootloader#500 - we must drop the #[unsafe(no_mangle)] from the pub fn panic handler.


Now I can build and run the sample again. Here are the full changes again:

Cargo.toml

Same changes as in #88

diff --git a/Cargo.toml b/Cargo.toml
index f442d53..cfdc16f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,6 @@ build-command = ["build"]
 run-command = ["qemu-system-x86_64", "-drive", "format=raw,file={}"]
 
 [dependencies]
-bootloader = "0.11.10"
-x86_64 = "0.15.2"
+bootloader = "0.9.22"
+x86_64 = "0.14.10"
 

fledge.json

diff --git a/fledge.json b/fledge.json
index a24b987..7bac1b2 100644
--- a/fledge.json
+++ b/fledge.json
@@ -1,6 +1,6 @@
 {
   "llvm-target": "x86_64-unknown-none",
-  "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
+  "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
   "arch": "x86_64",
   "target-endian": "little",
   "target-pointer-width": "64",
@@ -11,5 +11,6 @@
   "executables": true,
   "features": "-mmx,-sse,+soft-float",
   "disable-redzone": true,
-  "panic-strategy": "abort"
+  "panic-strategy": "abort",
+  "rustc-abi": "x86-softfloat"
 }

src/main.rs

diff --git a/src/main.rs b/src/main.rs
index cb31204..db2af4c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,13 +6,12 @@ use core::intrinsics;
 use core::panic::PanicInfo;
 
 #[panic_handler]
-#[no_mangle]
 pub fn panic(_info: &PanicInfo) -> ! {
     // Crash the kernel on panic
     intrinsics::abort();
 }
 
-#[no_mangle]
+#[unsafe(no_mangle)]
 pub extern "C" fn _start() -> ! {
     let framebuffer = 0xb8000 as *mut u8;
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions