Skip to content

Missing atomic builtins for RP2040 (cortex_m0plus) #19054

@captain-error

Description

@captain-error

Zig Version

0.12.0-dev.2833+8802ec583

Steps to Reproduce and Observed Behavior

Hello everyone,
thank you for this amazing language! I am using Zig since a few weeks for a personal project, and I am still refreshed everytime I work with it, especially in comparison to the C++ I do at work.

Not sure if this is a bug or a usage error.
My program runs on a Raspberry Pi Pico (RP2040 microcontroller).
The main program is written in zig and built as a static lib which is then linked with a minimal C-Program which just calls zigMain() in that lib. The linking is done with the official Rapsberry Pi Pico toolchain (GCC and CMake based).
This works well (the program is now about 2000 lines of zig code) unless I use the builtins for atomic memory access.

This happens both for zig 0.11.0 and 0.12.0-dev.2833+8802ec583 (build system: Windows 11).

Here is a minimal Zig program to reproduce the problem.

export var stuff: i32 = 42;
export fn zigMain() i32 {
    return @atomicLoad(i32, &stuff, .Monotonic);
}

My build.zig looks like this and produces libmissing_atomics.a

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = std.zig.CrossTarget{ // for 0.12.0-dev the target definition uses b.resolveTargetQuery(....)
    
        .cpu_arch = std.Target.Cpu.Arch.arm,
        .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0plus },
        .os_tag = .freestanding,
        .abi = .eabi,
    };
    const optimize = b.standardOptimizeOption(.{});
    
    const lib = b.addStaticLibrary(.{
        .name = "missing_atomics",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    lib.bundle_compiler_rt = true;
    
    b.installArtifact(lib);
}

The error messages I get from the linker look like this (paths abreviated):

[build] ...../gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: ...../libmissing_atomics.a(...../libmissing_atomics.a.o): in function `zigMain':
[build] ...../main.zig:6: undefined reference to `__atomic_load_4'
[build] collect2.exe: error: ld returned 1 exit status

What am I doing wrong?
What exactly does the line "lib.bundle_compiler_rt = true;" do?

Expected Behavior

Functions like __atomic_load_4, __atomic_store_4, ... should be in the compiled lib and the linker should find them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-arm32-bit ArmbugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions