0

I'm trying to identify the largest symbols in an .elf file for each memory section (.text, .data, .bss). So far I'm using GNU nm to get the largest symbols:

nm foo.elf --size-sort --reverse-sort --radix=d --demangle --line-numbers

Is there a builtin way in nm to filter the ouput by section or do I need to resort to text filtering?

nm outputs a section type for every symbol as single letter code (B: .bss, D: .data, T: .text), but there seems no way to filter by symbol type.

Background: The code runs on a microcontroller which is able to execute instruction directly from flash memory. The instructions from the .text section stay in the flash memory during execution, .bss and .data are loaded into the RAM. That's way I would like to be able to identify the largest symbols in each section independently.

S. Marti
  • 313
  • 1
  • 5
  • 13
  • Why would you not use the map file generated by the linker? Using nm is a bit "reverse-engineering" when you would have had the information first hand. – Clifford Nov 13 '19 at 18:59
  • You have "over tagged" this issue. The fact that the target the map refers to is an embedded microcontroller is largely irrelevant, as is elf - it is just a text processing issue that many can answer. An _"easy way"_ is subjective making it way too broad. – Clifford Nov 13 '19 at 19:01
  • @Clifford I removed the embedded and microcontroller tags. – S. Marti Nov 15 '19 at 10:43
  • @Clifford Do you know of any tool that can find the largest symbols sorted by section from a .map file? That was actually what I tried first, but both tools I looked at ([AMAP](http://www.sikorskiy.net/prj/amap/) and [MapViewer](https://github.com/govind-mukundan/MapViewer)) don't offer the possibility to sort by section and size. Of course I could write a script for that purpose, but why build a new tool if I can just use nm? – S. Marti Nov 19 '19 at 17:54
  • Because building a new tool may have taken less time that you have spent on this question already. Paste the content into a spreadsheet and sort it is by advice. https://support.office.com/en-gb/article/split-text-into-different-columns-with-the-convert-text-to-columns-wizard-30b14928-5550-41f5-97ca-7a3e9c363ed7 – Clifford Nov 19 '19 at 18:43

1 Answers1

1

there seems no way to filter by symbol type.

Just use grep to perform any filtering you may need.

You may also want to look at Bloaty McBloatface: a size profiler for binaries.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks, sometimes it's hard to see the solution although good ol' grep is right in front of your nose. I used the following one liner in the end: `nm foo.elf --size-sort --reverse-sort --radix=d --demangle --line-numbers | grep -i " t " | less` `grep -i " t "`will filter for all symbols in the .text section (the filtering could probably be made more elegantly using a regex). – S. Marti Nov 13 '19 at 08:33