44

I need a way to analyze output file of my GCC compiler for ARM. I am compiling for bare metal and I am quite concerned with size. I can use arm-none-eabi-objdump provided by the cross-compiler but parsing the output is not something I would be eager to do if there exists a tool for this task. Do you know of such a tool existing? My search turned out no results.

One more thing, every function in my own code is in its own section.

RushPL
  • 4,732
  • 1
  • 34
  • 44

4 Answers4

92

You can use nm and size to get the size of functions and ELF sections.

To get the size of the functions (and objects with static storage duration):

$ nm --print-size --size-sort --radix=d tst.o

The second column shows the size in decimal of function and objects.

To get the size of the sections:

$ size -A -d tst.o

The second column shows the size in decimal of the sections.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • `nm`, isn't that Windows? If so, sorry - I did not say I am using Linux. – RushPL Jul 30 '12 at 19:50
  • 6
    Windows? No, `nm` and `size` are GNU tools part and are a part of the `binutils`. Add the required platform prefix to run the tools in a cross platform (e.g., type `arm-none-eabi-nm` and `arm-none-eabi-size`), – ouah Jul 30 '12 at 21:28
  • Interestingly, this also has the size-effect of removing ARM's [Mapping Symbols](https://sourceware.org/binutils/docs-2.20/as/ARM-Mapping-Symbols.html). These symbols are printed without a size field (i.e. the field is missing, not printed as zero), and they are removed when `nm` is used with `--size-sort`. – sherrellbc Sep 21 '17 at 17:56
11

The readelf utility is handy for displaying a variety of section information, including section sizes, e.g.:

arm-none-eabi-readelf -e foo.o

If you're interested in the run-time memory footprint, you can ignore the sections that do not have the 'A' (allocate) flag set.

Lance Richardson
  • 4,610
  • 23
  • 30
  • `arm-none-eabi-readelf -s build/program.out|perl -ne 'if(/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { print $3 . " " . $8. "\n";}'|sort -n` – RushPL Jul 30 '12 at 19:48
  • Thank you, the above did the trick for me! I did not know about the readelf. – RushPL Jul 30 '12 at 19:49
4

When re-visiting this question 10 years later one must mention the little Python-based wrapper for readelf and nm that is elf-size-analyze: screenshot

stefanct
  • 2,503
  • 1
  • 28
  • 32
0

puncover uses objdump and a few other gcc tools to generate html pages you can easily browse to figure out where your code and data space is going.

It's a much nicer frontend than the text output of the gcc tools.

sarfata
  • 4,625
  • 4
  • 28
  • 36