[MachO] Crash/fatal error in backend when global variable section attribute is invalid for Mach-O
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
### Key Observation / Root Cause
Clang's frontend (`Sema`) checks the target OS instead of the target object format when validating the `section` attribute.
- `clang --target=x86_64-apple-darwin`: Frontend correctly catches the invalid Mach-O section specifier and issues a clean error.
- `clang --target=x86_64-unknown-freebsd-macho`: Frontend assumes ELF rules based on `freebsd` OS, skipping Mach-O validation. The error is only caught during CodeGen by `TargetLoweringObjectFileMachO`, triggering an Internal Compiler Error / `fatal error`.
### Bug Description
When targeting Mach-O with a non-Darwin OS (e.g., `x86_64-unknown-freebsd-macho`), specifying a section name via `__attribute__((section(...)))` that lacks the mandatory `segment,section` Mach-O format causes Clang to crash in the backend with a `fatal error` instead of issuing a frontend diagnostic error.
### Diagnostic msg
[test-ed24c9.c](https://github.com/user-attachments/files/30375642/test-ed24c9.c)
[test-ed24c9.sh](https://github.com/user-attachments/files/30375641/test-ed24c9.sh)
### Full command output
[out.txt](https://github.com/user-attachments/files/30375700/out.txt)
Contributor guide
Research direction
Start by running the supplied test-ed24c9.sh with test-ed24c9.c and the x86_64-unknown-freebsd-macho target to reproduce the backend fatal error. Read Clang's Sema section-attribute validation and TargetLoweringObjectFileMachO handling, then ensure the invalid Mach-O form receives a frontend diagnostic without reaching the fatal backend path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100