Get address of member of struct using pyelftools

1k views Asked by At

I am using the pyelftools to read an elf file. How can I get an offset value or address of a member in a struct? For example, say I have the following struct in C.

typedef struct 
{
    int valA;
} TsA;

typedef struct
{
    int valB;
} TsB;

typedef struct 
{
    int valC;
    TsB b;
} TsC;

typedef struct
{
    TsA a;
    TsC c;
} TsStruct;

TsStrcut myStruct;

How can I get an address of myStruct.c.b.valB? I found a similar question here but did not find any good answer.

1

There are 1 answers

0
Seva Alekseyev On

Find the DIE for the structure, the one with tag DW_TAG_structure_type and DW_AT_name equal to structure names.

Enumerate the DW_TAG_member subdies under it. While there, look at the DW_AT_member_location, it's the offset of the corresponding structure element.

It might help if you take a look at the DIE structure visually first. DWARF Explorer might help (disclaimer: I wrote it).