How to parse DW_OP_call_frame_cfa?

2

I am trying to get the variables location from a C (x86_64) code using libdwarf, but GCC even with optimizations off (-O0) generates the frame base relative from DW_OP_call_frame_cfa. In turn, clang uses DW_OP_reg6, which points to rbp in x86_64 ABI.

I have read the DWARF-4 Standard but I can not figure out how to get the actual address, relative from the stack or base pointer. I also encountered a similar question What do I do with DW_OP_call_frame_cfa here, but without success.

A simple code like:

int main()
{
    int va = 1;
    int vb = 2;
    va = va + vb;
    return (va);
}

will produce the following output in dwarfdump:

< 1><0x0000002d>    DW_TAG_subprogram
                      DW_AT_external              yes(1)
                      DW_AT_name                  main
                      DW_AT_decl_file             0x00000001 file.c
                      DW_AT_decl_line             0x00000001
                      DW_AT_type                  <0x00000069>
                      DW_AT_low_pc                0x00400620
                      DW_AT_high_pc               <offset-from-lowpc>29
                      DW_AT_frame_base            len 0x0001: 9c: DW_OP_call_frame_cfa
                      DW_AT_GNU_all_call_sites    yes(1)
                      DW_AT_sibling               <0x00000069>
< 2><0x0000004e>      DW_TAG_variable
                        DW_AT_name                  va
                        DW_AT_decl_file             0x00000001 file.c
                        DW_AT_decl_line             0x00000003
                        DW_AT_type                  <0x00000069>
                        DW_AT_location              len 0x0002: 916c: DW_OP_fbreg -20
< 2><0x0000005b>      DW_TAG_variable
                        DW_AT_name                  vb
                        DW_AT_decl_file             0x00000001 file.c
                        DW_AT_decl_line             0x00000004
                        DW_AT_type                  <0x00000069>
                        DW_AT_location              len 0x0002: 9168: DW_OP_fbreg -24

and the following information in readelf -wf file

00000070 000000000000001c 00000044 FDE cie=00000030 pc=0000000000400620..000000000040063d
  DW_CFA_advance_loc: 1 to 0000000000400621
  DW_CFA_def_cfa_offset: 16
  DW_CFA_offset: r6 (rbp) at cfa-16
  DW_CFA_advance_loc: 3 to 0000000000400624
  DW_CFA_def_cfa_register: r6 (rbp)
  DW_CFA_advance_loc: 24 to 000000000040063c
  DW_CFA_def_cfa: r7 (rsp) ofs 8
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop

from what I read, I have to implement a minimal stack machine, but I do not even know how to parse this manually.

Is there a way to force GCC to not use the DW_OP_call_frame_cfa and use a simple register or do I really need to interpret the frame information? and if so, how?

c
gcc
clang
dwarf
asked on Stack Overflow May 19, 2019 by DavidsonDFGL

1 Answer

2

I am not really familiar with DWARF or debug info formats in general, but to aswer just the

Is there a way to force GCC to not use the DW_OP_call_frame_cfa

part, I've found out from grepping for DW_OP_call_frame_cfa in GCC sources that the -gdwarf-2 gcc switch makes it produce this instead:

DW_AT_frame_base            <loclist at offset 0x00000000 with 4 entries follows>
  [ 0]< offset pair low-off : 0x00000000 addr  0x00001119 high-off  0x00000001 addr 0x0000111a>DW_OP_breg7+8
  [ 1]< offset pair low-off : 0x00000001 addr  0x0000111a high-off  0x00000004 addr 0x0000111d>DW_OP_breg7+16
  [ 2]< offset pair low-off : 0x00000004 addr  0x0000111d high-off  0x0000001c addr 0x00001135>DW_OP_breg6+16
  [ 3]< offset pair low-off : 0x0000001c addr  0x00001135 high-off  0x0000001d addr 0x00001136>DW_OP_breg7+8

Not sure whether switching to version 2 of DWARF suits you though.

answered on Stack Overflow May 21, 2019 by Vladislav Ivanishin

User contributions licensed under CC BY-SA 3.0