lucas-jacques opened PR #15180 from lucas-jacques:macho-shared-custom-sections to bytecodealliance:main:
Hello, I hit this with rustc_codegen_cranelift's experimental unwinding support on aarch64-apple-darwin, where every function's LSDA goes into _TEXT, _gcc_except_tab, but it applies to any frontend putting many data objects in one custom section (for example Rust's #link_section]).
Problem
Currently every data object with a custom section currently gets its own section. A Mach-0 object file can contain at most 255 sections, so emitting more data objects than that into the same custom section fails with "Too many sections".
Changes
On Mach-0, data objects with the same custom section now share a single section, like LLVM does. Individual data objects can still be dead stripped, since the object file uses subsections via symbols. ELF and COFF are unchanged and still get one section per data object.
Two edge cases: using the same section name with different section types or attributes now returns an error rather than emitting two conflicting sections, and zero-initialized data in such a section is written out as zeros, because uninitialized and initialized data can't share a section.
Duplicate
First, this PR addresses the same problem than #14985, but i believe there is a bug:
On macOS, data objects with the same custom section name now share one section. But zero-filled data (define_zeroinit) and normal data get the same cache key, so both kinds can land in the same section. Say the section gets a zero-filled object A (8 bytes), then a normal object B:
- A is added as zero-fill. The section's size grows to 8, but nothing is written to its byte buffer.
- B is appended at the end of the byte buffer, which is still offset 0.
- Result: A and B both point to offset 0. Reading A returns B's bytes instead of zeros.
Testing
- Added
macho_custom_section_is_shared_between_data_objects: defines 300 data objects (half of them zero-initialized) in__DATA,__customonaarch64-apple-darwin, and checks that the object has a single__customsection of the expected size, with every symbol at its own offset and the expected contents. Without the change this fails: the first zero-initialized object trips theis_bss()debug assertion inobject, and without zero-initialized objects emitting fails withToo many sections.- Added
macho_custom_section_rejects_conflicting_flags, which fails without the change.cargo test -p cranelift-object(debug and release),cargo clippy -p cranelift-object --all-targetsandcargo fmt --checkpass.Other test i did:
- End to end with rustc_codegen_cranelift on aarch64-apple-darwin (on top of cranelift-object 0.137.0): a program with 300
#[link_section = "__DATA,__custom"]statics, 100 of them all-zero and 30 of themAtomicU64, failed to compile withToo many sections: 0x130before. With the change it compiles, links with ld64 and runs: all values are read back correctly and the atomics can be written. I can share a gist if you would like to run the test.
lucas-jacques requested cfallin for a review on PR #15180.
lucas-jacques removed the review request for wasmtime-compiler-reviewers from PR #15180.
lucas-jacques requested wasmtime-compiler-reviewers for a review on PR #15180.
lucas-jacques updated PR #15180.
github-actions[bot] added the label cranelift on PR #15180.
github-actions[bot] added the label cranelift:module on PR #15180.
bjorn3 commented on PR #15180:
First, this PR addresses the same problem than https://github.com/bytecodealliance/wasmtime/pull/14985, but i believe there is a bug:
Instead of creating an entirely new PR you could also have left a comment avout this bug on that PR.
lucas-jacques commented on PR #15180:
First, this PR addresses the same problem than #14985, but i believe there is a bug:
Instead of creating an entirely new PR you could also have left a comment avout this bug on that PR.
Fair, I discovered the PR after i made this one, and noticed the bug afterward.
Should i close this one ? Just wanted to help having this fix merged upstream.
lucas-jacques edited a comment on PR #15180:
First, this PR addresses the same problem than #14985, but i believe there is a bug:
Instead of creating an entirely new PR you could also have left a comment avout this bug on that PR.
Fair, I discovered the PR after i made this fix, and noticed the bug afterward.
Should i close this one ? Just wanted to help having this fix merged upstream.
cfallin commented on PR #15180:
Yes, @lucas-jacques, if you wouldn't mind, maybe comment on the prior PR? I see that one is stalled on maintainer signoff; feel free to ping me when it's ready, @bjorn3, and I can merge it.
Last updated: Nov 19 2026 at 20:05 UTC