tags:

views:

51

answers:

3

Is there some way using binutils tools to get this? For example:

// 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 analyze a.out to get sizeof(MyInt)? GDB can do it, but I don't want to use GDB because it's really slow for large binaries.

A: 

I'd say you're better off sticking with gdb for this.

bintuls deals mostly with the executable format (ELF) - the debugging stuff stuff is kept in the dwarf format. Other than running objdump -g -x yourbinary I've not seen many utilities dealing with the debugging symbols.

Altogether, parsing through elf and dwarf to pull out the types is pretty scary and non-trivial - though exactly what a debugger already has done.

nos
A: 

You will need to do what GDB is doing and read the DWARF debug info for yourself. There are a couple of tools that can help with this like readelf and dwarfdump. Go read the FAQ and other goodies at http://dwarfstd.org/ and see if that will do what you need.

This answer is assuming that you are running on a vanilla Linux platform. There are other tools for other platforms that may also work for you, but I am not the guy to ask about those.

Ukko
A: 

readelf is capable of listing dwarf debug information:

readelf -wi

Find the DW_TAG_variable element that har DW_AT_name equal to MyInt and is immediate child of the DW_TAG_compile_unit representing this compile unit.

Use the DW_AT_type attribute to look up the DW_TAG_base_type of this variable. THe DW_AT_type attribute is the offset from the start of this CU where information about this type is stored. These offsets are listed on the left-hand side in the output (in <>s). Look at the DW_AT_byte_size attribut of this DIE. This is the size of the type in bytes.

Torleif
objdump -W also lists DWARF debug information
Torleif