开发者

Given an ELF binary built from C++ with gcc, how can I determine the sizeof some type in it?

开发者 https://www.devze.com 2022-12-27 09:08 出处:网络
Is there some way using binutils tools to get this? For example: // x.cc typedef long long MyInt; int main(int argc, char* argv[]) {

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 bla开发者_JAVA百科h
}

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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号