Given a binary ELF built from C ++ with gcc, how can I figure out the size of some type in it?

Is there a way to use binutils tools for this? For instance:

// x.cc
typedef long long MyInt;
int main(int argc, char* argv[]) {
  // blah blah blah
}

      

Then:

g++ -g x.cc -o a.out

      

How can I parse a.out to get sizeof (MyInt)? GDB can do this, but I don't want to use GDB because it is very slow for large binaries.

+2


a source to share


3 answers


readelf is capable of displaying dwarf debugging information:

readelf -wi

      



Find the DW_TAG_variable element, which has the name DW_AT_ equal to MyInt and is an immediate child of DW_TAG_compile_unit that represents this compiler.

Use the DW_AT_type attribute to find the DW_TAG_base_type of this variable. The DW_AT_type attribute is the offset from the beginning of this CU where information about this type is stored. These offsets are listed on the left in the output (in <> s). Have a look at the DW_AT_byte_size attribute of this DIE. This is the size of the type in bytes.

+1


a source


I would say that you are better off sticking with gdb for this.

bintuls mainly works with Executable Format (ELF) - debug stuff is stored in dwarf format . Other than launching, objdump -g -x yourbinary

I have not seen many utilities related to debug symbols.



All in all, parsing elves and dwarfs for pulling out types is pretty daunting and non-trivial - albeit exactly what the debugger has already done.

0


a source


You will need to do what GDB does and read the DWARF debugging information for yourself. There are several tools that can help with this, like readelf and dwarfdump. Go read the FAQ and other goodies at http://dwarfstd.org/ and see if this does what you want.

This answer assumes you are on vanilla Linux platform. There are other tools for other platforms that might work for you too, but I'm not the guy you are asking about.

0


a source







All Articles