Arm64汇编:帧指针管理异常——递归斐波那契GDB回溯失效
递归斐波那契函数GDB回溯异常问题
以练习为目的实现了递归斐波那契函数,程序功能完全正常,但存在一个问题:使用GDB单步调试该函数时,backtrace(简称bt)命令无法正常工作。
源码
fib.s
# fib.s .data .text globl fib fib: stp x29, x30, [sp, #-16]! mov x29, sp # Recursive compute fib(n). Even though this is a really dumb way to do Fibonacci, # this exercise is practically mandatory # x0: uint64_t -> Result in x0 mov x1, #1 cmp x0, x1 ble root # Gotta do some recursive shenanigans stp x19, x20, [sp, #-16]! sub x0, x0, 1 mov x19, x0 bl fib mov x20, x0 mov x0, x19 sub x0, x0, 1 bl fib add x0, x0, x20 ldp x19, x20, [sp], #16 root: ldp x29, x30, [sp], #16 ret
main.c
# main.c #include <stdio.h> #include <stdlib.h> #include <stdint.h> extern uint64_t fib(uint64_t); int main() { uint64_t i; printf("Enter a number please: "); if (scanf("%lu", &i) == EOF) { printf("HOLY MOLY That's not a number"); exit(1); } printf("Fib(%lu) = %lu\n", i, fib(i)); exit(0); }
编译步骤
mkdir -p build gcc -g -Wall -Wextra -c main.c -o build/ul5_1.o as -g fib.s -o ul5_2.o ld -lc --entry main build/ul5* -o build/l5.o
调试异常现象
使用gdbtui启动调试,程序请求输入时输入4,会发现bt命令在执行到第二个stp指令前能正常工作,之后则会出现如下错误:
Backtrace stopped: Previous frame identical to this frame (corrupt stack?)
我认为fib函数符合AAPCS64规范,且栈管理正确,请问我遗漏了什么或哪里出错了?
内容的提问来源于stack exchange,提问作者pipsqueaker117
相关产品推荐
相关产品推荐

