2

I am having a bit of trouble understanding how stack frames work in ARM. It is my current understanding that a stack frame is region of memory between the Stack Pointer and the Frame Pointer, and that this is the space programs store information in. However, a program I am debugging has both the stack pointer and frame pointer, pointing to the same memory address.

Does anyone know if this is a valid state? I haven't been able to find any documentation on this condition, I'm wondering if anyone else would know how the computer handles this kind of condition.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Zoey
  • 135
  • 1
  • 1
  • 7
  • 4
    The computer doesn't know what a base pointer is. It's just a convention programmers use. – fuz Feb 10 '21 at 20:19
  • 1
    You might be thinking of conventions from other systems. On x86-64, for instance, a typical stack frame setup looks like `push rbp ; mov rbp, rsp ; sub rsp, NNN` to create a stack frame with NNN bytes for local variables. In that case the frame pointer `rbp` points to the top of the local variable area, and `rsp` to the bottom. But on ARM64, it is more efficient to do something like `stp x29, x30, [sp, #-(NNN+16)]! ; mov x29, sp`, which leaves the frame pointer `x29` pointing to the bottom of the locals along with `sp`. [...] – Nate Eldredge Feb 10 '21 at 21:38
  • 3
    There isn't anything wrong with that, and the computer doesn't have to "handle" it in any particular way; the compiler just knows what points where and generates code accordingly. I think your definition of stack frame as " region of memory between the Stack Pointer and the Frame Pointer" is simply too narrow and not applicable on this architecture. I'd just define stack frame as "region of memory where saved registers and local variables are kept"; how the machine keeps track of it is an implementation detail, and not a defining characteristic. – Nate Eldredge Feb 10 '21 at 21:40
  • Certainly you can have functions where the frame pointer and stack pointer are the same value. Fairly easy to create one. – old_timer Feb 11 '21 at 04:10
  • [Here](https://godbolt.org/z/97bGKP) is an example, if you like. – Nate Eldredge Feb 11 '21 at 04:53

1 Answers1

2

I think you want to look on Procedure Call Standard for the Arm 64-bit Architecture

In section 6.2.3 there is

Conforming code shall construct a linked list of stack-frames. Each frame shall link to the frame of its caller by means of a frame record of two 64-bit values on the stack (independent of the data model). The frame record for the innermost frame (belonging to the most recent routine invocation) shall be pointed to by the Frame Pointer register (FP). The lowest addressed double-word shall point to the previous frame record and the highest addressed double-word shall contain the value passed in LR on entry to the current function.

So if function uses stack to pass arguments to a callee function or makes 'dynamic allocations', SP would not be same as FP. Otherwise addresses in SP and FP are same.

PS: this document is very stingy on details, imho. I'd appreciate being corrected if I got this specification wrong

Frame Pointer Scheme

user3124812
  • 1,861
  • 3
  • 18
  • 39
  • Local variables and preserved registers are *above* FP. If the function doesn't do any dynamic allocation (VLAs or `alloca`), nor call any other functions that need args passed on the stack, then there will be nothing between FP and SP in the diagrams and they will be equal. – Nate Eldredge Feb 11 '21 at 04:50