Clang -nostdlib option not working

I've updated my M1 mac to Monterey and installed Xcode 13.1. I have a shared lib project I compile with -nostdlib. It's a CPU emulator that doesn't use any external code, no system calls, no std c lib functions... nothing. The library doesn't require initialization either.

Now I get this error when building the library

ld: dynamic main executables must link with libSystem.dylib for architecture arm64

which can be solved by passing -lSystem to the linker.

The question is: is this a bug? I've not changed anything in my project. Is this a new Apple policy? to force everything to be linked against libSystem even when it's not necessary?

Replies

Is this a new Apple policy?

Yes (r. 75177082). And your resolution, linking with the libSystem, is the correct one.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • What does "(r. 75177082)" mean?

    BTW, I've found a workaround. Passing -Wl,-dead_strip_dylibs instead of -nostdlib produces the correct result, since the linker avoids linking against libSystem as it doesn't find any actual usage of symbols from that library.

    So, if this "problem" is by design, and this is a new policy from Apple, well, they have failed to enforce mandatory dependency on libSystem.

Add a Comment

What does "(r. 75177082)" mean?

It’s a reference to the bug report tracking this change (the r stands for Radar, Apple’s bug tracking system). You do not have access to this. I post bug numbers like this so that other Apple folks, including Future Quinn™, can more easily find the context.

they have failed to enforce mandatory dependency on libSystem.

Honestly, I don’t know enough about the background to this issue to comment on your discovery, but I’ve filed a bug so that the team can look into it (r. 84876988).

To reiterate, the correct solution here is to link with libSystem.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Here you can see what exactly I try to do. This exaple library does not use any system call. Enforcing linking against libSystem makes no sense in this case. I don't like this policy but I can understand some at Apple have reasons for this nonsense...

But, if this is a policy, then, why don't you remove -nostdlib, because preserving this compiler flag will only lead to confusion and errors. Now I have to change my CMake build system just to cover your operating system. Others may have the same problem if this flag doesn't work or produce errors.

By the way, you can also avoid linking against libSystem on GCC by using -Wl,-dead_strip_dylibs. Sometimes -nolibc is also needed.