3

I am trying to print the DW_AT_data_member_location attribute value from the dwarf info to get the member variables offset from a struct name but no helper function seems to work out.

Here is the dwarf_info from objdump:

<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)

I am able to get the attribute code but not attribute value:

 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              

What is the type of this variable ? How to get its Value? Which helper function would work here ?

Udit Gupta
  • 3,162
  • 11
  • 43
  • 71
  • I hope the code-snippet you show is just pseudo-code, because otherwise the comparison of a variable with a literal string is highly suspect. In fact, if that's real code the condition will *never* be true. Also, if that's real code the `printf` call above is suspect as well. – Some programmer dude Jul 30 '14 at 21:35

2 Answers2

4

The DWARF debugging format allows the offset of a member to be expressed as either a simple constant or as an expression that needs to be evaluated in order calculate the offset. For whatever reason the debugging information you're trying to process has expressed a simple constant offset in the form of an expression. You'll need to "evaluate" the expression to determine what the offset is. Something like this:

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;
    }
}
Udit Gupta
  • 3,162
  • 11
  • 43
  • 71
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • Thanks a lot. I understood it now. I have made some edits to your code. May be you did not run the code so by mistake you assumed some (pointer to struct) as (struct) itself. Thanks a ton again :-) – Udit Gupta Jul 31 '14 at 23:40
  • 1
    I didn't test it at all, as I'd have to write a bunch of other code just to get to where this code could work. Thanks for updating the answer with the correct code. – Ross Ridge Aug 01 '14 at 00:04
  • Can you also comment on how can I get the location(address) of a variable ? – Udit Gupta Aug 13 '14 at 04:44
  • Code sample has a memory leak in last branch. The following deallocation will free the memory allocated by libdwarf: dwarf_dealloc (debug, locdescs[0]->ld_s, DW_DLA_LOC_BLOCK); dwarf_dealloc (debug, locdescs[0], DW_DLA_LOCDESC); dwarf_dealloc (debug, locdescs, DW_DLA_LIST); – Herman Narkaytis Sep 12 '20 at 05:22
  • Or replace last branch with: if (form == DW_FORM_block1) { Dwarf_Block * block; int rv = dwarf_formblock (dw_attribute, &block, NULL); assert (rv == DW_DLV_OK); if (DW_OP_plus_uconst == *(uint8_t*)block->bl_data) { uint8_t * leb128 = block->bl_data + sizeof (uint8_t); int i, shift = 0; offset = 0; for (i = 1; i < block->bl_len; ++i) { offset |= (*leb128++ & ((1 << 7) - 1)) << shift; shift += 7; } } } – Herman Narkaytis Sep 12 '20 at 05:25
1

If you prefer using JVM, I wrote a simple library for parsing dwarf which creates an EMF based model. Below is the sample code to print struct member locations:

def someFunction() {
  var elf = new Elf32Context(buffer)
  var dwarf = new Dwarf32Context(elf)
  var model = DwarfModelFactory::createModel(dwarf)
  model.eAllContents.filter(StructureType).forEach[it.dumpStruct]
}


def dumpStruct(CompositeType struct) '''
  «switch(struct) {StructureType:'struct' UnionType:'union' default:'composite?'}» «struct.typedef?.name» {
    «FOR m : struct.members»
        «m.type.dumpType» «m.name»  @«m.dataMemberLocation»
    «ENDFOR»
  }
'''
Aykut Kllic
  • 898
  • 9
  • 14