Skip to content

Stage2 ARM Thumb Debug: LLD fails with "undefined symbol: __clzsi2" #13465

Closed
@bcrist

Description

@bcrist

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

When compiling embedded firmware targeting ARM Cortex M0+, using the stage2 compiler, in debug mode, the linker fails with this message:

LLD Link... ld.lld: error: undefined symbol: __clzsi2
>>> referenced by compiler_rt
>>>               C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a.o:(__udivsi3) in archive C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a
>>> referenced by compiler_rt
>>>               C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a.o:(__udivsi3) in archive C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a
>>> referenced by compiler_rt
>>>               C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a.o:(__umodsi3) in archive C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a
>>> referenced 1 more times
>>> did you mean: __ctzsi2
>>> defined in: C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a(C:\...\zig\o\abc2490849d1b9cc45a64ab5470e2f9d\libcompiler_rt.a.o)

This doesn't happen when using any of the -Drelease-* modes, or when using the stage1 compiler. The reason it doesn't happen in release modes may just be that all the integer division and modulo operations are optimized out. I'm not exactly sure where those are coming from, even in debug; the only division I can think of should be comptime only.

It looks like lib/compiler_rt/count0bits.zig does have an implementation of __clzsi2 specifically for Thumb targets, but somehow the linker doesn't see it.

The specific CrossTarget being used is:

std.zig.CrossTarget{
    .cpu_arch = .thumb,
    .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0plus },
    .os_tag = .freestanding,
    .abi = .none,
},

It can be reproduced by running zig build after checking out this commit: https://github.com/bcrist/microbe/tree/0b7404760350ef3504d95eb1200d6900c8b2a374

Running zig build again after the failure doesn't print the error again, it just triggers a FileNotFound error, but I think that's caused by a different issue: #13432

Deleting the zig-cache directory and building again shows the linker error again.

Expected Behavior

The linker should succeed and generate a valid ELF binary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved 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