# BUG: Wrong segment ordering for macOS using the non-LLVM ARM64 backend

**URL:** <https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221>\
**Category:** Help\
**Tags:** compiler\
**Created:** [November 26, 2025, 9:40am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221 "2025-11-26T09:40:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [November 26, 2025, 9:40am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/1 "2025-11-26T09:40:54Z")

</div>

When using the non-LLVM ARM64 backend (`-fno-llvm -fno-lld`) on macOS, binaries fail to run with:

```zig
dyld: segment '__CONST_ZIG' vm address out of order

```

macOS dyld requires Mach-O segments to appear in ascending VM address order.  
The ZIG segments were all assigned the same rank (0xe) in `getSegmentRank()`, causing `segmentLessThan()` to fall back to alphabetical ordering:

**Before (wrong - alphabetical):**

```zig
__BSS_ZIG
__CONST_ZIG
__DATA_ZIG
__TEXT_ZIG

```

**After (correct - VM address order):**

```zig
__TEXT_ZIG
__CONST_ZIG
__DATA_ZIG
__BSS_ZIG

```

## Fix

Assign each ZIG segment a unique rank in `getSegmentRank()`:

```zig
if (mem.eql(u8, segname, "__TEXT_ZIG")) return 0xa;
if (mem.eql(u8, segname, "__CONST_ZIG")) return 0xb;
if (mem.eql(u8, segname, "__DATA_ZIG")) return 0xc;
if (mem.eql(u8, segname, "__BSS_ZIG")) return 0xd;

```

Test

```zig
$ zig build-exe -fno-llvm -fno-lld test.zig -femit-bin=test_bin
$ ./test_bin
$ echo $?
42

```

I put the fix [here](https://github.com/joelreymont/zig/pull/1) since I can’t open issues or PRs in the Zig Github repo.

---

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [November 26, 2025, 9:47am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/2 "2025-11-26T09:47:47Z")

</div>

How do I know?

Before fix (upstream master)

```zig
$ /Users/joel/Work/zig/master/out-llvm/bin/zig build-exe -fno-llvm -fno-lld /tmp/test_arm64_macos.zig -femit-bin=/tmp/test_unfixed

$ /tmp/test_unfixed
dyld[78487]: segment '__CONST_ZIG' vm address out of order in /private/tmp/test_unfixed
dyld[78487]: segment '__CONST_ZIG' vm address out of order

$ echo $?
134

$ otool -l /tmp/test_unfixed | grep "segname" | uniq
  segname __PAGEZERO
  segname __TEXT
  segname __DATA_CONST
  segname __DATA
  segname __BSS_ZIG
  segname __CONST_ZIG
  segname __DATA_ZIG
  segname __TEXT_ZIG
  segname __LINKEDIT

```

After fix

```zig
$ /Users/joel/Work/zig/claude/zig-out/bin/zig build-exe -fno-llvm -fno-lld /tmp/test_arm64_macos.zig -femit-bin=/tmp/test_fixed

$ /tmp/test_fixed

$ echo $?
42

$ otool -l /tmp/test_fixed | grep "segname" | uniq
  segname __PAGEZERO
  segname __TEXT
  segname __DATA_CONST
  segname __DATA
  segname __TEXT_ZIG
  segname __CONST_ZIG
  segname __DATA_ZIG
  segname __BSS_ZIG
  segname __LINKEDIT

```

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [November 26, 2025, 9:53am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/3 "2025-11-26T09:53:32Z")

</div>

Looks like there might be a tracking issue for this: [https://github.com/ziglang/zig/issues/25521](https://github.com/ziglang/zig/issues/25521)

---

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [November 26, 2025, 9:54am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/4 "2025-11-26T09:54:51Z")

</div>

I’m on Apple Silicon. Could you update that issue with the link to my PR, please? I think the solution might be universal.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [November 26, 2025, 10:08am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/5 "2025-11-26T10:08:38Z")

</div>

> [@joelreymont](#):
>
> Could you update that issue with the link to my PR, please?

I won’t, sorry. You’ve been warned about breaking Zig’s strict AI policy on Zig’s github before, and this PR also reeks of LLM.

---

<div class="post-metadata">

**Author:** ![tensorush](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@tensorush](https://ziggit.dev/u/tensorush)\
**Post date:** [November 26, 2025, 10:11am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/6 "2025-11-26T10:11:36Z")

</div>



---

<div class="post-metadata">

**Author:** ![tensorush](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@tensorush](https://ziggit.dev/u/tensorush)\
**Post date:** [November 26, 2025, 10:19am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/7 "2025-11-26T10:19:44Z")

</div>

I’ve unlisted the topic since its only purpose is to promote a compiler bugfix from an untrusted contributor.

---

<div class="post-metadata">

**Author:** ![joelreymont](https://ziggit.dev/user_avatar/ziggit.dev/joelreymont/32/3634_2.png) [@joelreymont](https://ziggit.dev/u/joelreymont)\
**Post date:** [November 26, 2025, 11:07am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/8 "2025-11-26T11:07:24Z")

</div>

I explained what the bug is.

I gave a solution and tests for it.

Not much else I can do.

---

<div class="post-metadata">

**Author:** ![tensorush](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@tensorush](https://ziggit.dev/u/tensorush)\
**Post date:** [November 26, 2025, 11:08am UTC](https://ziggit.dev/t/bug-wrong-segment-ordering-for-macos-using-the-non-llvm-arm64-backend/13221/9 "2025-11-26T11:08:40Z")

</div>


