Why can't I store data on the stack properly?

50 Views Asked by At

I'm writing a compiler, and it emitted the following (Intel-syntax) assembly code for Linux (x86-64):

lea r13, _s1
mov qword ptr [rbp + -2*8], r13
mov r10, qword ptr [rbp + -2*8]
lea r13, qword ptr [r10 + 8]

If I'm reading this correctly, this code is supposed to load the address of label _s1 into r13, store it on the stack, read it from the stack into r10, add 8, and store the reuslt in r13. That matches the expected behaviour of my program, and it seems to work as expected (program doesn't crash here) when I am not debugging the program.

However, when I try to debug the program using VSCode with CodeLLDB, as I step through the program, I see behaviour that I can't understand. This is what seems to happen according to CodeLLDB:

  • lea r13, [0x425290]: 0x425290 is loaded into r13
  • mov qword ptr [rbp - 0x10], r13: 0x425290 is written to address 0x7fffffffe1f0; I confirmed this by running memory read -s1 $rbp-0x10 in LLDB
  • mov r10, qword ptr [rbp - 0x10]: 0xffffe6f400000000 is read into r10. Why?
  • lea r13, [r10 + 0x8]: 0xffffe6f400000008 is loaded into r13

Why is the value that I read back from the stack different from the value that I wrote into it?

Edit: It seems like this weird behaviour doesn't happen when I use VSCode's built-in GDB debugger. Am I misusing LLDB?

0

There are 0 best solutions below