ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.07k stars 2.56k forks source link

Implement backtracing for MIPS #6690

Open FireFox317 opened 4 years ago

FireFox317 commented 4 years ago

EDIT: This issue has been turned into a tracking issue to implement backtracing on MIPS. Some useful information is provided by @LemonBoy in the comment down below.

I think I found an upstream bug regarding frame pointers on MIPS.

For -target mips64-freestanding (64bit) the following function prologue is emitted:

daddiu  $sp, $sp, -32
sd      $ra, 24($sp)
sd      $fp, 16($sp)
move    $fp, $sp
move    $1, $4
sb      $4, 8($fp)

To have a working stack trace on MIPS we should be able to also use a fp_offset of 2 * @sizeOf(usize). But that breaks the 32 bit MIPS code. See below. https://github.com/ziglang/zig/blob/3b4432d9a6ae7f01f66e5ddcc15bf579d2c47460/lib/std/debug.zig#L342-L349

For -target mips-freestanding (32bit) the following function prologue is emitted:

addiu   $sp, $sp, -24
sw      $ra, 20($sp)
sw      $fp, 16($sp)
move    $fp, $sp
move    $1, $4
sb      $4, 8($fp)

As can be seen for 32bit mips, we also need a offset of 16 to get the previous frame pointer. I would guess that an offset of 8 would be fine too, but that is not what is emitted.

@LemonBoy Is this an upstream bug, or is this required on mips32 because of alignment or some other reason?

LemonBoy commented 4 years ago

I think I found an upstream bug regarding frame pointers on MIPS.

Welcome to the MIPS world, where somebody had the brilliant idea to make the frame pointer point to the bottom of the stack frame. The only reason why I never implemented this for MIPS is that it becomes quite hairy and couldn't be arsed to do so.

If you want to know why the stack frames are oversized check out this small introduction to the o32 (one of the three or four) ABI.

FireFox317 commented 4 years ago

Thanks for the information @LemonBoy! So it's not a bug.