Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementation of .cfi_remember_state

I was wondering how exactly .cfi_remember_state is implemented. I know it is a pseudo-op, so I suppose it is converted into a couple of instructions when assembling. I am interested what exact instructions are used to implement it. I tried many ways to figure it out. Namely:

  • Read GAS source code. But failed to find anything useful enough.
  • Read GAS documentation. But the .cfi_remember_state entry is just a simple joke (literally).
  • Tried to find a gcc switch that would make gcc generate asm from C code with pseudo-ops "expanded". Failed to find such a switch for x86 / x86-64. (Would be nice if someone could point me to such a switch, assuming it exists, BTW.)
  • Google-fu && searching on SO did not yield anything useful.

The only other solution in my mind would be to read the binary of an assembled executable file and try to deduce the instructions. Yet I would like to avoid such a daunting task. Could any of You, who knows, enlighten me, how exactly it is implemented on x86 and/or x86-64? Maybe along with sharing how / where that information was acquired, so I could check other pseudo-ops, if I ever have the need to?

like image 346
librin.so.1 Avatar asked Oct 21 '22 18:10

librin.so.1


1 Answers

This directive is a part of DWARF information (really all it does is emit DW_CFA_remember_state directive). Excerpt from DWARF3 standard:

The DW_CFA_remember_state instruction takes no operands. The required action is to push the set of rules for every register onto an implicit stack.

You may play with DWARF information using objdump. Lets begin with simple void assembler file:

  .text
.globl main
  .type main, @function
main:
.LFB0:
.cfi_startproc
#.cfi_remember_state
.cfi_endproc
.LFE0:
  .size main, .-main

Compile it with gcc cfirem.s -c -o cfirem.o

Now disassemble generated DWARF section with objdump --dwarf cfirem.o You will get:

00000018 00000014 0000001c FDE cie=00000000 pc=00000000..00000000
  DW_CFA_nop
  DW_CFA_nop
  ...

If you will uncomment .cfi_remember_state, you will see instead:

00000018 00000014 0000001c FDE cie=00000000 pc=00000000..00000000
  DW_CFA_remember_state
  DW_CFA_nop
  DW_CFA_nop
  ...

So it is not really converting in assembler instructions (try objdump -d to see that there are no assembler instructions in our sample at all). It is converted in DWARF pseudo-instructions, that are used when debugger like GDB processes your variable locations, stack information and so on.

like image 169
Konstantin Vladimirov Avatar answered Oct 27 '22 16:10

Konstantin Vladimirov