Skip to content

build: work around zig linker bug in macos-aarch64 openssl assembly#815

Merged
pkova merged 5 commits intomsl/zig-v0.14.0from
pkova/zig-linker-bug
May 16, 2025
Merged

build: work around zig linker bug in macos-aarch64 openssl assembly#815
pkova merged 5 commits intomsl/zig-v0.14.0from
pkova/zig-linker-bug

Conversation

@pkova
Copy link
Copy Markdown
Collaborator

@pkova pkova commented May 16, 2025

All vere versions since the introduction of the zig build system in vere-v3.2 have had a misconfigured openssl build. This was eventually caught by simply doing +https://facebook.com in the dojo on mac or linux aarch64, segfaulting the binary instantly. Facebook uses TLS 1.3 with the TLS_CHACHA20_POLY1305_SHA256 cryptosuite, exercising the vendored assembly file poly1305-armv8.S. We had mistakenly defined the macro __ILP32__ for this translation unit which means integers, longs and pointers are 32 bits which is obviously wrong. Fixing this bug lead to a more insidious problem, however.

The obvious fix of removing the __ILP32__ macro fixed the facebook problem on linux-aarch64. On macos-aarch64 the fix caused an immediate segfault in the macos loader (dyld) when starting the vere binary.

The zig build system shells out to the LLVM linker LLD in all cases except Mach-O. When inspecting the vere binary and the operation of the zig Mach-O linker it became clear that the segfault in the loader happens because the zig Mach-O linker emits a rebase into the read-only __TEXT section of the vere binary. When running the build with --verbose-link and grabbing the final zig link command and switching out the linker to the macos native ld the vere binary was
completely fine. This is in other words a bug in the zig Mach-O linker.

Further examination revealed that the incorrectly rebased symbol was _OPENSSL_armcap_P. This is a constant ARMV7_NEON on macos-aarch64 so we work around the zig linker bug by not using the symbol at all.

pkova added 5 commits May 16, 2025 15:26
This is just wrong, the macro means "INT" "LONG" "POINTER" == 32 bits. It was
muna here since the beginning to work around a very peculiar issue, see next
commit for details.
All vere versions since the introduction of the zig build system in vere-v3.2
have had a misconfigured openssl build. This was eventually caught by simply
doing +https://facebook.com in the dojo on mac or linux aarch64, segfaulting the binary instantly.
Facebook uses TLS 1.3 with the TLS_CHACHA20_POLY1305_SHA256 cryptosuite,
exercising the vendored assembly file poly1305-armv8.S. We had mistakenly
defined the macro __ILP32__ for this translation unit which means integers,
longs and pointers are 32 bits which is obviously wrong. Fixing this bug lead to
a more insidious problem, however.

The obvious fix of removing the __ILP32__ macro fixed the facebook problem on
linux-aarch64. On macos-aarch64 the fix caused an immediate segfault in the
macos loader (dyld) when starting the vere binary.

The zig build system shells out to the LLVM linker LLD in all cases except
Mach-O. When inspecting the vere binary and the operation of the zig Mach-O linker it
became clear that the segfault in the loader happens because the zig Mach-O
linker emits a rebase into the read-only __TEXT section of the vere binary. When
running the build with --verbose-link and grabbing the final zig link command
and switching out the linker to the macos native ld the vere binary was
completely fine. This is in other words a bug in the zig Mach-O linker.

Further examination revealed that the incorrectly rebased symbol was
_OPENSSL_armcap_P. This is a constant ARMV7_NEON on macos-aarch64 so we work
around the zig linker bug by not using the symbol at all.
@pkova pkova requested a review from a team as a code owner May 16, 2025 12:56
@pkova pkova merged commit 193e96e into msl/zig-v0.14.0 May 16, 2025
2 checks passed
@pkova pkova deleted the pkova/zig-linker-bug branch May 16, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant