Cross compiling undefined reference

Hi all !

Posting my issue because I'm stuck now...
Here is the thing. I'm working on some bare metal rust project for Raspberry Pi B+. I followed some tutorials to get a led blinking and it worked. Now i'm on my way to have the UART working.
My problem is the following, this piece of code :

pub fn register_write(reg: usize, offset: usize, mask: usize, val: usize) 
{
    unsafe { 
    let register = reg as *mut usize;
        *(register) &= !mask;
        *(register) |= val << offset; 
    }
}

Is leading to this linker error :

undefined reference to 'core::panicking::panic_misaligned_pointer_dereference'

So here is how I compile :

#!/bin/bash

cargo build \
    --target arm-none-eabihf.json \
    -Z build-std=core,compiler_builtins
sleep 1
arm-none-eabi-gcc -g -mcpu=arm1176jzf-s -fpic -ffreestanding -c boot.S -o boot.o
arm-none-eabi-gcc -g -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o $1

where $1 is the .rlib issued by cargo.

my cargo.toml

[package]
name = "rust-kernel"
version = "0.1.0"
edition = "2021"

[dependencies]
[profile.dev]
panic = "abort"
debug-assertions = true # !
incremental = false
codegen-units = 1
opt-level = 'z' # !
debug = 2
overflow-checks = false
debug-assertions=false

And my arm-none-eabihf.json

{
  "llvm-target": "arm-none-eabihf",
  "target-endian": "little",
  "target-pointer-width": "32",
  "target-c-int-width": "32",
  "os": "none",
  "env": "eabi",
  "vendor": "unknown",
  "arch": "arm",
  "linker-flavor": "gcc",
  "linker": "arm-none-eabihf-gcc",
  "pre-link-args": {
        "gcc": [
            "-Wall",
            "-nostdlib",
            "-nostartfiles",
            "-ffreestanding",
            "-march=armv6"
        ]
    },
  "data-layout": "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
  "executables": "true",
  "relocation-model": "static",
  "no-compiler-rt": "true"
}

So my understanding is that, for some reason, gcc does not find this function anywhere. Which is understandable according to core::panicking - Rust because this function seems to be private ?

Anyway. I'm about to rede things using llvm as it may be more relevant but I'm afraid to face the same issue without understanding what's going on.

Can someone enlight me ?

full (dirty) code is here GitHub - ClementViel/SimpleKernel: Baremetal experimentations for RaspberryPI

.rlibs are for use by future invocations of rustc only — they contain the symbols of a single library and not its dependencies. To make a self-contained object that you can usefully link with a command other than rustc, you need the staticlib crate type; put this in your Cargo.toml:

[lib]
crate-type = ["staticlib"]

and link the output of that, not the .rlib (which will no longer be built).

Thanks !

I have now the .a static library generated. Which now leads to another undefined reference _atomic_load_1 but I think I know how to solve this one.

Just for my knowledge, what would be the difference between a rlib and a .so ?

  • An .rlib is for static linking, not dynamic.
  • An .rlib contains metadata and intermediate code that allows the compiler receiving it to type check against the crate's types, and to instantiate generic code and types from the crate. A .so doesn't have this metadata and must expose a "C-style" API with no generics.
1 Like