Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to list the size of static declarations in object file?

Tags:

c

gcc

mingw

I'm trying to reduce the size of an executable (using MINGW/GCC) by finding all large statically declared arrays. Is there a way to dump the list of data symbols along with their aggregate sizes?? I've looked at man pages for nm and objdump but dont see anything useful so far.

Thx.

like image 524
Saideira Avatar asked Dec 13 '22 11:12

Saideira


2 Answers

Use the nm utility from GNU binutils with the --print-size option (abbreviated -S). For example, to find the 20 largest symbols in an object file, do this:

nm --print-size --size-sort objfile.o | tail -20
like image 141
Adam Rosenfield Avatar answered Mar 17 '23 14:03

Adam Rosenfield


The linker can generate a MAP file which will provide that information.

If you are using gcc to compile and link, you pass linker options to the linker with:

-Wl,<comma-separate-options-list>

If calling ld directly, you obviously just pass the linker options.

The linker option you need is:

-Map=<mapfilename>

You may also use --cref to include a cross-reference table in the map file.

like image 41
Clifford Avatar answered Mar 17 '23 13:03

Clifford