Search code examples
stackreverse-engineeringida

How does IDA calculate stack variable offsets?


IDA is able to read the machine instructions of a subroutine and display the relative offsets of each variable that gets stored on the stack.

enter image description here

I'm writing a program that analyzes stack memory, and I would like to be able to put the values stored in the stack memory into their respective variable types. What logic is going on under the hood that enables IDA to display the stack variable offsets?

Thanks for your time.


Solution

  • It infers that from the function's code by looking on where and how stack addresses are used. Like, loading into a 4-byte register and doing arithmetics probably mean that stack memory from which the load was performed belongs to a some int variable.

    If you want details on IDA's algorithm, I doubt you can found it. You can look at Avast's Retargetable Decompiler open source project, that performs analysis much like IDA and study its code.