c - 如何从矮人信息中获取结构成员偏移量?

标签 c struct offset objdump dwarf

我正在尝试从 dwarf 信息中打印 DW_AT_data_member_location 属性值,以从结构名称中获取成员变量的偏移量,但似乎没有辅助函数可以解决。

这是来自 objdump 的 dwarf_info:

<1><1bf>: Abbrev Number: 5 (DW_TAG_structure_type)
<1c0>   DW_AT_name        : (indirect string, offset: 0xf0): class  
<1c4>   DW_AT_byte_size   : 208 
<1c5>   DW_AT_decl_file   : 1   
<1c6>   DW_AT_decl_line   : 10  
<1c7>   DW_AT_sibling     : <0x1f8> 
<2><1cb>: Abbrev Number: 6 (DW_TAG_member)
<1cc>   DW_AT_name        : (indirect string, offset: 0xc7): schools    
<1d0>   DW_AT_decl_file   : 1   
<1d1>   DW_AT_decl_line   : 11  
<1d2>   DW_AT_type        : <0x1f8> 
<1d6>   DW_AT_data_member_location: 2 byte block: 23 0  (DW_OP_plus_uconst: 0)
<2><1d9>: Abbrev Number: 6 (DW_TAG_member)
<1da>   DW_AT_name        : (indirect string, offset: 0xd8): size   
<1de>   DW_AT_decl_file   : 1   
<1df>   DW_AT_decl_line   : 12  
<1e0>   DW_AT_type        : <0x159> 
<1e4>   DW_AT_data_member_location: 3 byte block: 23 c8 1   (DW_OP_plus_uconst: 200)
<2><1e8>: Abbrev Number: 6 (DW_TAG_member)
<1e9>   DW_AT_name        : (indirect string, offset: 0xf6): record 
<1ed>   DW_AT_decl_file   : 1   
<1ee>   DW_AT_decl_line   : 13  
<1ef>   DW_AT_type        : <0x1b8> 
<1f3>   DW_AT_data_member_location: 3 byte block: 23 cc 1   (DW_OP_plus_uconst: 204)

我能够获取属性代码但不能获取属性值:

 if(dwarf_whatattr(attrs[i],&attrcode,&error) != DW_DLV_OK)
                      printf("Error");

 printf("Attrcode: %d\n",attrcode); // This one works

 if(attrcode==DW_AT_data_member_location) 
           dwarf_formudata(attrs[i],&offset,0) // This one does not work              

这个变量的类型是什么? 如何获得它的值(value)? 哪个辅助函数可以在这里工作?

最佳答案

DWARF 调试格式允许将成员的偏移量表示为简单常量或需要计算偏移量的表达式。无论出于何种原因,您尝试处理的调试信息都以表达式的形式表达了一个简单的常量偏移量。您需要“评估”表达式以确定偏移量是多少。像这样:

if (attrcode == DW_AT_data_member_location) {
    Dwarf_Half form;
    dwarf_whatform(attrs[i], &form, &error);
    if (form == DW_FORM_data1 || form == DW_FORM_data2
        form == DW_FORM_data2 || form == DW_FORM_data4
        form == DW_FORM_data8 || form == DW_FORM_udata) {
        dwarf_formudata(attrs[i], &offset, 0);
    } else if (form == DW_FORM_sdata) {
        Dwarf_Signed soffset;
        dwarf_formsdata(attrs[i], &soffset, 0);
        if (soffset < 0) {
             printf("unsupported negative offset\n");
             /* FAIL */
        }
        offset = (Dwarf_Unsigned) soffset;
    } else {
        Dwarf_Locdesc **locdescs;
        Dwarf_Signed len;
        if (dwarf_loclist_n(attrs[i], &locdescs, &len,  &error) == DW_DLV_ERROR) {
             printf("unsupported member offset\n");
             /* FAIL */
        }
        if (len != 1
            || locdescs[0]->ld_cents != 1
            || (locdescs[0]->ld_s[0]).lr_atom != DW_OP_plus_uconst) {
             printf("unsupported location expression\n");
             /* FAIL */
        }
        offset = (locdescs[0]->ld_s[0]).lr_number;
    }
}

关于c - 如何从矮人信息中获取结构成员偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047329/

相关文章:

c - 为什么这个结构定义会额外增加一个字节的内存使用量?

c - 此代码中的最后一个 printf 如何工作?

c - 将存储在 char 数组中的 char 显示为 int 值

C:结构语法链表

c - C中结构体的动态内存分配

sql - 如何计算从现在开始的 'x'工作时间? (首选 sql 或 postgresql 9.3)

php - 在 PHP MySQL LIMIT 中使用 $variable

c - 我应该使用 GTK 线程/关于 GTK 线程的好教程吗?

c - 为什么我的 qsort 结果不正确?

css - 位置固定且top>0时的隐藏内容