0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
Commit graph

76 commits

Author SHA1 Message Date
Krzysztof Wolicki
60635c2bda Remove deprecated functions from std.Target (#37229)
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37229
Reviewed-by: Ryan Liptak <squeek502@noreply.codeberg.org>
2026-11-07 00:35:54 +01:00
B. Crist
72c7ff4b8e std.debug: Support FP unwinding on thumb-* targets (#37482)
When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15.  Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list."

`std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers.  It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet.

On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack.  In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case.  The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`.

For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead.  As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR.

So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb.

The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+.  This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured.

With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+):
```
src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7c in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware)
```

But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect.  For example, here's the disassembly for the top two functions:

```
main.iAmAFunctionYesIAm:
10000b48  80b5       push    {r7, lr}
10000b4a  00af       add     r7, sp, #0
10000b4c  00f001f8   bl      #main.dumpCurrentStack
10000b50  80bd       pop     {r7, pc}

main.dumpCurrentStack:
10000b52  80b5       push    {r7, lr}
10000b54  00af       add     r7, sp, #0
10000b56  00f001f8   bl      #debug.writeCurrentStackTrace
10000b5a  80bd       pop     {r7, pc}
```

In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous.  We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction.  I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so).

With all of the changes, my RP2040 test now looks correct:
```
src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7b in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware)
```

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482
Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
Alex Rønne Petersen
9da2875656 Merge pull request 'second round of std.c ABI auditing' (#37469) from alexrp/zig:std-c-audit-round-2 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37469
2026-10-30 08:45:00 +01:00
Alex Rønne Petersen
8850ea7b93
remove remnants of x86-illumos (32-bit) support 2026-10-29 17:52:27 +01:00
Alex Rønne Petersen
6d6cd0cb6e
std.Target: remove x86 16bit_mode/32bit_mode features, add new x86c16 arch tag
closes https://codeberg.org/ziglang/zig/issues/37421
2026-10-29 14:42:05 +01:00
Alex Rønne Petersen
1c5c725431
remove mips64el-openbsd support
OpenBSD 8.0 dropped this target.
2026-10-21 19:49:58 +02:00
Alex Rønne Petersen
72e0d1cd27
std.debug.cpu_context: signal context conversion for sparc*-netbsd/openbsd 2026-10-11 15:00:48 +02:00
Alex Rønne Petersen
9e9047b7e8
std.debug.cpu_context.X86_16: fix register number for ip to match eip on X86 2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
19de614e9d
std.debug.cpu_context.Ve: simplify getting ic in current assembly
Turns out the architecture has an instruction that just... gives you the ic.
Revolutionary!
2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
f376cbc37b
std.debug.cpu_context.Sparc: align to 8 bytes to make std on 32-bit safe
Otherwise we might get SIGBUS due to misalignment.
2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
4a9c642b64
std.debug.cpu_context.Lanai: fix typos 2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
225deb64e9
std.debug.cpu_context.Kvx: fix pc/ra being swapped vs current assembly 2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
d9e11f4f31
std.debug.cpu_context.Arm: make current assembly work in Thumb-1 2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
39f0cf90e5
std.debug.cpu_context.Alpha: fix current assembly not saving r31
It's always zero, but we shouldn't leave that element of the array undefined.
2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
0d947ae87a
std.debug: use a different register number for pc on sparc
Also report the floating-point registers as unsupported rather than invalid.
2026-08-09 17:53:04 +02:00
Alex Rønne Petersen
3b947932ab
std: avoid pointless delay slots and lr writes in inline assembly
And fill in some delay slots where we can't avoid them.
2026-08-09 16:56:23 +02:00
Jacob Young
865f329e4f cbe: update for compiler_rt abi changes
* `tools/generate_c_size_and_align_checks.zig`: superceded by any cbe
   output artifact
 * `cTypePreferredAlignment`: no uses, if brought back, cbe should be
   updated to verify `__alignof`

Closes #19991
Closes #20881
Closes #35523
2026-07-27 14:35:12 -04:00
Alex Rønne Petersen
cf13ecab29
std.debug.cpu_context: always declare a Gpr type on Native 2026-06-16 12:09:31 +02:00
Alex Rønne Petersen
9cc9779ba0
std.debug.cpu_context: fix compile errors on alpha 2026-05-31 06:17:58 +02:00
Alex Rønne Petersen
a4058856a4
std.debug.cpu_context: fix compile error on arc 2026-05-30 18:45:44 +02:00
Alex Rønne Petersen
5c64311fab
std.debug.cpu_context: add mcontext_t for x86-freebsd 2026-05-27 00:28:00 +02:00
Alex Rønne Petersen
833c6d9792
std.debug.cpu_context: add missing align(16) on x86_64-freebsd mcontext_t 2026-05-27 00:28:00 +02:00
Alex Rønne Petersen
6aa6a07955
std.debug.cpu_context: add mcontext_t for hppa-linux and hppa64-linux 2026-05-27 00:28:00 +02:00
Alex Rønne Petersen
12790e69a7
std.debug.cpu_context: add mcontext_t for hppa-netbsd 2026-05-27 00:27:06 +02:00
Alex Rønne Petersen
7d5e400911
std.debug.cpu_context: sheb-openbsd doesn't exist 2026-05-26 23:30:48 +02:00
Alex Rønne Petersen
6ed30064d3
std.debug.cpu_context: add mcontext_t for riscv32-netbsd and riscv64-netbsd 2026-05-26 15:40:42 +02:00
Alex Rønne Petersen
79e87e5753
std.debug: add m88k-openbsd support
closes https://codeberg.org/ziglang/zig/issues/31817
2026-05-23 11:02:56 +02:00
Alex Rønne Petersen
a439978f05
std.debug.cpu_context: fix alpha-openbsd ucontext_t
These fields need to be wrapped in an mcontext field so the common code in
fromPosixSignalContext() can work with it.
2026-05-23 11:02:38 +02:00
Mai-Lapyst
7534ac7367
Adding more support for DEC Alpha to std
This adds support to std, primarily for linux, to support the Alpha architecture.

closes #30940
closes #30926
2026-05-23 06:15:24 +02:00
Alex Rønne Petersen
3b3833acf7
remove remnants of {m68k,powerpc,sparc64}-haiku support
These ports of Haiku have been nonfunctional/incomplete for years. We do not
generally treat such ports as "real" targets.

1efffd83ff
2026-05-21 06:38:51 +02:00
Alex Rønne Petersen
eeb62ef346
std.debug.cpu_context: add loongarch32-linux support 2026-03-19 19:53:03 +01:00
Alex Rønne Petersen
9ab7eec23e represent Mac Catalyst as aarch64-maccatalyst-none rather than aarch64-ios-macabi
Apple's own headers and tbd files prefer to think of Mac Catalyst as a distinct
OS target. Earlier, when DriverKit support was added to LLVM, it was represented
a distinct OS. So why Apple decided to only represent Mac Catalyst as an ABI in
the target triple is beyond me. But this isn't the first time they've ignored
established target triple norms (see: armv7k and aarch64_32) and it probably
won't be the last.

While doing this, I also audited all Darwin OS prongs throughout the codebase
and made sure they cover all the tags.
2025-11-14 11:33:35 +01:00
Matthew Lugg
92bc619c49 std.debug: allow fp unwind from context
It's easy to do FP unwinding from a CPU context: you just report the
captured ip/pc value first, and then unwind from the captured fp value.
All this really needed was a couple of new functions on the
`std.debug.cpu_context` implementations so that we don't need to rely on
`std.debug.Dwarf` to access the captured registers.

Resolves: #25576
2025-11-12 21:02:38 +00:00
Alex Rønne Petersen
94b6c7ce5f
std.debug.cpu_context: add kvx context implementation 2025-11-10 09:40:44 +01:00
Alex Rønne Petersen
0ca4df540f
std.debug.cpu_context: fix signal_ucontext_t for arm-linux 2025-10-30 20:28:31 +01:00
GasInfinity
ef4f6e6c05
feat: add x86_16 debug cpu_context 2025-10-29 14:35:33 +01:00
Alex Rønne Petersen
dba1bf9353 remove all Oracle Solaris support
There is no straightforward way for the Zig team to access the Solaris system
headers; to do this, one has to create an Oracle account, accept their EULA to
download the installer ISO, and finally install it on a machine or VM. We do not
have to jump through hoops like this for any other OS that we support, and no
one on the team has expressed willingness to do it.

As a result, we cannot audit any Solaris contributions to std.c or other
similarly sensitive parts of the standard library. The best we would be able to
do is assume that Solaris and illumos are 100% compatible with no way to verify
that assumption. But at that point, the solaris and illumos OS tags would be
functionally identical anyway.

For Solaris especially, any contributions that involve APIs introduced after the
OS was made closed-source would also be inherently more risky than equivalent
contributions for other proprietary OSs due to the case of Google LLC v. Oracle
America, Inc., wherein Oracle clearly demonstrated its willingness to pursue
legal action against entities that merely copy API declarations.

Finally, Oracle laid off most of the Solaris team in 2017; the OS has been in
maintenance mode since, presumably to be retired completely sometime in the 2030s.

For these reasons, this commit removes all Oracle Solaris support.

Anyone who still wishes to use Zig on Solaris can try their luck by simply using
illumos instead of solaris in target triples - chances are it'll work. But there
will be no effort from the Zig team to support this use case; we recommend that
people move to illumos instead.
2025-10-27 07:35:38 -07:00
Alex Rønne Petersen
c9e0df97f9 std.debug.cpu_context: fix mcontext alignment for x86_64-illumos
It contains a upad128_t array which increases its alignment.
2025-10-26 08:48:21 +01:00
Stephen Gregoratto
f4ef7e8761
std.debug.cpu_context: add missing signal_ucontext_t fields for x86_64-solaris/illumos 2025-10-25 12:44:17 +02:00
Alex Rønne Petersen
d99cf5061c
std.debug.cpu_context: add signal_ucontext_t for alpha, hppa, microblaze, sh 2025-10-23 19:34:02 +02:00
Alex Rønne Petersen
3e2daa509a
std.Target: add arceb and xtensaeb Cpu.Arch tags 2025-10-23 09:27:17 +02:00
Alex Rønne Petersen
9fd7f38600
std.debug.cpu_context.Sparc: fix bad use of call delay slot 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
727942bc03
std.debug.cpu_context: let the compiler deal with clobbers
Otherwise we might be restoring registers we don't even need to.
2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
1f15e265fe
std.debug.cpu_context: sort context decls according to switch prongs (NFC) 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
4c81a496e7
std.debug: add CPU context and DWARF mappings for arc 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
ba9ab3fb67
std.debug: add CPU context and DWARF mappings for m68k 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
eb36a45ed9
std.debug: add CPU context and DWARF mappings for or1k 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
de3947608c
std.debug: add CPU context and DWARF mappings for csky 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
81fe640dd2
std.debug: add CPU context and DWARF mappings for lanai 2025-10-18 00:36:52 +02:00
Alex Rønne Petersen
0b55393a2f
std.debug: add CPU context and DWARF mappings for ve 2025-10-18 00:36:52 +02:00