Skip to content

Instantly share code, notes, and snippets.

@antiguru
Last active November 18, 2021 14:52
Show Gist options
  • Save antiguru/32e482a0fc052d464280e16c5b49b60e to your computer and use it in GitHub Desktop.
Save antiguru/32e482a0fc052d464280e16c5b49b60e to your computer and use it in GitHub Desktop.

Size of stack frames

This determines the size of stack frames in a binary by inspecting the sub %rsp instructions in a very crude way. It might be incorrect on account of not all functions using the stack the same way or consistently.

objdump -d target/release/materialized \
  | grep 'sub.*rsp$\|>:' \
  | sed -n '$!N;/sub/P;D' \
  | grep "sub " -1 \
  | paste -d " "  - - \
  | awk '{print $NF " " $2}' \
  | grep "^\$0x" \
  | cut -c 4- \
  | sed 's/,/ /' \
  | perl -ne'print hex $_; print " "; print $_' \
  | sort -n 

Let's go over its part to figure out what it does:

  1. objdump -d target/release/materialized Disassemble all symbols in the .text section.
  2. grep 'sub.*rsp$\|>:' Find all lines that contain sub.*rsp (allocating a stack frame) or >: (indicating a symbol).
  3. sed -n '$!N;/sub/P;D' Not sure
  4. grep "sub " -1 Find all lines containing sub and print it including the preceding line.
  5. paste -d " " - - Concat every two lines into one.
  6. awk '{print $NF " " $2}' Print column 2 and then the whole line
  7. grep "^\$0x" Only print lines that start with a hex code.
  8. cut -c 4- Trim off the hex 0x header.
  9. sed 's/,/ /' Replace , by a space.
  10. perl -ne'print hex $_; print " "; print $_' Convert the hex number to a decimal.
  11. sort -n Sort numerically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment