GCC Linker : How To Generate A Report Of Per File Contribution On Output Sections

Recently I meet a problem when trying to link my program. It report .text can't fit in specified memory region. Obviously the source code grows too large to be linked in limited memory region.

What I want to do now is to analyze which file contribute most significantly to the ".text" section so that follow up code optimization can be performed. I tried many ways but don't make it.

  • nm -s output.elf gives size of each symbol, but don't group the symbol under each source file.
  • nm -s file.obj go through every object file don't work because -fdata-section-ffunction-section-Wl are specified, so not all content in an object file will get linked to final output
  • readelf -s output.elf gives information from file to file, but it simply list symbols under each file and their size. A script can be written to sum all the size under a file, but the total value seems wrong, an obvious error here is some symbols may point to the same memory location, so the same memory region may be calculated many times.

When gcc link do its work, it should know all the details of what is extracted from a object file and put to the output section, but seems it don't provide a switch to generate a detail report (Or am I missing something?)

Is there any tool which can do this job?



Perhaps --gc-sections together with --print-gc-sections and/or --print-map-discarded?

If everything is in separate sections, then you have all your sections as input, and list of discarded sections. Then simple script shall produce list of used sections, their size and file mapping.